class-post-metabox-formatter.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Formatter
  6. */
  7. /**
  8. * This class provides data for the post metabox by return its values for localization.
  9. */
  10. class WPSEO_Post_Metabox_Formatter implements WPSEO_Metabox_Formatter_Interface {
  11. /**
  12. * Holds the WordPress Post.
  13. *
  14. * @var WP_Post
  15. */
  16. private $post;
  17. /**
  18. * The permalink to follow.
  19. *
  20. * @var string
  21. */
  22. private $permalink;
  23. /**
  24. * The date helper.
  25. *
  26. * @var WPSEO_Date_Helper
  27. */
  28. protected $date;
  29. /**
  30. * Constructor.
  31. *
  32. * @param WP_Post|array $post Post object.
  33. * @param array $options Title options to use.
  34. * @param string $structure The permalink to follow.
  35. */
  36. public function __construct( $post, array $options, $structure ) {
  37. $this->post = $post;
  38. $this->permalink = $structure;
  39. $this->date = new WPSEO_Date_Helper();
  40. }
  41. /**
  42. * Returns the translated values.
  43. *
  44. * @return array
  45. */
  46. public function get_values() {
  47. $values = [
  48. 'search_url' => $this->search_url(),
  49. 'post_edit_url' => $this->edit_url(),
  50. 'base_url' => $this->base_url_for_js(),
  51. 'metaDescriptionDate' => '',
  52. ];
  53. if ( $this->post instanceof WP_Post ) {
  54. $values_to_set = [
  55. 'keyword_usage' => $this->get_focus_keyword_usage(),
  56. 'title_template' => $this->get_title_template(),
  57. 'metadesc_template' => $this->get_metadesc_template(),
  58. 'metaDescriptionDate' => $this->get_metadesc_date(),
  59. 'social_preview_image_url' => $this->get_image_url(),
  60. ];
  61. $values = ( $values_to_set + $values );
  62. }
  63. return $values;
  64. }
  65. /**
  66. * Gets the image URL for the post's social preview.
  67. *
  68. * @return string|null The image URL for the social preview.
  69. */
  70. protected function get_image_url() {
  71. $post_id = $this->post->ID;
  72. if ( has_post_thumbnail( $post_id ) ) {
  73. $featured_image_info = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'thumbnail' );
  74. return $featured_image_info[0];
  75. }
  76. return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id );
  77. }
  78. /**
  79. * Returns the url to search for keyword for the post.
  80. *
  81. * @return string
  82. */
  83. private function search_url() {
  84. return admin_url( 'edit.php?seo_kw_filter={keyword}' );
  85. }
  86. /**
  87. * Returns the url to edit the taxonomy.
  88. *
  89. * @return string
  90. */
  91. private function edit_url() {
  92. return admin_url( 'post.php?post={id}&action=edit' );
  93. }
  94. /**
  95. * Returns a base URL for use in the JS, takes permalink structure into account.
  96. *
  97. * @return string
  98. */
  99. private function base_url_for_js() {
  100. global $pagenow;
  101. // The default base is the home_url.
  102. $base_url = home_url( '/', null );
  103. if ( 'post-new.php' === $pagenow ) {
  104. return $base_url;
  105. }
  106. // If %postname% is the last tag, just strip it and use that as a base.
  107. if ( 1 === preg_match( '#%postname%/?$#', $this->permalink ) ) {
  108. $base_url = preg_replace( '#%postname%/?$#', '', $this->permalink );
  109. }
  110. return $base_url;
  111. }
  112. /**
  113. * Counts the number of given keywords used for other posts other than the given post_id.
  114. *
  115. * @return array The keyword and the associated posts that use it.
  116. */
  117. private function get_focus_keyword_usage() {
  118. $keyword = WPSEO_Meta::get_value( 'focuskw', $this->post->ID );
  119. $usage = [ $keyword => $this->get_keyword_usage_for_current_post( $keyword ) ];
  120. if ( WPSEO_Utils::is_yoast_seo_premium() ) {
  121. return $this->get_premium_keywords( $usage );
  122. }
  123. return $usage;
  124. }
  125. /**
  126. * Retrieves the additional keywords from Premium, that are associated with the post.
  127. *
  128. * @param array $usage The original keyword usage for the main keyword.
  129. *
  130. * @return array The keyword usage, including the additional keywords.
  131. */
  132. protected function get_premium_keywords( $usage ) {
  133. $additional_keywords = json_decode( WPSEO_Meta::get_value( 'focuskeywords', $this->post->ID ), true );
  134. if ( empty( $additional_keywords ) ) {
  135. return $usage;
  136. }
  137. foreach ( $additional_keywords as $additional_keyword ) {
  138. $keyword = $additional_keyword['keyword'];
  139. $usage[ $keyword ] = $this->get_keyword_usage_for_current_post( $keyword );
  140. }
  141. return $usage;
  142. }
  143. /**
  144. * Gets the keyword usage for the current post and the specified keyword.
  145. *
  146. * @param string $keyword The keyword to check the usage of.
  147. *
  148. * @return array The post IDs which use the passed keyword.
  149. */
  150. protected function get_keyword_usage_for_current_post( $keyword ) {
  151. return WPSEO_Meta::keyword_usage( $keyword, $this->post->ID );
  152. }
  153. /**
  154. * Retrieves the title template.
  155. *
  156. * @return string The title template.
  157. */
  158. private function get_title_template() {
  159. $title = $this->get_template( 'title' );
  160. if ( $title === '' ) {
  161. return '%%title%% %%sep%% %%sitename%%';
  162. }
  163. return $title;
  164. }
  165. /**
  166. * Retrieves the metadesc template.
  167. *
  168. * @return string
  169. */
  170. private function get_metadesc_template() {
  171. return $this->get_template( 'metadesc' );
  172. }
  173. /**
  174. * Retrieves a template.
  175. *
  176. * @param String $template_option_name The name of the option in which the template you want to get is saved.
  177. *
  178. * @return string
  179. */
  180. private function get_template( $template_option_name ) {
  181. $needed_option = $template_option_name . '-' . $this->post->post_type;
  182. if ( WPSEO_Options::get( $needed_option, '' ) !== '' ) {
  183. return WPSEO_Options::get( $needed_option );
  184. }
  185. return '';
  186. }
  187. /**
  188. * Determines the date to be displayed in the snippet preview.
  189. *
  190. * @return string
  191. */
  192. private function get_metadesc_date() {
  193. $date = '';
  194. if ( $this->is_show_date_enabled() ) {
  195. $date = $this->date->format_translated( $this->post->post_date, 'M j, Y' );
  196. }
  197. return $date;
  198. }
  199. /**
  200. * Returns whether or not showing the date in the snippet preview is enabled.
  201. *
  202. * @return bool
  203. */
  204. private function is_show_date_enabled() {
  205. $key = sprintf( 'showdate-%s', $this->post->post_type );
  206. return WPSEO_Options::get( $key, true );
  207. }
  208. }