class-taxonomy-settings-fields.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * This class parses all the values for the general tab in the Yoast SEO settings metabox.
  9. */
  10. class WPSEO_Taxonomy_Settings_Fields extends WPSEO_Taxonomy_Fields {
  11. /**
  12. * Options array for the no-index options, including translated labels.
  13. *
  14. * @var array
  15. */
  16. private $no_index_options = [];
  17. /**
  18. * The WPSEO_Taxonomy_Settings_Fields class constructor.
  19. *
  20. * @param stdClass $term The current taxonomy.
  21. */
  22. public function __construct( $term ) {
  23. parent::__construct( $term );
  24. $this->translate_meta_options();
  25. }
  26. /**
  27. * Returns array with the fields for the General tab.
  28. *
  29. * @return array Fields to be used on the General tab.
  30. */
  31. public function get() {
  32. $labels = $this->get_taxonomy_labels();
  33. $fields = [
  34. 'noindex' => $this->get_field_config(
  35. /* translators: %s = taxonomy name. */
  36. esc_html( sprintf( __( 'Allow search engines to show this %s in search results?', 'wordpress-seo' ), $labels->singular_name ) ),
  37. '',
  38. 'select',
  39. $this->get_noindex_options()
  40. ),
  41. 'bctitle' => $this->get_field_config(
  42. __( 'Breadcrumbs Title', 'wordpress-seo' ),
  43. esc_html__( 'The Breadcrumbs Title is used in the breadcrumbs where this taxonomy appears.', 'wordpress-seo' ),
  44. 'text',
  45. '',
  46. ( WPSEO_Options::get( 'breadcrumbs-enable' ) !== true )
  47. ),
  48. 'canonical' => $this->get_field_config(
  49. __( 'Canonical URL', 'wordpress-seo' ),
  50. esc_html__( 'The canonical link is shown on the archive page for this term.', 'wordpress-seo' )
  51. ),
  52. ];
  53. return $this->filter_hidden_fields( $fields );
  54. }
  55. /**
  56. * Translate options text strings for use in the select fields.
  57. *
  58. * {@internal IMPORTANT: if you want to add a new string (option) somewhere, make sure you add
  59. * that array key to the main options definition array in the class WPSEO_Taxonomy_Meta() as well!!!!}}
  60. */
  61. private function translate_meta_options() {
  62. $this->no_index_options = WPSEO_Taxonomy_Meta::$no_index_options;
  63. /* translators: %1$s expands to the taxonomy name %2$s expands to the current taxonomy index value */
  64. $this->no_index_options['default'] = __( '%2$s (current default for %1$s)', 'wordpress-seo' );
  65. $this->no_index_options['index'] = __( 'Yes', 'wordpress-seo' );
  66. $this->no_index_options['noindex'] = __( 'No', 'wordpress-seo' );
  67. }
  68. /**
  69. * Getting the data for the noindex fields.
  70. *
  71. * @return array Array containing the no_index options.
  72. */
  73. private function get_noindex_options() {
  74. $labels = $this->get_taxonomy_labels();
  75. $noindex_options['options'] = $this->no_index_options;
  76. $noindex_options['options']['default'] = sprintf( $noindex_options['options']['default'], $labels->name, $this->get_robot_index() );
  77. return $noindex_options;
  78. }
  79. /**
  80. * Retrieve the taxonomies plural for use in sentences.
  81. *
  82. * @return object Object containing the taxonomy's labels.
  83. */
  84. private function get_taxonomy_labels() {
  85. $taxonomy = get_taxonomy( $this->term->taxonomy );
  86. return $taxonomy->labels;
  87. }
  88. /**
  89. * Returns the current robot index value for the taxonomy
  90. *
  91. * @return string
  92. */
  93. private function get_robot_index() {
  94. $robot_index = $this->no_index_options['index'];
  95. $index_option = 'noindex-tax-' . $this->term->taxonomy;
  96. if ( WPSEO_Options::get( $index_option, false ) ) {
  97. $robot_index = $this->no_index_options['noindex'];
  98. }
  99. return $robot_index;
  100. }
  101. }