Completed
Push — develop ( f27ebb...226c4f )
by David
56s
created
src/wordlift/dataset/class-sync-term-hooks.php 2 patches
Indentation   +104 added lines, -104 removed lines patch added patch discarded remove patch
@@ -5,109 +5,109 @@
 block discarded – undo
5 5
 use Wordlift\Object_Type_Enum;
6 6
 
7 7
 class Sync_Term_Hooks extends Abstract_Sync_Hooks {
8
-	/**
9
-	 * @var \Wordlift_Log_Service
10
-	 */
11
-	private $log;
12
-
13
-	/**
14
-	 * @var Sync_Service
15
-	 */
16
-	private $sync_service;
17
-
18
-	/**
19
-	 * @var Sync_Object_Adapter_Factory
20
-	 */
21
-	private $sync_object_factory;
22
-
23
-	/**
24
-	 * Sync_Term_Hooks constructor.
25
-	 *
26
-	 * @param Sync_Service $sync_service
27
-	 * @param Sync_Object_Adapter_Factory $sync_object_factory
28
-	 */
29
-	function __construct( $sync_service, $sync_object_factory ) {
30
-		parent::__construct();
31
-
32
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
33
-
34
-		$this->sync_service        = $sync_service;
35
-		$this->sync_object_factory = $sync_object_factory;
36
-
37
-		$this->register_hooks();
38
-	}
39
-
40
-	private function register_hooks() {
41
-		/**
42
-		 * Register hooks for post and meta.
43
-		 */
44
-		add_action( 'create_term', array( $this, 'do_sync' ) );
45
-		add_action( 'edit_term', array( $this, 'do_sync' ) );
46
-		add_action( 'added_term_meta', array( $this, 'changed_term_meta' ), 10, 4 );
47
-		add_action( 'updated_term_meta', array( $this, 'changed_term_meta' ), 10, 4 );
48
-		add_action( 'deleted_term_meta', array( $this, 'changed_term_meta' ), 10, 4 );
49
-		add_action( 'pre_delete_term', array( $this, 'delete_term' ) );
50
-
51
-	}
52
-
53
-	public function saved_term( $term_id ) {
54
-
55
-		// Sync all the terms without filtering.
56
-		$this->sync( $term_id );
57
-
58
-	}
59
-
60
-	public function changed_term_meta( $meta_id, $term_id, $meta_key, $_meta_value ) {
61
-
62
-		if ( in_array( $meta_key,
63
-			apply_filters( 'wl_dataset__sync_post_hooks__ignored_meta_keys',
64
-				apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys',
65
-					array(
66
-						'_pingme',
67
-						'_encloseme',
68
-						'entity_url',
69
-					) ) ) )
70
-		) {
71
-			return;
72
-		}
73
-
74
-		$this->sync( $term_id );
75
-
76
-	}
77
-
78
-	private function sync( $term_id ) {
79
-
80
-		$this->enqueue( array( 'do_sync', $term_id ) );
81
-	}
82
-
83
-	public function do_sync( $term_id ) {
84
-
85
-		try {
86
-			$term = get_term( $term_id );
87
-			if ( ! isset( $term ) ) {
88
-				return;
89
-			}
90
-			$this->sync_service->sync_many( array(
91
-				$this->sync_object_factory->create( Object_Type_Enum::TERM, $term_id ),
92
-			) );
93
-		} catch ( \Exception $e ) {
94
-			$this->log->error( "An error occurred while trying to sync post $term_id: " . $e->getMessage(), $e );
95
-		}
96
-	}
97
-
98
-	/**
99
-	 * @param $term \WP_Term
100
-	 */
101
-	public function delete_term( $term ) {
102
-		$this->enqueue( array( 'do_delete', $term->term_id ) );
103
-	}
104
-
105
-	public function do_delete( $term_id ) {
106
-		try {
107
-			$this->sync_service->delete_one( Object_Type_Enum::TERM, $term_id,  get_term_meta( $term_id, 'entity_url', true ) );
108
-		} catch ( \Exception $e ) {
109
-			$this->log->error( "An error occurred while trying to delete term $term_id: " . $e->getMessage(), $e );
110
-		}
111
-	}
8
+    /**
9
+     * @var \Wordlift_Log_Service
10
+     */
11
+    private $log;
12
+
13
+    /**
14
+     * @var Sync_Service
15
+     */
16
+    private $sync_service;
17
+
18
+    /**
19
+     * @var Sync_Object_Adapter_Factory
20
+     */
21
+    private $sync_object_factory;
22
+
23
+    /**
24
+     * Sync_Term_Hooks constructor.
25
+     *
26
+     * @param Sync_Service $sync_service
27
+     * @param Sync_Object_Adapter_Factory $sync_object_factory
28
+     */
29
+    function __construct( $sync_service, $sync_object_factory ) {
30
+        parent::__construct();
31
+
32
+        $this->log = \Wordlift_Log_Service::get_logger( get_class() );
33
+
34
+        $this->sync_service        = $sync_service;
35
+        $this->sync_object_factory = $sync_object_factory;
36
+
37
+        $this->register_hooks();
38
+    }
39
+
40
+    private function register_hooks() {
41
+        /**
42
+         * Register hooks for post and meta.
43
+         */
44
+        add_action( 'create_term', array( $this, 'do_sync' ) );
45
+        add_action( 'edit_term', array( $this, 'do_sync' ) );
46
+        add_action( 'added_term_meta', array( $this, 'changed_term_meta' ), 10, 4 );
47
+        add_action( 'updated_term_meta', array( $this, 'changed_term_meta' ), 10, 4 );
48
+        add_action( 'deleted_term_meta', array( $this, 'changed_term_meta' ), 10, 4 );
49
+        add_action( 'pre_delete_term', array( $this, 'delete_term' ) );
50
+
51
+    }
52
+
53
+    public function saved_term( $term_id ) {
54
+
55
+        // Sync all the terms without filtering.
56
+        $this->sync( $term_id );
57
+
58
+    }
59
+
60
+    public function changed_term_meta( $meta_id, $term_id, $meta_key, $_meta_value ) {
61
+
62
+        if ( in_array( $meta_key,
63
+            apply_filters( 'wl_dataset__sync_post_hooks__ignored_meta_keys',
64
+                apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys',
65
+                    array(
66
+                        '_pingme',
67
+                        '_encloseme',
68
+                        'entity_url',
69
+                    ) ) ) )
70
+        ) {
71
+            return;
72
+        }
73
+
74
+        $this->sync( $term_id );
75
+
76
+    }
77
+
78
+    private function sync( $term_id ) {
79
+
80
+        $this->enqueue( array( 'do_sync', $term_id ) );
81
+    }
82
+
83
+    public function do_sync( $term_id ) {
84
+
85
+        try {
86
+            $term = get_term( $term_id );
87
+            if ( ! isset( $term ) ) {
88
+                return;
89
+            }
90
+            $this->sync_service->sync_many( array(
91
+                $this->sync_object_factory->create( Object_Type_Enum::TERM, $term_id ),
92
+            ) );
93
+        } catch ( \Exception $e ) {
94
+            $this->log->error( "An error occurred while trying to sync post $term_id: " . $e->getMessage(), $e );
95
+        }
96
+    }
97
+
98
+    /**
99
+     * @param $term \WP_Term
100
+     */
101
+    public function delete_term( $term ) {
102
+        $this->enqueue( array( 'do_delete', $term->term_id ) );
103
+    }
104
+
105
+    public function do_delete( $term_id ) {
106
+        try {
107
+            $this->sync_service->delete_one( Object_Type_Enum::TERM, $term_id,  get_term_meta( $term_id, 'entity_url', true ) );
108
+        } catch ( \Exception $e ) {
109
+            $this->log->error( "An error occurred while trying to delete term $term_id: " . $e->getMessage(), $e );
110
+        }
111
+    }
112 112
 
