@@ -15,52 +15,52 @@ |
||
| 15 | 15 | |
| 16 | 16 | abstract class Relation { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var int |
|
| 20 | - */ |
|
| 21 | - private $id; |
|
| 18 | + /** |
|
| 19 | + * @var int |
|
| 20 | + */ |
|
| 21 | + private $id; |
|
| 22 | 22 | |
| 23 | - private $relation_type; |
|
| 23 | + private $relation_type; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Represents a subject type. |
|
| 27 | - * @var int {@link Object_Type_Enum} |
|
| 28 | - */ |
|
| 29 | - private $subject_type; |
|
| 25 | + /** |
|
| 26 | + * Represents a subject type. |
|
| 27 | + * @var int {@link Object_Type_Enum} |
|
| 28 | + */ |
|
| 29 | + private $subject_type; |
|
| 30 | 30 | |
| 31 | - public function __construct( $id, $relation_type, $subject_type ) { |
|
| 32 | - $this->id = $id; |
|
| 33 | - $this->relation_type = $relation_type; |
|
| 34 | - $this->subject_type = $subject_type; |
|
| 35 | - } |
|
| 31 | + public function __construct( $id, $relation_type, $subject_type ) { |
|
| 32 | + $this->id = $id; |
|
| 33 | + $this->relation_type = $relation_type; |
|
| 34 | + $this->subject_type = $subject_type; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Return Object id. |
|
| 39 | - * @return int |
|
| 40 | - */ |
|
| 41 | - public function get_object_id() { |
|
| 42 | - return $this->id; |
|
| 43 | - } |
|
| 37 | + /** |
|
| 38 | + * Return Object id. |
|
| 39 | + * @return int |
|
| 40 | + */ |
|
| 41 | + public function get_object_id() { |
|
| 42 | + return $this->id; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @return int Represents the {@link Object_Type_Enum} |
|
| 47 | - */ |
|
| 48 | - abstract function get_object_type(); |
|
| 45 | + /** |
|
| 46 | + * @return int Represents the {@link Object_Type_Enum} |
|
| 47 | + */ |
|
| 48 | + abstract function get_object_type(); |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @return int Represents the {@link Object_Type_Enum} |
|
| 52 | - */ |
|
| 53 | - function get_subject_type() { |
|
| 54 | - return $this->subject_type; |
|
| 55 | - } |
|
| 50 | + /** |
|
| 51 | + * @return int Represents the {@link Object_Type_Enum} |
|
| 52 | + */ |
|
| 53 | + function get_subject_type() { |
|
| 54 | + return $this->subject_type; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Returns relation type. |
|
| 60 | - * @return string Relation type {@link WL_WHAT_RELATION} etc. |
|
| 61 | - */ |
|
| 62 | - function get_relation_type() { |
|
| 63 | - return $this->relation_type; |
|
| 64 | - } |
|
| 58 | + /** |
|
| 59 | + * Returns relation type. |
|
| 60 | + * @return string Relation type {@link WL_WHAT_RELATION} etc. |
|
| 61 | + */ |
|
| 62 | + function get_relation_type() { |
|
| 63 | + return $this->relation_type; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | 66 | } |
| 67 | 67 | \ No newline at end of file |
@@ -28,7 +28,7 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | private $subject_type; |
| 30 | 30 | |
| 31 | - public function __construct( $id, $relation_type, $subject_type ) { |
|
| 31 | + public function __construct($id, $relation_type, $subject_type) { |
|
| 32 | 32 | $this->id = $id; |
| 33 | 33 | $this->relation_type = $relation_type; |
| 34 | 34 | $this->subject_type = $subject_type; |
@@ -16,7 +16,7 @@ |
||
| 16 | 16 | |
| 17 | 17 | class Term_Relation extends Relation { |
| 18 | 18 | |
| 19 | - function get_object_type() { |
|
| 20 | - return Object_Type_Enum::TERM; |
|
| 21 | - } |
|
| 19 | + function get_object_type() { |
|
| 20 | + return Object_Type_Enum::TERM; |
|
| 21 | + } |
|
| 22 | 22 | } |
| 23 | 23 | \ No newline at end of file |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | |
| 15 | 15 | use Wordlift\Object_Type_Enum; |
| 16 | 16 | |
| 17 | -class Term_Relation extends Relation { |
|
| 17 | +class Term_Relation extends Relation { |
|
| 18 | 18 | |
| 19 | 19 | function get_object_type() { |
| 20 | 20 | return Object_Type_Enum::TERM; |
@@ -16,8 +16,8 @@ |
||
| 16 | 16 | |
| 17 | 17 | class Post_Relation extends Relation { |
| 18 | 18 | |
| 19 | - function get_object_type() { |
|
| 20 | - return Object_Type_Enum::POST; |
|
| 21 | - } |
|
| 19 | + function get_object_type() { |
|
| 20 | + return Object_Type_Enum::POST; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | 23 | } |
| 24 | 24 | \ No newline at end of file |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | |
| 15 | 15 | use Wordlift\Object_Type_Enum; |
| 16 | 16 | |
| 17 | -class Post_Relation extends Relation { |
|
| 17 | +class Post_Relation extends Relation { |
|
| 18 | 18 | |
| 19 | 19 | function get_object_type() { |
| 20 | 20 | return Object_Type_Enum::POST; |
@@ -7,222 +7,222 @@ |
||
| 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 | - return true; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param Sync_Object_Adapter[] $objects |
|
| 128 | - * @param bool $force Force synchronization even if the json-ld hash hasn't changed. |
|
| 129 | - * |
|
| 130 | - * @return bool |
|
| 131 | - * @throws Exception |
|
| 132 | - */ |
|
| 133 | - public function sync_many( $objects, $force = false ) { |
|
| 134 | - |
|
| 135 | - $hashes = array(); |
|
| 136 | - $payloads = array(); |
|
| 137 | - foreach ( $objects as $object ) { |
|
| 138 | - // Bail out if no payload. |
|
| 139 | - $payload_as_string = $this->get_payload_as_string( $object ); |
|
| 140 | - if ( empty( $payload_as_string ) ) { |
|
| 141 | - continue; |
|
| 142 | - } |
|
| 143 | - $new_hash = sha1( $payload_as_string ); |
|
| 144 | - $old_hash = $object->get_meta( self::JSONLD_HASH, true ); |
|
| 145 | - // JSON-LD hasn't changed, bail out. |
|
| 146 | - if ( ! $force && $new_hash === $old_hash ) { |
|
| 147 | - continue; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - // Collect the hashes and the payloads. |
|
| 151 | - $hashes[] = array( $object, $new_hash ); |
|
| 152 | - $payloads[] = $payload_as_string; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - // Bail out if payloads are empty. |
|
| 156 | - if ( empty( $payloads ) ) { |
|
| 157 | - return false; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - $response = $this->api_service->request( |
|
| 161 | - 'POST', '/middleware/dataset/batch', |
|
| 162 | - array( 'Content-Type' => 'application/json', ), |
|
| 163 | - // Put the payload in a JSON array w/o decoding/encoding again. |
|
| 164 | - '[ ' . implode( ', ', $payloads ) . ' ]' ); |
|
| 165 | - // Update the sync date in case of success, otherwise log an error. |
|
| 166 | - if ( ! $response->is_success() ) { |
|
| 167 | - return false; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - // If successful update the hashes and sync'ed datetime. |
|
| 171 | - foreach ( $hashes as $hash ) { |
|
| 172 | - $object = $hash[0]; |
|
| 173 | - $new_hash = $hash[1]; |
|
| 174 | - $object->update_meta( self::JSONLD_HASH, $new_hash ); |
|
| 175 | - $object->update_meta( self::SYNCED_GMT, current_time( 'mysql', true ) ); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - return true; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * @param Sync_Object_Adapter $object |
|
| 183 | - * |
|
| 184 | - * @return false|string |
|
| 185 | - * @throws Exception |
|
| 186 | - */ |
|
| 187 | - private function get_payload_as_string( $object ) { |
|
| 188 | - $type = $object->get_type(); |
|
| 189 | - $object_id = $object->get_object_id(); |
|
| 190 | - $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 191 | - $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) ); |
|
| 192 | - $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 193 | - |
|
| 194 | - // Entity URL isn't set, bail out. |
|
| 195 | - if ( empty( $uri ) ) { |
|
| 196 | - return false; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return wp_json_encode( array( |
|
| 200 | - 'uri' => $uri, |
|
| 201 | - 'model' => $jsonld_as_string, |
|
| 202 | - 'private' => ! $object->is_public(), |
|
| 203 | - ) ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @param $post_id |
|
| 208 | - * |
|
| 209 | - * @todo Complete the delete item. |
|
| 210 | - */ |
|
| 211 | - public function delete_item( $post_id ) { |
|
| 212 | - $uri = get_post_meta( $post_id, 'entity_url', true ); |
|
| 213 | - // Make a request to the remote endpoint. |
|
| 214 | - $response = $this->api_service->request( |
|
| 215 | - 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 216 | - array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - public function get_batch_size() { |
|
| 220 | - |
|
| 221 | - return $this->batch_size; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - public function delete_all() { |
|
| 225 | - $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 226 | - } |
|
| 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 | + return true; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param Sync_Object_Adapter[] $objects |
|
| 128 | + * @param bool $force Force synchronization even if the json-ld hash hasn't changed. |
|
| 129 | + * |
|
| 130 | + * @return bool |
|
| 131 | + * @throws Exception |
|
| 132 | + */ |
|
| 133 | + public function sync_many( $objects, $force = false ) { |
|
| 134 | + |
|
| 135 | + $hashes = array(); |
|
| 136 | + $payloads = array(); |
|
| 137 | + foreach ( $objects as $object ) { |
|
| 138 | + // Bail out if no payload. |
|
| 139 | + $payload_as_string = $this->get_payload_as_string( $object ); |
|
| 140 | + if ( empty( $payload_as_string ) ) { |
|
| 141 | + continue; |
|
| 142 | + } |
|
| 143 | + $new_hash = sha1( $payload_as_string ); |
|
| 144 | + $old_hash = $object->get_meta( self::JSONLD_HASH, true ); |
|
| 145 | + // JSON-LD hasn't changed, bail out. |
|
| 146 | + if ( ! $force && $new_hash === $old_hash ) { |
|
| 147 | + continue; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + // Collect the hashes and the payloads. |
|
| 151 | + $hashes[] = array( $object, $new_hash ); |
|
| 152 | + $payloads[] = $payload_as_string; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + // Bail out if payloads are empty. |
|
| 156 | + if ( empty( $payloads ) ) { |
|
| 157 | + return false; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + $response = $this->api_service->request( |
|
| 161 | + 'POST', '/middleware/dataset/batch', |
|
| 162 | + array( 'Content-Type' => 'application/json', ), |
|
| 163 | + // Put the payload in a JSON array w/o decoding/encoding again. |
|
| 164 | + '[ ' . implode( ', ', $payloads ) . ' ]' ); |
|
| 165 | + // Update the sync date in case of success, otherwise log an error. |
|
| 166 | + if ( ! $response->is_success() ) { |
|
| 167 | + return false; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + // If successful update the hashes and sync'ed datetime. |
|
| 171 | + foreach ( $hashes as $hash ) { |
|
| 172 | + $object = $hash[0]; |
|
| 173 | + $new_hash = $hash[1]; |
|
| 174 | + $object->update_meta( self::JSONLD_HASH, $new_hash ); |
|
| 175 | + $object->update_meta( self::SYNCED_GMT, current_time( 'mysql', true ) ); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + return true; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * @param Sync_Object_Adapter $object |
|
| 183 | + * |
|
| 184 | + * @return false|string |
|
| 185 | + * @throws Exception |
|
| 186 | + */ |
|
| 187 | + private function get_payload_as_string( $object ) { |
|
| 188 | + $type = $object->get_type(); |
|
| 189 | + $object_id = $object->get_object_id(); |
|
| 190 | + $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 191 | + $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) ); |
|
| 192 | + $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 193 | + |
|
| 194 | + // Entity URL isn't set, bail out. |
|
| 195 | + if ( empty( $uri ) ) { |
|
| 196 | + return false; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return wp_json_encode( array( |
|
| 200 | + 'uri' => $uri, |
|
| 201 | + 'model' => $jsonld_as_string, |
|
| 202 | + 'private' => ! $object->is_public(), |
|
| 203 | + ) ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @param $post_id |
|
| 208 | + * |
|
| 209 | + * @todo Complete the delete item. |
|
| 210 | + */ |
|
| 211 | + public function delete_item( $post_id ) { |
|
| 212 | + $uri = get_post_meta( $post_id, 'entity_url', true ); |
|
| 213 | + // Make a request to the remote endpoint. |
|
| 214 | + $response = $this->api_service->request( |
|
| 215 | + 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 216 | + array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + public function get_batch_size() { |
|
| 220 | + |
|
| 221 | + return $this->batch_size; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + public function delete_all() { |
|
| 225 | + $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | 228 | } |
@@ -5,109 +5,109 @@ |
||
| 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 ); |
|
| 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 ); |
|
| 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 |
@@ -26,10 +26,10 @@ discard block |
||
| 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 |
||
| 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 ); |
|
| 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); |
|
| 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 | |
@@ -13,35 +13,35 @@ |
||
| 13 | 13 | use Wordlift\Jsonld\Jsonld_Service; |
| 14 | 14 | |
| 15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | - exit; |
|
| 16 | + exit; |
|
| 17 | 17 | } |
| 18 | 18 | // Register the Dataset JSON Endpoint. The `$api_service` variable must be defined in the calling file (wordlift.php). |
| 19 | 19 | if ( apply_filters( 'wl_feature__enable__dataset-ng', false ) ) { |
| 20 | 20 | |
| 21 | - $sync_object_adapter_factory = new Sync_Object_Adapter_Factory(); |
|
| 22 | - $sync_service = new Sync_Service( $api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
| 23 | - new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 24 | - new Sync_User_Hooks( $sync_service ); |
|
| 25 | - |
|
| 26 | - |
|
| 27 | - if ( apply_filters( 'wl_feature__enable__no-vocabulary-terms', false ) ) { |
|
| 28 | - new Sync_Term_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 29 | - } |
|
| 30 | - /** |
|
| 31 | - * @since 3.28.0 |
|
| 32 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1186 |
|
| 33 | - */ |
|
| 34 | - new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() ); |
|
| 35 | - |
|
| 36 | - if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) { |
|
| 37 | - new Sync_Hooks_Wordpress_Ontology(); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) { |
|
| 41 | - // Set up the sync background process. |
|
| 42 | - $sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory ); |
|
| 43 | - new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process ); |
|
| 44 | - new Sync_Page(); |
|
| 45 | - } |
|
| 21 | + $sync_object_adapter_factory = new Sync_Object_Adapter_Factory(); |
|
| 22 | + $sync_service = new Sync_Service( $api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
| 23 | + new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 24 | + new Sync_User_Hooks( $sync_service ); |
|
| 25 | + |
|
| 26 | + |
|
| 27 | + if ( apply_filters( 'wl_feature__enable__no-vocabulary-terms', false ) ) { |
|
| 28 | + new Sync_Term_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 29 | + } |
|
| 30 | + /** |
|
| 31 | + * @since 3.28.0 |
|
| 32 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1186 |
|
| 33 | + */ |
|
| 34 | + new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() ); |
|
| 35 | + |
|
| 36 | + if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) { |
|
| 37 | + new Sync_Hooks_Wordpress_Ontology(); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) { |
|
| 41 | + // Set up the sync background process. |
|
| 42 | + $sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory ); |
|
| 43 | + new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process ); |
|
| 44 | + new Sync_Page(); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | } |
@@ -12,35 +12,35 @@ |
||
| 12 | 12 | use Wordlift\Dataset\Sync_User_Hooks; |
| 13 | 13 | use Wordlift\Jsonld\Jsonld_Service; |
| 14 | 14 | |
| 15 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 15 | +if ( ! defined('ABSPATH')) { |
|
| 16 | 16 | exit; |
| 17 | 17 | } |
| 18 | 18 | // Register the Dataset JSON Endpoint. The `$api_service` variable must be defined in the calling file (wordlift.php). |
| 19 | -if ( apply_filters( 'wl_feature__enable__dataset-ng', false ) ) { |
|
| 19 | +if (apply_filters('wl_feature__enable__dataset-ng', false)) { |
|
| 20 | 20 | |
| 21 | 21 | $sync_object_adapter_factory = new Sync_Object_Adapter_Factory(); |
| 22 | - $sync_service = new Sync_Service( $api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
| 23 | - new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 24 | - new Sync_User_Hooks( $sync_service ); |
|
| 22 | + $sync_service = new Sync_Service($api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance()); |
|
| 23 | + new Sync_Post_Hooks($sync_service, $sync_object_adapter_factory); |
|
| 24 | + new Sync_User_Hooks($sync_service); |
|
| 25 | 25 | |
| 26 | 26 | |
| 27 | - if ( apply_filters( 'wl_feature__enable__no-vocabulary-terms', false ) ) { |
|
| 28 | - new Sync_Term_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 27 | + if (apply_filters('wl_feature__enable__no-vocabulary-terms', false)) { |
|
| 28 | + new Sync_Term_Hooks($sync_service, $sync_object_adapter_factory); |
|
| 29 | 29 | } |
| 30 | 30 | /** |
| 31 | 31 | * @since 3.28.0 |
| 32 | 32 | * @see https://github.com/insideout10/wordlift-plugin/issues/1186 |
| 33 | 33 | */ |
| 34 | - new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() ); |
|
| 34 | + new Sync_Hooks_Entity_Relation(Wordlift_Entity_Service::get_instance()); |
|
| 35 | 35 | |
| 36 | - if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) { |
|
| 36 | + if (apply_filters('wl_feature__enable__wordpress-ontology', false)) { |
|
| 37 | 37 | new Sync_Hooks_Wordpress_Ontology(); |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) { |
|
| 40 | + if (apply_filters('wl_feature__enable__sync-background', false)) { |
|
| 41 | 41 | // Set up the sync background process. |
| 42 | - $sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory ); |
|
| 43 | - new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process ); |
|
| 42 | + $sync_background_process = new Sync_Background_Process($sync_service, $sync_object_adapter_factory); |
|
| 43 | + new Sync_Background_Process_Wpjson_Endpoint($sync_background_process); |
|
| 44 | 44 | new Sync_Page(); |
| 45 | 45 | } |
| 46 | 46 | |
@@ -16,75 +16,75 @@ |
||
| 16 | 16 | * @package Wordlift\Term |
| 17 | 17 | */ |
| 18 | 18 | class Type_Service extends Singleton { |
| 19 | - /** |
|
| 20 | - * @var \Wordlift_Schema_Service |
|
| 21 | - */ |
|
| 22 | - private $schema_service; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @return Type_Service |
|
| 26 | - */ |
|
| 27 | - public static function get_instance() { |
|
| 28 | - return parent::get_instance(); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public function __construct() { |
|
| 32 | - parent::__construct(); |
|
| 33 | - $this->schema_service = \Wordlift_Schema_Service::get_instance(); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - public function get_entity_types_labels( $term_id ) { |
|
| 37 | - $entity_types = $this->get_entity_types( $term_id ); |
|
| 38 | - return array_map( function ( $entity_type ) { |
|
| 39 | - return $entity_type->name; |
|
| 40 | - }, $entity_types ); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Returns the entity types selected for the term. |
|
| 45 | - * |
|
| 46 | - * @param $term_id int |
|
| 47 | - * |
|
| 48 | - * @return \WP_Term[] |
|
| 49 | - */ |
|
| 50 | - public function get_entity_types( $term_id ) { |
|
| 51 | - |
|
| 52 | - $entity_type_slugs = get_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 53 | - |
|
| 54 | - $types = array_filter( array_map( function ( $type_slug ) { |
|
| 55 | - $term = get_term_by( 'slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 56 | - if ( ! $term ) { |
|
| 57 | - return false; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $term; |
|
| 61 | - }, $entity_type_slugs ) ); |
|
| 62 | - |
|
| 63 | - $types = array_filter( $types ); |
|
| 64 | - |
|
| 65 | - if ( 0 !== count( $types ) ) { |
|
| 66 | - return $types; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return array( get_term_by( 'slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @param $term_id int |
|
| 75 | - * |
|
| 76 | - * @return array|null |
|
| 77 | - */ |
|
| 78 | - public function get_schema( $term_id ) { |
|
| 79 | - $entity_types = $this->get_entity_types( $term_id ); |
|
| 80 | - foreach ( $entity_types as $entity_type ) { |
|
| 81 | - $schema = $this->schema_service->get_schema( $entity_type->slug ); |
|
| 82 | - if ( ! $schema ) { |
|
| 83 | - break; |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - return $this->schema_service->get_schema( 'thing' ); |
|
| 88 | - } |
|
| 19 | + /** |
|
| 20 | + * @var \Wordlift_Schema_Service |
|
| 21 | + */ |
|
| 22 | + private $schema_service; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @return Type_Service |
|
| 26 | + */ |
|
| 27 | + public static function get_instance() { |
|
| 28 | + return parent::get_instance(); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public function __construct() { |
|
| 32 | + parent::__construct(); |
|
| 33 | + $this->schema_service = \Wordlift_Schema_Service::get_instance(); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + public function get_entity_types_labels( $term_id ) { |
|
| 37 | + $entity_types = $this->get_entity_types( $term_id ); |
|
| 38 | + return array_map( function ( $entity_type ) { |
|
| 39 | + return $entity_type->name; |
|
| 40 | + }, $entity_types ); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Returns the entity types selected for the term. |
|
| 45 | + * |
|
| 46 | + * @param $term_id int |
|
| 47 | + * |
|
| 48 | + * @return \WP_Term[] |
|
| 49 | + */ |
|
| 50 | + public function get_entity_types( $term_id ) { |
|
| 51 | + |
|
| 52 | + $entity_type_slugs = get_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 53 | + |
|
| 54 | + $types = array_filter( array_map( function ( $type_slug ) { |
|
| 55 | + $term = get_term_by( 'slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 56 | + if ( ! $term ) { |
|
| 57 | + return false; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $term; |
|
| 61 | + }, $entity_type_slugs ) ); |
|
| 62 | + |
|
| 63 | + $types = array_filter( $types ); |
|
| 64 | + |
|
| 65 | + if ( 0 !== count( $types ) ) { |
|
| 66 | + return $types; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return array( get_term_by( 'slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @param $term_id int |
|
| 75 | + * |
|
| 76 | + * @return array|null |
|
| 77 | + */ |
|
| 78 | + public function get_schema( $term_id ) { |
|
| 79 | + $entity_types = $this->get_entity_types( $term_id ); |
|
| 80 | + foreach ( $entity_types as $entity_type ) { |
|
| 81 | + $schema = $this->schema_service->get_schema( $entity_type->slug ); |
|
| 82 | + if ( ! $schema ) { |
|
| 83 | + break; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + return $this->schema_service->get_schema( 'thing' ); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | 90 | } |
| 91 | 91 | \ No newline at end of file |
@@ -33,11 +33,11 @@ discard block |
||
| 33 | 33 | $this->schema_service = \Wordlift_Schema_Service::get_instance(); |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - public function get_entity_types_labels( $term_id ) { |
|
| 37 | - $entity_types = $this->get_entity_types( $term_id ); |
|
| 38 | - return array_map( function ( $entity_type ) { |
|
| 36 | + public function get_entity_types_labels($term_id) { |
|
| 37 | + $entity_types = $this->get_entity_types($term_id); |
|
| 38 | + return array_map(function($entity_type) { |
|
| 39 | 39 | return $entity_type->name; |
| 40 | - }, $entity_types ); |
|
| 40 | + }, $entity_types); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -47,26 +47,26 @@ discard block |
||
| 47 | 47 | * |
| 48 | 48 | * @return \WP_Term[] |
| 49 | 49 | */ |
| 50 | - public function get_entity_types( $term_id ) { |
|
| 50 | + public function get_entity_types($term_id) { |
|
| 51 | 51 | |
| 52 | - $entity_type_slugs = get_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 52 | + $entity_type_slugs = get_term_meta($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 53 | 53 | |
| 54 | - $types = array_filter( array_map( function ( $type_slug ) { |
|
| 55 | - $term = get_term_by( 'slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 56 | - if ( ! $term ) { |
|
| 54 | + $types = array_filter(array_map(function($type_slug) { |
|
| 55 | + $term = get_term_by('slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 56 | + if ( ! $term) { |
|
| 57 | 57 | return false; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | return $term; |
| 61 | - }, $entity_type_slugs ) ); |
|
| 61 | + }, $entity_type_slugs)); |
|
| 62 | 62 | |
| 63 | - $types = array_filter( $types ); |
|
| 63 | + $types = array_filter($types); |
|
| 64 | 64 | |
| 65 | - if ( 0 !== count( $types ) ) { |
|
| 65 | + if (0 !== count($types)) { |
|
| 66 | 66 | return $types; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - return array( get_term_by( 'slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ); |
|
| 69 | + return array(get_term_by('slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME)); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | |
@@ -75,16 +75,16 @@ discard block |
||
| 75 | 75 | * |
| 76 | 76 | * @return array|null |
| 77 | 77 | */ |
| 78 | - public function get_schema( $term_id ) { |
|
| 79 | - $entity_types = $this->get_entity_types( $term_id ); |
|
| 80 | - foreach ( $entity_types as $entity_type ) { |
|
| 81 | - $schema = $this->schema_service->get_schema( $entity_type->slug ); |
|
| 82 | - if ( ! $schema ) { |
|
| 78 | + public function get_schema($term_id) { |
|
| 79 | + $entity_types = $this->get_entity_types($term_id); |
|
| 80 | + foreach ($entity_types as $entity_type) { |
|
| 81 | + $schema = $this->schema_service->get_schema($entity_type->slug); |
|
| 82 | + if ( ! $schema) { |
|
| 83 | 83 | break; |
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - return $this->schema_service->get_schema( 'thing' ); |
|
| 87 | + return $this->schema_service->get_schema('thing'); |
|
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | } |
| 91 | 91 | \ No newline at end of file |
@@ -10,47 +10,47 @@ |
||
| 10 | 10 | use Wordlift\Common\Singleton; |
| 11 | 11 | |
| 12 | 12 | class Uri_Service extends Singleton { |
| 13 | - /** |
|
| 14 | - * @return Uri_Service |
|
| 15 | - */ |
|
| 16 | - public static function get_instance() { |
|
| 17 | - return parent::get_instance(); |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @param $term_id |
|
| 22 | - * |
|
| 23 | - * @return string |
|
| 24 | - */ |
|
| 25 | - public function get_uri_by_term( $term_id ) { |
|
| 26 | - return get_term_meta( $term_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param $uri string |
|
| 31 | - * |
|
| 32 | - * @return \WP_Term | bool |
|
| 33 | - */ |
|
| 34 | - public function get_term( $uri ) { |
|
| 35 | - |
|
| 36 | - global $wpdb; |
|
| 37 | - $query_template = <<<EOF |
|
| 13 | + /** |
|
| 14 | + * @return Uri_Service |
|
| 15 | + */ |
|
| 16 | + public static function get_instance() { |
|
| 17 | + return parent::get_instance(); |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @param $term_id |
|
| 22 | + * |
|
| 23 | + * @return string |
|
| 24 | + */ |
|
| 25 | + public function get_uri_by_term( $term_id ) { |
|
| 26 | + return get_term_meta( $term_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param $uri string |
|
| 31 | + * |
|
| 32 | + * @return \WP_Term | bool |
|
| 33 | + */ |
|
| 34 | + public function get_term( $uri ) { |
|
| 35 | + |
|
| 36 | + global $wpdb; |
|
| 37 | + $query_template = <<<EOF |
|
| 38 | 38 | SELECT t.term_id FROM $wpdb->terms AS t INNER JOIN $wpdb->termmeta AS tm ON t.term_id = tm.term_id |
| 39 | 39 | WHERE tm.meta_key='entity_url' AND tm.meta_value = %s |
| 40 | 40 | EOF; |
| 41 | - $query = $wpdb->prepare( $query_template, $uri ); |
|
| 41 | + $query = $wpdb->prepare( $query_template, $uri ); |
|
| 42 | 42 | |
| 43 | - $term_ids = $wpdb->get_col( $query ); |
|
| 43 | + $term_ids = $wpdb->get_col( $query ); |
|
| 44 | 44 | |
| 45 | - if ( count( $term_ids ) === 0 ) { |
|
| 46 | - return false; |
|
| 47 | - } |
|
| 45 | + if ( count( $term_ids ) === 0 ) { |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - return get_term( $term_ids[0] ); |
|
| 50 | - } |
|
| 49 | + return get_term( $term_ids[0] ); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - public function set_entity_uri( $term_id, $uri ) { |
|
| 53 | - update_term_meta( $term_id, WL_ENTITY_URL_META_NAME, $uri ); |
|
| 54 | - } |
|
| 52 | + public function set_entity_uri( $term_id, $uri ) { |
|
| 53 | + update_term_meta( $term_id, WL_ENTITY_URL_META_NAME, $uri ); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | 56 | } |
@@ -22,8 +22,8 @@ discard block |
||
| 22 | 22 | * |
| 23 | 23 | * @return string |
| 24 | 24 | */ |
| 25 | - public function get_uri_by_term( $term_id ) { |
|
| 26 | - return get_term_meta( $term_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 25 | + public function get_uri_by_term($term_id) { |
|
| 26 | + return get_term_meta($term_id, WL_ENTITY_URL_META_NAME, true); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | /** |
@@ -31,26 +31,26 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @return \WP_Term | bool |
| 33 | 33 | */ |
| 34 | - public function get_term( $uri ) { |
|
| 34 | + public function get_term($uri) { |
|
| 35 | 35 | |
| 36 | 36 | global $wpdb; |
| 37 | 37 | $query_template = <<<EOF |
| 38 | 38 | SELECT t.term_id FROM $wpdb->terms AS t INNER JOIN $wpdb->termmeta AS tm ON t.term_id = tm.term_id |
| 39 | 39 | WHERE tm.meta_key='entity_url' AND tm.meta_value = %s |
| 40 | 40 | EOF; |
| 41 | - $query = $wpdb->prepare( $query_template, $uri ); |
|
| 41 | + $query = $wpdb->prepare($query_template, $uri); |
|
| 42 | 42 | |
| 43 | - $term_ids = $wpdb->get_col( $query ); |
|
| 43 | + $term_ids = $wpdb->get_col($query); |
|
| 44 | 44 | |
| 45 | - if ( count( $term_ids ) === 0 ) { |
|
| 45 | + if (count($term_ids) === 0) { |
|
| 46 | 46 | return false; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - return get_term( $term_ids[0] ); |
|
| 49 | + return get_term($term_ids[0]); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - public function set_entity_uri( $term_id, $uri ) { |
|
| 53 | - update_term_meta( $term_id, WL_ENTITY_URL_META_NAME, $uri ); |
|
| 52 | + public function set_entity_uri($term_id, $uri) { |
|
| 53 | + update_term_meta($term_id, WL_ENTITY_URL_META_NAME, $uri); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | } |
@@ -11,15 +11,15 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Synonyms_Service extends Singleton { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @return Synonyms_Service |
|
| 16 | - */ |
|
| 17 | - public static function get_instance() { |
|
| 18 | - return parent::get_instance(); |
|
| 19 | - } |
|
| 14 | + /** |
|
| 15 | + * @return Synonyms_Service |
|
| 16 | + */ |
|
| 17 | + public static function get_instance() { |
|
| 18 | + return parent::get_instance(); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - public function get_synonyms( $term_id ) { |
|
| 22 | - return get_term_meta( $term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ); |
|
| 23 | - } |
|
| 21 | + public function get_synonyms( $term_id ) { |
|
| 22 | + return get_term_meta( $term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | 25 | } |
| 26 | 26 | \ No newline at end of file |
@@ -18,8 +18,8 @@ |
||
| 18 | 18 | return parent::get_instance(); |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | - public function get_synonyms( $term_id ) { |
|
| 22 | - return get_term_meta( $term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ); |
|
| 21 | + public function get_synonyms($term_id) { |
|
| 22 | + return get_term_meta($term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | } |
| 26 | 26 | \ No newline at end of file |