class-term-metabox-formatter.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * @var WP_Term|stdClass
  13. */
  14. private $term;
  15. /**
  16. * @var stdClass
  17. */
  18. private $taxonomy;
  19. /**
  20. * @var array Array with the WPSEO_Titles options.
  21. */
  22. protected $options;
  23. /**
  24. * WPSEO_Taxonomy_Scraper constructor.
  25. *
  26. * @param stdClass $taxonomy Taxonomy.
  27. * @param WP_Term|stdClass $term Term.
  28. */
  29. public function __construct( $taxonomy, $term ) {
  30. $this->term = $term;
  31. $this->taxonomy = $taxonomy;
  32. }
  33. /**
  34. * Returns the translated values.
  35. *
  36. * @return array
  37. */
  38. public function get_values() {
  39. $values = array();
  40. // 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 kw doubles.
  41. if ( is_object( $this->term ) && property_exists( $this->term, 'taxonomy' ) ) {
  42. $values = array(
  43. 'search_url' => $this->search_url(),
  44. 'post_edit_url' => $this->edit_url(),
  45. 'base_url' => $this->base_url_for_js(),
  46. 'taxonomy' => $this->term->taxonomy,
  47. 'keyword_usage' => $this->get_focus_keyword_usage(),
  48. 'title_template' => $this->get_title_template(),
  49. 'metadesc_template' => $this->get_metadesc_template(),
  50. );
  51. }
  52. return $values;
  53. }
  54. /**
  55. * Returns the url to search for keyword for the taxonomy
  56. *
  57. * @return string
  58. */
  59. private function search_url() {
  60. return admin_url( 'edit-tags.php?taxonomy=' . $this->term->taxonomy . '&seo_kw_filter={keyword}' );
  61. }
  62. /**
  63. * Returns the url to edit the taxonomy
  64. *
  65. * @return string
  66. */
  67. private function edit_url() {
  68. global $wp_version;
  69. $script_filename = version_compare( $wp_version, '4.5', '<' ) ? 'edit-tags' : 'term';
  70. return admin_url( $script_filename . '.php?action=edit&taxonomy=' . $this->term->taxonomy . '&tag_ID={id}' );
  71. }
  72. /**
  73. * Returns a base URL for use in the JS, takes permalink structure into account
  74. *
  75. * @return string
  76. */
  77. private function base_url_for_js() {
  78. $base_url = home_url( '/', null );
  79. if ( ! WPSEO_Options::get( 'stripcategorybase', false ) ) {
  80. $base_url = trailingslashit( $base_url . $this->taxonomy->rewrite['slug'] );
  81. }
  82. return $base_url;
  83. }
  84. /**
  85. * Counting the number of given keyword used for other term than given term_id
  86. *
  87. * @return array
  88. */
  89. private function get_focus_keyword_usage() {
  90. $focuskw = WPSEO_Taxonomy_Meta::get_term_meta( $this->term, $this->term->taxonomy, 'focuskw' );
  91. return WPSEO_Taxonomy_Meta::get_keyword_usage( $focuskw, $this->term->term_id, $this->term->taxonomy );
  92. }
  93. /**
  94. * Retrieves the title template.
  95. *
  96. * @return string The title template.
  97. */
  98. private function get_title_template() {
  99. $title = $this->get_template( 'title' );
  100. if ( $title === '' ) {
  101. return '%%title%% %%sep%% %%sitename%%';
  102. }
  103. return $title;
  104. }
  105. /**
  106. * Retrieves the metadesc template.
  107. *
  108. * @return string
  109. */
  110. private function get_metadesc_template() {
  111. return $this->get_template( 'metadesc' );
  112. }
  113. /**
  114. * Retrieves a template.
  115. *
  116. * @param String $template_option_name The name of the option in which the template you want to get is saved.
  117. *
  118. * @return string
  119. */
  120. private function get_template( $template_option_name ) {
  121. $needed_option = $template_option_name . '-tax-' . $this->term->taxonomy;
  122. return WPSEO_Options::get( $needed_option, '' );
  123. }
  124. }