getKey( 'tab' ); if ( $tab !== 'adsense' ) { return; } wp_add_inline_script( 'adpresso-admin-main', $this->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 AdSense settings here directly since the options aren't registered yet. $adsense_settings = (array) get_option( 'adpresso_settings_adsense', [] ); $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', ]; $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(); ?>
inline_js = " document.addEventListener( 'DOMContentLoaded', function () { const button = document.getElementById( 'adpresso-create-entities-btn' ); const spinner = button.nextElementSibling; const feedback = document.getElementById( 'adpresso-create-entities-feedback' ); button.addEventListener( 'click', function () { button.disabled = true; spinner.style.display = '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', '" . esc_js( (int) $old_values['ad_id'] ) . "' ); data.append( 'placement_id', '" . esc_js( (int) $old_values['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'; } ); } ); } ); "; return ob_get_clean(); } /** * Generates the HTML for the Auto Ads status section. * * @param array $settings * * @return string * @throws Exception */ private function get_auto_ads_status_html( array $settings ): string { $placement_id = $settings['auto_ads_placement_id'] ?? 0; $ad_id = $settings['auto_ads_ad_id'] ?? 0; $setup_ready = $placement_id > 0 && $ad_id > 0; $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 .= '