PageRenderTime 16ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/wordpress-seo/admin/metabox/class-metabox-tab-section.php

https://bitbucket.org/carloskikea/helpet
PHP | 168 lines | 79 code | 21 blank | 68 comment | 4 complexity | ad29df0a076a861488144c8a2e53407a MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Admin
  6 */
  7
  8/**
  9 * Generates and displays the HTML for a metabox section.
 10 */
 11class WPSEO_Metabox_Tab_Section implements WPSEO_Metabox_Section {
 12
 13	/**
 14	 * @var WPSEO_Metabox_Tab[]
 15	 */
 16	public $tabs = array();
 17
 18	/**
 19	 * @var string
 20	 */
 21	public $name;
 22
 23	/**
 24	 * @var string
 25	 */
 26	private $link_content;
 27
 28	/**
 29	 * @var string
 30	 */
 31	private $link_title;
 32
 33	/**
 34	 * @var string
 35	 */
 36	private $link_class;
 37
 38	/**
 39	 * @var string
 40	 */
 41	private $link_aria_label;
 42
 43	/**
 44	 * Constructor.
 45	 *
 46	 * @param string $name         The name of the section, used as an identifier in the html. Can only contain URL safe characters.
 47	 * @param string $link_content The text content of the section link.
 48	 * @param array  $tabs         The metabox tabs (`WPSEO_Metabox_Tabs[]`) to be included in the section.
 49	 * @param array  $options      Optional link attributes.
 50	 */
 51	public function __construct( $name, $link_content, array $tabs = array(), array $options = array() ) {
 52		$default_options = array(
 53			'link_title'      => '',
 54			'link_class'      => '',
 55			'link_aria_label' => '',
 56		);
 57
 58		$options = array_merge( $default_options, $options );
 59
 60		$this->name = $name;
 61
 62		// Filter out invalid tab instances.
 63		$valid_tabs = array_filter( $tabs, array( $this, 'is_valid_tab' ) );
 64
 65		foreach ( $valid_tabs as $tab ) {
 66			$this->add_tab( $tab );
 67		}
 68		$this->link_content    = $link_content;
 69		$this->link_title      = $options['link_title'];
 70		$this->link_class      = $options['link_class'];
 71		$this->link_aria_label = $options['link_aria_label'];
 72	}
 73
 74	/**
 75	 * Determines whether the passed tab is considered valid.
 76	 *
 77	 * @param mixed $tab The potential tab that needs to be validated.
 78	 *
 79	 * @return bool Whether or not the tab is valid.
 80	 */
 81	protected function is_valid_tab( $tab ) {
 82		if ( $tab instanceof WPSEO_Metabox_Tab && ! $tab instanceof WPSEO_Metabox_Null_Tab ) {
 83			return true;
 84		}
 85
 86		return false;
 87	}
 88
 89	/**
 90	 * Outputs the section link if any tab has been added.
 91	 */
 92	public function display_link() {
 93		if ( $this->has_tabs() ) {
 94			printf(
 95				'<li><a href="#wpseo-meta-section-%1$s" class="wpseo-meta-section-link %2$s"%3$s%4$s>%5$s</a></li>',
 96				esc_attr( $this->name ),
 97				esc_attr( $this->link_class ),
 98				( '' !== $this->link_title ) ? ' title="' . esc_attr( $this->link_title ) . '"' : '',
 99				( '' !== $this->link_aria_label ) ? ' aria-label="' . esc_attr( $this->link_aria_label ) . '"' : '',
100				$this->link_content
101			);
102		}
103	}
104
105	/**
106	 * Outputs the section content if any tab has been added.
107	 */
108	public function display_content() {
109		if ( $this->has_tabs() ) {
110			$html  = '<div id="%1$s" class="wpseo-meta-section">';
111			$html .= '<div class="wpseo-metabox-tabs-div">';
112			$html .= '<ul class="wpseo-metabox-tabs %2$s">%3$s</ul>%4$s';
113			$html .= '</div></div>';
114
115			printf(
116				$html,
117				esc_attr( 'wpseo-meta-section-' . $this->name ),
118				esc_attr( 'wpseo-metabox-tab-' . $this->name ),
119				$this->tab_links(),
120				$this->tab_content()
121			);
122		}
123	}
124
125	/**
126	 * Add a `WPSEO_Metabox_Tab` object to the tabs.
127	 *
128	 * @param WPSEO_Metabox_Tab $tab Tab to add.
129	 */
130	public function add_tab( WPSEO_Metabox_Tab $tab ) {
131		$this->tabs[] = $tab;
132	}
133
134	/**
135	 * Checks if any tabs have been added to the section.
136	 *
137	 * @return bool
138	 */
139	protected function has_tabs() {
140		return ! empty( $this->tabs );
141	}
142
143	/**
144	 * Concatenates all tabs' links into one html string.
145	 *
146	 * @return string
147	 */
148	private function tab_links() {
149		$links = '';
150		foreach ( $this->tabs as $tab ) {
151			$links .= $tab->link();
152		}
153		return $links;
154	}
155
156	/**
157	 * Concatenates all tabs' content into one html string.
158	 *
159	 * @return string
160	 */
161	private function tab_content() {
162		$content = '';
163		foreach ( $this->tabs as $tab ) {
164			$content .= $tab->content();
165		}
166		return $content;
167	}
168}