WPSEO_Post_Metabox_Formatter

This class provides data for the post metabox by return its values for localization.

Defined (1)

The class is defined in the following location(s).

/admin/formatter/class-post-metabox-formatter.php  
  1. class WPSEO_Post_Metabox_Formatter implements WPSEO_Metabox_Formatter_Interface { 
  2.  
  3. /** 
  4. * @var WP_Post 
  5. */ 
  6. private $post; 
  7.  
  8. /** 
  9. * @var array Array with the WPSEO_Titles options. 
  10. */ 
  11. protected $options; 
  12.  
  13. /** 
  14. * @var string The permalink to follow. 
  15. */ 
  16. private $permalink; 
  17.  
  18. /** 
  19. * Constructor. 
  20. * @param WP_Post|array $post Post object. 
  21. * @param array $options Title options to use. 
  22. * @param string $structure The permalink to follow. 
  23. */ 
  24. public function __construct( $post, array $options, $structure ) { 
  25. $this->post = $post; 
  26. $this->options = $options; 
  27. $this->permalink = $structure; 
  28.  
  29. /** 
  30. * Returns the translated values. 
  31. * @return array 
  32. */ 
  33. public function get_values() { 
  34. $values = array( 
  35. 'search_url' => $this->search_url(),  
  36. 'post_edit_url' => $this->edit_url(),  
  37. 'base_url' => $this->base_url_for_js(),  
  38. 'metaDescriptionDate' => '',  
  39. ); 
  40.  
  41. if ( $this->post instanceof WP_Post ) { 
  42. $values_to_set = array( 
  43. 'keyword_usage' => $this->get_focus_keyword_usage(),  
  44. 'title_template' => $this->get_title_template(),  
  45. 'metadesc_template' => $this->get_metadesc_template(),  
  46. 'metaDescriptionDate' => $this->get_metadesc_date(),  
  47. ); 
  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. * @return string 
  56. */ 
  57. private function search_url() { 
  58. return admin_url( 'edit.php?seo_kw_filter={keyword}' ); 
  59.  
  60. /** 
  61. * Returns the url to edit the taxonomy 
  62. * @return string 
  63. */ 
  64. private function edit_url() { 
  65. return admin_url( 'post.php?post={id}&action=edit' ); 
  66.  
  67. /** 
  68. * Returns a base URL for use in the JS, takes permalink structure into account 
  69. * @return string 
  70. */ 
  71. private function base_url_for_js() { 
  72. global $pagenow; 
  73.  
  74. // The default base is the home_url. 
  75. $base_url = home_url( '/', null ); 
  76.  
  77. if ( 'post-new.php' === $pagenow ) { 
  78. return $base_url; 
  79.  
  80. // If %postname% is the last tag, just strip it and use that as a base. 
  81. if ( 1 === preg_match( '#%postname%/?$#', $this->permalink ) ) { 
  82. $base_url = preg_replace( '#%postname%/?$#', '', $this->permalink ); 
  83.  
  84. return $base_url; 
  85.  
  86. /** 
  87. * Counting the number of given keyword used for other posts than given post_id 
  88. * @return array 
  89. */ 
  90. private function get_focus_keyword_usage() { 
  91. $keyword = WPSEO_Meta::get_value( 'focuskw', $this->post->ID ); 
  92.  
  93. return array( 
  94. $keyword => WPSEO_Meta::keyword_usage( $keyword, $this->post->ID ),  
  95. ); 
  96.  
  97. /** 
  98. * Retrieves the title template. 
  99. * @return string 
  100. */ 
  101. private function get_title_template() { 
  102. return $this->get_template( 'title' ); 
  103.  
  104. /** 
  105. * Retrieves the metadesc template. 
  106. * @return string 
  107. */ 
  108. private function get_metadesc_template() { 
  109. return $this->get_template( 'metadesc' ); 
  110.  
  111. /** 
  112. * Retrieves a template. 
  113. * @param String $template_option_name The name of the option in which the template you want to get is saved. 
  114. * @return string 
  115. */ 
  116. private function get_template( $template_option_name ) { 
  117. $needed_option = $template_option_name . '-' . $this->post->post_type; 
  118.  
  119. if ( isset( $this->options[ $needed_option ] ) && $this->options[ $needed_option ] !== '' ) { 
  120. return $this->options[ $needed_option ]; 
  121.  
  122. return ''; 
  123.  
  124. /** 
  125. * Determines the date to be displayed in the snippet preview 
  126. * @return string 
  127. */ 
  128. private function get_metadesc_date() { 
  129. $date = ''; 
  130.  
  131. if ( $this->is_show_date_enabled() ) { 
  132. $date = date_i18n( 'M j, Y', mysql2date( 'U', $this->post->post_date ) ); 
  133.  
  134. return $date; 
  135.  
  136. /** 
  137. * Returns whether or not showing the date in the snippet preview is enabled. 
  138. * @return bool 
  139. */ 
  140. private function is_show_date_enabled() { 
  141. $post_type = $this->post->post_type; 
  142. $key = sprintf( 'showdate-%s', $post_type ); 
  143.  
  144. return isset( $this->options[ $key ] ) && true === $this->options[ $key ];