Completed
Pull Request — develop (#1698)
by
unknown
01:26
created
src/wordlift/dataset/class-sync-user-hooks.php 2 patches
Indentation   +105 added lines, -105 removed lines patch added patch discarded remove patch
@@ -7,110 +7,110 @@
 block discarded – undo
7 7
 use Wordlift\Object_Type_Enum;
8 8
 
9 9
 class Sync_User_Hooks extends Abstract_Sync_Hooks {
10
-	/**
11
-	 * @var \Wordlift_Log_Service
12
-	 */
13
-	private $log;
14
-
15
-	/**
16
-	 * @var Sync_Service
17
-	 */
18
-	private $sync_service;
19
-
20
-	/**
21
-	 * Sync_User_Hooks constructor.
22
-	 *
23
-	 * @param Sync_Service $sync_service
24
-	 */
25
-	public function __construct( Sync_Service $sync_service ) {
26
-		parent::__construct();
27
-
28
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
29
-
30
-		$this->sync_service = $sync_service;
31
-
32
-		$this->register_hooks();
33
-
34
-	}
35
-
36
-	private function register_hooks() {
37
-		/**
38
-		 * Register hooks for user and meta.
39
-		 */
40
-		add_action( 'user_register', array( $this, 'changed_user' ) );
41
-		add_action( 'profile_update', array( $this, 'changed_user' ) );
42
-		add_action( 'added_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
43
-		add_action( 'updated_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
44
-		add_action( 'deleted_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
45
-		add_action( 'delete_user', array( $this, 'delete_user' ) );
46
-
47
-	}
48
-
49
-	public function changed_user( $user_id ) {
50
-
51
-		$this->sync( $user_id );
52
-
53
-	}
54
-
55
-	public function changed_user_meta( $meta_id, $user_id, $meta_key ) {
56
-
57
-		if ( in_array(
58
-			$meta_key,
59
-			apply_filters(
60
-				'wl_dataset__sync_user_hooks__ignored_meta_keys',
61
-				apply_filters(
62
-					'wl_dataset__sync_hooks__ignored_meta_keys',
63
-					array(
64
-						'rich_editing',
65
-						'comment_shortcuts',
66
-						'admin_color',
67
-						'use_ssl',
68
-						'show_admin_bar_front',
69
-						'wptests_capabilities',
70
-						'wptests_user_level',
71
-						'dismissed_wp_pointers',
72
-						'entity_url',
73
-					)
74
-				)
75
-			),
76
-			true
77
-		) ) {
78
-			return;
79
-		}
80
-
81
-		$this->sync( $user_id );
82
-
83
-	}
84
-
85
-	private function sync( $user_id ) {
86
-		$this->enqueue( array( 'do_sync', $user_id ) );
87
-	}
88
-
89
-	public function do_sync( $user_id ) {
90
-
91
-		try {
92
-			$this->sync_service->sync_one( Object_Type_Enum::USER, (int) $user_id );
93
-		} catch ( \Exception $e ) {
94
-			$this->log->error( "An error occurred while trying to sync user $user_id: " . $e->getMessage(), $e );
95
-		}
96
-
97
-	}
98
-
99
-	public function delete_user( $user_id ) {
100
-		$this->enqueue( array( 'do_delete', $user_id ) );
101
-	}
102
-
103
-	public function do_delete( $user_id ) {
104
-		try {
105
-			$this->sync_service->delete_one(
106
-				Object_Type_Enum::USER,
107
-				$user_id,
108
-				Wordpress_User_Content_Legacy_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_user( $user_id ) )
109
-			);
110
-		} catch ( \Exception $e ) {
111
-			$this->log->error( "An error occurred while trying to delete user $user_id: " . $e->getMessage(), $e );
112
-		}
113
-
114
-	}
10
+    /**
11
+     * @var \Wordlift_Log_Service
12
+     */
13
+    private $log;
14
+
15
+    /**
16
+     * @var Sync_Service
17
+     */
18
+    private $sync_service;
19
+
20
+    /**
21
+     * Sync_User_Hooks constructor.
22
+     *
23
+     * @param Sync_Service $sync_service
24
+     */
25
+    public function __construct( Sync_Service $sync_service ) {
26
+        parent::__construct();
27
+
28
+        $this->log = \Wordlift_Log_Service::get_logger( get_class() );
29
+
30
+        $this->sync_service = $sync_service;
31
+
32
+        $this->register_hooks();
33
+
34
+    }
35
+
36
+    private function register_hooks() {
37
+        /**
38
+         * Register hooks for user and meta.
39
+         */
40
+        add_action( 'user_register', array( $this, 'changed_user' ) );
41
+        add_action( 'profile_update', array( $this, 'changed_user' ) );
42
+        add_action( 'added_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
43
+        add_action( 'updated_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
44
+        add_action( 'deleted_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
45
+        add_action( 'delete_user', array( $this, 'delete_user' ) );
46
+
47
+    }
48
+
49
+    public function changed_user( $user_id ) {
50
+
51
+        $this->sync( $user_id );
52
+
53
+    }
54
+
55
+    public function changed_user_meta( $meta_id, $user_id, $meta_key ) {
56
+
57
+        if ( in_array(
58
+            $meta_key,
59
+            apply_filters(
60
+                'wl_dataset__sync_user_hooks__ignored_meta_keys',
61
+                apply_filters(
62
+                    'wl_dataset__sync_hooks__ignored_meta_keys',
63
+                    array(
64
+                        'rich_editing',
65
+                        'comment_shortcuts',
66
+                        'admin_color',
67
+                        'use_ssl',
68
+                        'show_admin_bar_front',
69
+                        'wptests_capabilities',
70
+                        'wptests_user_level',
71
+                        'dismissed_wp_pointers',
72
+                        'entity_url',
73
+                    )
74
+                )
75
+            ),
76
+            true
77
+        ) ) {
78
+            return;
79
+        }
80
+
81
+        $this->sync( $user_id );
82
+
83
+    }
84
+
85
+    private function sync( $user_id ) {
86
+        $this->enqueue( array( 'do_sync', $user_id ) );
87
+    }
88
+
89
+    public function do_sync( $user_id ) {
90
+
91
+        try {
92
+            $this->sync_service->sync_one( Object_Type_Enum::USER, (int) $user_id );
93
+        } catch ( \Exception $e ) {
94
+            $this->log->error( "An error occurred while trying to sync user $user_id: " . $e->getMessage(), $e );
95
+        }
96
+
97
+    }
98
+
99
+    public function delete_user( $user_id ) {
100
+        $this->enqueue( array( 'do_delete', $user_id ) );
101
+    }
102
+
103
+    public function do_delete( $user_id ) {
104
+        try {
105
+            $this->sync_service->delete_one(
106
+                Object_Type_Enum::USER,
107
+                $user_id,
108
+                Wordpress_User_Content_Legacy_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_user( $user_id ) )
109
+            );
110
+        } catch ( \Exception $e ) {
111
+            $this->log->error( "An error occurred while trying to delete user $user_id: " . $e->getMessage(), $e );
112
+        }
113
+
114
+    }
115 115
 
116 116
 }
Please login to merge, or discard this patch.
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -22,10 +22,10 @@  discard block
 block discarded – undo
22 22
 	 *
23 23
 	 * @param Sync_Service $sync_service
24 24
 	 */
25
-	public function __construct( Sync_Service $sync_service ) {
25
+	public function __construct(Sync_Service $sync_service) {
26 26
 		parent::__construct();
27 27
 
28
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
28
+		$this->log = \Wordlift_Log_Service::get_logger(get_class());
29 29
 
30 30
 		$this->sync_service = $sync_service;
31 31
 
@@ -37,24 +37,24 @@  discard block
 block discarded – undo
37 37
 		/**
38 38
 		 * Register hooks for user and meta.
39 39
 		 */
40
-		add_action( 'user_register', array( $this, 'changed_user' ) );
41
-		add_action( 'profile_update', array( $this, 'changed_user' ) );
42
-		add_action( 'added_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
43
-		add_action( 'updated_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
44
-		add_action( 'deleted_user_meta', array( $this, 'changed_user_meta' ), 10, 3 );
45
-		add_action( 'delete_user', array( $this, 'delete_user' ) );
40
+		add_action('user_register', array($this, 'changed_user'));
41
+		add_action('profile_update', array($this, 'changed_user'));
42
+		add_action('added_user_meta', array($this, 'changed_user_meta'), 10, 3);
43
+		add_action('updated_user_meta', array($this, 'changed_user_meta'), 10, 3);
44
+		add_action('deleted_user_meta', array($this, 'changed_user_meta'), 10, 3);
45
+		add_action('delete_user', array($this, 'delete_user'));
46 46
 
47 47
 	}
48 48
 
49
-	public function changed_user( $user_id ) {
49
+	public function changed_user($user_id) {
50 50
 
51
-		$this->sync( $user_id );
51
+		$this->sync($user_id);
52 52
 
53 53
 	}
54 54
 
55
-	public function changed_user_meta( $meta_id, $user_id, $meta_key ) {
55
+	public function changed_user_meta($meta_id, $user_id, $meta_key) {
56 56
 
57
-		if ( in_array(
57
+		if (in_array(
58 58
 			$meta_key,
59 59
 			apply_filters(
60 60
 				'wl_dataset__sync_user_hooks__ignored_meta_keys',
@@ -74,41 +74,41 @@  discard block
 block discarded – undo
74 74
 				)
75 75
 			),
76 76
 			true
77
-		) ) {
77
+		)) {
78 78
 			return;
79 79
 		}
80 80
 
81
-		$this->sync( $user_id );
81
+		$this->sync($user_id);
82 82
 
83 83
 	}
84 84
 
85
-	private function sync( $user_id ) {
86
-		$this->enqueue( array( 'do_sync', $user_id ) );
85
+	private function sync($user_id) {
86
+		$this->enqueue(array('do_sync', $user_id));
87 87
 	}
88 88
 
89
-	public function do_sync( $user_id ) {
89
+	public function do_sync($user_id) {
90 90
 
91 91
 		try {
92
-			$this->sync_service->sync_one( Object_Type_Enum::USER, (int) $user_id );
93
-		} catch ( \Exception $e ) {
94
-			$this->log->error( "An error occurred while trying to sync user $user_id: " . $e->getMessage(), $e );
92
+			$this->sync_service->sync_one(Object_Type_Enum::USER, (int) $user_id);
93
+		} catch (\Exception $e) {
94
+			$this->log->error("An error occurred while trying to sync user $user_id: ".$e->getMessage(), $e);
95 95
 		}
96 96
 
97 97
 	}
98 98
 
99
-	public function delete_user( $user_id ) {
100
-		$this->enqueue( array( 'do_delete', $user_id ) );
99
+	public function delete_user($user_id) {
100
+		$this->enqueue(array('do_delete', $user_id));
101 101
 	}
102 102
 
103
-	public function do_delete( $user_id ) {
103
+	public function do_delete($user_id) {
104 104
 		try {
105 105
 			$this->sync_service->delete_one(
106 106
 				Object_Type_Enum::USER,
107 107
 				$user_id,
108
-				Wordpress_User_Content_Legacy_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_user( $user_id ) )
108
+				Wordpress_User_Content_Legacy_Service::get_instance()->get_entity_id(Wordpress_Content_Id::create_user($user_id))
109 109
 			);
110
-		} catch ( \Exception $e ) {
111
-			$this->log->error( "An error occurred while trying to delete user $user_id: " . $e->getMessage(), $e );
110
+		} catch (\Exception $e) {
111
+			$this->log->error("An error occurred while trying to delete user $user_id: ".$e->getMessage(), $e);
112 112
 		}
113 113
 
114 114
 	}
Please login to merge, or discard this patch.
src/wordlift/dataset/class-sync-user-adapter.php 2 patches
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -6,33 +6,33 @@
 block discarded – undo
6 6
 
7 7
 class Sync_User_Adapter extends Abstract_Sync_Object_Adapter {
8 8
 
9
-	/**
10
-	 * Sync_User_Adapter constructor.
11
-	 *
12
-	 * @param int $user_id
13
-	 *
14
-	 * @throws \Exception when an error occurs.
15
-	 */
16
-	public function __construct( $user_id ) {
17
-		parent::__construct( Object_Type_Enum::USER, $user_id );
18
-	}
19
-
20
-	public function is_published() {
21
-		return true;
22
-	}
23
-
24
-	public function is_public() {
25
-		return true;
26
-	}
27
-
28
-	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
29
-	public function set_values( $arr ) {
30
-		// @@todo
31
-	}
32
-
33
-	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
34
-	public function get_value( $key ) {
35
-		// @@todo
36
-	}
9
+    /**
10
+     * Sync_User_Adapter constructor.
11
+     *
12
+     * @param int $user_id
13
+     *
14
+     * @throws \Exception when an error occurs.
15
+     */
16
+    public function __construct( $user_id ) {
17
+        parent::__construct( Object_Type_Enum::USER, $user_id );
18
+    }
19
+
20
+    public function is_published() {
21
+        return true;
22
+    }
23
+
24
+    public function is_public() {
25
+        return true;
26
+    }
27
+
28
+    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
29
+    public function set_values( $arr ) {
30
+        // @@todo
31
+    }
32
+
33
+    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
34
+    public function get_value( $key ) {
35
+        // @@todo
36
+    }
37 37
 
38 38
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -13,8 +13,8 @@  discard block
 block discarded – undo
13 13
 	 *
14 14
 	 * @throws \Exception when an error occurs.
15 15
 	 */
16
-	public function __construct( $user_id ) {
17
-		parent::__construct( Object_Type_Enum::USER, $user_id );
16
+	public function __construct($user_id) {
17
+		parent::__construct(Object_Type_Enum::USER, $user_id);
18 18
 	}
19 19
 
20 20
 	public function is_published() {
@@ -26,12 +26,12 @@  discard block
 block discarded – undo
26 26
 	}
27 27
 
28 28
 	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
29
-	public function set_values( $arr ) {
29
+	public function set_values($arr) {
30 30
 		// @@todo
31 31
 	}
32 32
 
33 33
 	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
34
-	public function get_value( $key ) {
34
+	public function get_value($key) {
35 35
 		// @@todo
36 36
 	}
37 37
 
Please login to merge, or discard this patch.
src/wordlift/dataset/class-sync-service.php 2 patches
Indentation   +266 added lines, -266 removed lines patch added patch discarded remove patch
@@ -7,271 +7,271 @@
 block discarded – undo
7 7
 use Wordlift\Object_Type_Enum;
8 8
 
9 9
 class Sync_Service {
10
-	const JSONLD_HASH = 'jsonld_hash';
11
-	const SYNCED_GMT  = 'synced_gmt';
12
-
13
-	/**
14
-	 * @var \Wordlift_Log_Service
15
-	 */
16
-	private $log;
17
-
18
-	/**
19
-	 * @var Api_Service
20
-	 */
21
-	private $api_service;
22
-
23
-	/**
24
-	 * @var Jsonld_Service
25
-	 */
26
-	private $jsonld_service;
27
-
28
-	/**
29
-	 * @var Sync_Background_Process
30
-	 */
31
-	private $sync_background_process;
32
-
33
-	/**
34
-	 * The number of posts processed in one call.
35
-	 *
36
-	 * @var int The batch size.
37
-	 */
38
-	private $batch_size;
39
-
40
-	/**
41
-	 * @var Sync_Object_Adapter_Factory
42
-	 */
43
-	private $sync_object_adapter_factory;
44
-
45
-	/**
46
-	 * @var Sync_Service
47
-	 */
48
-	private static $instance;
49
-	private $entity_service;
50
-
51
-	/**
52
-	 * Constructor.
53
-	 *
54
-	 * @param Api_Service                 $api_service The {@link Api_Service} used to communicate with the remote APIs.
55
-	 * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory
56
-	 * @param Jsonld_Service              $jsonld_service
57
-	 * @param \Wordlift_Entity_Service    $entity_service
58
-	 */
59
-	public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) {
60
-
61
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
62
-
63
-		$this->api_service                 = $api_service;
64
-		$this->sync_object_adapter_factory = $sync_object_adapter_factory;
65
-		$this->jsonld_service              = $jsonld_service;
66
-		$this->entity_service              = $entity_service;
67
-		$this->batch_size                  = 10;
68
-
69
-		// You need to initialize this early, otherwise the Background Process isn't registered in AJAX calls.
70
-		// $this->sync_background_process = new Sync_Background_Process( $this );;
71
-
72
-		// Exclude the JSONLD_HASH meta key from those that require a resync.
73
-		add_filter(
74
-			'wl_dataset__sync_hooks__ignored_meta_keys',
75
-			function ( $args ) {
76
-				$args[] = Sync_Service::JSONLD_HASH;
77
-				$args[] = Sync_Service::SYNCED_GMT;
78
-
79
-				return $args;
80
-			}
81
-		);
82
-
83
-		self::$instance = $this;
84
-	}
85
-
86
-	public static function get_instance() {
87
-		return self::$instance;
88
-	}
89
-
90
-	/**
91
-	 * @param int $type
92
-	 * @param int $object_id
93
-	 *
94
-	 * @return array|false
95
-	 * @throws \Exception when an error occurs.
96
-	 */
97
-	public function sync_one( $type, $object_id ) {
98
-
99
-		$object = $this->sync_object_adapter_factory->create( $type, $object_id );
100
-
101
-		return $this->sync_many( array( $object ) );
102
-	}
103
-
104
-	/**
105
-	 * @param $type string Post or User.
106
-	 * @param $object_id  int Post or User id
107
-	 * @param $uri string Entity uri , This needs to be supplied before deletion, if we
108
-	 * get it from meta it might not be available.
109
-	 *
110
-	 * @return bool
111
-	 */
112
-	public function delete_one( $type, $object_id, $uri ) {
113
-		// Entity URL isn't set, bail out.
114
-		if ( empty( $uri ) ) {
115
-			return false;
116
-		}
117
-
118
-		$response = $this->api_service->request(
119
-			'DELETE',
120
-			sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) )
121
-		);
122
-
123
-		// Update the sync date in case of success, otherwise log an error.
124
-		if ( ! $response->is_success() ) {
125
-			return false;
126
-		}
127
-
128
-		/**
129
-		 * Allow 3rd parties to run additional sync work.
130
-		 */
131
-		do_action( 'wl_sync__delete_one', $type, $object_id, $uri );
132
-
133
-		return true;
134
-	}
135
-
136
-	/**
137
-	 * @param Sync_Object_Adapter[] $objects
138
-	 * @param bool                  $force Force synchronization even if the json-ld hash hasn't changed.
139
-	 *
140
-	 * @return bool
141
-	 * @throws \Exception when an error occurs.
142
-	 */
143
-	public function sync_many( $objects, $force = false ) {
144
-
145
-		$hashes   = array();
146
-		$payloads = array();
147
-		/** @var Sync_Object_Adapter $object */
148
-		foreach ( $objects as $object ) {
149
-			// Bail out if no payload.
150
-			$payload_as_string = $this->get_payload_as_string( $object );
151
-			if ( empty( $payload_as_string ) ) {
152
-				continue;
153
-			}
154
-			$new_hash = sha1( $payload_as_string );
155
-			$old_hash = $object->get_value( self::JSONLD_HASH );
156
-
157
-			// JSON-LD hasn't changed, bail out.
158
-			$should_sync = $force || $new_hash !== $old_hash;
159
-			if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) {
160
-				continue;
161
-			}
162
-
163
-			// Collect the hashes and the payloads.
164
-			$hashes[]   = array( $object, $new_hash, $payload_as_string );
165
-			$payloads[] = $payload_as_string;
166
-		}
167
-
168
-		// Bail out if payloads are empty.
169
-		if ( empty( $payloads ) ) {
170
-			return false;
171
-		}
172
-
173
-		// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
174
-		$blocking = apply_filters( 'wl_feature__enable__sync-blocking', false );
175
-		$response = $this->api_service->request(
176
-			'POST',
177
-			'/middleware/dataset/batch',
178
-			array( 'Content-Type' => 'application/json' ),
179
-			// Put the payload in a JSON array w/o decoding/encoding again.
180
-			'[ ' . implode( ', ', $payloads ) . ' ]',
181
-			$blocking ? 60 : 0.001,
182
-			null,
183
-			array( 'blocking' => $blocking )
184
-		);
185
-
186
-		// Update the sync date in case of success, otherwise log an error.
187
-		if ( $blocking && ! $response->is_success() ) {
188
-			return false;
189
-		}
190
-
191
-		// If successful update the hashes and sync datetime.
192
-		foreach ( $hashes as $hash ) {
193
-			$object   = $hash[0];
194
-			$new_hash = $hash[1];
195
-
196
-			$object->set_values(
197
-				array(
198
-					self::JSONLD_HASH => $new_hash,
199
-					self::SYNCED_GMT  => current_time( 'mysql', true ),
200
-				)
201
-			);
202
-		}
203
-
204
-		/**
205
-		 * Allow 3rd parties to run additional sync work.
206
-		 */
207
-		do_action( 'wl_sync__sync_many', $hashes );
208
-
209
-		return true;
210
-	}
211
-
212
-	/**
213
-	 * @param Sync_Object_Adapter $object
214
-	 *
215
-	 * @return false|string
216
-	 * @throws \Exception when an error occurs.
217
-	 */
218
-	private function get_payload_as_string( $object ) {
219
-		$type             = $object->get_type();
220
-		$object_id        = $object->get_object_id();
221
-		$jsonld_as_string = wp_json_encode(
222
-			apply_filters(
223
-				'wl_dataset__sync_service__sync_item__jsonld',
224
-				$this->jsonld_service->get( $type, $object_id ),
225
-				$type,
226
-				$object_id
227
-			),
228
-			64
229
-		); // JSON_UNESCAPED_SLASHES
230
-		$uri              = $this->entity_service->get_uri( $object_id, $type );
231
-
232
-		// Entity URL isn't set, bail out.
233
-		if ( empty( $uri ) ) {
234
-			return false;
235
-		}
236
-
237
-		return wp_json_encode(
238
-			array(
239
-				'uri'     => $uri,
240
-				'model'   => $jsonld_as_string,
241
-				'private' => ! ( $object->is_public() && $object->is_published() ),
242
-			),
243
-			64
244
-		); // JSON_UNESCAPED_SLASHES
245
-	}
246
-
247
-	/**
248
-	 * @param $post_id
249
-	 *
250
-	 * @todo Complete the delete item.
251
-	 */
252
-	public function delete_item( $post_id ) {
253
-		$uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST );
254
-
255
-		if ( ! isset( $uri ) ) {
256
-			return;
257
-		}
258
-
259
-		// Make a request to the remote endpoint.
260
-		$this->api_service->request(
261
-			'DELETE',
262
-			'/middleware/dataset?uri=' . rawurlencode( $uri ),
263
-			array( 'Content-Type' => 'application/ld+json' )
264
-		);
265
-
266
-	}
267
-
268
-	public function get_batch_size() {
269
-
270
-		return $this->batch_size;
271
-	}
272
-
273
-	public function delete_all() {
274
-		$this->api_service->request( 'DELETE', '/middleware/dataset/all' );
275
-	}
10
+    const JSONLD_HASH = 'jsonld_hash';
11
+    const SYNCED_GMT  = 'synced_gmt';
12
+
13
+    /**
14
+     * @var \Wordlift_Log_Service
15
+     */
16
+    private $log;
17
+
18
+    /**
19
+     * @var Api_Service
20
+     */
21
+    private $api_service;
22
+
23
+    /**
24
+     * @var Jsonld_Service
25
+     */
26
+    private $jsonld_service;
27
+
28
+    /**
29
+     * @var Sync_Background_Process
30
+     */
31
+    private $sync_background_process;
32
+
33
+    /**
34
+     * The number of posts processed in one call.
35
+     *
36
+     * @var int The batch size.
37
+     */
38
+    private $batch_size;
39
+
40
+    /**
41
+     * @var Sync_Object_Adapter_Factory
42
+     */
43
+    private $sync_object_adapter_factory;
44
+
45
+    /**
46
+     * @var Sync_Service
47
+     */
48
+    private static $instance;
49
+    private $entity_service;
50
+
51
+    /**
52
+     * Constructor.
53
+     *
54
+     * @param Api_Service                 $api_service The {@link Api_Service} used to communicate with the remote APIs.
55
+     * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory
56
+     * @param Jsonld_Service              $jsonld_service
57
+     * @param \Wordlift_Entity_Service    $entity_service
58
+     */
59
+    public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) {
60
+
61
+        $this->log = \Wordlift_Log_Service::get_logger( get_class() );
62
+
63
+        $this->api_service                 = $api_service;
64
+        $this->sync_object_adapter_factory = $sync_object_adapter_factory;
65
+        $this->jsonld_service              = $jsonld_service;
66
+        $this->entity_service              = $entity_service;
67
+        $this->batch_size                  = 10;
68
+
69
+        // You need to initialize this early, otherwise the Background Process isn't registered in AJAX calls.
70
+        // $this->sync_background_process = new Sync_Background_Process( $this );;
71
+
72
+        // Exclude the JSONLD_HASH meta key from those that require a resync.
73
+        add_filter(
74
+            'wl_dataset__sync_hooks__ignored_meta_keys',
75
+            function ( $args ) {
76
+                $args[] = Sync_Service::JSONLD_HASH;
77
+                $args[] = Sync_Service::SYNCED_GMT;
78
+
79
+                return $args;
80
+            }
81
+        );
82
+
83
+        self::$instance = $this;
84
+    }
85
+
86
+    public static function get_instance() {
87
+        return self::$instance;
88
+    }
89
+
90
+    /**
91
+     * @param int $type
92
+     * @param int $object_id
93
+     *
94
+     * @return array|false
95
+     * @throws \Exception when an error occurs.
96
+     */
97
+    public function sync_one( $type, $object_id ) {
98
+
99
+        $object = $this->sync_object_adapter_factory->create( $type, $object_id );
100
+
101
+        return $this->sync_many( array( $object ) );
102
+    }
103
+
104
+    /**
105
+     * @param $type string Post or User.
106
+     * @param $object_id  int Post or User id
107
+     * @param $uri string Entity uri , This needs to be supplied before deletion, if we
108
+     * get it from meta it might not be available.
109
+     *
110
+     * @return bool
111
+     */
112
+    public function delete_one( $type, $object_id, $uri ) {
113
+        // Entity URL isn't set, bail out.
114
+        if ( empty( $uri ) ) {
115
+            return false;
116
+        }
117
+
118
+        $response = $this->api_service->request(
119
+            'DELETE',
120
+            sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) )
121
+        );
122
+
123
+        // Update the sync date in case of success, otherwise log an error.
124
+        if ( ! $response->is_success() ) {
125
+            return false;
126
+        }
127
+
128
+        /**
129
+         * Allow 3rd parties to run additional sync work.
130
+         */
131
+        do_action( 'wl_sync__delete_one', $type, $object_id, $uri );
132
+
133
+        return true;
134
+    }
135
+
136
+    /**
137
+     * @param Sync_Object_Adapter[] $objects
138
+     * @param bool                  $force Force synchronization even if the json-ld hash hasn't changed.
139
+     *
140
+     * @return bool
141
+     * @throws \Exception when an error occurs.
142
+     */
143
+    public function sync_many( $objects, $force = false ) {
144
+
145
+        $hashes   = array();
146
+        $payloads = array();
147
+        /** @var Sync_Object_Adapter $object */
148
+        foreach ( $objects as $object ) {
149
+            // Bail out if no payload.
150
+            $payload_as_string = $this->get_payload_as_string( $object );
151
+            if ( empty( $payload_as_string ) ) {
152
+                continue;
153
+            }
154
+            $new_hash = sha1( $payload_as_string );
155
+            $old_hash = $object->get_value( self::JSONLD_HASH );
156
+
157
+            // JSON-LD hasn't changed, bail out.
158
+            $should_sync = $force || $new_hash !== $old_hash;
159
+            if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) {
160
+                continue;
161
+            }
162
+
163
+            // Collect the hashes and the payloads.
164
+            $hashes[]   = array( $object, $new_hash, $payload_as_string );
165
+            $payloads[] = $payload_as_string;
166
+        }
167
+
168
+        // Bail out if payloads are empty.
169
+        if ( empty( $payloads ) ) {
170
+            return false;
171
+        }
172
+
173
+        // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
174
+        $blocking = apply_filters( 'wl_feature__enable__sync-blocking', false );
175
+        $response = $this->api_service->request(
176
+            'POST',
177
+            '/middleware/dataset/batch',
178
+            array( 'Content-Type' => 'application/json' ),
179
+            // Put the payload in a JSON array w/o decoding/encoding again.
180
+            '[ ' . implode( ', ', $payloads ) . ' ]',
181
+            $blocking ? 60 : 0.001,
182
+            null,
183
+            array( 'blocking' => $blocking )
184
+        );
185
+
186
+        // Update the sync date in case of success, otherwise log an error.
187
+        if ( $blocking && ! $response->is_success() ) {
188
+            return false;
189
+        }
190
+
191
+        // If successful update the hashes and sync datetime.
192
+        foreach ( $hashes as $hash ) {
193
+            $object   = $hash[0];
194
+            $new_hash = $hash[1];
195
+
196
+            $object->set_values(
197
+                array(
198
+                    self::JSONLD_HASH => $new_hash,
199
+                    self::SYNCED_GMT  => current_time( 'mysql', true ),
200
+                )
201
+            );
202
+        }
203
+
204
+        /**
205
+         * Allow 3rd parties to run additional sync work.
206
+         */
207
+        do_action( 'wl_sync__sync_many', $hashes );
208
+
209
+        return true;
210
+    }
211
+
212
+    /**
213
+     * @param Sync_Object_Adapter $object
214
+     *
215
+     * @return false|string
216
+     * @throws \Exception when an error occurs.
217
+     */
218
+    private function get_payload_as_string( $object ) {
219
+        $type             = $object->get_type();
220
+        $object_id        = $object->get_object_id();
221
+        $jsonld_as_string = wp_json_encode(
222
+            apply_filters(
223
+                'wl_dataset__sync_service__sync_item__jsonld',
224
+                $this->jsonld_service->get( $type, $object_id ),
225
+                $type,
226
+                $object_id
227
+            ),
228
+            64
229
+        ); // JSON_UNESCAPED_SLASHES
230
+        $uri              = $this->entity_service->get_uri( $object_id, $type );
231
+
232
+        // Entity URL isn't set, bail out.
233
+        if ( empty( $uri ) ) {
234
+            return false;
235
+        }
236
+
237
+        return wp_json_encode(
238
+            array(
239
+                'uri'     => $uri,
240
+                'model'   => $jsonld_as_string,
241
+                'private' => ! ( $object->is_public() && $object->is_published() ),
242
+            ),
243
+            64
244
+        ); // JSON_UNESCAPED_SLASHES
245
+    }
246
+
247
+    /**
248
+     * @param $post_id
249
+     *
250
+     * @todo Complete the delete item.
251
+     */
252
+    public function delete_item( $post_id ) {
253
+        $uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST );
254
+
255
+        if ( ! isset( $uri ) ) {
256
+            return;
257
+        }
258
+
259
+        // Make a request to the remote endpoint.
260
+        $this->api_service->request(
261
+            'DELETE',
262
+            '/middleware/dataset?uri=' . rawurlencode( $uri ),
263
+            array( 'Content-Type' => 'application/ld+json' )
264
+        );
265
+
266
+    }
267
+
268
+    public function get_batch_size() {
269
+
270
+        return $this->batch_size;
271
+    }
272
+
273
+    public function delete_all() {
274
+        $this->api_service->request( 'DELETE', '/middleware/dataset/all' );
275
+    }
276 276
 
277 277
 }
Please login to merge, or discard this patch.
Spacing   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -56,9 +56,9 @@  discard block
 block discarded – undo
56 56
 	 * @param Jsonld_Service              $jsonld_service
57 57
 	 * @param \Wordlift_Entity_Service    $entity_service
58 58
 	 */
59
-	public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) {
59
+	public function __construct($api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service) {
60 60
 
61
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
61
+		$this->log = \Wordlift_Log_Service::get_logger(get_class());
62 62
 
63 63
 		$this->api_service                 = $api_service;
64 64
 		$this->sync_object_adapter_factory = $sync_object_adapter_factory;
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
 		// Exclude the JSONLD_HASH meta key from those that require a resync.
73 73
 		add_filter(
74 74
 			'wl_dataset__sync_hooks__ignored_meta_keys',
75
-			function ( $args ) {
75
+			function($args) {
76 76
 				$args[] = Sync_Service::JSONLD_HASH;
77 77
 				$args[] = Sync_Service::SYNCED_GMT;
78 78
 
@@ -94,11 +94,11 @@  discard block
 block discarded – undo
94 94
 	 * @return array|false
95 95
 	 * @throws \Exception when an error occurs.
96 96
 	 */
97
-	public function sync_one( $type, $object_id ) {
97
+	public function sync_one($type, $object_id) {
98 98
 
99
-		$object = $this->sync_object_adapter_factory->create( $type, $object_id );
99
+		$object = $this->sync_object_adapter_factory->create($type, $object_id);
100 100
 
101
-		return $this->sync_many( array( $object ) );
101
+		return $this->sync_many(array($object));
102 102
 	}
103 103
 
104 104
 	/**
@@ -109,26 +109,26 @@  discard block
 block discarded – undo
109 109
 	 *
110 110
 	 * @return bool
111 111
 	 */
112
-	public function delete_one( $type, $object_id, $uri ) {
112
+	public function delete_one($type, $object_id, $uri) {
113 113
 		// Entity URL isn't set, bail out.
114
-		if ( empty( $uri ) ) {
114
+		if (empty($uri)) {
115 115
 			return false;
116 116
 		}
117 117
 
118 118
 		$response = $this->api_service->request(
119 119
 			'DELETE',
120
-			sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) )
120
+			sprintf('/middleware/dataset?uri=%s', rawurlencode($uri))
121 121
 		);
122 122
 
123 123
 		// Update the sync date in case of success, otherwise log an error.
124
-		if ( ! $response->is_success() ) {
124
+		if ( ! $response->is_success()) {
125 125
 			return false;
126 126
 		}
127 127
 
128 128
 		/**
129 129
 		 * Allow 3rd parties to run additional sync work.
130 130
 		 */
131
-		do_action( 'wl_sync__delete_one', $type, $object_id, $uri );
131
+		do_action('wl_sync__delete_one', $type, $object_id, $uri);
132 132
 
133 133
 		return true;
134 134
 	}
@@ -140,63 +140,63 @@  discard block
 block discarded – undo
140 140
 	 * @return bool
141 141
 	 * @throws \Exception when an error occurs.
142 142
 	 */
143
-	public function sync_many( $objects, $force = false ) {
143
+	public function sync_many($objects, $force = false) {
144 144
 
145 145
 		$hashes   = array();
146 146
 		$payloads = array();
147 147
 		/** @var Sync_Object_Adapter $object */
148
-		foreach ( $objects as $object ) {
148
+		foreach ($objects as $object) {
149 149
 			// Bail out if no payload.
150
-			$payload_as_string = $this->get_payload_as_string( $object );
151
-			if ( empty( $payload_as_string ) ) {
150
+			$payload_as_string = $this->get_payload_as_string($object);
151
+			if (empty($payload_as_string)) {
152 152
 				continue;
153 153
 			}
154
-			$new_hash = sha1( $payload_as_string );
155
-			$old_hash = $object->get_value( self::JSONLD_HASH );
154
+			$new_hash = sha1($payload_as_string);
155
+			$old_hash = $object->get_value(self::JSONLD_HASH);
156 156
 
157 157
 			// JSON-LD hasn't changed, bail out.
158 158
 			$should_sync = $force || $new_hash !== $old_hash;
159
-			if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) {
159
+			if ( ! apply_filters('wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string)) {
160 160
 				continue;
161 161
 			}
162 162
 
163 163
 			// Collect the hashes and the payloads.
164
-			$hashes[]   = array( $object, $new_hash, $payload_as_string );
164
+			$hashes[]   = array($object, $new_hash, $payload_as_string);
165 165
 			$payloads[] = $payload_as_string;
166 166
 		}
167 167
 
168 168
 		// Bail out if payloads are empty.
169
-		if ( empty( $payloads ) ) {
169
+		if (empty($payloads)) {
170 170
 			return false;
171 171
 		}
172 172
 
173 173
 		// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
174
-		$blocking = apply_filters( 'wl_feature__enable__sync-blocking', false );
174
+		$blocking = apply_filters('wl_feature__enable__sync-blocking', false);
175 175
 		$response = $this->api_service->request(
176 176
 			'POST',
177 177
 			'/middleware/dataset/batch',
178
-			array( 'Content-Type' => 'application/json' ),
178
+			array('Content-Type' => 'application/json'),
179 179
 			// Put the payload in a JSON array w/o decoding/encoding again.
180
-			'[ ' . implode( ', ', $payloads ) . ' ]',
180
+			'[ '.implode(', ', $payloads).' ]',
181 181
 			$blocking ? 60 : 0.001,
182 182
 			null,
183
-			array( 'blocking' => $blocking )
183
+			array('blocking' => $blocking)
184 184
 		);
185 185
 
186 186
 		// Update the sync date in case of success, otherwise log an error.
187
-		if ( $blocking && ! $response->is_success() ) {
187
+		if ($blocking && ! $response->is_success()) {
188 188
 			return false;
189 189
 		}
190 190
 
191 191
 		// If successful update the hashes and sync datetime.
192
-		foreach ( $hashes as $hash ) {
192
+		foreach ($hashes as $hash) {
193 193
 			$object   = $hash[0];
194 194
 			$new_hash = $hash[1];
195 195
 
196 196
 			$object->set_values(
197 197
 				array(
198 198
 					self::JSONLD_HASH => $new_hash,
199
-					self::SYNCED_GMT  => current_time( 'mysql', true ),
199
+					self::SYNCED_GMT  => current_time('mysql', true),
200 200
 				)
201 201
 			);
202 202
 		}
@@ -204,7 +204,7 @@  discard block
 block discarded – undo
204 204
 		/**
205 205
 		 * Allow 3rd parties to run additional sync work.
206 206
 		 */
207
-		do_action( 'wl_sync__sync_many', $hashes );
207
+		do_action('wl_sync__sync_many', $hashes);
208 208
 
209 209
 		return true;
210 210
 	}
@@ -215,22 +215,22 @@  discard block
 block discarded – undo
215 215
 	 * @return false|string
216 216
 	 * @throws \Exception when an error occurs.
217 217
 	 */
218
-	private function get_payload_as_string( $object ) {
218
+	private function get_payload_as_string($object) {
219 219
 		$type             = $object->get_type();
220 220
 		$object_id        = $object->get_object_id();
221 221
 		$jsonld_as_string = wp_json_encode(
222 222
 			apply_filters(
223 223
 				'wl_dataset__sync_service__sync_item__jsonld',
224
-				$this->jsonld_service->get( $type, $object_id ),
224
+				$this->jsonld_service->get($type, $object_id),
225 225
 				$type,
226 226
 				$object_id
227 227
 			),
228 228
 			64
229 229
 		); // JSON_UNESCAPED_SLASHES
230
-		$uri              = $this->entity_service->get_uri( $object_id, $type );
230
+		$uri = $this->entity_service->get_uri($object_id, $type);
231 231
 
232 232
 		// Entity URL isn't set, bail out.
233
-		if ( empty( $uri ) ) {
233
+		if (empty($uri)) {
234 234
 			return false;
235 235
 		}
236 236
 
@@ -238,7 +238,7 @@  discard block
 block discarded – undo
238 238
 			array(
239 239
 				'uri'     => $uri,
240 240
 				'model'   => $jsonld_as_string,
241
-				'private' => ! ( $object->is_public() && $object->is_published() ),
241
+				'private' => ! ($object->is_public() && $object->is_published()),
242 242
 			),
243 243
 			64
244 244
 		); // JSON_UNESCAPED_SLASHES
@@ -249,18 +249,18 @@  discard block
 block discarded – undo
249 249
 	 *
250 250
 	 * @todo Complete the delete item.
251 251
 	 */
252
-	public function delete_item( $post_id ) {
253
-		$uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST );
252
+	public function delete_item($post_id) {
253
+		$uri = $this->entity_service->get_uri($post_id, Object_Type_Enum::POST);
254 254
 
255
-		if ( ! isset( $uri ) ) {
255
+		if ( ! isset($uri)) {
256 256
 			return;
257 257
 		}
258 258
 
259 259
 		// Make a request to the remote endpoint.
260 260
 		$this->api_service->request(
261 261
 			'DELETE',
262
-			'/middleware/dataset?uri=' . rawurlencode( $uri ),
263
-			array( 'Content-Type' => 'application/ld+json' )
262
+			'/middleware/dataset?uri='.rawurlencode($uri),
263
+			array('Content-Type' => 'application/ld+json')
264 264
 		);
265 265
 
266 266
 	}
@@ -271,7 +271,7 @@  discard block
 block discarded – undo
271 271
 	}
272 272
 
273 273
 	public function delete_all() {
274
-		$this->api_service->request( 'DELETE', '/middleware/dataset/all' );
274
+		$this->api_service->request('DELETE', '/middleware/dataset/all');
275 275
 	}
276 276
 
277 277
 }
Please login to merge, or discard this patch.
src/wordlift/dataset/index.php 2 patches
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -13,39 +13,39 @@
 block discarded – undo
13 13
 use Wordlift\Dataset\Sync_User_Hooks;
14 14
 use Wordlift\Jsonld\Jsonld_Service;
15 15
 
16
-if ( ! defined( 'ABSPATH' ) ) {
16
+if ( ! defined('ABSPATH')) {
17 17
 	exit;
18 18
 }
19 19
 
20 20
 // Register the Dataset JSON Endpoint.
21 21
 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
22
-if ( apply_filters( 'wl_feature__enable__dataset-ng', false ) ) {
22
+if (apply_filters('wl_feature__enable__dataset-ng', false)) {
23 23
 
24 24
 	$sync_object_adapter_factory = new Sync_Object_Adapter_Factory();
25
-	$sync_service                = new Sync_Service( Default_Api_Service::get_instance(), $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() );
26
-	new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory );
27
-	new Sync_User_Hooks( $sync_service );
25
+	$sync_service                = new Sync_Service(Default_Api_Service::get_instance(), $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance());
26
+	new Sync_Post_Hooks($sync_service, $sync_object_adapter_factory);
27
+	new Sync_User_Hooks($sync_service);
28 28
 
29 29
 	// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
30
-	if ( apply_filters( 'wl_feature__enable__no-vocabulary-terms', false ) ) {
31
-		new Sync_Term_Hooks( $sync_service, $sync_object_adapter_factory );
30
+	if (apply_filters('wl_feature__enable__no-vocabulary-terms', false)) {
31
+		new Sync_Term_Hooks($sync_service, $sync_object_adapter_factory);
32 32
 	}
33 33
 	/**
34 34
 	 * @since 3.28.0
35 35
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/1186
36 36
 	 */
37
-	new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() );
37
+	new Sync_Hooks_Entity_Relation(Wordlift_Entity_Service::get_instance());
38 38
 
39 39
 	// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
40
-	if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) {
40
+	if (apply_filters('wl_feature__enable__wordpress-ontology', false)) {
41 41
 		new Sync_Hooks_Wordpress_Ontology();
42 42
 	}
43 43
 
44 44
 	// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
45
-	if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) {
45
+	if (apply_filters('wl_feature__enable__sync-background', false)) {
46 46
 		// Set up the sync background process.
47
-		$sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory );
48
-		new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process );
47
+		$sync_background_process = new Sync_Background_Process($sync_service, $sync_object_adapter_factory);
48
+		new Sync_Background_Process_Wpjson_Endpoint($sync_background_process);
49 49
 		new Sync_Page();
50 50
 	}
51 51
 }
Please login to merge, or discard this patch.
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -15,38 +15,38 @@
 block discarded – undo
15 15
 use Wordlift\Jsonld\Jsonld_Service;
16 16
 
17 17
 if ( ! defined( 'ABSPATH' ) ) {
18
-	exit;
18
+    exit;
19 19
 }
20 20
 
21 21
 // Register the Dataset JSON Endpoint.
22 22
 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
23 23
 if ( apply_filters( 'wl_feature__enable__dataset-ng', false ) ) {
24 24
 
25
-	$sync_object_adapter_factory = new Sync_Object_Adapter_Factory();
26
-	$sync_service                = new Sync_Service( Default_Api_Service::get_instance(), $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() );
27
-	new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory );
28
-	new Sync_User_Hooks( $sync_service );
29
-
30
-	// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
31
-	if ( apply_filters( 'wl_feature__enable__no-vocabulary-terms', false ) ) {
32
-		new Sync_Term_Hooks( $sync_service, $sync_object_adapter_factory );
33
-	}
34
-	/**
35
-	 * @since 3.28.0
36
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/1186
37
-	 */
38
-	new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() );
39
-
40
-	// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
41
-	if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) {
42
-		new Sync_Hooks_Wordpress_Ontology();
43
-	}
44
-
45
-	// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
46
-	if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) {
47
-		// Set up the sync background process.
48
-		$sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory );
49
-		new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process );
50
-		new Sync_Page();
51
-	}
25
+    $sync_object_adapter_factory = new Sync_Object_Adapter_Factory();
26
+    $sync_service                = new Sync_Service( Default_Api_Service::get_instance(), $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() );
27
+    new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory );
28
+    new Sync_User_Hooks( $sync_service );
29
+
30
+    // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
31
+    if ( apply_filters( 'wl_feature__enable__no-vocabulary-terms', false ) ) {
32
+        new Sync_Term_Hooks( $sync_service, $sync_object_adapter_factory );
33
+    }
34
+    /**
35
+     * @since 3.28.0
36
+     * @see https://github.com/insideout10/wordlift-plugin/issues/1186
37
+     */
38
+    new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() );
39
+
40
+    // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
41
+    if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) {
42
+        new Sync_Hooks_Wordpress_Ontology();
43
+    }
44
+
45
+    // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
46
+    if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) {
47
+        // Set up the sync background process.
48
+        $sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory );
49
+        new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process );
50
+        new Sync_Page();
51
+    }
52 52
 }
Please login to merge, or discard this patch.
src/wordlift/dataset/class-sync-hooks-wordpress-ontology.php 2 patches
Indentation   +60 added lines, -60 removed lines patch added patch discarded remove patch
@@ -7,65 +7,65 @@
 block discarded – undo
7 7
 // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledClassName
8 8
 class Sync_Hooks_Wordpress_Ontology {
9 9
 
10
-	const HTTP_PURL_ORG_WORDPRESS_1_0 = 'http://purl.org/wordpress/1.0/';
11
-
12
-	public function __construct() {
13
-		add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 );
14
-	}
15
-
16
-	public function jsonld( $jsonld, $type, $object_id ) {
17
-
18
-		$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'id' ] = $object_id;
19
-
20
-		switch ( $type ) {
21
-
22
-			case Object_Type_Enum::TERM:
23
-				$term = get_term( $object_id );
24
-
25
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ]        = 'term';
26
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'name' ]        = $term->name;
27
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'description' ] = $term->description;
28
-				break;
29
-
30
-			case Object_Type_Enum::USER:
31
-				$user = get_userdata( $object_id );
32
-
33
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ]        = 'user';
34
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'displayName' ] = $user->display_name;
35
-				break;
36
-
37
-			case Object_Type_Enum::POST:
38
-				$post = get_post( $object_id );
39
-
40
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ]       = 'post';
41
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'customType' ] = $post->post_type;
42
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'title' ]      = $post->post_title;
43
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'status' ]     = $post->post_status;
44
-				$content = has_blocks( $post ) ?
45
-					do_blocks( $post->post_content ) : do_shortcode( $post->post_content );
46
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'content' ]   = $content;
47
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'permalink' ] = get_permalink( $post );
48
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'sticky' ]    = is_sticky( $post->ID );
49
-
50
-				$taxonomies = get_post_taxonomies( $post );
51
-				$_tmp_terms = array();
52
-				foreach ( $taxonomies as $taxonomy ) {
53
-					$terms = wp_get_post_terms( $post->ID, $taxonomy );
54
-					/** @var \WP_Term $term */
55
-					foreach ( $terms as $term ) {
56
-						$_tmp_terms[] = "$taxonomy:$term->name";
57
-					}
58
-				}
59
-				if ( ! empty( $_tmp_terms ) ) {
60
-					$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'terms' ] = $_tmp_terms;
61
-				}
62
-
63
-				break;
64
-
65
-			default:
66
-		}
67
-
68
-		return $jsonld;
69
-	}
10
+    const HTTP_PURL_ORG_WORDPRESS_1_0 = 'http://purl.org/wordpress/1.0/';
11
+
12
+    public function __construct() {
13
+        add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 );
14
+    }
15
+
16
+    public function jsonld( $jsonld, $type, $object_id ) {
17
+
18
+        $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'id' ] = $object_id;
19
+
20
+        switch ( $type ) {
21
+
22
+            case Object_Type_Enum::TERM:
23
+                $term = get_term( $object_id );
24
+
25
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ]        = 'term';
26
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'name' ]        = $term->name;
27
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'description' ] = $term->description;
28
+                break;
29
+
30
+            case Object_Type_Enum::USER:
31
+                $user = get_userdata( $object_id );
32
+
33
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ]        = 'user';
34
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'displayName' ] = $user->display_name;
35
+                break;
36
+
37
+            case Object_Type_Enum::POST:
38
+                $post = get_post( $object_id );
39
+
40
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ]       = 'post';
41
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'customType' ] = $post->post_type;
42
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'title' ]      = $post->post_title;
43
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'status' ]     = $post->post_status;
44
+                $content = has_blocks( $post ) ?
45
+                    do_blocks( $post->post_content ) : do_shortcode( $post->post_content );
46
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'content' ]   = $content;
47
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'permalink' ] = get_permalink( $post );
48
+                $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'sticky' ]    = is_sticky( $post->ID );
49
+
50
+                $taxonomies = get_post_taxonomies( $post );
51
+                $_tmp_terms = array();
52
+                foreach ( $taxonomies as $taxonomy ) {
53
+                    $terms = wp_get_post_terms( $post->ID, $taxonomy );
54
+                    /** @var \WP_Term $term */
55
+                    foreach ( $terms as $term ) {
56
+                        $_tmp_terms[] = "$taxonomy:$term->name";
57
+                    }
58
+                }
59
+                if ( ! empty( $_tmp_terms ) ) {
60
+                    $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'terms' ] = $_tmp_terms;
61
+                }
62
+
63
+                break;
64
+
65
+            default:
66
+        }
67
+
68
+        return $jsonld;
69
+    }
70 70
 
