class-custom-fields-plugin.php
2.01 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
<?php
/**
* @package WPSEO\Premium\Classes
*/
/**
* Enqueues a JavaScript plugin for YoastSEO.js that adds custom fields to the content that were defined in the titles
* and meta's section of the Yoast SEO settings when those fields are available.
*/
class WPSEO_Custom_Fields_Plugin {
/**
* Initialize the AJAX hooks
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
}
/**
* Enqueues all the needed JS scripts.
*/
public function enqueue() {
wp_enqueue_script( 'wp-seo-premium-custom-fields-plugin', plugin_dir_url( WPSEO_PREMIUM_FILE ) . 'assets/js/dist/wp-seo-premium-custom-fields-plugin-350' . WPSEO_CSSJS_SUFFIX . '.js', array( 'jquery' ), WPSEO_VERSION );
wp_localize_script( 'wp-seo-premium-custom-fields-plugin', 'YoastCustomFieldsPluginL10', $this->localize_script() );
}
/**
* Loads the custom fields translations
*
* @return array
*/
public function localize_script() {
return array(
'custom_field_names' => $this->get_custom_field_names(),
);
}
/**
* Retrieve all custom field names set in SEO ->
*
* @return array
*/
private function get_custom_field_names() {
$custom_field_names = array();
$post = $this->get_post();
$options = get_option( WPSEO_Options::get_option_instance( 'wpseo_titles' )->option_name, array() );
if ( is_object( $post ) ) {
$target_option = 'page-analyse-extra-' . $post->post_type;
if ( array_key_exists( $target_option, $options ) ) {
$custom_field_names = explode( ',', $options[ $target_option ] );
}
}
return $custom_field_names;
}
/**
* Retrieves post data given a post ID or the global
*
* @return array|null|WP_Post Returns a post if found, otherwise returns an empty array.
*/
private function get_post() {
if ( $post = filter_input( INPUT_GET, 'post' ) ) {
$post_id = (int) WPSEO_Utils::validate_int( $post );
return get_post( $post_id );
}
if ( isset( $GLOBALS['post'] ) ) {
return $GLOBALS['post'];
}
return array();
}
}