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' => '
'; 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 .= '