class-post-metabox-formatter.php 5.1 KB

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