@@ -17,98 +17,98 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Entity_Uri_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Holds the {@link Wordlift_Entity_Uri_Service} instance. |
|
| 22 | - * |
|
| 23 | - * @since 3.21.5 |
|
| 24 | - * @access private |
|
| 25 | - * @var Wordlift_Entity_Uri_Service $instance The {@link Wordlift_Entity_Uri_Service} singleton. |
|
| 26 | - */ |
|
| 27 | - private static $instance; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * A {@link Wordlift_Log_Service} instance. |
|
| 31 | - * |
|
| 32 | - * @since 3.16.3 |
|
| 33 | - * @access private |
|
| 34 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 35 | - */ |
|
| 36 | - private $log; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 40 | - * |
|
| 41 | - * @since 3.16.3 |
|
| 42 | - * @access private |
|
| 43 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 44 | - */ |
|
| 45 | - private $configuration_service; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * An array of URIs to post ID valid for the current request. |
|
| 49 | - * |
|
| 50 | - * @since 3.16.3 |
|
| 51 | - * @access private |
|
| 52 | - * @var array $uri_to_post An array of URIs to post ID valid for the current request. |
|
| 53 | - */ |
|
| 54 | - protected $uri_to_post; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Create a {@link Wordlift_Entity_Uri_Service} instance. |
|
| 58 | - * |
|
| 59 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 60 | - * |
|
| 61 | - * @since 3.16.3 |
|
| 62 | - * |
|
| 63 | - */ |
|
| 64 | - public function __construct( $configuration_service ) { |
|
| 65 | - |
|
| 66 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 67 | - |
|
| 68 | - $this->configuration_service = $configuration_service; |
|
| 69 | - |
|
| 70 | - // Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`. |
|
| 71 | - add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) ); |
|
| 72 | - |
|
| 73 | - self::$instance = $this; |
|
| 74 | - |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get the singleton. |
|
| 79 | - * |
|
| 80 | - * @return Wordlift_Entity_Uri_Service The singleton instance. |
|
| 81 | - * @since 3.21.5 |
|
| 82 | - */ |
|
| 83 | - public static function get_instance() { |
|
| 84 | - |
|
| 85 | - return self::$instance; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Preload the provided URIs in the local cache. |
|
| 90 | - * |
|
| 91 | - * This function will populate the local `$uri_to_post` array by running a |
|
| 92 | - * single query with all the URIs and returning the mappings in the array. |
|
| 93 | - * |
|
| 94 | - * @param array $uris An array of URIs. |
|
| 95 | - * |
|
| 96 | - * @since 3.16.3 |
|
| 97 | - * |
|
| 98 | - */ |
|
| 99 | - public function preload_uris( $uris ) { |
|
| 100 | - |
|
| 101 | - // Bail out if there are no URIs. |
|
| 102 | - if ( 0 === count( $uris ) ) { |
|
| 103 | - return; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
| 107 | - |
|
| 108 | - global $wpdb; |
|
| 109 | - $in_post_types = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
| 110 | - $in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) ); |
|
| 111 | - $sql = " |
|
| 20 | + /** |
|
| 21 | + * Holds the {@link Wordlift_Entity_Uri_Service} instance. |
|
| 22 | + * |
|
| 23 | + * @since 3.21.5 |
|
| 24 | + * @access private |
|
| 25 | + * @var Wordlift_Entity_Uri_Service $instance The {@link Wordlift_Entity_Uri_Service} singleton. |
|
| 26 | + */ |
|
| 27 | + private static $instance; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * A {@link Wordlift_Log_Service} instance. |
|
| 31 | + * |
|
| 32 | + * @since 3.16.3 |
|
| 33 | + * @access private |
|
| 34 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 35 | + */ |
|
| 36 | + private $log; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 40 | + * |
|
| 41 | + * @since 3.16.3 |
|
| 42 | + * @access private |
|
| 43 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 44 | + */ |
|
| 45 | + private $configuration_service; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * An array of URIs to post ID valid for the current request. |
|
| 49 | + * |
|
| 50 | + * @since 3.16.3 |
|
| 51 | + * @access private |
|
| 52 | + * @var array $uri_to_post An array of URIs to post ID valid for the current request. |
|
| 53 | + */ |
|
| 54 | + protected $uri_to_post; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Create a {@link Wordlift_Entity_Uri_Service} instance. |
|
| 58 | + * |
|
| 59 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 60 | + * |
|
| 61 | + * @since 3.16.3 |
|
| 62 | + * |
|
| 63 | + */ |
|
| 64 | + public function __construct( $configuration_service ) { |
|
| 65 | + |
|
| 66 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 67 | + |
|
| 68 | + $this->configuration_service = $configuration_service; |
|
| 69 | + |
|
| 70 | + // Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`. |
|
| 71 | + add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) ); |
|
| 72 | + |
|
| 73 | + self::$instance = $this; |
|
| 74 | + |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get the singleton. |
|
| 79 | + * |
|
| 80 | + * @return Wordlift_Entity_Uri_Service The singleton instance. |
|
| 81 | + * @since 3.21.5 |
|
| 82 | + */ |
|
| 83 | + public static function get_instance() { |
|
| 84 | + |
|
| 85 | + return self::$instance; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Preload the provided URIs in the local cache. |
|
| 90 | + * |
|
| 91 | + * This function will populate the local `$uri_to_post` array by running a |
|
| 92 | + * single query with all the URIs and returning the mappings in the array. |
|
| 93 | + * |
|
| 94 | + * @param array $uris An array of URIs. |
|
| 95 | + * |
|
| 96 | + * @since 3.16.3 |
|
| 97 | + * |
|
| 98 | + */ |
|
| 99 | + public function preload_uris( $uris ) { |
|
| 100 | + |
|
| 101 | + // Bail out if there are no URIs. |
|
| 102 | + if ( 0 === count( $uris ) ) { |
|
| 103 | + return; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
| 107 | + |
|
| 108 | + global $wpdb; |
|
| 109 | + $in_post_types = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
| 110 | + $in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) ); |
|
| 111 | + $sql = " |
|
| 112 | 112 | SELECT ID FROM $wpdb->posts p |
| 113 | 113 | INNER JOIN $wpdb->postmeta pm |
| 114 | 114 | ON pm.post_id = p.ID |
@@ -118,156 +118,156 @@ discard block |
||
| 118 | 118 | AND p.post_status IN ( 'publish', 'draft', 'private', 'future' ) |
| 119 | 119 | "; |
| 120 | 120 | |
| 121 | - // Get the posts. |
|
| 122 | - $posts = $wpdb->get_col( $sql ); |
|
| 123 | - |
|
| 124 | - // Populate the array. We reinitialize the array on purpose because |
|
| 125 | - // we don't want these data to long live. |
|
| 126 | - $this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) { |
|
| 127 | - $uris = array_merge( |
|
| 128 | - get_post_meta( $item, WL_ENTITY_URL_META_NAME ), |
|
| 129 | - get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS ) |
|
| 130 | - ); |
|
| 131 | - |
|
| 132 | - return $carry |
|
| 133 | - // Get the URI related to the post and fill them with the item id. |
|
| 134 | - + array_fill_keys( $uris, $item ); |
|
| 135 | - }, array() ); |
|
| 136 | - |
|
| 137 | - // Add the not found URIs. |
|
| 138 | - $this->uri_to_post += array_fill_keys( $uris, null ); |
|
| 139 | - |
|
| 140 | - $this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." ); |
|
| 141 | - |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Reset the URI to post local cache. |
|
| 146 | - * |
|
| 147 | - * @since 3.16.3 |
|
| 148 | - */ |
|
| 149 | - public function reset_uris() { |
|
| 150 | - |
|
| 151 | - $this->uri_to_post = array(); |
|
| 152 | - |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 157 | - * |
|
| 158 | - * @param string $uri The entity URI. |
|
| 159 | - * |
|
| 160 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 161 | - * @since 3.2.0 |
|
| 162 | - * |
|
| 163 | - */ |
|
| 164 | - public function get_entity( $uri ) { |
|
| 165 | - |
|
| 166 | - $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
| 167 | - |
|
| 168 | - // Check if we've been provided with a value otherwise return null. |
|
| 169 | - if ( empty( $uri ) ) { |
|
| 170 | - return null; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - $this->log->debug( "Querying post for $uri..." ); |
|
| 174 | - |
|
| 175 | - $query_args = array( |
|
| 176 | - // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 177 | - 'ignore_sticky_posts' => 1, |
|
| 178 | - 'posts_per_page' => 1, |
|
| 179 | - 'post_status' => 'any', |
|
| 180 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 181 | - 'meta_query' => array( |
|
| 182 | - array( |
|
| 183 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 184 | - 'value' => $uri, |
|
| 185 | - 'compare' => '=', |
|
| 186 | - ), |
|
| 187 | - ), |
|
| 188 | - ); |
|
| 189 | - |
|
| 190 | - // Only if the current uri is not an internal uri, entity search is |
|
| 191 | - // performed also looking at sameAs values. |
|
| 192 | - // |
|
| 193 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 194 | - if ( ! $this->is_internal( $uri ) ) { |
|
| 195 | - |
|
| 196 | - $query_args['meta_query']['relation'] = 'OR'; |
|
| 197 | - $query_args['meta_query'][] = array( |
|
| 198 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 199 | - 'value' => $uri, |
|
| 200 | - 'compare' => '=', |
|
| 201 | - ); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - $posts = get_posts( $query_args ); |
|
| 205 | - |
|
| 206 | - // Attempt to find post by URI (only for local entity URLs) |
|
| 207 | - if ( empty( $posts ) ) { |
|
| 208 | - |
|
| 209 | - $this->log->debug( "Finding post by $uri..." ); |
|
| 210 | - $postid = url_to_postid( $uri ); |
|
| 211 | - if ( $postid !== 0 ) { |
|
| 212 | - $this->log->trace( "Found post $postid by URL" ); |
|
| 213 | - |
|
| 214 | - return get_post( $postid ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - // Return null if no post is found. |
|
| 220 | - if ( empty( $posts ) ) { |
|
| 221 | - $this->log->warn( "No post for URI $uri." ); |
|
| 222 | - |
|
| 223 | - return null; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // Return the found post. |
|
| 227 | - return current( $posts ); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Determines whether a given uri is an internal uri or not. |
|
| 232 | - * |
|
| 233 | - * @param string $uri An uri. |
|
| 234 | - * |
|
| 235 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 236 | - * @since 3.16.3 |
|
| 237 | - * |
|
| 238 | - */ |
|
| 239 | - public function is_internal( $uri ) { |
|
| 240 | - |
|
| 241 | - return ( 0 === strrpos( $uri, (string) $this->configuration_service->get_dataset_uri() ) ); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Hook to `rest_post_dispatch` to alter the response and add the `wl_entity_url` post meta as `wl:entity_url`. |
|
| 246 | - * |
|
| 247 | - * We're using this filter instead of the well known `register_meta` / `register_rest_field` because we still need |
|
| 248 | - * to provide full compatibility with WordPress 4.4+. |
|
| 249 | - * |
|
| 250 | - * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response. |
|
| 251 | - * |
|
| 252 | - * @return WP_HTTP_Response The result to send to the client. |
|
| 253 | - * |
|
| 254 | - * @since 3.23.0 |
|
| 255 | - */ |
|
| 256 | - public function rest_post_dispatch( $result ) { |
|
| 257 | - |
|
| 258 | - // Get a reference to the actual data. |
|
| 259 | - $data = &$result->data; |
|
| 260 | - |
|
| 261 | - // Bail out if we don't have the required parameters, or if the type is not a valid entity. |
|
| 262 | - if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] ) |
|
| 263 | - || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) { |
|
| 264 | - return $result; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - // Add the `wl:entity_url`. |
|
| 268 | - $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] ); |
|
| 269 | - |
|
| 270 | - return $result; |
|
| 271 | - } |
|
| 121 | + // Get the posts. |
|
| 122 | + $posts = $wpdb->get_col( $sql ); |
|
| 123 | + |
|
| 124 | + // Populate the array. We reinitialize the array on purpose because |
|
| 125 | + // we don't want these data to long live. |
|
| 126 | + $this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) { |
|
| 127 | + $uris = array_merge( |
|
| 128 | + get_post_meta( $item, WL_ENTITY_URL_META_NAME ), |
|
| 129 | + get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS ) |
|
| 130 | + ); |
|
| 131 | + |
|
| 132 | + return $carry |
|
| 133 | + // Get the URI related to the post and fill them with the item id. |
|
| 134 | + + array_fill_keys( $uris, $item ); |
|
| 135 | + }, array() ); |
|
| 136 | + |
|
| 137 | + // Add the not found URIs. |
|
| 138 | + $this->uri_to_post += array_fill_keys( $uris, null ); |
|
| 139 | + |
|
| 140 | + $this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." ); |
|
| 141 | + |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Reset the URI to post local cache. |
|
| 146 | + * |
|
| 147 | + * @since 3.16.3 |
|
| 148 | + */ |
|
| 149 | + public function reset_uris() { |
|
| 150 | + |
|
| 151 | + $this->uri_to_post = array(); |
|
| 152 | + |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 157 | + * |
|
| 158 | + * @param string $uri The entity URI. |
|
| 159 | + * |
|
| 160 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 161 | + * @since 3.2.0 |
|
| 162 | + * |
|
| 163 | + */ |
|
| 164 | + public function get_entity( $uri ) { |
|
| 165 | + |
|
| 166 | + $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
| 167 | + |
|
| 168 | + // Check if we've been provided with a value otherwise return null. |
|
| 169 | + if ( empty( $uri ) ) { |
|
| 170 | + return null; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + $this->log->debug( "Querying post for $uri..." ); |
|
| 174 | + |
|
| 175 | + $query_args = array( |
|
| 176 | + // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 177 | + 'ignore_sticky_posts' => 1, |
|
| 178 | + 'posts_per_page' => 1, |
|
| 179 | + 'post_status' => 'any', |
|
| 180 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 181 | + 'meta_query' => array( |
|
| 182 | + array( |
|
| 183 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 184 | + 'value' => $uri, |
|
| 185 | + 'compare' => '=', |
|
| 186 | + ), |
|
| 187 | + ), |
|
| 188 | + ); |
|
| 189 | + |
|
| 190 | + // Only if the current uri is not an internal uri, entity search is |
|
| 191 | + // performed also looking at sameAs values. |
|
| 192 | + // |
|
| 193 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 194 | + if ( ! $this->is_internal( $uri ) ) { |
|
| 195 | + |
|
| 196 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 197 | + $query_args['meta_query'][] = array( |
|
| 198 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 199 | + 'value' => $uri, |
|
| 200 | + 'compare' => '=', |
|
| 201 | + ); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + $posts = get_posts( $query_args ); |
|
| 205 | + |
|
| 206 | + // Attempt to find post by URI (only for local entity URLs) |
|
| 207 | + if ( empty( $posts ) ) { |
|
| 208 | + |
|
| 209 | + $this->log->debug( "Finding post by $uri..." ); |
|
| 210 | + $postid = url_to_postid( $uri ); |
|
| 211 | + if ( $postid !== 0 ) { |
|
| 212 | + $this->log->trace( "Found post $postid by URL" ); |
|
| 213 | + |
|
| 214 | + return get_post( $postid ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + // Return null if no post is found. |
|
| 220 | + if ( empty( $posts ) ) { |
|
| 221 | + $this->log->warn( "No post for URI $uri." ); |
|
| 222 | + |
|
| 223 | + return null; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // Return the found post. |
|
| 227 | + return current( $posts ); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Determines whether a given uri is an internal uri or not. |
|
| 232 | + * |
|
| 233 | + * @param string $uri An uri. |
|
| 234 | + * |
|
| 235 | + * @return true if the uri internal to the current dataset otherwise false. |
|
| 236 | + * @since 3.16.3 |
|
| 237 | + * |
|
| 238 | + */ |
|
| 239 | + public function is_internal( $uri ) { |
|
| 240 | + |
|
| 241 | + return ( 0 === strrpos( $uri, (string) $this->configuration_service->get_dataset_uri() ) ); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Hook to `rest_post_dispatch` to alter the response and add the `wl_entity_url` post meta as `wl:entity_url`. |
|
| 246 | + * |
|
| 247 | + * We're using this filter instead of the well known `register_meta` / `register_rest_field` because we still need |
|
| 248 | + * to provide full compatibility with WordPress 4.4+. |
|
| 249 | + * |
|
| 250 | + * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response. |
|
| 251 | + * |
|
| 252 | + * @return WP_HTTP_Response The result to send to the client. |
|
| 253 | + * |
|
| 254 | + * @since 3.23.0 |
|
| 255 | + */ |
|
| 256 | + public function rest_post_dispatch( $result ) { |
|
| 257 | + |
|
| 258 | + // Get a reference to the actual data. |
|
| 259 | + $data = &$result->data; |
|
| 260 | + |
|
| 261 | + // Bail out if we don't have the required parameters, or if the type is not a valid entity. |
|
| 262 | + if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] ) |
|
| 263 | + || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) { |
|
| 264 | + return $result; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + // Add the `wl:entity_url`. |
|
| 268 | + $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] ); |
|
| 269 | + |
|
| 270 | + return $result; |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | 273 | } |
@@ -61,14 +61,14 @@ discard block |
||
| 61 | 61 | * @since 3.16.3 |
| 62 | 62 | * |
| 63 | 63 | */ |
| 64 | - public function __construct( $configuration_service ) { |
|
| 64 | + public function __construct($configuration_service) { |
|
| 65 | 65 | |
| 66 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 66 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 67 | 67 | |
| 68 | 68 | $this->configuration_service = $configuration_service; |
| 69 | 69 | |
| 70 | 70 | // Add a filter to the `rest_post_dispatch` filter to add the wl_entity_url meta as `wl:entity_url`. |
| 71 | - add_filter( 'rest_post_dispatch', array( $this, 'rest_post_dispatch' ) ); |
|
| 71 | + add_filter('rest_post_dispatch', array($this, 'rest_post_dispatch')); |
|
| 72 | 72 | |
| 73 | 73 | self::$instance = $this; |
| 74 | 74 | |
@@ -96,18 +96,18 @@ discard block |
||
| 96 | 96 | * @since 3.16.3 |
| 97 | 97 | * |
| 98 | 98 | */ |
| 99 | - public function preload_uris( $uris ) { |
|
| 99 | + public function preload_uris($uris) { |
|
| 100 | 100 | |
| 101 | 101 | // Bail out if there are no URIs. |
| 102 | - if ( 0 === count( $uris ) ) { |
|
| 102 | + if (0 === count($uris)) { |
|
| 103 | 103 | return; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | - $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
| 106 | + $this->log->trace('Preloading '.count($uris).' URI(s)...'); |
|
| 107 | 107 | |
| 108 | 108 | global $wpdb; |
| 109 | - $in_post_types = implode( "','", array_map( 'esc_sql', Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
| 110 | - $in_entity_uris = implode( "','", array_map( 'esc_sql', $uris ) ); |
|
| 109 | + $in_post_types = implode("','", array_map('esc_sql', Wordlift_Entity_Service::valid_entity_post_types())); |
|
| 110 | + $in_entity_uris = implode("','", array_map('esc_sql', $uris)); |
|
| 111 | 111 | $sql = " |
| 112 | 112 | SELECT ID FROM $wpdb->posts p |
| 113 | 113 | INNER JOIN $wpdb->postmeta pm |
@@ -119,25 +119,25 @@ discard block |
||
| 119 | 119 | "; |
| 120 | 120 | |
| 121 | 121 | // Get the posts. |
| 122 | - $posts = $wpdb->get_col( $sql ); |
|
| 122 | + $posts = $wpdb->get_col($sql); |
|
| 123 | 123 | |
| 124 | 124 | // Populate the array. We reinitialize the array on purpose because |
| 125 | 125 | // we don't want these data to long live. |
| 126 | - $this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) { |
|
| 126 | + $this->uri_to_post = array_reduce($posts, function($carry, $item) { |
|
| 127 | 127 | $uris = array_merge( |
| 128 | - get_post_meta( $item, WL_ENTITY_URL_META_NAME ), |
|
| 129 | - get_post_meta( $item, Wordlift_Schema_Service::FIELD_SAME_AS ) |
|
| 128 | + get_post_meta($item, WL_ENTITY_URL_META_NAME), |
|
| 129 | + get_post_meta($item, Wordlift_Schema_Service::FIELD_SAME_AS) |
|
| 130 | 130 | ); |
| 131 | 131 | |
| 132 | 132 | return $carry |
| 133 | 133 | // Get the URI related to the post and fill them with the item id. |
| 134 | - + array_fill_keys( $uris, $item ); |
|
| 135 | - }, array() ); |
|
| 134 | + + array_fill_keys($uris, $item); |
|
| 135 | + }, array()); |
|
| 136 | 136 | |
| 137 | 137 | // Add the not found URIs. |
| 138 | - $this->uri_to_post += array_fill_keys( $uris, null ); |
|
| 138 | + $this->uri_to_post += array_fill_keys($uris, null); |
|
| 139 | 139 | |
| 140 | - $this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." ); |
|
| 140 | + $this->log->debug(count($this->uri_to_post)." URI(s) preloaded."); |
|
| 141 | 141 | |
| 142 | 142 | } |
| 143 | 143 | |
@@ -161,16 +161,16 @@ discard block |
||
| 161 | 161 | * @since 3.2.0 |
| 162 | 162 | * |
| 163 | 163 | */ |
| 164 | - public function get_entity( $uri ) { |
|
| 164 | + public function get_entity($uri) { |
|
| 165 | 165 | |
| 166 | - $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
| 166 | + $this->log->trace("Getting an entity post for URI $uri..."); |
|
| 167 | 167 | |
| 168 | 168 | // Check if we've been provided with a value otherwise return null. |
| 169 | - if ( empty( $uri ) ) { |
|
| 169 | + if (empty($uri)) { |
|
| 170 | 170 | return null; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - $this->log->debug( "Querying post for $uri..." ); |
|
| 173 | + $this->log->debug("Querying post for $uri..."); |
|
| 174 | 174 | |
| 175 | 175 | $query_args = array( |
| 176 | 176 | // See https://github.com/insideout10/wordlift-plugin/issues/654. |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | // performed also looking at sameAs values. |
| 192 | 192 | // |
| 193 | 193 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 194 | - if ( ! $this->is_internal( $uri ) ) { |
|
| 194 | + if ( ! $this->is_internal($uri)) { |
|
| 195 | 195 | |
| 196 | 196 | $query_args['meta_query']['relation'] = 'OR'; |
| 197 | 197 | $query_args['meta_query'][] = array( |
@@ -201,30 +201,30 @@ discard block |
||
| 201 | 201 | ); |
| 202 | 202 | } |
| 203 | 203 | |
| 204 | - $posts = get_posts( $query_args ); |
|
| 204 | + $posts = get_posts($query_args); |
|
| 205 | 205 | |
| 206 | 206 | // Attempt to find post by URI (only for local entity URLs) |
| 207 | - if ( empty( $posts ) ) { |
|
| 207 | + if (empty($posts)) { |
|
| 208 | 208 | |
| 209 | - $this->log->debug( "Finding post by $uri..." ); |
|
| 210 | - $postid = url_to_postid( $uri ); |
|
| 211 | - if ( $postid !== 0 ) { |
|
| 212 | - $this->log->trace( "Found post $postid by URL" ); |
|
| 209 | + $this->log->debug("Finding post by $uri..."); |
|
| 210 | + $postid = url_to_postid($uri); |
|
| 211 | + if ($postid !== 0) { |
|
| 212 | + $this->log->trace("Found post $postid by URL"); |
|
| 213 | 213 | |
| 214 | - return get_post( $postid ); |
|
| 214 | + return get_post($postid); |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | // Return null if no post is found. |
| 220 | - if ( empty( $posts ) ) { |
|
| 221 | - $this->log->warn( "No post for URI $uri." ); |
|
| 220 | + if (empty($posts)) { |
|
| 221 | + $this->log->warn("No post for URI $uri."); |
|
| 222 | 222 | |
| 223 | 223 | return null; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | // Return the found post. |
| 227 | - return current( $posts ); |
|
| 227 | + return current($posts); |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | /** |
@@ -236,9 +236,9 @@ discard block |
||
| 236 | 236 | * @since 3.16.3 |
| 237 | 237 | * |
| 238 | 238 | */ |
| 239 | - public function is_internal( $uri ) { |
|
| 239 | + public function is_internal($uri) { |
|
| 240 | 240 | |
| 241 | - return ( 0 === strrpos( $uri, (string) $this->configuration_service->get_dataset_uri() ) ); |
|
| 241 | + return (0 === strrpos($uri, (string) $this->configuration_service->get_dataset_uri())); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -253,19 +253,19 @@ discard block |
||
| 253 | 253 | * |
| 254 | 254 | * @since 3.23.0 |
| 255 | 255 | */ |
| 256 | - public function rest_post_dispatch( $result ) { |
|
| 256 | + public function rest_post_dispatch($result) { |
|
| 257 | 257 | |
| 258 | 258 | // Get a reference to the actual data. |
| 259 | 259 | $data = &$result->data; |
| 260 | 260 | |
| 261 | 261 | // Bail out if we don't have the required parameters, or if the type is not a valid entity. |
| 262 | - if ( ! is_array( $data ) || ! isset( $data['id'] ) || ! isset( $data['type'] ) |
|
| 263 | - || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type( $data['type'] ) ) { |
|
| 262 | + if ( ! is_array($data) || ! isset($data['id']) || ! isset($data['type']) |
|
| 263 | + || ! Wordlift_Entity_Type_Service::is_valid_entity_post_type($data['type'])) { |
|
| 264 | 264 | return $result; |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | // Add the `wl:entity_url`. |
| 268 | - $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri( $data['id'] ); |
|
| 268 | + $data['wl:entity_url'] = Wordlift_Entity_Service::get_instance()->get_uri($data['id']); |
|
| 269 | 269 | |
| 270 | 270 | return $result; |
| 271 | 271 | } |