@@ -7,235 +7,235 @@ |
||
| 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 | - do_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 | - /** |
|
| 187 | - * Allow 3rd parties to run additional sync work. |
|
| 188 | - */ |
|
| 189 | - do_action( 'wl_sync__sync_many', $payloads, wp_list_pluck( $hashes, 0 ) ); |
|
| 190 | - |
|
| 191 | - return true; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @param Sync_Object_Adapter $object |
|
| 196 | - * |
|
| 197 | - * @return false|string |
|
| 198 | - * @throws Exception |
|
| 199 | - */ |
|
| 200 | - private function get_payload_as_string( $object ) { |
|
| 201 | - $type = $object->get_type(); |
|
| 202 | - $object_id = $object->get_object_id(); |
|
| 203 | - $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 204 | - $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) ); |
|
| 205 | - $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 206 | - |
|
| 207 | - // Entity URL isn't set, bail out. |
|
| 208 | - if ( empty( $uri ) ) { |
|
| 209 | - return false; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - return wp_json_encode( array( |
|
| 213 | - 'uri' => $uri, |
|
| 214 | - 'model' => $jsonld_as_string, |
|
| 215 | - 'private' => ! $object->is_public(), |
|
| 216 | - ) ); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * @param $post_id |
|
| 221 | - * |
|
| 222 | - * @todo Complete the delete item. |
|
| 223 | - */ |
|
| 224 | - public function delete_item( $post_id ) { |
|
| 225 | - $uri = get_post_meta( $post_id, 'entity_url', true ); |
|
| 226 | - // Make a request to the remote endpoint. |
|
| 227 | - $response = $this->api_service->request( |
|
| 228 | - 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 229 | - array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - public function get_batch_size() { |
|
| 233 | - |
|
| 234 | - return $this->batch_size; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - public function delete_all() { |
|
| 238 | - $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 239 | - } |
|
| 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 | + do_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 | + /** |
|
| 187 | + * Allow 3rd parties to run additional sync work. |
|
| 188 | + */ |
|
| 189 | + do_action( 'wl_sync__sync_many', $payloads, wp_list_pluck( $hashes, 0 ) ); |
|
| 190 | + |
|
| 191 | + return true; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @param Sync_Object_Adapter $object |
|
| 196 | + * |
|
| 197 | + * @return false|string |
|
| 198 | + * @throws Exception |
|
| 199 | + */ |
|
| 200 | + private function get_payload_as_string( $object ) { |
|
| 201 | + $type = $object->get_type(); |
|
| 202 | + $object_id = $object->get_object_id(); |
|
| 203 | + $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 204 | + $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) ); |
|
| 205 | + $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 206 | + |
|
| 207 | + // Entity URL isn't set, bail out. |
|
| 208 | + if ( empty( $uri ) ) { |
|
| 209 | + return false; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + return wp_json_encode( array( |
|
| 213 | + 'uri' => $uri, |
|
| 214 | + 'model' => $jsonld_as_string, |
|
| 215 | + 'private' => ! $object->is_public(), |
|
| 216 | + ) ); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * @param $post_id |
|
| 221 | + * |
|
| 222 | + * @todo Complete the delete item. |
|
| 223 | + */ |
|
| 224 | + public function delete_item( $post_id ) { |
|
| 225 | + $uri = get_post_meta( $post_id, 'entity_url', true ); |
|
| 226 | + // Make a request to the remote endpoint. |
|
| 227 | + $response = $this->api_service->request( |
|
| 228 | + 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 229 | + array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + public function get_batch_size() { |
|
| 233 | + |
|
| 234 | + return $this->batch_size; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + public function delete_all() { |
|
| 238 | + $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | 241 | } |
@@ -56,9 +56,9 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 | - do_action( 'wl_sync__delete_one', $type, $object_id, $uri ); |
|
| 126 | + do_action('wl_sync__delete_one', $type, $object_id, $uri); |
|
| 127 | 127 | |
| 128 | 128 | return true; |
| 129 | 129 | } |
@@ -135,58 +135,58 @@ discard block |
||
| 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 | /** |
| 187 | 187 | * Allow 3rd parties to run additional sync work. |
| 188 | 188 | */ |
| 189 | - do_action( 'wl_sync__sync_many', $payloads, wp_list_pluck( $hashes, 0 ) ); |
|
| 189 | + do_action('wl_sync__sync_many', $payloads, wp_list_pluck($hashes, 0)); |
|
| 190 | 190 | |
| 191 | 191 | return true; |
| 192 | 192 | } |
@@ -197,23 +197,23 @@ discard block |
||
| 197 | 197 | * @return false|string |
| 198 | 198 | * @throws Exception |
| 199 | 199 | */ |
| 200 | - private function get_payload_as_string( $object ) { |
|
| 200 | + private function get_payload_as_string($object) { |
|
| 201 | 201 | $type = $object->get_type(); |
| 202 | 202 | $object_id = $object->get_object_id(); |
| 203 | - $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 204 | - $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) ); |
|
| 205 | - $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 203 | + $jsonld_as_string = wp_json_encode(apply_filters('wl_dataset__sync_service__sync_item__jsonld', |
|
| 204 | + $this->jsonld_service->get($type, $object_id), $type, $object_id)); |
|
| 205 | + $uri = $this->entity_service->get_uri($object_id, $type); |
|
| 206 | 206 | |
| 207 | 207 | // Entity URL isn't set, bail out. |
| 208 | - if ( empty( $uri ) ) { |
|
| 208 | + if (empty($uri)) { |
|
| 209 | 209 | return false; |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | - return wp_json_encode( array( |
|
| 212 | + return wp_json_encode(array( |
|
| 213 | 213 | 'uri' => $uri, |
| 214 | 214 | 'model' => $jsonld_as_string, |
| 215 | 215 | 'private' => ! $object->is_public(), |
| 216 | - ) ); |
|
| 216 | + )); |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | /** |
@@ -221,12 +221,12 @@ discard block |
||
| 221 | 221 | * |
| 222 | 222 | * @todo Complete the delete item. |
| 223 | 223 | */ |
| 224 | - public function delete_item( $post_id ) { |
|
| 225 | - $uri = get_post_meta( $post_id, 'entity_url', true ); |
|
| 224 | + public function delete_item($post_id) { |
|
| 225 | + $uri = get_post_meta($post_id, 'entity_url', true); |
|
| 226 | 226 | // Make a request to the remote endpoint. |
| 227 | 227 | $response = $this->api_service->request( |
| 228 | - 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 229 | - array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 228 | + 'DELETE', '/middleware/dataset?uri='.rawurlencode($uri), |
|
| 229 | + array('Content-Type' => 'application/ld+json',) ); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | public function get_batch_size() { |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | public function delete_all() { |
| 238 | - $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 238 | + $this->api_service->request('DELETE', '/middleware/dataset/all'); |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | } |