_amp_conditions = array( 'adsforwp_after_featured_image', 'adsforwp_below_the_header', 'adsforwp_below_the_footer', 'adsforwp_above_the_footer', 'adsforwp_above_the_post_content', 'adsforwp_below_the_post_content', 'adsforwp_below_the_title', 'adsforwp_above_related_post', 'adsforwp_below_author_box', 'adsforwp_ads_in_loops', ); $this->_display_tag_list = array( '

' => 'p_tag', '' => 'div_tag', '' => 'img_tag', '' => 'h1', '' => 'h2', '' => 'h3', '' => 'h4', '' => 'h5', '' => 'h6', ); } /** * We are here calling all required hooks */ public function adsforwp_hooks() { if ( ! is_admin() ) { if ( ( function_exists( 'ampforwp_is_amp_endpoint' ) ) || function_exists( 'is_amp_endpoint' ) ) { add_action( 'amp_init', array( $this, 'init' ) ); } else { add_action( 'init', array( $this, 'init' ) ); } } // Adsense Auto Ads hooks for amp and non amp starts here add_filter( 'widget_text', 'do_shortcode' ); add_action( 'wp_head', array( $this, 'adsforwp_ezoic_ads_script' ), 10 ); add_action( 'wp_head', array( $this, 'adsforwp_mediavines_ads_script' ) ); add_action( 'wp_head', array( $this, 'adsforwp_taboola_ads_script' ), 10 ); add_action( 'wp_head', array( $this, 'adsforwp_outbrain_script' ), 10 ); add_action( 'wp_head', array( $this, 'adsforwp_adblocker_detector' ) ); add_action( 'wp_footer', array( $this, 'adsforwp_adblocker_popup_notice' ) ); add_action( 'wp_footer', array( $this, 'adsforwp_adblocker_notice_jsondata' ) ); add_action( 'wp_body_open', array( $this, 'adsforwp_adblocker_notice_bar' ) ); add_action( 'wp_head', array( $this, 'adsforwp_adsense_auto_ads' ) ); add_action( 'wp_head', array( $this, 'adsforwp_doubleclick_head_code' ) ); // Sticky Adsense Ads add_action( 'amp_post_template_footer', array( $this, 'adsforwp_insert_sticky_ads_code' ), 12 ); // Background Ad add_action( 'amp_post_template_head', array( $this, 'adsforwp_adsense_auto_ads_amp_script' ), 1 ); add_action( 'wp_head', array( $this, 'adsforwp_preload_image_' ), 10 ); add_action( 'amp_post_template_head', array( $this, 'adsforwp_preload_images_amp' ) ); add_action( 'amp_post_template_footer', array( $this, 'adsforwp_adsense_auto_ads_amp_tag' ) ); // Adsense Auto Ads hooks for amp and non amp ends here add_filter( 'the_content', array( $this, 'adsforwp_display_ads' ) ); add_shortcode( 'adsforwp', array( $this, 'adsforwp_manual_ads' ) ); add_shortcode( 'adsforwp-group', array( $this, 'adsforwp_group_ads' ) ); // Hooks for sticky ads add_action( 'wp_footer', array( $this, 'adsforwp_display_sticky_ads' ) ); add_action( 'wp_footer', array( $this, 'adsforwp_taboola_footer_loader_js' ) ); add_filter( 'amp_post_template_data', array( $this, 'adsforwp_enque_ads_specific_amp_script' ) ); add_action( 'amp_post_template_css', array( $this, 'adsforwp_add_amp_stick_ad_css' ) ); add_action( 'amp_post_template_css', array( $this, 'adsforwp_add_amp_floating_ad_css' ) ); add_action( 'amp_post_template_css', array( $this, 'adsforwp_global_css_for_amp' ) ); add_action( 'amp_post_template_footer', array( $this, 'adsforwp_display_sticky_ads_amp' ), 9 ); add_action( 'wp_ajax_nopriv_adsforwp_update_amp_sticky_ad_status', array( $this, 'adsforwp_update_amp_sticky_ad_status' ) ); add_action( 'wp_ajax_nopriv_adsforwp_check_amp_sticky_ad_status', array( $this, 'adsforwp_check_amp_sticky_ad_status' ) ); add_action( 'wp_ajax_adsforwp_update_amp_sticky_ad_status', array( $this, 'adsforwp_update_amp_sticky_ad_status' ) ); add_action( 'wp_ajax_adsforwp_check_amp_sticky_ad_status', array( $this, 'adsforwp_check_amp_sticky_ad_status' ) ); add_action( 'amp_post_template_css', array( $this, 'adsforwp_background_ad_css' ) ); add_action( 'init', array( $this, 'adsforwp_add_query_var_front_js' ) ); add_action( 'parse_query', array( $this, 'adsforwp_serve_front_js' ) ); add_action( 'amp_post_template_above_footer', array( $this, 'adsforwp_ads_script_end_tag' ) ); add_action( 'ampforwp_after_header', array( $this, 'adsforwp_ads_condition_browser_data' ) ); // Amp Story Auto Ads filter add_filter( 'amp_story_auto_ads_configuration', array( $this, 'adsforwp_amp_story_auto_ads' ) ); } public function adsforwp_ads_browser_width_condition() { $conditions = array(); $data = array(); $all_ads_post = adsforwp_get_ad_ids(); $and_or_conditions = array(); if ( $all_ads_post ) { foreach ( $all_ads_post as $ads ) { $post_ad_id = $ads; $visitor_condition_enable = get_post_meta( $post_ad_id, $key = 'adsforwp_v_condition_enable', true ); $visitor_conditions_array = get_post_meta( $post_ad_id, 'visitor_conditions_array', true ) ; $service = new Adsforwp_Output_Service(); $ad_status = $service->adsforwp_is_condition( $post_ad_id ); if ( isset( $visitor_condition_enable ) && $visitor_condition_enable == 'enable' ) { for ( $j = 0;$j < count( $visitor_conditions_array );$j++ ) { $conditions = $visitor_conditions_array[ 'group-' . $j ]['visitor_conditions']; foreach ( $conditions as $key => $value ) { if ( in_array( 'browser_width', $value ) && $ad_status ) { if ( count( $conditions ) > 1 ) { $and_or_conditions[ $post_ad_id ]['and'][] = $conditions[ $key ]; } else { $and_or_conditions[ $post_ad_id ]['or'][] = $conditions[ $key ]; } } } } } } $data = $and_or_conditions; } return $data; } public function adsforwp_ads_script_end_tag() { $browser_data = $this->adsforwp_ads_browser_width_condition(); if ( ! empty( $browser_data ) ) { echo ''; } } public function adsforwp_ads_condition_browser_data() { $browser_data = $this->adsforwp_ads_browser_width_condition(); if ( ! empty( $browser_data ) ) { if ( ADSFORWP_ENVIRONMENT == 'DEV' ) { $amp_script_file = ADSFORWP_PLUGIN_DIR_URI . 'public/assets/js/ads-frontend-amp.js'; } else { $amp_script_file = ADSFORWP_PLUGIN_DIR_URI . 'public/assets/js/ads-frontend-amp.min.js'; } echo ' '; echo ''; } } public function adsforwp_amp_story_auto_ads( $data ) { $all_ads_post = adsforwp_get_ad_ids(); // $service = new Adsforwp_Output_Service(); $post_meta_dataset = array(); $data = array(); if ( $all_ads_post ) { foreach ( $all_ads_post as $ads ) { $post_ad_id = $ads; $post_type = get_post_meta( $post_ad_id, 'select_adtype', true ); $common_function_obj = new Adsforwp_Admin_Common_Functions(); $in_group = $common_function_obj->adsforwp_check_ads_in_group( $post_ad_id ); if ( empty( $in_group ) ) { if ( $post_type == 'amp_story_ads' ) { $post_meta_dataset = get_post_meta( $post_ad_id, $key = '', true ); $amp_story_adtypes = adsforwp_rmv_warnings( $post_meta_dataset, 'amp_story_adtypes', 'adsforwp_array' ); if ( $amp_story_adtypes == 'doubleclick' ) { $ad_slot_id = adsforwp_rmv_warnings( $post_meta_dataset, 'ampstory_dfp_slot_id', 'adsforwp_array' ); $data[] = array( 'ad-attributes' => array( 'type' => 'doubleclick', 'data-slot' => $ad_slot_id, ), ); } } } } } return $data; } public function adsforwp_serve_front_js( WP_Query $query ) { if ( $query->get( 'adsforwp_front_js' ) == 1 ) { if ( ! function_exists( 'WP_Filesystem' ) ) { include_once ABSPATH . '/wp-admin/includes/file.php'; } global $wp_filesystem; if ( ! $wp_filesystem ) { WP_Filesystem(); } $swhtmlcontent_escaped = $wp_filesystem->get_contents( ADSFORWP_PLUGIN_DIR . 'public/assets/js/ads-front.js' ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: $swHtmlContent contains static js code echo $swhtmlcontent_escaped; header( 'Content-Type: application/javascript' ); exit; } } public function adsforwp_add_query_var_front_js() { global $wp; $wp->add_query_var( 'adsforwp_front_js' ); } public function adsforwp_is_file_inroot() { //phpcs:ignore if(is_writable(ABSPATH)){ return true; } else { return false; } } public function init() { set_transient( 'adsforwp_transient_amp_ids', '' ); ob_start( array( $this, 'adsforwp_display_custom_target_ad' ) ); ob_start( array( $this, 'adsforwp_display_background_ad' ) ); ob_start( array( $this, 'adsforwp_display_popup_ad' ) ); } /** * This function is used to show ads based on html element target by user * * @param type $content * @return type string */ public function adsforwp_get_custom_target_ad_code( $content, $ad_id, $ad_type ) { if ( $ad_type == 'group' ) { $ad_code = $this->adsforwp_group_ads( $atts = null, $ad_id, '' ); } if ( $ad_type == 'ad' ) { $ad_code = $this->adsforwp_get_ad_code( $ad_id, $type = 'AD' ); } $post_meta = get_post_meta( $ad_id, $key = '', true ); if ( adsforwp_rmv_warnings( $post_meta, 'adsforwp_custom_target_position', 'adsforwp_array' ) == 'existing_element' ) { $action = adsforwp_rmv_warnings( $post_meta, 'adsforwp_existing_element_action', 'adsforwp_array' ); $jquery_selector = adsforwp_rmv_warnings( $post_meta, 'adsforwp_jquery_selector', 'adsforwp_array' ); if ( $jquery_selector ) { if ( strchr( $jquery_selector, '#' ) ) { $jquery_selector = str_replace( '#', '', $jquery_selector ); preg_match_all( '/<[^>]*id="[^"]*\b' . $jquery_selector . '\b[^"]*"[^>]*>/', $content, $matches ); $split = preg_split( '/<[^>]*id="[^"]*\b' . $jquery_selector . '\b[^"]*"[^>]*>/', $content ); } if ( strchr( $jquery_selector, '.' ) ) { $jquery_selector = str_replace( '.', '', $jquery_selector ); preg_match_all( '/<[^>]*class="[^"]*\b' . $jquery_selector . '\b[^"]*"[^>]*>/', $content, $matches ); $split = preg_split( '/<[^>]*class="[^"]*\b' . $jquery_selector . '\b[^"]*"[^>]*>/', $content ); } if ( is_array( $split ) && ! empty( $split ) ) { $all_matches = $split; foreach ( $all_matches as $key => $match ) { if ( isset( $matches[0][ $key ] ) ) { if ( $action == 'prepend_content' ) { $all_matches[ $key ] = $match . $ad_code . $matches[0][ $key ]; } if ( $action == 'append_content' ) { $all_matches[ $key ] = $match . $matches[0][ $key ] . $ad_code; } } } $content = implode( '', $all_matches ); } } } if ( adsforwp_rmv_warnings( $post_meta, 'adsforwp_custom_target_position', 'adsforwp_array' ) == 'new_element' ) { $new_element_div = html_entity_decode( adsforwp_rmv_warnings( $post_meta, 'adsforwp_new_element', 'adsforwp_array' ) ); $content = str_replace( $new_element_div, $ad_code, $content ); } return $content; } public function adsforwp_display_popup_ad($content){ // For single ad starts here $all_ads_id = adsforwp_get_ad_ids(); $service = new Adsforwp_Output_Service(); if ( ! empty( $all_ads_id ) ) { foreach ($all_ads_id as $key => $post_ad_id) { $post_meta_dataset = get_post_meta( $post_ad_id, $key_name = '', true ); $ad_type = adsforwp_rmv_warnings( $post_meta_dataset, 'select_adtype', 'adsforwp_array' ); if($ad_type=='popupad'){ $condition_status = $service->adsforwp_is_condition( $post_ad_id ); $popup_ad_id = adsforwp_rmv_warnings( $post_meta_dataset, 'select_popupad', 'adsforwp_array' ); $popup_ad_id = str_replace("'",'',$popup_ad_id); $popup_type = adsforwp_rmv_warnings( $post_meta_dataset, 'select_popupad_type', 'adsforwp_array' ); $popup_type_time = adsforwp_rmv_warnings( $post_meta_dataset, 'select_popupad_type_time', 'adsforwp_array' ); $popupad_cookie = adsforwp_rmv_warnings( $post_meta_dataset, 'select_popupad_cookie', 'adsforwp_array' ); $popupad_cookie_expiry_days = adsforwp_rmv_warnings( $post_meta_dataset, 'select_popupad_cookie_expiry', 'adsforwp_array' ); if($popupad_cookie_expiry_days==""){ $popupad_cookie_expiry_days = 1; } $select_popupad_scroll_percent = adsforwp_rmv_warnings( $post_meta_dataset, 'select_popupad_scroll_percent', 'adsforwp_array' ); if($select_popupad_scroll_percent==""){ $select_popupad_scroll_percent = 0; } $popup_cookie = false; if(!isset($_COOKIE['adsforwp_popupad'])){ $popup_cookie = true; if($popupad_cookie=='no_expiry'){ $popupad_cookie_expiry_days = (86400/2); }else if($popupad_cookie=='expiry'){ $popupad_cookie_expiry_days =(86400 * $popupad_cookie_expiry_days); } } if($condition_status && $popup_cookie){ $ad_data = array(); $ad_data['post_ad_id'] = $post_ad_id; $ad_data['popup_ad_id'] = $popup_ad_id; $ad_data['popup_type'] = $popup_type; $ad_data['popup_type_time'] = $popup_type_time; $ad_data['popup_scroll_percent'] = $select_popupad_scroll_percent; $ad_data['ad_type'] = 'ad'; $ad_data['popupad_cookie'] = $popupad_cookie; $ad_data['popupad_cookie_expiry_days'] = $popupad_cookie_expiry_days; $ad_status = $service->adsforwp_is_condition( $popup_ad_id ); if ( $ad_status ) { $content = $this->adsforwp_prepare_popup_ad_content($content, $ad_data); } } } } } return $content; } public function adsforwp_prepare_popup_ad_content($content, $ad_data){ $popup_type = $ad_data['popup_type']; $ad_type = $ad_data['ad_type']; $ad_id = $ad_data['popup_ad_id']; $post_ad_id = $ad_data['post_ad_id']; $popup_type_time = $ad_data['popup_type_time']; $popup_scroll_percent = $ad_data['popup_scroll_percent']; $popupad_cookie_expiry_days = $ad_data['popupad_cookie_expiry_days']; $popupad_cookie = $ad_data['popupad_cookie']; $ad_code=''; if ( $ad_type == 'group' ) { $ad_code = $this->adsforwp_group_ads( $atts = null, $ad_id, '' ); } if ( $ad_type == 'ad' ) { $ad_code = $this->adsforwp_get_ad_code( $ad_id, $type = 'AD' ); } $popup_ad_content = ''; if($ad_code!=''){ $script_code = ''; $close_popup_function = 'function handleClosePopupAdd(){ let exdays = '.$popupad_cookie_expiry_days.'; let cname = "adsforwp_popupad"; let cvalue = "popupad"; document.getElementById("popup-ad-overlay").style.display = "none"; const d = new Date(); d.setTime(d.getTime() + (exdays)); let expires = "expires="+ d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; }'; $custom_class = 'popup-ad-box-center'; if($popup_type=='instantly'){ $script_code = ''; }else if($popup_type=='specific_time'){ if($popup_type_time!=''){ $script_code = ''; } }else if($popup_type=='on_scroll'){ if($popup_scroll_percent!=''){ $script_code = ''; } }else if($popup_type=='on_top'){ $custom_class = 'popup-ad-box-top'; if($popup_type_time!=''){ $script_code = ''; } }else if($popup_type=='on_bottom'){ $custom_class = 'popup-ad-box-bottom'; if($popup_type_time!=''){ $script_code = ''; } } $popup_ad_content = ''.$script_code.''; $content = str_replace('',$popup_ad_content,$content); } return $content; } public function adsforwp_display_custom_target_ad( $content ) { // For single ad starts here $all_ads_id = adsforwp_get_ad_ids(); $service = new Adsforwp_Output_Service(); if ( ! empty( $all_ads_id ) ) { foreach ( $all_ads_id as $ad_id ) { $wheretodisplay = get_post_meta( $ad_id, $key = 'wheretodisplay', true ); if ( $wheretodisplay == 'custom_target' ) { $ad_status = $service->adsforwp_is_condition( $ad_id ); if ( $ad_status ) { $content = $this->adsforwp_get_custom_target_ad_code( $content, $ad_id, 'ad' ); } } } } // For group ads $all_ads_id = adsforwp_get_group_ad_ids(); if ( ! empty( $all_ads_id ) ) { foreach ( $all_ads_id as $ad_id ) { $wheretodisplay = get_post_meta( $ad_id, $key = 'wheretodisplay', true ); if ( $wheretodisplay == 'custom_target' ) { $ad_status = $service->adsforwp_is_condition( $ad_id ); if ( $ad_status ) { $content = $this->adsforwp_get_custom_target_ad_code( $content, $ad_id, 'group' ); } } } } // For group ads ends here return $content; } /** * Function to add css globally on AMP */ public function adsforwp_global_css_for_amp() { $settings = adsforwp_defaultSettings(); ?> ins{ background: yellow; } .afw a { display:block; } adsforwp_get_ad_code( $ad_id, $type = 'AD' ); if ( $ad_code ) { $output_service = new Adsforwp_Output_Service(); $output_service->adsforwp_enque_amp_floating_ad_css( $ad_id ); } } } } } /** * Function to add css for sticky ads on AMP */ public function adsforwp_add_amp_stick_ad_css() { $all_ads_id = adsforwp_get_ad_ids(); if ( ! empty( $all_ads_id ) ) { foreach ( $all_ads_id as $ad_id ) { $wheretodisplay = get_post_meta( $ad_id, $key = 'wheretodisplay', true ); if ( $wheretodisplay == 'sticky' ) { $ad_code = $this->adsforwp_get_ad_code( $ad_id, $type = 'AD' ); if ( $ad_code ) { $output_service = new Adsforwp_Output_Service(); $output_service->adsforwp_enque_amp_sticky_ad_css( $ad_id ); } } } } } public function adsforwp_update_amp_sticky_ad_status() { if ( ! isset( $_GET['adsforwp_front_nonce'] ) ) { return; } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason Validating nonce so sanitization not needed if ( ! wp_verify_nonce( $_GET['adsforwp_front_nonce'], 'adsforwp_ajax_check_front_nonce' ) ) { return; } //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash --Reason using it to set the cookie, so unslash not needed $ad_id = isset( $_GET['ad_id'] ) ? sanitize_text_field( $_GET['ad_id'] ) : ''; $cookie_data = ''; if ( ! isset( $_COOKIE['adsforwp-stick-ad-id7'] ) && $_COOKIE['adsforwp-stick-ad-id7'] == '' ) { $cookie_data .= esc_attr( $ad_id ); } else { $cookie_data .= ',' . esc_attr( $ad_id ); } setcookie( 'adsforwp-stick-ad-id7', $cookie_data, time() + ( 86400 * 15 ), '/' ); } public function adsforwp_check_amp_sticky_ad_status() { if ( ! isset( $_GET['adsforwp_front_nonce'] ) ) { return; } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason Validating nonce so sanitization not needed if ( ! wp_verify_nonce( $_GET['adsforwp_front_nonce'], 'adsforwp_ajax_check_front_nonce' ) ) { return; } //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash --Reason Using it to just fetch the data $ad_id = isset( $_GET['ad_id'] ) ? sanitize_text_field( $_GET['ad_id'] ) : ''; $common_function_obj = new Adsforwp_Admin_Common_Functions(); $in_group = $common_function_obj->adsforwp_check_ads_in_group( $ad_id ); if ( isset( $_COOKIE['adsforwp-stick-ad-id7'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized --Reason Using it for condition check $ad_id_list = $_COOKIE['adsforwp-stick-ad-id7']; $explod_ad_id = explode( ',', $ad_id_list ); } $wheretodisplay = get_post_meta( $ad_id, $key = 'wheretodisplay', true ); if ( get_post_type( $ad_id ) == 'adsforwp-groups' ) { if ( $wheretodisplay == 'sticky' && ! in_array( $ad_id, $explod_ad_id ) ) { echo wp_json_encode( array( 'showNotification' => true ) ); } else { echo wp_json_encode( array( 'showNotification' => false ) ); } } elseif ( $wheretodisplay == 'sticky' && ! in_array( $ad_id, $explod_ad_id ) && empty( $in_group ) ) { echo wp_json_encode( array( 'showNotification' => true ) ); } else { echo wp_json_encode( array( 'showNotification' => false ) ); } wp_die(); } public function adsforwp_display_sticky_ads_amp() { // Ads stick starts here $all_ads_id = adsforwp_get_ad_ids(); if ( ! empty( $all_ads_id ) ) { foreach ( $all_ads_id as $ad_id ) { $wheretodisplay = get_post_meta( $ad_id, $key = 'wheretodisplay', true ); if ( $wheretodisplay == 'sticky' ) { $ad_code_escaped = $this->adsforwp_get_ad_code( $ad_id, $type = 'AD' ); if ( $ad_code_escaped ) { ?>
adsforwp_group_ads( $atts = null, $ad_id ); if ( $ad_code_escaped ) { ?>
adsforwp_check_ads_in_group( $ad_id ); if ( empty( $in_group ) ) { $ad_code_escaped .= $this->adsforwp_get_ad_code( $ad_id, $type = 'AD' ); } } } } if ( $ad_code_escaped ) { ?>