@@ -7,467 +7,467 @@ 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_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 126 | - |
|
| 127 | - if ( is_wp_error( $terms ) ) { |
|
| 128 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 129 | - |
|
| 130 | - return false; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - if ( empty( $terms ) ) { |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - // We don't consider an `article` to be an entity. |
|
| 138 | - if ( 'article' !== $terms[0]->slug ) { |
|
| 139 | - return true; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return false; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Get the proper classification scope for a given entity post |
|
| 147 | - * |
|
| 148 | - * @since 3.5.0 |
|
| 149 | - * |
|
| 150 | - * @param integer $post_id An entity post id. |
|
| 151 | - * |
|
| 152 | - * @param string $default The default classification scope, `what` if not |
|
| 153 | - * provided. |
|
| 154 | - * |
|
| 155 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 156 | - */ |
|
| 157 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 158 | - |
|
| 159 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 160 | - return $default; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // Retrieve the entity type |
|
| 164 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 165 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 166 | - // Retrieve classification boxes configuration |
|
| 167 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 168 | - foreach ( $classification_boxes as $cb ) { |
|
| 169 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 170 | - return $cb['id']; |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - return $default; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Check whether a {@link WP_Post} is used. |
|
| 179 | - * |
|
| 180 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 181 | - * |
|
| 182 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 183 | - */ |
|
| 184 | - public function is_used( $post_id ) { |
|
| 185 | - |
|
| 186 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 187 | - return null; |
|
| 188 | - } |
|
| 189 | - // Retrieve the post |
|
| 190 | - $entity = get_post( $post_id ); |
|
| 191 | - |
|
| 192 | - global $wpdb; |
|
| 193 | - // Retrieve Wordlift relation instances table name |
|
| 194 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 195 | - |
|
| 196 | - // Check is it's referenced / related to another post / entity |
|
| 197 | - $stmt = $wpdb->prepare( |
|
| 198 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 199 | - $entity->ID |
|
| 200 | - ); |
|
| 201 | - |
|
| 202 | - // Perform the query |
|
| 203 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 204 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 205 | - if ( 0 < $relation_instances ) { |
|
| 206 | - return true; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - // Check if the entity uri is used as meta_value |
|
| 210 | - $stmt = $wpdb->prepare( |
|
| 211 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 212 | - $entity->ID, |
|
| 213 | - wl_get_entity_uri( $entity->ID ) |
|
| 214 | - ); |
|
| 215 | - // Perform the query |
|
| 216 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 217 | - |
|
| 218 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 219 | - if ( 0 < $meta_instances ) { |
|
| 220 | - return true; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - // If we are here, it means the current entity is not used at the moment |
|
| 224 | - return false; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 229 | - * |
|
| 230 | - * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 231 | - * @since 3.2.0 |
|
| 232 | - * |
|
| 233 | - * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 234 | - * |
|
| 235 | - * @param string $uri The entity URI. |
|
| 236 | - * |
|
| 237 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 238 | - */ |
|
| 239 | - public function get_entity_post_by_uri( $uri ) { |
|
| 240 | - |
|
| 241 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 246 | - * we check that the post we're saving is the current post. |
|
| 247 | - * |
|
| 248 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 249 | - * |
|
| 250 | - * @since 3.2.0 |
|
| 251 | - * |
|
| 252 | - * @param int $post_id Post ID. |
|
| 253 | - * @param WP_Post $post Post object. |
|
| 254 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 255 | - */ |
|
| 256 | - public function save_post( $post_id, $post, $update ) { |
|
| 257 | - |
|
| 258 | - // Avoid doing anything if post is autosave or a revision. |
|
| 259 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 260 | - return; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - // We're setting the alternative label that have been provided via the UI |
|
| 264 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 265 | - // programmatically by some other function: we need to check therefore if |
|
| 266 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 267 | - // |
|
| 268 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 269 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 270 | - return; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - // Get the alt labels from the request (or empty array). |
|
| 274 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 275 | - |
|
| 276 | - // Set the alternative labels. |
|
| 277 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 278 | - |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Set the alternative labels. |
|
| 283 | - * |
|
| 284 | - * @since 3.2.0 |
|
| 285 | - * |
|
| 286 | - * @param int $post_id The post id. |
|
| 287 | - * @param array $alt_labels An array of labels. |
|
| 288 | - */ |
|
| 289 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 290 | - |
|
| 291 | - // Force $alt_labels to be an array |
|
| 292 | - if ( ! is_array( $alt_labels ) ) { |
|
| 293 | - $alt_labels = array( $alt_labels ); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 297 | - |
|
| 298 | - // Delete all the existing alternate labels. |
|
| 299 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 300 | - |
|
| 301 | - // Set the alternative labels. |
|
| 302 | - foreach ( $alt_labels as $alt_label ) { |
|
| 303 | - if ( ! empty( $alt_label ) ) { |
|
| 304 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Retrieve the alternate labels. |
|
| 312 | - * |
|
| 313 | - * @since 3.2.0 |
|
| 314 | - * |
|
| 315 | - * @param int $post_id Post id. |
|
| 316 | - * |
|
| 317 | - * @return mixed An array of alternative labels. |
|
| 318 | - */ |
|
| 319 | - public function get_alternative_labels( $post_id ) { |
|
| 320 | - |
|
| 321 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 326 | - * |
|
| 327 | - * @since 3.12.0 |
|
| 328 | - * |
|
| 329 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 330 | - * |
|
| 331 | - * @return array An array with the entity title and labels. |
|
| 332 | - */ |
|
| 333 | - public function get_labels( $post_id ) { |
|
| 334 | - |
|
| 335 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 340 | - * |
|
| 341 | - * @since 3.2.0 |
|
| 342 | - * |
|
| 343 | - * @param WP_Post $post Post object. |
|
| 344 | - */ |
|
| 345 | - public function edit_form_before_permalink( $post ) { |
|
| 346 | - |
|
| 347 | - // If it's not an entity, return. |
|
| 348 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 349 | - return; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - // Print the input template. |
|
| 353 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 354 | - |
|
| 355 | - // Print all the currently set alternative labels. |
|
| 356 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 357 | - |
|
| 358 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 359 | - |
|
| 360 | - }; |
|
| 361 | - |
|
| 362 | - // Print the button. |
|
| 363 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 364 | - |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Get the URI for the entity with the specified post id. |
|
| 369 | - * |
|
| 370 | - * @since 3.6.0 |
|
| 371 | - * |
|
| 372 | - * @param int $post_id The entity post id. |
|
| 373 | - * |
|
| 374 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 375 | - */ |
|
| 376 | - public function get_uri( $post_id ) { |
|
| 377 | - |
|
| 378 | - // If a null is given, nothing to do |
|
| 379 | - if ( null == $post_id ) { |
|
| 380 | - return null; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 384 | - |
|
| 385 | - // If the dataset uri is not properly configured, null is returned |
|
| 386 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 387 | - return null; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - // Set the URI if it isn't set yet. |
|
| 391 | - $post_status = get_post_status( $post_id ); |
|
| 392 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 393 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 394 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - return $uri; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * Get the alternative label input HTML code. |
|
| 403 | - * |
|
| 404 | - * @since 3.2.0 |
|
| 405 | - * |
|
| 406 | - * @param string $value The input value. |
|
| 407 | - * |
|
| 408 | - * @return string The input HTML code. |
|
| 409 | - */ |
|
| 410 | - private function get_alternative_label_input( $value = '' ) { |
|
| 411 | - |
|
| 412 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * Get the number of entity posts published in this blog. |
|
| 417 | - * |
|
| 418 | - * @since 3.6.0 |
|
| 419 | - * |
|
| 420 | - * @return int The number of published entity posts. |
|
| 421 | - */ |
|
| 422 | - public function count() { |
|
| 423 | - |
|
| 424 | - $posts = get_posts( $this->add_criterias( array( |
|
| 425 | - 'post_status' => 'any', |
|
| 426 | - 'numberposts' => - 1, |
|
| 427 | - ) ) ); |
|
| 428 | - |
|
| 429 | - return count( $posts ); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 434 | - * call. |
|
| 435 | - * |
|
| 436 | - * @since 3.15.0 |
|
| 437 | - * |
|
| 438 | - * @param array $args The arguments for a `get_posts` call. |
|
| 439 | - * |
|
| 440 | - * @return array The arguments for a `get_posts` call. |
|
| 441 | - */ |
|
| 442 | - public static function add_criterias( $args ) { |
|
| 443 | - |
|
| 444 | - // Build an optimal tax-query. |
|
| 445 | - $tax_query = array( |
|
| 446 | - 'relation' => 'AND', |
|
| 447 | - array( |
|
| 448 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 449 | - 'operator' => 'EXISTS', |
|
| 450 | - ), |
|
| 451 | - array( |
|
| 452 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 453 | - 'field' => 'slug', |
|
| 454 | - 'terms' => 'article', |
|
| 455 | - 'operator' => 'NOT IN', |
|
| 456 | - ), |
|
| 457 | - ); |
|
| 458 | - |
|
| 459 | - return $args + array( |
|
| 460 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 461 | - // Since 3.17.0: should this be faster? |
|
| 462 | - 'tax_query' => $tax_query, |
|
| 463 | - // 'tax_query' => array( |
|
| 464 | - // array( |
|
| 465 | - // 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 466 | - // 'terms' => self::get_entity_terms(), |
|
| 467 | - // ), |
|
| 468 | - // ), |
|
| 469 | - ); |
|
| 470 | - } |
|
| 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_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 126 | + |
|
| 127 | + if ( is_wp_error( $terms ) ) { |
|
| 128 | + $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 129 | + |
|
| 130 | + return false; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + if ( empty( $terms ) ) { |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + // We don't consider an `article` to be an entity. |
|
| 138 | + if ( 'article' !== $terms[0]->slug ) { |
|
| 139 | + return true; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return false; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Get the proper classification scope for a given entity post |
|
| 147 | + * |
|
| 148 | + * @since 3.5.0 |
|
| 149 | + * |
|
| 150 | + * @param integer $post_id An entity post id. |
|
| 151 | + * |
|
| 152 | + * @param string $default The default classification scope, `what` if not |
|
| 153 | + * provided. |
|
| 154 | + * |
|
| 155 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 156 | + */ |
|
| 157 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 158 | + |
|
| 159 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 160 | + return $default; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // Retrieve the entity type |
|
| 164 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 165 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 166 | + // Retrieve classification boxes configuration |
|
| 167 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 168 | + foreach ( $classification_boxes as $cb ) { |
|
| 169 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 170 | + return $cb['id']; |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + return $default; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Check whether a {@link WP_Post} is used. |
|
| 179 | + * |
|
| 180 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 181 | + * |
|
| 182 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 183 | + */ |
|
| 184 | + public function is_used( $post_id ) { |
|
| 185 | + |
|
| 186 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 187 | + return null; |
|
| 188 | + } |
|
| 189 | + // Retrieve the post |
|
| 190 | + $entity = get_post( $post_id ); |
|
| 191 | + |
|
| 192 | + global $wpdb; |
|
| 193 | + // Retrieve Wordlift relation instances table name |
|
| 194 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 195 | + |
|
| 196 | + // Check is it's referenced / related to another post / entity |
|
| 197 | + $stmt = $wpdb->prepare( |
|
| 198 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 199 | + $entity->ID |
|
| 200 | + ); |
|
| 201 | + |
|
| 202 | + // Perform the query |
|
| 203 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 204 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 205 | + if ( 0 < $relation_instances ) { |
|
| 206 | + return true; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + // Check if the entity uri is used as meta_value |
|
| 210 | + $stmt = $wpdb->prepare( |
|
| 211 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 212 | + $entity->ID, |
|
| 213 | + wl_get_entity_uri( $entity->ID ) |
|
| 214 | + ); |
|
| 215 | + // Perform the query |
|
| 216 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 217 | + |
|
| 218 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 219 | + if ( 0 < $meta_instances ) { |
|
| 220 | + return true; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + // If we are here, it means the current entity is not used at the moment |
|
| 224 | + return false; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 229 | + * |
|
| 230 | + * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 231 | + * @since 3.2.0 |
|
| 232 | + * |
|
| 233 | + * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 234 | + * |
|
| 235 | + * @param string $uri The entity URI. |
|
| 236 | + * |
|
| 237 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 238 | + */ |
|
| 239 | + public function get_entity_post_by_uri( $uri ) { |
|
| 240 | + |
|
| 241 | + return $this->entity_uri_service->get_entity( $uri ); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 246 | + * we check that the post we're saving is the current post. |
|
| 247 | + * |
|
| 248 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 249 | + * |
|
| 250 | + * @since 3.2.0 |
|
| 251 | + * |
|
| 252 | + * @param int $post_id Post ID. |
|
| 253 | + * @param WP_Post $post Post object. |
|
| 254 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 255 | + */ |
|
| 256 | + public function save_post( $post_id, $post, $update ) { |
|
| 257 | + |
|
| 258 | + // Avoid doing anything if post is autosave or a revision. |
|
| 259 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 260 | + return; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + // We're setting the alternative label that have been provided via the UI |
|
| 264 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 265 | + // programmatically by some other function: we need to check therefore if |
|
| 266 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 267 | + // |
|
| 268 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 269 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 270 | + return; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + // Get the alt labels from the request (or empty array). |
|
| 274 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 275 | + |
|
| 276 | + // Set the alternative labels. |
|
| 277 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 278 | + |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Set the alternative labels. |
|
| 283 | + * |
|
| 284 | + * @since 3.2.0 |
|
| 285 | + * |
|
| 286 | + * @param int $post_id The post id. |
|
| 287 | + * @param array $alt_labels An array of labels. |
|
| 288 | + */ |
|
| 289 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 290 | + |
|
| 291 | + // Force $alt_labels to be an array |
|
| 292 | + if ( ! is_array( $alt_labels ) ) { |
|
| 293 | + $alt_labels = array( $alt_labels ); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 297 | + |
|
| 298 | + // Delete all the existing alternate labels. |
|
| 299 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 300 | + |
|
| 301 | + // Set the alternative labels. |
|
| 302 | + foreach ( $alt_labels as $alt_label ) { |
|
| 303 | + if ( ! empty( $alt_label ) ) { |
|
| 304 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Retrieve the alternate labels. |
|
| 312 | + * |
|
| 313 | + * @since 3.2.0 |
|
| 314 | + * |
|
| 315 | + * @param int $post_id Post id. |
|
| 316 | + * |
|
| 317 | + * @return mixed An array of alternative labels. |
|
| 318 | + */ |
|
| 319 | + public function get_alternative_labels( $post_id ) { |
|
| 320 | + |
|
| 321 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 326 | + * |
|
| 327 | + * @since 3.12.0 |
|
| 328 | + * |
|
| 329 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 330 | + * |
|
| 331 | + * @return array An array with the entity title and labels. |
|
| 332 | + */ |
|
| 333 | + public function get_labels( $post_id ) { |
|
| 334 | + |
|
| 335 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 340 | + * |
|
| 341 | + * @since 3.2.0 |
|
| 342 | + * |
|
| 343 | + * @param WP_Post $post Post object. |
|
| 344 | + */ |
|
| 345 | + public function edit_form_before_permalink( $post ) { |
|
| 346 | + |
|
| 347 | + // If it's not an entity, return. |
|
| 348 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 349 | + return; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + // Print the input template. |
|
| 353 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 354 | + |
|
| 355 | + // Print all the currently set alternative labels. |
|
| 356 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 357 | + |
|
| 358 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 359 | + |
|
| 360 | + }; |
|
| 361 | + |
|
| 362 | + // Print the button. |
|
| 363 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 364 | + |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Get the URI for the entity with the specified post id. |
|
| 369 | + * |
|
| 370 | + * @since 3.6.0 |
|
| 371 | + * |
|
| 372 | + * @param int $post_id The entity post id. |
|
| 373 | + * |
|
| 374 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 375 | + */ |
|
| 376 | + public function get_uri( $post_id ) { |
|
| 377 | + |
|
| 378 | + // If a null is given, nothing to do |
|
| 379 | + if ( null == $post_id ) { |
|
| 380 | + return null; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 384 | + |
|
| 385 | + // If the dataset uri is not properly configured, null is returned |
|
| 386 | + if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 387 | + return null; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + // Set the URI if it isn't set yet. |
|
| 391 | + $post_status = get_post_status( $post_id ); |
|
| 392 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 393 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 394 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + return $uri; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * Get the alternative label input HTML code. |
|
| 403 | + * |
|
| 404 | + * @since 3.2.0 |
|
| 405 | + * |
|
| 406 | + * @param string $value The input value. |
|
| 407 | + * |
|
| 408 | + * @return string The input HTML code. |
|
| 409 | + */ |
|
| 410 | + private function get_alternative_label_input( $value = '' ) { |
|
| 411 | + |
|
| 412 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * Get the number of entity posts published in this blog. |
|
| 417 | + * |
|
| 418 | + * @since 3.6.0 |
|
| 419 | + * |
|
| 420 | + * @return int The number of published entity posts. |
|
| 421 | + */ |
|
| 422 | + public function count() { |
|
| 423 | + |
|
| 424 | + $posts = get_posts( $this->add_criterias( array( |
|
| 425 | + 'post_status' => 'any', |
|
| 426 | + 'numberposts' => - 1, |
|
| 427 | + ) ) ); |
|
| 428 | + |
|
| 429 | + return count( $posts ); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 434 | + * call. |
|
| 435 | + * |
|
| 436 | + * @since 3.15.0 |
|
| 437 | + * |
|
| 438 | + * @param array $args The arguments for a `get_posts` call. |
|
| 439 | + * |
|
| 440 | + * @return array The arguments for a `get_posts` call. |
|
| 441 | + */ |
|
| 442 | + public static function add_criterias( $args ) { |
|
| 443 | + |
|
| 444 | + // Build an optimal tax-query. |
|
| 445 | + $tax_query = array( |
|
| 446 | + 'relation' => 'AND', |
|
| 447 | + array( |
|
| 448 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 449 | + 'operator' => 'EXISTS', |
|
| 450 | + ), |
|
| 451 | + array( |
|
| 452 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 453 | + 'field' => 'slug', |
|
| 454 | + 'terms' => 'article', |
|
| 455 | + 'operator' => 'NOT IN', |
|
| 456 | + ), |
|
| 457 | + ); |
|
| 458 | + |
|
| 459 | + return $args + array( |
|
| 460 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 461 | + // Since 3.17.0: should this be faster? |
|
| 462 | + 'tax_query' => $tax_query, |
|
| 463 | + // 'tax_query' => array( |
|
| 464 | + // array( |
|
| 465 | + // 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 466 | + // 'terms' => self::get_entity_terms(), |
|
| 467 | + // ), |
|
| 468 | + // ), |
|
| 469 | + ); |
|
| 470 | + } |
|
| 471 | 471 | |
| 472 | 472 | // /** |
| 473 | 473 | // * Get the entity terms IDs which represent an entity. |
@@ -497,97 +497,97 @@ discard block |
||
| 497 | 497 | // } ) ); |
| 498 | 498 | // } |
| 499 | 499 | |
| 500 | - /** |
|
| 501 | - * Create a new entity. |
|
| 502 | - * |
|
| 503 | - * @since 3.9.0 |
|
| 504 | - * |
|
| 505 | - * @param string $name The entity name. |
|
| 506 | - * @param string $type_uri The entity's type URI. |
|
| 507 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 508 | - * @param string $status The post status, by default 'publish'. |
|
| 509 | - * |
|
| 510 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 511 | - */ |
|
| 512 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 513 | - |
|
| 514 | - // Create an entity for the publisher. |
|
| 515 | - $post_id = wp_insert_post( array( |
|
| 516 | - 'post_type' => self::TYPE_NAME, |
|
| 517 | - 'post_title' => $name, |
|
| 518 | - 'post_status' => $status, |
|
| 519 | - 'post_content' => '', |
|
| 520 | - ) ); |
|
| 521 | - |
|
| 522 | - // Return the error if any. |
|
| 523 | - if ( is_wp_error( $post_id ) ) { |
|
| 524 | - return $post_id; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - // Set the entity logo. |
|
| 528 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 529 | - set_post_thumbnail( $post_id, $logo ); |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - // Set the entity type. |
|
| 533 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 534 | - |
|
| 535 | - return $post_id; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Get the entities related to the one with the specified id. By default only |
|
| 540 | - * published entities will be returned. |
|
| 541 | - * |
|
| 542 | - * @since 3.10.0 |
|
| 543 | - * |
|
| 544 | - * @param int $id The post id. |
|
| 545 | - * @param string $post_status The target post status (default = publish). |
|
| 546 | - * |
|
| 547 | - * @return array An array of post ids. |
|
| 548 | - */ |
|
| 549 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 550 | - |
|
| 551 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * Get the list of entities. |
|
| 556 | - * |
|
| 557 | - * @since 3.12.2 |
|
| 558 | - * |
|
| 559 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 560 | - * |
|
| 561 | - * @return array An array of entity posts. |
|
| 562 | - */ |
|
| 563 | - public function get( $params = array() ) { |
|
| 564 | - |
|
| 565 | - // Set the defaults. |
|
| 566 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 567 | - |
|
| 568 | - // Merge the defaults with the provided parameters. |
|
| 569 | - $args = wp_parse_args( $params, $defaults ); |
|
| 570 | - |
|
| 571 | - // Call the `get_posts` function. |
|
| 572 | - return get_posts( $args ); |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * The list of post type names which can be used for entities |
|
| 577 | - * |
|
| 578 | - * Criteria is that the post type is public. The list of valid post types |
|
| 579 | - * can be overridden with a filter. |
|
| 580 | - * |
|
| 581 | - * @since 3.15.0 |
|
| 582 | - * |
|
| 583 | - * @return array Array containing the names of the valid post types. |
|
| 584 | - */ |
|
| 585 | - static function valid_entity_post_types() { |
|
| 586 | - |
|
| 587 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 588 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 589 | - |
|
| 590 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 591 | - } |
|
| 500 | + /** |
|
| 501 | + * Create a new entity. |
|
| 502 | + * |
|
| 503 | + * @since 3.9.0 |
|
| 504 | + * |
|
| 505 | + * @param string $name The entity name. |
|
| 506 | + * @param string $type_uri The entity's type URI. |
|
| 507 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 508 | + * @param string $status The post status, by default 'publish'. |
|
| 509 | + * |
|
| 510 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 511 | + */ |
|
| 512 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 513 | + |
|
| 514 | + // Create an entity for the publisher. |
|
| 515 | + $post_id = wp_insert_post( array( |
|
| 516 | + 'post_type' => self::TYPE_NAME, |
|
| 517 | + 'post_title' => $name, |
|
| 518 | + 'post_status' => $status, |
|
| 519 | + 'post_content' => '', |
|
| 520 | + ) ); |
|
| 521 | + |
|
| 522 | + // Return the error if any. |
|
| 523 | + if ( is_wp_error( $post_id ) ) { |
|
| 524 | + return $post_id; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + // Set the entity logo. |
|
| 528 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 529 | + set_post_thumbnail( $post_id, $logo ); |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + // Set the entity type. |
|
| 533 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 534 | + |
|
| 535 | + return $post_id; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Get the entities related to the one with the specified id. By default only |
|
| 540 | + * published entities will be returned. |
|
| 541 | + * |
|
| 542 | + * @since 3.10.0 |
|
| 543 | + * |
|
| 544 | + * @param int $id The post id. |
|
| 545 | + * @param string $post_status The target post status (default = publish). |
|
| 546 | + * |
|
| 547 | + * @return array An array of post ids. |
|
| 548 | + */ |
|
| 549 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 550 | + |
|
| 551 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Get the list of entities. |
|
| 556 | + * |
|
| 557 | + * @since 3.12.2 |
|
| 558 | + * |
|
| 559 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 560 | + * |
|
| 561 | + * @return array An array of entity posts. |
|
| 562 | + */ |
|
| 563 | + public function get( $params = array() ) { |
|
| 564 | + |
|
| 565 | + // Set the defaults. |
|
| 566 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 567 | + |
|
| 568 | + // Merge the defaults with the provided parameters. |
|
| 569 | + $args = wp_parse_args( $params, $defaults ); |
|
| 570 | + |
|
| 571 | + // Call the `get_posts` function. |
|
| 572 | + return get_posts( $args ); |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + /** |
|
| 576 | + * The list of post type names which can be used for entities |
|
| 577 | + * |
|
| 578 | + * Criteria is that the post type is public. The list of valid post types |
|
| 579 | + * can be overridden with a filter. |
|
| 580 | + * |
|
| 581 | + * @since 3.15.0 |
|
| 582 | + * |
|
| 583 | + * @return array Array containing the names of the valid post types. |
|
| 584 | + */ |
|
| 585 | + static function valid_entity_post_types() { |
|
| 586 | + |
|
| 587 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 588 | + $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 589 | + |
|
| 590 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 591 | + } |
|
| 592 | 592 | |
| 593 | 593 | } |
@@ -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,22 +120,22 @@ 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_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 125 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 126 | 126 | |
| 127 | - if ( is_wp_error( $terms ) ) { |
|
| 128 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 127 | + if (is_wp_error($terms)) { |
|
| 128 | + $this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message()); |
|
| 129 | 129 | |
| 130 | 130 | return false; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - if ( empty( $terms ) ) { |
|
| 133 | + if (empty($terms)) { |
|
| 134 | 134 | return false; |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | // We don't consider an `article` to be an entity. |
| 138 | - if ( 'article' !== $terms[0]->slug ) { |
|
| 138 | + if ('article' !== $terms[0]->slug) { |
|
| 139 | 139 | return true; |
| 140 | 140 | } |
| 141 | 141 | |
@@ -154,19 +154,19 @@ discard block |
||
| 154 | 154 | * |
| 155 | 155 | * @return string Returns a classification scope (e.g. 'what'). |
| 156 | 156 | */ |
| 157 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 157 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 158 | 158 | |
| 159 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 159 | + if (false === $this->is_entity($post_id)) { |
|
| 160 | 160 | return $default; |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | // Retrieve the entity type |
| 164 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 165 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 164 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 165 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 166 | 166 | // Retrieve classification boxes configuration |
| 167 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 168 | - foreach ( $classification_boxes as $cb ) { |
|
| 169 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 167 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 168 | + foreach ($classification_boxes as $cb) { |
|
| 169 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 170 | 170 | return $cb['id']; |
| 171 | 171 | } |
| 172 | 172 | } |
@@ -181,13 +181,13 @@ discard block |
||
| 181 | 181 | * |
| 182 | 182 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 183 | 183 | */ |
| 184 | - public function is_used( $post_id ) { |
|
| 184 | + public function is_used($post_id) { |
|
| 185 | 185 | |
| 186 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 186 | + if (false === $this->is_entity($post_id)) { |
|
| 187 | 187 | return null; |
| 188 | 188 | } |
| 189 | 189 | // Retrieve the post |
| 190 | - $entity = get_post( $post_id ); |
|
| 190 | + $entity = get_post($post_id); |
|
| 191 | 191 | |
| 192 | 192 | global $wpdb; |
| 193 | 193 | // Retrieve Wordlift relation instances table name |
@@ -200,9 +200,9 @@ discard block |
||
| 200 | 200 | ); |
| 201 | 201 | |
| 202 | 202 | // Perform the query |
| 203 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 203 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 204 | 204 | // If there is at least one relation instance for the current entity, then it's used |
| 205 | - if ( 0 < $relation_instances ) { |
|
| 205 | + if (0 < $relation_instances) { |
|
| 206 | 206 | return true; |
| 207 | 207 | } |
| 208 | 208 | |
@@ -210,13 +210,13 @@ discard block |
||
| 210 | 210 | $stmt = $wpdb->prepare( |
| 211 | 211 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 212 | 212 | $entity->ID, |
| 213 | - wl_get_entity_uri( $entity->ID ) |
|
| 213 | + wl_get_entity_uri($entity->ID) |
|
| 214 | 214 | ); |
| 215 | 215 | // Perform the query |
| 216 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 216 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 217 | 217 | |
| 218 | 218 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 219 | - if ( 0 < $meta_instances ) { |
|
| 219 | + if (0 < $meta_instances) { |
|
| 220 | 220 | return true; |
| 221 | 221 | } |
| 222 | 222 | |
@@ -236,9 +236,9 @@ discard block |
||
| 236 | 236 | * |
| 237 | 237 | * @return WP_Post|null A WP_Post instance or null if not found. |
| 238 | 238 | */ |
| 239 | - public function get_entity_post_by_uri( $uri ) { |
|
| 239 | + public function get_entity_post_by_uri($uri) { |
|
| 240 | 240 | |
| 241 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 241 | + return $this->entity_uri_service->get_entity($uri); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -253,10 +253,10 @@ discard block |
||
| 253 | 253 | * @param WP_Post $post Post object. |
| 254 | 254 | * @param bool $update Whether this is an existing post being updated or not. |
| 255 | 255 | */ |
| 256 | - public function save_post( $post_id, $post, $update ) { |
|
| 256 | + public function save_post($post_id, $post, $update) { |
|
| 257 | 257 | |
| 258 | 258 | // Avoid doing anything if post is autosave or a revision. |
| 259 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 259 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 260 | 260 | return; |
| 261 | 261 | } |
| 262 | 262 | |
@@ -266,15 +266,15 @@ discard block |
||
| 266 | 266 | // the $post_id in the save_post call matches the post id set in the request. |
| 267 | 267 | // |
| 268 | 268 | // If this is not the current post being saved or if it's not an entity, return. |
| 269 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 269 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 270 | 270 | return; |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | // Get the alt labels from the request (or empty array). |
| 274 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 274 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 275 | 275 | |
| 276 | 276 | // Set the alternative labels. |
| 277 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 277 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 278 | 278 | |
| 279 | 279 | } |
| 280 | 280 | |
@@ -286,22 +286,22 @@ discard block |
||
| 286 | 286 | * @param int $post_id The post id. |
| 287 | 287 | * @param array $alt_labels An array of labels. |
| 288 | 288 | */ |
| 289 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 289 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 290 | 290 | |
| 291 | 291 | // Force $alt_labels to be an array |
| 292 | - if ( ! is_array( $alt_labels ) ) { |
|
| 293 | - $alt_labels = array( $alt_labels ); |
|
| 292 | + if ( ! is_array($alt_labels)) { |
|
| 293 | + $alt_labels = array($alt_labels); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 296 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 297 | 297 | |
| 298 | 298 | // Delete all the existing alternate labels. |
| 299 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 299 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 300 | 300 | |
| 301 | 301 | // Set the alternative labels. |
| 302 | - foreach ( $alt_labels as $alt_label ) { |
|
| 303 | - if ( ! empty( $alt_label ) ) { |
|
| 304 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 302 | + foreach ($alt_labels as $alt_label) { |
|
| 303 | + if ( ! empty($alt_label)) { |
|
| 304 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 305 | 305 | } |
| 306 | 306 | } |
| 307 | 307 | |
@@ -316,9 +316,9 @@ discard block |
||
| 316 | 316 | * |
| 317 | 317 | * @return mixed An array of alternative labels. |
| 318 | 318 | */ |
| 319 | - public function get_alternative_labels( $post_id ) { |
|
| 319 | + public function get_alternative_labels($post_id) { |
|
| 320 | 320 | |
| 321 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 321 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | /** |
@@ -330,9 +330,9 @@ discard block |
||
| 330 | 330 | * |
| 331 | 331 | * @return array An array with the entity title and labels. |
| 332 | 332 | */ |
| 333 | - public function get_labels( $post_id ) { |
|
| 333 | + public function get_labels($post_id) { |
|
| 334 | 334 | |
| 335 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 335 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | /** |
@@ -342,25 +342,25 @@ discard block |
||
| 342 | 342 | * |
| 343 | 343 | * @param WP_Post $post Post object. |
| 344 | 344 | */ |
| 345 | - public function edit_form_before_permalink( $post ) { |
|
| 345 | + public function edit_form_before_permalink($post) { |
|
| 346 | 346 | |
| 347 | 347 | // If it's not an entity, return. |
| 348 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 348 | + if ( ! $this->is_entity($post->ID)) { |
|
| 349 | 349 | return; |
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | // Print the input template. |
| 353 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 353 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 354 | 354 | |
| 355 | 355 | // Print all the currently set alternative labels. |
| 356 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 356 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 357 | 357 | |
| 358 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 358 | + echo $this->get_alternative_label_input($alt_label); |
|
| 359 | 359 | |
| 360 | 360 | }; |
| 361 | 361 | |
| 362 | 362 | // Print the button. |
| 363 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 363 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 364 | 364 | |
| 365 | 365 | } |
| 366 | 366 | |
@@ -373,25 +373,25 @@ discard block |
||
| 373 | 373 | * |
| 374 | 374 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
| 375 | 375 | */ |
| 376 | - public function get_uri( $post_id ) { |
|
| 376 | + public function get_uri($post_id) { |
|
| 377 | 377 | |
| 378 | 378 | // If a null is given, nothing to do |
| 379 | - if ( null == $post_id ) { |
|
| 379 | + if (null == $post_id) { |
|
| 380 | 380 | return null; |
| 381 | 381 | } |
| 382 | 382 | |
| 383 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 383 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 384 | 384 | |
| 385 | 385 | // If the dataset uri is not properly configured, null is returned |
| 386 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 386 | + if ('' === wl_configuration_get_redlink_dataset_uri()) { |
|
| 387 | 387 | return null; |
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | // Set the URI if it isn't set yet. |
| 391 | - $post_status = get_post_status( $post_id ); |
|
| 392 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 393 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 394 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 391 | + $post_status = get_post_status($post_id); |
|
| 392 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 393 | + $uri = wl_build_entity_uri($post_id); |
|
| 394 | + wl_set_entity_uri($post_id, $uri); |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | return $uri; |
@@ -407,9 +407,9 @@ discard block |
||
| 407 | 407 | * |
| 408 | 408 | * @return string The input HTML code. |
| 409 | 409 | */ |
| 410 | - private function get_alternative_label_input( $value = '' ) { |
|
| 410 | + private function get_alternative_label_input($value = '') { |
|
| 411 | 411 | |
| 412 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 412 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | /** |
@@ -421,12 +421,12 @@ discard block |
||
| 421 | 421 | */ |
| 422 | 422 | public function count() { |
| 423 | 423 | |
| 424 | - $posts = get_posts( $this->add_criterias( array( |
|
| 424 | + $posts = get_posts($this->add_criterias(array( |
|
| 425 | 425 | 'post_status' => 'any', |
| 426 | - 'numberposts' => - 1, |
|
| 427 | - ) ) ); |
|
| 426 | + 'numberposts' => -1, |
|
| 427 | + ))); |
|
| 428 | 428 | |
| 429 | - return count( $posts ); |
|
| 429 | + return count($posts); |
|
| 430 | 430 | } |
| 431 | 431 | |
| 432 | 432 | /** |
@@ -439,7 +439,7 @@ discard block |
||
| 439 | 439 | * |
| 440 | 440 | * @return array The arguments for a `get_posts` call. |
| 441 | 441 | */ |
| 442 | - public static function add_criterias( $args ) { |
|
| 442 | + public static function add_criterias($args) { |
|
| 443 | 443 | |
| 444 | 444 | // Build an optimal tax-query. |
| 445 | 445 | $tax_query = array( |
@@ -509,28 +509,28 @@ discard block |
||
| 509 | 509 | * |
| 510 | 510 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
| 511 | 511 | */ |
| 512 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 512 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 513 | 513 | |
| 514 | 514 | // Create an entity for the publisher. |
| 515 | - $post_id = wp_insert_post( array( |
|
| 515 | + $post_id = wp_insert_post(array( |
|
| 516 | 516 | 'post_type' => self::TYPE_NAME, |
| 517 | 517 | 'post_title' => $name, |
| 518 | 518 | 'post_status' => $status, |
| 519 | 519 | 'post_content' => '', |
| 520 | - ) ); |
|
| 520 | + )); |
|
| 521 | 521 | |
| 522 | 522 | // Return the error if any. |
| 523 | - if ( is_wp_error( $post_id ) ) { |
|
| 523 | + if (is_wp_error($post_id)) { |
|
| 524 | 524 | return $post_id; |
| 525 | 525 | } |
| 526 | 526 | |
| 527 | 527 | // Set the entity logo. |
| 528 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 529 | - set_post_thumbnail( $post_id, $logo ); |
|
| 528 | + if ($logo && is_numeric($logo)) { |
|
| 529 | + set_post_thumbnail($post_id, $logo); |
|
| 530 | 530 | } |
| 531 | 531 | |
| 532 | 532 | // Set the entity type. |
| 533 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 533 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 534 | 534 | |
| 535 | 535 | return $post_id; |
| 536 | 536 | } |
@@ -546,9 +546,9 @@ discard block |
||
| 546 | 546 | * |
| 547 | 547 | * @return array An array of post ids. |
| 548 | 548 | */ |
| 549 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 549 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 550 | 550 | |
| 551 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 551 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | /** |
@@ -560,16 +560,16 @@ discard block |
||
| 560 | 560 | * |
| 561 | 561 | * @return array An array of entity posts. |
| 562 | 562 | */ |
| 563 | - public function get( $params = array() ) { |
|
| 563 | + public function get($params = array()) { |
|
| 564 | 564 | |
| 565 | 565 | // Set the defaults. |
| 566 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 566 | + $defaults = array('post_type' => 'entity'); |
|
| 567 | 567 | |
| 568 | 568 | // Merge the defaults with the provided parameters. |
| 569 | - $args = wp_parse_args( $params, $defaults ); |
|
| 569 | + $args = wp_parse_args($params, $defaults); |
|
| 570 | 570 | |
| 571 | 571 | // Call the `get_posts` function. |
| 572 | - return get_posts( $args ); |
|
| 572 | + return get_posts($args); |
|
| 573 | 573 | } |
| 574 | 574 | |
| 575 | 575 | /** |
@@ -585,9 +585,9 @@ discard block |
||
| 585 | 585 | static function valid_entity_post_types() { |
| 586 | 586 | |
| 587 | 587 | // Ignore builtins in the call to avoid getting attachments. |
| 588 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 588 | + $post_types = array('post', 'page', self::TYPE_NAME); |
|
| 589 | 589 | |
| 590 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 590 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 591 | 591 | } |
| 592 | 592 | |
| 593 | 593 | } |
@@ -13,209 +13,209 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Schema_Url_Property_Service extends Wordlift_Property_Service { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * The meta key used to store data for this property. We don't use wl_url to |
|
| 18 | - * avoid potential confusion about other URLs. |
|
| 19 | - * |
|
| 20 | - * @since 3.6.0 |
|
| 21 | - */ |
|
| 22 | - const META_KEY = 'wl_schema_url'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - public function get_rdf_predicate() { |
|
| 28 | - |
|
| 29 | - return 'http://schema.org/url'; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * {@inheritdoc} |
|
| 34 | - */ |
|
| 35 | - public function get_rdf_data_type() { |
|
| 36 | - |
|
| 37 | - return 'xsd:anyURI'; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * {@inheritdoc} |
|
| 42 | - */ |
|
| 43 | - public function get_data_type() { |
|
| 44 | - |
|
| 45 | - return Wordlift_Schema_Service::DATA_TYPE_URI; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * {@inheritdoc} |
|
| 50 | - */ |
|
| 51 | - public function get_cardinality() { |
|
| 52 | - |
|
| 53 | - return INF; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * {@inheritdoc} |
|
| 58 | - */ |
|
| 59 | - public function get_metabox_class() { |
|
| 60 | - |
|
| 61 | - return 'WL_Metabox_Field'; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * {@inheritdoc} |
|
| 66 | - */ |
|
| 67 | - public function get_metabox_label() { |
|
| 68 | - |
|
| 69 | - return __( 'Web Site(s)', 'wordlift' ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @var |
|
| 74 | - */ |
|
| 75 | - private $sparql_service; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Create a Wordlift_Schema_Url_Property_Service instance. |
|
| 79 | - * @since 3.6.0 |
|
| 80 | - * |
|
| 81 | - * @param Wordlift_Sparql_Service $sparql_service |
|
| 82 | - */ |
|
| 83 | - public function __construct( $sparql_service ) { |
|
| 84 | - parent::__construct(); |
|
| 85 | - |
|
| 86 | - // Finally listen for metadata requests for this field. |
|
| 87 | - $this->add_filter_get_post_metadata(); |
|
| 88 | - |
|
| 89 | - $this->sparql_service = $sparql_service; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Get the schema:url value for the specified post/entity. |
|
| 94 | - * |
|
| 95 | - * @since 3.6.0 |
|
| 96 | - * |
|
| 97 | - * @param int $post_id The post id. |
|
| 98 | - * |
|
| 99 | - * @return array|NULL The schema:url value or NULL if not set. |
|
| 100 | - */ |
|
| 101 | - public function get( $post_id ) { |
|
| 102 | - |
|
| 103 | - // Get the schema:url values set in WP. |
|
| 104 | - $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
| 105 | - |
|
| 106 | - // If the property has never been set, we set its default value the first |
|
| 107 | - // time to <permalink>. |
|
| 108 | - if ( empty( $values ) ) { |
|
| 109 | - return array( '<permalink>' ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // If there's only one value and that value is empty, we return NULL, i.e. |
|
| 113 | - // variable not set. |
|
| 114 | - if ( 1 === count( $values ) && empty( $values[0] ) ) { |
|
| 115 | - return null; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - // Finally return whatever values the editor set. |
|
| 119 | - return $values; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * {@inheritdoc} |
|
| 124 | - */ |
|
| 125 | - public function sanitize( $value ) { |
|
| 126 | - |
|
| 127 | - // TODO: check that it's an URL or that is <permalink> |
|
| 128 | - |
|
| 129 | - return $value; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Generate an insert query that inserts the schema:url values for the specified |
|
| 134 | - * post. |
|
| 135 | - * |
|
| 136 | - * @since 3.6.0 |
|
| 137 | - * |
|
| 138 | - * @param string $s The subject URI. |
|
| 139 | - * @param int $post_id The post id. |
|
| 140 | - * |
|
| 141 | - * @return string The insert query or an empty string. |
|
| 142 | - */ |
|
| 143 | - public function get_insert_query( $s, $post_id ) { |
|
| 144 | - |
|
| 145 | - // If we have no value, return an empty string (no query). |
|
| 146 | - if ( null === ( $values = $this->get( $post_id ) ) ) { |
|
| 147 | - return ''; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - // Create the insert query. |
|
| 151 | - $q = Wordlift_Query_Builder::new_instance()->insert(); |
|
| 152 | - |
|
| 153 | - // Add each schema:url, replacing <permalink> with the actual post permalink. |
|
| 154 | - foreach ( $values as $value ) { |
|
| 155 | - $q = $q->statement( $s, $this->get_rdf_predicate(), '<permalink>' === $value ? get_permalink( $post_id ) : $value, Wordlift_Query_Builder::OBJECT_URI ); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - // Build and return the query. |
|
| 159 | - return $q->build(); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Get direct calls to read this meta and alter the response according to our |
|
| 164 | - * own strategy, i.e. if a value has never been set for this meta, then return |
|
| 165 | - * <permalink>. |
|
| 166 | - * |
|
| 167 | - * @since 3.6.0 |
|
| 168 | - * |
|
| 169 | - * @param mixed $value The original value. |
|
| 170 | - * @param int $object_id The post id. |
|
| 171 | - * @param string $meta_key The meta key. We expect wl_schema_url or we return straight the value. |
|
| 172 | - * @param bool $single Whether to return a single value. |
|
| 173 | - * |
|
| 174 | - * @return array|mixed|NULL|string |
|
| 175 | - */ |
|
| 176 | - public function get_post_metadata( $value, $object_id, $meta_key, $single ) { |
|
| 177 | - |
|
| 178 | - // It's not us, return the value. |
|
| 179 | - if ( self::META_KEY !== $meta_key ) { |
|
| 180 | - return $value; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - $this->remove_filter_get_post_metadata(); |
|
| 184 | - |
|
| 185 | - $new_value = $this->get( $object_id ); |
|
| 186 | - |
|
| 187 | - $this->add_filter_get_post_metadata(); |
|
| 188 | - |
|
| 189 | - // If we must return a single value, but we don't have a value, then return an empty string. |
|
| 190 | - if ( $single && ( is_null( $new_value ) || empty( $new_value ) ) ) { |
|
| 191 | - return ''; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - // If we have a value and we need to return it as single, return the first value. |
|
| 195 | - if ( $single ) { |
|
| 196 | - return $new_value[0]; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - // Otherwise return the array. |
|
| 200 | - return $new_value; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - private function add_filter_get_post_metadata() { |
|
| 204 | - |
|
| 205 | - add_filter( 'get_post_metadata', array( |
|
| 206 | - $this, |
|
| 207 | - 'get_post_metadata', |
|
| 208 | - ), 10, 4 ); |
|
| 209 | - |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - private function remove_filter_get_post_metadata() { |
|
| 213 | - |
|
| 214 | - remove_filter( 'get_post_metadata', array( |
|
| 215 | - $this, |
|
| 216 | - 'get_post_metadata', |
|
| 217 | - ), 10 ); |
|
| 218 | - |
|
| 219 | - } |
|
| 16 | + /** |
|
| 17 | + * The meta key used to store data for this property. We don't use wl_url to |
|
| 18 | + * avoid potential confusion about other URLs. |
|
| 19 | + * |
|
| 20 | + * @since 3.6.0 |
|
| 21 | + */ |
|
| 22 | + const META_KEY = 'wl_schema_url'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + public function get_rdf_predicate() { |
|
| 28 | + |
|
| 29 | + return 'http://schema.org/url'; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * {@inheritdoc} |
|
| 34 | + */ |
|
| 35 | + public function get_rdf_data_type() { |
|
| 36 | + |
|
| 37 | + return 'xsd:anyURI'; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * {@inheritdoc} |
|
| 42 | + */ |
|
| 43 | + public function get_data_type() { |
|
| 44 | + |
|
| 45 | + return Wordlift_Schema_Service::DATA_TYPE_URI; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * {@inheritdoc} |
|
| 50 | + */ |
|
| 51 | + public function get_cardinality() { |
|
| 52 | + |
|
| 53 | + return INF; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * {@inheritdoc} |
|
| 58 | + */ |
|
| 59 | + public function get_metabox_class() { |
|
| 60 | + |
|
| 61 | + return 'WL_Metabox_Field'; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * {@inheritdoc} |
|
| 66 | + */ |
|
| 67 | + public function get_metabox_label() { |
|
| 68 | + |
|
| 69 | + return __( 'Web Site(s)', 'wordlift' ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @var |
|
| 74 | + */ |
|
| 75 | + private $sparql_service; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Create a Wordlift_Schema_Url_Property_Service instance. |
|
| 79 | + * @since 3.6.0 |
|
| 80 | + * |
|
| 81 | + * @param Wordlift_Sparql_Service $sparql_service |
|
| 82 | + */ |
|
| 83 | + public function __construct( $sparql_service ) { |
|
| 84 | + parent::__construct(); |
|
| 85 | + |
|
| 86 | + // Finally listen for metadata requests for this field. |
|
| 87 | + $this->add_filter_get_post_metadata(); |
|
| 88 | + |
|
| 89 | + $this->sparql_service = $sparql_service; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Get the schema:url value for the specified post/entity. |
|
| 94 | + * |
|
| 95 | + * @since 3.6.0 |
|
| 96 | + * |
|
| 97 | + * @param int $post_id The post id. |
|
| 98 | + * |
|
| 99 | + * @return array|NULL The schema:url value or NULL if not set. |
|
| 100 | + */ |
|
| 101 | + public function get( $post_id ) { |
|
| 102 | + |
|
| 103 | + // Get the schema:url values set in WP. |
|
| 104 | + $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
| 105 | + |
|
| 106 | + // If the property has never been set, we set its default value the first |
|
| 107 | + // time to <permalink>. |
|
| 108 | + if ( empty( $values ) ) { |
|
| 109 | + return array( '<permalink>' ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // If there's only one value and that value is empty, we return NULL, i.e. |
|
| 113 | + // variable not set. |
|
| 114 | + if ( 1 === count( $values ) && empty( $values[0] ) ) { |
|
| 115 | + return null; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + // Finally return whatever values the editor set. |
|
| 119 | + return $values; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * {@inheritdoc} |
|
| 124 | + */ |
|
| 125 | + public function sanitize( $value ) { |
|
| 126 | + |
|
| 127 | + // TODO: check that it's an URL or that is <permalink> |
|
| 128 | + |
|
| 129 | + return $value; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Generate an insert query that inserts the schema:url values for the specified |
|
| 134 | + * post. |
|
| 135 | + * |
|
| 136 | + * @since 3.6.0 |
|
| 137 | + * |
|
| 138 | + * @param string $s The subject URI. |
|
| 139 | + * @param int $post_id The post id. |
|
| 140 | + * |
|
| 141 | + * @return string The insert query or an empty string. |
|
| 142 | + */ |
|
| 143 | + public function get_insert_query( $s, $post_id ) { |
|
| 144 | + |
|
| 145 | + // If we have no value, return an empty string (no query). |
|
| 146 | + if ( null === ( $values = $this->get( $post_id ) ) ) { |
|
| 147 | + return ''; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + // Create the insert query. |
|
| 151 | + $q = Wordlift_Query_Builder::new_instance()->insert(); |
|
| 152 | + |
|
| 153 | + // Add each schema:url, replacing <permalink> with the actual post permalink. |
|
| 154 | + foreach ( $values as $value ) { |
|
| 155 | + $q = $q->statement( $s, $this->get_rdf_predicate(), '<permalink>' === $value ? get_permalink( $post_id ) : $value, Wordlift_Query_Builder::OBJECT_URI ); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + // Build and return the query. |
|
| 159 | + return $q->build(); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Get direct calls to read this meta and alter the response according to our |
|
| 164 | + * own strategy, i.e. if a value has never been set for this meta, then return |
|
| 165 | + * <permalink>. |
|
| 166 | + * |
|
| 167 | + * @since 3.6.0 |
|
| 168 | + * |
|
| 169 | + * @param mixed $value The original value. |
|
| 170 | + * @param int $object_id The post id. |
|
| 171 | + * @param string $meta_key The meta key. We expect wl_schema_url or we return straight the value. |
|
| 172 | + * @param bool $single Whether to return a single value. |
|
| 173 | + * |
|
| 174 | + * @return array|mixed|NULL|string |
|
| 175 | + */ |
|
| 176 | + public function get_post_metadata( $value, $object_id, $meta_key, $single ) { |
|
| 177 | + |
|
| 178 | + // It's not us, return the value. |
|
| 179 | + if ( self::META_KEY !== $meta_key ) { |
|
| 180 | + return $value; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + $this->remove_filter_get_post_metadata(); |
|
| 184 | + |
|
| 185 | + $new_value = $this->get( $object_id ); |
|
| 186 | + |
|
| 187 | + $this->add_filter_get_post_metadata(); |
|
| 188 | + |
|
| 189 | + // If we must return a single value, but we don't have a value, then return an empty string. |
|
| 190 | + if ( $single && ( is_null( $new_value ) || empty( $new_value ) ) ) { |
|
| 191 | + return ''; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + // If we have a value and we need to return it as single, return the first value. |
|
| 195 | + if ( $single ) { |
|
| 196 | + return $new_value[0]; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + // Otherwise return the array. |
|
| 200 | + return $new_value; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + private function add_filter_get_post_metadata() { |
|
| 204 | + |
|
| 205 | + add_filter( 'get_post_metadata', array( |
|
| 206 | + $this, |
|
| 207 | + 'get_post_metadata', |
|
| 208 | + ), 10, 4 ); |
|
| 209 | + |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + private function remove_filter_get_post_metadata() { |
|
| 213 | + |
|
| 214 | + remove_filter( 'get_post_metadata', array( |
|
| 215 | + $this, |
|
| 216 | + 'get_post_metadata', |
|
| 217 | + ), 10 ); |
|
| 218 | + |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | 221 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | */ |
| 67 | 67 | public function get_metabox_label() { |
| 68 | 68 | |
| 69 | - return __( 'Web Site(s)', 'wordlift' ); |
|
| 69 | + return __('Web Site(s)', 'wordlift'); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @param Wordlift_Sparql_Service $sparql_service |
| 82 | 82 | */ |
| 83 | - public function __construct( $sparql_service ) { |
|
| 83 | + public function __construct($sparql_service) { |
|
| 84 | 84 | parent::__construct(); |
| 85 | 85 | |
| 86 | 86 | // Finally listen for metadata requests for this field. |
@@ -98,20 +98,20 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @return array|NULL The schema:url value or NULL if not set. |
| 100 | 100 | */ |
| 101 | - public function get( $post_id ) { |
|
| 101 | + public function get($post_id) { |
|
| 102 | 102 | |
| 103 | 103 | // Get the schema:url values set in WP. |
| 104 | - $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
| 104 | + $values = get_post_meta($post_id, self::META_KEY, false); |
|
| 105 | 105 | |
| 106 | 106 | // If the property has never been set, we set its default value the first |
| 107 | 107 | // time to <permalink>. |
| 108 | - if ( empty( $values ) ) { |
|
| 109 | - return array( '<permalink>' ); |
|
| 108 | + if (empty($values)) { |
|
| 109 | + return array('<permalink>'); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | // If there's only one value and that value is empty, we return NULL, i.e. |
| 113 | 113 | // variable not set. |
| 114 | - if ( 1 === count( $values ) && empty( $values[0] ) ) { |
|
| 114 | + if (1 === count($values) && empty($values[0])) { |
|
| 115 | 115 | return null; |
| 116 | 116 | } |
| 117 | 117 | |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | /** |
| 123 | 123 | * {@inheritdoc} |
| 124 | 124 | */ |
| 125 | - public function sanitize( $value ) { |
|
| 125 | + public function sanitize($value) { |
|
| 126 | 126 | |
| 127 | 127 | // TODO: check that it's an URL or that is <permalink> |
| 128 | 128 | |
@@ -140,10 +140,10 @@ discard block |
||
| 140 | 140 | * |
| 141 | 141 | * @return string The insert query or an empty string. |
| 142 | 142 | */ |
| 143 | - public function get_insert_query( $s, $post_id ) { |
|
| 143 | + public function get_insert_query($s, $post_id) { |
|
| 144 | 144 | |
| 145 | 145 | // If we have no value, return an empty string (no query). |
| 146 | - if ( null === ( $values = $this->get( $post_id ) ) ) { |
|
| 146 | + if (null === ($values = $this->get($post_id))) { |
|
| 147 | 147 | return ''; |
| 148 | 148 | } |
| 149 | 149 | |
@@ -151,8 +151,8 @@ discard block |
||
| 151 | 151 | $q = Wordlift_Query_Builder::new_instance()->insert(); |
| 152 | 152 | |
| 153 | 153 | // Add each schema:url, replacing <permalink> with the actual post permalink. |
| 154 | - foreach ( $values as $value ) { |
|
| 155 | - $q = $q->statement( $s, $this->get_rdf_predicate(), '<permalink>' === $value ? get_permalink( $post_id ) : $value, Wordlift_Query_Builder::OBJECT_URI ); |
|
| 154 | + foreach ($values as $value) { |
|
| 155 | + $q = $q->statement($s, $this->get_rdf_predicate(), '<permalink>' === $value ? get_permalink($post_id) : $value, Wordlift_Query_Builder::OBJECT_URI); |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | // Build and return the query. |
@@ -173,26 +173,26 @@ discard block |
||
| 173 | 173 | * |
| 174 | 174 | * @return array|mixed|NULL|string |
| 175 | 175 | */ |
| 176 | - public function get_post_metadata( $value, $object_id, $meta_key, $single ) { |
|
| 176 | + public function get_post_metadata($value, $object_id, $meta_key, $single) { |
|
| 177 | 177 | |
| 178 | 178 | // It's not us, return the value. |
| 179 | - if ( self::META_KEY !== $meta_key ) { |
|
| 179 | + if (self::META_KEY !== $meta_key) { |
|
| 180 | 180 | return $value; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | $this->remove_filter_get_post_metadata(); |
| 184 | 184 | |
| 185 | - $new_value = $this->get( $object_id ); |
|
| 185 | + $new_value = $this->get($object_id); |
|
| 186 | 186 | |
| 187 | 187 | $this->add_filter_get_post_metadata(); |
| 188 | 188 | |
| 189 | 189 | // If we must return a single value, but we don't have a value, then return an empty string. |
| 190 | - if ( $single && ( is_null( $new_value ) || empty( $new_value ) ) ) { |
|
| 190 | + if ($single && (is_null($new_value) || empty($new_value))) { |
|
| 191 | 191 | return ''; |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | // If we have a value and we need to return it as single, return the first value. |
| 195 | - if ( $single ) { |
|
| 195 | + if ($single) { |
|
| 196 | 196 | return $new_value[0]; |
| 197 | 197 | } |
| 198 | 198 | |
@@ -202,19 +202,19 @@ discard block |
||
| 202 | 202 | |
| 203 | 203 | private function add_filter_get_post_metadata() { |
| 204 | 204 | |
| 205 | - add_filter( 'get_post_metadata', array( |
|
| 205 | + add_filter('get_post_metadata', array( |
|
| 206 | 206 | $this, |
| 207 | 207 | 'get_post_metadata', |
| 208 | - ), 10, 4 ); |
|
| 208 | + ), 10, 4); |
|
| 209 | 209 | |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | private function remove_filter_get_post_metadata() { |
| 213 | 213 | |
| 214 | - remove_filter( 'get_post_metadata', array( |
|
| 214 | + remove_filter('get_post_metadata', array( |
|
| 215 | 215 | $this, |
| 216 | 216 | 'get_post_metadata', |
| 217 | - ), 10 ); |
|
| 217 | + ), 10); |
|
| 218 | 218 | |
| 219 | 219 | } |
| 220 | 220 | |
@@ -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 ( empty( $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 ( empty( $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 ( empty( $posts ) ) { |
|
| 202 | - $this->log->warn( "No post for URI $uri." ); |
|
| 201 | + if (empty($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 |
@@ -7,322 +7,322 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Query_Builder { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The INSERT statement template. |
|
| 12 | - * |
|
| 13 | - * @since 3.1.7 |
|
| 14 | - */ |
|
| 15 | - const INSERT = 'INSERT DATA { %s };'; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * The DELETE statement template (it repeats the statements in the WHERE clause. |
|
| 19 | - * |
|
| 20 | - * @since 3.1.7 |
|
| 21 | - */ |
|
| 22 | - const DELETE = 'DELETE { %s } WHERE { %1$s };'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Tell the statement function to guess the object type (URI, value or parameter). |
|
| 26 | - * |
|
| 27 | - * @since 3.1.7 |
|
| 28 | - */ |
|
| 29 | - const OBJECT_AUTO = - 1; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Tell the statement function that the object is a URI. |
|
| 33 | - * |
|
| 34 | - * @since 3.1.7 |
|
| 35 | - */ |
|
| 36 | - const OBJECT_URI = 0; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Tell the statement function that the object is a value. |
|
| 40 | - * |
|
| 41 | - * @since 3.1.7 |
|
| 42 | - */ |
|
| 43 | - const OBJECT_VALUE = 1; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Tell the statement function that the object is a parameter. |
|
| 47 | - * |
|
| 48 | - * @since 3.1.7 |
|
| 49 | - */ |
|
| 50 | - const OBJECT_PARAMETER = 2; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The RDFS type. |
|
| 54 | - * |
|
| 55 | - * @since 3.1.7 |
|
| 56 | - */ |
|
| 57 | - const RDFS_TYPE_URI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The schema.org/Person type. |
|
| 61 | - * |
|
| 62 | - * @since 3.1.7 |
|
| 63 | - */ |
|
| 64 | - const SCHEMA_PERSON_URI = 'http://schema.org/Person'; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The schema.org given name predicate. |
|
| 68 | - * |
|
| 69 | - * @since 3.1.7 |
|
| 70 | - */ |
|
| 71 | - const SCHEMA_GIVEN_NAME_URI = 'http://schema.org/givenName'; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * The schema.org family name predicate. |
|
| 75 | - * |
|
| 76 | - * @since 3.1.7 |
|
| 77 | - */ |
|
| 78 | - const SCHEMA_FAMILY_NAME_URI = 'http://schema.org/familyName'; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * The schema.org url predicate. |
|
| 82 | - * |
|
| 83 | - * @since 3.1.7 |
|
| 84 | - */ |
|
| 85 | - const SCHEMA_URL_URI = 'http://schema.org/url'; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @since 3.14.0 |
|
| 89 | - */ |
|
| 90 | - const SCHEMA_IMAGE_URI = 'http://schema.org/image'; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * The location created predicate. |
|
| 94 | - * |
|
| 95 | - * @since 3.14.0 |
|
| 96 | - */ |
|
| 97 | - const SCHEMA_LOCATION_CREATED_URI = 'http://schema.org/locationCreated'; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @since 3.14.0 |
|
| 101 | - */ |
|
| 102 | - const SCHEMA_AUTHOR_URI = 'http://schema.org/author'; |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @since 3.14.0 |
|
| 106 | - */ |
|
| 107 | - const SCHEMA_INTERACTION_COUNT_URI = 'http://schema.org/interactionCount'; |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @since 3.14.0 |
|
| 111 | - */ |
|
| 112 | - const DCTERMS_SUBJECT_URI = 'http://purl.org/dc/terms/subject'; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @since 3.14.0 |
|
| 116 | - */ |
|
| 117 | - const DCTERMS_REFERENCES_URI = 'http://purl.org/dc/terms/references'; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * @since 3.15.0 |
|
| 121 | - */ |
|
| 122 | - const DCTERMS_RELATION_URI = 'http://purl.org/dc/terms/relation'; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * The RDF label. |
|
| 126 | - * |
|
| 127 | - * @since 3.1.7 |
|
| 128 | - */ |
|
| 129 | - const RDFS_LABEL_URI = 'http://www.w3.org/2000/01/rdf-schema#label'; |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * The schema.org headline. |
|
| 133 | - * |
|
| 134 | - * @since 3.18.0 |
|
| 135 | - */ |
|
| 136 | - const SCHEMA_HEADLINE_URI = 'http://schema.org/headline'; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Hold the template (INSERT or DELETE). |
|
| 140 | - * |
|
| 141 | - * @since 3.1.7 |
|
| 142 | - * @access private |
|
| 143 | - * @var string $template The query template. |
|
| 144 | - */ |
|
| 145 | - private $template; |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * An array of statements (in the form of subject, predicate, object). |
|
| 149 | - * |
|
| 150 | - * @since 3.1.7 |
|
| 151 | - * @access private |
|
| 152 | - * @var array $statements An array of statements. |
|
| 153 | - */ |
|
| 154 | - private $statements = array(); |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Create a new instance of the Query builder (compatible with PHP 5.3). |
|
| 158 | - * |
|
| 159 | - * @since 3.1.7 |
|
| 160 | - * @return Wordlift_Query_Builder A new instance of the Query builder. |
|
| 161 | - */ |
|
| 162 | - public static function new_instance() { |
|
| 163 | - |
|
| 164 | - return new Wordlift_Query_Builder(); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Set the query to INSERT. |
|
| 169 | - * |
|
| 170 | - * @since 3.1.7 |
|
| 171 | - * @return Wordlift_Query_Builder The Query builder. |
|
| 172 | - */ |
|
| 173 | - public function insert() { |
|
| 174 | - |
|
| 175 | - $this->template = self::INSERT; |
|
| 176 | - |
|
| 177 | - return $this; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Set the query to DELETE. |
|
| 182 | - * |
|
| 183 | - * @since 3.1.7 |
|
| 184 | - * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 185 | - */ |
|
| 186 | - public function delete() { |
|
| 187 | - |
|
| 188 | - $this->template = self::DELETE; |
|
| 189 | - |
|
| 190 | - return $this; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Set the query to SELECT. |
|
| 195 | - * |
|
| 196 | - * @since 3.12.2 |
|
| 197 | - * |
|
| 198 | - * @param string $props The list of properties to read. |
|
| 199 | - * |
|
| 200 | - * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 201 | - */ |
|
| 202 | - public function select( $props = '*' ) { |
|
| 203 | - |
|
| 204 | - $this->template = "SELECT $props WHERE { %s }"; |
|
| 205 | - |
|
| 206 | - return $this; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Add a statement. |
|
| 211 | - * |
|
| 212 | - * @since 3.1.7 |
|
| 213 | - * |
|
| 214 | - * @param string $subject The subject of the statement (must be a URI). |
|
| 215 | - * @param string $predicate The predicate (must be a URI). |
|
| 216 | - * @param string $object The object, can be a URI or a value. |
|
| 217 | - * @param int $object_type The object type, either a {@link OBJECT_URI} or a value {@link OBJECT_VALUE}. If set to {@link OBJECT_AUTO}, the Query builder will try to guess. |
|
| 218 | - * @param string|null $data_type The data type (or null). |
|
| 219 | - * @param string|null $language The language code (or null). |
|
| 220 | - * |
|
| 221 | - * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 222 | - */ |
|
| 223 | - public function statement( $subject, $predicate, $object, $object_type = self::OBJECT_AUTO, $data_type = null, $language = null ) { |
|
| 224 | - |
|
| 225 | - // If no value has been provided, we don't set any statement. |
|
| 226 | - if ( empty( $object ) ) { |
|
| 227 | - return $this; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - // Guess the subject type. |
|
| 231 | - $subject_value_type = $this->guess_subject_type( $subject ); |
|
| 232 | - |
|
| 233 | - // Get the object type if set, otherwise try to guess it. |
|
| 234 | - $object_value_type = ( self::OBJECT_AUTO === $object_type ? $this->guess_object_type( $predicate, $object ) : $object_type ); |
|
| 235 | - |
|
| 236 | - // Prepare the statement template. |
|
| 237 | - $template = |
|
| 238 | - // Subject as a parameter, no `<`, `>`. |
|
| 239 | - ( self::OBJECT_PARAMETER === $subject_value_type ? '%1$s' : '<%1$s>' ) . |
|
| 240 | - // Predicate. |
|
| 241 | - ' <%2$s> ' . |
|
| 242 | - // Object. |
|
| 243 | - ( self::OBJECT_URI === $object_value_type ? '<%3$s>' : |
|
| 244 | - ( self::OBJECT_PARAMETER === $object_value_type ? '%3$s' : |
|
| 245 | - // self::OBJECT_VALUE === $object_value_type |
|
| 246 | - '"%3$s"' . ( isset( $data_type ) ? '^^%4$s' : '' ) . ( isset( $language ) ? '@%5$s' : '' ) ) ); |
|
| 247 | - |
|
| 248 | - // Escape the subject, predicate and object. |
|
| 249 | - $escaped_subject = Wordlift_Sparql_Service::escape_uri( $subject ); |
|
| 250 | - $escaped_predicate = Wordlift_Sparql_Service::escape_uri( $predicate ); |
|
| 251 | - $escaped_object = ( self::OBJECT_URI === $object_value_type ? Wordlift_Sparql_Service::escape_uri( $object ) : Wordlift_Sparql_Service::escape( $object ) ); |
|
| 252 | - |
|
| 253 | - // Prepare the statement and add it to the list of statements. |
|
| 254 | - $this->statements[] = sprintf( $template, $escaped_subject, $escaped_predicate, $escaped_object, $data_type, $language ); |
|
| 255 | - |
|
| 256 | - return $this; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Build the query. |
|
| 261 | - * |
|
| 262 | - * @since 3.1.7 |
|
| 263 | - * @return string The query string. |
|
| 264 | - */ |
|
| 265 | - public function build() { |
|
| 266 | - |
|
| 267 | - // If there are no statements return an empty string. |
|
| 268 | - if ( empty( $this->statements ) ) { |
|
| 269 | - return ''; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - return sprintf( $this->template, implode( ' . ', $this->statements ) ) . "\n"; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Guess the statement object type. |
|
| 277 | - * |
|
| 278 | - * @since 3.1.7 |
|
| 279 | - * |
|
| 280 | - * @param string $predicate The predicate. |
|
| 281 | - * @param string $object The object. |
|
| 282 | - * |
|
| 283 | - * @return int {@link Wordlift_Query_Builder::OBJECT_URI} if the Query builder thinks the object must be an URI, {@link Wordlift_Query_Builder::OBJECT_VALUE} otherwise. |
|
| 284 | - */ |
|
| 285 | - private function guess_object_type( $predicate, $object ) { |
|
| 286 | - |
|
| 287 | - // If the object starts with a question mark, it's a parameter. |
|
| 288 | - if ( 0 === strpos( $object, '?' ) ) { |
|
| 289 | - return self::OBJECT_PARAMETER; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - // Guess based on the predicate. |
|
| 293 | - switch ( $predicate ) { |
|
| 294 | - |
|
| 295 | - case self::DCTERMS_REFERENCES_URI: |
|
| 296 | - case self::DCTERMS_SUBJECT_URI: |
|
| 297 | - case self::RDFS_TYPE_URI: |
|
| 298 | - case self::SCHEMA_AUTHOR_URI: |
|
| 299 | - case self::SCHEMA_LOCATION_CREATED_URI: |
|
| 300 | - case self::SCHEMA_URL_URI: |
|
| 301 | - case self::SCHEMA_IMAGE_URI: |
|
| 302 | - return self::OBJECT_URI; |
|
| 303 | - |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - return self::OBJECT_VALUE; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Guess the subject type. |
|
| 311 | - * |
|
| 312 | - * @since 3.12.3 |
|
| 313 | - * |
|
| 314 | - * @param string $subject The subject string. |
|
| 315 | - * |
|
| 316 | - * @return int {@link Wordlift_Query_Builder::OBJECT_PARAMETER} if the Query builder thinks the subject is a parameter (starts with ?), otherwise {@link Wordlift_Query_Builder::OBJECT_URI}. |
|
| 317 | - */ |
|
| 318 | - private function guess_subject_type( $subject ) { |
|
| 319 | - |
|
| 320 | - // If the object starts with a question mark, it's a parameter. |
|
| 321 | - if ( 0 === strpos( $subject, '?' ) ) { |
|
| 322 | - return self::OBJECT_PARAMETER; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - return self::OBJECT_URI; |
|
| 326 | - } |
|
| 10 | + /** |
|
| 11 | + * The INSERT statement template. |
|
| 12 | + * |
|
| 13 | + * @since 3.1.7 |
|
| 14 | + */ |
|
| 15 | + const INSERT = 'INSERT DATA { %s };'; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * The DELETE statement template (it repeats the statements in the WHERE clause. |
|
| 19 | + * |
|
| 20 | + * @since 3.1.7 |
|
| 21 | + */ |
|
| 22 | + const DELETE = 'DELETE { %s } WHERE { %1$s };'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Tell the statement function to guess the object type (URI, value or parameter). |
|
| 26 | + * |
|
| 27 | + * @since 3.1.7 |
|
| 28 | + */ |
|
| 29 | + const OBJECT_AUTO = - 1; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Tell the statement function that the object is a URI. |
|
| 33 | + * |
|
| 34 | + * @since 3.1.7 |
|
| 35 | + */ |
|
| 36 | + const OBJECT_URI = 0; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Tell the statement function that the object is a value. |
|
| 40 | + * |
|
| 41 | + * @since 3.1.7 |
|
| 42 | + */ |
|
| 43 | + const OBJECT_VALUE = 1; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Tell the statement function that the object is a parameter. |
|
| 47 | + * |
|
| 48 | + * @since 3.1.7 |
|
| 49 | + */ |
|
| 50 | + const OBJECT_PARAMETER = 2; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The RDFS type. |
|
| 54 | + * |
|
| 55 | + * @since 3.1.7 |
|
| 56 | + */ |
|
| 57 | + const RDFS_TYPE_URI = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The schema.org/Person type. |
|
| 61 | + * |
|
| 62 | + * @since 3.1.7 |
|
| 63 | + */ |
|
| 64 | + const SCHEMA_PERSON_URI = 'http://schema.org/Person'; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The schema.org given name predicate. |
|
| 68 | + * |
|
| 69 | + * @since 3.1.7 |
|
| 70 | + */ |
|
| 71 | + const SCHEMA_GIVEN_NAME_URI = 'http://schema.org/givenName'; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * The schema.org family name predicate. |
|
| 75 | + * |
|
| 76 | + * @since 3.1.7 |
|
| 77 | + */ |
|
| 78 | + const SCHEMA_FAMILY_NAME_URI = 'http://schema.org/familyName'; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * The schema.org url predicate. |
|
| 82 | + * |
|
| 83 | + * @since 3.1.7 |
|
| 84 | + */ |
|
| 85 | + const SCHEMA_URL_URI = 'http://schema.org/url'; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @since 3.14.0 |
|
| 89 | + */ |
|
| 90 | + const SCHEMA_IMAGE_URI = 'http://schema.org/image'; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * The location created predicate. |
|
| 94 | + * |
|
| 95 | + * @since 3.14.0 |
|
| 96 | + */ |
|
| 97 | + const SCHEMA_LOCATION_CREATED_URI = 'http://schema.org/locationCreated'; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @since 3.14.0 |
|
| 101 | + */ |
|
| 102 | + const SCHEMA_AUTHOR_URI = 'http://schema.org/author'; |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @since 3.14.0 |
|
| 106 | + */ |
|
| 107 | + const SCHEMA_INTERACTION_COUNT_URI = 'http://schema.org/interactionCount'; |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @since 3.14.0 |
|
| 111 | + */ |
|
| 112 | + const DCTERMS_SUBJECT_URI = 'http://purl.org/dc/terms/subject'; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @since 3.14.0 |
|
| 116 | + */ |
|
| 117 | + const DCTERMS_REFERENCES_URI = 'http://purl.org/dc/terms/references'; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * @since 3.15.0 |
|
| 121 | + */ |
|
| 122 | + const DCTERMS_RELATION_URI = 'http://purl.org/dc/terms/relation'; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * The RDF label. |
|
| 126 | + * |
|
| 127 | + * @since 3.1.7 |
|
| 128 | + */ |
|
| 129 | + const RDFS_LABEL_URI = 'http://www.w3.org/2000/01/rdf-schema#label'; |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * The schema.org headline. |
|
| 133 | + * |
|
| 134 | + * @since 3.18.0 |
|
| 135 | + */ |
|
| 136 | + const SCHEMA_HEADLINE_URI = 'http://schema.org/headline'; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Hold the template (INSERT or DELETE). |
|
| 140 | + * |
|
| 141 | + * @since 3.1.7 |
|
| 142 | + * @access private |
|
| 143 | + * @var string $template The query template. |
|
| 144 | + */ |
|
| 145 | + private $template; |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * An array of statements (in the form of subject, predicate, object). |
|
| 149 | + * |
|
| 150 | + * @since 3.1.7 |
|
| 151 | + * @access private |
|
| 152 | + * @var array $statements An array of statements. |
|
| 153 | + */ |
|
| 154 | + private $statements = array(); |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Create a new instance of the Query builder (compatible with PHP 5.3). |
|
| 158 | + * |
|
| 159 | + * @since 3.1.7 |
|
| 160 | + * @return Wordlift_Query_Builder A new instance of the Query builder. |
|
| 161 | + */ |
|
| 162 | + public static function new_instance() { |
|
| 163 | + |
|
| 164 | + return new Wordlift_Query_Builder(); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Set the query to INSERT. |
|
| 169 | + * |
|
| 170 | + * @since 3.1.7 |
|
| 171 | + * @return Wordlift_Query_Builder The Query builder. |
|
| 172 | + */ |
|
| 173 | + public function insert() { |
|
| 174 | + |
|
| 175 | + $this->template = self::INSERT; |
|
| 176 | + |
|
| 177 | + return $this; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Set the query to DELETE. |
|
| 182 | + * |
|
| 183 | + * @since 3.1.7 |
|
| 184 | + * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 185 | + */ |
|
| 186 | + public function delete() { |
|
| 187 | + |
|
| 188 | + $this->template = self::DELETE; |
|
| 189 | + |
|
| 190 | + return $this; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Set the query to SELECT. |
|
| 195 | + * |
|
| 196 | + * @since 3.12.2 |
|
| 197 | + * |
|
| 198 | + * @param string $props The list of properties to read. |
|
| 199 | + * |
|
| 200 | + * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 201 | + */ |
|
| 202 | + public function select( $props = '*' ) { |
|
| 203 | + |
|
| 204 | + $this->template = "SELECT $props WHERE { %s }"; |
|
| 205 | + |
|
| 206 | + return $this; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Add a statement. |
|
| 211 | + * |
|
| 212 | + * @since 3.1.7 |
|
| 213 | + * |
|
| 214 | + * @param string $subject The subject of the statement (must be a URI). |
|
| 215 | + * @param string $predicate The predicate (must be a URI). |
|
| 216 | + * @param string $object The object, can be a URI or a value. |
|
| 217 | + * @param int $object_type The object type, either a {@link OBJECT_URI} or a value {@link OBJECT_VALUE}. If set to {@link OBJECT_AUTO}, the Query builder will try to guess. |
|
| 218 | + * @param string|null $data_type The data type (or null). |
|
| 219 | + * @param string|null $language The language code (or null). |
|
| 220 | + * |
|
| 221 | + * @return $this \Wordlift_Query_Builder The Query builder. |
|
| 222 | + */ |
|
| 223 | + public function statement( $subject, $predicate, $object, $object_type = self::OBJECT_AUTO, $data_type = null, $language = null ) { |
|
| 224 | + |
|
| 225 | + // If no value has been provided, we don't set any statement. |
|
| 226 | + if ( empty( $object ) ) { |
|
| 227 | + return $this; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + // Guess the subject type. |
|
| 231 | + $subject_value_type = $this->guess_subject_type( $subject ); |
|
| 232 | + |
|
| 233 | + // Get the object type if set, otherwise try to guess it. |
|
| 234 | + $object_value_type = ( self::OBJECT_AUTO === $object_type ? $this->guess_object_type( $predicate, $object ) : $object_type ); |
|
| 235 | + |
|
| 236 | + // Prepare the statement template. |
|
| 237 | + $template = |
|
| 238 | + // Subject as a parameter, no `<`, `>`. |
|
| 239 | + ( self::OBJECT_PARAMETER === $subject_value_type ? '%1$s' : '<%1$s>' ) . |
|
| 240 | + // Predicate. |
|
| 241 | + ' <%2$s> ' . |
|
| 242 | + // Object. |
|
| 243 | + ( self::OBJECT_URI === $object_value_type ? '<%3$s>' : |
|
| 244 | + ( self::OBJECT_PARAMETER === $object_value_type ? '%3$s' : |
|
| 245 | + // self::OBJECT_VALUE === $object_value_type |
|
| 246 | + '"%3$s"' . ( isset( $data_type ) ? '^^%4$s' : '' ) . ( isset( $language ) ? '@%5$s' : '' ) ) ); |
|
| 247 | + |
|
| 248 | + // Escape the subject, predicate and object. |
|
| 249 | + $escaped_subject = Wordlift_Sparql_Service::escape_uri( $subject ); |
|
| 250 | + $escaped_predicate = Wordlift_Sparql_Service::escape_uri( $predicate ); |
|
| 251 | + $escaped_object = ( self::OBJECT_URI === $object_value_type ? Wordlift_Sparql_Service::escape_uri( $object ) : Wordlift_Sparql_Service::escape( $object ) ); |
|
| 252 | + |
|
| 253 | + // Prepare the statement and add it to the list of statements. |
|
| 254 | + $this->statements[] = sprintf( $template, $escaped_subject, $escaped_predicate, $escaped_object, $data_type, $language ); |
|
| 255 | + |
|
| 256 | + return $this; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Build the query. |
|
| 261 | + * |
|
| 262 | + * @since 3.1.7 |
|
| 263 | + * @return string The query string. |
|
| 264 | + */ |
|
| 265 | + public function build() { |
|
| 266 | + |
|
| 267 | + // If there are no statements return an empty string. |
|
| 268 | + if ( empty( $this->statements ) ) { |
|
| 269 | + return ''; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + return sprintf( $this->template, implode( ' . ', $this->statements ) ) . "\n"; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Guess the statement object type. |
|
| 277 | + * |
|
| 278 | + * @since 3.1.7 |
|
| 279 | + * |
|
| 280 | + * @param string $predicate The predicate. |
|
| 281 | + * @param string $object The object. |
|
| 282 | + * |
|
| 283 | + * @return int {@link Wordlift_Query_Builder::OBJECT_URI} if the Query builder thinks the object must be an URI, {@link Wordlift_Query_Builder::OBJECT_VALUE} otherwise. |
|
| 284 | + */ |
|
| 285 | + private function guess_object_type( $predicate, $object ) { |
|
| 286 | + |
|
| 287 | + // If the object starts with a question mark, it's a parameter. |
|
| 288 | + if ( 0 === strpos( $object, '?' ) ) { |
|
| 289 | + return self::OBJECT_PARAMETER; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + // Guess based on the predicate. |
|
| 293 | + switch ( $predicate ) { |
|
| 294 | + |
|
| 295 | + case self::DCTERMS_REFERENCES_URI: |
|
| 296 | + case self::DCTERMS_SUBJECT_URI: |
|
| 297 | + case self::RDFS_TYPE_URI: |
|
| 298 | + case self::SCHEMA_AUTHOR_URI: |
|
| 299 | + case self::SCHEMA_LOCATION_CREATED_URI: |
|
| 300 | + case self::SCHEMA_URL_URI: |
|
| 301 | + case self::SCHEMA_IMAGE_URI: |
|
| 302 | + return self::OBJECT_URI; |
|
| 303 | + |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + return self::OBJECT_VALUE; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Guess the subject type. |
|
| 311 | + * |
|
| 312 | + * @since 3.12.3 |
|
| 313 | + * |
|
| 314 | + * @param string $subject The subject string. |
|
| 315 | + * |
|
| 316 | + * @return int {@link Wordlift_Query_Builder::OBJECT_PARAMETER} if the Query builder thinks the subject is a parameter (starts with ?), otherwise {@link Wordlift_Query_Builder::OBJECT_URI}. |
|
| 317 | + */ |
|
| 318 | + private function guess_subject_type( $subject ) { |
|
| 319 | + |
|
| 320 | + // If the object starts with a question mark, it's a parameter. |
|
| 321 | + if ( 0 === strpos( $subject, '?' ) ) { |
|
| 322 | + return self::OBJECT_PARAMETER; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + return self::OBJECT_URI; |
|
| 326 | + } |
|
| 327 | 327 | |
| 328 | 328 | } |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | * |
| 200 | 200 | * @return $this \Wordlift_Query_Builder The Query builder. |
| 201 | 201 | */ |
| 202 | - public function select( $props = '*' ) { |
|
| 202 | + public function select($props = '*') { |
|
| 203 | 203 | |
| 204 | 204 | $this->template = "SELECT $props WHERE { %s }"; |
| 205 | 205 | |
@@ -220,38 +220,36 @@ discard block |
||
| 220 | 220 | * |
| 221 | 221 | * @return $this \Wordlift_Query_Builder The Query builder. |
| 222 | 222 | */ |
| 223 | - public function statement( $subject, $predicate, $object, $object_type = self::OBJECT_AUTO, $data_type = null, $language = null ) { |
|
| 223 | + public function statement($subject, $predicate, $object, $object_type = self::OBJECT_AUTO, $data_type = null, $language = null) { |
|
| 224 | 224 | |
| 225 | 225 | // If no value has been provided, we don't set any statement. |
| 226 | - if ( empty( $object ) ) { |
|
| 226 | + if (empty($object)) { |
|
| 227 | 227 | return $this; |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | // Guess the subject type. |
| 231 | - $subject_value_type = $this->guess_subject_type( $subject ); |
|
| 231 | + $subject_value_type = $this->guess_subject_type($subject); |
|
| 232 | 232 | |
| 233 | 233 | // Get the object type if set, otherwise try to guess it. |
| 234 | - $object_value_type = ( self::OBJECT_AUTO === $object_type ? $this->guess_object_type( $predicate, $object ) : $object_type ); |
|
| 234 | + $object_value_type = (self::OBJECT_AUTO === $object_type ? $this->guess_object_type($predicate, $object) : $object_type); |
|
| 235 | 235 | |
| 236 | 236 | // Prepare the statement template. |
| 237 | 237 | $template = |
| 238 | 238 | // Subject as a parameter, no `<`, `>`. |
| 239 | - ( self::OBJECT_PARAMETER === $subject_value_type ? '%1$s' : '<%1$s>' ) . |
|
| 239 | + (self::OBJECT_PARAMETER === $subject_value_type ? '%1$s' : '<%1$s>'). |
|
| 240 | 240 | // Predicate. |
| 241 | - ' <%2$s> ' . |
|
| 241 | + ' <%2$s> '. |
|
| 242 | 242 | // Object. |
| 243 | - ( self::OBJECT_URI === $object_value_type ? '<%3$s>' : |
|
| 244 | - ( self::OBJECT_PARAMETER === $object_value_type ? '%3$s' : |
|
| 245 | - // self::OBJECT_VALUE === $object_value_type |
|
| 246 | - '"%3$s"' . ( isset( $data_type ) ? '^^%4$s' : '' ) . ( isset( $language ) ? '@%5$s' : '' ) ) ); |
|
| 243 | + (self::OBJECT_URI === $object_value_type ? '<%3$s>' : (self::OBJECT_PARAMETER === $object_value_type ? '%3$s' : // self::OBJECT_VALUE === $object_value_type |
|
| 244 | + '"%3$s"'.(isset($data_type) ? '^^%4$s' : '').(isset($language) ? '@%5$s' : ''))); |
|
| 247 | 245 | |
| 248 | 246 | // Escape the subject, predicate and object. |
| 249 | - $escaped_subject = Wordlift_Sparql_Service::escape_uri( $subject ); |
|
| 250 | - $escaped_predicate = Wordlift_Sparql_Service::escape_uri( $predicate ); |
|
| 251 | - $escaped_object = ( self::OBJECT_URI === $object_value_type ? Wordlift_Sparql_Service::escape_uri( $object ) : Wordlift_Sparql_Service::escape( $object ) ); |
|
| 247 | + $escaped_subject = Wordlift_Sparql_Service::escape_uri($subject); |
|
| 248 | + $escaped_predicate = Wordlift_Sparql_Service::escape_uri($predicate); |
|
| 249 | + $escaped_object = (self::OBJECT_URI === $object_value_type ? Wordlift_Sparql_Service::escape_uri($object) : Wordlift_Sparql_Service::escape($object)); |
|
| 252 | 250 | |
| 253 | 251 | // Prepare the statement and add it to the list of statements. |
| 254 | - $this->statements[] = sprintf( $template, $escaped_subject, $escaped_predicate, $escaped_object, $data_type, $language ); |
|
| 252 | + $this->statements[] = sprintf($template, $escaped_subject, $escaped_predicate, $escaped_object, $data_type, $language); |
|
| 255 | 253 | |
| 256 | 254 | return $this; |
| 257 | 255 | } |
@@ -265,11 +263,11 @@ discard block |
||
| 265 | 263 | public function build() { |
| 266 | 264 | |
| 267 | 265 | // If there are no statements return an empty string. |
| 268 | - if ( empty( $this->statements ) ) { |
|
| 266 | + if (empty($this->statements)) { |
|
| 269 | 267 | return ''; |
| 270 | 268 | } |
| 271 | 269 | |
| 272 | - return sprintf( $this->template, implode( ' . ', $this->statements ) ) . "\n"; |
|
| 270 | + return sprintf($this->template, implode(' . ', $this->statements))."\n"; |
|
| 273 | 271 | } |
| 274 | 272 | |
| 275 | 273 | /** |
@@ -282,15 +280,15 @@ discard block |
||
| 282 | 280 | * |
| 283 | 281 | * @return int {@link Wordlift_Query_Builder::OBJECT_URI} if the Query builder thinks the object must be an URI, {@link Wordlift_Query_Builder::OBJECT_VALUE} otherwise. |
| 284 | 282 | */ |
| 285 | - private function guess_object_type( $predicate, $object ) { |
|
| 283 | + private function guess_object_type($predicate, $object) { |
|
| 286 | 284 | |
| 287 | 285 | // If the object starts with a question mark, it's a parameter. |
| 288 | - if ( 0 === strpos( $object, '?' ) ) { |
|
| 286 | + if (0 === strpos($object, '?')) { |
|
| 289 | 287 | return self::OBJECT_PARAMETER; |
| 290 | 288 | } |
| 291 | 289 | |
| 292 | 290 | // Guess based on the predicate. |
| 293 | - switch ( $predicate ) { |
|
| 291 | + switch ($predicate) { |
|
| 294 | 292 | |
| 295 | 293 | case self::DCTERMS_REFERENCES_URI: |
| 296 | 294 | case self::DCTERMS_SUBJECT_URI: |
@@ -315,10 +313,10 @@ discard block |
||
| 315 | 313 | * |
| 316 | 314 | * @return int {@link Wordlift_Query_Builder::OBJECT_PARAMETER} if the Query builder thinks the subject is a parameter (starts with ?), otherwise {@link Wordlift_Query_Builder::OBJECT_URI}. |
| 317 | 315 | */ |
| 318 | - private function guess_subject_type( $subject ) { |
|
| 316 | + private function guess_subject_type($subject) { |
|
| 319 | 317 | |
| 320 | 318 | // If the object starts with a question mark, it's a parameter. |
| 321 | - if ( 0 === strpos( $subject, '?' ) ) { |
|
| 319 | + if (0 === strpos($subject, '?')) { |
|
| 322 | 320 | return self::OBJECT_PARAMETER; |
| 323 | 321 | } |
| 324 | 322 | |
@@ -18,296 +18,296 @@ |
||
| 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 | - private $entity_post_ids_linked_from_post_content = array(); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 59 | - * |
|
| 60 | - * @since 3.16.3 |
|
| 61 | - * @access private |
|
| 62 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 63 | - */ |
|
| 64 | - private $entity_uri_service; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * A {@link Wordlift_Log_Service} instance. |
|
| 68 | - * |
|
| 69 | - * @since 3.16.0 |
|
| 70 | - * |
|
| 71 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 72 | - */ |
|
| 73 | - private $log; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 77 | - * |
|
| 78 | - * @since 3.14.2 |
|
| 79 | - * @access private |
|
| 80 | - * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 81 | - */ |
|
| 82 | - private static $instance; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 86 | - * |
|
| 87 | - * @since 3.8.0 |
|
| 88 | - * |
|
| 89 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 90 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 91 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 92 | - */ |
|
| 93 | - public function __construct( $entity_service, $configuration_service, $entity_uri_service ) { |
|
| 94 | - |
|
| 95 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 96 | - |
|
| 97 | - $this->entity_service = $entity_service; |
|
| 98 | - $this->configuration_service = $configuration_service; |
|
| 99 | - $this->entity_uri_service = $entity_uri_service; |
|
| 100 | - |
|
| 101 | - self::$instance = $this; |
|
| 102 | - |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 107 | - * |
|
| 108 | - * @since 3.14.2 |
|
| 109 | - * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 110 | - */ |
|
| 111 | - public static function get_instance() { |
|
| 112 | - |
|
| 113 | - return self::$instance; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Mangle the content by adding links to the entity pages. This function is |
|
| 118 | - * hooked to the 'the_content' WP's filter. |
|
| 119 | - * |
|
| 120 | - * @since 3.8.0 |
|
| 121 | - * |
|
| 122 | - * @param string $content The content being filtered. |
|
| 123 | - * |
|
| 124 | - * @return string The filtered content. |
|
| 125 | - */ |
|
| 126 | - public function the_content( $content ) { |
|
| 127 | - |
|
| 128 | - $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 129 | - |
|
| 130 | - // Links should be added only on the front end and not for RSS. |
|
| 131 | - if ( is_feed() ) { |
|
| 132 | - return $content; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // Preload the `link by default` setting. |
|
| 136 | - $this->is_link_by_default = $this->configuration_service->is_link_by_default(); |
|
| 137 | - |
|
| 138 | - // Reset the array of of entity post ids linked from the post content. |
|
| 139 | - // This is used to avoid linking more the once the same post. |
|
| 140 | - $this->entity_post_ids_linked_from_post_content = array(); |
|
| 141 | - |
|
| 142 | - // Preload URIs. |
|
| 143 | - $matches = array(); |
|
| 144 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 145 | - |
|
| 146 | - // Bail out if there are no URIs. |
|
| 147 | - if ( empty( $matches[3] ) ) { |
|
| 148 | - return $content; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - // Preload the URIs. |
|
| 152 | - $this->entity_uri_service->preload_uris( $matches[3] ); |
|
| 153 | - |
|
| 154 | - // Replace each match of the entity tag with the entity link. If an error |
|
| 155 | - // occurs fail silently returning the original content. |
|
| 156 | - $result = preg_replace_callback( self::PATTERN, array( |
|
| 157 | - $this, |
|
| 158 | - 'link', |
|
| 159 | - ), $content ) ?: $content; |
|
| 160 | - |
|
| 161 | - $this->entity_uri_service->reset_uris(); |
|
| 162 | - |
|
| 163 | - return $result; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Get the entity match and replace it with a page link. |
|
| 168 | - * |
|
| 169 | - * @since 3.8.0 |
|
| 170 | - * |
|
| 171 | - * @param array $matches An array of matches. |
|
| 172 | - * |
|
| 173 | - * @return string The replaced text with the link to the entity page. |
|
| 174 | - */ |
|
| 175 | - private function link( $matches ) { |
|
| 176 | - |
|
| 177 | - // Get the entity itemid URI and label. |
|
| 178 | - $css_class = $matches[2]; |
|
| 179 | - $uri = $matches[3]; |
|
| 180 | - $label = $matches[4]; |
|
| 181 | - |
|
| 182 | - // Get the entity post by URI. |
|
| 183 | - $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 184 | - |
|
| 185 | - // @todo: revise the `test-content-filter-service.php` before switching |
|
| 186 | - // to the `entity_uri_service`. This is required, because the test injects |
|
| 187 | - // itself as `entity_service` to mock the requests to get a post by |
|
| 188 | - // entity uri. |
|
| 189 | - // |
|
| 190 | - // $post = $this->entity_uri_service->get_entity( $uri ); |
|
| 191 | - |
|
| 192 | - if ( null === $post ) { |
|
| 193 | - |
|
| 194 | - // If the entity post is not found return the label w/o the markup |
|
| 195 | - // around it. |
|
| 196 | - // |
|
| 197 | - // See https://github.com/insideout10/wordlift-plugin/issues/461. |
|
| 198 | - return $label; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ) |
|
| 202 | - // Do not link if already linked. |
|
| 203 | - || in_array( $post->ID, $this->entity_post_ids_linked_from_post_content ); |
|
| 204 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 205 | - |
|
| 206 | - // Don't link if links are disabled and the entity is not link or the |
|
| 207 | - // entity is do not link. |
|
| 208 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 209 | - |
|
| 210 | - // Return the label if it's don't link. |
|
| 211 | - if ( $dont_link ) { |
|
| 212 | - return $label; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - // Add the entity post id to the array of already linked entities, so that |
|
| 216 | - // only the first entity occurrence is linked. |
|
| 217 | - $this->entity_post_ids_linked_from_post_content[] = $post->ID; |
|
| 218 | - |
|
| 219 | - // Get the link. |
|
| 220 | - $href = get_permalink( $post ); |
|
| 221 | - |
|
| 222 | - // Get an alternative title attribute. |
|
| 223 | - $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 224 | - |
|
| 225 | - // Return the link. |
|
| 226 | - return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Get a `title` attribute with an alternative label for the link. |
|
| 231 | - * |
|
| 232 | - * If an alternative title isn't available an empty string is returned. |
|
| 233 | - * |
|
| 234 | - * @since 3.15.0 |
|
| 235 | - * |
|
| 236 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 237 | - * @param string $label The main link label. |
|
| 238 | - * |
|
| 239 | - * @return string A `title` attribute with an alternative label or an empty |
|
| 240 | - * string if none available. |
|
| 241 | - */ |
|
| 242 | - private function get_title_attribute( $post_id, $label ) { |
|
| 243 | - |
|
| 244 | - // Get an alternative title. |
|
| 245 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 246 | - if ( ! empty( $title ) ) { |
|
| 247 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - return ''; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Get a string to be used as a title attribute in links to a post |
|
| 255 | - * |
|
| 256 | - * @since 3.15.0 |
|
| 257 | - * |
|
| 258 | - * @param int $post_id The post id of the post being linked. |
|
| 259 | - * @param string $ignore_label A label to ignore. |
|
| 260 | - * |
|
| 261 | - * @return string The title to be used in the link. An empty string when |
|
| 262 | - * there is no alternative that is not the $ignore_label. |
|
| 263 | - */ |
|
| 264 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 265 | - |
|
| 266 | - // Get possible alternative labels we can select from. |
|
| 267 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 268 | - |
|
| 269 | - /* |
|
| 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 | + private $entity_post_ids_linked_from_post_content = array(); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 59 | + * |
|
| 60 | + * @since 3.16.3 |
|
| 61 | + * @access private |
|
| 62 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 63 | + */ |
|
| 64 | + private $entity_uri_service; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * A {@link Wordlift_Log_Service} instance. |
|
| 68 | + * |
|
| 69 | + * @since 3.16.0 |
|
| 70 | + * |
|
| 71 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 72 | + */ |
|
| 73 | + private $log; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 77 | + * |
|
| 78 | + * @since 3.14.2 |
|
| 79 | + * @access private |
|
| 80 | + * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 81 | + */ |
|
| 82 | + private static $instance; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 86 | + * |
|
| 87 | + * @since 3.8.0 |
|
| 88 | + * |
|
| 89 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 90 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 91 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 92 | + */ |
|
| 93 | + public function __construct( $entity_service, $configuration_service, $entity_uri_service ) { |
|
| 94 | + |
|
| 95 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 96 | + |
|
| 97 | + $this->entity_service = $entity_service; |
|
| 98 | + $this->configuration_service = $configuration_service; |
|
| 99 | + $this->entity_uri_service = $entity_uri_service; |
|
| 100 | + |
|
| 101 | + self::$instance = $this; |
|
| 102 | + |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 107 | + * |
|
| 108 | + * @since 3.14.2 |
|
| 109 | + * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 110 | + */ |
|
| 111 | + public static function get_instance() { |
|
| 112 | + |
|
| 113 | + return self::$instance; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Mangle the content by adding links to the entity pages. This function is |
|
| 118 | + * hooked to the 'the_content' WP's filter. |
|
| 119 | + * |
|
| 120 | + * @since 3.8.0 |
|
| 121 | + * |
|
| 122 | + * @param string $content The content being filtered. |
|
| 123 | + * |
|
| 124 | + * @return string The filtered content. |
|
| 125 | + */ |
|
| 126 | + public function the_content( $content ) { |
|
| 127 | + |
|
| 128 | + $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 129 | + |
|
| 130 | + // Links should be added only on the front end and not for RSS. |
|
| 131 | + if ( is_feed() ) { |
|
| 132 | + return $content; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // Preload the `link by default` setting. |
|
| 136 | + $this->is_link_by_default = $this->configuration_service->is_link_by_default(); |
|
| 137 | + |
|
| 138 | + // Reset the array of of entity post ids linked from the post content. |
|
| 139 | + // This is used to avoid linking more the once the same post. |
|
| 140 | + $this->entity_post_ids_linked_from_post_content = array(); |
|
| 141 | + |
|
| 142 | + // Preload URIs. |
|
| 143 | + $matches = array(); |
|
| 144 | + preg_match_all( self::PATTERN, $content, $matches ); |
|
| 145 | + |
|
| 146 | + // Bail out if there are no URIs. |
|
| 147 | + if ( empty( $matches[3] ) ) { |
|
| 148 | + return $content; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + // Preload the URIs. |
|
| 152 | + $this->entity_uri_service->preload_uris( $matches[3] ); |
|
| 153 | + |
|
| 154 | + // Replace each match of the entity tag with the entity link. If an error |
|
| 155 | + // occurs fail silently returning the original content. |
|
| 156 | + $result = preg_replace_callback( self::PATTERN, array( |
|
| 157 | + $this, |
|
| 158 | + 'link', |
|
| 159 | + ), $content ) ?: $content; |
|
| 160 | + |
|
| 161 | + $this->entity_uri_service->reset_uris(); |
|
| 162 | + |
|
| 163 | + return $result; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Get the entity match and replace it with a page link. |
|
| 168 | + * |
|
| 169 | + * @since 3.8.0 |
|
| 170 | + * |
|
| 171 | + * @param array $matches An array of matches. |
|
| 172 | + * |
|
| 173 | + * @return string The replaced text with the link to the entity page. |
|
| 174 | + */ |
|
| 175 | + private function link( $matches ) { |
|
| 176 | + |
|
| 177 | + // Get the entity itemid URI and label. |
|
| 178 | + $css_class = $matches[2]; |
|
| 179 | + $uri = $matches[3]; |
|
| 180 | + $label = $matches[4]; |
|
| 181 | + |
|
| 182 | + // Get the entity post by URI. |
|
| 183 | + $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 184 | + |
|
| 185 | + // @todo: revise the `test-content-filter-service.php` before switching |
|
| 186 | + // to the `entity_uri_service`. This is required, because the test injects |
|
| 187 | + // itself as `entity_service` to mock the requests to get a post by |
|
| 188 | + // entity uri. |
|
| 189 | + // |
|
| 190 | + // $post = $this->entity_uri_service->get_entity( $uri ); |
|
| 191 | + |
|
| 192 | + if ( null === $post ) { |
|
| 193 | + |
|
| 194 | + // If the entity post is not found return the label w/o the markup |
|
| 195 | + // around it. |
|
| 196 | + // |
|
| 197 | + // See https://github.com/insideout10/wordlift-plugin/issues/461. |
|
| 198 | + return $label; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $no_link = - 1 < strpos( $css_class, 'wl-no-link' ) |
|
| 202 | + // Do not link if already linked. |
|
| 203 | + || in_array( $post->ID, $this->entity_post_ids_linked_from_post_content ); |
|
| 204 | + $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 205 | + |
|
| 206 | + // Don't link if links are disabled and the entity is not link or the |
|
| 207 | + // entity is do not link. |
|
| 208 | + $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 209 | + |
|
| 210 | + // Return the label if it's don't link. |
|
| 211 | + if ( $dont_link ) { |
|
| 212 | + return $label; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + // Add the entity post id to the array of already linked entities, so that |
|
| 216 | + // only the first entity occurrence is linked. |
|
| 217 | + $this->entity_post_ids_linked_from_post_content[] = $post->ID; |
|
| 218 | + |
|
| 219 | + // Get the link. |
|
| 220 | + $href = get_permalink( $post ); |
|
| 221 | + |
|
| 222 | + // Get an alternative title attribute. |
|
| 223 | + $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 224 | + |
|
| 225 | + // Return the link. |
|
| 226 | + return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Get a `title` attribute with an alternative label for the link. |
|
| 231 | + * |
|
| 232 | + * If an alternative title isn't available an empty string is returned. |
|
| 233 | + * |
|
| 234 | + * @since 3.15.0 |
|
| 235 | + * |
|
| 236 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 237 | + * @param string $label The main link label. |
|
| 238 | + * |
|
| 239 | + * @return string A `title` attribute with an alternative label or an empty |
|
| 240 | + * string if none available. |
|
| 241 | + */ |
|
| 242 | + private function get_title_attribute( $post_id, $label ) { |
|
| 243 | + |
|
| 244 | + // Get an alternative title. |
|
| 245 | + $title = $this->get_link_title( $post_id, $label ); |
|
| 246 | + if ( ! empty( $title ) ) { |
|
| 247 | + return 'title="' . esc_attr( $title ) . '"'; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + return ''; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Get a string to be used as a title attribute in links to a post |
|
| 255 | + * |
|
| 256 | + * @since 3.15.0 |
|
| 257 | + * |
|
| 258 | + * @param int $post_id The post id of the post being linked. |
|
| 259 | + * @param string $ignore_label A label to ignore. |
|
| 260 | + * |
|
| 261 | + * @return string The title to be used in the link. An empty string when |
|
| 262 | + * there is no alternative that is not the $ignore_label. |
|
| 263 | + */ |
|
| 264 | + function get_link_title( $post_id, $ignore_label ) { |
|
| 265 | + |
|
| 266 | + // Get possible alternative labels we can select from. |
|
| 267 | + $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 268 | + |
|
| 269 | + /* |
|
| 270 | 270 | * Since the original text might use an alternative label than the |
| 271 | 271 | * Entity title, add the title itself which is not returned by the api. |
| 272 | 272 | */ |
| 273 | - $labels[] = get_the_title( $post_id ); |
|
| 274 | - |
|
| 275 | - // Add some randomness to the label selection. |
|
| 276 | - shuffle( $labels ); |
|
| 277 | - |
|
| 278 | - // Select the first label which is not to be ignored. |
|
| 279 | - $title = ''; |
|
| 280 | - foreach ( $labels as $label ) { |
|
| 281 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 282 | - $title = $label; |
|
| 283 | - break; |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - return $title; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 292 | - * the provided content. |
|
| 293 | - * |
|
| 294 | - * @since 3.14.2 |
|
| 295 | - * |
|
| 296 | - * @param string $content The content. |
|
| 297 | - * |
|
| 298 | - * @return array An array of URIs. |
|
| 299 | - */ |
|
| 300 | - public function get_entity_uris( $content ) { |
|
| 301 | - |
|
| 302 | - $matches = array(); |
|
| 303 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 304 | - |
|
| 305 | - // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 306 | - // to turn it into an object (since if the 3rd match isn't found the index |
|
| 307 | - // is not sequential. |
|
| 308 | - // |
|
| 309 | - // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 310 | - return array_values( array_unique( $matches[3] ) ); |
|
| 311 | - } |
|
| 273 | + $labels[] = get_the_title( $post_id ); |
|
| 274 | + |
|
| 275 | + // Add some randomness to the label selection. |
|
| 276 | + shuffle( $labels ); |
|
| 277 | + |
|
| 278 | + // Select the first label which is not to be ignored. |
|
| 279 | + $title = ''; |
|
| 280 | + foreach ( $labels as $label ) { |
|
| 281 | + if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 282 | + $title = $label; |
|
| 283 | + break; |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + return $title; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 292 | + * the provided content. |
|
| 293 | + * |
|
| 294 | + * @since 3.14.2 |
|
| 295 | + * |
|
| 296 | + * @param string $content The content. |
|
| 297 | + * |
|
| 298 | + * @return array An array of URIs. |
|
| 299 | + */ |
|
| 300 | + public function get_entity_uris( $content ) { |
|
| 301 | + |
|
| 302 | + $matches = array(); |
|
| 303 | + preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 304 | + |
|
| 305 | + // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 306 | + // to turn it into an object (since if the 3rd match isn't found the index |
|
| 307 | + // is not sequential. |
|
| 308 | + // |
|
| 309 | + // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 310 | + return array_values( array_unique( $matches[3] ) ); |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | 313 | } |
@@ -90,9 +90,9 @@ discard block |
||
| 90 | 90 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 91 | 91 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
| 92 | 92 | */ |
| 93 | - public function __construct( $entity_service, $configuration_service, $entity_uri_service ) { |
|
| 93 | + public function __construct($entity_service, $configuration_service, $entity_uri_service) { |
|
| 94 | 94 | |
| 95 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 95 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 96 | 96 | |
| 97 | 97 | $this->entity_service = $entity_service; |
| 98 | 98 | $this->configuration_service = $configuration_service; |
@@ -123,12 +123,12 @@ discard block |
||
| 123 | 123 | * |
| 124 | 124 | * @return string The filtered content. |
| 125 | 125 | */ |
| 126 | - public function the_content( $content ) { |
|
| 126 | + public function the_content($content) { |
|
| 127 | 127 | |
| 128 | - $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 128 | + $this->log->trace("Filtering content [ ".(is_singular() ? 'yes' : 'no')." ]..."); |
|
| 129 | 129 | |
| 130 | 130 | // Links should be added only on the front end and not for RSS. |
| 131 | - if ( is_feed() ) { |
|
| 131 | + if (is_feed()) { |
|
| 132 | 132 | return $content; |
| 133 | 133 | } |
| 134 | 134 | |
@@ -141,22 +141,22 @@ discard block |
||
| 141 | 141 | |
| 142 | 142 | // Preload URIs. |
| 143 | 143 | $matches = array(); |
| 144 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 144 | + preg_match_all(self::PATTERN, $content, $matches); |
|
| 145 | 145 | |
| 146 | 146 | // Bail out if there are no URIs. |
| 147 | - if ( empty( $matches[3] ) ) { |
|
| 147 | + if (empty($matches[3])) { |
|
| 148 | 148 | return $content; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | // Preload the URIs. |
| 152 | - $this->entity_uri_service->preload_uris( $matches[3] ); |
|
| 152 | + $this->entity_uri_service->preload_uris($matches[3]); |
|
| 153 | 153 | |
| 154 | 154 | // Replace each match of the entity tag with the entity link. If an error |
| 155 | 155 | // occurs fail silently returning the original content. |
| 156 | - $result = preg_replace_callback( self::PATTERN, array( |
|
| 156 | + $result = preg_replace_callback(self::PATTERN, array( |
|
| 157 | 157 | $this, |
| 158 | 158 | 'link', |
| 159 | - ), $content ) ?: $content; |
|
| 159 | + ), $content) ?: $content; |
|
| 160 | 160 | |
| 161 | 161 | $this->entity_uri_service->reset_uris(); |
| 162 | 162 | |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * |
| 173 | 173 | * @return string The replaced text with the link to the entity page. |
| 174 | 174 | */ |
| 175 | - private function link( $matches ) { |
|
| 175 | + private function link($matches) { |
|
| 176 | 176 | |
| 177 | 177 | // Get the entity itemid URI and label. |
| 178 | 178 | $css_class = $matches[2]; |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | $label = $matches[4]; |
| 181 | 181 | |
| 182 | 182 | // Get the entity post by URI. |
| 183 | - $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 183 | + $post = $this->entity_service->get_entity_post_by_uri($uri); |
|
| 184 | 184 | |
| 185 | 185 | // @todo: revise the `test-content-filter-service.php` before switching |
| 186 | 186 | // to the `entity_uri_service`. This is required, because the test injects |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | // |
| 190 | 190 | // $post = $this->entity_uri_service->get_entity( $uri ); |
| 191 | 191 | |
| 192 | - if ( null === $post ) { |
|
| 192 | + if (null === $post) { |
|
| 193 | 193 | |
| 194 | 194 | // If the entity post is not found return the label w/o the markup |
| 195 | 195 | // around it. |
@@ -198,17 +198,17 @@ discard block |
||
| 198 | 198 | return $label; |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ) |
|
| 201 | + $no_link = - 1 < strpos($css_class, 'wl-no-link') |
|
| 202 | 202 | // Do not link if already linked. |
| 203 | - || in_array( $post->ID, $this->entity_post_ids_linked_from_post_content ); |
|
| 204 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 203 | + || in_array($post->ID, $this->entity_post_ids_linked_from_post_content); |
|
| 204 | + $link = - 1 < strpos($css_class, 'wl-link'); |
|
| 205 | 205 | |
| 206 | 206 | // Don't link if links are disabled and the entity is not link or the |
| 207 | 207 | // entity is do not link. |
| 208 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 208 | + $dont_link = ( ! $this->is_link_by_default && ! $link) || $no_link; |
|
| 209 | 209 | |
| 210 | 210 | // Return the label if it's don't link. |
| 211 | - if ( $dont_link ) { |
|
| 211 | + if ($dont_link) { |
|
| 212 | 212 | return $label; |
| 213 | 213 | } |
| 214 | 214 | |
@@ -217,10 +217,10 @@ discard block |
||
| 217 | 217 | $this->entity_post_ids_linked_from_post_content[] = $post->ID; |
| 218 | 218 | |
| 219 | 219 | // Get the link. |
| 220 | - $href = get_permalink( $post ); |
|
| 220 | + $href = get_permalink($post); |
|
| 221 | 221 | |
| 222 | 222 | // Get an alternative title attribute. |
| 223 | - $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 223 | + $title_attribute = $this->get_title_attribute($post->ID, $label); |
|
| 224 | 224 | |
| 225 | 225 | // Return the link. |
| 226 | 226 | return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
@@ -239,12 +239,12 @@ discard block |
||
| 239 | 239 | * @return string A `title` attribute with an alternative label or an empty |
| 240 | 240 | * string if none available. |
| 241 | 241 | */ |
| 242 | - private function get_title_attribute( $post_id, $label ) { |
|
| 242 | + private function get_title_attribute($post_id, $label) { |
|
| 243 | 243 | |
| 244 | 244 | // Get an alternative title. |
| 245 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 246 | - if ( ! empty( $title ) ) { |
|
| 247 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 245 | + $title = $this->get_link_title($post_id, $label); |
|
| 246 | + if ( ! empty($title)) { |
|
| 247 | + return 'title="'.esc_attr($title).'"'; |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | return ''; |
@@ -261,24 +261,24 @@ discard block |
||
| 261 | 261 | * @return string The title to be used in the link. An empty string when |
| 262 | 262 | * there is no alternative that is not the $ignore_label. |
| 263 | 263 | */ |
| 264 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 264 | + function get_link_title($post_id, $ignore_label) { |
|
| 265 | 265 | |
| 266 | 266 | // Get possible alternative labels we can select from. |
| 267 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 267 | + $labels = $this->entity_service->get_alternative_labels($post_id); |
|
| 268 | 268 | |
| 269 | 269 | /* |
| 270 | 270 | * Since the original text might use an alternative label than the |
| 271 | 271 | * Entity title, add the title itself which is not returned by the api. |
| 272 | 272 | */ |
| 273 | - $labels[] = get_the_title( $post_id ); |
|
| 273 | + $labels[] = get_the_title($post_id); |
|
| 274 | 274 | |
| 275 | 275 | // Add some randomness to the label selection. |
| 276 | - shuffle( $labels ); |
|
| 276 | + shuffle($labels); |
|
| 277 | 277 | |
| 278 | 278 | // Select the first label which is not to be ignored. |
| 279 | 279 | $title = ''; |
| 280 | - foreach ( $labels as $label ) { |
|
| 281 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 280 | + foreach ($labels as $label) { |
|
| 281 | + if (0 !== strcasecmp($label, $ignore_label)) { |
|
| 282 | 282 | $title = $label; |
| 283 | 283 | break; |
| 284 | 284 | } |
@@ -297,17 +297,17 @@ discard block |
||
| 297 | 297 | * |
| 298 | 298 | * @return array An array of URIs. |
| 299 | 299 | */ |
| 300 | - public function get_entity_uris( $content ) { |
|
| 300 | + public function get_entity_uris($content) { |
|
| 301 | 301 | |
| 302 | 302 | $matches = array(); |
| 303 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 303 | + preg_match_all(Wordlift_Content_Filter_Service::PATTERN, $content, $matches); |
|
| 304 | 304 | |
| 305 | 305 | // We need to use `array_values` here in order to avoid further `json_encode` |
| 306 | 306 | // to turn it into an object (since if the 3rd match isn't found the index |
| 307 | 307 | // is not sequential. |
| 308 | 308 | // |
| 309 | 309 | // See https://github.com/insideout10/wordlift-plugin/issues/646. |
| 310 | - return array_values( array_unique( $matches[3] ) ); |
|
| 310 | + return array_values(array_unique($matches[3])); |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | } |
@@ -13,163 +13,163 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Entity_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * A {@link Wordlift_Property_Getter} instance. |
|
| 18 | - * |
|
| 19 | - * @since 3.8.0 |
|
| 20 | - * @access private |
|
| 21 | - * @var \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance. |
|
| 22 | - */ |
|
| 23 | - private $property_getter; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Wordlift_Entity_To_Jsonld_Converter constructor. |
|
| 27 | - * |
|
| 28 | - * @since 3.8.0 |
|
| 29 | - * |
|
| 30 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 31 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 32 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 33 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 34 | - * @param \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance. |
|
| 35 | - */ |
|
| 36 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $property_getter ) { |
|
| 37 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 38 | - |
|
| 39 | - $this->property_getter = $property_getter; |
|
| 40 | - |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 45 | - * found while processing the post is set in the $references array. |
|
| 46 | - * |
|
| 47 | - * @since 3.8.0 |
|
| 48 | - * |
|
| 49 | - * @param int $post_id The {@link WP_Post} id. |
|
| 50 | - * |
|
| 51 | - * @param array $references An array of entity references. |
|
| 52 | - * |
|
| 53 | - * @return array A JSON-LD array. |
|
| 54 | - */ |
|
| 55 | - public function convert( $post_id, &$references = array() ) { |
|
| 56 | - |
|
| 57 | - // Get the post instance. |
|
| 58 | - $post = get_post( $post_id ); |
|
| 59 | - if ( null === $post ) { |
|
| 60 | - // Post not found. |
|
| 61 | - return null; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 65 | - $jsonld = parent::convert( $post_id, $references ); |
|
| 66 | - |
|
| 67 | - // Get the entity name. |
|
| 68 | - $jsonld['name'] = $post->post_title; |
|
| 69 | - |
|
| 70 | - // 3.13.0, add alternate names. |
|
| 71 | - $alternative_labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 72 | - if ( 0 < count( $alternative_labels ) ) { |
|
| 73 | - $jsonld['alternateName'] = $alternative_labels; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - // Get the entity @type. |
|
| 77 | - $type = $this->entity_type_service->get( $post_id ); |
|
| 78 | - |
|
| 79 | - // Get the configured type custom fields. |
|
| 80 | - $fields = $type['custom_fields']; |
|
| 81 | - |
|
| 82 | - // Set a reference to use in closures. |
|
| 83 | - $converter = $this; |
|
| 84 | - |
|
| 85 | - // Try each field on the entity. |
|
| 86 | - foreach ( $fields as $key => $value ) { |
|
| 87 | - |
|
| 88 | - // Get the predicate. |
|
| 89 | - $name = $this->relative_to_context( $value['predicate'] ); |
|
| 90 | - |
|
| 91 | - // Get the value, the property service will get the right extractor |
|
| 92 | - // for that property. |
|
| 93 | - $value = $this->property_getter->get( $post->ID, $key ); |
|
| 94 | - |
|
| 95 | - if ( empty( $value ) ) { |
|
| 96 | - continue; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // Map the value to the property name. |
|
| 100 | - // If we got an array with just one value, we return that one value. |
|
| 101 | - // If we got a Wordlift_Property_Entity_Reference we get the URL. |
|
| 102 | - $jsonld[ $name ] = $this->make_one( array_map( function ( $item ) use ( $converter, &$references ) { |
|
| 103 | - |
|
| 104 | - if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
| 105 | - |
|
| 106 | - $url = $item->getURL(); |
|
| 107 | - |
|
| 108 | - // The refactored converters require the entity id. |
|
| 109 | - $references[] = $item->getID(); |
|
| 110 | - |
|
| 111 | - return array( |
|
| 112 | - '@id' => $url, |
|
| 113 | - ); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return $converter->relative_to_context( $item ); |
|
| 117 | - }, $value ) ); |
|
| 118 | - |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - return $this->post_process( $jsonld ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * If the provided array of values contains only one value, then one single |
|
| 126 | - * value is returned, otherwise the original array is returned. |
|
| 127 | - * |
|
| 128 | - * @since 3.8.0 |
|
| 129 | - * @access private |
|
| 130 | - * |
|
| 131 | - * @param array $value An array of values. |
|
| 132 | - * |
|
| 133 | - * @return mixed|array A single value or the original array. |
|
| 134 | - */ |
|
| 135 | - private function make_one( $value ) { |
|
| 136 | - |
|
| 137 | - return 1 === count( $value ) ? $value[0] : $value; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Post process the generated JSON to reorganize values which are stored as 1st |
|
| 142 | - * level in WP but are really 2nd level. |
|
| 143 | - * |
|
| 144 | - * @since 3.8.0 |
|
| 145 | - * |
|
| 146 | - * @param array $jsonld An array of JSON-LD properties and values. |
|
| 147 | - * |
|
| 148 | - * @return array The array remapped. |
|
| 149 | - */ |
|
| 150 | - private function post_process( $jsonld ) { |
|
| 151 | - |
|
| 152 | - foreach ( $jsonld as $key => $value ) { |
|
| 153 | - if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) { |
|
| 154 | - $jsonld['address']['@type'] = 'PostalAddress'; |
|
| 155 | - $jsonld['address'][ $key ] = $value; |
|
| 156 | - unset( $jsonld[ $key ] ); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - if ( 'latitude' === $key || 'longitude' === $key ) { |
|
| 160 | - $jsonld['geo']['@type'] = 'GeoCoordinates'; |
|
| 161 | - $jsonld['geo'][ $key ] = $value; |
|
| 162 | - unset( $jsonld[ $key ] ); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - if ( 'calories' === $key ) { |
|
| 166 | - $jsonld['nutrition']['@type'] = 'NutritionInformation'; |
|
| 167 | - $jsonld['nutrition'][ $key ] = $value; |
|
| 168 | - unset( $jsonld[ $key ] ); |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - return $jsonld; |
|
| 173 | - } |
|
| 16 | + /** |
|
| 17 | + * A {@link Wordlift_Property_Getter} instance. |
|
| 18 | + * |
|
| 19 | + * @since 3.8.0 |
|
| 20 | + * @access private |
|
| 21 | + * @var \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance. |
|
| 22 | + */ |
|
| 23 | + private $property_getter; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Wordlift_Entity_To_Jsonld_Converter constructor. |
|
| 27 | + * |
|
| 28 | + * @since 3.8.0 |
|
| 29 | + * |
|
| 30 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 31 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 32 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 33 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 34 | + * @param \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance. |
|
| 35 | + */ |
|
| 36 | + public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $property_getter ) { |
|
| 37 | + parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 38 | + |
|
| 39 | + $this->property_getter = $property_getter; |
|
| 40 | + |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 45 | + * found while processing the post is set in the $references array. |
|
| 46 | + * |
|
| 47 | + * @since 3.8.0 |
|
| 48 | + * |
|
| 49 | + * @param int $post_id The {@link WP_Post} id. |
|
| 50 | + * |
|
| 51 | + * @param array $references An array of entity references. |
|
| 52 | + * |
|
| 53 | + * @return array A JSON-LD array. |
|
| 54 | + */ |
|
| 55 | + public function convert( $post_id, &$references = array() ) { |
|
| 56 | + |
|
| 57 | + // Get the post instance. |
|
| 58 | + $post = get_post( $post_id ); |
|
| 59 | + if ( null === $post ) { |
|
| 60 | + // Post not found. |
|
| 61 | + return null; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 65 | + $jsonld = parent::convert( $post_id, $references ); |
|
| 66 | + |
|
| 67 | + // Get the entity name. |
|
| 68 | + $jsonld['name'] = $post->post_title; |
|
| 69 | + |
|
| 70 | + // 3.13.0, add alternate names. |
|
| 71 | + $alternative_labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 72 | + if ( 0 < count( $alternative_labels ) ) { |
|
| 73 | + $jsonld['alternateName'] = $alternative_labels; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + // Get the entity @type. |
|
| 77 | + $type = $this->entity_type_service->get( $post_id ); |
|
| 78 | + |
|
| 79 | + // Get the configured type custom fields. |
|
| 80 | + $fields = $type['custom_fields']; |
|
| 81 | + |
|
| 82 | + // Set a reference to use in closures. |
|
| 83 | + $converter = $this; |
|
| 84 | + |
|
| 85 | + // Try each field on the entity. |
|
| 86 | + foreach ( $fields as $key => $value ) { |
|
| 87 | + |
|
| 88 | + // Get the predicate. |
|
| 89 | + $name = $this->relative_to_context( $value['predicate'] ); |
|
| 90 | + |
|
| 91 | + // Get the value, the property service will get the right extractor |
|
| 92 | + // for that property. |
|
| 93 | + $value = $this->property_getter->get( $post->ID, $key ); |
|
| 94 | + |
|
| 95 | + if ( empty( $value ) ) { |
|
| 96 | + continue; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // Map the value to the property name. |
|
| 100 | + // If we got an array with just one value, we return that one value. |
|
| 101 | + // If we got a Wordlift_Property_Entity_Reference we get the URL. |
|
| 102 | + $jsonld[ $name ] = $this->make_one( array_map( function ( $item ) use ( $converter, &$references ) { |
|
| 103 | + |
|
| 104 | + if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
| 105 | + |
|
| 106 | + $url = $item->getURL(); |
|
| 107 | + |
|
| 108 | + // The refactored converters require the entity id. |
|
| 109 | + $references[] = $item->getID(); |
|
| 110 | + |
|
| 111 | + return array( |
|
| 112 | + '@id' => $url, |
|
| 113 | + ); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return $converter->relative_to_context( $item ); |
|
| 117 | + }, $value ) ); |
|
| 118 | + |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + return $this->post_process( $jsonld ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * If the provided array of values contains only one value, then one single |
|
| 126 | + * value is returned, otherwise the original array is returned. |
|
| 127 | + * |
|
| 128 | + * @since 3.8.0 |
|
| 129 | + * @access private |
|
| 130 | + * |
|
| 131 | + * @param array $value An array of values. |
|
| 132 | + * |
|
| 133 | + * @return mixed|array A single value or the original array. |
|
| 134 | + */ |
|
| 135 | + private function make_one( $value ) { |
|
| 136 | + |
|
| 137 | + return 1 === count( $value ) ? $value[0] : $value; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Post process the generated JSON to reorganize values which are stored as 1st |
|
| 142 | + * level in WP but are really 2nd level. |
|
| 143 | + * |
|
| 144 | + * @since 3.8.0 |
|
| 145 | + * |
|
| 146 | + * @param array $jsonld An array of JSON-LD properties and values. |
|
| 147 | + * |
|
| 148 | + * @return array The array remapped. |
|
| 149 | + */ |
|
| 150 | + private function post_process( $jsonld ) { |
|
| 151 | + |
|
| 152 | + foreach ( $jsonld as $key => $value ) { |
|
| 153 | + if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) { |
|
| 154 | + $jsonld['address']['@type'] = 'PostalAddress'; |
|
| 155 | + $jsonld['address'][ $key ] = $value; |
|
| 156 | + unset( $jsonld[ $key ] ); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + if ( 'latitude' === $key || 'longitude' === $key ) { |
|
| 160 | + $jsonld['geo']['@type'] = 'GeoCoordinates'; |
|
| 161 | + $jsonld['geo'][ $key ] = $value; |
|
| 162 | + unset( $jsonld[ $key ] ); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + if ( 'calories' === $key ) { |
|
| 166 | + $jsonld['nutrition']['@type'] = 'NutritionInformation'; |
|
| 167 | + $jsonld['nutrition'][ $key ] = $value; |
|
| 168 | + unset( $jsonld[ $key ] ); |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + return $jsonld; |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | 175 | } |
@@ -33,8 +33,8 @@ discard block |
||
| 33 | 33 | * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
| 34 | 34 | * @param \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance. |
| 35 | 35 | */ |
| 36 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $property_getter ) { |
|
| 37 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 36 | + public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service, $property_getter) { |
|
| 37 | + parent::__construct($entity_type_service, $entity_service, $user_service, $attachment_service); |
|
| 38 | 38 | |
| 39 | 39 | $this->property_getter = $property_getter; |
| 40 | 40 | |
@@ -52,29 +52,29 @@ discard block |
||
| 52 | 52 | * |
| 53 | 53 | * @return array A JSON-LD array. |
| 54 | 54 | */ |
| 55 | - public function convert( $post_id, &$references = array() ) { |
|
| 55 | + public function convert($post_id, &$references = array()) { |
|
| 56 | 56 | |
| 57 | 57 | // Get the post instance. |
| 58 | - $post = get_post( $post_id ); |
|
| 59 | - if ( null === $post ) { |
|
| 58 | + $post = get_post($post_id); |
|
| 59 | + if (null === $post) { |
|
| 60 | 60 | // Post not found. |
| 61 | 61 | return null; |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | // Get the base JSON-LD and the list of entities referenced by this entity. |
| 65 | - $jsonld = parent::convert( $post_id, $references ); |
|
| 65 | + $jsonld = parent::convert($post_id, $references); |
|
| 66 | 66 | |
| 67 | 67 | // Get the entity name. |
| 68 | 68 | $jsonld['name'] = $post->post_title; |
| 69 | 69 | |
| 70 | 70 | // 3.13.0, add alternate names. |
| 71 | - $alternative_labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 72 | - if ( 0 < count( $alternative_labels ) ) { |
|
| 71 | + $alternative_labels = $this->entity_service->get_alternative_labels($post_id); |
|
| 72 | + if (0 < count($alternative_labels)) { |
|
| 73 | 73 | $jsonld['alternateName'] = $alternative_labels; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // Get the entity @type. |
| 77 | - $type = $this->entity_type_service->get( $post_id ); |
|
| 77 | + $type = $this->entity_type_service->get($post_id); |
|
| 78 | 78 | |
| 79 | 79 | // Get the configured type custom fields. |
| 80 | 80 | $fields = $type['custom_fields']; |
@@ -83,25 +83,25 @@ discard block |
||
| 83 | 83 | $converter = $this; |
| 84 | 84 | |
| 85 | 85 | // Try each field on the entity. |
| 86 | - foreach ( $fields as $key => $value ) { |
|
| 86 | + foreach ($fields as $key => $value) { |
|
| 87 | 87 | |
| 88 | 88 | // Get the predicate. |
| 89 | - $name = $this->relative_to_context( $value['predicate'] ); |
|
| 89 | + $name = $this->relative_to_context($value['predicate']); |
|
| 90 | 90 | |
| 91 | 91 | // Get the value, the property service will get the right extractor |
| 92 | 92 | // for that property. |
| 93 | - $value = $this->property_getter->get( $post->ID, $key ); |
|
| 93 | + $value = $this->property_getter->get($post->ID, $key); |
|
| 94 | 94 | |
| 95 | - if ( empty( $value ) ) { |
|
| 95 | + if (empty($value)) { |
|
| 96 | 96 | continue; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | // Map the value to the property name. |
| 100 | 100 | // If we got an array with just one value, we return that one value. |
| 101 | 101 | // If we got a Wordlift_Property_Entity_Reference we get the URL. |
| 102 | - $jsonld[ $name ] = $this->make_one( array_map( function ( $item ) use ( $converter, &$references ) { |
|
| 102 | + $jsonld[$name] = $this->make_one(array_map(function($item) use ($converter, &$references) { |
|
| 103 | 103 | |
| 104 | - if ( $item instanceof Wordlift_Property_Entity_Reference ) { |
|
| 104 | + if ($item instanceof Wordlift_Property_Entity_Reference) { |
|
| 105 | 105 | |
| 106 | 106 | $url = $item->getURL(); |
| 107 | 107 | |
@@ -113,12 +113,12 @@ discard block |
||
| 113 | 113 | ); |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - return $converter->relative_to_context( $item ); |
|
| 117 | - }, $value ) ); |
|
| 116 | + return $converter->relative_to_context($item); |
|
| 117 | + }, $value)); |
|
| 118 | 118 | |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - return $this->post_process( $jsonld ); |
|
| 121 | + return $this->post_process($jsonld); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | /** |
@@ -132,9 +132,9 @@ discard block |
||
| 132 | 132 | * |
| 133 | 133 | * @return mixed|array A single value or the original array. |
| 134 | 134 | */ |
| 135 | - private function make_one( $value ) { |
|
| 135 | + private function make_one($value) { |
|
| 136 | 136 | |
| 137 | - return 1 === count( $value ) ? $value[0] : $value; |
|
| 137 | + return 1 === count($value) ? $value[0] : $value; |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
@@ -147,25 +147,25 @@ discard block |
||
| 147 | 147 | * |
| 148 | 148 | * @return array The array remapped. |
| 149 | 149 | */ |
| 150 | - private function post_process( $jsonld ) { |
|
| 150 | + private function post_process($jsonld) { |
|
| 151 | 151 | |
| 152 | - foreach ( $jsonld as $key => $value ) { |
|
| 153 | - if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) { |
|
| 152 | + foreach ($jsonld as $key => $value) { |
|
| 153 | + if ('streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key) { |
|
| 154 | 154 | $jsonld['address']['@type'] = 'PostalAddress'; |
| 155 | - $jsonld['address'][ $key ] = $value; |
|
| 156 | - unset( $jsonld[ $key ] ); |
|
| 155 | + $jsonld['address'][$key] = $value; |
|
| 156 | + unset($jsonld[$key]); |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - if ( 'latitude' === $key || 'longitude' === $key ) { |
|
| 159 | + if ('latitude' === $key || 'longitude' === $key) { |
|
| 160 | 160 | $jsonld['geo']['@type'] = 'GeoCoordinates'; |
| 161 | - $jsonld['geo'][ $key ] = $value; |
|
| 162 | - unset( $jsonld[ $key ] ); |
|
| 161 | + $jsonld['geo'][$key] = $value; |
|
| 162 | + unset($jsonld[$key]); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - if ( 'calories' === $key ) { |
|
| 165 | + if ('calories' === $key) { |
|
| 166 | 166 | $jsonld['nutrition']['@type'] = 'NutritionInformation'; |
| 167 | - $jsonld['nutrition'][ $key ] = $value; |
|
| 168 | - unset( $jsonld[ $key ] ); |
|
| 167 | + $jsonld['nutrition'][$key] = $value; |
|
| 168 | + unset($jsonld[$key]); |
|
| 169 | 169 | } |
| 170 | 170 | } |
| 171 | 171 | |
@@ -14,74 +14,74 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | function wl_shortcode_faceted_search( $atts ) { |
| 16 | 16 | |
| 17 | - // Extract attributes and set default values. |
|
| 18 | - $shortcode_atts = shortcode_atts( array( |
|
| 19 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 20 | - 'show_facets' => true, |
|
| 21 | - 'with_carousel' => true, |
|
| 22 | - 'squared_thumbs' => false, |
|
| 23 | - 'limit' => 20, |
|
| 24 | - |
|
| 25 | - ), $atts ); |
|
| 26 | - |
|
| 27 | - foreach ( |
|
| 28 | - array( |
|
| 29 | - 'show_facets', |
|
| 30 | - 'with_carousel', |
|
| 31 | - 'squared_thumbs', |
|
| 32 | - ) as $att |
|
| 33 | - ) { |
|
| 34 | - |
|
| 35 | - // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode. |
|
| 36 | - $shortcode_atts[ $att ] = filter_var( |
|
| 37 | - $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 38 | - ); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - // If the current post is not an entity and has no related entities |
|
| 42 | - // than the shortcode cannot be rendered |
|
| 43 | - // TODO Add an alert visibile only for connected admin users. |
|
| 44 | - $current_post = get_post(); |
|
| 45 | - |
|
| 46 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 47 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 48 | - array( $current_post->ID ) : |
|
| 49 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 50 | - |
|
| 51 | - // Bail if there are no entity ids. |
|
| 52 | - if ( 0 === count( $entity_ids ) ) { |
|
| 53 | - return ''; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - $div_id = 'wordlift-faceted-entity-search-widget'; |
|
| 57 | - |
|
| 58 | - wp_enqueue_style( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-faceted-entity-search-widget.min.css' ); |
|
| 59 | - wp_enqueue_script( 'angularjs', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular.min.js' ); |
|
| 60 | - wp_enqueue_script( 'angularjs-touch', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-touch.min.js' ); |
|
| 61 | - |
|
| 62 | - wp_enqueue_script( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-faceted-entity-search-widget.min.js' ); |
|
| 63 | - |
|
| 64 | - wp_localize_script( |
|
| 65 | - 'wordlift-faceted-search', |
|
| 66 | - 'wl_faceted_search_params', array( |
|
| 67 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 68 | - 'action' => 'wl_faceted_search', |
|
| 69 | - 'post_id' => $current_post->ID, |
|
| 70 | - 'entity_ids' => $entity_ids, |
|
| 71 | - 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 72 | - 'div_id' => $div_id, |
|
| 73 | - 'defaultThumbnailPath' => WL_DEFAULT_THUMBNAIL_PATH, |
|
| 74 | - 'attrs' => $shortcode_atts, |
|
| 75 | - 'l10n' => array( |
|
| 76 | - 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 77 | - 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 78 | - 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 79 | - 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 80 | - ), |
|
| 81 | - ) |
|
| 82 | - ); |
|
| 83 | - |
|
| 84 | - return '<div id="' . $div_id . '" style="width:100%"></div>'; |
|
| 17 | + // Extract attributes and set default values. |
|
| 18 | + $shortcode_atts = shortcode_atts( array( |
|
| 19 | + 'title' => __( 'Related articles', 'wordlift' ), |
|
| 20 | + 'show_facets' => true, |
|
| 21 | + 'with_carousel' => true, |
|
| 22 | + 'squared_thumbs' => false, |
|
| 23 | + 'limit' => 20, |
|
| 24 | + |
|
| 25 | + ), $atts ); |
|
| 26 | + |
|
| 27 | + foreach ( |
|
| 28 | + array( |
|
| 29 | + 'show_facets', |
|
| 30 | + 'with_carousel', |
|
| 31 | + 'squared_thumbs', |
|
| 32 | + ) as $att |
|
| 33 | + ) { |
|
| 34 | + |
|
| 35 | + // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode. |
|
| 36 | + $shortcode_atts[ $att ] = filter_var( |
|
| 37 | + $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 38 | + ); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + // If the current post is not an entity and has no related entities |
|
| 42 | + // than the shortcode cannot be rendered |
|
| 43 | + // TODO Add an alert visibile only for connected admin users. |
|
| 44 | + $current_post = get_post(); |
|
| 45 | + |
|
| 46 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 47 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 48 | + array( $current_post->ID ) : |
|
| 49 | + wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 50 | + |
|
| 51 | + // Bail if there are no entity ids. |
|
| 52 | + if ( 0 === count( $entity_ids ) ) { |
|
| 53 | + return ''; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + $div_id = 'wordlift-faceted-entity-search-widget'; |
|
| 57 | + |
|
| 58 | + wp_enqueue_style( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-faceted-entity-search-widget.min.css' ); |
|
| 59 | + wp_enqueue_script( 'angularjs', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular.min.js' ); |
|
| 60 | + wp_enqueue_script( 'angularjs-touch', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-touch.min.js' ); |
|
| 61 | + |
|
| 62 | + wp_enqueue_script( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-faceted-entity-search-widget.min.js' ); |
|
| 63 | + |
|
| 64 | + wp_localize_script( |
|
| 65 | + 'wordlift-faceted-search', |
|
| 66 | + 'wl_faceted_search_params', array( |
|
| 67 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 68 | + 'action' => 'wl_faceted_search', |
|
| 69 | + 'post_id' => $current_post->ID, |
|
| 70 | + 'entity_ids' => $entity_ids, |
|
| 71 | + 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 72 | + 'div_id' => $div_id, |
|
| 73 | + 'defaultThumbnailPath' => WL_DEFAULT_THUMBNAIL_PATH, |
|
| 74 | + 'attrs' => $shortcode_atts, |
|
| 75 | + 'l10n' => array( |
|
| 76 | + 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 77 | + 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 78 | + 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 79 | + 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 80 | + ), |
|
| 81 | + ) |
|
| 82 | + ); |
|
| 83 | + |
|
| 84 | + return '<div id="' . $div_id . '" style="width:100%"></div>'; |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | add_shortcode( 'wl_faceted_search', 'wl_shortcode_faceted_search' ); |
@@ -92,106 +92,106 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | function wl_shortcode_faceted_search_ajax( $http_raw_data = null ) { |
| 94 | 94 | |
| 95 | - // Post ID must be defined. |
|
| 96 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 97 | - wp_die( 'No post_id given' ); |
|
| 95 | + // Post ID must be defined. |
|
| 96 | + if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 97 | + wp_die( 'No post_id given' ); |
|
| 98 | 98 | |
| 99 | - return; |
|
| 100 | - } |
|
| 99 | + return; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - // Extract filtering conditions. |
|
| 103 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 104 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 102 | + // Extract filtering conditions. |
|
| 103 | + $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 104 | + $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 105 | 105 | |
| 106 | - $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 107 | - $current_post = get_post( $current_post_id ); |
|
| 106 | + $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
|
| 107 | + $current_post = get_post( $current_post_id ); |
|
| 108 | 108 | |
| 109 | - // Post ID has to match an existing item. |
|
| 110 | - if ( null === $current_post ) { |
|
| 111 | - wp_die( 'No valid post_id given' ); |
|
| 109 | + // Post ID has to match an existing item. |
|
| 110 | + if ( null === $current_post ) { |
|
| 111 | + wp_die( 'No valid post_id given' ); |
|
| 112 | 112 | |
| 113 | - return; |
|
| 114 | - } |
|
| 113 | + return; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - // If the current post is an entity, |
|
| 117 | - // the current post is used as main entity. |
|
| 118 | - // Otherwise, current post related entities are used. |
|
| 119 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 120 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 121 | - array( $current_post->ID ) : |
|
| 122 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 116 | + // If the current post is an entity, |
|
| 117 | + // the current post is used as main entity. |
|
| 118 | + // Otherwise, current post related entities are used. |
|
| 119 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 120 | + $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 121 | + array( $current_post->ID ) : |
|
| 122 | + wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 123 | 123 | |
| 124 | - // If there are no entities we cannot render the widget. |
|
| 125 | - if ( 0 === count( $entity_ids ) ) { |
|
| 126 | - wp_die( 'No entities available' ); |
|
| 124 | + // If there are no entities we cannot render the widget. |
|
| 125 | + if ( 0 === count( $entity_ids ) ) { |
|
| 126 | + wp_die( 'No entities available' ); |
|
| 127 | 127 | |
| 128 | - return; |
|
| 129 | - } |
|
| 128 | + return; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - // Retrieve requested type |
|
| 132 | - $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 131 | + // Retrieve requested type |
|
| 132 | + $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 133 | 133 | |
| 134 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 134 | + $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 135 | 135 | |
| 136 | - $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 137 | - $entity_ids, |
|
| 138 | - '*', |
|
| 139 | - null, |
|
| 140 | - 'publish', |
|
| 141 | - array( $current_post_id ), |
|
| 142 | - $limit |
|
| 143 | - ); |
|
| 136 | + $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 137 | + $entity_ids, |
|
| 138 | + '*', |
|
| 139 | + null, |
|
| 140 | + 'publish', |
|
| 141 | + array( $current_post_id ), |
|
| 142 | + $limit |
|
| 143 | + ); |
|
| 144 | 144 | |
| 145 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 146 | - return $p->ID; |
|
| 147 | - }, $referencing_posts ); |
|
| 148 | - $results = array(); |
|
| 145 | + $referencing_post_ids = array_map( function ( $p ) { |
|
| 146 | + return $p->ID; |
|
| 147 | + }, $referencing_posts ); |
|
| 148 | + $results = array(); |
|
| 149 | 149 | |
| 150 | - if ( 'posts' === $required_type ) { |
|
| 150 | + if ( 'posts' === $required_type ) { |
|
| 151 | 151 | |
| 152 | - // Required filtered posts. |
|
| 153 | - wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 152 | + // Required filtered posts. |
|
| 153 | + wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 154 | 154 | |
| 155 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 156 | - $referencing_posts : |
|
| 157 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 158 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 159 | - '*', |
|
| 160 | - null, |
|
| 161 | - null, |
|
| 162 | - array(), |
|
| 163 | - null, |
|
| 164 | - $referencing_post_ids |
|
| 165 | - ); |
|
| 155 | + $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 156 | + $referencing_posts : |
|
| 157 | + Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 158 | + wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 159 | + '*', |
|
| 160 | + null, |
|
| 161 | + null, |
|
| 162 | + array(), |
|
| 163 | + null, |
|
| 164 | + $referencing_post_ids |
|
| 165 | + ); |
|
| 166 | 166 | |
| 167 | - if ( $filtered_posts ) { |
|
| 168 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 167 | + if ( $filtered_posts ) { |
|
| 168 | + foreach ( $filtered_posts as $post_obj ) { |
|
| 169 | 169 | |
| 170 | - $thumbnail = wp_get_attachment_url( get_post_thumbnail_id( $post_obj->ID, 'thumbnail' ) ); |
|
| 171 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 172 | - $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 173 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 170 | + $thumbnail = wp_get_attachment_url( get_post_thumbnail_id( $post_obj->ID, 'thumbnail' ) ); |
|
| 171 | + $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 172 | + $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
|
| 173 | + $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 174 | 174 | |
| 175 | - $results[] = $post_obj; |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - } else { |
|
| 175 | + $results[] = $post_obj; |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + } else { |
|
| 179 | 179 | |
| 180 | - global $wpdb; |
|
| 180 | + global $wpdb; |
|
| 181 | 181 | |
| 182 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 182 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 183 | 183 | |
| 184 | - // Retrieve Wordlift relation instances table name. |
|
| 185 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 184 | + // Retrieve Wordlift relation instances table name. |
|
| 185 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 186 | 186 | |
| 187 | - /* |
|
| 187 | + /* |
|
| 188 | 188 | * Make sure we have some referenced post, otherwise the IN parts of |
| 189 | 189 | * the SQL will produce an SQL error. |
| 190 | 190 | */ |
| 191 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 192 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 191 | + if ( ! empty( $referencing_post_ids ) ) { |
|
| 192 | + $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 193 | 193 | |
| 194 | - $query = " |
|
| 194 | + $query = " |
|
| 195 | 195 | SELECT |
| 196 | 196 | object_id AS ID, |
| 197 | 197 | count( object_id ) AS counter |
@@ -203,29 +203,29 @@ discard block |
||
| 203 | 203 | LIMIT $limit; |
| 204 | 204 | "; |
| 205 | 205 | |
| 206 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 206 | + wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 207 | 207 | |
| 208 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 208 | + $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 209 | 209 | |
| 210 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 210 | + wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 211 | 211 | |
| 212 | - foreach ( $entities as $obj ) { |
|
| 212 | + foreach ( $entities as $obj ) { |
|
| 213 | 213 | |
| 214 | - $entity = get_post( $obj->ID ); |
|
| 215 | - // Ensure only valid and published entities are returned. |
|
| 216 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 214 | + $entity = get_post( $obj->ID ); |
|
| 215 | + // Ensure only valid and published entities are returned. |
|
| 216 | + if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 217 | 217 | |
| 218 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 219 | - $serialized_entity['counter'] = $obj->counter; |
|
| 220 | - $serialized_entity['createdAt'] = $entity->post_date; |
|
| 218 | + $serialized_entity = wl_serialize_entity( $entity ); |
|
| 219 | + $serialized_entity['counter'] = $obj->counter; |
|
| 220 | + $serialized_entity['createdAt'] = $entity->post_date; |
|
| 221 | 221 | |
| 222 | - $results[] = $serialized_entity; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - } |
|
| 222 | + $results[] = $serialized_entity; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - wl_core_send_json( $results ); |
|
| 228 | + wl_core_send_json( $results ); |
|
| 229 | 229 | |
| 230 | 230 | } |
| 231 | 231 | |
@@ -12,17 +12,17 @@ discard block |
||
| 12 | 12 | * |
| 13 | 13 | * @param array $atts Shortcode attributes. |
| 14 | 14 | */ |
| 15 | -function wl_shortcode_faceted_search( $atts ) { |
|
| 15 | +function wl_shortcode_faceted_search($atts) { |
|
| 16 | 16 | |
| 17 | 17 | // Extract attributes and set default values. |
| 18 | - $shortcode_atts = shortcode_atts( array( |
|
| 19 | - 'title' => __( 'Related articles', 'wordlift' ), |
|
| 18 | + $shortcode_atts = shortcode_atts(array( |
|
| 19 | + 'title' => __('Related articles', 'wordlift'), |
|
| 20 | 20 | 'show_facets' => true, |
| 21 | 21 | 'with_carousel' => true, |
| 22 | 22 | 'squared_thumbs' => false, |
| 23 | 23 | 'limit' => 20, |
| 24 | 24 | |
| 25 | - ), $atts ); |
|
| 25 | + ), $atts); |
|
| 26 | 26 | |
| 27 | 27 | foreach ( |
| 28 | 28 | array( |
@@ -33,8 +33,8 @@ discard block |
||
| 33 | 33 | ) { |
| 34 | 34 | |
| 35 | 35 | // See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode. |
| 36 | - $shortcode_atts[ $att ] = filter_var( |
|
| 37 | - $shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN |
|
| 36 | + $shortcode_atts[$att] = filter_var( |
|
| 37 | + $shortcode_atts[$att], FILTER_VALIDATE_BOOLEAN |
|
| 38 | 38 | ); |
| 39 | 39 | } |
| 40 | 40 | |
@@ -44,71 +44,70 @@ discard block |
||
| 44 | 44 | $current_post = get_post(); |
| 45 | 45 | |
| 46 | 46 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 47 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 48 | - array( $current_post->ID ) : |
|
| 49 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 47 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 48 | + array($current_post->ID) : wl_core_get_related_entity_ids($current_post->ID); |
|
| 50 | 49 | |
| 51 | 50 | // Bail if there are no entity ids. |
| 52 | - if ( 0 === count( $entity_ids ) ) { |
|
| 51 | + if (0 === count($entity_ids)) { |
|
| 53 | 52 | return ''; |
| 54 | 53 | } |
| 55 | 54 | |
| 56 | 55 | $div_id = 'wordlift-faceted-entity-search-widget'; |
| 57 | 56 | |
| 58 | - wp_enqueue_style( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-faceted-entity-search-widget.min.css' ); |
|
| 59 | - wp_enqueue_script( 'angularjs', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular.min.js' ); |
|
| 60 | - wp_enqueue_script( 'angularjs-touch', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-touch.min.js' ); |
|
| 57 | + wp_enqueue_style('wordlift-faceted-search', dirname(plugin_dir_url(__FILE__)).'/css/wordlift-faceted-entity-search-widget.min.css'); |
|
| 58 | + wp_enqueue_script('angularjs', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular.min.js'); |
|
| 59 | + wp_enqueue_script('angularjs-touch', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-touch.min.js'); |
|
| 61 | 60 | |
| 62 | - wp_enqueue_script( 'wordlift-faceted-search', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wordlift-faceted-entity-search-widget.min.js' ); |
|
| 61 | + wp_enqueue_script('wordlift-faceted-search', dirname(plugin_dir_url(__FILE__)).'/js/wordlift-faceted-entity-search-widget.min.js'); |
|
| 63 | 62 | |
| 64 | 63 | wp_localize_script( |
| 65 | 64 | 'wordlift-faceted-search', |
| 66 | 65 | 'wl_faceted_search_params', array( |
| 67 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), |
|
| 66 | + 'ajax_url' => admin_url('admin-ajax.php'), |
|
| 68 | 67 | 'action' => 'wl_faceted_search', |
| 69 | 68 | 'post_id' => $current_post->ID, |
| 70 | 69 | 'entity_ids' => $entity_ids, |
| 71 | - 'limit' => apply_filters( 'wl_faceted_search_limit', $shortcode_atts['limit'] ), |
|
| 70 | + 'limit' => apply_filters('wl_faceted_search_limit', $shortcode_atts['limit']), |
|
| 72 | 71 | 'div_id' => $div_id, |
| 73 | 72 | 'defaultThumbnailPath' => WL_DEFAULT_THUMBNAIL_PATH, |
| 74 | 73 | 'attrs' => $shortcode_atts, |
| 75 | 74 | 'l10n' => array( |
| 76 | - 'what' => _x( 'What', 'Faceted Search Widget', 'wordlift' ), |
|
| 77 | - 'who' => _x( 'Who', 'Faceted Search Widget', 'wordlift' ), |
|
| 78 | - 'where' => _x( 'Where', 'Faceted Search Widget', 'wordlift' ), |
|
| 79 | - 'when' => _x( 'When', 'Faceted Search Widget', 'wordlift' ), |
|
| 75 | + 'what' => _x('What', 'Faceted Search Widget', 'wordlift'), |
|
| 76 | + 'who' => _x('Who', 'Faceted Search Widget', 'wordlift'), |
|
| 77 | + 'where' => _x('Where', 'Faceted Search Widget', 'wordlift'), |
|
| 78 | + 'when' => _x('When', 'Faceted Search Widget', 'wordlift'), |
|
| 80 | 79 | ), |
| 81 | 80 | ) |
| 82 | 81 | ); |
| 83 | 82 | |
| 84 | - return '<div id="' . $div_id . '" style="width:100%"></div>'; |
|
| 83 | + return '<div id="'.$div_id.'" style="width:100%"></div>'; |
|
| 85 | 84 | } |
| 86 | 85 | |
| 87 | -add_shortcode( 'wl_faceted_search', 'wl_shortcode_faceted_search' ); |
|
| 86 | +add_shortcode('wl_faceted_search', 'wl_shortcode_faceted_search'); |
|
| 88 | 87 | |
| 89 | 88 | |
| 90 | 89 | /** |
| 91 | 90 | * Ajax call for the faceted search widget |
| 92 | 91 | */ |
| 93 | -function wl_shortcode_faceted_search_ajax( $http_raw_data = null ) { |
|
| 92 | +function wl_shortcode_faceted_search_ajax($http_raw_data = null) { |
|
| 94 | 93 | |
| 95 | 94 | // Post ID must be defined. |
| 96 | - if ( ! isset( $_GET['post_id'] ) ) { // WPCS: input var ok; CSRF ok. |
|
| 97 | - wp_die( 'No post_id given' ); |
|
| 95 | + if ( ! isset($_GET['post_id'])) { // WPCS: input var ok; CSRF ok. |
|
| 96 | + wp_die('No post_id given'); |
|
| 98 | 97 | |
| 99 | 98 | return; |
| 100 | 99 | } |
| 101 | 100 | |
| 102 | 101 | // Extract filtering conditions. |
| 103 | - $filtering_entity_uris = ( null == $http_raw_data ) ? file_get_contents( 'php://input' ) : $http_raw_data; |
|
| 104 | - $filtering_entity_uris = json_decode( $filtering_entity_uris ); |
|
| 102 | + $filtering_entity_uris = (null == $http_raw_data) ? file_get_contents('php://input') : $http_raw_data; |
|
| 103 | + $filtering_entity_uris = json_decode($filtering_entity_uris); |
|
| 105 | 104 | |
| 106 | 105 | $current_post_id = $_GET['post_id']; // WPCS: input var ok; CSRF ok. |
| 107 | - $current_post = get_post( $current_post_id ); |
|
| 106 | + $current_post = get_post($current_post_id); |
|
| 108 | 107 | |
| 109 | 108 | // Post ID has to match an existing item. |
| 110 | - if ( null === $current_post ) { |
|
| 111 | - wp_die( 'No valid post_id given' ); |
|
| 109 | + if (null === $current_post) { |
|
| 110 | + wp_die('No valid post_id given'); |
|
| 112 | 111 | |
| 113 | 112 | return; |
| 114 | 113 | } |
@@ -117,45 +116,43 @@ discard block |
||
| 117 | 116 | // the current post is used as main entity. |
| 118 | 117 | // Otherwise, current post related entities are used. |
| 119 | 118 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 120 | - $entity_ids = $entity_service->is_entity( $current_post->ID ) ? |
|
| 121 | - array( $current_post->ID ) : |
|
| 122 | - wl_core_get_related_entity_ids( $current_post->ID ); |
|
| 119 | + $entity_ids = $entity_service->is_entity($current_post->ID) ? |
|
| 120 | + array($current_post->ID) : wl_core_get_related_entity_ids($current_post->ID); |
|
| 123 | 121 | |
| 124 | 122 | // If there are no entities we cannot render the widget. |
| 125 | - if ( 0 === count( $entity_ids ) ) { |
|
| 126 | - wp_die( 'No entities available' ); |
|
| 123 | + if (0 === count($entity_ids)) { |
|
| 124 | + wp_die('No entities available'); |
|
| 127 | 125 | |
| 128 | 126 | return; |
| 129 | 127 | } |
| 130 | 128 | |
| 131 | 129 | // Retrieve requested type |
| 132 | - $required_type = ( isset( $_GET['type'] ) ) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 130 | + $required_type = (isset($_GET['type'])) ? $_GET['type'] : null; // WPCS: input var ok; CSRF ok. |
|
| 133 | 131 | |
| 134 | - $limit = ( isset( $_GET['limit'] ) ) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 132 | + $limit = (isset($_GET['limit'])) ? (int) $_GET['limit'] : 20; // WPCS: input var ok; CSRF ok. |
|
| 135 | 133 | |
| 136 | 134 | $referencing_posts = Wordlift_Relation_Service::get_instance()->get_article_subjects( |
| 137 | 135 | $entity_ids, |
| 138 | 136 | '*', |
| 139 | 137 | null, |
| 140 | 138 | 'publish', |
| 141 | - array( $current_post_id ), |
|
| 139 | + array($current_post_id), |
|
| 142 | 140 | $limit |
| 143 | 141 | ); |
| 144 | 142 | |
| 145 | - $referencing_post_ids = array_map( function ( $p ) { |
|
| 143 | + $referencing_post_ids = array_map(function($p) { |
|
| 146 | 144 | return $p->ID; |
| 147 | - }, $referencing_posts ); |
|
| 145 | + }, $referencing_posts); |
|
| 148 | 146 | $results = array(); |
| 149 | 147 | |
| 150 | - if ( 'posts' === $required_type ) { |
|
| 148 | + if ('posts' === $required_type) { |
|
| 151 | 149 | |
| 152 | 150 | // Required filtered posts. |
| 153 | - wl_write_log( "Going to find related posts for the current post [ post ID :: $current_post_id ]" ); |
|
| 151 | + wl_write_log("Going to find related posts for the current post [ post ID :: $current_post_id ]"); |
|
| 154 | 152 | |
| 155 | - $filtered_posts = ( empty( $filtering_entity_uris ) ) ? |
|
| 156 | - $referencing_posts : |
|
| 157 | - Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 158 | - wl_get_entity_post_ids_by_uris( $filtering_entity_uris ), |
|
| 153 | + $filtered_posts = (empty($filtering_entity_uris)) ? |
|
| 154 | + $referencing_posts : Wordlift_Relation_Service::get_instance()->get_article_subjects( |
|
| 155 | + wl_get_entity_post_ids_by_uris($filtering_entity_uris), |
|
| 159 | 156 | '*', |
| 160 | 157 | null, |
| 161 | 158 | null, |
@@ -164,13 +161,13 @@ discard block |
||
| 164 | 161 | $referencing_post_ids |
| 165 | 162 | ); |
| 166 | 163 | |
| 167 | - if ( $filtered_posts ) { |
|
| 168 | - foreach ( $filtered_posts as $post_obj ) { |
|
| 164 | + if ($filtered_posts) { |
|
| 165 | + foreach ($filtered_posts as $post_obj) { |
|
| 169 | 166 | |
| 170 | - $thumbnail = wp_get_attachment_url( get_post_thumbnail_id( $post_obj->ID, 'thumbnail' ) ); |
|
| 171 | - $post_obj->thumbnail = ( $thumbnail ) ? |
|
| 167 | + $thumbnail = wp_get_attachment_url(get_post_thumbnail_id($post_obj->ID, 'thumbnail')); |
|
| 168 | + $post_obj->thumbnail = ($thumbnail) ? |
|
| 172 | 169 | $thumbnail : WL_DEFAULT_THUMBNAIL_PATH; |
| 173 | - $post_obj->permalink = get_post_permalink( $post_obj->ID ); |
|
| 170 | + $post_obj->permalink = get_post_permalink($post_obj->ID); |
|
| 174 | 171 | |
| 175 | 172 | $results[] = $post_obj; |
| 176 | 173 | } |
@@ -179,7 +176,7 @@ discard block |
||
| 179 | 176 | |
| 180 | 177 | global $wpdb; |
| 181 | 178 | |
| 182 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ]" ); |
|
| 179 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ]"); |
|
| 183 | 180 | |
| 184 | 181 | // Retrieve Wordlift relation instances table name. |
| 185 | 182 | $table_name = wl_core_get_relation_instances_table_name(); |
@@ -188,8 +185,8 @@ discard block |
||
| 188 | 185 | * Make sure we have some referenced post, otherwise the IN parts of |
| 189 | 186 | * the SQL will produce an SQL error. |
| 190 | 187 | */ |
| 191 | - if ( ! empty( $referencing_post_ids ) ) { |
|
| 192 | - $subject_ids = implode( ',', $referencing_post_ids ); |
|
| 188 | + if ( ! empty($referencing_post_ids)) { |
|
| 189 | + $subject_ids = implode(',', $referencing_post_ids); |
|
| 193 | 190 | |
| 194 | 191 | $query = " |
| 195 | 192 | SELECT |
@@ -203,19 +200,19 @@ discard block |
||
| 203 | 200 | LIMIT $limit; |
| 204 | 201 | "; |
| 205 | 202 | |
| 206 | - wl_write_log( "Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]" ); |
|
| 203 | + wl_write_log("Going to find related entities for the current post [ post ID :: $current_post_id ] [ query :: $query ]"); |
|
| 207 | 204 | |
| 208 | - $entities = $wpdb->get_results( $query, OBJECT ); // No cache ok. |
|
| 205 | + $entities = $wpdb->get_results($query, OBJECT); // No cache ok. |
|
| 209 | 206 | |
| 210 | - wl_write_log( 'Entities found ' . count( $entities ) ); |
|
| 207 | + wl_write_log('Entities found '.count($entities)); |
|
| 211 | 208 | |
| 212 | - foreach ( $entities as $obj ) { |
|
| 209 | + foreach ($entities as $obj) { |
|
| 213 | 210 | |
| 214 | - $entity = get_post( $obj->ID ); |
|
| 211 | + $entity = get_post($obj->ID); |
|
| 215 | 212 | // Ensure only valid and published entities are returned. |
| 216 | - if ( ( null !== $entity ) && ( 'publish' === $entity->post_status ) ) { |
|
| 213 | + if ((null !== $entity) && ('publish' === $entity->post_status)) { |
|
| 217 | 214 | |
| 218 | - $serialized_entity = wl_serialize_entity( $entity ); |
|
| 215 | + $serialized_entity = wl_serialize_entity($entity); |
|
| 219 | 216 | $serialized_entity['counter'] = $obj->counter; |
| 220 | 217 | $serialized_entity['createdAt'] = $entity->post_date; |
| 221 | 218 | |
@@ -225,9 +222,9 @@ discard block |
||
| 225 | 222 | } |
| 226 | 223 | } |
| 227 | 224 | |
| 228 | - wl_core_send_json( $results ); |
|
| 225 | + wl_core_send_json($results); |
|
| 229 | 226 | |
| 230 | 227 | } |
| 231 | 228 | |
| 232 | -add_action( 'wp_ajax_wl_faceted_search', 'wl_shortcode_faceted_search_ajax' ); |
|
| 233 | -add_action( 'wp_ajax_nopriv_wl_faceted_search', 'wl_shortcode_faceted_search_ajax' ); |
|
| 229 | +add_action('wp_ajax_wl_faceted_search', 'wl_shortcode_faceted_search_ajax'); |
|
| 230 | +add_action('wp_ajax_nopriv_wl_faceted_search', 'wl_shortcode_faceted_search_ajax'); |
|
@@ -18,121 +18,121 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Vocabulary_Shortcode extends Wordlift_Shortcode { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The shortcode. |
|
| 23 | - * |
|
| 24 | - * @since 3.17.0 |
|
| 25 | - */ |
|
| 26 | - const SHORTCODE = 'wl_vocabulary'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.11.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $configuration_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * A {@link Wordlift_Log_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.17.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $log; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Create a {@link Wordlift_Glossary_Shortcode} instance. |
|
| 48 | - * |
|
| 49 | - * @since 3.16.0 |
|
| 50 | - * |
|
| 51 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 52 | - */ |
|
| 53 | - public function __construct( $configuration_service ) { |
|
| 54 | - parent::__construct(); |
|
| 55 | - |
|
| 56 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 57 | - |
|
| 58 | - $this->configuration_service = $configuration_service; |
|
| 59 | - |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Check whether the requirements for this shortcode to work are available. |
|
| 64 | - * |
|
| 65 | - * @since 3.17.0 |
|
| 66 | - * @return bool True if the requirements are satisfied otherwise false. |
|
| 67 | - */ |
|
| 68 | - private static function are_requirements_satisfied() { |
|
| 69 | - |
|
| 70 | - return function_exists( 'mb_strlen' ) && |
|
| 71 | - function_exists( 'mb_substr' ) && |
|
| 72 | - function_exists( 'mb_convert_case' ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Render the shortcode. |
|
| 77 | - * |
|
| 78 | - * @since 3.16.0 |
|
| 79 | - * |
|
| 80 | - * @param array $atts An array of shortcode attributes as set by the editor. |
|
| 81 | - * |
|
| 82 | - * @return string The output html code. |
|
| 83 | - */ |
|
| 84 | - public function render( $atts ) { |
|
| 85 | - |
|
| 86 | - // Bail out if the requirements aren't satisfied: we need mbstring for |
|
| 87 | - // the vocabulary widget to work. |
|
| 88 | - if ( ! self::are_requirements_satisfied() ) { |
|
| 89 | - $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
| 90 | - |
|
| 91 | - return ''; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css' ); |
|
| 95 | - |
|
| 96 | - // Extract attributes and set default values. |
|
| 97 | - $atts = shortcode_atts( array( |
|
| 98 | - // The entity type, such as `person`, `organization`, ... |
|
| 99 | - 'type' => 'all', |
|
| 100 | - // Limit the number of posts to 100 by default. Use -1 to remove the limit. |
|
| 101 | - 'limit' => 100, |
|
| 102 | - // Sort by title. |
|
| 103 | - 'orderby' => 'title', |
|
| 104 | - ), $atts ); |
|
| 105 | - |
|
| 106 | - // Get the posts. Note that if a `type` is specified before, then the |
|
| 107 | - // `tax_query` from the `add_criterias` call isn't added. |
|
| 108 | - $posts = $this->get_posts( $atts ); |
|
| 109 | - |
|
| 110 | - // Get the alphabet. |
|
| 111 | - $language_code = $this->configuration_service->get_language_code(); |
|
| 112 | - $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
| 113 | - |
|
| 114 | - // Add posts to the alphabet. |
|
| 115 | - foreach ( $posts as $post ) { |
|
| 116 | - $this->add_to_alphabet( $alphabet, $post->ID ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - // Generate the header. |
|
| 120 | - $header = array_reduce( array_keys( $alphabet ), function ( $carry, $item ) use ( $alphabet ) { |
|
| 121 | - $template = ( empty( $alphabet[ $item ] ) |
|
| 122 | - ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
|
| 123 | - : '<a href="#wl-vocabulary-widget-%2$s">%1$s</a>' ); |
|
| 124 | - |
|
| 125 | - return $carry . sprintf( $template, esc_html( $item ), esc_attr( $item ) ); |
|
| 126 | - }, '' ); |
|
| 127 | - |
|
| 128 | - // Generate the sections. |
|
| 129 | - $that = $this; |
|
| 130 | - $sections = array_reduce( array_keys( $alphabet ), function ( $carry, $item ) use ( $alphabet, $that ) { |
|
| 131 | - return $carry . $that->get_section( $item, $alphabet[ $item ] ); |
|
| 132 | - }, '' ); |
|
| 133 | - |
|
| 134 | - // Return HTML template. |
|
| 135 | - return " |
|
| 21 | + /** |
|
| 22 | + * The shortcode. |
|
| 23 | + * |
|
| 24 | + * @since 3.17.0 |
|
| 25 | + */ |
|
| 26 | + const SHORTCODE = 'wl_vocabulary'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.11.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $configuration_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * A {@link Wordlift_Log_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.17.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $log; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Create a {@link Wordlift_Glossary_Shortcode} instance. |
|
| 48 | + * |
|
| 49 | + * @since 3.16.0 |
|
| 50 | + * |
|
| 51 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 52 | + */ |
|
| 53 | + public function __construct( $configuration_service ) { |
|
| 54 | + parent::__construct(); |
|
| 55 | + |
|
| 56 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 57 | + |
|
| 58 | + $this->configuration_service = $configuration_service; |
|
| 59 | + |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Check whether the requirements for this shortcode to work are available. |
|
| 64 | + * |
|
| 65 | + * @since 3.17.0 |
|
| 66 | + * @return bool True if the requirements are satisfied otherwise false. |
|
| 67 | + */ |
|
| 68 | + private static function are_requirements_satisfied() { |
|
| 69 | + |
|
| 70 | + return function_exists( 'mb_strlen' ) && |
|
| 71 | + function_exists( 'mb_substr' ) && |
|
| 72 | + function_exists( 'mb_convert_case' ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Render the shortcode. |
|
| 77 | + * |
|
| 78 | + * @since 3.16.0 |
|
| 79 | + * |
|
| 80 | + * @param array $atts An array of shortcode attributes as set by the editor. |
|
| 81 | + * |
|
| 82 | + * @return string The output html code. |
|
| 83 | + */ |
|
| 84 | + public function render( $atts ) { |
|
| 85 | + |
|
| 86 | + // Bail out if the requirements aren't satisfied: we need mbstring for |
|
| 87 | + // the vocabulary widget to work. |
|
| 88 | + if ( ! self::are_requirements_satisfied() ) { |
|
| 89 | + $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
| 90 | + |
|
| 91 | + return ''; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css' ); |
|
| 95 | + |
|
| 96 | + // Extract attributes and set default values. |
|
| 97 | + $atts = shortcode_atts( array( |
|
| 98 | + // The entity type, such as `person`, `organization`, ... |
|
| 99 | + 'type' => 'all', |
|
| 100 | + // Limit the number of posts to 100 by default. Use -1 to remove the limit. |
|
| 101 | + 'limit' => 100, |
|
| 102 | + // Sort by title. |
|
| 103 | + 'orderby' => 'title', |
|
| 104 | + ), $atts ); |
|
| 105 | + |
|
| 106 | + // Get the posts. Note that if a `type` is specified before, then the |
|
| 107 | + // `tax_query` from the `add_criterias` call isn't added. |
|
| 108 | + $posts = $this->get_posts( $atts ); |
|
| 109 | + |
|
| 110 | + // Get the alphabet. |
|
| 111 | + $language_code = $this->configuration_service->get_language_code(); |
|
| 112 | + $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
| 113 | + |
|
| 114 | + // Add posts to the alphabet. |
|
| 115 | + foreach ( $posts as $post ) { |
|
| 116 | + $this->add_to_alphabet( $alphabet, $post->ID ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + // Generate the header. |
|
| 120 | + $header = array_reduce( array_keys( $alphabet ), function ( $carry, $item ) use ( $alphabet ) { |
|
| 121 | + $template = ( empty( $alphabet[ $item ] ) |
|
| 122 | + ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
|
| 123 | + : '<a href="#wl-vocabulary-widget-%2$s">%1$s</a>' ); |
|
| 124 | + |
|
| 125 | + return $carry . sprintf( $template, esc_html( $item ), esc_attr( $item ) ); |
|
| 126 | + }, '' ); |
|
| 127 | + |
|
| 128 | + // Generate the sections. |
|
| 129 | + $that = $this; |
|
| 130 | + $sections = array_reduce( array_keys( $alphabet ), function ( $carry, $item ) use ( $alphabet, $that ) { |
|
| 131 | + return $carry . $that->get_section( $item, $alphabet[ $item ] ); |
|
| 132 | + }, '' ); |
|
| 133 | + |
|
| 134 | + // Return HTML template. |
|
| 135 | + return " |
|
| 136 | 136 | <div class='wl-vocabulary'> |
| 137 | 137 | <nav class='wl-vocabulary-alphabet-nav'> |
| 138 | 138 | $header |
@@ -143,28 +143,28 @@ discard block |
||
| 143 | 143 | </div> |
| 144 | 144 | "; |
| 145 | 145 | |
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Generate the html code for the section. |
|
| 150 | - * |
|
| 151 | - * @since 3.17.0 |
|
| 152 | - * |
|
| 153 | - * @param string $letter The section's letter. |
|
| 154 | - * @param array $posts An array of `$post_id => $post_title` associated with |
|
| 155 | - * the section. |
|
| 156 | - * |
|
| 157 | - * @return string The section html code (or an empty string if the section has |
|
| 158 | - * no posts). |
|
| 159 | - */ |
|
| 160 | - private function get_section( $letter, $posts ) { |
|
| 161 | - |
|
| 162 | - // Return an empty string if there are no posts. |
|
| 163 | - if ( 0 === count( $posts ) ) { |
|
| 164 | - return ''; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return sprintf( ' |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Generate the html code for the section. |
|
| 150 | + * |
|
| 151 | + * @since 3.17.0 |
|
| 152 | + * |
|
| 153 | + * @param string $letter The section's letter. |
|
| 154 | + * @param array $posts An array of `$post_id => $post_title` associated with |
|
| 155 | + * the section. |
|
| 156 | + * |
|
| 157 | + * @return string The section html code (or an empty string if the section has |
|
| 158 | + * no posts). |
|
| 159 | + */ |
|
| 160 | + private function get_section( $letter, $posts ) { |
|
| 161 | + |
|
| 162 | + // Return an empty string if there are no posts. |
|
| 163 | + if ( 0 === count( $posts ) ) { |
|
| 164 | + return ''; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return sprintf( ' |
|
| 168 | 168 | <div class="wl-vocabulary-letter-block" id="wl-vocabulary-widget-%s"> |
| 169 | 169 | <aside class="wl-vocabulary-left-column">%s</aside> |
| 170 | 170 | <div class="wl-vocabulary-right-column"> |
@@ -174,107 +174,107 @@ discard block |
||
| 174 | 174 | </div> |
| 175 | 175 | </div> |
| 176 | 176 | ', esc_attr( $letter ), esc_html( $letter ), $this->format_posts_as_list( $posts ) ); |
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Format an array post `$post_id => $post_title` as a list. |
|
| 181 | - * |
|
| 182 | - * @since 3.17.0 |
|
| 183 | - * |
|
| 184 | - * @param array $posts An array of `$post_id => $post_title` key, value pairs. |
|
| 185 | - * |
|
| 186 | - * @return string A list. |
|
| 187 | - */ |
|
| 188 | - private function format_posts_as_list( $posts ) { |
|
| 189 | - |
|
| 190 | - return array_reduce( array_keys( $posts ), function ( $carry, $item ) use ( $posts ) { |
|
| 191 | - return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
| 192 | - }, '' ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Get the posts from WordPress using the provided attributes. |
|
| 197 | - * |
|
| 198 | - * @since 3.17.0 |
|
| 199 | - * |
|
| 200 | - * @param array $atts The shortcode attributes. |
|
| 201 | - * |
|
| 202 | - * @return array An array of {@link WP_Post}s. |
|
| 203 | - */ |
|
| 204 | - private function get_posts( $atts ) { |
|
| 205 | - |
|
| 206 | - // The default arguments for the query. |
|
| 207 | - $args = array( |
|
| 208 | - 'numberposts' => intval( $atts['limit'] ), |
|
| 209 | - 'update_post_meta_cache' => false, |
|
| 210 | - 'update_post_term_cache' => false, |
|
| 211 | - // Exclude the publisher. |
|
| 212 | - 'post__not_in' => array( $this->configuration_service->get_publisher_id() ), |
|
| 213 | - ); |
|
| 214 | - |
|
| 215 | - // Limit the based entity type if needed. |
|
| 216 | - if ( 'all' !== $atts['type'] ) { |
|
| 217 | - $args['tax_query'] = array( |
|
| 218 | - array( |
|
| 219 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 220 | - 'field' => 'slug', |
|
| 221 | - 'terms' => $atts['type'], |
|
| 222 | - ), |
|
| 223 | - ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // Get the posts. Note that if a `type` is specified before, then the |
|
| 227 | - // `tax_query` from the `add_criterias` call isn't added. |
|
| 228 | - return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
| 229 | - |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Populate the alphabet with posts. |
|
| 234 | - * |
|
| 235 | - * @since 3.17.0 |
|
| 236 | - * |
|
| 237 | - * @param array $alphabet An array of letters. |
|
| 238 | - * @param int $post_id The {@link WP_Post} id. |
|
| 239 | - */ |
|
| 240 | - private function add_to_alphabet( &$alphabet, $post_id ) { |
|
| 241 | - |
|
| 242 | - // Get the title without accents. |
|
| 243 | - $title = remove_accents( get_the_title( $post_id ) ); |
|
| 244 | - |
|
| 245 | - // Get the initial letter. |
|
| 246 | - $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
| 247 | - |
|
| 248 | - // Add the post. |
|
| 249 | - $alphabet[ $letter ][ $post_id ] = $title; |
|
| 250 | - |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Find the first letter in the alphabet. |
|
| 255 | - * |
|
| 256 | - * In some alphabets a letter is a compound of letters, therefore this function |
|
| 257 | - * will look for groups of 2 or 3 letters in the alphabet before looking for a |
|
| 258 | - * single letter. In case the letter is not found a # (hash) key is returned. |
|
| 259 | - * |
|
| 260 | - * @since 3.17.0 |
|
| 261 | - * |
|
| 262 | - * @param array $alphabet An array of alphabet letters. |
|
| 263 | - * @param string $title The title to match. |
|
| 264 | - * |
|
| 265 | - * @return string The initial letter or a `#` key. |
|
| 266 | - */ |
|
| 267 | - private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
| 268 | - |
|
| 269 | - // Need to handle letters which consist of 3 and 2 characters. |
|
| 270 | - for ( $i = 3; $i > 0; $i -- ) { |
|
| 271 | - $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
| 272 | - if ( isset( $alphabet[ $letter ] ) ) { |
|
| 273 | - return $letter; |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - return '#'; |
|
| 278 | - } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Format an array post `$post_id => $post_title` as a list. |
|
| 181 | + * |
|
| 182 | + * @since 3.17.0 |
|
| 183 | + * |
|
| 184 | + * @param array $posts An array of `$post_id => $post_title` key, value pairs. |
|
| 185 | + * |
|
| 186 | + * @return string A list. |
|
| 187 | + */ |
|
| 188 | + private function format_posts_as_list( $posts ) { |
|
| 189 | + |
|
| 190 | + return array_reduce( array_keys( $posts ), function ( $carry, $item ) use ( $posts ) { |
|
| 191 | + return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
| 192 | + }, '' ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Get the posts from WordPress using the provided attributes. |
|
| 197 | + * |
|
| 198 | + * @since 3.17.0 |
|
| 199 | + * |
|
| 200 | + * @param array $atts The shortcode attributes. |
|
| 201 | + * |
|
| 202 | + * @return array An array of {@link WP_Post}s. |
|
| 203 | + */ |
|
| 204 | + private function get_posts( $atts ) { |
|
| 205 | + |
|
| 206 | + // The default arguments for the query. |
|
| 207 | + $args = array( |
|
| 208 | + 'numberposts' => intval( $atts['limit'] ), |
|
| 209 | + 'update_post_meta_cache' => false, |
|
| 210 | + 'update_post_term_cache' => false, |
|
| 211 | + // Exclude the publisher. |
|
| 212 | + 'post__not_in' => array( $this->configuration_service->get_publisher_id() ), |
|
| 213 | + ); |
|
| 214 | + |
|
| 215 | + // Limit the based entity type if needed. |
|
| 216 | + if ( 'all' !== $atts['type'] ) { |
|
| 217 | + $args['tax_query'] = array( |
|
| 218 | + array( |
|
| 219 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 220 | + 'field' => 'slug', |
|
| 221 | + 'terms' => $atts['type'], |
|
| 222 | + ), |
|
| 223 | + ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // Get the posts. Note that if a `type` is specified before, then the |
|
| 227 | + // `tax_query` from the `add_criterias` call isn't added. |
|
| 228 | + return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
| 229 | + |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Populate the alphabet with posts. |
|
| 234 | + * |
|
| 235 | + * @since 3.17.0 |
|
| 236 | + * |
|
| 237 | + * @param array $alphabet An array of letters. |
|
| 238 | + * @param int $post_id The {@link WP_Post} id. |
|
| 239 | + */ |
|
| 240 | + private function add_to_alphabet( &$alphabet, $post_id ) { |
|
| 241 | + |
|
| 242 | + // Get the title without accents. |
|
| 243 | + $title = remove_accents( get_the_title( $post_id ) ); |
|
| 244 | + |
|
| 245 | + // Get the initial letter. |
|
| 246 | + $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
| 247 | + |
|
| 248 | + // Add the post. |
|
| 249 | + $alphabet[ $letter ][ $post_id ] = $title; |
|
| 250 | + |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Find the first letter in the alphabet. |
|
| 255 | + * |
|
| 256 | + * In some alphabets a letter is a compound of letters, therefore this function |
|
| 257 | + * will look for groups of 2 or 3 letters in the alphabet before looking for a |
|
| 258 | + * single letter. In case the letter is not found a # (hash) key is returned. |
|
| 259 | + * |
|
| 260 | + * @since 3.17.0 |
|
| 261 | + * |
|
| 262 | + * @param array $alphabet An array of alphabet letters. |
|
| 263 | + * @param string $title The title to match. |
|
| 264 | + * |
|
| 265 | + * @return string The initial letter or a `#` key. |
|
| 266 | + */ |
|
| 267 | + private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
| 268 | + |
|
| 269 | + // Need to handle letters which consist of 3 and 2 characters. |
|
| 270 | + for ( $i = 3; $i > 0; $i -- ) { |
|
| 271 | + $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
| 272 | + if ( isset( $alphabet[ $letter ] ) ) { |
|
| 273 | + return $letter; |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + return '#'; |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | 280 | } |
@@ -50,10 +50,10 @@ discard block |
||
| 50 | 50 | * |
| 51 | 51 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 52 | 52 | */ |
| 53 | - public function __construct( $configuration_service ) { |
|
| 53 | + public function __construct($configuration_service) { |
|
| 54 | 54 | parent::__construct(); |
| 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 | |
@@ -67,9 +67,9 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | private static function are_requirements_satisfied() { |
| 69 | 69 | |
| 70 | - return function_exists( 'mb_strlen' ) && |
|
| 71 | - function_exists( 'mb_substr' ) && |
|
| 72 | - function_exists( 'mb_convert_case' ); |
|
| 70 | + return function_exists('mb_strlen') && |
|
| 71 | + function_exists('mb_substr') && |
|
| 72 | + function_exists('mb_convert_case'); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -81,55 +81,55 @@ discard block |
||
| 81 | 81 | * |
| 82 | 82 | * @return string The output html code. |
| 83 | 83 | */ |
| 84 | - public function render( $atts ) { |
|
| 84 | + public function render($atts) { |
|
| 85 | 85 | |
| 86 | 86 | // Bail out if the requirements aren't satisfied: we need mbstring for |
| 87 | 87 | // the vocabulary widget to work. |
| 88 | - if ( ! self::are_requirements_satisfied() ) { |
|
| 89 | - $this->log->warn( "The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements." ); |
|
| 88 | + if ( ! self::are_requirements_satisfied()) { |
|
| 89 | + $this->log->warn("The vocabulary widget cannot be displayed because this WordPress installation doesn't satisfy its requirements."); |
|
| 90 | 90 | |
| 91 | 91 | return ''; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - wp_enqueue_style( 'wl-vocabulary-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/public/css/wordlift-vocabulary-shortcode.css' ); |
|
| 94 | + wp_enqueue_style('wl-vocabulary-shortcode', dirname(plugin_dir_url(__FILE__)).'/public/css/wordlift-vocabulary-shortcode.css'); |
|
| 95 | 95 | |
| 96 | 96 | // Extract attributes and set default values. |
| 97 | - $atts = shortcode_atts( array( |
|
| 97 | + $atts = shortcode_atts(array( |
|
| 98 | 98 | // The entity type, such as `person`, `organization`, ... |
| 99 | 99 | 'type' => 'all', |
| 100 | 100 | // Limit the number of posts to 100 by default. Use -1 to remove the limit. |
| 101 | 101 | 'limit' => 100, |
| 102 | 102 | // Sort by title. |
| 103 | 103 | 'orderby' => 'title', |
| 104 | - ), $atts ); |
|
| 104 | + ), $atts); |
|
| 105 | 105 | |
| 106 | 106 | // Get the posts. Note that if a `type` is specified before, then the |
| 107 | 107 | // `tax_query` from the `add_criterias` call isn't added. |
| 108 | - $posts = $this->get_posts( $atts ); |
|
| 108 | + $posts = $this->get_posts($atts); |
|
| 109 | 109 | |
| 110 | 110 | // Get the alphabet. |
| 111 | 111 | $language_code = $this->configuration_service->get_language_code(); |
| 112 | - $alphabet = Wordlift_Alphabet_Service::get( $language_code ); |
|
| 112 | + $alphabet = Wordlift_Alphabet_Service::get($language_code); |
|
| 113 | 113 | |
| 114 | 114 | // Add posts to the alphabet. |
| 115 | - foreach ( $posts as $post ) { |
|
| 116 | - $this->add_to_alphabet( $alphabet, $post->ID ); |
|
| 115 | + foreach ($posts as $post) { |
|
| 116 | + $this->add_to_alphabet($alphabet, $post->ID); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // Generate the header. |
| 120 | - $header = array_reduce( array_keys( $alphabet ), function ( $carry, $item ) use ( $alphabet ) { |
|
| 121 | - $template = ( empty( $alphabet[ $item ] ) |
|
| 120 | + $header = array_reduce(array_keys($alphabet), function($carry, $item) use ($alphabet) { |
|
| 121 | + $template = (empty($alphabet[$item]) |
|
| 122 | 122 | ? '<span class="wl-vocabulary-widget-disabled">%s</span>' |
| 123 | - : '<a href="#wl-vocabulary-widget-%2$s">%1$s</a>' ); |
|
| 123 | + : '<a href="#wl-vocabulary-widget-%2$s">%1$s</a>'); |
|
| 124 | 124 | |
| 125 | - return $carry . sprintf( $template, esc_html( $item ), esc_attr( $item ) ); |
|
| 126 | - }, '' ); |
|
| 125 | + return $carry.sprintf($template, esc_html($item), esc_attr($item)); |
|
| 126 | + }, ''); |
|
| 127 | 127 | |
| 128 | 128 | // Generate the sections. |
| 129 | 129 | $that = $this; |
| 130 | - $sections = array_reduce( array_keys( $alphabet ), function ( $carry, $item ) use ( $alphabet, $that ) { |
|
| 131 | - return $carry . $that->get_section( $item, $alphabet[ $item ] ); |
|
| 132 | - }, '' ); |
|
| 130 | + $sections = array_reduce(array_keys($alphabet), function($carry, $item) use ($alphabet, $that) { |
|
| 131 | + return $carry.$that->get_section($item, $alphabet[$item]); |
|
| 132 | + }, ''); |
|
| 133 | 133 | |
| 134 | 134 | // Return HTML template. |
| 135 | 135 | return " |
@@ -157,14 +157,14 @@ discard block |
||
| 157 | 157 | * @return string The section html code (or an empty string if the section has |
| 158 | 158 | * no posts). |
| 159 | 159 | */ |
| 160 | - private function get_section( $letter, $posts ) { |
|
| 160 | + private function get_section($letter, $posts) { |
|
| 161 | 161 | |
| 162 | 162 | // Return an empty string if there are no posts. |
| 163 | - if ( 0 === count( $posts ) ) { |
|
| 163 | + if (0 === count($posts)) { |
|
| 164 | 164 | return ''; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - return sprintf( ' |
|
| 167 | + return sprintf(' |
|
| 168 | 168 | <div class="wl-vocabulary-letter-block" id="wl-vocabulary-widget-%s"> |
| 169 | 169 | <aside class="wl-vocabulary-left-column">%s</aside> |
| 170 | 170 | <div class="wl-vocabulary-right-column"> |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | </ul> |
| 174 | 174 | </div> |
| 175 | 175 | </div> |
| 176 | - ', esc_attr( $letter ), esc_html( $letter ), $this->format_posts_as_list( $posts ) ); |
|
| 176 | + ', esc_attr($letter), esc_html($letter), $this->format_posts_as_list($posts)); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /** |
@@ -185,11 +185,11 @@ discard block |
||
| 185 | 185 | * |
| 186 | 186 | * @return string A list. |
| 187 | 187 | */ |
| 188 | - private function format_posts_as_list( $posts ) { |
|
| 188 | + private function format_posts_as_list($posts) { |
|
| 189 | 189 | |
| 190 | - return array_reduce( array_keys( $posts ), function ( $carry, $item ) use ( $posts ) { |
|
| 191 | - return $carry . sprintf( '<li><a href="%s">%s</a></li>', esc_attr( get_permalink( $item ) ), esc_html( $posts[ $item ] ) ); |
|
| 192 | - }, '' ); |
|
| 190 | + return array_reduce(array_keys($posts), function($carry, $item) use ($posts) { |
|
| 191 | + return $carry.sprintf('<li><a href="%s">%s</a></li>', esc_attr(get_permalink($item)), esc_html($posts[$item])); |
|
| 192 | + }, ''); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | /** |
@@ -201,19 +201,19 @@ discard block |
||
| 201 | 201 | * |
| 202 | 202 | * @return array An array of {@link WP_Post}s. |
| 203 | 203 | */ |
| 204 | - private function get_posts( $atts ) { |
|
| 204 | + private function get_posts($atts) { |
|
| 205 | 205 | |
| 206 | 206 | // The default arguments for the query. |
| 207 | 207 | $args = array( |
| 208 | - 'numberposts' => intval( $atts['limit'] ), |
|
| 208 | + 'numberposts' => intval($atts['limit']), |
|
| 209 | 209 | 'update_post_meta_cache' => false, |
| 210 | 210 | 'update_post_term_cache' => false, |
| 211 | 211 | // Exclude the publisher. |
| 212 | - 'post__not_in' => array( $this->configuration_service->get_publisher_id() ), |
|
| 212 | + 'post__not_in' => array($this->configuration_service->get_publisher_id()), |
|
| 213 | 213 | ); |
| 214 | 214 | |
| 215 | 215 | // Limit the based entity type if needed. |
| 216 | - if ( 'all' !== $atts['type'] ) { |
|
| 216 | + if ('all' !== $atts['type']) { |
|
| 217 | 217 | $args['tax_query'] = array( |
| 218 | 218 | array( |
| 219 | 219 | 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | |
| 226 | 226 | // Get the posts. Note that if a `type` is specified before, then the |
| 227 | 227 | // `tax_query` from the `add_criterias` call isn't added. |
| 228 | - return get_posts( Wordlift_Entity_Service::add_criterias( $args ) ); |
|
| 228 | + return get_posts(Wordlift_Entity_Service::add_criterias($args)); |
|
| 229 | 229 | |
| 230 | 230 | } |
| 231 | 231 | |
@@ -237,16 +237,16 @@ discard block |
||
| 237 | 237 | * @param array $alphabet An array of letters. |
| 238 | 238 | * @param int $post_id The {@link WP_Post} id. |
| 239 | 239 | */ |
| 240 | - private function add_to_alphabet( &$alphabet, $post_id ) { |
|
| 240 | + private function add_to_alphabet(&$alphabet, $post_id) { |
|
| 241 | 241 | |
| 242 | 242 | // Get the title without accents. |
| 243 | - $title = remove_accents( get_the_title( $post_id ) ); |
|
| 243 | + $title = remove_accents(get_the_title($post_id)); |
|
| 244 | 244 | |
| 245 | 245 | // Get the initial letter. |
| 246 | - $letter = $this->get_first_letter_in_alphabet_or_hash( $alphabet, $title ); |
|
| 246 | + $letter = $this->get_first_letter_in_alphabet_or_hash($alphabet, $title); |
|
| 247 | 247 | |
| 248 | 248 | // Add the post. |
| 249 | - $alphabet[ $letter ][ $post_id ] = $title; |
|
| 249 | + $alphabet[$letter][$post_id] = $title; |
|
| 250 | 250 | |
| 251 | 251 | } |
| 252 | 252 | |
@@ -264,12 +264,12 @@ discard block |
||
| 264 | 264 | * |
| 265 | 265 | * @return string The initial letter or a `#` key. |
| 266 | 266 | */ |
| 267 | - private function get_first_letter_in_alphabet_or_hash( $alphabet, $title ) { |
|
| 267 | + private function get_first_letter_in_alphabet_or_hash($alphabet, $title) { |
|
| 268 | 268 | |
| 269 | 269 | // Need to handle letters which consist of 3 and 2 characters. |
| 270 | - for ( $i = 3; $i > 0; $i -- ) { |
|
| 271 | - $letter = mb_convert_case( mb_substr( $title, 0, $i ), MB_CASE_UPPER ); |
|
| 272 | - if ( isset( $alphabet[ $letter ] ) ) { |
|
| 270 | + for ($i = 3; $i > 0; $i--) { |
|
| 271 | + $letter = mb_convert_case(mb_substr($title, 0, $i), MB_CASE_UPPER); |
|
| 272 | + if (isset($alphabet[$letter])) { |
|
| 273 | 273 | return $letter; |
| 274 | 274 | } |
| 275 | 275 | } |