Yoast_Form

Admin form class.

Defined (1)

The class is defined in the following location(s).

/admin/class-yoast-form.php  
  1. class Yoast_Form { 
  2.  
  3. /** 
  4. * @var object Instance of this class 
  5. * @since 2.0 
  6. */ 
  7. public static $instance; 
  8.  
  9. /** 
  10. * @var string 
  11. * @since 2.0 
  12. */ 
  13. public $option_name; 
  14.  
  15. /** 
  16. * @var array 
  17. * @since 2.0 
  18. */ 
  19. public $options; 
  20.  
  21. /** 
  22. * Get the singleton instance of this class 
  23. * @since 2.0 
  24. * @return Yoast_Form 
  25. */ 
  26. public static function get_instance() { 
  27. if ( ! ( self::$instance instanceof self ) ) { 
  28. self::$instance = new self(); 
  29.  
  30. return self::$instance; 
  31.  
  32. /** 
  33. * Generates the header for admin pages 
  34. * @since 2.0 
  35. * @param bool $form Whether or not the form start tag should be included. 
  36. * @param string $option The short name of the option to use for the current page. 
  37. * @param bool $contains_files Whether the form should allow for file uploads. 
  38. * @param bool $option_long_name Group name of the option. 
  39. */ 
  40. public function admin_header( $form = true, $option = 'wpseo', $contains_files = false, $option_long_name = false ) { 
  41. if ( ! $option_long_name ) { 
  42. $option_long_name = WPSEO_Options::get_group_name( $option ); 
  43. ?> 
  44. <div class="wrap yoast wpseo-admin-page page-<?php echo $option; ?>"> 
  45. <?php 
  46. /** 
  47. * Display the updated/error messages 
  48. * Only needed as our settings page is not under options, otherwise it will automatically be included 
  49. * @see settings_errors() 
  50. */ 
  51. require_once( ABSPATH . 'wp-admin/options-head.php' ); 
  52. ?> 
  53. <h1 id="wpseo-title"><?php echo esc_html( get_admin_page_title() ); ?></h1> 
  54. <div class="wpseo_content_wrapper"> 
  55. <div class="wpseo_content_cell" id="wpseo_content_top"> 
  56. <?php 
  57. if ( $form === true ) { 
  58. $enctype = ( $contains_files ) ? ' enctype="multipart/form-data"' : ''; 
  59. echo '<form action="' . esc_url( admin_url( 'options.php' ) ) . '" method="post" id="wpseo-conf"' . $enctype . ' accept-charset="' . esc_attr( get_bloginfo( 'charset' ) ) . '">'; 
  60. settings_fields( $option_long_name ); 
  61. $this->set_option( $option ); 
  62.  
  63. /** 
  64. * Set the option used in output for form elements 
  65. * @since 2.0 
  66. * @param string $option_name Option key. 
  67. */ 
  68. public function set_option( $option_name ) { 
  69. $this->option_name = $option_name; 
  70. $this->options = $this->get_option(); 
  71.  
  72. /** 
  73. * Retrieve options based on whether we're on multisite or not. 
  74. * @since 1.2.4 
  75. * @since 2.0 Moved to this class. 
  76. * @return array 
  77. */ 
  78. private function get_option() { 
  79. if ( is_network_admin() ) { 
  80. return get_site_option( $this->option_name ); 
  81.  
  82. return get_option( $this->option_name ); 
  83.  
  84. /** 
  85. * Generates the footer for admin pages 
  86. * @since 2.0 
  87. * @param bool $submit Whether or not a submit button and form end tag should be shown. 
  88. * @param bool $show_sidebar Whether or not to show the banner sidebar - used by premium plugins to disable it. 
  89. */ 
  90. public function admin_footer( $submit = true, $show_sidebar = true ) { 
  91. if ( $submit ) { 
  92. submit_button( __( 'Save changes', 'wordpress-seo' ) ); 
  93.  
  94. echo ' 
  95. </form>'; 
  96.  
  97. /** 
  98. * Apply general admin_footer hooks 
  99. */ 
  100. do_action( 'wpseo_admin_footer' ); 
  101.  
  102. /** 
  103. * Run possibly set actions to add for example an i18n box 
  104. */ 
  105. do_action( 'wpseo_admin_promo_footer' ); 
  106.  
  107. echo ' 
  108. </div><!-- end of div wpseo_content_top -->'; 
  109.  
  110. if ( $show_sidebar ) { 
  111. $this->admin_sidebar(); 
  112.  
  113. echo '</div><!-- end of div wpseo_content_wrapper -->'; 
  114.  
  115.  
  116. if ( ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) ) { 
  117. $xdebug = ( extension_loaded( 'xdebug' ) ? true : false ); 
  118. echo ' 
  119. <div id="poststuff"> 
  120. <div id="wpseo-debug-info" class="postbox"> 
  121.  
  122. <h2 class="hndle"><span>' . __( 'Debug Information', 'wordpress-seo' ) . '</span></h2> 
  123. <div class="inside"> 
  124. <h3 class="wpseo-debug-heading">' . esc_html( __( 'Current option:', 'wordpress-seo' ) ) . ' <span class="wpseo-debug">' . esc_html( $this->option_name ) . '</span></h3> 
  125. ' . ( ( $xdebug ) ? '' : '<pre>' ); 
  126. var_dump( $this->get_option() ); 
  127. echo ' 
  128. ' . ( ( $xdebug ) ? '' : '</pre>' ) . ' 
  129. </div> 
  130. </div> 
  131. </div>'; 
  132.  
  133. echo ' 
  134. </div><!-- end of wrap -->'; 
  135.  
  136. /** 
  137. * Generates the sidebar for admin pages. 
  138. * @since 2.0 
  139. */ 
  140. public function admin_sidebar() { 
  141.  
  142. // No banners in Premium. 
  143. if ( class_exists( 'WPSEO_Product_Premium' ) ) { 
  144. $license_manager = new Yoast_Plugin_License_Manager( new WPSEO_Product_Premium() ); 
  145. if ( $license_manager->license_is_valid() ) { 
  146. return; 
  147.  
  148. $sidebar_renderer = new WPSEO_Admin_Banner_Sidebar_Renderer( new WPSEO_Admin_Banner_Spot_Renderer() ); 
  149.  
  150. $banner_renderer = new WPSEO_Admin_Banner_Renderer; 
  151. $banner_renderer->set_base_path( plugins_url( 'images/banner/', WPSEO_FILE ) ); 
  152.  
  153. $sidebar = new WPSEO_Admin_Banner_Sidebar( sprintf( '%1s recommendations for you', 'Yoast' ), $banner_renderer ); 
  154. $sidebar->initialize( new WPSEO_Features() ); 
  155.  
  156. echo $sidebar_renderer->render( $sidebar ); 
  157.  
  158.  
  159. /** 
  160. * Output a label element 
  161. * @since 2.0 
  162. * @param string $text Label text string. 
  163. * @param array $attr HTML attributes set. 
  164. */ 
  165. public function label( $text, $attr ) { 
  166. $attr = wp_parse_args( $attr, array( 
  167. 'class' => 'checkbox',  
  168. 'close' => true,  
  169. 'for' => '',  
  170. ); 
  171. echo "<label class='" . $attr['class'] . "' for='" . esc_attr( $attr['for'] ) . "'>$text"; 
  172. if ( $attr['close'] ) { 
  173. echo '</label>'; 
  174.  
  175. /** 
  176. * Output a legend element. 
  177. * @since 3.4 
  178. * @param string $text Legend text string. 
  179. * @param array $attr HTML attributes set. 
  180. */ 
  181. public function legend( $text, $attr ) { 
  182. $attr = wp_parse_args( $attr, array( 
  183. 'id' => '',  
  184. 'class' => '',  
  185. ); 
  186. $id = ( '' === $attr['id'] ) ? '' : ' id="' . esc_attr( $attr['id'] ) . '"'; 
  187. echo '<legend class="yoast-form-legend ' . $attr['class'] . '"' . $id . '>' . $text . '</legend>'; 
  188.  
  189. /** 
  190. * Create a Checkbox input field. 
  191. * @since 2.0 
  192. * @param string $var The variable within the option to create the checkbox for. 
  193. * @param string $label The label to show for the variable. 
  194. * @param bool $label_left Whether the label should be left (true) or right (false). 
  195. */ 
  196. public function checkbox( $var, $label, $label_left = false ) { 
  197. if ( ! isset( $this->options[ $var ] ) ) { 
  198. $this->options[ $var ] = false; 
  199.  
  200. if ( $this->options[ $var ] === true ) { 
  201. $this->options[ $var ] = 'on'; 
  202.  
  203. $class = ''; 
  204. if ( $label_left !== false ) { 
  205. if ( ! empty( $label_left ) ) { 
  206. $label_left .= ':'; 
  207. $this->label( $label_left, array( 'for' => $var ) ); 
  208. else { 
  209. $class = 'double'; 
  210.  
  211. 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 ), '/>'; 
  212.  
  213. if ( ! empty( $label ) ) { 
  214. $this->label( $label, array( 'for' => $var ) ); 
  215.  
  216. echo '<br class="clear" />'; 
  217.  
  218. /** 
  219. * Create a light switch input field. 
  220. * @since 3.1 
  221. * @param string $var The variable within the option to create the checkbox for. 
  222. * @param string $label The label to show for the variable. 
  223. * @param array $buttons Array of two labels for the buttons (defaults Off/On). 
  224. * @param boolean $reverse Reverse order of buttons (default true). 
  225. */ 
  226. public function light_switch( $var, $label, $buttons = array(), $reverse = true ) { 
  227.  
  228. if ( ! isset( $this->options[ $var ] ) ) { 
  229. $this->options[ $var ] = false; 
  230.  
  231. if ( $this->options[ $var ] === true ) { 
  232. $this->options[ $var ] = 'on'; 
  233.  
  234. $class = 'switch-light switch-candy switch-yoast-seo'; 
  235. $aria_labelledby = esc_attr( $var ) . '-label'; 
  236.  
  237. if ( $reverse ) { 
  238. $class .= ' switch-yoast-seo-reverse'; 
  239.  
  240. if ( empty( $buttons ) ) { 
  241. $buttons = array( __( 'Disabled', 'wordpress-seo' ), __( 'Enabled', 'wordpress-seo' ) ); 
  242.  
  243. list( $off_button, $on_button ) = $buttons; 
  244.  
  245. echo '<div class="switch-container">',  
  246. '<label class="', esc_attr( $class ), '"><b class="switch-yoast-seo-jaws-a11y"> </b>',  
  247. '<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 ), '/>',  
  248. "<b class='label-text' id='{$aria_labelledby}'>{$label}</b>",  
  249. '<span aria-hidden="true"> 
  250. <span>', esc_html( $off_button ) , '</span> 
  251. <span>', esc_html( $on_button ) , '</span> 
  252. <a></a> 
  253. </span> 
  254. </label><div class="clear"></div></div>'; 
  255.  
  256. /** 
  257. * Create a Text input field. 
  258. * @since 2.0 
  259. * @since 2.1 Introduced the `$attr` parameter. 
  260. * @param string $var The variable within the option to create the text input field for. 
  261. * @param string $label The label to show for the variable. 
  262. * @param array|string $attr Extra class to add to the input field. 
  263. */ 
  264. public function textinput( $var, $label, $attr = array() ) { 
  265. if ( ! is_array( $attr ) ) { 
  266. $attr = array( 
  267. 'class' => $attr,  
  268. ); 
  269. $attr = wp_parse_args( $attr, array( 
  270. 'placeholder' => '',  
  271. 'class' => '',  
  272. ) ); 
  273. $val = ( isset( $this->options[ $var ] ) ) ? $this->options[ $var ] : ''; 
  274.  
  275. $this->label( $label . ':', array( 'for' => $var, 'class' => 'textinput' ) ); 
  276. 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" />'; 
  277.  
  278. /** 
  279. * Create a textarea. 
  280. * @since 2.0 
  281. * @param string $var The variable within the option to create the textarea for. 
  282. * @param string $label The label to show for the variable. 
  283. * @param array $attr The CSS class to assign to the textarea. 
  284. */ 
  285. public function textarea( $var, $label, $attr = array() ) { 
  286. if ( ! is_array( $attr ) ) { 
  287. $attr = array( 
  288. 'class' => $attr,  
  289. ); 
  290. $attr = wp_parse_args( $attr, array( 
  291. 'cols' => '',  
  292. 'rows' => '',  
  293. 'class' => '',  
  294. ) ); 
  295. $val = ( isset( $this->options[ $var ] ) ) ? $this->options[ $var ] : ''; 
  296.  
  297. $this->label( $label . ':', array( 'for' => $var, 'class' => 'textinput' ) ); 
  298. 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" />'; 
  299.  
  300. /** 
  301. * Create a hidden input field. 
  302. * @since 2.0 
  303. * @param string $var The variable within the option to create the hidden input for. 
  304. * @param string $id The ID of the element. 
  305. */ 
  306. public function hidden( $var, $id = '' ) { 
  307. $val = ( isset( $this->options[ $var ] ) ) ? $this->options[ $var ] : ''; 
  308. if ( is_bool( $val ) ) { 
  309. $val = ( $val === true ) ? 'true' : 'false'; 
  310.  
  311. if ( '' === $id ) { 
  312. $id = 'hidden_' . $var; 
  313.  
  314. echo '<input type="hidden" id="' . esc_attr( $id ) . '" name="' . esc_attr( $this->option_name ) . '[' . esc_attr( $var ) . ']" value="' . esc_attr( $val ) . '"/>'; 
  315.  
  316. /** 
  317. * Create a Select Box. 
  318. * @since 2.0 
  319. * @param string $field_name The variable within the option to create the select for. 
  320. * @param string $label The label to show for the variable. 
  321. * @param array $select_options The select options to choose from. 
  322. */ 
  323. public function select( $field_name, $label, array $select_options ) { 
  324.  
  325. if ( empty( $select_options ) ) { 
  326. return; 
  327.  
  328. $this->label( $label . ':', array( 'for' => $field_name, 'class' => 'select' ) ); 
  329.  
  330. $select_name = esc_attr( $this->option_name ) . '[' . esc_attr( $field_name ) . ']'; 
  331. $active_option = ( isset( $this->options[ $field_name ] ) ) ? $this->options[ $field_name ] : ''; 
  332.  
  333. $select = new Yoast_Input_Select( $field_name, $select_name, $select_options, $active_option ); 
  334. $select->add_attribute( 'class', 'select' ); 
  335. $select->output_html(); 
  336.  
  337. echo '<br class="clear"/>'; 
  338.  
  339. /** 
  340. * Create a File upload field. 
  341. * @since 2.0 
  342. * @param string $var The variable within the option to create the file upload field for. 
  343. * @param string $label The label to show for the variable. 
  344. */ 
  345. public function file_upload( $var, $label ) { 
  346. $val = ''; 
  347. if ( isset( $this->options[ $var ] ) && is_array( $this->options[ $var ] ) ) { 
  348. $val = $this->options[ $var ]['url']; 
  349.  
  350. $var_esc = esc_attr( $var ); 
  351. $this->label( $label . ':', array( 'for' => $var, 'class' => 'select' ) ); 
  352. echo '<input type="file" value="' . esc_attr( $val ) . '" class="textinput" name="' . esc_attr( $this->option_name ) . '[' . $var_esc . ']" id="' . $var_esc . '"/>'; 
  353.  
  354. // Need to save separate array items in hidden inputs, because empty file inputs type will be deleted by settings API. 
  355. if ( ! empty( $this->options[ $var ] ) ) { 
  356. $this->hidden( 'file', $this->option_name . '_file' ); 
  357. $this->hidden( 'url', $this->option_name . '_url' ); 
  358. $this->hidden( 'type', $this->option_name . '_type' ); 
  359. echo '<br class="clear"/>'; 
  360.  
  361. /** 
  362. * Media input 
  363. * @since 2.0 
  364. * @param string $var Option name. 
  365. * @param string $label Label message. 
  366. */ 
  367. public function media_input( $var, $label ) { 
  368. $val = ''; 
  369. if ( isset( $this->options[ $var ] ) ) { 
  370. $val = $this->options[ $var ]; 
  371.  
  372. $var_esc = esc_attr( $var ); 
  373.  
  374. $this->label( $label . ':', array( 'for' => 'wpseo_' . $var, 'class' => 'select' ) ); 
  375. echo '<input class="textinput" id="wpseo_', $var_esc, '" type="text" size="36" name="', esc_attr( $this->option_name ), '[', $var_esc, ']" value="', esc_attr( $val ), '" />'; 
  376. echo '<input id="wpseo_', $var_esc, '_button" class="wpseo_image_upload_button button" type="button" value="', esc_attr__( 'Upload Image', 'wordpress-seo' ), '" />'; 
  377. echo '<br class="clear"/>'; 
  378.  
  379. /** 
  380. * Create a Radio input field. 
  381. * @since 2.0 
  382. * @param string $var The variable within the option to create the radio button for. 
  383. * @param array $values The radio options to choose from. 
  384. * @param string $legend Optional. The legend to show for the field set, if any. 
  385. * @param array $legend_attr Optional. The attributes for the legend, if any. 
  386. */ 
  387. public function radio( $var, $values, $legend = '', $legend_attr = array() ) { 
  388. if ( ! is_array( $values ) || $values === array() ) { 
  389. return; 
  390. if ( ! isset( $this->options[ $var ] ) ) { 
  391. $this->options[ $var ] = false; 
  392.  
  393. $var_esc = esc_attr( $var ); 
  394.  
  395. echo '<fieldset class="yoast-form-fieldset wpseo_radio_block" id="' . $var_esc . '">'; 
  396.  
  397. if ( is_string( $legend ) && '' !== $legend ) { 
  398.  
  399. $legend_attr = wp_parse_args( $legend_attr, array( 
  400. 'id' => '',  
  401. 'class' => 'radiogroup',  
  402. ) ); 
  403.  
  404. $this->legend( $legend, $legend_attr ); 
  405.  
  406. foreach ( $values as $key => $value ) { 
  407. $key_esc = esc_attr( $key ); 
  408. 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 ) . ' />'; 
  409. $this->label( $value, array( 'for' => $var_esc . '-' . $key_esc, 'class' => 'radio' ) ); 
  410. echo '</fieldset>'; 
  411.  
  412.  
  413. /** 
  414. * Create a toggle switch input field. 
  415. * @since 3.1 
  416. * @param string $var The variable within the option to create the file upload field for. 
  417. * @param array $values The radio options to choose from. 
  418. * @param string $label The label to show for the variable. 
  419. */ 
  420. public function toggle_switch( $var, $values, $label ) { 
  421. if ( ! is_array( $values ) || $values === array() ) { 
  422. return; 
  423. if ( ! isset( $this->options[ $var ] ) ) { 
  424. $this->options[ $var ] = false; 
  425. if ( $this->options[ $var ] === true ) { 
  426. $this->options[ $var ] = 'on'; 
  427. if ( $this->options[ $var ] === false ) { 
  428. $this->options[ $var ] = 'off'; 
  429.  
  430. $var_esc = esc_attr( $var ); 
  431.  
  432. echo '<div class="switch-container">'; 
  433. echo '<fieldset id="', $var_esc, '" class="fieldset-switch-toggle"><legend>', $label, '</legend> 
  434. <div class="switch-toggle switch-candy switch-yoast-seo">'; 
  435.  
  436. foreach ( $values as $key => $value ) { 
  437. $key_esc = esc_attr( $key ); 
  438. $for = $var_esc . '-' . $key_esc; 
  439. echo '<input type="radio" id="' . $for . '" name="' . esc_attr( $this->option_name ) . '[' . $var_esc . ']" value="' . $key_esc . '" ' . checked( $this->options[ $var ], $key_esc, false ) . ' />',  
  440. '<label for="', $for, '">', $value, '</label>'; 
  441.  
  442. echo '<a></a></div></fieldset><div class="clear"></div></div>' . "\n\n";