class-metabox-form-tab.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Generates the HTML for a metabox tab.
  9. */
  10. class WPSEO_Metabox_Form_Tab implements WPSEO_Metabox_Tab {
  11. /**
  12. * The tab identifier.
  13. *
  14. * @var string
  15. */
  16. private $name;
  17. /**
  18. * The tab content.
  19. *
  20. * @var string
  21. */
  22. private $content;
  23. /**
  24. * The tab link content.
  25. *
  26. * @var string
  27. */
  28. private $link_content;
  29. /**
  30. * Additional tab content class.
  31. *
  32. * @var string
  33. */
  34. private $tab_class;
  35. /**
  36. * Additional tab link class.
  37. *
  38. * @var string
  39. */
  40. private $link_class;
  41. /**
  42. * Title attribute on the link span.
  43. *
  44. * @var string
  45. */
  46. private $link_title;
  47. /**
  48. * Arial label attribute on the link span.
  49. *
  50. * @var string
  51. */
  52. private $link_aria_label;
  53. /**
  54. * Does it contain a single tab.
  55. *
  56. * @var boolean
  57. */
  58. private $single;
  59. /**
  60. * Constructor.
  61. *
  62. * @param string $name The name of the tab, used as an identifier in the html.
  63. * @param string $content The tab content.
  64. * @param string $link_content The text content of the tab link.
  65. * @param array $options Optional link attributes.
  66. */
  67. public function __construct( $name, $content, $link_content, array $options = [] ) {
  68. $default_options = [
  69. 'tab_class' => '',
  70. 'link_class' => '',
  71. 'link_title' => '',
  72. 'link_aria_label' => '',
  73. 'single' => false,
  74. ];
  75. $options = array_merge( $default_options, $options );
  76. $this->name = $name;
  77. $this->content = $content;
  78. $this->link_content = $link_content;
  79. $this->tab_class = $options['tab_class'];
  80. $this->link_class = $options['link_class'];
  81. $this->link_title = $options['link_title'];
  82. $this->link_aria_label = $options['link_aria_label'];
  83. $this->single = $options['single'];
  84. }
  85. /**
  86. * Returns the html for the tab link.
  87. *
  88. * @return string
  89. */
  90. public function link() {
  91. $html = '<li class="%1$s%2$s"><a class="wpseo_tablink%3$s" href="#wpseo_%1$s"%4$s%5$s>%6$s</a></li>';
  92. if ( $this->single ) {
  93. $html = '<li class="%1$s%2$s"><span class="wpseo_tablink%3$s"%4$s%5$s>%6$s</span></li>';
  94. }
  95. return sprintf(
  96. $html,
  97. esc_attr( $this->name ),
  98. ( '' !== $this->tab_class ) ? ' ' . esc_attr( $this->tab_class ) : '',
  99. ( '' !== $this->link_class ) ? ' ' . esc_attr( $this->link_class ) : '',
  100. ( '' !== $this->link_title ) ? ' title="' . esc_attr( $this->link_title ) . '"' : '',
  101. ( '' !== $this->link_aria_label ) ? ' aria-label="' . esc_attr( $this->link_aria_label ) . '"' : '',
  102. $this->link_content
  103. );
  104. }
  105. /**
  106. * Returns the html for the tab content.
  107. *
  108. * @return string
  109. */
  110. public function content() {
  111. return sprintf(
  112. '<div id="%1$s" class="wpseotab %2$s">%3$s</div>',
  113. esc_attr( 'wpseo_' . $this->name ),
  114. esc_attr( $this->name ),
  115. $this->content
  116. );
  117. }
  118. }