class-admin-help-panel.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Generates the HTML for an inline Help Button and Panel.
  9. */
  10. class WPSEO_Admin_Help_Panel {
  11. /**
  12. * Unique identifier of the element the inline help refers to, used as an identifier in the html.
  13. *
  14. * @var string
  15. */
  16. private $id;
  17. /**
  18. * The Help Button text. Needs a properly escaped string.
  19. *
  20. * @var string
  21. */
  22. private $help_button_text;
  23. /**
  24. * The Help Panel content. Needs a properly escaped string (might contain HTML).
  25. *
  26. * @var string
  27. */
  28. private $help_content;
  29. /**
  30. * Optional Whether to print out a container div element for the Help Panel, used for styling.
  31. *
  32. * @var string
  33. */
  34. private $wrapper;
  35. /**
  36. * Constructor.
  37. *
  38. * @param string $id Unique identifier of the element the inline help refers to, used as
  39. * an identifier in the html.
  40. * @param string $help_button_text The Help Button text. Needs a properly escaped string.
  41. * @param string $help_content The Help Panel content. Needs a properly escaped string (might contain HTML).
  42. * @param string $wrapper Optional Whether to print out a container div element for the Help Panel,
  43. * used for styling.
  44. * Pass a `has-wrapper` value to print out the container. Default: no container.
  45. */
  46. public function __construct( $id, $help_button_text, $help_content, $wrapper = '' ) {
  47. $this->id = $id;
  48. $this->help_button_text = $help_button_text;
  49. $this->help_content = $help_content;
  50. $this->wrapper = $wrapper;
  51. }
  52. /**
  53. * Returns the html for the Help Button.
  54. *
  55. * @return string
  56. */
  57. public function get_button_html() {
  58. if ( ! $this->id || ! $this->help_button_text || ! $this->help_content ) {
  59. return '';
  60. }
  61. return sprintf(
  62. ' <button type="button" class="yoast_help yoast-help-button dashicons" id="%1$s-help-toggle" aria-expanded="false" aria-controls="%1$s-help"><span class="yoast-help-icon" aria-hidden="true"></span><span class="screen-reader-text">%2$s</span></button>',
  63. esc_attr( $this->id ),
  64. $this->help_button_text
  65. );
  66. }
  67. /**
  68. * Returns the html for the Help Panel.
  69. *
  70. * @return string
  71. */
  72. public function get_panel_html() {
  73. if ( ! $this->id || ! $this->help_button_text || ! $this->help_content ) {
  74. return '';
  75. }
  76. $wrapper_start = '';
  77. $wrapper_end = '';
  78. if ( 'has-wrapper' === $this->wrapper ) {
  79. $wrapper_start = '<div class="yoast-seo-help-container">';
  80. $wrapper_end = '</div>';
  81. }
  82. return sprintf(
  83. '%1$s<p id="%2$s-help" class="yoast-help-panel">%3$s</p>%4$s',
  84. $wrapper_start,
  85. esc_attr( $this->id ),
  86. $this->help_content,
  87. $wrapper_end
  88. );
  89. }
  90. }