*/ const DEFAULTS = array( 'tag_post_types' => 'all', 'tag_exclude_logged_in' => false, 'tag_exclude_admins' => false, 'tag_exclude_urls' => '', 'tag_consent_mode' => 'off', 'show_dashboard_widget' => true, 'default_date_range' => '30d', 'api_base' => '', 'console_url' => '', 'tag_url' => '', ); /** * The tag's consent modes (AdPriva/tag#138), rendered as data-consent-mode. * * @var array */ const CONSENT_MODES = array( 'off', 'tcf', 'manual' ); /** * Map of override key => the wp-config constant that pins (and overrides) it. * * @var array */ const ENDPOINT_CONSTANTS = array( 'api_base' => 'ADPRIVA_API_BASE', 'console_url' => 'ADPRIVA_CONSOLE_URL', 'tag_url' => 'ADPRIVA_TAG_URL', ); /** * Register hooks (Settings API registration runs on admin_init). * * @return void */ public function hooks() { add_action( 'admin_init', array( __CLASS__, 'register' ) ); } /** * Register the setting, its sections, and its fields with the Settings API. * * @return void */ public static function register() { register_setting( self::OPTION_GROUP, self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( __CLASS__, 'sanitize' ), 'default' => self::DEFAULTS, ) ); add_settings_section( 'adpriva_tag', __( 'Tag targeting', 'adpriva' ), array( __CLASS__, 'section_tag' ), self::PAGE ); add_settings_field( 'tag_post_types', __( 'Load on post types', 'adpriva' ), array( __CLASS__, 'field_post_types' ), self::PAGE, 'adpriva_tag' ); add_settings_field( 'tag_exclude_logged_in', __( 'Exclude logged-in users', 'adpriva' ), array( __CLASS__, 'field_exclude_logged_in' ), self::PAGE, 'adpriva_tag' ); add_settings_field( 'tag_exclude_admins', __( 'Exclude administrators', 'adpriva' ), array( __CLASS__, 'field_exclude_admins' ), self::PAGE, 'adpriva_tag' ); add_settings_field( 'tag_exclude_urls', __( 'Exclude URLs', 'adpriva' ), array( __CLASS__, 'field_exclude_urls' ), self::PAGE, 'adpriva_tag' ); add_settings_section( 'adpriva_tag_consent', __( 'Visitor consent', 'adpriva' ), array( __CLASS__, 'section_tag_consent' ), self::PAGE ); add_settings_field( 'tag_consent_mode', __( 'Consent mode', 'adpriva' ), array( __CLASS__, 'field_tag_consent_mode' ), self::PAGE, 'adpriva_tag_consent' ); add_settings_section( 'adpriva_dashboard', __( 'Dashboard', 'adpriva' ), array( __CLASS__, 'section_dashboard' ), self::PAGE ); add_settings_field( 'show_dashboard_widget', __( 'Show dashboard widget', 'adpriva' ), array( __CLASS__, 'field_show_widget' ), self::PAGE, 'adpriva_dashboard' ); add_settings_field( 'default_date_range', __( 'Default date range', 'adpriva' ), array( __CLASS__, 'field_default_range' ), self::PAGE, 'adpriva_dashboard' ); add_settings_section( 'adpriva_advanced', __( 'Advanced (endpoints)', 'adpriva' ), array( __CLASS__, 'section_advanced' ), self::PAGE ); add_settings_field( 'api_base', __( 'API base URL', 'adpriva' ), array( __CLASS__, 'field_api_base' ), self::PAGE, 'adpriva_advanced' ); add_settings_field( 'console_url', __( 'Console URL', 'adpriva' ), array( __CLASS__, 'field_console_url' ), self::PAGE, 'adpriva_advanced' ); add_settings_field( 'tag_url', __( 'Tag script URL', 'adpriva' ), array( __CLASS__, 'field_tag_url' ), self::PAGE, 'adpriva_advanced' ); } /** * Sanitize+normalize the submitted settings into the canonical option shape. * Pure: defaults are applied here, so a partial or empty input is always valid. * * @param mixed $input Raw submitted values (expected to be an array). * @return array */ public static function sanitize( $input ): array { $input = is_array( $input ) ? $input : array(); $out = self::DEFAULTS; // tag_post_types: the string 'all' or a list of post-type slugs. if ( isset( $input['tag_post_types'] ) && is_array( $input['tag_post_types'] ) ) { $types = array(); foreach ( $input['tag_post_types'] as $slug ) { $slug = sanitize_key( (string) $slug ); if ( '' !== $slug ) { $types[] = $slug; } } $out['tag_post_types'] = ! empty( $types ) ? $types : 'all'; } else { $out['tag_post_types'] = 'all'; } // Checkboxes: present+truthy in POST => true; absent (unchecked) => false. $out['tag_exclude_logged_in'] = ! empty( $input['tag_exclude_logged_in'] ); $out['tag_exclude_admins'] = ! empty( $input['tag_exclude_admins'] ); $out['show_dashboard_widget'] = ! empty( $input['show_dashboard_widget'] ); // Exclude-URL list: one path per line, trimmed, blanks dropped. $raw = isset( $input['tag_exclude_urls'] ) ? (string) $input['tag_exclude_urls'] : ''; $lines = array(); foreach ( preg_split( '/\r\n|\r|\n/', $raw ) as $line ) { $line = trim( $line ); if ( '' !== $line ) { $lines[] = sanitize_text_field( $line ); } } $out['tag_exclude_urls'] = implode( "\n", $lines ); // Whitelisted enums: an unknown value falls back to the default. $mode = isset( $input['tag_consent_mode'] ) ? (string) $input['tag_consent_mode'] : ''; $out['tag_consent_mode'] = in_array( $mode, self::CONSENT_MODES, true ) ? $mode : 'off'; $range = isset( $input['default_date_range'] ) ? (string) $input['default_date_range'] : ''; $out['default_date_range'] = in_array( $range, array( '7d', '30d', '90d' ), true ) ? $range : '30d'; // Advanced endpoint overrides: a pinned wp-config constant wins, so any // override for an endpoint whose constant is defined is dropped. foreach ( array_keys( self::ENDPOINT_CONSTANTS ) as $key ) { if ( self::endpoint_locked( $key ) ) { $out[ $key ] = ''; continue; } $val = isset( $input[ $key ] ) ? trim( (string) $input[ $key ] ) : ''; $out[ $key ] = '' !== $val ? esc_url_raw( $val ) : ''; } return $out; } /** * The full option merged over defaults. * * @return array */ public static function all() { $stored = get_option( self::OPTION, array() ); if ( ! is_array( $stored ) ) { $stored = array(); } return array_merge( self::DEFAULTS, $stored ); } /** * Single setting value (defaults applied). * * @param string $key Setting key. * @return mixed */ public static function get( $key ) { $all = self::all(); return array_key_exists( $key, $all ) ? $all[ $key ] : null; } /** * 'all' or an array of post-type slugs the tag may load on. * * @return string|array */ public static function tag_post_types() { return self::get( 'tag_post_types' ); } /** * Whether to suppress the tag for logged-in users. * * @return bool */ public static function exclude_logged_in() { return (bool) self::get( 'tag_exclude_logged_in' ); } /** * Whether to suppress the tag for administrators. * * @return bool */ public static function exclude_admins() { return (bool) self::get( 'tag_exclude_admins' ); } /** * Excluded URL fragments as a trimmed, non-empty list. * * @return array */ public static function exclude_urls() { $raw = (string) self::get( 'tag_exclude_urls' ); if ( '' === trim( $raw ) ) { return array(); } $out = array(); foreach ( preg_split( '/\r\n|\r|\n/', $raw ) as $line ) { $line = trim( $line ); if ( '' !== $line ) { $out[] = $line; } } return $out; } /** * The tag's consent mode: 'off' (default, the tag starts at page load), 'tcf' * (wait for the page's IAB TCF v2 CMP) or 'manual' (wait for AdPriva.grantConsent()). * * @return string */ public static function tag_consent_mode() { $mode = (string) self::get( 'tag_consent_mode' ); return in_array( $mode, self::CONSENT_MODES, true ) ? $mode : 'off'; } /** * Whether the WP-dashboard summary widget is enabled. * * @return bool */ public static function show_widget() { return (bool) self::get( 'show_dashboard_widget' ); } /** * The default dashboard date range ('7d' | '30d' | '90d'). * * @return string */ public static function default_range() { return (string) self::get( 'default_date_range' ); } /** * Resolved API base URL: wp-config constant wins, then a saved override, then the default. * * @return string */ public static function api_base() { return self::endpoint( 'api_base', 'https://api.adpriva.com' ); } /** * Resolved console URL (same precedence as api_base()). * * @return string */ public static function console_url() { return self::endpoint( 'console_url', 'https://console.adpriva.com' ); } /** * Resolved tag script URL (same precedence as api_base()). * * @return string */ public static function tag_url() { return self::endpoint( 'tag_url', 'https://cdn.adpriva-ads.com/tag.js' ); } /** * Whether an endpoint is pinned by a wp-config constant (so its override is locked). * * @param string $key One of api_base|console_url|tag_url. * @return bool */ public static function endpoint_locked( $key ) { return isset( self::ENDPOINT_CONSTANTS[ $key ] ) && defined( self::ENDPOINT_CONSTANTS[ $key ] ); } /** * Resolve an endpoint by the constant-wins precedence. * * @param string $key Endpoint key. * @param string $fallback Value when neither a constant nor an override is set. * @return string */ private static function endpoint( $key, $fallback ) { $const = isset( self::ENDPOINT_CONSTANTS[ $key ] ) ? self::ENDPOINT_CONSTANTS[ $key ] : ''; if ( '' !== $const && defined( $const ) ) { return (string) constant( $const ); } $override = (string) self::get( $key ); return '' !== $override ? $override : $fallback; } // ── Settings API section + field renderers ───────────────────────────────── /** * Render a section intro. * * @return void */ public static function section_tag() { echo '

' . esc_html__( 'Control where the AdPriva verification tag loads on your site.', 'adpriva' ) . '

'; } /** * Render a section intro. * * @return void */ public static function section_tag_consent() { echo '

' . esc_html__( 'Choose when the AdPriva tag may observe the page and send data. With TCF or Manual, it sends nothing until the visitor consents and stops when consent is withdrawn.', 'adpriva' ) . '

'; } /** * Render a section intro. * * @return void */ public static function section_dashboard() { echo '

' . esc_html__( 'Preferences for the in-admin AdPriva dashboard.', 'adpriva' ) . '

'; } /** * Render a section intro. * * @return void */ public static function section_advanced() { echo '

' . esc_html__( 'Override the AdPriva service endpoints. Leave blank to use the defaults. An endpoint pinned in wp-config.php cannot be changed here.', 'adpriva' ) . '

'; } /** * Render the post-types selector. * * @return void */ public static function field_post_types() { $current = self::tag_post_types(); $all = ( 'all' === $current || ! is_array( $current ) ); $types = function_exists( 'get_post_types' ) ? get_post_types( array( 'public' => true ), 'objects' ) : array(); echo '
'; echo '

' . esc_html__( 'Leave all unchecked to load on every public post type.', 'adpriva' ) . '

'; foreach ( $types as $type ) { $slug = isset( $type->name ) ? $type->name : ''; $label = isset( $type->label ) ? $type->label : $slug; $checked = ( ! $all && is_array( $current ) && in_array( $slug, $current, true ) ); printf( '', esc_attr( self::OPTION ), esc_attr( $slug ), checked( $checked, true, false ), esc_html( $label ) ); } echo '
'; } /** * Render the "exclude logged-in" checkbox. * * @return void */ public static function field_exclude_logged_in() { self::render_checkbox( 'tag_exclude_logged_in', __( 'Do not load the tag for logged-in users.', 'adpriva' ) ); } /** * Render the "exclude admins" checkbox. * * @return void */ public static function field_exclude_admins() { self::render_checkbox( 'tag_exclude_admins', __( 'Do not load the tag for administrators.', 'adpriva' ) ); } /** * Render the exclude-URLs textarea. * * @return void */ public static function field_exclude_urls() { printf( '', esc_attr( self::OPTION ), esc_textarea( (string) self::get( 'tag_exclude_urls' ) ) ); echo '

' . esc_html__( 'One path fragment per line. The tag is skipped on any URL containing a listed fragment.', 'adpriva' ) . '

'; } /** * Render the consent-mode selector (AdPriva/tag#138). * * @return void */ public static function field_tag_consent_mode() { self::render_select( 'tag_consent_mode', array( 'off' => __( 'Off: start on page load (only where consent is not required)', 'adpriva' ), 'tcf' => __( 'TCF: wait for consent from my IAB TCF v2 consent banner', 'adpriva' ), 'manual' => __( 'Manual: wait until my site calls AdPriva.grantConsent()', 'adpriva' ), ) ); echo '

' . esc_html__( 'TCF needs a TCF v2 consent banner (CMP) on the page and checks purpose 1, "Store and/or access information on a device". Use Manual for other consent tools, such as Google Tag Manager consent triggers or a CMP that lists AdPriva as a custom vendor.', 'adpriva' ) . '

'; } /** * Render the "show widget" checkbox. * * @return void */ public static function field_show_widget() { self::render_checkbox( 'show_dashboard_widget', __( 'Show the AdPriva summary widget on the WordPress dashboard.', 'adpriva' ) ); } /** * Render the default-range selector. * * @return void */ public static function field_default_range() { self::render_select( 'default_date_range', array( '7d' => __( 'Last 7 days', 'adpriva' ), '30d' => __( 'Last 30 days', 'adpriva' ), '90d' => __( 'Last 90 days', 'adpriva' ), ) ); } /** * Render the API-base override field. * * @return void */ public static function field_api_base() { self::render_endpoint_field( 'api_base' ); } /** * Render the console-URL override field. * * @return void */ public static function field_console_url() { self::render_endpoint_field( 'console_url' ); } /** * Render the tag-URL override field. * * @return void */ public static function field_tag_url() { self::render_endpoint_field( 'tag_url' ); } /** * Shared checkbox renderer. * * @param string $key Setting key. * @param string $label Inline label. * @return void */ private static function render_checkbox( $key, $label ) { printf( '', esc_attr( self::OPTION ), esc_attr( $key ), checked( (bool) self::get( $key ), true, false ), esc_html( $label ) ); } /** * Shared single-select renderer. * * @param string $key Setting key. * @param array $options value => label. * @return void */ private static function render_select( $key, array $options ) { $current = (string) self::get( $key ); printf( ''; } /** * Shared endpoint-override renderer (locked + showing the pinned value when defined). * * @param string $key Endpoint key. * @return void */ private static function render_endpoint_field( $key ) { if ( self::endpoint_locked( $key ) ) { $const = self::ENDPOINT_CONSTANTS[ $key ]; printf( ' %2$s', esc_attr( (string) constant( $const ) ), esc_html( sprintf( /* translators: %s: PHP constant name. */ __( 'Pinned by %s in wp-config.php.', 'adpriva' ), $const ) ) ); return; } printf( '', esc_attr( self::OPTION ), esc_attr( $key ), esc_attr( (string) self::get( $key ) ), esc_attr( self::endpoint( $key, '' ) ) ); } }