'0.4.2' ) { return require AMPFORWP_PLUGIN_DIR .'templates/customizer/customizer-new.php' ; } else { return require AMPFORWP_PLUGIN_DIR .'templates/customizer/customizer.php' ; } } else { return require AMPFORWP_PLUGIN_DIR .'templates/customizer/customizer.php' ; } } } ampforwp_include_customizer_files(); //0. define('AMPFORWP_COMMENTS_PER_PAGE', ampforwp_define_comments_number() ); // Define number of comments function ampforwp_define_comments_number(){ global $redux_builder_amp; $number_of_comments = ''; if(isset($redux_builder_amp['ampforwp-number-of-comments'])){ $number_of_comments = $redux_builder_amp['ampforwp-number-of-comments']; } return $number_of_comments; } // 1. Add Home REL canonical // Add AMP rel-canonical for home and archive pages add_action('amp_init','ampforwp_allow_homepage'); function ampforwp_allow_homepage() { add_action( 'wp', 'ampforwp_add_endpoint_actions' ); } function ampforwp_add_endpoint_actions() { $ampforwp_is_amp_endpoint = ampforwp_is_amp_endpoint(); if ( $ampforwp_is_amp_endpoint ) { amp_prepare_render(); } else { add_action( 'wp_head', 'ampforwp_home_archive_rel_canonical', 1 ); } $cpage_var = get_query_var('cpage'); if ( $cpage_var >= 1) : remove_action( 'wp_head', 'ampforwp_home_archive_rel_canonical', 1 ); endif; } function ampforwp_amphtml_generator(){ global $redux_builder_amp; global $wp, $post; $post_id = ''; $endpoint_check = false; $endpoint_check = $redux_builder_amp['amp-core-end-point']; if( is_attachment() ) { return; } if( is_home() && is_front_page() && !$redux_builder_amp['ampforwp-homepage-on-off-support'] ) { return; } if( is_front_page() && ! $redux_builder_amp['ampforwp-homepage-on-off-support'] ) { return; } if ( is_archive() && !$redux_builder_amp['ampforwp-archive-support'] ) { return; } // #1192 Password Protected posts exclusion if(post_password_required( $post )){ return; } // #2018 404 exclusion if(is_404()){ return; } // #1443 AMP should be skip on the check out page if(class_exists( 'WooCommerce' )){ if(function_exists('is_checkout') && is_checkout()){ return; } } // no-amphtml for search if(is_search()){ return; } // #872 no-amphtml if selected as hide from settings if(is_archive() && $redux_builder_amp['ampforwp-archive-support']){ if(is_tag() && is_array($redux_builder_amp['hide-amp-tags-bulk-option'])) { $all_tags = get_the_tags(); $tagsOnPost = array(); if ( $all_tags ) { foreach ($all_tags as $tagskey => $tagsvalue) { $tagsOnPost[] = $tagsvalue->term_id; } } $get_tags_checkbox = array_keys(array_filter($redux_builder_amp['hide-amp-tags-bulk-option'])); if( count(array_intersect($get_tags_checkbox,$tagsOnPost))>0 ){ return; } }//tags area closed $selected_cats = $current_cats_ids = array(); $get_categories_from_checkbox = $redux_builder_amp['hide-amp-categories']; // Check if $get_categories_from_checkbox has some cats then only show if ( $get_categories_from_checkbox ) { $get_selected_cats = array_filter($get_categories_from_checkbox); foreach ($get_selected_cats as $key => $value) { $selected_cats[] = $key; } $current_cats = get_the_category(get_the_ID()); if ( $current_cats ) { foreach ($current_cats as $key => $cats) { $current_cats_ids[] =$cats->cat_ID; } if( count(array_intersect($selected_cats,$current_cats_ids))>0 ){ return; } } } } if ( is_page() && ! $redux_builder_amp['amp-on-off-for-all-pages'] && ! is_home() && ! is_front_page() ) { return; } if ( is_home() && ! ampforwp_is_blog() && !$redux_builder_amp['ampforwp-homepage-on-off-support'] ) { return; } if ( ampforwp_is_blog() && ! $redux_builder_amp['amp-on-off-for-all-pages'] ) { return; } $query_arg_array = $wp->query_vars; if( in_array( "cpage" , $query_arg_array ) ) { if( is_front_page() && $wp->query_vars['cpage'] >= '2' ) { return; } if( is_singular() && $wp->query_vars['cpage'] >= '2' ) { return; } } if ( is_home() || is_front_page() || is_archive() ){ global $wp; $current_archive_url = home_url( $wp->request ); $amp_url = trailingslashit($current_archive_url); } else { $amp_url = amp_get_permalink( get_queried_object_id() ); } global $post; if ( is_singular() ) { $post_id = get_the_ID(); } if ( ampforwp_is_blog() ) { $post_id = ampforwp_get_blog_details('id'); } $ampforwp_amp_post_on_off_meta = get_post_meta( $post_id,'ampforwp-amp-on-off',true); if ( ( is_singular() || ampforwp_is_blog() ) && $ampforwp_amp_post_on_off_meta === 'hide-amp' ) { //dont Echo anything } else { $supported_types = ampforwp_get_all_post_types(); $supported_types = apply_filters('get_amp_supported_post_types',$supported_types); $type = get_post_type(); if(is_home() || is_front_page()){ if(isset($redux_builder_amp['ampforwp-homepage-on-off-support']) && $redux_builder_amp['ampforwp-homepage-on-off-support'] == 1 && isset($redux_builder_amp['amp-on-off-for-all-posts']) && $redux_builder_amp['amp-on-off-for-all-posts'] == 0 && isset($redux_builder_amp['amp-on-off-for-all-pages']) && $redux_builder_amp['amp-on-off-for-all-pages'] == 0 ){ $supported_types['post'] = 'post'; } } $supported_amp_post_types = in_array( $type , $supported_types ); $query_arg_array = $wp->query_vars; if( array_key_exists( 'paged' , $query_arg_array ) ) { if ( (is_home() || is_archive()) && $wp->query_vars['paged'] >= '2' ) { $new_url = home_url('/'); $category_path = $wp->request; if ( null != $category_path && true != $endpoint_check) { $explode_path = explode("/",$category_path); $inserted = array(AMPFORWP_AMP_QUERY_VAR); array_splice( $explode_path, -2, 0, $inserted ); $impode_url = implode('/', $explode_path); $amp_url = $new_url . $impode_url ; } } if( is_search() && $wp->query_vars['paged'] >= '2' ) { $current_search_url =trailingslashit(get_home_url()) . $wp->request .'/'."?amp=1&s=".get_search_query(); } } $amp_url = user_trailingslashit($amp_url); if( is_search() ) { $current_search_url =trailingslashit(get_home_url())."?amp=1&s=".get_search_query(); $amp_url = untrailingslashit($current_search_url); } include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); if( get_option('permalink_structure') && is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' )){ global $sitepress_settings, $wp; if($sitepress_settings[ 'language_negotiation_type' ] == 3){ if( is_singular() ){ $active_langs = $sitepress_settings['active_languages']; $found == ''; $wpml_url =get_permalink( get_queried_object_id() ); $untrail_wpml_url = untrailingslashit($wpml_url); $explode_url = explode('/', $untrail_wpml_url); $append_amp = 'amp'; foreach ($active_langs as $active_lang) { foreach($explode_url as $a) { if (stripos('?lang='.$active_lang ,$a) !== false){ $amp_url = add_query_arg('amp','1',$wpml_url); $found = 'found'; break 2; } } } if($found == ''){ array_splice( $explode_url, count($explode_url), 0, $append_amp ); $impode_url = implode('/', $explode_url); $amp_url = untrailingslashit($impode_url); } } if ( is_home() || is_archive() ){ global $wp; $current_archive_url = home_url( $wp->request ); $explode_path = explode("/",$current_archive_url); $inserted = array(AMPFORWP_AMP_QUERY_VAR); $query_arg_array = $wp->query_vars; if( array_key_exists( 'paged' , $query_arg_array ) ) { $active_langs = $sitepress_settings['active_languages']; $found = ''; foreach ($active_langs as $active_lang) { foreach($explode_path as $a) { if (stripos('?lang='.$active_lang ,$a) !== false){ $amp_url = add_query_arg('amp','1',$current_archive_url); $found = 'found'; break 2; } } } if($found == ''){ array_splice( $explode_path, count($explode_path), 0, $inserted ); $impode_url = implode('/', $explode_path); $amp_url = $impode_url; } } else{ $active_langs = $sitepress_settings['active_languages']; $found = ''; foreach ($active_langs as $active_lang) { foreach($explode_path as $a) { if (stripos('?lang='.$active_lang ,$a) !== false){ $amp_url = add_query_arg('amp','1',$current_archive_url); $found = 'found'; break 2; } } } if($found == ''){ array_splice( $explode_path, count($explode_path), 0, $inserted ); $impode_url = implode('/', $explode_path); $amp_url = $impode_url; } } } } } // URL Purifier $amp_url = ampforwp_url_purifier($amp_url); $amp_url = apply_filters('ampforwp_modify_rel_canonical',$amp_url); if( $supported_amp_post_types) { return $amp_url; } } return; } // AMPHTML when using custom page and then creating a blog page add_action('amp_init','ampforwp_allow_homepage_as_blog'); function ampforwp_allow_homepage_as_blog() { add_action( 'wp', 'ampforwp_static_blog' , 11 ); } function ampforwp_static_blog(){ global $page; if ( ampforwp_is_front_page() && $page >= 2 && is_page() ) { add_filter('ampforwp_modify_rel_canonical','ampforwp_modify_amphtml_static_blog'); } } function ampforwp_modify_amphtml_static_blog($amp_url) { $explode_url = $amp_endpoint = $offset = ""; $explode_url = explode('/', $amp_url); $explode_url = array_flip($explode_url); unset($explode_url['amp']); $explode_url = array_flip($explode_url); $amp_endpoint = array('amp'); $offset = count($explode_url) - 2; array_splice( $explode_url, $offset, 0, $amp_endpoint ); $amp_url = implode('/', $explode_url); return $amp_url; } function ampforwp_home_archive_rel_canonical() { $amp_url = ""; $amp_url = ampforwp_amphtml_generator(); if ( $amp_url ) { printf('', esc_url($amp_url)); } } //end of ampforwp_home_archive_rel_canonical() // Remove default wordpress rel canonical add_filter('amp_frontend_show_canonical','ampforwp_remove_default_canonical'); if (! function_exists('ampforwp_remove_default_canonical') ) { function ampforwp_remove_default_canonical() { return false; } } // 2. Custom Design // Add Homepage AMP file code add_filter( 'amp_post_template_file', 'ampforwp_custom_template', 10, 3 ); function ampforwp_custom_template( $file, $type, $post ) { global $redux_builder_amp; // Custom Homepage and Archive file $slug = array(); $current_url_in_pieces = array(); $ampforwp_custom_post_page = ampforwp_custom_post_page(); if ( 'single' === $type ) { // Homepage and FrontPage if ( is_home() || ( true == $redux_builder_amp['ampforwp-amp-takeover'] && is_front_page() ) ) { $file = AMPFORWP_PLUGIN_DIR . '/templates/design-manager/design-'. ampforwp_design_selector() .'/index.php'; } if ( ampforwp_is_blog() ) { $file = AMPFORWP_PLUGIN_DIR . '/templates/design-manager/design-'. ampforwp_design_selector() .'/index.php'; } if ( ampforwp_is_front_page() || ( true == $redux_builder_amp['ampforwp-amp-takeover'] && is_front_page() && $redux_builder_amp['amp-frontpage-select-option']) ) { $file = AMPFORWP_PLUGIN_DIR . '/templates/design-manager/design-'. ampforwp_design_selector() .'/frontpage.php'; } // Archive Pages if ( is_archive() && $redux_builder_amp['ampforwp-archive-support'] && 'single' === $type ) { $file = AMPFORWP_PLUGIN_DIR . '/templates/design-manager/design-'. ampforwp_design_selector() .'/archive.php'; } // Search pages if ( is_search() ) { $file = AMPFORWP_PLUGIN_DIR . '/templates/design-manager/design-'. ampforwp_design_selector() .'/search.php'; } // 404 Pages #2042 if ( is_404() && 'single' === $type ) { add_filter('ampforwp_modify_rel_url','ampforwp_404_canonical'); $file = AMPFORWP_PLUGIN_DIR . '/templates/design-manager/design-'. ampforwp_design_selector() .'/404.php'; } } // Polylang compatibility // For Frontpage if ( 'single' === $type && ampforwp_polylang_front_page() && true == $redux_builder_amp['amp-frontpage-select-option'] ) { $file = AMPFORWP_PLUGIN_DIR . '/templates/design-manager/design-'. ampforwp_design_selector() .'/frontpage.php'; } return $file; } add_filter('amp_post_template_dir','ampforwp_new_dir'); function ampforwp_new_dir( $dir ) { global $redux_builder_amp; if ( 1 == $redux_builder_amp['amp-design-selector'] || 2 == $redux_builder_amp['amp-design-selector'] || 3 == $redux_builder_amp['amp-design-selector'] ) { $dir = AMPFORWP_PLUGIN_DIR . '/templates/design-manager/design-'. ampforwp_design_selector(); } else { $dir = AMPFORWP_CUSTOM_THEME; } return $dir; } //3.5 add_filter( 'amp_post_template_file', 'ampforwp_empty_filter', 10, 3 ); function ampforwp_empty_filter( $file, $type, $post ) { if ( 'empty-filter' === $type ) { $file = AMPFORWP_PLUGIN_DIR . '/templates/design-manager/empty-filter.php'; } return $file; } // 4. Custom Header files add_filter( 'amp_post_template_file', 'ampforwp_custom_header', 10, 3 ); function ampforwp_custom_header( $file, $type, $post ) { if ( 'header-bar' === $type ) { $file = AMPFORWP_PLUGIN_DIR . '/templates/design-manager/design-'. ampforwp_design_selector() .'/header-bar.php'; } return $file; } // 4.1 Custom Meta-Author files add_filter( 'amp_post_template_file', 'ampforwp_set_custom_meta_author', 10, 3 ); function ampforwp_set_custom_meta_author( $file, $type, $post ) { if ( 'meta-author' === $type ) { $file = AMPFORWP_PLUGIN_DIR . '/templates/design-manager/empty-filter.php'; } return $file; } // 4.2 Custom Meta-Taxonomy files add_filter( 'amp_post_template_file', 'ampforwp_set_custom_meta_taxonomy', 10, 3 ); function ampforwp_set_custom_meta_taxonomy( $file, $type, $post ) { if ( 'meta-taxonomy' === $type ) { $file = AMPFORWP_PLUGIN_DIR . 'templates/design-manager/empty-filter.php'; } return $file; } // 5. Customize with Width of the site add_filter( 'amp_content_max_width', 'ampforwp_change_content_width' ); function ampforwp_change_content_width( $content_max_width ) { return 1000; } // 6. Add required Javascripts for extra AMP features // Code updated and added the JS proper way #336 add_filter('amp_post_template_data','ampforwp_add_amp_social_share_script', 20); function ampforwp_add_amp_social_share_script( $data ){ global $redux_builder_amp; $social_check = $social_check_page = false; if ( is_page() && isset($redux_builder_amp['ampforwp-page-social']) && $redux_builder_amp['ampforwp-page-social'] ) { $social_check_page = true; } if ( '4' === $redux_builder_amp['amp-design-selector'] ) { $social_check = true; } if ( '4' !== $redux_builder_amp['amp-design-selector'] && defined('AMPFORWP_DM_SOCIAL_CHECK') && 'true' === AMPFORWP_DM_SOCIAL_CHECK ) { $social_check = true; } if( $redux_builder_amp['enable-single-social-icons'] == true || defined('AMPFORWP_DM_SOCIAL_CHECK') && AMPFORWP_DM_SOCIAL_CHECK === 'true' ) { if( (is_singular() || $social_check_page ) && is_socialshare_or_socialsticky_enabled_in_ampforwp() ) { if ( empty( $data['amp_component_scripts']['amp-social-share'] ) ) { $data['amp_component_scripts']['amp-social-share'] = 'https://cdn.ampproject.org/v0/amp-social-share-0.1.js'; } } } // Facebook Like Script if( true == $redux_builder_amp['ampforwp-facebook-like-button'] && (is_single() || $social_check_page ) && $social_check && (! checkAMPforPageBuilderStatus( get_the_ID() ) ) ){ if(empty($data['amp_component_scripts']['amp-facebook-like'])){ $data['amp_component_scripts']['amp-facebook-like'] = 'https://cdn.ampproject.org/v0/amp-facebook-like-0.1.js'; } } return $data; } // 6.1 Adding Analytics Scripts add_filter('amp_post_template_data','ampforwp_register_analytics_script', 20); function ampforwp_register_analytics_script( $data ){ global $redux_builder_amp; if( true == $redux_builder_amp['ampforwp-ga-switch'] || true == $redux_builder_amp['ampforwp-Segment-switch'] || true == $redux_builder_amp['ampforwp-Quantcast-switch'] || true == $redux_builder_amp['ampforwp-comScore-switch'] || true == $redux_builder_amp['ampforwp-Yandex-switch'] || true == $redux_builder_amp['ampforwp-Chartbeat-switch']|| true == $redux_builder_amp['ampforwp-Alexa-switch'] || true == $redux_builder_amp['ampforwp-afs-analytics-switch'] || true == $redux_builder_amp['ampforwp-adobe-analytics-switch'] ) { if ( empty( $data['amp_component_scripts']['amp-analytics'] ) ) { $data['amp_component_scripts']['amp-analytics'] = 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js'; } } return $data; } add_filter( 'amp_post_template_data', 'ampforwp_add_amp_related_scripts', 20 ); function ampforwp_add_amp_related_scripts( $data ) { global $redux_builder_amp; // Adding Sidebar Script if ( isset($redux_builder_amp['ampforwp-amp-menu']) && $redux_builder_amp['ampforwp-amp-menu'] && 4 != $redux_builder_amp['amp-design-selector'] ) { if ( empty( $data['amp_component_scripts']['amp-sidebar'] ) ) { $data['amp_component_scripts']['amp-sidebar'] = 'https://cdn.ampproject.org/v0/amp-sidebar-0.1.js'; } } return $data; } add_action('ampforwp_global_after_footer','ampforwp_footer'); function ampforwp_footer() { global $redux_builder_amp; ?>