| @@ -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,222 +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 | - return $base_url; | |
| 141 | - } | |
| 142 | - | |
| 143 | - /** | |
| 144 | - * Counts the number of given keywords used for other posts other than the given post_id. | |
| 145 | - * | |
| 146 | - * @return array The keyword and the associated posts that use it. | |
| 147 | - */ | |
| 148 | - private function get_focus_keyword_usage() { | |
| 149 | - $keyword = WPSEO_Meta::get_value( 'focuskw', $this->post->ID ); | |
| 150 | - $usage = [ $keyword => $this->get_keyword_usage_for_current_post( $keyword ) ]; | |
| 151 | - | |
| 152 | - if ( YoastSEO()->helpers->product->is_premium() ) { | |
| 153 | - return $this->get_premium_keywords( $usage ); | |
| 154 | - } | |
| 155 | - | |
| 156 | - return $usage; | |
| 157 | - } | |
| 158 | - | |
| 159 | - /** | |
| 160 | - * Retrieves the additional keywords from Premium, that are associated with the post. | |
| 161 | - * | |
| 162 | - * @param array $usage The original keyword usage for the main keyword. | |
| 163 | - * | |
| 164 | - * @return array The keyword usage, including the additional keywords. | |
| 165 | - */ | |
| 166 | - protected function get_premium_keywords( $usage ) { | |
| 167 | - $additional_keywords = json_decode( WPSEO_Meta::get_value( 'focuskeywords', $this->post->ID ), true ); | |
| 168 | - | |
| 169 | - if ( empty( $additional_keywords ) ) { | |
| 170 | - return $usage; | |
| 171 | - } | |
| 172 | - | |
| 173 | - foreach ( $additional_keywords as $additional_keyword ) { | |
| 174 | - $keyword = $additional_keyword['keyword']; | |
| 175 | - | |
| 176 | - $usage[ $keyword ] = $this->get_keyword_usage_for_current_post( $keyword ); | |
| 177 | - } | |
| 178 | - | |
| 179 | - return $usage; | |
| 180 | - } | |
| 181 | - | |
| 182 | - /** | |
| 183 | - * Gets the keyword usage for the current post and the specified keyword. | |
| 184 | - * | |
| 185 | - * @param string $keyword The keyword to check the usage of. | |
| 186 | - * | |
| 187 | - * @return array The post IDs which use the passed keyword. | |
| 188 | - */ | |
| 189 | - protected function get_keyword_usage_for_current_post( $keyword ) { | |
| 190 | - return WPSEO_Meta::keyword_usage( $keyword, $this->post->ID ); | |
| 191 | - } | |
| 192 | - | |
| 193 | - /** | |
| 194 | - * Retrieves the title template. | |
| 195 | - * | |
| 196 | - * @param bool $fallback Whether to return the hardcoded fallback if the template value is empty. | |
| 197 | - * | |
| 198 | - * @return string The title template. | |
| 199 | - */ | |
| 200 | - private function get_title_template( $fallback = true ) { | |
| 201 | - $title = $this->get_template( 'title' ); | |
| 202 | - | |
| 203 | - if ( $title === '' && $fallback === true ) { | |
| 204 | - return '%%title%% %%page%% %%sep%% %%sitename%%'; | |
| 205 | - } | |
| 206 | - | |
| 207 | - return $title; | |
| 208 | - } | |
| 209 | - | |
| 210 | - /** | |
| 211 | - * Retrieves the metadesc template. | |
| 212 | - * | |
| 213 | - * @return string The metadesc template. | |
| 214 | - */ | |
| 215 | - private function get_metadesc_template() { | |
| 216 | - return $this->get_template( 'metadesc' ); | |
| 217 | - } | |
| 218 | - | |
| 219 | - /** | |
| 220 | - * Retrieves the social title template. | |
| 221 | - * | |
| 222 | - * @return string The social title template. | |
| 223 | - */ | |
| 224 | - private function get_social_title_template() { | |
| 225 | - if ( $this->use_social_templates ) { | |
| 226 | - return $this->get_template( 'social-title' ); | |
| 227 | - } | |
| 228 | - | |
| 229 | - return ''; | |
| 230 | - } | |
| 231 | - | |
| 232 | - /** | |
| 233 | - * Retrieves the social description template. | |
| 234 | - * | |
| 235 | - * @return string The social description template. | |
| 236 | - */ | |
| 237 | - private function get_social_description_template() { | |
| 238 | - if ( $this->use_social_templates ) { | |
| 239 | - return $this->get_template( 'social-description' ); | |
| 240 | - } | |
| 241 | - | |
| 242 | - return ''; | |
| 243 | - } | |
| 244 | - | |
| 245 | - /** | |
| 246 | - * Retrieves the social image template. | |
| 247 | - * | |
| 248 | - * @return string The social description template. | |
| 249 | - */ | |
| 250 | - private function get_social_image_template() { | |
| 251 | - if ( $this->use_social_templates ) { | |
| 252 | - return $this->get_template( 'social-image-url' ); | |
| 253 | - } | |
| 254 | - | |
| 255 | - return ''; | |
| 256 | - } | |
| 257 | - | |
| 258 | - /** | |
| 259 | - * Retrieves a template. | |
| 260 | - * | |
| 261 | - * @param string $template_option_name The name of the option in which the template you want to get is saved. | |
| 262 | - * | |
| 263 | - * @return string | |
| 264 | - */ | |
| 265 | - private function get_template( $template_option_name ) { | |
| 266 | - $needed_option = $template_option_name . '-' . $this->post->post_type; | |
| 267 | - | |
| 268 | - if ( WPSEO_Options::get( $needed_option, '' ) !== '' ) { | |
| 269 | - return WPSEO_Options::get( $needed_option ); | |
| 270 | - } | |
| 271 | - | |
| 272 | - return ''; | |
| 273 | - } | |
| 274 | - | |
| 275 | - /** | |
| 276 | - * Determines the date to be displayed in the snippet preview. | |
| 277 | - * | |
| 278 | - * @return string | |
| 279 | - */ | |
| 280 | - private function get_metadesc_date() { | |
| 281 | - 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 ); | |
| 282 | 94 | } |
| 283 | 95 | } |