113 113
 }
114 114
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -26,10 +26,10 @@  discard block
 block discarded – undo
26 26
 	 * @param Sync_Service $sync_service
27 27
 	 * @param Sync_Object_Adapter_Factory $sync_object_factory
28 28
 	 */
29
-	function __construct( $sync_service, $sync_object_factory ) {
29
+	function __construct($sync_service, $sync_object_factory) {
30 30
 		parent::__construct();
31 31
 
32
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
32
+		$this->log = \Wordlift_Log_Service::get_logger(get_class());
33 33
 
34 34
 		$this->sync_service        = $sync_service;
35 35
 		$this->sync_object_factory = $sync_object_factory;
@@ -41,72 +41,72 @@  discard block
 block discarded – undo
41 41
 		/**
42 42
 		 * Register hooks for post and meta.
43 43
 		 */
44
-		add_action( 'create_term', array( $this, 'do_sync' ) );
45
-		add_action( 'edit_term', array( $this, 'do_sync' ) );
46
-		add_action( 'added_term_meta', array( $this, 'changed_term_meta' ), 10, 4 );
47
-		add_action( 'updated_term_meta', array( $this, 'changed_term_meta' ), 10, 4 );
48
-		add_action( 'deleted_term_meta', array( $this, 'changed_term_meta' ), 10, 4 );
49
-		add_action( 'pre_delete_term', array( $this, 'delete_term' ) );
44
+		add_action('create_term', array($this, 'do_sync'));
45
+		add_action('edit_term', array($this, 'do_sync'));
46
+		add_action('added_term_meta', array($this, 'changed_term_meta'), 10, 4);
47
+		add_action('updated_term_meta', array($this, 'changed_term_meta'), 10, 4);
48
+		add_action('deleted_term_meta', array($this, 'changed_term_meta'), 10, 4);
49
+		add_action('pre_delete_term', array($this, 'delete_term'));
50 50
 
51 51
 	}
52 52
 
53
-	public function saved_term( $term_id ) {
53
+	public function saved_term($term_id) {
54 54
 
55 55
 		// Sync all the terms without filtering.
56
-		$this->sync( $term_id );
56
+		$this->sync($term_id);
57 57
 
58 58
 	}
59 59
 
60
-	public function changed_term_meta( $meta_id, $term_id, $meta_key, $_meta_value ) {
60
+	public function changed_term_meta($meta_id, $term_id, $meta_key, $_meta_value) {
61 61
 
62
-		if ( in_array( $meta_key,
63
-			apply_filters( 'wl_dataset__sync_post_hooks__ignored_meta_keys',
64
-				apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys',
62
+		if (in_array($meta_key,
63
+			apply_filters('wl_dataset__sync_post_hooks__ignored_meta_keys',
64
+				apply_filters('wl_dataset__sync_hooks__ignored_meta_keys',
65 65
 					array(
66 66
 						'_pingme',
67 67
 						'_encloseme',
68 68
 						'entity_url',
69
-					) ) ) )
69
+					))))
70 70
 		) {
71 71
 			return;
72 72
 		}
73 73
 
74
-		$this->sync( $term_id );
74
+		$this->sync($term_id);
75 75
 
76 76
 	}
77 77
 
78
-	private function sync( $term_id ) {
78
+	private function sync($term_id) {
79 79
 
80
-		$this->enqueue( array( 'do_sync', $term_id ) );
80
+		$this->enqueue(array('do_sync', $term_id));
81 81
 	}
82 82
 
83
-	public function do_sync( $term_id ) {
83
+	public function do_sync($term_id) {
84 84
 
85 85
 		try {
86
-			$term = get_term( $term_id );
87
-			if ( ! isset( $term ) ) {
86
+			$term = get_term($term_id);
87
+			if ( ! isset($term)) {
88 88
 				return;
89 89
 			}
90
-			$this->sync_service->sync_many( array(
91
-				$this->sync_object_factory->create( Object_Type_Enum::TERM, $term_id ),
92
-			) );
93
-		} catch ( \Exception $e ) {
94
-			$this->log->error( "An error occurred while trying to sync post $term_id: " . $e->getMessage(), $e );
90
+			$this->sync_service->sync_many(array(
91
+				$this->sync_object_factory->create(Object_Type_Enum::TERM, $term_id),
92
+			));
93
+		} catch (\Exception $e) {
94
+			$this->log->error("An error occurred while trying to sync post $term_id: ".$e->getMessage(), $e);
95 95
 		}
96 96
 	}
97 97
 
98 98
 	/**
99 99
 	 * @param $term \WP_Term
100 100
 	 */
101
-	public function delete_term( $term ) {
102
-		$this->enqueue( array( 'do_delete', $term->term_id ) );
101
+	public function delete_term($term) {
102
+		$this->enqueue(array('do_delete', $term->term_id));
103 103
 	}
104 104
 
105
-	public function do_delete( $term_id ) {
105
+	public function do_delete($term_id) {
106 106
 		try {
107
-			$this->sync_service->delete_one( Object_Type_Enum::TERM, $term_id,  get_term_meta( $term_id, 'entity_url', true ) );
108
-		} catch ( \Exception $e ) {
109
-			$this->log->error( "An error occurred while trying to delete term $term_id: " . $e->getMessage(), $e );
107
+			$this->sync_service->delete_one(Object_Type_Enum::TERM, $term_id, get_term_meta($term_id, 'entity_url', true));
108
+		} catch (\Exception $e) {
109
+			$this->log->error("An error occurred while trying to delete term $term_id: ".$e->getMessage(), $e);
110 110
 		}
111 111
 	}
112 112
 
Please login to merge, or discard this patch.
src/wordlift/dataset/class-sync-service.php 2 patches
Indentation   +227 added lines, -227 removed lines patch added patch discarded remove patch
@@ -7,234 +7,234 @@
 block discarded – undo
7 7
 use Wordlift\Jsonld\Jsonld_Service;
8 8
 
9 9
 class Sync_Service {
10
-	const JSONLD_HASH = '_wl_jsonld_hash';
11
-	const SYNCED_GMT = '_wl_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.
10
+    const JSONLD_HASH = '_wl_jsonld_hash';
11
+    const SYNCED_GMT = '_wl_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 70
 //		$this->sync_background_process = new Sync_Background_Process( $this );;
71 71
 
72
-		// Exclude the JSONLD_HASH meta key from those that require a resync.
73
-		add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) {
74
-			$args[] = Sync_Service::JSONLD_HASH;
75
-			$args[] = Sync_Service::SYNCED_GMT;
76
-
77
-			return $args;
78
-		} );
79
-
80
-		self::$instance = $this;
81
-	}
82
-
83
-	public static function get_instance() {
84
-		return self::$instance;
85
-	}
86
-
87
-	/**
88
-	 * @param int $type
89
-	 * @param int $object_id
90
-	 *
91
-	 * @return array|false
92
-	 * @throws Exception
93
-	 */
94
-	public function sync_one( $type, $object_id ) {
95
-
96
-		$object = $this->sync_object_adapter_factory->create( $type, $object_id );
97
-
98
-		return $this->sync_many( array( $object ) );
99
-	}
100
-
101
-	/**
102
-	 * @param $type string Post or User.
103
-	 * @param $object_id  int Post or User id
104
-	 * @param $uri string Entity uri , This needs to be supplied before deletion, if we
105
-	 * get it from meta it might not be available.
106
-	 *
107
-	 * @return bool
108
-	 */
109
-	public function delete_one( $type, $object_id, $uri ) {
110
-		// Entity URL isn't set, bail out.
111
-		if ( empty( $uri ) ) {
112
-			return false;
113
-		}
114
-
115
-		$response = $this->api_service->request(
116
-			'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) );
117
-
118
-		// Update the sync date in case of success, otherwise log an error.
119
-		if ( ! $response->is_success() ) {
120
-			return false;
121
-		}
122
-
123
-		/**
124
-		 * Allow 3rd parties to run additional sync work.
125
-		 */
126
-		add_action( 'wl_sync__delete_one', $type, $object_id, $uri );
127
-
128
-		return true;
129
-	}
130
-
131
-	/**
132
-	 * @param Sync_Object_Adapter[] $objects
133
-	 * @param bool $force Force synchronization even if the json-ld hash hasn't changed.
134
-	 *
135
-	 * @return bool
136
-	 * @throws Exception
137
-	 */
138
-	public function sync_many( $objects, $force = false ) {
139
-
140
-		$hashes   = array();
141
-		$payloads = array();
142
-		foreach ( $objects as $object ) {
143
-			// Bail out if no payload.
144
-			$payload_as_string = $this->get_payload_as_string( $object );
145
-			if ( empty( $payload_as_string ) ) {
146
-				continue;
147
-			}
148
-			$new_hash = sha1( $payload_as_string );
149
-			$old_hash = $object->get_meta( self::JSONLD_HASH, true );
150
-
151
-			// JSON-LD hasn't changed, bail out.
152
-			if ( ! $force && $new_hash === $old_hash ) {
153
-				continue;
154
-			}
155
-
156
-			// Collect the hashes and the payloads.
157
-			$hashes[]   = array( $object, $new_hash );
158
-			$payloads[] = $payload_as_string;
159
-		}
160
-
161
-		// Bail out if payloads are empty.
162
-		if ( empty( $payloads ) ) {
163
-			return false;
164
-		}
165
-
166
-		$response = $this->api_service->request(
167
-			'POST', '/middleware/dataset/batch',
168
-			array( 'Content-Type' => 'application/json', ),
169
-			// Put the payload in a JSON array w/o decoding/encoding again.
170
-			'[ ' . implode( ', ', $payloads ) . ' ]' );
171
-
172
-		// Update the sync date in case of success, otherwise log an error.
173
-		if ( ! $response->is_success() ) {
174
-			return false;
175
-		}
176
-
177
-		// If successful update the hashes and sync'ed datetime.
178
-		foreach ( $hashes as $hash ) {
179
-			$object   = $hash[0];
180
-			$new_hash = $hash[1];
181
-			$object->update_meta( self::JSONLD_HASH, $new_hash );
182
-			$object->update_meta( self::SYNCED_GMT, current_time( 'mysql', true ) );
183
-		}
184
-
185
-		/**
186
-		 * Allow 3rd parties to run additional sync work.
187
-		 */
188
-		add_action( 'wl_sync__sync_many', $payloads, array_column( $hashes, 0 ) );
189
-
190
-		return true;
191
-	}
192
-
193
-	/**
194
-	 * @param Sync_Object_Adapter $object
195
-	 *
196
-	 * @return false|string
197
-	 * @throws Exception
198
-	 */
199
-	private function get_payload_as_string( $object ) {
200
-		$type             = $object->get_type();
201
-		$object_id        = $object->get_object_id();
202
-		$jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld',
203
-			$this->jsonld_service->get( $type, $object_id ), $type, $object_id ) );
204
-		$uri              = $this->entity_service->get_uri( $object_id, $type );
205
-
206
-		// Entity URL isn't set, bail out.
207
-		if ( empty( $uri ) ) {
208
-			return false;
209
-		}
210
-
211
-		return wp_json_encode( array(
212
-			'uri'     => $uri,
213
-			'model'   => $jsonld_as_string,
214
-			'private' => ! $object->is_public(),
215
-		) );
216
-	}
217
-
218
-	/**
219
-	 * @param $post_id
220
-	 *
221
-	 * @todo Complete the delete item.
222
-	 */
223
-	public function delete_item( $post_id ) {
224
-		$uri = get_post_meta( $post_id, 'entity_url', true );
225
-		// Make a request to the remote endpoint.
226
-		$response = $this->api_service->request(
227
-			'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ),
228
-			array( 'Content-Type' => 'application/ld+json', ) );
229
-	}
230
-
231
-	public function get_batch_size() {
232
-
233
-		return $this->batch_size;
234
-	}
235
-
236
-	public function delete_all() {
237
-		$this->api_service->request( 'DELETE', '/middleware/dataset/all' );
238
-	}
72
+        // Exclude the JSONLD_HASH meta key from those that require a resync.
73
+        add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) {
74
+            $args[] = Sync_Service::JSONLD_HASH;
75
+            $args[] = Sync_Service::SYNCED_GMT;
76
+
77
+            return $args;
78
+        } );
79
+
80
+        self::$instance = $this;
81
+    }
82
+
83
+    public static function get_instance() {
84
+        return self::$instance;
85
+    }
86
+
87
+    /**
88
+     * @param int $type
89
+     * @param int $object_id
90
+     *
91
+     * @return array|false
92
+     * @throws Exception
93
+     */
94
+    public function sync_one( $type, $object_id ) {
95
+
96
+        $object = $this->sync_object_adapter_factory->create( $type, $object_id );
97
+
98
+        return $this->sync_many( array( $object ) );
99
+    }
100
+
101
+    /**
102
+     * @param $type string Post or User.
103
+     * @param $object_id  int Post or User id
104
+     * @param $uri string Entity uri , This needs to be supplied before deletion, if we
105
+     * get it from meta it might not be available.
106
+     *
107
+     * @return bool
108
+     */
109
+    public function delete_one( $type, $object_id, $uri ) {
110
+        // Entity URL isn't set, bail out.
111
+        if ( empty( $uri ) ) {
112
+            return false;
113
+        }
114
+
115
+        $response = $this->api_service->request(
116
+            'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) );
117
+
118
+        // Update the sync date in case of success, otherwise log an error.
119
+        if ( ! $response->is_success() ) {
120
+            return false;
121
+        }
122
+
123
+        /**
124
+         * Allow 3rd parties to run additional sync work.
125
+         */
126
+        add_action( 'wl_sync__delete_one', $type, $object_id, $uri );
127
+
128
+        return true;
129
+    }
130
+
131
+    /**
132
+     * @param Sync_Object_Adapter[] $objects
133
+     * @param bool $force Force synchronization even if the json-ld hash hasn't changed.
134
+     *
135
+     * @return bool
136
+     * @throws Exception
137
+     */
138
+    public function sync_many( $objects, $force = false ) {
139
+
140
+        $hashes   = array();
141
+        $payloads = array();
142
+        foreach ( $objects as $object ) {
143
+            // Bail out if no payload.
144
+            $payload_as_string = $this->get_payload_as_string( $object );
145
+            if ( empty( $payload_as_string ) ) {
146
+                continue;
147
+            }
148
+            $new_hash = sha1( $payload_as_string );
149
+            $old_hash = $object->get_meta( self::JSONLD_HASH, true );
150
+
151
+            // JSON-LD hasn't changed, bail out.
152
+            if ( ! $force && $new_hash === $old_hash ) {
153
+                continue;
154
+            }
155
+
156
+            // Collect the hashes and the payloads.
157
+            $hashes[]   = array( $object, $new_hash );
158
+            $payloads[] = $payload_as_string;
159
+        }
160
+
161
+        // Bail out if payloads are empty.
162
+        if ( empty( $payloads ) ) {
163
+            return false;
164
+        }
165
+
166
+        $response = $this->api_service->request(
167
+            'POST', '/middleware/dataset/batch',
168
+            array( 'Content-Type' => 'application/json', ),
169
+            // Put the payload in a JSON array w/o decoding/encoding again.
170
+            '[ ' . implode( ', ', $payloads ) . ' ]' );
171
+
172
+        // Update the sync date in case of success, otherwise log an error.
173
+        if ( ! $response->is_success() ) {
174
+            return false;
175
+        }
176
+
177
+        // If successful update the hashes and sync'ed datetime.
178
+        foreach ( $hashes as $hash ) {
179
+            $object   = $hash[0];
180
+            $new_hash = $hash[1];
181
+            $object->update_meta( self::JSONLD_HASH, $new_hash );
182
+            $object->update_meta( self::SYNCED_GMT, current_time( 'mysql', true ) );
183
+        }
184
+
185
+        /**
186
+         * Allow 3rd parties to run additional sync work.
187
+         */
188
+        add_action( 'wl_sync__sync_many', $payloads, array_column( $hashes, 0 ) );
189
+
190
+        return true;
191
+    }
192
+
193
+    /**
194
+     * @param Sync_Object_Adapter $object
195
+     *
196
+     * @return false|string
197
+     * @throws Exception
198
+     */
199
+    private function get_payload_as_string( $object ) {
200
+        $type             = $object->get_type();
201
+        $object_id        = $object->get_object_id();
202
+        $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld',
203
+            $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) );
204
+        $uri              = $this->entity_service->get_uri( $object_id, $type );
205
+
206
+        // Entity URL isn't set, bail out.
207
+        if ( empty( $uri ) ) {
208
+            return false;
209
+        }
210
+
211
+        return wp_json_encode( array(
212
+            'uri'     => $uri,
213
+            'model'   => $jsonld_as_string,
214
+            'private' => ! $object->is_public(),
215
+        ) );
216
+    }
217
+
218
+    /**
219
+     * @param $post_id
220
+     *
221
+     * @todo Complete the delete item.
222
+     */
223
+    public function delete_item( $post_id ) {
224
+        $uri = get_post_meta( $post_id, 'entity_url', true );
225
+        // Make a request to the remote endpoint.
226
+        $response = $this->api_service->request(
227
+            'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ),
228
+            array( 'Content-Type' => 'application/ld+json', ) );
229
+    }
230
+
231
+    public function get_batch_size() {
232
+
233
+        return $this->batch_size;
234
+    }
235
+
236
+    public function delete_all() {
237
+        $this->api_service->request( 'DELETE', '/middleware/dataset/all' );
238
+    }
239 239
 
