@@ -8,251 +8,251 @@ |
||
| 8 | 8 | use Wordlift\Object_Type_Enum; |
| 9 | 9 | |
| 10 | 10 | class Sync_Service { |
| 11 | - const JSONLD_HASH = 'jsonld_hash'; |
|
| 12 | - const SYNCED_GMT = 'synced_gmt'; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * @var \Wordlift_Log_Service |
|
| 16 | - */ |
|
| 17 | - private $log; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var Api_Service |
|
| 21 | - */ |
|
| 22 | - private $api_service; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var Jsonld_Service |
|
| 26 | - */ |
|
| 27 | - private $jsonld_service; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var Sync_Background_Process |
|
| 31 | - */ |
|
| 32 | - private $sync_background_process; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The number of posts processed in one call. |
|
| 36 | - * |
|
| 37 | - * @var int The batch size. |
|
| 38 | - */ |
|
| 39 | - private $batch_size; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var Sync_Object_Adapter_Factory |
|
| 43 | - */ |
|
| 44 | - private $sync_object_adapter_factory; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var Sync_Service |
|
| 48 | - */ |
|
| 49 | - private static $instance; |
|
| 50 | - private $entity_service; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Constructor. |
|
| 54 | - * |
|
| 55 | - * @param Api_Service $api_service The {@link Api_Service} used to communicate with the remote APIs. |
|
| 56 | - * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 57 | - * @param Jsonld_Service $jsonld_service |
|
| 58 | - * @param \Wordlift_Entity_Service $entity_service |
|
| 59 | - */ |
|
| 60 | - public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 61 | - |
|
| 62 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 63 | - |
|
| 64 | - $this->api_service = $api_service; |
|
| 65 | - $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 66 | - $this->jsonld_service = $jsonld_service; |
|
| 67 | - $this->entity_service = $entity_service; |
|
| 68 | - $this->batch_size = 10; |
|
| 69 | - |
|
| 70 | - // You need to initialize this early, otherwise the Background Process isn't registered in AJAX calls. |
|
| 11 | + const JSONLD_HASH = 'jsonld_hash'; |
|
| 12 | + const SYNCED_GMT = 'synced_gmt'; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * @var \Wordlift_Log_Service |
|
| 16 | + */ |
|
| 17 | + private $log; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var Api_Service |
|
| 21 | + */ |
|
| 22 | + private $api_service; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var Jsonld_Service |
|
| 26 | + */ |
|
| 27 | + private $jsonld_service; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var Sync_Background_Process |
|
| 31 | + */ |
|
| 32 | + private $sync_background_process; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The number of posts processed in one call. |
|
| 36 | + * |
|
| 37 | + * @var int The batch size. |
|
| 38 | + */ |
|
| 39 | + private $batch_size; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var Sync_Object_Adapter_Factory |
|
| 43 | + */ |
|
| 44 | + private $sync_object_adapter_factory; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var Sync_Service |
|
| 48 | + */ |
|
| 49 | + private static $instance; |
|
| 50 | + private $entity_service; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Constructor. |
|
| 54 | + * |
|
| 55 | + * @param Api_Service $api_service The {@link Api_Service} used to communicate with the remote APIs. |
|
| 56 | + * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 57 | + * @param Jsonld_Service $jsonld_service |
|
| 58 | + * @param \Wordlift_Entity_Service $entity_service |
|
| 59 | + */ |
|
| 60 | + public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 61 | + |
|
| 62 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 63 | + |
|
| 64 | + $this->api_service = $api_service; |
|
| 65 | + $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 66 | + $this->jsonld_service = $jsonld_service; |
|
| 67 | + $this->entity_service = $entity_service; |
|
| 68 | + $this->batch_size = 10; |
|
| 69 | + |
|
| 70 | + // You need to initialize this early, otherwise the Background Process isn't registered in AJAX calls. |
|
| 71 | 71 | // $this->sync_background_process = new Sync_Background_Process( $this );; |
| 72 | 72 | |
| 73 | - // Exclude the JSONLD_HASH meta key from those that require a resync. |
|
| 74 | - add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 75 | - $args[] = Sync_Service::JSONLD_HASH; |
|
| 76 | - $args[] = Sync_Service::SYNCED_GMT; |
|
| 77 | - |
|
| 78 | - return $args; |
|
| 79 | - } ); |
|
| 80 | - |
|
| 81 | - self::$instance = $this; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public static function get_instance() { |
|
| 85 | - return self::$instance; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param int $type |
|
| 90 | - * @param int $object_id |
|
| 91 | - * |
|
| 92 | - * @return array|false |
|
| 93 | - * @throws Exception |
|
| 94 | - */ |
|
| 95 | - public function sync_one( $type, $object_id ) { |
|
| 96 | - |
|
| 97 | - $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 98 | - |
|
| 99 | - return $this->sync_many( array( $object ) ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param $type string Post or User. |
|
| 104 | - * @param $object_id int Post or User id |
|
| 105 | - * @param $uri string Entity uri , This needs to be supplied before deletion, if we |
|
| 106 | - * get it from meta it might not be available. |
|
| 107 | - * |
|
| 108 | - * @return bool |
|
| 109 | - */ |
|
| 110 | - public function delete_one( $type, $object_id, $uri ) { |
|
| 111 | - // Entity URL isn't set, bail out. |
|
| 112 | - if ( empty( $uri ) ) { |
|
| 113 | - return false; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $response = $this->api_service->request( |
|
| 117 | - 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - // Update the sync date in case of success, otherwise log an error. |
|
| 121 | - if ( ! $response->is_success() ) { |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Allow 3rd parties to run additional sync work. |
|
| 127 | - */ |
|
| 128 | - do_action( 'wl_sync__delete_one', $type, $object_id, $uri ); |
|
| 129 | - |
|
| 130 | - return true; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @param Sync_Object_Adapter[] $objects |
|
| 135 | - * @param bool $force Force synchronization even if the json-ld hash hasn't changed. |
|
| 136 | - * |
|
| 137 | - * @return bool |
|
| 138 | - * @throws Exception |
|
| 139 | - */ |
|
| 140 | - public function sync_many( $objects, $force = false ) { |
|
| 141 | - |
|
| 142 | - $hashes = array(); |
|
| 143 | - $payloads = array(); |
|
| 144 | - /** @var Sync_Object_Adapter $object */ |
|
| 145 | - foreach ( $objects as $object ) { |
|
| 146 | - // Bail out if no payload. |
|
| 147 | - $payload_as_string = $this->get_payload_as_string( $object ); |
|
| 148 | - if ( empty( $payload_as_string ) ) { |
|
| 149 | - continue; |
|
| 150 | - } |
|
| 151 | - $new_hash = sha1( $payload_as_string ); |
|
| 152 | - $old_hash = $object->get_value( self::JSONLD_HASH ); |
|
| 153 | - |
|
| 154 | - // JSON-LD hasn't changed, bail out. |
|
| 155 | - $should_sync = $force || $new_hash !== $old_hash; |
|
| 156 | - if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) { |
|
| 157 | - continue; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - // Collect the hashes and the payloads. |
|
| 161 | - $hashes[] = array( $object, $new_hash, $payload_as_string ); |
|
| 162 | - $payloads[] = $payload_as_string; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - // Bail out if payloads are empty. |
|
| 166 | - if ( empty( $payloads ) ) { |
|
| 167 | - return false; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $blocking = apply_filters( 'wl_feature__enable__sync-blocking', false ); |
|
| 171 | - $response = $this->api_service->request( |
|
| 172 | - 'POST', '/middleware/dataset/batch', |
|
| 173 | - array( 'Content-Type' => 'application/json', ), |
|
| 174 | - // Put the payload in a JSON array w/o decoding/encoding again. |
|
| 175 | - '[ ' . implode( ', ', $payloads ) . ' ]', |
|
| 176 | - $blocking ? 60 : 0.001, |
|
| 177 | - null, |
|
| 178 | - array( 'blocking' => $blocking ) ); |
|
| 179 | - |
|
| 180 | - // Update the sync date in case of success, otherwise log an error. |
|
| 181 | - if ( $blocking && ! $response->is_success() ) { |
|
| 182 | - return false; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // If successful update the hashes and sync datetime. |
|
| 186 | - foreach ( $hashes as $hash ) { |
|
| 187 | - $object = $hash[0]; |
|
| 188 | - $new_hash = $hash[1]; |
|
| 189 | - |
|
| 190 | - $object->set_values( array( |
|
| 191 | - self::JSONLD_HASH => $new_hash, |
|
| 192 | - self::SYNCED_GMT => current_time( 'mysql', true ), |
|
| 193 | - ) ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Allow 3rd parties to run additional sync work. |
|
| 199 | - */ |
|
| 200 | - do_action( 'wl_sync__sync_many', $hashes ); |
|
| 201 | - |
|
| 202 | - return true; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @param Sync_Object_Adapter $object |
|
| 207 | - * |
|
| 208 | - * @return false|string |
|
| 209 | - * @throws Exception |
|
| 210 | - */ |
|
| 211 | - private function get_payload_as_string( $object ) { |
|
| 212 | - $type = $object->get_type(); |
|
| 213 | - $object_id = $object->get_object_id(); |
|
| 214 | - $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 215 | - $this->jsonld_service->get( $type, $object_id ), $type, $object_id ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 216 | - $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 217 | - |
|
| 218 | - // Entity URL isn't set, bail out. |
|
| 219 | - if ( empty( $uri ) ) { |
|
| 220 | - return false; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return wp_json_encode( array( |
|
| 224 | - 'uri' => $uri, |
|
| 225 | - 'model' => $jsonld_as_string, |
|
| 226 | - 'private' => ! ( $object->is_public() && $object->is_published() ), |
|
| 227 | - ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * @param $post_id |
|
| 232 | - * |
|
| 233 | - * @todo Complete the delete item. |
|
| 234 | - */ |
|
| 235 | - public function delete_item( $post_id ) { |
|
| 236 | - $uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST ); |
|
| 237 | - |
|
| 238 | - if ( ! isset( $uri ) ) { |
|
| 239 | - return; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - // Make a request to the remote endpoint. |
|
| 243 | - $response = $this->api_service->request( |
|
| 244 | - 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 245 | - array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 246 | - |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - public function get_batch_size() { |
|
| 250 | - |
|
| 251 | - return $this->batch_size; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - public function delete_all() { |
|
| 255 | - $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 256 | - } |
|
| 73 | + // Exclude the JSONLD_HASH meta key from those that require a resync. |
|
| 74 | + add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 75 | + $args[] = Sync_Service::JSONLD_HASH; |
|
| 76 | + $args[] = Sync_Service::SYNCED_GMT; |
|
| 77 | + |
|
| 78 | + return $args; |
|
| 79 | + } ); |
|
| 80 | + |
|
| 81 | + self::$instance = $this; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public static function get_instance() { |
|
| 85 | + return self::$instance; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param int $type |
|
| 90 | + * @param int $object_id |
|
| 91 | + * |
|
| 92 | + * @return array|false |
|
| 93 | + * @throws Exception |
|
| 94 | + */ |
|
| 95 | + public function sync_one( $type, $object_id ) { |
|
| 96 | + |
|
| 97 | + $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 98 | + |
|
| 99 | + return $this->sync_many( array( $object ) ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param $type string Post or User. |
|
| 104 | + * @param $object_id int Post or User id |
|
| 105 | + * @param $uri string Entity uri , This needs to be supplied before deletion, if we |
|
| 106 | + * get it from meta it might not be available. |
|
| 107 | + * |
|
| 108 | + * @return bool |
|
| 109 | + */ |
|
| 110 | + public function delete_one( $type, $object_id, $uri ) { |
|
| 111 | + // Entity URL isn't set, bail out. |
|
| 112 | + if ( empty( $uri ) ) { |
|
| 113 | + return false; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $response = $this->api_service->request( |
|
| 117 | + 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + // Update the sync date in case of success, otherwise log an error. |
|
| 121 | + if ( ! $response->is_success() ) { |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Allow 3rd parties to run additional sync work. |
|
| 127 | + */ |
|
| 128 | + do_action( 'wl_sync__delete_one', $type, $object_id, $uri ); |
|
| 129 | + |
|
| 130 | + return true; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @param Sync_Object_Adapter[] $objects |
|
| 135 | + * @param bool $force Force synchronization even if the json-ld hash hasn't changed. |
|
| 136 | + * |
|
| 137 | + * @return bool |
|
| 138 | + * @throws Exception |
|
| 139 | + */ |
|
| 140 | + public function sync_many( $objects, $force = false ) { |
|
| 141 | + |
|
| 142 | + $hashes = array(); |
|
| 143 | + $payloads = array(); |
|
| 144 | + /** @var Sync_Object_Adapter $object */ |
|
| 145 | + foreach ( $objects as $object ) { |
|
| 146 | + // Bail out if no payload. |
|
| 147 | + $payload_as_string = $this->get_payload_as_string( $object ); |
|
| 148 | + if ( empty( $payload_as_string ) ) { |
|
| 149 | + continue; |
|
| 150 | + } |
|
| 151 | + $new_hash = sha1( $payload_as_string ); |
|
| 152 | + $old_hash = $object->get_value( self::JSONLD_HASH ); |
|
| 153 | + |
|
| 154 | + // JSON-LD hasn't changed, bail out. |
|
| 155 | + $should_sync = $force || $new_hash !== $old_hash; |
|
| 156 | + if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) { |
|
| 157 | + continue; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + // Collect the hashes and the payloads. |
|
| 161 | + $hashes[] = array( $object, $new_hash, $payload_as_string ); |
|
| 162 | + $payloads[] = $payload_as_string; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + // Bail out if payloads are empty. |
|
| 166 | + if ( empty( $payloads ) ) { |
|
| 167 | + return false; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $blocking = apply_filters( 'wl_feature__enable__sync-blocking', false ); |
|
| 171 | + $response = $this->api_service->request( |
|
| 172 | + 'POST', '/middleware/dataset/batch', |
|
| 173 | + array( 'Content-Type' => 'application/json', ), |
|
| 174 | + // Put the payload in a JSON array w/o decoding/encoding again. |
|
| 175 | + '[ ' . implode( ', ', $payloads ) . ' ]', |
|
| 176 | + $blocking ? 60 : 0.001, |
|
| 177 | + null, |
|
| 178 | + array( 'blocking' => $blocking ) ); |
|
| 179 | + |
|
| 180 | + // Update the sync date in case of success, otherwise log an error. |
|
| 181 | + if ( $blocking && ! $response->is_success() ) { |
|
| 182 | + return false; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // If successful update the hashes and sync datetime. |
|
| 186 | + foreach ( $hashes as $hash ) { |
|
| 187 | + $object = $hash[0]; |
|
| 188 | + $new_hash = $hash[1]; |
|
| 189 | + |
|
| 190 | + $object->set_values( array( |
|
| 191 | + self::JSONLD_HASH => $new_hash, |
|
| 192 | + self::SYNCED_GMT => current_time( 'mysql', true ), |
|
| 193 | + ) ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Allow 3rd parties to run additional sync work. |
|
| 199 | + */ |
|
| 200 | + do_action( 'wl_sync__sync_many', $hashes ); |
|
| 201 | + |
|
| 202 | + return true; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @param Sync_Object_Adapter $object |
|
| 207 | + * |
|
| 208 | + * @return false|string |
|
| 209 | + * @throws Exception |
|
| 210 | + */ |
|
| 211 | + private function get_payload_as_string( $object ) { |
|
| 212 | + $type = $object->get_type(); |
|
| 213 | + $object_id = $object->get_object_id(); |
|
| 214 | + $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 215 | + $this->jsonld_service->get( $type, $object_id ), $type, $object_id ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 216 | + $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 217 | + |
|
| 218 | + // Entity URL isn't set, bail out. |
|
| 219 | + if ( empty( $uri ) ) { |
|
| 220 | + return false; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return wp_json_encode( array( |
|
| 224 | + 'uri' => $uri, |
|
| 225 | + 'model' => $jsonld_as_string, |
|
| 226 | + 'private' => ! ( $object->is_public() && $object->is_published() ), |
|
| 227 | + ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * @param $post_id |
|
| 232 | + * |
|
| 233 | + * @todo Complete the delete item. |
|
| 234 | + */ |
|
| 235 | + public function delete_item( $post_id ) { |
|
| 236 | + $uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST ); |
|
| 237 | + |
|
| 238 | + if ( ! isset( $uri ) ) { |
|
| 239 | + return; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + // Make a request to the remote endpoint. |
|
| 243 | + $response = $this->api_service->request( |
|
| 244 | + 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 245 | + array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 246 | + |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + public function get_batch_size() { |
|
| 250 | + |
|
| 251 | + return $this->batch_size; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + public function delete_all() { |
|
| 255 | + $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | 258 | } |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | * @param Jsonld_Service $jsonld_service |
| 58 | 58 | * @param \Wordlift_Entity_Service $entity_service |
| 59 | 59 | */ |
| 60 | - public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 60 | + public function __construct($api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service) { |
|
| 61 | 61 | |
| 62 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 62 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 63 | 63 | |
| 64 | 64 | $this->api_service = $api_service; |
| 65 | 65 | $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | // $this->sync_background_process = new Sync_Background_Process( $this );; |
| 72 | 72 | |
| 73 | 73 | // Exclude the JSONLD_HASH meta key from those that require a resync. |
| 74 | - add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 74 | + add_filter('wl_dataset__sync_hooks__ignored_meta_keys', function($args) { |
|
| 75 | 75 | $args[] = Sync_Service::JSONLD_HASH; |
| 76 | 76 | $args[] = Sync_Service::SYNCED_GMT; |
| 77 | 77 | |
@@ -92,11 +92,11 @@ discard block |
||
| 92 | 92 | * @return array|false |
| 93 | 93 | * @throws Exception |
| 94 | 94 | */ |
| 95 | - public function sync_one( $type, $object_id ) { |
|
| 95 | + public function sync_one($type, $object_id) { |
|
| 96 | 96 | |
| 97 | - $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 97 | + $object = $this->sync_object_adapter_factory->create($type, $object_id); |
|
| 98 | 98 | |
| 99 | - return $this->sync_many( array( $object ) ); |
|
| 99 | + return $this->sync_many(array($object)); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
@@ -107,25 +107,25 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @return bool |
| 109 | 109 | */ |
| 110 | - public function delete_one( $type, $object_id, $uri ) { |
|
| 110 | + public function delete_one($type, $object_id, $uri) { |
|
| 111 | 111 | // Entity URL isn't set, bail out. |
| 112 | - if ( empty( $uri ) ) { |
|
| 112 | + if (empty($uri)) { |
|
| 113 | 113 | return false; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | $response = $this->api_service->request( |
| 117 | - 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 117 | + 'DELETE', sprintf('/middleware/dataset?uri=%s', rawurlencode($uri)) ); |
|
| 118 | 118 | |
| 119 | 119 | |
| 120 | 120 | // Update the sync date in case of success, otherwise log an error. |
| 121 | - if ( ! $response->is_success() ) { |
|
| 121 | + if ( ! $response->is_success()) { |
|
| 122 | 122 | return false; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
| 126 | 126 | * Allow 3rd parties to run additional sync work. |
| 127 | 127 | */ |
| 128 | - do_action( 'wl_sync__delete_one', $type, $object_id, $uri ); |
|
| 128 | + do_action('wl_sync__delete_one', $type, $object_id, $uri); |
|
| 129 | 129 | |
| 130 | 130 | return true; |
| 131 | 131 | } |
@@ -137,67 +137,67 @@ discard block |
||
| 137 | 137 | * @return bool |
| 138 | 138 | * @throws Exception |
| 139 | 139 | */ |
| 140 | - public function sync_many( $objects, $force = false ) { |
|
| 140 | + public function sync_many($objects, $force = false) { |
|
| 141 | 141 | |
| 142 | 142 | $hashes = array(); |
| 143 | 143 | $payloads = array(); |
| 144 | 144 | /** @var Sync_Object_Adapter $object */ |
| 145 | - foreach ( $objects as $object ) { |
|
| 145 | + foreach ($objects as $object) { |
|
| 146 | 146 | // Bail out if no payload. |
| 147 | - $payload_as_string = $this->get_payload_as_string( $object ); |
|
| 148 | - if ( empty( $payload_as_string ) ) { |
|
| 147 | + $payload_as_string = $this->get_payload_as_string($object); |
|
| 148 | + if (empty($payload_as_string)) { |
|
| 149 | 149 | continue; |
| 150 | 150 | } |
| 151 | - $new_hash = sha1( $payload_as_string ); |
|
| 152 | - $old_hash = $object->get_value( self::JSONLD_HASH ); |
|
| 151 | + $new_hash = sha1($payload_as_string); |
|
| 152 | + $old_hash = $object->get_value(self::JSONLD_HASH); |
|
| 153 | 153 | |
| 154 | 154 | // JSON-LD hasn't changed, bail out. |
| 155 | 155 | $should_sync = $force || $new_hash !== $old_hash; |
| 156 | - if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) { |
|
| 156 | + if ( ! apply_filters('wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string)) { |
|
| 157 | 157 | continue; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // Collect the hashes and the payloads. |
| 161 | - $hashes[] = array( $object, $new_hash, $payload_as_string ); |
|
| 161 | + $hashes[] = array($object, $new_hash, $payload_as_string); |
|
| 162 | 162 | $payloads[] = $payload_as_string; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | // Bail out if payloads are empty. |
| 166 | - if ( empty( $payloads ) ) { |
|
| 166 | + if (empty($payloads)) { |
|
| 167 | 167 | return false; |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | - $blocking = apply_filters( 'wl_feature__enable__sync-blocking', false ); |
|
| 170 | + $blocking = apply_filters('wl_feature__enable__sync-blocking', false); |
|
| 171 | 171 | $response = $this->api_service->request( |
| 172 | 172 | 'POST', '/middleware/dataset/batch', |
| 173 | - array( 'Content-Type' => 'application/json', ), |
|
| 173 | + array('Content-Type' => 'application/json',), |
|
| 174 | 174 | // Put the payload in a JSON array w/o decoding/encoding again. |
| 175 | - '[ ' . implode( ', ', $payloads ) . ' ]', |
|
| 175 | + '[ '.implode(', ', $payloads).' ]', |
|
| 176 | 176 | $blocking ? 60 : 0.001, |
| 177 | 177 | null, |
| 178 | - array( 'blocking' => $blocking ) ); |
|
| 178 | + array('blocking' => $blocking) ); |
|
| 179 | 179 | |
| 180 | 180 | // Update the sync date in case of success, otherwise log an error. |
| 181 | - if ( $blocking && ! $response->is_success() ) { |
|
| 181 | + if ($blocking && ! $response->is_success()) { |
|
| 182 | 182 | return false; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | // If successful update the hashes and sync datetime. |
| 186 | - foreach ( $hashes as $hash ) { |
|
| 186 | + foreach ($hashes as $hash) { |
|
| 187 | 187 | $object = $hash[0]; |
| 188 | 188 | $new_hash = $hash[1]; |
| 189 | 189 | |
| 190 | - $object->set_values( array( |
|
| 190 | + $object->set_values(array( |
|
| 191 | 191 | self::JSONLD_HASH => $new_hash, |
| 192 | - self::SYNCED_GMT => current_time( 'mysql', true ), |
|
| 193 | - ) ); |
|
| 192 | + self::SYNCED_GMT => current_time('mysql', true), |
|
| 193 | + )); |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | |
| 197 | 197 | /** |
| 198 | 198 | * Allow 3rd parties to run additional sync work. |
| 199 | 199 | */ |
| 200 | - do_action( 'wl_sync__sync_many', $hashes ); |
|
| 200 | + do_action('wl_sync__sync_many', $hashes); |
|
| 201 | 201 | |
| 202 | 202 | return true; |
| 203 | 203 | } |
@@ -208,23 +208,23 @@ discard block |
||
| 208 | 208 | * @return false|string |
| 209 | 209 | * @throws Exception |
| 210 | 210 | */ |
| 211 | - private function get_payload_as_string( $object ) { |
|
| 211 | + private function get_payload_as_string($object) { |
|
| 212 | 212 | $type = $object->get_type(); |
| 213 | 213 | $object_id = $object->get_object_id(); |
| 214 | - $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 215 | - $this->jsonld_service->get( $type, $object_id ), $type, $object_id ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 216 | - $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 214 | + $jsonld_as_string = wp_json_encode(apply_filters('wl_dataset__sync_service__sync_item__jsonld', |
|
| 215 | + $this->jsonld_service->get($type, $object_id), $type, $object_id), 64); // JSON_UNESCAPED_SLASHES |
|
| 216 | + $uri = $this->entity_service->get_uri($object_id, $type); |
|
| 217 | 217 | |
| 218 | 218 | // Entity URL isn't set, bail out. |
| 219 | - if ( empty( $uri ) ) { |
|
| 219 | + if (empty($uri)) { |
|
| 220 | 220 | return false; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - return wp_json_encode( array( |
|
| 223 | + return wp_json_encode(array( |
|
| 224 | 224 | 'uri' => $uri, |
| 225 | 225 | 'model' => $jsonld_as_string, |
| 226 | - 'private' => ! ( $object->is_public() && $object->is_published() ), |
|
| 227 | - ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 226 | + 'private' => ! ($object->is_public() && $object->is_published()), |
|
| 227 | + ), 64); // JSON_UNESCAPED_SLASHES |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | /** |
@@ -232,17 +232,17 @@ discard block |
||
| 232 | 232 | * |
| 233 | 233 | * @todo Complete the delete item. |
| 234 | 234 | */ |
| 235 | - public function delete_item( $post_id ) { |
|
| 236 | - $uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST ); |
|
| 235 | + public function delete_item($post_id) { |
|
| 236 | + $uri = $this->entity_service->get_uri($post_id, Object_Type_Enum::POST); |
|
| 237 | 237 | |
| 238 | - if ( ! isset( $uri ) ) { |
|
| 238 | + if ( ! isset($uri)) { |
|
| 239 | 239 | return; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | // Make a request to the remote endpoint. |
| 243 | 243 | $response = $this->api_service->request( |
| 244 | - 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 245 | - array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 244 | + 'DELETE', '/middleware/dataset?uri='.rawurlencode($uri), |
|
| 245 | + array('Content-Type' => 'application/ld+json',) ); |
|
| 246 | 246 | |
| 247 | 247 | } |
| 248 | 248 | |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | public function delete_all() { |
| 255 | - $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 255 | + $this->api_service->request('DELETE', '/middleware/dataset/all'); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | } |
@@ -14,130 +14,130 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Redirect_Service { |
| 16 | 16 | |
| 17 | - const LOD_ENDPOINT = 'http://www.lodview.it'; |
|
| 18 | - const LOD_HOST = 'www.lodview.it'; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The Log service. |
|
| 22 | - * |
|
| 23 | - * @since 3.2.0 |
|
| 24 | - * @access private |
|
| 25 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 26 | - */ |
|
| 27 | - private $log_service; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The Entity URI service. |
|
| 31 | - * |
|
| 32 | - * @since 3.2.0 |
|
| 33 | - * @access private |
|
| 34 | - * @var Wordlift_Entity_Uri_Service $entity_uri_service The Entity service. |
|
| 35 | - */ |
|
| 36 | - private $entity_uri_service; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * A singleton instance of the Redirect service (useful for unit tests). |
|
| 40 | - * |
|
| 41 | - * @since 3.2.0 |
|
| 42 | - * @access private |
|
| 43 | - * @var \Wordlift_Redirect_Service $instance The singleton instance. |
|
| 44 | - */ |
|
| 45 | - private static $instance; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Create a Wordlift_Redirect_Service instance. |
|
| 49 | - * |
|
| 50 | - * @param Wordlift_Entity_Uri_Service $entity_uri_service The Entity service. |
|
| 51 | - * |
|
| 52 | - * @since 3.2.0 |
|
| 53 | - * |
|
| 54 | - */ |
|
| 55 | - public function __construct( $entity_uri_service ) { |
|
| 56 | - |
|
| 57 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Redirect_Service' ); |
|
| 58 | - |
|
| 59 | - $this->entity_uri_service = $entity_uri_service; |
|
| 60 | - |
|
| 61 | - self::$instance = $this; |
|
| 62 | - |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get the singleton instance of the Wordlift_Redirect_Service |
|
| 67 | - * |
|
| 68 | - * @return \Wordlift_Redirect_Service The singleton instance of the Wordlift_Redirect_Service. |
|
| 69 | - * @since 3.2.0 |
|
| 70 | - * |
|
| 71 | - */ |
|
| 72 | - public static function get_instance() { |
|
| 73 | - |
|
| 74 | - return self::$instance; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Perform redirect depending on entity uri and target |
|
| 79 | - * |
|
| 80 | - * @since 3.2.0 |
|
| 81 | - */ |
|
| 82 | - public function ajax_redirect() { |
|
| 83 | - |
|
| 84 | - // Check the `uri` parameter. |
|
| 85 | - if ( ! ( $entity_uri = filter_input( INPUT_GET, 'uri', FILTER_VALIDATE_URL ) ) ) { |
|
| 86 | - wp_die( __( 'Invalid URI.', 'wordlift' ), __( 'Invalid URI.', 'wordlift' ), array( |
|
| 87 | - 'response' => 400, |
|
| 88 | - 'back_link' => true, |
|
| 89 | - ) ); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // Check the `to` parameter. |
|
| 93 | - if ( ! ( $target = filter_input( INPUT_GET, 'to' ) ) ) { |
|
| 94 | - wp_die( __( 'Invalid `to` parameter.', 'wordlift' ), __( 'Invalid `to` parameter.', 'wordlift' ), array( |
|
| 95 | - 'response' => 400, |
|
| 96 | - 'back_link' => true, |
|
| 97 | - ) ); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** @var Wordpress_Content $content */ |
|
| 101 | - $content = Wordpress_Content_Service::get_instance() |
|
| 102 | - ->get_by_entity_id_or_same_as( $entity_uri ); |
|
| 103 | - |
|
| 104 | - if ( ! isset( $content ) ) { |
|
| 105 | - wp_die( __( 'Entity not found.', 'wordlift' ), __( 'Entity not found.', 'wordlift' ), array( |
|
| 106 | - 'response' => 404, |
|
| 107 | - 'back_link' => true, |
|
| 108 | - ) ); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - switch ( $target ) { |
|
| 112 | - case 'edit': |
|
| 113 | - $redirect_url = $content->get_edit_link(); |
|
| 114 | - break; |
|
| 115 | - case 'lod': |
|
| 116 | - $redirect_url = self::LOD_ENDPOINT . '/lodview/?IRI=' . urlencode( $entity_uri ); |
|
| 117 | - break; |
|
| 118 | - case 'permalink': |
|
| 119 | - $redirect_url = $content->get_permalink(); |
|
| 120 | - break; |
|
| 121 | - default: |
|
| 122 | - wp_die( 'Unsupported redirect target.' ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Perform the redirect |
|
| 126 | - wp_safe_redirect( $redirect_url ); |
|
| 127 | - exit; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Register custom allowed hosts. |
|
| 132 | - * @see https://developer.wordpress.org/reference/functions/wp_safe_redirect/ |
|
| 133 | - * |
|
| 134 | - * @since 3.2.0 |
|
| 135 | - * |
|
| 136 | - * @return array permalink. |
|
| 137 | - */ |
|
| 138 | - public function allowed_redirect_hosts( $content ) { |
|
| 139 | - |
|
| 140 | - return array_merge( $content, array( self::LOD_HOST ) ); |
|
| 141 | - } |
|
| 17 | + const LOD_ENDPOINT = 'http://www.lodview.it'; |
|
| 18 | + const LOD_HOST = 'www.lodview.it'; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The Log service. |
|
| 22 | + * |
|
| 23 | + * @since 3.2.0 |
|
| 24 | + * @access private |
|
| 25 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 26 | + */ |
|
| 27 | + private $log_service; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The Entity URI service. |
|
| 31 | + * |
|
| 32 | + * @since 3.2.0 |
|
| 33 | + * @access private |
|
| 34 | + * @var Wordlift_Entity_Uri_Service $entity_uri_service The Entity service. |
|
| 35 | + */ |
|
| 36 | + private $entity_uri_service; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * A singleton instance of the Redirect service (useful for unit tests). |
|
| 40 | + * |
|
| 41 | + * @since 3.2.0 |
|
| 42 | + * @access private |
|
| 43 | + * @var \Wordlift_Redirect_Service $instance The singleton instance. |
|
| 44 | + */ |
|
| 45 | + private static $instance; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Create a Wordlift_Redirect_Service instance. |
|
| 49 | + * |
|
| 50 | + * @param Wordlift_Entity_Uri_Service $entity_uri_service The Entity service. |
|
| 51 | + * |
|
| 52 | + * @since 3.2.0 |
|
| 53 | + * |
|
| 54 | + */ |
|
| 55 | + public function __construct( $entity_uri_service ) { |
|
| 56 | + |
|
| 57 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Redirect_Service' ); |
|
| 58 | + |
|
| 59 | + $this->entity_uri_service = $entity_uri_service; |
|
| 60 | + |
|
| 61 | + self::$instance = $this; |
|
| 62 | + |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get the singleton instance of the Wordlift_Redirect_Service |
|
| 67 | + * |
|
| 68 | + * @return \Wordlift_Redirect_Service The singleton instance of the Wordlift_Redirect_Service. |
|
| 69 | + * @since 3.2.0 |
|
| 70 | + * |
|
| 71 | + */ |
|
| 72 | + public static function get_instance() { |
|
| 73 | + |
|
| 74 | + return self::$instance; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Perform redirect depending on entity uri and target |
|
| 79 | + * |
|
| 80 | + * @since 3.2.0 |
|
| 81 | + */ |
|
| 82 | + public function ajax_redirect() { |
|
| 83 | + |
|
| 84 | + // Check the `uri` parameter. |
|
| 85 | + if ( ! ( $entity_uri = filter_input( INPUT_GET, 'uri', FILTER_VALIDATE_URL ) ) ) { |
|
| 86 | + wp_die( __( 'Invalid URI.', 'wordlift' ), __( 'Invalid URI.', 'wordlift' ), array( |
|
| 87 | + 'response' => 400, |
|
| 88 | + 'back_link' => true, |
|
| 89 | + ) ); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // Check the `to` parameter. |
|
| 93 | + if ( ! ( $target = filter_input( INPUT_GET, 'to' ) ) ) { |
|
| 94 | + wp_die( __( 'Invalid `to` parameter.', 'wordlift' ), __( 'Invalid `to` parameter.', 'wordlift' ), array( |
|
| 95 | + 'response' => 400, |
|
| 96 | + 'back_link' => true, |
|
| 97 | + ) ); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** @var Wordpress_Content $content */ |
|
| 101 | + $content = Wordpress_Content_Service::get_instance() |
|
| 102 | + ->get_by_entity_id_or_same_as( $entity_uri ); |
|
| 103 | + |
|
| 104 | + if ( ! isset( $content ) ) { |
|
| 105 | + wp_die( __( 'Entity not found.', 'wordlift' ), __( 'Entity not found.', 'wordlift' ), array( |
|
| 106 | + 'response' => 404, |
|
| 107 | + 'back_link' => true, |
|
| 108 | + ) ); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + switch ( $target ) { |
|
| 112 | + case 'edit': |
|
| 113 | + $redirect_url = $content->get_edit_link(); |
|
| 114 | + break; |
|
| 115 | + case 'lod': |
|
| 116 | + $redirect_url = self::LOD_ENDPOINT . '/lodview/?IRI=' . urlencode( $entity_uri ); |
|
| 117 | + break; |
|
| 118 | + case 'permalink': |
|
| 119 | + $redirect_url = $content->get_permalink(); |
|
| 120 | + break; |
|
| 121 | + default: |
|
| 122 | + wp_die( 'Unsupported redirect target.' ); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Perform the redirect |
|
| 126 | + wp_safe_redirect( $redirect_url ); |
|
| 127 | + exit; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Register custom allowed hosts. |
|
| 132 | + * @see https://developer.wordpress.org/reference/functions/wp_safe_redirect/ |
|
| 133 | + * |
|
| 134 | + * @since 3.2.0 |
|
| 135 | + * |
|
| 136 | + * @return array permalink. |
|
| 137 | + */ |
|
| 138 | + public function allowed_redirect_hosts( $content ) { |
|
| 139 | + |
|
| 140 | + return array_merge( $content, array( self::LOD_HOST ) ); |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | 143 | } |
@@ -52,9 +52,9 @@ discard block |
||
| 52 | 52 | * @since 3.2.0 |
| 53 | 53 | * |
| 54 | 54 | */ |
| 55 | - public function __construct( $entity_uri_service ) { |
|
| 55 | + public function __construct($entity_uri_service) { |
|
| 56 | 56 | |
| 57 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Redirect_Service' ); |
|
| 57 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Redirect_Service'); |
|
| 58 | 58 | |
| 59 | 59 | $this->entity_uri_service = $entity_uri_service; |
| 60 | 60 | |
@@ -82,48 +82,48 @@ discard block |
||
| 82 | 82 | public function ajax_redirect() { |
| 83 | 83 | |
| 84 | 84 | // Check the `uri` parameter. |
| 85 | - if ( ! ( $entity_uri = filter_input( INPUT_GET, 'uri', FILTER_VALIDATE_URL ) ) ) { |
|
| 86 | - wp_die( __( 'Invalid URI.', 'wordlift' ), __( 'Invalid URI.', 'wordlift' ), array( |
|
| 85 | + if ( ! ($entity_uri = filter_input(INPUT_GET, 'uri', FILTER_VALIDATE_URL))) { |
|
| 86 | + wp_die(__('Invalid URI.', 'wordlift'), __('Invalid URI.', 'wordlift'), array( |
|
| 87 | 87 | 'response' => 400, |
| 88 | 88 | 'back_link' => true, |
| 89 | - ) ); |
|
| 89 | + )); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | // Check the `to` parameter. |
| 93 | - if ( ! ( $target = filter_input( INPUT_GET, 'to' ) ) ) { |
|
| 94 | - wp_die( __( 'Invalid `to` parameter.', 'wordlift' ), __( 'Invalid `to` parameter.', 'wordlift' ), array( |
|
| 93 | + if ( ! ($target = filter_input(INPUT_GET, 'to'))) { |
|
| 94 | + wp_die(__('Invalid `to` parameter.', 'wordlift'), __('Invalid `to` parameter.', 'wordlift'), array( |
|
| 95 | 95 | 'response' => 400, |
| 96 | 96 | 'back_link' => true, |
| 97 | - ) ); |
|
| 97 | + )); |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | /** @var Wordpress_Content $content */ |
| 101 | 101 | $content = Wordpress_Content_Service::get_instance() |
| 102 | - ->get_by_entity_id_or_same_as( $entity_uri ); |
|
| 102 | + ->get_by_entity_id_or_same_as($entity_uri); |
|
| 103 | 103 | |
| 104 | - if ( ! isset( $content ) ) { |
|
| 105 | - wp_die( __( 'Entity not found.', 'wordlift' ), __( 'Entity not found.', 'wordlift' ), array( |
|
| 104 | + if ( ! isset($content)) { |
|
| 105 | + wp_die(__('Entity not found.', 'wordlift'), __('Entity not found.', 'wordlift'), array( |
|
| 106 | 106 | 'response' => 404, |
| 107 | 107 | 'back_link' => true, |
| 108 | - ) ); |
|
| 108 | + )); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - switch ( $target ) { |
|
| 111 | + switch ($target) { |
|
| 112 | 112 | case 'edit': |
| 113 | 113 | $redirect_url = $content->get_edit_link(); |
| 114 | 114 | break; |
| 115 | 115 | case 'lod': |
| 116 | - $redirect_url = self::LOD_ENDPOINT . '/lodview/?IRI=' . urlencode( $entity_uri ); |
|
| 116 | + $redirect_url = self::LOD_ENDPOINT.'/lodview/?IRI='.urlencode($entity_uri); |
|
| 117 | 117 | break; |
| 118 | 118 | case 'permalink': |
| 119 | 119 | $redirect_url = $content->get_permalink(); |
| 120 | 120 | break; |
| 121 | 121 | default: |
| 122 | - wp_die( 'Unsupported redirect target.' ); |
|
| 122 | + wp_die('Unsupported redirect target.'); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Perform the redirect |
| 126 | - wp_safe_redirect( $redirect_url ); |
|
| 126 | + wp_safe_redirect($redirect_url); |
|
| 127 | 127 | exit; |
| 128 | 128 | } |
| 129 | 129 | |
@@ -135,9 +135,9 @@ discard block |
||
| 135 | 135 | * |
| 136 | 136 | * @return array permalink. |
| 137 | 137 | */ |
| 138 | - public function allowed_redirect_hosts( $content ) { |
|
| 138 | + public function allowed_redirect_hosts($content) { |
|
| 139 | 139 | |
| 140 | - return array_merge( $content, array( self::LOD_HOST ) ); |
|
| 140 | + return array_merge($content, array(self::LOD_HOST)); |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | } |
@@ -10,70 +10,70 @@ |
||
| 10 | 10 | |
| 11 | 11 | class Wordpress_Content implements Content { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * The actual content. |
|
| 15 | - * |
|
| 16 | - * @var WP_Post|WP_Term|WP_User $bag |
|
| 17 | - */ |
|
| 18 | - private $bag; |
|
| 13 | + /** |
|
| 14 | + * The actual content. |
|
| 15 | + * |
|
| 16 | + * @var WP_Post|WP_Term|WP_User $bag |
|
| 17 | + */ |
|
| 18 | + private $bag; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Creates a WordPress content instance. |
|
| 22 | - * |
|
| 23 | - * @param WP_Post|WP_Term|WP_User $bag |
|
| 24 | - */ |
|
| 25 | - public function __construct( $bag ) { |
|
| 26 | - $this->bag = $bag; |
|
| 27 | - } |
|
| 20 | + /** |
|
| 21 | + * Creates a WordPress content instance. |
|
| 22 | + * |
|
| 23 | + * @param WP_Post|WP_Term|WP_User $bag |
|
| 24 | + */ |
|
| 25 | + public function __construct( $bag ) { |
|
| 26 | + $this->bag = $bag; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The actual content. |
|
| 31 | - * |
|
| 32 | - * @return WP_Post|WP_Term|WP_User |
|
| 33 | - */ |
|
| 34 | - function get_bag() { |
|
| 35 | - return $this->bag; |
|
| 36 | - } |
|
| 29 | + /** |
|
| 30 | + * The actual content. |
|
| 31 | + * |
|
| 32 | + * @return WP_Post|WP_Term|WP_User |
|
| 33 | + */ |
|
| 34 | + function get_bag() { |
|
| 35 | + return $this->bag; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - function get_id() { |
|
| 39 | - switch ( get_class( $this->bag ) ) { |
|
| 40 | - case 'WP_Post': |
|
| 41 | - case 'WP_User': |
|
| 42 | - return $this->bag->ID; |
|
| 43 | - case 'WP_Term': |
|
| 44 | - return $this->bag->term_id; |
|
| 45 | - } |
|
| 38 | + function get_id() { |
|
| 39 | + switch ( get_class( $this->bag ) ) { |
|
| 40 | + case 'WP_Post': |
|
| 41 | + case 'WP_User': |
|
| 42 | + return $this->bag->ID; |
|
| 43 | + case 'WP_Term': |
|
| 44 | + return $this->bag->term_id; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - return null; |
|
| 48 | - } |
|
| 47 | + return null; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - function get_object_type_enum() { |
|
| 51 | - return Object_Type_Enum::from_wordpress_instance( $this->bag ); |
|
| 52 | - } |
|
| 50 | + function get_object_type_enum() { |
|
| 51 | + return Object_Type_Enum::from_wordpress_instance( $this->bag ); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - function get_permalink() { |
|
| 55 | - switch ( get_class( $this->bag ) ) { |
|
| 56 | - case 'WP_Post': |
|
| 57 | - return get_permalink( $this->get_bag()->ID ); |
|
| 58 | - case 'WP_User': |
|
| 59 | - return get_author_posts_url( $this->get_bag()->ID ); |
|
| 60 | - case 'WP_Term': |
|
| 61 | - return get_term_link( $this->bag->term_id ); |
|
| 62 | - } |
|
| 54 | + function get_permalink() { |
|
| 55 | + switch ( get_class( $this->bag ) ) { |
|
| 56 | + case 'WP_Post': |
|
| 57 | + return get_permalink( $this->get_bag()->ID ); |
|
| 58 | + case 'WP_User': |
|
| 59 | + return get_author_posts_url( $this->get_bag()->ID ); |
|
| 60 | + case 'WP_Term': |
|
| 61 | + return get_term_link( $this->bag->term_id ); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - return null; |
|
| 65 | - } |
|
| 64 | + return null; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - function get_edit_link() { |
|
| 68 | - switch ( get_class( $this->bag ) ) { |
|
| 69 | - case 'WP_Post': |
|
| 70 | - return get_edit_post_link( $this->get_bag()->ID ); |
|
| 71 | - case 'WP_User': |
|
| 72 | - return get_edit_user_link( $this->get_bag()->ID ); |
|
| 73 | - case 'WP_Term': |
|
| 74 | - return get_edit_term_link( $this->bag->term_id ); |
|
| 75 | - } |
|
| 67 | + function get_edit_link() { |
|
| 68 | + switch ( get_class( $this->bag ) ) { |
|
| 69 | + case 'WP_Post': |
|
| 70 | + return get_edit_post_link( $this->get_bag()->ID ); |
|
| 71 | + case 'WP_User': |
|
| 72 | + return get_edit_user_link( $this->get_bag()->ID ); |
|
| 73 | + case 'WP_Term': |
|
| 74 | + return get_edit_term_link( $this->bag->term_id ); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - return null; |
|
| 78 | - } |
|
| 77 | + return null; |
|
| 78 | + } |
|
| 79 | 79 | } |
| 80 | 80 | \ No newline at end of file |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * |
| 23 | 23 | * @param WP_Post|WP_Term|WP_User $bag |
| 24 | 24 | */ |
| 25 | - public function __construct( $bag ) { |
|
| 25 | + public function __construct($bag) { |
|
| 26 | 26 | $this->bag = $bag; |
| 27 | 27 | } |
| 28 | 28 | |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | function get_id() { |
| 39 | - switch ( get_class( $this->bag ) ) { |
|
| 39 | + switch (get_class($this->bag)) { |
|
| 40 | 40 | case 'WP_Post': |
| 41 | 41 | case 'WP_User': |
| 42 | 42 | return $this->bag->ID; |
@@ -48,30 +48,30 @@ discard block |
||
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | function get_object_type_enum() { |
| 51 | - return Object_Type_Enum::from_wordpress_instance( $this->bag ); |
|
| 51 | + return Object_Type_Enum::from_wordpress_instance($this->bag); |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | function get_permalink() { |
| 55 | - switch ( get_class( $this->bag ) ) { |
|
| 55 | + switch (get_class($this->bag)) { |
|
| 56 | 56 | case 'WP_Post': |
| 57 | - return get_permalink( $this->get_bag()->ID ); |
|
| 57 | + return get_permalink($this->get_bag()->ID); |
|
| 58 | 58 | case 'WP_User': |
| 59 | - return get_author_posts_url( $this->get_bag()->ID ); |
|
| 59 | + return get_author_posts_url($this->get_bag()->ID); |
|
| 60 | 60 | case 'WP_Term': |
| 61 | - return get_term_link( $this->bag->term_id ); |
|
| 61 | + return get_term_link($this->bag->term_id); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | return null; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | function get_edit_link() { |
| 68 | - switch ( get_class( $this->bag ) ) { |
|
| 68 | + switch (get_class($this->bag)) { |
|
| 69 | 69 | case 'WP_Post': |
| 70 | - return get_edit_post_link( $this->get_bag()->ID ); |
|
| 70 | + return get_edit_post_link($this->get_bag()->ID); |
|
| 71 | 71 | case 'WP_User': |
| 72 | - return get_edit_user_link( $this->get_bag()->ID ); |
|
| 72 | + return get_edit_user_link($this->get_bag()->ID); |
|
| 73 | 73 | case 'WP_Term': |
| 74 | - return get_edit_term_link( $this->bag->term_id ); |
|
| 74 | + return get_edit_term_link($this->bag->term_id); |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | return null; |
@@ -4,37 +4,37 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface Content { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * Get the actual content. |
|
| 9 | - * |
|
| 10 | - * @return mixed Get the actual content. |
|
| 11 | - */ |
|
| 12 | - function get_bag(); |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * Get the content id. |
|
| 16 | - * |
|
| 17 | - * @return mixed |
|
| 18 | - */ |
|
| 19 | - function get_id(); |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Get the content type. |
|
| 23 | - * |
|
| 24 | - * @return mixed |
|
| 25 | - */ |
|
| 26 | - function get_object_type_enum(); |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Get the permalink. |
|
| 30 | - * @return string |
|
| 31 | - */ |
|
| 32 | - function get_permalink(); |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Get the edit link. |
|
| 36 | - * @return string |
|
| 37 | - */ |
|
| 38 | - function get_edit_link(); |
|
| 7 | + /** |
|
| 8 | + * Get the actual content. |
|
| 9 | + * |
|
| 10 | + * @return mixed Get the actual content. |
|
| 11 | + */ |
|
| 12 | + function get_bag(); |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * Get the content id. |
|
| 16 | + * |
|
| 17 | + * @return mixed |
|
| 18 | + */ |
|
| 19 | + function get_id(); |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Get the content type. |
|
| 23 | + * |
|
| 24 | + * @return mixed |
|
| 25 | + */ |
|
| 26 | + function get_object_type_enum(); |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Get the permalink. |
|
| 30 | + * @return string |
|
| 31 | + */ |
|
| 32 | + function get_permalink(); |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Get the edit link. |
|
| 36 | + * @return string |
|
| 37 | + */ |
|
| 38 | + function get_edit_link(); |
|
| 39 | 39 | |
| 40 | 40 | } |
@@ -11,119 +11,119 @@ |
||
| 11 | 11 | use Wordlift\Object_Type_Enum; |
| 12 | 12 | |
| 13 | 13 | class Object_Link_Provider extends Singleton { |
| 14 | - /** |
|
| 15 | - * @var array<Link> |
|
| 16 | - */ |
|
| 17 | - private $link_providers; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @return Object_Link_Provider |
|
| 21 | - */ |
|
| 22 | - public static function get_instance() { |
|
| 23 | - return parent::get_instance(); |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - public function __construct() { |
|
| 27 | - parent::__construct(); |
|
| 28 | - $this->link_providers = array( |
|
| 29 | - Object_Type_Enum::POST => Post_Link::get_instance(), |
|
| 30 | - Object_Type_Enum::TERM => Term_Link::get_instance() |
|
| 31 | - ); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @param $id int |
|
| 36 | - * @param $label_to_be_ignored string |
|
| 37 | - * @param $object_type int |
|
| 38 | - * |
|
| 39 | - * @return string |
|
| 40 | - */ |
|
| 41 | - public function get_link_title( $id, $label_to_be_ignored, $object_type ) { |
|
| 42 | - $provider = $this->get_provider( $object_type ); |
|
| 43 | - if ( ! $provider ) { |
|
| 44 | - return ''; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - return $provider->get_link_title( $id, $label_to_be_ignored ); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Return the object type by the entity uri. |
|
| 53 | - * @return int which can be any of the {@link Object_Type_Enum} values. |
|
| 54 | - */ |
|
| 55 | - public function get_object_type( $uri ) { |
|
| 56 | - |
|
| 57 | - $link_providers = $this->link_providers; |
|
| 58 | - foreach ( $link_providers as $type => $provider ) { |
|
| 59 | - /** |
|
| 60 | - * @var $provider Link |
|
| 61 | - */ |
|
| 62 | - $id = $provider->get_id( $uri ); |
|
| 63 | - if ( $id ) { |
|
| 64 | - return $type; |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - return Object_Type_Enum::UNKNOWN; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function get_same_as_uris( $id, $object_type ) { |
|
| 72 | - |
|
| 73 | - $provider = $this->get_provider( $object_type ); |
|
| 74 | - if ( ! $provider ) { |
|
| 75 | - return array(); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - return $provider->get_same_as_uris( $id ); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @param $object_type |
|
| 83 | - * |
|
| 84 | - * @return mixed|Link |
|
| 85 | - */ |
|
| 86 | - private function get_provider( $object_type ) { |
|
| 87 | - |
|
| 88 | - if ( ! array_key_exists( $object_type, $this->link_providers ) ) { |
|
| 89 | - return false; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - return $this->link_providers[ $object_type ]; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - public function get_permalink( $id, $object_type ) { |
|
| 97 | - $provider = $this->get_provider( $object_type ); |
|
| 98 | - if ( ! $provider ) { |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - return $provider->get_permalink( $id ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Return the edit term page link. |
|
| 107 | - * |
|
| 108 | - * @param $object_id |
|
| 109 | - * @param $uri |
|
| 110 | - * |
|
| 111 | - * @return string | false |
|
| 112 | - * @since 3.32.0 |
|
| 113 | - */ |
|
| 114 | - public function get_edit_page_link( $object_id, $uri ) { |
|
| 115 | - |
|
| 116 | - $object_type = $this->get_object_type( $uri ); |
|
| 117 | - |
|
| 118 | - $provider = $this->get_provider( $object_type ); |
|
| 119 | - |
|
| 120 | - if ( ! $provider ) { |
|
| 121 | - return false; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $provider->get_edit_page_link( $object_id ); |
|
| 125 | - |
|
| 126 | - } |
|
| 14 | + /** |
|
| 15 | + * @var array<Link> |
|
| 16 | + */ |
|
| 17 | + private $link_providers; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @return Object_Link_Provider |
|
| 21 | + */ |
|
| 22 | + public static function get_instance() { |
|
| 23 | + return parent::get_instance(); |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + public function __construct() { |
|
| 27 | + parent::__construct(); |
|
| 28 | + $this->link_providers = array( |
|
| 29 | + Object_Type_Enum::POST => Post_Link::get_instance(), |
|
| 30 | + Object_Type_Enum::TERM => Term_Link::get_instance() |
|
| 31 | + ); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @param $id int |
|
| 36 | + * @param $label_to_be_ignored string |
|
| 37 | + * @param $object_type int |
|
| 38 | + * |
|
| 39 | + * @return string |
|
| 40 | + */ |
|
| 41 | + public function get_link_title( $id, $label_to_be_ignored, $object_type ) { |
|
| 42 | + $provider = $this->get_provider( $object_type ); |
|
| 43 | + if ( ! $provider ) { |
|
| 44 | + return ''; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + return $provider->get_link_title( $id, $label_to_be_ignored ); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Return the object type by the entity uri. |
|
| 53 | + * @return int which can be any of the {@link Object_Type_Enum} values. |
|
| 54 | + */ |
|
| 55 | + public function get_object_type( $uri ) { |
|
| 56 | + |
|
| 57 | + $link_providers = $this->link_providers; |
|
| 58 | + foreach ( $link_providers as $type => $provider ) { |
|
| 59 | + /** |
|
| 60 | + * @var $provider Link |
|
| 61 | + */ |
|
| 62 | + $id = $provider->get_id( $uri ); |
|
| 63 | + if ( $id ) { |
|
| 64 | + return $type; |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + return Object_Type_Enum::UNKNOWN; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function get_same_as_uris( $id, $object_type ) { |
|
| 72 | + |
|
| 73 | + $provider = $this->get_provider( $object_type ); |
|
| 74 | + if ( ! $provider ) { |
|
| 75 | + return array(); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + return $provider->get_same_as_uris( $id ); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @param $object_type |
|
| 83 | + * |
|
| 84 | + * @return mixed|Link |
|
| 85 | + */ |
|
| 86 | + private function get_provider( $object_type ) { |
|
| 87 | + |
|
| 88 | + if ( ! array_key_exists( $object_type, $this->link_providers ) ) { |
|
| 89 | + return false; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + return $this->link_providers[ $object_type ]; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + public function get_permalink( $id, $object_type ) { |
|
| 97 | + $provider = $this->get_provider( $object_type ); |
|
| 98 | + if ( ! $provider ) { |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + return $provider->get_permalink( $id ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Return the edit term page link. |
|
| 107 | + * |
|
| 108 | + * @param $object_id |
|
| 109 | + * @param $uri |
|
| 110 | + * |
|
| 111 | + * @return string | false |
|
| 112 | + * @since 3.32.0 |
|
| 113 | + */ |
|
| 114 | + public function get_edit_page_link( $object_id, $uri ) { |
|
| 115 | + |
|
| 116 | + $object_type = $this->get_object_type( $uri ); |
|
| 117 | + |
|
| 118 | + $provider = $this->get_provider( $object_type ); |
|
| 119 | + |
|
| 120 | + if ( ! $provider ) { |
|
| 121 | + return false; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $provider->get_edit_page_link( $object_id ); |
|
| 125 | + |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | 128 | |
| 129 | 129 | } |
| 130 | 130 | \ No newline at end of file |
@@ -38,13 +38,13 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @return string |
| 40 | 40 | */ |
| 41 | - public function get_link_title( $id, $label_to_be_ignored, $object_type ) { |
|
| 42 | - $provider = $this->get_provider( $object_type ); |
|
| 43 | - if ( ! $provider ) { |
|
| 41 | + public function get_link_title($id, $label_to_be_ignored, $object_type) { |
|
| 42 | + $provider = $this->get_provider($object_type); |
|
| 43 | + if ( ! $provider) { |
|
| 44 | 44 | return ''; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - return $provider->get_link_title( $id, $label_to_be_ignored ); |
|
| 47 | + return $provider->get_link_title($id, $label_to_be_ignored); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | |
@@ -52,15 +52,15 @@ discard block |
||
| 52 | 52 | * Return the object type by the entity uri. |
| 53 | 53 | * @return int which can be any of the {@link Object_Type_Enum} values. |
| 54 | 54 | */ |
| 55 | - public function get_object_type( $uri ) { |
|
| 55 | + public function get_object_type($uri) { |
|
| 56 | 56 | |
| 57 | 57 | $link_providers = $this->link_providers; |
| 58 | - foreach ( $link_providers as $type => $provider ) { |
|
| 58 | + foreach ($link_providers as $type => $provider) { |
|
| 59 | 59 | /** |
| 60 | 60 | * @var $provider Link |
| 61 | 61 | */ |
| 62 | - $id = $provider->get_id( $uri ); |
|
| 63 | - if ( $id ) { |
|
| 62 | + $id = $provider->get_id($uri); |
|
| 63 | + if ($id) { |
|
| 64 | 64 | return $type; |
| 65 | 65 | } |
| 66 | 66 | } |
@@ -68,14 +68,14 @@ discard block |
||
| 68 | 68 | return Object_Type_Enum::UNKNOWN; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - public function get_same_as_uris( $id, $object_type ) { |
|
| 71 | + public function get_same_as_uris($id, $object_type) { |
|
| 72 | 72 | |
| 73 | - $provider = $this->get_provider( $object_type ); |
|
| 74 | - if ( ! $provider ) { |
|
| 73 | + $provider = $this->get_provider($object_type); |
|
| 74 | + if ( ! $provider) { |
|
| 75 | 75 | return array(); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - return $provider->get_same_as_uris( $id ); |
|
| 78 | + return $provider->get_same_as_uris($id); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | /** |
@@ -83,23 +83,23 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @return mixed|Link |
| 85 | 85 | */ |
| 86 | - private function get_provider( $object_type ) { |
|
| 86 | + private function get_provider($object_type) { |
|
| 87 | 87 | |
| 88 | - if ( ! array_key_exists( $object_type, $this->link_providers ) ) { |
|
| 88 | + if ( ! array_key_exists($object_type, $this->link_providers)) { |
|
| 89 | 89 | return false; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - return $this->link_providers[ $object_type ]; |
|
| 92 | + return $this->link_providers[$object_type]; |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | |
| 96 | - public function get_permalink( $id, $object_type ) { |
|
| 97 | - $provider = $this->get_provider( $object_type ); |
|
| 98 | - if ( ! $provider ) { |
|
| 96 | + public function get_permalink($id, $object_type) { |
|
| 97 | + $provider = $this->get_provider($object_type); |
|
| 98 | + if ( ! $provider) { |
|
| 99 | 99 | return false; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - return $provider->get_permalink( $id ); |
|
| 102 | + return $provider->get_permalink($id); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | /** |
@@ -111,17 +111,17 @@ discard block |
||
| 111 | 111 | * @return string | false |
| 112 | 112 | * @since 3.32.0 |
| 113 | 113 | */ |
| 114 | - public function get_edit_page_link( $object_id, $uri ) { |
|
| 114 | + public function get_edit_page_link($object_id, $uri) { |
|
| 115 | 115 | |
| 116 | - $object_type = $this->get_object_type( $uri ); |
|
| 116 | + $object_type = $this->get_object_type($uri); |
|
| 117 | 117 | |
| 118 | - $provider = $this->get_provider( $object_type ); |
|
| 118 | + $provider = $this->get_provider($object_type); |
|
| 119 | 119 | |
| 120 | - if ( ! $provider ) { |
|
| 120 | + if ( ! $provider) { |
|
| 121 | 121 | return false; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - return $provider->get_edit_page_link( $object_id ); |
|
| 124 | + return $provider->get_edit_page_link($object_id); |
|
| 125 | 125 | |
| 126 | 126 | } |
| 127 | 127 | |