__( 'AdSense In-feed', 'advanced-ads-adsense-in-feed' ), 'description' => __( 'Display an AdSense In-feed ad between posts', 'advanced-ads-adsense-in-feed' ), 'image' => plugin_dir_url( __FILE__ ) . 'adsense-in-feed.png', 'order' => 21, 'options' => array( 'show_position' => true, 'show_lazy_load' => true, ), ); return $types; } /** * Echo ad before/after posts in loops on archive pages * * @param array $post post object. * @param WP_Query $wp_query query object. * * @since 1.0 */ public function inject_in_feed( $post, $wp_query = null ) { if ( ! $wp_query instanceof WP_Query || is_feed() || is_admin() || $wp_query->is_singular() || ! $wp_query->in_the_loop || ! isset( $wp_query->current_post ) || ! $wp_query->is_main_query() ) { return; } $curr_index = $wp_query->current_post + 1; // normalize index. // handle the situation when wp_reset_postdata() is used after secondary query inside main query. static $handled_indexes = array(); if ( isset( $handled_indexes[ $curr_index ] ) ) { return; } $handled_indexes[] = $curr_index; $placements = get_option( 'advads-ads-placements', array() ); if ( is_array( $placements ) ) { foreach ( $placements as $_placement_id => $_placement ) { if ( empty( $_placement['item'] ) ) { continue; } if ( isset( $_placement['type'] ) && 'adsense_in_feed' === $_placement['type'] ) { $_options = isset( $_placement['options'] ) ? $_placement['options'] : array(); $ad_index = isset( $_options['adsense_in_feed_pages_index'] ) ? absint( ( $_options['adsense_in_feed_pages_index'] ) ) : 1; if ( $ad_index === $curr_index ) { $_options['placement']['type'] = $_placement['type']; if ( isset( $_placement['item'] ) && $this->is_infeed_ad_item( $_placement['item'] ) ) { echo Advanced_Ads_Select::get_instance()->get_ad_by_method( $_placement_id, Advanced_Ads_Select::PLACEMENT, $_options ); } } } } } } /** * Check if the current ad is indeed an infeed item * * @param string $item_id item ID from a placement. * * @return bool true if it is an infeed ad */ private function is_infeed_ad_item( $item_id ) { $_item = explode( '_', $item_id ); if ( 'ad' !== $_item[0] || ! isset( $_item[1] ) || empty( $_item[1] ) ) { return false; } // load the ad. $ad = new Advanced_Ads_Ad( $_item[1] ); if ( isset( $ad->type ) && 'adsense' === $ad->type && isset( $ad->content ) && strpos( $ad->content, 'in-feed' ) ) { return true; } return false; } /** * Show warning about missing Advanced Ads basic plugin */ public function show_advads_version_notice() { $plugin_data = get_plugin_data( __FILE__ ); $plugins = get_plugins(); if ( isset( $plugins['advanced-ads/advanced-ads.php'] ) ) { // is installed, but not active. $link = '' . __( 'Activate Now', 'advanced-ads-adsense-in-feed' ) . ''; } else { $link = '' . __( 'Install Now', 'advanced-ads-adsense-in-feed' ) . ''; } echo '

' . sprintf( __( '%s requires the Advanced Ads plugin to be installed and activated on your site.', 'advanced-ads-adsense-in-feed' ), $plugin_data['Name'] ) . ' ' . $link . '

'; } function load_plugin_textdomain() { load_plugin_textdomain( 'advanced-ads-adsense-in-feed', false, basename( dirname( __FILE__ ) ) . '/languages/' ); } /** * Check if this is indeed an In-feed ad * * @param string $placement_slug id of the placement. * @param array $placement array with placement options. * * @since 1.0 */ public function placement_options_check( $placement_slug, $placement ) { if ( isset( $placement['type'] ) && 'adsense_in_feed' === $placement['type'] ) { // check if this is the correct item type (AdSense In-feed). if ( isset( $placement['item'] ) && ! $this->is_infeed_ad_item( $placement['item'] ) ) { echo '

' . sprintf( // translators: %s is a link. __( 'This placement can only deliver a single In-feed ad. Use the Post Lists placement for other ad types.', 'advanced-ads-adsense-in-feed' ), esc_url( ADVADS_URL ) . 'manual/placement-post-lists/' ) . '

'; } } } /** * Render placement options * * @param string $placement_slug id of the placement. * @param array $placement array with placement options. * * @since 1.0 */ public function placement_options( $placement_slug, $placement ) { if ( isset( $placement['type'] ) && 'adsense_in_feed' === $placement['type'] ) { $index = ( isset( $placement['options']['adsense_in_feed_pages_index'] ) ) ? $placement['options']['adsense_in_feed_pages_index'] : 1; $index_option = ''; $option_content = sprintf( // translators: $s is an index, e.g., "5". __( 'Inject before %s. post', 'advanced-ads-adsense-in-feed' ), $index_option ); $description = __( 'Before which post to inject the ad on post lists.', 'advanced-ads-adsense-in-feed' ); if ( class_exists( 'Advanced_Ads_Admin_Options' ) ) { Advanced_Ads_Admin_Options::render_option( 'placement-in-feed-position', __( 'position', 'advanced-ads-adsense-in-feed' ), $option_content, $description ); } } } } /** * Halt code remove with new release. * * @return void */ function wp_advads_in_feed_halt_code() { global $advads_halt_notices; // Early bail!! if ( ! defined( 'ADVADS_VERSION' ) ) { return; } if ( version_compare( ADVADS_VERSION, '2.0.0', '>=' ) ) { if ( ! isset( $advads_halt_notices ) ) { $advads_halt_notices = []; } $advads_halt_notices[] = __( 'Advanced Ads – Google AdSense In-feed Placement', 'advanced-ads-adsense-in-feed' ); add_action( 'all_admin_notices', static function () { global $advads_halt_notices; // Early bail!! if ( 'plugins' === get_current_screen()->base || empty( $advads_halt_notices ) ) { return; } ?>

Advanced Ads 2.0 and have been deactivated. Please update them to their latest version. If you cannot update, e.g., due to an expired license, you can roll back to a compatible version of the Advanced Ads plugin at any time or renew your license.', 'advanced-ads-adsense-in-feed' ), esc_url( admin_url( 'admin.php?page=advanced-ads-tools&sub_page=version' ) ), 'https://wpadvancedads.com/account/#h-licenses' ) ) ?>

'; wp_admin_notice( sprintf( /* translators: %s: Plugin name */ __( 'Your version of Advanced Ads – Google AdSense In-feed Placement is incompatible with Advanced Ads 2.0 and has been deactivated. Please update the plugin to the latest version. If you cannot update the plugin, e.g., due to an expired license, you can roll back to a compatible version of the Advanced Ads plugin at any time or renew your license.', 'advanced-ads-pro' ), esc_url( admin_url( 'admin.php?page=advanced-ads-tools&sub_page=version' ) ), 'https://wpadvancedads.com/account/#h-licenses' ), [ 'type' => 'error', 'additional_classes' => array( 'notice-alt', 'inline', 'update-message' ), ] ); echo ''; } ); return; } // Autoload and activate. new Advanced_Ads_In_Feed(); } add_action( 'plugins_loaded', 'wp_advads_in_feed_halt_code', 5 );