@@ -482,7 +482,7 @@ |
||
| 482 | 482 | * @param null $logo The entity logo id (or NULL if none). |
| 483 | 483 | * @param string $status The post status, by default 'publish'. |
| 484 | 484 | * |
| 485 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 485 | + * @return integer The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 486 | 486 | */ |
| 487 | 487 | public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
| 488 | 488 | |
@@ -7,461 +7,461 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Entity_Service { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The Log service. |
|
| 12 | - * |
|
| 13 | - * @since 3.2.0 |
|
| 14 | - * @access private |
|
| 15 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | - */ |
|
| 17 | - private $log; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The UI service. |
|
| 21 | - * |
|
| 22 | - * @since 3.2.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | - */ |
|
| 26 | - private $ui_service; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.15.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $relation_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.16.3 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $entity_uri_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The entity post type name. |
|
| 48 | - * |
|
| 49 | - * @since 3.1.0 |
|
| 50 | - */ |
|
| 51 | - const TYPE_NAME = 'entity'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The alternative label meta key. |
|
| 55 | - * |
|
| 56 | - * @since 3.2.0 |
|
| 57 | - */ |
|
| 58 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The alternative label input template. |
|
| 62 | - * |
|
| 63 | - * @since 3.2.0 |
|
| 64 | - */ |
|
| 65 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 66 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 10 | + /** |
|
| 11 | + * The Log service. |
|
| 12 | + * |
|
| 13 | + * @since 3.2.0 |
|
| 14 | + * @access private |
|
| 15 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | + */ |
|
| 17 | + private $log; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The UI service. |
|
| 21 | + * |
|
| 22 | + * @since 3.2.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | + */ |
|
| 26 | + private $ui_service; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.15.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $relation_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.16.3 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $entity_uri_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The entity post type name. |
|
| 48 | + * |
|
| 49 | + * @since 3.1.0 |
|
| 50 | + */ |
|
| 51 | + const TYPE_NAME = 'entity'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The alternative label meta key. |
|
| 55 | + * |
|
| 56 | + * @since 3.2.0 |
|
| 57 | + */ |
|
| 58 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The alternative label input template. |
|
| 62 | + * |
|
| 63 | + * @since 3.2.0 |
|
| 64 | + */ |
|
| 65 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 66 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 67 | 67 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 68 | 68 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 69 | 69 | <button class="button wl-delete-button">%s</button> |
| 70 | 70 | </div>'; |
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * A singleton instance of the Entity service. |
|
| 74 | - * |
|
| 75 | - * @since 3.2.0 |
|
| 76 | - * @access private |
|
| 77 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 78 | - */ |
|
| 79 | - private static $instance; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Create a Wordlift_Entity_Service instance. |
|
| 83 | - * |
|
| 84 | - * @since 3.2.0 |
|
| 85 | - * |
|
| 86 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 87 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 88 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 89 | - */ |
|
| 90 | - public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 91 | - |
|
| 92 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 93 | - |
|
| 94 | - $this->ui_service = $ui_service; |
|
| 95 | - $this->relation_service = $relation_service; |
|
| 96 | - $this->entity_uri_service = $entity_uri_service; |
|
| 97 | - |
|
| 98 | - // Set the singleton instance. |
|
| 99 | - self::$instance = $this; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Get the singleton instance of the Entity service. |
|
| 104 | - * |
|
| 105 | - * @since 3.2.0 |
|
| 106 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 107 | - */ |
|
| 108 | - public static function get_instance() { |
|
| 109 | - |
|
| 110 | - return self::$instance; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 115 | - * something which is not an article. |
|
| 116 | - * |
|
| 117 | - * @since 3.1.0 |
|
| 118 | - * |
|
| 119 | - * @param int $post_id A post id. |
|
| 120 | - * |
|
| 121 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 122 | - */ |
|
| 123 | - public function is_entity( $post_id ) { |
|
| 124 | - |
|
| 125 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 126 | - |
|
| 127 | - if ( 0 === count( $terms ) ) { |
|
| 128 | - return false; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - // We don't consider an `article` to be an entity. |
|
| 132 | - if ( 'article' !== $terms[0]->slug ) { |
|
| 133 | - return true; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return false; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Get the proper classification scope for a given entity post |
|
| 141 | - * |
|
| 142 | - * @since 3.5.0 |
|
| 143 | - * |
|
| 144 | - * @param integer $post_id An entity post id. |
|
| 145 | - * |
|
| 146 | - * @param string $default The default classification scope, `what` if not |
|
| 147 | - * provided. |
|
| 148 | - * |
|
| 149 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 150 | - */ |
|
| 151 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 152 | - |
|
| 153 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 154 | - return $default; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - // Retrieve the entity type |
|
| 158 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 159 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 160 | - // Retrieve classification boxes configuration |
|
| 161 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 162 | - foreach ( $classification_boxes as $cb ) { |
|
| 163 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 164 | - return $cb['id']; |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return $default; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Check whether a {@link WP_Post} is used. |
|
| 173 | - * |
|
| 174 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 175 | - * |
|
| 176 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 177 | - */ |
|
| 178 | - public function is_used( $post_id ) { |
|
| 179 | - |
|
| 180 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 181 | - return null; |
|
| 182 | - } |
|
| 183 | - // Retrieve the post |
|
| 184 | - $entity = get_post( $post_id ); |
|
| 185 | - |
|
| 186 | - global $wpdb; |
|
| 187 | - // Retrieve Wordlift relation instances table name |
|
| 188 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 189 | - |
|
| 190 | - // Check is it's referenced / related to another post / entity |
|
| 191 | - $stmt = $wpdb->prepare( |
|
| 192 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 193 | - $entity->ID |
|
| 194 | - ); |
|
| 195 | - |
|
| 196 | - // Perform the query |
|
| 197 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 198 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 199 | - if ( 0 < $relation_instances ) { |
|
| 200 | - return true; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - // Check if the entity uri is used as meta_value |
|
| 204 | - $stmt = $wpdb->prepare( |
|
| 205 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 206 | - $entity->ID, |
|
| 207 | - wl_get_entity_uri( $entity->ID ) |
|
| 208 | - ); |
|
| 209 | - // Perform the query |
|
| 210 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 211 | - |
|
| 212 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 213 | - if ( 0 < $meta_instances ) { |
|
| 214 | - return true; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // If we are here, it means the current entity is not used at the moment |
|
| 218 | - return false; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 223 | - * |
|
| 224 | - * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 225 | - * @since 3.2.0 |
|
| 226 | - * |
|
| 227 | - * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 228 | - * |
|
| 229 | - * @param string $uri The entity URI. |
|
| 230 | - * |
|
| 231 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 232 | - */ |
|
| 233 | - public function get_entity_post_by_uri( $uri ) { |
|
| 234 | - |
|
| 235 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 240 | - * we check that the post we're saving is the current post. |
|
| 241 | - * |
|
| 242 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 243 | - * |
|
| 244 | - * @since 3.2.0 |
|
| 245 | - * |
|
| 246 | - * @param int $post_id Post ID. |
|
| 247 | - * @param WP_Post $post Post object. |
|
| 248 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 249 | - */ |
|
| 250 | - public function save_post( $post_id, $post, $update ) { |
|
| 251 | - |
|
| 252 | - // Avoid doing anything if post is autosave or a revision. |
|
| 253 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 254 | - return; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - // We're setting the alternative label that have been provided via the UI |
|
| 258 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 259 | - // programmatically by some other function: we need to check therefore if |
|
| 260 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 261 | - // |
|
| 262 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 263 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 264 | - return; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - // Get the alt labels from the request (or empty array). |
|
| 268 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 269 | - |
|
| 270 | - // Set the alternative labels. |
|
| 271 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 272 | - |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Set the alternative labels. |
|
| 277 | - * |
|
| 278 | - * @since 3.2.0 |
|
| 279 | - * |
|
| 280 | - * @param int $post_id The post id. |
|
| 281 | - * @param array $alt_labels An array of labels. |
|
| 282 | - */ |
|
| 283 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 284 | - |
|
| 285 | - // Force $alt_labels to be an array |
|
| 286 | - if ( ! is_array( $alt_labels ) ) { |
|
| 287 | - $alt_labels = array( $alt_labels ); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 291 | - |
|
| 292 | - // Delete all the existing alternate labels. |
|
| 293 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 294 | - |
|
| 295 | - // Set the alternative labels. |
|
| 296 | - foreach ( $alt_labels as $alt_label ) { |
|
| 297 | - if ( ! empty( $alt_label ) ) { |
|
| 298 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 299 | - } |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Retrieve the alternate labels. |
|
| 306 | - * |
|
| 307 | - * @since 3.2.0 |
|
| 308 | - * |
|
| 309 | - * @param int $post_id Post id. |
|
| 310 | - * |
|
| 311 | - * @return mixed An array of alternative labels. |
|
| 312 | - */ |
|
| 313 | - public function get_alternative_labels( $post_id ) { |
|
| 314 | - |
|
| 315 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 320 | - * |
|
| 321 | - * @since 3.12.0 |
|
| 322 | - * |
|
| 323 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 324 | - * |
|
| 325 | - * @return array An array with the entity title and labels. |
|
| 326 | - */ |
|
| 327 | - public function get_labels( $post_id ) { |
|
| 328 | - |
|
| 329 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 334 | - * |
|
| 335 | - * @since 3.2.0 |
|
| 336 | - * |
|
| 337 | - * @param WP_Post $post Post object. |
|
| 338 | - */ |
|
| 339 | - public function edit_form_before_permalink( $post ) { |
|
| 340 | - |
|
| 341 | - // If it's not an entity, return. |
|
| 342 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 343 | - return; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - // Print the input template. |
|
| 347 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 348 | - |
|
| 349 | - // Print all the currently set alternative labels. |
|
| 350 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 351 | - |
|
| 352 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 353 | - |
|
| 354 | - }; |
|
| 355 | - |
|
| 356 | - // Print the button. |
|
| 357 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 358 | - |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Get the URI for the entity with the specified post id. |
|
| 363 | - * |
|
| 364 | - * @since 3.6.0 |
|
| 365 | - * |
|
| 366 | - * @param int $post_id The entity post id. |
|
| 367 | - * |
|
| 368 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 369 | - */ |
|
| 370 | - public function get_uri( $post_id ) { |
|
| 371 | - |
|
| 372 | - // If a null is given, nothing to do |
|
| 373 | - if ( null == $post_id ) { |
|
| 374 | - return null; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 378 | - |
|
| 379 | - // If the dataset uri is not properly configured, null is returned |
|
| 380 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 381 | - return null; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - // Set the URI if it isn't set yet. |
|
| 385 | - $post_status = get_post_status( $post_id ); |
|
| 386 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 387 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 388 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - return $uri; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Get the alternative label input HTML code. |
|
| 397 | - * |
|
| 398 | - * @since 3.2.0 |
|
| 399 | - * |
|
| 400 | - * @param string $value The input value. |
|
| 401 | - * |
|
| 402 | - * @return string The input HTML code. |
|
| 403 | - */ |
|
| 404 | - private function get_alternative_label_input( $value = '' ) { |
|
| 405 | - |
|
| 406 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Get the number of entity posts published in this blog. |
|
| 411 | - * |
|
| 412 | - * @since 3.6.0 |
|
| 413 | - * |
|
| 414 | - * @return int The number of published entity posts. |
|
| 415 | - */ |
|
| 416 | - public function count() { |
|
| 417 | - |
|
| 418 | - $posts = get_posts( $this->add_criterias( array( |
|
| 419 | - 'post_status' => 'any', |
|
| 420 | - 'numberposts' => - 1, |
|
| 421 | - ) ) ); |
|
| 422 | - |
|
| 423 | - return count( $posts ); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 428 | - * call. |
|
| 429 | - * |
|
| 430 | - * @since 3.15.0 |
|
| 431 | - * |
|
| 432 | - * @param array $args The arguments for a `get_posts` call. |
|
| 433 | - * |
|
| 434 | - * @return array The arguments for a `get_posts` call. |
|
| 435 | - */ |
|
| 436 | - public static function add_criterias( $args ) { |
|
| 437 | - |
|
| 438 | - // Build an optimal tax-query. |
|
| 439 | - $tax_query = array( |
|
| 440 | - 'relation' => 'AND', |
|
| 441 | - array( |
|
| 442 | - 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 443 | - 'operator' => 'EXISTS', |
|
| 444 | - ), |
|
| 445 | - array( |
|
| 446 | - 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 447 | - 'field' => 'slug', |
|
| 448 | - 'terms' => 'article', |
|
| 449 | - 'operator' => 'NOT IN', |
|
| 450 | - ), |
|
| 451 | - ); |
|
| 452 | - |
|
| 453 | - return $args + array( |
|
| 454 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 455 | - // Since 3.17.0: should this be faster? |
|
| 456 | - 'tax_query' => $tax_query, |
|
| 457 | - // 'tax_query' => array( |
|
| 458 | - // array( |
|
| 459 | - // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 460 | - // 'terms' => self::get_entity_terms(), |
|
| 461 | - // ), |
|
| 462 | - // ), |
|
| 463 | - ); |
|
| 464 | - } |
|
| 72 | + /** |
|
| 73 | + * A singleton instance of the Entity service. |
|
| 74 | + * |
|
| 75 | + * @since 3.2.0 |
|
| 76 | + * @access private |
|
| 77 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 78 | + */ |
|
| 79 | + private static $instance; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Create a Wordlift_Entity_Service instance. |
|
| 83 | + * |
|
| 84 | + * @since 3.2.0 |
|
| 85 | + * |
|
| 86 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 87 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 88 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 89 | + */ |
|
| 90 | + public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 91 | + |
|
| 92 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 93 | + |
|
| 94 | + $this->ui_service = $ui_service; |
|
| 95 | + $this->relation_service = $relation_service; |
|
| 96 | + $this->entity_uri_service = $entity_uri_service; |
|
| 97 | + |
|
| 98 | + // Set the singleton instance. |
|
| 99 | + self::$instance = $this; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Get the singleton instance of the Entity service. |
|
| 104 | + * |
|
| 105 | + * @since 3.2.0 |
|
| 106 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 107 | + */ |
|
| 108 | + public static function get_instance() { |
|
| 109 | + |
|
| 110 | + return self::$instance; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 115 | + * something which is not an article. |
|
| 116 | + * |
|
| 117 | + * @since 3.1.0 |
|
| 118 | + * |
|
| 119 | + * @param int $post_id A post id. |
|
| 120 | + * |
|
| 121 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 122 | + */ |
|
| 123 | + public function is_entity( $post_id ) { |
|
| 124 | + |
|
| 125 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 126 | + |
|
| 127 | + if ( 0 === count( $terms ) ) { |
|
| 128 | + return false; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + // We don't consider an `article` to be an entity. |
|
| 132 | + if ( 'article' !== $terms[0]->slug ) { |
|
| 133 | + return true; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return false; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Get the proper classification scope for a given entity post |
|
| 141 | + * |
|
| 142 | + * @since 3.5.0 |
|
| 143 | + * |
|
| 144 | + * @param integer $post_id An entity post id. |
|
| 145 | + * |
|
| 146 | + * @param string $default The default classification scope, `what` if not |
|
| 147 | + * provided. |
|
| 148 | + * |
|
| 149 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 150 | + */ |
|
| 151 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 152 | + |
|
| 153 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 154 | + return $default; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + // Retrieve the entity type |
|
| 158 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 159 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 160 | + // Retrieve classification boxes configuration |
|
| 161 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 162 | + foreach ( $classification_boxes as $cb ) { |
|
| 163 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 164 | + return $cb['id']; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return $default; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Check whether a {@link WP_Post} is used. |
|
| 173 | + * |
|
| 174 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 175 | + * |
|
| 176 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 177 | + */ |
|
| 178 | + public function is_used( $post_id ) { |
|
| 179 | + |
|
| 180 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 181 | + return null; |
|
| 182 | + } |
|
| 183 | + // Retrieve the post |
|
| 184 | + $entity = get_post( $post_id ); |
|
| 185 | + |
|
| 186 | + global $wpdb; |
|
| 187 | + // Retrieve Wordlift relation instances table name |
|
| 188 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 189 | + |
|
| 190 | + // Check is it's referenced / related to another post / entity |
|
| 191 | + $stmt = $wpdb->prepare( |
|
| 192 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 193 | + $entity->ID |
|
| 194 | + ); |
|
| 195 | + |
|
| 196 | + // Perform the query |
|
| 197 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 198 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 199 | + if ( 0 < $relation_instances ) { |
|
| 200 | + return true; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + // Check if the entity uri is used as meta_value |
|
| 204 | + $stmt = $wpdb->prepare( |
|
| 205 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 206 | + $entity->ID, |
|
| 207 | + wl_get_entity_uri( $entity->ID ) |
|
| 208 | + ); |
|
| 209 | + // Perform the query |
|
| 210 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 211 | + |
|
| 212 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 213 | + if ( 0 < $meta_instances ) { |
|
| 214 | + return true; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // If we are here, it means the current entity is not used at the moment |
|
| 218 | + return false; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 223 | + * |
|
| 224 | + * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 225 | + * @since 3.2.0 |
|
| 226 | + * |
|
| 227 | + * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 228 | + * |
|
| 229 | + * @param string $uri The entity URI. |
|
| 230 | + * |
|
| 231 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 232 | + */ |
|
| 233 | + public function get_entity_post_by_uri( $uri ) { |
|
| 234 | + |
|
| 235 | + return $this->entity_uri_service->get_entity( $uri ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 240 | + * we check that the post we're saving is the current post. |
|
| 241 | + * |
|
| 242 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 243 | + * |
|
| 244 | + * @since 3.2.0 |
|
| 245 | + * |
|
| 246 | + * @param int $post_id Post ID. |
|
| 247 | + * @param WP_Post $post Post object. |
|
| 248 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 249 | + */ |
|
| 250 | + public function save_post( $post_id, $post, $update ) { |
|
| 251 | + |
|
| 252 | + // Avoid doing anything if post is autosave or a revision. |
|
| 253 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 254 | + return; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + // We're setting the alternative label that have been provided via the UI |
|
| 258 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 259 | + // programmatically by some other function: we need to check therefore if |
|
| 260 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 261 | + // |
|
| 262 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 263 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 264 | + return; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + // Get the alt labels from the request (or empty array). |
|
| 268 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 269 | + |
|
| 270 | + // Set the alternative labels. |
|
| 271 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 272 | + |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Set the alternative labels. |
|
| 277 | + * |
|
| 278 | + * @since 3.2.0 |
|
| 279 | + * |
|
| 280 | + * @param int $post_id The post id. |
|
| 281 | + * @param array $alt_labels An array of labels. |
|
| 282 | + */ |
|
| 283 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 284 | + |
|
| 285 | + // Force $alt_labels to be an array |
|
| 286 | + if ( ! is_array( $alt_labels ) ) { |
|
| 287 | + $alt_labels = array( $alt_labels ); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 291 | + |
|
| 292 | + // Delete all the existing alternate labels. |
|
| 293 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 294 | + |
|
| 295 | + // Set the alternative labels. |
|
| 296 | + foreach ( $alt_labels as $alt_label ) { |
|
| 297 | + if ( ! empty( $alt_label ) ) { |
|
| 298 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Retrieve the alternate labels. |
|
| 306 | + * |
|
| 307 | + * @since 3.2.0 |
|
| 308 | + * |
|
| 309 | + * @param int $post_id Post id. |
|
| 310 | + * |
|
| 311 | + * @return mixed An array of alternative labels. |
|
| 312 | + */ |
|
| 313 | + public function get_alternative_labels( $post_id ) { |
|
| 314 | + |
|
| 315 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 320 | + * |
|
| 321 | + * @since 3.12.0 |
|
| 322 | + * |
|
| 323 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 324 | + * |
|
| 325 | + * @return array An array with the entity title and labels. |
|
| 326 | + */ |
|
| 327 | + public function get_labels( $post_id ) { |
|
| 328 | + |
|
| 329 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 334 | + * |
|
| 335 | + * @since 3.2.0 |
|
| 336 | + * |
|
| 337 | + * @param WP_Post $post Post object. |
|
| 338 | + */ |
|
| 339 | + public function edit_form_before_permalink( $post ) { |
|
| 340 | + |
|
| 341 | + // If it's not an entity, return. |
|
| 342 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 343 | + return; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + // Print the input template. |
|
| 347 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 348 | + |
|
| 349 | + // Print all the currently set alternative labels. |
|
| 350 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 351 | + |
|
| 352 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 353 | + |
|
| 354 | + }; |
|
| 355 | + |
|
| 356 | + // Print the button. |
|
| 357 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 358 | + |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Get the URI for the entity with the specified post id. |
|
| 363 | + * |
|
| 364 | + * @since 3.6.0 |
|
| 365 | + * |
|
| 366 | + * @param int $post_id The entity post id. |
|
| 367 | + * |
|
| 368 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 369 | + */ |
|
| 370 | + public function get_uri( $post_id ) { |
|
| 371 | + |
|
| 372 | + // If a null is given, nothing to do |
|
| 373 | + if ( null == $post_id ) { |
|
| 374 | + return null; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 378 | + |
|
| 379 | + // If the dataset uri is not properly configured, null is returned |
|
| 380 | + if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 381 | + return null; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + // Set the URI if it isn't set yet. |
|
| 385 | + $post_status = get_post_status( $post_id ); |
|
| 386 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 387 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 388 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + return $uri; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Get the alternative label input HTML code. |
|
| 397 | + * |
|
| 398 | + * @since 3.2.0 |
|
| 399 | + * |
|
| 400 | + * @param string $value The input value. |
|
| 401 | + * |
|
| 402 | + * @return string The input HTML code. |
|
| 403 | + */ |
|
| 404 | + private function get_alternative_label_input( $value = '' ) { |
|
| 405 | + |
|
| 406 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Get the number of entity posts published in this blog. |
|
| 411 | + * |
|
| 412 | + * @since 3.6.0 |
|
| 413 | + * |
|
| 414 | + * @return int The number of published entity posts. |
|
| 415 | + */ |
|
| 416 | + public function count() { |
|
| 417 | + |
|
| 418 | + $posts = get_posts( $this->add_criterias( array( |
|
| 419 | + 'post_status' => 'any', |
|
| 420 | + 'numberposts' => - 1, |
|
| 421 | + ) ) ); |
|
| 422 | + |
|
| 423 | + return count( $posts ); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 428 | + * call. |
|
| 429 | + * |
|
| 430 | + * @since 3.15.0 |
|
| 431 | + * |
|
| 432 | + * @param array $args The arguments for a `get_posts` call. |
|
| 433 | + * |
|
| 434 | + * @return array The arguments for a `get_posts` call. |
|
| 435 | + */ |
|
| 436 | + public static function add_criterias( $args ) { |
|
| 437 | + |
|
| 438 | + // Build an optimal tax-query. |
|
| 439 | + $tax_query = array( |
|
| 440 | + 'relation' => 'AND', |
|
| 441 | + array( |
|
| 442 | + 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 443 | + 'operator' => 'EXISTS', |
|
| 444 | + ), |
|
| 445 | + array( |
|
| 446 | + 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 447 | + 'field' => 'slug', |
|
| 448 | + 'terms' => 'article', |
|
| 449 | + 'operator' => 'NOT IN', |
|
| 450 | + ), |
|
| 451 | + ); |
|
| 452 | + |
|
| 453 | + return $args + array( |
|
| 454 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 455 | + // Since 3.17.0: should this be faster? |
|
| 456 | + 'tax_query' => $tax_query, |
|
| 457 | + // 'tax_query' => array( |
|
| 458 | + // array( |
|
| 459 | + // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 460 | + // 'terms' => self::get_entity_terms(), |
|
| 461 | + // ), |
|
| 462 | + // ), |
|
| 463 | + ); |
|
| 464 | + } |
|
| 465 | 465 | |
| 466 | 466 | // /** |
| 467 | 467 | // * Get the entity terms IDs which represent an entity. |
@@ -491,97 +491,97 @@ discard block |
||
| 491 | 491 | // } ) ); |
| 492 | 492 | // } |
| 493 | 493 | |
| 494 | - /** |
|
| 495 | - * Create a new entity. |
|
| 496 | - * |
|
| 497 | - * @since 3.9.0 |
|
| 498 | - * |
|
| 499 | - * @param string $name The entity name. |
|
| 500 | - * @param string $type_uri The entity's type URI. |
|
| 501 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 502 | - * @param string $status The post status, by default 'publish'. |
|
| 503 | - * |
|
| 504 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 505 | - */ |
|
| 506 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 507 | - |
|
| 508 | - // Create an entity for the publisher. |
|
| 509 | - $post_id = wp_insert_post( array( |
|
| 510 | - 'post_type' => self::TYPE_NAME, |
|
| 511 | - 'post_title' => $name, |
|
| 512 | - 'post_status' => $status, |
|
| 513 | - 'post_content' => '', |
|
| 514 | - ) ); |
|
| 515 | - |
|
| 516 | - // Return the error if any. |
|
| 517 | - if ( is_wp_error( $post_id ) ) { |
|
| 518 | - return $post_id; |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - // Set the entity logo. |
|
| 522 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 523 | - set_post_thumbnail( $post_id, $logo ); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - // Set the entity type. |
|
| 527 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 528 | - |
|
| 529 | - return $post_id; |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * Get the entities related to the one with the specified id. By default only |
|
| 534 | - * published entities will be returned. |
|
| 535 | - * |
|
| 536 | - * @since 3.10.0 |
|
| 537 | - * |
|
| 538 | - * @param int $id The post id. |
|
| 539 | - * @param string $post_status The target post status (default = publish). |
|
| 540 | - * |
|
| 541 | - * @return array An array of post ids. |
|
| 542 | - */ |
|
| 543 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 544 | - |
|
| 545 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * Get the list of entities. |
|
| 550 | - * |
|
| 551 | - * @since 3.12.2 |
|
| 552 | - * |
|
| 553 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 554 | - * |
|
| 555 | - * @return array An array of entity posts. |
|
| 556 | - */ |
|
| 557 | - public function get( $params = array() ) { |
|
| 558 | - |
|
| 559 | - // Set the defaults. |
|
| 560 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 561 | - |
|
| 562 | - // Merge the defaults with the provided parameters. |
|
| 563 | - $args = wp_parse_args( $params, $defaults ); |
|
| 564 | - |
|
| 565 | - // Call the `get_posts` function. |
|
| 566 | - return get_posts( $args ); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * The list of post type names which can be used for entities |
|
| 571 | - * |
|
| 572 | - * Criteria is that the post type is public. The list of valid post types |
|
| 573 | - * can be overridden with a filter. |
|
| 574 | - * |
|
| 575 | - * @since 3.15.0 |
|
| 576 | - * |
|
| 577 | - * @return array Array containing the names of the valid post types. |
|
| 578 | - */ |
|
| 579 | - static function valid_entity_post_types() { |
|
| 580 | - |
|
| 581 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 582 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 583 | - |
|
| 584 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 585 | - } |
|
| 494 | + /** |
|
| 495 | + * Create a new entity. |
|
| 496 | + * |
|
| 497 | + * @since 3.9.0 |
|
| 498 | + * |
|
| 499 | + * @param string $name The entity name. |
|
| 500 | + * @param string $type_uri The entity's type URI. |
|
| 501 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 502 | + * @param string $status The post status, by default 'publish'. |
|
| 503 | + * |
|
| 504 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 505 | + */ |
|
| 506 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 507 | + |
|
| 508 | + // Create an entity for the publisher. |
|
| 509 | + $post_id = wp_insert_post( array( |
|
| 510 | + 'post_type' => self::TYPE_NAME, |
|
| 511 | + 'post_title' => $name, |
|
| 512 | + 'post_status' => $status, |
|
| 513 | + 'post_content' => '', |
|
| 514 | + ) ); |
|
| 515 | + |
|
| 516 | + // Return the error if any. |
|
| 517 | + if ( is_wp_error( $post_id ) ) { |
|
| 518 | + return $post_id; |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + // Set the entity logo. |
|
| 522 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 523 | + set_post_thumbnail( $post_id, $logo ); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + // Set the entity type. |
|
| 527 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 528 | + |
|
| 529 | + return $post_id; |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + /** |
|
| 533 | + * Get the entities related to the one with the specified id. By default only |
|
| 534 | + * published entities will be returned. |
|
| 535 | + * |
|
| 536 | + * @since 3.10.0 |
|
| 537 | + * |
|
| 538 | + * @param int $id The post id. |
|
| 539 | + * @param string $post_status The target post status (default = publish). |
|
| 540 | + * |
|
| 541 | + * @return array An array of post ids. |
|
| 542 | + */ |
|
| 543 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 544 | + |
|
| 545 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * Get the list of entities. |
|
| 550 | + * |
|
| 551 | + * @since 3.12.2 |
|
| 552 | + * |
|
| 553 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 554 | + * |
|
| 555 | + * @return array An array of entity posts. |
|
| 556 | + */ |
|
| 557 | + public function get( $params = array() ) { |
|
| 558 | + |
|
| 559 | + // Set the defaults. |
|
| 560 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 561 | + |
|
| 562 | + // Merge the defaults with the provided parameters. |
|
| 563 | + $args = wp_parse_args( $params, $defaults ); |
|
| 564 | + |
|
| 565 | + // Call the `get_posts` function. |
|
| 566 | + return get_posts( $args ); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * The list of post type names which can be used for entities |
|
| 571 | + * |
|
| 572 | + * Criteria is that the post type is public. The list of valid post types |
|
| 573 | + * can be overridden with a filter. |
|
| 574 | + * |
|
| 575 | + * @since 3.15.0 |
|
| 576 | + * |
|
| 577 | + * @return array Array containing the names of the valid post types. |
|
| 578 | + */ |
|
| 579 | + static function valid_entity_post_types() { |
|
| 580 | + |
|
| 581 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 582 | + $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 583 | + |
|
| 584 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 585 | + } |
|
| 586 | 586 | |
| 587 | 587 | } |
@@ -87,9 +87,9 @@ discard block |
||
| 87 | 87 | * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
| 88 | 88 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
| 89 | 89 | */ |
| 90 | - public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 90 | + public function __construct($ui_service, $relation_service, $entity_uri_service) { |
|
| 91 | 91 | |
| 92 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 92 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 93 | 93 | |
| 94 | 94 | $this->ui_service = $ui_service; |
| 95 | 95 | $this->relation_service = $relation_service; |
@@ -120,16 +120,16 @@ discard block |
||
| 120 | 120 | * |
| 121 | 121 | * @return bool Return true if the post is an entity otherwise false. |
| 122 | 122 | */ |
| 123 | - public function is_entity( $post_id ) { |
|
| 123 | + public function is_entity($post_id) { |
|
| 124 | 124 | |
| 125 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 125 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); |
|
| 126 | 126 | |
| 127 | - if ( 0 === count( $terms ) ) { |
|
| 127 | + if (0 === count($terms)) { |
|
| 128 | 128 | return false; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // We don't consider an `article` to be an entity. |
| 132 | - if ( 'article' !== $terms[0]->slug ) { |
|
| 132 | + if ('article' !== $terms[0]->slug) { |
|
| 133 | 133 | return true; |
| 134 | 134 | } |
| 135 | 135 | |
@@ -148,19 +148,19 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @return string Returns a classification scope (e.g. 'what'). |
| 150 | 150 | */ |
| 151 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 151 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 152 | 152 | |
| 153 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 153 | + if (false === $this->is_entity($post_id)) { |
|
| 154 | 154 | return $default; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | // Retrieve the entity type |
| 158 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 159 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 158 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 159 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 160 | 160 | // Retrieve classification boxes configuration |
| 161 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 162 | - foreach ( $classification_boxes as $cb ) { |
|
| 163 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 161 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 162 | + foreach ($classification_boxes as $cb) { |
|
| 163 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 164 | 164 | return $cb['id']; |
| 165 | 165 | } |
| 166 | 166 | } |
@@ -175,13 +175,13 @@ discard block |
||
| 175 | 175 | * |
| 176 | 176 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 177 | 177 | */ |
| 178 | - public function is_used( $post_id ) { |
|
| 178 | + public function is_used($post_id) { |
|
| 179 | 179 | |
| 180 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 180 | + if (false === $this->is_entity($post_id)) { |
|
| 181 | 181 | return null; |
| 182 | 182 | } |
| 183 | 183 | // Retrieve the post |
| 184 | - $entity = get_post( $post_id ); |
|
| 184 | + $entity = get_post($post_id); |
|
| 185 | 185 | |
| 186 | 186 | global $wpdb; |
| 187 | 187 | // Retrieve Wordlift relation instances table name |
@@ -194,9 +194,9 @@ discard block |
||
| 194 | 194 | ); |
| 195 | 195 | |
| 196 | 196 | // Perform the query |
| 197 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 197 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 198 | 198 | // If there is at least one relation instance for the current entity, then it's used |
| 199 | - if ( 0 < $relation_instances ) { |
|
| 199 | + if (0 < $relation_instances) { |
|
| 200 | 200 | return true; |
| 201 | 201 | } |
| 202 | 202 | |
@@ -204,13 +204,13 @@ discard block |
||
| 204 | 204 | $stmt = $wpdb->prepare( |
| 205 | 205 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 206 | 206 | $entity->ID, |
| 207 | - wl_get_entity_uri( $entity->ID ) |
|
| 207 | + wl_get_entity_uri($entity->ID) |
|
| 208 | 208 | ); |
| 209 | 209 | // Perform the query |
| 210 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 210 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 211 | 211 | |
| 212 | 212 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 213 | - if ( 0 < $meta_instances ) { |
|
| 213 | + if (0 < $meta_instances) { |
|
| 214 | 214 | return true; |
| 215 | 215 | } |
| 216 | 216 | |
@@ -230,9 +230,9 @@ discard block |
||
| 230 | 230 | * |
| 231 | 231 | * @return WP_Post|null A WP_Post instance or null if not found. |
| 232 | 232 | */ |
| 233 | - public function get_entity_post_by_uri( $uri ) { |
|
| 233 | + public function get_entity_post_by_uri($uri) { |
|
| 234 | 234 | |
| 235 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 235 | + return $this->entity_uri_service->get_entity($uri); |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
@@ -247,10 +247,10 @@ discard block |
||
| 247 | 247 | * @param WP_Post $post Post object. |
| 248 | 248 | * @param bool $update Whether this is an existing post being updated or not. |
| 249 | 249 | */ |
| 250 | - public function save_post( $post_id, $post, $update ) { |
|
| 250 | + public function save_post($post_id, $post, $update) { |
|
| 251 | 251 | |
| 252 | 252 | // Avoid doing anything if post is autosave or a revision. |
| 253 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 253 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 254 | 254 | return; |
| 255 | 255 | } |
| 256 | 256 | |
@@ -260,15 +260,15 @@ discard block |
||
| 260 | 260 | // the $post_id in the save_post call matches the post id set in the request. |
| 261 | 261 | // |
| 262 | 262 | // If this is not the current post being saved or if it's not an entity, return. |
| 263 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 263 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 264 | 264 | return; |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | // Get the alt labels from the request (or empty array). |
| 268 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 268 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 269 | 269 | |
| 270 | 270 | // Set the alternative labels. |
| 271 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 271 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 272 | 272 | |
| 273 | 273 | } |
| 274 | 274 | |
@@ -280,22 +280,22 @@ discard block |
||
| 280 | 280 | * @param int $post_id The post id. |
| 281 | 281 | * @param array $alt_labels An array of labels. |
| 282 | 282 | */ |
| 283 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 283 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 284 | 284 | |
| 285 | 285 | // Force $alt_labels to be an array |
| 286 | - if ( ! is_array( $alt_labels ) ) { |
|
| 287 | - $alt_labels = array( $alt_labels ); |
|
| 286 | + if ( ! is_array($alt_labels)) { |
|
| 287 | + $alt_labels = array($alt_labels); |
|
| 288 | 288 | } |
| 289 | 289 | |
| 290 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 290 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 291 | 291 | |
| 292 | 292 | // Delete all the existing alternate labels. |
| 293 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 293 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 294 | 294 | |
| 295 | 295 | // Set the alternative labels. |
| 296 | - foreach ( $alt_labels as $alt_label ) { |
|
| 297 | - if ( ! empty( $alt_label ) ) { |
|
| 298 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 296 | + foreach ($alt_labels as $alt_label) { |
|
| 297 | + if ( ! empty($alt_label)) { |
|
| 298 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 299 | 299 | } |
| 300 | 300 | } |
| 301 | 301 | |
@@ -310,9 +310,9 @@ discard block |
||
| 310 | 310 | * |
| 311 | 311 | * @return mixed An array of alternative labels. |
| 312 | 312 | */ |
| 313 | - public function get_alternative_labels( $post_id ) { |
|
| 313 | + public function get_alternative_labels($post_id) { |
|
| 314 | 314 | |
| 315 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 315 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | /** |
@@ -324,9 +324,9 @@ discard block |
||
| 324 | 324 | * |
| 325 | 325 | * @return array An array with the entity title and labels. |
| 326 | 326 | */ |
| 327 | - public function get_labels( $post_id ) { |
|
| 327 | + public function get_labels($post_id) { |
|
| 328 | 328 | |
| 329 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 329 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | /** |
@@ -336,25 +336,25 @@ discard block |
||
| 336 | 336 | * |
| 337 | 337 | * @param WP_Post $post Post object. |
| 338 | 338 | */ |
| 339 | - public function edit_form_before_permalink( $post ) { |
|
| 339 | + public function edit_form_before_permalink($post) { |
|
| 340 | 340 | |
| 341 | 341 | // If it's not an entity, return. |
| 342 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 342 | + if ( ! $this->is_entity($post->ID)) { |
|
| 343 | 343 | return; |
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | // Print the input template. |
| 347 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 347 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 348 | 348 | |
| 349 | 349 | // Print all the currently set alternative labels. |
| 350 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 350 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 351 | 351 | |
| 352 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 352 | + echo $this->get_alternative_label_input($alt_label); |
|
| 353 | 353 | |
| 354 | 354 | }; |
| 355 | 355 | |
| 356 | 356 | // Print the button. |
| 357 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 357 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 358 | 358 | |
| 359 | 359 | } |
| 360 | 360 | |
@@ -367,25 +367,25 @@ discard block |
||
| 367 | 367 | * |
| 368 | 368 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
| 369 | 369 | */ |
| 370 | - public function get_uri( $post_id ) { |
|
| 370 | + public function get_uri($post_id) { |
|
| 371 | 371 | |
| 372 | 372 | // If a null is given, nothing to do |
| 373 | - if ( null == $post_id ) { |
|
| 373 | + if (null == $post_id) { |
|
| 374 | 374 | return null; |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 377 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 378 | 378 | |
| 379 | 379 | // If the dataset uri is not properly configured, null is returned |
| 380 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 380 | + if ('' === wl_configuration_get_redlink_dataset_uri()) { |
|
| 381 | 381 | return null; |
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | // Set the URI if it isn't set yet. |
| 385 | - $post_status = get_post_status( $post_id ); |
|
| 386 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 387 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 388 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 385 | + $post_status = get_post_status($post_id); |
|
| 386 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 387 | + $uri = wl_build_entity_uri($post_id); |
|
| 388 | + wl_set_entity_uri($post_id, $uri); |
|
| 389 | 389 | } |
| 390 | 390 | |
| 391 | 391 | return $uri; |
@@ -401,9 +401,9 @@ discard block |
||
| 401 | 401 | * |
| 402 | 402 | * @return string The input HTML code. |
| 403 | 403 | */ |
| 404 | - private function get_alternative_label_input( $value = '' ) { |
|
| 404 | + private function get_alternative_label_input($value = '') { |
|
| 405 | 405 | |
| 406 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 406 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | /** |
@@ -415,12 +415,12 @@ discard block |
||
| 415 | 415 | */ |
| 416 | 416 | public function count() { |
| 417 | 417 | |
| 418 | - $posts = get_posts( $this->add_criterias( array( |
|
| 418 | + $posts = get_posts($this->add_criterias(array( |
|
| 419 | 419 | 'post_status' => 'any', |
| 420 | - 'numberposts' => - 1, |
|
| 421 | - ) ) ); |
|
| 420 | + 'numberposts' => -1, |
|
| 421 | + ))); |
|
| 422 | 422 | |
| 423 | - return count( $posts ); |
|
| 423 | + return count($posts); |
|
| 424 | 424 | } |
| 425 | 425 | |
| 426 | 426 | /** |
@@ -433,7 +433,7 @@ discard block |
||
| 433 | 433 | * |
| 434 | 434 | * @return array The arguments for a `get_posts` call. |
| 435 | 435 | */ |
| 436 | - public static function add_criterias( $args ) { |
|
| 436 | + public static function add_criterias($args) { |
|
| 437 | 437 | |
| 438 | 438 | // Build an optimal tax-query. |
| 439 | 439 | $tax_query = array( |
@@ -503,28 +503,28 @@ discard block |
||
| 503 | 503 | * |
| 504 | 504 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
| 505 | 505 | */ |
| 506 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 506 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 507 | 507 | |
| 508 | 508 | // Create an entity for the publisher. |
| 509 | - $post_id = wp_insert_post( array( |
|
| 509 | + $post_id = wp_insert_post(array( |
|
| 510 | 510 | 'post_type' => self::TYPE_NAME, |
| 511 | 511 | 'post_title' => $name, |
| 512 | 512 | 'post_status' => $status, |
| 513 | 513 | 'post_content' => '', |
| 514 | - ) ); |
|
| 514 | + )); |
|
| 515 | 515 | |
| 516 | 516 | // Return the error if any. |
| 517 | - if ( is_wp_error( $post_id ) ) { |
|
| 517 | + if (is_wp_error($post_id)) { |
|
| 518 | 518 | return $post_id; |
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | // Set the entity logo. |
| 522 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 523 | - set_post_thumbnail( $post_id, $logo ); |
|
| 522 | + if ($logo && is_numeric($logo)) { |
|
| 523 | + set_post_thumbnail($post_id, $logo); |
|
| 524 | 524 | } |
| 525 | 525 | |
| 526 | 526 | // Set the entity type. |
| 527 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 527 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 528 | 528 | |
| 529 | 529 | return $post_id; |
| 530 | 530 | } |
@@ -540,9 +540,9 @@ discard block |
||
| 540 | 540 | * |
| 541 | 541 | * @return array An array of post ids. |
| 542 | 542 | */ |
| 543 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 543 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 544 | 544 | |
| 545 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 545 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 546 | 546 | } |
| 547 | 547 | |
| 548 | 548 | /** |
@@ -554,16 +554,16 @@ discard block |
||
| 554 | 554 | * |
| 555 | 555 | * @return array An array of entity posts. |
| 556 | 556 | */ |
| 557 | - public function get( $params = array() ) { |
|
| 557 | + public function get($params = array()) { |
|
| 558 | 558 | |
| 559 | 559 | // Set the defaults. |
| 560 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 560 | + $defaults = array('post_type' => 'entity'); |
|
| 561 | 561 | |
| 562 | 562 | // Merge the defaults with the provided parameters. |
| 563 | - $args = wp_parse_args( $params, $defaults ); |
|
| 563 | + $args = wp_parse_args($params, $defaults); |
|
| 564 | 564 | |
| 565 | 565 | // Call the `get_posts` function. |
| 566 | - return get_posts( $args ); |
|
| 566 | + return get_posts($args); |
|
| 567 | 567 | } |
| 568 | 568 | |
| 569 | 569 | /** |
@@ -579,9 +579,9 @@ discard block |
||
| 579 | 579 | static function valid_entity_post_types() { |
| 580 | 580 | |
| 581 | 581 | // Ignore builtins in the call to avoid getting attachments. |
| 582 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 582 | + $post_types = array('post', 'page', self::TYPE_NAME); |
|
| 583 | 583 | |
| 584 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 584 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 585 | 585 | } |
| 586 | 586 | |
| 587 | 587 | } |
@@ -215,7 +215,7 @@ |
||
| 215 | 215 | * |
| 216 | 216 | * @param int $uri An uri. |
| 217 | 217 | * |
| 218 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 218 | + * @return boolean if the uri internal to the current dataset otherwise false. |
|
| 219 | 219 | */ |
| 220 | 220 | public function is_internal( $uri ) { |
| 221 | 221 | |
@@ -17,209 +17,209 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Entity_Uri_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * A {@link Wordlift_Log_Service} instance. |
|
| 22 | - * |
|
| 23 | - * @since 3.16.3 |
|
| 24 | - * @access private |
|
| 25 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 26 | - */ |
|
| 27 | - private $log; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 31 | - * |
|
| 32 | - * @since 3.16.3 |
|
| 33 | - * @access private |
|
| 34 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 35 | - */ |
|
| 36 | - private $configuration_service; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * An array of URIs to post ID valid for the current request. |
|
| 40 | - * |
|
| 41 | - * @since 3.16.3 |
|
| 42 | - * @access private |
|
| 43 | - * @var array $uri_to_post An array of URIs to post ID valid for the current request. |
|
| 44 | - */ |
|
| 45 | - protected $uri_to_post; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Create a {@link Wordlift_Entity_Uri_Service} instance. |
|
| 49 | - * |
|
| 50 | - * @since 3.16.3 |
|
| 51 | - * |
|
| 52 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 53 | - */ |
|
| 54 | - public function __construct( $configuration_service ) { |
|
| 55 | - |
|
| 56 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 57 | - |
|
| 58 | - $this->configuration_service = $configuration_service; |
|
| 59 | - |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Preload the provided URIs in the local cache. |
|
| 64 | - * |
|
| 65 | - * This function will populate the local `$uri_to_post` array by running a |
|
| 66 | - * single query with all the URIs and returning the mappings in the array. |
|
| 67 | - * |
|
| 68 | - * @since 3.16.3 |
|
| 69 | - * |
|
| 70 | - * @param array $uris An array of URIs. |
|
| 71 | - */ |
|
| 72 | - public function preload_uris( $uris ) { |
|
| 73 | - |
|
| 74 | - // Bail out if there are no URIs. |
|
| 75 | - if ( 0 === count( $uris ) ) { |
|
| 76 | - return; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
| 80 | - |
|
| 81 | - $that = $this; |
|
| 82 | - $external_uris = array_filter( $uris, function ( $item ) use ( $that ) { |
|
| 83 | - return ! $that->is_internal( $item ); |
|
| 84 | - } ); |
|
| 85 | - |
|
| 86 | - $query_args = array( |
|
| 87 | - // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 88 | - 'ignore_sticky_posts' => 1, |
|
| 89 | - 'cache_results' => false, |
|
| 90 | - 'numberposts' => - 1, |
|
| 91 | - 'post_status' => 'any', |
|
| 92 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 93 | - 'meta_query' => array( |
|
| 94 | - array( |
|
| 95 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 96 | - 'value' => $uris, |
|
| 97 | - 'compare' => 'IN', |
|
| 98 | - ), |
|
| 99 | - ), |
|
| 100 | - ); |
|
| 101 | - |
|
| 102 | - // Only if the current uri is not an internal uri, entity search is |
|
| 103 | - // performed also looking at sameAs values. |
|
| 104 | - // |
|
| 105 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 106 | - if ( 0 < count( $external_uris ) ) { |
|
| 107 | - |
|
| 108 | - $query_args['meta_query']['relation'] = 'OR'; |
|
| 109 | - $query_args['meta_query'][] = array( |
|
| 110 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 111 | - 'value' => $external_uris, |
|
| 112 | - 'compare' => 'IN', |
|
| 113 | - ); |
|
| 114 | - |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - // Get the posts. |
|
| 118 | - $posts = get_posts( $query_args ); |
|
| 119 | - |
|
| 120 | - // Populate the array. We reinitialize the array on purpose because |
|
| 121 | - // we don't want these data to long live. |
|
| 122 | - $this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) use ( $that ) { |
|
| 123 | - $uris = get_post_meta( $item->ID, WL_ENTITY_URL_META_NAME ) |
|
| 124 | - + get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
| 125 | - |
|
| 126 | - return $carry |
|
| 127 | - // Get the URI related to the post and fill them with the item id. |
|
| 128 | - + array_fill_keys( $uris, $item ); |
|
| 129 | - }, array() ); |
|
| 130 | - |
|
| 131 | - // Add the not found URIs. |
|
| 132 | - $this->uri_to_post += array_fill_keys( $uris, null ); |
|
| 133 | - |
|
| 134 | - $this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." ); |
|
| 135 | - |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Reset the URI to post local cache. |
|
| 140 | - * |
|
| 141 | - * @since 3.16.3 |
|
| 142 | - */ |
|
| 143 | - public function reset_uris() { |
|
| 144 | - |
|
| 145 | - $this->uri_to_post = array(); |
|
| 146 | - |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 151 | - * |
|
| 152 | - * @since 3.2.0 |
|
| 153 | - * |
|
| 154 | - * @param string $uri The entity URI. |
|
| 155 | - * |
|
| 156 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 157 | - */ |
|
| 158 | - public function get_entity( $uri ) { |
|
| 159 | - |
|
| 160 | - $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
| 161 | - |
|
| 162 | - // Check if we've been provided with a value otherwise return null. |
|
| 163 | - if ( empty( $uri ) ) { |
|
| 164 | - return null; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $this->log->debug( "Querying post for $uri..." ); |
|
| 168 | - |
|
| 169 | - $query_args = array( |
|
| 170 | - // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 171 | - 'ignore_sticky_posts' => 1, |
|
| 172 | - 'posts_per_page' => 1, |
|
| 173 | - 'post_status' => 'any', |
|
| 174 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 175 | - 'meta_query' => array( |
|
| 176 | - array( |
|
| 177 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 178 | - 'value' => $uri, |
|
| 179 | - 'compare' => '=', |
|
| 180 | - ), |
|
| 181 | - ), |
|
| 182 | - ); |
|
| 183 | - |
|
| 184 | - // Only if the current uri is not an internal uri, entity search is |
|
| 185 | - // performed also looking at sameAs values. |
|
| 186 | - // |
|
| 187 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 188 | - if ( ! $this->is_internal( $uri ) ) { |
|
| 189 | - |
|
| 190 | - $query_args['meta_query']['relation'] = 'OR'; |
|
| 191 | - $query_args['meta_query'][] = array( |
|
| 192 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 193 | - 'value' => $uri, |
|
| 194 | - 'compare' => '=', |
|
| 195 | - ); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - $posts = get_posts( $query_args ); |
|
| 199 | - |
|
| 200 | - // Return null if no post is found. |
|
| 201 | - if ( 0 === count( $posts ) ) { |
|
| 202 | - $this->log->warn( "No post for URI $uri." ); |
|
| 203 | - |
|
| 204 | - return null; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - // Return the found post. |
|
| 208 | - return current( $posts ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Determines whether a given uri is an internal uri or not. |
|
| 213 | - * |
|
| 214 | - * @since 3.16.3 |
|
| 215 | - * |
|
| 216 | - * @param int $uri An uri. |
|
| 217 | - * |
|
| 218 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 219 | - */ |
|
| 220 | - public function is_internal( $uri ) { |
|
| 221 | - |
|
| 222 | - return ( 0 === strrpos( $uri, $this->configuration_service->get_dataset_uri() ) ); |
|
| 223 | - } |
|
| 20 | + /** |
|
| 21 | + * A {@link Wordlift_Log_Service} instance. |
|
| 22 | + * |
|
| 23 | + * @since 3.16.3 |
|
| 24 | + * @access private |
|
| 25 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 26 | + */ |
|
| 27 | + private $log; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 31 | + * |
|
| 32 | + * @since 3.16.3 |
|
| 33 | + * @access private |
|
| 34 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 35 | + */ |
|
| 36 | + private $configuration_service; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * An array of URIs to post ID valid for the current request. |
|
| 40 | + * |
|
| 41 | + * @since 3.16.3 |
|
| 42 | + * @access private |
|
| 43 | + * @var array $uri_to_post An array of URIs to post ID valid for the current request. |
|
| 44 | + */ |
|
| 45 | + protected $uri_to_post; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Create a {@link Wordlift_Entity_Uri_Service} instance. |
|
| 49 | + * |
|
| 50 | + * @since 3.16.3 |
|
| 51 | + * |
|
| 52 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 53 | + */ |
|
| 54 | + public function __construct( $configuration_service ) { |
|
| 55 | + |
|
| 56 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 57 | + |
|
| 58 | + $this->configuration_service = $configuration_service; |
|
| 59 | + |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Preload the provided URIs in the local cache. |
|
| 64 | + * |
|
| 65 | + * This function will populate the local `$uri_to_post` array by running a |
|
| 66 | + * single query with all the URIs and returning the mappings in the array. |
|
| 67 | + * |
|
| 68 | + * @since 3.16.3 |
|
| 69 | + * |
|
| 70 | + * @param array $uris An array of URIs. |
|
| 71 | + */ |
|
| 72 | + public function preload_uris( $uris ) { |
|
| 73 | + |
|
| 74 | + // Bail out if there are no URIs. |
|
| 75 | + if ( 0 === count( $uris ) ) { |
|
| 76 | + return; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
| 80 | + |
|
| 81 | + $that = $this; |
|
| 82 | + $external_uris = array_filter( $uris, function ( $item ) use ( $that ) { |
|
| 83 | + return ! $that->is_internal( $item ); |
|
| 84 | + } ); |
|
| 85 | + |
|
| 86 | + $query_args = array( |
|
| 87 | + // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 88 | + 'ignore_sticky_posts' => 1, |
|
| 89 | + 'cache_results' => false, |
|
| 90 | + 'numberposts' => - 1, |
|
| 91 | + 'post_status' => 'any', |
|
| 92 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 93 | + 'meta_query' => array( |
|
| 94 | + array( |
|
| 95 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 96 | + 'value' => $uris, |
|
| 97 | + 'compare' => 'IN', |
|
| 98 | + ), |
|
| 99 | + ), |
|
| 100 | + ); |
|
| 101 | + |
|
| 102 | + // Only if the current uri is not an internal uri, entity search is |
|
| 103 | + // performed also looking at sameAs values. |
|
| 104 | + // |
|
| 105 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 106 | + if ( 0 < count( $external_uris ) ) { |
|
| 107 | + |
|
| 108 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 109 | + $query_args['meta_query'][] = array( |
|
| 110 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 111 | + 'value' => $external_uris, |
|
| 112 | + 'compare' => 'IN', |
|
| 113 | + ); |
|
| 114 | + |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + // Get the posts. |
|
| 118 | + $posts = get_posts( $query_args ); |
|
| 119 | + |
|
| 120 | + // Populate the array. We reinitialize the array on purpose because |
|
| 121 | + // we don't want these data to long live. |
|
| 122 | + $this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) use ( $that ) { |
|
| 123 | + $uris = get_post_meta( $item->ID, WL_ENTITY_URL_META_NAME ) |
|
| 124 | + + get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
| 125 | + |
|
| 126 | + return $carry |
|
| 127 | + // Get the URI related to the post and fill them with the item id. |
|
| 128 | + + array_fill_keys( $uris, $item ); |
|
| 129 | + }, array() ); |
|
| 130 | + |
|
| 131 | + // Add the not found URIs. |
|
| 132 | + $this->uri_to_post += array_fill_keys( $uris, null ); |
|
| 133 | + |
|
| 134 | + $this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." ); |
|
| 135 | + |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Reset the URI to post local cache. |
|
| 140 | + * |
|
| 141 | + * @since 3.16.3 |
|
| 142 | + */ |
|
| 143 | + public function reset_uris() { |
|
| 144 | + |
|
| 145 | + $this->uri_to_post = array(); |
|
| 146 | + |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 151 | + * |
|
| 152 | + * @since 3.2.0 |
|
| 153 | + * |
|
| 154 | + * @param string $uri The entity URI. |
|
| 155 | + * |
|
| 156 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 157 | + */ |
|
| 158 | + public function get_entity( $uri ) { |
|
| 159 | + |
|
| 160 | + $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
| 161 | + |
|
| 162 | + // Check if we've been provided with a value otherwise return null. |
|
| 163 | + if ( empty( $uri ) ) { |
|
| 164 | + return null; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $this->log->debug( "Querying post for $uri..." ); |
|
| 168 | + |
|
| 169 | + $query_args = array( |
|
| 170 | + // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 171 | + 'ignore_sticky_posts' => 1, |
|
| 172 | + 'posts_per_page' => 1, |
|
| 173 | + 'post_status' => 'any', |
|
| 174 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 175 | + 'meta_query' => array( |
|
| 176 | + array( |
|
| 177 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 178 | + 'value' => $uri, |
|
| 179 | + 'compare' => '=', |
|
| 180 | + ), |
|
| 181 | + ), |
|
| 182 | + ); |
|
| 183 | + |
|
| 184 | + // Only if the current uri is not an internal uri, entity search is |
|
| 185 | + // performed also looking at sameAs values. |
|
| 186 | + // |
|
| 187 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 188 | + if ( ! $this->is_internal( $uri ) ) { |
|
| 189 | + |
|
| 190 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 191 | + $query_args['meta_query'][] = array( |
|
| 192 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 193 | + 'value' => $uri, |
|
| 194 | + 'compare' => '=', |
|
| 195 | + ); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + $posts = get_posts( $query_args ); |
|
| 199 | + |
|
| 200 | + // Return null if no post is found. |
|
| 201 | + if ( 0 === count( $posts ) ) { |
|
| 202 | + $this->log->warn( "No post for URI $uri." ); |
|
| 203 | + |
|
| 204 | + return null; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + // Return the found post. |
|
| 208 | + return current( $posts ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Determines whether a given uri is an internal uri or not. |
|
| 213 | + * |
|
| 214 | + * @since 3.16.3 |
|
| 215 | + * |
|
| 216 | + * @param int $uri An uri. |
|
| 217 | + * |
|
| 218 | + * @return true if the uri internal to the current dataset otherwise false. |
|
| 219 | + */ |
|
| 220 | + public function is_internal( $uri ) { |
|
| 221 | + |
|
| 222 | + return ( 0 === strrpos( $uri, $this->configuration_service->get_dataset_uri() ) ); |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | 225 | } |
| 226 | 226 | \ No newline at end of file |
@@ -51,9 +51,9 @@ discard block |
||
| 51 | 51 | * |
| 52 | 52 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 53 | 53 | */ |
| 54 | - public function __construct( $configuration_service ) { |
|
| 54 | + public function __construct($configuration_service) { |
|
| 55 | 55 | |
| 56 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 56 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 57 | 57 | |
| 58 | 58 | $this->configuration_service = $configuration_service; |
| 59 | 59 | |
@@ -69,25 +69,25 @@ discard block |
||
| 69 | 69 | * |
| 70 | 70 | * @param array $uris An array of URIs. |
| 71 | 71 | */ |
| 72 | - public function preload_uris( $uris ) { |
|
| 72 | + public function preload_uris($uris) { |
|
| 73 | 73 | |
| 74 | 74 | // Bail out if there are no URIs. |
| 75 | - if ( 0 === count( $uris ) ) { |
|
| 75 | + if (0 === count($uris)) { |
|
| 76 | 76 | return; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - $this->log->trace( 'Preloading ' . count( $uris ) . ' URI(s)...' ); |
|
| 79 | + $this->log->trace('Preloading '.count($uris).' URI(s)...'); |
|
| 80 | 80 | |
| 81 | 81 | $that = $this; |
| 82 | - $external_uris = array_filter( $uris, function ( $item ) use ( $that ) { |
|
| 83 | - return ! $that->is_internal( $item ); |
|
| 82 | + $external_uris = array_filter($uris, function($item) use ($that) { |
|
| 83 | + return ! $that->is_internal($item); |
|
| 84 | 84 | } ); |
| 85 | 85 | |
| 86 | 86 | $query_args = array( |
| 87 | 87 | // See https://github.com/insideout10/wordlift-plugin/issues/654. |
| 88 | 88 | 'ignore_sticky_posts' => 1, |
| 89 | 89 | 'cache_results' => false, |
| 90 | - 'numberposts' => - 1, |
|
| 90 | + 'numberposts' => -1, |
|
| 91 | 91 | 'post_status' => 'any', |
| 92 | 92 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
| 93 | 93 | 'meta_query' => array( |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | // performed also looking at sameAs values. |
| 104 | 104 | // |
| 105 | 105 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 106 | - if ( 0 < count( $external_uris ) ) { |
|
| 106 | + if (0 < count($external_uris)) { |
|
| 107 | 107 | |
| 108 | 108 | $query_args['meta_query']['relation'] = 'OR'; |
| 109 | 109 | $query_args['meta_query'][] = array( |
@@ -115,23 +115,23 @@ discard block |
||
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | // Get the posts. |
| 118 | - $posts = get_posts( $query_args ); |
|
| 118 | + $posts = get_posts($query_args); |
|
| 119 | 119 | |
| 120 | 120 | // Populate the array. We reinitialize the array on purpose because |
| 121 | 121 | // we don't want these data to long live. |
| 122 | - $this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) use ( $that ) { |
|
| 123 | - $uris = get_post_meta( $item->ID, WL_ENTITY_URL_META_NAME ) |
|
| 124 | - + get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
| 122 | + $this->uri_to_post = array_reduce($posts, function($carry, $item) use ($that) { |
|
| 123 | + $uris = get_post_meta($item->ID, WL_ENTITY_URL_META_NAME) |
|
| 124 | + + get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_SAME_AS); |
|
| 125 | 125 | |
| 126 | 126 | return $carry |
| 127 | 127 | // Get the URI related to the post and fill them with the item id. |
| 128 | - + array_fill_keys( $uris, $item ); |
|
| 129 | - }, array() ); |
|
| 128 | + + array_fill_keys($uris, $item); |
|
| 129 | + }, array()); |
|
| 130 | 130 | |
| 131 | 131 | // Add the not found URIs. |
| 132 | - $this->uri_to_post += array_fill_keys( $uris, null ); |
|
| 132 | + $this->uri_to_post += array_fill_keys($uris, null); |
|
| 133 | 133 | |
| 134 | - $this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." ); |
|
| 134 | + $this->log->debug(count($this->uri_to_post)." URI(s) preloaded."); |
|
| 135 | 135 | |
| 136 | 136 | } |
| 137 | 137 | |
@@ -155,16 +155,16 @@ discard block |
||
| 155 | 155 | * |
| 156 | 156 | * @return WP_Post|null A WP_Post instance or null if not found. |
| 157 | 157 | */ |
| 158 | - public function get_entity( $uri ) { |
|
| 158 | + public function get_entity($uri) { |
|
| 159 | 159 | |
| 160 | - $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
| 160 | + $this->log->trace("Getting an entity post for URI $uri..."); |
|
| 161 | 161 | |
| 162 | 162 | // Check if we've been provided with a value otherwise return null. |
| 163 | - if ( empty( $uri ) ) { |
|
| 163 | + if (empty($uri)) { |
|
| 164 | 164 | return null; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - $this->log->debug( "Querying post for $uri..." ); |
|
| 167 | + $this->log->debug("Querying post for $uri..."); |
|
| 168 | 168 | |
| 169 | 169 | $query_args = array( |
| 170 | 170 | // See https://github.com/insideout10/wordlift-plugin/issues/654. |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | // performed also looking at sameAs values. |
| 186 | 186 | // |
| 187 | 187 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 188 | - if ( ! $this->is_internal( $uri ) ) { |
|
| 188 | + if ( ! $this->is_internal($uri)) { |
|
| 189 | 189 | |
| 190 | 190 | $query_args['meta_query']['relation'] = 'OR'; |
| 191 | 191 | $query_args['meta_query'][] = array( |
@@ -195,17 +195,17 @@ discard block |
||
| 195 | 195 | ); |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - $posts = get_posts( $query_args ); |
|
| 198 | + $posts = get_posts($query_args); |
|
| 199 | 199 | |
| 200 | 200 | // Return null if no post is found. |
| 201 | - if ( 0 === count( $posts ) ) { |
|
| 202 | - $this->log->warn( "No post for URI $uri." ); |
|
| 201 | + if (0 === count($posts)) { |
|
| 202 | + $this->log->warn("No post for URI $uri."); |
|
| 203 | 203 | |
| 204 | 204 | return null; |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | // Return the found post. |
| 208 | - return current( $posts ); |
|
| 208 | + return current($posts); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | /** |
@@ -217,9 +217,9 @@ discard block |
||
| 217 | 217 | * |
| 218 | 218 | * @return true if the uri internal to the current dataset otherwise false. |
| 219 | 219 | */ |
| 220 | - public function is_internal( $uri ) { |
|
| 220 | + public function is_internal($uri) { |
|
| 221 | 221 | |
| 222 | - return ( 0 === strrpos( $uri, $this->configuration_service->get_dataset_uri() ) ); |
|
| 222 | + return (0 === strrpos($uri, $this->configuration_service->get_dataset_uri())); |
|
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | } |
| 226 | 226 | \ No newline at end of file |
@@ -18,276 +18,276 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Content_Filter_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The pattern to find entities in text. |
|
| 23 | - * |
|
| 24 | - * @since 3.8.0 |
|
| 25 | - */ |
|
| 26 | - const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.8.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.13.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $configuration_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The `link by default` setting. |
|
| 48 | - * |
|
| 49 | - * @since 3.13.0 |
|
| 50 | - * @access private |
|
| 51 | - * @var bool True if link by default is enabled otherwise false. |
|
| 52 | - */ |
|
| 53 | - private $is_link_by_default; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 57 | - * |
|
| 58 | - * @since 3.16.3 |
|
| 59 | - * @access private |
|
| 60 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 61 | - */ |
|
| 62 | - private $entity_uri_service; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * A {@link Wordlift_Log_Service} instance. |
|
| 66 | - * |
|
| 67 | - * @since 3.16.0 |
|
| 68 | - * |
|
| 69 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 70 | - */ |
|
| 71 | - private $log; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 75 | - * |
|
| 76 | - * @since 3.14.2 |
|
| 77 | - * @access private |
|
| 78 | - * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 79 | - */ |
|
| 80 | - private static $instance; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 84 | - * |
|
| 85 | - * @since 3.8.0 |
|
| 86 | - * |
|
| 87 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 88 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 89 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 90 | - */ |
|
| 91 | - public function __construct( $entity_service, $configuration_service, $entity_uri_service ) { |
|
| 92 | - |
|
| 93 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 94 | - |
|
| 95 | - $this->entity_service = $entity_service; |
|
| 96 | - $this->configuration_service = $configuration_service; |
|
| 97 | - $this->entity_uri_service = $entity_uri_service; |
|
| 98 | - |
|
| 99 | - self::$instance = $this; |
|
| 100 | - |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 105 | - * |
|
| 106 | - * @since 3.14.2 |
|
| 107 | - * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 108 | - */ |
|
| 109 | - public static function get_instance() { |
|
| 110 | - |
|
| 111 | - return self::$instance; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Mangle the content by adding links to the entity pages. This function is |
|
| 116 | - * hooked to the 'the_content' WP's filter. |
|
| 117 | - * |
|
| 118 | - * @since 3.8.0 |
|
| 119 | - * |
|
| 120 | - * @param string $content The content being filtered. |
|
| 121 | - * |
|
| 122 | - * @return string The filtered content. |
|
| 123 | - */ |
|
| 124 | - public function the_content( $content ) { |
|
| 125 | - |
|
| 126 | - $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 127 | - |
|
| 128 | - // Links should be added only on the front end and not for RSS. |
|
| 129 | - if ( is_feed() ) { |
|
| 130 | - return $content; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - // Preload the `link by default` setting. |
|
| 134 | - $this->is_link_by_default = $this->configuration_service->is_link_by_default(); |
|
| 135 | - |
|
| 136 | - // Preload URIs. |
|
| 137 | - $matches = array(); |
|
| 138 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 139 | - |
|
| 140 | - // Bail out if there are no URIs. |
|
| 141 | - if ( 0 === count( $matches[3] ) ) { |
|
| 142 | - return $content; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // Preload the URIs. |
|
| 146 | - $this->entity_uri_service->preload_uris( $matches[3] ); |
|
| 147 | - |
|
| 148 | - // Replace each match of the entity tag with the entity link. If an error |
|
| 149 | - // occurs fail silently returning the original content. |
|
| 150 | - $result = preg_replace_callback( self::PATTERN, array( |
|
| 151 | - $this, |
|
| 152 | - 'link', |
|
| 153 | - ), $content ) ?: $content; |
|
| 154 | - |
|
| 155 | - $this->entity_uri_service->reset_uris(); |
|
| 156 | - |
|
| 157 | - return $result; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Get the entity match and replace it with a page link. |
|
| 162 | - * |
|
| 163 | - * @since 3.8.0 |
|
| 164 | - * |
|
| 165 | - * @param array $matches An array of matches. |
|
| 166 | - * |
|
| 167 | - * @return string The replaced text with the link to the entity page. |
|
| 168 | - */ |
|
| 169 | - private function link( $matches ) { |
|
| 170 | - |
|
| 171 | - // Get the entity itemid URI and label. |
|
| 172 | - $css_class = $matches[2]; |
|
| 173 | - $uri = $matches[3]; |
|
| 174 | - $label = $matches[4]; |
|
| 175 | - |
|
| 176 | - // Get the entity post by URI. |
|
| 177 | - $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 178 | - if ( null === $post ) { |
|
| 179 | - |
|
| 180 | - // If the entity post is not found return the label w/o the markup |
|
| 181 | - // around it. |
|
| 182 | - // |
|
| 183 | - // See https://github.com/insideout10/wordlift-plugin/issues/461. |
|
| 184 | - return $label; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 188 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 189 | - |
|
| 190 | - // Don't link if links are disabled and the entity is not link or the |
|
| 191 | - // entity is do not link. |
|
| 192 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 193 | - |
|
| 194 | - // Return the label if it's don't link. |
|
| 195 | - if ( $dont_link ) { |
|
| 196 | - return $label; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - // Get the link. |
|
| 200 | - $href = get_permalink( $post ); |
|
| 201 | - |
|
| 202 | - // Get an alternative title attribute. |
|
| 203 | - $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 204 | - |
|
| 205 | - // Return the link. |
|
| 206 | - return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Get a `title` attribute with an alternative label for the link. |
|
| 211 | - * |
|
| 212 | - * If an alternative title isn't available an empty string is returned. |
|
| 213 | - * |
|
| 214 | - * @since 3.15.0 |
|
| 215 | - * |
|
| 216 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 217 | - * @param string $label The main link label. |
|
| 218 | - * |
|
| 219 | - * @return string A `title` attribute with an alternative label or an empty |
|
| 220 | - * string if none available. |
|
| 221 | - */ |
|
| 222 | - private function get_title_attribute( $post_id, $label ) { |
|
| 223 | - |
|
| 224 | - // Get an alternative title. |
|
| 225 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 226 | - if ( ! empty( $title ) ) { |
|
| 227 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return ''; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Get a string to be used as a title attribute in links to a post |
|
| 235 | - * |
|
| 236 | - * @since 3.15.0 |
|
| 237 | - * |
|
| 238 | - * @param int $post_id The post id of the post being linked. |
|
| 239 | - * @param string $ignore_label A label to ignore. |
|
| 240 | - * |
|
| 241 | - * @return string The title to be used in the link. An empty string when |
|
| 242 | - * there is no alternative that is not the $ignore_label. |
|
| 243 | - */ |
|
| 244 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 245 | - |
|
| 246 | - // Get possible alternative labels we can select from. |
|
| 247 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 248 | - |
|
| 249 | - /* |
|
| 21 | + /** |
|
| 22 | + * The pattern to find entities in text. |
|
| 23 | + * |
|
| 24 | + * @since 3.8.0 |
|
| 25 | + */ |
|
| 26 | + const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.8.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.13.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $configuration_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The `link by default` setting. |
|
| 48 | + * |
|
| 49 | + * @since 3.13.0 |
|
| 50 | + * @access private |
|
| 51 | + * @var bool True if link by default is enabled otherwise false. |
|
| 52 | + */ |
|
| 53 | + private $is_link_by_default; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 57 | + * |
|
| 58 | + * @since 3.16.3 |
|
| 59 | + * @access private |
|
| 60 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 61 | + */ |
|
| 62 | + private $entity_uri_service; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * A {@link Wordlift_Log_Service} instance. |
|
| 66 | + * |
|
| 67 | + * @since 3.16.0 |
|
| 68 | + * |
|
| 69 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 70 | + */ |
|
| 71 | + private $log; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 75 | + * |
|
| 76 | + * @since 3.14.2 |
|
| 77 | + * @access private |
|
| 78 | + * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 79 | + */ |
|
| 80 | + private static $instance; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 84 | + * |
|
| 85 | + * @since 3.8.0 |
|
| 86 | + * |
|
| 87 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 88 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 89 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 90 | + */ |
|
| 91 | + public function __construct( $entity_service, $configuration_service, $entity_uri_service ) { |
|
| 92 | + |
|
| 93 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 94 | + |
|
| 95 | + $this->entity_service = $entity_service; |
|
| 96 | + $this->configuration_service = $configuration_service; |
|
| 97 | + $this->entity_uri_service = $entity_uri_service; |
|
| 98 | + |
|
| 99 | + self::$instance = $this; |
|
| 100 | + |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 105 | + * |
|
| 106 | + * @since 3.14.2 |
|
| 107 | + * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 108 | + */ |
|
| 109 | + public static function get_instance() { |
|
| 110 | + |
|
| 111 | + return self::$instance; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Mangle the content by adding links to the entity pages. This function is |
|
| 116 | + * hooked to the 'the_content' WP's filter. |
|
| 117 | + * |
|
| 118 | + * @since 3.8.0 |
|
| 119 | + * |
|
| 120 | + * @param string $content The content being filtered. |
|
| 121 | + * |
|
| 122 | + * @return string The filtered content. |
|
| 123 | + */ |
|
| 124 | + public function the_content( $content ) { |
|
| 125 | + |
|
| 126 | + $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 127 | + |
|
| 128 | + // Links should be added only on the front end and not for RSS. |
|
| 129 | + if ( is_feed() ) { |
|
| 130 | + return $content; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + // Preload the `link by default` setting. |
|
| 134 | + $this->is_link_by_default = $this->configuration_service->is_link_by_default(); |
|
| 135 | + |
|
| 136 | + // Preload URIs. |
|
| 137 | + $matches = array(); |
|
| 138 | + preg_match_all( self::PATTERN, $content, $matches ); |
|
| 139 | + |
|
| 140 | + // Bail out if there are no URIs. |
|
| 141 | + if ( 0 === count( $matches[3] ) ) { |
|
| 142 | + return $content; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // Preload the URIs. |
|
| 146 | + $this->entity_uri_service->preload_uris( $matches[3] ); |
|
| 147 | + |
|
| 148 | + // Replace each match of the entity tag with the entity link. If an error |
|
| 149 | + // occurs fail silently returning the original content. |
|
| 150 | + $result = preg_replace_callback( self::PATTERN, array( |
|
| 151 | + $this, |
|
| 152 | + 'link', |
|
| 153 | + ), $content ) ?: $content; |
|
| 154 | + |
|
| 155 | + $this->entity_uri_service->reset_uris(); |
|
| 156 | + |
|
| 157 | + return $result; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Get the entity match and replace it with a page link. |
|
| 162 | + * |
|
| 163 | + * @since 3.8.0 |
|
| 164 | + * |
|
| 165 | + * @param array $matches An array of matches. |
|
| 166 | + * |
|
| 167 | + * @return string The replaced text with the link to the entity page. |
|
| 168 | + */ |
|
| 169 | + private function link( $matches ) { |
|
| 170 | + |
|
| 171 | + // Get the entity itemid URI and label. |
|
| 172 | + $css_class = $matches[2]; |
|
| 173 | + $uri = $matches[3]; |
|
| 174 | + $label = $matches[4]; |
|
| 175 | + |
|
| 176 | + // Get the entity post by URI. |
|
| 177 | + $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 178 | + if ( null === $post ) { |
|
| 179 | + |
|
| 180 | + // If the entity post is not found return the label w/o the markup |
|
| 181 | + // around it. |
|
| 182 | + // |
|
| 183 | + // See https://github.com/insideout10/wordlift-plugin/issues/461. |
|
| 184 | + return $label; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 188 | + $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 189 | + |
|
| 190 | + // Don't link if links are disabled and the entity is not link or the |
|
| 191 | + // entity is do not link. |
|
| 192 | + $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 193 | + |
|
| 194 | + // Return the label if it's don't link. |
|
| 195 | + if ( $dont_link ) { |
|
| 196 | + return $label; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + // Get the link. |
|
| 200 | + $href = get_permalink( $post ); |
|
| 201 | + |
|
| 202 | + // Get an alternative title attribute. |
|
| 203 | + $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 204 | + |
|
| 205 | + // Return the link. |
|
| 206 | + return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Get a `title` attribute with an alternative label for the link. |
|
| 211 | + * |
|
| 212 | + * If an alternative title isn't available an empty string is returned. |
|
| 213 | + * |
|
| 214 | + * @since 3.15.0 |
|
| 215 | + * |
|
| 216 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 217 | + * @param string $label The main link label. |
|
| 218 | + * |
|
| 219 | + * @return string A `title` attribute with an alternative label or an empty |
|
| 220 | + * string if none available. |
|
| 221 | + */ |
|
| 222 | + private function get_title_attribute( $post_id, $label ) { |
|
| 223 | + |
|
| 224 | + // Get an alternative title. |
|
| 225 | + $title = $this->get_link_title( $post_id, $label ); |
|
| 226 | + if ( ! empty( $title ) ) { |
|
| 227 | + return 'title="' . esc_attr( $title ) . '"'; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return ''; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Get a string to be used as a title attribute in links to a post |
|
| 235 | + * |
|
| 236 | + * @since 3.15.0 |
|
| 237 | + * |
|
| 238 | + * @param int $post_id The post id of the post being linked. |
|
| 239 | + * @param string $ignore_label A label to ignore. |
|
| 240 | + * |
|
| 241 | + * @return string The title to be used in the link. An empty string when |
|
| 242 | + * there is no alternative that is not the $ignore_label. |
|
| 243 | + */ |
|
| 244 | + function get_link_title( $post_id, $ignore_label ) { |
|
| 245 | + |
|
| 246 | + // Get possible alternative labels we can select from. |
|
| 247 | + $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 248 | + |
|
| 249 | + /* |
|
| 250 | 250 | * Since the original text might use an alternative label than the |
| 251 | 251 | * Entity title, add the title itself which is not returned by the api. |
| 252 | 252 | */ |
| 253 | - $labels[] = get_the_title( $post_id ); |
|
| 254 | - |
|
| 255 | - // Add some randomness to the label selection. |
|
| 256 | - shuffle( $labels ); |
|
| 257 | - |
|
| 258 | - // Select the first label which is not to be ignored. |
|
| 259 | - $title = ''; |
|
| 260 | - foreach ( $labels as $label ) { |
|
| 261 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 262 | - $title = $label; |
|
| 263 | - break; |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - return $title; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 272 | - * the provided content. |
|
| 273 | - * |
|
| 274 | - * @since 3.14.2 |
|
| 275 | - * |
|
| 276 | - * @param string $content The content. |
|
| 277 | - * |
|
| 278 | - * @return array An array of URIs. |
|
| 279 | - */ |
|
| 280 | - public function get_entity_uris( $content ) { |
|
| 281 | - |
|
| 282 | - $matches = array(); |
|
| 283 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 284 | - |
|
| 285 | - // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 286 | - // to turn it into an object (since if the 3rd match isn't found the index |
|
| 287 | - // is not sequential. |
|
| 288 | - // |
|
| 289 | - // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 290 | - return array_values( array_unique( $matches[3] ) ); |
|
| 291 | - } |
|
| 253 | + $labels[] = get_the_title( $post_id ); |
|
| 254 | + |
|
| 255 | + // Add some randomness to the label selection. |
|
| 256 | + shuffle( $labels ); |
|
| 257 | + |
|
| 258 | + // Select the first label which is not to be ignored. |
|
| 259 | + $title = ''; |
|
| 260 | + foreach ( $labels as $label ) { |
|
| 261 | + if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 262 | + $title = $label; |
|
| 263 | + break; |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + return $title; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 272 | + * the provided content. |
|
| 273 | + * |
|
| 274 | + * @since 3.14.2 |
|
| 275 | + * |
|
| 276 | + * @param string $content The content. |
|
| 277 | + * |
|
| 278 | + * @return array An array of URIs. |
|
| 279 | + */ |
|
| 280 | + public function get_entity_uris( $content ) { |
|
| 281 | + |
|
| 282 | + $matches = array(); |
|
| 283 | + preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 284 | + |
|
| 285 | + // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 286 | + // to turn it into an object (since if the 3rd match isn't found the index |
|
| 287 | + // is not sequential. |
|
| 288 | + // |
|
| 289 | + // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 290 | + return array_values( array_unique( $matches[3] ) ); |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | 293 | } |
@@ -88,9 +88,9 @@ discard block |
||
| 88 | 88 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 89 | 89 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
| 90 | 90 | */ |
| 91 | - public function __construct( $entity_service, $configuration_service, $entity_uri_service ) { |
|
| 91 | + public function __construct($entity_service, $configuration_service, $entity_uri_service) { |
|
| 92 | 92 | |
| 93 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 93 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 94 | 94 | |
| 95 | 95 | $this->entity_service = $entity_service; |
| 96 | 96 | $this->configuration_service = $configuration_service; |
@@ -121,12 +121,12 @@ discard block |
||
| 121 | 121 | * |
| 122 | 122 | * @return string The filtered content. |
| 123 | 123 | */ |
| 124 | - public function the_content( $content ) { |
|
| 124 | + public function the_content($content) { |
|
| 125 | 125 | |
| 126 | - $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 126 | + $this->log->trace("Filtering content [ ".(is_singular() ? 'yes' : 'no')." ]..."); |
|
| 127 | 127 | |
| 128 | 128 | // Links should be added only on the front end and not for RSS. |
| 129 | - if ( is_feed() ) { |
|
| 129 | + if (is_feed()) { |
|
| 130 | 130 | return $content; |
| 131 | 131 | } |
| 132 | 132 | |
@@ -135,22 +135,22 @@ discard block |
||
| 135 | 135 | |
| 136 | 136 | // Preload URIs. |
| 137 | 137 | $matches = array(); |
| 138 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 138 | + preg_match_all(self::PATTERN, $content, $matches); |
|
| 139 | 139 | |
| 140 | 140 | // Bail out if there are no URIs. |
| 141 | - if ( 0 === count( $matches[3] ) ) { |
|
| 141 | + if (0 === count($matches[3])) { |
|
| 142 | 142 | return $content; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | // Preload the URIs. |
| 146 | - $this->entity_uri_service->preload_uris( $matches[3] ); |
|
| 146 | + $this->entity_uri_service->preload_uris($matches[3]); |
|
| 147 | 147 | |
| 148 | 148 | // Replace each match of the entity tag with the entity link. If an error |
| 149 | 149 | // occurs fail silently returning the original content. |
| 150 | - $result = preg_replace_callback( self::PATTERN, array( |
|
| 150 | + $result = preg_replace_callback(self::PATTERN, array( |
|
| 151 | 151 | $this, |
| 152 | 152 | 'link', |
| 153 | - ), $content ) ?: $content; |
|
| 153 | + ), $content) ?: $content; |
|
| 154 | 154 | |
| 155 | 155 | $this->entity_uri_service->reset_uris(); |
| 156 | 156 | |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | * |
| 167 | 167 | * @return string The replaced text with the link to the entity page. |
| 168 | 168 | */ |
| 169 | - private function link( $matches ) { |
|
| 169 | + private function link($matches) { |
|
| 170 | 170 | |
| 171 | 171 | // Get the entity itemid URI and label. |
| 172 | 172 | $css_class = $matches[2]; |
@@ -174,8 +174,8 @@ discard block |
||
| 174 | 174 | $label = $matches[4]; |
| 175 | 175 | |
| 176 | 176 | // Get the entity post by URI. |
| 177 | - $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 178 | - if ( null === $post ) { |
|
| 177 | + $post = $this->entity_service->get_entity_post_by_uri($uri); |
|
| 178 | + if (null === $post) { |
|
| 179 | 179 | |
| 180 | 180 | // If the entity post is not found return the label w/o the markup |
| 181 | 181 | // around it. |
@@ -184,23 +184,23 @@ discard block |
||
| 184 | 184 | return $label; |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 188 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 187 | + $no_link = - 1 < strpos($css_class, 'wl-no-link'); |
|
| 188 | + $link = - 1 < strpos($css_class, 'wl-link'); |
|
| 189 | 189 | |
| 190 | 190 | // Don't link if links are disabled and the entity is not link or the |
| 191 | 191 | // entity is do not link. |
| 192 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 192 | + $dont_link = ( ! $this->is_link_by_default && ! $link) || $no_link; |
|
| 193 | 193 | |
| 194 | 194 | // Return the label if it's don't link. |
| 195 | - if ( $dont_link ) { |
|
| 195 | + if ($dont_link) { |
|
| 196 | 196 | return $label; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | // Get the link. |
| 200 | - $href = get_permalink( $post ); |
|
| 200 | + $href = get_permalink($post); |
|
| 201 | 201 | |
| 202 | 202 | // Get an alternative title attribute. |
| 203 | - $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 203 | + $title_attribute = $this->get_title_attribute($post->ID, $label); |
|
| 204 | 204 | |
| 205 | 205 | // Return the link. |
| 206 | 206 | return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
@@ -219,12 +219,12 @@ discard block |
||
| 219 | 219 | * @return string A `title` attribute with an alternative label or an empty |
| 220 | 220 | * string if none available. |
| 221 | 221 | */ |
| 222 | - private function get_title_attribute( $post_id, $label ) { |
|
| 222 | + private function get_title_attribute($post_id, $label) { |
|
| 223 | 223 | |
| 224 | 224 | // Get an alternative title. |
| 225 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 226 | - if ( ! empty( $title ) ) { |
|
| 227 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 225 | + $title = $this->get_link_title($post_id, $label); |
|
| 226 | + if ( ! empty($title)) { |
|
| 227 | + return 'title="'.esc_attr($title).'"'; |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | return ''; |
@@ -241,24 +241,24 @@ discard block |
||
| 241 | 241 | * @return string The title to be used in the link. An empty string when |
| 242 | 242 | * there is no alternative that is not the $ignore_label. |
| 243 | 243 | */ |
| 244 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 244 | + function get_link_title($post_id, $ignore_label) { |
|
| 245 | 245 | |
| 246 | 246 | // Get possible alternative labels we can select from. |
| 247 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 247 | + $labels = $this->entity_service->get_alternative_labels($post_id); |
|
| 248 | 248 | |
| 249 | 249 | /* |
| 250 | 250 | * Since the original text might use an alternative label than the |
| 251 | 251 | * Entity title, add the title itself which is not returned by the api. |
| 252 | 252 | */ |
| 253 | - $labels[] = get_the_title( $post_id ); |
|
| 253 | + $labels[] = get_the_title($post_id); |
|
| 254 | 254 | |
| 255 | 255 | // Add some randomness to the label selection. |
| 256 | - shuffle( $labels ); |
|
| 256 | + shuffle($labels); |
|
| 257 | 257 | |
| 258 | 258 | // Select the first label which is not to be ignored. |
| 259 | 259 | $title = ''; |
| 260 | - foreach ( $labels as $label ) { |
|
| 261 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 260 | + foreach ($labels as $label) { |
|
| 261 | + if (0 !== strcasecmp($label, $ignore_label)) { |
|
| 262 | 262 | $title = $label; |
| 263 | 263 | break; |
| 264 | 264 | } |
@@ -277,17 +277,17 @@ discard block |
||
| 277 | 277 | * |
| 278 | 278 | * @return array An array of URIs. |
| 279 | 279 | */ |
| 280 | - public function get_entity_uris( $content ) { |
|
| 280 | + public function get_entity_uris($content) { |
|
| 281 | 281 | |
| 282 | 282 | $matches = array(); |
| 283 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 283 | + preg_match_all(Wordlift_Content_Filter_Service::PATTERN, $content, $matches); |
|
| 284 | 284 | |
| 285 | 285 | // We need to use `array_values` here in order to avoid further `json_encode` |
| 286 | 286 | // to turn it into an object (since if the 3rd match isn't found the index |
| 287 | 287 | // is not sequential. |
| 288 | 288 | // |
| 289 | 289 | // See https://github.com/insideout10/wordlift-plugin/issues/646. |
| 290 | - return array_values( array_unique( $matches[3] ) ); |
|
| 290 | + return array_values(array_unique($matches[3])); |
|
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | } |
@@ -20,134 +20,134 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | class Wordlift_Rebuild_Service extends Wordlift_Listable { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * A {@link Wordlift_Log_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.6.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $log; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * A {@link Wordlift_Sparql_Service} instance. |
|
| 34 | - * |
|
| 35 | - * @since 3.6.0 |
|
| 36 | - * @access private |
|
| 37 | - * @var \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance. |
|
| 38 | - */ |
|
| 39 | - private $sparql_service; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The {@link Wordlift_Uri_Service} instance. |
|
| 43 | - * |
|
| 44 | - * @since 3.15.0 |
|
| 45 | - * @access private |
|
| 46 | - * @var \Wordlift_Uri_Service $uri_service The {@link Wordlift_Uri_Service} instance. |
|
| 47 | - */ |
|
| 48 | - private $uri_service; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Create an instance of Wordlift_Rebuild_Service. |
|
| 52 | - * |
|
| 53 | - * @since 3.6.0 |
|
| 54 | - * |
|
| 55 | - * @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
|
| 56 | - * @param \Wordlift_Uri_Service $uri_service |
|
| 57 | - */ |
|
| 58 | - public function __construct( $sparql_service, $uri_service ) { |
|
| 59 | - |
|
| 60 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 61 | - |
|
| 62 | - $this->sparql_service = $sparql_service; |
|
| 63 | - $this->uri_service = $uri_service; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Rebuild the Linked Data remote dataset by clearing it out and repopulating |
|
| 68 | - * it with local data. |
|
| 69 | - * |
|
| 70 | - * @since 3.6.0 |
|
| 71 | - */ |
|
| 72 | - public function rebuild() { |
|
| 73 | - |
|
| 74 | - ob_clean(); |
|
| 75 | - |
|
| 76 | - // Give ourselves some time to process the data. |
|
| 77 | - set_time_limit( 21600 ); // 6 hours |
|
| 78 | - |
|
| 79 | - // Send textual output. |
|
| 80 | - header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 81 | - |
|
| 82 | - // We start at 0 by default and get to max. |
|
| 83 | - $offset = $_GET['offset'] ?: 0; |
|
| 84 | - $limit = $_GET['limit'] ?: 1; |
|
| 85 | - $max = $offset + $limit; |
|
| 86 | - |
|
| 87 | - // Whether we should run queries asynchronously, this is handled in `wordlift_constants.php`. |
|
| 88 | - $asynchronous = isset( $_GET['wl-async'] ) && 'true' === $_GET['wl-async']; |
|
| 89 | - |
|
| 90 | - // If we're starting at offset 0, then delete existing URIs and data from |
|
| 91 | - // the remote dataset. |
|
| 92 | - if ( 0 === $offset ) { |
|
| 93 | - |
|
| 94 | - // Clear out all generated URIs, since the dataset URI might have changed |
|
| 95 | - // in the process. |
|
| 96 | - $this->uri_service->delete_all(); |
|
| 97 | - |
|
| 98 | - // Delete all the triples in the remote dataset. |
|
| 99 | - $this->sparql_service->execute( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 100 | - |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - // Go through the list of published entities and posts and call the (legacy) |
|
| 104 | - // `wl_linked_data_save_post` function for each one. We're using the `process` |
|
| 105 | - // function which is provided by the parent `Wordlift_Listable` abstract class |
|
| 106 | - // and will cycle through all the posts w/ a very small memory footprint |
|
| 107 | - // in order to avoid memory errors. |
|
| 108 | - |
|
| 109 | - $count = 0; |
|
| 110 | - $log = $this->log; |
|
| 111 | - $this->process( function ( $post ) use ( &$count, $log ) { |
|
| 112 | - $count ++; |
|
| 113 | - $log->trace( "Going to save post $count, ID $post->ID..." ); |
|
| 114 | - wl_linked_data_save_post( $post->ID ); |
|
| 115 | - }, array( |
|
| 116 | - 'post_status' => 'publish', |
|
| 117 | - ), $offset, $max ); |
|
| 118 | - |
|
| 119 | - // Redirect to the next chunk. |
|
| 120 | - if ( $count == $limit ) { |
|
| 121 | - $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 122 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Flush the cache. |
|
| 126 | - Wordlift_File_Cache_Service::flush_all(); |
|
| 127 | - |
|
| 128 | - $this->log->info( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
|
| 129 | - echo( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
|
| 130 | - |
|
| 131 | - // If we're being called as AJAX, die here. |
|
| 132 | - if ( DOING_AJAX ) { |
|
| 133 | - wp_die(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Redirect using a client-side meta to avoid browsers' redirect restrictions. |
|
| 140 | - * |
|
| 141 | - * @since 3.9.8 |
|
| 142 | - * |
|
| 143 | - * @param string $url The URL to redirect to. |
|
| 144 | - */ |
|
| 145 | - private function redirect( $url ) { |
|
| 146 | - |
|
| 147 | - ob_clean(); |
|
| 148 | - |
|
| 149 | - @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 150 | - ?> |
|
| 23 | + /** |
|
| 24 | + * A {@link Wordlift_Log_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.6.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $log; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * A {@link Wordlift_Sparql_Service} instance. |
|
| 34 | + * |
|
| 35 | + * @since 3.6.0 |
|
| 36 | + * @access private |
|
| 37 | + * @var \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance. |
|
| 38 | + */ |
|
| 39 | + private $sparql_service; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The {@link Wordlift_Uri_Service} instance. |
|
| 43 | + * |
|
| 44 | + * @since 3.15.0 |
|
| 45 | + * @access private |
|
| 46 | + * @var \Wordlift_Uri_Service $uri_service The {@link Wordlift_Uri_Service} instance. |
|
| 47 | + */ |
|
| 48 | + private $uri_service; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Create an instance of Wordlift_Rebuild_Service. |
|
| 52 | + * |
|
| 53 | + * @since 3.6.0 |
|
| 54 | + * |
|
| 55 | + * @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
|
| 56 | + * @param \Wordlift_Uri_Service $uri_service |
|
| 57 | + */ |
|
| 58 | + public function __construct( $sparql_service, $uri_service ) { |
|
| 59 | + |
|
| 60 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 61 | + |
|
| 62 | + $this->sparql_service = $sparql_service; |
|
| 63 | + $this->uri_service = $uri_service; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Rebuild the Linked Data remote dataset by clearing it out and repopulating |
|
| 68 | + * it with local data. |
|
| 69 | + * |
|
| 70 | + * @since 3.6.0 |
|
| 71 | + */ |
|
| 72 | + public function rebuild() { |
|
| 73 | + |
|
| 74 | + ob_clean(); |
|
| 75 | + |
|
| 76 | + // Give ourselves some time to process the data. |
|
| 77 | + set_time_limit( 21600 ); // 6 hours |
|
| 78 | + |
|
| 79 | + // Send textual output. |
|
| 80 | + header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 81 | + |
|
| 82 | + // We start at 0 by default and get to max. |
|
| 83 | + $offset = $_GET['offset'] ?: 0; |
|
| 84 | + $limit = $_GET['limit'] ?: 1; |
|
| 85 | + $max = $offset + $limit; |
|
| 86 | + |
|
| 87 | + // Whether we should run queries asynchronously, this is handled in `wordlift_constants.php`. |
|
| 88 | + $asynchronous = isset( $_GET['wl-async'] ) && 'true' === $_GET['wl-async']; |
|
| 89 | + |
|
| 90 | + // If we're starting at offset 0, then delete existing URIs and data from |
|
| 91 | + // the remote dataset. |
|
| 92 | + if ( 0 === $offset ) { |
|
| 93 | + |
|
| 94 | + // Clear out all generated URIs, since the dataset URI might have changed |
|
| 95 | + // in the process. |
|
| 96 | + $this->uri_service->delete_all(); |
|
| 97 | + |
|
| 98 | + // Delete all the triples in the remote dataset. |
|
| 99 | + $this->sparql_service->execute( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 100 | + |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + // Go through the list of published entities and posts and call the (legacy) |
|
| 104 | + // `wl_linked_data_save_post` function for each one. We're using the `process` |
|
| 105 | + // function which is provided by the parent `Wordlift_Listable` abstract class |
|
| 106 | + // and will cycle through all the posts w/ a very small memory footprint |
|
| 107 | + // in order to avoid memory errors. |
|
| 108 | + |
|
| 109 | + $count = 0; |
|
| 110 | + $log = $this->log; |
|
| 111 | + $this->process( function ( $post ) use ( &$count, $log ) { |
|
| 112 | + $count ++; |
|
| 113 | + $log->trace( "Going to save post $count, ID $post->ID..." ); |
|
| 114 | + wl_linked_data_save_post( $post->ID ); |
|
| 115 | + }, array( |
|
| 116 | + 'post_status' => 'publish', |
|
| 117 | + ), $offset, $max ); |
|
| 118 | + |
|
| 119 | + // Redirect to the next chunk. |
|
| 120 | + if ( $count == $limit ) { |
|
| 121 | + $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 122 | + $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Flush the cache. |
|
| 126 | + Wordlift_File_Cache_Service::flush_all(); |
|
| 127 | + |
|
| 128 | + $this->log->info( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
|
| 129 | + echo( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
|
| 130 | + |
|
| 131 | + // If we're being called as AJAX, die here. |
|
| 132 | + if ( DOING_AJAX ) { |
|
| 133 | + wp_die(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Redirect using a client-side meta to avoid browsers' redirect restrictions. |
|
| 140 | + * |
|
| 141 | + * @since 3.9.8 |
|
| 142 | + * |
|
| 143 | + * @param string $url The URL to redirect to. |
|
| 144 | + */ |
|
| 145 | + private function redirect( $url ) { |
|
| 146 | + |
|
| 147 | + ob_clean(); |
|
| 148 | + |
|
| 149 | + @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 150 | + ?> |
|
| 151 | 151 | <html> |
| 152 | 152 | <head> |
| 153 | 153 | <meta http-equiv="refresh" |
@@ -159,32 +159,32 @@ discard block |
||
| 159 | 159 | </html> |
| 160 | 160 | <?php |
| 161 | 161 | |
| 162 | - exit; |
|
| 163 | - |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * List the items starting at the specified offset and up to the specified limit. |
|
| 168 | - * |
|
| 169 | - * @since 3.6.0 |
|
| 170 | - * |
|
| 171 | - * @param int $offset The start offset. |
|
| 172 | - * @param int $limit The maximum number of items to return. |
|
| 173 | - * @param array $args Additional arguments. |
|
| 174 | - * |
|
| 175 | - * @return array A array of items (or an empty array if no items are found). |
|
| 176 | - */ |
|
| 177 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 178 | - |
|
| 179 | - return get_posts( wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 180 | - 'offset' => $offset, |
|
| 181 | - 'numberposts' => $limit, |
|
| 182 | - 'fields' => 'all', |
|
| 183 | - 'orderby' => 'ID', |
|
| 184 | - 'order' => 'ASC', |
|
| 185 | - 'post_status' => 'any', |
|
| 186 | - 'cache_results' => false, |
|
| 187 | - ) ) ) ); |
|
| 188 | - } |
|
| 162 | + exit; |
|
| 163 | + |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * List the items starting at the specified offset and up to the specified limit. |
|
| 168 | + * |
|
| 169 | + * @since 3.6.0 |
|
| 170 | + * |
|
| 171 | + * @param int $offset The start offset. |
|
| 172 | + * @param int $limit The maximum number of items to return. |
|
| 173 | + * @param array $args Additional arguments. |
|
| 174 | + * |
|
| 175 | + * @return array A array of items (or an empty array if no items are found). |
|
| 176 | + */ |
|
| 177 | + function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 178 | + |
|
| 179 | + return get_posts( wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 180 | + 'offset' => $offset, |
|
| 181 | + 'numberposts' => $limit, |
|
| 182 | + 'fields' => 'all', |
|
| 183 | + 'orderby' => 'ID', |
|
| 184 | + 'order' => 'ASC', |
|
| 185 | + 'post_status' => 'any', |
|
| 186 | + 'cache_results' => false, |
|
| 187 | + ) ) ) ); |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | 190 | } |
@@ -55,9 +55,9 @@ discard block |
||
| 55 | 55 | * @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
| 56 | 56 | * @param \Wordlift_Uri_Service $uri_service |
| 57 | 57 | */ |
| 58 | - public function __construct( $sparql_service, $uri_service ) { |
|
| 58 | + public function __construct($sparql_service, $uri_service) { |
|
| 59 | 59 | |
| 60 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 60 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Rebuild_Service'); |
|
| 61 | 61 | |
| 62 | 62 | $this->sparql_service = $sparql_service; |
| 63 | 63 | $this->uri_service = $uri_service; |
@@ -74,10 +74,10 @@ discard block |
||
| 74 | 74 | ob_clean(); |
| 75 | 75 | |
| 76 | 76 | // Give ourselves some time to process the data. |
| 77 | - set_time_limit( 21600 ); // 6 hours |
|
| 77 | + set_time_limit(21600); // 6 hours |
|
| 78 | 78 | |
| 79 | 79 | // Send textual output. |
| 80 | - header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 80 | + header('Content-type: text/plain; charset=utf-8'); |
|
| 81 | 81 | |
| 82 | 82 | // We start at 0 by default and get to max. |
| 83 | 83 | $offset = $_GET['offset'] ?: 0; |
@@ -85,18 +85,18 @@ discard block |
||
| 85 | 85 | $max = $offset + $limit; |
| 86 | 86 | |
| 87 | 87 | // Whether we should run queries asynchronously, this is handled in `wordlift_constants.php`. |
| 88 | - $asynchronous = isset( $_GET['wl-async'] ) && 'true' === $_GET['wl-async']; |
|
| 88 | + $asynchronous = isset($_GET['wl-async']) && 'true' === $_GET['wl-async']; |
|
| 89 | 89 | |
| 90 | 90 | // If we're starting at offset 0, then delete existing URIs and data from |
| 91 | 91 | // the remote dataset. |
| 92 | - if ( 0 === $offset ) { |
|
| 92 | + if (0 === $offset) { |
|
| 93 | 93 | |
| 94 | 94 | // Clear out all generated URIs, since the dataset URI might have changed |
| 95 | 95 | // in the process. |
| 96 | 96 | $this->uri_service->delete_all(); |
| 97 | 97 | |
| 98 | 98 | // Delete all the triples in the remote dataset. |
| 99 | - $this->sparql_service->execute( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 99 | + $this->sparql_service->execute('DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };'); |
|
| 100 | 100 | |
| 101 | 101 | } |
| 102 | 102 | |
@@ -108,28 +108,28 @@ discard block |
||
| 108 | 108 | |
| 109 | 109 | $count = 0; |
| 110 | 110 | $log = $this->log; |
| 111 | - $this->process( function ( $post ) use ( &$count, $log ) { |
|
| 112 | - $count ++; |
|
| 113 | - $log->trace( "Going to save post $count, ID $post->ID..." ); |
|
| 114 | - wl_linked_data_save_post( $post->ID ); |
|
| 111 | + $this->process(function($post) use (&$count, $log) { |
|
| 112 | + $count++; |
|
| 113 | + $log->trace("Going to save post $count, ID $post->ID..."); |
|
| 114 | + wl_linked_data_save_post($post->ID); |
|
| 115 | 115 | }, array( |
| 116 | 116 | 'post_status' => 'publish', |
| 117 | - ), $offset, $max ); |
|
| 117 | + ), $offset, $max); |
|
| 118 | 118 | |
| 119 | 119 | // Redirect to the next chunk. |
| 120 | - if ( $count == $limit ) { |
|
| 121 | - $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 122 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 120 | + if ($count == $limit) { |
|
| 121 | + $log->trace('Redirecting to post #'.($offset + 1).'...'); |
|
| 122 | + $this->redirect(admin_url('admin-ajax.php?action=wl_rebuild&offset='.($offset + $limit).'&limit='.$limit.'&wl-async='.($asynchronous ? 'true' : 'false'))); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Flush the cache. |
| 126 | 126 | Wordlift_File_Cache_Service::flush_all(); |
| 127 | 127 | |
| 128 | - $this->log->info( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
|
| 129 | - echo( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
|
| 128 | + $this->log->info("Rebuild complete [ count :: $count ][ limit :: $limit ]"); |
|
| 129 | + echo("Rebuild complete [ count :: $count ][ limit :: $limit ]"); |
|
| 130 | 130 | |
| 131 | 131 | // If we're being called as AJAX, die here. |
| 132 | - if ( DOING_AJAX ) { |
|
| 132 | + if (DOING_AJAX) { |
|
| 133 | 133 | wp_die(); |
| 134 | 134 | } |
| 135 | 135 | |
@@ -142,16 +142,16 @@ discard block |
||
| 142 | 142 | * |
| 143 | 143 | * @param string $url The URL to redirect to. |
| 144 | 144 | */ |
| 145 | - private function redirect( $url ) { |
|
| 145 | + private function redirect($url) { |
|
| 146 | 146 | |
| 147 | 147 | ob_clean(); |
| 148 | 148 | |
| 149 | - @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 149 | + @header('Content-Type: text/html; charset='.get_option('blog_charset')); |
|
| 150 | 150 | ?> |
| 151 | 151 | <html> |
| 152 | 152 | <head> |
| 153 | 153 | <meta http-equiv="refresh" |
| 154 | - content="0; <?php echo esc_attr( $url ); ?>"> |
|
| 154 | + content="0; <?php echo esc_attr($url); ?>"> |
|
| 155 | 155 | </head> |
| 156 | 156 | <body> |
| 157 | 157 | Rebuilding, please wait... |
@@ -174,9 +174,9 @@ discard block |
||
| 174 | 174 | * |
| 175 | 175 | * @return array A array of items (or an empty array if no items are found). |
| 176 | 176 | */ |
| 177 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 177 | + function find($offset = 0, $limit = 10, $args = array()) { |
|
| 178 | 178 | |
| 179 | - return get_posts( wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 179 | + return get_posts(wp_parse_args($args, Wordlift_Entity_Service::add_criterias(array( |
|
| 180 | 180 | 'offset' => $offset, |
| 181 | 181 | 'numberposts' => $limit, |
| 182 | 182 | 'fields' => 'all', |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | 'order' => 'ASC', |
| 185 | 185 | 'post_status' => 'any', |
| 186 | 186 | 'cache_results' => false, |
| 187 | - ) ) ) ); |
|
| 187 | + )))); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | } |
@@ -16,165 +16,165 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Entity_Post_Type_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The entity post type. |
|
| 21 | - * |
|
| 22 | - * @since 3.6.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var string $post_type The entity post type. |
|
| 25 | - */ |
|
| 26 | - private $post_type; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The entity type slug. |
|
| 30 | - * |
|
| 31 | - * @since 3.6.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var string $slug The entity type slug. |
|
| 34 | - */ |
|
| 35 | - private $slug; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * A {@link Wordlift_Log_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.16.3 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $log; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * A singleton instance of the entity type service. |
|
| 48 | - * |
|
| 49 | - * @since 3.6.0 |
|
| 50 | - * @access private |
|
| 51 | - * @var Wordlift_Entity_Post_Type_Service |
|
| 52 | - */ |
|
| 53 | - private static $instance; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Create an entity type service instance. |
|
| 57 | - * |
|
| 58 | - * @since 3.6.0 |
|
| 59 | - * |
|
| 60 | - * @param string $post_type The post type, e.g. entity. |
|
| 61 | - * @param string $slug The entity type slug, if the slug is empty, the default slug will be used. |
|
| 62 | - */ |
|
| 63 | - public function __construct( $post_type, $slug ) { |
|
| 64 | - |
|
| 65 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 66 | - |
|
| 67 | - $this->post_type = $post_type; |
|
| 68 | - |
|
| 69 | - // We cannot assign an empty slug to the register_post_type function, therefore if the slug is empty we default |
|
| 70 | - // to the type name. |
|
| 71 | - $this->slug = $slug ?: $post_type; |
|
| 72 | - |
|
| 73 | - self::$instance = $this; |
|
| 74 | - |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get the entity type service singleton instance. |
|
| 79 | - * |
|
| 80 | - * @since 3.6.0 |
|
| 81 | - * |
|
| 82 | - * @return Wordlift_Entity_Post_Type_Service The entity type service singleton instance. |
|
| 83 | - */ |
|
| 84 | - public static function get_instance() { |
|
| 85 | - |
|
| 86 | - return self::$instance; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Get the entity type slug. |
|
| 91 | - * |
|
| 92 | - * @since 3.6.0 |
|
| 93 | - * |
|
| 94 | - * @return string The entity type slug. |
|
| 95 | - */ |
|
| 96 | - public function get_slug() { |
|
| 97 | - |
|
| 98 | - return $this->slug; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get the entity post type name. |
|
| 103 | - * |
|
| 104 | - * @since 3.6.0 |
|
| 105 | - * |
|
| 106 | - * @return string The entity post type. |
|
| 107 | - */ |
|
| 108 | - public function get_post_type() { |
|
| 109 | - |
|
| 110 | - return $this->post_type; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Register the WordLift entity post type. This method is hooked to WordPress' init action. |
|
| 116 | - * |
|
| 117 | - * @since 3.6.0 |
|
| 118 | - */ |
|
| 119 | - public function register() { |
|
| 120 | - |
|
| 121 | - $this->log->debug( "Registering post type $this->post_type..." ); |
|
| 122 | - |
|
| 123 | - $labels = array( |
|
| 124 | - 'name' => _x( 'Vocabulary', 'post type general name', 'wordlift' ), |
|
| 125 | - 'singular_name' => _x( 'Entity', 'post type singular name', 'wordlift' ), |
|
| 126 | - 'add_new' => _x( 'Add New Entity', 'entity', 'wordlift' ), |
|
| 127 | - 'add_new_item' => __( 'Add New Entity', 'wordlift' ), |
|
| 128 | - 'edit_item' => __( 'Edit Entity', 'wordlift' ), |
|
| 129 | - 'new_item' => __( 'New Entity', 'wordlift' ), |
|
| 130 | - 'all_items' => __( 'All Entities', 'wordlift' ), |
|
| 131 | - 'view_item' => __( 'View Entity', 'wordlift' ), |
|
| 132 | - 'search_items' => __( 'Search in Vocabulary', 'wordlift' ), |
|
| 133 | - 'not_found' => __( 'No entities found', 'wordlift' ), |
|
| 134 | - 'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ), |
|
| 135 | - 'parent_item_colon' => '', |
|
| 136 | - 'menu_name' => __( 'Vocabulary', 'wordlift' ), |
|
| 137 | - ); |
|
| 138 | - |
|
| 139 | - $args = array( |
|
| 140 | - 'labels' => $labels, |
|
| 141 | - 'description' => 'Holds our vocabulary (set of entities) and entity specific data', |
|
| 142 | - 'public' => true, |
|
| 143 | - 'menu_position' => 20, |
|
| 144 | - // after the pages menu. |
|
| 145 | - // Add support for 'authors' and 'revisions': |
|
| 146 | - // * see https://github.com/insideout10/wordlift-plugin/issues/395 |
|
| 147 | - // * see https://github.com/insideout10/wordlift-plugin/issues/376 |
|
| 148 | - 'supports' => array( |
|
| 149 | - 'title', |
|
| 150 | - 'editor', |
|
| 151 | - 'thumbnail', |
|
| 152 | - 'excerpt', |
|
| 153 | - 'comments', |
|
| 154 | - 'author', |
|
| 155 | - 'revisions', |
|
| 156 | - ), |
|
| 157 | - 'has_archive' => true, |
|
| 158 | - 'menu_icon' => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
| 159 | - // Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service. |
|
| 160 | - 'rewrite' => array( 'slug' => $this->slug ), |
|
| 161 | - 'capability_type' => array( |
|
| 162 | - 'wordlift_entity', |
|
| 163 | - 'wordlift_entities', |
|
| 164 | - ), |
|
| 165 | - ); |
|
| 166 | - |
|
| 167 | - register_post_type( $this->post_type, $args ); |
|
| 168 | - |
|
| 169 | - // Enable WP's standard `category` taxonomy for entities. |
|
| 170 | - // |
|
| 171 | - // While this enables editors to bind entities to the WP posts' category |
|
| 172 | - // taxonomy, in Wordlift_Category_Taxonomy_Service we also need to alter |
|
| 173 | - // WP's main category query to include the `entity` post type. |
|
| 174 | - // |
|
| 175 | - // See https://github.com/insideout10/wordlift-plugin/issues/442 |
|
| 176 | - register_taxonomy_for_object_type( 'category', $this->post_type ); |
|
| 177 | - |
|
| 178 | - } |
|
| 19 | + /** |
|
| 20 | + * The entity post type. |
|
| 21 | + * |
|
| 22 | + * @since 3.6.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var string $post_type The entity post type. |
|
| 25 | + */ |
|
| 26 | + private $post_type; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The entity type slug. |
|
| 30 | + * |
|
| 31 | + * @since 3.6.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var string $slug The entity type slug. |
|
| 34 | + */ |
|
| 35 | + private $slug; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * A {@link Wordlift_Log_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.16.3 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $log; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * A singleton instance of the entity type service. |
|
| 48 | + * |
|
| 49 | + * @since 3.6.0 |
|
| 50 | + * @access private |
|
| 51 | + * @var Wordlift_Entity_Post_Type_Service |
|
| 52 | + */ |
|
| 53 | + private static $instance; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Create an entity type service instance. |
|
| 57 | + * |
|
| 58 | + * @since 3.6.0 |
|
| 59 | + * |
|
| 60 | + * @param string $post_type The post type, e.g. entity. |
|
| 61 | + * @param string $slug The entity type slug, if the slug is empty, the default slug will be used. |
|
| 62 | + */ |
|
| 63 | + public function __construct( $post_type, $slug ) { |
|
| 64 | + |
|
| 65 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 66 | + |
|
| 67 | + $this->post_type = $post_type; |
|
| 68 | + |
|
| 69 | + // We cannot assign an empty slug to the register_post_type function, therefore if the slug is empty we default |
|
| 70 | + // to the type name. |
|
| 71 | + $this->slug = $slug ?: $post_type; |
|
| 72 | + |
|
| 73 | + self::$instance = $this; |
|
| 74 | + |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get the entity type service singleton instance. |
|
| 79 | + * |
|
| 80 | + * @since 3.6.0 |
|
| 81 | + * |
|
| 82 | + * @return Wordlift_Entity_Post_Type_Service The entity type service singleton instance. |
|
| 83 | + */ |
|
| 84 | + public static function get_instance() { |
|
| 85 | + |
|
| 86 | + return self::$instance; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Get the entity type slug. |
|
| 91 | + * |
|
| 92 | + * @since 3.6.0 |
|
| 93 | + * |
|
| 94 | + * @return string The entity type slug. |
|
| 95 | + */ |
|
| 96 | + public function get_slug() { |
|
| 97 | + |
|
| 98 | + return $this->slug; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get the entity post type name. |
|
| 103 | + * |
|
| 104 | + * @since 3.6.0 |
|
| 105 | + * |
|
| 106 | + * @return string The entity post type. |
|
| 107 | + */ |
|
| 108 | + public function get_post_type() { |
|
| 109 | + |
|
| 110 | + return $this->post_type; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Register the WordLift entity post type. This method is hooked to WordPress' init action. |
|
| 116 | + * |
|
| 117 | + * @since 3.6.0 |
|
| 118 | + */ |
|
| 119 | + public function register() { |
|
| 120 | + |
|
| 121 | + $this->log->debug( "Registering post type $this->post_type..." ); |
|
| 122 | + |
|
| 123 | + $labels = array( |
|
| 124 | + 'name' => _x( 'Vocabulary', 'post type general name', 'wordlift' ), |
|
| 125 | + 'singular_name' => _x( 'Entity', 'post type singular name', 'wordlift' ), |
|
| 126 | + 'add_new' => _x( 'Add New Entity', 'entity', 'wordlift' ), |
|
| 127 | + 'add_new_item' => __( 'Add New Entity', 'wordlift' ), |
|
| 128 | + 'edit_item' => __( 'Edit Entity', 'wordlift' ), |
|
| 129 | + 'new_item' => __( 'New Entity', 'wordlift' ), |
|
| 130 | + 'all_items' => __( 'All Entities', 'wordlift' ), |
|
| 131 | + 'view_item' => __( 'View Entity', 'wordlift' ), |
|
| 132 | + 'search_items' => __( 'Search in Vocabulary', 'wordlift' ), |
|
| 133 | + 'not_found' => __( 'No entities found', 'wordlift' ), |
|
| 134 | + 'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ), |
|
| 135 | + 'parent_item_colon' => '', |
|
| 136 | + 'menu_name' => __( 'Vocabulary', 'wordlift' ), |
|
| 137 | + ); |
|
| 138 | + |
|
| 139 | + $args = array( |
|
| 140 | + 'labels' => $labels, |
|
| 141 | + 'description' => 'Holds our vocabulary (set of entities) and entity specific data', |
|
| 142 | + 'public' => true, |
|
| 143 | + 'menu_position' => 20, |
|
| 144 | + // after the pages menu. |
|
| 145 | + // Add support for 'authors' and 'revisions': |
|
| 146 | + // * see https://github.com/insideout10/wordlift-plugin/issues/395 |
|
| 147 | + // * see https://github.com/insideout10/wordlift-plugin/issues/376 |
|
| 148 | + 'supports' => array( |
|
| 149 | + 'title', |
|
| 150 | + 'editor', |
|
| 151 | + 'thumbnail', |
|
| 152 | + 'excerpt', |
|
| 153 | + 'comments', |
|
| 154 | + 'author', |
|
| 155 | + 'revisions', |
|
| 156 | + ), |
|
| 157 | + 'has_archive' => true, |
|
| 158 | + 'menu_icon' => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
| 159 | + // Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service. |
|
| 160 | + 'rewrite' => array( 'slug' => $this->slug ), |
|
| 161 | + 'capability_type' => array( |
|
| 162 | + 'wordlift_entity', |
|
| 163 | + 'wordlift_entities', |
|
| 164 | + ), |
|
| 165 | + ); |
|
| 166 | + |
|
| 167 | + register_post_type( $this->post_type, $args ); |
|
| 168 | + |
|
| 169 | + // Enable WP's standard `category` taxonomy for entities. |
|
| 170 | + // |
|
| 171 | + // While this enables editors to bind entities to the WP posts' category |
|
| 172 | + // taxonomy, in Wordlift_Category_Taxonomy_Service we also need to alter |
|
| 173 | + // WP's main category query to include the `entity` post type. |
|
| 174 | + // |
|
| 175 | + // See https://github.com/insideout10/wordlift-plugin/issues/442 |
|
| 176 | + register_taxonomy_for_object_type( 'category', $this->post_type ); |
|
| 177 | + |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | 180 | } |
@@ -60,9 +60,9 @@ discard block |
||
| 60 | 60 | * @param string $post_type The post type, e.g. entity. |
| 61 | 61 | * @param string $slug The entity type slug, if the slug is empty, the default slug will be used. |
| 62 | 62 | */ |
| 63 | - public function __construct( $post_type, $slug ) { |
|
| 63 | + public function __construct($post_type, $slug) { |
|
| 64 | 64 | |
| 65 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 65 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 66 | 66 | |
| 67 | 67 | $this->post_type = $post_type; |
| 68 | 68 | |
@@ -118,22 +118,22 @@ discard block |
||
| 118 | 118 | */ |
| 119 | 119 | public function register() { |
| 120 | 120 | |
| 121 | - $this->log->debug( "Registering post type $this->post_type..." ); |
|
| 121 | + $this->log->debug("Registering post type $this->post_type..."); |
|
| 122 | 122 | |
| 123 | 123 | $labels = array( |
| 124 | - 'name' => _x( 'Vocabulary', 'post type general name', 'wordlift' ), |
|
| 125 | - 'singular_name' => _x( 'Entity', 'post type singular name', 'wordlift' ), |
|
| 126 | - 'add_new' => _x( 'Add New Entity', 'entity', 'wordlift' ), |
|
| 127 | - 'add_new_item' => __( 'Add New Entity', 'wordlift' ), |
|
| 128 | - 'edit_item' => __( 'Edit Entity', 'wordlift' ), |
|
| 129 | - 'new_item' => __( 'New Entity', 'wordlift' ), |
|
| 130 | - 'all_items' => __( 'All Entities', 'wordlift' ), |
|
| 131 | - 'view_item' => __( 'View Entity', 'wordlift' ), |
|
| 132 | - 'search_items' => __( 'Search in Vocabulary', 'wordlift' ), |
|
| 133 | - 'not_found' => __( 'No entities found', 'wordlift' ), |
|
| 134 | - 'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ), |
|
| 124 | + 'name' => _x('Vocabulary', 'post type general name', 'wordlift'), |
|
| 125 | + 'singular_name' => _x('Entity', 'post type singular name', 'wordlift'), |
|
| 126 | + 'add_new' => _x('Add New Entity', 'entity', 'wordlift'), |
|
| 127 | + 'add_new_item' => __('Add New Entity', 'wordlift'), |
|
| 128 | + 'edit_item' => __('Edit Entity', 'wordlift'), |
|
| 129 | + 'new_item' => __('New Entity', 'wordlift'), |
|
| 130 | + 'all_items' => __('All Entities', 'wordlift'), |
|
| 131 | + 'view_item' => __('View Entity', 'wordlift'), |
|
| 132 | + 'search_items' => __('Search in Vocabulary', 'wordlift'), |
|
| 133 | + 'not_found' => __('No entities found', 'wordlift'), |
|
| 134 | + 'not_found_in_trash' => __('No entities found in the Trash', 'wordlift'), |
|
| 135 | 135 | 'parent_item_colon' => '', |
| 136 | - 'menu_name' => __( 'Vocabulary', 'wordlift' ), |
|
| 136 | + 'menu_name' => __('Vocabulary', 'wordlift'), |
|
| 137 | 137 | ); |
| 138 | 138 | |
| 139 | 139 | $args = array( |
@@ -155,16 +155,16 @@ discard block |
||
| 155 | 155 | 'revisions', |
| 156 | 156 | ), |
| 157 | 157 | 'has_archive' => true, |
| 158 | - 'menu_icon' => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
| 158 | + 'menu_icon' => WP_CONTENT_URL.'/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
| 159 | 159 | // Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service. |
| 160 | - 'rewrite' => array( 'slug' => $this->slug ), |
|
| 160 | + 'rewrite' => array('slug' => $this->slug), |
|
| 161 | 161 | 'capability_type' => array( |
| 162 | 162 | 'wordlift_entity', |
| 163 | 163 | 'wordlift_entities', |
| 164 | 164 | ), |
| 165 | 165 | ); |
| 166 | 166 | |
| 167 | - register_post_type( $this->post_type, $args ); |
|
| 167 | + register_post_type($this->post_type, $args); |
|
| 168 | 168 | |
| 169 | 169 | // Enable WP's standard `category` taxonomy for entities. |
| 170 | 170 | // |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | // WP's main category query to include the `entity` post type. |
| 174 | 174 | // |
| 175 | 175 | // See https://github.com/insideout10/wordlift-plugin/issues/442 |
| 176 | - register_taxonomy_for_object_type( 'category', $this->post_type ); |
|
| 176 | + register_taxonomy_for_object_type('category', $this->post_type); |
|
| 177 | 177 | |
| 178 | 178 | } |
| 179 | 179 | |
@@ -21,18 +21,18 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Deactivator { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Short Description. (use period) |
|
| 26 | - * |
|
| 27 | - * Long Description. |
|
| 28 | - * |
|
| 29 | - * @since 1.0.0 |
|
| 30 | - */ |
|
| 31 | - public static function deactivate() { |
|
| 24 | + /** |
|
| 25 | + * Short Description. (use period) |
|
| 26 | + * |
|
| 27 | + * Long Description. |
|
| 28 | + * |
|
| 29 | + * @since 1.0.0 |
|
| 30 | + */ |
|
| 31 | + public static function deactivate() { |
|
| 32 | 32 | |
| 33 | - // Clear caches. |
|
| 34 | - Wordlift_File_Cache_Service::flush_all(); |
|
| 33 | + // Clear caches. |
|
| 34 | + Wordlift_File_Cache_Service::flush_all(); |
|
| 35 | 35 | |
| 36 | - } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | 38 | } |
@@ -8,7 +8,7 @@ |
||
| 8 | 8 | * @package Wordlift |
| 9 | 9 | * @subpackage Wordlift/includes/cache |
| 10 | 10 | */ |
| 11 | -require_once plugin_dir_path( dirname( dirname( __FILE__ ) ) ) . 'includes/cache/intf-wordlift-cache-service.php'; |
|
| 12 | -require_once plugin_dir_path( dirname( dirname( __FILE__ ) ) ) . 'includes/cache/class-wordlift-file-cache-service.php'; |
|
| 13 | -require_once plugin_dir_path( dirname( dirname( __FILE__ ) ) ) . 'includes/cache/class-wordlift-cached-post-converter.php'; |
|
| 14 | -require_once plugin_dir_path( dirname( dirname( __FILE__ ) ) ) . 'includes/cache/class-wordlift-cached-entity-uri-service.php'; |
|
| 11 | +require_once plugin_dir_path(dirname(dirname(__FILE__))).'includes/cache/intf-wordlift-cache-service.php'; |
|
| 12 | +require_once plugin_dir_path(dirname(dirname(__FILE__))).'includes/cache/class-wordlift-file-cache-service.php'; |
|
| 13 | +require_once plugin_dir_path(dirname(dirname(__FILE__))).'includes/cache/class-wordlift-cached-post-converter.php'; |
|
| 14 | +require_once plugin_dir_path(dirname(dirname(__FILE__))).'includes/cache/class-wordlift-cached-entity-uri-service.php'; |
|
@@ -17,53 +17,53 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | interface Wordlift_Cache_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Get the cached response for the specified `id`. |
|
| 22 | - * |
|
| 23 | - * @since 3.16.0 |
|
| 24 | - * |
|
| 25 | - * @param string $id The cache `id`. |
|
| 26 | - * |
|
| 27 | - * @return mixed|false The cached contents or false if the cache isn't found. |
|
| 28 | - */ |
|
| 29 | - function get_cache( $id ); |
|
| 20 | + /** |
|
| 21 | + * Get the cached response for the specified `id`. |
|
| 22 | + * |
|
| 23 | + * @since 3.16.0 |
|
| 24 | + * |
|
| 25 | + * @param string $id The cache `id`. |
|
| 26 | + * |
|
| 27 | + * @return mixed|false The cached contents or false if the cache isn't found. |
|
| 28 | + */ |
|
| 29 | + function get_cache( $id ); |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Check whether we have cached results for the provided id. |
|
| 33 | - * |
|
| 34 | - * @since 3.16.3 |
|
| 35 | - * |
|
| 36 | - * @param string $id The cache `id`. |
|
| 37 | - * |
|
| 38 | - * @return bool True if we have cached results otherwise false. |
|
| 39 | - */ |
|
| 40 | - function has_cache( $id ); |
|
| 31 | + /** |
|
| 32 | + * Check whether we have cached results for the provided id. |
|
| 33 | + * |
|
| 34 | + * @since 3.16.3 |
|
| 35 | + * |
|
| 36 | + * @param string $id The cache `id`. |
|
| 37 | + * |
|
| 38 | + * @return bool True if we have cached results otherwise false. |
|
| 39 | + */ |
|
| 40 | + function has_cache( $id ); |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Set the cache contents for the specified `id`. |
|
| 44 | - * |
|
| 45 | - * @since 3.16.0 |
|
| 46 | - * |
|
| 47 | - * @param string $id The cache id. |
|
| 48 | - * @param mixed $contents The cache contents. |
|
| 49 | - */ |
|
| 50 | - function set_cache( $id, $contents ); |
|
| 42 | + /** |
|
| 43 | + * Set the cache contents for the specified `id`. |
|
| 44 | + * |
|
| 45 | + * @since 3.16.0 |
|
| 46 | + * |
|
| 47 | + * @param string $id The cache id. |
|
| 48 | + * @param mixed $contents The cache contents. |
|
| 49 | + */ |
|
| 50 | + function set_cache( $id, $contents ); |
|
| 51 | 51 | |
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Delete the cache for the specified `id`. |
|
| 55 | - * |
|
| 56 | - * @since 3.16.0 |
|
| 57 | - * |
|
| 58 | - * @param string $id The cache `id`. |
|
| 59 | - */ |
|
| 60 | - function delete_cache( $id ); |
|
| 53 | + /** |
|
| 54 | + * Delete the cache for the specified `id`. |
|
| 55 | + * |
|
| 56 | + * @since 3.16.0 |
|
| 57 | + * |
|
| 58 | + * @param string $id The cache `id`. |
|
| 59 | + */ |
|
| 60 | + function delete_cache( $id ); |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Flush the whole cache. |
|
| 64 | - * |
|
| 65 | - * @since 3.16.0 |
|
| 66 | - */ |
|
| 67 | - function flush(); |
|
| 62 | + /** |
|
| 63 | + * Flush the whole cache. |
|
| 64 | + * |
|
| 65 | + * @since 3.16.0 |
|
| 66 | + */ |
|
| 67 | + function flush(); |
|
| 68 | 68 | |
| 69 | 69 | } |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | * |
| 27 | 27 | * @return mixed|false The cached contents or false if the cache isn't found. |
| 28 | 28 | */ |
| 29 | - function get_cache( $id ); |
|
| 29 | + function get_cache($id); |
|
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | 32 | * Check whether we have cached results for the provided id. |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * |
| 38 | 38 | * @return bool True if we have cached results otherwise false. |
| 39 | 39 | */ |
| 40 | - function has_cache( $id ); |
|
| 40 | + function has_cache($id); |
|
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | 43 | * Set the cache contents for the specified `id`. |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | * @param string $id The cache id. |
| 48 | 48 | * @param mixed $contents The cache contents. |
| 49 | 49 | */ |
| 50 | - function set_cache( $id, $contents ); |
|
| 50 | + function set_cache($id, $contents); |
|
| 51 | 51 | |
| 52 | 52 | |
| 53 | 53 | /** |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @param string $id The cache `id`. |
| 59 | 59 | */ |
| 60 | - function delete_cache( $id ); |
|
| 60 | + function delete_cache($id); |
|
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | 63 | * Flush the whole cache. |
@@ -14,200 +14,200 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Cached_Entity_Uri_Service extends Wordlift_Entity_Uri_Service { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The {@link Wordlift_Cache_Service} instance. |
|
| 19 | - * |
|
| 20 | - * @since 3.16.3 |
|
| 21 | - * @access private |
|
| 22 | - * @var \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance. |
|
| 23 | - */ |
|
| 24 | - private $cache_service; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * A {@link Wordlift_Log_Service} instance. |
|
| 28 | - * |
|
| 29 | - * @since 3.16.3 |
|
| 30 | - * @access private |
|
| 31 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 32 | - */ |
|
| 33 | - private $log; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Create a {@link Wordlift_Cached_Entity_Uri_Service} instance. |
|
| 37 | - * |
|
| 38 | - * @since 3.16.3 |
|
| 39 | - * |
|
| 40 | - * @param \Wordlift_Configuration_Service $configuration_service |
|
| 41 | - * @param \Wordlift_Cache_Service $cache_service |
|
| 42 | - */ |
|
| 43 | - public function __construct( $configuration_service, $cache_service ) { |
|
| 44 | - parent::__construct( $configuration_service ); |
|
| 45 | - |
|
| 46 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 47 | - |
|
| 48 | - // Add hooks for meta being added/modified/deleted. |
|
| 49 | - $this->cache_service = $cache_service; |
|
| 50 | - |
|
| 51 | - add_action( 'add_post_meta', array( |
|
| 52 | - $this, |
|
| 53 | - 'on_before_post_meta_add', |
|
| 54 | - ), 10, 3 ); |
|
| 55 | - add_action( 'update_post_meta', array( |
|
| 56 | - $this, |
|
| 57 | - 'on_before_post_meta_change', |
|
| 58 | - ), 10, 4 ); |
|
| 59 | - add_action( 'delete_post_meta', array( |
|
| 60 | - $this, |
|
| 61 | - 'on_before_post_meta_change', |
|
| 62 | - ), 10, 4 ); |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Preload the URIs. |
|
| 68 | - * |
|
| 69 | - * @since 3.16.3 |
|
| 70 | - * |
|
| 71 | - * @param array $uris Preload an array of URIs. |
|
| 72 | - */ |
|
| 73 | - public function preload_uris( $uris ) { |
|
| 74 | - |
|
| 75 | - // Filter the URIs which aren't yet cached. |
|
| 76 | - $cache_service = $this->cache_service; |
|
| 77 | - $uris_to_cache = array_filter( (array) $uris, function ( $item ) use ( $cache_service ) { |
|
| 78 | - return ! $cache_service->has_cache( $item ); |
|
| 79 | - } ); |
|
| 80 | - |
|
| 81 | - // Preload the URIs. |
|
| 82 | - parent::preload_uris( $uris_to_cache ); |
|
| 83 | - |
|
| 84 | - // Store them in cache. |
|
| 85 | - if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) { |
|
| 86 | - foreach ( $this->uri_to_post as $uri => $post ) { |
|
| 87 | - $this->set_cache( $uri, $post ); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Get the entity post for the specified URI. |
|
| 95 | - * |
|
| 96 | - * @since 3.16.3 |
|
| 97 | - * |
|
| 98 | - * @param string $uri The URI. |
|
| 99 | - * |
|
| 100 | - * @return null|WP_Post The {@link WP_Post} or null if not found. |
|
| 101 | - */ |
|
| 102 | - public function get_entity( $uri ) { |
|
| 103 | - |
|
| 104 | - $this->log->trace( "Getting entity for uri $uri..." ); |
|
| 105 | - |
|
| 106 | - // Get the cached post for the specified URI. |
|
| 107 | - $cache = $this->cache_service->get_cache( $uri ); |
|
| 108 | - |
|
| 109 | - // Return the cached data if valid. |
|
| 110 | - if ( false !== $cache && is_numeric( $cache ) ) { |
|
| 111 | - $this->log->debug( "Cached entity $cache for uri $uri found." ); |
|
| 112 | - |
|
| 113 | - return get_post( $cache ); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - // Get the actual result. |
|
| 117 | - $post = parent::get_entity( $uri ); |
|
| 118 | - |
|
| 119 | - // Cache the result, if a post is not found we cache -1. |
|
| 120 | - $this->set_cache( $uri, $post ); |
|
| 121 | - |
|
| 122 | - // Return the result. |
|
| 123 | - return $post; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Set the cached URI for the specified {@link WP_Post}. |
|
| 128 | - * |
|
| 129 | - * @since 3.16.3 |
|
| 130 | - * |
|
| 131 | - * @param string $uri The URI. |
|
| 132 | - * @param WP_Post $post The post. |
|
| 133 | - */ |
|
| 134 | - private function set_cache( $uri, $post ) { |
|
| 135 | - |
|
| 136 | - // Cache the result, if a post is not found we cache -1. |
|
| 137 | - $this->cache_service->set_cache( $uri, ( null === $post ? - 1 : $post->ID ) ); |
|
| 138 | - |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Delete the cache for the specified URIs. |
|
| 143 | - * |
|
| 144 | - * @since 3.16.3 |
|
| 145 | - * |
|
| 146 | - * @param array $uris An array of URIs. |
|
| 147 | - */ |
|
| 148 | - private function delete_cache( $uris ) { |
|
| 149 | - |
|
| 150 | - // Delete the cache for each URI. |
|
| 151 | - foreach ( $uris as $uri ) { |
|
| 152 | - // Delete the single cache file. |
|
| 153 | - $this->cache_service->delete_cache( $uri ); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Before post meta change. |
|
| 160 | - * |
|
| 161 | - * When a post meta is going to be changed, we check if the `meta_key` is |
|
| 162 | - * either the `entity_url` or the `same_as` in which case we delete the cache |
|
| 163 | - * for all the associated URIs. |
|
| 164 | - * |
|
| 165 | - * @since 3.16.3 |
|
| 166 | - * |
|
| 167 | - * @param int|array $meta_ids The {@link WP_Post} meta id(s). |
|
| 168 | - * @param int $post_id The {@link WP_Post} id. |
|
| 169 | - * @param string $meta_key The meta key. |
|
| 170 | - * @param mixed $meta_value The meta value(s). |
|
| 171 | - */ |
|
| 172 | - public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) { |
|
| 173 | - |
|
| 174 | - // Bail out if we're not interested in the meta key. |
|
| 175 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 176 | - return; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 180 | - |
|
| 181 | - // The list of existing URIs, plus the list of URIs being deleted/updated. |
|
| 182 | - $uris = get_post_meta( $post_id, $meta_key ) + (array) $meta_value; |
|
| 183 | - |
|
| 184 | - // Delete the cache for those URIs. |
|
| 185 | - $this->delete_cache( $uris ); |
|
| 186 | - |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Hook to meta add for a {@link WP_Post}, will cause the cache to |
|
| 191 | - * invalidate. |
|
| 192 | - * |
|
| 193 | - * @since 3.16.3 |
|
| 194 | - * |
|
| 195 | - * @param int $post_id The {@link WP_Post} id. |
|
| 196 | - * @param string $meta_key The meta key. |
|
| 197 | - * @param mixed $meta_value The meta value(s). |
|
| 198 | - */ |
|
| 199 | - public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) { |
|
| 200 | - |
|
| 201 | - // Bail out if we're not interested in the meta key. |
|
| 202 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 203 | - return; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - $this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 207 | - |
|
| 208 | - // Delete the cache for the URIs being added. |
|
| 209 | - $this->delete_cache( (array) $meta_value ); |
|
| 210 | - |
|
| 211 | - } |
|
| 17 | + /** |
|
| 18 | + * The {@link Wordlift_Cache_Service} instance. |
|
| 19 | + * |
|
| 20 | + * @since 3.16.3 |
|
| 21 | + * @access private |
|
| 22 | + * @var \Wordlift_Cache_Service $cache_service The {@link Wordlift_Cache_Service} instance. |
|
| 23 | + */ |
|
| 24 | + private $cache_service; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * A {@link Wordlift_Log_Service} instance. |
|
| 28 | + * |
|
| 29 | + * @since 3.16.3 |
|
| 30 | + * @access private |
|
| 31 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 32 | + */ |
|
| 33 | + private $log; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Create a {@link Wordlift_Cached_Entity_Uri_Service} instance. |
|
| 37 | + * |
|
| 38 | + * @since 3.16.3 |
|
| 39 | + * |
|
| 40 | + * @param \Wordlift_Configuration_Service $configuration_service |
|
| 41 | + * @param \Wordlift_Cache_Service $cache_service |
|
| 42 | + */ |
|
| 43 | + public function __construct( $configuration_service, $cache_service ) { |
|
| 44 | + parent::__construct( $configuration_service ); |
|
| 45 | + |
|
| 46 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 47 | + |
|
| 48 | + // Add hooks for meta being added/modified/deleted. |
|
| 49 | + $this->cache_service = $cache_service; |
|
| 50 | + |
|
| 51 | + add_action( 'add_post_meta', array( |
|
| 52 | + $this, |
|
| 53 | + 'on_before_post_meta_add', |
|
| 54 | + ), 10, 3 ); |
|
| 55 | + add_action( 'update_post_meta', array( |
|
| 56 | + $this, |
|
| 57 | + 'on_before_post_meta_change', |
|
| 58 | + ), 10, 4 ); |
|
| 59 | + add_action( 'delete_post_meta', array( |
|
| 60 | + $this, |
|
| 61 | + 'on_before_post_meta_change', |
|
| 62 | + ), 10, 4 ); |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Preload the URIs. |
|
| 68 | + * |
|
| 69 | + * @since 3.16.3 |
|
| 70 | + * |
|
| 71 | + * @param array $uris Preload an array of URIs. |
|
| 72 | + */ |
|
| 73 | + public function preload_uris( $uris ) { |
|
| 74 | + |
|
| 75 | + // Filter the URIs which aren't yet cached. |
|
| 76 | + $cache_service = $this->cache_service; |
|
| 77 | + $uris_to_cache = array_filter( (array) $uris, function ( $item ) use ( $cache_service ) { |
|
| 78 | + return ! $cache_service->has_cache( $item ); |
|
| 79 | + } ); |
|
| 80 | + |
|
| 81 | + // Preload the URIs. |
|
| 82 | + parent::preload_uris( $uris_to_cache ); |
|
| 83 | + |
|
| 84 | + // Store them in cache. |
|
| 85 | + if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) { |
|
| 86 | + foreach ( $this->uri_to_post as $uri => $post ) { |
|
| 87 | + $this->set_cache( $uri, $post ); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Get the entity post for the specified URI. |
|
| 95 | + * |
|
| 96 | + * @since 3.16.3 |
|
| 97 | + * |
|
| 98 | + * @param string $uri The URI. |
|
| 99 | + * |
|
| 100 | + * @return null|WP_Post The {@link WP_Post} or null if not found. |
|
| 101 | + */ |
|
| 102 | + public function get_entity( $uri ) { |
|
| 103 | + |
|
| 104 | + $this->log->trace( "Getting entity for uri $uri..." ); |
|
| 105 | + |
|
| 106 | + // Get the cached post for the specified URI. |
|
| 107 | + $cache = $this->cache_service->get_cache( $uri ); |
|
| 108 | + |
|
| 109 | + // Return the cached data if valid. |
|
| 110 | + if ( false !== $cache && is_numeric( $cache ) ) { |
|
| 111 | + $this->log->debug( "Cached entity $cache for uri $uri found." ); |
|
| 112 | + |
|
| 113 | + return get_post( $cache ); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + // Get the actual result. |
|
| 117 | + $post = parent::get_entity( $uri ); |
|
| 118 | + |
|
| 119 | + // Cache the result, if a post is not found we cache -1. |
|
| 120 | + $this->set_cache( $uri, $post ); |
|
| 121 | + |
|
| 122 | + // Return the result. |
|
| 123 | + return $post; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Set the cached URI for the specified {@link WP_Post}. |
|
| 128 | + * |
|
| 129 | + * @since 3.16.3 |
|
| 130 | + * |
|
| 131 | + * @param string $uri The URI. |
|
| 132 | + * @param WP_Post $post The post. |
|
| 133 | + */ |
|
| 134 | + private function set_cache( $uri, $post ) { |
|
| 135 | + |
|
| 136 | + // Cache the result, if a post is not found we cache -1. |
|
| 137 | + $this->cache_service->set_cache( $uri, ( null === $post ? - 1 : $post->ID ) ); |
|
| 138 | + |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Delete the cache for the specified URIs. |
|
| 143 | + * |
|
| 144 | + * @since 3.16.3 |
|
| 145 | + * |
|
| 146 | + * @param array $uris An array of URIs. |
|
| 147 | + */ |
|
| 148 | + private function delete_cache( $uris ) { |
|
| 149 | + |
|
| 150 | + // Delete the cache for each URI. |
|
| 151 | + foreach ( $uris as $uri ) { |
|
| 152 | + // Delete the single cache file. |
|
| 153 | + $this->cache_service->delete_cache( $uri ); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Before post meta change. |
|
| 160 | + * |
|
| 161 | + * When a post meta is going to be changed, we check if the `meta_key` is |
|
| 162 | + * either the `entity_url` or the `same_as` in which case we delete the cache |
|
| 163 | + * for all the associated URIs. |
|
| 164 | + * |
|
| 165 | + * @since 3.16.3 |
|
| 166 | + * |
|
| 167 | + * @param int|array $meta_ids The {@link WP_Post} meta id(s). |
|
| 168 | + * @param int $post_id The {@link WP_Post} id. |
|
| 169 | + * @param string $meta_key The meta key. |
|
| 170 | + * @param mixed $meta_value The meta value(s). |
|
| 171 | + */ |
|
| 172 | + public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) { |
|
| 173 | + |
|
| 174 | + // Bail out if we're not interested in the meta key. |
|
| 175 | + if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 176 | + return; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 180 | + |
|
| 181 | + // The list of existing URIs, plus the list of URIs being deleted/updated. |
|
| 182 | + $uris = get_post_meta( $post_id, $meta_key ) + (array) $meta_value; |
|
| 183 | + |
|
| 184 | + // Delete the cache for those URIs. |
|
| 185 | + $this->delete_cache( $uris ); |
|
| 186 | + |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Hook to meta add for a {@link WP_Post}, will cause the cache to |
|
| 191 | + * invalidate. |
|
| 192 | + * |
|
| 193 | + * @since 3.16.3 |
|
| 194 | + * |
|
| 195 | + * @param int $post_id The {@link WP_Post} id. |
|
| 196 | + * @param string $meta_key The meta key. |
|
| 197 | + * @param mixed $meta_value The meta value(s). |
|
| 198 | + */ |
|
| 199 | + public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) { |
|
| 200 | + |
|
| 201 | + // Bail out if we're not interested in the meta key. |
|
| 202 | + if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 203 | + return; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + $this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 207 | + |
|
| 208 | + // Delete the cache for the URIs being added. |
|
| 209 | + $this->delete_cache( (array) $meta_value ); |
|
| 210 | + |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | 213 | } |
| 214 | 214 | \ No newline at end of file |
@@ -40,26 +40,26 @@ discard block |
||
| 40 | 40 | * @param \Wordlift_Configuration_Service $configuration_service |
| 41 | 41 | * @param \Wordlift_Cache_Service $cache_service |
| 42 | 42 | */ |
| 43 | - public function __construct( $configuration_service, $cache_service ) { |
|
| 44 | - parent::__construct( $configuration_service ); |
|
| 43 | + public function __construct($configuration_service, $cache_service) { |
|
| 44 | + parent::__construct($configuration_service); |
|
| 45 | 45 | |
| 46 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 46 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 47 | 47 | |
| 48 | 48 | // Add hooks for meta being added/modified/deleted. |
| 49 | 49 | $this->cache_service = $cache_service; |
| 50 | 50 | |
| 51 | - add_action( 'add_post_meta', array( |
|
| 51 | + add_action('add_post_meta', array( |
|
| 52 | 52 | $this, |
| 53 | 53 | 'on_before_post_meta_add', |
| 54 | - ), 10, 3 ); |
|
| 55 | - add_action( 'update_post_meta', array( |
|
| 54 | + ), 10, 3); |
|
| 55 | + add_action('update_post_meta', array( |
|
| 56 | 56 | $this, |
| 57 | 57 | 'on_before_post_meta_change', |
| 58 | - ), 10, 4 ); |
|
| 59 | - add_action( 'delete_post_meta', array( |
|
| 58 | + ), 10, 4); |
|
| 59 | + add_action('delete_post_meta', array( |
|
| 60 | 60 | $this, |
| 61 | 61 | 'on_before_post_meta_change', |
| 62 | - ), 10, 4 ); |
|
| 62 | + ), 10, 4); |
|
| 63 | 63 | |
| 64 | 64 | } |
| 65 | 65 | |
@@ -70,21 +70,21 @@ discard block |
||
| 70 | 70 | * |
| 71 | 71 | * @param array $uris Preload an array of URIs. |
| 72 | 72 | */ |
| 73 | - public function preload_uris( $uris ) { |
|
| 73 | + public function preload_uris($uris) { |
|
| 74 | 74 | |
| 75 | 75 | // Filter the URIs which aren't yet cached. |
| 76 | 76 | $cache_service = $this->cache_service; |
| 77 | - $uris_to_cache = array_filter( (array) $uris, function ( $item ) use ( $cache_service ) { |
|
| 78 | - return ! $cache_service->has_cache( $item ); |
|
| 77 | + $uris_to_cache = array_filter((array) $uris, function($item) use ($cache_service) { |
|
| 78 | + return ! $cache_service->has_cache($item); |
|
| 79 | 79 | } ); |
| 80 | 80 | |
| 81 | 81 | // Preload the URIs. |
| 82 | - parent::preload_uris( $uris_to_cache ); |
|
| 82 | + parent::preload_uris($uris_to_cache); |
|
| 83 | 83 | |
| 84 | 84 | // Store them in cache. |
| 85 | - if ( is_array( $this->uri_to_post ) && ! empty( $this->uri_to_post ) ) { |
|
| 86 | - foreach ( $this->uri_to_post as $uri => $post ) { |
|
| 87 | - $this->set_cache( $uri, $post ); |
|
| 85 | + if (is_array($this->uri_to_post) && ! empty($this->uri_to_post)) { |
|
| 86 | + foreach ($this->uri_to_post as $uri => $post) { |
|
| 87 | + $this->set_cache($uri, $post); |
|
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | |
@@ -99,25 +99,25 @@ discard block |
||
| 99 | 99 | * |
| 100 | 100 | * @return null|WP_Post The {@link WP_Post} or null if not found. |
| 101 | 101 | */ |
| 102 | - public function get_entity( $uri ) { |
|
| 102 | + public function get_entity($uri) { |
|
| 103 | 103 | |
| 104 | - $this->log->trace( "Getting entity for uri $uri..." ); |
|
| 104 | + $this->log->trace("Getting entity for uri $uri..."); |
|
| 105 | 105 | |
| 106 | 106 | // Get the cached post for the specified URI. |
| 107 | - $cache = $this->cache_service->get_cache( $uri ); |
|
| 107 | + $cache = $this->cache_service->get_cache($uri); |
|
| 108 | 108 | |
| 109 | 109 | // Return the cached data if valid. |
| 110 | - if ( false !== $cache && is_numeric( $cache ) ) { |
|
| 111 | - $this->log->debug( "Cached entity $cache for uri $uri found." ); |
|
| 110 | + if (false !== $cache && is_numeric($cache)) { |
|
| 111 | + $this->log->debug("Cached entity $cache for uri $uri found."); |
|
| 112 | 112 | |
| 113 | - return get_post( $cache ); |
|
| 113 | + return get_post($cache); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | // Get the actual result. |
| 117 | - $post = parent::get_entity( $uri ); |
|
| 117 | + $post = parent::get_entity($uri); |
|
| 118 | 118 | |
| 119 | 119 | // Cache the result, if a post is not found we cache -1. |
| 120 | - $this->set_cache( $uri, $post ); |
|
| 120 | + $this->set_cache($uri, $post); |
|
| 121 | 121 | |
| 122 | 122 | // Return the result. |
| 123 | 123 | return $post; |
@@ -131,10 +131,10 @@ discard block |
||
| 131 | 131 | * @param string $uri The URI. |
| 132 | 132 | * @param WP_Post $post The post. |
| 133 | 133 | */ |
| 134 | - private function set_cache( $uri, $post ) { |
|
| 134 | + private function set_cache($uri, $post) { |
|
| 135 | 135 | |
| 136 | 136 | // Cache the result, if a post is not found we cache -1. |
| 137 | - $this->cache_service->set_cache( $uri, ( null === $post ? - 1 : $post->ID ) ); |
|
| 137 | + $this->cache_service->set_cache($uri, (null === $post ? -1 : $post->ID)); |
|
| 138 | 138 | |
| 139 | 139 | } |
| 140 | 140 | |
@@ -145,12 +145,12 @@ discard block |
||
| 145 | 145 | * |
| 146 | 146 | * @param array $uris An array of URIs. |
| 147 | 147 | */ |
| 148 | - private function delete_cache( $uris ) { |
|
| 148 | + private function delete_cache($uris) { |
|
| 149 | 149 | |
| 150 | 150 | // Delete the cache for each URI. |
| 151 | - foreach ( $uris as $uri ) { |
|
| 151 | + foreach ($uris as $uri) { |
|
| 152 | 152 | // Delete the single cache file. |
| 153 | - $this->cache_service->delete_cache( $uri ); |
|
| 153 | + $this->cache_service->delete_cache($uri); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | } |
@@ -169,20 +169,20 @@ discard block |
||
| 169 | 169 | * @param string $meta_key The meta key. |
| 170 | 170 | * @param mixed $meta_value The meta value(s). |
| 171 | 171 | */ |
| 172 | - public function on_before_post_meta_change( $meta_ids, $post_id, $meta_key, $meta_value ) { |
|
| 172 | + public function on_before_post_meta_change($meta_ids, $post_id, $meta_key, $meta_value) { |
|
| 173 | 173 | |
| 174 | 174 | // Bail out if we're not interested in the meta key. |
| 175 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 175 | + if (WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key) { |
|
| 176 | 176 | return; |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - $this->log->trace( "Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 179 | + $this->log->trace("Updating/deleting $meta_key for post $post_id ($meta_key), invalidating cache..."); |
|
| 180 | 180 | |
| 181 | 181 | // The list of existing URIs, plus the list of URIs being deleted/updated. |
| 182 | - $uris = get_post_meta( $post_id, $meta_key ) + (array) $meta_value; |
|
| 182 | + $uris = get_post_meta($post_id, $meta_key) + (array) $meta_value; |
|
| 183 | 183 | |
| 184 | 184 | // Delete the cache for those URIs. |
| 185 | - $this->delete_cache( $uris ); |
|
| 185 | + $this->delete_cache($uris); |
|
| 186 | 186 | |
| 187 | 187 | } |
| 188 | 188 | |
@@ -196,17 +196,17 @@ discard block |
||
| 196 | 196 | * @param string $meta_key The meta key. |
| 197 | 197 | * @param mixed $meta_value The meta value(s). |
| 198 | 198 | */ |
| 199 | - public function on_before_post_meta_add( $post_id, $meta_key, $meta_value ) { |
|
| 199 | + public function on_before_post_meta_add($post_id, $meta_key, $meta_value) { |
|
| 200 | 200 | |
| 201 | 201 | // Bail out if we're not interested in the meta key. |
| 202 | - if ( WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key ) { |
|
| 202 | + if (WL_ENTITY_URL_META_NAME !== $meta_key && Wordlift_Schema_Service::FIELD_SAME_AS !== $meta_key) { |
|
| 203 | 203 | return; |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - $this->log->trace( "Adding $meta_key for post $post_id ($meta_key), invalidating cache..." ); |
|
| 206 | + $this->log->trace("Adding $meta_key for post $post_id ($meta_key), invalidating cache..."); |
|
| 207 | 207 | |
| 208 | 208 | // Delete the cache for the URIs being added. |
| 209 | - $this->delete_cache( (array) $meta_value ); |
|
| 209 | + $this->delete_cache((array) $meta_value); |
|
| 210 | 210 | |
| 211 | 211 | } |
| 212 | 212 | |