class-term-metabox-formatter.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Formatter
  6. */
  7. /**
  8. * This class provides data for the term metabox by return its values for localization.
  9. */
  10. class WPSEO_Term_Metabox_Formatter implements WPSEO_Metabox_Formatter_Interface {
  11. /**
  12. * The term the metabox formatter is for.
  13. *
  14. * @var WP_Term|stdClass
  15. */
  16. private $term;
  17. /**
  18. * The term's taxonomy.
  19. *
  20. * @var stdClass
  21. */
  22. private $taxonomy;
  23. /**
  24. * Array with the WPSEO_Titles options.
  25. *
  26. * @var array
  27. */
  28. protected $options;
  29. /**
  30. * WPSEO_Taxonomy_Scraper constructor.
  31. *
  32. * @param stdClass $taxonomy Taxonomy.
  33. * @param WP_Term|stdClass $term Term.
  34. */
  35. public function __construct( $taxonomy, $term ) {
  36. $this->taxonomy = $taxonomy;
  37. $this->term = $term;
  38. }
  39. /**
  40. * Returns the translated values.
  41. *
  42. * @return array
  43. */
  44. public function get_values() {
  45. $values = [];
  46. // Todo: a column needs to be added on the termpages to add a filter for the keyword, so this can be used in the focus keyphrase doubles.
  47. if ( is_object( $this->term ) && property_exists( $this->term, 'taxonomy' ) ) {
  48. $values = [
  49. 'search_url' => $this->search_url(),
  50. 'post_edit_url' => $this->edit_url(),
  51. 'base_url' => $this->base_url_for_js(),
  52. 'taxonomy' => $this->term->taxonomy,
  53. 'keyword_usage' => $this->get_focus_keyword_usage(),
  54. 'title_template' => $this->get_title_template(),
  55. 'metadesc_template' => $this->get_metadesc_template(),
  56. 'social_preview_image_url' => $this->get_image_url(),
  57. ];
  58. }
  59. return $values;
  60. }
  61. /**
  62. * Gets the image URL for the term's social preview.
  63. *
  64. * @return string|null The image URL for the social preview.
  65. */
  66. protected function get_image_url() {
  67. return WPSEO_Image_Utils::get_first_content_image_for_term( $this->term->term_id );
  68. }
  69. /**
  70. * Returns the url to search for keyword for the taxonomy.
  71. *
  72. * @return string
  73. */
  74. private function search_url() {
  75. return admin_url( 'edit-tags.php?taxonomy=' . $this->term->taxonomy . '&seo_kw_filter={keyword}' );
  76. }
  77. /**
  78. * Returns the url to edit the taxonomy.
  79. *
  80. * @return string
  81. */
  82. private function edit_url() {
  83. global $wp_version;
  84. $script_filename = version_compare( $wp_version, '4.5', '<' ) ? 'edit-tags' : 'term';
  85. return admin_url( $script_filename . '.php?action=edit&taxonomy=' . $this->term->taxonomy . '&tag_ID={id}' );
  86. }
  87. /**
  88. * Returns a base URL for use in the JS, takes permalink structure into account.
  89. *
  90. * @return string
  91. */
  92. private function base_url_for_js() {
  93. $base_url = home_url( '/', null );
  94. if ( ! WPSEO_Options::get( 'stripcategorybase', false ) ) {
  95. $base_url = trailingslashit( $base_url . $this->taxonomy->rewrite['slug'] );
  96. }
  97. return $base_url;
  98. }
  99. /**
  100. * Counting the number of given keyword used for other term than given term_id.
  101. *
  102. * @return array
  103. */
  104. private function get_focus_keyword_usage() {
  105. $focuskw = WPSEO_Taxonomy_Meta::get_term_meta( $this->term, $this->term->taxonomy, 'focuskw' );
  106. return WPSEO_Taxonomy_Meta::get_keyword_usage( $focuskw, $this->term->term_id, $this->term->taxonomy );
  107. }
  108. /**
  109. * Retrieves the title template.
  110. *
  111. * @return string The title template.
  112. */
  113. private function get_title_template() {
  114. $title = $this->get_template( 'title' );
  115. if ( $title === '' ) {
  116. return '%%title%% %%sep%% %%sitename%%';
  117. }
  118. return $title;
  119. }
  120. /**
  121. * Retrieves the metadesc template.
  122. *
  123. * @return string
  124. */
  125. private function get_metadesc_template() {
  126. return $this->get_template( 'metadesc' );
  127. }
  128. /**
  129. * Retrieves a template.
  130. *
  131. * @param String $template_option_name The name of the option in which the template you want to get is saved.
  132. *
  133. * @return string
  134. */
  135. private function get_template( $template_option_name ) {
  136. $needed_option = $template_option_name . '-tax-' . $this->term->taxonomy;
  137. return WPSEO_Options::get( $needed_option, '' );
  138. }
  139. }