PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
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
← All changes | admin/formatter/class-post-metabox-formatter.php +26 -219 18.6 → trunk View file →
@@ -4,8 +4,10 @@
4 4 *
5 5 * @package WPSEO\Admin\Formatter
6 6 */
7 7
8 +use Yoast\WP\SEO\Editors\Application\Seo\Post_Seo_Information_Repository;
9 +
8 10 /**
9 11 * This class provides data for the post metabox by return its values for localization.
10 12 */
11 13 class WPSEO_Post_Metabox_Formatter implements WPSEO_Metabox_Formatter_Interface {
@@ -24,15 +26,8 @@
24 26 */
25 27 private $permalink;
26 28
27 29 /**
28 - * Whether we must return social templates values.
29 - *
30 - * @var bool
31 - */
32 - private $use_social_templates = false;
33 -
34 - /**
35 30 * Constructor.
36 31 *
37 32 * @param WP_Post|array $post Post object.
38 33 * @param array $options Title options to use.
@@ -40,22 +35,20 @@
40 35 */
41 36 public function __construct( $post, array $options, $structure ) {
42 37 $this->post = $post;
43 38 $this->permalink = $structure;
44 -
45 - $this->use_social_templates = $this->use_social_templates();
46 39 }
47 40
48 41 /**
49 42 * Determines whether the social templates should be used.
50 43 *
51 - * @return bool Whether the social templates should be used.
44 + * @deprecated 23.1
45 + * @codeCoverageIgnore
46 + *
47 + * @return void
52 48 */
53 49 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;
50 + _deprecated_function( __METHOD__, 'Yoast SEO 23.1' );
58 51 }
59 52
60 53 /**
61 54 * Returns the translated values.
@@ -62,227 +55,41 @@
62 55 *
63 56 * @return array
64 57 */
65 58 public function get_values() {
59 +
66 60 $values = [
67 - 'search_url' => $this->search_url(),
68 - 'post_edit_url' => $this->edit_url(),
69 - 'base_url' => $this->base_url_for_js(),
70 61 'metaDescriptionDate' => '',
71 -
72 62 ];
73 63
74 64 if ( $this->post instanceof WP_Post ) {
65 +
66 + /** @var Post_Seo_Information_Repository $repo */
67 + $repo = YoastSEO()->classes->get( Post_Seo_Information_Repository::class );
68 + $repo->set_post( $this->post );
69 +
75 70 $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(),
71 + 'isInsightsEnabled' => $this->is_insights_enabled(),
85 72 ];
86 73
87 74 $values = ( $values_to_set + $values );
75 + $values = ( $repo->get_seo_data() + $values );
88 76 }
89 77
90 - return $values;
78 + /**
79 + * Filter: 'wpseo_post_edit_values' - Allows changing the values Yoast SEO uses inside the post editor.
80 + *
81 + * @param array $values The key-value map Yoast SEO uses inside the post editor.
82 + * @param WP_Post $post The post opened in the editor.
83 + */
84 + return apply_filters( 'wpseo_post_edit_values', $values, $this->post );
91 85 }
92 86
93 87 /**
94 - * Gets the image URL for the post's social preview.
88 + * Determines whether the insights feature is enabled for this post.
95 89 *
96 - * @return string|null The image URL for the social preview.
90 + * @return bool
97 91 */
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' );
92 + protected function is_insights_enabled() {
93 + return WPSEO_Options::get( 'enable_metabox_insights', false );
287 94 }
288 95 }