AdSenseService::get()->create_entities() ); add_action( 'admin_enqueue_scripts', function( $hook_suffix ) { $tab = Params::get()->getKey( 'tab' ); if ( $tab !== 'adsense' ) { return; } $this->enqueue_delivery_check(); $inline_js = " document.addEventListener( 'DOMContentLoaded', function () { const announce = function ( msg ) { if ( window.wp && wp.a11y && wp.a11y.speak ) { wp.a11y.speak( msg ); } }; // Re-shows the success message after the reload below, so it isn't lost. try { const saved = sessionStorage.getItem( 'adpresso_adsense_feedback' ); if ( saved ) { sessionStorage.removeItem( 'adpresso_adsense_feedback' ); const target = document.getElementById( 'adpresso-create-entities-feedback' ); if ( target ) { target.style.display = 'block'; target.style.color = '#46b450'; target.textContent = saved; } announce( saved ); } } catch ( e ) {} const button = document.getElementById( 'adpresso-create-entities-btn' ); if ( !button ) { return; } const spinner = document.getElementById( 'adpresso-create-entities-spinner' ); const feedback = document.getElementById( 'adpresso-create-entities-feedback' ); button.addEventListener( 'click', function () { button.disabled = true; spinner.style.display = 'inline-block'; feedback.style.display = 'block'; feedback.textContent = '" . esc_js( __( 'Creating...', 'adpresso' ) ) . "'; feedback.style.color = ''; const data = new URLSearchParams(); data.append( 'action', '" . esc_js( self::CREATE_ENTITIES_AJAX_ACTION ) . "' ); data.append( '_ajax_nonce', '" . esc_js( wp_create_nonce( self::CREATE_ENTITIES_AJAX_ACTION ) ) . "' ); fetch( ajaxurl, { method: 'POST', body: data } ) .then( response => response.json() ) .then( result => { if ( result.success ) { feedback.textContent = result.data.message; feedback.style.color = '#46b450'; announce( result.data.message ); try { sessionStorage.setItem( 'adpresso_adsense_feedback', result.data.message ); } catch ( e ) {} // Reload the page to show the new status. setTimeout( () => window.location.reload(), 1500 ); } else { feedback.textContent = '" . esc_js( __( 'Error:', 'adpresso' ) ) . " ' + result.data.message; feedback.style.color = '#d63638'; announce( feedback.textContent ); } } ) .catch( error => { feedback.textContent = '" . esc_js( __( 'An unexpected error occurred.', 'adpresso' ) ) . "'; feedback.style.color = '#d63638'; announce( feedback.textContent ); console.error( 'Entities creation error:', error ); } ) .finally( () => { button.disabled = false; spinner.style.display = 'none'; } ); } ); } ); "; wp_enqueue_script( 'wp-a11y' ); wp_add_inline_script( 'adpresso-admin-main', $inline_js ); } ); } /** * @return string */ public function get_id(): string { return 'adsense'; } /** * @return string */ public function get_title(): string { return __( 'AdSense', 'adpresso' ); } /** * Define the sections for this settings tab. * * @return string[] section_id => title */ public function get_sections(): array { return [ 'adsense_module_activation' => [ 'title' => __( 'AdSense Settings', 'adpresso' ), ], 'adsense_connect' => [ 'title' => __( 'Account Identity', 'adpresso' ), 'controlled_by' => 'enable_module_' . $this->get_id(), 'initially_open' => true, ], 'adsense_auto_ads' => [ 'title' => __( 'AdSense Auto Ads', 'adpresso' ), 'description' => __( 'Connect your website to AdSense to enable Google Auto Ads and verify your domain.', 'adpresso' ), 'controlled_by' => 'enable_module_' . $this->get_id(), 'initially_open' => true, ], 'adsense_delivery' => [ 'title' => __( 'Delivery', 'adpresso' ), 'description' => __( 'Whether Google actually shows ads on your site. A new site or account sees nothing until Google has approved it.', 'adpresso' ), 'controlled_by' => 'enable_module_' . $this->get_id(), 'initially_open' => true, ], ]; } /** * Define the options for this settings tab. * * @return array */ protected function define_options(): array { $options = []; // Need to get the settings here directly since the options aren't registered yet. $adsense_settings = (array) get_option( 'adpresso_settings_adsense', [] ); $adstxt_settings = (array) get_option( 'adpresso_settings_ads_txt', [] ); $adstxt_src = $adstxt_settings['ads_txt_source'] ?? 'virtual'; $is_pub_id_found = false; if ( $adstxt_src === 'physical' ) { $ads_txt_records = AdsTxtService::parse_physical_ads_txt(); foreach ( $ads_txt_records as $record ) { if ( $record['domain'] === 'google.com' && $record['publisher_id'] === ( $adsense_settings['adsense_publisher_id'] ?? '' ) ) { $is_pub_id_found = true; break; } } } $options[ 'enable_module_' . $this->get_id() ] = [ 'type' => 'toggle', 'label' => __( 'Enable AdSense Module', 'adpresso' ), 'default' => 0, 'description' => __( 'Access the full AdSense functionality, including Auto Ads management, website verification, and the automatic inclusion of the required ads.txt entry.', 'adpresso' ), 'link' => 'https://wpadpresso.com/docs/adsense-settings/?utm_source=plugin-backend&utm_medium=manual-link&utm_campaign=adsense&utm_content=settings-page', 'sanitize_type' => 'uint', 'highlighted' => true, 'section' => 'adsense_module_activation', ]; // Without an ID there is nothing to look for in ads.txt yet. if ( 'physical' === $adstxt_src && '' !== trim( (string) ( $adsense_settings['adsense_publisher_id'] ?? '' ) ) ) { if ( $is_pub_id_found ) { $options['adsense_publisher_id_notification'] = [ 'type' => 'html-content', 'html' => '
' . __( 'Your AdSense Publisher ID is already part of your physical ads.txt.', 'adpresso' ) . '
', 'section' => 'adsense_connect', ]; } else { $options['adsense_publisher_id_notification'] = [ 'type' => 'html-content', 'html' => '
' . __( 'Attention: Your AdSense Publisher ID is not part of your physical ads.txt.', 'adpresso' ) . '
', 'section' => 'adsense_connect', ]; } } $options['adsense_publisher_id'] = [ 'type' => 'text', 'label' => __( 'Fill in your AdSense Publisher ID to enable ad delivery on this website.', 'adpresso' ), 'description' => __( 'e.g. pub-123456789', 'adpresso' ), 'section' => 'adsense_connect', 'default' => '', 'placeholder' => 'pub-123456789', 'depends_on' => [ [ 'field_id' => 'enable_module_' . $this->get_id(), 'operator' => 'is', 'value' => 1, ], ], ]; $options['auto_ads_status'] = [ 'type' => 'html-content', 'section' => 'adsense_auto_ads', 'label' => __( 'Auto Ads status', 'adpresso' ), 'html' => function() use ( $adsense_settings ) { return $this->get_auto_ads_status_html( $adsense_settings ); }, 'depends_on' => [ [ 'field_id' => 'enable_module_' . $this->get_id(), 'operator' => 'is', 'value' => 1, ], ], ]; $auto_ads_setup_ready = ! empty( $adsense_settings['auto_ads_placement_id'] ) && ! empty( $adsense_settings['auto_ads_ad_id'] ); $options['adsense_enable_auto_ads'] = [ 'type' => 'toggle', 'label' => __( 'Embed Auto Ads code', 'adpresso' ), 'description' => $auto_ads_setup_ready ? __( 'Publishes the Auto Ads ad and placement, which insert the global AdSense code to verify your website and enable Auto Ads. Switching it off sets both to draft.', 'adpresso' ) : __( 'Available after setup.', 'adpresso' ), 'default' => 0, 'sanitize_type' => 'uint', 'section' => 'adsense_auto_ads', 'disabled' => ! $auto_ads_setup_ready, 'depends_on' => [ [ 'field_id' => 'enable_module_' . $this->get_id(), 'operator' => 'is', 'value' => 1, ], ], ]; $options['adsense_delivery_status'] = [ 'type' => 'html-content', 'section' => 'adsense_delivery', 'label' => __( 'Delivery check', 'adpresso' ), 'html' => fn() => $this->get_delivery_status_html( $adsense_settings ), 'depends_on' => [ [ 'field_id' => 'enable_module_' . $this->get_id(), 'operator' => 'is', 'value' => 1, ], ], ]; // Managed by AdSenseService, never rendered (no section). Registered so get_setting() resolves them. foreach ( self::AUTO_ADS_ID_KEYS as $key ) { $options[ $key ] = [ 'type' => 'html-content', 'section' => '', 'sanitize_type' => 'id', ]; } return $options; } /** * Delivery section: the counters of the last days and the check button. Without any AdSense code from * AdPresso on the site there is nothing to check: a hint says what is missing instead. * * @param array $settings AdSense settings. * * @return string */ private function get_delivery_status_html( array $settings ): string { if ( empty( $settings[ 'enable_module_' . $this->get_id() ] ) ) { return ''; } $summary = AdSenseDelivery::get_summary( AdSenseDelivery::SILENCE_DAYS ); $auto_ads = AdSenseService::auto_ads_running( $settings ); $ad_ids = AdSenseService::adsense_ad_ids( $settings, 4 ); $has_code = $auto_ads || ! empty( $ad_ids ); $has_pub_id = '' !== trim( (string) ( $settings['adsense_publisher_id'] ?? '' ) ); // Where the code comes from, shown as ad tags: the Auto Ads ad, then up to three ads with AdSense code. $code_sources = []; if ( $auto_ads ) { $code_sources[ (int) $settings['auto_ads_ad_id'] ] = __( 'Auto Ads', 'adpresso' ); } foreach ( array_slice( $ad_ids, 0, 3 ) as $ad_id ) { $code_sources[ $ad_id ] = (string) get_the_title( $ad_id ); } ob_start(); ?>
0 ) : ?>