240 240
 }
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;
@@ -70,7 +70,7 @@  discard block
 block discarded – undo
70 70
 //		$this->sync_background_process = new Sync_Background_Process( $this );;
71 71
 
72 72
 		// Exclude the JSONLD_HASH meta key from those that require a resync.
73
-		add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) {
73
+		add_filter('wl_dataset__sync_hooks__ignored_meta_keys', function($args) {
74 74
 			$args[] = Sync_Service::JSONLD_HASH;
75 75
 			$args[] = Sync_Service::SYNCED_GMT;
76 76
 
@@ -91,11 +91,11 @@  discard block
 block discarded – undo
91 91
 	 * @return array|false
92 92
 	 * @throws Exception
93 93
 	 */
94
-	public function sync_one( $type, $object_id ) {
94
+	public function sync_one($type, $object_id) {
95 95
 
96
-		$object = $this->sync_object_adapter_factory->create( $type, $object_id );
96
+		$object = $this->sync_object_adapter_factory->create($type, $object_id);
97 97
 
98
-		return $this->sync_many( array( $object ) );
98
+		return $this->sync_many(array($object));
99 99
 	}
100 100
 
101 101
 	/**
@@ -106,24 +106,24 @@  discard block
 block discarded – undo
106 106
 	 *
107 107
 	 * @return bool
108 108
 	 */
109
-	public function delete_one( $type, $object_id, $uri ) {
109
+	public function delete_one($type, $object_id, $uri) {
110 110
 		// Entity URL isn't set, bail out.
111
-		if ( empty( $uri ) ) {
111
+		if (empty($uri)) {
112 112
 			return false;
113 113
 		}
114 114
 
115 115
 		$response = $this->api_service->request(
116
-			'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) );
116
+			'DELETE', sprintf('/middleware/dataset?uri=%s', rawurlencode($uri)) );
117 117
 
118 118
 		// Update the sync date in case of success, otherwise log an error.
119
-		if ( ! $response->is_success() ) {
119
+		if ( ! $response->is_success()) {
120 120
 			return false;
121 121
 		}
122 122
 
123 123
 		/**
124 124
 		 * Allow 3rd parties to run additional sync work.
125 125
 		 */
126
-		add_action( 'wl_sync__delete_one', $type, $object_id, $uri );
126
+		add_action('wl_sync__delete_one', $type, $object_id, $uri);
127 127
 
128 128
 		return true;
129 129
 	}
@@ -135,57 +135,57 @@  discard block
 block discarded – undo
135 135
 	 * @return bool
136 136
 	 * @throws Exception
137 137
 	 */
138
-	public function sync_many( $objects, $force = false ) {
138
+	public function sync_many($objects, $force = false) {
139 139
 
140 140
 		$hashes   = array();
141 141
 		$payloads = array();
142
-		foreach ( $objects as $object ) {
142
+		foreach ($objects as $object) {
143 143
 			// Bail out if no payload.
144
-			$payload_as_string = $this->get_payload_as_string( $object );
145
-			if ( empty( $payload_as_string ) ) {
144
+			$payload_as_string = $this->get_payload_as_string($object);
145
+			if (empty($payload_as_string)) {
146 146
 				continue;
147 147
 			}
148
-			$new_hash = sha1( $payload_as_string );
149
-			$old_hash = $object->get_meta( self::JSONLD_HASH, true );
148
+			$new_hash = sha1($payload_as_string);
149
+			$old_hash = $object->get_meta(self::JSONLD_HASH, true);
150 150
 
151 151
 			// JSON-LD hasn't changed, bail out.
152
-			if ( ! $force && $new_hash === $old_hash ) {
152
+			if ( ! $force && $new_hash === $old_hash) {
153 153
 				continue;
154 154
 			}
155 155
 
156 156
 			// Collect the hashes and the payloads.
157
-			$hashes[]   = array( $object, $new_hash );
157
+			$hashes[]   = array($object, $new_hash);
158 158
 			$payloads[] = $payload_as_string;
159 159
 		}
160 160
 
161 161
 		// Bail out if payloads are empty.
162
-		if ( empty( $payloads ) ) {
162
+		if (empty($payloads)) {
163 163
 			return false;
164 164
 		}
165 165
 
166 166
 		$response = $this->api_service->request(
167 167
 			'POST', '/middleware/dataset/batch',
168
-			array( 'Content-Type' => 'application/json', ),
168
+			array('Content-Type' => 'application/json',),
169 169
 			// Put the payload in a JSON array w/o decoding/encoding again.
170
-			'[ ' . implode( ', ', $payloads ) . ' ]' );
170
+			'[ '.implode(', ', $payloads).' ]' );
171 171
 
172 172
 		// Update the sync date in case of success, otherwise log an error.
173
-		if ( ! $response->is_success() ) {
173
+		if ( ! $response->is_success()) {
174 174
 			return false;
175 175
 		}
176 176
 
177 177
 		// If successful update the hashes and sync'ed datetime.
178
-		foreach ( $hashes as $hash ) {
178
+		foreach ($hashes as $hash) {
179 179
 			$object   = $hash[0];
180 180
 			$new_hash = $hash[1];
181
-			$object->update_meta( self::JSONLD_HASH, $new_hash );
182
-			$object->update_meta( self::SYNCED_GMT, current_time( 'mysql', true ) );
181
+			$object->update_meta(self::JSONLD_HASH, $new_hash);
182
+			$object->update_meta(self::SYNCED_GMT, current_time('mysql', true));
183 183
 		}
184 184
 
185 185
 		/**
186 186
 		 * Allow 3rd parties to run additional sync work.
187 187
 		 */
188
-		add_action( 'wl_sync__sync_many', $payloads, array_column( $hashes, 0 ) );
188
+		add_action('wl_sync__sync_many', $payloads, array_column($hashes, 0));
189 189
 
190 190
 		return true;
191 191
 	}
@@ -196,23 +196,23 @@  discard block
 block discarded – undo
196 196
 	 * @return false|string
197 197
 	 * @throws Exception
198 198
 	 */
199
-	private function get_payload_as_string( $object ) {
199
+	private function get_payload_as_string($object) {
200 200
 		$type             = $object->get_type();
201 201
 		$object_id        = $object->get_object_id();
202
-		$jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld',
203
-			$this->jsonld_service->get( $type, $object_id ), $type, $object_id ) );
204
-		$uri              = $this->entity_service->get_uri( $object_id, $type );
202
+		$jsonld_as_string = wp_json_encode(apply_filters('wl_dataset__sync_service__sync_item__jsonld',
203
+			$this->jsonld_service->get($type, $object_id), $type, $object_id));
204
+		$uri              = $this->entity_service->get_uri($object_id, $type);
205 205
 
206 206
 		// Entity URL isn't set, bail out.
207
-		if ( empty( $uri ) ) {
207
+		if (empty($uri)) {
208 208
 			return false;
209 209
 		}
210 210
 
211
-		return wp_json_encode( array(
211
+		return wp_json_encode(array(
212 212
 			'uri'     => $uri,
213 213
 			'model'   => $jsonld_as_string,
214 214
 			'private' => ! $object->is_public(),
215
-		) );
215
+		));
216 216
 	}
217 217
 
218 218
 	/**
@@ -220,12 +220,12 @@  discard block
 block discarded – undo
220 220
 	 *
221 221
 	 * @todo Complete the delete item.
222 222
 	 */
223
-	public function delete_item( $post_id ) {
224
-		$uri = get_post_meta( $post_id, 'entity_url', true );
223
+	public function delete_item($post_id) {
224
+		$uri = get_post_meta($post_id, 'entity_url', true);
225 225
 		// Make a request to the remote endpoint.
226 226
 		$response = $this->api_service->request(
227
-			'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ),
228
-			array( 'Content-Type' => 'application/ld+json', ) );
227
+			'DELETE', '/middleware/dataset?uri='.rawurlencode($uri),
228
+			array('Content-Type' => 'application/ld+json',) );
229 229
 	}
230 230
 
231 231
 	public function get_batch_size() {
@@ -234,7 +234,7 @@  discard block
 block discarded – undo
234 234
 	}
235 235
 
236 236
 	public function delete_all() {
237
-		$this->api_service->request( 'DELETE', '/middleware/dataset/all' );
237
+		$this->api_service->request('DELETE', '/middleware/dataset/all');
238 238
 	}
239 239
 
240 240
 }
Please login to merge, or discard this patch.