| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Menu |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders a replacement variable editor. |
| 10 |
*/ |
| 11 |
class WPSEO_Replacevar_Editor { |
| 12 |
|
| 13 |
/** |
| 14 |
* Yoast Forms instance. |
| 15 |
* |
| 16 |
* @var Yoast_Form |
| 17 |
*/ |
| 18 |
private $yform; |
| 19 |
|
| 20 |
/** |
| 21 |
* The arguments required for the div to render. |
| 22 |
* |
| 23 |
* @var array { |
| 24 |
* @type string $title The title field id. |
| 25 |
* @type string $description The description field id. |
| 26 |
* @type string $page_type_recommended The page type for the context of the recommended replace vars. |
| 27 |
* @type string $page_type_specific The page type for the context of the editor specific replace vars. |
| 28 |
* @type bool $paper_style Optional. Whether the editor has paper style. |
| 29 |
* @type string $label_title Optional. The label to use for the title field. |
| 30 |
* @type string $label_description Optional. The label to use for the description field. |
| 31 |
* @type string $description_placeholder Optional. The placeholder text to use for the description field. |
| 32 |
* @type bool $has_new_badge Optional. Whether to show the "New" badge. |
| 33 |
* @type bool $has_premium_badge Optional. Whether to show the "Premium" badge. |
| 34 |
* } |
| 35 |
*/ |
| 36 |
private $arguments; |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructs the object. |
| 40 |
* |
| 41 |
* @param Yoast_Form $yform Yoast forms. |
| 42 |
* @param array $arguments { |
| 43 |
* The arguments that can be given. |
| 44 |
* |
| 45 |
* @type string $title The title field id. |
| 46 |
* @type string $description The description field id. |
| 47 |
* @type string $page_type_recommended The page type for the context of the recommended replace vars. |
| 48 |
* @type string $page_type_specific The page type for the context of the editor specific replace vars. |
| 49 |
* @type bool $paper_style Optional. Whether the editor has paper style. |
| 50 |
* @type string $label_title Optional. The label to use for the title field. |
| 51 |
* @type string $label_description Optional. The label to use for the description field. |
| 52 |
* @type string $description_placeholder Optional. The placeholder text to use for the description field. |
| 53 |
* @type bool $has_new_badge Optional. Whether to show the "New" badge. |
| 54 |
* @type bool $has_premium_badge Optional. Whether to show the "Premium" badge. |
| 55 |
* } |
| 56 |
*/ |
| 57 |
public function __construct( Yoast_Form $yform, $arguments ) { |
| 58 |
$arguments = wp_parse_args( |
| 59 |
$arguments, |
| 60 |
[ |
| 61 |
'paper_style' => true, |
| 62 |
'label_title' => '', |
| 63 |
'label_description' => '', |
| 64 |
'description_placeholder' => '', |
| 65 |
'has_new_badge' => false, |
| 66 |
'is_disabled' => false, |
| 67 |
'has_premium_badge' => false, |
| 68 |
], |
| 69 |
); |
| 70 |
|
| 71 |
$this->validate_arguments( $arguments ); |
| 72 |
|
| 73 |
$this->yform = $yform; |
| 74 |
$this->arguments = [ |
| 75 |
'title' => (string) $arguments['title'], |
| 76 |
'description' => (string) $arguments['description'], |
| 77 |
'page_type_recommended' => (string) $arguments['page_type_recommended'], |
| 78 |
'page_type_specific' => (string) $arguments['page_type_specific'], |
| 79 |
'paper_style' => (bool) $arguments['paper_style'], |
| 80 |
'label_title' => (string) $arguments['label_title'], |
| 81 |
'label_description' => (string) $arguments['label_description'], |
| 82 |
'description_placeholder' => (string) $arguments['description_placeholder'], |
| 83 |
'has_new_badge' => (bool) $arguments['has_new_badge'], |
| 84 |
'is_disabled' => (bool) $arguments['is_disabled'], |
| 85 |
'has_premium_badge' => (bool) $arguments['has_premium_badge'], |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Renders a div for the react application to mount to, and hidden inputs where |
| 91 |
* the app should store it's value so they will be properly saved when the form |
| 92 |
* is submitted. |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function render() { |
| 97 |
$this->yform->hidden( $this->arguments['title'], $this->arguments['title'] ); |
| 98 |
$this->yform->hidden( $this->arguments['description'], $this->arguments['description'] ); |
| 99 |
|
| 100 |
printf( |
| 101 |
'<div |
| 102 |
data-react-replacevar-editor |
| 103 |
data-react-replacevar-title-field-id="%1$s" |
| 104 |
data-react-replacevar-metadesc-field-id="%2$s" |
| 105 |
data-react-replacevar-page-type-recommended="%3$s" |
| 106 |
data-react-replacevar-page-type-specific="%4$s" |
| 107 |
data-react-replacevar-paper-style="%5$s" |
| 108 |
data-react-replacevar-label-title="%6$s" |
| 109 |
data-react-replacevar-label-description="%7$s" |
| 110 |
data-react-replacevar-description-placeholder="%8$s" |
| 111 |
data-react-replacevar-has-new-badge="%9$s" |
| 112 |
data-react-replacevar-is-disabled="%10$s" |
| 113 |
data-react-replacevar-has-premium-badge="%11$s" |
| 114 |
></div>', |
| 115 |
esc_attr( $this->arguments['title'] ), |
| 116 |
esc_attr( $this->arguments['description'] ), |
| 117 |
esc_attr( $this->arguments['page_type_recommended'] ), |
| 118 |
esc_attr( $this->arguments['page_type_specific'] ), |
| 119 |
esc_attr( $this->arguments['paper_style'] ), |
| 120 |
esc_attr( $this->arguments['label_title'] ), |
| 121 |
esc_attr( $this->arguments['label_description'] ), |
| 122 |
esc_attr( $this->arguments['description_placeholder'] ), |
| 123 |
esc_attr( $this->arguments['has_new_badge'] ), |
| 124 |
esc_attr( $this->arguments['is_disabled'] ), |
| 125 |
esc_attr( $this->arguments['has_premium_badge'] ), |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Validates the replacement variable editor arguments. |
| 131 |
* |
| 132 |
* @param array $arguments The arguments to validate. |
| 133 |
* |
| 134 |
* @return void |
| 135 |
* |
| 136 |
* @throws InvalidArgumentException Thrown when not all required arguments are present. |
| 137 |
*/ |
| 138 |
protected function validate_arguments( array $arguments ) { |
| 139 |
$required_arguments = [ |
| 140 |
'title', |
| 141 |
'description', |
| 142 |
'page_type_recommended', |
| 143 |
'page_type_specific', |
| 144 |
'paper_style', |
| 145 |
]; |
| 146 |
|
| 147 |
foreach ( $required_arguments as $field_name ) { |
| 148 |
if ( ! array_key_exists( $field_name, $arguments ) ) { |
| 149 |
throw new InvalidArgumentException( |
| 150 |
sprintf( |
| 151 |
/* translators: %1$s expands to the missing field name. */ |
| 152 |
__( 'Not all required fields are given. Missing field %1$s', 'wordpress-seo' ), |
| 153 |
$field_name, |
| 154 |
), |
| 155 |
); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|