class-taxonomy-metabox.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * This class generates the metabox on the edit term page.
  9. */
  10. class WPSEO_Taxonomy_Metabox {
  11. /**
  12. * The term currently being edited.
  13. *
  14. * @var WP_Term
  15. */
  16. private $term;
  17. /**
  18. * The term's taxonomy.
  19. *
  20. * @var string
  21. */
  22. private $taxonomy;
  23. /**
  24. * Renders the taxonomy field.
  25. *
  26. * @var WPSEO_Taxonomy_Fields_Presenter
  27. */
  28. private $taxonomy_tab_content;
  29. /**
  30. * Renders the taxonomy social fields.
  31. *
  32. * @var WPSEO_Taxonomy_Social_Fields
  33. */
  34. private $taxonomy_social_fields;
  35. /**
  36. * This class adds the Social tab to the Yoast SEO metabox and makes sure the settings are saved.
  37. *
  38. * @var WPSEO_Social_Admin
  39. */
  40. private $social_admin;
  41. /**
  42. * The constructor.
  43. *
  44. * @param string $taxonomy The taxonomy.
  45. * @param stdClass $term The term.
  46. */
  47. public function __construct( $taxonomy, $term ) {
  48. $this->term = $term;
  49. $this->taxonomy = $taxonomy;
  50. $this->taxonomy_tab_content = new WPSEO_Taxonomy_Fields_Presenter( $this->term );
  51. }
  52. /**
  53. * Shows the Yoast SEO metabox for the term.
  54. */
  55. public function display() {
  56. $content_sections = $this->get_content_sections();
  57. $product_title = 'Yoast SEO';
  58. if ( file_exists( WPSEO_PATH . 'premium/' ) ) {
  59. $product_title .= ' Premium';
  60. }
  61. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: $product_title is hardcoded.
  62. printf( '<div id="wpseo_meta" class="postbox yoast wpseo-taxonomy-metabox-postbox"><h2><span>%1$s</span></h2>', $product_title );
  63. echo '<div class="inside">';
  64. echo '<div id="taxonomy_overall"></div>';
  65. echo '<div class="wpseo-metabox-content">';
  66. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: $product_title is hardcoded.
  67. printf( '<div class="wpseo-metabox-menu"><ul role="tablist" class="yoast-aria-tabs" aria-label="%s">', $product_title );
  68. foreach ( $content_sections as $content_section ) {
  69. $content_section->display_link();
  70. }
  71. echo '</ul></div>';
  72. foreach ( $content_sections as $content_section ) {
  73. $content_section->display_content();
  74. }
  75. echo '</div></div>';
  76. echo '</div>';
  77. }
  78. /**
  79. * Returns the relevant metabox sections for the current view.
  80. *
  81. * @return WPSEO_Metabox_Section[]
  82. */
  83. private function get_content_sections() {
  84. $content_sections = [];
  85. $content_sections[] = $this->get_seo_meta_section();
  86. $readability_analysis = new WPSEO_Metabox_Analysis_Readability();
  87. if ( $readability_analysis->is_enabled() ) {
  88. $content_sections[] = $this->get_readability_meta_section();
  89. }
  90. $content_sections[] = $this->get_social_meta_section();
  91. return $content_sections;
  92. }
  93. /**
  94. * Returns the metabox section for the content analysis.
  95. *
  96. * @return WPSEO_Metabox_Section
  97. */
  98. private function get_seo_meta_section() {
  99. $taxonomy_content_fields = new WPSEO_Taxonomy_Content_Fields( $this->term );
  100. $content = $this->taxonomy_tab_content->html( $taxonomy_content_fields->get( $this->term ) );
  101. $seo_analysis = new WPSEO_Metabox_Analysis_SEO();
  102. $label = __( 'SEO', 'wordpress-seo' );
  103. if ( $seo_analysis->is_enabled() ) {
  104. $label = '<span class="wpseo-score-icon-container" id="wpseo-seo-score-icon"></span>' . $label;
  105. }
  106. $html_after = '';
  107. if ( WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) || WPSEO_Options::get( 'disableadvanced_meta' ) === false ) {
  108. $taxonomy_settings_fields = new WPSEO_Taxonomy_Settings_Fields( $this->term );
  109. $advanced_collapsible = new WPSEO_Paper_Presenter(
  110. __( 'Advanced', 'wordpress-seo' ),
  111. null,
  112. [
  113. 'collapsible' => true,
  114. 'class' => 'metabox wpseo-form wpseo-collapsible-container',
  115. 'content' => $this->taxonomy_tab_content->html( $taxonomy_settings_fields->get() ),
  116. 'paper_id' => 'collapsible-advanced-settings',
  117. ]
  118. );
  119. $html_after = '<div class="wpseo_content_wrapper">' . $advanced_collapsible->get_output() . '</div>';
  120. }
  121. return new WPSEO_Metabox_Section_React(
  122. 'content',
  123. $label,
  124. $content,
  125. [
  126. 'html_after' => $html_after,
  127. ]
  128. );
  129. }
  130. /**
  131. * Returns the metabox section for the readability analysis.
  132. *
  133. * @return WPSEO_Metabox_Section
  134. */
  135. private function get_readability_meta_section() {
  136. return new WPSEO_Metabox_Section_Readability();
  137. }
  138. /**
  139. * Returns the metabox section for the social settings.
  140. *
  141. * @return WPSEO_Metabox_Section
  142. */
  143. private function get_social_meta_section() {
  144. $this->taxonomy_social_fields = new WPSEO_Taxonomy_Social_Fields( $this->term );
  145. $this->social_admin = new WPSEO_Social_Admin();
  146. $collapsibles = [];
  147. $collapsibles[] = $this->create_collapsible( 'facebook', 'opengraph', 'facebook-alt', __( 'Facebook', 'wordpress-seo' ) );
  148. $collapsibles[] = $this->create_collapsible( 'twitter', 'twitter', 'twitter', __( 'Twitter', 'wordpress-seo' ) );
  149. return new WPSEO_Metabox_Collapsibles_Sections(
  150. 'social',
  151. '<span class="dashicons dashicons-share"></span>' . __( 'Social', 'wordpress-seo' ),
  152. $collapsibles
  153. );
  154. }
  155. /**
  156. * Creates a social network tab.
  157. *
  158. * @param string $name The name of the tab.
  159. * @param string $network The network of the tab.
  160. * @param string $icon The icon for the tab.
  161. * @param string $label The label for the tab.
  162. *
  163. * @return WPSEO_Metabox_Tab A WPSEO_Metabox_Tab instance.
  164. */
  165. private function create_collapsible( $name, $network, $icon, $label ) {
  166. if ( WPSEO_Options::get( $network ) !== true ) {
  167. return new WPSEO_Metabox_Null_Tab();
  168. }
  169. $meta_fields = $this->taxonomy_social_fields->get_by_network( $network );
  170. $content = $this->taxonomy_tab_content->html( $meta_fields );
  171. /**
  172. * If premium hide the form to show the social preview instead, we still need the fields to be output because
  173. * the values of the social preview are saved in the hidden field.
  174. */
  175. $features = new WPSEO_Features();
  176. if ( $features->is_premium() ) {
  177. $content = $this->hide_form( $content );
  178. }
  179. $tab_settings = new WPSEO_Metabox_Collapsible(
  180. $name,
  181. $this->social_admin->get_premium_notice( $network ) . $content,
  182. $label
  183. );
  184. return $tab_settings;
  185. }
  186. /**
  187. * Hides the given output when rendered to HTML.
  188. *
  189. * @param string $tab_content The social tab content.
  190. *
  191. * @return string The content.
  192. */
  193. private function hide_form( $tab_content ) {
  194. return '<div class="hidden">' . $tab_content . '</div>';
  195. }
  196. }