Completed
Push — develop ( da4181...8a2a4a )
by Naveen
26s queued 10s
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   +108 added lines, -108 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,11 +164,11 @@  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
             $taxonomy_config = array(
173 173
                 'taxonomy' => $post_taxonomy->name,
174 174
                 'hide_empty' => false
@@ -176,7 +176,7 @@  discard block
 block discarded – undo
176 176
 
177 177
             $total_terms = wp_count_terms($taxonomy_config);
178 178
 
179
-            $post_taxonomy_terms = get_terms( $taxonomy_config );
179
+            $post_taxonomy_terms = get_terms($taxonomy_config);
180 180
 
181 181
             if ($total_terms) {
182 182
                 $group_taxonomy = array('parentValue' => 'post_taxonomy', 'group_name' => $post_taxonomy->label, 'group_options' => array());
@@ -184,7 +184,7 @@  discard block
 block discarded – undo
184 184
                 foreach ($post_taxonomy_terms as $post_taxonomy_term) {
185 185
                     array_push($group_taxonomy['group_options'],
186 186
                         array(
187
-                            'label' => ' - ' . $post_taxonomy_term->name,
187
+                            'label' => ' - '.$post_taxonomy_term->name,
188 188
                             'value' => $post_taxonomy_term->slug,
189 189
                             'taxonomy' => 'post_taxonomy',
190 190
                         )
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
 
198 198
                         array_push($group_taxonomy['group_options'],
199 199
                             array(
200
-                                'label' => ' -- ' . $child_term->name,
200
+                                'label' => ' -- '.$child_term->name,
201 201
                                 'value' => $child_term->slug,
202 202
                                 'taxonomy' => 'post_taxonomy',
203 203
                             )
@@ -218,21 +218,21 @@  discard block
 block discarded – undo
218 218
 	 *
219 219
 	 * @return array The array of the terms for the taxonomy.
220 220
 	 */
221
-	public static function get_terms_for_the_posted_taxonomy( $request ) {
221
+	public static function get_terms_for_the_posted_taxonomy($request) {
222 222
 		$post_data = $request->get_params();
223
-		if ( ! array_key_exists( 'taxonomy', $post_data ) ) {
223
+		if ( ! array_key_exists('taxonomy', $post_data)) {
224 224
 			return array(
225 225
 				'status'  => 'failure',
226
-				'message' => __( 'Request not valid, must post a taxonomy to get terms', 'wordlift' )
226
+				'message' => __('Request not valid, must post a taxonomy to get terms', 'wordlift')
227 227
 			);
228 228
 		} else {
229 229
 			$taxonomy = $post_data['taxonomy'];
230
-			$terms    = get_terms( $taxonomy, array( 'hide_empty' => false, ) );
231
-			if ( is_wp_error( $terms ) ) {
230
+			$terms    = get_terms($taxonomy, array('hide_empty' => false,));
231
+			if (is_wp_error($terms)) {
232 232
 				// Return error response, if the taxonomy is not valid.
233 233
 				return array(
234 234
 					'status'  => 'failure',
235
-					'message' => __( 'Request not valid, must post a valid taxonomy', 'wordlift' )
235
+					'message' => __('Request not valid, must post a valid taxonomy', 'wordlift')
236 236
 				);
237 237
 			}
238 238
 
@@ -247,43 +247,43 @@  discard block
 block discarded – undo
247 247
 	 *
248 248
 	 * @return array
249 249
 	 */
250
-	public static function clone_mapping_items( $request ) {
250
+	public static function clone_mapping_items($request) {
251 251
 		$dbo           = new Mappings_DBO();
252 252
 		$post_data     = (array) $request->get_params();
253 253
 		$mapping_items = (array) $post_data['mapping_items'];
254
-		foreach ( $mapping_items as $mapping_item ) {
254
+		foreach ($mapping_items as $mapping_item) {
255 255
 			$mapping_id = (int) $mapping_item['mapping_id'];
256 256
 			// Clone the current mapping item.
257
-			$cloned_mapping_id = $dbo->insert_mapping_item( $mapping_item['mapping_title'] );
257
+			$cloned_mapping_id = $dbo->insert_mapping_item($mapping_item['mapping_title']);
258 258
 			// Clone all the rule groups.
259
-			$rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping( $mapping_id );
259
+			$rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping($mapping_id);
260 260
 			// Clone all the properties.
261
-			$properties_to_be_cloned = $dbo->get_properties( $mapping_id );
262
-			foreach ( $properties_to_be_cloned as $property ) {
261
+			$properties_to_be_cloned = $dbo->get_properties($mapping_id);
262
+			foreach ($properties_to_be_cloned as $property) {
263 263
 				// Assign a new mapping id.
264 264
 				$property['mapping_id'] = $cloned_mapping_id;
265 265
 				// Removing this property id, since a new id needed to be created for
266 266
 				// new property.
267
-				unset( $property['property_id'] );
268
-				$dbo->insert_or_update_property( $property );
267
+				unset($property['property_id']);
268
+				$dbo->insert_or_update_property($property);
269 269
 			}
270 270
 			// Loop through the rule groups and insert them in table with the mapping id.
271
-			foreach ( $rule_groups_to_be_cloned as $rule_group ) {
272
-				$cloned_rule_group_id = $dbo->insert_rule_group( $cloned_mapping_id );
271
+			foreach ($rule_groups_to_be_cloned as $rule_group) {
272
+				$cloned_rule_group_id = $dbo->insert_rule_group($cloned_mapping_id);
273 273
 				$original_rules       = (array) $rule_group['rules'];
274 274
 				// Now we need to insert these rules for the cloned rule group id.
275
-				foreach ( $original_rules as $clone_rule ) {
275
+				foreach ($original_rules as $clone_rule) {
276 276
 					// We should replace only rule group id in the cloned rules.
277 277
 					$clone_rule['rule_group_id'] = (int) $cloned_rule_group_id;
278
-					unset( $clone_rule['rule_id'] );
279
-					$dbo->insert_or_update_rule_item( $clone_rule );
278
+					unset($clone_rule['rule_id']);
279
+					$dbo->insert_or_update_rule_item($clone_rule);
280 280
 				}
281 281
 			}
282 282
 		}
283 283
 
284 284
 		return array(
285 285
 			'status'  => 'success',
286
-			'message' => __( 'Successfully cloned mapping items', 'wordlift' ),
286
+			'message' => __('Successfully cloned mapping items', 'wordlift'),
287 287
 		);
288 288
 	}
289 289
 
@@ -294,23 +294,23 @@  discard block
 block discarded – undo
294 294
 	 *
295 295
 	 * @return array
296 296
 	 */
297
-	public static function update_mapping_items( $request ) {
297
+	public static function update_mapping_items($request) {
298 298
 		$dbo       = new Mappings_DBO();
299 299
 		$post_data = $request->get_params();
300
-		if ( array_key_exists( 'mapping_items', $post_data ) ) {
300
+		if (array_key_exists('mapping_items', $post_data)) {
301 301
 			$mapping_items = (array) $post_data['mapping_items'];
302
-			foreach ( $mapping_items as $mapping_item ) {
303
-				$dbo->insert_or_update_mapping_item( $mapping_item );
302
+			foreach ($mapping_items as $mapping_item) {
303
+				$dbo->insert_or_update_mapping_item($mapping_item);
304 304
 			}
305 305
 
306 306
 			return array(
307 307
 				'status'  => 'success',
308
-				'message' => __( 'Mapping items successfully updated', 'wordlift' ),
308
+				'message' => __('Mapping items successfully updated', 'wordlift'),
309 309
 			);
310 310
 		} else {
311 311
 			return array(
312 312
 				'status'  => 'failure',
313
-				'message' => __( 'Unable to update mapping item', 'wordlift' ),
313
+				'message' => __('Unable to update mapping item', 'wordlift'),
314 314
 			);
315 315
 		}
316 316
 	}
@@ -322,23 +322,23 @@  discard block
 block discarded – undo
322 322
 	 *
323 323
 	 * @return array
324 324
 	 */
325
-	public static function delete_mapping_items( $request ) {
325
+	public static function delete_mapping_items($request) {
326 326
 		$dbo       = new Mappings_DBO();
327 327
 		$post_data = $request->get_params();
328
-		if ( array_key_exists( 'mapping_items', $post_data ) ) {
328
+		if (array_key_exists('mapping_items', $post_data)) {
329 329
 			$mapping_items = (array) $post_data['mapping_items'];
330
-			foreach ( $mapping_items as $mapping_item ) {
331
-				$dbo->delete_mapping_item( $mapping_item['mapping_id'] );
330
+			foreach ($mapping_items as $mapping_item) {
331
+				$dbo->delete_mapping_item($mapping_item['mapping_id']);
332 332
 			}
333 333
 
334 334
 			return array(
335 335
 				'status'  => 'success',
336
-				'message' => __( 'successfully deleted mapping items', 'wordlift' )
336
+				'message' => __('successfully deleted mapping items', 'wordlift')
337 337
 			);
338 338
 		} else {
339 339
 			return array(
340 340
 				'status'  => 'failure',
341
-				'message' => __( 'Unable to delete mapping items', 'wordlift' )
341
+				'message' => __('Unable to delete mapping items', 'wordlift')
342 342
 			);
343 343
 		}
344 344
 	}
@@ -350,7 +350,7 @@  discard block
 block discarded – undo
350 350
 	 *
351 351
 	 * @return array
352 352
 	 */
353
-	public static function list_mapping_items( $request ) {
353
+	public static function list_mapping_items($request) {
354 354
 		$dbo = new Mappings_DBO();
355 355
 
356 356
 		return $dbo->get_mappings();
@@ -364,11 +364,11 @@  discard block
 block discarded – undo
364 364
 	 *
365 365
 	 * @return array A list of rule ids.
366 366
 	 */
367
-	private static function get_rule_ids( $dbo, $rule_group_id ) {
368
-		$rule_rows_in_db = $dbo->get_rules_by_rule_group( $rule_group_id );
367
+	private static function get_rule_ids($dbo, $rule_group_id) {
368
+		$rule_rows_in_db = $dbo->get_rules_by_rule_group($rule_group_id);
369 369
 		$rule_ids        = array();
370
-		foreach ( $rule_rows_in_db as $rule_row ) {
371
-			array_push( $rule_ids, (int) $rule_row['rule_id'] );
370
+		foreach ($rule_rows_in_db as $rule_row) {
371
+			array_push($rule_ids, (int) $rule_row['rule_id']);
372 372
 		}
373 373
 
374 374
 		return $rule_ids;
@@ -383,27 +383,27 @@  discard block
 block discarded – undo
383 383
 	 *
384 384
 	 * @return void
385 385
 	 */
386
-	private static function save_rules( $dbo, $rule_group_id, $rule_list ) {
387
-		$rule_ids = self::get_rule_ids( $dbo, $rule_group_id );
388
-		foreach ( $rule_list as $rule ) {
386
+	private static function save_rules($dbo, $rule_group_id, $rule_list) {
387
+		$rule_ids = self::get_rule_ids($dbo, $rule_group_id);
388
+		foreach ($rule_list as $rule) {
389 389
 			// Some rules may not have rule group id, because they are inserted
390 390
 			// in ui, so lets add them any way.
391 391
 			$rule['rule_group_id'] = $rule_group_id;
392
-			$dbo->insert_or_update_rule_item( $rule );
393
-			if ( array_key_exists( 'rule_id', $rule ) ) {
392
+			$dbo->insert_or_update_rule_item($rule);
393
+			if (array_key_exists('rule_id', $rule)) {
394 394
 				$index_to_be_removed = array_search(
395 395
 					(int) $rule['rule_id'],
396 396
 					$rule_ids,
397 397
 					true
398 398
 				);
399
-				if ( false !== $index_to_be_removed ) {
400
-					unset( $rule_ids[ $index_to_be_removed ] );
399
+				if (false !== $index_to_be_removed) {
400
+					unset($rule_ids[$index_to_be_removed]);
401 401
 				}
402 402
 			}
403 403
 		}
404
-		foreach ( $rule_ids as $rule_id ) {
404
+		foreach ($rule_ids as $rule_id) {
405 405
 			// Delete all the rule ids which are not posted.
406
-			$dbo->delete_rule_item( $rule_id );
406
+			$dbo->delete_rule_item($rule_id);
407 407
 		}
408 408
 	}
409 409
 
@@ -416,14 +416,14 @@  discard block
 block discarded – undo
416 416
 	 *
417 417
 	 * @return void
418 418
 	 */
419
-	private static function save_property_list( $dbo, $mapping_id, $property_list ) {
420
-		$properties_needed_to_be_deleted = $dbo->get_properties( $mapping_id );
419
+	private static function save_property_list($dbo, $mapping_id, $property_list) {
420
+		$properties_needed_to_be_deleted = $dbo->get_properties($mapping_id);
421 421
 		$property_ids                    = array();
422
-		foreach ( $properties_needed_to_be_deleted as $property ) {
423
-			array_push( $property_ids, (int) $property['property_id'] );
422
+		foreach ($properties_needed_to_be_deleted as $property) {
423
+			array_push($property_ids, (int) $property['property_id']);
424 424
 		}
425
-		foreach ( $property_list as $property ) {
426
-			if ( array_key_exists( 'property_id', $property ) ) {
425
+		foreach ($property_list as $property) {
426
+			if (array_key_exists('property_id', $property)) {
427 427
 				// Remove the id from the list of property ids needed to be deleted
428 428
 				// because it is posted.
429 429
 				$index_to_be_removed = array_search(
@@ -431,18 +431,18 @@  discard block
 block discarded – undo
431 431
 					$property_ids,
432 432
 					true
433 433
 				);
434
-				if ( false !== $index_to_be_removed ) {
435
-					unset( $property_ids[ $index_to_be_removed ] );
434
+				if (false !== $index_to_be_removed) {
435
+					unset($property_ids[$index_to_be_removed]);
436 436
 				}
437 437
 			}
438 438
 			// Add mapping id to property data.
439 439
 			$property['mapping_id'] = $mapping_id;
440
-			$dbo->insert_or_update_property( $property );
440
+			$dbo->insert_or_update_property($property);
441 441
 
442 442
 		}
443 443
 		// At the end remove all the property ids which are not posted.
444
-		foreach ( $property_ids as $property_id ) {
445
-			$dbo->delete_property( $property_id );
444
+		foreach ($property_ids as $property_id) {
445
+			$dbo->delete_property($property_id);
446 446
 		}
447 447
 
448 448
 	}
@@ -455,11 +455,11 @@  discard block
 block discarded – undo
455 455
 	 *
456 456
 	 * @return array $rule_group_ids A list of rule group ids.
457 457
 	 */
458
-	private static function get_rule_group_ids( $dbo, $mapping_id ) {
459
-		$rule_group_rows = $dbo->get_rule_group_list( $mapping_id );
458
+	private static function get_rule_group_ids($dbo, $mapping_id) {
459
+		$rule_group_rows = $dbo->get_rule_group_list($mapping_id);
460 460
 		$rule_group_ids  = array();
461
-		foreach ( $rule_group_rows as $rule_group_row ) {
462
-			array_push( $rule_group_ids, (int) $rule_group_row['rule_group_id'] );
461
+		foreach ($rule_group_rows as $rule_group_row) {
462
+			array_push($rule_group_ids, (int) $rule_group_row['rule_group_id']);
463 463
 		}
464 464
 
465 465
 		return $rule_group_ids;
@@ -474,31 +474,31 @@  discard block
 block discarded – undo
474 474
 	 *
475 475
 	 * @return void
476 476
 	 */
477
-	private static function save_rule_group_list( $dbo, $mapping_id, $rule_group_list ) {
477
+	private static function save_rule_group_list($dbo, $mapping_id, $rule_group_list) {
478 478
 		// The rule groups not posted should be deleted.
479
-		$rule_group_ids = self::get_rule_group_ids( $dbo, $mapping_id );
479
+		$rule_group_ids = self::get_rule_group_ids($dbo, $mapping_id);
480 480
 		// Loop through rule group list and save the rule group.
481
-		foreach ( $rule_group_list as $rule_group ) {
482
-			if ( array_key_exists( 'rule_group_id', $rule_group ) ) {
481
+		foreach ($rule_group_list as $rule_group) {
482
+			if (array_key_exists('rule_group_id', $rule_group)) {
483 483
 				$rule_group_id = $rule_group['rule_group_id'];
484 484
 			} else {
485 485
 				// New rule group, should create new rule group id.
486
-				$rule_group_id = $dbo->insert_rule_group( $mapping_id );
486
+				$rule_group_id = $dbo->insert_rule_group($mapping_id);
487 487
 			}
488 488
 			$index_to_be_removed = array_search(
489 489
 				(int) $rule_group_id,
490 490
 				$rule_group_ids,
491 491
 				true
492 492
 			);
493
-			if ( false !== $index_to_be_removed ) {
494
-				unset( $rule_group_ids[ $index_to_be_removed ] );
493
+			if (false !== $index_to_be_removed) {
494
+				unset($rule_group_ids[$index_to_be_removed]);
495 495
 			}
496
-			self::save_rules( $dbo, $rule_group_id, $rule_group['rules'] );
496
+			self::save_rules($dbo, $rule_group_id, $rule_group['rules']);
497 497
 		}
498 498
 
499 499
 		// Remove all the rule groups which are not posted.
500
-		foreach ( $rule_group_ids as $rule_group_id ) {
501
-			$dbo->delete_rule_group_item( $rule_group_id );
500
+		foreach ($rule_group_ids as $rule_group_id) {
501
+			$dbo->delete_rule_group_item($rule_group_id);
502 502
 		}
503 503
 	}
504 504
 
@@ -509,33 +509,33 @@  discard block
 block discarded – undo
509 509
 	 *
510 510
 	 * @return array
511 511
 	 */
512
-	public static function insert_or_update_mapping_item( $request ) {
512
+	public static function insert_or_update_mapping_item($request) {
513 513
 		$post_data = $request->get_params() === null ? array() : $request->get_params();
514 514
 		$dbo       = new Mappings_DBO();
515 515
 		// check if valid object is posted.
516
-		if ( array_key_exists( 'mapping_title', $post_data ) &&
517
-		     array_key_exists( 'rule_group_list', $post_data ) &&
518
-		     array_key_exists( 'property_list', $post_data ) ) {
516
+		if (array_key_exists('mapping_title', $post_data) &&
517
+		     array_key_exists('rule_group_list', $post_data) &&
518
+		     array_key_exists('property_list', $post_data)) {
519 519
 			// Do validation, remove all incomplete data.
520 520
 			$mapping_item = array();
521
-			if ( array_key_exists( 'mapping_id', $post_data ) ) {
521
+			if (array_key_exists('mapping_id', $post_data)) {
522 522
 				$mapping_item['mapping_id'] = $post_data['mapping_id'];
523 523
 			}
524 524
 			$mapping_item['mapping_title'] = $post_data['mapping_title'];
525 525
 			// lets save the mapping item.
526
-			$mapping_id = $dbo->insert_or_update_mapping_item( $mapping_item );
527
-			self::save_rule_group_list( $dbo, $mapping_id, $post_data['rule_group_list'] );
528
-			self::save_property_list( $dbo, $mapping_id, $post_data['property_list'] );
526
+			$mapping_id = $dbo->insert_or_update_mapping_item($mapping_item);
527
+			self::save_rule_group_list($dbo, $mapping_id, $post_data['rule_group_list']);
528
+			self::save_property_list($dbo, $mapping_id, $post_data['property_list']);
529 529
 
530 530
 			return array(
531 531
 				'status'     => 'success',
532
-				'message'    => __( 'Successfully saved mapping item', 'wordlift' ),
532
+				'message'    => __('Successfully saved mapping item', 'wordlift'),
533 533
 				'mapping_id' => (int) $mapping_id,
534 534
 			);
535 535
 		} else {
536 536
 			return array(
537 537
 				'status'  => 'error',
538
-				'message' => __( 'Unable to save mapping item', 'wordlift' ),
538
+				'message' => __('Unable to save mapping item', 'wordlift'),
539 539
 			);
540 540
 		}
541 541
 	}
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
             'api_source' => 'post_taxonomy'
268 268
         );
@@ -280,14 +280,14 @@  discard block
 block discarded – undo
280 280
 	 * @param array $edit_mapping_settings
281 281
 	 * @return array Loads the logic field options to the $edit_mapping_settings.
282 282
 	 */
283
-	private function load_logic_field_options( array $edit_mapping_settings ) {
283
+	private function load_logic_field_options(array $edit_mapping_settings) {
284 284
 		$edit_mapping_settings['wl_logic_field_options'] = array(
285 285
 			array(
286
-				'label' => __( 'is equal to', 'wordlift' ),
286
+				'label' => __('is equal to', 'wordlift'),
287 287
 				'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_EQUAL_TO,
288 288
 			),
289 289
 			array(
290
-				'label' => __( 'is not equal to', 'wordlift' ),
290
+				'label' => __('is not equal to', 'wordlift'),
291 291
 				'value' => Wordlift\Mappings\Validators\Rule_Validator::IS_NOT_EQUAL_TO,
292 292
 			),
293 293
 		);
@@ -301,12 +301,12 @@  discard block
 block discarded – undo
301 301
 	 * @param array $edit_mapping_settings
302 302
 	 * @return array Edit mapping settings array with the mapping id if the nonce is valid.
303 303
 	 */
304
-	private function validate_nonce_and_assign_mapping_id( array $edit_mapping_settings ) {
304
+	private function validate_nonce_and_assign_mapping_id(array $edit_mapping_settings) {
305 305
         // We verify the nonce before making to load the edit mapping page for the wl_edit_mapping_id
306
-		if ( isset( $_REQUEST['_wl_edit_mapping_nonce'] )
307
-		     && wp_verify_nonce( $_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce' ) ) {
306
+		if (isset($_REQUEST['_wl_edit_mapping_nonce'])
307
+		     && wp_verify_nonce($_REQUEST['_wl_edit_mapping_nonce'], 'wl-edit-mapping-nonce')) {
308 308
 			// We're using `INPUT_GET` here because this is a link from the UI, i.e. no POST.
309
-			$edit_mapping_settings['wl_edit_mapping_id'] = (int) filter_var( $_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT );
309
+			$edit_mapping_settings['wl_edit_mapping_id'] = (int) filter_var($_REQUEST['wl_edit_mapping_id'], FILTER_VALIDATE_INT);
310 310
 		}
311 311
 
312 312
 		return $edit_mapping_settings;
@@ -318,13 +318,13 @@  discard block
 block discarded – undo
318 318
 	 * @param array $edit_mapping_settings
319 319
 	 * @return array
320 320
 	 */
321
-	private function load_rest_settings( array $edit_mapping_settings ) {
322
-		$edit_mapping_settings['rest_url']                   = get_rest_url(
321
+	private function load_rest_settings(array $edit_mapping_settings) {
322
+		$edit_mapping_settings['rest_url'] = get_rest_url(
323 323
 			null,
324
-			WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE
324
+			WL_REST_ROUTE_DEFAULT_NAMESPACE.Mappings_REST_Controller::MAPPINGS_NAMESPACE
325 325
 		);
326
-		$edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce( 'wp_rest' );
327
-		$edit_mapping_settings                               = $this->validate_nonce_and_assign_mapping_id( $edit_mapping_settings );
326
+		$edit_mapping_settings['wl_edit_mapping_rest_nonce'] = wp_create_nonce('wp_rest');
327
+		$edit_mapping_settings                               = $this->validate_nonce_and_assign_mapping_id($edit_mapping_settings);
328 328
 
329 329
 		return $edit_mapping_settings;
330 330
 	}
@@ -336,7 +336,7 @@  discard block
 block discarded – undo
336 336
 	 *
337 337
 	 * @return array Return the settings.
338 338
  	 */
339
-	private function load_rule_field_options( array $edit_mapping_settings ) {
339
+	private function load_rule_field_options(array $edit_mapping_settings) {
340 340
 		// Load the rule field options.
341 341
 		$rule_field_data                                    = self::get_post_taxonomies_and_terms();
342 342
 		$edit_mapping_settings['wl_rule_field_one_options'] = $rule_field_data['taxonomy_options'];
@@ -386,14 +386,14 @@  discard block
 block discarded – undo
386 386
 	 *
387 387
 	 * @return array
388 388
 	 */
389
-	private function load_field_type_and_name_options( array $edit_mapping_settings ) {
389
+	private function load_field_type_and_name_options(array $edit_mapping_settings) {
390 390
 		$all_field_name_options  = self::get_all_field_name_options();
391
-		$all_field_types_options = array_map( function ( $item ) {
391
+		$all_field_types_options = array_map(function($item) {
392 392
 			return array(
393 393
 				'label' => $item['label'],
394 394
 				'value' => $item['field_type'],
395 395
 			);
396
-		}, $all_field_name_options );
396
+		}, $all_field_name_options);
397 397
 
398 398
 		$edit_mapping_settings['wl_field_type_options'] = $all_field_types_options;
399 399
 		// Add wl_edit_field_name_options.
Please login to merge, or discard this patch.
wordlift/dataset/background/class-sync-background-process-stopped-state.php 3 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -9,6 +9,9 @@
 block discarded – undo
9 9
 	 */
10 10
 	private $context;
11 11
 
12
+	/**
13
+	 * @param Sync_Background_Process $context
14
+	 */
12 15
 	function __construct( $context ) {
13 16
 		parent::__construct( Sync_Background_Process::STATE_STOPPED );
14 17
 
Please login to merge, or discard this patch.
Indentation   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -4,30 +4,30 @@
 block discarded – undo
4 4
 
5 5
 class Sync_Background_Process_Stopped_State extends Abstract_Sync_Background_Process_State {
6 6
 
7
-	/**
8
-	 * @var Sync_Background_Process
9
-	 */
10
-	private $context;
7
+    /**
8
+     * @var Sync_Background_Process
9
+     */
10
+    private $context;
11 11
 
12
-	function __construct( $context ) {
13
-		parent::__construct( Sync_Background_Process::STATE_STOPPED );
12
+    function __construct( $context ) {
13
+        parent::__construct( Sync_Background_Process::STATE_STOPPED );
14 14
 
15
-		$this->context = $context;
16
-	}
15
+        $this->context = $context;
16
+    }
17 17
 
18
-	function enter() {
19
-		$this->context->set_state( Sync_Background_Process::STATE_STOPPED );
20
-	}
18
+    function enter() {
19
+        $this->context->set_state( Sync_Background_Process::STATE_STOPPED );
20
+    }
21 21
 
22
-	function leave() {
23
-		$this->context->set_state( null );
24
-	}
22
+    function leave() {
23
+        $this->context->set_state( null );
24
+    }
25 25
 
26
-	function task( $item ) {
26
+    function task( $item ) {
27 27
 
28
-		$this->context->cancel_process();
28
+        $this->context->cancel_process();
29 29
 
30
-		return false;
31
-	}
30
+        return false;
31
+    }
32 32
 
33 33
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -9,21 +9,21 @@
 block discarded – undo
9 9
 	 */
10 10
 	private $context;
11 11
 
12
-	function __construct( $context ) {
13
-		parent::__construct( Sync_Background_Process::STATE_STOPPED );
12
+	function __construct($context) {
13
+		parent::__construct(Sync_Background_Process::STATE_STOPPED);
14 14
 
15 15
 		$this->context = $context;
16 16
 	}
17 17
 
18 18
 	function enter() {
19
-		$this->context->set_state( Sync_Background_Process::STATE_STOPPED );
19
+		$this->context->set_state(Sync_Background_Process::STATE_STOPPED);
20 20
 	}
21 21
 
22 22
 	function leave() {
23
-		$this->context->set_state( null );
23
+		$this->context->set_state(null);
24 24
 	}
25 25
 
26
-	function task( $item ) {
26
+	function task($item) {
27 27
 
28 28
 		$this->context->cancel_process();
29 29
 
Please login to merge, or discard this patch.
src/wordlift/cache/class-ttl-cache.php 2 patches
Indentation   +157 added lines, -157 removed lines patch added patch discarded remove patch
@@ -17,177 +17,177 @@
 block discarded – undo
17 17
 // @@todo: add a hook to clear the cached files now and then.
18 18
 class Ttl_Cache {
19 19
 
20
-	/**
21
-	 * The cache name.
22
-	 *
23
-	 * @var string $name The cache name.
24
-	 * @access private
25
-	 * @since 3.21.2
26
-	 */
27
-	private $name;
28
-
29
-	/**
30
-	 * The TTL of cached responses in seconds.
31
-	 *
32
-	 * @var int $ttl The TTL in seconds.
33
-	 * @access private
34
-	 * @since 3.21.2
35
-	 */
36
-	private $ttl;
37
-
38
-	/**
39
-	 * The cache dir where the cached data is written.
40
-	 *
41
-	 * @since 3.21.2
42
-	 * @access private
43
-	 * @var string $cache_dir The cache dir where the cached responses are written.
44
-	 */
45
-	private $cache_dir;
46
-
47
-	/**
48
-	 * A {@link Wordlift_Log_Service} instance.
49
-	 *
50
-	 * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
51
-	 * @access private
52
-	 * @since 3.21.2
53
-	 */
54
-	private $log;
55
-
56
-	/**
57
-	 * @var array
58
-	 */
59
-	private static $caches = array();
60
-
61
-	/**
62
-	 * Create a {@link Ttl_Cache} with the specified TTL, default 900 secs.
63
-	 *
64
-	 * @param string $name The cache name.
65
-	 * @param int $ttl The cache TTL, default 900 secs.
66
-	 *
67
-	 * @since 3.21.2
68
-	 */
69
-	public function __construct( $name, $ttl = 900 ) {
70
-
71
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
72
-
73
-		$this->name = $name;
74
-		$this->ttl  = $ttl;
75
-
76
-		$this->cache_dir = self::get_cache_folder() . DIRECTORY_SEPARATOR . md5( $name );
77
-
78
-		$this->log->trace( "Creating the cache folder {$this->cache_dir}..." );
79
-		wp_mkdir_p( $this->cache_dir );
80
-
81
-		self::$caches[ $name ] = $this;
82
-
83
-	}
84
-
85
-	/**
86
-	 * Get the root cache folder.
87
-	 *
88
-	 * This is useful to introduce a cache cleaning procedure which will scan and delete older stale cache files.
89
-	 *
90
-	 * @return string The root cache folder.
91
-	 * @since 3.22.5
92
-	 */
93
-	public static function get_cache_folder() {
94
-
95
-		// Get the temp dir and add the directory separator if missing.
96
-		$temp_dir = get_temp_dir();
97
-		if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) {
98
-			$temp_dir .= DIRECTORY_SEPARATOR;
99
-		}
100
-
101
-		return $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( home_url() );
102
-	}
103
-
104
-	/**
105
-	 * Get the cached data for the specified key.
106
-	 *
107
-	 * @param mixed $key A serializable key.
108
-	 *
109
-	 * @return mixed|null
110
-	 * @since 3.21.2
111
-	 */
112
-	public function get( $key ) {
113
-
114
-		$filename = $this->get_filename( $key );
115
-
116
-		// If the cache file exists and it's not too old, then return it.
117
-		if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) {
118
-			$this->log->trace( "Cache HIT.\n" );
119
-
120
-			return json_decode( file_get_contents( $filename ), true );
121
-		}
122
-
123
-		$this->log->trace( "Cache MISS, filename $filename.\n" );
124
-
125
-		return null;
126
-	}
127
-
128
-	public function put( $key, $data ) {
129
-
130
-		$filename = $this->get_filename( $key );
131
-
132
-		// Cache.
133
-		if ( file_exists( $filename ) ) {
134
-			@unlink( $filename );
135
-		}
136
-		@file_put_contents( $filename, wp_json_encode( $data ) );
20
+    /**
21
+     * The cache name.
22
+     *
23
+     * @var string $name The cache name.
24
+     * @access private
25
+     * @since 3.21.2
26
+     */
27
+    private $name;
28
+
29
+    /**
30
+     * The TTL of cached responses in seconds.
31
+     *
32
+     * @var int $ttl The TTL in seconds.
33
+     * @access private
34
+     * @since 3.21.2
35
+     */
36
+    private $ttl;
37
+
38
+    /**
39
+     * The cache dir where the cached data is written.
40
+     *
41
+     * @since 3.21.2
42
+     * @access private
43
+     * @var string $cache_dir The cache dir where the cached responses are written.
44
+     */
45
+    private $cache_dir;
46
+
47
+    /**
48
+     * A {@link Wordlift_Log_Service} instance.
49
+     *
50
+     * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
51
+     * @access private
52
+     * @since 3.21.2
53
+     */
54
+    private $log;
55
+
56
+    /**
57
+     * @var array
58
+     */
59
+    private static $caches = array();
60
+
61
+    /**
62
+     * Create a {@link Ttl_Cache} with the specified TTL, default 900 secs.
63
+     *
64
+     * @param string $name The cache name.
65
+     * @param int $ttl The cache TTL, default 900 secs.
66
+     *
67
+     * @since 3.21.2
68
+     */
69
+    public function __construct( $name, $ttl = 900 ) {
70
+
71
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
72
+
73
+        $this->name = $name;
74
+        $this->ttl  = $ttl;
75
+
76
+        $this->cache_dir = self::get_cache_folder() . DIRECTORY_SEPARATOR . md5( $name );
77
+
78
+        $this->log->trace( "Creating the cache folder {$this->cache_dir}..." );
79
+        wp_mkdir_p( $this->cache_dir );
80
+
81
+        self::$caches[ $name ] = $this;
82
+
83
+    }
84
+
85
+    /**
86
+     * Get the root cache folder.
87
+     *
88
+     * This is useful to introduce a cache cleaning procedure which will scan and delete older stale cache files.
89
+     *
90
+     * @return string The root cache folder.
91
+     * @since 3.22.5
92
+     */
93
+    public static function get_cache_folder() {
94
+
95
+        // Get the temp dir and add the directory separator if missing.
96
+        $temp_dir = get_temp_dir();
97
+        if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) {
98
+            $temp_dir .= DIRECTORY_SEPARATOR;
99
+        }
100
+
101
+        return $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( home_url() );
102
+    }
103
+
104
+    /**
105
+     * Get the cached data for the specified key.
106
+     *
107
+     * @param mixed $key A serializable key.
108
+     *
109
+     * @return mixed|null
110
+     * @since 3.21.2
111
+     */
112
+    public function get( $key ) {
113
+
114
+        $filename = $this->get_filename( $key );
115
+
116
+        // If the cache file exists and it's not too old, then return it.
117
+        if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) {
118
+            $this->log->trace( "Cache HIT.\n" );
119
+
120
+            return json_decode( file_get_contents( $filename ), true );
121
+        }
122
+
123
+        $this->log->trace( "Cache MISS, filename $filename.\n" );
124
+
125
+        return null;
126
+    }
127
+
128
+    public function put( $key, $data ) {
129
+
130
+        $filename = $this->get_filename( $key );
131
+
132
+        // Cache.
133
+        if ( file_exists( $filename ) ) {
134
+            @unlink( $filename );
135
+        }
136
+        @file_put_contents( $filename, wp_json_encode( $data ) );
137 137
 
138
-	}
139
-
140
-	public function delete( $key ) {
138
+    }
139
+
140
+    public function delete( $key ) {
141 141
 
142
-		$filename = $this->get_filename( $key );
142
+        $filename = $this->get_filename( $key );
143 143
 
144
-		// Delete.
145
-		if ( file_exists( $filename ) ) {
146
-			@unlink( $filename );
147
-		}
144
+        // Delete.
145
+        if ( file_exists( $filename ) ) {
146
+            @unlink( $filename );
147
+        }
148 148
 
149
-	}
149
+    }
150 150
 
151
-	public function flush() {
151
+    public function flush() {
152 152
 
153
-		$files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' );
154
-		foreach ( $files as $file ) { // iterate files
155
-			if ( is_file( $file ) ) {
156
-				@unlink( $file );
157
-			}
158
-		}
153
+        $files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' );
154
+        foreach ( $files as $file ) { // iterate files
155
+            if ( is_file( $file ) ) {
156
+                @unlink( $file );
157
+            }
158
+        }
159 159
 
160
-	}
160
+    }
161 161
 
162
-	public static function flush_all() {
162
+    public static function flush_all() {
163 163
 
164
-		/** @var Ttl_Cache $cache */
165
-		foreach ( self::$caches as $cache ) {
166
-			$cache->flush();
167
-		}
164
+        /** @var Ttl_Cache $cache */
165
+        foreach ( self::$caches as $cache ) {
166
+            $cache->flush();
167
+        }
168 168
 
169
-	}
169
+    }
170 170
 
171
-	/**
172
-	 * Get the full path for the given `$hash`. The file is not checked for its existence.
173
-	 *
174
-	 * @param string $hash A file hash.
175
-	 *
176
-	 * @return string The full path to the file.
177
-	 * @since 3.21.2
178
-	 */
179
-	private function get_path( $hash ) {
171
+    /**
172
+     * Get the full path for the given `$hash`. The file is not checked for its existence.
173
+     *
174
+     * @param string $hash A file hash.
175
+     *
176
+     * @return string The full path to the file.
177
+     * @since 3.21.2
178
+     */
179
+    private function get_path( $hash ) {
180 180
 
181
-		return $this->cache_dir . DIRECTORY_SEPARATOR . $hash;
182
-	}
181
+        return $this->cache_dir . DIRECTORY_SEPARATOR . $hash;
182
+    }
183 183
 
184
-	private function get_filename( $key ) {
184
+    private function get_filename( $key ) {
185 185
 
186
-		// Create a hash and a path to the cache file.
187
-		$hash     = md5( json_encode( $key ) );
188
-		$filename = $this->get_path( $hash );
186
+        // Create a hash and a path to the cache file.
187
+        $hash     = md5( json_encode( $key ) );
188
+        $filename = $this->get_path( $hash );
189 189
 
190
-		return $filename;
191
-	}
190
+        return $filename;
191
+    }
192 192
 
193 193
 }
Please login to merge, or discard this patch.
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -66,19 +66,19 @@  discard block
 block discarded – undo
66 66
 	 *
67 67
 	 * @since 3.21.2
68 68
 	 */
69
-	public function __construct( $name, $ttl = 900 ) {
69
+	public function __construct($name, $ttl = 900) {
70 70
 
71
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
71
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
72 72
 
73 73
 		$this->name = $name;
74 74
 		$this->ttl  = $ttl;
75 75
 
76
-		$this->cache_dir = self::get_cache_folder() . DIRECTORY_SEPARATOR . md5( $name );
76
+		$this->cache_dir = self::get_cache_folder().DIRECTORY_SEPARATOR.md5($name);
77 77
 
78
-		$this->log->trace( "Creating the cache folder {$this->cache_dir}..." );
79
-		wp_mkdir_p( $this->cache_dir );
78
+		$this->log->trace("Creating the cache folder {$this->cache_dir}...");
79
+		wp_mkdir_p($this->cache_dir);
80 80
 
81
-		self::$caches[ $name ] = $this;
81
+		self::$caches[$name] = $this;
82 82
 
83 83
 	}
84 84
 
@@ -94,11 +94,11 @@  discard block
 block discarded – undo
94 94
 
95 95
 		// Get the temp dir and add the directory separator if missing.
96 96
 		$temp_dir = get_temp_dir();
97
-		if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) {
97
+		if (DIRECTORY_SEPARATOR !== substr($temp_dir, - strlen(DIRECTORY_SEPARATOR))) {
98 98
 			$temp_dir .= DIRECTORY_SEPARATOR;
99 99
 		}
100 100
 
101
-		return $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( home_url() );
101
+		return $temp_dir.'wl.cache'.DIRECTORY_SEPARATOR.md5(home_url());
102 102
 	}
103 103
 
104 104
 	/**
@@ -109,51 +109,51 @@  discard block
 block discarded – undo
109 109
 	 * @return mixed|null
110 110
 	 * @since 3.21.2
111 111
 	 */
112
-	public function get( $key ) {
112
+	public function get($key) {
113 113
 
114
-		$filename = $this->get_filename( $key );
114
+		$filename = $this->get_filename($key);
115 115
 
116 116
 		// If the cache file exists and it's not too old, then return it.
117
-		if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) {
118
-			$this->log->trace( "Cache HIT.\n" );
117
+		if (file_exists($filename) && $this->ttl >= time() - filemtime($filename)) {
118
+			$this->log->trace("Cache HIT.\n");
119 119
 
120
-			return json_decode( file_get_contents( $filename ), true );
120
+			return json_decode(file_get_contents($filename), true);
121 121
 		}
122 122
 
123
-		$this->log->trace( "Cache MISS, filename $filename.\n" );
123
+		$this->log->trace("Cache MISS, filename $filename.\n");
124 124
 
125 125
 		return null;
126 126
 	}
127 127
 
128
-	public function put( $key, $data ) {
128
+	public function put($key, $data) {
129 129
 
130
-		$filename = $this->get_filename( $key );
130
+		$filename = $this->get_filename($key);
131 131
 
132 132
 		// Cache.
133
-		if ( file_exists( $filename ) ) {
134
-			@unlink( $filename );
133
+		if (file_exists($filename)) {
134
+			@unlink($filename);
135 135
 		}
136
-		@file_put_contents( $filename, wp_json_encode( $data ) );
136
+		@file_put_contents($filename, wp_json_encode($data));
137 137
 
138 138
 	}
139 139
 
140
-	public function delete( $key ) {
140
+	public function delete($key) {
141 141
 
142
-		$filename = $this->get_filename( $key );
142
+		$filename = $this->get_filename($key);
143 143
 
144 144
 		// Delete.
145
-		if ( file_exists( $filename ) ) {
146
-			@unlink( $filename );
145
+		if (file_exists($filename)) {
146
+			@unlink($filename);
147 147
 		}
148 148
 
149 149
 	}
150 150
 
151 151
 	public function flush() {
152 152
 
153
-		$files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' );
154
-		foreach ( $files as $file ) { // iterate files
155
-			if ( is_file( $file ) ) {
156
-				@unlink( $file );
153
+		$files = glob($this->cache_dir.DIRECTORY_SEPARATOR.'*');
154
+		foreach ($files as $file) { // iterate files
155
+			if (is_file($file)) {
156
+				@unlink($file);
157 157
 			}
158 158
 		}
159 159
 
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
 	public static function flush_all() {
163 163
 
164 164
 		/** @var Ttl_Cache $cache */
165
-		foreach ( self::$caches as $cache ) {
165
+		foreach (self::$caches as $cache) {
166 166
 			$cache->flush();
167 167
 		}
168 168
 
@@ -176,16 +176,16 @@  discard block
 block discarded – undo
176 176
 	 * @return string The full path to the file.
177 177
 	 * @since 3.21.2
178 178
 	 */
179
-	private function get_path( $hash ) {
179
+	private function get_path($hash) {
180 180
 
181
-		return $this->cache_dir . DIRECTORY_SEPARATOR . $hash;
181
+		return $this->cache_dir.DIRECTORY_SEPARATOR.$hash;
182 182
 	}
183 183
 
184
-	private function get_filename( $key ) {
184
+	private function get_filename($key) {
185 185
 
186 186
 		// Create a hash and a path to the cache file.
187
-		$hash     = md5( json_encode( $key ) );
188
-		$filename = $this->get_path( $hash );
187
+		$hash     = md5(json_encode($key));
188
+		$filename = $this->get_path($hash);
189 189
 
190 190
 		return $filename;
191 191
 	}
Please login to merge, or discard this patch.
src/wordlift/dataset/index.php 2 patches
Indentation   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -12,31 +12,31 @@
 block discarded – undo
12 12
 use Wordlift\Jsonld\Jsonld_Service;
13 13
 
14 14
 if ( ! defined( 'ABSPATH' ) ) {
15
-	exit;
15
+    exit;
16 16
 }
17 17
 // Register the Dataset JSON Endpoint. The `$api_service` variable must be defined in the calling file (wordlift.php).
18 18
 if ( apply_filters( 'wl_feature__enable__dataset-ng', false ) ) {
19 19
 
20
-	$sync_object_adapter_factory = new Sync_Object_Adapter_Factory();
21
-	$sync_service                = new Sync_Service( $api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() );
22
-	new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory );
23
-	new Sync_User_Hooks( $sync_service );
20
+    $sync_object_adapter_factory = new Sync_Object_Adapter_Factory();
21
+    $sync_service                = new Sync_Service( $api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() );
22
+    new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory );
23
+    new Sync_User_Hooks( $sync_service );
24 24
 
25
-	/**
26
-	 * @since 3.28.0
27
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/1186
28
-	 */
29
-	new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() );
25
+    /**
26
+     * @since 3.28.0
27
+     * @see https://github.com/insideout10/wordlift-plugin/issues/1186
28
+     */
29
+    new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() );
30 30
 
31
-	if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) {
32
-		new Sync_Hooks_Wordpress_Ontology();
33
-	}
31
+    if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) {
32
+        new Sync_Hooks_Wordpress_Ontology();
33
+    }
34 34
 
35
-	if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) {
36
-		// Set up the sync background process.
37
-		$sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory );
38
-		new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process );
39
-		new Sync_Page();
40
-	}
35
+    if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) {
36
+        // Set up the sync background process.
37
+        $sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory );
38
+        new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process );
39
+        new Sync_Page();
40
+    }
41 41
 
42 42
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -11,31 +11,31 @@
 block discarded – undo
11 11
 use Wordlift\Dataset\Sync_User_Hooks;
12 12
 use Wordlift\Jsonld\Jsonld_Service;
13 13
 
14
-if ( ! defined( 'ABSPATH' ) ) {
14
+if ( ! defined('ABSPATH')) {
15 15
 	exit;
16 16
 }
17 17
 // Register the Dataset JSON Endpoint. The `$api_service` variable must be defined in the calling file (wordlift.php).
18
-if ( apply_filters( 'wl_feature__enable__dataset-ng', false ) ) {
18
+if (apply_filters('wl_feature__enable__dataset-ng', false)) {
19 19
 
20 20
 	$sync_object_adapter_factory = new Sync_Object_Adapter_Factory();
21
-	$sync_service                = new Sync_Service( $api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() );
22
-	new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory );
23
-	new Sync_User_Hooks( $sync_service );
21
+	$sync_service                = new Sync_Service($api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance());
22
+	new Sync_Post_Hooks($sync_service, $sync_object_adapter_factory);
23
+	new Sync_User_Hooks($sync_service);
24 24
 
25 25
 	/**
26 26
 	 * @since 3.28.0
27 27
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/1186
28 28
 	 */
29
-	new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() );
29
+	new Sync_Hooks_Entity_Relation(Wordlift_Entity_Service::get_instance());
30 30
 
31
-	if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) {
31
+	if (apply_filters('wl_feature__enable__wordpress-ontology', false)) {
32 32
 		new Sync_Hooks_Wordpress_Ontology();
33 33
 	}
34 34
 
35
-	if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) {
35
+	if (apply_filters('wl_feature__enable__sync-background', false)) {
36 36
 		// Set up the sync background process.
37
-		$sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory );
38
-		new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process );
37
+		$sync_background_process = new Sync_Background_Process($sync_service, $sync_object_adapter_factory);
38
+		new Sync_Background_Process_Wpjson_Endpoint($sync_background_process);
39 39
 		new Sync_Page();
40 40
 	}
41 41
 
Please login to merge, or discard this patch.
src/wordlift/dataset/class-sync-hooks-wordpress-ontology.php 2 patches
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -6,47 +6,47 @@
 block discarded – undo
6 6
 
7 7
 class Sync_Hooks_Wordpress_Ontology {
8 8
 
9
-	const HTTP_PURL_ORG_WORDPRESS_1_0 = 'http://purl.org/wordpress/1.0/';
9
+    const HTTP_PURL_ORG_WORDPRESS_1_0 = 'http://purl.org/wordpress/1.0/';
10 10
 
11
-	public function __construct() {
12
-		add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 );
13
-	}
11
+    public function __construct() {
12
+        add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 );
13
+    }
14 14
 
15
-	public function jsonld( $jsonld, $type, $object_id ) {
15
+    public function jsonld( $jsonld, $type, $object_id ) {
16 16
 
17
-		$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'id' ] = $object_id;
17
+        $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'id' ] = $object_id;
18 18
 
19
-		switch ( $type ) {
19
+        switch ( $type ) {
20 20
 
21
-			case Object_Type_Enum::TERM:
22
-				$term = get_term( $object_id );
21
+            case Object_Type_Enum::TERM:
22
+                $term = get_term( $object_id );
23 23
 
24
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'contentType' ] = 'term';
25
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'name' ]        = $term->name;
26
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'description' ] = $term->description;
27
-				break;
24
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'contentType' ] = 'term';
25
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'name' ]        = $term->name;
26
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'description' ] = $term->description;
27
+                break;
28 28
 
29
-			case Object_Type_Enum::USER:
30
-				$user = get_userdata( $object_id );
29
+            case Object_Type_Enum::USER:
30
+                $user = get_userdata( $object_id );
31 31
 
32
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'contentType' ] = 'user';
33
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'displayName' ] = $user->display_name;
34
-				break;
32
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'contentType' ] = 'user';
33
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'displayName' ] = $user->display_name;
34
+                break;
35 35
 
36
-			case Object_Type_Enum::POST:
37
-				$post = get_post( $object_id );
36
+            case Object_Type_Enum::POST:
37
+                $post = get_post( $object_id );
38 38
 
39
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'contentType' ] = 'post';
40
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'postType' ]    = $post->post_type;
41
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'postTitle' ]   = $post->post_title;
42
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'postStatus' ]  = $post->post_status;
43
-				break;
39
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'contentType' ] = 'post';
40
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'postType' ]    = $post->post_type;
41
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'postTitle' ]   = $post->post_title;
42
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'postStatus' ]  = $post->post_status;
43
+                break;
44 44
 
45
-			default:
45
+            default:
46 46
 
47
-		}
47
+        }
48 48
 
49
-		return $jsonld;
50
-	}
49
+        return $jsonld;
50
+    }
51 51
 
52 52
 }
Please login to merge, or discard this patch.
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -9,37 +9,37 @@
 block discarded – undo
9 9
 	const HTTP_PURL_ORG_WORDPRESS_1_0 = 'http://purl.org/wordpress/1.0/';
10 10
 
11 11
 	public function __construct() {
12
-		add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 );
12
+		add_filter('wl_dataset__sync_service__sync_item__jsonld', array($this, 'jsonld'), 10, 3);
13 13
 	}
14 14
 
15
-	public function jsonld( $jsonld, $type, $object_id ) {
15
+	public function jsonld($jsonld, $type, $object_id) {
16 16
 
17
-		$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'id' ] = $object_id;
17
+		$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'id'] = $object_id;
18 18
 
19
-		switch ( $type ) {
19
+		switch ($type) {
20 20
 
21 21
 			case Object_Type_Enum::TERM:
22
-				$term = get_term( $object_id );
22
+				$term = get_term($object_id);
23 23
 
24
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'contentType' ] = 'term';
25
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'name' ]        = $term->name;
26
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'description' ] = $term->description;
24
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'contentType'] = 'term';
25
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'name']        = $term->name;
26
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'description'] = $term->description;
27 27
 				break;
28 28
 
29 29
 			case Object_Type_Enum::USER:
30
-				$user = get_userdata( $object_id );
30
+				$user = get_userdata($object_id);
31 31
 
32
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'contentType' ] = 'user';
33
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'displayName' ] = $user->display_name;
32
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'contentType'] = 'user';
33
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'displayName'] = $user->display_name;
34 34
 				break;
35 35
 
36 36
 			case Object_Type_Enum::POST:
37
-				$post = get_post( $object_id );
37
+				$post = get_post($object_id);
38 38
 
39
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'contentType' ] = 'post';
40
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'postType' ]    = $post->post_type;
41
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'postTitle' ]   = $post->post_title;
42
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'postStatus' ]  = $post->post_status;
39
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'contentType'] = 'post';
40
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'postType']    = $post->post_type;
41
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'postTitle']   = $post->post_title;
42
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'postStatus']  = $post->post_status;
43 43
 				break;
44 44
 
45 45
 			default:
Please login to merge, or discard this patch.
src/wordlift/dataset/class-sync-object-adapter-factory.php 2 patches
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -6,34 +6,34 @@
 block discarded – undo
6 6
 
7 7
 class Sync_Object_Adapter_Factory {
8 8
 
9
-	/**
10
-	 * @param int $type One of Object_Type_Enum::POST, Object_Type_Enum::USER, ...
11
-	 * @param int $object_id The object id.
12
-	 *
13
-	 * @return Sync_Object_Adapter
14
-	 * @throws \Exception
15
-	 */
16
-	function create( $type, $object_id ) {
17
-
18
-		switch ( $type ) {
19
-			case Object_Type_Enum::POST:
20
-				return new Sync_Post_Adapter( $object_id );
21
-			case Object_Type_Enum::USER:
22
-				return new Sync_User_Adapter( $object_id );
23
-			case Object_Type_Enum::TERM:
24
-				return new Sync_Term_Adapter( $object_id );
25
-			default:
26
-				throw new \Exception( "Unsupported type $type." );
27
-		}
28
-
29
-	}
30
-
31
-	function create_many( $type, $object_ids ) {
32
-		$that = $this;
33
-
34
-		return array_map( function ( $item ) use ( $type, $that ) {
35
-			return $that->create( $type, $item );
36
-		}, (array) $object_ids );
37
-	}
9
+    /**
10
+     * @param int $type One of Object_Type_Enum::POST, Object_Type_Enum::USER, ...
11
+     * @param int $object_id The object id.
12
+     *
13
+     * @return Sync_Object_Adapter
14
+     * @throws \Exception
15
+     */
16
+    function create( $type, $object_id ) {
17
+
18
+        switch ( $type ) {
19
+            case Object_Type_Enum::POST:
20
+                return new Sync_Post_Adapter( $object_id );
21
+            case Object_Type_Enum::USER:
22
+                return new Sync_User_Adapter( $object_id );
23
+            case Object_Type_Enum::TERM:
24
+                return new Sync_Term_Adapter( $object_id );
25
+            default:
26
+                throw new \Exception( "Unsupported type $type." );
27
+        }
28
+
29
+    }
30
+
31
+    function create_many( $type, $object_ids ) {
32
+        $that = $this;
33
+
34
+        return array_map( function ( $item ) use ( $type, $that ) {
35
+            return $that->create( $type, $item );
36
+        }, (array) $object_ids );
37
+    }
38 38
 
39 39
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -13,27 +13,27 @@
 block discarded – undo
13 13
 	 * @return Sync_Object_Adapter
14 14
 	 * @throws \Exception
15 15
 	 */
16
-	function create( $type, $object_id ) {
16
+	function create($type, $object_id) {
17 17
 
18
-		switch ( $type ) {
18
+		switch ($type) {
19 19
 			case Object_Type_Enum::POST:
20
-				return new Sync_Post_Adapter( $object_id );
20
+				return new Sync_Post_Adapter($object_id);
21 21
 			case Object_Type_Enum::USER:
22
-				return new Sync_User_Adapter( $object_id );
22
+				return new Sync_User_Adapter($object_id);
23 23
 			case Object_Type_Enum::TERM:
24
-				return new Sync_Term_Adapter( $object_id );
24
+				return new Sync_Term_Adapter($object_id);
25 25
 			default:
26
-				throw new \Exception( "Unsupported type $type." );
26
+				throw new \Exception("Unsupported type $type.");
27 27
 		}
28 28
 
29 29
 	}
30 30
 
31
-	function create_many( $type, $object_ids ) {
31
+	function create_many($type, $object_ids) {
32 32
 		$that = $this;
33 33
 
34
-		return array_map( function ( $item ) use ( $type, $that ) {
35
-			return $that->create( $type, $item );
36
-		}, (array) $object_ids );
34
+		return array_map(function($item) use ($type, $that) {
35
+			return $that->create($type, $item);
36
+		}, (array) $object_ids);
37 37
 	}
38 38
 
39 39
 }
Please login to merge, or discard this patch.
src/wordlift/dataset/class-sync-post-adapter.php 1 patch
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -5,34 +5,34 @@
 block discarded – undo
5 5
 use Wordlift\Object_Type_Enum;
6 6
 
7 7
 class Sync_Post_Adapter extends Abstract_Sync_Object_Adapter {
8
-	/**
9
-	 * @var int
10
-	 */
11
-	private $post_id;
12
-
13
-	/**
14
-	 * Sync_Term_Adapter constructor.
15
-	 *
16
-	 * @param int $post_id
17
-	 *
18
-	 * @throws \Exception
19
-	 */
20
-	function __construct( $post_id ) {
21
-		parent::__construct( Object_Type_Enum::POST, $post_id );
22
-
23
-		$this->post_id = $post_id;
24
-	}
25
-
26
-	function is_published() {
27
-		return ( 'publish' === get_post_status( $this->post_id ) );
28
-	}
29
-
30
-	function is_public() {
31
-		// Check if the post type is public.
32
-		$post_type     = get_post_type( $this->post_id );
33
-		$post_type_obj = get_post_type_object( $post_type );
34
-
35
-		return $post_type_obj->public;
36
-	}
8
+    /**
9
+     * @var int
10
+     */
11
+    private $post_id;
12
+
13
+    /**
14
+     * Sync_Term_Adapter constructor.
15
+     *
16
+     * @param int $post_id
17
+     *
18
+     * @throws \Exception
19
+     */
20
+    function __construct( $post_id ) {
21
+        parent::__construct( Object_Type_Enum::POST, $post_id );
22
+
23
+        $this->post_id = $post_id;
24
+    }
25
+
26
+    function is_published() {
27
+        return ( 'publish' === get_post_status( $this->post_id ) );
28
+    }
29
+
30
+    function is_public() {
31
+        // Check if the post type is public.
32
+        $post_type     = get_post_type( $this->post_id );
33
+        $post_type_obj = get_post_type_object( $post_type );
34
+
35
+        return $post_type_obj->public;
36
+    }
37 37
 
38 38
 }
Please login to merge, or discard this patch.
src/wordlift/dataset/class-sync-page.php 2 patches
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -4,39 +4,39 @@  discard block
 block discarded – undo
4 4
 
5 5
 class Sync_Page {
6 6
 
7
-	/**
8
-	 * Sync_Page constructor.
9
-	 */
10
-	public function __construct() {
7
+    /**
8
+     * Sync_Page constructor.
9
+     */
10
+    public function __construct() {
11 11
 
12
-		add_action( 'admin_menu', array( $this, 'admin_menu' ) );
12
+        add_action( 'admin_menu', array( $this, 'admin_menu' ) );
13 13
 
14
-	}
14
+    }
15 15
 
16
-	public function admin_menu() {
16
+    public function admin_menu() {
17 17
 
18
-		add_submenu_page( 'wl_admin_menu', __( 'Synchronize Dataset', 'wordlift' ), __( 'Synchronize Dataset', 'wordlift' ), 'manage_options', 'wl_dataset_sync', array(
19
-			$this,
20
-			'render'
21
-		) );
18
+        add_submenu_page( 'wl_admin_menu', __( 'Synchronize Dataset', 'wordlift' ), __( 'Synchronize Dataset', 'wordlift' ), 'manage_options', 'wl_dataset_sync', array(
19
+            $this,
20
+            'render'
21
+        ) );
22 22
 
23
-	}
23
+    }
24 24
 
25
-	public function render() {
25
+    public function render() {
26 26
 
27
-		wp_enqueue_style(
28
-			'wl-tasks-page',
29
-			plugin_dir_url( dirname( __FILE__ ) ) . 'tasks/admin/assets/tasks-page.css',
30
-			array(),
31
-			\Wordlift::get_instance()->get_version(),
32
-			'all' );
33
-		wp_enqueue_script(
34
-			'wl-dataset-sync-page',
35
-			plugin_dir_url( __FILE__ ) . 'assets/sync-page.js',
36
-			array( 'wp-api' ),
37
-			\Wordlift::get_instance()->get_version() );
27
+        wp_enqueue_style(
28
+            'wl-tasks-page',
29
+            plugin_dir_url( dirname( __FILE__ ) ) . 'tasks/admin/assets/tasks-page.css',
30
+            array(),
31
+            \Wordlift::get_instance()->get_version(),
32
+            'all' );
33
+        wp_enqueue_script(
34
+            'wl-dataset-sync-page',
35
+            plugin_dir_url( __FILE__ ) . 'assets/sync-page.js',
36
+            array( 'wp-api' ),
37
+            \Wordlift::get_instance()->get_version() );
38 38
 
39
-		?>
39
+        ?>
40 40
         <div class="wrap">
41 41
             <h2><?php esc_html_e( 'Synchronize Dataset', 'wordlift' ); ?></h2>
42 42
 
@@ -46,12 +46,12 @@  discard block
 block discarded – undo
46 46
             </div>
47 47
 
48 48
             <button id="wl-start-btn" type="button" class="button button-large button-primary"><?php
49
-				esc_html_e( 'Start', 'wordlift-framework' ); ?></button>
49
+                esc_html_e( 'Start', 'wordlift-framework' ); ?></button>
50 50
             <button id="wl-stop-btn" type="button" class="button button-large button-primary hidden"><?php
51
-				esc_html_e( 'Stop', 'wordlift-framework' ); ?></button>
51
+                esc_html_e( 'Stop', 'wordlift-framework' ); ?></button>
52 52
 
53 53
         </div>
54 54
 		<?php
55
-	}
55
+    }
56 56
 
57 57
 }
58 58
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -9,16 +9,16 @@  discard block
 block discarded – undo
9 9
 	 */
10 10
 	public function __construct() {
11 11
 
12
-		add_action( 'admin_menu', array( $this, 'admin_menu' ) );
12
+		add_action('admin_menu', array($this, 'admin_menu'));
13 13
 
14 14
 	}
15 15
 
16 16
 	public function admin_menu() {
17 17
 
18
-		add_submenu_page( 'wl_admin_menu', __( 'Synchronize Dataset', 'wordlift' ), __( 'Synchronize Dataset', 'wordlift' ), 'manage_options', 'wl_dataset_sync', array(
18
+		add_submenu_page('wl_admin_menu', __('Synchronize Dataset', 'wordlift'), __('Synchronize Dataset', 'wordlift'), 'manage_options', 'wl_dataset_sync', array(
19 19
 			$this,
20 20
 			'render'
21
-		) );
21
+		));
22 22
 
23 23
 	}
24 24
 
@@ -26,19 +26,19 @@  discard block
 block discarded – undo
26 26
 
27 27
 		wp_enqueue_style(
28 28
 			'wl-tasks-page',
29
-			plugin_dir_url( dirname( __FILE__ ) ) . 'tasks/admin/assets/tasks-page.css',
29
+			plugin_dir_url(dirname(__FILE__)).'tasks/admin/assets/tasks-page.css',
30 30
 			array(),
31 31
 			\Wordlift::get_instance()->get_version(),
32 32
 			'all' );
33 33
 		wp_enqueue_script(
34 34
 			'wl-dataset-sync-page',
35
-			plugin_dir_url( __FILE__ ) . 'assets/sync-page.js',
36
-			array( 'wp-api' ),
35
+			plugin_dir_url(__FILE__).'assets/sync-page.js',
36
+			array('wp-api'),
37 37
 			\Wordlift::get_instance()->get_version() );
38 38
 
39 39
 		?>
40 40
         <div class="wrap">
41
-            <h2><?php esc_html_e( 'Synchronize Dataset', 'wordlift' ); ?></h2>
41
+            <h2><?php esc_html_e('Synchronize Dataset', 'wordlift'); ?></h2>
42 42
 
43 43
             <div class="wl-task__progress" style="border: 1px solid #23282D; height: 20px; margin: 8px 0;">
44 44
                 <div class="wl-task__progress__bar"
@@ -46,9 +46,9 @@  discard block
 block discarded – undo
46 46
             </div>
47 47
 
48 48
             <button id="wl-start-btn" type="button" class="button button-large button-primary"><?php
49
-				esc_html_e( 'Start', 'wordlift-framework' ); ?></button>
49
+				esc_html_e('Start', 'wordlift-framework'); ?></button>
50 50
             <button id="wl-stop-btn" type="button" class="button button-large button-primary hidden"><?php
51
-				esc_html_e( 'Stop', 'wordlift-framework' ); ?></button>
51
+				esc_html_e('Stop', 'wordlift-framework'); ?></button>
52 52
 
53 53
         </div>
54 54
 		<?php
Please login to merge, or discard this patch.