PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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