| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class WPSEO_Taxonomy_Presenter. |
| 10 |
*/ |
| 11 |
class WPSEO_Taxonomy_Fields_Presenter { |
| 12 |
|
| 13 |
/** |
| 14 |
* The taxonomy meta data for the current term. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
private $tax_meta; |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructs the WPSEO_Taxonomy_Fields_Presenter class. |
| 22 |
* |
| 23 |
* @param stdClass $term The current term. |
| 24 |
*/ |
| 25 |
public function __construct( $term ) { |
| 26 |
$this->tax_meta = WPSEO_Taxonomy_Meta::get_term_meta( (int) $term->term_id, $term->taxonomy ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Displaying the form fields. |
| 31 |
* |
| 32 |
* @param array $fields Array with the fields that will be displayed. |
| 33 |
* |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public function html( array $fields ) { |
| 37 |
$content = ''; |
| 38 |
foreach ( $fields as $field_name => $field_configuration ) { |
| 39 |
$content .= $this->form_row( 'wpseo_' . $field_name, $field_configuration ); |
| 40 |
} |
| 41 |
return $content; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Create a row in the form table. |
| 46 |
* |
| 47 |
* @param string $field_name Variable the row controls. |
| 48 |
* @param array $field_configuration Array with the field configuration. |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
private function form_row( $field_name, array $field_configuration ) { |
| 53 |
$esc_field_name = esc_attr( $field_name ); |
| 54 |
|
| 55 |
$options = (array) $field_configuration['options']; |
| 56 |
|
| 57 |
if ( ! empty( $field_configuration['description'] ) ) { |
| 58 |
$options['description'] = $field_configuration['description']; |
| 59 |
} |
| 60 |
|
| 61 |
$label = $this->get_label( $field_configuration['label'], $esc_field_name ); |
| 62 |
$field = $this->get_field( $field_configuration['type'], $esc_field_name, $this->get_field_value( $field_name ), $options ); |
| 63 |
$help_content = ( $field_configuration['options']['help'] ?? '' ); |
| 64 |
$help_button_text = ( $field_configuration['options']['help-button'] ?? '' ); |
| 65 |
$help = new WPSEO_Admin_Help_Panel( $field_name, $help_button_text, $help_content ); |
| 66 |
|
| 67 |
return $this->parse_row( $label, $help, $field ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Generates the html for the given field config. |
| 72 |
* |
| 73 |
* @param string $field_type The fieldtype, e.g: text, checkbox, etc. |
| 74 |
* @param string $field_name The name of the field. |
| 75 |
* @param string $field_value The value of the field. |
| 76 |
* @param array $options Array with additional options. |
| 77 |
* |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
private function get_field( $field_type, $field_name, $field_value, array $options ) { |
| 81 |
|
| 82 |
$class = $this->get_class( $options ); |
| 83 |
$field = ''; |
| 84 |
$description = ''; |
| 85 |
$aria_describedby = ''; |
| 86 |
|
| 87 |
if ( ! empty( $options['description'] ) ) { |
| 88 |
$aria_describedby = ' aria-describedby="' . $field_name . '-desc"'; |
| 89 |
$description = '<p id="' . $field_name . '-desc" class="yoast-metabox__description">' . $options['description'] . '</p>'; |
| 90 |
} |
| 91 |
|
| 92 |
switch ( $field_type ) { |
| 93 |
case 'div': |
| 94 |
$field .= '<div id="' . $field_name . '"></div>'; |
| 95 |
break; |
| 96 |
case 'url': |
| 97 |
$field .= '<input name="' . $field_name . '" id="' . $field_name . '" ' . $class . ' type="url" value="' . esc_attr( urldecode( $field_value ) ) . '" size="40"' . $aria_describedby . '/>'; |
| 98 |
break; |
| 99 |
case 'text': |
| 100 |
$field .= '<input name="' . $field_name . '" id="' . $field_name . '" ' . $class . ' type="text" value="' . esc_attr( $field_value ) . '" size="40"' . $aria_describedby . '/>'; |
| 101 |
break; |
| 102 |
case 'checkbox': |
| 103 |
$field .= '<input name="' . $field_name . '" id="' . $field_name . '" type="checkbox" ' . checked( $field_value ) . $aria_describedby . '/>'; |
| 104 |
break; |
| 105 |
case 'textarea': |
| 106 |
$rows = 3; |
| 107 |
if ( ! empty( $options['rows'] ) ) { |
| 108 |
$rows = $options['rows']; |
| 109 |
} |
| 110 |
$field .= '<textarea class="large-text" rows="' . esc_attr( $rows ) . '" id="' . $field_name . '" name="' . $field_name . '"' . $aria_describedby . '>' . esc_textarea( $field_value ) . '</textarea>'; |
| 111 |
break; |
| 112 |
case 'select': |
| 113 |
if ( is_array( $options ) && $options !== [] ) { |
| 114 |
$field .= '<select name="' . $field_name . '" id="' . $field_name . '"' . $aria_describedby . '>'; |
| 115 |
|
| 116 |
$select_options = ( array_key_exists( 'options', $options ) ) ? $options['options'] : $options; |
| 117 |
|
| 118 |
foreach ( $select_options as $option => $option_label ) { |
| 119 |
$selected = selected( $option, $field_value, false ); |
| 120 |
$field .= '<option ' . $selected . ' value="' . esc_attr( $option ) . '">' . esc_html( $option_label ) . '</option>'; |
| 121 |
} |
| 122 |
unset( $option, $option_label, $selected ); |
| 123 |
|
| 124 |
$field .= '</select>'; |
| 125 |
} |
| 126 |
break; |
| 127 |
case 'hidden': |
| 128 |
$field .= '<input name="' . $field_name . '" id="hidden_' . $field_name . '" type="hidden" value="' . esc_attr( $field_value ) . '" />'; |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
return $field . $description; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Getting the value for given field_name. |
| 137 |
* |
| 138 |
* @param string $field_name The fieldname to get the value for. |
| 139 |
* |
| 140 |
* @return string |
| 141 |
*/ |
| 142 |
private function get_field_value( $field_name ) { |
| 143 |
if ( isset( $this->tax_meta[ $field_name ] ) && $this->tax_meta[ $field_name ] !== '' ) { |
| 144 |
return $this->tax_meta[ $field_name ]; |
| 145 |
} |
| 146 |
|
| 147 |
return ''; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Getting the class attributes if $options contains a class key. |
| 152 |
* |
| 153 |
* @param array $options The array with field options. |
| 154 |
* |
| 155 |
* @return string |
| 156 |
*/ |
| 157 |
private function get_class( array $options ) { |
| 158 |
if ( ! empty( $options['class'] ) ) { |
| 159 |
return ' class="' . esc_attr( $options['class'] ) . '"'; |
| 160 |
} |
| 161 |
|
| 162 |
return ''; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Getting the label HTML. |
| 167 |
* |
| 168 |
* @param string $label The label value. |
| 169 |
* @param string $field_name The target field. |
| 170 |
* |
| 171 |
* @return string |
| 172 |
*/ |
| 173 |
private function get_label( $label, $field_name ) { |
| 174 |
if ( $label !== '' ) { |
| 175 |
return '<label for="' . $field_name . '">' . esc_html( $label ) . '</label>'; |
| 176 |
} |
| 177 |
|
| 178 |
return ''; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns the HTML for the row which contains label, help and the field. |
| 183 |
* |
| 184 |
* @param string $label The html for the label if there was a label set. |
| 185 |
* @param WPSEO_Admin_Help_Panel $help The help panel to render in this row. |
| 186 |
* @param string $field The html for the field. |
| 187 |
* |
| 188 |
* @return string |
| 189 |
*/ |
| 190 |
private function parse_row( $label, WPSEO_Admin_Help_Panel $help, $field ) { |
| 191 |
if ( $label !== '' || $help !== '' ) { |
| 192 |
return $label . $help->get_button_html() . $help->get_panel_html() . $field; |
| 193 |
} |
| 194 |
|
| 195 |
return $field; |
| 196 |
} |
| 197 |
} |
| 198 |
|