: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
if ( ! defined( 'ABSPATH' ) ) {
Class AMPforWP_theme_mode{
public function __construct(){
add_action( 'init', array("AMPforWP_theme_mode", 'removeUnusedAction'));
add_action( 'init', array("AMPforWP_theme_mode", 'removeUnusedMenuWidgets'), 11);
if(!is_admin() && $GLOBALS['pagenow'] !== 'wp-login.php' && (!is_preview() && !isset($_GET['elementor-preview'])) ){
add_action( 'init', array($this, 'rm_wp_core'), 20 );
add_filter("ampforwp_is_amp_endpoint", array($this, 'ampforwp_theme_mode_enable'));
add_action( 'init', array($this, 'dynamic_sidebar_callback_bkp') );
add_filter( 'amp_post_template_dir', array($this, 'template_mode_new_dir') );
add_filter( 'amp_post_template_file', array($this, 'custom_template_file_name'), 11, 3 );
add_filter( "post_thumbnail_html", array($this, 'featured_image_html'), 11, 5 );
add_filter("language_attributes", array($this, 'add_amp_html_attr'), 10, 2);
add_filter( "the_content", array($this, 'amp_the_content') );
add_action( "levelup_head", array($this, 'amp_head_content') );
add_action( "levelup_css", array($this, 'amp_head_css') );
add_filter( "get_search_form", array($this, 'search_form'), 99 );
add_filter( "get_custom_logo", array($this, 'get_custom_logo'), 10, 2 );
add_action( "levelup_footer", array($this, 'amp_footer_content') );
add_action( "get_avatar", array($this,'get_avatar'), 11,6);
add_filter( "the_author_description", array($this, 'author_meta_desctiption_amp'),10, 2 );
add_filter("ampforwp_the_content_last_filter", array($this, "comment_form_conversion") );
add_filter("amp_post_template_data", array($this, 'amp_comment_mustache_script'), 11 );
if( is_user_logged_in() ){
$pref = get_user_option( "show_admin_bar_front", get_current_user_id() );
add_action("wp_body_open", function(){
add_action( 'admin_bar_init', array($this,'init_admin_bar'));
add_action( 'wp_before_admin_bar_render', function(){
remove_action( 'wp_before_admin_bar_render', 'wp_customize_support_script' );
add_action( 'admin_bar_menu', array($this, 'remove_adminbar_search'));
require_once AMPFORWP_PLUGIN_DIR.'/templates/template-mode/admin-settings.php';
add_filter( 'plugin_action_links_accelerated-mobile-pages/accelerated-moblie-pages.php', array($this, 'ampforwp_plugin_settings_link'), 999, 4 );
add_action( 'wp_ajax_amp_theme_ajaxcomments', array($this, 'amp_theme_ajaxcomments') );
add_action( 'wp_ajax_nopriv_amp_theme_ajaxcomments', array($this, 'amp_theme_ajaxcomments') );
static function removeUnusedMenuWidgets(){
unregister_nav_menu( 'amp-menu' );
unregister_nav_menu( 'amp-footer-menu' );
unregister_sidebar('ampforwp-above-loop');
unregister_sidebar('ampforwp-below-loop');
unregister_sidebar('ampforwp-below-header');
unregister_sidebar('ampforwp-above-footer');
unregister_sidebar('swift-footer-widget-area');
unregister_sidebar('swift-sidebar');
public static function removeUnusedAction(){
remove_action( 'init', 'ampforwp_menu', 10 );
remove_action( 'init', 'ampforwp_footermenu', 10 );
remove_action( 'init', 'swifttheme_footer_widgets_init', 10 );
remove_action( 'init', 'ampforwp_add_widget_support', 10);
remove_action( 'template_redirect', 'ampforwp_redirection', 10 );
remove_action( 'template_redirect', 'ampforwp_check_amp_page_status', 10 );
remove_action( 'template_redirect', 'ampforwp_page_template_redirect', 10 );
remove_action( 'template_redirect', 'ampforwp_page_template_redirect_archive', 10 );
remove_filter( 'query_vars', 'ampforwp_custom_query_var' );
remove_action( 'template_redirect', 'ampforwp_redirect_to_orginal_url' );
remove_action('template_redirect', 'ampforwp_redirect_proper_qendpoint' );
remove_action('get_search_form', 'ampforwp_search_form' );
remove_action( 'init', 'ampforwp_add_custom_post_support',11);
remove_action( 'init', 'ampforwp_add_custom_rewrite_rules', 25 );
remove_action( 'init', 'ampforwp_custom_rewrite_rules_for_product_category' );
function ampforwp_theme_mode_enable($opt){
function ampforwp_plugin_settings_link( $actions, $plugin_file, $plugin_data, $context ) {
$amp_activate = array('<span style="color:black;">'.esc_html__("Status: Template Mode", "accelerated-mobile-pages").'</span> |');
$actions = array_merge( $actions, $amp_activate );
function amp_comment_mustache_script($data){
if(isset($data['amp_component_scripts']['amp-next-page'])){
unset($data['amp_component_scripts']['amp-next-page']);
if ( empty( $data['amp_component_scripts']['amp-mustache'] ) ) {
$data['amp_component_scripts']['amp-mustache'] = 'https://cdn.ampproject.org/v0/amp-mustache-latest.js';
if ( empty( $data['amp_component_scripts']['amp-form'] ) ) {
$data['amp_component_scripts']['amp-form'] = 'https://cdn.ampproject.org/v0/amp-form-latest.js';
unset($data['amp_component_scripts']['amp-addthis']);
function amp_theme_ajaxcomments(){
global $redux_builder_amp;
header("access-control-allow-credentials:true");
header("access-control-allow-headers:Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token");
header("Access-Control-Allow-Origin:".esc_attr($_SERVER['HTTP_ORIGIN']) );
$siteUrl = parse_url( get_site_url() );
header("AMP-Access-Control-Allow-Source-Origin:".esc_attr($siteUrl['scheme']) . '://' . esc_attr($siteUrl['host']) );
header("access-control-expose-headers:AMP-Access-Control-Allow-Source-Origin");
header("Content-Type:application/json;charset=utf-8");
if(wp_verify_nonce($_POST['amp_comment_form_nonce'], 'commentform_submission')){
$comment_status = array('response' => 'Nonce not verified' );
echo wp_json_encode($comment_status);
$comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
$text_data = 'Comment Successfully submitted';
if ($redux_builder_amp['amp-comments-Successful-message']){
$text_data = esc_html__( $redux_builder_amp['amp-comments-Successful-message'], 'accelerated-mobile-pages');
if ( is_wp_error( $comment ) ) {
$error_data = intval( $comment->get_error_data() );
if ( ! empty( $error_data ) ) {
$comment_html = $comment->get_error_message();
$comment_html = str_replace("’","'",$comment_html);
$comment_status = array('response' => $comment_html );
echo wp_json_encode($comment_status);
wp_die( 'Unknown error' );
$user = wp_get_current_user();
do_action('set_comment_cookies', $comment, $user);
$comment_parent = $comment->comment_parent;
while( $comment_parent ){
$parent_comment = get_comment( $comment_parent );
$comment_parent = $parent_comment->comment_parent;
$GLOBALS['comment'] = $comment;
$GLOBALS['comment_depth'] = $comment_depth;
$comment_html = $text_data;
$comment_status = array('response' => $comment_html );
echo wp_json_encode($comment_status);
function comment_form_conversion($content){
if(strpos($content, 'id="commentform"')!==false){
$submit_url = admin_url('admin-ajax.php?action=amp_theme_ajaxcomments');
$submit_url = str_replace("http:", "", $submit_url);
$comment_nonce = wp_create_nonce( 'commentform_submission' );
$mustache = '<input type="hidden" name="amp_comment_form_nonce" value="'.esc_attr($comment_nonce).'"><div submit-success>
<template type="amp-mustache">
<template type="amp-mustache">
if(preg_match('/method\s*=\s*"\s*get\s*"/', $content)){
$content = preg_replace("/<form(.*?)action=[\"|'](.*?)[\"|'](.*?)id=[\"|']commentform[\"|'](.*?)>/s", '<form$1action="'.esc_url_raw($submit_url).'"$3id="commentform" $4 on="submit-success:commentform.reset()">'.$mustache, $content);
if(preg_match('/method\s*=\s*"\s*post\s*"/', $content)){
$content = preg_replace("/<form action=[\"|'](.*?)[\"|'](.*?)id=[\"|']commentform[\"|'](.*?)>/s", '<form action-xhr="$1"$2id="commentform" $3 on="submit-success:commentform.reset()">'.$mustache, $content);
* Remove Unwanted hooks from template
public function rm_wp_core(){
remove_all_actions( 'wp_head' );
remove_all_actions( 'wp_print_styles' );
remove_all_actions( 'wp_print_head_scripts' );
remove_all_actions( 'wp_footer' );
remove_all_actions( 'wp_enqueue_scripts' );
remove_all_actions( 'after_wp_tiny_mce' );
remove_all_actions( 'comment_form' );
remove_action( 'amp_post_template_head','ampforwp_add_meta_viewport', 9);
remove_filter( 'amp_post_template_file', 'ampforwp_custom_template', 10, 3 );
remove_filter( 'ampforwp_post_template_data', 'ampforwp_backtotop' );
remove_filter( 'amp_post_template_data', 'ampforwp_backtotop' );
remove_filter('amp_post_template_dir','ampforwp_new_dir');
remove_filter( 'amp_post_template_file', 'ampforwp_empty_filter', 10, 3 );
* Replacing callback of widgets for conversion
public function dynamic_sidebar_callback_bkp(){
global $wp_registered_widgets, $ampforwp_wp_registered_widgets;
if(count($wp_registered_widgets)){
foreach ($wp_registered_widgets as $key => $value) {
$wp_registered_widgets[$key]['amp_callback'] = $value['callback'];
$wp_registered_widgets[$key]['amp_params'] = $value['params'];
$wp_registered_widgets[$key]['params'] = array(array('public function_name'=> $value['id'] ));
$wp_registered_widgets[$key]['callback'] = array($this, 'ampforwp_template_mode_reg_callback_widgets');
* Change Templates directory for AMP theme
public function template_mode_new_dir( $dir ) {
global $redux_builder_amp;
$dir = get_template_directory()."/";
* Calling name of template from theme directory
public function custom_template_file_name( $file, $type, $post ) {
global $redux_builder_amp;
if( 'single' === $type && is_404() ) {
$file = get_template_directory() . '/404.php';
if( 'single' === $type && ! ('product' === $post->post_type) ) {
$file = get_template_directory() . '/page.php';
if ( 'loop' === $type ) {
$file = get_template_directory() . '/loop.php';
if ( 'single' === $type ) {
$file = get_template_directory() . '/archive.php';
$ampforwp_custom_post_page = ampforwp_custom_post_page();
if ( 'single' === $type ) {
$file = get_template_directory() . '/index.php';
if ( $redux_builder_amp['amp-frontpage-select-option'] == 1 ) {
$file = get_template_directory() . '/page.php';
if ( ampforwp_is_blog() ) {
$file = get_template_directory() . '/index.php';
if ( 'single' === $type ) {
$file = get_template_directory() . '/search.php';
$term = get_queried_object();
if ( ! empty( $term->slug ) ) {
$taxonomy = $term->taxonomy;
$slug_decoded = urldecode( $term->slug );
if ( $slug_decoded !== $term->slug ) {
$templates[] = get_template_directory() . "/taxonomy-$taxonomy-{$slug_decoded}.php";
$templates[] = get_template_directory() . "/taxonomy-$taxonomy-{$term->slug}.php";
$templates[] = get_template_directory() . "/taxonomy-$taxonomy.php";
$templates[] = get_template_directory() . "/taxonomy.php";
foreach ( $templates as $key => $value ) {
if ( 'single' === $type && file_exists($value) ) {
$category = get_queried_object();
if ( ! empty( $category->slug ) ) {
$slug_decoded = urldecode( $category->slug );
if ( $slug_decoded !== $category->slug ) {
$templates[] = get_template_directory() . "/category-{$slug_decoded}.php";
$templates[] = get_template_directory() . "/category-{$category->slug}.php";
$templates[] = get_template_directory() . "/category-{$category->term_id}.php";
$templates[] = get_template_directory() . '/category.php';
foreach ( $templates as $key => $value ) {
if ( 'single' === $type && file_exists($value) ) {
$tag = get_queried_object();
if ( ! empty( $tag->slug ) ) {
$slug_decoded = urldecode( $tag->slug );
if ( $slug_decoded !== $tag->slug ) {
$templates[] = get_template_directory() . "/tag-{$slug_decoded}.php";
$templates[] = get_template_directory() . "/tag-{$tag->slug}.php";
$templates[] = get_template_directory() . "/tag-{$tag->term_id}.php";
$templates[] = get_template_directory() . '/tag.php';
foreach ( $templates as $key => $value ) {
if ( 'single' === $type && file_exists($value) ) {
$author = get_queried_object();
if ( $author instanceof WP_User ) {
$templates[] = get_template_directory() . "/author-{$author->user_nicename}.php";
$templates[] = get_template_directory() . "/author-{$author->ID}.php";
$templates[] = get_template_directory() . "/author.php";
foreach ( $templates as $key => $value ) {
if ( 'single' === $type && file_exists($value) ) {
$post_types = array_filter( (array) get_query_var( 'post_type' ) );
if ( count( $post_types ) == 1 ) {
$post_type = reset( $post_types );
$templates[] = get_template_directory() . "/archive-{$post_type}.php";
$templates[] = get_template_directory() . '/archive.php';
foreach ( $templates as $key => $value ) {
if ( 'single' === $type && file_exists($value) ) {
case (is_post_type_archive()):
$post_type = get_query_var( 'post_type' );
if ( is_array( $post_type ) )
$post_type = reset( $post_type );
$obj = get_post_type_object( $post_type );
if ( ! ($obj instanceof WP_Post_Type) || ! $obj->has_archive ) {
$post_types = array_filter( (array) get_query_var( 'post_type' ) );
if ( count( $post_types ) == 1 ) {
$post_type = reset( $post_types );
$templates[] = get_template_directory() . "/archive-{$post_type}.php";
$templates[] = get_template_directory() . '/archive.php';
foreach ( $templates as $key => $value ) {
if ( 'single' === $type && file_exists($value) ) {
$object = get_queried_object();
if ( ! empty( $object->post_type ) ) {
$template = get_page_template_slug( $object );
if ( $template && 0 === validate_file( $template ) ) {
$templates[] = get_template_directory().'/'.$template;
$name_decoded = urldecode( $object->post_name );
if ( $name_decoded !== $object->post_name ) {
$templates[] = get_template_directory() . "/single-{$object->post_type}-{$name_decoded}.php";
$templates[] = get_template_directory() . "/single-{$object->post_type}-{$object->post_name}.php";
$templates[] = get_template_directory() . "/single-{$object->post_type}.php";
$templates[] = get_template_directory() . "/single.php";
foreach ( $templates as $key => $value ) {
if ( 'single' === $type && file_exists($value) ) {
$id = get_queried_object_id();
$template = get_page_template_slug();
$pagename = get_query_var('pagename');
if ( ! $pagename && $id ) {
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
$post = get_queried_object();
$pagename = $post->post_name;
if ( $template && 0 === validate_file( $template ) )
$templates[] = get_template_directory().'/'.$template;
$pagename_decoded = urldecode( $pagename );
if ( $pagename_decoded !== $pagename ) {
$templates[] = get_template_directory() . "/page-{$pagename_decoded}.php";
$templates[] = get_template_directory() . "/page-{$pagename}.php";
$templates[] = get_template_directory() . "/page-{$id}.php";
$templates[] = get_template_directory() . "/page.php";
foreach ( $templates as $key => $value ) {
if ( 'single' === $type && file_exists($value) ) {
// Polylang Frontpage #1779
if ( 'single' === $type && ampforwp_polylang_front_page() && true == $redux_builder_amp['amp-frontpage-select-option'] ) {
$file = get_template_directory() . '/page.php';
* Sanitize featured image
public function featured_image_html($html, $post_id, $post_thumbnail_id, $size, $attr ){
$html = $this->ampforwp_template_mode_cnt_sanitizer($html);
public function add_amp_html_attr($output, $doctype){
public function amp_the_content($content){
global $ampforwpTemplate;
if(empty($ampforwpTemplate)){ return $content; }
$new_content = $ampforwpTemplate->get( 'post_amp_content' );
$new_content = apply_filters("ampforwp_template_mode_the_content", $new_content);
public function amp_head_content(){
global $ampforwpTemplate;
$amp_url = get_permalink( get_queried_object_id() );
printf( '<link rel="amphtml" href="%s" />', esc_url( $amp_url ) );
do_action( 'amp_meta', $ampforwpTemplate );
do_action( 'amp_post_template_head', $ampforwpTemplate );
public function amp_head_css(){
global $ampforwpTemplate, $redux_builder_amp;
$stylesheetUri = get_stylesheet_uri();
echo "<style amp-custom>";
do_action( 'amp_post_template_css', $ampforwpTemplate );
do_action( 'amp_css', $ampforwpTemplate );
$stylesheetCss = $this->ampforwp_get_remote_content($stylesheetUri);
$stylesheetCss = str_replace(" img", 'amp-img', $stylesheetCss);
$valuesrc = get_stylesheet_directory_uri();
$stylesheetCss = preg_replace_callback('/url[(](.*?)[)]/', function($matches)use($valuesrc){
$matches[1] = str_replace(array('"', "'"), array('', ''), $matches[1]);
if(!wp_http_validate_url($matches[1]) && strpos($matches[1],"data:")===false){
return 'url('.$valuesrc."/".$matches[1].")";
$css .= ampforwp_get_setting('css_editor');
$css = str_replace(array('.accordion-mod'), array('.apac'), $css);
if( is_user_logged_in() ){
$pref = get_user_option( "show_admin_bar_front", get_current_user_id() );
$css .= $this->ampforwp_get_remote_content(AMPFORWP_PLUGIN_DIR_URI."/templates/template-mode/admin-bar.css");
echo $this->css_sanitizer($css); // sanitized above