| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Presenters\Admin; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Presenters\Abstract_Presenter; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Light_Switch_Presenter. |
| 9 |
* |
| 10 |
* @package Yoast\WP\SEO\Presenters\Admin |
| 11 |
*/ |
| 12 |
class Light_Switch_Presenter extends Abstract_Presenter { |
| 13 |
|
| 14 |
/** |
| 15 |
* The variable to create the checkbox for. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
protected $var; |
| 20 |
|
| 21 |
/** |
| 22 |
* The visual label text for the toggle. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $label; |
| 27 |
|
| 28 |
/** |
| 29 |
* Array of two visual labels for the buttons. |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
protected $buttons; |
| 34 |
|
| 35 |
/** |
| 36 |
* The name of the underlying checkbox. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
protected $name; |
| 41 |
|
| 42 |
/** |
| 43 |
* The variable current value. |
| 44 |
* |
| 45 |
* @var string|bool |
| 46 |
*/ |
| 47 |
protected $value; |
| 48 |
|
| 49 |
/** |
| 50 |
* Reverse order of buttons. |
| 51 |
* |
| 52 |
* @var bool |
| 53 |
*/ |
| 54 |
protected $reverse; |
| 55 |
|
| 56 |
/** |
| 57 |
* The inline Help HTML. |
| 58 |
* |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
protected $help; |
| 62 |
|
| 63 |
/** |
| 64 |
* Whether the visual label is displayed in strong text. |
| 65 |
* |
| 66 |
* @var bool |
| 67 |
*/ |
| 68 |
protected $strong; |
| 69 |
|
| 70 |
/** |
| 71 |
* The disabled attribute HTML. |
| 72 |
* |
| 73 |
* @var string |
| 74 |
*/ |
| 75 |
protected $disabled_attribute; |
| 76 |
|
| 77 |
/** |
| 78 |
* Light_Switch_Presenter constructor. |
| 79 |
* |
| 80 |
* @param string $variable The variable to create the checkbox for. |
| 81 |
* @param string $label The visual label text for the toggle. |
| 82 |
* @param array $buttons Array of two visual labels for the buttons (defaults Disabled/Enabled). |
| 83 |
* @param string $name The name of the underlying checkbox. |
| 84 |
* @param string|bool $value The variable current value, to determine the checked attribute. |
| 85 |
* @param bool $reverse Optional. Reverse order of buttons (default true). |
| 86 |
* @param string $help Optional. Inline Help HTML that will be printed out before the toggle. Default is empty. |
| 87 |
* @param bool $strong Optional. Whether the visual label is displayed in strong text. Default is false. |
| 88 |
* Starting from Yoast SEO 16.5, the visual label is forced to bold via CSS. |
| 89 |
* @param string $disabled_attribute Optional. The disabled HTML attribute. Default is empty. |
| 90 |
*/ |
| 91 |
public function __construct( |
| 92 |
$variable, |
| 93 |
$label, |
| 94 |
$buttons, |
| 95 |
$name, |
| 96 |
$value, |
| 97 |
$reverse = true, |
| 98 |
$help = '', |
| 99 |
$strong = false, |
| 100 |
$disabled_attribute = '' |
| 101 |
) { |
| 102 |
$this->var = $variable; |
| 103 |
$this->label = $label; |
| 104 |
$this->buttons = $buttons; |
| 105 |
$this->name = $name; |
| 106 |
$this->value = $value; |
| 107 |
$this->reverse = $reverse; |
| 108 |
$this->help = $help; |
| 109 |
$this->strong = $strong; |
| 110 |
$this->disabled_attribute = $disabled_attribute; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Presents the light switch toggle. |
| 115 |
* |
| 116 |
* @return string The light switch's HTML. |
| 117 |
*/ |
| 118 |
public function present() { |
| 119 |
if ( empty( $this->buttons ) ) { |
| 120 |
$this->buttons = [ \__( 'Disabled', 'wordpress-seo' ), \__( 'Enabled', 'wordpress-seo' ) ]; |
| 121 |
} |
| 122 |
|
| 123 |
list( $off_button, $on_button ) = $this->buttons; |
| 124 |
|
| 125 |
$class = 'switch-light switch-candy switch-yoast-seo'; |
| 126 |
|
| 127 |
if ( $this->reverse ) { |
| 128 |
$class .= ' switch-yoast-seo-reverse'; |
| 129 |
} |
| 130 |
|
| 131 |
$help_class = ! empty( $this->help ) ? ' switch-container__has-help' : ''; |
| 132 |
$strong_class = ( $this->strong ) ? ' switch-light-visual-label__strong' : ''; |
| 133 |
|
| 134 |
$output = '<div class="switch-container' . $help_class . '">'; |
| 135 |
$output .= \sprintf( |
| 136 |
'<span class="switch-light-visual-label%1$s" id="%2$s">%3$s</span>%4$s', |
| 137 |
$strong_class, // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $strong_class output is hardcoded. |
| 138 |
\esc_attr( $this->var . '-label' ), |
| 139 |
\esc_html( $this->label ), |
| 140 |
$this->help, // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: The help contains HTML. |
| 141 |
); |
| 142 |
$output .= '<label class="' . $class . '"><b class="switch-yoast-seo-jaws-a11y"> </b>'; |
| 143 |
$output .= \sprintf( |
| 144 |
'<input type="checkbox" aria-labelledby="%1$s" id="%2$s" name="%3$s" value="on"%4$s%5$s/>', |
| 145 |
\esc_attr( $this->var . '-label' ), |
| 146 |
\esc_attr( $this->var ), |
| 147 |
\esc_attr( $this->name ), |
| 148 |
\checked( $this->value, 'on', false ), // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: The output is hardcoded by WordPress. |
| 149 |
$this->disabled_attribute, // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: $disabled_attribute output is hardcoded. |
| 150 |
); |
| 151 |
$output .= '<span aria-hidden="true">'; |
| 152 |
$output .= '<span>' . \esc_html( $off_button ) . '</span>'; |
| 153 |
$output .= '<span>' . \esc_html( $on_button ) . '</span>'; |
| 154 |
$output .= '<a></a>'; |
| 155 |
$output .= '</span></label><div class="clear"></div></div>'; |
| 156 |
|
| 157 |
return $output; |
| 158 |
} |
| 159 |
} |
| 160 |
|