-
Notifications
You must be signed in to change notification settings - Fork 3
/
yoast-seo-acf-content-analysis.php
168 lines (136 loc) · 5.19 KB
/
yoast-seo-acf-content-analysis.php
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
Plugin Name: ACF Content Analysis for Yoast SEO
Plugin URI: http://angrycreative.se
Description: Ensure that Yoast SEO analysize all ACF content including Flexible Content and Repeaters.
Version: 1.2.6
Author: ViktorFroberg, marol87, pekz0r, angrycreative
Author URI: http://angrycreative.se
License: GPL
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
define('AC_SEO_ACF_ANALYSIS_PLUGIN_SLUG', 'ac-yoast-seo-acf-content-analysis');
define('AC_SEO_ACF_ANALYSIS_PLUGIN_PATH', plugin_dir_path( __FILE__ ));
define('AC_SEO_ACF_ANALYSIS_PLUGIN_URL', plugins_url('', __FILE__).'/');
define('AC_SEO_ACF_ANALYSIS_PLUGIN_NAME', untrailingslashit(plugin_basename(__FILE__)));
class AC_Yoast_SEO_ACF_Content_Analysis
{
/**
* Plugin version, used for automatic updates and for cache-busting of style and script file references.
*
* @since 0.1.0
* @var string
*/
const VERSION = '1.2.6';
/**
* Unique identifier for the plugin.
* This value is used as the text domain when internationalizing strings of text. It should
* match the Text Domain file header in the main plugin file.
*
* @since 0.1.0
* @var string
*/
public $plugin_slug = 'ysacf';
/**
* Holds the global `$pagenow` variable's value.
*
* @since 1.1.0
* @var string
*/
private $pagenow = '';
/**
* variable containing places where the plugin will fetch acf data
*
* @since 1.1.0
* @var array
*/
private $analysize_page_types = array(
'term.php',
'post.php',
'edit-tags.php', // will be removed in future versions of the plugin.
'post-new.php',
);
function __construct(){
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
add_action( 'admin_print_scripts-post-new.php', array($this, 'enqueue_admin_scripts') , 999 );
add_action( 'admin_print_scripts-post.php', array($this, 'enqueue_admin_scripts'), 999 );
add_action( 'wp_ajax_' . $this->plugin_slug . '_get_fields', array($this, 'ajax_get_fields') );
if(isset($GLOBALS['pagenow'])) {
$this->pagenow = $GLOBALS['pagenow'];
}
}
function get_excluded_fields() {
return apply_filters( 'ysacf_exclude_fields', array() );
}
/**
* Filter what ACF Fields not to score
* @param field name array
*/
function get_field_data($fields) {
$data = '';
if(!empty($fields)) {
foreach($fields as $key =>$item) {
if(in_array((string)$key, $this->get_excluded_fields()) ){
continue;
} else {
switch(gettype($item)) {
case 'string':
$data = $data.' '.$item;
break;
case 'array':
if(isset($item['sizes']['thumbnail'])) {
// put all images in img tags for scoring.
$alt = '';
if(isset($item['alt'])) {
$alt = $item['alt'];
}
$title = '';
if(isset($item['title'])) {
$title = $item['title'];
}
$data = $data.' <img src="'.$item['sizes']['thumbnail'] . '" alt="' . $alt .'" title="' . $title . '"/>';
} else {
$data = $data.' '.$this->get_field_data($item);
}
break;
}
}
}
}
return $data;
}
function ajax_get_fields() {
$pid = filter_input(INPUT_POST, 'postId', FILTER_SANITIZE_STRING);
$fields = get_fields( $pid );
wp_send_json( $this->get_field_data( $fields ) );
}
/**
* Register and enqueue admin-specific JavaScript.
*
* @since 0.1.0
*/
public function enqueue_admin_scripts() {
if( in_array( $this->pagenow, $this->analysize_page_types ) ) {
// if this is a taxonomy, get the taxonomy id else get the post id
if($this->pagenow === 'term.php' || $this->pagenow === 'edit-tags.php') {
$id = filter_input(INPUT_GET, 'taxonomy', FILTER_SANITIZE_STRING) . '_' . filter_input(INPUT_GET, 'tag_ID', FILTER_SANITIZE_NUMBER_INT);
} else {
global $post;
$id = $post->ID;
}
wp_enqueue_script($this->plugin_slug, AC_SEO_ACF_ANALYSIS_PLUGIN_URL . 'yoast-seo-plugin.js', array('jquery'), self::VERSION);
wp_localize_script($this->plugin_slug, 'yoast_acf_settings', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'id' => $id,
'ajax_action' => $this->plugin_slug . '_get_fields'
));
}
}
}
add_action( 'plugins_loaded', 'init_ysacf' );
function init_ysacf() {
new AC_Yoast_SEO_ACF_Content_Analysis();
}