hooks(); } return self::$instance; } private function __construct() { } /** * Registers all necessary hooks for the admin side of the module. */ public function hooks(): void { add_action( 'pre_update_option_' . self::OPTION_NAME, [ $this, 'handle_auto_ads_on_save' ], 10, 3 ); add_filter( 'display_post_states', [ $this, 'mark_auto_ads_post_states' ], 10, 2 ); add_action( 'before_delete_post', [ $this, 'clear_auto_ads_ids_on_delete' ], 10, 1 ); } /** * Removes the old Auto Ads IDs from the settings when a post is deleted. * * @param int $post_id * * @return void */ public function clear_auto_ads_ids_on_delete( int $post_id ): void { $settings = Settings::get_setting( self::OPTION_NAME, [] ); if ( ! is_array( $settings ) ) { return; } $changed = false; if ( (int) ( $settings['auto_ads_ad_id'] ?? 0 ) === $post_id ) { $settings['auto_ads_ad_id'] = 0; $changed = true; } elseif ( (int) ( $settings['auto_ads_placement_id'] ?? 0 ) === $post_id ) { $settings['auto_ads_placement_id'] = 0; $changed = true; } if ( ! $changed ) { return; } /** * Important: Since we are filtering via `pre_update_option_{option}` * to process auto ads logic when saving, so * we need to remove the filter temporarily, that he doesn't replace the 0's * with the old IDs. */ remove_filter( 'pre_update_option_' . self::OPTION_NAME, [ $this, 'handle_auto_ads_on_save' ], 10 ); update_option( self::OPTION_NAME, $settings ); // Filter wieder hinzufügen add_filter( 'pre_update_option_' . self::OPTION_NAME, [ $this, 'handle_auto_ads_on_save' ], 10, 3 ); } /** * Adds an 'AdSense Auto Ad' or 'AdSense Auto Placement' state to the post list table. * * @param array $states * @param WP_Post $post * * @return array */ public function mark_auto_ads_post_states( array $states, WP_Post $post ): array { $settings = get_option( 'adpresso_settings_adsense', [] ); $ad_id = (int) ( $settings['auto_ads_ad_id'] ?? 0 ); $placement_id = (int) ( $settings['auto_ads_placement_id'] ?? 0 ); if ( $post->ID === $ad_id ) { $states['adpresso_adsense_ad'] = __( 'AdSense Auto Ad', 'adpresso' ); } elseif ( $post->ID === $placement_id ) { $states['adpresso_adsense_placement'] = __( 'AdSense Auto Placement', 'adpresso' ); } return $states; } /** * Handles the creation, update, or drafting of Auto Ads entities when settings are saved. * * @param array $old_value The old option value. * @param array $new_value The new option value. * * @return array The updated option value. */ public function handle_auto_ads_on_save( array $new_value, array $old_value, $option_name ): array { $was_enabled = ! empty( $old_value['adsense_enable_auto_ads'] ); $is_enabled = ! empty( $new_value['adsense_enable_auto_ads'] ); $publisher_id_changed = ( $old_value['adsense_publisher_id'] ?? '' ) !== ( $new_value['adsense_publisher_id'] ?? '' ); $new_value['auto_ads_placement_id'] = $old_value['auto_ads_placement_id'] ?? 0; $new_value['auto_ads_ad_id'] = $old_value['auto_ads_ad_id'] ?? 0; error_log( 'Auto Ads: was_enabled: ' . var_export( $was_enabled, true ) ); error_log( 'Auto Ads: is_enabled: ' . var_export( $is_enabled, true ) ); error_log( 'Auto Ads: publisher_id_changed: ' . var_export( $publisher_id_changed, true ) ); // Auto ads were activated if ( ! $was_enabled && $is_enabled ) { $this->activate_auto_ads( $new_value ); } elseif ( $was_enabled && ! $is_enabled ) { // Auto ads were disabled $this->deactivate_auto_ads( $new_value ); } elseif ( $is_enabled && $publisher_id_changed ) { // Publisher-ID changed $this->update_auto_ads_publisher_id( $new_value ); } else { } return $new_value; } /** * Creates and publishes the necessary Ad and Placement for Auto Ads. * * @param array $settings * * @return void */ private function activate_auto_ads( array &$settings ): void { $placement_id = $settings['auto_ads_placement_id'] ?? 0; $ad_id = $settings['auto_ads_ad_id'] ?? 0; $placement_exists = $placement_id && get_post_status( $placement_id ); $ad_exists = $ad_id && get_post_status( $ad_id ); if ( $placement_exists && $ad_exists ) { error_log( 'Auto Ads: Placement and Ad already exist. Skipping creation.' ); wp_update_post( [ 'ID' => $placement_id, 'post_status' => 'publish' ] ); wp_update_post( [ 'ID' => $ad_id, 'post_status' => 'publish' ] ); return; } $publisher_id = $settings['adsense_publisher_id'] ?? ''; if ( empty( $publisher_id ) ) { return; } // Create ad $new_ad_id = $this->create_auto_ads_ad( $publisher_id ); if ( ! $new_ad_id ) { error_log( 'Auto Ads: Failed to create Ad. Skipping creation.' ); return; } // Create placement $new_placement_id = $this->create_auto_ads_placement(); if ( ! $new_placement_id ) { error_log( 'Auto Ads: Failed to create Placement. Skipping creation.' ); return; } $this->assign_ad_to_placement( $new_placement_id, $new_ad_id ); error_log( 'Auto Ads: Placement and Ad created successfully.' ); // Save IDs. $settings['auto_ads_placement_id'] = $new_placement_id; $settings['auto_ads_ad_id'] = $new_ad_id; } /** * Sets the auto-generated Placement to 'draft' status. * * @param array $settings * * @return void */ private function deactivate_auto_ads( array $settings ): void { $placement_id = $settings['auto_ads_placement_id'] ?? 0; error_log( 'Auto Ads: Deactivating Auto Ads. Placement ID: ' . $placement_id ); if ( $placement_id && get_post( $placement_id ) ) { error_log( 'Auto Ads: Placement found. Setting to draft.' ); wp_update_post( [ 'ID' => $placement_id, 'post_status' => 'draft' ] ); } } /** * Updates the Ad content with a new Publisher ID. * * @param array $settings * * @return void */ private function update_auto_ads_publisher_id( array $settings ): void { $ad_id = $settings['auto_ads_ad_id'] ?? 0; $publisher_id = $settings['adsense_publisher_id'] ?? ''; error_log( 'Auto Ads: Updating Ad with new Publisher ID. Ad ID: ' . $ad_id ); if ( $ad_id && $publisher_id && get_post( $ad_id ) ) { error_log( 'Auto Ads: Ad found. Updating content.' ); $new_script_content = $this->get_auto_ads_script_content( $publisher_id ); wp_update_post( [ 'ID' => $ad_id, 'post_content' => $new_script_content, ] ); } } /** * Create the ad for the Auto Ads script. * * @param string $publisher_id * * @return int */ private function create_auto_ads_ad( string $publisher_id ): int { $script_content = $this->get_auto_ads_script_content( $publisher_id ); $ad_id = wp_insert_post( [ 'post_type' => Constants::POST_TYPE_AD, 'post_title' => 'AdSense Auto Ads Script (Auto-Generated)', 'post_content' => $script_content, 'post_status' => 'publish', ] ); error_log( 'Auto Ads: Ad ID: ' . $ad_id ); if ( is_wp_error( $ad_id ) ) { return 0; } error_log( 'Auto Ads: Ad created successfully.' ); update_post_meta( $ad_id, Constants::META_PREFIX . 'ad_type', 'html' ); return $ad_id; } /** * Creates a new header placement for the Auto Ads script. * * @return int */ private function create_auto_ads_placement(): int { $placement_id = wp_insert_post( [ 'post_type' => Constants::POST_TYPE_PLACEMENT, 'post_title' => 'AdSense Auto Ads Placement (Auto-Generated)', 'post_status' => 'publish', ] ); error_log( 'Auto Ads: Placement ID: ' . $placement_id ); if ( is_wp_error( $placement_id ) ) { return 0; } error_log( 'Auto Ads: Placement created successfully.' ); update_post_meta( $placement_id, Constants::META_PREFIX . 'placement_type', HeaderPlacement::ID ); return $placement_id; } /** * Assign ad to placement. * * @param int $placement_id * @param int $ad_id * * @return void */ private function assign_ad_to_placement( int $placement_id, int $ad_id ): void { update_post_meta( $placement_id, Constants::META_PREFIX . 'placement_item_type', 'ad' ); update_post_meta( $placement_id, Constants::META_PREFIX . 'placement_item_id', $ad_id ); } /** * Creates the Auto Ads script content based on the Publisher ID. * * @param string $publisher_id * * @return string */ private function get_auto_ads_script_content( string $publisher_id ): string { $normalized_id = $this->normalize_adsense_publisher_id( $publisher_id ); return sprintf( // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript -- don't allow any changes on Google AdSense code. '', esc_attr( $normalized_id ) ); } /** * Normalizes the AdSense Publisher ID to a standard format. * * @param string $id * * @return string */ private function normalize_adsense_publisher_id( string $id ): string { $id = strtolower( trim( $id ) ); // Removes common prefixes $id = preg_replace( '/^(ca-)?(pub-)?/', '', $id ); // Only digits are allowed $id = preg_replace( '/\D/', '', $id ); // Add the standard prefix if not present return 'ca-pub-' . $id; } }