@@ -19,339 +19,339 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class WL_Metabox_Field { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * A {@link Wordlift_Log_Service} instance. |
|
| 24 | - * |
|
| 25 | - * @since 3.15.0 |
|
| 26 | - * @access protected |
|
| 27 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 28 | - */ |
|
| 29 | - protected $log; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The meta name for this field's value. |
|
| 33 | - * |
|
| 34 | - * @var string $meta_name The meta name for this field's value. |
|
| 35 | - */ |
|
| 36 | - public $meta_name; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The custom field settings. |
|
| 40 | - * |
|
| 41 | - * @var null|array $raw_custom_field The custom field settings. |
|
| 42 | - */ |
|
| 43 | - public $raw_custom_field; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The schema.org predicate. |
|
| 47 | - * |
|
| 48 | - * @var string $predicate The schema.org predicate. |
|
| 49 | - */ |
|
| 50 | - public $predicate; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The field's label. |
|
| 54 | - * |
|
| 55 | - * @var string $label The field's label. |
|
| 56 | - */ |
|
| 57 | - public $label; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The WordLift data type. |
|
| 61 | - * |
|
| 62 | - * @var string $expected_wl_type The WordLift data type. |
|
| 63 | - */ |
|
| 64 | - public $expected_wl_type; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The RDF data type. |
|
| 68 | - * |
|
| 69 | - * @var string $expected_uri_type The RDF data type. |
|
| 70 | - */ |
|
| 71 | - public $expected_uri_type; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * The cardinality. |
|
| 75 | - * |
|
| 76 | - * @var int $cardinality The cardinality. |
|
| 77 | - */ |
|
| 78 | - public $cardinality; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * The current value. |
|
| 82 | - * |
|
| 83 | - * @var array $data The current value. |
|
| 84 | - */ |
|
| 85 | - public $data; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Create a {@link WL_Metabox_Field} instance. |
|
| 89 | - * |
|
| 90 | - * @param array $args An array of parameters. |
|
| 91 | - */ |
|
| 92 | - public function __construct( $args ) { |
|
| 93 | - |
|
| 94 | - $this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' ); |
|
| 95 | - |
|
| 96 | - if ( empty( $args ) ) { |
|
| 97 | - return; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // Save a copy of the custom field's params. |
|
| 101 | - $this->raw_custom_field = reset( $args ); |
|
| 102 | - |
|
| 103 | - // Extract meta name (post_meta key for the DB). |
|
| 104 | - $this->meta_name = key( $args ); |
|
| 105 | - |
|
| 106 | - // Extract linked data predicate. |
|
| 107 | - if ( isset( $this->raw_custom_field['predicate'] ) ) { |
|
| 108 | - $this->predicate = $this->raw_custom_field['predicate']; |
|
| 109 | - } else { |
|
| 110 | - return; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - // Extract human readable label. |
|
| 114 | - $exploded_predicate = explode( '/', $this->predicate ); |
|
| 115 | - |
|
| 116 | - // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate. |
|
| 117 | - $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate ); |
|
| 118 | - |
|
| 119 | - // Extract field constraints (numerosity, expected type). |
|
| 120 | - // Default constaints: accept one string.. |
|
| 121 | - if ( isset( $this->raw_custom_field['type'] ) ) { |
|
| 122 | - $this->expected_wl_type = $this->raw_custom_field['type']; |
|
| 123 | - } else { |
|
| 124 | - $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $this->cardinality = 1; |
|
| 128 | - if ( isset( $this->raw_custom_field['constraints'] ) ) { |
|
| 129 | - |
|
| 130 | - $constraints = $this->raw_custom_field['constraints']; |
|
| 131 | - |
|
| 132 | - // Extract cardinality. |
|
| 133 | - if ( isset( $constraints['cardinality'] ) ) { |
|
| 134 | - $this->cardinality = $constraints['cardinality']; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - // Which type of entity can we accept (e.g. Place, Event, ecc.)? . |
|
| 138 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) { |
|
| 139 | - $this->expected_uri_type = is_array( $constraints['uri_type'] ) |
|
| 140 | - ? $constraints['uri_type'] |
|
| 141 | - : array( $constraints['uri_type'] ); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Return nonce HTML. |
|
| 149 | - * |
|
| 150 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 151 | - */ |
|
| 152 | - public function html_nonce() { |
|
| 153 | - |
|
| 154 | - return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Verify nonce. |
|
| 159 | - * |
|
| 160 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 161 | - * |
|
| 162 | - * @return boolean Nonce verification. |
|
| 163 | - */ |
|
| 164 | - public function verify_nonce() { |
|
| 165 | - |
|
| 166 | - $nonce_name = 'wordlift_' . $this->meta_name . '_entity_box_nonce'; |
|
| 167 | - $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box'; |
|
| 168 | - |
|
| 169 | - if ( ! isset( $_POST[ $nonce_name ] ) ) { |
|
| 170 | - return false; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // Verify that the nonce is valid. |
|
| 174 | - return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify ); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Load data from DB and store the resulting array in $this->data. |
|
| 179 | - * |
|
| 180 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 181 | - */ |
|
| 182 | - public function get_data() { |
|
| 183 | - |
|
| 184 | - $data = get_post_meta( get_the_ID(), $this->meta_name ); |
|
| 185 | - |
|
| 186 | - // Values are always contained in an array (makes it easier to manage cardinality). |
|
| 187 | - if ( ! is_array( $data ) ) { |
|
| 188 | - $data = array( $data ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - $this->data = $data; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Sanitizes data before saving to DB. Default sanitization trashes empty |
|
| 196 | - * values. |
|
| 197 | - * |
|
| 198 | - * Stores the sanitized values into $this->data so they can be later processed. |
|
| 199 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 200 | - * |
|
| 201 | - * @param array $values Array of values to be sanitized and then stored into |
|
| 202 | - * $this->data. |
|
| 203 | - */ |
|
| 204 | - public function sanitize_data( $values ) { |
|
| 205 | - |
|
| 206 | - $sanitized_data = array(); |
|
| 207 | - |
|
| 208 | - if ( ! is_array( $values ) ) { |
|
| 209 | - $values = array( $values ); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - foreach ( $values as $value ) { |
|
| 213 | - $sanitized_value = $this->sanitize_data_filter( $value ); |
|
| 214 | - if ( ! is_null( $sanitized_value ) ) { |
|
| 215 | - $sanitized_data[] = $sanitized_value; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $this->data = $sanitized_data; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Sanitize a single value. Called from $this->sanitize_data. Default |
|
| 224 | - * sanitization excludes empty values. |
|
| 225 | - * |
|
| 226 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 227 | - * |
|
| 228 | - * @param string $value The value to sanitize. |
|
| 229 | - * |
|
| 230 | - * @return mixed Returns sanitized value, or null. |
|
| 231 | - */ |
|
| 232 | - public function sanitize_data_filter( $value ) { |
|
| 233 | - |
|
| 234 | - // TODO: all fields should provide their own sanitize which shouldn't |
|
| 235 | - // be part of a UI class. |
|
| 236 | - |
|
| 237 | - // If the field provides its own validation, use it. |
|
| 238 | - if ( isset( $this->raw_custom_field['sanitize'] ) ) { |
|
| 239 | - return call_user_func( $this->raw_custom_field['sanitize'], $value ); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 243 | - return $value; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - return null; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Save data to DB. |
|
| 251 | - * |
|
| 252 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 253 | - * |
|
| 254 | - * @param array $values Array of values to be sanitized and then stored into $this->data. |
|
| 255 | - */ |
|
| 256 | - public function save_data( $values ) { |
|
| 257 | - |
|
| 258 | - // Will sanitize data and store them in $field->data. |
|
| 259 | - $this->sanitize_data( $values ); |
|
| 260 | - |
|
| 261 | - $entity_id = get_the_ID(); |
|
| 262 | - |
|
| 263 | - // Take away old values. |
|
| 264 | - delete_post_meta( $entity_id, $this->meta_name ); |
|
| 265 | - |
|
| 266 | - // insert new values, respecting cardinality. |
|
| 267 | - $single = ( 1 === $this->cardinality ); |
|
| 268 | - foreach ( $this->data as $value ) { |
|
| 269 | - add_post_meta( $entity_id, $this->meta_name, $value, $single ); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Returns the HTML tag that will contain the Field. By default the we |
|
| 275 | - * return a <div> with data- attributes on cardinality and expected types. |
|
| 276 | - * |
|
| 277 | - * It is useful to provide data- attributes for the JS scripts. |
|
| 278 | - * |
|
| 279 | - * Overwrite this method in a child class to obtain custom behaviour. |
|
| 280 | - */ |
|
| 281 | - public function html_wrapper_open() { |
|
| 282 | - |
|
| 283 | - return "<div class='wl-field' data-cardinality='$this->cardinality'>"; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Returns Field HTML (nonce included). |
|
| 288 | - * |
|
| 289 | - * Overwrite this method (or methods called from this method) in a child |
|
| 290 | - * class to obtain custom behaviour. |
|
| 291 | - */ |
|
| 292 | - public function html() { |
|
| 293 | - |
|
| 294 | - // Open main <div> for the Field. |
|
| 295 | - $html = $this->html_wrapper_open(); |
|
| 296 | - |
|
| 297 | - // Label. |
|
| 298 | - $html .= "<h3>$this->label</h3>"; |
|
| 299 | - |
|
| 300 | - // print nonce. |
|
| 301 | - $html .= $this->html_nonce(); |
|
| 302 | - |
|
| 303 | - // print data loaded from DB. |
|
| 304 | - $count = 0; |
|
| 305 | - if ( $this->data ) { |
|
| 306 | - foreach ( $this->data as $value ) { |
|
| 307 | - if ( $count < $this->cardinality ) { |
|
| 308 | - $html .= $this->html_input( $value ); |
|
| 309 | - } |
|
| 310 | - $count ++; |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - // Print the empty <input> to add new values. |
|
| 315 | - if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 316 | - $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 317 | - $count ++; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - // If cardiality allows it, print button to add new values. |
|
| 321 | - if ( $count < $this->cardinality ) { |
|
| 322 | - $html .= '<button class="button wl-add-input wl-button" type="button">Add</button>'; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - // Close the HTML wrapper. |
|
| 326 | - $html .= $this->html_wrapper_close(); |
|
| 327 | - |
|
| 328 | - return $html; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Return a single <input> tag for the Field. |
|
| 333 | - * |
|
| 334 | - * @param mixed $value Input value. |
|
| 335 | - * |
|
| 336 | - * @return string The html code fragment. |
|
| 337 | - */ |
|
| 338 | - public function html_input( $value ) { |
|
| 339 | - $html = <<<EOF |
|
| 22 | + /** |
|
| 23 | + * A {@link Wordlift_Log_Service} instance. |
|
| 24 | + * |
|
| 25 | + * @since 3.15.0 |
|
| 26 | + * @access protected |
|
| 27 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 28 | + */ |
|
| 29 | + protected $log; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The meta name for this field's value. |
|
| 33 | + * |
|
| 34 | + * @var string $meta_name The meta name for this field's value. |
|
| 35 | + */ |
|
| 36 | + public $meta_name; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The custom field settings. |
|
| 40 | + * |
|
| 41 | + * @var null|array $raw_custom_field The custom field settings. |
|
| 42 | + */ |
|
| 43 | + public $raw_custom_field; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The schema.org predicate. |
|
| 47 | + * |
|
| 48 | + * @var string $predicate The schema.org predicate. |
|
| 49 | + */ |
|
| 50 | + public $predicate; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The field's label. |
|
| 54 | + * |
|
| 55 | + * @var string $label The field's label. |
|
| 56 | + */ |
|
| 57 | + public $label; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The WordLift data type. |
|
| 61 | + * |
|
| 62 | + * @var string $expected_wl_type The WordLift data type. |
|
| 63 | + */ |
|
| 64 | + public $expected_wl_type; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The RDF data type. |
|
| 68 | + * |
|
| 69 | + * @var string $expected_uri_type The RDF data type. |
|
| 70 | + */ |
|
| 71 | + public $expected_uri_type; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * The cardinality. |
|
| 75 | + * |
|
| 76 | + * @var int $cardinality The cardinality. |
|
| 77 | + */ |
|
| 78 | + public $cardinality; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * The current value. |
|
| 82 | + * |
|
| 83 | + * @var array $data The current value. |
|
| 84 | + */ |
|
| 85 | + public $data; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Create a {@link WL_Metabox_Field} instance. |
|
| 89 | + * |
|
| 90 | + * @param array $args An array of parameters. |
|
| 91 | + */ |
|
| 92 | + public function __construct( $args ) { |
|
| 93 | + |
|
| 94 | + $this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' ); |
|
| 95 | + |
|
| 96 | + if ( empty( $args ) ) { |
|
| 97 | + return; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // Save a copy of the custom field's params. |
|
| 101 | + $this->raw_custom_field = reset( $args ); |
|
| 102 | + |
|
| 103 | + // Extract meta name (post_meta key for the DB). |
|
| 104 | + $this->meta_name = key( $args ); |
|
| 105 | + |
|
| 106 | + // Extract linked data predicate. |
|
| 107 | + if ( isset( $this->raw_custom_field['predicate'] ) ) { |
|
| 108 | + $this->predicate = $this->raw_custom_field['predicate']; |
|
| 109 | + } else { |
|
| 110 | + return; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + // Extract human readable label. |
|
| 114 | + $exploded_predicate = explode( '/', $this->predicate ); |
|
| 115 | + |
|
| 116 | + // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate. |
|
| 117 | + $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate ); |
|
| 118 | + |
|
| 119 | + // Extract field constraints (numerosity, expected type). |
|
| 120 | + // Default constaints: accept one string.. |
|
| 121 | + if ( isset( $this->raw_custom_field['type'] ) ) { |
|
| 122 | + $this->expected_wl_type = $this->raw_custom_field['type']; |
|
| 123 | + } else { |
|
| 124 | + $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $this->cardinality = 1; |
|
| 128 | + if ( isset( $this->raw_custom_field['constraints'] ) ) { |
|
| 129 | + |
|
| 130 | + $constraints = $this->raw_custom_field['constraints']; |
|
| 131 | + |
|
| 132 | + // Extract cardinality. |
|
| 133 | + if ( isset( $constraints['cardinality'] ) ) { |
|
| 134 | + $this->cardinality = $constraints['cardinality']; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + // Which type of entity can we accept (e.g. Place, Event, ecc.)? . |
|
| 138 | + if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) { |
|
| 139 | + $this->expected_uri_type = is_array( $constraints['uri_type'] ) |
|
| 140 | + ? $constraints['uri_type'] |
|
| 141 | + : array( $constraints['uri_type'] ); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Return nonce HTML. |
|
| 149 | + * |
|
| 150 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 151 | + */ |
|
| 152 | + public function html_nonce() { |
|
| 153 | + |
|
| 154 | + return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Verify nonce. |
|
| 159 | + * |
|
| 160 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 161 | + * |
|
| 162 | + * @return boolean Nonce verification. |
|
| 163 | + */ |
|
| 164 | + public function verify_nonce() { |
|
| 165 | + |
|
| 166 | + $nonce_name = 'wordlift_' . $this->meta_name . '_entity_box_nonce'; |
|
| 167 | + $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box'; |
|
| 168 | + |
|
| 169 | + if ( ! isset( $_POST[ $nonce_name ] ) ) { |
|
| 170 | + return false; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // Verify that the nonce is valid. |
|
| 174 | + return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify ); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Load data from DB and store the resulting array in $this->data. |
|
| 179 | + * |
|
| 180 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 181 | + */ |
|
| 182 | + public function get_data() { |
|
| 183 | + |
|
| 184 | + $data = get_post_meta( get_the_ID(), $this->meta_name ); |
|
| 185 | + |
|
| 186 | + // Values are always contained in an array (makes it easier to manage cardinality). |
|
| 187 | + if ( ! is_array( $data ) ) { |
|
| 188 | + $data = array( $data ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + $this->data = $data; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Sanitizes data before saving to DB. Default sanitization trashes empty |
|
| 196 | + * values. |
|
| 197 | + * |
|
| 198 | + * Stores the sanitized values into $this->data so they can be later processed. |
|
| 199 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 200 | + * |
|
| 201 | + * @param array $values Array of values to be sanitized and then stored into |
|
| 202 | + * $this->data. |
|
| 203 | + */ |
|
| 204 | + public function sanitize_data( $values ) { |
|
| 205 | + |
|
| 206 | + $sanitized_data = array(); |
|
| 207 | + |
|
| 208 | + if ( ! is_array( $values ) ) { |
|
| 209 | + $values = array( $values ); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + foreach ( $values as $value ) { |
|
| 213 | + $sanitized_value = $this->sanitize_data_filter( $value ); |
|
| 214 | + if ( ! is_null( $sanitized_value ) ) { |
|
| 215 | + $sanitized_data[] = $sanitized_value; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $this->data = $sanitized_data; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Sanitize a single value. Called from $this->sanitize_data. Default |
|
| 224 | + * sanitization excludes empty values. |
|
| 225 | + * |
|
| 226 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 227 | + * |
|
| 228 | + * @param string $value The value to sanitize. |
|
| 229 | + * |
|
| 230 | + * @return mixed Returns sanitized value, or null. |
|
| 231 | + */ |
|
| 232 | + public function sanitize_data_filter( $value ) { |
|
| 233 | + |
|
| 234 | + // TODO: all fields should provide their own sanitize which shouldn't |
|
| 235 | + // be part of a UI class. |
|
| 236 | + |
|
| 237 | + // If the field provides its own validation, use it. |
|
| 238 | + if ( isset( $this->raw_custom_field['sanitize'] ) ) { |
|
| 239 | + return call_user_func( $this->raw_custom_field['sanitize'], $value ); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 243 | + return $value; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + return null; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Save data to DB. |
|
| 251 | + * |
|
| 252 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 253 | + * |
|
| 254 | + * @param array $values Array of values to be sanitized and then stored into $this->data. |
|
| 255 | + */ |
|
| 256 | + public function save_data( $values ) { |
|
| 257 | + |
|
| 258 | + // Will sanitize data and store them in $field->data. |
|
| 259 | + $this->sanitize_data( $values ); |
|
| 260 | + |
|
| 261 | + $entity_id = get_the_ID(); |
|
| 262 | + |
|
| 263 | + // Take away old values. |
|
| 264 | + delete_post_meta( $entity_id, $this->meta_name ); |
|
| 265 | + |
|
| 266 | + // insert new values, respecting cardinality. |
|
| 267 | + $single = ( 1 === $this->cardinality ); |
|
| 268 | + foreach ( $this->data as $value ) { |
|
| 269 | + add_post_meta( $entity_id, $this->meta_name, $value, $single ); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Returns the HTML tag that will contain the Field. By default the we |
|
| 275 | + * return a <div> with data- attributes on cardinality and expected types. |
|
| 276 | + * |
|
| 277 | + * It is useful to provide data- attributes for the JS scripts. |
|
| 278 | + * |
|
| 279 | + * Overwrite this method in a child class to obtain custom behaviour. |
|
| 280 | + */ |
|
| 281 | + public function html_wrapper_open() { |
|
| 282 | + |
|
| 283 | + return "<div class='wl-field' data-cardinality='$this->cardinality'>"; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Returns Field HTML (nonce included). |
|
| 288 | + * |
|
| 289 | + * Overwrite this method (or methods called from this method) in a child |
|
| 290 | + * class to obtain custom behaviour. |
|
| 291 | + */ |
|
| 292 | + public function html() { |
|
| 293 | + |
|
| 294 | + // Open main <div> for the Field. |
|
| 295 | + $html = $this->html_wrapper_open(); |
|
| 296 | + |
|
| 297 | + // Label. |
|
| 298 | + $html .= "<h3>$this->label</h3>"; |
|
| 299 | + |
|
| 300 | + // print nonce. |
|
| 301 | + $html .= $this->html_nonce(); |
|
| 302 | + |
|
| 303 | + // print data loaded from DB. |
|
| 304 | + $count = 0; |
|
| 305 | + if ( $this->data ) { |
|
| 306 | + foreach ( $this->data as $value ) { |
|
| 307 | + if ( $count < $this->cardinality ) { |
|
| 308 | + $html .= $this->html_input( $value ); |
|
| 309 | + } |
|
| 310 | + $count ++; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + // Print the empty <input> to add new values. |
|
| 315 | + if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 316 | + $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 317 | + $count ++; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + // If cardiality allows it, print button to add new values. |
|
| 321 | + if ( $count < $this->cardinality ) { |
|
| 322 | + $html .= '<button class="button wl-add-input wl-button" type="button">Add</button>'; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + // Close the HTML wrapper. |
|
| 326 | + $html .= $this->html_wrapper_close(); |
|
| 327 | + |
|
| 328 | + return $html; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Return a single <input> tag for the Field. |
|
| 333 | + * |
|
| 334 | + * @param mixed $value Input value. |
|
| 335 | + * |
|
| 336 | + * @return string The html code fragment. |
|
| 337 | + */ |
|
| 338 | + public function html_input( $value ) { |
|
| 339 | + $html = <<<EOF |
|
| 340 | 340 | <div class="wl-input-wrapper"> |
| 341 | 341 | <input type="text" id="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$value" style="width:88%" /> |
| 342 | 342 | <button class="button wl-remove-input wl-button" type="button">Remove</button> |
| 343 | 343 | </div> |
| 344 | 344 | EOF; |
| 345 | 345 | |
| 346 | - return $html; |
|
| 347 | - } |
|
| 346 | + return $html; |
|
| 347 | + } |
|
| 348 | 348 | |
| 349 | - /** |
|
| 350 | - * Returns closing for the wrapper HTML tag. |
|
| 351 | - */ |
|
| 352 | - public function html_wrapper_close() { |
|
| 349 | + /** |
|
| 350 | + * Returns closing for the wrapper HTML tag. |
|
| 351 | + */ |
|
| 352 | + public function html_wrapper_close() { |
|
| 353 | 353 | |
| 354 | - return '</div><hr>'; |
|
| 355 | - } |
|
| 354 | + return '</div><hr>'; |
|
| 355 | + } |
|
| 356 | 356 | |
| 357 | 357 | } |
@@ -89,56 +89,56 @@ discard block |
||
| 89 | 89 | * |
| 90 | 90 | * @param array $args An array of parameters. |
| 91 | 91 | */ |
| 92 | - public function __construct( $args ) { |
|
| 92 | + public function __construct($args) { |
|
| 93 | 93 | |
| 94 | - $this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' ); |
|
| 94 | + $this->log = Wordlift_Log_Service::get_logger('WL_Metabox_Field'); |
|
| 95 | 95 | |
| 96 | - if ( empty( $args ) ) { |
|
| 96 | + if (empty($args)) { |
|
| 97 | 97 | return; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | // Save a copy of the custom field's params. |
| 101 | - $this->raw_custom_field = reset( $args ); |
|
| 101 | + $this->raw_custom_field = reset($args); |
|
| 102 | 102 | |
| 103 | 103 | // Extract meta name (post_meta key for the DB). |
| 104 | - $this->meta_name = key( $args ); |
|
| 104 | + $this->meta_name = key($args); |
|
| 105 | 105 | |
| 106 | 106 | // Extract linked data predicate. |
| 107 | - if ( isset( $this->raw_custom_field['predicate'] ) ) { |
|
| 107 | + if (isset($this->raw_custom_field['predicate'])) { |
|
| 108 | 108 | $this->predicate = $this->raw_custom_field['predicate']; |
| 109 | 109 | } else { |
| 110 | 110 | return; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | // Extract human readable label. |
| 114 | - $exploded_predicate = explode( '/', $this->predicate ); |
|
| 114 | + $exploded_predicate = explode('/', $this->predicate); |
|
| 115 | 115 | |
| 116 | 116 | // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate. |
| 117 | - $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate ); |
|
| 117 | + $this->label = isset($this->raw_custom_field['metabox']['label']) ? $this->raw_custom_field['metabox']['label'] : end($exploded_predicate); |
|
| 118 | 118 | |
| 119 | 119 | // Extract field constraints (numerosity, expected type). |
| 120 | 120 | // Default constaints: accept one string.. |
| 121 | - if ( isset( $this->raw_custom_field['type'] ) ) { |
|
| 121 | + if (isset($this->raw_custom_field['type'])) { |
|
| 122 | 122 | $this->expected_wl_type = $this->raw_custom_field['type']; |
| 123 | 123 | } else { |
| 124 | 124 | $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | $this->cardinality = 1; |
| 128 | - if ( isset( $this->raw_custom_field['constraints'] ) ) { |
|
| 128 | + if (isset($this->raw_custom_field['constraints'])) { |
|
| 129 | 129 | |
| 130 | 130 | $constraints = $this->raw_custom_field['constraints']; |
| 131 | 131 | |
| 132 | 132 | // Extract cardinality. |
| 133 | - if ( isset( $constraints['cardinality'] ) ) { |
|
| 133 | + if (isset($constraints['cardinality'])) { |
|
| 134 | 134 | $this->cardinality = $constraints['cardinality']; |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | // Which type of entity can we accept (e.g. Place, Event, ecc.)? . |
| 138 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) { |
|
| 139 | - $this->expected_uri_type = is_array( $constraints['uri_type'] ) |
|
| 138 | + if (Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset($constraints['uri_type'])) { |
|
| 139 | + $this->expected_uri_type = is_array($constraints['uri_type']) |
|
| 140 | 140 | ? $constraints['uri_type'] |
| 141 | - : array( $constraints['uri_type'] ); |
|
| 141 | + : array($constraints['uri_type']); |
|
| 142 | 142 | } |
| 143 | 143 | } |
| 144 | 144 | |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | public function html_nonce() { |
| 153 | 153 | |
| 154 | - return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false ); |
|
| 154 | + return wp_nonce_field('wordlift_'.$this->meta_name.'_entity_box', 'wordlift_'.$this->meta_name.'_entity_box_nonce', true, false); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -163,15 +163,15 @@ discard block |
||
| 163 | 163 | */ |
| 164 | 164 | public function verify_nonce() { |
| 165 | 165 | |
| 166 | - $nonce_name = 'wordlift_' . $this->meta_name . '_entity_box_nonce'; |
|
| 167 | - $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box'; |
|
| 166 | + $nonce_name = 'wordlift_'.$this->meta_name.'_entity_box_nonce'; |
|
| 167 | + $nonce_verify = 'wordlift_'.$this->meta_name.'_entity_box'; |
|
| 168 | 168 | |
| 169 | - if ( ! isset( $_POST[ $nonce_name ] ) ) { |
|
| 169 | + if ( ! isset($_POST[$nonce_name])) { |
|
| 170 | 170 | return false; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | // Verify that the nonce is valid. |
| 174 | - return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify ); |
|
| 174 | + return wp_verify_nonce($_POST[$nonce_name], $nonce_verify); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | /** |
@@ -181,11 +181,11 @@ discard block |
||
| 181 | 181 | */ |
| 182 | 182 | public function get_data() { |
| 183 | 183 | |
| 184 | - $data = get_post_meta( get_the_ID(), $this->meta_name ); |
|
| 184 | + $data = get_post_meta(get_the_ID(), $this->meta_name); |
|
| 185 | 185 | |
| 186 | 186 | // Values are always contained in an array (makes it easier to manage cardinality). |
| 187 | - if ( ! is_array( $data ) ) { |
|
| 188 | - $data = array( $data ); |
|
| 187 | + if ( ! is_array($data)) { |
|
| 188 | + $data = array($data); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | $this->data = $data; |
@@ -201,17 +201,17 @@ discard block |
||
| 201 | 201 | * @param array $values Array of values to be sanitized and then stored into |
| 202 | 202 | * $this->data. |
| 203 | 203 | */ |
| 204 | - public function sanitize_data( $values ) { |
|
| 204 | + public function sanitize_data($values) { |
|
| 205 | 205 | |
| 206 | 206 | $sanitized_data = array(); |
| 207 | 207 | |
| 208 | - if ( ! is_array( $values ) ) { |
|
| 209 | - $values = array( $values ); |
|
| 208 | + if ( ! is_array($values)) { |
|
| 209 | + $values = array($values); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | - foreach ( $values as $value ) { |
|
| 213 | - $sanitized_value = $this->sanitize_data_filter( $value ); |
|
| 214 | - if ( ! is_null( $sanitized_value ) ) { |
|
| 212 | + foreach ($values as $value) { |
|
| 213 | + $sanitized_value = $this->sanitize_data_filter($value); |
|
| 214 | + if ( ! is_null($sanitized_value)) { |
|
| 215 | 215 | $sanitized_data[] = $sanitized_value; |
| 216 | 216 | } |
| 217 | 217 | } |
@@ -229,17 +229,17 @@ discard block |
||
| 229 | 229 | * |
| 230 | 230 | * @return mixed Returns sanitized value, or null. |
| 231 | 231 | */ |
| 232 | - public function sanitize_data_filter( $value ) { |
|
| 232 | + public function sanitize_data_filter($value) { |
|
| 233 | 233 | |
| 234 | 234 | // TODO: all fields should provide their own sanitize which shouldn't |
| 235 | 235 | // be part of a UI class. |
| 236 | 236 | |
| 237 | 237 | // If the field provides its own validation, use it. |
| 238 | - if ( isset( $this->raw_custom_field['sanitize'] ) ) { |
|
| 239 | - return call_user_func( $this->raw_custom_field['sanitize'], $value ); |
|
| 238 | + if (isset($this->raw_custom_field['sanitize'])) { |
|
| 239 | + return call_user_func($this->raw_custom_field['sanitize'], $value); |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | - if ( ! is_null( $value ) && '' !== $value ) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 242 | + if ( ! is_null($value) && '' !== $value) { // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ . |
|
| 243 | 243 | return $value; |
| 244 | 244 | } |
| 245 | 245 | |
@@ -253,20 +253,20 @@ discard block |
||
| 253 | 253 | * |
| 254 | 254 | * @param array $values Array of values to be sanitized and then stored into $this->data. |
| 255 | 255 | */ |
| 256 | - public function save_data( $values ) { |
|
| 256 | + public function save_data($values) { |
|
| 257 | 257 | |
| 258 | 258 | // Will sanitize data and store them in $field->data. |
| 259 | - $this->sanitize_data( $values ); |
|
| 259 | + $this->sanitize_data($values); |
|
| 260 | 260 | |
| 261 | 261 | $entity_id = get_the_ID(); |
| 262 | 262 | |
| 263 | 263 | // Take away old values. |
| 264 | - delete_post_meta( $entity_id, $this->meta_name ); |
|
| 264 | + delete_post_meta($entity_id, $this->meta_name); |
|
| 265 | 265 | |
| 266 | 266 | // insert new values, respecting cardinality. |
| 267 | - $single = ( 1 === $this->cardinality ); |
|
| 268 | - foreach ( $this->data as $value ) { |
|
| 269 | - add_post_meta( $entity_id, $this->meta_name, $value, $single ); |
|
| 267 | + $single = (1 === $this->cardinality); |
|
| 268 | + foreach ($this->data as $value) { |
|
| 269 | + add_post_meta($entity_id, $this->meta_name, $value, $single); |
|
| 270 | 270 | } |
| 271 | 271 | } |
| 272 | 272 | |
@@ -302,23 +302,23 @@ discard block |
||
| 302 | 302 | |
| 303 | 303 | // print data loaded from DB. |
| 304 | 304 | $count = 0; |
| 305 | - if ( $this->data ) { |
|
| 306 | - foreach ( $this->data as $value ) { |
|
| 307 | - if ( $count < $this->cardinality ) { |
|
| 308 | - $html .= $this->html_input( $value ); |
|
| 305 | + if ($this->data) { |
|
| 306 | + foreach ($this->data as $value) { |
|
| 307 | + if ($count < $this->cardinality) { |
|
| 308 | + $html .= $this->html_input($value); |
|
| 309 | 309 | } |
| 310 | - $count ++; |
|
| 310 | + $count++; |
|
| 311 | 311 | } |
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | // Print the empty <input> to add new values. |
| 315 | - if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 316 | - $html .= $this->html_input( '' ); // Will print an empty <input>. |
|
| 317 | - $count ++; |
|
| 315 | + if (0 === $count) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings. |
|
| 316 | + $html .= $this->html_input(''); // Will print an empty <input>. |
|
| 317 | + $count++; |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | // If cardiality allows it, print button to add new values. |
| 321 | - if ( $count < $this->cardinality ) { |
|
| 321 | + if ($count < $this->cardinality) { |
|
| 322 | 322 | $html .= '<button class="button wl-add-input wl-button" type="button">Add</button>'; |
| 323 | 323 | } |
| 324 | 324 | |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | * |
| 336 | 336 | * @return string The html code fragment. |
| 337 | 337 | */ |
| 338 | - public function html_input( $value ) { |
|
| 338 | + public function html_input($value) { |
|
| 339 | 339 | $html = <<<EOF |
| 340 | 340 | <div class="wl-input-wrapper"> |
| 341 | 341 | <input type="text" id="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$value" style="width:88%" /> |