getKey( 'tab' ); if ( $tab !== 'adsense' ) { return; } $settings = (array) get_option( 'adpresso_settings_adsense', [] ); $ad_id = (int) ( $settings['auto_ads_ad_id'] ?? 0 ); $placement_id = (int) ( $settings['auto_ads_placement_id'] ?? 0 ); $inline_js = " document.addEventListener( 'DOMContentLoaded', function () { 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 ) ) . "' ); data.append( 'ad_id', '" . $ad_id . "' ); data.append( 'placement_id', '" . $placement_id . "' ); fetch( ajaxurl, { method: 'POST', body: data } ) .then( response => response.json() ) .then( result => { if ( result.success ) { feedback.textContent = result.data.message; feedback.style.color = '#46b450'; // 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'; } } ) .catch( error => { feedback.textContent = '" . esc_js( __( 'An unexpected error occurred.', 'adpresso' ) ) . "'; feedback.style.color = '#d63638'; console.error( 'Entities creation error:', error ); } ) .finally( () => { button.disabled = false; spinner.style.display = 'none'; } ); } ); } ); "; 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, ], ]; } /** * 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', ]; if($adstxt_src === 'physical') { 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 ? __( 'Insert the global AdSense code to verify your website and enable Auto Ads functionality.', '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, ], ], ]; return $options; } /** * 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'; 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(); ?>

0 && $ad_id > 0; $output = '
'; $placement = adpresso_get_placement( $placement_id, false ); $ad = adpresso_get_ad( $ad_id, false ); $output .= '

'; if ( ! $setup_ready ) { $output .= '' . __( 'AdSense Auto Ads are not set up.', 'adpresso' ) . ' '; } elseif ( $placement && $placement->get_post()->post_status === 'publish' && $ad && $ad->get_post()->post_status === 'publish' ) { $output .= '' . __( 'AdSense Auto Ads are ready.', 'adpresso' ) . ' ' . __( 'The required ad and placement full functional and running. ', 'adpresso' ); } $output .= 'Learn more'; $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' => $placement_id, 'ad_id' => $ad_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 = get_edit_post_link( $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(); } }