Completed
Pull Request — master (#1475)
by Naveen
01:11
created
src/wordlift/metabox/class-wl-abstract-metabox.php 2 patches
Indentation   +346 added lines, -346 removed lines patch added patch discarded remove patch
@@ -18,364 +18,364 @@
 block discarded – undo
18 18
  * @subpackage Wordlift/admin/WL_Metabox
19 19
  */
20 20
 class Wl_Abstract_Metabox {
21
-	/**
22
-	 * The metabox custom fields for the current {@link WP_Post}.
23
-	 *
24
-	 * @since  3.1.0
25
-	 * @access public
26
-	 * @var array $fields The metabox custom fields.
27
-	 */
28
-	public $fields;
29
-
30
-	/**
31
-	 * A {@link Wordlift_Log_Service} instance.
32
-	 *
33
-	 * @since 3.15.4
34
-	 *
35
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
36
-	 */
37
-	private $log;
38
-
39
-	/**
40
-	 * WL_Metabox constructor.
41
-	 *
42
-	 * @since 3.1.0
43
-	 */
44
-	public function __construct() {
45
-
46
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
47
-	}
48
-
49
-	/**
50
-	 * Add a callback to print the metabox in page.
51
-	 * Wordpress will fire the $this->html() callback at the right time.
52
-	 */
53
-	public function add_main_metabox() {
54
-
55
-		// Build the fields we need to print.
56
-		$this->instantiate_fields( get_the_ID(), Object_Type_Enum::POST );
57
-
58
-		// Bailout if there are no actual fields, we do not need a metabox in that case.
59
-		if ( empty( $this->fields ) ) {
60
-			return;
61
-		}
62
-
63
-		// Add main metabox (will print also the inner fields).
64
-		$id    = uniqid( 'wl-metabox-' );
65
-		$title = get_the_title() . ' ' . __( 'properties', 'wordlift' );
66
-
67
-		// WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit.
68
-		foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) {
69
-			add_meta_box( $id, $title, array(
70
-				$this,
71
-				'html',
72
-			), $screen, 'normal', 'high' );
73
-		}
74
-
75
-		// Add filter to change the metabox CSS class.
76
-		//
77
-		// @since 3.20.0 Since we support post types other than `entity` for entities, we need to set the `screen`
78
-		//  dynamically according to the `get_current_screen()` function.
79
-		$current_screen = get_current_screen();
80
-		$screen         = $current_screen ? $current_screen->post_type : 'entity';
81
-		add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' );
82
-
83
-	}
84
-
85
-	/**
86
-	 * Render the metabox html.
87
-	 *
88
-	 * @since 3.1.0
89
-	 *
90
-	 */
91
-	public function html() {
92
-
93
-		// Loop over the fields.
94
-		foreach ( $this->fields as $field ) {
95
-
96
-			// load data from DB (values will be available in $field->data).
97
-			$field->get_data();
98
-
99
-			// print field HTML (nonce included).
100
-			echo $field->html();
101
-		}
102
-
103
-	}
104
-
105
-	/**
106
-	 * Read the WL <-> Schema mapping and build the Fields for the entity being edited.
107
-	 *
108
-	 * Note: the first function that calls this method will instantiate the fields.
109
-	 * Why it isn't called from the constructor? Because we need to hook this process as late as possible.
110
-	 *
111
-	 * @param int $id | $term_id The post id or term id.
112
-	 *
113
-	 * @param $type int Post or Term
114
-	 *
115
-	 * @since 3.1.0
116
-	 */
117
-	public function instantiate_fields( $id, $type ) {
118
-
119
-		$this->log->trace( "Instantiating fields for entity post $id..." );
120
-
121
-		// This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering.
122
-		if ( isset( $this->fields ) ) {
123
-			return;
124
-		}
125
-		if ( $type === Object_Type_Enum::POST ) {
126
-			$entity_type = wl_entity_taxonomy_get_custom_fields( $id );
127
-		} else if ( $type === Object_Type_Enum::TERM ) {
128
-			$term_entity_types = get_term_meta( $id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
129
-			$term_entity_types = array_map( function ( $term ) {
130
-				return get_term_by(
131
-					'slug',
132
-					$term,
133
-					Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME
134
-				);
135
-			}, $term_entity_types );
136
-			$entity_type       = wl_get_custom_fields_by_entity_type( $term_entity_types );
137
-		}
138
-		if ( isset( $entity_type ) ) {
139
-
140
-			/*
21
+    /**
22
+     * The metabox custom fields for the current {@link WP_Post}.
23
+     *
24
+     * @since  3.1.0
25
+     * @access public
26
+     * @var array $fields The metabox custom fields.
27
+     */
28
+    public $fields;
29
+
30
+    /**
31
+     * A {@link Wordlift_Log_Service} instance.
32
+     *
33
+     * @since 3.15.4
34
+     *
35
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
36
+     */
37
+    private $log;
38
+
39
+    /**
40
+     * WL_Metabox constructor.
41
+     *
42
+     * @since 3.1.0
43
+     */
44
+    public function __construct() {
45
+
46
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
47
+    }
48
+
49
+    /**
50
+     * Add a callback to print the metabox in page.
51
+     * Wordpress will fire the $this->html() callback at the right time.
52
+     */
53
+    public function add_main_metabox() {
54
+
55
+        // Build the fields we need to print.
56
+        $this->instantiate_fields( get_the_ID(), Object_Type_Enum::POST );
57
+
58
+        // Bailout if there are no actual fields, we do not need a metabox in that case.
59
+        if ( empty( $this->fields ) ) {
60
+            return;
61
+        }
62
+
63
+        // Add main metabox (will print also the inner fields).
64
+        $id    = uniqid( 'wl-metabox-' );
65
+        $title = get_the_title() . ' ' . __( 'properties', 'wordlift' );
66
+
67
+        // WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit.
68
+        foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) {
69
+            add_meta_box( $id, $title, array(
70
+                $this,
71
+                'html',
72
+            ), $screen, 'normal', 'high' );
73
+        }
74
+
75
+        // Add filter to change the metabox CSS class.
76
+        //
77
+        // @since 3.20.0 Since we support post types other than `entity` for entities, we need to set the `screen`
78
+        //  dynamically according to the `get_current_screen()` function.
79
+        $current_screen = get_current_screen();
80
+        $screen         = $current_screen ? $current_screen->post_type : 'entity';
81
+        add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' );
82
+
83
+    }
84
+
85
+    /**
86
+     * Render the metabox html.
87
+     *
88
+     * @since 3.1.0
89
+     *
90
+     */
91
+    public function html() {
92
+
93
+        // Loop over the fields.
94
+        foreach ( $this->fields as $field ) {
95
+
96
+            // load data from DB (values will be available in $field->data).
97
+            $field->get_data();
98
+
99
+            // print field HTML (nonce included).
100
+            echo $field->html();
101
+        }
102
+
103
+    }
104
+
105
+    /**
106
+     * Read the WL <-> Schema mapping and build the Fields for the entity being edited.
107
+     *
108
+     * Note: the first function that calls this method will instantiate the fields.
109
+     * Why it isn't called from the constructor? Because we need to hook this process as late as possible.
110
+     *
111
+     * @param int $id | $term_id The post id or term id.
112
+     *
113
+     * @param $type int Post or Term
114
+     *
115
+     * @since 3.1.0
116
+     */
117
+    public function instantiate_fields( $id, $type ) {
118
+
119
+        $this->log->trace( "Instantiating fields for entity post $id..." );
120
+
121
+        // This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering.
122
+        if ( isset( $this->fields ) ) {
123
+            return;
124
+        }
125
+        if ( $type === Object_Type_Enum::POST ) {
126
+            $entity_type = wl_entity_taxonomy_get_custom_fields( $id );
127
+        } else if ( $type === Object_Type_Enum::TERM ) {
128
+            $term_entity_types = get_term_meta( $id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
129
+            $term_entity_types = array_map( function ( $term ) {
130
+                return get_term_by(
131
+                    'slug',
132
+                    $term,
133
+                    Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME
134
+                );
135
+            }, $term_entity_types );
136
+            $entity_type       = wl_get_custom_fields_by_entity_type( $term_entity_types );
137
+        }
138
+        if ( isset( $entity_type ) ) {
139
+
140
+            /*
141 141
 			 * Might not have any relevant meta box field, for example for articles,
142 142
 			 * therefor make sure fields are at least an empty array to help the considered
143 143
 			 * in other functions using it.
144 144
 			 */
145
-			$this->fields = array();
145
+            $this->fields = array();
146 146
 
147
-			/**
148
-			 * In some special case, properties must be grouped in one field (e.g. coordinates) or dealed with custom methods.
149
-			 * We must divide fields in two groups:
150
-			 * - simple: accept values for one property
151
-			 * - grouped: accept values for more properties, or for one property that needs a specific metabox.
152
-			 */
153
-			$metaboxes         = $this->group_properties_by_input_field( $entity_type );
154
-			$simple_metaboxes  = $metaboxes[0];
155
-			$grouped_metaboxes = $metaboxes[1];
156
-
157
-			// Loop over simple entity properties.
158
-			foreach ( $simple_metaboxes as $key => $property ) {
159
-
160
-				// Info passed to the metabox.
161
-				$info         = array();
162
-				$info[ $key ] = $property;
163
-
164
-				// Build the requested field as WL_Metabox_Field_ object.
165
-				$this->add_field( $info, false, $type, $id );
166
-
167
-			}
168
-
169
-			// Loop over grouped properties.
170
-			foreach ( $grouped_metaboxes as $key => $property ) {
171
-
172
-				// Info passed to the metabox.
173
-				$info         = array();
174
-				$info[ $key ] = $property;
147
+            /**
148
+             * In some special case, properties must be grouped in one field (e.g. coordinates) or dealed with custom methods.
149
+             * We must divide fields in two groups:
150
+             * - simple: accept values for one property
151
+             * - grouped: accept values for more properties, or for one property that needs a specific metabox.
152
+             */
153
+            $metaboxes         = $this->group_properties_by_input_field( $entity_type );
154
+            $simple_metaboxes  = $metaboxes[0];
155
+            $grouped_metaboxes = $metaboxes[1];
175 156
 
176
-				// Build the requested field group as WL_Metabox_Field_ object.
177
-				$this->add_field( $info, true, $type, $id );
157
+            // Loop over simple entity properties.
158
+            foreach ( $simple_metaboxes as $key => $property ) {
178 159
 
179
-			}
180
-		}
160
+                // Info passed to the metabox.
161
+                $info         = array();
162
+                $info[ $key ] = $property;
181 163
 
182
-	}
164
+                // Build the requested field as WL_Metabox_Field_ object.
165
+                $this->add_field( $info, false, $type, $id );
183 166
 
184
-	/**
185
-	 * Separates metaboxes in simple and grouped.
186
-	 *
187
-	 * @param array $custom_fields Information on the entity type.
188
-	 *
189
-	 * @return array
190
-	 */
191
-	public function group_properties_by_input_field( $custom_fields ) {
167
+            }
192 168
 
193
-		$simple_properties  = array();
194
-		$grouped_properties = array();
169
+            // Loop over grouped properties.
170
+            foreach ( $grouped_metaboxes as $key => $property ) {
195 171
 
196
-		// Loop over possible entity properties.
197
-		foreach ( $custom_fields as $key => $property ) {
198
-
199
-			// Check presence of predicate and type.
200
-			if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) {
201
-
202
-				// Check if input_field is defined.
203
-				if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) {
204
-
205
-					$grouped_key = $property['input_field'];
206
-
207
-					// Update list of grouped properties.
208
-					$grouped_properties[ $grouped_key ][ $key ] = $property;
209
-
210
-				} else {
211
-
212
-					// input_field not defined, add simple metabox.
213
-					$simple_properties[ $key ] = $property;
214
-				}
215
-			}
216
-		}
172
+                // Info passed to the metabox.
173
+                $info         = array();
174
+                $info[ $key ] = $property;
217 175
 
218
-		return array( $simple_properties, $grouped_properties );
219
-	}
176
+                // Build the requested field group as WL_Metabox_Field_ object.
177
+                $this->add_field( $info, true, $type, $id );
220 178
 
221
-	/**
222
-	 * Add a Field to the current Metabox, based on the description of the Field.
223
-	 * This method is a rude factory for Field objects.
224
-	 *
225
-	 * @param array $args The field's information.
226
-	 * @param bool $grouped Flag to distinguish between simple and grouped fields.
227
-	 * @param int $type Post or Term, based on the correct decorator would be selected.
228
-	 * @param int $id Identifier for the type.
229
-	 */
230
-	public function add_field( $args, $grouped, $type, $id ) {
179
+            }
180
+        }
181
+
182
+    }
231 183
 
232
-		if ( $grouped ) {
233
-
234
-			// Special fields (sameas, coordinates, etc.).
235
-			//
236
-			// Build Field with a custom class (e.g. WL_Metabox_Field_date).
237
-			$field_class = 'Wl_Metabox_Field_' . key( $args );
238
-
239
-		} else {
240
-
241
-			// Simple fields (string, uri, boolean, etc.).
242
-			//
243
-			// Which field? We want to use the class that is specific for the field.
244
-			$meta      = key( $args );
245
-			$this_meta = $args[ $meta ];
246
-
247
-			// If the field declares what metabox it wants, use that one.
248
-			if ( isset( $this_meta['metabox']['class'] ) ) {
249
-
250
-				$field_class = $this_meta['metabox']['class'];
251
-
252
-			} elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) {
253
-
254
-				// TODO: all fields should explicitly declare the required WL_Metabox.
255
-				// When they will remove this.
256
-				//
257
-				// Use default Wl_Metabox_Field (manages strings).
258
-				$field_class = 'Wl_Metabox_Field';
259
-
260
-			} else {
261
-
262
-				// TODO: all fields should explicitly declare the required WL_Metabox.
263
-				// When they will remove this.
264
-				//
265
-				// Build Field with a custom class (e.g. Wl_Metabox_Field_date).
266
-				$field_class = 'Wl_Metabox_Field_' . $this_meta['type'];
267
-
268
-			}
269
-
270
-		}
271
-		/**
272
-		 * @since 3.31.6
273
-		 * Add namespace to initialize class.
274
-		 */
275
-		/**
276
-		 * @since 3.31.6
277
-		 * Add namespace to initialize class.
278
-		 */
279
-		if ( substr( $field_class, 0, 1 ) !== '\\' ) {
280
-			$field_class = 'Wordlift\Metabox\Field\\' . $field_class;
281
-			// End if().
282
-		}
283
-
284
-		if ( class_exists( $field_class ) ) {
285
-			// Get decorator and use it as wrapper for save_data and get_data methods.
286
-			$instance = new $field_class( $args, $id, $type );
287
-			// Call apropriate constructor (e.g. Wl_Metabox_Field... ).
288
-			$this->fields[] = $instance;
289
-		}
290
-
291
-	}
292
-
293
-	/**
294
-	 * Save the form data for the specified entity {@link WP_Post}'s id.
295
-	 *
296
-	 * @param int $id The entity's {@link WP_Post}'s id.
297
-	 *
298
-	 * @param $type int Post or term
299
-	 *
300
-	 * @since 3.5.4
301
-	 */
302
-	public function save_form_data( $id, $type ) {
303
-
304
-		$this->log->trace( "Saving form data for entity post $id..." );
305
-
306
-		// Build Field objects.
307
-		$this->instantiate_fields( $id, $type );
308
-
309
-		// Check if WL metabox form was posted.
310
-		if ( ! isset( $_POST['wl_metaboxes'] ) ) {
311
-			$this->log->debug( "`wl_metaboxes`, skipping..." );
312
-
313
-			return;
314
-		}
315
-
316
-		$posted_data = $_POST['wl_metaboxes'];
317
-
318
-		foreach ( $this->fields as $field ) {
319
-
320
-			// Verify nonce.
321
-			$valid_nonce = $field->verify_nonce();
322
-
323
-			if ( $valid_nonce ) {
324
-				$field_name = $field->meta_name;
325
-				// Each Filed only deals with its values.
326
-				if ( isset( $posted_data[ $field_name ] ) ) {
327
-
328
-					$values = $posted_data[ $field_name ];
329
-					if ( ! is_array( $values ) ) {
330
-						$values = array( $values );
331
-					}
332
-					// Save data permanently
333
-					$field->save_data( $values );
334
-				}
335
-			}
336
-		}
337
-
338
-		/**
339
-		 * Filter: 'wl_save_form_pre_push_entity' - Allow to hook right
340
-		 * before the triples are pushed to the linked dataset.
341
-		 *
342
-		 * @param int $id The entity id.
343
-		 * @param int $id The post data.
344
-		 *
345
-		 * @since  3.18.2
346
-		 *
347
-		 */
348
-		do_action( 'wl_save_form_pre_push_entity', $id, $_POST );
349
-
350
-		do_action( 'wl_legacy_linked_data__push', $id );
351
-
352
-	}
353
-
354
-	/**
355
-	 * Enqueue scripts and styles.
356
-	 *
357
-	 * @since 3.0.0
358
-	 */
359
-	public function enqueue_scripts_and_styles() {
360
-
361
-		// Use the minified version if PW_DEBUG isn't set.
362
-		$min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : '';
363
-
364
-		// Load the jquery-ui-timepicker-addon library.
365
-		wp_enqueue_style( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.css", array(), '3.0.6' );
366
-		wp_enqueue_script( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true );
367
-
368
-		// Leaflet.
369
-		wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
370
-		wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0' );
371
-
372
-		// Add AJAX autocomplete to facilitate metabox editing.
373
-		wp_enqueue_script( 'wl-entity-metabox-utility', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/admin/js/wl_entity_metabox_utilities.js' );
374
-		wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
375
-				'ajax_url' => admin_url( 'admin-ajax.php' ),
376
-				'action'   => 'entity_by_title',
377
-			)
378
-		);
379
-
380
-	}
184
+    /**
185
+     * Separates metaboxes in simple and grouped.
186
+     *
187
+     * @param array $custom_fields Information on the entity type.
188
+     *
189
+     * @return array
190
+     */
191
+    public function group_properties_by_input_field( $custom_fields ) {
192
+
193
+        $simple_properties  = array();
194
+        $grouped_properties = array();
195
+
196
+        // Loop over possible entity properties.
197
+        foreach ( $custom_fields as $key => $property ) {
198
+
199
+            // Check presence of predicate and type.
200
+            if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) {
201
+
202
+                // Check if input_field is defined.
203
+                if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) {
204
+
205
+                    $grouped_key = $property['input_field'];
206
+
207
+                    // Update list of grouped properties.
208
+                    $grouped_properties[ $grouped_key ][ $key ] = $property;
209
+
210
+                } else {
211
+
212
+                    // input_field not defined, add simple metabox.
213
+                    $simple_properties[ $key ] = $property;
214
+                }
215
+            }
216
+        }
217
+
218
+        return array( $simple_properties, $grouped_properties );
219
+    }
220
+
221
+    /**
222
+     * Add a Field to the current Metabox, based on the description of the Field.
223
+     * This method is a rude factory for Field objects.
224
+     *
225
+     * @param array $args The field's information.
226
+     * @param bool $grouped Flag to distinguish between simple and grouped fields.
227
+     * @param int $type Post or Term, based on the correct decorator would be selected.
228
+     * @param int $id Identifier for the type.
229
+     */
230
+    public function add_field( $args, $grouped, $type, $id ) {
231
+
232
+        if ( $grouped ) {
233
+
234
+            // Special fields (sameas, coordinates, etc.).
235
+            //
236
+            // Build Field with a custom class (e.g. WL_Metabox_Field_date).
237
+            $field_class = 'Wl_Metabox_Field_' . key( $args );
238
+
239
+        } else {
240
+
241
+            // Simple fields (string, uri, boolean, etc.).
242
+            //
243
+            // Which field? We want to use the class that is specific for the field.
244
+            $meta      = key( $args );
245
+            $this_meta = $args[ $meta ];
246
+
247
+            // If the field declares what metabox it wants, use that one.
248
+            if ( isset( $this_meta['metabox']['class'] ) ) {
249
+
250
+                $field_class = $this_meta['metabox']['class'];
251
+
252
+            } elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) {
253
+
254
+                // TODO: all fields should explicitly declare the required WL_Metabox.
255
+                // When they will remove this.
256
+                //
257
+                // Use default Wl_Metabox_Field (manages strings).
258
+                $field_class = 'Wl_Metabox_Field';
259
+
260
+            } else {
261
+
262
+                // TODO: all fields should explicitly declare the required WL_Metabox.
263
+                // When they will remove this.
264
+                //
265
+                // Build Field with a custom class (e.g. Wl_Metabox_Field_date).
266
+                $field_class = 'Wl_Metabox_Field_' . $this_meta['type'];
267
+
268
+            }
269
+
270
+        }
271
+        /**
272
+         * @since 3.31.6
273
+         * Add namespace to initialize class.
274
+         */
275
+        /**
276
+         * @since 3.31.6
277
+         * Add namespace to initialize class.
278
+         */
279
+        if ( substr( $field_class, 0, 1 ) !== '\\' ) {
280
+            $field_class = 'Wordlift\Metabox\Field\\' . $field_class;
281
+            // End if().
282
+        }
283
+
284
+        if ( class_exists( $field_class ) ) {
285
+            // Get decorator and use it as wrapper for save_data and get_data methods.
286
+            $instance = new $field_class( $args, $id, $type );
287
+            // Call apropriate constructor (e.g. Wl_Metabox_Field... ).
288
+            $this->fields[] = $instance;
289
+        }
290
+
291
+    }
292
+
293
+    /**
294
+     * Save the form data for the specified entity {@link WP_Post}'s id.
295
+     *
296
+     * @param int $id The entity's {@link WP_Post}'s id.
297
+     *
298
+     * @param $type int Post or term
299
+     *
300
+     * @since 3.5.4
301
+     */
302
+    public function save_form_data( $id, $type ) {
303
+
304
+        $this->log->trace( "Saving form data for entity post $id..." );
305
+
306
+        // Build Field objects.
307
+        $this->instantiate_fields( $id, $type );
308
+
309
+        // Check if WL metabox form was posted.
310
+        if ( ! isset( $_POST['wl_metaboxes'] ) ) {
311
+            $this->log->debug( "`wl_metaboxes`, skipping..." );
312
+
313
+            return;
314
+        }
315
+
316
+        $posted_data = $_POST['wl_metaboxes'];
317
+
318
+        foreach ( $this->fields as $field ) {
319
+
320
+            // Verify nonce.
321
+            $valid_nonce = $field->verify_nonce();
322
+
323
+            if ( $valid_nonce ) {
324
+                $field_name = $field->meta_name;
325
+                // Each Filed only deals with its values.
326
+                if ( isset( $posted_data[ $field_name ] ) ) {
327
+
328
+                    $values = $posted_data[ $field_name ];
329
+                    if ( ! is_array( $values ) ) {
330
+                        $values = array( $values );
331
+                    }
332
+                    // Save data permanently
333
+                    $field->save_data( $values );
334
+                }
335
+            }
336
+        }
337
+
338
+        /**
339
+         * Filter: 'wl_save_form_pre_push_entity' - Allow to hook right
340
+         * before the triples are pushed to the linked dataset.
341
+         *
342
+         * @param int $id The entity id.
343
+         * @param int $id The post data.
344
+         *
345
+         * @since  3.18.2
346
+         *
347
+         */
348
+        do_action( 'wl_save_form_pre_push_entity', $id, $_POST );
349
+
350
+        do_action( 'wl_legacy_linked_data__push', $id );
351
+
352
+    }
353
+
354
+    /**
355
+     * Enqueue scripts and styles.
356
+     *
357
+     * @since 3.0.0
358
+     */
359
+    public function enqueue_scripts_and_styles() {
360
+
361
+        // Use the minified version if PW_DEBUG isn't set.
362
+        $min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : '';
363
+
364
+        // Load the jquery-ui-timepicker-addon library.
365
+        wp_enqueue_style( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.css", array(), '3.0.6' );
366
+        wp_enqueue_script( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true );
367
+
368
+        // Leaflet.
369
+        wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
370
+        wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0' );
371
+
372
+        // Add AJAX autocomplete to facilitate metabox editing.
373
+        wp_enqueue_script( 'wl-entity-metabox-utility', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/admin/js/wl_entity_metabox_utilities.js' );
374
+        wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
375
+                'ajax_url' => admin_url( 'admin-ajax.php' ),
376
+                'action'   => 'entity_by_title',
377
+            )
378
+        );
379
+
380
+    }
381 381
 }
