class-metabox-form-tab.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * @var string
  13. */
  14. private $name;
  15. /**
  16. * @var string
  17. */
  18. private $content;
  19. /**
  20. * @var string
  21. */
  22. private $link_content;
  23. /**
  24. * @var string
  25. */
  26. private $tab_class;
  27. /**
  28. * @var string
  29. */
  30. private $link_class;
  31. /**
  32. * @var string
  33. */
  34. private $link_title;
  35. /**
  36. * @var string
  37. */
  38. private $link_aria_label;
  39. /**
  40. * @var boolean
  41. */
  42. private $single;
  43. /**
  44. * Constructor.
  45. *
  46. * @param string $name The name of the tab, used as an identifier in the html.
  47. * @param string $content The tab content.
  48. * @param string $link_content The text content of the tab link.
  49. * @param array $options Optional link attributes.
  50. */
  51. public function __construct( $name, $content, $link_content, array $options = array() ) {
  52. $default_options = array(
  53. 'tab_class' => '',
  54. 'link_class' => '',
  55. 'link_title' => '',
  56. 'link_aria_label' => '',
  57. 'single' => false,
  58. );
  59. $options = array_merge( $default_options, $options );
  60. $this->name = $name;
  61. $this->content = $content;
  62. $this->link_content = $link_content;
  63. $this->tab_class = $options['tab_class'];
  64. $this->link_class = $options['link_class'];
  65. $this->link_title = $options['link_title'];
  66. $this->link_aria_label = $options['link_aria_label'];
  67. $this->single = $options['single'];
  68. }
  69. /**
  70. * Returns the html for the tab link.
  71. *
  72. * @return string
  73. */
  74. public function link() {
  75. $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>';
  76. if ( $this->single ) {
  77. $html = '<li class="%1$s%2$s"><span class="wpseo_tablink%3$s"%4$s%5$s>%6$s</span></li>';
  78. }
  79. return sprintf(
  80. $html,
  81. esc_attr( $this->name ),
  82. ( '' !== $this->tab_class ) ? ' ' . esc_attr( $this->tab_class ) : '',
  83. ( '' !== $this->link_class ) ? ' ' . esc_attr( $this->link_class ) : '',
  84. ( '' !== $this->link_title ) ? ' title="' . esc_attr( $this->link_title ) . '"' : '',
  85. ( '' !== $this->link_aria_label ) ? ' aria-label="' . esc_attr( $this->link_aria_label ) . '"' : '',
  86. $this->link_content
  87. );
  88. }
  89. /**
  90. * Returns the html for the tab content.
  91. *
  92. * @return string
  93. */
  94. public function content() {
  95. return sprintf(
  96. '<div id="%1$s" class="wpseotab %2$s">%3$s</div>',
  97. esc_attr( 'wpseo_' . $this->name ),
  98. esc_attr( $this->name ),
  99. $this->content
  100. );
  101. }
  102. }