71 71
 }
Please login to merge, or discard this patch.
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -10,54 +10,54 @@
 block discarded – undo
10 10
 	const HTTP_PURL_ORG_WORDPRESS_1_0 = 'http://purl.org/wordpress/1.0/';
11 11
 
12 12
 	public function __construct() {
13
-		add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 );
13
+		add_filter('wl_dataset__sync_service__sync_item__jsonld', array($this, 'jsonld'), 10, 3);
14 14
 	}
15 15
 
16
-	public function jsonld( $jsonld, $type, $object_id ) {
16
+	public function jsonld($jsonld, $type, $object_id) {
17 17
 
18
-		$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'id' ] = $object_id;
18
+		$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'id'] = $object_id;
19 19
 
20
-		switch ( $type ) {
20
+		switch ($type) {
21 21
 
22 22
 			case Object_Type_Enum::TERM:
23
-				$term = get_term( $object_id );
23
+				$term = get_term($object_id);
24 24
 
25
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ]        = 'term';
26
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'name' ]        = $term->name;
27
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'description' ] = $term->description;
25
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'type']        = 'term';
26
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'name']        = $term->name;
27
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'description'] = $term->description;
28 28
 				break;
29 29
 
30 30
 			case Object_Type_Enum::USER:
31
-				$user = get_userdata( $object_id );
31
+				$user = get_userdata($object_id);
32 32
 
