/wp-content/plugins/wordpress-seo/admin/formatter/class-post-metabox-formatter.php

https://bitbucket.org/carloskikea/helpet · PHP · 213 lines · 90 code · 34 blank · 89 comment · 7 complexity · 8e4cf6c3129986b326ef2da4a5de226f MD5 · raw file

  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
  132. */
  133. private function get_title_template() {
  134. return $this->get_template( 'title' );
  135. }
  136. /**
  137. * Retrieves the metadesc template.
  138. *
  139. * @return string
  140. */
  141. private function get_metadesc_template() {
  142. return $this->get_template( 'metadesc' );
  143. }
  144. /**
  145. * Retrieves a template.
  146. *
  147. * @param String $template_option_name The name of the option in which the template you want to get is saved.
  148. *
  149. * @return string
  150. */
  151. private function get_template( $template_option_name ) {
  152. $needed_option = $template_option_name . '-' . $this->post->post_type;
  153. if ( WPSEO_Options::get( $needed_option, '' ) !== '' ) {
  154. return WPSEO_Options::get( $needed_option );
  155. }
  156. return '';
  157. }
  158. /**
  159. * Determines the date to be displayed in the snippet preview
  160. *
  161. * @return string
  162. */
  163. private function get_metadesc_date() {
  164. $date = '';
  165. if ( $this->is_show_date_enabled() ) {
  166. $date = date_i18n( 'M j, Y', mysql2date( 'U', $this->post->post_date ) );
  167. }
  168. return $date;
  169. }
  170. /**
  171. * Returns whether or not showing the date in the snippet preview is enabled.
  172. *
  173. * @return bool
  174. */
  175. private function is_show_date_enabled() {
  176. $key = sprintf( 'showdate-%s', $this->post->post_type );
  177. return WPSEO_Options::get( $key, true );
  178. }
  179. }