Please login to merge, or discard this patch.
Spacing   +69 added lines, -69 removed lines patch added patch discarded remove patch
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
 	 */
44 44
 	public function __construct() {
45 45
 
46
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
46
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
47 47
 	}
48 48
 
49 49
 	/**
@@ -53,23 +53,23 @@  discard block
 block discarded – undo
53 53
 	public function add_main_metabox() {
54 54
 
55 55
 		// Build the fields we need to print.
56
-		$this->instantiate_fields( get_the_ID(), Object_Type_Enum::POST );
56
+		$this->instantiate_fields(get_the_ID(), Object_Type_Enum::POST);
57 57
 
58 58
 		// Bailout if there are no actual fields, we do not need a metabox in that case.
59
-		if ( empty( $this->fields ) ) {
59
+		if (empty($this->fields)) {
60 60
 			return;
61 61
 		}
62 62
 
63 63
 		// Add main metabox (will print also the inner fields).
64
-		$id    = uniqid( 'wl-metabox-' );
65
-		$title = get_the_title() . ' ' . __( 'properties', 'wordlift' );
64
+		$id    = uniqid('wl-metabox-');
65
+		$title = get_the_title().' '.__('properties', 'wordlift');
66 66
 
67 67
 		// WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit.
68
-		foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) {
69
-			add_meta_box( $id, $title, array(
68
+		foreach (Wordlift_Entity_Service::valid_entity_post_types() as $screen) {
69
+			add_meta_box($id, $title, array(
70 70
 				$this,
71 71
 				'html',
72
-			), $screen, 'normal', 'high' );
72
+			), $screen, 'normal', 'high');
73 73
 		}
74 74
 
75 75
 		// Add filter to change the metabox CSS class.
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
 		//  dynamically according to the `get_current_screen()` function.
79 79
 		$current_screen = get_current_screen();
80 80
 		$screen         = $current_screen ? $current_screen->post_type : 'entity';
81
-		add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' );
81
+		add_filter("postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class');
82 82
 
83 83
 	}
84 84
 
@@ -91,7 +91,7 @@  discard block
 block discarded – undo
91 91
 	public function html() {
92 92
 
93 93
 		// Loop over the fields.
94
-		foreach ( $this->fields as $field ) {
94
+		foreach ($this->fields as $field) {
95 95
 
96 96
 			// load data from DB (values will be available in $field->data).
97 97
 			$field->get_data();
@@ -114,28 +114,28 @@  discard block
 block discarded – undo
114 114
 	 *
115 115
 	 * @since 3.1.0
116 116
 	 */
