Last active
May 25, 2019 15:30
-
-
Save wpmudev-sls/43840a91b74e63fe4ddd4d1169953b99 to your computer and use it in GitHub Desktop.
[SmartCrawl] Add support seo meta for Extra Theme layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [SmartCrawl] Add support seo meta for Extra Theme layout | |
* Description: [SmartCrawl] Add support seo meta for Extra Theme layout. And to avoid conflict with SmartCrawl, this MU also disable meta render function from Extra Theme (elegant_description, elegant_keywords, elegant_canonical, elegant_titles_filter) and remove SEO tab from Extra Theme options - 1124086828417629 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_smartcarwl_disable_elegant_render_meta_func', 100 ); | |
function wpmudev_smartcarwl_disable_elegant_render_meta_func(){ | |
if( ! function_exists('elegant_description') ){ | |
function elegant_description(){} | |
} | |
if( ! function_exists('elegant_keywords') ){ | |
function elegant_keywords(){} | |
} | |
if( ! function_exists('elegant_canonical') ){ | |
function elegant_canonical(){} | |
} | |
} | |
add_action( 'after_setup_theme', 'wpmudev_sc_add_support_seo_meta_for_extra_theme_layout_func', 100 ); | |
function wpmudev_sc_add_support_seo_meta_for_extra_theme_layout_func() { | |
function wpmudev_smartcrawl_is_set_home_on_extra_theme_layout(){ | |
return (boolean) ( 'layout' == get_option( 'show_on_front' ) && extra_get_home_layout_id() ); | |
} | |
if ( defined( 'SMARTCRAWL_VERSION' ) && SMARTCRAWL_VERSION | |
&& function_exists('extra_get_home_layout_id') && wpmudev_smartcrawl_is_set_home_on_extra_theme_layout() | |
&& class_exists( 'Smartcrawl_Loader' ) | |
) { | |
class WPMUDEV_MC_Extra_Theme{ | |
private $post_type; | |
private $home_page_id; | |
public function __construct(){ | |
global $shortname; | |
// init | |
$this->post_type = $this->get_layout_post_type(); | |
$this->home_page_id = extra_get_home_layout_id(); | |
// remove extra theme filter | |
remove_filter( 'pre_get_document_title', 'elegant_titles_filter' ); | |
// remove seo tab from theme option | |
add_filter( 'et_epanel_tab_names', array( $this, 'disable_seo_tab_from_extra_theme_option' ) ); | |
if ( user_can_see_seo_metabox() ) { | |
add_action( 'admin_menu', array( $this, 'add_seo_meta_for_extra_theme_layout' ) ); | |
add_action( "save_post_{$this->post_type}", array( $this, 'save_seo_meta' ), 0 ); | |
} | |
add_action( 'wp_head', array( $this, 'fix_seo_meta_for_extra_theme_layout' ), 0 ); | |
add_filter( 'post_type_link', array( $this, 'retrive_extra_theme_home_page_link'), 10, 2); | |
} | |
public function disable_extra_meta_render(){ | |
return 'off'; | |
} | |
public function disable_seo_tab_from_extra_theme_option( $tabs ){ | |
if( isset( $tabs['seo'] ) ){ | |
unset( $tabs['seo'] ); | |
} | |
return $tabs; | |
} | |
public function get_layout_post_type(){ | |
return defined('EXTRA_LAYOUT_POST_TYPE') ? EXTRA_LAYOUT_POST_TYPE : 'layout'; | |
} | |
public function add_seo_meta_for_extra_theme_layout(){ | |
add_meta_box( 'wds-wds-meta-box', __( 'SmartCrawl', 'wds' ), array( $this, 'seo_meta_boxes' ), $this->post_type, 'normal', 'high' ); | |
add_action( 'admin_print_scripts', array( $this, 'load_meta_boxes_assets' ) ); | |
if( isset( $_GET['page'] ) && Smartcrawl_Settings::TAB_ONPAGE ){ | |
add_filter( 'pre_option_page_on_front', 'extra_get_home_layout_id' ); | |
} | |
} | |
public function seo_meta_boxes($post ) { | |
return Smartcrawl_Simple_Renderer::render( 'metabox/metabox-main', array( | |
'post' => $post, | |
) ); | |
} | |
public function load_meta_boxes_assets(){ | |
wp_enqueue_script( Smartcrawl_Controller_Assets::METABOX_JS ); | |
wp_enqueue_media(); | |
wp_enqueue_style( Smartcrawl_Controller_Assets::APP_CSS ); | |
} | |
public function save_seo_meta(){ | |
if( $request_data = isset( $_POST['_wds_nonce'] ) && wp_verify_nonce( $_POST['_wds_nonce'], 'wds-metabox-nonce' ) ? stripslashes_deep( $_POST ) : array() ){ | |
$_POST['post_type'] = 'post'; // this trick will help we can use smartcrawl_save_postdata func | |
return Smartcrawl_Metabox::get()->smartcrawl_save_postdata( $request_data['post_ID'] ); | |
} | |
} | |
public function fix_seo_meta_for_extra_theme_layout(){ | |
if( is_home() && 'posts' !== get_option( 'show_on_front' ) ){ | |
add_filter( 'pre_option_page_for_posts', 'extra_get_home_layout_id' ); | |
// change canonical link | |
add_filter( 'wds_filter_canonical', array( $this, 'get_home_url' ) ); | |
} | |
} | |
public function get_home_url(){ | |
return home_url('/'); | |
} | |
public function retrive_extra_theme_home_page_link( $link, $post ){ | |
if( $post->ID === $this->home_page_id ){ | |
$link = home_url( '/' ); | |
} | |
return $link; | |
} | |
} | |
$run = new WPMUDEV_MC_Extra_Theme; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment