Completed
Pull Request — develop (#1298)
by
unknown
03:53
created
src/admin/WL_Metabox/class-wl-metabox-field.php 2 patches
Indentation   +446 added lines, -446 removed lines patch added patch discarded remove patch
@@ -19,443 +19,443 @@  discard block
 block discarded – undo
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
-	 * The current {@link WP_Post} id.
89
-	 *
90
-	 * @since 3.15.3
91
-	 *
92
-	 * @var int The current {@link WP_Post} id.
93
-	 */
94
-	private $post_id;
95
-
96
-	/**
97
-	 * Create a {@link WL_Metabox_Field} instance.
98
-	 *
99
-	 * @param array $args An array of parameters.
100
-	 */
101
-	public function __construct( $args ) {
102
-
103
-		$this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' );
104
-
105
-		if ( empty( $args ) ) {
106
-			return;
107
-		}
108
-
109
-		// Save a copy of the custom field's params.
110
-		$this->raw_custom_field = reset( $args );
111
-
112
-		// Extract meta name (post_meta key for the DB).
113
-		$this->meta_name = key( $args );
114
-
115
-		// Extract linked data predicate.
116
-		if ( isset( $this->raw_custom_field['predicate'] ) ) {
117
-			$this->predicate = $this->raw_custom_field['predicate'];
118
-		} else {
119
-			return;
120
-		}
121
-
122
-		// Extract human readable label.
123
-		$exploded_predicate = explode( '/', $this->predicate );
124
-
125
-		// Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate.
126
-		$this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate );
127
-
128
-		// Extract field constraints (numerosity, expected type).
129
-		// Default constaints: accept one string..
130
-		if ( isset( $this->raw_custom_field['type'] ) ) {
131
-			$this->expected_wl_type = $this->raw_custom_field['type'];
132
-		} else {
133
-			$this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING;
134
-		}
135
-
136
-		$this->cardinality = 1;
137
-		if ( isset( $this->raw_custom_field['constraints'] ) ) {
138
-
139
-			$constraints = $this->raw_custom_field['constraints'];
140
-
141
-			// Extract cardinality.
142
-			if ( isset( $constraints['cardinality'] ) ) {
143
-				$this->cardinality = $constraints['cardinality'];
144
-			}
145
-
146
-			// Which type of entity can we accept (e.g. Place, Event, ecc.)? .
147
-			if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) {
148
-				$this->expected_uri_type = is_array( $constraints['uri_type'] )
149
-					? $constraints['uri_type']
150
-					: array( $constraints['uri_type'] );
151
-			}
152
-		}
153
-
154
-		// Save early the post id to avoid other plugins messing up with it.
155
-		//
156
-		// See https://github.com/insideout10/wordlift-plugin/issues/665.
157
-		$this->post_id = get_the_ID();
158
-
159
-	}
160
-
161
-	/**
162
-	 * Return nonce HTML.
163
-	 *
164
-	 * Overwrite this method in a child class to obtain custom behaviour.
165
-	 */
166
-	public function html_nonce() {
167
-
168
-		return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false );
169
-	}
170
-
171
-	/**
172
-	 * Verify nonce.
173
-	 *
174
-	 * Overwrite this method in a child class to obtain custom behaviour.
175
-	 *
176
-	 * @return bool Nonce verification.
177
-	 */
178
-	public function verify_nonce() {
179
-
180
-		$nonce_name   = 'wordlift_' . $this->meta_name . '_entity_box_nonce';
181
-		$nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box';
182
-
183
-		if ( ! isset( $_POST[ $nonce_name ] ) ) {
184
-			return false;
185
-		}
186
-
187
-		// Verify that the nonce is valid.
188
-		return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify );
189
-	}
190
-
191
-	/**
192
-	 * Load data from DB and store the resulting array in $this->data.
193
-	 *
194
-	 * Overwrite this method in a child class to obtain custom behaviour.
195
-	 */
196
-	public function get_data() {
197
-
198
-		// Get the post id and load the data.
199
-		$post_id    = $this->post_id;
200
-		$this->data = get_post_meta( $post_id, $this->meta_name );
201
-
202
-	}
203
-
204
-	/**
205
-	 * Sanitizes data before saving to DB. Default sanitization trashes empty
206
-	 * values.
207
-	 *
208
-	 * Stores the sanitized values into $this->data so they can be later processed.
209
-	 * Overwrite this method in a child class to obtain custom behaviour.
210
-	 *
211
-	 * @param array $values Array of values to be sanitized and then stored into
212
-	 *                      $this->data.
213
-	 */
214
-	public function sanitize_data( $values ) {
215
-
216
-		$sanitized_data = array();
217
-
218
-		if ( ! is_array( $values ) ) {
219
-			$values = array( $values );
220
-		}
221
-
222
-		foreach ( $values as $value ) {
223
-			$sanitized_value = $this->sanitize_data_filter( $value );
224
-			if ( ! is_null( $sanitized_value ) ) {
225
-				$sanitized_data[] = $sanitized_value;
226
-			}
227
-		}
228
-
229
-		$this->data = $sanitized_data;
230
-	}
231
-
232
-	/**
233
-	 * Sanitize a single value. Called from $this->sanitize_data. Default
234
-	 * sanitization excludes empty values.
235
-	 *
236
-	 * Overwrite this method in a child class to obtain custom behaviour.
237
-	 *
238
-	 * @param string $value The value to sanitize.
239
-	 *
240
-	 * @return mixed Returns sanitized value, or null.
241
-	 */
242
-	public function sanitize_data_filter( $value ) {
243
-
244
-		// TODO: all fields should provide their own sanitize which shouldn't
245
-		// be part of a UI class.
246
-
247
-		// If the field provides its own validation, use it.
248
-		if ( isset( $this->raw_custom_field['sanitize'] ) ) {
249
-			return call_user_func( $this->raw_custom_field['sanitize'], $value );
250
-		}
251
-
252
-		if ( ! is_null( $value ) && '' !== $value ) {         // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ .
253
-			return $value;
254
-		}
255
-
256
-		return null;
257
-	}
258
-
259
-	/**
260
-	 * Save data to DB.
261
-	 *
262
-	 * Overwrite this method in a child class to obtain custom behaviour.
263
-	 *
264
-	 * @param array $values Array of values to be sanitized and then stored into $this->data.
265
-	 */
266
-	public function save_data( $values ) {
267
-
268
-		// Will sanitize data and store them in $field->data.
269
-		$this->sanitize_data( $values );
270
-
271
-		// Bail out, if the post id isn't set in the request or isn't numeric.
272
-		//
273
-		// See https://github.com/insideout10/wordlift-plugin/issues/665.
274
-		if ( ! isset( $_POST['post_ID'] ) || ! is_numeric( $_POST['post_ID'] ) ) {
275
-			return;
276
-		}
277
-
278
-		$entity_id = intval( $_POST['post_ID'] );
279
-
280
-		// Take away old values.
281
-		delete_post_meta( $entity_id, $this->meta_name );
282
-
283
-		// insert new values, respecting cardinality.
284
-		$single = ( 1 === $this->cardinality );
285
-		foreach ( $this->data as $value ) {
286
-			$this->log->trace( "Saving $value to $this->meta_name for entity $entity_id..." );
287
-			// To avoid duplicate values
288
-			delete_post_meta( $entity_id, $this->meta_name, $value );
289
-			$meta_id = add_post_meta( $entity_id, $this->meta_name, $value, $single );
290
-			$this->log->debug( "$value to $this->meta_name for entity $entity_id saved with id $meta_id." );
291
-		}
292
-	}
293
-
294
-	/**
295
-	 * Returns the HTML tag that will contain the Field. By default the we
296
-	 * return a <div> with data- attributes on cardinality and expected types.
297
-	 *
298
-	 * It is useful to provide data- attributes for the JS scripts.
299
-	 *
300
-	 * Overwrite this method in a child class to obtain custom behaviour.
301
-	 */
302
-	public function html_wrapper_open() {
303
-
304
-		return "<div class='wl-field' data-cardinality='$this->cardinality'>";
305
-	}
306
-
307
-	/**
308
-	 * Returns Field HTML (nonce included).
309
-	 *
310
-	 * Overwrite this method (or methods called from this method) in a child
311
-	 * class to obtain custom behaviour.
312
-	 *
313
-	 * The HTML fragment includes the following parts:
314
-	 * * html wrapper open.
315
-	 * * heading.
316
-	 * * nonce.
317
-	 * * stored values.
318
-	 * * an empty input when there are no stored values.
319
-	 * * an add button to add more values.
320
-	 * * html wrapper close.
321
-	 */
322
-	public function html() {
323
-
324
-		// Open main <div> for the Field.
325
-		$html = $this->html_wrapper_open();
326
-
327
-		// Label.
328
-		$html .= $this->get_heading_html();
329
-
330
-		// print nonce.
331
-		$html .= $this->html_nonce();
332
-
333
-		// print data loaded from DB.
334
-		$count = 0;
335
-		$html  .= $this->get_stored_values_html( $count );
336
-
337
-		// Print the empty <input> to add new values.
338
-		if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings.
339
-			$this->log->debug( 'Going to print an empty HTML input...' );
340
-			$html .= $this->html_input( '' );    // Will print an empty <input>.
341
-			$count ++;
342
-		}
343
-
344
-		// If cardinality allows it, print button to add new values.
345
-		$html .= $this->get_add_button_html( $count );
346
-
347
-		// Close the HTML wrapper.
348
-		$html .= $this->html_wrapper_close();
349
-
350
-		return $html;
351
-	}
352
-
353
-	/**
354
-	 * Print the heading with the label for the metabox.
355
-	 *
356
-	 * @since 3.15.0
357
-	 * @return string The heading html fragment.
358
-	 */
359
-	protected function get_heading_html() {
360
-
361
-		return "<h3>$this->label</h3>";
362
-	}
363
-
364
-	/**
365
-	 * Print the stored values.
366
-	 *
367
-	 * @since 3.15.0
368
-	 *
369
-	 * @param int $count An output value: the number of printed values.
370
-	 *
371
-	 * @return string The html fragment.
372
-	 */
373
-	protected function get_stored_values_html( &$count ) {
374
-
375
-		$html = '';
376
-
377
-		// print data loaded from DB.
378
-		$count = 0;
379
-		if ( $this->data ) {
380
-			foreach ( $this->data as $value ) {
381
-				if ( $count < $this->cardinality ) {
382
-					$this->log->debug( "Going to print an HTML input #$count with $value..." );
383
-					$fragment = $this->html_input( $value );
384
-
385
-					// If the fragment is empty, continue to the next one. This is necessary because the
386
-					// metabox may reference an invalid value which would cause the metabox not to print,
387
-					// returning an empty html fragment.
388
-					//
389
-					// See https://github.com/insideout10/wordlift-plugin/issues/818
390
-					if ( '' === $fragment ) {
391
-						continue;
392
-					}
393
-
394
-					$html .= $fragment;
395
-					$count ++;
396
-				}
397
-			}
398
-		}
399
-
400
-		return $html;
401
-	}
402
-
403
-	/**
404
-	 * Get the add button html.
405
-	 *
406
-	 * This function is protected, allowing extending class to further customize
407
-	 * the add button html code.
408
-	 *
409
-	 * @since 3.15.0
410
-	 *
411
-	 * @param int $count The current number of values.
412
-	 *
413
-	 * @return string The add button html code.
414
-	 */
415
-	protected function get_add_button_html( $count ) {
416
-
417
-		// If cardinality allows it, print button to add new values.
418
-		if ( $count < $this->cardinality ) {
419
-			return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>';
420
-		}
421
-
422
-		// Return an empty string.
423
-		return '';
424
-	}
425
-
426
-	/**
427
-	 * Get the add custom button html.
428
-	 *
429
-	 * This function is protected, allowing extending class to further customize
430
-	 * the add button html code.
431
-	 *
432
-	 * @since 3.15.0
433
-	 *
434
-	 * @param int $count The current number of values.
435
-	 *
436
-	 * @return string The add button html code.
437
-	 */
438
-	protected function get_add_custom_button_html( $count, $label, $class = '' ) {
439
-
440
-		// If cardinality allows it, print button to add new values.
441
-		if ( $count < $this->cardinality ) {
442
-			return '<button class="button wl-add-input wl-button wl-add-input--sameas '.$class.'" type="button">' . esc_html__( $label ) . '</button>';
443
-		}
444
-
445
-		// Return an empty string.
446
-		return '';
447
-	}
448
-
449
-	/**
450
-	 * Return a single <input> tag for the Field.
451
-	 *
452
-	 * @param mixed $value Input value.
453
-	 *
454
-	 * @return string The html code fragment.
455
-	 */
456
-	public function html_input( $value ) {
457
-		@ob_start();
458
-		?>
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
+     * The current {@link WP_Post} id.
89
+     *
90
+     * @since 3.15.3
91
+     *
92
+     * @var int The current {@link WP_Post} id.
93
+     */
94
+    private $post_id;
95
+
96
+    /**
97
+     * Create a {@link WL_Metabox_Field} instance.
98
+     *
99
+     * @param array $args An array of parameters.
100
+     */
101
+    public function __construct( $args ) {
102
+
103
+        $this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' );
104
+
105
+        if ( empty( $args ) ) {
106
+            return;
107
+        }
108
+
109
+        // Save a copy of the custom field's params.
110
+        $this->raw_custom_field = reset( $args );
111
+
112
+        // Extract meta name (post_meta key for the DB).
113
+        $this->meta_name = key( $args );
114
+
115
+        // Extract linked data predicate.
116
+        if ( isset( $this->raw_custom_field['predicate'] ) ) {
117
+            $this->predicate = $this->raw_custom_field['predicate'];
118
+        } else {
119
+            return;
120
+        }
121
+
122
+        // Extract human readable label.
123
+        $exploded_predicate = explode( '/', $this->predicate );
124
+
125
+        // Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate.
126
+        $this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate );
127
+
128
+        // Extract field constraints (numerosity, expected type).
129
+        // Default constaints: accept one string..
130
+        if ( isset( $this->raw_custom_field['type'] ) ) {
131
+            $this->expected_wl_type = $this->raw_custom_field['type'];
132
+        } else {
133
+            $this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING;
134
+        }
135
+
136
+        $this->cardinality = 1;
137
+        if ( isset( $this->raw_custom_field['constraints'] ) ) {
138
+
139
+            $constraints = $this->raw_custom_field['constraints'];
140
+
141
+            // Extract cardinality.
142
+            if ( isset( $constraints['cardinality'] ) ) {
143
+                $this->cardinality = $constraints['cardinality'];
144
+            }
145
+
146
+            // Which type of entity can we accept (e.g. Place, Event, ecc.)? .
147
+            if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) {
148
+                $this->expected_uri_type = is_array( $constraints['uri_type'] )
149
+                    ? $constraints['uri_type']
150
+                    : array( $constraints['uri_type'] );
151
+            }
152
+        }
153
+
154
+        // Save early the post id to avoid other plugins messing up with it.
155
+        //
156
+        // See https://github.com/insideout10/wordlift-plugin/issues/665.
157
+        $this->post_id = get_the_ID();
158
+
159
+    }
160
+
161
+    /**
162
+     * Return nonce HTML.
163
+     *
164
+     * Overwrite this method in a child class to obtain custom behaviour.
165
+     */
166
+    public function html_nonce() {
167
+
168
+        return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false );
169
+    }
170
+
171
+    /**
172
+     * Verify nonce.
173
+     *
174
+     * Overwrite this method in a child class to obtain custom behaviour.
175
+     *
176
+     * @return bool Nonce verification.
177
+     */
178
+    public function verify_nonce() {
179
+
180
+        $nonce_name   = 'wordlift_' . $this->meta_name . '_entity_box_nonce';
181
+        $nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box';
182
+
183
+        if ( ! isset( $_POST[ $nonce_name ] ) ) {
184
+            return false;
185
+        }
186
+
187
+        // Verify that the nonce is valid.
188
+        return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify );
189
+    }
190
+
191
+    /**
192
+     * Load data from DB and store the resulting array in $this->data.
193
+     *
194
+     * Overwrite this method in a child class to obtain custom behaviour.
195
+     */
196
+    public function get_data() {
197
+
198
+        // Get the post id and load the data.
199
+        $post_id    = $this->post_id;
200
+        $this->data = get_post_meta( $post_id, $this->meta_name );
201
+
202
+    }
203
+
204
+    /**
205
+     * Sanitizes data before saving to DB. Default sanitization trashes empty
206
+     * values.
207
+     *
208
+     * Stores the sanitized values into $this->data so they can be later processed.
209
+     * Overwrite this method in a child class to obtain custom behaviour.
210
+     *
211
+     * @param array $values Array of values to be sanitized and then stored into
212
+     *                      $this->data.
213
+     */
214
+    public function sanitize_data( $values ) {
215
+
216
+        $sanitized_data = array();
217
+
218
+        if ( ! is_array( $values ) ) {
219
+            $values = array( $values );
220
+        }
221
+
222
+        foreach ( $values as $value ) {
223
+            $sanitized_value = $this->sanitize_data_filter( $value );
224
+            if ( ! is_null( $sanitized_value ) ) {
225
+                $sanitized_data[] = $sanitized_value;
226
+            }
227
+        }
228
+
229
+        $this->data = $sanitized_data;
230
+    }
231
+
232
+    /**
233
+     * Sanitize a single value. Called from $this->sanitize_data. Default
234
+     * sanitization excludes empty values.
235
+     *
236
+     * Overwrite this method in a child class to obtain custom behaviour.
237
+     *
238
+     * @param string $value The value to sanitize.
239
+     *
240
+     * @return mixed Returns sanitized value, or null.
241
+     */
242
+    public function sanitize_data_filter( $value ) {
243
+
244
+        // TODO: all fields should provide their own sanitize which shouldn't
245
+        // be part of a UI class.
246
+
247
+        // If the field provides its own validation, use it.
248
+        if ( isset( $this->raw_custom_field['sanitize'] ) ) {
249
+            return call_user_func( $this->raw_custom_field['sanitize'], $value );
250
+        }
251
+
252
+        if ( ! is_null( $value ) && '' !== $value ) {         // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ .
253
+            return $value;
254
+        }
255
+
256
+        return null;
257
+    }
258
+
259
+    /**
260
+     * Save data to DB.
261
+     *
262
+     * Overwrite this method in a child class to obtain custom behaviour.
263
+     *
264
+     * @param array $values Array of values to be sanitized and then stored into $this->data.
265
+     */
266
+    public function save_data( $values ) {
267
+
268
+        // Will sanitize data and store them in $field->data.
269
+        $this->sanitize_data( $values );
270
+
271
+        // Bail out, if the post id isn't set in the request or isn't numeric.
272
+        //
273
+        // See https://github.com/insideout10/wordlift-plugin/issues/665.
274
+        if ( ! isset( $_POST['post_ID'] ) || ! is_numeric( $_POST['post_ID'] ) ) {
275
+            return;
276
+        }
277
+
278
+        $entity_id = intval( $_POST['post_ID'] );
279
+
280
+        // Take away old values.
281
+        delete_post_meta( $entity_id, $this->meta_name );
282
+
283
+        // insert new values, respecting cardinality.
284
+        $single = ( 1 === $this->cardinality );
285
+        foreach ( $this->data as $value ) {
286
+            $this->log->trace( "Saving $value to $this->meta_name for entity $entity_id..." );
287
+            // To avoid duplicate values
288
+            delete_post_meta( $entity_id, $this->meta_name, $value );
289
+            $meta_id = add_post_meta( $entity_id, $this->meta_name, $value, $single );
290
+            $this->log->debug( "$value to $this->meta_name for entity $entity_id saved with id $meta_id." );
291
+        }
292
+    }
293
+
294
+    /**
295
+     * Returns the HTML tag that will contain the Field. By default the we
296
+     * return a <div> with data- attributes on cardinality and expected types.
297
+     *
298
+     * It is useful to provide data- attributes for the JS scripts.
299
+     *
300
+     * Overwrite this method in a child class to obtain custom behaviour.
301
+     */
302
+    public function html_wrapper_open() {
303
+
304
+        return "<div class='wl-field' data-cardinality='$this->cardinality'>";
305
+    }
306
+
307
+    /**
308
+     * Returns Field HTML (nonce included).
309
+     *
310
+     * Overwrite this method (or methods called from this method) in a child
311
+     * class to obtain custom behaviour.
312
+     *
313
+     * The HTML fragment includes the following parts:
314
+     * * html wrapper open.
315
+     * * heading.
316
+     * * nonce.
317
+     * * stored values.
318
+     * * an empty input when there are no stored values.
319
+     * * an add button to add more values.
320
+     * * html wrapper close.
321
+     */
322
+    public function html() {
323
+
324
+        // Open main <div> for the Field.
325
+        $html = $this->html_wrapper_open();
326
+
327
+        // Label.
328
+        $html .= $this->get_heading_html();
329
+
330
+        // print nonce.
331
+        $html .= $this->html_nonce();
332
+
333
+        // print data loaded from DB.
334
+        $count = 0;
335
+        $html  .= $this->get_stored_values_html( $count );
336
+
337
+        // Print the empty <input> to add new values.
338
+        if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings.
339
+            $this->log->debug( 'Going to print an empty HTML input...' );
340
+            $html .= $this->html_input( '' );    // Will print an empty <input>.
341
+            $count ++;
342
+        }
343
+
344
+        // If cardinality allows it, print button to add new values.
345
+        $html .= $this->get_add_button_html( $count );
346
+
347
+        // Close the HTML wrapper.
348
+        $html .= $this->html_wrapper_close();
349
+
350
+        return $html;
351
+    }
352
+
353
+    /**
354
+     * Print the heading with the label for the metabox.
355
+     *
356
+     * @since 3.15.0
357
+     * @return string The heading html fragment.
358
+     */
359
+    protected function get_heading_html() {
360
+
361
+        return "<h3>$this->label</h3>";
362
+    }
363
+
364
+    /**
365
+     * Print the stored values.
366
+     *
367
+     * @since 3.15.0
368
+     *
369
+     * @param int $count An output value: the number of printed values.
370
+     *
371
+     * @return string The html fragment.
372
+     */
373
+    protected function get_stored_values_html( &$count ) {
374
+
375
+        $html = '';
376
+
377
+        // print data loaded from DB.
378
+        $count = 0;
379
+        if ( $this->data ) {
380
+            foreach ( $this->data as $value ) {
381
+                if ( $count < $this->cardinality ) {
382
+                    $this->log->debug( "Going to print an HTML input #$count with $value..." );
383
+                    $fragment = $this->html_input( $value );
384
+
385
+                    // If the fragment is empty, continue to the next one. This is necessary because the
386
+                    // metabox may reference an invalid value which would cause the metabox not to print,
387
+                    // returning an empty html fragment.
388
+                    //
389
+                    // See https://github.com/insideout10/wordlift-plugin/issues/818
390
+                    if ( '' === $fragment ) {
391
+                        continue;
392
+                    }
393
+
394
+                    $html .= $fragment;
395
+                    $count ++;
396
+                }
397
+            }
398
+        }
399
+
400
+        return $html;
401
+    }
402
+
403
+    /**
404
+     * Get the add button html.
405
+     *
406
+     * This function is protected, allowing extending class to further customize
407
+     * the add button html code.
408
+     *
409
+     * @since 3.15.0
410
+     *
411
+     * @param int $count The current number of values.
412
+     *
413
+     * @return string The add button html code.
414
+     */
415
+    protected function get_add_button_html( $count ) {
416
+
417
+        // If cardinality allows it, print button to add new values.
418
+        if ( $count < $this->cardinality ) {
419
+            return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>';
420
+        }
421
+
422
+        // Return an empty string.
423
+        return '';
424
+    }
425
+
426
+    /**
427
+     * Get the add custom button html.
428
+     *
429
+     * This function is protected, allowing extending class to further customize
430
+     * the add button html code.
431
+     *
432
+     * @since 3.15.0
433
+     *
434
+     * @param int $count The current number of values.
435
+     *
436
+     * @return string The add button html code.
437
+     */
438
+    protected function get_add_custom_button_html( $count, $label, $class = '' ) {
439
+
440
+        // If cardinality allows it, print button to add new values.
441
+        if ( $count < $this->cardinality ) {
442
+            return '<button class="button wl-add-input wl-button wl-add-input--sameas '.$class.'" type="button">' . esc_html__( $label ) . '</button>';
443
+        }
444
+
445
+        // Return an empty string.
446
+        return '';
447
+    }
448
+
449
+    /**
450
+     * Return a single <input> tag for the Field.
451
+     *
452
+     * @param mixed $value Input value.
453
+     *
454
+     * @return string The html code fragment.
455
+     */
456
+    public function html_input( $value ) {
457
+        @ob_start();
458
+        ?>
459 459
         <div class="wl-input-wrapper">
460 460
             <input
461 461
                     type="text"
@@ -470,17 +470,17 @@  discard block
 block discarded – undo
470 470
             </button>
471 471
         </div>
472 472
 		<?php
473
-		$html = ob_get_clean();
473
+        $html = ob_get_clean();
474 474
 
475
-		return $html;
476
-	}
475
+        return $html;
476
+    }
477 477
 
478
-	/**
479
-	 * Returns closing for the wrapper HTML tag.
480
-	 */
481
-	public function html_wrapper_close() {
478
+    /**
479
+     * Returns closing for the wrapper HTML tag.
480
+     */
481
+    public function html_wrapper_close() {
482 482
 
483
-		return '</div>';
484
-	}
483
+        return '</div>';
484
+    }
485 485
 
486 486
 }
Please login to merge, or discard this patch.
Spacing   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -98,56 +98,56 @@  discard block
 block discarded – undo
98 98
 	 *
99 99
 	 * @param array $args An array of parameters.
100 100
 	 */
101
-	public function __construct( $args ) {
101
+	public function __construct($args) {
102 102
 
103
-		$this->log = Wordlift_Log_Service::get_logger( 'WL_Metabox_Field' );
103
+		$this->log = Wordlift_Log_Service::get_logger('WL_Metabox_Field');
104 104
 
105
-		if ( empty( $args ) ) {
105
+		if (empty($args)) {
106 106
 			return;
107 107
 		}
108 108
 
109 109
 		// Save a copy of the custom field's params.
110
-		$this->raw_custom_field = reset( $args );
110
+		$this->raw_custom_field = reset($args);
111 111
 
112 112
 		// Extract meta name (post_meta key for the DB).
113
-		$this->meta_name = key( $args );
113
+		$this->meta_name = key($args);
114 114
 
115 115
 		// Extract linked data predicate.
116
-		if ( isset( $this->raw_custom_field['predicate'] ) ) {
116
+		if (isset($this->raw_custom_field['predicate'])) {
117 117
 			$this->predicate = $this->raw_custom_field['predicate'];
118 118
 		} else {
119 119
 			return;
120 120
 		}
121 121
 
122 122
 		// Extract human readable label.
123
-		$exploded_predicate = explode( '/', $this->predicate );
123
+		$exploded_predicate = explode('/', $this->predicate);
124 124
 
125 125
 		// Use the label defined for the property if set, otherwise the last part of the schema.org/xyz predicate.
126
-		$this->label = isset( $this->raw_custom_field['metabox']['label'] ) ? $this->raw_custom_field['metabox']['label'] : end( $exploded_predicate );
126
+		$this->label = isset($this->raw_custom_field['metabox']['label']) ? $this->raw_custom_field['metabox']['label'] : end($exploded_predicate);
127 127
 
128 128
 		// Extract field constraints (numerosity, expected type).
129 129
 		// Default constaints: accept one string..
130
-		if ( isset( $this->raw_custom_field['type'] ) ) {
130
+		if (isset($this->raw_custom_field['type'])) {
131 131
 			$this->expected_wl_type = $this->raw_custom_field['type'];
132 132
 		} else {
133 133
 			$this->expected_wl_type = Wordlift_Schema_Service::DATA_TYPE_STRING;
134 134
 		}
135 135
 
136 136
 		$this->cardinality = 1;
137
-		if ( isset( $this->raw_custom_field['constraints'] ) ) {
137
+		if (isset($this->raw_custom_field['constraints'])) {
138 138
 
139 139
 			$constraints = $this->raw_custom_field['constraints'];
140 140
 
141 141
 			// Extract cardinality.
142
-			if ( isset( $constraints['cardinality'] ) ) {
142
+			if (isset($constraints['cardinality'])) {
143 143
 				$this->cardinality = $constraints['cardinality'];
144 144
 			}
145 145
 
146 146
 			// Which type of entity can we accept (e.g. Place, Event, ecc.)? .
147
-			if ( Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset( $constraints['uri_type'] ) ) {
148
-				$this->expected_uri_type = is_array( $constraints['uri_type'] )
147
+			if (Wordlift_Schema_Service::DATA_TYPE_URI === $this->expected_wl_type && isset($constraints['uri_type'])) {
148
+				$this->expected_uri_type = is_array($constraints['uri_type'])
149 149
 					? $constraints['uri_type']
150
-					: array( $constraints['uri_type'] );
150
+					: array($constraints['uri_type']);
151 151
 			}
152 152
 		}
153 153
 
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
 	 */
166 166
 	public function html_nonce() {
167 167
 
168
-		return wp_nonce_field( 'wordlift_' . $this->meta_name . '_entity_box', 'wordlift_' . $this->meta_name . '_entity_box_nonce', true, false );
168
+		return wp_nonce_field('wordlift_'.$this->meta_name.'_entity_box', 'wordlift_'.$this->meta_name.'_entity_box_nonce', true, false);
169 169
 	}
170 170
 
171 171
 	/**
@@ -177,15 +177,15 @@  discard block
 block discarded – undo
177 177
 	 */
178 178
 	public function verify_nonce() {
179 179
 
180
-		$nonce_name   = 'wordlift_' . $this->meta_name . '_entity_box_nonce';
181
-		$nonce_verify = 'wordlift_' . $this->meta_name . '_entity_box';
180
+		$nonce_name   = 'wordlift_'.$this->meta_name.'_entity_box_nonce';
181
+		$nonce_verify = 'wordlift_'.$this->meta_name.'_entity_box';
182 182
 
183
-		if ( ! isset( $_POST[ $nonce_name ] ) ) {
183
+		if ( ! isset($_POST[$nonce_name])) {
184 184
 			return false;
185 185
 		}
186 186
 
187 187
 		// Verify that the nonce is valid.
188
-		return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_verify );
188
+		return wp_verify_nonce($_POST[$nonce_name], $nonce_verify);
189 189
 	}
190 190
 
191 191
 	/**
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
 
198 198
 		// Get the post id and load the data.
199 199
 		$post_id    = $this->post_id;
200
-		$this->data = get_post_meta( $post_id, $this->meta_name );
200
+		$this->data = get_post_meta($post_id, $this->meta_name);
201 201
 
202 202
 	}
203 203
 
@@ -211,17 +211,17 @@  discard block
 block discarded – undo
211 211
 	 * @param array $values Array of values to be sanitized and then stored into
212 212
 	 *                      $this->data.
213 213
 	 */
214
-	public function sanitize_data( $values ) {
214
+	public function sanitize_data($values) {
215 215
 
216 216
 		$sanitized_data = array();
217 217
 
218
-		if ( ! is_array( $values ) ) {
219
-			$values = array( $values );
218
+		if ( ! is_array($values)) {
219
+			$values = array($values);
220 220
 		}
221 221
 
222
-		foreach ( $values as $value ) {
223
-			$sanitized_value = $this->sanitize_data_filter( $value );
224
-			if ( ! is_null( $sanitized_value ) ) {
222
+		foreach ($values as $value) {
223
+			$sanitized_value = $this->sanitize_data_filter($value);
224
+			if ( ! is_null($sanitized_value)) {
225 225
 				$sanitized_data[] = $sanitized_value;
226 226
 			}
227 227
 		}
@@ -239,17 +239,17 @@  discard block
 block discarded – undo
239 239
 	 *
240 240
 	 * @return mixed Returns sanitized value, or null.
241 241
 	 */
242
-	public function sanitize_data_filter( $value ) {
242
+	public function sanitize_data_filter($value) {
243 243
 
244 244
 		// TODO: all fields should provide their own sanitize which shouldn't
245 245
 		// be part of a UI class.
246 246
 
247 247
 		// If the field provides its own validation, use it.
248
-		if ( isset( $this->raw_custom_field['sanitize'] ) ) {
249
-			return call_user_func( $this->raw_custom_field['sanitize'], $value );
248
+		if (isset($this->raw_custom_field['sanitize'])) {
249
+			return call_user_func($this->raw_custom_field['sanitize'], $value);
250 250
 		}
251 251
 
252
-		if ( ! is_null( $value ) && '' !== $value ) {         // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ .
252
+		if ( ! is_null($value) && '' !== $value) {         // do not use 'empty()' -> https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ .
253 253
 			return $value;
254 254
 		}
255 255
 
@@ -263,31 +263,31 @@  discard block
 block discarded – undo
263 263
 	 *
264 264
 	 * @param array $values Array of values to be sanitized and then stored into $this->data.
265 265
 	 */
266
-	public function save_data( $values ) {
266
+	public function save_data($values) {
267 267
 
268 268
 		// Will sanitize data and store them in $field->data.
269
-		$this->sanitize_data( $values );
269
+		$this->sanitize_data($values);
270 270
 
271 271
 		// Bail out, if the post id isn't set in the request or isn't numeric.
272 272
 		//
273 273
 		// See https://github.com/insideout10/wordlift-plugin/issues/665.
274
-		if ( ! isset( $_POST['post_ID'] ) || ! is_numeric( $_POST['post_ID'] ) ) {
274
+		if ( ! isset($_POST['post_ID']) || ! is_numeric($_POST['post_ID'])) {
275 275
 			return;
276 276
 		}
277 277
 
278
-		$entity_id = intval( $_POST['post_ID'] );
278
+		$entity_id = intval($_POST['post_ID']);
279 279
 
280 280
 		// Take away old values.
281
-		delete_post_meta( $entity_id, $this->meta_name );
281
+		delete_post_meta($entity_id, $this->meta_name);
282 282
 
283 283
 		// insert new values, respecting cardinality.
284
-		$single = ( 1 === $this->cardinality );
285
-		foreach ( $this->data as $value ) {
286
-			$this->log->trace( "Saving $value to $this->meta_name for entity $entity_id..." );
284
+		$single = (1 === $this->cardinality);
285
+		foreach ($this->data as $value) {
286
+			$this->log->trace("Saving $value to $this->meta_name for entity $entity_id...");
287 287
 			// To avoid duplicate values
288
-			delete_post_meta( $entity_id, $this->meta_name, $value );
289
-			$meta_id = add_post_meta( $entity_id, $this->meta_name, $value, $single );
290
-			$this->log->debug( "$value to $this->meta_name for entity $entity_id saved with id $meta_id." );
288
+			delete_post_meta($entity_id, $this->meta_name, $value);
289
+			$meta_id = add_post_meta($entity_id, $this->meta_name, $value, $single);
290
+			$this->log->debug("$value to $this->meta_name for entity $entity_id saved with id $meta_id.");
291 291
 		}
292 292
 	}
293 293
 
@@ -332,17 +332,17 @@  discard block
 block discarded – undo
332 332
 
333 333
 		// print data loaded from DB.
334 334
 		$count = 0;
335
-		$html  .= $this->get_stored_values_html( $count );
335
+		$html .= $this->get_stored_values_html($count);
336 336
 
337 337
 		// Print the empty <input> to add new values.
338
-		if ( 0 === $count ) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings.
339
-			$this->log->debug( 'Going to print an empty HTML input...' );
340
-			$html .= $this->html_input( '' );    // Will print an empty <input>.
341
-			$count ++;
338
+		if (0 === $count) { // } || $count < $this->cardinality ) { DO NOT print empty inputs unless requested by the editor since fields might support empty strings.
339
+			$this->log->debug('Going to print an empty HTML input...');
340
+			$html .= $this->html_input(''); // Will print an empty <input>.
341
+			$count++;
342 342
 		}
343 343
 
344 344
 		// If cardinality allows it, print button to add new values.
345
-		$html .= $this->get_add_button_html( $count );
345
+		$html .= $this->get_add_button_html($count);
346 346
 
347 347
 		// Close the HTML wrapper.
348 348
 		$html .= $this->html_wrapper_close();
@@ -370,29 +370,29 @@  discard block
 block discarded – undo
370 370
 	 *
371 371
 	 * @return string The html fragment.
372 372
 	 */
373
-	protected function get_stored_values_html( &$count ) {
373
+	protected function get_stored_values_html(&$count) {
374 374
 
375 375
 		$html = '';
376 376
 
377 377
 		// print data loaded from DB.
378 378
 		$count = 0;
379
-		if ( $this->data ) {
380
-			foreach ( $this->data as $value ) {
381
-				if ( $count < $this->cardinality ) {
382
-					$this->log->debug( "Going to print an HTML input #$count with $value..." );
383
-					$fragment = $this->html_input( $value );
379
+		if ($this->data) {
380
+			foreach ($this->data as $value) {
381
+				if ($count < $this->cardinality) {
382
+					$this->log->debug("Going to print an HTML input #$count with $value...");
383
+					$fragment = $this->html_input($value);
384 384
 
385 385
 					// If the fragment is empty, continue to the next one. This is necessary because the
386 386
 					// metabox may reference an invalid value which would cause the metabox not to print,
387 387
 					// returning an empty html fragment.
388 388
 					//
389 389
 					// See https://github.com/insideout10/wordlift-plugin/issues/818
390
-					if ( '' === $fragment ) {
390
+					if ('' === $fragment) {
391 391
 						continue;
392 392
 					}
393 393
 
394 394
 					$html .= $fragment;
395
-					$count ++;
395
+					$count++;
396 396
 				}
397 397
 			}
398 398
 		}
@@ -412,11 +412,11 @@  discard block
 block discarded – undo
412 412
 	 *
413 413
 	 * @return string The add button html code.
414 414
 	 */
415
-	protected function get_add_button_html( $count ) {
415
+	protected function get_add_button_html($count) {
416 416
 
417 417
 		// If cardinality allows it, print button to add new values.
418
-		if ( $count < $this->cardinality ) {
419
-			return '<button class="button wl-add-input wl-button" type="button">' . esc_html__( 'Add' ) . '</button>';
418
+		if ($count < $this->cardinality) {
419
+			return '<button class="button wl-add-input wl-button" type="button">'.esc_html__('Add').'</button>';
420 420
 		}
421 421
 
422 422
 		// Return an empty string.
@@ -435,11 +435,11 @@  discard block
 block discarded – undo
435 435
 	 *
436 436
 	 * @return string The add button html code.
437 437
 	 */
438
-	protected function get_add_custom_button_html( $count, $label, $class = '' ) {
438
+	protected function get_add_custom_button_html($count, $label, $class = '') {
439 439
 
440 440
 		// If cardinality allows it, print button to add new values.
441
-		if ( $count < $this->cardinality ) {
442
-			return '<button class="button wl-add-input wl-button wl-add-input--sameas '.$class.'" type="button">' . esc_html__( $label ) . '</button>';
441
+		if ($count < $this->cardinality) {
442
+			return '<button class="button wl-add-input wl-button wl-add-input--sameas '.$class.'" type="button">'.esc_html__($label).'</button>';
443 443
 		}
444 444
 
445 445
 		// Return an empty string.
@@ -453,20 +453,20 @@  discard block
 block discarded – undo
453 453
 	 *
454 454
 	 * @return string The html code fragment.
455 455
 	 */
456
-	public function html_input( $value ) {
456
+	public function html_input($value) {
457 457
 		@ob_start();
458 458
 		?>
459 459
         <div class="wl-input-wrapper">
460 460
             <input
461 461
                     type="text"
462
-                    id="<?php echo esc_attr( $this->meta_name ); ?>"
462
+                    id="<?php echo esc_attr($this->meta_name); ?>"
463 463
                     name="wl_metaboxes[<?php echo $this->meta_name ?>][]"
464
-                    value="<?php echo esc_attr( $value ); ?>"
464
+                    value="<?php echo esc_attr($value); ?>"
465 465
                     style="width:88%"
466 466
             />
467 467
 
468 468
             <button class="button wl-remove-input wl-button" type="button">
469
-				<?php esc_html_e( 'Remove', 'wordlift' ); ?>
469
+				<?php esc_html_e('Remove', 'wordlift'); ?>
470 470
             </button>
471 471
         </div>
472 472
 		<?php
Please login to merge, or discard this patch.
src/admin/WL_Metabox/class-wl-metabox-field-sameas.php 2 patches
Indentation   +132 added lines, -132 removed lines patch added patch discarded remove patch
@@ -16,131 +16,131 @@  discard block
 block discarded – undo
16 16
  */
17 17
 class WL_Metabox_Field_sameas extends WL_Metabox_Field {
18 18
 
19
-	/**
20
-	 * @inheritdoc
21
-	 */
22
-	public function __construct( $args ) {
23
-		parent::__construct( $args['sameas'] );
24
-	}
25
-
26
-	/**
27
-	 * @inheritdoc
28
-	 */
29
-	public function save_data( $values ) {
30
-		// The autocomplete select may send JSON arrays in input values.
31
-
32
-		// Only use mb_* functions when mbstring is available.
33
-		//
34
-		// See https://github.com/insideout10/wordlift-plugin/issues/693.
35
-		if ( extension_loaded( 'mbstring' ) ) {
36
-			mb_regex_encoding( 'UTF-8' );
37
-
38
-			$merged = array_reduce( (array) $values, function ( $carry, $item ) {
39
-				return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) );
40
-			}, array() );
41
-		} else {
42
-			$merged = array_reduce( (array) $values, function ( $carry, $item ) {
43
-				return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) );
44
-			}, array() );
45
-		}
46
-
47
-		// Convert all escaped special characters to their original.
48
-		$merged = array_map( 'urldecode', $merged );
49
-
50
-		$merged = $this->filter_urls( $merged );
51
-
52
-		parent::save_data( $merged );
53
-	}
54
-
55
-	/**
56
-	 * @inheritdoc
57
-	 */
58
-	public function sanitize_data_filter( $value ) {
59
-
60
-		// Call our sanitizer helper.
61
-		return Wordlift_Sanitizer::sanitize_url( $value );
62
-	}
63
-
64
-	/**
65
-	 * @inheritdoc
66
-	 */
67
-	protected function get_heading_html() {
68
-
69
-		// Add the select html fragment after the heading.
70
-		return parent::get_heading_html()
71
-		       . $this->get_select_html();
72
-	}
73
-
74
-	/**
75
-	 * Get the select html fragment.
76
-	 *
77
-	 * @return string The html fragment.
78
-	 * @since 3.15.0
79
-	 */
80
-	private function get_select_html() {
81
-		// Return an element where the new Autocomplete Select will attach to.
82
-		return '<p>'
83
-		       . esc_html__( 'Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift' )
84
-		       . '<div id="wl-metabox-field-sameas"></div></p>';
85
-	}
86
-
87
-	/**
88
-	 * @inheritdoc
89
-	 */
90
-	protected function get_add_button_html( $count ) {
91
-
92
-		$placeholder = esc_attr_x( 'Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift' );
93
-
94
-		return
95
-  		'<button class="wl-add-input wl-add-input--link">'.esc_html__( 'Click here to manually add URLs', 'wordlift' ).'</button>'
19
+    /**
20
+     * @inheritdoc
21
+     */
22
+    public function __construct( $args ) {
23
+        parent::__construct( $args['sameas'] );
24
+    }
25
+
26
+    /**
27
+     * @inheritdoc
28
+     */
29
+    public function save_data( $values ) {
30
+        // The autocomplete select may send JSON arrays in input values.
31
+
32
+        // Only use mb_* functions when mbstring is available.
33
+        //
34
+        // See https://github.com/insideout10/wordlift-plugin/issues/693.
35
+        if ( extension_loaded( 'mbstring' ) ) {
36
+            mb_regex_encoding( 'UTF-8' );
37
+
38
+            $merged = array_reduce( (array) $values, function ( $carry, $item ) {
39
+                return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) );
40
+            }, array() );
41
+        } else {
42
+            $merged = array_reduce( (array) $values, function ( $carry, $item ) {
43
+                return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) );
44
+            }, array() );
45
+        }
46
+
47
+        // Convert all escaped special characters to their original.
48
+        $merged = array_map( 'urldecode', $merged );
49
+
50
+        $merged = $this->filter_urls( $merged );
51
+
52
+        parent::save_data( $merged );
53
+    }
54
+
55
+    /**
56
+     * @inheritdoc
57
+     */
58
+    public function sanitize_data_filter( $value ) {
59
+
60
+        // Call our sanitizer helper.
61
+        return Wordlift_Sanitizer::sanitize_url( $value );
62
+    }
63
+
64
+    /**
65
+     * @inheritdoc
66
+     */
67
+    protected function get_heading_html() {
68
+
69
+        // Add the select html fragment after the heading.
70
+        return parent::get_heading_html()
71
+                . $this->get_select_html();
72
+    }
73
+
74
+    /**
75
+     * Get the select html fragment.
76
+     *
77
+     * @return string The html fragment.
78
+     * @since 3.15.0
79
+     */
80
+    private function get_select_html() {
81
+        // Return an element where the new Autocomplete Select will attach to.
82
+        return '<p>'
83
+                . esc_html__( 'Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift' )
84
+                . '<div id="wl-metabox-field-sameas"></div></p>';
85
+    }
86
+
87
+    /**
88
+     * @inheritdoc
89
+     */
90
+    protected function get_add_button_html( $count ) {
91
+
92
+        $placeholder = esc_attr_x( 'Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift' );
93
+
94
+        return
95
+            '<button class="wl-add-input wl-add-input--link">'.esc_html__( 'Click here to manually add URLs', 'wordlift' ).'</button>'
96 96
         .'<div style="display: none;"><div class="wl-input-wrapper">'
97 97
         ."<input type='text' id='$this->meta_name' name='wl_metaboxes[$this->meta_name][]' placeholder='$placeholder' />"
98 98
         .'<button class="wl-remove-input wl-remove-input--sameas"></button>'
99 99
         .'</div></div>'
100 100
         . '<fieldset id="wl-input-container">'.$this->get_stored_values_html( $count ).'</fieldset>'
101 101
         .parent::get_add_custom_button_html( $count, 'Add Another URL', 'hide' );
102
-	}
102
+    }
103 103
 
104
-	/**
105
-	 * @inheritdoc
106
-	 */
107
-	protected function get_stored_values_html( &$count ) {
104
+    /**
105
+     * @inheritdoc
106
+     */
107
+    protected function get_stored_values_html( &$count ) {
108 108
 
109
-		return  parent::get_stored_values_html( $count );
110
-	}
109
+        return  parent::get_stored_values_html( $count );
110
+    }
111 111
 
112
-	/**
113
-	 * @inheritdoc
114
-	 */
115
-	public function html() {
112
+    /**
113
+     * @inheritdoc
114
+     */
115
+    public function html() {
116 116
 
117
-		// Open main <div> for the Field.
118
-		$html = $this->html_wrapper_open();
117
+        // Open main <div> for the Field.
118
+        $html = $this->html_wrapper_open();
119 119
 
120
-		// Label.
121
-		$html .= $this->get_heading_html();
120
+        // Label.
121
+        $html .= $this->get_heading_html();
122 122
 
123
-		// print nonce.
124
-		$html .= $this->html_nonce();
123
+        // print nonce.
124
+        $html .= $this->html_nonce();
125 125
 
126
-		// print data loaded from DB.
127
-		$count = 0;
126
+        // print data loaded from DB.
127
+        $count = 0;
128 128
 
129
-		// If cardinality allows it, print button to add new values.
130
-		$html .= $this->get_add_button_html( $count );
129
+        // If cardinality allows it, print button to add new values.
130
+        $html .= $this->get_add_button_html( $count );
131 131
 
132
-		// Close the HTML wrapper.
133
-		$html .= $this->html_wrapper_close();
132
+        // Close the HTML wrapper.
133
+        $html .= $this->html_wrapper_close();
134 134
 
135
-		return $html;
136
-	}
135
+        return $html;
136
+    }
137 137
 
138
-	/**
139
-	 * @inheritdoc
140
-	 */
141
-	public function html_input( $value ) {
142
-		@ob_start();
143
-		?>
138
+    /**
139
+     * @inheritdoc
140
+     */
141
+    public function html_input( $value ) {
142
+        @ob_start();
143
+        ?>
144 144
         <div class="wl-input-wrapper wl-input-wrapper-readonly">
145 145
             <input
146 146
                     type="text"
@@ -154,29 +154,29 @@  discard block
 block discarded – undo
154 154
         </div>
155 155
 		<?php
156 156
 
157
-		$html = ob_get_clean();
158
-
159
-		return $html;
160
-	}
161
-
162
-	/**
163
-	 * @param array $urls
164
-	 *
165
-	 * @return array
166
-	 */
167
-	private function filter_urls( $urls ) {
168
-		$configuration_service = Wordlift_Configuration_Service::get_instance();
169
-		$dataset_uri           = $configuration_service->get_dataset_uri();
170
-
171
-		return array_filter( $urls, function ( $url ) use ( $dataset_uri ) {
172
-			$url_validation = filter_var( $url, FILTER_VALIDATE_URL );
173
-			if ( null === $dataset_uri ) {
174
-				return $url_validation;
175
-			}
176
-
177
-			// URLs should not start with local dataset uri.
178
-			return $url_validation && ( strpos( $url, $dataset_uri, 0 ) === false );
179
-		} );
180
-	}
157
+        $html = ob_get_clean();
158
+
159
+        return $html;
160
+    }
161
+
162
+    /**
163
+     * @param array $urls
164
+     *
165
+     * @return array
166
+     */
167
+    private function filter_urls( $urls ) {
168
+        $configuration_service = Wordlift_Configuration_Service::get_instance();
169
+        $dataset_uri           = $configuration_service->get_dataset_uri();
170
+
171
+        return array_filter( $urls, function ( $url ) use ( $dataset_uri ) {
172
+            $url_validation = filter_var( $url, FILTER_VALIDATE_URL );
173
+            if ( null === $dataset_uri ) {
174
+                return $url_validation;
175
+            }
176
+
177
+            // URLs should not start with local dataset uri.
178
+            return $url_validation && ( strpos( $url, $dataset_uri, 0 ) === false );
179
+        } );
180
+    }
181 181
 
182 182
 }
Please login to merge, or discard this patch.
Spacing   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -19,46 +19,46 @@  discard block
 block discarded – undo
19 19
 	/**
20 20
 	 * @inheritdoc
21 21
 	 */
22
-	public function __construct( $args ) {
23
-		parent::__construct( $args['sameas'] );
22
+	public function __construct($args) {
23
+		parent::__construct($args['sameas']);
24 24
 	}
25 25
 
26 26
 	/**
27 27
 	 * @inheritdoc
28 28
 	 */
29
-	public function save_data( $values ) {
29
+	public function save_data($values) {
30 30
 		// The autocomplete select may send JSON arrays in input values.
31 31
 
32 32
 		// Only use mb_* functions when mbstring is available.
33 33
 		//
34 34
 		// See https://github.com/insideout10/wordlift-plugin/issues/693.
35
-		if ( extension_loaded( 'mbstring' ) ) {
36
-			mb_regex_encoding( 'UTF-8' );
35
+		if (extension_loaded('mbstring')) {
36
+			mb_regex_encoding('UTF-8');
37 37
 
38
-			$merged = array_reduce( (array) $values, function ( $carry, $item ) {
39
-				return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) );
40
-			}, array() );
38
+			$merged = array_reduce((array) $values, function($carry, $item) {
39
+				return array_merge($carry, mb_split("\x{2063}", wp_unslash($item)));
40
+			}, array());
41 41
 		} else {
42
-			$merged = array_reduce( (array) $values, function ( $carry, $item ) {
43
-				return array_merge( $carry, preg_split( "/\x{2063}/u", wp_unslash( $item ) ) );
44
-			}, array() );
42
+			$merged = array_reduce((array) $values, function($carry, $item) {
43
+				return array_merge($carry, preg_split("/\x{2063}/u", wp_unslash($item)));
44
+			}, array());
45 45
 		}
46 46
 
47 47
 		// Convert all escaped special characters to their original.
48
-		$merged = array_map( 'urldecode', $merged );
48
+		$merged = array_map('urldecode', $merged);
49 49
 
50
-		$merged = $this->filter_urls( $merged );
50
+		$merged = $this->filter_urls($merged);
51 51
 
52
-		parent::save_data( $merged );
52
+		parent::save_data($merged);
53 53
 	}
54 54
 
55 55
 	/**
56 56
 	 * @inheritdoc
57 57
 	 */
58
-	public function sanitize_data_filter( $value ) {
58
+	public function sanitize_data_filter($value) {
59 59
 
60 60
 		// Call our sanitizer helper.
61
-		return Wordlift_Sanitizer::sanitize_url( $value );
61
+		return Wordlift_Sanitizer::sanitize_url($value);
62 62
 	}
63 63
 
64 64
 	/**
@@ -80,33 +80,33 @@  discard block
 block discarded – undo
80 80
 	private function get_select_html() {
81 81
 		// Return an element where the new Autocomplete Select will attach to.
82 82
 		return '<p>'
83
-		       . esc_html__( 'Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift' )
83
+		       . esc_html__('Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift')
84 84
 		       . '<div id="wl-metabox-field-sameas"></div></p>';
85 85
 	}
86 86
 
87 87
 	/**
88 88
 	 * @inheritdoc
89 89
 	 */
90
-	protected function get_add_button_html( $count ) {
90
+	protected function get_add_button_html($count) {
91 91
 
92
-		$placeholder = esc_attr_x( 'Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift' );
92
+		$placeholder = esc_attr_x('Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift');
93 93
 
94 94
 		return
95
-  		'<button class="wl-add-input wl-add-input--link">'.esc_html__( 'Click here to manually add URLs', 'wordlift' ).'</button>'
95
+  		'<button class="wl-add-input wl-add-input--link">'.esc_html__('Click here to manually add URLs', 'wordlift').'</button>'
96 96
         .'<div style="display: none;"><div class="wl-input-wrapper">'
97 97
         ."<input type='text' id='$this->meta_name' name='wl_metaboxes[$this->meta_name][]' placeholder='$placeholder' />"
98 98
         .'<button class="wl-remove-input wl-remove-input--sameas"></button>'
99 99
         .'</div></div>'
100
-        . '<fieldset id="wl-input-container">'.$this->get_stored_values_html( $count ).'</fieldset>'
101
-        .parent::get_add_custom_button_html( $count, 'Add Another URL', 'hide' );
100
+        . '<fieldset id="wl-input-container">'.$this->get_stored_values_html($count).'</fieldset>'
101
+        .parent::get_add_custom_button_html($count, 'Add Another URL', 'hide');
102 102
 	}
103 103
 
104 104
 	/**
105 105
 	 * @inheritdoc
106 106
 	 */
107
-	protected function get_stored_values_html( &$count ) {
107
+	protected function get_stored_values_html(&$count) {
108 108
 
109
-		return  parent::get_stored_values_html( $count );
109
+		return  parent::get_stored_values_html($count);
110 110
 	}
111 111
 
112 112
 	/**
@@ -127,7 +127,7 @@  discard block
 block discarded – undo
127 127
 		$count = 0;
128 128
 
129 129
 		// If cardinality allows it, print button to add new values.
130
-		$html .= $this->get_add_button_html( $count );
130
+		$html .= $this->get_add_button_html($count);
131 131
 
132 132
 		// Close the HTML wrapper.
133 133
 		$html .= $this->html_wrapper_close();
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
 	/**
139 139
 	 * @inheritdoc
140 140
 	 */
141
-	public function html_input( $value ) {
141
+	public function html_input($value) {
142 142
 		@ob_start();
143 143
 		?>
144 144
         <div class="wl-input-wrapper wl-input-wrapper-readonly">
@@ -164,18 +164,18 @@  discard block
 block discarded – undo
164 164
 	 *
165 165
 	 * @return array
166 166
 	 */
167
-	private function filter_urls( $urls ) {
167
+	private function filter_urls($urls) {
168 168
 		$configuration_service = Wordlift_Configuration_Service::get_instance();
169 169
 		$dataset_uri           = $configuration_service->get_dataset_uri();
170 170
 
171
-		return array_filter( $urls, function ( $url ) use ( $dataset_uri ) {
172
-			$url_validation = filter_var( $url, FILTER_VALIDATE_URL );
173
-			if ( null === $dataset_uri ) {
171
+		return array_filter($urls, function($url) use ($dataset_uri) {
172
+			$url_validation = filter_var($url, FILTER_VALIDATE_URL);
173
+			if (null === $dataset_uri) {
174 174
 				return $url_validation;
175 175
 			}
176 176
 
177 177
 			// URLs should not start with local dataset uri.
178
-			return $url_validation && ( strpos( $url, $dataset_uri, 0 ) === false );
178
+			return $url_validation && (strpos($url, $dataset_uri, 0) === false);
179 179
 		} );
180 180
 	}
181 181
 
Please login to merge, or discard this patch.