Completed
Pull Request — develop (#1272)
by
unknown
02:56
created
src/wordlift/mappings/class-mappings-rest-controller.php 2 patches
Indentation   +458 added lines, -458 removed lines patch added patch discarded remove patch
@@ -13,135 +13,135 @@  discard block
 block discarded – undo
13 13
  * @subpackage Wordlift/includes/sync-mappings
14 14
  */
15 15
 class Mappings_REST_Controller {
16
-	// Namespace for CRUD mappings.
17
-	const MAPPINGS_NAMESPACE = '/mappings';
18
-
19
-	/**
20
-	 * Registers route on rest api initialisation.
21
-	 */
22
-	public static function register_routes() {
23
-
24
-		add_action( 'rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback' );
25
-
26
-	}
27
-
28
-	/**
29
-	 * Get a single mapping item by its mapping_id
30
-	 *
31
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
32
-	 *
33
-	 * @return array
34
-	 */
35
-	public static function get_mapping_item( $request ) {
36
-		$dbo             = new Mappings_DBO();
37
-		$mapping_id      = $request['id'];
38
-		$mapping_id_data = array();
39
-		$rule_groups     = $dbo->get_rule_groups_by_mapping( $mapping_id );
40
-		$properties      = $dbo->get_properties( $mapping_id );
41
-		$mapping_row     = $dbo->get_mapping_item_data( $mapping_id );
42
-
43
-		$mapping_id_data['mapping_id']      = $mapping_id;
44
-		$mapping_id_data['property_list']   = $properties;
45
-		$mapping_id_data['rule_group_list'] = $rule_groups;
46
-		$mapping_id_data['mapping_title']   = $mapping_row['mapping_title'];
47
-
48
-		return $mapping_id_data;
49
-	}
50
-
51
-	/**
52
-	 * Register route call back function, called when rest api gets initialised
53
-	 *
54
-	 * @return void
55
-	 */
56
-	public static function register_route_callback() {
57
-		register_rest_route(
58
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
59
-			'/mappings',
60
-			array(
61
-				'methods'             => WP_REST_Server::CREATABLE,
62
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::insert_or_update_mapping_item',
63
-				'permission_callback' => function () {
64
-					return current_user_can( 'manage_options' );
65
-				},
66
-			)
67
-		);
68
-		// Get list of mapping items.
69
-		register_rest_route(
70
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
71
-			'/mappings',
72
-			array(
73
-				'methods'             => WP_REST_Server::READABLE,
74
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::list_mapping_items',
75
-				'permission_callback' => function () {
76
-					return current_user_can( 'manage_options' );
77
-				},
78
-			)
79
-		);
80
-
81
-		// Delete mapping items by id.
82
-		register_rest_route(
83
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
84
-			'mappings',
85
-			array(
86
-				'methods'             => WP_REST_Server::DELETABLE,
87
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::delete_mapping_items',
88
-				'permission_callback' => function () {
89
-					return current_user_can( 'manage_options' );
90
-				},
91
-			)
92
-		);
93
-
94
-		// Get single mapping item route.
95
-		register_rest_route(
96
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
97
-			'mappings/(?P<id>\d+)',
98
-			array(
99
-				'methods'             => WP_REST_Server::READABLE,
100
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_mapping_item',
101
-				'permission_callback' => function () {
102
-					return current_user_can( 'manage_options' );
103
-				},
104
-			)
105
-		);
106
-
107
-		// Update mapping items.
108
-		register_rest_route(
109
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
110
-			'mappings',
111
-			array(
112
-				'methods'             => WP_REST_Server::EDITABLE,
113
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::update_mapping_items',
114
-				'permission_callback' => function () {
115
-					return current_user_can( 'manage_options' );
116
-				},
117
-			)
118
-		);
119
-
120
-		// Clone mapping items.
121
-		register_rest_route(
122
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
123
-			'mappings/clone',
124
-			array(
125
-				'methods'             => WP_REST_Server::CREATABLE,
126
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::clone_mapping_items',
127
-				'permission_callback' => function () {
128
-					return current_user_can( 'manage_options' );
129
-				},
130
-			)
131
-		);
132
-
133
-		// Register rest endpoint to get the terms.
134
-		register_rest_route(
135
-			WL_REST_ROUTE_DEFAULT_NAMESPACE,
136
-			'mappings/get_terms',
137
-			array(
138
-				'methods'             => WP_REST_Server::CREATABLE,
139
-				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_terms_for_the_posted_taxonomy',
140
-				'permission_callback' => function () {
141
-					return current_user_can( 'manage_options' );
142
-				},
143
-			)
144
-		);
16
+    // Namespace for CRUD mappings.
17
+    const MAPPINGS_NAMESPACE = '/mappings';
18
+
19
+    /**
20
+     * Registers route on rest api initialisation.
21
+     */
22
+    public static function register_routes() {
23
+
24
+        add_action( 'rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback' );
25
+
26
+    }
27
+
28
+    /**
29
+     * Get a single mapping item by its mapping_id
30
+     *
31
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
32
+     *
33
+     * @return array
34
+     */
35
+    public static function get_mapping_item( $request ) {
36
+        $dbo             = new Mappings_DBO();
37
+        $mapping_id      = $request['id'];
38
+        $mapping_id_data = array();
39
+        $rule_groups     = $dbo->get_rule_groups_by_mapping( $mapping_id );
40
+        $properties      = $dbo->get_properties( $mapping_id );
41
+        $mapping_row     = $dbo->get_mapping_item_data( $mapping_id );
42
+
43
+        $mapping_id_data['mapping_id']      = $mapping_id;
44
+        $mapping_id_data['property_list']   = $properties;
45
+        $mapping_id_data['rule_group_list'] = $rule_groups;
46
+        $mapping_id_data['mapping_title']   = $mapping_row['mapping_title'];
47
+
48
+        return $mapping_id_data;
49
+    }
50
+
51
+    /**
52
+     * Register route call back function, called when rest api gets initialised
53
+     *
54
+     * @return void
55
+     */
56
+    public static function register_route_callback() {
57
+        register_rest_route(
58
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
59
+            '/mappings',
60
+            array(
61
+                'methods'             => WP_REST_Server::CREATABLE,
62
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::insert_or_update_mapping_item',
63
+                'permission_callback' => function () {
64
+                    return current_user_can( 'manage_options' );
65
+                },
66
+            )
67
+        );
68
+        // Get list of mapping items.
69
+        register_rest_route(
70
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
71
+            '/mappings',
72
+            array(
73
+                'methods'             => WP_REST_Server::READABLE,
74
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::list_mapping_items',
75
+                'permission_callback' => function () {
76
+                    return current_user_can( 'manage_options' );
77
+                },
78
+            )
79
+        );
80
+
81
+        // Delete mapping items by id.
82
+        register_rest_route(
83
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
84
+            'mappings',
85
+            array(
86
+                'methods'             => WP_REST_Server::DELETABLE,
87
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::delete_mapping_items',
88
+                'permission_callback' => function () {
89
+                    return current_user_can( 'manage_options' );
90
+                },
91
+            )
92
+        );
93
+
94
+        // Get single mapping item route.
95
+        register_rest_route(
96
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
97
+            'mappings/(?P<id>\d+)',
98
+            array(
99
+                'methods'             => WP_REST_Server::READABLE,
100
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_mapping_item',
101
+                'permission_callback' => function () {
102
+                    return current_user_can( 'manage_options' );
103
+                },
104
+            )
105
+        );
106
+
107
+        // Update mapping items.
108
+        register_rest_route(
109
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
110
+            'mappings',
111
+            array(
112
+                'methods'             => WP_REST_Server::EDITABLE,
113
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::update_mapping_items',
114
+                'permission_callback' => function () {
115
+                    return current_user_can( 'manage_options' );
116
+                },
117
+            )
118
+        );
119
+
120
+        // Clone mapping items.
121
+        register_rest_route(
122
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
123
+            'mappings/clone',
124
+            array(
125
+                'methods'             => WP_REST_Server::CREATABLE,
126
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::clone_mapping_items',
127
+                'permission_callback' => function () {
128
+                    return current_user_can( 'manage_options' );
129
+                },
130
+            )
131
+        );
132
+
133
+        // Register rest endpoint to get the terms.
134
+        register_rest_route(
135
+            WL_REST_ROUTE_DEFAULT_NAMESPACE,
136
+            'mappings/get_terms',
137
+            array(
138
+                'methods'             => WP_REST_Server::CREATABLE,
139
+                'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_terms_for_the_posted_taxonomy',
140
+                'permission_callback' => function () {
141
+                    return current_user_can( 'manage_options' );
142
+                },
143
+            )
144
+        );
145 145
 
146 146
         // Register rest endpoint to get the terms.
147 147
         register_rest_route(
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
                 },
156 156
             )
157 157
         );
158
-	}
158
+    }
159 159
 
