*/
namespace RankMath\Admin;
use CMB2_hookup;
use RankMath\KB;
use RankMath\CMB2;
use RankMath\Helper;
use RankMath\Runner;
use RankMath\Replace_Vars;
use RankMath\Traits\Hooker;
use MyThemeShop\Helpers\Str;
use MyThemeShop\Helpers\Url;
use MyThemeShop\Helpers\Param;
defined( 'ABSPATH' ) || exit;
/**
* Metabox class.
*/
class Metabox implements Runner {
use Hooker;
/**
* Metabox id.
*
* @var string
*/
private $metabox_id = 'rank_math_metabox';
/**
* Register hooks.
*/
public function hooks() {
$this->action( 'rank_math/admin/enqueue_scripts', 'enqueue' );
$this->action( 'cmb2_admin_init', 'add_main_metabox', 30 );
$this->action( 'cmb2_admin_init', 'add_link_suggestion_metabox', 30 );
$this->action( 'cmb2_' . CMB2::current_object_type() . '_process_fields_' . $this->metabox_id, 'save_meta' );
$this->action( 'cmb2_save_field', 'invalidate_facebook_object_cache', 10, 4 );
}
/**
* Enqueue styles and scripts for the metabox.
*/
public function enqueue() {
// Early bail if we're not on the valid screens or if it's WPBakery's Frontend editor.
$screen = get_current_screen();
if (
! in_array( $screen->base, array( 'post', 'term', 'profile', 'user-edit' ), true ) ||
( class_exists( 'Vc_Manager' ) && \MyThemeShop\Helpers\Param::get( 'vc_action' ) )
) {
return;
}
// Styles.
CMB2_hookup::enqueue_cmb_css();
Replace_Vars::setup_json();
wp_enqueue_style( 'rank-math-metabox', rank_math()->plugin_url() . '/assets/admin/css/metabox.css', [ 'rank-math-common', 'rank-math-cmb2' ], rank_math()->version );
// JSON data.
Helper::add_json( 'locale', substr( get_locale(), 0, 2 ) );
Helper::add_json( 'overlayImages', Helper::choices_overlay_images() );
Helper::add_json( 'customPermalinks', (bool) get_option( 'permalink_structure', false ) );
Helper::add_json( 'defautOgImage', Helper::get_settings( 'titles.open_graph_image', '' ) );
Helper::add_json(
'postSettings',
[
'linkSuggestions' => Helper::get_settings( 'titles.pt_' . $screen->post_type . '_link_suggestions' ),
'useFocusKeyword' => 'focus_keywords' === Helper::get_settings( 'titles.pt_' . $screen->post_type . '_ls_use_fk' ),
]
);
$js = rank_math()->plugin_url() . 'assets/admin/js/';
wp_enqueue_script( 'jquery-caret', rank_math()->plugin_url() . 'assets/vendor/jquery.caret.min.js', [ 'jquery' ], '1.3.3', true );
wp_enqueue_script( 'jquery-tag-editor', $js . 'jquery.tag-editor.js', [ 'jquery-ui-autocomplete', 'jquery-caret' ], '1.0.21', true );
wp_enqueue_script( 'rank-math-assessor', $js . 'assessor.js', null, rank_math()->version, true );
if ( Admin_Helper::is_post_edit() ) {
global $post;
Helper::add_json( 'objectID', $post->ID );
Helper::add_json( 'objectType', 'post' );
Helper::add_json( 'parentDomain', Url::get_domain( home_url() ) );
Helper::add_json( 'noFollowDomains', Str::to_arr_no_empty( Helper::get_settings( 'general.nofollow_domains' ) ) );
Helper::add_json( 'noFollowExcludeDomains', Str::to_arr_no_empty( Helper::get_settings( 'general.nofollow_exclude_domains' ) ) );
Helper::add_json( 'noFollowExternalLinks', Helper::get_settings( 'general.nofollow_external_links' ) );
Helper::add_json( 'featuredImageNotice', esc_html__( 'The featured image should be at least 200 by 200 pixels to be picked up by Facebook and other social media sites.', 'rank-math' ) );
wp_enqueue_script( 'rank-math-post-metabox', $js . 'post-metabox.js', [ 'clipboard', 'rank-math-common', 'rank-math-assessor', 'jquery-tag-editor', 'rank-math-validate' ], rank_math()->version, true );
}
if ( Admin_Helper::is_term_edit() ) {
Helper::add_json( 'objectID', Param::request( 'tag_ID', 0, FILTER_VALIDATE_INT ) );
Helper::add_json( 'objectType', 'term' );
wp_enqueue_script( 'rank-math-term-metabox', $js . 'term-metabox.js', [ 'rank-math-common', 'rank-math-assessor', 'jquery-tag-editor' ], rank_math()->version, true );
}
if ( $this->is_user_metabox() ) {
global $user_id;
Helper::add_json( 'objectID', $user_id );
Helper::add_json( 'objectType', 'user' );
wp_enqueue_script( 'rank-math-user-metabox', $js . 'user-metabox.js', [ 'rank-math-common', 'rank-math-assessor', 'jquery-tag-editor' ], rank_math()->version, true );
}
$this->assessor();
}
/**
* Add main metabox.
*/
public function add_main_metabox() {
if ( $this->can_add_metabox() ) {
return;
}
$cmb = new_cmb2_box(
[
'id' => $this->metabox_id,
'title' => esc_html__( 'Rank Math SEO', 'rank-math' ),
'object_types' => $this->get_object_types(),
'taxonomies' => Helper::get_allowed_taxonomies(),
'new_term_section' => false,
'new_user_section' => 'add-existing-user',
'context' => 'normal',
'priority' => $this->get_priority(),
'cmb_styles' => false,
'classes' => 'rank-math-metabox-wrap' . ( Admin_Helper::is_term_profile_page() ? ' rank-math-metabox-frame' : '' ),
]
);
$tabs = $this->get_tabs();
$cmb->add_field(
[
'id' => 'setting-panel-container-' . $this->metabox_id,
'type' => 'meta_tab_container_open',
'tabs' => $tabs,
]
);
foreach ( $tabs as $id => $tab ) {
if ( ! Helper::has_cap( $tab['capability'] ) ) {
continue;
}
$cmb->add_field(
[
'id' => 'setting-panel-' . $id,
'type' => 'tab_open',
]
);
include_once $tab['file'];
/**
* Add setting into specific tab of main metabox.
*
* The dynamic part of the hook name. $id, is the tab id.
*
* @param CMB2 $cmb CMB2 object.
*/
$this->do_action( 'metabox/settings/' . $id, $cmb );
$cmb->add_field(
[
'id' => 'setting-panel-' . $id . '-close',
'type' => 'tab_close',
]
);
}
$cmb->add_field(
[
'id' => 'setting-panel-container-close-' . $this->metabox_id,
'type' => 'tab_container_close',
]
);
CMB2::pre_init( $cmb );
}
/**
* Add link suggestion metabox.
*/
public function add_link_suggestion_metabox() {
$allowed_post_types = [];
foreach ( Helper::get_accessible_post_types() as $post_type ) {
if ( false === Helper::get_settings( 'titles.pt_' . $post_type . '_link_suggestions' ) ) {
continue;
}
$allowed_post_types[] = $post_type;
}
// Early bail.
if ( empty( $allowed_post_types ) ) {
return;
}
$cmb = new_cmb2_box(
[
'id' => $this->metabox_id . '_link_suggestions',
'title' => esc_html__( 'Link Suggestions', 'rank-math' ),
'object_types' => $allowed_post_types,
'context' => 'side',
'priority' => 'default',
]
);
$cmb->add_field(
[
'id' => $this->metabox_id . '_link_suggestions_tooltip',
'type' => 'raw',
'content' => '
' . Admin_Helper::get_tooltip( esc_html__( 'Click on the button to copy URL or insert link in content. You can also drag and drop links in the post content.', 'rank-math' ) ) . '
',
]
);
$cmb->add_field(
[
'id' => 'rank_math_social_tabs',
'type' => 'raw',
'file' => rank_math()->includes_dir() . 'metaboxes/link-suggestions.php',
'not_found' => '' . esc_html__( 'We can\'t show any link suggestions for this post. Try selecting categories and tags for this post, and mark other posts as Pillar Content to make them show up here.', 'rank-math' ) . '',
]
);
CMB2::pre_init( $cmb );
}
/**
* Output the WordPress editor for term SEO description.
*
* @param object $term Current taxonomy term object.
*/
public function category_description_editor( $term ) {
?>