>

    $title ) : ?>
  • 3 ) : ?>

add_query_arg( [ 'adpresso_delivery_check' => '1', 'adpresso_ts' => time(), ], home_url( '/' ) ), 'timeout' => 12000, 'messages' => [ 'running' => __( 'Loading your homepage and waiting for AdSense…', 'adpresso' ), 'unreachable' => __( 'Your homepage could not be inspected from here. Open it in a new tab and look for AdSense errors in the browser console.', 'adpresso' ), 'missing' => __( 'The AdSense code is not on your homepage: the Auto Ads code is not embedded, or the Privacy module holds it back until a visitor consents. Ad units placed only on other pages are not covered by this check.', 'adpresso' ), 'blocked' => __( 'The AdSense script did not load in this browser. An ad blocker or privacy extension is the usual reason: switch it off for this site and run the check again.', 'adpresso' ), /* translators: %d: number of seconds */ 'silent' => __( 'AdSense loaded but delivered no ad within %d seconds. Google serves nothing while a site is not approved: check in AdSense under Sites that this domain is listed as Ready, that Auto Ads are switched on for it, and that your account has passed the review. Approval can take days to weeks. An approved site can still get empty slots when few advertisers bid on it.', 'adpresso' ), 'unfilled' => __( 'AdSense answered but left every ad unit empty (unfilled). The site or account is probably not approved yet, or the units are new. Check the status under Sites in AdSense; on an approved site this means few advertisers bid on this page.', 'adpresso' ), /* translators: 1: filled ad units, 2: unfilled ad units, 3: units inserted by Auto Ads */ 'delivered' => __( 'AdSense delivered ads. Ad units: %1$d filled, %2$d unfilled. Units inserted by Auto Ads: %3$d.', 'adpresso' ), ], ] ); } /** * The form never posts the Auto Ads IDs (nor a usable Auto Ads toggle while it's disabled), * so keep the stored ones unless they are written explicitly. * * @param mixed $options Submitted tab options. * * @return array */ public function sanitize_settings( $options ): array { $sanitized = parent::sanitize_settings( $options ); $stored = (array) get_option( 'adpresso_settings_adsense', [] ); foreach ( self::AUTO_ADS_ID_KEYS as $key ) { if ( ! isset( $sanitized[ $key ] ) && isset( $stored[ $key ] ) ) { $sanitized[ $key ] = absint( $stored[ $key ] ); } } // A disabled toggle only posts its hidden "0", so keep the stored value instead. // Writes that bring a ready setup along (Auto Ads entity creation) are taken as-is. $fields = $this->get_options(); $incoming_ready = ! empty( $sanitized['auto_ads_placement_id'] ) && ! empty( $sanitized['auto_ads_ad_id'] ); if ( ! empty( $fields['adsense_enable_auto_ads']['disabled'] ) && ! $incoming_ready ) { unset( $sanitized['adsense_enable_auto_ads'] ); if ( isset( $stored['adsense_enable_auto_ads'] ) ) { $sanitized['adsense_enable_auto_ads'] = absint( $stored['adsense_enable_auto_ads'] ); } } return $sanitized; } /** * Renders the button to create the Auto Ads entities automatically. * * @param array $old_values * * @return string */ public function render_create_entities_button( array $old_values ): string { $update = false; $label = __( 'Set up Auto Ads', 'adpresso' ); $color_class = 'adpresso-color-primary'; $has_pub_id = $old_values['has_pub_id'] ?? true; if ( $old_values['placement_id'] > 0 && $old_values['ad_id'] > 0 ) { $update = true; $label = __( 'Regenerate ad and placement', 'adpresso' ); $color_class = 'adpresso-color-accent'; } ob_start(); ?>

'; $placement = adpresso_get_placement( $placement_id, false ); $ad = adpresso_get_ad( $ad_id, false ); $published = static fn( $entity ) => $entity && 'publish' === $entity->get_post()->post_status; $has_pub_id = '' !== trim( (string) ( $settings['adsense_publisher_id'] ?? '' ) ); $output .= '

'; if ( ! $ad && ! $placement ) { $output .= '' . esc_html__( 'AdSense Auto Ads are not set up.', 'adpresso' ) . ' '; } elseif ( ! $ad || ! $placement ) { $output .= '' . esc_html( $ad ? __( 'The Auto Ads placement is missing.', 'adpresso' ) : __( 'The Auto Ads ad is missing.', 'adpresso' ) ) . ' ' . esc_html__( 'Run the setup again to recreate it.', 'adpresso' ) . ' '; } elseif ( ( ! $published( $ad ) || ! $published( $placement ) ) && empty( $settings['adsense_enable_auto_ads'] ) ) { $output .= '' . esc_html__( 'Auto Ads are set up but switched off.', 'adpresso' ) . ' ' . esc_html__( 'Switch on "Embed Auto Ads code" below to publish the ad and placement.', 'adpresso' ) . ' '; } elseif ( ! $published( $ad ) || ! $published( $placement ) ) { $output .= '' . esc_html__( 'The Auto Ads ad or placement is not published.', 'adpresso' ) . ' ' . esc_html__( 'Run the setup again to publish both.', 'adpresso' ) . ' '; } else { $output .= '' . esc_html__( 'AdSense Auto Ads are ready.', 'adpresso' ) . ' ' . esc_html__( 'The Auto Ads code is on your pages.', 'adpresso' ) . ' '; } $output .= '' . esc_html__( 'Learn more', 'adpresso' ) . ''; $output .= '

'; $output .= '
'; $output .= $this->render_entity_card( $ad, $ad_id, __( 'No AdSense ad found', 'adpresso' ) ); $output .= $this->render_entity_card( $placement, $placement_id, __( 'No AdSense placement found', 'adpresso' ) ); $output .= '
'; $output .= $this->render_create_entities_button( [ 'placement_id' => $ad && $placement ? $placement_id : 0, 'ad_id' => $ad && $placement ? $ad_id : 0, 'has_pub_id' => $has_pub_id, ] ); $output .= ''; return $output; } /** * Renders an entity card. * * @param $entity * @param $entity_id * @param $empty_text * * @return bool|string */ private function render_entity_card( $entity, $entity_id, $empty_text ): bool|string { if ( ! $entity ) { return '
' . esc_html( $empty_text ) . '
'; } $title = $entity->get_title(); $type = $entity->get_type_object()->get_title(); $edit_link = Util::get_post_edit_url( (int) $entity_id ); $image = $entity->get_image(); $status_obj = get_post_status_object( $entity->get_post()->post_status ); $card_notice = $status_obj ? $status_obj->label : ''; ob_start(); require ADPRESSO_ABSPATH . 'views/admin/entity-card.php'; return ob_get_clean(); } }