117
-	public function instantiate_fields( $id, $type ) {
117
+	public function instantiate_fields($id, $type) {
118 118
 
119
-		$this->log->trace( "Instantiating fields for entity post $id..." );
119
+		$this->log->trace("Instantiating fields for entity post $id...");
120 120
 
121 121
 		// This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering.
122
-		if ( isset( $this->fields ) ) {
122
+		if (isset($this->fields)) {
123 123
 			return;
124 124
 		}
125
-		if ( $type === Object_Type_Enum::POST ) {
126
-			$entity_type = wl_entity_taxonomy_get_custom_fields( $id );
127
-		} else if ( $type === Object_Type_Enum::TERM ) {
128
-			$term_entity_types = get_term_meta( $id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
129
-			$term_entity_types = array_map( function ( $term ) {
125
+		if ($type === Object_Type_Enum::POST) {
126
+			$entity_type = wl_entity_taxonomy_get_custom_fields($id);
127
+		} else if ($type === Object_Type_Enum::TERM) {
128
+			$term_entity_types = get_term_meta($id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
129
+			$term_entity_types = array_map(function($term) {
130 130
 				return get_term_by(
131 131
 					'slug',
132 132
 					$term,
133 133
 					Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME
134 134
 				);
135
-			}, $term_entity_types );
136
-			$entity_type       = wl_get_custom_fields_by_entity_type( $term_entity_types );
135
+			}, $term_entity_types);
136
+			$entity_type = wl_get_custom_fields_by_entity_type($term_entity_types);
137 137
 		}
138
-		if ( isset( $entity_type ) ) {
138
+		if (isset($entity_type)) {
139 139
 
140 140
 			/*
141 141
 			 * Might not have any relevant meta box field, for example for articles,
@@ -150,31 +150,31 @@  discard block
 block discarded – undo
150 150
 			 * - simple: accept values for one property
151 151
 			 * - grouped: accept values for more properties, or for one property that needs a specific metabox.
152 152
 			 */
153
-			$metaboxes         = $this->group_properties_by_input_field( $entity_type );
153
+			$metaboxes         = $this->group_properties_by_input_field($entity_type);
154 154
 			$simple_metaboxes  = $metaboxes[0];
155 155
 			$grouped_metaboxes = $metaboxes[1];
156 156
 
157 157
 			// Loop over simple entity properties.
158
-			foreach ( $simple_metaboxes as $key => $property ) {
158
+			foreach ($simple_metaboxes as $key => $property) {
159 159
 
160 160
 				// Info passed to the metabox.
161 161
 				$info         = array();
162
-				$info[ $key ] = $property;
162
+				$info[$key] = $property;
163 163
 
164 164
 				// Build the requested field as WL_Metabox_Field_ object.
165
-				$this->add_field( $info, false, $type, $id );
165
+				$this->add_field($info, false, $type, $id);
166 166
 
167 167
 			}
168 168
 
169 169
 			// Loop over grouped properties.
170
-			foreach ( $grouped_metaboxes as $key => $property ) {
170
+			foreach ($grouped_metaboxes as $key => $property) {
171 171
 
172 172
 				// Info passed to the metabox.
173 173
 				$info         = array();
174
-				$info[ $key ] = $property;
174
+				$info[$key] = $property;
175 175
 
176 176
 				// Build the requested field group as WL_Metabox_Field_ object.
177
-				$this->add_field( $info, true, $type, $id );
177
+				$this->add_field($info, true, $type, $id);
178 178
 
179 179
 			}
180 180
 		}
@@ -188,34 +188,34 @@  discard block
 block discarded – undo
188 188
 	 *
189 189
 	 * @return array
190 190
 	 */
191
-	public function group_properties_by_input_field( $custom_fields ) {
191
+	public function group_properties_by_input_field($custom_fields) {
192 192
 
193 193
 		$simple_properties  = array();
194 194
 		$grouped_properties = array();
195 195
 
196 196
 		// Loop over possible entity properties.
197
-		foreach ( $custom_fields as $key => $property ) {
197
+		foreach ($custom_fields as $key => $property) {
198 198
 
199 199
 			// Check presence of predicate and type.
200
-			if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) {
200
+			if (isset($property['predicate']) && isset($property['type'])) {
201 201
 
202 202
 				// Check if input_field is defined.
203
-				if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) {
203
+				if (isset($property['input_field']) && '' !== $property['input_field']) {
204 204
 
205 205
 					$grouped_key = $property['input_field'];
206 206
 
207 207
 					// Update list of grouped properties.
208
-					$grouped_properties[ $grouped_key ][ $key ] = $property;
208
+					$grouped_properties[$grouped_key][$key] = $property;
209 209
 
210 210
 				} else {
211 211
 
212 212
 					// input_field not defined, add simple metabox.
213
-					$simple_properties[ $key ] = $property;
213
+					$simple_properties[$key] = $property;
214 214
 				}
215 215
 			}
216 216
 		}
217 217
 
218
-		return array( $simple_properties, $grouped_properties );
218
+		return array($simple_properties, $grouped_properties);
219 219
 	}
220 220
 
221 221
 	/**
@@ -227,29 +227,29 @@  discard block
 block discarded – undo
227 227
 	 * @param int $type Post or Term, based on the correct decorator would be selected.
228 228
 	 * @param int $id Identifier for the type.
229 229
 	 */
230
-	public function add_field( $args, $grouped, $type, $id ) {
230
+	public function add_field($args, $grouped, $type, $id) {
231 231
 
232
-		if ( $grouped ) {
232
+		if ($grouped) {
233 233
 
234 234
 			// Special fields (sameas, coordinates, etc.).
235 235
 			//
236 236
 			// Build Field with a custom class (e.g. WL_Metabox_Field_date).
237
-			$field_class = 'Wl_Metabox_Field_' . key( $args );
237
+			$field_class = 'Wl_Metabox_Field_'.key($args);
238 238
 
239 239
 		} else {
240 240
 
241 241
 			// Simple fields (string, uri, boolean, etc.).
242 242
 			//
243 243
 			// Which field? We want to use the class that is specific for the field.
244
-			$meta      = key( $args );
245
-			$this_meta = $args[ $meta ];
244
+			$meta      = key($args);
245
+			$this_meta = $args[$meta];
246 246
 
247 247
 			// If the field declares what metabox it wants, use that one.
248
-			if ( isset( $this_meta['metabox']['class'] ) ) {
248
+			if (isset($this_meta['metabox']['class'])) {
249 249
 
250 250
 				$field_class = $this_meta['metabox']['class'];
251 251
 
252
-			} elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) {
252
+			} elseif ( ! isset($this_meta['type']) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type']) {
253 253
 
254 254
 				// TODO: all fields should explicitly declare the required WL_Metabox.
255 255
 				// When they will remove this.
@@ -263,7 +263,7 @@  discard block
 block discarded – undo
263 263
 				// When they will remove this.
264 264
 				//
265 265
 				// Build Field with a custom class (e.g. Wl_Metabox_Field_date).
266
-				$field_class = 'Wl_Metabox_Field_' . $this_meta['type'];
266
+				$field_class = 'Wl_Metabox_Field_'.$this_meta['type'];
267 267
 
268 268
 			}
269 269
 
@@ -276,14 +276,14 @@  discard block
 block discarded – undo
276 276
 		 * @since 3.31.6
277 277
 		 * Add namespace to initialize class.
278 278
 		 */
279
-		if ( substr( $field_class, 0, 1 ) !== '\\' ) {
280
-			$field_class = 'Wordlift\Metabox\Field\\' . $field_class;
279
+		if (substr($field_class, 0, 1) !== '\\') {
280
+			$field_class = 'Wordlift\Metabox\Field\\'.$field_class;
281 281
 			// End if().
282 282
 		}
283 283
 
284
-		if ( class_exists( $field_class ) ) {
284
+		if (class_exists($field_class)) {
285 285
 			// Get decorator and use it as wrapper for save_data and get_data methods.
286
-			$instance = new $field_class( $args, $id, $type );
286
+			$instance = new $field_class($args, $id, $type);
287 287
 			// Call apropriate constructor (e.g. Wl_Metabox_Field... ).
288 288
 			$this->fields[] = $instance;
289 289
 		}
@@ -299,38 +299,38 @@  discard block
 block discarded – undo
299 299
 	 *
300 300
 	 * @since 3.5.4
301 301
 	 */
302
-	public function save_form_data( $id, $type ) {
302
+	public function save_form_data($id, $type) {
303 303
 
304
-		$this->log->trace( "Saving form data for entity post $id..." );
304
+		$this->log->trace("Saving form data for entity post $id...");
305 305
 
306 306
 		// Build Field objects.
307
-		$this->instantiate_fields( $id, $type );
307
+		$this->instantiate_fields($id, $type);
308 308
 
309 309
 		// Check if WL metabox form was posted.
310
-		if ( ! isset( $_POST['wl_metaboxes'] ) ) {
311
-			$this->log->debug( "`wl_metaboxes`, skipping..." );
310
+		if ( ! isset($_POST['wl_metaboxes'])) {
311
+			$this->log->debug("`wl_metaboxes`, skipping...");
312 312
 
313 313
 			return;
314 314
 		}
315 315
 
316 316
 		$posted_data = $_POST['wl_metaboxes'];
317 317
 
318
-		foreach ( $this->fields as $field ) {
318
+		foreach ($this->fields as $field) {
319 319
 
320 320
 			// Verify nonce.
321 321
 			$valid_nonce = $field->verify_nonce();
322 322
 
323
-			if ( $valid_nonce ) {
323
+			if ($valid_nonce) {
324 324
 				$field_name = $field->meta_name;
325 325
 				// Each Filed only deals with its values.
326
-				if ( isset( $posted_data[ $field_name ] ) ) {
326
+				if (isset($posted_data[$field_name])) {
327 327
 
328
-					$values = $posted_data[ $field_name ];
329
-					if ( ! is_array( $values ) ) {
330
-						$values = array( $values );
328
+					$values = $posted_data[$field_name];
329
+					if ( ! is_array($values)) {
330
+						$values = array($values);
331 331
 					}
332 332
 					// Save data permanently
333
-					$field->save_data( $values );
333
+					$field->save_data($values);
334 334
 				}
335 335
 			}
336 336
 		}
@@ -345,9 +345,9 @@  discard block
 block discarded – undo
345 345
 		 * @since  3.18.2
346 346
 		 *
347 347
 		 */
348
-		do_action( 'wl_save_form_pre_push_entity', $id, $_POST );
348
+		do_action('wl_save_form_pre_push_entity', $id, $_POST);
349 349
 
350
-		do_action( 'wl_legacy_linked_data__push', $id );
350
+		do_action('wl_legacy_linked_data__push', $id);
351 351
 
352 352
 	}
353 353
 
@@ -359,20 +359,20 @@  discard block
 block discarded – undo
359 359
 	public function enqueue_scripts_and_styles() {
360 360
 
361 361
 		// Use the minified version if PW_DEBUG isn't set.
362
-		$min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : '';
362
+		$min = ! defined('WP_DEBUG') || ! WP_DEBUG ? '.min' : '';
363 363
 
364 364
 		// Load the jquery-ui-timepicker-addon library.
365
-		wp_enqueue_style( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.css", array(), '3.0.6' );
366
-		wp_enqueue_script( 'wl-flatpickr', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . "/admin/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true );
365
+		wp_enqueue_style('wl-flatpickr', dirname(dirname(plugin_dir_url(__FILE__)))."/admin/js/flatpickr/flatpickr$min.css", array(), '3.0.6');
366
+		wp_enqueue_script('wl-flatpickr', dirname(dirname(plugin_dir_url(__FILE__)))."/admin/js/flatpickr/flatpickr$min.js", array('jquery'), '3.0.6', true);
367 367
 
368 368
 		// Leaflet.
369
-		wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
370
-		wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0' );
369
+		wp_enqueue_style('wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0');
370
+		wp_enqueue_script('wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0');
371 371
 
372 372
 		// Add AJAX autocomplete to facilitate metabox editing.
373
-		wp_enqueue_script( 'wl-entity-metabox-utility', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/admin/js/wl_entity_metabox_utilities.js' );
374
-		wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
375
-				'ajax_url' => admin_url( 'admin-ajax.php' ),
373
+		wp_enqueue_script('wl-entity-metabox-utility', dirname(dirname(plugin_dir_url(__FILE__))).'/admin/js/wl_entity_metabox_utilities.js');
374
+		wp_localize_script('wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
375
+				'ajax_url' => admin_url('admin-ajax.php'),
376 376
 				'action'   => 'entity_by_title',
377 377
 			)
378 378
 		);
Please login to merge, or discard this patch.