160 160
     /**
161 161
      * Get the taxonomy & terms
@@ -206,334 +206,334 @@  discard block
 block discarded – undo
206 206
         return $taxonomy_terms;
207 207
     }
208 208
 
209
-	/**
210
-	 * Get the terms for the posted taxonomy name.
211
-	 *
212
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
213
-	 *
214
-	 * @return array The array of the terms for the taxonomy.
215
-	 */
216
-	public static function get_terms_for_the_posted_taxonomy( $request ) {
217
-		$post_data = $request->get_params();
218
-		if ( ! array_key_exists( 'taxonomy', $post_data ) ) {
219
-			return array(
220
-				'status'  => 'failure',
221
-				'message' => __( 'Request not valid, must post a taxonomy to get terms', 'wordlift' )
222
-			);
223
-		} else {
224
-			$taxonomy = $post_data['taxonomy'];
225
-			$terms    = get_terms( $taxonomy, array( 'hide_empty' => false, ) );
226
-			if ( is_wp_error( $terms ) ) {
227
-				// Return error response, if the taxonomy is not valid.
228
-				return array(
229
-					'status'  => 'failure',
230
-					'message' => __( 'Request not valid, must post a valid taxonomy', 'wordlift' )
231
-				);
232
-			}
233
-
234
-			return $terms;
235
-		}
236
-	}
237
-
238
-	/**
239
-	 * Clone posted mapping items.
240
-	 *
241
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
242
-	 *
243
-	 * @return array
244
-	 */
245
-	public static function clone_mapping_items( $request ) {
246
-		$dbo           = new Mappings_DBO();
247
-		$post_data     = (array) $request->get_params();
248
-		$mapping_items = (array) $post_data['mapping_items'];
249
-		foreach ( $mapping_items as $mapping_item ) {
250
-			$mapping_id = (int) $mapping_item['mapping_id'];
251
-			// Clone the current mapping item.
252
-			$cloned_mapping_id = $dbo->insert_mapping_item( $mapping_item['mapping_title'] );
253
-			// Clone all the rule groups.
254
-			$rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping( $mapping_id );
255
-			// Clone all the properties.
256
-			$properties_to_be_cloned = $dbo->get_properties( $mapping_id );
257
-			foreach ( $properties_to_be_cloned as $property ) {
258
-				// Assign a new mapping id.
259
-				$property['mapping_id'] = $cloned_mapping_id;
260
-				// Removing this property id, since a new id needed to be created for
261
-				// new property.
262
-				unset( $property['property_id'] );
263
-				$dbo->insert_or_update_property( $property );
264
-			}
265
-			// Loop through the rule groups and insert them in table with the mapping id.
266
-			foreach ( $rule_groups_to_be_cloned as $rule_group ) {
267
-				$cloned_rule_group_id = $dbo->insert_rule_group( $cloned_mapping_id );
268
-				$original_rules       = (array) $rule_group['rules'];
269
-				// Now we need to insert these rules for the cloned rule group id.
270
-				foreach ( $original_rules as $clone_rule ) {
271
-					// We should replace only rule group id in the cloned rules.
272
-					$clone_rule['rule_group_id'] = (int) $cloned_rule_group_id;
273
-					unset( $clone_rule['rule_id'] );
274
-					$dbo->insert_or_update_rule_item( $clone_rule );
275
-				}
276
-			}
277
-		}
278
-
279
-		return array(
280
-			'status'  => 'success',
281
-			'message' => __( 'Successfully cloned mapping items', 'wordlift' ),
282
-		);
283
-	}
284
-
285
-	/**
286
-	 * Update posted mapping items.
287
-	 *
288
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
289
-	 *
290
-	 * @return array
291
-	 */
292
-	public static function update_mapping_items( $request ) {
293
-		$dbo       = new Mappings_DBO();
294
-		$post_data = $request->get_params();
295
-		if ( array_key_exists( 'mapping_items', $post_data ) ) {
296
-			$mapping_items = (array) $post_data['mapping_items'];
297
-			foreach ( $mapping_items as $mapping_item ) {
298
-				$dbo->insert_or_update_mapping_item( $mapping_item );
299
-			}
300
-
301
-			return array(
302
-				'status'  => 'success',
303
-				'message' => __( 'Mapping items successfully updated', 'wordlift' ),
304
-			);
305
-		} else {
306
-			return array(
307
-				'status'  => 'failure',
308
-				'message' => __( 'Unable to update mapping item', 'wordlift' ),
309
-			);
310
-		}
311
-	}
312
-
313
-	/**
314
-	 * Delete mapping items by mapping id
315
-	 *
316
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
317
-	 *
318
-	 * @return array
319
-	 */
320
-	public static function delete_mapping_items( $request ) {
321
-		$dbo       = new Mappings_DBO();
322
-		$post_data = $request->get_params();
323
-		if ( array_key_exists( 'mapping_items', $post_data ) ) {
324
-			$mapping_items = (array) $post_data['mapping_items'];
325
-			foreach ( $mapping_items as $mapping_item ) {
326
-				$dbo->delete_mapping_item( $mapping_item['mapping_id'] );
327
-			}
328
-
329
-			return array(
330
-				'status'  => 'success',
331
-				'message' => __( 'successfully deleted mapping items', 'wordlift' )
332
-			);
333
-		} else {
334
-			return array(
335
-				'status'  => 'failure',
336
-				'message' => __( 'Unable to delete mapping items', 'wordlift' )
337
-			);
338
-		}
339
-	}
340
-
341
-	/**
342
-	 * Get all mapping items
343
-	 *
344
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
345
-	 *
346
-	 * @return array
347
-	 */
348
-	public static function list_mapping_items( $request ) {
349
-		$dbo = new Mappings_DBO();
350
-
351
-		return $dbo->get_mappings();
352
-	}
353
-
354
-	/**
355
-	 * Returns a array of rule ids for the rule group id
356
-	 *
357
-	 * @param Object $dbo Instance of {@link Mappings_DBO } class.
358
-	 * @param int $rule_group_id Primary key of rule group table.
359
-	 *
360
-	 * @return array A list of rule ids.
361
-	 */
362
-	private static function get_rule_ids( $dbo, $rule_group_id ) {
363
-		$rule_rows_in_db = $dbo->get_rules_by_rule_group( $rule_group_id );
364
-		$rule_ids        = array();
365
-		foreach ( $rule_rows_in_db as $rule_row ) {
366
-			array_push( $rule_ids, (int) $rule_row['rule_id'] );
367
-		}
368
-
369
-		return $rule_ids;
370
-	}
371
-
372
-	/**
373
-	 * Insert or update mapping item depends on data
374
-	 *
375
-	 * @param Object $dbo Instance of {@link Mappings_DBO } class.
376
-	 * @param int $rule_group_id Refers to a rule group which this rule belongs to.
377
-	 * @param array $rule_list Array of rule  items.
378
-	 *
379
-	 * @return void
380
-	 */
381
-	private static function save_rules( $dbo, $rule_group_id, $rule_list ) {
382
-		$rule_ids = self::get_rule_ids( $dbo, $rule_group_id );
383
-		foreach ( $rule_list as $rule ) {
384
-			// Some rules may not have rule group id, because they are inserted
385
-			// in ui, so lets add them any way.
386
-			$rule['rule_group_id'] = $rule_group_id;
387
-			$dbo->insert_or_update_rule_item( $rule );
388
-			if ( array_key_exists( 'rule_id', $rule ) ) {
389
-				$index_to_be_removed = array_search(
390
-					(int) $rule['rule_id'],
391
-					$rule_ids,
392
-					true
393
-				);
394
-				if ( false !== $index_to_be_removed ) {
395
-					unset( $rule_ids[ $index_to_be_removed ] );
396
-				}
397
-			}
398
-		}
399
-		foreach ( $rule_ids as $rule_id ) {
400
-			// Delete all the rule ids which are not posted.
401
-			$dbo->delete_rule_item( $rule_id );
402
-		}
403
-	}
404
-
405
-	/**
406
-	 * Insert or update rule group list based on data
407
-	 *
408
-	 * @param Object $dbo Instance of {@link Mappings_DBO } class.
409
-	 * @param int $mapping_id Primary key of mapping table.
410
-	 * @param array $property_list { Array of property items }.
411
-	 *
412
-	 * @return void
413
-	 */
414
-	private static function save_property_list( $dbo, $mapping_id, $property_list ) {
415
-		$properties_needed_to_be_deleted = $dbo->get_properties( $mapping_id );
416
-		$property_ids                    = array();
417
-		foreach ( $properties_needed_to_be_deleted as $property ) {
418
-			array_push( $property_ids, (int) $property['property_id'] );
419
-		}
420
-		foreach ( $property_list as $property ) {
421
-			if ( array_key_exists( 'property_id', $property ) ) {
422
-				// Remove the id from the list of property ids needed to be deleted
423
-				// because it is posted.
424
-				$index_to_be_removed = array_search(
425
-					(int) $property['property_id'],
426
-					$property_ids,
427
-					true
428
-				);
429
-				if ( false !== $index_to_be_removed ) {
430
-					unset( $property_ids[ $index_to_be_removed ] );
431
-				}
432
-			}
433
-			// Add mapping id to property data.
434
-			$property['mapping_id'] = $mapping_id;
435
-			$dbo->insert_or_update_property( $property );
436
-
437
-		}
438
-		// At the end remove all the property ids which are not posted.
439
-		foreach ( $property_ids as $property_id ) {
440
-			$dbo->delete_property( $property_id );
441
-		}
442
-
443
-	}
444
-
445
-	/**
446
-	 * Returns a array of rule group ids for the mapping id
447
-	 *
448
-	 * @param Object $dbo Instance of {@link Mappings_DBO } class.
449
-	 * @param int $mapping_id Primary key of mapping table.
450
-	 *
451
-	 * @return array $rule_group_ids A list of rule group ids.
452
-	 */
453
-	private static function get_rule_group_ids( $dbo, $mapping_id ) {
454
-		$rule_group_rows = $dbo->get_rule_group_list( $mapping_id );
455
-		$rule_group_ids  = array();
456
-		foreach ( $rule_group_rows as $rule_group_row ) {
457
-			array_push( $rule_group_ids, (int) $rule_group_row['rule_group_id'] );
458
-		}
459
-
460
-		return $rule_group_ids;
461
-	}
462
-
463
-	/**
464
-	 * Insert or update rule group list
465
-	 *
466
-	 * @param Object $dbo Instance of {@link Mappings_DBO } class.
467
-	 * @param int $mapping_id Primary key of mapping table.
468
-	 * @param array $rule_group_list { Array of rule group items }.
469
-	 *
470
-	 * @return void
471
-	 */
472
-	private static function save_rule_group_list( $dbo, $mapping_id, $rule_group_list ) {
473
-		// The rule groups not posted should be deleted.
474
-		$rule_group_ids = self::get_rule_group_ids( $dbo, $mapping_id );
475
-		// Loop through rule group list and save the rule group.
476
-		foreach ( $rule_group_list as $rule_group ) {
477
-			if ( array_key_exists( 'rule_group_id', $rule_group ) ) {
478
-				$rule_group_id = $rule_group['rule_group_id'];
479
-			} else {
480
-				// New rule group, should create new rule group id.
481
-				$rule_group_id = $dbo->insert_rule_group( $mapping_id );
482
-			}
483
-			$index_to_be_removed = array_search(
484
-				(int) $rule_group_id,
485
-				$rule_group_ids,
486
-				true
487
-			);
488
-			if ( false !== $index_to_be_removed ) {
489
-				unset( $rule_group_ids[ $index_to_be_removed ] );
490
-			}
491
-			self::save_rules( $dbo, $rule_group_id, $rule_group['rules'] );
492
-		}
493
-
494
-		// Remove all the rule groups which are not posted.
495
-		foreach ( $rule_group_ids as $rule_group_id ) {
496
-			$dbo->delete_rule_group_item( $rule_group_id );
497
-		}
498
-	}
499
-
500
-	/**
501
-	 * Insert or update mapping item depends on data
502
-	 *
503
-	 * @param WP_REST_Request $request {@link WP_REST_Request instance}.
504
-	 *
505
-	 * @return array
506
-	 */
507
-	public static function insert_or_update_mapping_item( $request ) {
508
-		$post_data = $request->get_params() === null ? array() : $request->get_params();
509
-		$dbo       = new Mappings_DBO();
510
-		// check if valid object is posted.
511
-		if ( array_key_exists( 'mapping_title', $post_data ) &&
512
-		     array_key_exists( 'rule_group_list', $post_data ) &&
513
-		     array_key_exists( 'property_list', $post_data ) ) {
514
-			// Do validation, remove all incomplete data.
515
-			$mapping_item = array();
516
-			if ( array_key_exists( 'mapping_id', $post_data ) ) {
517
-				$mapping_item['mapping_id'] = $post_data['mapping_id'];
518
-			}
519
-			$mapping_item['mapping_title'] = $post_data['mapping_title'];
520
-			// lets save the mapping item.
521
-			$mapping_id = $dbo->insert_or_update_mapping_item( $mapping_item );
522
-			self::save_rule_group_list( $dbo, $mapping_id, $post_data['rule_group_list'] );
523
-			self::save_property_list( $dbo, $mapping_id, $post_data['property_list'] );
524
-
525
-			return array(
526
-				'status'     => 'success',
527
-				'message'    => __( 'Successfully saved mapping item', 'wordlift' ),
528
-				'mapping_id' => (int) $mapping_id,
529
-			);
530
-		} else {
531
-			return array(
532
-				'status'  => 'error',
533
-				'message' => __( 'Unable to save mapping item', 'wordlift' ),
534
-			);
535
-		}
536
-	}
209
+    /**
210
+     * Get the terms for the posted taxonomy name.
211
+     *
212
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
213
+     *
214
+     * @return array The array of the terms for the taxonomy.
215
+     */
216
+    public static function get_terms_for_the_posted_taxonomy( $request ) {
217
+        $post_data = $request->get_params();
218
+        if ( ! array_key_exists( 'taxonomy', $post_data ) ) {
219
+            return array(
220
+                'status'  => 'failure',
221
+                'message' => __( 'Request not valid, must post a taxonomy to get terms', 'wordlift' )
222
+            );
223
+        } else {
224
+            $taxonomy = $post_data['taxonomy'];
225
+            $terms    = get_terms( $taxonomy, array( 'hide_empty' => false, ) );
226
+            if ( is_wp_error( $terms ) ) {
227
+                // Return error response, if the taxonomy is not valid.
228
+                return array(
229
+                    'status'  => 'failure',
230
+                    'message' => __( 'Request not valid, must post a valid taxonomy', 'wordlift' )
231
+                );
232
+            }
233
+
234
+            return $terms;
235
+        }
236
+    }
237
+
238
+    /**
239
+     * Clone posted mapping items.
240
+     *
241
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
242
+     *
243
+     * @return array
244
+     */
245
+    public static function clone_mapping_items( $request ) {
246
+        $dbo           = new Mappings_DBO();
247
+        $post_data     = (array) $request->get_params();
248
+        $mapping_items = (array) $post_data['mapping_items'];
249
+        foreach ( $mapping_items as $mapping_item ) {
250
+            $mapping_id = (int) $mapping_item['mapping_id'];
251
+            // Clone the current mapping item.
252
+            $cloned_mapping_id = $dbo->insert_mapping_item( $mapping_item['mapping_title'] );
253
+            // Clone all the rule groups.
254
+            $rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping( $mapping_id );
255
+            // Clone all the properties.
256
+            $properties_to_be_cloned = $dbo->get_properties( $mapping_id );
257
+            foreach ( $properties_to_be_cloned as $property ) {
258
+                // Assign a new mapping id.
259
+                $property['mapping_id'] = $cloned_mapping_id;
260
+                // Removing this property id, since a new id needed to be created for
261
+                // new property.
262
+                unset( $property['property_id'] );
263
+                $dbo->insert_or_update_property( $property );
264
+            }
265
+            // Loop through the rule groups and insert them in table with the mapping id.
266
+            foreach ( $rule_groups_to_be_cloned as $rule_group ) {
267
+                $cloned_rule_group_id = $dbo->insert_rule_group( $cloned_mapping_id );
268
+                $original_rules       = (array) $rule_group['rules'];
269
+                // Now we need to insert these rules for the cloned rule group id.
270
+                foreach ( $original_rules as $clone_rule ) {
271
+                    // We should replace only rule group id in the cloned rules.
272
+                    $clone_rule['rule_group_id'] = (int) $cloned_rule_group_id;
273
+                    unset( $clone_rule['rule_id'] );
274
+                    $dbo->insert_or_update_rule_item( $clone_rule );
275
+                }
276
+            }
277
+        }
278
+
279
+        return array(
280
+            'status'  => 'success',
281
+            'message' => __( 'Successfully cloned mapping items', 'wordlift' ),
282
+        );
283
+    }
284
+
285
+    /**
286
+     * Update posted mapping items.
287
+     *
288
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
289
+     *
290
+     * @return array
291
+     */
292
+    public static function update_mapping_items( $request ) {
293
+        $dbo       = new Mappings_DBO();
294
+        $post_data = $request->get_params();
295
+        if ( array_key_exists( 'mapping_items', $post_data ) ) {
296
+            $mapping_items = (array) $post_data['mapping_items'];
297
+            foreach ( $mapping_items as $mapping_item ) {
298
+                $dbo->insert_or_update_mapping_item( $mapping_item );
299
+            }
300
+
301
+            return array(
302
+                'status'  => 'success',
303
+                'message' => __( 'Mapping items successfully updated', 'wordlift' ),
304
+            );
305
+        } else {
306
+            return array(
307
+                'status'  => 'failure',
308
+                'message' => __( 'Unable to update mapping item', 'wordlift' ),
309
+            );
310
+        }
311
+    }
312
+
313
+    /**
314
+     * Delete mapping items by mapping id
315
+     *
316
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
317
+     *
318
+     * @return array
319
+     */
320
+    public static function delete_mapping_items( $request ) {
321
+        $dbo       = new Mappings_DBO();
322
+        $post_data = $request->get_params();
323
+        if ( array_key_exists( 'mapping_items', $post_data ) ) {
324
+            $mapping_items = (array) $post_data['mapping_items'];
325
+            foreach ( $mapping_items as $mapping_item ) {
326
+                $dbo->delete_mapping_item( $mapping_item['mapping_id'] );
327
+            }
328
+
329
+            return array(
330
+                'status'  => 'success',
331
+                'message' => __( 'successfully deleted mapping items', 'wordlift' )
332
+            );
333
+        } else {
334
+            return array(
335
+                'status'  => 'failure',
336
+                'message' => __( 'Unable to delete mapping items', 'wordlift' )
337
+            );
338
+        }
339
+    }
340
+
341
+    /**
342
+     * Get all mapping items
343
+     *
344
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
345
+     *
346
+     * @return array
347
+     */
348
+    public static function list_mapping_items( $request ) {
349
+        $dbo = new Mappings_DBO();
350
+
351
+        return $dbo->get_mappings();
352
+    }
353
+
354
+    /**
355
+     * Returns a array of rule ids for the rule group id
356
+     *
357
+     * @param Object $dbo Instance of {@link Mappings_DBO } class.
358
+     * @param int $rule_group_id Primary key of rule group table.
359
+     *
360
+     * @return array A list of rule ids.
361
+     */
362
+    private static function get_rule_ids( $dbo, $rule_group_id ) {
363
+        $rule_rows_in_db = $dbo->get_rules_by_rule_group( $rule_group_id );
364
+        $rule_ids        = array();
365
+        foreach ( $rule_rows_in_db as $rule_row ) {
366
+            array_push( $rule_ids, (int) $rule_row['rule_id'] );
367
+        }
368
+
369
+        return $rule_ids;
370
+    }
371
+
372
+    /**
373
+     * Insert or update mapping item depends on data
374
+     *
375
+     * @param Object $dbo Instance of {@link Mappings_DBO } class.
376
+     * @param int $rule_group_id Refers to a rule group which this rule belongs to.
377
+     * @param array $rule_list Array of rule  items.
378
+     *
379
+     * @return void
380
+     */
381
+    private static function save_rules( $dbo, $rule_group_id, $rule_list ) {
382
+        $rule_ids = self::get_rule_ids( $dbo, $rule_group_id );
383
+        foreach ( $rule_list as $rule ) {
384
+            // Some rules may not have rule group id, because they are inserted
385
+            // in ui, so lets add them any way.
386
+            $rule['rule_group_id'] = $rule_group_id;
387
+            $dbo->insert_or_update_rule_item( $rule );
388
+            if ( array_key_exists( 'rule_id', $rule ) ) {
389
+                $index_to_be_removed = array_search(
390
+                    (int) $rule['rule_id'],
391
+                    $rule_ids,
392
+                    true
393
+                );
394
+                if ( false !== $index_to_be_removed ) {
395
+                    unset( $rule_ids[ $index_to_be_removed ] );
396
+                }
397
+            }
398
+        }
399
+        foreach ( $rule_ids as $rule_id ) {
400
+            // Delete all the rule ids which are not posted.
401
+            $dbo->delete_rule_item( $rule_id );
402
+        }
403
+    }
404
+
405
+    /**
406
+     * Insert or update rule group list based on data
407
+     *
408
+     * @param Object $dbo Instance of {@link Mappings_DBO } class.
409
+     * @param int $mapping_id Primary key of mapping table.
410
+     * @param array $property_list { Array of property items }.
411
+     *
412
+     * @return void
413
+     */
414
+    private static function save_property_list( $dbo, $mapping_id, $property_list ) {
415
+        $properties_needed_to_be_deleted = $dbo->get_properties( $mapping_id );
416
+        $property_ids                    = array();
417
+        foreach ( $properties_needed_to_be_deleted as $property ) {
418
+            array_push( $property_ids, (int) $property['property_id'] );
419
+        }
420
+        foreach ( $property_list as $property ) {
421
+            if ( array_key_exists( 'property_id', $property ) ) {
422
+                // Remove the id from the list of property ids needed to be deleted
423
+                // because it is posted.
424
+                $index_to_be_removed = array_search(
425
+                    (int) $property['property_id'],
426
+                    $property_ids,
427
+                    true
428
+                );
429
+                if ( false !== $index_to_be_removed ) {
430
+                    unset( $property_ids[ $index_to_be_removed ] );
431
+                }
432
+            }
433
+            // Add mapping id to property data.
434
+            $property['mapping_id'] = $mapping_id;
435
+            $dbo->insert_or_update_property( $property );
436
+
437
+        }
438
+        // At the end remove all the property ids which are not posted.
439
+        foreach ( $property_ids as $property_id ) {
440
+            $dbo->delete_property( $property_id );
441
+        }
442
+
443
+    }
444
+
445
+    /**
446
+     * Returns a array of rule group ids for the mapping id
447
+     *
448
+     * @param Object $dbo Instance of {@link Mappings_DBO } class.
449
+     * @param int $mapping_id Primary key of mapping table.
450
+     *
451
+     * @return array $rule_group_ids A list of rule group ids.
452
+     */
453
+    private static function get_rule_group_ids( $dbo, $mapping_id ) {
454
+        $rule_group_rows = $dbo->get_rule_group_list( $mapping_id );
455
+        $rule_group_ids  = array();
456
+        foreach ( $rule_group_rows as $rule_group_row ) {
457
+            array_push( $rule_group_ids, (int) $rule_group_row['rule_group_id'] );
458
+        }
459
+
460
+        return $rule_group_ids;
461
+    }
462
+
463
+    /**
464
+     * Insert or update rule group list
465
+     *
466
+     * @param Object $dbo Instance of {@link Mappings_DBO } class.
467
+     * @param int $mapping_id Primary key of mapping table.
468
+     * @param array $rule_group_list { Array of rule group items }.
469
+     *
470
+     * @return void
471
+     */
472
+    private static function save_rule_group_list( $dbo, $mapping_id, $rule_group_list ) {
473
+        // The rule groups not posted should be deleted.
474
+        $rule_group_ids = self::get_rule_group_ids( $dbo, $mapping_id );
475
+        // Loop through rule group list and save the rule group.
476
+        foreach ( $rule_group_list as $rule_group ) {
477
+            if ( array_key_exists( 'rule_group_id', $rule_group ) ) {
478
+                $rule_group_id = $rule_group['rule_group_id'];
479
+            } else {
480
+                // New rule group, should create new rule group id.
481
+                $rule_group_id = $dbo->insert_rule_group( $mapping_id );
482
+            }
483
+            $index_to_be_removed = array_search(
484
+                (int) $rule_group_id,
485
+                $rule_group_ids,
486
+                true
487
+            );
488
+            if ( false !== $index_to_be_removed ) {
489
+                unset( $rule_group_ids[ $index_to_be_removed ] );
490
+            }
491
+            self::save_rules( $dbo, $rule_group_id, $rule_group['rules'] );
492
+        }
493
+
494
+        // Remove all the rule groups which are not posted.
495
+        foreach ( $rule_group_ids as $rule_group_id ) {
496
+            $dbo->delete_rule_group_item( $rule_group_id );
497
+        }
498
+    }
499
+
500
+    /**
501
+     * Insert or update mapping item depends on data
502
+     *
503
+     * @param WP_REST_Request $request {@link WP_REST_Request instance}.
504
+     *
505
+     * @return array
506
+     */
507
+    public static function insert_or_update_mapping_item( $request ) {
508
+        $post_data = $request->get_params() === null ? array() : $request->get_params();
509
+        $dbo       = new Mappings_DBO();
510
+        // check if valid object is posted.
511
+        if ( array_key_exists( 'mapping_title', $post_data ) &&
512
+             array_key_exists( 'rule_group_list', $post_data ) &&
513
+             array_key_exists( 'property_list', $post_data ) ) {
514
+            // Do validation, remove all incomplete data.
515
+            $mapping_item = array();
516
+            if ( array_key_exists( 'mapping_id', $post_data ) ) {
517
+                $mapping_item['mapping_id'] = $post_data['mapping_id'];
518
+            }
519
+            $mapping_item['mapping_title'] = $post_data['mapping_title'];
520
+            // lets save the mapping item.
521
+            $mapping_id = $dbo->insert_or_update_mapping_item( $mapping_item );
522
+            self::save_rule_group_list( $dbo, $mapping_id, $post_data['rule_group_list'] );
523
+            self::save_property_list( $dbo, $mapping_id, $post_data['property_list'] );
524
+
525
+            return array(
526
+                'status'     => 'success',
527
+                'message'    => __( 'Successfully saved mapping item', 'wordlift' ),
528
+                'mapping_id' => (int) $mapping_id,
529
+            );
530
+        } else {
531
+            return array(
532
+                'status'  => 'error',
533
+                'message' => __( 'Unable to save mapping item', 'wordlift' ),
534
+            );
535
+        }
536
+    }
537 537
 }