33
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ]        = 'user';
34
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'displayName' ] = $user->display_name;
33
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'type']        = 'user';
34
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'displayName'] = $user->display_name;
35 35
 				break;
36 36
 
37 37
 			case Object_Type_Enum::POST:
38
-				$post = get_post( $object_id );
39
-
40
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ]       = 'post';
41
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'customType' ] = $post->post_type;
42
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'title' ]      = $post->post_title;
43
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'status' ]     = $post->post_status;
44
-				$content = has_blocks( $post ) ?
45
-					do_blocks( $post->post_content ) : do_shortcode( $post->post_content );
46
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'content' ]   = $content;
47
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'permalink' ] = get_permalink( $post );
48
-				$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'sticky' ]    = is_sticky( $post->ID );
49
-
50
-				$taxonomies = get_post_taxonomies( $post );
38
+				$post = get_post($object_id);
39
+
40
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'type']       = 'post';
41
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'customType'] = $post->post_type;
42
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'title']      = $post->post_title;
43
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'status']     = $post->post_status;
44
+				$content = has_blocks($post) ?
45
+					do_blocks($post->post_content) : do_shortcode($post->post_content);
46
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'content']   = $content;
47
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'permalink'] = get_permalink($post);
48
+				$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'sticky']    = is_sticky($post->ID);
49
+
50
+				$taxonomies = get_post_taxonomies($post);
51 51
 				$_tmp_terms = array();
