-
Notifications
You must be signed in to change notification settings - Fork 877
/
class-post-metabox-formatter.php
268 lines (232 loc) · 7.42 KB
/
class-post-metabox-formatter.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Formatter
*/
/**
* This class provides data for the post metabox by return its values for localization.
*/
class WPSEO_Post_Metabox_Formatter implements WPSEO_Metabox_Formatter_Interface {
/**
* Holds the WordPress Post.
*
* @var WP_Post
*/
private $post;
/**
* The permalink to follow.
*
* @var string
*/
private $permalink;
/**
* Whether we must return social templates values.
*
* @var bool
*/
private $use_social_templates = false;
/**
* Constructor.
*
* @param WP_Post|array $post Post object.
* @param array $options Title options to use.
* @param string $structure The permalink to follow.
*/
public function __construct( $post, array $options, $structure ) {
$this->post = $post;
$this->permalink = $structure;
$this->use_social_templates = $this->use_social_templates();
}
/**
* Determines whether the social templates should be used.
*
* @return bool Whether the social templates should be used.
*/
public function use_social_templates() {
return WPSEO_Options::get( 'opengraph', false ) === true;
}
/**
* Returns the translated values.
*
* @return array
*/
public function get_values() {
$values = [
'metaDescriptionDate' => '',
];
if ( $this->post instanceof WP_Post ) {
$keyword_usage = $this->get_focus_keyword_usage();
$values_to_set = [
'keyword_usage' => $keyword_usage,
'keyword_usage_post_types' => $this->get_post_types_for_all_ids( $keyword_usage ),
'title_template' => $this->get_title_template(),
'title_template_no_fallback' => $this->get_title_template( false ),
'metadesc_template' => $this->get_metadesc_template(),
'metaDescriptionDate' => $this->get_metadesc_date(),
'first_content_image' => $this->get_image_url(),
'social_title_template' => $this->get_social_title_template(),
'social_description_template' => $this->get_social_description_template(),
'social_image_template' => $this->get_social_image_template(),
'isInsightsEnabled' => $this->is_insights_enabled(),
];
$values = ( $values_to_set + $values );
}
/**
* Filter: 'wpseo_post_edit_values' - Allows changing the values Yoast SEO uses inside the post editor.
*
* @param array $values The key-value map Yoast SEO uses inside the post editor.
* @param WP_Post $post The post opened in the editor.
*/
return apply_filters( 'wpseo_post_edit_values', $values, $this->post );
}
/**
* Gets the image URL for the post's social preview.
*
* @return string|null The image URL for the social preview.
*/
protected function get_image_url() {
return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $this->post->ID );
}
/**
* Counts the number of given keywords used for other posts other than the given post_id.
*
* @return array The keyword and the associated posts that use it.
*/
private function get_focus_keyword_usage() {
$keyword = WPSEO_Meta::get_value( 'focuskw', $this->post->ID );
$usage = [ $keyword => $this->get_keyword_usage_for_current_post( $keyword ) ];
/**
* Allows enhancing the array of posts' that share their focus keywords with the post's related keywords.
*
* @param array $usage The array of posts' ids that share their focus keywords with the post.
* @param int $post_id The id of the post we're finding the usage of related keywords for.
*/
return apply_filters( 'wpseo_posts_for_related_keywords', $usage, $this->post->ID );
}
/**
* Retrieves the post types for the given post IDs.
*
* @param array $post_ids_per_keyword An associative array with keywords as keys and an array of post ids where those keywords are used.
* @return array The post types for the given post IDs.
*/
private function get_post_types_for_all_ids( $post_ids_per_keyword ) {
$post_type_per_keyword_result = [];
foreach ( $post_ids_per_keyword as $keyword => $post_ids ) {
$post_type_per_keyword_result[ $keyword ] = WPSEO_Meta::post_types_for_ids( $post_ids );
}
return $post_type_per_keyword_result;
}
/**
* Gets the keyword usage for the current post and the specified keyword.
*
* @param string $keyword The keyword to check the usage of.
*
* @return array The post IDs which use the passed keyword.
*/
protected function get_keyword_usage_for_current_post( $keyword ) {
return WPSEO_Meta::keyword_usage( $keyword, $this->post->ID );
}
/**
* Retrieves the title template.
*
* @param bool $fallback Whether to return the hardcoded fallback if the template value is empty.
*
* @return string The title template.
*/
private function get_title_template( $fallback = true ) {
$title = $this->get_template( 'title' );
if ( $title === '' && $fallback === true ) {
return '%%title%% %%page%% %%sep%% %%sitename%%';
}
return $title;
}
/**
* Retrieves the metadesc template.
*
* @return string The metadesc template.
*/
private function get_metadesc_template() {
return $this->get_template( 'metadesc' );
}
/**
* Retrieves the social title template.
*
* @return string The social title template.
*/
private function get_social_title_template() {
if ( $this->use_social_templates ) {
return $this->get_social_template( 'title' );
}
return '';
}
/**
* Retrieves the social description template.
*
* @return string The social description template.
*/
private function get_social_description_template() {
if ( $this->use_social_templates ) {
return $this->get_social_template( 'description' );
}
return '';
}
/**
* Retrieves the social image template.
*
* @return string The social description template.
*/
private function get_social_image_template() {
if ( $this->use_social_templates ) {
return $this->get_social_template( 'image-url' );
}
return '';
}
/**
* Retrieves a template.
*
* @param string $template_option_name The name of the option in which the template you want to get is saved.
*
* @return string
*/
private function get_template( $template_option_name ) {
$needed_option = $template_option_name . '-' . $this->post->post_type;
if ( WPSEO_Options::get( $needed_option, '' ) !== '' ) {
return WPSEO_Options::get( $needed_option );
}
return '';
}
/**
* Retrieves a social template.
*
* @param string $template_option_name The name of the option in which the template you want to get is saved.
*
* @return string
*/
private function get_social_template( $template_option_name ) {
/**
* Filters the social template value for a given post type.
*
* @param string $template The social template value, defaults to empty string.
* @param string $template_option_name The subname of the option in which the template you want to get is saved.
* @param string $post_type The name of the post type.
*/
return apply_filters( 'wpseo_social_template_post_type', '', $template_option_name, $this->post->post_type );
}
/**
* Determines the date to be displayed in the snippet preview.
*
* @return string
*/
private function get_metadesc_date() {
return YoastSEO()->helpers->date->format_translated( $this->post->post_date, 'M j, Y' );
}
/**
* Determines whether the insights feature is enabled for this post.
*
* @return bool
*/
protected function is_insights_enabled() {
return WPSEO_Options::get( 'enable_metabox_insights', false );
}
}