538 538
 
539 539
 Mappings_REST_Controller::register_routes();
Please login to merge, or discard this patch.
Spacing   +113 added lines, -113 removed lines patch added patch discarded remove patch
@@ -21,7 +21,7 @@  discard block
 block discarded – undo
21 21
 	 */
22 22
 	public static function register_routes() {
23 23
 
24
-		add_action( 'rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback' );
24
+		add_action('rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback');
25 25
 
26 26
 	}
27 27
 
@@ -32,13 +32,13 @@  discard block
 block discarded – undo
32 32
 	 *
33 33
 	 * @return array
34 34
 	 */
35
-	public static function get_mapping_item( $request ) {
35
+	public static function get_mapping_item($request) {
36 36
 		$dbo             = new Mappings_DBO();
37 37
 		$mapping_id      = $request['id'];
38 38
 		$mapping_id_data = array();
39
-		$rule_groups     = $dbo->get_rule_groups_by_mapping( $mapping_id );
40
-		$properties      = $dbo->get_properties( $mapping_id );
41
-		$mapping_row     = $dbo->get_mapping_item_data( $mapping_id );
39
+		$rule_groups     = $dbo->get_rule_groups_by_mapping($mapping_id);
40
+		$properties      = $dbo->get_properties($mapping_id);
41
+		$mapping_row     = $dbo->get_mapping_item_data($mapping_id);
42 42
 
43 43
 		$mapping_id_data['mapping_id']      = $mapping_id;
44 44
 		$mapping_id_data['property_list']   = $properties;
@@ -60,8 +60,8 @@  discard block
 block discarded – undo
60 60
 			array(
61 61
 				'methods'             => WP_REST_Server::CREATABLE,
62 62
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::insert_or_update_mapping_item',
63
-				'permission_callback' => function () {
64
-					return current_user_can( 'manage_options' );
63
+				'permission_callback' => function() {
64
+					return current_user_can('manage_options');
65 65
 				},
66 66
 			)
67 67
 		);
@@ -72,8 +72,8 @@  discard block
 block discarded – undo
72 72
 			array(
73 73
 				'methods'             => WP_REST_Server::READABLE,
74 74
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::list_mapping_items',
75
-				'permission_callback' => function () {
76
-					return current_user_can( 'manage_options' );
75
+				'permission_callback' => function() {
76
+					return current_user_can('manage_options');
77 77
 				},
78 78
 			)
79 79
 		);
@@ -85,8 +85,8 @@  discard block
 block discarded – undo
85 85
 			array(
86 86
 				'methods'             => WP_REST_Server::DELETABLE,
87 87
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::delete_mapping_items',
88
-				'permission_callback' => function () {
89
-					return current_user_can( 'manage_options' );
88
+				'permission_callback' => function() {
89
+					return current_user_can('manage_options');
90 90
 				},
91 91
 			)
92 92
 		);
@@ -98,8 +98,8 @@  discard block
 block discarded – undo
98 98
 			array(
99 99
 				'methods'             => WP_REST_Server::READABLE,
100 100
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_mapping_item',
101
-				'permission_callback' => function () {
102
-					return current_user_can( 'manage_options' );
101
+				'permission_callback' => function() {
102
+					return current_user_can('manage_options');
103 103
 				},
104 104
 			)
105 105
 		);
@@ -111,8 +111,8 @@  discard block
 block discarded – undo
111 111
 			array(
112 112
 				'methods'             => WP_REST_Server::EDITABLE,
113 113
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::update_mapping_items',
114
-				'permission_callback' => function () {
115
-					return current_user_can( 'manage_options' );
114
+				'permission_callback' => function() {
115
+					return current_user_can('manage_options');
116 116
 				},
117 117
 			)
118 118
 		);
@@ -124,8 +124,8 @@  discard block
 block discarded – undo
124 124
 			array(
125 125
 				'methods'             => WP_REST_Server::CREATABLE,
126 126
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::clone_mapping_items',
127
-				'permission_callback' => function () {
128
-					return current_user_can( 'manage_options' );
127
+				'permission_callback' => function() {
128
+					return current_user_can('manage_options');
129 129
 				},
130 130
 			)
131 131
 		);
@@ -137,8 +137,8 @@  discard block
 block discarded – undo
137 137
 			array(
138 138
 				'methods'             => WP_REST_Server::CREATABLE,
139 139
 				'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_terms_for_the_posted_taxonomy',
140
-				'permission_callback' => function () {
141
-					return current_user_can( 'manage_options' );
140
+				'permission_callback' => function() {
141
+					return current_user_can('manage_options');
142 142
 				},
143 143
 			)
144 144
 		);
@@ -150,8 +150,8 @@  discard block
 block discarded – undo
150 150
             array(
151 151
                 'methods'             => WP_REST_Server::CREATABLE,
152 152
                 'callback'            => 'Wordlift\Mappings\Mappings_REST_Controller::get_taxonomy_terms_for_the_posted_taxonomy',
153
-                'permission_callback' => function () {
154
-                    return current_user_can( 'manage_options' );
153
+                'permission_callback' => function() {
154
+                    return current_user_can('manage_options');
155 155
                 },
156 156
             )
157 157
         );
@@ -164,23 +164,23 @@  discard block
 block discarded – undo
164 164
      *
165 165
      * @return array The array of the taxonomies & terms.
166 166
      */
167
-    public static function get_taxonomy_terms_for_the_posted_taxonomy( $request ) {
167
+    public static function get_taxonomy_terms_for_the_posted_taxonomy($request) {
168 168
         $taxonomy_terms = array();
169
-        $post_taxonomies = get_taxonomies( array(), 'objects' );
169
+        $post_taxonomies = get_taxonomies(array(), 'objects');
170 170
 
171
-        foreach ( $post_taxonomies as $post_taxonomy ) {
171
+        foreach ($post_taxonomies as $post_taxonomy) {
172 172
 
173
-            $group_taxonomy = array( 'parentValue' => 'post_taxonomy', 'group_name' => $post_taxonomy->label, 'group_options' => array() );
173
+            $group_taxonomy = array('parentValue' => 'post_taxonomy', 'group_name' => $post_taxonomy->label, 'group_options' => array());
174 174
 
175
-            $post_taxonomy_terms = get_terms( array(
175
+            $post_taxonomy_terms = get_terms(array(
176 176
                 'taxonomy' => $post_taxonomy->name,
177 177
                 'hide_empty' => true
178
-            ) );
178
+            ));
179 179
 
180
-            foreach ( $post_taxonomy_terms as $post_taxonomy_term ) {
180
+            foreach ($post_taxonomy_terms as $post_taxonomy_term) {
181 181
                 array_push($group_taxonomy['group_options'],
182 182
                     array(
183
-                        'label'        => ' - ' . $post_taxonomy_term->name,
183
+                        'label'        => ' - '.$post_taxonomy_term->name,
184 184
                         'value'        => $post_taxonomy_term->slug,
185 185
                         'taxonomy'    => 'post_taxonomy',
186 186
                     )
@@ -188,12 +188,12 @@  discard block
 block discarded – undo
188 188
 
189 189
                 $post_term_children = get_term_children($post_taxonomy_term->term_id, $post_taxonomy->name);
190 190
 
191
-                foreach ( $post_term_children as $post_term_child ) {
192
-                    $child_term =  get_term_by('id', $post_term_child, $post_taxonomy->name);
191
+                foreach ($post_term_children as $post_term_child) {
192
+                    $child_term = get_term_by('id', $post_term_child, $post_taxonomy->name);
193 193
 
194 194
                     array_push($group_taxonomy['group_options'],
195 195
                         array(
196
-                            'label'        => ' -- ' . $child_term->name,
196
+                            'label'        => ' -- '.$child_term->name,
197 197
                             'value'        => $child_term->slug,
198 198
                             'taxonomy'    => 'post_taxonomy',
199 199
                         )
@@ -213,21 +213,21 @@  discard block
 block discarded – undo
213 213
 	 *
214 214
 	 * @return array The array of the terms for the taxonomy.
215 215
 	 */
216
-	public static function get_terms_for_the_posted_taxonomy( $request ) {
216
+	public static function get_terms_for_the_posted_taxonomy($request) {
217 217
 		$post_data = $request->get_params();
218
-		if ( ! array_key_exists( 'taxonomy', $post_data ) ) {
218
+		if ( ! array_key_exists('taxonomy', $post_data)) {
219 219
 			return array(
220 220
 				'status'  => 'failure',
221
-				'message' => __( 'Request not valid, must post a taxonomy to get terms', 'wordlift' )
221
+				'message' => __('Request not valid, must post a taxonomy to get terms', 'wordlift')
222 222
 			);
223 223
 		} else {
224 224
 			$taxonomy = $post_data['taxonomy'];
225
-			$terms    = get_terms( $taxonomy, array( 'hide_empty' => false, ) );
226
-			if ( is_wp_error( $terms ) ) {
225
+			$terms    = get_terms($taxonomy, array('hide_empty' => false,));
226
+			if (is_wp_error($terms)) {
227 227
 				// Return error response, if the taxonomy is not valid.
228 228
 				return array(
229 229
 					'status'  => 'failure',
230
-					'message' => __( 'Request not valid, must post a valid taxonomy', 'wordlift' )
230
+					'message' => __('Request not valid, must post a valid taxonomy', 'wordlift')
231 231
 				);
232 232
 			}
233 233
 
@@ -242,43 +242,43 @@  discard block
 block discarded – undo
242 242
 	 *
243 243
 	 * @return array
244 244
 	 */
245
-	public static function clone_mapping_items( $request ) {
245
+	public static function clone_mapping_items($request) {
246 246
 		$dbo           = new Mappings_DBO();
247 247
 		$post_data     = (array) $request->get_params();
248 248
 		$mapping_items = (array) $post_data['mapping_items'];
249
-		foreach ( $mapping_items as $mapping_item ) {
249
+		foreach ($mapping_items as $mapping_item) {
250 250
 			$mapping_id = (int) $mapping_item['mapping_id'];
251 251
 			// Clone the current mapping item.
252
-			$cloned_mapping_id = $dbo->insert_mapping_item( $mapping_item['mapping_title'] );
252
+			$cloned_mapping_id = $dbo->insert_mapping_item($mapping_item['mapping_title']);
253 253
 			// Clone all the rule groups.
254
-			$rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping( $mapping_id );
254
+			$rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping($mapping_id);
255 255
 			// Clone all the properties.
256
-			$properties_to_be_cloned = $dbo->get_properties( $mapping_id );
257
-			foreach ( $properties_to_be_cloned as $property ) {
256
+			$properties_to_be_cloned = $dbo->get_properties($mapping_id);
257
+			foreach ($properties_to_be_cloned as $property) {
258 258
 				// Assign a new mapping id.
259 259
 				$property['mapping_id'] = $cloned_mapping_id;
260 260
 				// Removing this property id, since a new id needed to be created for
261 261
 				// new property.
262
-				unset( $property['property_id'] );
263
-				$dbo->insert_or_update_property( $property );
262
+				unset($property['property_id']);
263
+				$dbo->insert_or_update_property($property);
264 264
 			}
265 265
 			// Loop through the rule groups and insert them in table with the mapping id.
266
-			foreach ( $rule_groups_to_be_cloned as $rule_group ) {
267
-				$cloned_rule_group_id = $dbo->insert_rule_group( $cloned_mapping_id );
266
+			foreach ($rule_groups_to_be_cloned as $rule_group) {
267
+				$cloned_rule_group_id = $dbo->insert_rule_group($cloned_mapping_id);
268 268
 				$original_rules       = (array) $rule_group['rules'];
269 269
 				// Now we need to insert these rules for the cloned rule group id.
270
-				foreach ( $original_rules as $clone_rule ) {
270
+				foreach ($original_rules as $clone_rule) {
271 271
 					// We should replace only rule group id in the cloned rules.
272 272
 					$clone_rule['rule_group_id'] = (int) $cloned_rule_group_id;
273
-					unset( $clone_rule['rule_id'] );
274
-					$dbo->insert_or_update_rule_item( $clone_rule );
273
+					unset($clone_rule['rule_id']);
274
+					$dbo->insert_or_update_rule_item($clone_rule);
275 275
 				}
276 276
 			}
277 277
 		}
278 278
 
279 279
 		return array(
280 280
 			'status'  => 'success',
281
-			'message' => __( 'Successfully cloned mapping items', 'wordlift' ),
281
+			'message' => __('Successfully cloned mapping items', 'wordlift'),
282 282
 		);
283 283
 	}
284 284
 
@@ -289,23 +289,23 @@  discard block
 block discarded – undo
289 289
 	 *
290 290
 	 * @return array
291 291
 	 */
292
-	public static function update_mapping_items( $request ) {
292
+	public static function update_mapping_items($request) {
293 293
 		$dbo       = new Mappings_DBO();
294 294
 		$post_data = $request->get_params();
295
-		if ( array_key_exists( 'mapping_items', $post_data ) ) {
295
+		if (array_key_exists('mapping_items', $post_data)) {
296 296
 			$mapping_items = (array) $post_data['mapping_items'];
297
-			foreach ( $mapping_items as $mapping_item ) {
298
-				$dbo->insert_or_update_mapping_item( $mapping_item );
297
+			foreach ($mapping_items as $mapping_item) {
298
+				$dbo->insert_or_update_mapping_item($mapping_item);
299 299
 			}
300 300
 
301 301
 			return array(
302 302
 				'status'  => 'success',
303
-				'message' => __( 'Mapping items successfully updated', 'wordlift' ),
303
+				'message' => __('Mapping items successfully updated', 'wordlift'),
304 304
 			);
305 305
 		} else {
306 306
 			return array(
307 307
 				'status'  => 'failure',
308
-				'message' => __( 'Unable to update mapping item', 'wordlift' ),
308
+				'message' => __('Unable to update mapping item', 'wordlift'),
309 309
 			);
310 310
 		}
311 311
 	}
@@ -317,23 +317,23 @@  discard block
 block discarded – undo
317 317
 	 *
318 318
 	 * @return array
319 319
 	 */
320
-	public static function delete_mapping_items( $request ) {
320
+	public static function delete_mapping_items($request) {
321 321
 		$dbo       = new Mappings_DBO();
322 322
 		$post_data = $request->get_params();
323
-		if ( array_key_exists( 'mapping_items', $post_data ) ) {
323
+		if (array_key_exists('mapping_items', $post_data)) {
324 324
 			$mapping_items = (array) $post_data['mapping_items'];
325
-			foreach ( $mapping_items as $mapping_item ) {
326
-				$dbo->delete_mapping_item( $mapping_item['mapping_id'] );
325
+			foreach ($mapping_items as $mapping_item) {
326
+				$dbo->delete_mapping_item($mapping_item['mapping_id']);
327 327
 			}
328 328
 
329 329
 			return array(
330 330
 				'status'  => 'success',
331
-				'message' => __( 'successfully deleted mapping items', 'wordlift' )
331
+				'message' => __('successfully deleted mapping items', 'wordlift')
332 332
 			);
333 333
 		} else {
334 334
 			return array(
335 335
 				'status'  => 'failure',
336
-				'message' => __( 'Unable to delete mapping items', 'wordlift' )
336
+				'message' => __('Unable to delete mapping items', 'wordlift')
337 337
 			);
338 338
 		}
339 339
 	}
@@ -345,7 +345,7 @@  discard block
 block discarded – undo
345 345
 	 *
346 346
 	 * @return array
347 347
 	 */
348
-	public static function list_mapping_items( $request ) {
348
+	public static function list_mapping_items($request) {
349 349
 		$dbo = new Mappings_DBO();
350 350
 
351 351
 		return $dbo->get_mappings();
@@ -359,11 +359,11 @@  discard block
 block discarded – undo
359 359
 	 *
360 360
 	 * @return array A list of rule ids.
361 361
 	 */
362
-	private static function get_rule_ids( $dbo, $rule_group_id ) {
363
-		$rule_rows_in_db = $dbo->get_rules_by_rule_group( $rule_group_id );
362
+	private static function get_rule_ids($dbo, $rule_group_id) {
363
+		$rule_rows_in_db = $dbo->get_rules_by_rule_group($rule_group_id);
364 364
 		$rule_ids        = array();
365
-		foreach ( $rule_rows_in_db as $rule_row ) {
366
-			array_push( $rule_ids, (int) $rule_row['rule_id'] );
365
+		foreach ($rule_rows_in_db as $rule_row) {
366
+			array_push($rule_ids, (int) $rule_row['rule_id']);
367 367
 		}
368 368
 
369 369
 		return $rule_ids;
@@ -378,27 +378,27 @@  discard block
 block discarded – undo
378 378
 	 *
379 379
 	 * @return void
380 380
 	 */
381
-	private static function save_rules( $dbo, $rule_group_id, $rule_list ) {
382
-		$rule_ids = self::get_rule_ids( $dbo, $rule_group_id );
383
-		foreach ( $rule_list as $rule ) {
381
+	private static function save_rules($dbo, $rule_group_id, $rule_list) {
382
+		$rule_ids = self::get_rule_ids($dbo, $rule_group_id);
383
+		foreach ($rule_list as $rule) {
384 384
 			// Some rules may not have rule group id, because they are inserted
385 385
 			// in ui, so lets add them any way.
386 386
 			$rule['rule_group_id'] = $rule_group_id;
387
-			$dbo->insert_or_update_rule_item( $rule );
388
-			if ( array_key_exists( 'rule_id', $rule ) ) {
387
+			$dbo->insert_or_update_rule_item($rule);
388
+			if (array_key_exists('rule_id', $rule)) {
389 389
 				$index_to_be_removed = array_search(
390 390
 					(int) $rule['rule_id'],
391 391
 					$rule_ids,
392 392
 					true
393 393
 				);
394
-				if ( false !== $index_to_be_removed ) {
395
-					unset( $rule_ids[ $index_to_be_removed ] );
394
+				if (false !== $index_to_be_removed) {
395
+					unset($rule_ids[$index_to_be_removed]);
396 396
 				}
397 397
 			}
398 398
 		}
399
-		foreach ( $rule_ids as $rule_id ) {
399
+		foreach ($rule_ids as $rule_id) {
400 400
 			// Delete all the rule ids which are not posted.
401
-			$dbo->delete_rule_item( $rule_id );
401
+			$dbo->delete_rule_item($rule_id);
402 402
 		}
403 403
 	}
404 404
 
@@ -411,14 +411,14 @@  discard block
 block discarded – undo
411 411
 	 *
412 412
 	 * @return void
413 413
 	 */
414
-	private static function save_property_list( $dbo, $mapping_id, $property_list ) {
415
-		$properties_needed_to_be_deleted = $dbo->get_properties( $mapping_id );
414
+	private static function save_property_list($dbo, $mapping_id, $property_list) {
415
+		$properties_needed_to_be_deleted = $dbo->get_properties($mapping_id);
416 416
 		$property_ids                    = array();
417
-		foreach ( $properties_needed_to_be_deleted as $property ) {
418
-			array_push( $property_ids, (int) $property['property_id'] );
417
+		foreach ($properties_needed_to_be_deleted as $property) {
418
+			array_push($property_ids, (int) $property['property_id']);
419 419
 		}
420
-		foreach ( $property_list as $property ) {
421
-			if ( array_key_exists( 'property_id', $property ) ) {
420
+		foreach ($property_list as $property) {
421
+			if (array_key_exists('property_id', $property)) {
422 422
 				// Remove the id from the list of property ids needed to be deleted
423 423
 				// because it is posted.
424 424
 				$index_to_be_removed = array_search(
@@ -426,18 +426,18 @@  discard block
 block discarded – undo
426 426
 					$property_ids,
427 427
 					true
428 428
 				);
429
-				if ( false !== $index_to_be_removed ) {
430
-					unset( $property_ids[ $index_to_be_removed ] );
429
+				if (false !== $index_to_be_removed) {
430
+					unset($property_ids[$index_to_be_removed]);
431 431
 				}
432 432
 			}
433 433
 			// Add mapping id to property data.
434 434
 			$property['mapping_id'] = $mapping_id;
435
-			$dbo->insert_or_update_property( $property );
435
+			$dbo->insert_or_update_property($property);
436 436
 
437 437
 		}
438 438
 		// At the end remove all the property ids which are not posted.
439
-		foreach ( $property_ids as $property_id ) {
440
-			$dbo->delete_property( $property_id );
439
+		foreach ($property_ids as $property_id) {
440
+			$dbo->delete_property($property_id);
441 441
 		}
442 442
 
443 443
 	}
@@ -450,11 +450,11 @@  discard block
 block discarded – undo
450 450
 	 *
451 451
 	 * @return array $rule_group_ids A list of rule group ids.
452 452
 	 */
453
-	private static function get_rule_group_ids( $dbo, $mapping_id ) {
454
-		$rule_group_rows = $dbo->get_rule_group_list( $mapping_id );
453
+	private static function get_rule_group_ids($dbo, $mapping_id) {
454
+		$rule_group_rows = $dbo->get_rule_group_list($mapping_id);
455 455
 		$rule_group_ids  = array();
456
-		foreach ( $rule_group_rows as $rule_group_row ) {
457
-			array_push( $rule_group_ids, (int) $rule_group_row['rule_group_id'] );
456
+		foreach ($rule_group_rows as $rule_group_row) {
457
+			array_push($rule_group_ids, (int) $rule_group_row['rule_group_id']);
458 458
 		}
459 459
 
460 460
 		return $rule_group_ids;
@@ -469,31 +469,31 @@  discard block
 block discarded – undo
469 469
 	 *
470 470
 	 * @return void
471 471
 	 */
472
-	private static function save_rule_group_list( $dbo, $mapping_id, $rule_group_list ) {
472
+	private static function save_rule_group_list($dbo, $mapping_id, $rule_group_list) {
473 473
 		// The rule groups not posted should be deleted.
474
-		$rule_group_ids = self::get_rule_group_ids( $dbo, $mapping_id );
474
+		$rule_group_ids = self::get_rule_group_ids($dbo, $mapping_id);
475 475
 		// Loop through rule group list and save the rule group.
476
-		foreach ( $rule_group_list as $rule_group ) {
477
-			if ( array_key_exists( 'rule_group_id', $rule_group ) ) {
476
+		foreach ($rule_group_list as $rule_group) {
477
+			if (array_key_exists('rule_group_id', $rule_group)) {
478 478
 				$rule_group_id = $rule_group['rule_group_id'];
479 479
 			} else {
480 480
 				// New rule group, should create new rule group id.
481
-				$rule_group_id = $dbo->insert_rule_group( $mapping_id );
481
+				$rule_group_id = $dbo->insert_rule_group($mapping_id);
482 482
 			}
483 483
 			$index_to_be_removed = array_search(
484 484
 				(int) $rule_group_id,
485 485
 				$rule_group_ids,
486 486
 				true
487 487
 			);
488
-			if ( false !== $index_to_be_removed ) {
489
-				unset( $rule_group_ids[ $index_to_be_removed ] );
488
+			if (false !== $index_to_be_removed) {
489
+				unset($rule_group_ids[$index_to_be_removed]);
490 490
 			}
491
-			self::save_rules( $dbo, $rule_group_id, $rule_group['rules'] );
491
+			self::save_rules($dbo, $rule_group_id, $rule_group['rules']);
492 492
 		}
493 493
 
494 494
 		// Remove all the rule groups which are not posted.
495
-		foreach ( $rule_group_ids as $rule_group_id ) {
496
-			$dbo->delete_rule_group_item( $rule_group_id );
495
+		foreach ($rule_group_ids as $rule_group_id) {
496
+			$dbo->delete_rule_group_item($rule_group_id);
497 497
 		}
498 498
 	}
499 499
 
@@ -504,33 +504,33 @@  discard block
 block discarded – undo
504 504
 	 *
505 505
 	 * @return array
506 506
 	 */
507
-	public static function insert_or_update_mapping_item( $request ) {
507
+	public static function insert_or_update_mapping_item($request) {
508 508
 		$post_data = $request->get_params() === null ? array() : $request->get_params();
509 509
 		$dbo       = new Mappings_DBO();
510 510
 		// check if valid object is posted.
511
-		if ( array_key_exists( 'mapping_title', $post_data ) &&
512
-		     array_key_exists( 'rule_group_list', $post_data ) &&
513
-		     array_key_exists( 'property_list', $post_data ) ) {
511
+		if (array_key_exists('mapping_title', $post_data) &&
512
+		     array_key_exists('rule_group_list', $post_data) &&
513
+		     array_key_exists('property_list', $post_data)) {
514 514
 			// Do validation, remove all incomplete data.
515 515
 			$mapping_item = array();
516
-			if ( array_key_exists( 'mapping_id', $post_data ) ) {
516
+			if (array_key_exists('mapping_id', $post_data)) {
517 517
 				$mapping_item['mapping_id'] = $post_data['mapping_id'];
518 518
 			}
519 519
 			$mapping_item['mapping_title'] = $post_data['mapping_title'];
520 520
 			// lets save the mapping item.
521
-			$mapping_id = $dbo->insert_or_update_mapping_item( $mapping_item );
522
-			self::save_rule_group_list( $dbo, $mapping_id, $post_data['rule_group_list'] );
523
-			self::save_property_list( $dbo, $mapping_id, $post_data['property_list'] );
521
+			$mapping_id = $dbo->insert_or_update_mapping_item($mapping_item);
522
+			self::save_rule_group_list($dbo, $mapping_id, $post_data['rule_group_list']);
523
+			self::save_property_list($dbo, $mapping_id, $post_data['property_list']);
524 524
 
525 525
 			return array(
526 526
 				'status'     => 'success',
527
-				'message'    => __( 'Successfully saved mapping item', 'wordlift' ),
527
+				'message'    => __('Successfully saved mapping item', 'wordlift'),
528 528
 				'mapping_id' => (int) $mapping_id,
529 529
 			);
530 530
 		} else {
531 531
 			return array(
532 532
 				'status'  => 'error',
533
-				'message' => __( 'Unable to save mapping item', 'wordlift' ),
533
+				'message' => __('Unable to save mapping item', 'wordlift'),
534 534
 			);
535 535
 		}
536 536
 	}
Please login to merge, or discard this patch.
src/wordlift/mappings/pages/class-edit-mappings-page.php 2 patches
Indentation   +351 added lines, -351 removed lines patch added patch discarded remove patch
@@ -24,235 +24,235 @@  discard block
 block discarded – undo
24 24
  */
25 25
 class Edit_Mappings_Page extends Wordlift_Admin_Page {
26 26
 
27
-	/** Instance to store the registry class.
28
-	 * @var Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance}
29
-	 */
30
-	public $transform_function_registry;
31
-
32
-	/**
33
-	 * Edit_Mappings_Page constructor.
34
-	 *
35
-	 * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance }
36
-	 */
37
-	public function __construct( $transform_function_registry ) {
38
-		parent::__construct();
39
-		$this->transform_function_registry = $transform_function_registry;
40
-	}
41
-
42
-	public function render() {
43
-		// Render all the settings when this method is called, because the partial page is loaded after
44
-		// this method.
45
-		// Load the UI dependencies.
46
-		$edit_mapping_settings = $this->get_ui_settings_array();
47
-		// Supply the settings to js client.
48
-		wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings );
49
-
50
-		parent::render();
51
-	}
52
-
53
-	/**
54
-	 * Load the text settings needed for the edit_mappings_page.
55
-	 * @param array $edit_mapping_settings Key value pair of settings used by edit mappings page.
56
-	 *
57
-	 * @return array Adding text settings to the main settings array.
58
-	 */
59
-	private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) {
60
-		$edit_mapping_settings['wl_add_mapping_text']     = __( 'Add Mapping', 'wordlift' );
61
-		$edit_mapping_settings['wl_edit_mapping_text']    = __( 'Edit Mapping', 'wordlift' );
62
-		$edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' );
63
-		$edit_mapping_settings['page']                    = 'wl_edit_mapping';
64
-		return $edit_mapping_settings;
65
-	}
66
-
67
-	/**
68
-	 * The base class {@link Wordlift_Admin_Page} will add the admin page to the WordLift menu.
69
-	 *
70
-	 * We don't want this page to be in the menu though. Therefore we override the `parent_slug` so that WordPress won't
71
-	 * show it there.
72
-	 *
73
-	 * @return null return null to avoid this page to be displayed in WordLift's menu.
74
-	 */
75
-	protected function get_parent_slug() {
76
-		return null;
77
-	}
78
-
79
-	/**
80
-	 * {@inheritdoc}
81
-	 */
82
-	public function get_page_title() {
83
-
84
-		return __( 'Edit Mappings', 'wordlift' );
85
-	}
86
-
87
-	/**
88
-	 * {@inheritdoc}
89
-	 */
90
-	public function get_menu_title() {
91
-
92
-		return __( 'Edit Mappings', 'wordlift' );
93
-	}
94
-
95
-	/**
96
-	 * {@inheritdoc}
97
-	 */
98
-	public function get_menu_slug() {
99
-
100
-		return 'wl_edit_mapping';
101
-	}
102
-
103
-	/**
104
-	 * {@inheritdoc}
105
-	 */
106
-	public function get_partial_name() {
107
-		return 'wordlift-admin-mappings-edit.php';
108
-	}
109
-
110
-	/**
111
-	 * {@inheritdoc}
112
-	 */
113
-	public function enqueue_scripts() {
114
-
115
-		// Enqueue the script.
116
-		Scripts_Helper::enqueue_based_on_wordpress_version(
117
-			'wl-mappings-edit',
118
-			plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit',
119
-			array( 'react', 'react-dom', 'wp-polyfill' ),
120
-			true
121
-		);
122
-
123
-		// Enqueue the style.
124
-		wp_enqueue_style(
125
-			'wl-mappings-edit',
126
-			plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit.css',
127
-			Wordlift::get_instance()->get_version()
128
-		);
129
-	}
130
-
131
-	/**
132
-	 * Returns field name options based on the chosen field type.
133
-	 * if string is returned a text field would be shown to user, if an array of options is returned
134
-	 * then the select box would be shown to user.
135
-	 *
136
-	 * @return array Array of the options.
137
-	 */
138
-	public static function get_all_field_name_options() {
139
-
140
-		$options = array(
141
-			array(
142
-				'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD,
143
-				'value'      => '',
144
-				'label'      => __( 'Fixed Text', 'wordlift' ),
145
-			),
146
-			// @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings.
147
-			array(
148
-				'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD,
149
-				'value'      => '',
150
-				'label'      => __( 'Custom Field', 'wordlift' ),
151
-			),
152
-		);
153
-
154
-		/**
155
-		 * Allow 3rd parties to add field types.
156
-		 *
157
-		 * @param array An array of Field Types.
158
-		 *
159
-		 * @return array An array of Field Types.
160
-		 *
161
-		 * @since 3.25.0
162
-		 */
163
-		return apply_filters( 'wl_mappings_field_types', $options );
164
-	}
165
-
166
-	/**
167
-	 * @since 3.25.0
168
-	 * Load dependencies required for js client.
169
-	 * @return array An Array containing key value pairs of settings.
170
-	 */
171
-	public function get_ui_settings_array() {
172
-		// Create ui settings array to be used by js client.
173
-		$edit_mapping_settings                               = array();
174
-		$edit_mapping_settings                               = $this->load_rest_settings( $edit_mapping_settings );
175
-		$edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings );
176
-		$edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options();
177
-		$edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings );
178
-		// Load logic field options.
179
-		$edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings );
180
-		$edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings );
181
-
182
-		return $edit_mapping_settings;
183
-	}
184
-
185
-	/**
186
-	 * Returns post type, post category, or any other post taxonomies
187
-	 * @return array An array of select options
188
-	 */
189
-	private static function get_post_taxonomies_and_terms() {
190
-		$taxonomy_options = array();
191
-		$term_options     = array();
192
-		$taxonomies       = get_object_taxonomies( 'post', 'objects' );
193
-
194
-		foreach ( $taxonomies as $taxonomy ) {
195
-			array_push(
196
-				$taxonomy_options,
197
-				array(
198
-					'label'      => $taxonomy->label,
199
-					'value'      => $taxonomy->name,
200
-					'api_source' => 'taxonomy'
201
-				)
202
-			);
203
-		}
204
-		// Post type is also included in the list of taxonomies, so get the post type and merge with options.
205
-		$post_type_array =  self::get_post_type_key_and_value();
206
-		$post_type_option = $post_type_array['post_type_option_name'];
207
-		// Get also the list of post types from the post_type_array.
208
-		$post_type_option_values = $post_type_array['post_type_option_values'];
27
+    /** Instance to store the registry class.
28
+     * @var Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance}
29
+     */
30
+    public $transform_function_registry;
31
+
32
+    /**
33
+     * Edit_Mappings_Page constructor.
34
+     *
35
+     * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance }
36
+     */
37
+    public function __construct( $transform_function_registry ) {
38
+        parent::__construct();
39
+        $this->transform_function_registry = $transform_function_registry;
40
+    }
41
+
42
+    public function render() {
43
+        // Render all the settings when this method is called, because the partial page is loaded after
44
+        // this method.
45
+        // Load the UI dependencies.
46
+        $edit_mapping_settings = $this->get_ui_settings_array();
47
+        // Supply the settings to js client.
48
+        wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings );
49
+
50
+        parent::render();
51
+    }
52
+
53
+    /**
54
+     * Load the text settings needed for the edit_mappings_page.
55
+     * @param array $edit_mapping_settings Key value pair of settings used by edit mappings page.
56
+     *
57
+     * @return array Adding text settings to the main settings array.
58
+     */
59
+    private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) {
60
+        $edit_mapping_settings['wl_add_mapping_text']     = __( 'Add Mapping', 'wordlift' );
61
+        $edit_mapping_settings['wl_edit_mapping_text']    = __( 'Edit Mapping', 'wordlift' );
62
+        $edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' );
63
+        $edit_mapping_settings['page']                    = 'wl_edit_mapping';
64
+        return $edit_mapping_settings;
65
+    }
66
+
67
+    /**
68
+     * The base class {@link Wordlift_Admin_Page} will add the admin page to the WordLift menu.
69
+     *
70
+     * We don't want this page to be in the menu though. Therefore we override the `parent_slug` so that WordPress won't
71
+     * show it there.
72
+     *
73
+     * @return null return null to avoid this page to be displayed in WordLift's menu.
74
+     */
75
+    protected function get_parent_slug() {
76
+        return null;
77
+    }
78
+
79
+    /**
80
+     * {@inheritdoc}
81
+     */
82
+    public function get_page_title() {
83
+
84
+        return __( 'Edit Mappings', 'wordlift' );
85
+    }
86
+
87
+    /**
88
+     * {@inheritdoc}
89
+     */
90
+    public function get_menu_title() {
91
+
92
+        return __( 'Edit Mappings', 'wordlift' );
93
+    }
94
+
95
+    /**
96
+     * {@inheritdoc}
97
+     */
98
+    public function get_menu_slug() {
99
+
100
+        return 'wl_edit_mapping';
101
+    }
102
+
103
+    /**
104
+     * {@inheritdoc}
105
+     */
106
+    public function get_partial_name() {
107
+        return 'wordlift-admin-mappings-edit.php';
108
+    }
109
+
110
+    /**
111
+     * {@inheritdoc}
112
+     */
113
+    public function enqueue_scripts() {
114
+
115
+        // Enqueue the script.
116
+        Scripts_Helper::enqueue_based_on_wordpress_version(
117
+            'wl-mappings-edit',
118
+            plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit',
119
+            array( 'react', 'react-dom', 'wp-polyfill' ),
120
+            true
121
+        );
122
+
123
+        // Enqueue the style.
124
+        wp_enqueue_style(
125
+            'wl-mappings-edit',
126
+            plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit.css',
127
+            Wordlift::get_instance()->get_version()
128
+        );
129
+    }
130
+
131
+    /**
132
+     * Returns field name options based on the chosen field type.
133
+     * if string is returned a text field would be shown to user, if an array of options is returned
134
+     * then the select box would be shown to user.
135
+     *
136
+     * @return array Array of the options.
137
+     */
138
+    public static function get_all_field_name_options() {
139
+
140
+        $options = array(
141
+            array(
142
+                'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD,
143
+                'value'      => '',
144
+                'label'      => __( 'Fixed Text', 'wordlift' ),
145
+            ),
146
+            // @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings.
147
+            array(
148
+                'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD,
149
+                'value'      => '',
150
+                'label'      => __( 'Custom Field', 'wordlift' ),
151
+            ),
152
+        );
153
+
154
+        /**
155
+         * Allow 3rd parties to add field types.
156
+         *
157
+         * @param array An array of Field Types.
158
+         *
159
+         * @return array An array of Field Types.
160
+         *
161
+         * @since 3.25.0
162
+         */
163
+        return apply_filters( 'wl_mappings_field_types', $options );
164
+    }
165
+
166
+    /**
167
+     * @since 3.25.0
168
+     * Load dependencies required for js client.
169
+     * @return array An Array containing key value pairs of settings.
170
+     */
171
+    public function get_ui_settings_array() {
172
+        // Create ui settings array to be used by js client.
173
+        $edit_mapping_settings                               = array();
174
+        $edit_mapping_settings                               = $this->load_rest_settings( $edit_mapping_settings );
175
+        $edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings );
176
+        $edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options();
177
+        $edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings );
178
+        // Load logic field options.
179
+        $edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings );
180
+        $edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings );
181
+
182
+        return $edit_mapping_settings;
183
+    }
184
+
185
+    /**
186
+     * Returns post type, post category, or any other post taxonomies
187
+     * @return array An array of select options
188
+     */
189
+    private static function get_post_taxonomies_and_terms() {
190
+        $taxonomy_options = array();
191
+        $term_options     = array();
192
+        $taxonomies       = get_object_taxonomies( 'post', 'objects' );
193
+
194
+        foreach ( $taxonomies as $taxonomy ) {
195
+            array_push(
196
+                $taxonomy_options,
197
+                array(
198
+                    'label'      => $taxonomy->label,
199
+                    'value'      => $taxonomy->name,
200
+                    'api_source' => 'taxonomy'
201
+                )
202
+            );
203
+        }
204
+        // Post type is also included in the list of taxonomies, so get the post type and merge with options.
205
+        $post_type_array =  self::get_post_type_key_and_value();
206
+        $post_type_option = $post_type_array['post_type_option_name'];
207
+        // Get also the list of post types from the post_type_array.
208
+        $post_type_option_values = $post_type_array['post_type_option_values'];
209 209
 
210 210
         $post_taxonomy_array =  self::get_post_taxonomy_key_and_value();
211 211
         $post_taxonomy_option = $post_taxonomy_array['post_taxonomy_option_name'];
212 212
 
213
-		// Merge the post type option and post types in the taxonomy options
214
-		array_push( $taxonomy_options, $post_type_option, $post_taxonomy_option);
215
-		$term_options = array_merge( $term_options, $post_type_option_values );
216
-		return array(
217
-			'taxonomy_options' => $taxonomy_options,
218
-			'term_options' => $term_options
219
-		);
220
-	}
221
-
222
-	/**
223
-	 * Return post type option and post type option values.
224
-	 *
225
-	 * @return array Array of post_type_option and post_type_option_values.
226
-	 */
227
-	private static function get_post_type_key_and_value() {
228
-		$post_type_option_name   = array(
229
-			'label'      => __( 'Post type', 'wordlift' ),
230
-			'value'      => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE,
231
-			// Left empty since post types are provided locally.
232
-			'api_source' => '',
233
-		);
234
-		$post_type_option_values = array();
235
-		$post_types              = get_post_types(
236
-			array(),
237
-			'objects'
238
-		);
239
-
240
-		foreach ( $post_types as $post_type ) {
241
-			array_push(
242
-				$post_type_option_values,
243
-				array(
244
-					'label'        => $post_type->label,
245
-					'value'        => $post_type->name,
246
-					'parent_value' => 'post_type',
247
-				)
248
-			);
249
-		}
250
-
251
-		return array(
252
-			'post_type_option_name' =>  $post_type_option_name,
253
-			'post_type_option_values' => $post_type_option_values
254
-		);
255
-	}
213
+        // Merge the post type option and post types in the taxonomy options
214
+        array_push( $taxonomy_options, $post_type_option, $post_taxonomy_option);
215
+        $term_options = array_merge( $term_options, $post_type_option_values );
216
+        return array(
217
+            'taxonomy_options' => $taxonomy_options,
218
+            'term_options' => $term_options
219
+        );
220
+    }
221
+
222
+    /**
223
+     * Return post type option and post type option values.
224
+     *
225
+     * @return array Array of post_type_option and post_type_option_values.
226
+     */
227
+    private static function get_post_type_key_and_value() {
228
+        $post_type_option_name   = array(
229
+            'label'      => __( 'Post type', 'wordlift' ),
230
+            'value'      => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE,
231
+            // Left empty since post types are provided locally.
232
+            'api_source' => '',
233
+        );
234
+        $post_type_option_values = array();
235
+        $post_types              = get_post_types(
236
+            array(),
237
+            'objects'
238
+        );
239
+
240
+        foreach ( $post_types as $post_type ) {
241
+            array_push(
242
+                $post_type_option_values,
243
+                array(
244
+                    'label'        => $post_type->label,
245
+                    'value'        => $post_type->name,
246
+                    'parent_value' => 'post_type',
247
+                )
248
+            );
249
+        }
250
+
251
+        return array(
252
+            'post_type_option_name' =>  $post_type_option_name,
253
+            'post_type_option_values' => $post_type_option_values
254
+        );
255
+    }
256 256
 
257 257
     /**
258 258
      * Return post type option and post type option values.
@@ -275,132 +275,132 @@  discard block
 block discarded – undo
275 275
         );
276 276
     }
277 277
 
278
-	/**
279
-	 * This function loads the equal to, not equal to operator to the edit mapping settings.
280
-	 *
281
-	 * @param array $edit_mapping_settings
282
-	 * @return array Loads the logic field options to the $edit_mapping_settings.
283
-	 */
284
-	private function load_logic_field_options( array $edit_mapping_settings ) {
285
-		$edit_mapping_settings['wl_logic_field_options'] = array(
286
-			array(
287
-				'label' => __( 'is equal to', 'wordlift' ),
288
-				'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO,
289
-			),
290
-			array(
291
-				'label' => __( 'is not equal to', 'wordlift' ),
292
-				'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO,
293
-			),
294
-		);
295
-
296
-		return $edit_mapping_settings;
297
-	}
298
-
299
-	/**
300
-	 * Validates the nonce posted by client and then assign the mapping id which should be edited.
301
-	 *
302
-	 * @param array $edit_mapping_settings
303
-	 * @return array Edit mapping settings array with the mapping id if the nonce is valid.
304
-	 */
305
-	private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) {
278
+    /**
279
+     * This function loads the equal to, not equal to operator to the edit mapping settings.
280
+     *
281
+     * @param array $edit_mapping_settings
282
+     * @return array Loads the logic field options to the $edit_mapping_settings.
283
+     */
284
+    private function load_logic_field_options( array $edit_mapping_settings ) {
285
+        $edit_mapping_settings['wl_logic_field_options'] = array(
286
+            array(
287
+                'label' => __( 'is equal to', 'wordlift' ),
288
+                'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO,
289
+            ),
290
+            array(
291
+                'label' => __( 'is not equal to', 'wordlift' ),
292
+                'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO,
293
+            ),
294
+        );
295
+
296
+        return $edit_mapping_settings;
297
+    }
298
+
299
+    /**
300
+     * Validates the nonce posted by client and then assign the mapping id which should be edited.
301
+     *
302
+     * @param array $edit_mapping_settings
303
+     * @return array Edit mapping settings array with the mapping id if the nonce is valid.
304
+     */
305
+    private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) {
306 306
         // We verify the nonce before making to load the edit mapping page for the wl_edit_mapping_id
307
-		if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] )
308
-		     && wp_verify_nonce( $_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce' ) ) {
309
-			// We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST.
310
-			$edit_mapping_settings['wl_edit_mapping_id'] = (int) filter_var( $_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT );
311
-		}
312
-
313
-		return $edit_mapping_settings;
314
-	}
315
-
316
-	/**
317
-	 * Load the rest settings required for the edit_mappings js client.
318
-	 *
319
-	 * @param array $edit_mapping_settings
320
-	 * @return array
321
-	 */
322
-	private function load_rest_settings( array $edit_mapping_settings ) {
323
-		$edit_mapping_settings['rest_url']                   = get_rest_url(
324
-			null,
325
-			WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE
326
-		);
327
-		$edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' );
328
-		$edit_mapping_settings                               = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings );
329
-
330
-		return $edit_mapping_settings;
331
-	}
332
-
333
-	/**
334
-	 * Load the rule field options in to the settings.
335
-	 *
336
-	 * @param array $edit_mapping_settings
337
-	 *
338
-	 * @return array Return the settings.
339
- 	 */
340
-	private function load_rule_field_options( array $edit_mapping_settings ) {
341
-		// Load the rule field options.
342
-		$rule_field_data                                    = self::get_post_taxonomies_and_terms();
343
-		$edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options'];
344
-		$edit_mapping_settings['wl_rule_field_two_options'] = $rule_field_data['term_options'];
345
-
346
-		/**
347
-		 * Allow 3rd parties to add ui options.
348
-		 *
349
-		 * @param array Array of Rule field one options where each item is in format
350
-		 *
351
-		 *  array ( 'label' => string, 'value' => string, 'api_source'=>string);
352
-		 *
353
-		 *  Leave api_source empty string to ensure didnt fetch rule field two options
354
-		 *  from api.
355
-		 *
356
-		 * @return array Array of Rule field one options
357
-		 *
358
-		 * @since 3.27.0
359
-		 */
360
-		$edit_mapping_settings['wl_rule_field_one_options'] = apply_filters(
361
-			'wl_mappings_rule_field_one_options',
362
-			$edit_mapping_settings['wl_rule_field_one_options']
363
-		);
364
-
365
-		/**
366
-		 * Allow 3rd parties to add rule field two options.
367
-		 *
368
-		 * @param array Array of Rule field two option where each item is in format
369
-		 *
370
-		 * array ( 'label' => string, 'value' => string, 'parent_value' => string );
371
-		 *
372
-		 * where parent_value is the value of the parent option in the rule_field_one_option.
373
-		 *
374
-		 * @since 3.27.0
375
-		 */
376
-		$edit_mapping_settings['wl_rule_field_two_options'] = apply_filters(
377
-			'wl_mappings_rule_field_two_options',
378
-			$edit_mapping_settings['wl_rule_field_two_options']
379
-		);
380
-
381
-		return $edit_mapping_settings;
382
-	}
383
-
384
-	/**
385
-	 * Load field type and field name options to the settings array.
386
-	 * @param array $edit_mapping_settings
387
-	 *
388
-	 * @return array
389
-	 */
390
-	private function load_field_type_and_name_options( array $edit_mapping_settings ) {
391
-		$all_field_name_options  = self::get_all_field_name_options();
392
-		$all_field_types_options = array_map( function ( $item ) {
393
-			return array(
394
-				'label' => $item['label'],
395
-				'value' => $item['field_type'],
396
-			);
397
-		}, $all_field_name_options );
398
-
399
-		$edit_mapping_settings['wl_field_type_options'] = $all_field_types_options;
400
-		// Add wl_edit_field_name_options.
401
-		$edit_mapping_settings['wl_field_name_options'] = $all_field_name_options;
402
-
403
-		return $edit_mapping_settings;
404
-	}
307
+        if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] )
308
+             && wp_verify_nonce( $_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce' ) ) {
309
+            // We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST.
310
+            $edit_mapping_settings['wl_edit_mapping_id'] = (int) filter_var( $_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT );
311
+        }
312
+
313
+        return $edit_mapping_settings;
314
+    }
315
+
316
+    /**
317
+     * Load the rest settings required for the edit_mappings js client.
318
+     *
319
+     * @param array $edit_mapping_settings
320
+     * @return array
321
+     */
322
+    private function load_rest_settings( array $edit_mapping_settings ) {
323
+        $edit_mapping_settings['rest_url']                   = get_rest_url(
324
+            null,
325
+            WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE
326
+        );
327
+        $edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' );
328
+        $edit_mapping_settings                               = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings );
329
+
330
+        return $edit_mapping_settings;
331
+    }
332
+
333
+    /**
334
+     * Load the rule field options in to the settings.
335
+     *
336
+     * @param array $edit_mapping_settings
337
+     *
338
+     * @return array Return the settings.
339
+     */
340
+    private function load_rule_field_options( array $edit_mapping_settings ) {
341
+        // Load the rule field options.
342
+        $rule_field_data                                    = self::get_post_taxonomies_and_terms();
343
+        $edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options'];
344
+        $edit_mapping_settings['wl_rule_field_two_options'] = $rule_field_data['term_options'];
345
+
346
+        /**
347
+         * Allow 3rd parties to add ui options.
348
+         *
349
+         * @param array Array of Rule field one options where each item is in format
350
+         *
351
+         *  array ( 'label' => string, 'value' => string, 'api_source'=>string);
352
+         *
353
+         *  Leave api_source empty string to ensure didnt fetch rule field two options
354
+         *  from api.
355
+         *
356
+         * @return array Array of Rule field one options
357
+         *
358
+         * @since 3.27.0
359
+         */
360
+        $edit_mapping_settings['wl_rule_field_one_options'] = apply_filters(
361
+            'wl_mappings_rule_field_one_options',
362
+            $edit_mapping_settings['wl_rule_field_one_options']
363
+        );
364
+
365
+        /**
366
+         * Allow 3rd parties to add rule field two options.
367
+         *
368
+         * @param array Array of Rule field two option where each item is in format
369
+         *
370
+         * array ( 'label' => string, 'value' => string, 'parent_value' => string );
371
+         *
372
+         * where parent_value is the value of the parent option in the rule_field_one_option.
373
+         *
374
+         * @since 3.27.0
375
+         */
376
+        $edit_mapping_settings['wl_rule_field_two_options'] = apply_filters(
377
+            'wl_mappings_rule_field_two_options',
378
+            $edit_mapping_settings['wl_rule_field_two_options']
379
+        );
380
+
381
+        return $edit_mapping_settings;
382
+    }
383
+
384
+    /**
385
+     * Load field type and field name options to the settings array.
386
+     * @param array $edit_mapping_settings
387
+     *
388
+     * @return array
389
+     */
390
+    private function load_field_type_and_name_options( array $edit_mapping_settings ) {
391
+        $all_field_name_options  = self::get_all_field_name_options();
392
+        $all_field_types_options = array_map( function ( $item ) {
393
+            return array(
394
+                'label' => $item['label'],
395
+                'value' => $item['field_type'],
396
+            );
397
+        }, $all_field_name_options );
398
+
399
+        $edit_mapping_settings['wl_field_type_options'] = $all_field_types_options;
400
+        // Add wl_edit_field_name_options.
401
+        $edit_mapping_settings['wl_field_name_options'] = $all_field_name_options;
402
+
403
+        return $edit_mapping_settings;
404
+    }
405 405
 
406 406
 }
Please login to merge, or discard this patch.
Spacing   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
 	 *
35 35
 	 * @param $transform_function_registry Mappings_Transform_Functions_Registry { @link Mappings_Transform_Functions_Registry instance }
36 36
 	 */
37
-	public function __construct( $transform_function_registry ) {
37
+	public function __construct($transform_function_registry) {
38 38
 		parent::__construct();
39 39
 		$this->transform_function_registry = $transform_function_registry;
40 40
 	}
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
 		// Load the UI dependencies.
46 46
 		$edit_mapping_settings = $this->get_ui_settings_array();
47 47
 		// Supply the settings to js client.
48
-		wp_localize_script( 'wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings );
48
+		wp_localize_script('wl-mappings-edit', 'wl_edit_mappings_config', $edit_mapping_settings);
49 49
 
50 50
 		parent::render();
51 51
 	}
@@ -56,10 +56,10 @@  discard block
 block discarded – undo
56 56
 	 *
57 57
 	 * @return array Adding text settings to the main settings array.
58 58
 	 */
59
-	private function load_text_settings_for_edit_mapping_page( array $edit_mapping_settings ) {
60
-		$edit_mapping_settings['wl_add_mapping_text']     = __( 'Add Mapping', 'wordlift' );
61
-		$edit_mapping_settings['wl_edit_mapping_text']    = __( 'Edit Mapping', 'wordlift' );
62
-		$edit_mapping_settings['wl_edit_mapping_no_item'] = __( 'Unable to find the mapping item', 'wordlift' );
59
+	private function load_text_settings_for_edit_mapping_page(array $edit_mapping_settings) {
60
+		$edit_mapping_settings['wl_add_mapping_text']     = __('Add Mapping', 'wordlift');
61
+		$edit_mapping_settings['wl_edit_mapping_text']    = __('Edit Mapping', 'wordlift');
62
+		$edit_mapping_settings['wl_edit_mapping_no_item'] = __('Unable to find the mapping item', 'wordlift');
63 63
 		$edit_mapping_settings['page']                    = 'wl_edit_mapping';
64 64
 		return $edit_mapping_settings;
65 65
 	}
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
 	 */
82 82
 	public function get_page_title() {
83 83
 
84
-		return __( 'Edit Mappings', 'wordlift' );
84
+		return __('Edit Mappings', 'wordlift');
85 85
 	}
86 86
 
87 87
 	/**
@@ -89,7 +89,7 @@  discard block
 block discarded – undo
89 89
 	 */
90 90
 	public function get_menu_title() {
91 91
 
92
-		return __( 'Edit Mappings', 'wordlift' );
92
+		return __('Edit Mappings', 'wordlift');
93 93
 	}
94 94
 
95 95
 	/**
@@ -115,15 +115,15 @@  discard block
 block discarded – undo
115 115
 		// Enqueue the script.
116 116
 		Scripts_Helper::enqueue_based_on_wordpress_version(
117 117
 			'wl-mappings-edit',
118
-			plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit',
119
-			array( 'react', 'react-dom', 'wp-polyfill' ),
118
+			plugin_dir_url(dirname(dirname(dirname(__FILE__)))).'js/dist/mappings-edit',
119
+			array('react', 'react-dom', 'wp-polyfill'),
120 120
 			true
121 121
 		);
122 122
 
123 123
 		// Enqueue the style.
124 124
 		wp_enqueue_style(
125 125
 			'wl-mappings-edit',
126
-			plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings-edit.css',
126
+			plugin_dir_url(dirname(dirname(dirname(__FILE__)))).'js/dist/mappings-edit.css',
127 127
 			Wordlift::get_instance()->get_version()
128 128
 		);
129 129
 	}
@@ -141,13 +141,13 @@  discard block
 block discarded – undo
141 141
 			array(
142 142
 				'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_TEXT_FIELD,
143 143
 				'value'      => '',
144
-				'label'      => __( 'Fixed Text', 'wordlift' ),
144
+				'label'      => __('Fixed Text', 'wordlift'),
145 145
 			),
146 146
 			// @@todo maybe it makes sense to move this one as well to Wordlift/Mappings/Custom_Fields_Mappings.
147 147
 			array(
148 148
 				'field_type' => Wordlift\Mappings\Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD,
149 149
 				'value'      => '',
150
-				'label'      => __( 'Custom Field', 'wordlift' ),
150
+				'label'      => __('Custom Field', 'wordlift'),
151 151
 			),
152 152
 		);
153 153
 
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
 		 *
161 161
 		 * @since 3.25.0
162 162
 		 */
163
-		return apply_filters( 'wl_mappings_field_types', $options );
163
+		return apply_filters('wl_mappings_field_types', $options);
164 164
 	}
165 165
 
166 166
 	/**
@@ -171,13 +171,13 @@  discard block
 block discarded – undo
171 171
 	public function get_ui_settings_array() {
172 172
 		// Create ui settings array to be used by js client.
173 173
 		$edit_mapping_settings                               = array();
174
-		$edit_mapping_settings                               = $this->load_rest_settings( $edit_mapping_settings );
175
-		$edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page( $edit_mapping_settings );
174
+		$edit_mapping_settings                               = $this->load_rest_settings($edit_mapping_settings);
175
+		$edit_mapping_settings = $this->load_text_settings_for_edit_mapping_page($edit_mapping_settings);
176 176
 		$edit_mapping_settings['wl_transform_function_options'] = $this->transform_function_registry->get_options();
177
-		$edit_mapping_settings = $this->load_field_type_and_name_options( $edit_mapping_settings );
177
+		$edit_mapping_settings = $this->load_field_type_and_name_options($edit_mapping_settings);
178 178
 		// Load logic field options.
179
-		$edit_mapping_settings = $this->load_logic_field_options( $edit_mapping_settings );
180
-		$edit_mapping_settings = $this->load_rule_field_options( $edit_mapping_settings );
179
+		$edit_mapping_settings = $this->load_logic_field_options($edit_mapping_settings);
180
+		$edit_mapping_settings = $this->load_rule_field_options($edit_mapping_settings);
181 181
 
182 182
 		return $edit_mapping_settings;
183 183
 	}
@@ -189,9 +189,9 @@  discard block
 block discarded – undo
189 189
 	private static function get_post_taxonomies_and_terms() {
190 190
 		$taxonomy_options = array();
191 191
 		$term_options     = array();
192
-		$taxonomies       = get_object_taxonomies( 'post', 'objects' );
192
+		$taxonomies       = get_object_taxonomies('post', 'objects');
193 193
 
194
-		foreach ( $taxonomies as $taxonomy ) {
194
+		foreach ($taxonomies as $taxonomy) {
195 195
 			array_push(
196 196
 				$taxonomy_options,
197 197
 				array(
@@ -202,17 +202,17 @@  discard block
 block discarded – undo
202 202
 			);
203 203
 		}
204 204
 		// Post type is also included in the list of taxonomies, so get the post type and merge with options.
205
-		$post_type_array =  self::get_post_type_key_and_value();
205
+		$post_type_array = self::get_post_type_key_and_value();
206 206
 		$post_type_option = $post_type_array['post_type_option_name'];
207 207
 		// Get also the list of post types from the post_type_array.
208 208
 		$post_type_option_values = $post_type_array['post_type_option_values'];
209 209
 
210
-        $post_taxonomy_array =  self::get_post_taxonomy_key_and_value();
210
+        $post_taxonomy_array = self::get_post_taxonomy_key_and_value();
211 211
         $post_taxonomy_option = $post_taxonomy_array['post_taxonomy_option_name'];
212 212
 
213 213
 		// Merge the post type option and post types in the taxonomy options
214
-		array_push( $taxonomy_options, $post_type_option, $post_taxonomy_option);
215
-		$term_options = array_merge( $term_options, $post_type_option_values );
214
+		array_push($taxonomy_options, $post_type_option, $post_taxonomy_option);
215
+		$term_options = array_merge($term_options, $post_type_option_values);
216 216
 		return array(
217 217
 			'taxonomy_options' => $taxonomy_options,
218 218
 			'term_options' => $term_options
@@ -225,8 +225,8 @@  discard block
 block discarded – undo
225 225
 	 * @return array Array of post_type_option and post_type_option_values.
226 226
 	 */
227 227
 	private static function get_post_type_key_and_value() {
228
-		$post_type_option_name   = array(
229
-			'label'      => __( 'Post type', 'wordlift' ),
228
+		$post_type_option_name = array(
229
+			'label'      => __('Post type', 'wordlift'),
230 230
 			'value'      => Wordlift\Mappings\Validators\Post_Type_Rule_Validator::POST_TYPE,
231 231
 			// Left empty since post types are provided locally.
232 232
 			'api_source' => '',
@@ -237,7 +237,7 @@  discard block
 block discarded – undo
237 237
 			'objects'
238 238
 		);
239 239
 
240
-		foreach ( $post_types as $post_type ) {
240
+		foreach ($post_types as $post_type) {
241 241
 			array_push(
242 242
 				$post_type_option_values,
243 243
 				array(
@@ -261,8 +261,8 @@  discard block
 block discarded – undo
261 261
      */
262 262
     private static function get_post_taxonomy_key_and_value() {
263 263
 
264
-        $post_taxonomy_option_name   = array(
265
-            'label'      => __( 'Post Taxonomy', 'wordlift' ),
264
+        $post_taxonomy_option_name = array(
265
+            'label'      => __('Post Taxonomy', 'wordlift'),
266 266
             'value'      => Wordlift\Mappings\Validators\Post_Taxonomy_Term_Rule_Validator::POST_TAXONOMY,
267 267
             // Left empty since post types are provided locally.
268 268
             'api_source' => 'taxonomy'
@@ -281,14 +281,14 @@  discard block
 block discarded – undo
281 281
 	 * @param array $edit_mapping_settings
282 282
 	 * @return array Loads the logic field options to the $edit_mapping_settings.
283 283
 	 */
284
-	private function load_logic_field_options( array $edit_mapping_settings ) {
284
+	private function load_logic_field_options(array $edit_mapping_settings) {
285 285
 		$edit_mapping_settings['wl_logic_field_options'] = array(
286 286
 			array(
287
-				'label' => __( 'is equal to', 'wordlift' ),
287
+				'label' => __('is equal to', 'wordlift'),
288 288
 				'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO,
289 289
 			),
290 290
 			array(
291
-				'label' => __( 'is not equal to', 'wordlift' ),
291
+				'label' => __('is not equal to', 'wordlift'),
292 292
 				'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO,
293 293
 			),
294 294
 		);
@@ -302,12 +302,12 @@  discard block
 block discarded – undo
302 302
 	 * @param array $edit_mapping_settings
303 303
 	 * @return array Edit mapping settings array with the mapping id if the nonce is valid.
304 304
 	 */
305
-	private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) {
305
+	private function validate_nonce_and_assign_mapping_id(array $edit_mapping_settings) {
306 306
         // We verify the nonce before making to load the edit mapping page for the wl_edit_mapping_id
307
-		if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] )
308
-		     && wp_verify_nonce( $_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce' ) ) {
307
+		if (isset($_REQUEST['_wl_edit_mapping_nonce'])
308
+		     && wp_verify_nonce($_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce')) {
309 309
 			// We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST.
310
-			$edit_mapping_settings['wl_edit_mapping_id'] = (int) filter_var( $_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT );
310
+			$edit_mapping_settings['wl_edit_mapping_id'] = (int) filter_var($_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT);
311 311
 		}
312 312
 
313 313
 		return $edit_mapping_settings;
@@ -319,13 +319,13 @@  discard block
 block discarded – undo
319 319
 	 * @param array $edit_mapping_settings
320 320
 	 * @return array
321 321
 	 */
322
-	private function load_rest_settings( array $edit_mapping_settings ) {
323
-		$edit_mapping_settings['rest_url']                   = get_rest_url(
322
+	private function load_rest_settings(array $edit_mapping_settings) {
323
+		$edit_mapping_settings['rest_url'] = get_rest_url(
324 324
 			null,
325
-			WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE
325
+			WL_REST_ROUTE_DEFAULT_NAMESPACE.Mappings_REST_Controller::MAPPINGS_NAMESPACE
326 326
 		);
327
-		$edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' );
328
-		$edit_mapping_settings                               = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings );
327
+		$edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce('wp_rest');
328
+		$edit_mapping_settings                               = $this->validate_nonce_and_assign_mapping_id($edit_mapping_settings);
329 329
 
330 330
 		return $edit_mapping_settings;
331 331
 	}
@@ -337,7 +337,7 @@  discard block
 block discarded – undo
337 337
 	 *
338 338
 	 * @return array Return the settings.
339 339
  	 */
340
-	private function load_rule_field_options( array $edit_mapping_settings ) {
340
+	private function load_rule_field_options(array $edit_mapping_settings) {
341 341
 		// Load the rule field options.
342 342
 		$rule_field_data                                    = self::get_post_taxonomies_and_terms();
343 343
 		$edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options'];
@@ -387,14 +387,14 @@  discard block
 block discarded – undo
387 387
 	 *
388 388
 	 * @return array
389 389
 	 */
390
-	private function load_field_type_and_name_options( array $edit_mapping_settings ) {
390
+	private function load_field_type_and_name_options(array $edit_mapping_settings) {
391 391
 		$all_field_name_options  = self::get_all_field_name_options();
392
-		$all_field_types_options = array_map( function ( $item ) {
392
+		$all_field_types_options = array_map(function($item) {
393 393
 			return array(
394 394
 				'label' => $item['label'],
395 395
 				'value' => $item['field_type'],
396 396
 			);
397
-		}, $all_field_name_options );
397
+		}, $all_field_name_options);
398 398
 
399 399
 		$edit_mapping_settings['wl_field_type_options'] = $all_field_types_options;
400 400
 		// Add wl_edit_field_name_options.
Please login to merge, or discard this patch.
wordlift/mappings/validators/class-post-taxonomy-term-rule-validator.php 2 patches
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -17,48 +17,48 @@  discard block
 block discarded – undo
17 17
  * @package Wordlift\Mappings\Validators
18 18
  */
19 19
 class Post_Taxonomy_Term_Rule_Validator implements Rule_Validator {
20
-	/**
21
-	 * @since 3.25.0
22
-	 * Enum for the post type rule validator.
23
-	 */
24
-	const POST_TAXONOMY = 'post_taxonomy';
25
-
26
-	/**
27
-	 * Post_Type_Rule_Validator constructor.
28
-	 *
29
-	 * When initializing the class hooks to `wl_mappings_rule_validators`.
30
-	 */
31
-	public function __construct() {
32
-
33
-		add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) );
34
-
35
-	}
36
-
37
-	/**
38
-	 * Hook to `wl_mappings_rule_validators` to register ourselves.
39
-	 *
40
-	 * @param array $value An array with validators.
41
-	 *
42
-	 * @return array An array with validators plus ours.
43
-	 */
44
-	public function wl_mappings_rule_validators( $value ) {
45
-
46
-		$value[ self::POST_TAXONOMY ] = $this;
47
-
48
-		return $value;
49
-	}
50
-
51
-	/**
52
-	 * {@inheritdoc}
53
-	 */
54
-	public function get_label() {
55
-		return __( 'Post Taxonomy', 'wordlift' );
56
-	}
57
-
58
-	/**
59
-	 * {@inheritdoc}
60
-	 */
61
-	public function is_valid ( $identifier, $operator, $operand_1, $operand_2, $type ) {
20
+    /**
21
+     * @since 3.25.0
22
+     * Enum for the post type rule validator.
23
+     */
24
+    const POST_TAXONOMY = 'post_taxonomy';
25
+
26
+    /**
27
+     * Post_Type_Rule_Validator constructor.
28
+     *
29
+     * When initializing the class hooks to `wl_mappings_rule_validators`.
30
+     */
31
+    public function __construct() {
32
+
33
+        add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) );
34
+
35
+    }
36
+
37
+    /**
38
+     * Hook to `wl_mappings_rule_validators` to register ourselves.
39
+     *
40
+     * @param array $value An array with validators.
41
+     *
42
+     * @return array An array with validators plus ours.
43
+     */
44
+    public function wl_mappings_rule_validators( $value ) {
45
+
46
+        $value[ self::POST_TAXONOMY ] = $this;
47
+
48
+        return $value;
49
+    }
50
+
51
+    /**
52
+     * {@inheritdoc}
53
+     */
54
+    public function get_label() {
55
+        return __( 'Post Taxonomy', 'wordlift' );
56
+    }
57
+
58
+    /**
59
+     * {@inheritdoc}
60
+     */
61
+    public function is_valid ( $identifier, $operator, $operand_1, $operand_2, $type ) {
62 62
         $taxonomy  = $operand_1;
63 63
         $term_slug = $operand_2;
64 64
 
@@ -88,5 +88,5 @@  discard block
 block discarded – undo
88 88
         }
89 89
 
90 90
         return true;
91
-	}
91
+    }
92 92
 }
Please login to merge, or discard this patch.
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -30,7 +30,7 @@  discard block
 block discarded – undo
30 30
 	 */
31 31
 	public function __construct() {
32 32
 
33
-		add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) );
33
+		add_filter('wl_mappings_rule_validators', array($this, 'wl_mappings_rule_validators'));
34 34
 
35 35
 	}
36 36
 
@@ -41,9 +41,9 @@  discard block
 block discarded – undo
41 41
 	 *
42 42
 	 * @return array An array with validators plus ours.
43 43
 	 */
44
-	public function wl_mappings_rule_validators( $value ) {
44
+	public function wl_mappings_rule_validators($value) {
45 45
 
46
-		$value[ self::POST_TAXONOMY ] = $this;
46
+		$value[self::POST_TAXONOMY] = $this;
47 47
 
48 48
 		return $value;
49 49
 	}
@@ -52,39 +52,39 @@  discard block
 block discarded – undo
52 52
 	 * {@inheritdoc}
53 53
 	 */
54 54
 	public function get_label() {
55
-		return __( 'Post Taxonomy', 'wordlift' );
55
+		return __('Post Taxonomy', 'wordlift');
56 56
 	}
57 57
 
58 58
 	/**
59 59
 	 * {@inheritdoc}
60 60
 	 */
61
-	public function is_valid ( $identifier, $operator, $operand_1, $operand_2, $type ) {
61
+	public function is_valid($identifier, $operator, $operand_1, $operand_2, $type) {
62 62
         $taxonomy  = $operand_1;
63 63
         $term_slug = $operand_2;
64 64
 
65
-        $current_term = get_term( $identifier );
66
-        $terms = get_terms( $taxonomy, array( 'get' => 'all' ) );
65
+        $current_term = get_term($identifier);
66
+        $terms = get_terms($taxonomy, array('get' => 'all'));
67 67
 
68
-        $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug );
68
+        $is_object_in_term = is_object_in_term($identifier, $taxonomy, $term_slug);
69 69
 
70
-        $taxonomy = get_taxonomy( $term_slug );
70
+        $taxonomy = get_taxonomy($term_slug);
71 71
 
72
-        if (!in_array('post', $taxonomy->object_type )) {
72
+        if ( ! in_array('post', $taxonomy->object_type)) {
73 73
             return false;
74 74
         }
75 75
 
76
-        if ( is_wp_error( $is_object_in_term ) ) {
76
+        if (is_wp_error($is_object_in_term)) {
77 77
             return false;
78 78
         }
79 79
 
80
-        if ( $operator === Rule_Validator::IS_EQUAL_TO ) {
80
+        if ($operator === Rule_Validator::IS_EQUAL_TO) {
81 81
             // If we are in term page, then we need to check if the current
82 82
             // term belongs to the taxonomy
83
-            return in_array( $current_term->term_id, $terms );
83
+            return in_array($current_term->term_id, $terms);
84 84
         }
85 85
 
86
-        if ( $operator === Rule_Validator::IS_NOT_EQUAL_TO ) {
87
-            return ! in_array( $current_term->term_id, $terms );
86
+        if ($operator === Rule_Validator::IS_NOT_EQUAL_TO) {
87
+            return ! in_array($current_term->term_id, $terms);
88 88
         }
89 89
 
90 90
         return true;
Please login to merge, or discard this patch.