52
-				foreach ( $taxonomies as $taxonomy ) {
53
-					$terms = wp_get_post_terms( $post->ID, $taxonomy );
52
+				foreach ($taxonomies as $taxonomy) {
53
+					$terms = wp_get_post_terms($post->ID, $taxonomy);
54 54
 					/** @var \WP_Term $term */
55
-					foreach ( $terms as $term ) {
55
+					foreach ($terms as $term) {
56 56
 						$_tmp_terms[] = "$taxonomy:$term->name";
57 57
 					}
58 58
 				}
59
-				if ( ! empty( $_tmp_terms ) ) {
60
-					$jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'terms' ] = $_tmp_terms;
59
+				if ( ! empty($_tmp_terms)) {
60
+					$jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'terms'] = $_tmp_terms;
61 61
 				}
62 62
 
63 63
 				break;
Please login to merge, or discard this patch.
src/wordlift/dataset/class-sync-object-adapter-factory.php 2 patches
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -6,37 +6,37 @@
 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 when an error occurs.
15
-	 */
16
-	public 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
-	public function create_many( $type, $object_ids ) {
32
-		$that = $this;
33
-
34
-		return array_map(
35
-			function ( $item ) use ( $type, $that ) {
36
-				return $that->create( $type, $item );
37
-			},
38
-			(array) $object_ids
39
-		);
40
-	}
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 when an error occurs.
15
+     */
16
+    public 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
+    public function create_many( $type, $object_ids ) {
32
+        $that = $this;
33
+
34
+        return array_map(
35
+            function ( $item ) use ( $type, $that ) {
36
+                return $that->create( $type, $item );
37
+            },
38
+            (array) $object_ids
39
+        );
40
+    }
41 41
 
42 42
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 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 when an error occurs.
15 15
 	 */
16
-	public function create( $type, $object_id ) {
16
+	public 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
-	public function create_many( $type, $object_ids ) {
31
+	public function create_many($type, $object_ids) {
32 32
 		$that = $this;
33 33
 
34 34
 		return array_map(
35
-			function ( $item ) use ( $type, $that ) {
36
-				return $that->create( $type, $item );
35
+			function($item) use ($type, $that) {
36
+				return $that->create($type, $item);
37 37
 			},
38 38
 			(array) $object_ids
39 39
 		);
Please login to merge, or discard this patch.
src/wordlift/dataset/class-sync-page.php 2 patches
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -4,49 +4,49 @@  discard block
 block discarded – undo
4 4
 
5 5
 class Sync_Page {
6 6
 
7
-	/**
8
-	 * Sync_Page constructor.
9
-	 */
10
-	public function __construct() {
11
-
12
-		add_action( 'admin_menu', array( $this, 'admin_menu' ) );
13
-
14
-	}
15
-
16
-	public function admin_menu() {
17
-
18
-		add_submenu_page(
19
-			'wl_admin_menu',
20
-			__( 'Synchronize Dataset', 'wordlift' ),
21
-			__( 'Synchronize Dataset', 'wordlift' ),
22
-			'manage_options',
23
-			'wl_dataset_sync',
24
-			array(
25
-				$this,
26
-				'render',
27
-			)
28
-		);
29
-
30
-	}
31
-
32
-	public function render() {
33
-
34
-		wp_enqueue_style(
35
-			'wl-tasks-page',
36
-			plugin_dir_url( __DIR__ ) . 'tasks/admin/assets/tasks-page.css',
37
-			array(),
38
-			\Wordlift::get_instance()->get_version()
39
-		);
40
-
41
-		wp_enqueue_script(
42
-			'wl-dataset-sync-page',
43
-			plugin_dir_url( __FILE__ ) . 'assets/sync-page.js',
44
-			array( 'wp-api' ),
45
-			\Wordlift::get_instance()->get_version(),
46
-			false
47
-		);
48
-
49
-		?>
7
+    /**
8
+     * Sync_Page constructor.
9
+     */
10
+    public function __construct() {
11
+
12
+        add_action( 'admin_menu', array( $this, 'admin_menu' ) );
13
+
14
+    }
15
+
16
+    public function admin_menu() {
17
+
18
+        add_submenu_page(
19
+            'wl_admin_menu',
20
+            __( 'Synchronize Dataset', 'wordlift' ),
21
+            __( 'Synchronize Dataset', 'wordlift' ),
22
+            'manage_options',
23
+            'wl_dataset_sync',
24
+            array(
25
+                $this,
26
+                'render',
27
+            )
28
+        );
29
+
30
+    }
31
+
32
+    public function render() {
33
+
34
+        wp_enqueue_style(
35
+            'wl-tasks-page',
36
+            plugin_dir_url( __DIR__ ) . 'tasks/admin/assets/tasks-page.css',
37
+            array(),
38
+            \Wordlift::get_instance()->get_version()
39
+        );
40
+
41
+        wp_enqueue_script(
42
+            'wl-dataset-sync-page',
43
+            plugin_dir_url( __FILE__ ) . 'assets/sync-page.js',
44
+            array( 'wp-api' ),
45
+            \Wordlift::get_instance()->get_version(),
46
+            false
47
+        );
48
+
49
+        ?>
50 50
 		<div class="wrap">
51 51
 			<h2><?php esc_html_e( 'Synchronize Dataset', 'wordlift' ); ?></h2>
52 52
 
@@ -57,17 +57,17 @@  discard block
 block discarded – undo
57 57
 
58 58
 			<button id="wl-start-btn" type="button" class="button button-large button-primary">
59 59
 			<?php
60
-				esc_html_e( 'Start', 'wordlift' );
61
-			?>
60
+                esc_html_e( 'Start', 'wordlift' );
61
+            ?>
62 62
 				</button>
63 63
 			<button id="wl-stop-btn" type="button" class="button button-large button-primary hidden">
64 64
 			<?php
65
-				esc_html_e( 'Stop', 'wordlift' );
66
-			?>
65
+                esc_html_e( 'Stop', 'wordlift' );
66
+            ?>
67 67
 				</button>
68 68
 
69 69
 		</div>
70 70
 		<?php
71
-	}
71
+    }
72 72
 
73 73
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -9,7 +9,7 @@  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
 
@@ -17,8 +17,8 @@  discard block
 block discarded – undo
17 17
 
18 18
 		add_submenu_page(
19 19
 			'wl_admin_menu',
20
-			__( 'Synchronize Dataset', 'wordlift' ),
21
-			__( 'Synchronize Dataset', 'wordlift' ),
20
+			__('Synchronize Dataset', 'wordlift'),
21
+			__('Synchronize Dataset', 'wordlift'),
22 22
 			'manage_options',
23 23
 			'wl_dataset_sync',
24 24
 			array(
@@ -33,22 +33,22 @@  discard block
 block discarded – undo
33 33
 
34 34
 		wp_enqueue_style(
35 35
 			'wl-tasks-page',
36
-			plugin_dir_url( __DIR__ ) . 'tasks/admin/assets/tasks-page.css',
36
+			plugin_dir_url(__DIR__).'tasks/admin/assets/tasks-page.css',
37 37
 			array(),
38 38
 			\Wordlift::get_instance()->get_version()
39 39
 		);
40 40
 
41 41
 		wp_enqueue_script(
42 42
 			'wl-dataset-sync-page',
43
-			plugin_dir_url( __FILE__ ) . 'assets/sync-page.js',
44
-			array( 'wp-api' ),
43
+			plugin_dir_url(__FILE__).'assets/sync-page.js',
44
+			array('wp-api'),
45 45
 			\Wordlift::get_instance()->get_version(),
46 46
 			false
47 47
 		);
48 48
 
49 49
 		?>
50 50
 		<div class="wrap">
51
-			<h2><?php esc_html_e( 'Synchronize Dataset', 'wordlift' ); ?></h2>
51
+			<h2><?php esc_html_e('Synchronize Dataset', 'wordlift'); ?></h2>
52 52
 
53 53
 			<div class="wl-task__progress" style="border: 1px solid #23282D; height: 20px; margin: 8px 0;">
54 54
 				<div class="wl-task__progress__bar"
@@ -57,12 +57,12 @@  discard block
 block discarded – undo
57 57
 
58 58
 			<button id="wl-start-btn" type="button" class="button button-large button-primary">
59 59
 			<?php
60
-				esc_html_e( 'Start', 'wordlift' );
60
+				esc_html_e('Start', 'wordlift');
61 61
 			?>
62 62
 				</button>
63 63
 			<button id="wl-stop-btn" type="button" class="button button-large button-primary hidden">
64 64
 			<?php
65
-				esc_html_e( 'Stop', 'wordlift' );
65
+				esc_html_e('Stop', 'wordlift');
66 66
 			?>
67 67
 				</button>
68 68
 
Please login to merge, or discard this patch.
src/wordlift/faq/class-faq-content-filter.php 2 patches
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -10,33 +10,33 @@
 block discarded – undo
10 10
 namespace Wordlift\Faq;
11 11
 
12 12
 class Faq_Content_Filter {
13
-	/**
14
-	 * Constants used for replacing the tags in the html string.
15
-	 */
16
-	const FAQ_QUESTION_TAG_NAME = 'wl-faq-question';
17
-	const FAQ_ANSWER_TAG_NAME   = 'wl-faq-answer';
18
-	/**
19
-	 * Replaces all the html tags inserted by Faq highlighting code in the front end
20
-	 *
21
-	 * @param $content string Post content
22
-	 * @return string String after replacing all the opening and closing tags.
23
-	 */
24
-	public function remove_all_faq_question_and_answer_tags( $content ) {
25
-		/**
26
-		 * Replace all the question tags.
27
-		 */
28
-		$faq_question_closing_tag = '</' . self::FAQ_QUESTION_TAG_NAME . '>';
29
-		$content                  = preg_replace( '/<wl-faq-question class=".+?">/m', '', $content );
30
-		$content                  = str_replace( $faq_question_closing_tag, '', $content );
31
-		/**
32
-		 * Replace all the answer tags.
33
-		 */
34
-		$faq_answer_closing_tag = '</' . self::FAQ_ANSWER_TAG_NAME . '>';
35
-		$content                = preg_replace( '/<wl-faq-answer class=".+?">/m', '', $content );
36
-		$content                = str_replace( $faq_answer_closing_tag, '', $content );
13
+    /**
14
+     * Constants used for replacing the tags in the html string.
15
+     */
16
+    const FAQ_QUESTION_TAG_NAME = 'wl-faq-question';
17
+    const FAQ_ANSWER_TAG_NAME   = 'wl-faq-answer';
18
+    /**
19
+     * Replaces all the html tags inserted by Faq highlighting code in the front end
20
+     *
21
+     * @param $content string Post content
22
+     * @return string String after replacing all the opening and closing tags.
23
+     */
24
+    public function remove_all_faq_question_and_answer_tags( $content ) {
25
+        /**
26
+         * Replace all the question tags.
27
+         */
28
+        $faq_question_closing_tag = '</' . self::FAQ_QUESTION_TAG_NAME . '>';
29
+        $content                  = preg_replace( '/<wl-faq-question class=".+?">/m', '', $content );
30
+        $content                  = str_replace( $faq_question_closing_tag, '', $content );
31
+        /**
32
+         * Replace all the answer tags.
33
+         */
34
+        $faq_answer_closing_tag = '</' . self::FAQ_ANSWER_TAG_NAME . '>';
35
+        $content                = preg_replace( '/<wl-faq-answer class=".+?">/m', '', $content );
36
+        $content                = str_replace( $faq_answer_closing_tag, '', $content );
37 37
 
38
-		/** Return all the replaced content */
39
-		return $content;
40
-	}
38
+        /** Return all the replaced content */
39
+        return $content;
40
+    }
41 41
 
42 42
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -21,19 +21,19 @@
 block discarded – undo
21 21
 	 * @param $content string Post content
22 22
 	 * @return string String after replacing all the opening and closing tags.
23 23
 	 */
24
-	public function remove_all_faq_question_and_answer_tags( $content ) {
24
+	public function remove_all_faq_question_and_answer_tags($content) {
25 25
 		/**
26 26
 		 * Replace all the question tags.
27 27
 		 */
28
-		$faq_question_closing_tag = '</' . self::FAQ_QUESTION_TAG_NAME . '>';
29
-		$content                  = preg_replace( '/<wl-faq-question class=".+?">/m', '', $content );
30
-		$content                  = str_replace( $faq_question_closing_tag, '', $content );
28
+		$faq_question_closing_tag = '</'.self::FAQ_QUESTION_TAG_NAME.'>';
29
+		$content                  = preg_replace('/<wl-faq-question class=".+?">/m', '', $content);
30
+		$content                  = str_replace($faq_question_closing_tag, '', $content);
31 31
 		/**
32 32
 		 * Replace all the answer tags.
33 33
 		 */
34
-		$faq_answer_closing_tag = '</' . self::FAQ_ANSWER_TAG_NAME . '>';
35
-		$content                = preg_replace( '/<wl-faq-answer class=".+?">/m', '', $content );
36
-		$content                = str_replace( $faq_answer_closing_tag, '', $content );
34
+		$faq_answer_closing_tag = '</'.self::FAQ_ANSWER_TAG_NAME.'>';
35
+		$content                = preg_replace('/<wl-faq-answer class=".+?">/m', '', $content);
36
+		$content                = str_replace($faq_answer_closing_tag, '', $content);
37 37
 
38 38
 		/** Return all the replaced content */
39 39
 		return $content;
Please login to merge, or discard this patch.
src/wordlift/faq/class-faq-to-jsonld-converter.php 2 patches
Indentation   +71 added lines, -71 removed lines patch added patch discarded remove patch
@@ -17,80 +17,80 @@
 block discarded – undo
17 17
  */
18 18
 class Faq_To_Jsonld_Converter {
19 19
 
20
-	const FAQ_JSONLD_TYPE = 'FAQPage';
20
+    const FAQ_JSONLD_TYPE = 'FAQPage';
21 21
 
22
-	public function __construct() {
23
-		add_filter( 'wl_post_jsonld', array( $this, 'get_jsonld_for_faq' ), 11, 2 );
24
-		add_filter( 'wl_entity_jsonld', array( $this, 'get_jsonld_for_faq' ), 11, 2 );
25
-	}
22
+    public function __construct() {
23
+        add_filter( 'wl_post_jsonld', array( $this, 'get_jsonld_for_faq' ), 11, 2 );
24
+        add_filter( 'wl_entity_jsonld', array( $this, 'get_jsonld_for_faq' ), 11, 2 );
25
+    }
26 26
 
27
-	/**
28
-	 * Set the FAQ type to the json ld array
29
-	 *
30
-	 * @param $jsonld array The jsonld array.
31
-	 *
32
-	 * @return array Returns the json ld array with the type set.
33
-	 */
34
-	public function set_faq_type( $jsonld ) {
35
-		if ( array_key_exists( '@type', $jsonld ) ) {
36
-			if ( is_string( $jsonld['@type'] ) ) {
37
-				// If a plain string is present, create an array with previous items.
38
-				$jsonld['@type'] = array( $jsonld['@type'], self::FAQ_JSONLD_TYPE );
39
-				return $jsonld;
40
-			}
41
-			// check if it is a array, then append the type.
42
-			if ( is_array( $jsonld['@type'] ) && ! in_array( self::FAQ_JSONLD_TYPE, $jsonld['@type'], true ) ) {
43
-				array_push( $jsonld['@type'], self::FAQ_JSONLD_TYPE );
44
-				return $jsonld;
45
-			}
46
-		} else {
47
-			$jsonld['@type'] = array( self::FAQ_JSONLD_TYPE );
48
-		}
49
-		return $jsonld;
50
-	}
51
-	/**
52
-	 * @param $post_id int The id of the post.
53
-	 *
54
-	 * @return array Get the converted jsonld data
55
-	 */
56
-	public function get_jsonld_for_faq( $jsonld, $post_id ) {
27
+    /**
28
+     * Set the FAQ type to the json ld array
29
+     *
30
+     * @param $jsonld array The jsonld array.
31
+     *
32
+     * @return array Returns the json ld array with the type set.
33
+     */
34
+    public function set_faq_type( $jsonld ) {
35
+        if ( array_key_exists( '@type', $jsonld ) ) {
36
+            if ( is_string( $jsonld['@type'] ) ) {
37
+                // If a plain string is present, create an array with previous items.
38
+                $jsonld['@type'] = array( $jsonld['@type'], self::FAQ_JSONLD_TYPE );
39
+                return $jsonld;
40
+            }
41
+            // check if it is a array, then append the type.
42
+            if ( is_array( $jsonld['@type'] ) && ! in_array( self::FAQ_JSONLD_TYPE, $jsonld['@type'], true ) ) {
43
+                array_push( $jsonld['@type'], self::FAQ_JSONLD_TYPE );
44
+                return $jsonld;
45
+            }
46
+        } else {
47
+            $jsonld['@type'] = array( self::FAQ_JSONLD_TYPE );
48
+        }
49
+        return $jsonld;
50
+    }
51
+    /**
52
+     * @param $post_id int The id of the post.
53
+     *
54
+     * @return array Get the converted jsonld data
55
+     */
56
+    public function get_jsonld_for_faq( $jsonld, $post_id ) {
57 57
 
58
-		$faq_items = get_post_meta( $post_id, Faq_Rest_Controller::FAQ_META_KEY );
59
-		/**
60
-		 * Apply the FAQ mapping only if the FAQ items are present.
61
-		 */
62
-		if ( count( $faq_items ) > 0 ) {
63
-			$faq_data = $this->get_faq_data( $faq_items );
64
-			// Merge the FAQ data with jsonld.
65
-			$jsonld = array_merge( $jsonld, $faq_data );
66
-			// check if the @type is set on json ld
67
-			$jsonld = $this->set_faq_type( $jsonld );
68
-		}
69
-		return $jsonld;
70
-	}
58
+        $faq_items = get_post_meta( $post_id, Faq_Rest_Controller::FAQ_META_KEY );
59
+        /**
60
+         * Apply the FAQ mapping only if the FAQ items are present.
61
+         */
62
+        if ( count( $faq_items ) > 0 ) {
63
+            $faq_data = $this->get_faq_data( $faq_items );
64
+            // Merge the FAQ data with jsonld.
65
+            $jsonld = array_merge( $jsonld, $faq_data );
66
+            // check if the @type is set on json ld
67
+            $jsonld = $this->set_faq_type( $jsonld );
68
+        }
69
+        return $jsonld;
70
+    }
71 71
 
72
-	/**
73
-	 * @param $faq_items array List of FAQ items extracted from the meta.
74
-	 *
75
-	 * @return array Associtative array of type, mainEntity.
76
-	 */
77
-	private function get_faq_data( $faq_items ) {
78
-		$jsonld_data               = array();
79
-		$jsonld_data['mainEntity'] = array();
80
-		foreach ( $faq_items as $faq_item ) {
81
-			if ( 0 === strlen( $faq_item['question'] ) || 0 === strlen( $faq_item['answer'] ) ) {
82
-				// Bail out if question or answer is not present
83
-				continue;
84
-			}
85
-			$faq_data                            = array();
86
-			$faq_data['@type']                   = 'Question';
87
-			$faq_data['name']                    = $faq_item['question'];
88
-			$faq_data['acceptedAnswer']          = array();
89
-			$faq_data['acceptedAnswer']['@type'] = 'Answer';
90
-			$faq_data['acceptedAnswer']['text']  = $faq_item['answer'];
91
-			array_push( $jsonld_data['mainEntity'], $faq_data );
92
-		}
72
+    /**
73
+     * @param $faq_items array List of FAQ items extracted from the meta.
74
+     *
75
+     * @return array Associtative array of type, mainEntity.
76
+     */
77
+    private function get_faq_data( $faq_items ) {
78
+        $jsonld_data               = array();
79
+        $jsonld_data['mainEntity'] = array();
80
+        foreach ( $faq_items as $faq_item ) {
81
+            if ( 0 === strlen( $faq_item['question'] ) || 0 === strlen( $faq_item['answer'] ) ) {
82
+                // Bail out if question or answer is not present
83
+                continue;
84
+            }
85
+            $faq_data                            = array();
86
+            $faq_data['@type']                   = 'Question';
87
+            $faq_data['name']                    = $faq_item['question'];
88
+            $faq_data['acceptedAnswer']          = array();
89
+            $faq_data['acceptedAnswer']['@type'] = 'Answer';
90
+            $faq_data['acceptedAnswer']['text']  = $faq_item['answer'];
91
+            array_push( $jsonld_data['mainEntity'], $faq_data );
92
+        }
93 93
 
94
-		return $jsonld_data;
95
-	}
94
+        return $jsonld_data;
95
+    }
96 96
 }
Please login to merge, or discard this patch.
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -20,8 +20,8 @@  discard block
 block discarded – undo
20 20
 	const FAQ_JSONLD_TYPE = 'FAQPage';
21 21
 
22 22
 	public function __construct() {
23
-		add_filter( 'wl_post_jsonld', array( $this, 'get_jsonld_for_faq' ), 11, 2 );
24
-		add_filter( 'wl_entity_jsonld', array( $this, 'get_jsonld_for_faq' ), 11, 2 );
23
+		add_filter('wl_post_jsonld', array($this, 'get_jsonld_for_faq'), 11, 2);
24
+		add_filter('wl_entity_jsonld', array($this, 'get_jsonld_for_faq'), 11, 2);
25 25
 	}
26 26
 
27 27
 	/**
@@ -31,20 +31,20 @@  discard block
 block discarded – undo
31 31
 	 *
32 32
 	 * @return array Returns the json ld array with the type set.
33 33
 	 */
34
-	public function set_faq_type( $jsonld ) {
35
-		if ( array_key_exists( '@type', $jsonld ) ) {
36
-			if ( is_string( $jsonld['@type'] ) ) {
34
+	public function set_faq_type($jsonld) {
35
+		if (array_key_exists('@type', $jsonld)) {
36
+			if (is_string($jsonld['@type'])) {
37 37
 				// If a plain string is present, create an array with previous items.
38
-				$jsonld['@type'] = array( $jsonld['@type'], self::FAQ_JSONLD_TYPE );
38
+				$jsonld['@type'] = array($jsonld['@type'], self::FAQ_JSONLD_TYPE);
39 39
 				return $jsonld;
40 40
 			}
41 41
 			// check if it is a array, then append the type.
42
-			if ( is_array( $jsonld['@type'] ) && ! in_array( self::FAQ_JSONLD_TYPE, $jsonld['@type'], true ) ) {
43
-				array_push( $jsonld['@type'], self::FAQ_JSONLD_TYPE );
42
+			if (is_array($jsonld['@type']) && ! in_array(self::FAQ_JSONLD_TYPE, $jsonld['@type'], true)) {
43
+				array_push($jsonld['@type'], self::FAQ_JSONLD_TYPE);
44 44
 				return $jsonld;
45 45
 			}
46 46
 		} else {
47
-			$jsonld['@type'] = array( self::FAQ_JSONLD_TYPE );
47
+			$jsonld['@type'] = array(self::FAQ_JSONLD_TYPE);
48 48
 		}
49 49
 		return $jsonld;
50 50
 	}
@@ -53,18 +53,18 @@  discard block
 block discarded – undo
53 53
 	 *
54 54
 	 * @return array Get the converted jsonld data
55 55
 	 */
56
-	public function get_jsonld_for_faq( $jsonld, $post_id ) {
56
+	public function get_jsonld_for_faq($jsonld, $post_id) {
57 57
 
58
-		$faq_items = get_post_meta( $post_id, Faq_Rest_Controller::FAQ_META_KEY );
58
+		$faq_items = get_post_meta($post_id, Faq_Rest_Controller::FAQ_META_KEY);
59 59
 		/**
60 60
 		 * Apply the FAQ mapping only if the FAQ items are present.
61 61
 		 */
62
-		if ( count( $faq_items ) > 0 ) {
63
-			$faq_data = $this->get_faq_data( $faq_items );
62
+		if (count($faq_items) > 0) {
63
+			$faq_data = $this->get_faq_data($faq_items);
64 64
 			// Merge the FAQ data with jsonld.
65
-			$jsonld = array_merge( $jsonld, $faq_data );
65
+			$jsonld = array_merge($jsonld, $faq_data);
66 66
 			// check if the @type is set on json ld
67
-			$jsonld = $this->set_faq_type( $jsonld );
67
+			$jsonld = $this->set_faq_type($jsonld);
68 68
 		}
69 69
 		return $jsonld;
70 70
 	}
@@ -74,11 +74,11 @@  discard block
 block discarded – undo
74 74
 	 *
75 75
 	 * @return array Associtative array of type, mainEntity.
76 76
 	 */
77
-	private function get_faq_data( $faq_items ) {
77
+	private function get_faq_data($faq_items) {
78 78
 		$jsonld_data               = array();
79 79
 		$jsonld_data['mainEntity'] = array();
80
-		foreach ( $faq_items as $faq_item ) {
81
-			if ( 0 === strlen( $faq_item['question'] ) || 0 === strlen( $faq_item['answer'] ) ) {
80
+		foreach ($faq_items as $faq_item) {
81
+			if (0 === strlen($faq_item['question']) || 0 === strlen($faq_item['answer'])) {
82 82
 				// Bail out if question or answer is not present
83 83
 				continue;
84 84
 			}
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
 			$faq_data['acceptedAnswer']          = array();
89 89
 			$faq_data['acceptedAnswer']['@type'] = 'Answer';
90 90
 			$faq_data['acceptedAnswer']['text']  = $faq_item['answer'];
91
-			array_push( $jsonld_data['mainEntity'], $faq_data );
91
+			array_push($jsonld_data['mainEntity'], $faq_data);
92 92
 		}
93 93
 
94 94
 		return $jsonld_data;
Please login to merge, or discard this patch.