@@ -7,613 +7,613 @@ |
||
| 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 entity post type name. |
|
| 39 | - * |
|
| 40 | - * @since 3.1.0 |
|
| 41 | - */ |
|
| 42 | - const TYPE_NAME = 'entity'; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The alternative label meta key. |
|
| 46 | - * |
|
| 47 | - * @since 3.2.0 |
|
| 48 | - */ |
|
| 49 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * The alternative label input template. |
|
| 53 | - * |
|
| 54 | - * @since 3.2.0 |
|
| 55 | - */ |
|
| 56 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 57 | - 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 entity post type name. |
|
| 39 | + * |
|
| 40 | + * @since 3.1.0 |
|
| 41 | + */ |
|
| 42 | + const TYPE_NAME = 'entity'; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The alternative label meta key. |
|
| 46 | + * |
|
| 47 | + * @since 3.2.0 |
|
| 48 | + */ |
|
| 49 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * The alternative label input template. |
|
| 53 | + * |
|
| 54 | + * @since 3.2.0 |
|
| 55 | + */ |
|
| 56 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 57 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 58 | 58 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 59 | 59 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 60 | 60 | <button class="button wl-delete-button">%s</button> |
| 61 | 61 | </div>'; |
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * A singleton instance of the Entity service. |
|
| 65 | - * |
|
| 66 | - * @since 3.2.0 |
|
| 67 | - * @access private |
|
| 68 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 69 | - */ |
|
| 70 | - private static $instance; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Create a Wordlift_Entity_Service instance. |
|
| 74 | - * |
|
| 75 | - * @since 3.2.0 |
|
| 76 | - * |
|
| 77 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 78 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 79 | - */ |
|
| 80 | - public function __construct( $ui_service, $relation_service ) { |
|
| 81 | - |
|
| 82 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 83 | - |
|
| 84 | - $this->ui_service = $ui_service; |
|
| 85 | - $this->relation_service = $relation_service; |
|
| 86 | - |
|
| 87 | - // Set the singleton instance. |
|
| 88 | - self::$instance = $this; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get the singleton instance of the Entity service. |
|
| 93 | - * |
|
| 94 | - * @since 3.2.0 |
|
| 95 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 96 | - */ |
|
| 97 | - public static function get_instance() { |
|
| 98 | - |
|
| 99 | - return self::$instance; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 104 | - * something which is not an article. |
|
| 105 | - * |
|
| 106 | - * @since 3.1.0 |
|
| 107 | - * |
|
| 108 | - * @param int $post_id A post id. |
|
| 109 | - * |
|
| 110 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 111 | - */ |
|
| 112 | - public function is_entity( $post_id ) { |
|
| 113 | - |
|
| 114 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 115 | - |
|
| 116 | - if ( 0 === count( $terms ) ) { |
|
| 117 | - return false; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // We don't consider an `article` to be an entity. |
|
| 121 | - if ( 'article' !== $terms[0]->slug ) { |
|
| 122 | - return true; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - return false; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Get the proper classification scope for a given entity post |
|
| 130 | - * |
|
| 131 | - * @since 3.5.0 |
|
| 132 | - * |
|
| 133 | - * @param integer $post_id An entity post id. |
|
| 134 | - * |
|
| 135 | - * @param string $default The default classification scope, `what` if not |
|
| 136 | - * provided. |
|
| 137 | - * |
|
| 138 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 139 | - */ |
|
| 140 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 141 | - |
|
| 142 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 143 | - return $default; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // Retrieve the entity type |
|
| 147 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 148 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 149 | - // Retrieve classification boxes configuration |
|
| 150 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 151 | - foreach ( $classification_boxes as $cb ) { |
|
| 152 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 153 | - return $cb['id']; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return $default; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Check whether a {@link WP_Post} is used. |
|
| 162 | - * |
|
| 163 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 164 | - * |
|
| 165 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 166 | - */ |
|
| 167 | - public function is_used( $post_id ) { |
|
| 168 | - |
|
| 169 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 170 | - return null; |
|
| 171 | - } |
|
| 172 | - // Retrieve the post |
|
| 173 | - $entity = get_post( $post_id ); |
|
| 174 | - |
|
| 175 | - global $wpdb; |
|
| 176 | - // Retrieve Wordlift relation instances table name |
|
| 177 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 178 | - |
|
| 179 | - // Check is it's referenced / related to another post / entity |
|
| 180 | - $stmt = $wpdb->prepare( |
|
| 181 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 182 | - $entity->ID |
|
| 183 | - ); |
|
| 184 | - |
|
| 185 | - // Perform the query |
|
| 186 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 187 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 188 | - if ( 0 < $relation_instances ) { |
|
| 189 | - return true; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - // Check if the entity uri is used as meta_value |
|
| 193 | - $stmt = $wpdb->prepare( |
|
| 194 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 195 | - $entity->ID, |
|
| 196 | - wl_get_entity_uri( $entity->ID ) |
|
| 197 | - ); |
|
| 198 | - // Perform the query |
|
| 199 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 200 | - |
|
| 201 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 202 | - if ( 0 < $meta_instances ) { |
|
| 203 | - return true; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - // If we are here, it means the current entity is not used at the moment |
|
| 207 | - return false; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Determines whether a given uri is an internal uri or not. |
|
| 212 | - * |
|
| 213 | - * @since 3.3.2 |
|
| 214 | - * |
|
| 215 | - * @param int $uri An uri. |
|
| 216 | - * |
|
| 217 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 218 | - */ |
|
| 219 | - public function is_internal_uri( $uri ) { |
|
| 220 | - |
|
| 221 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 226 | - * |
|
| 227 | - * @since 3.2.0 |
|
| 228 | - * |
|
| 229 | - * @param string $uri The entity URI. |
|
| 230 | - * |
|
| 231 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 232 | - */ |
|
| 233 | - public function get_entity_post_by_uri( $uri ) { |
|
| 234 | - |
|
| 235 | - // Check if we've been provided with a value otherwise return null. |
|
| 236 | - if ( empty( $uri ) ) { |
|
| 237 | - return null; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - $query_args = array( |
|
| 241 | - 'posts_per_page' => 1, |
|
| 242 | - 'post_status' => 'any', |
|
| 243 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 244 | - 'meta_query' => array( |
|
| 245 | - array( |
|
| 246 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 247 | - 'value' => $uri, |
|
| 248 | - 'compare' => '=', |
|
| 249 | - ), |
|
| 250 | - ), |
|
| 251 | - $tax_query = array( |
|
| 252 | - array( |
|
| 253 | - 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 254 | - 'field' => 'slug', |
|
| 255 | - 'terms' => 'article', |
|
| 256 | - 'operator' => 'NOT IN', |
|
| 257 | - ), |
|
| 258 | - ), |
|
| 259 | - ); |
|
| 260 | - |
|
| 261 | - // Only if the current uri is not an internal uri, entity search is |
|
| 262 | - // performed also looking at sameAs values. |
|
| 263 | - // |
|
| 264 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 265 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 266 | - |
|
| 267 | - $query_args['meta_query']['relation'] = 'OR'; |
|
| 268 | - $query_args['meta_query'][] = array( |
|
| 269 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 270 | - 'value' => $uri, |
|
| 271 | - 'compare' => '=', |
|
| 272 | - ); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - $query = new WP_Query( $query_args ); |
|
| 276 | - |
|
| 277 | - // Get the matching entity posts. |
|
| 278 | - $posts = $query->get_posts(); |
|
| 279 | - |
|
| 280 | - // Return null if no post is found. |
|
| 281 | - if ( 0 === count( $posts ) ) { |
|
| 282 | - return null; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - // Return the found post. |
|
| 286 | - return $posts[0]; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 291 | - * we check that the post we're saving is the current post. |
|
| 292 | - * |
|
| 293 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 294 | - * |
|
| 295 | - * @since 3.2.0 |
|
| 296 | - * |
|
| 297 | - * @param int $post_id Post ID. |
|
| 298 | - * @param WP_Post $post Post object. |
|
| 299 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 300 | - */ |
|
| 301 | - public function save_post( $post_id, $post, $update ) { |
|
| 302 | - |
|
| 303 | - // Avoid doing anything if post is autosave or a revision. |
|
| 304 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 305 | - return; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - // We're setting the alternative label that have been provided via the UI |
|
| 309 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 310 | - // programmatically by some other function: we need to check therefore if |
|
| 311 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 312 | - // |
|
| 313 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 314 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 315 | - return; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - // Get the alt labels from the request (or empty array). |
|
| 319 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 320 | - |
|
| 321 | - // Set the alternative labels. |
|
| 322 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 323 | - |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Set the alternative labels. |
|
| 328 | - * |
|
| 329 | - * @since 3.2.0 |
|
| 330 | - * |
|
| 331 | - * @param int $post_id The post id. |
|
| 332 | - * @param array $alt_labels An array of labels. |
|
| 333 | - */ |
|
| 334 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 335 | - |
|
| 336 | - // Force $alt_labels to be an array |
|
| 337 | - if ( ! is_array( $alt_labels ) ) { |
|
| 338 | - $alt_labels = array( $alt_labels ); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 342 | - |
|
| 343 | - // Delete all the existing alternate labels. |
|
| 344 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 345 | - |
|
| 346 | - // Set the alternative labels. |
|
| 347 | - foreach ( $alt_labels as $alt_label ) { |
|
| 348 | - if ( ! empty( $alt_label ) ) { |
|
| 349 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Retrieve the alternate labels. |
|
| 357 | - * |
|
| 358 | - * @since 3.2.0 |
|
| 359 | - * |
|
| 360 | - * @param int $post_id Post id. |
|
| 361 | - * |
|
| 362 | - * @return mixed An array of alternative labels. |
|
| 363 | - */ |
|
| 364 | - public function get_alternative_labels( $post_id ) { |
|
| 365 | - |
|
| 366 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 371 | - * |
|
| 372 | - * @since 3.12.0 |
|
| 373 | - * |
|
| 374 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 375 | - * |
|
| 376 | - * @return array An array with the entity title and labels. |
|
| 377 | - */ |
|
| 378 | - public function get_labels( $post_id ) { |
|
| 379 | - |
|
| 380 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 385 | - * |
|
| 386 | - * @since 3.2.0 |
|
| 387 | - * |
|
| 388 | - * @param WP_Post $post Post object. |
|
| 389 | - */ |
|
| 390 | - public function edit_form_before_permalink( $post ) { |
|
| 391 | - |
|
| 392 | - // If it's not an entity, return. |
|
| 393 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 394 | - return; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - // Print the input template. |
|
| 398 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 399 | - |
|
| 400 | - // Print all the currently set alternative labels. |
|
| 401 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 402 | - |
|
| 403 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 404 | - |
|
| 405 | - }; |
|
| 406 | - |
|
| 407 | - // Print the button. |
|
| 408 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 409 | - |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Get the URI for the entity with the specified post id. |
|
| 414 | - * |
|
| 415 | - * @since 3.6.0 |
|
| 416 | - * |
|
| 417 | - * @param int $post_id The entity post id. |
|
| 418 | - * |
|
| 419 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 420 | - */ |
|
| 421 | - public function get_uri( $post_id ) { |
|
| 422 | - |
|
| 423 | - // If a null is given, nothing to do |
|
| 424 | - if ( null == $post_id ) { |
|
| 425 | - return null; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 429 | - |
|
| 430 | - // If the dataset uri is not properly configured, null is returned |
|
| 431 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 432 | - return null; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - // Set the URI if it isn't set yet. |
|
| 436 | - $post_status = get_post_status( $post_id ); |
|
| 437 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 438 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 439 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - return $uri; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * Get the alternative label input HTML code. |
|
| 448 | - * |
|
| 449 | - * @since 3.2.0 |
|
| 450 | - * |
|
| 451 | - * @param string $value The input value. |
|
| 452 | - * |
|
| 453 | - * @return string The input HTML code. |
|
| 454 | - */ |
|
| 455 | - private function get_alternative_label_input( $value = '' ) { |
|
| 456 | - |
|
| 457 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * Get the number of entity posts published in this blog. |
|
| 462 | - * |
|
| 463 | - * @since 3.6.0 |
|
| 464 | - * |
|
| 465 | - * @return int The number of published entity posts. |
|
| 466 | - */ |
|
| 467 | - public function count() { |
|
| 468 | - |
|
| 469 | - $posts = get_posts( $this->add_criterias( array( |
|
| 470 | - 'post_status' => 'any', |
|
| 471 | - 'numberposts' => - 1, |
|
| 472 | - ) ) ); |
|
| 473 | - |
|
| 474 | - return count( $posts ); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 479 | - * call. |
|
| 480 | - * |
|
| 481 | - * @since 3.15.0 |
|
| 482 | - * |
|
| 483 | - * @param array $args The arguments for a `get_posts` call. |
|
| 484 | - * |
|
| 485 | - * @return array The arguments for a `get_posts` call. |
|
| 486 | - */ |
|
| 487 | - public static function add_criterias( $args ) { |
|
| 488 | - |
|
| 489 | - return $args + array( |
|
| 490 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 491 | - 'tax_query' => array( |
|
| 492 | - array( |
|
| 493 | - 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 494 | - 'terms' => self::get_entity_terms(), |
|
| 495 | - ), |
|
| 496 | - ), |
|
| 497 | - ); |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - /** |
|
| 501 | - * Get the entity terms IDs which represent an entity. |
|
| 502 | - * |
|
| 503 | - * @since 3.15.0 |
|
| 504 | - * |
|
| 505 | - * @return array An array of terms' ids. |
|
| 506 | - */ |
|
| 507 | - public static function get_entity_terms() { |
|
| 508 | - |
|
| 509 | - $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 510 | - 'hide_empty' => false, |
|
| 511 | - // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 512 | - // An issue has been opened with the AAM plugin author as well. |
|
| 513 | - // |
|
| 514 | - // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 515 | - // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 516 | - 'fields' => 'all', |
|
| 517 | - ) ); |
|
| 518 | - |
|
| 519 | - return array_map( function ( $term ) { |
|
| 520 | - return $term->term_id; |
|
| 521 | - }, array_filter( $terms, function ( $term ) { |
|
| 522 | - return 'article' !== $term->slug; |
|
| 523 | - } ) ); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * Create a new entity. |
|
| 528 | - * |
|
| 529 | - * @since 3.9.0 |
|
| 530 | - * |
|
| 531 | - * @param string $name The entity name. |
|
| 532 | - * @param string $type_uri The entity's type URI. |
|
| 533 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 534 | - * @param string $status The post status, by default 'publish'. |
|
| 535 | - * |
|
| 536 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 537 | - */ |
|
| 538 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 539 | - |
|
| 540 | - // Create an entity for the publisher. |
|
| 541 | - $post_id = wp_insert_post( array( |
|
| 542 | - 'post_type' => self::TYPE_NAME, |
|
| 543 | - 'post_title' => $name, |
|
| 544 | - 'post_status' => $status, |
|
| 545 | - 'post_content' => '', |
|
| 546 | - ) ); |
|
| 547 | - |
|
| 548 | - // Return the error if any. |
|
| 549 | - if ( is_wp_error( $post_id ) ) { |
|
| 550 | - return $post_id; |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - // Set the entity logo. |
|
| 554 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 555 | - set_post_thumbnail( $post_id, $logo ); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - // Set the entity type. |
|
| 559 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 560 | - |
|
| 561 | - return $post_id; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - /** |
|
| 565 | - * Get the entities related to the one with the specified id. By default only |
|
| 566 | - * published entities will be returned. |
|
| 567 | - * |
|
| 568 | - * @since 3.10.0 |
|
| 569 | - * |
|
| 570 | - * @param int $id The post id. |
|
| 571 | - * @param string $post_status The target post status (default = publish). |
|
| 572 | - * |
|
| 573 | - * @return array An array of post ids. |
|
| 574 | - */ |
|
| 575 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 576 | - |
|
| 577 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Get the list of entities. |
|
| 582 | - * |
|
| 583 | - * @since 3.12.2 |
|
| 584 | - * |
|
| 585 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 586 | - * |
|
| 587 | - * @return array An array of entity posts. |
|
| 588 | - */ |
|
| 589 | - public function get( $params = array() ) { |
|
| 590 | - |
|
| 591 | - // Set the defaults. |
|
| 592 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 593 | - |
|
| 594 | - // Merge the defaults with the provided parameters. |
|
| 595 | - $args = wp_parse_args( $params, $defaults ); |
|
| 596 | - |
|
| 597 | - // Call the `get_posts` function. |
|
| 598 | - return get_posts( $args ); |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * The list of post type names which can be used for entities |
|
| 603 | - * |
|
| 604 | - * Criteria is that the post type is public. The list of valid post types |
|
| 605 | - * can be overridden with a filter. |
|
| 606 | - * |
|
| 607 | - * @since 3.15.0 |
|
| 608 | - * |
|
| 609 | - * @return array Array containing the names of the valid post types. |
|
| 610 | - */ |
|
| 611 | - static function valid_entity_post_types() { |
|
| 612 | - |
|
| 613 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 614 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 615 | - |
|
| 616 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 617 | - } |
|
| 63 | + /** |
|
| 64 | + * A singleton instance of the Entity service. |
|
| 65 | + * |
|
| 66 | + * @since 3.2.0 |
|
| 67 | + * @access private |
|
| 68 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 69 | + */ |
|
| 70 | + private static $instance; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Create a Wordlift_Entity_Service instance. |
|
| 74 | + * |
|
| 75 | + * @since 3.2.0 |
|
| 76 | + * |
|
| 77 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 78 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 79 | + */ |
|
| 80 | + public function __construct( $ui_service, $relation_service ) { |
|
| 81 | + |
|
| 82 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 83 | + |
|
| 84 | + $this->ui_service = $ui_service; |
|
| 85 | + $this->relation_service = $relation_service; |
|
| 86 | + |
|
| 87 | + // Set the singleton instance. |
|
| 88 | + self::$instance = $this; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get the singleton instance of the Entity service. |
|
| 93 | + * |
|
| 94 | + * @since 3.2.0 |
|
| 95 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 96 | + */ |
|
| 97 | + public static function get_instance() { |
|
| 98 | + |
|
| 99 | + return self::$instance; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 104 | + * something which is not an article. |
|
| 105 | + * |
|
| 106 | + * @since 3.1.0 |
|
| 107 | + * |
|
| 108 | + * @param int $post_id A post id. |
|
| 109 | + * |
|
| 110 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 111 | + */ |
|
| 112 | + public function is_entity( $post_id ) { |
|
| 113 | + |
|
| 114 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 115 | + |
|
| 116 | + if ( 0 === count( $terms ) ) { |
|
| 117 | + return false; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // We don't consider an `article` to be an entity. |
|
| 121 | + if ( 'article' !== $terms[0]->slug ) { |
|
| 122 | + return true; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + return false; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Get the proper classification scope for a given entity post |
|
| 130 | + * |
|
| 131 | + * @since 3.5.0 |
|
| 132 | + * |
|
| 133 | + * @param integer $post_id An entity post id. |
|
| 134 | + * |
|
| 135 | + * @param string $default The default classification scope, `what` if not |
|
| 136 | + * provided. |
|
| 137 | + * |
|
| 138 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 139 | + */ |
|
| 140 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 141 | + |
|
| 142 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 143 | + return $default; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // Retrieve the entity type |
|
| 147 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 148 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 149 | + // Retrieve classification boxes configuration |
|
| 150 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 151 | + foreach ( $classification_boxes as $cb ) { |
|
| 152 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 153 | + return $cb['id']; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return $default; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Check whether a {@link WP_Post} is used. |
|
| 162 | + * |
|
| 163 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 164 | + * |
|
| 165 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 166 | + */ |
|
| 167 | + public function is_used( $post_id ) { |
|
| 168 | + |
|
| 169 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 170 | + return null; |
|
| 171 | + } |
|
| 172 | + // Retrieve the post |
|
| 173 | + $entity = get_post( $post_id ); |
|
| 174 | + |
|
| 175 | + global $wpdb; |
|
| 176 | + // Retrieve Wordlift relation instances table name |
|
| 177 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 178 | + |
|
| 179 | + // Check is it's referenced / related to another post / entity |
|
| 180 | + $stmt = $wpdb->prepare( |
|
| 181 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 182 | + $entity->ID |
|
| 183 | + ); |
|
| 184 | + |
|
| 185 | + // Perform the query |
|
| 186 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 187 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 188 | + if ( 0 < $relation_instances ) { |
|
| 189 | + return true; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + // Check if the entity uri is used as meta_value |
|
| 193 | + $stmt = $wpdb->prepare( |
|
| 194 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 195 | + $entity->ID, |
|
| 196 | + wl_get_entity_uri( $entity->ID ) |
|
| 197 | + ); |
|
| 198 | + // Perform the query |
|
| 199 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 200 | + |
|
| 201 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 202 | + if ( 0 < $meta_instances ) { |
|
| 203 | + return true; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + // If we are here, it means the current entity is not used at the moment |
|
| 207 | + return false; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Determines whether a given uri is an internal uri or not. |
|
| 212 | + * |
|
| 213 | + * @since 3.3.2 |
|
| 214 | + * |
|
| 215 | + * @param int $uri An uri. |
|
| 216 | + * |
|
| 217 | + * @return true if the uri internal to the current dataset otherwise false. |
|
| 218 | + */ |
|
| 219 | + public function is_internal_uri( $uri ) { |
|
| 220 | + |
|
| 221 | + return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 226 | + * |
|
| 227 | + * @since 3.2.0 |
|
| 228 | + * |
|
| 229 | + * @param string $uri The entity URI. |
|
| 230 | + * |
|
| 231 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 232 | + */ |
|
| 233 | + public function get_entity_post_by_uri( $uri ) { |
|
| 234 | + |
|
| 235 | + // Check if we've been provided with a value otherwise return null. |
|
| 236 | + if ( empty( $uri ) ) { |
|
| 237 | + return null; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + $query_args = array( |
|
| 241 | + 'posts_per_page' => 1, |
|
| 242 | + 'post_status' => 'any', |
|
| 243 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 244 | + 'meta_query' => array( |
|
| 245 | + array( |
|
| 246 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 247 | + 'value' => $uri, |
|
| 248 | + 'compare' => '=', |
|
| 249 | + ), |
|
| 250 | + ), |
|
| 251 | + $tax_query = array( |
|
| 252 | + array( |
|
| 253 | + 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 254 | + 'field' => 'slug', |
|
| 255 | + 'terms' => 'article', |
|
| 256 | + 'operator' => 'NOT IN', |
|
| 257 | + ), |
|
| 258 | + ), |
|
| 259 | + ); |
|
| 260 | + |
|
| 261 | + // Only if the current uri is not an internal uri, entity search is |
|
| 262 | + // performed also looking at sameAs values. |
|
| 263 | + // |
|
| 264 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 265 | + if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 266 | + |
|
| 267 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 268 | + $query_args['meta_query'][] = array( |
|
| 269 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 270 | + 'value' => $uri, |
|
| 271 | + 'compare' => '=', |
|
| 272 | + ); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + $query = new WP_Query( $query_args ); |
|
| 276 | + |
|
| 277 | + // Get the matching entity posts. |
|
| 278 | + $posts = $query->get_posts(); |
|
| 279 | + |
|
| 280 | + // Return null if no post is found. |
|
| 281 | + if ( 0 === count( $posts ) ) { |
|
| 282 | + return null; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + // Return the found post. |
|
| 286 | + return $posts[0]; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 291 | + * we check that the post we're saving is the current post. |
|
| 292 | + * |
|
| 293 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 294 | + * |
|
| 295 | + * @since 3.2.0 |
|
| 296 | + * |
|
| 297 | + * @param int $post_id Post ID. |
|
| 298 | + * @param WP_Post $post Post object. |
|
| 299 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 300 | + */ |
|
| 301 | + public function save_post( $post_id, $post, $update ) { |
|
| 302 | + |
|
| 303 | + // Avoid doing anything if post is autosave or a revision. |
|
| 304 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 305 | + return; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + // We're setting the alternative label that have been provided via the UI |
|
| 309 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 310 | + // programmatically by some other function: we need to check therefore if |
|
| 311 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 312 | + // |
|
| 313 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 314 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 315 | + return; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + // Get the alt labels from the request (or empty array). |
|
| 319 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 320 | + |
|
| 321 | + // Set the alternative labels. |
|
| 322 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 323 | + |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Set the alternative labels. |
|
| 328 | + * |
|
| 329 | + * @since 3.2.0 |
|
| 330 | + * |
|
| 331 | + * @param int $post_id The post id. |
|
| 332 | + * @param array $alt_labels An array of labels. |
|
| 333 | + */ |
|
| 334 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 335 | + |
|
| 336 | + // Force $alt_labels to be an array |
|
| 337 | + if ( ! is_array( $alt_labels ) ) { |
|
| 338 | + $alt_labels = array( $alt_labels ); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 342 | + |
|
| 343 | + // Delete all the existing alternate labels. |
|
| 344 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 345 | + |
|
| 346 | + // Set the alternative labels. |
|
| 347 | + foreach ( $alt_labels as $alt_label ) { |
|
| 348 | + if ( ! empty( $alt_label ) ) { |
|
| 349 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Retrieve the alternate labels. |
|
| 357 | + * |
|
| 358 | + * @since 3.2.0 |
|
| 359 | + * |
|
| 360 | + * @param int $post_id Post id. |
|
| 361 | + * |
|
| 362 | + * @return mixed An array of alternative labels. |
|
| 363 | + */ |
|
| 364 | + public function get_alternative_labels( $post_id ) { |
|
| 365 | + |
|
| 366 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 371 | + * |
|
| 372 | + * @since 3.12.0 |
|
| 373 | + * |
|
| 374 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 375 | + * |
|
| 376 | + * @return array An array with the entity title and labels. |
|
| 377 | + */ |
|
| 378 | + public function get_labels( $post_id ) { |
|
| 379 | + |
|
| 380 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 385 | + * |
|
| 386 | + * @since 3.2.0 |
|
| 387 | + * |
|
| 388 | + * @param WP_Post $post Post object. |
|
| 389 | + */ |
|
| 390 | + public function edit_form_before_permalink( $post ) { |
|
| 391 | + |
|
| 392 | + // If it's not an entity, return. |
|
| 393 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 394 | + return; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + // Print the input template. |
|
| 398 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 399 | + |
|
| 400 | + // Print all the currently set alternative labels. |
|
| 401 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 402 | + |
|
| 403 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 404 | + |
|
| 405 | + }; |
|
| 406 | + |
|
| 407 | + // Print the button. |
|
| 408 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 409 | + |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Get the URI for the entity with the specified post id. |
|
| 414 | + * |
|
| 415 | + * @since 3.6.0 |
|
| 416 | + * |
|
| 417 | + * @param int $post_id The entity post id. |
|
| 418 | + * |
|
| 419 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 420 | + */ |
|
| 421 | + public function get_uri( $post_id ) { |
|
| 422 | + |
|
| 423 | + // If a null is given, nothing to do |
|
| 424 | + if ( null == $post_id ) { |
|
| 425 | + return null; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 429 | + |
|
| 430 | + // If the dataset uri is not properly configured, null is returned |
|
| 431 | + if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 432 | + return null; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + // Set the URI if it isn't set yet. |
|
| 436 | + $post_status = get_post_status( $post_id ); |
|
| 437 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 438 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 439 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + return $uri; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * Get the alternative label input HTML code. |
|
| 448 | + * |
|
| 449 | + * @since 3.2.0 |
|
| 450 | + * |
|
| 451 | + * @param string $value The input value. |
|
| 452 | + * |
|
| 453 | + * @return string The input HTML code. |
|
| 454 | + */ |
|
| 455 | + private function get_alternative_label_input( $value = '' ) { |
|
| 456 | + |
|
| 457 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + /** |
|
| 461 | + * Get the number of entity posts published in this blog. |
|
| 462 | + * |
|
| 463 | + * @since 3.6.0 |
|
| 464 | + * |
|
| 465 | + * @return int The number of published entity posts. |
|
| 466 | + */ |
|
| 467 | + public function count() { |
|
| 468 | + |
|
| 469 | + $posts = get_posts( $this->add_criterias( array( |
|
| 470 | + 'post_status' => 'any', |
|
| 471 | + 'numberposts' => - 1, |
|
| 472 | + ) ) ); |
|
| 473 | + |
|
| 474 | + return count( $posts ); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 479 | + * call. |
|
| 480 | + * |
|
| 481 | + * @since 3.15.0 |
|
| 482 | + * |
|
| 483 | + * @param array $args The arguments for a `get_posts` call. |
|
| 484 | + * |
|
| 485 | + * @return array The arguments for a `get_posts` call. |
|
| 486 | + */ |
|
| 487 | + public static function add_criterias( $args ) { |
|
| 488 | + |
|
| 489 | + return $args + array( |
|
| 490 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 491 | + 'tax_query' => array( |
|
| 492 | + array( |
|
| 493 | + 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 494 | + 'terms' => self::get_entity_terms(), |
|
| 495 | + ), |
|
| 496 | + ), |
|
| 497 | + ); |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * Get the entity terms IDs which represent an entity. |
|
| 502 | + * |
|
| 503 | + * @since 3.15.0 |
|
| 504 | + * |
|
| 505 | + * @return array An array of terms' ids. |
|
| 506 | + */ |
|
| 507 | + public static function get_entity_terms() { |
|
| 508 | + |
|
| 509 | + $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 510 | + 'hide_empty' => false, |
|
| 511 | + // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 512 | + // An issue has been opened with the AAM plugin author as well. |
|
| 513 | + // |
|
| 514 | + // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 515 | + // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 516 | + 'fields' => 'all', |
|
| 517 | + ) ); |
|
| 518 | + |
|
| 519 | + return array_map( function ( $term ) { |
|
| 520 | + return $term->term_id; |
|
| 521 | + }, array_filter( $terms, function ( $term ) { |
|
| 522 | + return 'article' !== $term->slug; |
|
| 523 | + } ) ); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * Create a new entity. |
|
| 528 | + * |
|
| 529 | + * @since 3.9.0 |
|
| 530 | + * |
|
| 531 | + * @param string $name The entity name. |
|
| 532 | + * @param string $type_uri The entity's type URI. |
|
| 533 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 534 | + * @param string $status The post status, by default 'publish'. |
|
| 535 | + * |
|
| 536 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 537 | + */ |
|
| 538 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 539 | + |
|
| 540 | + // Create an entity for the publisher. |
|
| 541 | + $post_id = wp_insert_post( array( |
|
| 542 | + 'post_type' => self::TYPE_NAME, |
|
| 543 | + 'post_title' => $name, |
|
| 544 | + 'post_status' => $status, |
|
| 545 | + 'post_content' => '', |
|
| 546 | + ) ); |
|
| 547 | + |
|
| 548 | + // Return the error if any. |
|
| 549 | + if ( is_wp_error( $post_id ) ) { |
|
| 550 | + return $post_id; |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + // Set the entity logo. |
|
| 554 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 555 | + set_post_thumbnail( $post_id, $logo ); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + // Set the entity type. |
|
| 559 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 560 | + |
|
| 561 | + return $post_id; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + /** |
|
| 565 | + * Get the entities related to the one with the specified id. By default only |
|
| 566 | + * published entities will be returned. |
|
| 567 | + * |
|
| 568 | + * @since 3.10.0 |
|
| 569 | + * |
|
| 570 | + * @param int $id The post id. |
|
| 571 | + * @param string $post_status The target post status (default = publish). |
|
| 572 | + * |
|
| 573 | + * @return array An array of post ids. |
|
| 574 | + */ |
|
| 575 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 576 | + |
|
| 577 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Get the list of entities. |
|
| 582 | + * |
|
| 583 | + * @since 3.12.2 |
|
| 584 | + * |
|
| 585 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 586 | + * |
|
| 587 | + * @return array An array of entity posts. |
|
| 588 | + */ |
|
| 589 | + public function get( $params = array() ) { |
|
| 590 | + |
|
| 591 | + // Set the defaults. |
|
| 592 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 593 | + |
|
| 594 | + // Merge the defaults with the provided parameters. |
|
| 595 | + $args = wp_parse_args( $params, $defaults ); |
|
| 596 | + |
|
| 597 | + // Call the `get_posts` function. |
|
| 598 | + return get_posts( $args ); |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * The list of post type names which can be used for entities |
|
| 603 | + * |
|
| 604 | + * Criteria is that the post type is public. The list of valid post types |
|
| 605 | + * can be overridden with a filter. |
|
| 606 | + * |
|
| 607 | + * @since 3.15.0 |
|
| 608 | + * |
|
| 609 | + * @return array Array containing the names of the valid post types. |
|
| 610 | + */ |
|
| 611 | + static function valid_entity_post_types() { |
|
| 612 | + |
|
| 613 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 614 | + $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 615 | + |
|
| 616 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 617 | + } |
|
| 618 | 618 | |
| 619 | 619 | } |
@@ -77,9 +77,9 @@ discard block |
||
| 77 | 77 | * @param \Wordlift_UI_Service $ui_service The UI service. |
| 78 | 78 | * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
| 79 | 79 | */ |
| 80 | - public function __construct( $ui_service, $relation_service ) { |
|
| 80 | + public function __construct($ui_service, $relation_service) { |
|
| 81 | 81 | |
| 82 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 82 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 83 | 83 | |
| 84 | 84 | $this->ui_service = $ui_service; |
| 85 | 85 | $this->relation_service = $relation_service; |
@@ -109,16 +109,16 @@ discard block |
||
| 109 | 109 | * |
| 110 | 110 | * @return bool Return true if the post is an entity otherwise false. |
| 111 | 111 | */ |
| 112 | - public function is_entity( $post_id ) { |
|
| 112 | + public function is_entity($post_id) { |
|
| 113 | 113 | |
| 114 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 114 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); |
|
| 115 | 115 | |
| 116 | - if ( 0 === count( $terms ) ) { |
|
| 116 | + if (0 === count($terms)) { |
|
| 117 | 117 | return false; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | // We don't consider an `article` to be an entity. |
| 121 | - if ( 'article' !== $terms[0]->slug ) { |
|
| 121 | + if ('article' !== $terms[0]->slug) { |
|
| 122 | 122 | return true; |
| 123 | 123 | } |
| 124 | 124 | |
@@ -137,19 +137,19 @@ discard block |
||
| 137 | 137 | * |
| 138 | 138 | * @return string Returns a classification scope (e.g. 'what'). |
| 139 | 139 | */ |
| 140 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 140 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 141 | 141 | |
| 142 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 142 | + if (false === $this->is_entity($post_id)) { |
|
| 143 | 143 | return $default; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // Retrieve the entity type |
| 147 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 148 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 147 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 148 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 149 | 149 | // Retrieve classification boxes configuration |
| 150 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 151 | - foreach ( $classification_boxes as $cb ) { |
|
| 152 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 150 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 151 | + foreach ($classification_boxes as $cb) { |
|
| 152 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 153 | 153 | return $cb['id']; |
| 154 | 154 | } |
| 155 | 155 | } |
@@ -164,13 +164,13 @@ discard block |
||
| 164 | 164 | * |
| 165 | 165 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 166 | 166 | */ |
| 167 | - public function is_used( $post_id ) { |
|
| 167 | + public function is_used($post_id) { |
|
| 168 | 168 | |
| 169 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 169 | + if (false === $this->is_entity($post_id)) { |
|
| 170 | 170 | return null; |
| 171 | 171 | } |
| 172 | 172 | // Retrieve the post |
| 173 | - $entity = get_post( $post_id ); |
|
| 173 | + $entity = get_post($post_id); |
|
| 174 | 174 | |
| 175 | 175 | global $wpdb; |
| 176 | 176 | // Retrieve Wordlift relation instances table name |
@@ -183,9 +183,9 @@ discard block |
||
| 183 | 183 | ); |
| 184 | 184 | |
| 185 | 185 | // Perform the query |
| 186 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 186 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 187 | 187 | // If there is at least one relation instance for the current entity, then it's used |
| 188 | - if ( 0 < $relation_instances ) { |
|
| 188 | + if (0 < $relation_instances) { |
|
| 189 | 189 | return true; |
| 190 | 190 | } |
| 191 | 191 | |
@@ -193,13 +193,13 @@ discard block |
||
| 193 | 193 | $stmt = $wpdb->prepare( |
| 194 | 194 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 195 | 195 | $entity->ID, |
| 196 | - wl_get_entity_uri( $entity->ID ) |
|
| 196 | + wl_get_entity_uri($entity->ID) |
|
| 197 | 197 | ); |
| 198 | 198 | // Perform the query |
| 199 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 199 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 200 | 200 | |
| 201 | 201 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 202 | - if ( 0 < $meta_instances ) { |
|
| 202 | + if (0 < $meta_instances) { |
|
| 203 | 203 | return true; |
| 204 | 204 | } |
| 205 | 205 | |
@@ -216,9 +216,9 @@ discard block |
||
| 216 | 216 | * |
| 217 | 217 | * @return true if the uri internal to the current dataset otherwise false. |
| 218 | 218 | */ |
| 219 | - public function is_internal_uri( $uri ) { |
|
| 219 | + public function is_internal_uri($uri) { |
|
| 220 | 220 | |
| 221 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 221 | + return (0 === strrpos($uri, wl_configuration_get_redlink_dataset_uri())); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
@@ -230,10 +230,10 @@ discard block |
||
| 230 | 230 | * |
| 231 | 231 | * @return WP_Post|null A WP_Post instance or null if not found. |
| 232 | 232 | */ |
| 233 | - public function get_entity_post_by_uri( $uri ) { |
|
| 233 | + public function get_entity_post_by_uri($uri) { |
|
| 234 | 234 | |
| 235 | 235 | // Check if we've been provided with a value otherwise return null. |
| 236 | - if ( empty( $uri ) ) { |
|
| 236 | + if (empty($uri)) { |
|
| 237 | 237 | return null; |
| 238 | 238 | } |
| 239 | 239 | |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | // performed also looking at sameAs values. |
| 263 | 263 | // |
| 264 | 264 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 265 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 265 | + if ( ! $this->is_internal_uri($uri)) { |
|
| 266 | 266 | |
| 267 | 267 | $query_args['meta_query']['relation'] = 'OR'; |
| 268 | 268 | $query_args['meta_query'][] = array( |
@@ -272,13 +272,13 @@ discard block |
||
| 272 | 272 | ); |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | - $query = new WP_Query( $query_args ); |
|
| 275 | + $query = new WP_Query($query_args); |
|
| 276 | 276 | |
| 277 | 277 | // Get the matching entity posts. |
| 278 | 278 | $posts = $query->get_posts(); |
| 279 | 279 | |
| 280 | 280 | // Return null if no post is found. |
| 281 | - if ( 0 === count( $posts ) ) { |
|
| 281 | + if (0 === count($posts)) { |
|
| 282 | 282 | return null; |
| 283 | 283 | } |
| 284 | 284 | |
@@ -298,10 +298,10 @@ discard block |
||
| 298 | 298 | * @param WP_Post $post Post object. |
| 299 | 299 | * @param bool $update Whether this is an existing post being updated or not. |
| 300 | 300 | */ |
| 301 | - public function save_post( $post_id, $post, $update ) { |
|
| 301 | + public function save_post($post_id, $post, $update) { |
|
| 302 | 302 | |
| 303 | 303 | // Avoid doing anything if post is autosave or a revision. |
| 304 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 304 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 305 | 305 | return; |
| 306 | 306 | } |
| 307 | 307 | |
@@ -311,15 +311,15 @@ discard block |
||
| 311 | 311 | // the $post_id in the save_post call matches the post id set in the request. |
| 312 | 312 | // |
| 313 | 313 | // If this is not the current post being saved or if it's not an entity, return. |
| 314 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 314 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 315 | 315 | return; |
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | // Get the alt labels from the request (or empty array). |
| 319 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 319 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 320 | 320 | |
| 321 | 321 | // Set the alternative labels. |
| 322 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 322 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 323 | 323 | |
| 324 | 324 | } |
| 325 | 325 | |
@@ -331,22 +331,22 @@ discard block |
||
| 331 | 331 | * @param int $post_id The post id. |
| 332 | 332 | * @param array $alt_labels An array of labels. |
| 333 | 333 | */ |
| 334 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 334 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 335 | 335 | |
| 336 | 336 | // Force $alt_labels to be an array |
| 337 | - if ( ! is_array( $alt_labels ) ) { |
|
| 338 | - $alt_labels = array( $alt_labels ); |
|
| 337 | + if ( ! is_array($alt_labels)) { |
|
| 338 | + $alt_labels = array($alt_labels); |
|
| 339 | 339 | } |
| 340 | 340 | |
| 341 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 341 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 342 | 342 | |
| 343 | 343 | // Delete all the existing alternate labels. |
| 344 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 344 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 345 | 345 | |
| 346 | 346 | // Set the alternative labels. |
| 347 | - foreach ( $alt_labels as $alt_label ) { |
|
| 348 | - if ( ! empty( $alt_label ) ) { |
|
| 349 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 347 | + foreach ($alt_labels as $alt_label) { |
|
| 348 | + if ( ! empty($alt_label)) { |
|
| 349 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 350 | 350 | } |
| 351 | 351 | } |
| 352 | 352 | |
@@ -361,9 +361,9 @@ discard block |
||
| 361 | 361 | * |
| 362 | 362 | * @return mixed An array of alternative labels. |
| 363 | 363 | */ |
| 364 | - public function get_alternative_labels( $post_id ) { |
|
| 364 | + public function get_alternative_labels($post_id) { |
|
| 365 | 365 | |
| 366 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 366 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 367 | 367 | } |
| 368 | 368 | |
| 369 | 369 | /** |
@@ -375,9 +375,9 @@ discard block |
||
| 375 | 375 | * |
| 376 | 376 | * @return array An array with the entity title and labels. |
| 377 | 377 | */ |
| 378 | - public function get_labels( $post_id ) { |
|
| 378 | + public function get_labels($post_id) { |
|
| 379 | 379 | |
| 380 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 380 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 381 | 381 | } |
| 382 | 382 | |
| 383 | 383 | /** |
@@ -387,25 +387,25 @@ discard block |
||
| 387 | 387 | * |
| 388 | 388 | * @param WP_Post $post Post object. |
| 389 | 389 | */ |
| 390 | - public function edit_form_before_permalink( $post ) { |
|
| 390 | + public function edit_form_before_permalink($post) { |
|
| 391 | 391 | |
| 392 | 392 | // If it's not an entity, return. |
| 393 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 393 | + if ( ! $this->is_entity($post->ID)) { |
|
| 394 | 394 | return; |
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | // Print the input template. |
| 398 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 398 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 399 | 399 | |
| 400 | 400 | // Print all the currently set alternative labels. |
| 401 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 401 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 402 | 402 | |
| 403 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 403 | + echo $this->get_alternative_label_input($alt_label); |
|
| 404 | 404 | |
| 405 | 405 | }; |
| 406 | 406 | |
| 407 | 407 | // Print the button. |
| 408 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 408 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 409 | 409 | |
| 410 | 410 | } |
| 411 | 411 | |
@@ -418,25 +418,25 @@ discard block |
||
| 418 | 418 | * |
| 419 | 419 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
| 420 | 420 | */ |
| 421 | - public function get_uri( $post_id ) { |
|
| 421 | + public function get_uri($post_id) { |
|
| 422 | 422 | |
| 423 | 423 | // If a null is given, nothing to do |
| 424 | - if ( null == $post_id ) { |
|
| 424 | + if (null == $post_id) { |
|
| 425 | 425 | return null; |
| 426 | 426 | } |
| 427 | 427 | |
| 428 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 428 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 429 | 429 | |
| 430 | 430 | // If the dataset uri is not properly configured, null is returned |
| 431 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 431 | + if ('' === wl_configuration_get_redlink_dataset_uri()) { |
|
| 432 | 432 | return null; |
| 433 | 433 | } |
| 434 | 434 | |
| 435 | 435 | // Set the URI if it isn't set yet. |
| 436 | - $post_status = get_post_status( $post_id ); |
|
| 437 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 438 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 439 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 436 | + $post_status = get_post_status($post_id); |
|
| 437 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 438 | + $uri = wl_build_entity_uri($post_id); |
|
| 439 | + wl_set_entity_uri($post_id, $uri); |
|
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | return $uri; |
@@ -452,9 +452,9 @@ discard block |
||
| 452 | 452 | * |
| 453 | 453 | * @return string The input HTML code. |
| 454 | 454 | */ |
| 455 | - private function get_alternative_label_input( $value = '' ) { |
|
| 455 | + private function get_alternative_label_input($value = '') { |
|
| 456 | 456 | |
| 457 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 457 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 458 | 458 | } |
| 459 | 459 | |
| 460 | 460 | /** |
@@ -466,12 +466,12 @@ discard block |
||
| 466 | 466 | */ |
| 467 | 467 | public function count() { |
| 468 | 468 | |
| 469 | - $posts = get_posts( $this->add_criterias( array( |
|
| 469 | + $posts = get_posts($this->add_criterias(array( |
|
| 470 | 470 | 'post_status' => 'any', |
| 471 | - 'numberposts' => - 1, |
|
| 472 | - ) ) ); |
|
| 471 | + 'numberposts' => -1, |
|
| 472 | + ))); |
|
| 473 | 473 | |
| 474 | - return count( $posts ); |
|
| 474 | + return count($posts); |
|
| 475 | 475 | } |
| 476 | 476 | |
| 477 | 477 | /** |
@@ -484,7 +484,7 @@ discard block |
||
| 484 | 484 | * |
| 485 | 485 | * @return array The arguments for a `get_posts` call. |
| 486 | 486 | */ |
| 487 | - public static function add_criterias( $args ) { |
|
| 487 | + public static function add_criterias($args) { |
|
| 488 | 488 | |
| 489 | 489 | return $args + array( |
| 490 | 490 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
@@ -506,7 +506,7 @@ discard block |
||
| 506 | 506 | */ |
| 507 | 507 | public static function get_entity_terms() { |
| 508 | 508 | |
| 509 | - $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 509 | + $terms = get_terms(Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 510 | 510 | 'hide_empty' => false, |
| 511 | 511 | // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
| 512 | 512 | // An issue has been opened with the AAM plugin author as well. |
@@ -514,13 +514,13 @@ discard block |
||
| 514 | 514 | // see https://github.com/insideout10/wordlift-plugin/issues/334 |
| 515 | 515 | // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
| 516 | 516 | 'fields' => 'all', |
| 517 | - ) ); |
|
| 517 | + )); |
|
| 518 | 518 | |
| 519 | - return array_map( function ( $term ) { |
|
| 519 | + return array_map(function($term) { |
|
| 520 | 520 | return $term->term_id; |
| 521 | - }, array_filter( $terms, function ( $term ) { |
|
| 521 | + }, array_filter($terms, function($term) { |
|
| 522 | 522 | return 'article' !== $term->slug; |
| 523 | - } ) ); |
|
| 523 | + } )); |
|
| 524 | 524 | } |
| 525 | 525 | |
| 526 | 526 | /** |
@@ -535,28 +535,28 @@ discard block |
||
| 535 | 535 | * |
| 536 | 536 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
| 537 | 537 | */ |
| 538 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 538 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 539 | 539 | |
| 540 | 540 | // Create an entity for the publisher. |
| 541 | - $post_id = wp_insert_post( array( |
|
| 541 | + $post_id = wp_insert_post(array( |
|
| 542 | 542 | 'post_type' => self::TYPE_NAME, |
| 543 | 543 | 'post_title' => $name, |
| 544 | 544 | 'post_status' => $status, |
| 545 | 545 | 'post_content' => '', |
| 546 | - ) ); |
|
| 546 | + )); |
|
| 547 | 547 | |
| 548 | 548 | // Return the error if any. |
| 549 | - if ( is_wp_error( $post_id ) ) { |
|
| 549 | + if (is_wp_error($post_id)) { |
|
| 550 | 550 | return $post_id; |
| 551 | 551 | } |
| 552 | 552 | |
| 553 | 553 | // Set the entity logo. |
| 554 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 555 | - set_post_thumbnail( $post_id, $logo ); |
|
| 554 | + if ($logo && is_numeric($logo)) { |
|
| 555 | + set_post_thumbnail($post_id, $logo); |
|
| 556 | 556 | } |
| 557 | 557 | |
| 558 | 558 | // Set the entity type. |
| 559 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 559 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 560 | 560 | |
| 561 | 561 | return $post_id; |
| 562 | 562 | } |
@@ -572,9 +572,9 @@ discard block |
||
| 572 | 572 | * |
| 573 | 573 | * @return array An array of post ids. |
| 574 | 574 | */ |
| 575 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 575 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 576 | 576 | |
| 577 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 577 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 578 | 578 | } |
| 579 | 579 | |
| 580 | 580 | /** |
@@ -586,16 +586,16 @@ discard block |
||
| 586 | 586 | * |
| 587 | 587 | * @return array An array of entity posts. |
| 588 | 588 | */ |
| 589 | - public function get( $params = array() ) { |
|
| 589 | + public function get($params = array()) { |
|
| 590 | 590 | |
| 591 | 591 | // Set the defaults. |
| 592 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 592 | + $defaults = array('post_type' => 'entity'); |
|
| 593 | 593 | |
| 594 | 594 | // Merge the defaults with the provided parameters. |
| 595 | - $args = wp_parse_args( $params, $defaults ); |
|
| 595 | + $args = wp_parse_args($params, $defaults); |
|
| 596 | 596 | |
| 597 | 597 | // Call the `get_posts` function. |
| 598 | - return get_posts( $args ); |
|
| 598 | + return get_posts($args); |
|
| 599 | 599 | } |
| 600 | 600 | |
| 601 | 601 | /** |
@@ -611,9 +611,9 @@ discard block |
||
| 611 | 611 | static function valid_entity_post_types() { |
| 612 | 612 | |
| 613 | 613 | // Ignore builtins in the call to avoid getting attachments. |
| 614 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 614 | + $post_types = array('post', 'page', self::TYPE_NAME); |
|
| 615 | 615 | |
| 616 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 616 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 617 | 617 | } |
| 618 | 618 | |
| 619 | 619 | } |