class-view-utils.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Views
  6. */
  7. /**
  8. * Class Yoast_View_Utils.
  9. */
  10. class Yoast_View_Utils {
  11. /**
  12. * Form to use.
  13. *
  14. * @var Yoast_Form
  15. */
  16. protected $form;
  17. /**
  18. * Yoast_View_Utils constructor.
  19. */
  20. public function __construct() {
  21. $this->form = Yoast_Form::get_instance();
  22. }
  23. /**
  24. * Shows the search results help question mark and help section.
  25. *
  26. * Used for all the Help sections for indexable objects like post types, taxonomies, or archives.
  27. *
  28. * @param string|object $post_type The post type to show the search results help for.
  29. * @param string $help_text_switch Switch the help text to one that's more appropriate
  30. * for the indexable object type the help section is for.
  31. *
  32. * @return object The help panel instance.
  33. */
  34. public function search_results_setting_help( $post_type, $help_text_switch = '' ) {
  35. if ( ! is_object( $post_type ) ) {
  36. $post_type = get_post_type_object( $post_type );
  37. }
  38. /* translators: 1: expands to an indexable object's name, like a post type or taxonomy; 2: expands to <code>noindex</code>; 3: link open tag; 4: link close tag. */
  39. $help_text = esc_html__( 'Not showing %1$s in the search results technically means those will have a %2$s robots meta and will be excluded from XML sitemaps. %3$sMore info on the search results settings%4$s.', 'wordpress-seo' );
  40. if ( $help_text_switch === 'archive' ) {
  41. /* translators: 1: expands to an indexable object's name, like a post type or taxonomy; 2: expands to <code>noindex</code>; 3: link open tag; 4: link close tag. */
  42. $help_text = esc_html__( 'Not showing the archive for %1$s in the search results technically means those will have a %2$s robots meta and will be excluded from XML sitemaps. %3$sMore info on the search results settings%4$s.', 'wordpress-seo' );
  43. }
  44. $help_panel = new WPSEO_Admin_Help_Panel(
  45. // Sometimes the same post type is used more than once in the same page, we need a unique ID though.
  46. uniqid( 'noindex-' . $post_type->name ),
  47. esc_html__( 'Help on this search results setting', 'wordpress-seo' ),
  48. sprintf(
  49. $help_text,
  50. $post_type->labels->name,
  51. '<code>noindex</code>',
  52. '<a href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/show-x' ) ) . '" target="_blank" rel="noopener noreferrer">',
  53. '</a>'
  54. )
  55. );
  56. return $help_panel;
  57. }
  58. /**
  59. * Shows the search appearance settings for a post type.
  60. *
  61. * @param string|object $post_type The post type to show the search appearance settings for.
  62. * @param bool $paper_style Whether or not the paper style should be shown.
  63. *
  64. * @return void
  65. */
  66. public function show_post_type_settings( $post_type, $paper_style = false ) {
  67. if ( ! is_object( $post_type ) ) {
  68. $post_type = get_post_type_object( $post_type );
  69. }
  70. $show_post_type_help = $this->search_results_setting_help( $post_type );
  71. $noindex_option_name = 'noindex-' . $post_type->name;
  72. $this->form->index_switch(
  73. $noindex_option_name,
  74. $post_type->labels->name,
  75. $show_post_type_help->get_button_html() . $show_post_type_help->get_panel_html()
  76. );
  77. $this->form->show_hide_switch(
  78. 'showdate-' . $post_type->name,
  79. __( 'Date in Snippet Preview', 'wordpress-seo' )
  80. );
  81. $this->form->show_hide_switch(
  82. 'display-metabox-pt-' . $post_type->name,
  83. /* translators: %1$s expands to Yoast SEO */
  84. sprintf( __( '%1$s Meta Box', 'wordpress-seo' ), 'Yoast SEO' )
  85. );
  86. $recommended_replace_vars = new WPSEO_Admin_Recommended_Replace_Vars();
  87. $editor_specific_replace_vars = new WPSEO_Admin_Editor_Specific_Replace_Vars();
  88. $editor = new WPSEO_Replacevar_Editor(
  89. $this->form,
  90. [
  91. 'title' => 'title-' . $post_type->name,
  92. 'description' => 'metadesc-' . $post_type->name,
  93. 'page_type_recommended' => $recommended_replace_vars->determine_for_post_type( $post_type->name ),
  94. 'page_type_specific' => $editor_specific_replace_vars->determine_for_post_type( $post_type->name ),
  95. 'paper_style' => $paper_style,
  96. ]
  97. );
  98. $editor->render();
  99. }
  100. }