/wp-content/plugins/wordpress-seo/admin/class-yoast-form.php

https://bitbucket.org/carloskikea/helpet · PHP · 656 lines · 348 code · 87 blank · 221 comment · 41 complexity · 6a6400524eb0b83b7124ee8643d796e9 MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Admin form class.
  9. *
  10. * @since 2.0
  11. */
  12. class Yoast_Form {
  13. /**
  14. * @var object Instance of this class
  15. * @since 2.0
  16. */
  17. public static $instance;
  18. /**
  19. * @var string
  20. * @since 2.0
  21. */
  22. public $option_name;
  23. /**
  24. * @var array
  25. * @since 2.0
  26. */
  27. public $options;
  28. /**
  29. * Get the singleton instance of this class
  30. *
  31. * @since 2.0
  32. *
  33. * @return Yoast_Form
  34. */
  35. public static function get_instance() {
  36. if ( ! ( self::$instance instanceof self ) ) {
  37. self::$instance = new self();
  38. }
  39. return self::$instance;
  40. }
  41. /**
  42. * Generates the header for admin pages
  43. *
  44. * @since 2.0
  45. *
  46. * @param bool $form Whether or not the form start tag should be included.
  47. * @param string $option The short name of the option to use for the current page.
  48. * @param bool $contains_files Whether the form should allow for file uploads.
  49. * @param bool $option_long_name Group name of the option.
  50. */
  51. public function admin_header( $form = true, $option = 'wpseo', $contains_files = false, $option_long_name = false ) {
  52. if ( ! $option_long_name ) {
  53. $option_long_name = WPSEO_Options::get_group_name( $option );
  54. }
  55. ?>
  56. <div class="wrap yoast wpseo-admin-page <?php echo esc_attr( 'page-' . $option ); ?>">
  57. <?php
  58. /**
  59. * Display the updated/error messages
  60. * Only needed as our settings page is not under options, otherwise it will automatically be included
  61. *
  62. * @see settings_errors()
  63. */
  64. require_once ABSPATH . 'wp-admin/options-head.php';
  65. ?>
  66. <h1 id="wpseo-title"><?php echo esc_html( get_admin_page_title() ); ?></h1>
  67. <div class="wpseo_content_wrapper">
  68. <div class="wpseo_content_cell" id="wpseo_content_top">
  69. <?php
  70. if ( $form === true ) {
  71. $enctype = ( $contains_files ) ? ' enctype="multipart/form-data"' : '';
  72. echo '<form action="' . esc_url( admin_url( 'options.php' ) ) . '" method="post" id="wpseo-conf"' . $enctype . ' accept-charset="' . esc_attr( get_bloginfo( 'charset' ) ) . '">';
  73. settings_fields( $option_long_name );
  74. }
  75. $this->set_option( $option );
  76. }
  77. /**
  78. * Set the option used in output for form elements
  79. *
  80. * @since 2.0
  81. *
  82. * @param string $option_name Option key.
  83. */
  84. public function set_option( $option_name ) {
  85. $this->option_name = $option_name;
  86. $this->options = $this->get_option();
  87. }
  88. /**
  89. * Sets a value in the options.
  90. *
  91. * @since 5.4
  92. *
  93. * @param string $key The key of the option to set.
  94. * @param mixed $value The value to set the option to.
  95. * @param bool $overwrite Whether to overwrite existing options. Default is false.
  96. */
  97. public function set_options_value( $key, $value, $overwrite = false ) {
  98. if ( $overwrite || ! array_key_exists( $key, $this->options ) ) {
  99. $this->options[ $key ] = $value;
  100. }
  101. }
  102. /**
  103. * Retrieve options based on whether we're on multisite or not.
  104. *
  105. * @since 1.2.4
  106. * @since 2.0 Moved to this class.
  107. *
  108. * @return array
  109. */
  110. public function get_option() {
  111. if ( is_network_admin() ) {
  112. return get_site_option( $this->option_name );
  113. }
  114. return get_option( $this->option_name );
  115. }
  116. /**
  117. * Generates the footer for admin pages
  118. *
  119. * @since 2.0
  120. *
  121. * @param bool $submit Whether or not a submit button and form end tag should be shown.
  122. * @param bool $show_sidebar Whether or not to show the banner sidebar - used by premium plugins to disable it.
  123. */
  124. public function admin_footer( $submit = true, $show_sidebar = true ) {
  125. if ( $submit ) {
  126. submit_button( __( 'Save changes', 'wordpress-seo' ) );
  127. echo '
  128. </form>';
  129. }
  130. /**
  131. * Apply general admin_footer hooks
  132. */
  133. do_action( 'wpseo_admin_footer', $this );
  134. /**
  135. * Run possibly set actions to add for example an i18n box
  136. */
  137. do_action( 'wpseo_admin_promo_footer' );
  138. echo '
  139. </div><!-- end of div wpseo_content_top -->';
  140. if ( $show_sidebar ) {
  141. $this->admin_sidebar();
  142. }
  143. echo '</div><!-- end of div wpseo_content_wrapper -->';
  144. do_action( 'wpseo_admin_below_content', $this );
  145. echo '
  146. </div><!-- end of wrap -->';
  147. }
  148. /**
  149. * Generates the sidebar for admin pages.
  150. *
  151. * @since 2.0
  152. */
  153. public function admin_sidebar() {
  154. // No banners in Premium.
  155. if ( class_exists( 'WPSEO_Product_Premium' ) ) {
  156. $product_premium = new WPSEO_Product_Premium();
  157. $extension_manager = new WPSEO_Extension_Manager();
  158. if ( $extension_manager->is_activated( $product_premium->get_slug() ) ) {
  159. return;
  160. }
  161. }
  162. $sidebar_renderer = new WPSEO_Admin_Banner_Sidebar_Renderer( new WPSEO_Admin_Banner_Spot_Renderer() );
  163. $banner_renderer = new WPSEO_Admin_Banner_Renderer();
  164. $banner_renderer->set_base_path( plugins_url( 'images/banner/', WPSEO_FILE ) );
  165. /* translators: %1$s expands to "Yoast". */
  166. $sidebar = new WPSEO_Admin_Banner_Sidebar( sprintf( __( '%1s recommendations for you', 'wordpress-seo' ), 'Yoast' ), $banner_renderer );
  167. $sidebar->initialize( new WPSEO_Features() );
  168. echo $sidebar_renderer->render( $sidebar );
  169. }
  170. /**
  171. * Output a label element
  172. *
  173. * @since 2.0
  174. *
  175. * @param string $text Label text string.
  176. * @param array $attr HTML attributes set.
  177. */
  178. public function label( $text, $attr ) {
  179. $attr = wp_parse_args( $attr, array(
  180. 'class' => 'checkbox',
  181. 'close' => true,
  182. 'for' => '',
  183. )
  184. );
  185. echo "<label class='" . esc_attr( $attr['class'] ) . "' for='" . esc_attr( $attr['for'] ) . "'>$text";
  186. if ( $attr['close'] ) {
  187. echo '</label>';
  188. }
  189. }
  190. /**
  191. * Output a legend element.
  192. *
  193. * @since 3.4
  194. *
  195. * @param string $text Legend text string.
  196. * @param array $attr HTML attributes set.
  197. */
  198. public function legend( $text, $attr ) {
  199. $attr = wp_parse_args( $attr, array(
  200. 'id' => '',
  201. 'class' => '',
  202. )
  203. );
  204. $id = ( '' === $attr['id'] ) ? '' : ' id="' . esc_attr( $attr['id'] ) . '"';
  205. echo '<legend class="yoast-form-legend ' . esc_attr( $attr['class'] ) . '"' . $id . '>' . $text . '</legend>';
  206. }
  207. /**
  208. * Create a Checkbox input field.
  209. *
  210. * @since 2.0
  211. *
  212. * @param string $var The variable within the option to create the checkbox for.
  213. * @param string $label The label to show for the variable.
  214. * @param bool $label_left Whether the label should be left (true) or right (false).
  215. */
  216. public function checkbox( $var, $label, $label_left = false ) {
  217. if ( ! isset( $this->options[ $var ] ) ) {
  218. $this->options[ $var ] = false;
  219. }
  220. if ( $this->options[ $var ] === true ) {
  221. $this->options[ $var ] = 'on';
  222. }
  223. $class = '';
  224. if ( $label_left !== false ) {
  225. if ( ! empty( $label_left ) ) {
  226. $label_left .= ':';
  227. }
  228. $this->label( $label_left, array( 'for' => $var ) );
  229. }
  230. else {
  231. $class = 'double';
  232. }
  233. echo '<input class="checkbox ', esc_attr( $class ), '" type="checkbox" id="', esc_attr( $var ), '" name="', esc_attr( $this->option_name ), '[', esc_attr( $var ), ']" value="on"', checked( $this->options[ $var ], 'on', false ), '/>';
  234. if ( ! empty( $label ) ) {
  235. $this->label( $label, array( 'for' => $var ) );
  236. }
  237. echo '<br class="clear" />';
  238. }
  239. /**
  240. * Create a light switch input field using a single checkbox.
  241. *
  242. * @since 3.1
  243. *
  244. * @param string $var The variable within the option to create the checkbox for.
  245. * @param string $label The label element text for the checkbox.
  246. * @param array $buttons Array of two visual labels for the buttons (defaults Disabled/Enabled).
  247. * @param boolean $reverse Reverse order of buttons (default true).
  248. * @param string $help Inline Help that will be printed out before the visible toggles text.
  249. */
  250. public function light_switch( $var, $label, $buttons = array(), $reverse = true, $help = '' ) {
  251. if ( ! isset( $this->options[ $var ] ) ) {
  252. $this->options[ $var ] = false;
  253. }
  254. if ( $this->options[ $var ] === true ) {
  255. $this->options[ $var ] = 'on';
  256. }
  257. $class = 'switch-light switch-candy switch-yoast-seo';
  258. $aria_labelledby = esc_attr( $var ) . '-label';
  259. if ( $reverse ) {
  260. $class .= ' switch-yoast-seo-reverse';
  261. }
  262. if ( empty( $buttons ) ) {
  263. $buttons = array( __( 'Disabled', 'wordpress-seo' ), __( 'Enabled', 'wordpress-seo' ) );
  264. }
  265. list( $off_button, $on_button ) = $buttons;
  266. $help_class = '';
  267. $screen_reader_text_class = '';
  268. $help_class = ! empty( $help ) ? ' switch-container__has-help' : '';
  269. echo "<div class='switch-container$help_class'>",
  270. "<span class='switch-light-visual-label'>{$label}</span>" . $help,
  271. '<label class="', $class, '"><b class="switch-yoast-seo-jaws-a11y">&nbsp;</b>',
  272. '<input type="checkbox" aria-labelledby="', $aria_labelledby, '" id="', esc_attr( $var ), '" name="', esc_attr( $this->option_name ), '[', esc_attr( $var ), ']" value="on"', checked( $this->options[ $var ], 'on', false ), '/>',
  273. "<b class='label-text screen-reader-text' id='{$aria_labelledby}'>{$label}</b>",
  274. '<span aria-hidden="true">
  275. <span>', esc_html( $off_button ) ,'</span>
  276. <span>', esc_html( $on_button ) ,'</span>
  277. <a></a>
  278. </span>
  279. </label><div class="clear"></div></div>';
  280. }
  281. /**
  282. * Create a Text input field.
  283. *
  284. * @since 2.0
  285. * @since 2.1 Introduced the `$attr` parameter.
  286. *
  287. * @param string $var The variable within the option to create the text input field for.
  288. * @param string $label The label to show for the variable.
  289. * @param array|string $attr Extra class to add to the input field.
  290. */
  291. public function textinput( $var, $label, $attr = array() ) {
  292. if ( ! is_array( $attr ) ) {
  293. $attr = array(
  294. 'class' => $attr,
  295. );
  296. }
  297. $attr = wp_parse_args( $attr, array(
  298. 'placeholder' => '',
  299. 'class' => '',
  300. ) );
  301. $val = ( isset( $this->options[ $var ] ) ) ? $this->options[ $var ] : '';
  302. $this->label(
  303. $label . ':',
  304. array(
  305. 'for' => $var,
  306. 'class' => 'textinput',
  307. )
  308. );
  309. echo '<input class="textinput ' . esc_attr( $attr['class'] ) . ' " placeholder="' . esc_attr( $attr['placeholder'] ) . '" type="text" id="', esc_attr( $var ), '" name="', esc_attr( $this->option_name ), '[', esc_attr( $var ), ']" value="', esc_attr( $val ), '"/>', '<br class="clear" />';
  310. }
  311. /**
  312. * Create a textarea.
  313. *
  314. * @since 2.0
  315. *
  316. * @param string $var The variable within the option to create the textarea for.
  317. * @param string $label The label to show for the variable.
  318. * @param array $attr The CSS class to assign to the textarea.
  319. */
  320. public function textarea( $var, $label, $attr = array() ) {
  321. if ( ! is_array( $attr ) ) {
  322. $attr = array(
  323. 'class' => $attr,
  324. );
  325. }
  326. $attr = wp_parse_args( $attr, array(
  327. 'cols' => '',
  328. 'rows' => '',
  329. 'class' => '',
  330. ) );
  331. $val = ( isset( $this->options[ $var ] ) ) ? $this->options[ $var ] : '';
  332. $this->label(
  333. $label . ':',
  334. array(
  335. 'for' => $var,
  336. 'class' => 'textinput',
  337. )
  338. );
  339. echo '<textarea cols="' . esc_attr( $attr['cols'] ) . '" rows="' . esc_attr( $attr['rows'] ) . '" class="textinput ' . esc_attr( $attr['class'] ) . '" id="' . esc_attr( $var ) . '" name="' . esc_attr( $this->option_name ) . '[' . esc_attr( $var ) . ']">' . esc_textarea( $val ) . '</textarea><br class="clear" />';
  340. }
  341. /**
  342. * Create a hidden input field.
  343. *
  344. * @since 2.0
  345. *
  346. * @param string $var The variable within the option to create the hidden input for.
  347. * @param string $id The ID of the element.
  348. */
  349. public function hidden( $var, $id = '' ) {
  350. $val = ( isset( $this->options[ $var ] ) ) ? $this->options[ $var ] : '';
  351. if ( is_bool( $val ) ) {
  352. $val = ( $val === true ) ? 'true' : 'false';
  353. }
  354. if ( '' === $id ) {
  355. $id = 'hidden_' . $var;
  356. }
  357. echo '<input type="hidden" id="' . esc_attr( $id ) . '" name="' . esc_attr( $this->option_name ) . '[' . esc_attr( $var ) . ']" value="' . esc_attr( $val ) . '"/>';
  358. }
  359. /**
  360. * Create a Select Box.
  361. *
  362. * @since 2.0
  363. *
  364. * @param string $field_name The variable within the option to create the select for.
  365. * @param string $label The label to show for the variable.
  366. * @param array $select_options The select options to choose from.
  367. */
  368. public function select( $field_name, $label, array $select_options ) {
  369. if ( empty( $select_options ) ) {
  370. return;
  371. }
  372. $this->label(
  373. $label . ':',
  374. array(
  375. 'for' => $field_name,
  376. 'class' => 'select',
  377. )
  378. );
  379. $select_name = esc_attr( $this->option_name ) . '[' . esc_attr( $field_name ) . ']';
  380. $active_option = ( isset( $this->options[ $field_name ] ) ) ? $this->options[ $field_name ] : '';
  381. $select = new Yoast_Input_Select( $field_name, $select_name, $select_options, $active_option );
  382. $select->add_attribute( 'class', 'select' );
  383. $select->output_html();
  384. echo '<br class="clear"/>';
  385. }
  386. /**
  387. * Create a File upload field.
  388. *
  389. * @since 2.0
  390. *
  391. * @param string $var The variable within the option to create the file upload field for.
  392. * @param string $label The label to show for the variable.
  393. */
  394. public function file_upload( $var, $label ) {
  395. $val = '';
  396. if ( isset( $this->options[ $var ] ) && is_array( $this->options[ $var ] ) ) {
  397. $val = $this->options[ $var ]['url'];
  398. }
  399. $var_esc = esc_attr( $var );
  400. $this->label(
  401. $label . ':',
  402. array(
  403. 'for' => $var,
  404. 'class' => 'select',
  405. )
  406. );
  407. echo '<input type="file" value="' . esc_attr( $val ) . '" class="textinput" name="' . esc_attr( $this->option_name ) . '[' . $var_esc . ']" id="' . $var_esc . '"/>';
  408. // Need to save separate array items in hidden inputs, because empty file inputs type will be deleted by settings API.
  409. if ( ! empty( $this->options[ $var ] ) ) {
  410. $this->hidden( 'file', $this->option_name . '_file' );
  411. $this->hidden( 'url', $this->option_name . '_url' );
  412. $this->hidden( 'type', $this->option_name . '_type' );
  413. }
  414. echo '<br class="clear"/>';
  415. }
  416. /**
  417. * Media input
  418. *
  419. * @since 2.0
  420. *
  421. * @param string $var Option name.
  422. * @param string $label Label message.
  423. */
  424. public function media_input( $var, $label ) {
  425. $val = '';
  426. if ( isset( $this->options[ $var ] ) ) {
  427. $val = $this->options[ $var ];
  428. }
  429. $var_esc = esc_attr( $var );
  430. $this->label(
  431. $label . ':',
  432. array(
  433. 'for' => 'wpseo_' . $var,
  434. 'class' => 'select',
  435. )
  436. );
  437. echo '<input class="textinput" id="wpseo_', $var_esc, '" type="text" size="36" name="', esc_attr( $this->option_name ), '[', $var_esc, ']" value="', esc_attr( $val ), '" />';
  438. echo '<input id="wpseo_', $var_esc, '_button" class="wpseo_image_upload_button button" type="button" value="', esc_attr__( 'Upload Image', 'wordpress-seo' ), '" />';
  439. echo '<br class="clear"/>';
  440. }
  441. /**
  442. * Create a Radio input field.
  443. *
  444. * @since 2.0
  445. *
  446. * @param string $var The variable within the option to create the radio button for.
  447. * @param array $values The radio options to choose from.
  448. * @param string $legend Optional. The legend to show for the field set, if any.
  449. * @param array $legend_attr Optional. The attributes for the legend, if any.
  450. */
  451. public function radio( $var, $values, $legend = '', $legend_attr = array() ) {
  452. if ( ! is_array( $values ) || $values === array() ) {
  453. return;
  454. }
  455. if ( ! isset( $this->options[ $var ] ) ) {
  456. $this->options[ $var ] = false;
  457. }
  458. $var_esc = esc_attr( $var );
  459. echo '<fieldset class="yoast-form-fieldset wpseo_radio_block" id="' . $var_esc . '">';
  460. if ( is_string( $legend ) && '' !== $legend ) {
  461. $legend_attr = wp_parse_args( $legend_attr, array(
  462. 'id' => '',
  463. 'class' => 'radiogroup',
  464. ) );
  465. $this->legend( $legend, $legend_attr );
  466. }
  467. foreach ( $values as $key => $value ) {
  468. $key_esc = esc_attr( $key );
  469. echo '<input type="radio" class="radio" id="' . $var_esc . '-' . $key_esc . '" name="' . esc_attr( $this->option_name ) . '[' . $var_esc . ']" value="' . $key_esc . '" ' . checked( $this->options[ $var ], $key_esc, false ) . ' />';
  470. $this->label(
  471. $value,
  472. array(
  473. 'for' => $var_esc . '-' . $key_esc,
  474. 'class' => 'radio',
  475. )
  476. );
  477. }
  478. echo '</fieldset>';
  479. }
  480. /**
  481. * Create a toggle switch input field using two radio buttons.
  482. *
  483. * @since 3.1
  484. *
  485. * @param string $var The variable within the option to create the radio buttons for.
  486. * @param array $values Associative array of on/off keys and their values to be used as
  487. * the label elements text for the radio buttons. Optionally, each
  488. * value can be an array of visible label text and screen reader text.
  489. * @param string $label The visual label for the radio buttons group, used as the fieldset legend.
  490. * @param string $help Inline Help that will be printed out before the visible toggles text.
  491. */
  492. public function toggle_switch( $var, $values, $label, $help = '' ) {
  493. if ( ! is_array( $values ) || $values === array() ) {
  494. return;
  495. }
  496. if ( ! isset( $this->options[ $var ] ) ) {
  497. $this->options[ $var ] = false;
  498. }
  499. if ( $this->options[ $var ] === true ) {
  500. $this->options[ $var ] = 'on';
  501. }
  502. if ( $this->options[ $var ] === false ) {
  503. $this->options[ $var ] = 'off';
  504. }
  505. $help_class = ! empty( $help ) ? ' switch-container__has-help' : '';
  506. $var_esc = esc_attr( $var );
  507. printf( '<div class="%s">', esc_attr( 'switch-container' . $help_class ) );
  508. echo '<fieldset id="', $var_esc, '" class="fieldset-switch-toggle"><legend>', $label, '</legend>', $help,
  509. '<div class="switch-toggle switch-candy switch-yoast-seo">';
  510. foreach ( $values as $key => $value ) {
  511. $screen_reader_text = '';
  512. $screen_reader_text_html = '';
  513. if ( is_array( $value ) ) {
  514. $screen_reader_text = $value['screen_reader_text'];
  515. $screen_reader_text_html = '<span class="screen-reader-text"> ' . esc_html( $screen_reader_text ) . '</span>';
  516. $value = $value['text'];
  517. }
  518. $key_esc = esc_attr( $key );
  519. $for = $var_esc . '-' . $key_esc;
  520. echo '<input type="radio" id="' . $for . '" name="' . esc_attr( $this->option_name ) . '[' . $var_esc . ']" value="' . $key_esc . '" ' . checked( $this->options[ $var ], $key_esc, false ) . ' />',
  521. '<label for="', $for, '">', esc_html( $value ), $screen_reader_text_html,'</label>';
  522. }
  523. echo '<a></a></div></fieldset><div class="clear"></div></div>' . "\n\n";
  524. }
  525. /**
  526. * Creates a toggle switch to define whether an indexable should be indexed or not.
  527. *
  528. * @param string $var The variable within the option to create the radio buttons for.
  529. * @param string $label The visual label for the radio buttons group, used as the fieldset legend.
  530. * @param string $help Inline Help that will be printed out before the visible toggles text.
  531. *
  532. * @return void
  533. */
  534. public function index_switch( $var, $label, $help = '' ) {
  535. $index_switch_values = array(
  536. 'off' => __( 'Yes', 'wordpress-seo' ),
  537. 'on' => __( 'No', 'wordpress-seo' ),
  538. );
  539. $this->toggle_switch(
  540. $var,
  541. $index_switch_values,
  542. sprintf(
  543. /* translators: %s expands to an indexable object's name, like a post type or taxonomy */
  544. esc_html__( 'Show %s in search results?', 'wordpress-seo' ),
  545. '<strong>' . esc_html( $label ) . '</strong>'
  546. ),
  547. $help
  548. );
  549. }
  550. /**
  551. * Creates a toggle switch to show hide certain options.
  552. *
  553. * @param string $var The variable within the option to create the radio buttons for.
  554. * @param string $label The visual label for the radio buttons group, used as the fieldset legend.
  555. * @param bool $inverse_keys Whether or not the option keys need to be inverted to support older functions.
  556. * @param string $help Inline Help that will be printed out before the visible toggles text.
  557. *
  558. * @return void
  559. */
  560. public function show_hide_switch( $var, $label, $inverse_keys = false, $help = '' ) {
  561. $on_key = ( $inverse_keys ) ? 'off' : 'on';
  562. $off_key = ( $inverse_keys ) ? 'on' : 'off';
  563. $show_hide_switch = array(
  564. $on_key => __( 'Show', 'wordpress-seo' ),
  565. $off_key => __( 'Hide', 'wordpress-seo' ),
  566. );
  567. $this->toggle_switch( $var, $show_hide_switch, $label, $help );
  568. }
  569. }