PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.8
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / admin / formatter / class-post-metabox-formatter.php

class-post-metabox-formatter.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.8, at admin/formatter/class-post-metabox-formatter.php

289 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Admin\Formatter
6 */
7
8 /**
9 * This class provides data for the post metabox by return its values for localization.
10 */
11 class WPSEO_Post_Metabox_Formatter implements WPSEO_Metabox_Formatter_Interface {
12
13 /**
14 * Holds the WordPress Post.
15 *
16 * @var WP_Post
17 */
18 private $post;
19
20 /**
21 * The permalink to follow.
22 *
23 * @var string
24 */
25 private $permalink;
26
27 /**
28 * Whether we must return social templates values.
29 *
30 * @var bool
31 */
32 private $use_social_templates = false;
33
34 /**
35 * Constructor.
36 *
37 * @param WP_Post|array $post Post object.
38 * @param array $options Title options to use.
39 * @param string $structure The permalink to follow.
40 */
41 public function __construct( $post, array $options, $structure ) {
42 $this->post = $post;
43 $this->permalink = $structure;
44
45 $this->use_social_templates = $this->use_social_templates();
46 }
47
48 /**
49 * Determines whether the social templates should be used.
50 *
51 * @return bool Whether the social templates should be used.
52 */
53 public function use_social_templates() {
54 return YoastSEO()->helpers->product->is_premium()
55 && defined( 'WPSEO_PREMIUM_VERSION' )
56 && version_compare( WPSEO_PREMIUM_VERSION, '16.5-RC0', '>=' )
57 && WPSEO_Options::get( 'opengraph', false ) === true;
58 }
59
60 /**
61 * Returns the translated values.
62 *
63 * @return array
64 */
65 public function get_values() {
66 $values = [
67 'search_url' => $this->search_url(),
68 'post_edit_url' => $this->edit_url(),
69 'base_url' => $this->base_url_for_js(),
70 'metaDescriptionDate' => '',
71
72 ];
73
74 if ( $this->post instanceof WP_Post ) {
75 $values_to_set = [
76 'keyword_usage' => $this->get_focus_keyword_usage(),
77 'title_template' => $this->get_title_template(),
78 'title_template_no_fallback' => $this->get_title_template( false ),
79 'metadesc_template' => $this->get_metadesc_template(),
80 'metaDescriptionDate' => $this->get_metadesc_date(),
81 'first_content_image' => $this->get_image_url(),
82 'social_title_template' => $this->get_social_title_template(),
83 'social_description_template' => $this->get_social_description_template(),
84 'social_image_template' => $this->get_social_image_template(),
85 ];
86
87 $values = ( $values_to_set + $values );
88 }
89
90 return $values;
91 }
92
93 /**
94 * Gets the image URL for the post's social preview.
95 *
96 * @return string|null The image URL for the social preview.
97 */
98 protected function get_image_url() {
99 return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $this->post->ID );
100 }
101
102 /**
103 * Returns the url to search for keyword for the post.
104 *
105 * @return string
106 */
107 private function search_url() {
108 return admin_url( 'edit.php?seo_kw_filter={keyword}' );
109 }
110
111 /**
112 * Returns the url to edit the taxonomy.
113 *
114 * @return string
115 */
116 private function edit_url() {
117 return admin_url( 'post.php?post={id}&action=edit' );
118 }
119
120 /**
121 * Returns a base URL for use in the JS, takes permalink structure into account.
122 *
123 * @return string
124 */
125 private function base_url_for_js() {
126 global $pagenow;
127
128 // The default base is the home_url.
129 $base_url = home_url( '/', null );
130
131 if ( $pagenow === 'post-new.php' ) {
132 return $base_url;
133 }
134
135 // If %postname% is the last tag, just strip it and use that as a base.
136 if ( preg_match( '#%postname%/?$#', $this->permalink ) === 1 ) {
137 $base_url = preg_replace( '#%postname%/?$#', '', $this->permalink );
138 }
139
140 // If %pagename% is the last tag, just strip it and use that as a base.
141 if ( preg_match( '#%pagename%/?$#', $this->permalink ) === 1 ) {
142 $base_url = preg_replace( '#%pagename%/?$#', '', $this->permalink );
143 }
144
145 return $base_url;
146 }
147
148 /**
149 * Counts the number of given keywords used for other posts other than the given post_id.
150 *
151 * @return array The keyword and the associated posts that use it.
152 */
153 private function get_focus_keyword_usage() {
154 $keyword = WPSEO_Meta::get_value( 'focuskw', $this->post->ID );
155 $usage = [ $keyword => $this->get_keyword_usage_for_current_post( $keyword ) ];
156
157 if ( YoastSEO()->helpers->product->is_premium() ) {
158 return $this->get_premium_keywords( $usage );
159 }
160
161 return $usage;
162 }
163
164 /**
165 * Retrieves the additional keywords from Premium, that are associated with the post.
166 *
167 * @param array $usage The original keyword usage for the main keyword.
168 *
169 * @return array The keyword usage, including the additional keywords.
170 */
171 protected function get_premium_keywords( $usage ) {
172 $additional_keywords = json_decode( WPSEO_Meta::get_value( 'focuskeywords', $this->post->ID ), true );
173
174 if ( empty( $additional_keywords ) ) {
175 return $usage;
176 }
177
178 foreach ( $additional_keywords as $additional_keyword ) {
179 $keyword = $additional_keyword['keyword'];
180
181 $usage[ $keyword ] = $this->get_keyword_usage_for_current_post( $keyword );
182 }
183
184 return $usage;
185 }
186
187 /**
188 * Gets the keyword usage for the current post and the specified keyword.
189 *
190 * @param string $keyword The keyword to check the usage of.
191 *
192 * @return array The post IDs which use the passed keyword.
193 */
194 protected function get_keyword_usage_for_current_post( $keyword ) {
195 return WPSEO_Meta::keyword_usage( $keyword, $this->post->ID );
196 }
197
198 /**
199 * Retrieves the title template.
200 *
201 * @param bool $fallback Whether to return the hardcoded fallback if the template value is empty.
202 *
203 * @return string The title template.
204 */
205 private function get_title_template( $fallback = true ) {
206 $title = $this->get_template( 'title' );
207
208 if ( $title === '' && $fallback === true ) {
209 return '%%title%% %%page%% %%sep%% %%sitename%%';
210 }
211
212 return $title;
213 }
214
215 /**
216 * Retrieves the metadesc template.
217 *
218 * @return string The metadesc template.
219 */
220 private function get_metadesc_template() {
221 return $this->get_template( 'metadesc' );
222 }
223
224 /**
225 * Retrieves the social title template.
226 *
227 * @return string The social title template.
228 */
229 private function get_social_title_template() {
230 if ( $this->use_social_templates ) {
231 return $this->get_template( 'social-title' );
232 }
233
234 return '';
235 }
236
237 /**
238 * Retrieves the social description template.
239 *
240 * @return string The social description template.
241 */
242 private function get_social_description_template() {
243 if ( $this->use_social_templates ) {
244 return $this->get_template( 'social-description' );
245 }
246
247 return '';
248 }
249
250 /**
251 * Retrieves the social image template.
252 *
253 * @return string The social description template.
254 */
255 private function get_social_image_template() {
256 if ( $this->use_social_templates ) {
257 return $this->get_template( 'social-image-url' );
258 }
259
260 return '';
261 }
262
263 /**
264 * Retrieves a template.
265 *
266 * @param string $template_option_name The name of the option in which the template you want to get is saved.
267 *
268 * @return string
269 */
270 private function get_template( $template_option_name ) {
271 $needed_option = $template_option_name . '-' . $this->post->post_type;
272
273 if ( WPSEO_Options::get( $needed_option, '' ) !== '' ) {
274 return WPSEO_Options::get( $needed_option );
275 }
276
277 return '';
278 }
279
280 /**
281 * Determines the date to be displayed in the snippet preview.
282 *
283 * @return string
284 */
285 private function get_metadesc_date() {
286 return YoastSEO()->helpers->date->format_translated( $this->post->post_date, 'M j, Y' );
287 }
288 }
289