class-view-utils.php 4.0 KB

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