| 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 |
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' ); |
| 282 |
} |
| 283 |
} |
| 284 |
|