class-premium-metabox.php
3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* @package WPSEO\Premium|Classes
*/
/**
* The metabox for premium
*/
class WPSEO_Premium_Metabox implements WPSEO_WordPress_Integration {
/**
* @var WPSEO_Metabox_Link_Suggestions
*/
protected $link_suggestions;
/**
* Creates the meta box class.
*
* @param WPSEO_Metabox_Link_Suggestions|null $link_suggestions The link suggestions meta box.
*/
public function __construct( WPSEO_Metabox_Link_Suggestions $link_suggestions = null ) {
if ( $link_suggestions === null ) {
$link_suggestions = new WPSEO_Metabox_Link_Suggestions();
}
$this->link_suggestions = $link_suggestions;
}
/**
* Registers relevant hooks to WordPress
*/
public function register_hooks() {
add_action( 'admin_init', array( $this, 'register_assets' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
$this->link_suggestions->register_hooks();
}
/**
* Registers assets to WordPress
*/
public function register_assets() {
wp_register_script(
WPSEO_Admin_Asset_Manager::PREFIX . 'premium-metabox',
plugin_dir_url( WPSEO_PREMIUM_FILE ) . 'assets/js/dist/wp-seo-premium-metabox-402' . WPSEO_CSSJS_SUFFIX . '.js',
array( 'jquery', 'wp-util', 'underscore' ),
WPSEO_VERSION
);
wp_register_style( WPSEO_Admin_Asset_Manager::PREFIX . 'premium-metabox', plugin_dir_url( WPSEO_PREMIUM_FILE ) . 'assets/css/dist/premium-metabox-400' . WPSEO_CSSJS_SUFFIX . '.css', array(), WPSEO_VERSION );
}
/**
* Enqueues assets when relevant
*/
public function enqueue_assets() {
if ( WPSEO_Metabox::is_post_edit( $GLOBALS['pagenow'] ) ) {
wp_enqueue_script( WPSEO_Admin_Asset_Manager::PREFIX . 'premium-metabox' );
wp_enqueue_style( WPSEO_Admin_Asset_Manager::PREFIX . 'premium-metabox' );
$this->send_data_to_assets();
}
}
/**
* Send data to assets by using wp_localize_script.
*/
public function send_data_to_assets() {
$options = WPSEO_Options::get_option( 'wpseo' );
$insights_enabled = ( isset( $options['enable_metabox_insights'] ) && $options['enable_metabox_insights'] );
$language = WPSEO_Utils::get_language( get_locale() );
if ( $language !== 'en' ) {
$insights_enabled = false;
}
$post = $this->get_post();
$post_type = get_post_type_object( $post->post_type );
$rest_base = isset( $post_type->rest_base ) ? $post_type->rest_base : '';
$data = array(
'insightsEnabled' => ( $insights_enabled ) ? 'enabled' : 'disabled',
'postID' => $this->get_post_ID(),
'restApi' => array(
'available' => WPSEO_Utils::is_api_available(),
'contentEndpointsAvailable' => WPSEO_Utils::are_content_endpoints_available(),
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'postTypeBase' => $rest_base,
),
'linkSuggestionsAvailable' => $this->link_suggestions->is_available( $post->post_type ),
'linkSuggestions' => $this->link_suggestions->get_js_data(),
);
// Use an extra level in the array to preserve booleans. WordPress sanitizes scalar values in the first level of the array.
wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'premium-metabox', 'wpseoPremiumMetaboxData', array( 'data' => $data ) );
}
/**
* Returns the post for the current admin page.
*
* @return WP_Post The post for the current admin page.
*/
protected function get_post() {
return get_post( $this->get_post_ID() );
}
/**
* Retrieves the post ID from the globals
*
* @return {int} The post ID.
*/
protected function get_post_ID() {
return $GLOBALS['post_ID'];
}
}