@@ -21,498 +21,498 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Entity_Service { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The Log service. |
|
| 26 | - * |
|
| 27 | - * @since 3.2.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 30 | - */ |
|
| 31 | - private $log; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.15.0 |
|
| 37 | - * @access private |
|
| 38 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 39 | - */ |
|
| 40 | - private $relation_service; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 44 | - * |
|
| 45 | - * @since 3.16.3 |
|
| 46 | - * @access private |
|
| 47 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 48 | - */ |
|
| 49 | - private $entity_uri_service; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * The entity post type name. |
|
| 53 | - * |
|
| 54 | - * @since 3.1.0 |
|
| 55 | - */ |
|
| 56 | - const TYPE_NAME = 'entity'; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * The alternative label meta key. |
|
| 60 | - * |
|
| 61 | - * @since 3.2.0 |
|
| 62 | - */ |
|
| 63 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * The alternative label input template. |
|
| 67 | - * |
|
| 68 | - * @since 3.2.0 |
|
| 69 | - */ |
|
| 70 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 71 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 24 | + /** |
|
| 25 | + * The Log service. |
|
| 26 | + * |
|
| 27 | + * @since 3.2.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 30 | + */ |
|
| 31 | + private $log; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.15.0 |
|
| 37 | + * @access private |
|
| 38 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 39 | + */ |
|
| 40 | + private $relation_service; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 44 | + * |
|
| 45 | + * @since 3.16.3 |
|
| 46 | + * @access private |
|
| 47 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 48 | + */ |
|
| 49 | + private $entity_uri_service; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * The entity post type name. |
|
| 53 | + * |
|
| 54 | + * @since 3.1.0 |
|
| 55 | + */ |
|
| 56 | + const TYPE_NAME = 'entity'; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * The alternative label meta key. |
|
| 60 | + * |
|
| 61 | + * @since 3.2.0 |
|
| 62 | + */ |
|
| 63 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * The alternative label input template. |
|
| 67 | + * |
|
| 68 | + * @since 3.2.0 |
|
| 69 | + */ |
|
| 70 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 71 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 72 | 72 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 73 | 73 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 74 | 74 | <button class="button wl-delete-button">%s</button> |
| 75 | 75 | </div>'; |
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Create a Wordlift_Entity_Service instance. |
|
| 79 | - * |
|
| 80 | - * @throws Exception if the `$content_service` is not of the `Content_Service` type. |
|
| 81 | - * @since 3.2.0 |
|
| 82 | - */ |
|
| 83 | - protected function __construct() { |
|
| 84 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 85 | - |
|
| 86 | - $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
| 87 | - $this->relation_service = Wordlift_Relation_Service::get_instance(); |
|
| 88 | - |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * A singleton instance of the Entity service. |
|
| 93 | - * |
|
| 94 | - * @since 3.2.0 |
|
| 95 | - * @access private |
|
| 96 | - * @var Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 97 | - */ |
|
| 98 | - private static $instance = null; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get the singleton instance of the Entity service. |
|
| 102 | - * |
|
| 103 | - * @return Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 104 | - * @since 3.2.0 |
|
| 105 | - */ |
|
| 106 | - public static function get_instance() { |
|
| 107 | - |
|
| 108 | - if ( ! isset( self::$instance ) ) { |
|
| 109 | - self::$instance = new self(); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return self::$instance; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 117 | - * something which is not an article. |
|
| 118 | - * |
|
| 119 | - * @param int $post_id A post id. |
|
| 120 | - * |
|
| 121 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 122 | - * @since 3.1.0 |
|
| 123 | - */ |
|
| 124 | - public function is_entity( $post_id ) { |
|
| 125 | - |
|
| 126 | - // Improve performance by giving for granted that a product is an entity. |
|
| 127 | - if ( 'product' === get_post_type( $post_id ) ) { |
|
| 128 | - return true; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 132 | - |
|
| 133 | - if ( is_wp_error( $terms ) ) { |
|
| 134 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 135 | - |
|
| 136 | - return false; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if ( empty( $terms ) ) { |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /* |
|
| 77 | + /** |
|
| 78 | + * Create a Wordlift_Entity_Service instance. |
|
| 79 | + * |
|
| 80 | + * @throws Exception if the `$content_service` is not of the `Content_Service` type. |
|
| 81 | + * @since 3.2.0 |
|
| 82 | + */ |
|
| 83 | + protected function __construct() { |
|
| 84 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 85 | + |
|
| 86 | + $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
| 87 | + $this->relation_service = Wordlift_Relation_Service::get_instance(); |
|
| 88 | + |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * A singleton instance of the Entity service. |
|
| 93 | + * |
|
| 94 | + * @since 3.2.0 |
|
| 95 | + * @access private |
|
| 96 | + * @var Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 97 | + */ |
|
| 98 | + private static $instance = null; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get the singleton instance of the Entity service. |
|
| 102 | + * |
|
| 103 | + * @return Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 104 | + * @since 3.2.0 |
|
| 105 | + */ |
|
| 106 | + public static function get_instance() { |
|
| 107 | + |
|
| 108 | + if ( ! isset( self::$instance ) ) { |
|
| 109 | + self::$instance = new self(); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return self::$instance; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 117 | + * something which is not an article. |
|
| 118 | + * |
|
| 119 | + * @param int $post_id A post id. |
|
| 120 | + * |
|
| 121 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 122 | + * @since 3.1.0 |
|
| 123 | + */ |
|
| 124 | + public function is_entity( $post_id ) { |
|
| 125 | + |
|
| 126 | + // Improve performance by giving for granted that a product is an entity. |
|
| 127 | + if ( 'product' === get_post_type( $post_id ) ) { |
|
| 128 | + return true; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 132 | + |
|
| 133 | + if ( is_wp_error( $terms ) ) { |
|
| 134 | + $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 135 | + |
|
| 136 | + return false; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if ( empty( $terms ) ) { |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /* |
|
| 144 | 144 | * We don't consider an `article` to be an entity. |
| 145 | 145 | * |
| 146 | 146 | * @since 3.20.0 At least one associated mustn't be an `article`. |
| 147 | 147 | * |
| 148 | 148 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 149 | 149 | */ |
| 150 | - foreach ( $terms as $term ) { |
|
| 151 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 152 | - return true; |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - return false; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Get the proper classification scope for a given entity post |
|
| 161 | - * |
|
| 162 | - * @param integer $post_id An entity post id. |
|
| 163 | - * |
|
| 164 | - * @param string $default The default classification scope, `what` if not |
|
| 165 | - * provided. |
|
| 166 | - * |
|
| 167 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 168 | - * @since 3.5.0 |
|
| 169 | - */ |
|
| 170 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 171 | - |
|
| 172 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 173 | - return $default; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // Retrieve the entity type |
|
| 177 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 178 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 179 | - // Retrieve classification boxes configuration |
|
| 180 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
| 181 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 182 | - foreach ( $classification_boxes as $cb ) { |
|
| 183 | - if ( in_array( $entity_type, $cb['registeredTypes'], true ) ) { |
|
| 184 | - return $cb['id']; |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return $default; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Check whether a {@link WP_Post} is used. |
|
| 193 | - * |
|
| 194 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 195 | - * |
|
| 196 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 197 | - */ |
|
| 198 | - public function is_used( $post_id ) { |
|
| 199 | - |
|
| 200 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 201 | - return null; |
|
| 202 | - } |
|
| 203 | - // Retrieve the post |
|
| 204 | - $entity = get_post( $post_id ); |
|
| 205 | - |
|
| 206 | - global $wpdb; |
|
| 207 | - |
|
| 208 | - // Perform the query |
|
| 209 | - $relation_instances = (int) $wpdb->get_var( |
|
| 210 | - $wpdb->prepare( |
|
| 211 | - "SELECT COUNT(*) FROM {$wpdb->prefix}wl_relation_instances WHERE object_id = %d", |
|
| 212 | - $entity->ID |
|
| 213 | - ) |
|
| 214 | - ); |
|
| 215 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 216 | - if ( 0 < $relation_instances ) { |
|
| 217 | - return true; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - // Perform the query |
|
| 221 | - $meta_instances = (int) $wpdb->get_var( |
|
| 222 | - $wpdb->prepare( |
|
| 223 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 224 | - $entity->ID, |
|
| 225 | - wl_get_entity_uri( $entity->ID ) |
|
| 226 | - ) |
|
| 227 | - ); |
|
| 228 | - |
|
| 229 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 230 | - if ( 0 < $meta_instances ) { |
|
| 231 | - return true; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // If we are here, it means the current entity is not used at the moment |
|
| 235 | - return false; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 240 | - * |
|
| 241 | - * @param string $uri The entity URI. |
|
| 242 | - * |
|
| 243 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 244 | - * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 245 | - * |
|
| 246 | - * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 247 | - * @since 3.2.0 |
|
| 248 | - */ |
|
| 249 | - public function get_entity_post_by_uri( $uri ) { |
|
| 250 | - |
|
| 251 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 256 | - * we check that the post we're saving is the current post. |
|
| 257 | - * |
|
| 258 | - * This function is called by a hook, so we're not the ones that need to check the `nonce`. |
|
| 259 | - * |
|
| 260 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 261 | - * |
|
| 262 | - * @since 3.2.0 |
|
| 263 | - * |
|
| 264 | - * @param int $post_id Post ID. |
|
| 265 | - * @param WP_Post $post Post object. |
|
| 266 | - */ |
|
| 267 | - public function save_post( $post_id, $post ) { |
|
| 268 | - // Avoid doing anything if post is autosave or a revision. |
|
| 269 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 270 | - return; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - // We expect a numeric value here. |
|
| 274 | - if ( ! isset( $_REQUEST['post_ID'] ) || ! is_numeric( $_REQUEST['post_ID'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 275 | - return; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - // Get the numeric post ID from the request. |
|
| 279 | - $request_post_id = intval( $_REQUEST['post_ID'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 280 | - |
|
| 281 | - // We're setting the alternative label that have been provided via the UI |
|
| 282 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 283 | - // programmatically by some other function: we need to check therefore if |
|
| 284 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 285 | - // |
|
| 286 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 287 | - if ( $request_post_id !== $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 288 | - return; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - if ( isset( $_REQUEST['wl_alternative_label'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 292 | - $data = filter_var_array( $_REQUEST, array( 'wl_alternative_label' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 293 | - // Get the alt labels from the request (or empty array). |
|
| 294 | - $alt_labels = isset( $data['wl_alternative_label'] ) ? $data['wl_alternative_label'] : array(); |
|
| 295 | - // This is via classic editor, so set the alternative labels. |
|
| 296 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Set the alternative labels. |
|
| 303 | - * |
|
| 304 | - * @param int $post_id The post id. |
|
| 305 | - * @param array $alt_labels An array of labels. |
|
| 306 | - * |
|
| 307 | - * @since 3.2.0 |
|
| 308 | - */ |
|
| 309 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 310 | - |
|
| 311 | - // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 312 | - // instead of post id. |
|
| 313 | - if ( ! is_numeric( $post_id ) ) { |
|
| 314 | - return; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - // Force $alt_labels to be an array |
|
| 318 | - if ( ! is_array( $alt_labels ) ) { |
|
| 319 | - $alt_labels = array( $alt_labels ); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . ' ]' ); |
|
| 323 | - |
|
| 324 | - // Delete all the existing alternate labels. |
|
| 325 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 326 | - |
|
| 327 | - // Save only unique synonymns. |
|
| 328 | - $alt_labels = array_unique( $alt_labels ); |
|
| 329 | - |
|
| 330 | - // Set the alternative labels. |
|
| 331 | - foreach ( $alt_labels as $alt_label ) { |
|
| 332 | - |
|
| 333 | - // Strip html code from synonym. |
|
| 334 | - $alt_label = wp_strip_all_tags( $alt_label ); |
|
| 335 | - |
|
| 336 | - if ( ! empty( $alt_label ) ) { |
|
| 337 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
| 344 | - |
|
| 345 | - $merged_labels = $this->get_alternative_labels( $post_id ); |
|
| 346 | - |
|
| 347 | - // Append new synonyms to the end. |
|
| 348 | - $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
| 349 | - |
|
| 350 | - $this->set_alternative_labels( $post_id, $merged_labels ); |
|
| 351 | - |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Retrieve the alternate labels. |
|
| 356 | - * |
|
| 357 | - * @param int $post_id Post id. |
|
| 358 | - * |
|
| 359 | - * @return mixed An array of alternative labels. |
|
| 360 | - * @since 3.2.0 |
|
| 361 | - */ |
|
| 362 | - public function get_alternative_labels( $post_id ) { |
|
| 363 | - |
|
| 364 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 369 | - * |
|
| 370 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 371 | - * @param int $object_type The object type {@link Object_Type_Enum} |
|
| 372 | - * |
|
| 373 | - * @return array An array with the entity title and labels. |
|
| 374 | - * @since 3.12.0 |
|
| 375 | - */ |
|
| 376 | - public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 377 | - if ( Object_Type_Enum::POST === $object_type ) { |
|
| 378 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - // Term Reference dont have synonyms yet. |
|
| 382 | - return array(); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 387 | - * |
|
| 388 | - * @param WP_Post $post Post object. |
|
| 389 | - * |
|
| 390 | - * @since 3.2.0 |
|
| 391 | - */ |
|
| 392 | - public function edit_form_before_permalink( $post ) { |
|
| 393 | - |
|
| 394 | - // If it's not an entity, return. |
|
| 395 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 396 | - return; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - // If disabled by filter, return. |
|
| 400 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 401 | - if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 402 | - return; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - // Print the input template. |
|
| 406 | - Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 407 | - |
|
| 408 | - // Print all the currently set alternative labels. |
|
| 409 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 410 | - |
|
| 411 | - echo wp_kses( $this->get_alternative_label_input( $alt_label ), Wordlift_UI_Service::get_template_allowed_html() ); |
|
| 412 | - |
|
| 413 | - }; |
|
| 414 | - |
|
| 415 | - // Print the button. |
|
| 416 | - Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 417 | - |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 421 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
| 422 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 423 | - $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
|
| 424 | - |
|
| 425 | - if ( ! isset( $entity_id ) || |
|
| 426 | - ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
| 427 | - $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
| 428 | - try { |
|
| 429 | - $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
| 430 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 431 | - } catch ( Exception $e ) { |
|
| 432 | - return null; |
|
| 433 | - } |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - return $entity_id; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Get the alternative label input HTML code. |
|
| 441 | - * |
|
| 442 | - * @param string $value The input value. |
|
| 443 | - * |
|
| 444 | - * @return string The input HTML code. |
|
| 445 | - * @since 3.2.0 |
|
| 446 | - */ |
|
| 447 | - private function get_alternative_label_input( $value = '' ) { |
|
| 448 | - |
|
| 449 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), esc_html__( 'Delete', 'wordlift' ) ); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Get the number of entity posts published in this blog. |
|
| 454 | - * |
|
| 455 | - * @return int The number of published entity posts. |
|
| 456 | - * @since 3.6.0 |
|
| 457 | - */ |
|
| 458 | - public function count() { |
|
| 459 | - global $wpdb; |
|
| 460 | - |
|
| 461 | - // Try to get the count from the transient. |
|
| 462 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 463 | - if ( false !== $count ) { |
|
| 464 | - return $count; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - // Query the count. |
|
| 468 | - $count = $wpdb->get_var( |
|
| 469 | - $wpdb->prepare( |
|
| 470 | - 'SELECT COUNT( DISTINCT( tr.object_id ) )' |
|
| 471 | - . " FROM {$wpdb->term_relationships} tr" |
|
| 472 | - . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 473 | - . ' ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id' |
|
| 474 | - . " INNER JOIN {$wpdb->terms} t" |
|
| 475 | - . ' ON t.term_id = tt.term_id AND t.name != %s', |
|
| 476 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 477 | - 'article' |
|
| 478 | - ) |
|
| 479 | - ); |
|
| 480 | - |
|
| 481 | - // Store the count in cache. |
|
| 482 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 483 | - |
|
| 484 | - return $count; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 489 | - * call. |
|
| 490 | - * |
|
| 491 | - * @param array $args The arguments for a `get_posts` call. |
|
| 492 | - * |
|
| 493 | - * @return array The arguments for a `get_posts` call. |
|
| 494 | - * @since 3.15.0 |
|
| 495 | - */ |
|
| 496 | - public static function add_criterias( $args ) { |
|
| 497 | - |
|
| 498 | - // Build an optimal tax-query. |
|
| 499 | - $tax_query = array( |
|
| 500 | - 'relation' => 'AND', |
|
| 501 | - array( |
|
| 502 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 503 | - 'operator' => 'EXISTS', |
|
| 504 | - ), |
|
| 505 | - array( |
|
| 506 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 507 | - 'field' => 'slug', |
|
| 508 | - 'terms' => 'article', |
|
| 509 | - 'operator' => 'NOT IN', |
|
| 510 | - ), |
|
| 511 | - ); |
|
| 512 | - |
|
| 513 | - return $args + array( |
|
| 514 | - 'post_type' => self::valid_entity_post_types(), |
|
| 515 | - /* |
|
| 150 | + foreach ( $terms as $term ) { |
|
| 151 | + if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 152 | + return true; |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + return false; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Get the proper classification scope for a given entity post |
|
| 161 | + * |
|
| 162 | + * @param integer $post_id An entity post id. |
|
| 163 | + * |
|
| 164 | + * @param string $default The default classification scope, `what` if not |
|
| 165 | + * provided. |
|
| 166 | + * |
|
| 167 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 168 | + * @since 3.5.0 |
|
| 169 | + */ |
|
| 170 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 171 | + |
|
| 172 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 173 | + return $default; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // Retrieve the entity type |
|
| 177 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 178 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 179 | + // Retrieve classification boxes configuration |
|
| 180 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
| 181 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 182 | + foreach ( $classification_boxes as $cb ) { |
|
| 183 | + if ( in_array( $entity_type, $cb['registeredTypes'], true ) ) { |
|
| 184 | + return $cb['id']; |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return $default; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Check whether a {@link WP_Post} is used. |
|
| 193 | + * |
|
| 194 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 195 | + * |
|
| 196 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 197 | + */ |
|
| 198 | + public function is_used( $post_id ) { |
|
| 199 | + |
|
| 200 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 201 | + return null; |
|
| 202 | + } |
|
| 203 | + // Retrieve the post |
|
| 204 | + $entity = get_post( $post_id ); |
|
| 205 | + |
|
| 206 | + global $wpdb; |
|
| 207 | + |
|
| 208 | + // Perform the query |
|
| 209 | + $relation_instances = (int) $wpdb->get_var( |
|
| 210 | + $wpdb->prepare( |
|
| 211 | + "SELECT COUNT(*) FROM {$wpdb->prefix}wl_relation_instances WHERE object_id = %d", |
|
| 212 | + $entity->ID |
|
| 213 | + ) |
|
| 214 | + ); |
|
| 215 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 216 | + if ( 0 < $relation_instances ) { |
|
| 217 | + return true; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + // Perform the query |
|
| 221 | + $meta_instances = (int) $wpdb->get_var( |
|
| 222 | + $wpdb->prepare( |
|
| 223 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 224 | + $entity->ID, |
|
| 225 | + wl_get_entity_uri( $entity->ID ) |
|
| 226 | + ) |
|
| 227 | + ); |
|
| 228 | + |
|
| 229 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 230 | + if ( 0 < $meta_instances ) { |
|
| 231 | + return true; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // If we are here, it means the current entity is not used at the moment |
|
| 235 | + return false; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 240 | + * |
|
| 241 | + * @param string $uri The entity URI. |
|
| 242 | + * |
|
| 243 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 244 | + * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 245 | + * |
|
| 246 | + * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 247 | + * @since 3.2.0 |
|
| 248 | + */ |
|
| 249 | + public function get_entity_post_by_uri( $uri ) { |
|
| 250 | + |
|
| 251 | + return $this->entity_uri_service->get_entity( $uri ); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 256 | + * we check that the post we're saving is the current post. |
|
| 257 | + * |
|
| 258 | + * This function is called by a hook, so we're not the ones that need to check the `nonce`. |
|
| 259 | + * |
|
| 260 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 261 | + * |
|
| 262 | + * @since 3.2.0 |
|
| 263 | + * |
|
| 264 | + * @param int $post_id Post ID. |
|
| 265 | + * @param WP_Post $post Post object. |
|
| 266 | + */ |
|
| 267 | + public function save_post( $post_id, $post ) { |
|
| 268 | + // Avoid doing anything if post is autosave or a revision. |
|
| 269 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 270 | + return; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + // We expect a numeric value here. |
|
| 274 | + if ( ! isset( $_REQUEST['post_ID'] ) || ! is_numeric( $_REQUEST['post_ID'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 275 | + return; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + // Get the numeric post ID from the request. |
|
| 279 | + $request_post_id = intval( $_REQUEST['post_ID'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 280 | + |
|
| 281 | + // We're setting the alternative label that have been provided via the UI |
|
| 282 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 283 | + // programmatically by some other function: we need to check therefore if |
|
| 284 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 285 | + // |
|
| 286 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 287 | + if ( $request_post_id !== $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 288 | + return; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + if ( isset( $_REQUEST['wl_alternative_label'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 292 | + $data = filter_var_array( $_REQUEST, array( 'wl_alternative_label' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 293 | + // Get the alt labels from the request (or empty array). |
|
| 294 | + $alt_labels = isset( $data['wl_alternative_label'] ) ? $data['wl_alternative_label'] : array(); |
|
| 295 | + // This is via classic editor, so set the alternative labels. |
|
| 296 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Set the alternative labels. |
|
| 303 | + * |
|
| 304 | + * @param int $post_id The post id. |
|
| 305 | + * @param array $alt_labels An array of labels. |
|
| 306 | + * |
|
| 307 | + * @since 3.2.0 |
|
| 308 | + */ |
|
| 309 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 310 | + |
|
| 311 | + // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 312 | + // instead of post id. |
|
| 313 | + if ( ! is_numeric( $post_id ) ) { |
|
| 314 | + return; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + // Force $alt_labels to be an array |
|
| 318 | + if ( ! is_array( $alt_labels ) ) { |
|
| 319 | + $alt_labels = array( $alt_labels ); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . ' ]' ); |
|
| 323 | + |
|
| 324 | + // Delete all the existing alternate labels. |
|
| 325 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 326 | + |
|
| 327 | + // Save only unique synonymns. |
|
| 328 | + $alt_labels = array_unique( $alt_labels ); |
|
| 329 | + |
|
| 330 | + // Set the alternative labels. |
|
| 331 | + foreach ( $alt_labels as $alt_label ) { |
|
| 332 | + |
|
| 333 | + // Strip html code from synonym. |
|
| 334 | + $alt_label = wp_strip_all_tags( $alt_label ); |
|
| 335 | + |
|
| 336 | + if ( ! empty( $alt_label ) ) { |
|
| 337 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
| 344 | + |
|
| 345 | + $merged_labels = $this->get_alternative_labels( $post_id ); |
|
| 346 | + |
|
| 347 | + // Append new synonyms to the end. |
|
| 348 | + $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
| 349 | + |
|
| 350 | + $this->set_alternative_labels( $post_id, $merged_labels ); |
|
| 351 | + |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Retrieve the alternate labels. |
|
| 356 | + * |
|
| 357 | + * @param int $post_id Post id. |
|
| 358 | + * |
|
| 359 | + * @return mixed An array of alternative labels. |
|
| 360 | + * @since 3.2.0 |
|
| 361 | + */ |
|
| 362 | + public function get_alternative_labels( $post_id ) { |
|
| 363 | + |
|
| 364 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 369 | + * |
|
| 370 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 371 | + * @param int $object_type The object type {@link Object_Type_Enum} |
|
| 372 | + * |
|
| 373 | + * @return array An array with the entity title and labels. |
|
| 374 | + * @since 3.12.0 |
|
| 375 | + */ |
|
| 376 | + public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 377 | + if ( Object_Type_Enum::POST === $object_type ) { |
|
| 378 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + // Term Reference dont have synonyms yet. |
|
| 382 | + return array(); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 387 | + * |
|
| 388 | + * @param WP_Post $post Post object. |
|
| 389 | + * |
|
| 390 | + * @since 3.2.0 |
|
| 391 | + */ |
|
| 392 | + public function edit_form_before_permalink( $post ) { |
|
| 393 | + |
|
| 394 | + // If it's not an entity, return. |
|
| 395 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 396 | + return; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + // If disabled by filter, return. |
|
| 400 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 401 | + if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 402 | + return; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + // Print the input template. |
|
| 406 | + Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 407 | + |
|
| 408 | + // Print all the currently set alternative labels. |
|
| 409 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 410 | + |
|
| 411 | + echo wp_kses( $this->get_alternative_label_input( $alt_label ), Wordlift_UI_Service::get_template_allowed_html() ); |
|
| 412 | + |
|
| 413 | + }; |
|
| 414 | + |
|
| 415 | + // Print the button. |
|
| 416 | + Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 417 | + |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 421 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
| 422 | + $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 423 | + $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
|
| 424 | + |
|
| 425 | + if ( ! isset( $entity_id ) || |
|
| 426 | + ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
| 427 | + $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
| 428 | + try { |
|
| 429 | + $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
| 430 | + $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 431 | + } catch ( Exception $e ) { |
|
| 432 | + return null; |
|
| 433 | + } |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + return $entity_id; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Get the alternative label input HTML code. |
|
| 441 | + * |
|
| 442 | + * @param string $value The input value. |
|
| 443 | + * |
|
| 444 | + * @return string The input HTML code. |
|
| 445 | + * @since 3.2.0 |
|
| 446 | + */ |
|
| 447 | + private function get_alternative_label_input( $value = '' ) { |
|
| 448 | + |
|
| 449 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), esc_html__( 'Delete', 'wordlift' ) ); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Get the number of entity posts published in this blog. |
|
| 454 | + * |
|
| 455 | + * @return int The number of published entity posts. |
|
| 456 | + * @since 3.6.0 |
|
| 457 | + */ |
|
| 458 | + public function count() { |
|
| 459 | + global $wpdb; |
|
| 460 | + |
|
| 461 | + // Try to get the count from the transient. |
|
| 462 | + $count = get_transient( '_wl_entity_service__count' ); |
|
| 463 | + if ( false !== $count ) { |
|
| 464 | + return $count; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + // Query the count. |
|
| 468 | + $count = $wpdb->get_var( |
|
| 469 | + $wpdb->prepare( |
|
| 470 | + 'SELECT COUNT( DISTINCT( tr.object_id ) )' |
|
| 471 | + . " FROM {$wpdb->term_relationships} tr" |
|
| 472 | + . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 473 | + . ' ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id' |
|
| 474 | + . " INNER JOIN {$wpdb->terms} t" |
|
| 475 | + . ' ON t.term_id = tt.term_id AND t.name != %s', |
|
| 476 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 477 | + 'article' |
|
| 478 | + ) |
|
| 479 | + ); |
|
| 480 | + |
|
| 481 | + // Store the count in cache. |
|
| 482 | + set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 483 | + |
|
| 484 | + return $count; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 489 | + * call. |
|
| 490 | + * |
|
| 491 | + * @param array $args The arguments for a `get_posts` call. |
|
| 492 | + * |
|
| 493 | + * @return array The arguments for a `get_posts` call. |
|
| 494 | + * @since 3.15.0 |
|
| 495 | + */ |
|
| 496 | + public static function add_criterias( $args ) { |
|
| 497 | + |
|
| 498 | + // Build an optimal tax-query. |
|
| 499 | + $tax_query = array( |
|
| 500 | + 'relation' => 'AND', |
|
| 501 | + array( |
|
| 502 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 503 | + 'operator' => 'EXISTS', |
|
| 504 | + ), |
|
| 505 | + array( |
|
| 506 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 507 | + 'field' => 'slug', |
|
| 508 | + 'terms' => 'article', |
|
| 509 | + 'operator' => 'NOT IN', |
|
| 510 | + ), |
|
| 511 | + ); |
|
| 512 | + |
|
| 513 | + return $args + array( |
|
| 514 | + 'post_type' => self::valid_entity_post_types(), |
|
| 515 | + /* |
|
| 516 | 516 | * Ensure compatibility with Polylang. |
| 517 | 517 | * |
| 518 | 518 | * @see https://github.com/insideout10/wordlift-plugin/issues/855. |
@@ -520,101 +520,101 @@ discard block |
||
| 520 | 520 | * |
| 521 | 521 | * @since 3.19.5 |
| 522 | 522 | */ |
| 523 | - 'lang' => '', |
|
| 524 | - 'tax_query' => $tax_query, |
|
| 525 | - ); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - /** |
|
| 529 | - * Create a new entity. |
|
| 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 | - * @since 3.9.0 |
|
| 538 | - */ |
|
| 539 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 540 | - |
|
| 541 | - // Create an entity for the publisher. |
|
| 542 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 543 | - $post_id = @wp_insert_post( |
|
| 544 | - array( |
|
| 545 | - 'post_type' => self::TYPE_NAME, |
|
| 546 | - 'post_title' => $name, |
|
| 547 | - 'post_status' => $status, |
|
| 548 | - 'post_content' => '', |
|
| 549 | - ) |
|
| 550 | - ); |
|
| 551 | - |
|
| 552 | - // Return the error if any. |
|
| 553 | - if ( is_wp_error( $post_id ) ) { |
|
| 554 | - return $post_id; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - // Set the entity logo. |
|
| 558 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 559 | - set_post_thumbnail( $post_id, $logo ); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - // Set the entity type. |
|
| 563 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 564 | - |
|
| 565 | - return $post_id; |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * Get the entities related to the one with the specified id. By default only |
|
| 570 | - * published entities will be returned. |
|
| 571 | - * |
|
| 572 | - * @param int $id The post id. |
|
| 573 | - * @param string $post_status The target post status (default = publish). |
|
| 574 | - * |
|
| 575 | - * @return array An array of post ids. |
|
| 576 | - * @since 3.10.0 |
|
| 577 | - */ |
|
| 578 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 579 | - |
|
| 580 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * Get the list of entities. |
|
| 585 | - * |
|
| 586 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 587 | - * |
|
| 588 | - * @return array An array of entity posts. |
|
| 589 | - * @since 3.12.2 |
|
| 590 | - */ |
|
| 591 | - public function get( $params = array() ) { |
|
| 592 | - |
|
| 593 | - // Set the defaults. |
|
| 594 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 595 | - |
|
| 596 | - // Merge the defaults with the provided parameters. |
|
| 597 | - $args = wp_parse_args( $params, $defaults ); |
|
| 598 | - |
|
| 599 | - // Call the `get_posts` function. |
|
| 600 | - return get_posts( $args ); |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * The list of post type names which can be used for entities |
|
| 605 | - * |
|
| 606 | - * Criteria is that the post type is public. The list of valid post types |
|
| 607 | - * can be overridden with a filter. |
|
| 608 | - * |
|
| 609 | - * @return array Array containing the names of the valid post types. |
|
| 610 | - * @since 3.15.0 |
|
| 611 | - */ |
|
| 612 | - public static function valid_entity_post_types() { |
|
| 613 | - |
|
| 614 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 615 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 616 | - |
|
| 617 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 618 | - } |
|
| 523 | + 'lang' => '', |
|
| 524 | + 'tax_query' => $tax_query, |
|
| 525 | + ); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + /** |
|
| 529 | + * Create a new entity. |
|
| 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 | + * @since 3.9.0 |
|
| 538 | + */ |
|
| 539 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 540 | + |
|
| 541 | + // Create an entity for the publisher. |
|
| 542 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 543 | + $post_id = @wp_insert_post( |
|
| 544 | + array( |
|
| 545 | + 'post_type' => self::TYPE_NAME, |
|
| 546 | + 'post_title' => $name, |
|
| 547 | + 'post_status' => $status, |
|
| 548 | + 'post_content' => '', |
|
| 549 | + ) |
|
| 550 | + ); |
|
| 551 | + |
|
| 552 | + // Return the error if any. |
|
| 553 | + if ( is_wp_error( $post_id ) ) { |
|
| 554 | + return $post_id; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + // Set the entity logo. |
|
| 558 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 559 | + set_post_thumbnail( $post_id, $logo ); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + // Set the entity type. |
|
| 563 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 564 | + |
|
| 565 | + return $post_id; |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * Get the entities related to the one with the specified id. By default only |
|
| 570 | + * published entities will be returned. |
|
| 571 | + * |
|
| 572 | + * @param int $id The post id. |
|
| 573 | + * @param string $post_status The target post status (default = publish). |
|
| 574 | + * |
|
| 575 | + * @return array An array of post ids. |
|
| 576 | + * @since 3.10.0 |
|
| 577 | + */ |
|
| 578 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 579 | + |
|
| 580 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * Get the list of entities. |
|
| 585 | + * |
|
| 586 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 587 | + * |
|
| 588 | + * @return array An array of entity posts. |
|
| 589 | + * @since 3.12.2 |
|
| 590 | + */ |
|
| 591 | + public function get( $params = array() ) { |
|
| 592 | + |
|
| 593 | + // Set the defaults. |
|
| 594 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 595 | + |
|
| 596 | + // Merge the defaults with the provided parameters. |
|
| 597 | + $args = wp_parse_args( $params, $defaults ); |
|
| 598 | + |
|
| 599 | + // Call the `get_posts` function. |
|
| 600 | + return get_posts( $args ); |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * The list of post type names which can be used for entities |
|
| 605 | + * |
|
| 606 | + * Criteria is that the post type is public. The list of valid post types |
|
| 607 | + * can be overridden with a filter. |
|
| 608 | + * |
|
| 609 | + * @return array Array containing the names of the valid post types. |
|
| 610 | + * @since 3.15.0 |
|
| 611 | + */ |
|
| 612 | + public static function valid_entity_post_types() { |
|
| 613 | + |
|
| 614 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 615 | + $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 616 | + |
|
| 617 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 618 | + } |
|
| 619 | 619 | |
| 620 | 620 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * @since 3.2.0 |
| 82 | 82 | */ |
| 83 | 83 | protected function __construct() { |
| 84 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 84 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 85 | 85 | |
| 86 | 86 | $this->entity_uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
| 87 | 87 | $this->relation_service = Wordlift_Relation_Service::get_instance(); |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | public static function get_instance() { |
| 107 | 107 | |
| 108 | - if ( ! isset( self::$instance ) ) { |
|
| 108 | + if ( ! isset(self::$instance)) { |
|
| 109 | 109 | self::$instance = new self(); |
| 110 | 110 | } |
| 111 | 111 | |
@@ -121,22 +121,22 @@ discard block |
||
| 121 | 121 | * @return bool Return true if the post is an entity otherwise false. |
| 122 | 122 | * @since 3.1.0 |
| 123 | 123 | */ |
| 124 | - public function is_entity( $post_id ) { |
|
| 124 | + public function is_entity($post_id) { |
|
| 125 | 125 | |
| 126 | 126 | // Improve performance by giving for granted that a product is an entity. |
| 127 | - if ( 'product' === get_post_type( $post_id ) ) { |
|
| 127 | + if ('product' === get_post_type($post_id)) { |
|
| 128 | 128 | return true; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 131 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 132 | 132 | |
| 133 | - if ( is_wp_error( $terms ) ) { |
|
| 134 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 133 | + if (is_wp_error($terms)) { |
|
| 134 | + $this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message()); |
|
| 135 | 135 | |
| 136 | 136 | return false; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - if ( empty( $terms ) ) { |
|
| 139 | + if (empty($terms)) { |
|
| 140 | 140 | return false; |
| 141 | 141 | } |
| 142 | 142 | |
@@ -147,8 +147,8 @@ discard block |
||
| 147 | 147 | * |
| 148 | 148 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 149 | 149 | */ |
| 150 | - foreach ( $terms as $term ) { |
|
| 151 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 150 | + foreach ($terms as $term) { |
|
| 151 | + if (1 !== preg_match('~(^|-)article$~', $term->slug)) { |
|
| 152 | 152 | return true; |
| 153 | 153 | } |
| 154 | 154 | } |
@@ -167,20 +167,20 @@ discard block |
||
| 167 | 167 | * @return string Returns a classification scope (e.g. 'what'). |
| 168 | 168 | * @since 3.5.0 |
| 169 | 169 | */ |
| 170 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 170 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 171 | 171 | |
| 172 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 172 | + if (false === $this->is_entity($post_id)) { |
|
| 173 | 173 | return $default; |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | // Retrieve the entity type |
| 177 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 178 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 177 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 178 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 179 | 179 | // Retrieve classification boxes configuration |
| 180 | 180 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
| 181 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 182 | - foreach ( $classification_boxes as $cb ) { |
|
| 183 | - if ( in_array( $entity_type, $cb['registeredTypes'], true ) ) { |
|
| 181 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 182 | + foreach ($classification_boxes as $cb) { |
|
| 183 | + if (in_array($entity_type, $cb['registeredTypes'], true)) { |
|
| 184 | 184 | return $cb['id']; |
| 185 | 185 | } |
| 186 | 186 | } |
@@ -195,13 +195,13 @@ discard block |
||
| 195 | 195 | * |
| 196 | 196 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 197 | 197 | */ |
| 198 | - public function is_used( $post_id ) { |
|
| 198 | + public function is_used($post_id) { |
|
| 199 | 199 | |
| 200 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 200 | + if (false === $this->is_entity($post_id)) { |
|
| 201 | 201 | return null; |
| 202 | 202 | } |
| 203 | 203 | // Retrieve the post |
| 204 | - $entity = get_post( $post_id ); |
|
| 204 | + $entity = get_post($post_id); |
|
| 205 | 205 | |
| 206 | 206 | global $wpdb; |
| 207 | 207 | |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | ) |
| 214 | 214 | ); |
| 215 | 215 | // If there is at least one relation instance for the current entity, then it's used |
| 216 | - if ( 0 < $relation_instances ) { |
|
| 216 | + if (0 < $relation_instances) { |
|
| 217 | 217 | return true; |
| 218 | 218 | } |
| 219 | 219 | |
@@ -222,12 +222,12 @@ discard block |
||
| 222 | 222 | $wpdb->prepare( |
| 223 | 223 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 224 | 224 | $entity->ID, |
| 225 | - wl_get_entity_uri( $entity->ID ) |
|
| 225 | + wl_get_entity_uri($entity->ID) |
|
| 226 | 226 | ) |
| 227 | 227 | ); |
| 228 | 228 | |
| 229 | 229 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 230 | - if ( 0 < $meta_instances ) { |
|
| 230 | + if (0 < $meta_instances) { |
|
| 231 | 231 | return true; |
| 232 | 232 | } |
| 233 | 233 | |
@@ -246,9 +246,9 @@ discard block |
||
| 246 | 246 | * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
| 247 | 247 | * @since 3.2.0 |
| 248 | 248 | */ |
| 249 | - public function get_entity_post_by_uri( $uri ) { |
|
| 249 | + public function get_entity_post_by_uri($uri) { |
|
| 250 | 250 | |
| 251 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 251 | + return $this->entity_uri_service->get_entity($uri); |
|
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | /** |
@@ -264,19 +264,19 @@ discard block |
||
| 264 | 264 | * @param int $post_id Post ID. |
| 265 | 265 | * @param WP_Post $post Post object. |
| 266 | 266 | */ |
| 267 | - public function save_post( $post_id, $post ) { |
|
| 267 | + public function save_post($post_id, $post) { |
|
| 268 | 268 | // Avoid doing anything if post is autosave or a revision. |
| 269 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 269 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 270 | 270 | return; |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | // We expect a numeric value here. |
| 274 | - if ( ! isset( $_REQUEST['post_ID'] ) || ! is_numeric( $_REQUEST['post_ID'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 274 | + if ( ! isset($_REQUEST['post_ID']) || ! is_numeric($_REQUEST['post_ID'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 275 | 275 | return; |
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | // Get the numeric post ID from the request. |
| 279 | - $request_post_id = intval( $_REQUEST['post_ID'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 279 | + $request_post_id = intval($_REQUEST['post_ID']); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 280 | 280 | |
| 281 | 281 | // We're setting the alternative label that have been provided via the UI |
| 282 | 282 | // (in fact we're using $_REQUEST), while save_post may be also called |
@@ -284,16 +284,16 @@ discard block |
||
| 284 | 284 | // the $post_id in the save_post call matches the post id set in the request. |
| 285 | 285 | // |
| 286 | 286 | // If this is not the current post being saved or if it's not an entity, return. |
| 287 | - if ( $request_post_id !== $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 287 | + if ($request_post_id !== $post_id || ! $this->is_entity($post_id)) { |
|
| 288 | 288 | return; |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | - if ( isset( $_REQUEST['wl_alternative_label'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 292 | - $data = filter_var_array( $_REQUEST, array( 'wl_alternative_label' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 291 | + if (isset($_REQUEST['wl_alternative_label'])) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 292 | + $data = filter_var_array($_REQUEST, array('wl_alternative_label' => array('flags' => FILTER_REQUIRE_ARRAY))); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 293 | 293 | // Get the alt labels from the request (or empty array). |
| 294 | - $alt_labels = isset( $data['wl_alternative_label'] ) ? $data['wl_alternative_label'] : array(); |
|
| 294 | + $alt_labels = isset($data['wl_alternative_label']) ? $data['wl_alternative_label'] : array(); |
|
| 295 | 295 | // This is via classic editor, so set the alternative labels. |
| 296 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 296 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | } |
@@ -306,48 +306,48 @@ discard block |
||
| 306 | 306 | * |
| 307 | 307 | * @since 3.2.0 |
| 308 | 308 | */ |
| 309 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 309 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 310 | 310 | |
| 311 | 311 | // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
| 312 | 312 | // instead of post id. |
| 313 | - if ( ! is_numeric( $post_id ) ) { |
|
| 313 | + if ( ! is_numeric($post_id)) { |
|
| 314 | 314 | return; |
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | // Force $alt_labels to be an array |
| 318 | - if ( ! is_array( $alt_labels ) ) { |
|
| 319 | - $alt_labels = array( $alt_labels ); |
|
| 318 | + if ( ! is_array($alt_labels)) { |
|
| 319 | + $alt_labels = array($alt_labels); |
|
| 320 | 320 | } |
| 321 | 321 | |
| 322 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . ' ]' ); |
|
| 322 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels).' ]'); |
|
| 323 | 323 | |
| 324 | 324 | // Delete all the existing alternate labels. |
| 325 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 325 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 326 | 326 | |
| 327 | 327 | // Save only unique synonymns. |
| 328 | - $alt_labels = array_unique( $alt_labels ); |
|
| 328 | + $alt_labels = array_unique($alt_labels); |
|
| 329 | 329 | |
| 330 | 330 | // Set the alternative labels. |
| 331 | - foreach ( $alt_labels as $alt_label ) { |
|
| 331 | + foreach ($alt_labels as $alt_label) { |
|
| 332 | 332 | |
| 333 | 333 | // Strip html code from synonym. |
| 334 | - $alt_label = wp_strip_all_tags( $alt_label ); |
|
| 334 | + $alt_label = wp_strip_all_tags($alt_label); |
|
| 335 | 335 | |
| 336 | - if ( ! empty( $alt_label ) ) { |
|
| 337 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label ); |
|
| 336 | + if ( ! empty($alt_label)) { |
|
| 337 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, (string) $alt_label); |
|
| 338 | 338 | } |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | 341 | } |
| 342 | 342 | |
| 343 | - public function append_alternative_labels( $post_id, $labels_to_append ) { |
|
| 343 | + public function append_alternative_labels($post_id, $labels_to_append) { |
|
| 344 | 344 | |
| 345 | - $merged_labels = $this->get_alternative_labels( $post_id ); |
|
| 345 | + $merged_labels = $this->get_alternative_labels($post_id); |
|
| 346 | 346 | |
| 347 | 347 | // Append new synonyms to the end. |
| 348 | - $merged_labels = array_merge( $merged_labels, $labels_to_append ); |
|
| 348 | + $merged_labels = array_merge($merged_labels, $labels_to_append); |
|
| 349 | 349 | |
| 350 | - $this->set_alternative_labels( $post_id, $merged_labels ); |
|
| 350 | + $this->set_alternative_labels($post_id, $merged_labels); |
|
| 351 | 351 | |
| 352 | 352 | } |
| 353 | 353 | |
@@ -359,9 +359,9 @@ discard block |
||
| 359 | 359 | * @return mixed An array of alternative labels. |
| 360 | 360 | * @since 3.2.0 |
| 361 | 361 | */ |
| 362 | - public function get_alternative_labels( $post_id ) { |
|
| 362 | + public function get_alternative_labels($post_id) { |
|
| 363 | 363 | |
| 364 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 364 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | /** |
@@ -373,9 +373,9 @@ discard block |
||
| 373 | 373 | * @return array An array with the entity title and labels. |
| 374 | 374 | * @since 3.12.0 |
| 375 | 375 | */ |
| 376 | - public function get_labels( $post_id, $object_type = Object_Type_Enum::POST ) { |
|
| 377 | - if ( Object_Type_Enum::POST === $object_type ) { |
|
| 378 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 376 | + public function get_labels($post_id, $object_type = Object_Type_Enum::POST) { |
|
| 377 | + if (Object_Type_Enum::POST === $object_type) { |
|
| 378 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | // Term Reference dont have synonyms yet. |
@@ -389,46 +389,46 @@ discard block |
||
| 389 | 389 | * |
| 390 | 390 | * @since 3.2.0 |
| 391 | 391 | */ |
| 392 | - public function edit_form_before_permalink( $post ) { |
|
| 392 | + public function edit_form_before_permalink($post) { |
|
| 393 | 393 | |
| 394 | 394 | // If it's not an entity, return. |
| 395 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 395 | + if ( ! $this->is_entity($post->ID)) { |
|
| 396 | 396 | return; |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | // If disabled by filter, return. |
| 400 | 400 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 401 | - if ( ! apply_filters( 'wl_feature__enable__add-synonyms', true ) ) { |
|
| 401 | + if ( ! apply_filters('wl_feature__enable__add-synonyms', true)) { |
|
| 402 | 402 | return; |
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | // Print the input template. |
| 406 | - Wordlift_UI_Service::print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 406 | + Wordlift_UI_Service::print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 407 | 407 | |
| 408 | 408 | // Print all the currently set alternative labels. |
| 409 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 409 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 410 | 410 | |
| 411 | - echo wp_kses( $this->get_alternative_label_input( $alt_label ), Wordlift_UI_Service::get_template_allowed_html() ); |
|
| 411 | + echo wp_kses($this->get_alternative_label_input($alt_label), Wordlift_UI_Service::get_template_allowed_html()); |
|
| 412 | 412 | |
| 413 | 413 | }; |
| 414 | 414 | |
| 415 | 415 | // Print the button. |
| 416 | - Wordlift_UI_Service::print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 416 | + Wordlift_UI_Service::print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 417 | 417 | |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 420 | + public function get_uri($object_id, $type = Object_Type_Enum::POST) { |
|
| 421 | 421 | $content_service = Wordpress_Content_Service::get_instance(); |
| 422 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 422 | + $entity_id = $content_service->get_entity_id(new Wordpress_Content_Id($object_id, $type)); |
|
| 423 | 423 | $dataset_uri = Wordlift_Configuration_Service::get_instance()->get_dataset_uri(); |
| 424 | 424 | |
| 425 | - if ( ! isset( $entity_id ) || |
|
| 426 | - ( ! empty( $dataset_uri ) && 0 !== strpos( $entity_id, $dataset_uri ) ) ) { |
|
| 427 | - $rel_uri = Entity_Uri_Generator::create_uri( $type, $object_id ); |
|
| 425 | + if ( ! isset($entity_id) || |
|
| 426 | + ( ! empty($dataset_uri) && 0 !== strpos($entity_id, $dataset_uri))) { |
|
| 427 | + $rel_uri = Entity_Uri_Generator::create_uri($type, $object_id); |
|
| 428 | 428 | try { |
| 429 | - $content_service->set_entity_id( new Wordpress_Content_Id( $object_id, $type ), $rel_uri ); |
|
| 430 | - $entity_id = $content_service->get_entity_id( new Wordpress_Content_Id( $object_id, $type ) ); |
|
| 431 | - } catch ( Exception $e ) { |
|
| 429 | + $content_service->set_entity_id(new Wordpress_Content_Id($object_id, $type), $rel_uri); |
|
| 430 | + $entity_id = $content_service->get_entity_id(new Wordpress_Content_Id($object_id, $type)); |
|
| 431 | + } catch (Exception $e) { |
|
| 432 | 432 | return null; |
| 433 | 433 | } |
| 434 | 434 | } |
@@ -444,9 +444,9 @@ discard block |
||
| 444 | 444 | * @return string The input HTML code. |
| 445 | 445 | * @since 3.2.0 |
| 446 | 446 | */ |
| 447 | - private function get_alternative_label_input( $value = '' ) { |
|
| 447 | + private function get_alternative_label_input($value = '') { |
|
| 448 | 448 | |
| 449 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), esc_html__( 'Delete', 'wordlift' ) ); |
|
| 449 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), esc_html__('Delete', 'wordlift')); |
|
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | /** |
@@ -459,8 +459,8 @@ discard block |
||
| 459 | 459 | global $wpdb; |
| 460 | 460 | |
| 461 | 461 | // Try to get the count from the transient. |
| 462 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 463 | - if ( false !== $count ) { |
|
| 462 | + $count = get_transient('_wl_entity_service__count'); |
|
| 463 | + if (false !== $count) { |
|
| 464 | 464 | return $count; |
| 465 | 465 | } |
| 466 | 466 | |
@@ -479,7 +479,7 @@ discard block |
||
| 479 | 479 | ); |
| 480 | 480 | |
| 481 | 481 | // Store the count in cache. |
| 482 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 482 | + set_transient('_wl_entity_service__count', $count, 900); |
|
| 483 | 483 | |
| 484 | 484 | return $count; |
| 485 | 485 | } |
@@ -493,7 +493,7 @@ discard block |
||
| 493 | 493 | * @return array The arguments for a `get_posts` call. |
| 494 | 494 | * @since 3.15.0 |
| 495 | 495 | */ |
| 496 | - public static function add_criterias( $args ) { |
|
| 496 | + public static function add_criterias($args) { |
|
| 497 | 497 | |
| 498 | 498 | // Build an optimal tax-query. |
| 499 | 499 | $tax_query = array( |
@@ -536,7 +536,7 @@ discard block |
||
| 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 | * @since 3.9.0 |
| 538 | 538 | */ |
| 539 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 539 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 540 | 540 | |
| 541 | 541 | // Create an entity for the publisher. |
| 542 | 542 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
@@ -550,17 +550,17 @@ discard block |
||
| 550 | 550 | ); |
| 551 | 551 | |
| 552 | 552 | // Return the error if any. |
| 553 | - if ( is_wp_error( $post_id ) ) { |
|
| 553 | + if (is_wp_error($post_id)) { |
|
| 554 | 554 | return $post_id; |
| 555 | 555 | } |
| 556 | 556 | |
| 557 | 557 | // Set the entity logo. |
| 558 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 559 | - set_post_thumbnail( $post_id, $logo ); |
|
| 558 | + if ($logo && is_numeric($logo)) { |
|
| 559 | + set_post_thumbnail($post_id, $logo); |
|
| 560 | 560 | } |
| 561 | 561 | |
| 562 | 562 | // Set the entity type. |
| 563 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 563 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 564 | 564 | |
| 565 | 565 | return $post_id; |
| 566 | 566 | } |
@@ -575,9 +575,9 @@ discard block |
||
| 575 | 575 | * @return array An array of post ids. |
| 576 | 576 | * @since 3.10.0 |
| 577 | 577 | */ |
| 578 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 578 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 579 | 579 | |
| 580 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 580 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 581 | 581 | } |
| 582 | 582 | |
| 583 | 583 | /** |
@@ -588,16 +588,16 @@ discard block |
||
| 588 | 588 | * @return array An array of entity posts. |
| 589 | 589 | * @since 3.12.2 |
| 590 | 590 | */ |
| 591 | - public function get( $params = array() ) { |
|
| 591 | + public function get($params = array()) { |
|
| 592 | 592 | |
| 593 | 593 | // Set the defaults. |
| 594 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 594 | + $defaults = array('post_type' => 'entity'); |
|
| 595 | 595 | |
| 596 | 596 | // Merge the defaults with the provided parameters. |
| 597 | - $args = wp_parse_args( $params, $defaults ); |
|
| 597 | + $args = wp_parse_args($params, $defaults); |
|
| 598 | 598 | |
| 599 | 599 | // Call the `get_posts` function. |
| 600 | - return get_posts( $args ); |
|
| 600 | + return get_posts($args); |
|
| 601 | 601 | } |
| 602 | 602 | |
| 603 | 603 | /** |
@@ -612,9 +612,9 @@ discard block |
||
| 612 | 612 | public static function valid_entity_post_types() { |
| 613 | 613 | |
| 614 | 614 | // Ignore builtins in the call to avoid getting attachments. |
| 615 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 615 | + $post_types = array('post', 'page', self::TYPE_NAME, 'product'); |
|
| 616 | 616 | |
| 617 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 617 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 618 | 618 | } |
| 619 | 619 | |
| 620 | 620 | } |
@@ -7,110 +7,110 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_UI_Service { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The button element HTML code. |
|
| 12 | - * |
|
| 13 | - * @since 3.2.0 |
|
| 14 | - */ |
|
| 15 | - const BUTTON_HTML = '<a id="%s" class="button wl-button">%s</a>'; |
|
| 10 | + /** |
|
| 11 | + * The button element HTML code. |
|
| 12 | + * |
|
| 13 | + * @since 3.2.0 |
|
| 14 | + */ |
|
| 15 | + const BUTTON_HTML = '<a id="%s" class="button wl-button">%s</a>'; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The template HTML code. |
|
| 19 | - * |
|
| 20 | - * @since 3.2.0 |
|
| 21 | - */ |
|
| 22 | - const TEMPLATE_HTML = '<script id="%s" type="text/template">%s</script>'; |
|
| 17 | + /** |
|
| 18 | + * The template HTML code. |
|
| 19 | + * |
|
| 20 | + * @since 3.2.0 |
|
| 21 | + */ |
|
| 22 | + const TEMPLATE_HTML = '<script id="%s" type="text/template">%s</script>'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Get the button HTML. |
|
| 26 | - * |
|
| 27 | - * @param string $element_id The button element id. |
|
| 28 | - * @param string $label The button (translated) label. |
|
| 29 | - * |
|
| 30 | - * @return string The button HTML code. |
|
| 31 | - * @since 3.2.0 |
|
| 32 | - */ |
|
| 33 | - public static function get_button_html( $element_id, $label ) { |
|
| 24 | + /** |
|
| 25 | + * Get the button HTML. |
|
| 26 | + * |
|
| 27 | + * @param string $element_id The button element id. |
|
| 28 | + * @param string $label The button (translated) label. |
|
| 29 | + * |
|
| 30 | + * @return string The button HTML code. |
|
| 31 | + * @since 3.2.0 |
|
| 32 | + */ |
|
| 33 | + public static function get_button_html( $element_id, $label ) { |
|
| 34 | 34 | |
| 35 | - return sprintf( self::BUTTON_HTML, $element_id, esc_html( $label ) ); |
|
| 36 | - } |
|
| 35 | + return sprintf( self::BUTTON_HTML, $element_id, esc_html( $label ) ); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Echo the button HTML. |
|
| 40 | - * |
|
| 41 | - * @param string $element_id The button element id. |
|
| 42 | - * @param string $label The button (translated) label. |
|
| 43 | - * |
|
| 44 | - * @since 3.2.0 |
|
| 45 | - */ |
|
| 46 | - public static function print_button( $element_id, $label ) { |
|
| 38 | + /** |
|
| 39 | + * Echo the button HTML. |
|
| 40 | + * |
|
| 41 | + * @param string $element_id The button element id. |
|
| 42 | + * @param string $label The button (translated) label. |
|
| 43 | + * |
|
| 44 | + * @since 3.2.0 |
|
| 45 | + */ |
|
| 46 | + public static function print_button( $element_id, $label ) { |
|
| 47 | 47 | |
| 48 | - echo wp_kses( |
|
| 49 | - self::get_button_html( $element_id, $label ), |
|
| 50 | - array( |
|
| 51 | - 'a' => array( |
|
| 52 | - 'id' => array(), |
|
| 53 | - 'class' => array(), |
|
| 54 | - ), |
|
| 55 | - ) |
|
| 56 | - ); |
|
| 48 | + echo wp_kses( |
|
| 49 | + self::get_button_html( $element_id, $label ), |
|
| 50 | + array( |
|
| 51 | + 'a' => array( |
|
| 52 | + 'id' => array(), |
|
| 53 | + 'class' => array(), |
|
| 54 | + ), |
|
| 55 | + ) |
|
| 56 | + ); |
|
| 57 | 57 | |
| 58 | - } |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Get the HTML code for a template tag. |
|
| 62 | - * |
|
| 63 | - * @param string $element_id The element id. |
|
| 64 | - * @param string $body The element content. |
|
| 65 | - * |
|
| 66 | - * @return string The HTML code. |
|
| 67 | - * @since 3.2.0 |
|
| 68 | - */ |
|
| 69 | - public static function get_template_html( $element_id, $body ) { |
|
| 60 | + /** |
|
| 61 | + * Get the HTML code for a template tag. |
|
| 62 | + * |
|
| 63 | + * @param string $element_id The element id. |
|
| 64 | + * @param string $body The element content. |
|
| 65 | + * |
|
| 66 | + * @return string The HTML code. |
|
| 67 | + * @since 3.2.0 |
|
| 68 | + */ |
|
| 69 | + public static function get_template_html( $element_id, $body ) { |
|
| 70 | 70 | |
| 71 | - return sprintf( self::TEMPLATE_HTML, $element_id, $body ); |
|
| 72 | - } |
|
| 71 | + return sprintf( self::TEMPLATE_HTML, $element_id, $body ); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Echo the HTML code for a template tag. |
|
| 76 | - * |
|
| 77 | - * @param string $element_id The element id. |
|
| 78 | - * @param string $body The element content. |
|
| 79 | - * |
|
| 80 | - * @since 3.2.0 |
|
| 81 | - */ |
|
| 82 | - public static function print_template( $element_id, $body ) { |
|
| 74 | + /** |
|
| 75 | + * Echo the HTML code for a template tag. |
|
| 76 | + * |
|
| 77 | + * @param string $element_id The element id. |
|
| 78 | + * @param string $body The element content. |
|
| 79 | + * |
|
| 80 | + * @since 3.2.0 |
|
| 81 | + */ |
|
| 82 | + public static function print_template( $element_id, $body ) { |
|
| 83 | 83 | |
| 84 | - echo wp_kses( |
|
| 85 | - self::get_template_html( $element_id, $body ), |
|
| 86 | - self::get_template_allowed_html() |
|
| 87 | - ); |
|
| 88 | - } |
|
| 84 | + echo wp_kses( |
|
| 85 | + self::get_template_html( $element_id, $body ), |
|
| 86 | + self::get_template_allowed_html() |
|
| 87 | + ); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * @return array[] |
|
| 92 | - */ |
|
| 93 | - public static function get_template_allowed_html() { |
|
| 94 | - return array( |
|
| 95 | - 'div' => array( 'class' => array() ), |
|
| 96 | - 'label' => array( |
|
| 97 | - 'class' => array(), |
|
| 98 | - 'id' => array(), |
|
| 99 | - 'for' => array(), |
|
| 100 | - ), |
|
| 101 | - 'input' => array( |
|
| 102 | - 'name' => array(), |
|
| 103 | - 'size' => array(), |
|
| 104 | - 'value' => array(), |
|
| 105 | - 'id' => array(), |
|
| 106 | - 'type' => array(), |
|
| 107 | - ), |
|
| 108 | - 'button' => array( 'class' => array() ), |
|
| 109 | - 'script' => array( |
|
| 110 | - 'id' => array(), |
|
| 111 | - 'type' => array(), |
|
| 112 | - ), |
|
| 113 | - ); |
|
| 114 | - } |
|
| 90 | + /** |
|
| 91 | + * @return array[] |
|
| 92 | + */ |
|
| 93 | + public static function get_template_allowed_html() { |
|
| 94 | + return array( |
|
| 95 | + 'div' => array( 'class' => array() ), |
|
| 96 | + 'label' => array( |
|
| 97 | + 'class' => array(), |
|
| 98 | + 'id' => array(), |
|
| 99 | + 'for' => array(), |
|
| 100 | + ), |
|
| 101 | + 'input' => array( |
|
| 102 | + 'name' => array(), |
|
| 103 | + 'size' => array(), |
|
| 104 | + 'value' => array(), |
|
| 105 | + 'id' => array(), |
|
| 106 | + 'type' => array(), |
|
| 107 | + ), |
|
| 108 | + 'button' => array( 'class' => array() ), |
|
| 109 | + 'script' => array( |
|
| 110 | + 'id' => array(), |
|
| 111 | + 'type' => array(), |
|
| 112 | + ), |
|
| 113 | + ); |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | 116 | } |
@@ -30,9 +30,9 @@ discard block |
||
| 30 | 30 | * @return string The button HTML code. |
| 31 | 31 | * @since 3.2.0 |
| 32 | 32 | */ |
| 33 | - public static function get_button_html( $element_id, $label ) { |
|
| 33 | + public static function get_button_html($element_id, $label) { |
|
| 34 | 34 | |
| 35 | - return sprintf( self::BUTTON_HTML, $element_id, esc_html( $label ) ); |
|
| 35 | + return sprintf(self::BUTTON_HTML, $element_id, esc_html($label)); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /** |
@@ -43,10 +43,10 @@ discard block |
||
| 43 | 43 | * |
| 44 | 44 | * @since 3.2.0 |
| 45 | 45 | */ |
| 46 | - public static function print_button( $element_id, $label ) { |
|
| 46 | + public static function print_button($element_id, $label) { |
|
| 47 | 47 | |
| 48 | 48 | echo wp_kses( |
| 49 | - self::get_button_html( $element_id, $label ), |
|
| 49 | + self::get_button_html($element_id, $label), |
|
| 50 | 50 | array( |
| 51 | 51 | 'a' => array( |
| 52 | 52 | 'id' => array(), |
@@ -66,9 +66,9 @@ discard block |
||
| 66 | 66 | * @return string The HTML code. |
| 67 | 67 | * @since 3.2.0 |
| 68 | 68 | */ |
| 69 | - public static function get_template_html( $element_id, $body ) { |
|
| 69 | + public static function get_template_html($element_id, $body) { |
|
| 70 | 70 | |
| 71 | - return sprintf( self::TEMPLATE_HTML, $element_id, $body ); |
|
| 71 | + return sprintf(self::TEMPLATE_HTML, $element_id, $body); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -79,10 +79,10 @@ discard block |
||
| 79 | 79 | * |
| 80 | 80 | * @since 3.2.0 |
| 81 | 81 | */ |
| 82 | - public static function print_template( $element_id, $body ) { |
|
| 82 | + public static function print_template($element_id, $body) { |
|
| 83 | 83 | |
| 84 | 84 | echo wp_kses( |
| 85 | - self::get_template_html( $element_id, $body ), |
|
| 85 | + self::get_template_html($element_id, $body), |
|
| 86 | 86 | self::get_template_allowed_html() |
| 87 | 87 | ); |
| 88 | 88 | } |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | public static function get_template_allowed_html() { |
| 94 | 94 | return array( |
| 95 | - 'div' => array( 'class' => array() ), |
|
| 95 | + 'div' => array('class' => array()), |
|
| 96 | 96 | 'label' => array( |
| 97 | 97 | 'class' => array(), |
| 98 | 98 | 'id' => array(), |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | 'id' => array(), |
| 106 | 106 | 'type' => array(), |
| 107 | 107 | ), |
| 108 | - 'button' => array( 'class' => array() ), |
|
| 108 | + 'button' => array('class' => array()), |
|
| 109 | 109 | 'script' => array( |
| 110 | 110 | 'id' => array(), |
| 111 | 111 | 'type' => array(), |
@@ -14,223 +14,223 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Entity_Type_Taxonomy_Service { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The WordPress taxonomy name. |
|
| 19 | - * |
|
| 20 | - * @since 1.0.0 |
|
| 21 | - */ |
|
| 22 | - const TAXONOMY_NAME = 'wl_entity_type'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Register the taxonomies. |
|
| 26 | - * |
|
| 27 | - * @since 3.23.6 we hook to `wp_get_object_terms` to ensure that a term is returned when a post is queries for the |
|
| 28 | - * `wl_entity_type` taxonomy. |
|
| 29 | - * @since 3.18.0 |
|
| 30 | - */ |
|
| 31 | - public function init() { |
|
| 32 | - |
|
| 33 | - $labels = array( |
|
| 34 | - 'name' => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ), |
|
| 35 | - 'singular_name' => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ), |
|
| 36 | - 'search_items' => __( 'Search Entity Types', 'wordlift' ), |
|
| 37 | - 'all_items' => __( 'All Entity Types', 'wordlift' ), |
|
| 38 | - 'parent_item' => __( 'Parent Entity Type', 'wordlift' ), |
|
| 39 | - 'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ), |
|
| 40 | - 'edit_item' => __( 'Edit Entity Type', 'wordlift' ), |
|
| 41 | - 'update_item' => __( 'Update Entity Type', 'wordlift' ), |
|
| 42 | - 'add_new_item' => __( 'Add New Entity Type', 'wordlift' ), |
|
| 43 | - 'new_item_name' => __( 'New Entity Type', 'wordlift' ), |
|
| 44 | - 'menu_name' => __( 'Entity Types', 'wordlift' ), |
|
| 45 | - ); |
|
| 46 | - |
|
| 47 | - // Take away GUI for taxonomy editing. |
|
| 48 | - // TODO: read capabilities when editing of the WL <-> schema.org mapping is possible. |
|
| 49 | - $capabilities = array( |
|
| 50 | - // We enable editors to change the title/description of terms: |
|
| 51 | - // |
|
| 52 | - // @see https://github.com/insideout10/wordlift-plugin/issues/398. |
|
| 53 | - 'manage_terms' => 'manage_options', |
|
| 54 | - 'edit_terms' => 'wl_entity_type_edit_term', |
|
| 55 | - 'delete_terms' => 'wl_entity_type_delete_term', |
|
| 56 | - 'assign_terms' => 'edit_posts', |
|
| 57 | - ); |
|
| 58 | - |
|
| 59 | - $args = array( |
|
| 60 | - 'labels' => $labels, |
|
| 61 | - 'capabilities' => $capabilities, |
|
| 62 | - 'hierarchical' => true, |
|
| 63 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 64 | - 'show_admin_column' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
| 65 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 66 | - 'show_in_rest' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
| 67 | - 'show_in_quick_edit' => false, |
|
| 68 | - 'publicly_queryable' => false, |
|
| 69 | - ); |
|
| 70 | - |
|
| 71 | - /* |
|
| 17 | + /** |
|
| 18 | + * The WordPress taxonomy name. |
|
| 19 | + * |
|
| 20 | + * @since 1.0.0 |
|
| 21 | + */ |
|
| 22 | + const TAXONOMY_NAME = 'wl_entity_type'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Register the taxonomies. |
|
| 26 | + * |
|
| 27 | + * @since 3.23.6 we hook to `wp_get_object_terms` to ensure that a term is returned when a post is queries for the |
|
| 28 | + * `wl_entity_type` taxonomy. |
|
| 29 | + * @since 3.18.0 |
|
| 30 | + */ |
|
| 31 | + public function init() { |
|
| 32 | + |
|
| 33 | + $labels = array( |
|
| 34 | + 'name' => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ), |
|
| 35 | + 'singular_name' => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ), |
|
| 36 | + 'search_items' => __( 'Search Entity Types', 'wordlift' ), |
|
| 37 | + 'all_items' => __( 'All Entity Types', 'wordlift' ), |
|
| 38 | + 'parent_item' => __( 'Parent Entity Type', 'wordlift' ), |
|
| 39 | + 'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ), |
|
| 40 | + 'edit_item' => __( 'Edit Entity Type', 'wordlift' ), |
|
| 41 | + 'update_item' => __( 'Update Entity Type', 'wordlift' ), |
|
| 42 | + 'add_new_item' => __( 'Add New Entity Type', 'wordlift' ), |
|
| 43 | + 'new_item_name' => __( 'New Entity Type', 'wordlift' ), |
|
| 44 | + 'menu_name' => __( 'Entity Types', 'wordlift' ), |
|
| 45 | + ); |
|
| 46 | + |
|
| 47 | + // Take away GUI for taxonomy editing. |
|
| 48 | + // TODO: read capabilities when editing of the WL <-> schema.org mapping is possible. |
|
| 49 | + $capabilities = array( |
|
| 50 | + // We enable editors to change the title/description of terms: |
|
| 51 | + // |
|
| 52 | + // @see https://github.com/insideout10/wordlift-plugin/issues/398. |
|
| 53 | + 'manage_terms' => 'manage_options', |
|
| 54 | + 'edit_terms' => 'wl_entity_type_edit_term', |
|
| 55 | + 'delete_terms' => 'wl_entity_type_delete_term', |
|
| 56 | + 'assign_terms' => 'edit_posts', |
|
| 57 | + ); |
|
| 58 | + |
|
| 59 | + $args = array( |
|
| 60 | + 'labels' => $labels, |
|
| 61 | + 'capabilities' => $capabilities, |
|
| 62 | + 'hierarchical' => true, |
|
| 63 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 64 | + 'show_admin_column' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
| 65 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 66 | + 'show_in_rest' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
| 67 | + 'show_in_quick_edit' => false, |
|
| 68 | + 'publicly_queryable' => false, |
|
| 69 | + ); |
|
| 70 | + |
|
| 71 | + /* |
|
| 72 | 72 | * If `All Entity Types` is enabled, use the new metabox. |
| 73 | 73 | * |
| 74 | 74 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 75 | 75 | * @since 3.20.0 |
| 76 | 76 | */ |
| 77 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 78 | - if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
| 79 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 80 | - $args['meta_box_cb'] = apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ) ? array( |
|
| 81 | - 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', |
|
| 82 | - 'render', |
|
| 83 | - ) : false; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - register_taxonomy( |
|
| 87 | - self::TAXONOMY_NAME, // Taxonomy name. |
|
| 88 | - Wordlift_Entity_Service::valid_entity_post_types(), // Taxonomy post types. |
|
| 89 | - $args // Taxonomy args. |
|
| 90 | - ); |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Register meta wl_entities_gutenberg for use in Gutenberg |
|
| 94 | - */ |
|
| 95 | - register_meta( |
|
| 96 | - 'post', |
|
| 97 | - 'wl_entities_gutenberg', |
|
| 98 | - array( |
|
| 99 | - 'show_in_rest' => true, |
|
| 100 | - 'single' => true, |
|
| 101 | - 'type' => 'string', |
|
| 102 | - ) |
|
| 103 | - ); |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Register meta _wl_alt_label for use in Gutenberg |
|
| 107 | - */ |
|
| 108 | - // register_meta( 'post', Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, array( |
|
| 109 | - // 'object_subtype' => '', |
|
| 110 | - // 'show_in_rest' => true, |
|
| 111 | - // 'single' => false, |
|
| 112 | - // 'type' => 'string', |
|
| 113 | - // 'auth_callback' => function () { |
|
| 114 | - // return current_user_can( 'edit_posts' ); |
|
| 115 | - // } |
|
| 116 | - // ) ); |
|
| 117 | - |
|
| 118 | - // see #1364: add custom fields support for CPTs that are valid entity post types in order to be able to |
|
| 119 | - // query for synonyms in Block Editor. |
|
| 120 | - add_filter( |
|
| 121 | - 'register_post_type_args', |
|
| 122 | - function ( $args, $post_type ) { |
|
| 123 | - if ( in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ) ) { |
|
| 124 | - if ( ! isset( $args['supports'] ) ) { |
|
| 125 | - $args['supports'] = array(); |
|
| 126 | - } |
|
| 127 | - $args['supports'][] = 'custom-fields'; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - return $args; |
|
| 131 | - }, |
|
| 132 | - 10, |
|
| 133 | - 2 |
|
| 134 | - ); |
|
| 135 | - |
|
| 136 | - // Add filter to change the metabox CSS class. |
|
| 137 | - add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
| 138 | - |
|
| 139 | - // Add a filter to preset the object term if none is set. |
|
| 140 | - // |
|
| 141 | - // DO NOT hook to `wp_get_object_terms`, because `wp_get_object_terms` returns imploded values for SQL queries. |
|
| 142 | - // |
|
| 143 | - // @see https://github.com/insideout10/wordlift-plugin/issues/995 |
|
| 144 | - // @since 3.23.6 |
|
| 145 | - add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Exclude sitemap creation for wl_entity_type taxonomy in Yoast |
|
| 149 | - * |
|
| 150 | - * @since 3.30.1 |
|
| 151 | - */ |
|
| 152 | - add_filter( 'wpseo_sitemap_exclude_taxonomy', array( $this, 'wpseo_sitemap_exclude_taxonomy' ), 10, 2 ); |
|
| 153 | - |
|
| 154 | - $this->hide_posts_count(); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - private function hide_posts_count() { |
|
| 158 | - $name = self::TAXONOMY_NAME; |
|
| 159 | - add_filter( |
|
| 160 | - "manage_edit-{$name}_columns", |
|
| 161 | - function ( $columns ) { |
|
| 162 | - unset( $columns['posts'] ); |
|
| 163 | - return $columns; |
|
| 164 | - } |
|
| 165 | - ); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Hook to the `get_object_terms` filter. |
|
| 170 | - * |
|
| 171 | - * We check if our taxonomy is requested and whether a term has been returned. If no term has been returned we |
|
| 172 | - * preset `Article` for posts/pages and 'Thing' for everything else and we query the terms again. |
|
| 173 | - * |
|
| 174 | - * @param array $terms Array of terms for the given object or objects. |
|
| 175 | - * @param int[] $object_ids Array of object IDs for which terms were retrieved. |
|
| 176 | - * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved. |
|
| 177 | - * @param array $args Array of arguments for retrieving terms for the given |
|
| 178 | - * object(s). See get_object_terms() for details. |
|
| 179 | - * |
|
| 180 | - * @return array|WP_Error |
|
| 181 | - * @since 3.23.6 |
|
| 182 | - */ |
|
| 183 | - public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) { |
|
| 184 | - // Get our entity type. |
|
| 185 | - $entity_type = self::TAXONOMY_NAME; |
|
| 186 | - |
|
| 187 | - // Check if this is a query for our entity type, that no terms have been found and that we have an article |
|
| 188 | - // term to preset in case. |
|
| 189 | - if ( ! taxonomy_exists( $entity_type ) |
|
| 190 | - || array( $entity_type ) !== (array) $taxonomies |
|
| 191 | - || ! empty( $terms ) |
|
| 192 | - || ! term_exists( 'article', $entity_type ) |
|
| 193 | - || ! term_exists( 'thing', $entity_type ) ) { |
|
| 194 | - |
|
| 195 | - // Return the input value. |
|
| 196 | - return $terms; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - // Avoid nested calls in case of issues. |
|
| 200 | - remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 ); |
|
| 201 | - |
|
| 202 | - // Set the default term for all the queried object. |
|
| 203 | - foreach ( (array) $object_ids as $object_id ) { |
|
| 204 | - $post_type = get_post_type( $object_id ); |
|
| 205 | - if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) { |
|
| 206 | - // Set the term to article for posts and pages, or to thing for everything else. |
|
| 207 | - $uris = Wordlift_Entity_Type_Adapter::get_entity_types( $post_type ); |
|
| 208 | - foreach ( $uris as $uri ) { |
|
| 209 | - // set the uri based on post type. |
|
| 210 | - if ( 'http://schema.org/Article' === $uri || 'http://schema.org/Thing' === $uri ) { |
|
| 211 | - $uri = Wordlift_Entity_Service::TYPE_NAME === $post_type ? |
|
| 212 | - 'http://schema.org/Thing' : 'http://schema.org/Article'; |
|
| 213 | - } |
|
| 214 | - Wordlift_Entity_Type_Service::get_instance()->set( $object_id, $uri ); |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - // Finally return the object terms. |
|
| 220 | - $terms = wp_get_object_terms( $object_ids, $taxonomies, $args ); |
|
| 221 | - |
|
| 222 | - // Re-enable nested calls in case of issues. |
|
| 223 | - add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
| 224 | - |
|
| 225 | - return $terms; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - public function wpseo_sitemap_exclude_taxonomy( $exclude, $tax ) { |
|
| 229 | - if ( self::TAXONOMY_NAME === $tax ) { |
|
| 230 | - return true; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - return $exclude; |
|
| 234 | - } |
|
| 77 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 78 | + if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
| 79 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 80 | + $args['meta_box_cb'] = apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ) ? array( |
|
| 81 | + 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', |
|
| 82 | + 'render', |
|
| 83 | + ) : false; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + register_taxonomy( |
|
| 87 | + self::TAXONOMY_NAME, // Taxonomy name. |
|
| 88 | + Wordlift_Entity_Service::valid_entity_post_types(), // Taxonomy post types. |
|
| 89 | + $args // Taxonomy args. |
|
| 90 | + ); |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Register meta wl_entities_gutenberg for use in Gutenberg |
|
| 94 | + */ |
|
| 95 | + register_meta( |
|
| 96 | + 'post', |
|
| 97 | + 'wl_entities_gutenberg', |
|
| 98 | + array( |
|
| 99 | + 'show_in_rest' => true, |
|
| 100 | + 'single' => true, |
|
| 101 | + 'type' => 'string', |
|
| 102 | + ) |
|
| 103 | + ); |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Register meta _wl_alt_label for use in Gutenberg |
|
| 107 | + */ |
|
| 108 | + // register_meta( 'post', Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY, array( |
|
| 109 | + // 'object_subtype' => '', |
|
| 110 | + // 'show_in_rest' => true, |
|
| 111 | + // 'single' => false, |
|
| 112 | + // 'type' => 'string', |
|
| 113 | + // 'auth_callback' => function () { |
|
| 114 | + // return current_user_can( 'edit_posts' ); |
|
| 115 | + // } |
|
| 116 | + // ) ); |
|
| 117 | + |
|
| 118 | + // see #1364: add custom fields support for CPTs that are valid entity post types in order to be able to |
|
| 119 | + // query for synonyms in Block Editor. |
|
| 120 | + add_filter( |
|
| 121 | + 'register_post_type_args', |
|
| 122 | + function ( $args, $post_type ) { |
|
| 123 | + if ( in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ) ) { |
|
| 124 | + if ( ! isset( $args['supports'] ) ) { |
|
| 125 | + $args['supports'] = array(); |
|
| 126 | + } |
|
| 127 | + $args['supports'][] = 'custom-fields'; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + return $args; |
|
| 131 | + }, |
|
| 132 | + 10, |
|
| 133 | + 2 |
|
| 134 | + ); |
|
| 135 | + |
|
| 136 | + // Add filter to change the metabox CSS class. |
|
| 137 | + add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
| 138 | + |
|
| 139 | + // Add a filter to preset the object term if none is set. |
|
| 140 | + // |
|
| 141 | + // DO NOT hook to `wp_get_object_terms`, because `wp_get_object_terms` returns imploded values for SQL queries. |
|
| 142 | + // |
|
| 143 | + // @see https://github.com/insideout10/wordlift-plugin/issues/995 |
|
| 144 | + // @since 3.23.6 |
|
| 145 | + add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Exclude sitemap creation for wl_entity_type taxonomy in Yoast |
|
| 149 | + * |
|
| 150 | + * @since 3.30.1 |
|
| 151 | + */ |
|
| 152 | + add_filter( 'wpseo_sitemap_exclude_taxonomy', array( $this, 'wpseo_sitemap_exclude_taxonomy' ), 10, 2 ); |
|
| 153 | + |
|
| 154 | + $this->hide_posts_count(); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + private function hide_posts_count() { |
|
| 158 | + $name = self::TAXONOMY_NAME; |
|
| 159 | + add_filter( |
|
| 160 | + "manage_edit-{$name}_columns", |
|
| 161 | + function ( $columns ) { |
|
| 162 | + unset( $columns['posts'] ); |
|
| 163 | + return $columns; |
|
| 164 | + } |
|
| 165 | + ); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Hook to the `get_object_terms` filter. |
|
| 170 | + * |
|
| 171 | + * We check if our taxonomy is requested and whether a term has been returned. If no term has been returned we |
|
| 172 | + * preset `Article` for posts/pages and 'Thing' for everything else and we query the terms again. |
|
| 173 | + * |
|
| 174 | + * @param array $terms Array of terms for the given object or objects. |
|
| 175 | + * @param int[] $object_ids Array of object IDs for which terms were retrieved. |
|
| 176 | + * @param string[] $taxonomies Array of taxonomy names from which terms were retrieved. |
|
| 177 | + * @param array $args Array of arguments for retrieving terms for the given |
|
| 178 | + * object(s). See get_object_terms() for details. |
|
| 179 | + * |
|
| 180 | + * @return array|WP_Error |
|
| 181 | + * @since 3.23.6 |
|
| 182 | + */ |
|
| 183 | + public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) { |
|
| 184 | + // Get our entity type. |
|
| 185 | + $entity_type = self::TAXONOMY_NAME; |
|
| 186 | + |
|
| 187 | + // Check if this is a query for our entity type, that no terms have been found and that we have an article |
|
| 188 | + // term to preset in case. |
|
| 189 | + if ( ! taxonomy_exists( $entity_type ) |
|
| 190 | + || array( $entity_type ) !== (array) $taxonomies |
|
| 191 | + || ! empty( $terms ) |
|
| 192 | + || ! term_exists( 'article', $entity_type ) |
|
| 193 | + || ! term_exists( 'thing', $entity_type ) ) { |
|
| 194 | + |
|
| 195 | + // Return the input value. |
|
| 196 | + return $terms; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + // Avoid nested calls in case of issues. |
|
| 200 | + remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 ); |
|
| 201 | + |
|
| 202 | + // Set the default term for all the queried object. |
|
| 203 | + foreach ( (array) $object_ids as $object_id ) { |
|
| 204 | + $post_type = get_post_type( $object_id ); |
|
| 205 | + if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) { |
|
| 206 | + // Set the term to article for posts and pages, or to thing for everything else. |
|
| 207 | + $uris = Wordlift_Entity_Type_Adapter::get_entity_types( $post_type ); |
|
| 208 | + foreach ( $uris as $uri ) { |
|
| 209 | + // set the uri based on post type. |
|
| 210 | + if ( 'http://schema.org/Article' === $uri || 'http://schema.org/Thing' === $uri ) { |
|
| 211 | + $uri = Wordlift_Entity_Service::TYPE_NAME === $post_type ? |
|
| 212 | + 'http://schema.org/Thing' : 'http://schema.org/Article'; |
|
| 213 | + } |
|
| 214 | + Wordlift_Entity_Type_Service::get_instance()->set( $object_id, $uri ); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + // Finally return the object terms. |
|
| 220 | + $terms = wp_get_object_terms( $object_ids, $taxonomies, $args ); |
|
| 221 | + |
|
| 222 | + // Re-enable nested calls in case of issues. |
|
| 223 | + add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
| 224 | + |
|
| 225 | + return $terms; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + public function wpseo_sitemap_exclude_taxonomy( $exclude, $tax ) { |
|
| 229 | + if ( self::TAXONOMY_NAME === $tax ) { |
|
| 230 | + return true; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + return $exclude; |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | 236 | } |
@@ -31,17 +31,17 @@ discard block |
||
| 31 | 31 | public function init() { |
| 32 | 32 | |
| 33 | 33 | $labels = array( |
| 34 | - 'name' => _x( 'Entity Types', 'taxonomy general name', 'wordlift' ), |
|
| 35 | - 'singular_name' => _x( 'Entity Type', 'taxonomy singular name', 'wordlift' ), |
|
| 36 | - 'search_items' => __( 'Search Entity Types', 'wordlift' ), |
|
| 37 | - 'all_items' => __( 'All Entity Types', 'wordlift' ), |
|
| 38 | - 'parent_item' => __( 'Parent Entity Type', 'wordlift' ), |
|
| 39 | - 'parent_item_colon' => __( 'Parent Entity Type:', 'wordlift' ), |
|
| 40 | - 'edit_item' => __( 'Edit Entity Type', 'wordlift' ), |
|
| 41 | - 'update_item' => __( 'Update Entity Type', 'wordlift' ), |
|
| 42 | - 'add_new_item' => __( 'Add New Entity Type', 'wordlift' ), |
|
| 43 | - 'new_item_name' => __( 'New Entity Type', 'wordlift' ), |
|
| 44 | - 'menu_name' => __( 'Entity Types', 'wordlift' ), |
|
| 34 | + 'name' => _x('Entity Types', 'taxonomy general name', 'wordlift'), |
|
| 35 | + 'singular_name' => _x('Entity Type', 'taxonomy singular name', 'wordlift'), |
|
| 36 | + 'search_items' => __('Search Entity Types', 'wordlift'), |
|
| 37 | + 'all_items' => __('All Entity Types', 'wordlift'), |
|
| 38 | + 'parent_item' => __('Parent Entity Type', 'wordlift'), |
|
| 39 | + 'parent_item_colon' => __('Parent Entity Type:', 'wordlift'), |
|
| 40 | + 'edit_item' => __('Edit Entity Type', 'wordlift'), |
|
| 41 | + 'update_item' => __('Update Entity Type', 'wordlift'), |
|
| 42 | + 'add_new_item' => __('Add New Entity Type', 'wordlift'), |
|
| 43 | + 'new_item_name' => __('New Entity Type', 'wordlift'), |
|
| 44 | + 'menu_name' => __('Entity Types', 'wordlift'), |
|
| 45 | 45 | ); |
| 46 | 46 | |
| 47 | 47 | // Take away GUI for taxonomy editing. |
@@ -61,9 +61,9 @@ discard block |
||
| 61 | 61 | 'capabilities' => $capabilities, |
| 62 | 62 | 'hierarchical' => true, |
| 63 | 63 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 64 | - 'show_admin_column' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
| 64 | + 'show_admin_column' => apply_filters('wl_feature__enable__entity-types-taxonomy', true), |
|
| 65 | 65 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 66 | - 'show_in_rest' => apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ), |
|
| 66 | + 'show_in_rest' => apply_filters('wl_feature__enable__entity-types-taxonomy', true), |
|
| 67 | 67 | 'show_in_quick_edit' => false, |
| 68 | 68 | 'publicly_queryable' => false, |
| 69 | 69 | ); |
@@ -75,9 +75,9 @@ discard block |
||
| 75 | 75 | * @since 3.20.0 |
| 76 | 76 | */ |
| 77 | 77 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 78 | - if ( apply_filters( 'wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES ) ) { |
|
| 78 | + if (apply_filters('wl_feature__enable__all-entity-types', WL_ALL_ENTITY_TYPES)) { |
|
| 79 | 79 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 80 | - $args['meta_box_cb'] = apply_filters( 'wl_feature__enable__entity-types-taxonomy', true ) ? array( |
|
| 80 | + $args['meta_box_cb'] = apply_filters('wl_feature__enable__entity-types-taxonomy', true) ? array( |
|
| 81 | 81 | 'Wordlift_Admin_Schemaorg_Taxonomy_Metabox', |
| 82 | 82 | 'render', |
| 83 | 83 | ) : false; |
@@ -119,9 +119,9 @@ discard block |
||
| 119 | 119 | // query for synonyms in Block Editor. |
| 120 | 120 | add_filter( |
| 121 | 121 | 'register_post_type_args', |
| 122 | - function ( $args, $post_type ) { |
|
| 123 | - if ( in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ) ) { |
|
| 124 | - if ( ! isset( $args['supports'] ) ) { |
|
| 122 | + function($args, $post_type) { |
|
| 123 | + if (in_array($post_type, Wordlift_Entity_Service::valid_entity_post_types(), true)) { |
|
| 124 | + if ( ! isset($args['supports'])) { |
|
| 125 | 125 | $args['supports'] = array(); |
| 126 | 126 | } |
| 127 | 127 | $args['supports'][] = 'custom-fields'; |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | ); |
| 135 | 135 | |
| 136 | 136 | // Add filter to change the metabox CSS class. |
| 137 | - add_filter( 'postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class' ); |
|
| 137 | + add_filter('postbox_classes_entity_wl_entity_typediv', 'wl_admin_metaboxes_add_css_class'); |
|
| 138 | 138 | |
| 139 | 139 | // Add a filter to preset the object term if none is set. |
| 140 | 140 | // |
@@ -142,14 +142,14 @@ discard block |
||
| 142 | 142 | // |
| 143 | 143 | // @see https://github.com/insideout10/wordlift-plugin/issues/995 |
| 144 | 144 | // @since 3.23.6 |
| 145 | - add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
| 145 | + add_filter('get_object_terms', array($this, 'get_object_terms'), 10, 4); |
|
| 146 | 146 | |
| 147 | 147 | /** |
| 148 | 148 | * Exclude sitemap creation for wl_entity_type taxonomy in Yoast |
| 149 | 149 | * |
| 150 | 150 | * @since 3.30.1 |
| 151 | 151 | */ |
| 152 | - add_filter( 'wpseo_sitemap_exclude_taxonomy', array( $this, 'wpseo_sitemap_exclude_taxonomy' ), 10, 2 ); |
|
| 152 | + add_filter('wpseo_sitemap_exclude_taxonomy', array($this, 'wpseo_sitemap_exclude_taxonomy'), 10, 2); |
|
| 153 | 153 | |
| 154 | 154 | $this->hide_posts_count(); |
| 155 | 155 | } |
@@ -158,8 +158,8 @@ discard block |
||
| 158 | 158 | $name = self::TAXONOMY_NAME; |
| 159 | 159 | add_filter( |
| 160 | 160 | "manage_edit-{$name}_columns", |
| 161 | - function ( $columns ) { |
|
| 162 | - unset( $columns['posts'] ); |
|
| 161 | + function($columns) { |
|
| 162 | + unset($columns['posts']); |
|
| 163 | 163 | return $columns; |
| 164 | 164 | } |
| 165 | 165 | ); |
@@ -180,53 +180,53 @@ discard block |
||
| 180 | 180 | * @return array|WP_Error |
| 181 | 181 | * @since 3.23.6 |
| 182 | 182 | */ |
| 183 | - public function get_object_terms( $terms, $object_ids, $taxonomies, $args ) { |
|
| 183 | + public function get_object_terms($terms, $object_ids, $taxonomies, $args) { |
|
| 184 | 184 | // Get our entity type. |
| 185 | 185 | $entity_type = self::TAXONOMY_NAME; |
| 186 | 186 | |
| 187 | 187 | // Check if this is a query for our entity type, that no terms have been found and that we have an article |
| 188 | 188 | // term to preset in case. |
| 189 | - if ( ! taxonomy_exists( $entity_type ) |
|
| 190 | - || array( $entity_type ) !== (array) $taxonomies |
|
| 191 | - || ! empty( $terms ) |
|
| 192 | - || ! term_exists( 'article', $entity_type ) |
|
| 193 | - || ! term_exists( 'thing', $entity_type ) ) { |
|
| 189 | + if ( ! taxonomy_exists($entity_type) |
|
| 190 | + || array($entity_type) !== (array) $taxonomies |
|
| 191 | + || ! empty($terms) |
|
| 192 | + || ! term_exists('article', $entity_type) |
|
| 193 | + || ! term_exists('thing', $entity_type)) { |
|
| 194 | 194 | |
| 195 | 195 | // Return the input value. |
| 196 | 196 | return $terms; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | // Avoid nested calls in case of issues. |
| 200 | - remove_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10 ); |
|
| 200 | + remove_filter('get_object_terms', array($this, 'get_object_terms'), 10); |
|
| 201 | 201 | |
| 202 | 202 | // Set the default term for all the queried object. |
| 203 | - foreach ( (array) $object_ids as $object_id ) { |
|
| 204 | - $post_type = get_post_type( $object_id ); |
|
| 205 | - if ( Wordlift_Entity_Type_Service::is_valid_entity_post_type( $post_type ) ) { |
|
| 203 | + foreach ((array) $object_ids as $object_id) { |
|
| 204 | + $post_type = get_post_type($object_id); |
|
| 205 | + if (Wordlift_Entity_Type_Service::is_valid_entity_post_type($post_type)) { |
|
| 206 | 206 | // Set the term to article for posts and pages, or to thing for everything else. |
| 207 | - $uris = Wordlift_Entity_Type_Adapter::get_entity_types( $post_type ); |
|
| 208 | - foreach ( $uris as $uri ) { |
|
| 207 | + $uris = Wordlift_Entity_Type_Adapter::get_entity_types($post_type); |
|
| 208 | + foreach ($uris as $uri) { |
|
| 209 | 209 | // set the uri based on post type. |
| 210 | - if ( 'http://schema.org/Article' === $uri || 'http://schema.org/Thing' === $uri ) { |
|
| 210 | + if ('http://schema.org/Article' === $uri || 'http://schema.org/Thing' === $uri) { |
|
| 211 | 211 | $uri = Wordlift_Entity_Service::TYPE_NAME === $post_type ? |
| 212 | 212 | 'http://schema.org/Thing' : 'http://schema.org/Article'; |
| 213 | 213 | } |
| 214 | - Wordlift_Entity_Type_Service::get_instance()->set( $object_id, $uri ); |
|
| 214 | + Wordlift_Entity_Type_Service::get_instance()->set($object_id, $uri); |
|
| 215 | 215 | } |
| 216 | 216 | } |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | // Finally return the object terms. |
| 220 | - $terms = wp_get_object_terms( $object_ids, $taxonomies, $args ); |
|
| 220 | + $terms = wp_get_object_terms($object_ids, $taxonomies, $args); |
|
| 221 | 221 | |
| 222 | 222 | // Re-enable nested calls in case of issues. |
| 223 | - add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 10, 4 ); |
|
| 223 | + add_filter('get_object_terms', array($this, 'get_object_terms'), 10, 4); |
|
| 224 | 224 | |
| 225 | 225 | return $terms; |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | - public function wpseo_sitemap_exclude_taxonomy( $exclude, $tax ) { |
|
| 229 | - if ( self::TAXONOMY_NAME === $tax ) { |
|
| 228 | + public function wpseo_sitemap_exclude_taxonomy($exclude, $tax) { |
|
| 229 | + if (self::TAXONOMY_NAME === $tax) { |
|
| 230 | 230 | return true; |
| 231 | 231 | } |
| 232 | 232 | |
@@ -4,35 +4,35 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Options_Cache implements Cache { |
| 6 | 6 | |
| 7 | - private $namespace; |
|
| 7 | + private $namespace; |
|
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Options_Cache constructor. |
|
| 11 | - * |
|
| 12 | - * @param $namespace |
|
| 13 | - */ |
|
| 14 | - public function __construct( $namespace ) { |
|
| 15 | - $this->namespace = $namespace; |
|
| 16 | - } |
|
| 9 | + /** |
|
| 10 | + * Options_Cache constructor. |
|
| 11 | + * |
|
| 12 | + * @param $namespace |
|
| 13 | + */ |
|
| 14 | + public function __construct( $namespace ) { |
|
| 15 | + $this->namespace = $namespace; |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - public function get( $cache_key ) { |
|
| 18 | + public function get( $cache_key ) { |
|
| 19 | 19 | |
| 20 | - return get_option( $this->namespace . '__' . $cache_key, false ); |
|
| 20 | + return get_option( $this->namespace . '__' . $cache_key, false ); |
|
| 21 | 21 | |
| 22 | - } |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public function put( $cache_key, $value ) { |
|
| 24 | + public function put( $cache_key, $value ) { |
|
| 25 | 25 | |
| 26 | - return update_option( $this->namespace . '__' . $cache_key, $value, false ); |
|
| 26 | + return update_option( $this->namespace . '__' . $cache_key, $value, false ); |
|
| 27 | 27 | |
| 28 | - } |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - public function flush_all() { |
|
| 31 | - if ( '' !== $this->namespace ) { |
|
| 32 | - global $wpdb; |
|
| 33 | - $namespace_esc = $wpdb->esc_like( $this->namespace ) . '__%'; |
|
| 34 | - $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name LIKE %s", $namespace_esc ) ); |
|
| 35 | - } |
|
| 36 | - } |
|
| 30 | + public function flush_all() { |
|
| 31 | + if ( '' !== $this->namespace ) { |
|
| 32 | + global $wpdb; |
|
| 33 | + $namespace_esc = $wpdb->esc_like( $this->namespace ) . '__%'; |
|
| 34 | + $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name LIKE %s", $namespace_esc ) ); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | 38 | } |
@@ -11,27 +11,27 @@ |
||
| 11 | 11 | * |
| 12 | 12 | * @param $namespace |
| 13 | 13 | */ |
| 14 | - public function __construct( $namespace ) { |
|
| 14 | + public function __construct($namespace) { |
|
| 15 | 15 | $this->namespace = $namespace; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | - public function get( $cache_key ) { |
|
| 18 | + public function get($cache_key) { |
|
| 19 | 19 | |
| 20 | - return get_option( $this->namespace . '__' . $cache_key, false ); |
|
| 20 | + return get_option($this->namespace.'__'.$cache_key, false); |
|
| 21 | 21 | |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - public function put( $cache_key, $value ) { |
|
| 24 | + public function put($cache_key, $value) { |
|
| 25 | 25 | |
| 26 | - return update_option( $this->namespace . '__' . $cache_key, $value, false ); |
|
| 26 | + return update_option($this->namespace.'__'.$cache_key, $value, false); |
|
| 27 | 27 | |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | public function flush_all() { |
| 31 | - if ( '' !== $this->namespace ) { |
|
| 31 | + if ('' !== $this->namespace) { |
|
| 32 | 32 | global $wpdb; |
| 33 | - $namespace_esc = $wpdb->esc_like( $this->namespace ) . '__%'; |
|
| 34 | - $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name LIKE %s", $namespace_esc ) ); |
|
| 33 | + $namespace_esc = $wpdb->esc_like($this->namespace).'__%'; |
|
| 34 | + $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->options WHERE option_name LIKE %s", $namespace_esc)); |
|
| 35 | 35 | } |
| 36 | 36 | } |
| 37 | 37 | |
@@ -8,101 +8,101 @@ |
||
| 8 | 8 | use Wordlift\Task\Task; |
| 9 | 9 | |
| 10 | 10 | class Background_Task extends Action_Scheduler_Background_Process implements Background_Route_Task { |
| 11 | - /** |
|
| 12 | - * The option prefix to store state. |
|
| 13 | - * |
|
| 14 | - * @var string $option_prefix |
|
| 15 | - */ |
|
| 16 | - private $option_prefix; |
|
| 17 | - /** |
|
| 18 | - * @var Task |
|
| 19 | - */ |
|
| 20 | - private $task; |
|
| 21 | - |
|
| 22 | - const STATE_STARTED = 'started'; |
|
| 23 | - const STATE_STOPPED = 'stopped'; |
|
| 24 | - /** |
|
| 25 | - * @var int |
|
| 26 | - */ |
|
| 27 | - private $batch_size; |
|
| 28 | - |
|
| 29 | - public function __construct( $hook, $group, $task, $option_prefix, $batch_size = 5 ) { |
|
| 30 | - parent::__construct( $hook, $group ); |
|
| 31 | - $this->task = $task; |
|
| 32 | - $this->option_prefix = $option_prefix; |
|
| 33 | - $this->batch_size = $batch_size; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 37 | - public function do_task( $args ) { |
|
| 38 | - if ( self::STATE_STOPPED === $this->get_process_state() ) { |
|
| 39 | - return State::complete(); |
|
| 40 | - } |
|
| 41 | - $context = $this->get_context(); |
|
| 42 | - $this->task->tick( null, $context->get_data() + array( 'batch_size' => $this->batch_size ) ); |
|
| 43 | - |
|
| 44 | - if ( ( $context->get_count() - $context->get_offset() ) >= 0 ) { |
|
| 45 | - $context->set_offset( $context->get_offset() + $this->batch_size )->set_updated( time() ); |
|
| 46 | - $this->set_info( $context ); |
|
| 47 | - return State::items_in_queue(); |
|
| 48 | - } else { |
|
| 49 | - $this->set_process_state( self::STATE_STOPPED ); |
|
| 50 | - return State::complete(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function start() { |
|
| 56 | - $this->delete_info(); |
|
| 57 | - $this->set_process_state( self::STATE_STARTED ); |
|
| 58 | - $this->schedule(); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - public function stop() { |
|
| 62 | - $this->set_process_state( self::STATE_STOPPED ); |
|
| 63 | - $this->unschedule(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - public function resume() { |
|
| 67 | - $this->set_process_state( self::STATE_STARTED ); |
|
| 68 | - $this->schedule(); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function get_info() { |
|
| 72 | - return $this->get_context()->get_data() + array( 'state' => $this->get_process_state() ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function get_context() { |
|
| 76 | - $data = get_option( |
|
| 77 | - "{$this->option_prefix}_state", |
|
| 78 | - null |
|
| 79 | - ); |
|
| 80 | - |
|
| 81 | - if ( null === $data ) { |
|
| 82 | - return Context::from( (int) $this->task->starting() ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - return Context::from_data( $data ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param $context Context |
|
| 90 | - * |
|
| 91 | - * @return void |
|
| 92 | - */ |
|
| 93 | - public function set_info( $context ) { |
|
| 94 | - update_option( "{$this->option_prefix}_state", $context->get_data(), false ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - private function delete_info() { |
|
| 98 | - delete_option( "{$this->option_prefix}_state" ); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - private function get_process_state() { |
|
| 102 | - return get_option( "{$this->option_prefix}_action_scheduler_state", self::STATE_STOPPED ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - private function set_process_state( $state ) { |
|
| 106 | - update_option( "{$this->option_prefix}_action_scheduler_state", $state ); |
|
| 107 | - } |
|
| 11 | + /** |
|
| 12 | + * The option prefix to store state. |
|
| 13 | + * |
|
| 14 | + * @var string $option_prefix |
|
| 15 | + */ |
|
| 16 | + private $option_prefix; |
|
| 17 | + /** |
|
| 18 | + * @var Task |
|
| 19 | + */ |
|
| 20 | + private $task; |
|
| 21 | + |
|
| 22 | + const STATE_STARTED = 'started'; |
|
| 23 | + const STATE_STOPPED = 'stopped'; |
|
| 24 | + /** |
|
| 25 | + * @var int |
|
| 26 | + */ |
|
| 27 | + private $batch_size; |
|
| 28 | + |
|
| 29 | + public function __construct( $hook, $group, $task, $option_prefix, $batch_size = 5 ) { |
|
| 30 | + parent::__construct( $hook, $group ); |
|
| 31 | + $this->task = $task; |
|
| 32 | + $this->option_prefix = $option_prefix; |
|
| 33 | + $this->batch_size = $batch_size; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 37 | + public function do_task( $args ) { |
|
| 38 | + if ( self::STATE_STOPPED === $this->get_process_state() ) { |
|
| 39 | + return State::complete(); |
|
| 40 | + } |
|
| 41 | + $context = $this->get_context(); |
|
| 42 | + $this->task->tick( null, $context->get_data() + array( 'batch_size' => $this->batch_size ) ); |
|
| 43 | + |
|
| 44 | + if ( ( $context->get_count() - $context->get_offset() ) >= 0 ) { |
|
| 45 | + $context->set_offset( $context->get_offset() + $this->batch_size )->set_updated( time() ); |
|
| 46 | + $this->set_info( $context ); |
|
| 47 | + return State::items_in_queue(); |
|
| 48 | + } else { |
|
| 49 | + $this->set_process_state( self::STATE_STOPPED ); |
|
| 50 | + return State::complete(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function start() { |
|
| 56 | + $this->delete_info(); |
|
| 57 | + $this->set_process_state( self::STATE_STARTED ); |
|
| 58 | + $this->schedule(); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + public function stop() { |
|
| 62 | + $this->set_process_state( self::STATE_STOPPED ); |
|
| 63 | + $this->unschedule(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + public function resume() { |
|
| 67 | + $this->set_process_state( self::STATE_STARTED ); |
|
| 68 | + $this->schedule(); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function get_info() { |
|
| 72 | + return $this->get_context()->get_data() + array( 'state' => $this->get_process_state() ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function get_context() { |
|
| 76 | + $data = get_option( |
|
| 77 | + "{$this->option_prefix}_state", |
|
| 78 | + null |
|
| 79 | + ); |
|
| 80 | + |
|
| 81 | + if ( null === $data ) { |
|
| 82 | + return Context::from( (int) $this->task->starting() ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + return Context::from_data( $data ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param $context Context |
|
| 90 | + * |
|
| 91 | + * @return void |
|
| 92 | + */ |
|
| 93 | + public function set_info( $context ) { |
|
| 94 | + update_option( "{$this->option_prefix}_state", $context->get_data(), false ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + private function delete_info() { |
|
| 98 | + delete_option( "{$this->option_prefix}_state" ); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + private function get_process_state() { |
|
| 102 | + return get_option( "{$this->option_prefix}_action_scheduler_state", self::STATE_STOPPED ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + private function set_process_state( $state ) { |
|
| 106 | + update_option( "{$this->option_prefix}_action_scheduler_state", $state ); |
|
| 107 | + } |
|
| 108 | 108 | } |
@@ -26,27 +26,27 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | private $batch_size; |
| 28 | 28 | |
| 29 | - public function __construct( $hook, $group, $task, $option_prefix, $batch_size = 5 ) { |
|
| 30 | - parent::__construct( $hook, $group ); |
|
| 29 | + public function __construct($hook, $group, $task, $option_prefix, $batch_size = 5) { |
|
| 30 | + parent::__construct($hook, $group); |
|
| 31 | 31 | $this->task = $task; |
| 32 | 32 | $this->option_prefix = $option_prefix; |
| 33 | 33 | $this->batch_size = $batch_size; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 37 | - public function do_task( $args ) { |
|
| 38 | - if ( self::STATE_STOPPED === $this->get_process_state() ) { |
|
| 37 | + public function do_task($args) { |
|
| 38 | + if (self::STATE_STOPPED === $this->get_process_state()) { |
|
| 39 | 39 | return State::complete(); |
| 40 | 40 | } |
| 41 | 41 | $context = $this->get_context(); |
| 42 | - $this->task->tick( null, $context->get_data() + array( 'batch_size' => $this->batch_size ) ); |
|
| 42 | + $this->task->tick(null, $context->get_data() + array('batch_size' => $this->batch_size)); |
|
| 43 | 43 | |
| 44 | - if ( ( $context->get_count() - $context->get_offset() ) >= 0 ) { |
|
| 45 | - $context->set_offset( $context->get_offset() + $this->batch_size )->set_updated( time() ); |
|
| 46 | - $this->set_info( $context ); |
|
| 44 | + if (($context->get_count() - $context->get_offset()) >= 0) { |
|
| 45 | + $context->set_offset($context->get_offset() + $this->batch_size)->set_updated(time()); |
|
| 46 | + $this->set_info($context); |
|
| 47 | 47 | return State::items_in_queue(); |
| 48 | 48 | } else { |
| 49 | - $this->set_process_state( self::STATE_STOPPED ); |
|
| 49 | + $this->set_process_state(self::STATE_STOPPED); |
|
| 50 | 50 | return State::complete(); |
| 51 | 51 | } |
| 52 | 52 | |
@@ -54,22 +54,22 @@ discard block |
||
| 54 | 54 | |
| 55 | 55 | public function start() { |
| 56 | 56 | $this->delete_info(); |
| 57 | - $this->set_process_state( self::STATE_STARTED ); |
|
| 57 | + $this->set_process_state(self::STATE_STARTED); |
|
| 58 | 58 | $this->schedule(); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | public function stop() { |
| 62 | - $this->set_process_state( self::STATE_STOPPED ); |
|
| 62 | + $this->set_process_state(self::STATE_STOPPED); |
|
| 63 | 63 | $this->unschedule(); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | public function resume() { |
| 67 | - $this->set_process_state( self::STATE_STARTED ); |
|
| 67 | + $this->set_process_state(self::STATE_STARTED); |
|
| 68 | 68 | $this->schedule(); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | public function get_info() { |
| 72 | - return $this->get_context()->get_data() + array( 'state' => $this->get_process_state() ); |
|
| 72 | + return $this->get_context()->get_data() + array('state' => $this->get_process_state()); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | public function get_context() { |
@@ -78,11 +78,11 @@ discard block |
||
| 78 | 78 | null |
| 79 | 79 | ); |
| 80 | 80 | |
| 81 | - if ( null === $data ) { |
|
| 82 | - return Context::from( (int) $this->task->starting() ); |
|
| 81 | + if (null === $data) { |
|
| 82 | + return Context::from((int) $this->task->starting()); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - return Context::from_data( $data ); |
|
| 85 | + return Context::from_data($data); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -90,19 +90,19 @@ discard block |
||
| 90 | 90 | * |
| 91 | 91 | * @return void |
| 92 | 92 | */ |
| 93 | - public function set_info( $context ) { |
|
| 94 | - update_option( "{$this->option_prefix}_state", $context->get_data(), false ); |
|
| 93 | + public function set_info($context) { |
|
| 94 | + update_option("{$this->option_prefix}_state", $context->get_data(), false); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | private function delete_info() { |
| 98 | - delete_option( "{$this->option_prefix}_state" ); |
|
| 98 | + delete_option("{$this->option_prefix}_state"); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | private function get_process_state() { |
| 102 | - return get_option( "{$this->option_prefix}_action_scheduler_state", self::STATE_STOPPED ); |
|
| 102 | + return get_option("{$this->option_prefix}_action_scheduler_state", self::STATE_STOPPED); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - private function set_process_state( $state ) { |
|
| 106 | - update_option( "{$this->option_prefix}_action_scheduler_state", $state ); |
|
| 105 | + private function set_process_state($state) { |
|
| 106 | + update_option("{$this->option_prefix}_action_scheduler_state", $state); |
|
| 107 | 107 | } |
| 108 | 108 | } |
@@ -21,186 +21,186 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Key_Validation_Service { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * A {@link Wordlift_Log_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.14.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $log; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var Ttl_Cache |
|
| 35 | - */ |
|
| 36 | - private $ttl_cache_service; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 40 | - * |
|
| 41 | - * @since 3.14.0 |
|
| 42 | - */ |
|
| 43 | - public function __construct() { |
|
| 44 | - |
|
| 45 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 46 | - |
|
| 47 | - add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 48 | - /** |
|
| 49 | - * Filter: wl_feature__enable__notices. |
|
| 50 | - * |
|
| 51 | - * @param bool whether notices need to be enabled or not. |
|
| 52 | - * |
|
| 53 | - * @return bool |
|
| 54 | - * @since 3.27.6 |
|
| 55 | - */ |
|
| 56 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 57 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
| 61 | - |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Validate the provided key. |
|
| 66 | - * |
|
| 67 | - * @param string $key WordLift's key to validate. |
|
| 68 | - * |
|
| 69 | - * @return WP_Error|array The response or WP_Error on failure. |
|
| 70 | - * @since 3.9.0 |
|
| 71 | - */ |
|
| 72 | - public function get_account_info( $key ) { |
|
| 73 | - |
|
| 74 | - $this->log->debug( 'Validating key...' ); |
|
| 75 | - |
|
| 76 | - $response = Default_Api_Service::get_instance()->get( |
|
| 77 | - '/accounts/info', |
|
| 78 | - array( |
|
| 79 | - 'Authorization' => "Key $key", |
|
| 80 | - ) |
|
| 81 | - ); |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @since 3.38.5 |
|
| 85 | - * This action is fired when the key is validated. |
|
| 86 | - * @param $response \Wordlift\Api\Response |
|
| 87 | - */ |
|
| 88 | - do_action( 'wl_key_validation_response', $response ); |
|
| 89 | - |
|
| 90 | - return $response->get_response(); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - private function key_validation_request( $key ) { |
|
| 94 | - $response = $this->get_account_info( $key ); |
|
| 95 | - |
|
| 96 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 97 | - throw new \Exception( __( 'An error occurred, please contact us at [email protected]', 'wordlift' ) ); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 101 | - |
|
| 102 | - $url = $res_body['url']; |
|
| 103 | - |
|
| 104 | - $enabled_features = array_keys( array_filter( $res_body['features'] ) ); |
|
| 105 | - $plugin_features = array( Entity_Type_Setter::STARTER_PLAN, Entity_Type_Setter::PROFESSIONAL_PLAN, Entity_Type_Setter::BUSINESS_PLAN ); |
|
| 106 | - |
|
| 107 | - if ( count( array_intersect( $enabled_features, $plugin_features ) ) === 0 ) { |
|
| 108 | - throw new \Exception( __( 'This key is not valid. Start building your Knowledge Graph by purchasing a WordLift subscription <a href=\'https://wordlift.io/pricing/\'>here</a>.', 'wordlift' ) ); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - // Considering that production URL may be filtered. |
|
| 112 | - $home_url = get_option( 'home' ); |
|
| 113 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 114 | - |
|
| 115 | - if ( $url !== $site_url ) { |
|
| 116 | - throw new \Exception( __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ) ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return true; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Check if key is valid |
|
| 124 | - * |
|
| 125 | - * @param $key string |
|
| 126 | - * |
|
| 127 | - * @return bool |
|
| 128 | - */ |
|
| 129 | - public function is_key_valid( $key ) { |
|
| 130 | - try { |
|
| 131 | - $this->key_validation_request( $key ); |
|
| 132 | - return true; |
|
| 133 | - } catch ( \Exception $e ) { |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 140 | - * |
|
| 141 | - * @since 3.9.0 |
|
| 142 | - */ |
|
| 143 | - public function validate_key() { |
|
| 144 | - |
|
| 145 | - // Ensure we don't have garbage before us. |
|
| 146 | - ob_clean(); |
|
| 147 | - |
|
| 148 | - // Check if we have a key. |
|
| 149 | - if ( ! isset( $_POST['key'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 150 | - wp_send_json_error( 'The key parameter is required.' ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
| 154 | - |
|
| 155 | - try { |
|
| 156 | - $this->key_validation_request( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 157 | - wp_send_json_success( |
|
| 158 | - array( |
|
| 159 | - 'valid' => true, |
|
| 160 | - 'message' => '', |
|
| 161 | - ) |
|
| 162 | - ); |
|
| 163 | - |
|
| 164 | - } catch ( \Exception $e ) { |
|
| 165 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 166 | - wp_send_json_success( |
|
| 167 | - array( |
|
| 168 | - 'valid' => false, |
|
| 169 | - 'message' => $e->getMessage(), |
|
| 170 | - 'api_url' => Default_Api_Service::get_instance()->get_base_url(), |
|
| 171 | - ) |
|
| 172 | - ); |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 178 | - */ |
|
| 179 | - public function wl_load_plugin() { |
|
| 180 | - |
|
| 181 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 182 | - $home_url = get_option( 'home' ); |
|
| 183 | - |
|
| 184 | - if ( ! $wl_blog_url ) { |
|
| 185 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 186 | - } elseif ( $wl_blog_url !== $home_url ) { |
|
| 187 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 188 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 189 | - set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * This function is hooked to the `admin_notices` to show admin notification. |
|
| 196 | - */ |
|
| 197 | - public function wl_key_update_notice() { |
|
| 198 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 199 | - ?> |
|
| 24 | + /** |
|
| 25 | + * A {@link Wordlift_Log_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.14.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $log; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var Ttl_Cache |
|
| 35 | + */ |
|
| 36 | + private $ttl_cache_service; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 40 | + * |
|
| 41 | + * @since 3.14.0 |
|
| 42 | + */ |
|
| 43 | + public function __construct() { |
|
| 44 | + |
|
| 45 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 46 | + |
|
| 47 | + add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 48 | + /** |
|
| 49 | + * Filter: wl_feature__enable__notices. |
|
| 50 | + * |
|
| 51 | + * @param bool whether notices need to be enabled or not. |
|
| 52 | + * |
|
| 53 | + * @return bool |
|
| 54 | + * @since 3.27.6 |
|
| 55 | + */ |
|
| 56 | + if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 57 | + add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
| 61 | + |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Validate the provided key. |
|
| 66 | + * |
|
| 67 | + * @param string $key WordLift's key to validate. |
|
| 68 | + * |
|
| 69 | + * @return WP_Error|array The response or WP_Error on failure. |
|
| 70 | + * @since 3.9.0 |
|
| 71 | + */ |
|
| 72 | + public function get_account_info( $key ) { |
|
| 73 | + |
|
| 74 | + $this->log->debug( 'Validating key...' ); |
|
| 75 | + |
|
| 76 | + $response = Default_Api_Service::get_instance()->get( |
|
| 77 | + '/accounts/info', |
|
| 78 | + array( |
|
| 79 | + 'Authorization' => "Key $key", |
|
| 80 | + ) |
|
| 81 | + ); |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @since 3.38.5 |
|
| 85 | + * This action is fired when the key is validated. |
|
| 86 | + * @param $response \Wordlift\Api\Response |
|
| 87 | + */ |
|
| 88 | + do_action( 'wl_key_validation_response', $response ); |
|
| 89 | + |
|
| 90 | + return $response->get_response(); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + private function key_validation_request( $key ) { |
|
| 94 | + $response = $this->get_account_info( $key ); |
|
| 95 | + |
|
| 96 | + if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 97 | + throw new \Exception( __( 'An error occurred, please contact us at [email protected]', 'wordlift' ) ); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 101 | + |
|
| 102 | + $url = $res_body['url']; |
|
| 103 | + |
|
| 104 | + $enabled_features = array_keys( array_filter( $res_body['features'] ) ); |
|
| 105 | + $plugin_features = array( Entity_Type_Setter::STARTER_PLAN, Entity_Type_Setter::PROFESSIONAL_PLAN, Entity_Type_Setter::BUSINESS_PLAN ); |
|
| 106 | + |
|
| 107 | + if ( count( array_intersect( $enabled_features, $plugin_features ) ) === 0 ) { |
|
| 108 | + throw new \Exception( __( 'This key is not valid. Start building your Knowledge Graph by purchasing a WordLift subscription <a href=\'https://wordlift.io/pricing/\'>here</a>.', 'wordlift' ) ); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + // Considering that production URL may be filtered. |
|
| 112 | + $home_url = get_option( 'home' ); |
|
| 113 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 114 | + |
|
| 115 | + if ( $url !== $site_url ) { |
|
| 116 | + throw new \Exception( __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ) ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return true; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Check if key is valid |
|
| 124 | + * |
|
| 125 | + * @param $key string |
|
| 126 | + * |
|
| 127 | + * @return bool |
|
| 128 | + */ |
|
| 129 | + public function is_key_valid( $key ) { |
|
| 130 | + try { |
|
| 131 | + $this->key_validation_request( $key ); |
|
| 132 | + return true; |
|
| 133 | + } catch ( \Exception $e ) { |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 140 | + * |
|
| 141 | + * @since 3.9.0 |
|
| 142 | + */ |
|
| 143 | + public function validate_key() { |
|
| 144 | + |
|
| 145 | + // Ensure we don't have garbage before us. |
|
| 146 | + ob_clean(); |
|
| 147 | + |
|
| 148 | + // Check if we have a key. |
|
| 149 | + if ( ! isset( $_POST['key'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 150 | + wp_send_json_error( 'The key parameter is required.' ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
| 154 | + |
|
| 155 | + try { |
|
| 156 | + $this->key_validation_request( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 157 | + wp_send_json_success( |
|
| 158 | + array( |
|
| 159 | + 'valid' => true, |
|
| 160 | + 'message' => '', |
|
| 161 | + ) |
|
| 162 | + ); |
|
| 163 | + |
|
| 164 | + } catch ( \Exception $e ) { |
|
| 165 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 166 | + wp_send_json_success( |
|
| 167 | + array( |
|
| 168 | + 'valid' => false, |
|
| 169 | + 'message' => $e->getMessage(), |
|
| 170 | + 'api_url' => Default_Api_Service::get_instance()->get_base_url(), |
|
| 171 | + ) |
|
| 172 | + ); |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 178 | + */ |
|
| 179 | + public function wl_load_plugin() { |
|
| 180 | + |
|
| 181 | + $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 182 | + $home_url = get_option( 'home' ); |
|
| 183 | + |
|
| 184 | + if ( ! $wl_blog_url ) { |
|
| 185 | + update_option( '_wl_blog_url', $home_url, true ); |
|
| 186 | + } elseif ( $wl_blog_url !== $home_url ) { |
|
| 187 | + update_option( '_wl_blog_url', $home_url, true ); |
|
| 188 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 189 | + set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * This function is hooked to the `admin_notices` to show admin notification. |
|
| 196 | + */ |
|
| 197 | + public function wl_key_update_notice() { |
|
| 198 | + if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 199 | + ?> |
|
| 200 | 200 | <div class="updated notice is-dismissible error"> |
| 201 | 201 | <p><?php esc_html( get_transient( 'wl-key-error-msg' ) ); ?></p> |
| 202 | 202 | </div> |
| 203 | 203 | <?php |
| 204 | - } |
|
| 205 | - } |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | 206 | } |
@@ -42,9 +42,9 @@ discard block |
||
| 42 | 42 | */ |
| 43 | 43 | public function __construct() { |
| 44 | 44 | |
| 45 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 45 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Key_Validation_Service'); |
|
| 46 | 46 | |
| 47 | - add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 47 | + add_action('admin_init', array($this, 'wl_load_plugin')); |
|
| 48 | 48 | /** |
| 49 | 49 | * Filter: wl_feature__enable__notices. |
| 50 | 50 | * |
@@ -53,11 +53,11 @@ discard block |
||
| 53 | 53 | * @return bool |
| 54 | 54 | * @since 3.27.6 |
| 55 | 55 | */ |
| 56 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 57 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 56 | + if (apply_filters('wl_feature__enable__notices', true)) { |
|
| 57 | + add_action('admin_notices', array($this, 'wl_key_update_notice')); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
| 60 | + $this->ttl_cache_service = new Ttl_Cache('key-validation-notification'); |
|
| 61 | 61 | |
| 62 | 62 | } |
| 63 | 63 | |
@@ -69,9 +69,9 @@ discard block |
||
| 69 | 69 | * @return WP_Error|array The response or WP_Error on failure. |
| 70 | 70 | * @since 3.9.0 |
| 71 | 71 | */ |
| 72 | - public function get_account_info( $key ) { |
|
| 72 | + public function get_account_info($key) { |
|
| 73 | 73 | |
| 74 | - $this->log->debug( 'Validating key...' ); |
|
| 74 | + $this->log->debug('Validating key...'); |
|
| 75 | 75 | |
| 76 | 76 | $response = Default_Api_Service::get_instance()->get( |
| 77 | 77 | '/accounts/info', |
@@ -85,35 +85,35 @@ discard block |
||
| 85 | 85 | * This action is fired when the key is validated. |
| 86 | 86 | * @param $response \Wordlift\Api\Response |
| 87 | 87 | */ |
| 88 | - do_action( 'wl_key_validation_response', $response ); |
|
| 88 | + do_action('wl_key_validation_response', $response); |
|
| 89 | 89 | |
| 90 | 90 | return $response->get_response(); |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - private function key_validation_request( $key ) { |
|
| 94 | - $response = $this->get_account_info( $key ); |
|
| 93 | + private function key_validation_request($key) { |
|
| 94 | + $response = $this->get_account_info($key); |
|
| 95 | 95 | |
| 96 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 97 | - throw new \Exception( __( 'An error occurred, please contact us at [email protected]', 'wordlift' ) ); |
|
| 96 | + if (is_wp_error($response) || 2 !== (int) $response['response']['code'] / 100) { |
|
| 97 | + throw new \Exception(__('An error occurred, please contact us at [email protected]', 'wordlift')); |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 100 | + $res_body = json_decode(wp_remote_retrieve_body($response), true); |
|
| 101 | 101 | |
| 102 | 102 | $url = $res_body['url']; |
| 103 | 103 | |
| 104 | - $enabled_features = array_keys( array_filter( $res_body['features'] ) ); |
|
| 105 | - $plugin_features = array( Entity_Type_Setter::STARTER_PLAN, Entity_Type_Setter::PROFESSIONAL_PLAN, Entity_Type_Setter::BUSINESS_PLAN ); |
|
| 104 | + $enabled_features = array_keys(array_filter($res_body['features'])); |
|
| 105 | + $plugin_features = array(Entity_Type_Setter::STARTER_PLAN, Entity_Type_Setter::PROFESSIONAL_PLAN, Entity_Type_Setter::BUSINESS_PLAN); |
|
| 106 | 106 | |
| 107 | - if ( count( array_intersect( $enabled_features, $plugin_features ) ) === 0 ) { |
|
| 108 | - throw new \Exception( __( 'This key is not valid. Start building your Knowledge Graph by purchasing a WordLift subscription <a href=\'https://wordlift.io/pricing/\'>here</a>.', 'wordlift' ) ); |
|
| 107 | + if (count(array_intersect($enabled_features, $plugin_features)) === 0) { |
|
| 108 | + throw new \Exception(__('This key is not valid. Start building your Knowledge Graph by purchasing a WordLift subscription <a href=\'https://wordlift.io/pricing/\'>here</a>.', 'wordlift')); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | // Considering that production URL may be filtered. |
| 112 | - $home_url = get_option( 'home' ); |
|
| 113 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 112 | + $home_url = get_option('home'); |
|
| 113 | + $site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url)); |
|
| 114 | 114 | |
| 115 | - if ( $url !== $site_url ) { |
|
| 116 | - throw new \Exception( __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ) ); |
|
| 115 | + if ($url !== $site_url) { |
|
| 116 | + throw new \Exception(__('The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift')); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | return true; |
@@ -126,11 +126,11 @@ discard block |
||
| 126 | 126 | * |
| 127 | 127 | * @return bool |
| 128 | 128 | */ |
| 129 | - public function is_key_valid( $key ) { |
|
| 129 | + public function is_key_valid($key) { |
|
| 130 | 130 | try { |
| 131 | - $this->key_validation_request( $key ); |
|
| 131 | + $this->key_validation_request($key); |
|
| 132 | 132 | return true; |
| 133 | - } catch ( \Exception $e ) { |
|
| 133 | + } catch (\Exception $e) { |
|
| 134 | 134 | return false; |
| 135 | 135 | } |
| 136 | 136 | } |
@@ -146,14 +146,14 @@ discard block |
||
| 146 | 146 | ob_clean(); |
| 147 | 147 | |
| 148 | 148 | // Check if we have a key. |
| 149 | - if ( ! isset( $_POST['key'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 150 | - wp_send_json_error( 'The key parameter is required.' ); |
|
| 149 | + if ( ! isset($_POST['key'])) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 150 | + wp_send_json_error('The key parameter is required.'); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
| 153 | + $this->ttl_cache_service->delete('is_key_valid'); |
|
| 154 | 154 | |
| 155 | 155 | try { |
| 156 | - $this->key_validation_request( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 156 | + $this->key_validation_request(sanitize_text_field(wp_unslash((string) $_POST['key']))); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
| 157 | 157 | wp_send_json_success( |
| 158 | 158 | array( |
| 159 | 159 | 'valid' => true, |
@@ -161,8 +161,8 @@ discard block |
||
| 161 | 161 | ) |
| 162 | 162 | ); |
| 163 | 163 | |
| 164 | - } catch ( \Exception $e ) { |
|
| 165 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 164 | + } catch (\Exception $e) { |
|
| 165 | + Wordlift_Configuration_Service::get_instance()->set_key(''); |
|
| 166 | 166 | wp_send_json_success( |
| 167 | 167 | array( |
| 168 | 168 | 'valid' => false, |
@@ -178,15 +178,15 @@ discard block |
||
| 178 | 178 | */ |
| 179 | 179 | public function wl_load_plugin() { |
| 180 | 180 | |
| 181 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 182 | - $home_url = get_option( 'home' ); |
|
| 181 | + $wl_blog_url = get_option('_wl_blog_url'); |
|
| 182 | + $home_url = get_option('home'); |
|
| 183 | 183 | |
| 184 | - if ( ! $wl_blog_url ) { |
|
| 185 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 186 | - } elseif ( $wl_blog_url !== $home_url ) { |
|
| 187 | - update_option( '_wl_blog_url', $home_url, true ); |
|
| 188 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 189 | - set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 184 | + if ( ! $wl_blog_url) { |
|
| 185 | + update_option('_wl_blog_url', $home_url, true); |
|
| 186 | + } elseif ($wl_blog_url !== $home_url) { |
|
| 187 | + update_option('_wl_blog_url', $home_url, true); |
|
| 188 | + Wordlift_Configuration_Service::get_instance()->set_key(''); |
|
| 189 | + set_transient('wl-key-error-msg', __("Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift'), 10); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | } |
@@ -195,10 +195,10 @@ discard block |
||
| 195 | 195 | * This function is hooked to the `admin_notices` to show admin notification. |
| 196 | 196 | */ |
| 197 | 197 | public function wl_key_update_notice() { |
| 198 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 198 | + if (get_transient('wl-key-error-msg')) { |
|
| 199 | 199 | ?> |
| 200 | 200 | <div class="updated notice is-dismissible error"> |
| 201 | - <p><?php esc_html( get_transient( 'wl-key-error-msg' ) ); ?></p> |
|
| 201 | + <p><?php esc_html(get_transient('wl-key-error-msg')); ?></p> |
|
| 202 | 202 | </div> |
| 203 | 203 | <?php |
| 204 | 204 | } |
@@ -4,39 +4,39 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Api_Headers_Service { |
| 6 | 6 | |
| 7 | - private static $instance = null; |
|
| 8 | - |
|
| 9 | - protected function __construct() { |
|
| 10 | - |
|
| 11 | - } |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * This function is used to append WordPress endpoint data to every request made. |
|
| 15 | - * |
|
| 16 | - * @return array |
|
| 17 | - */ |
|
| 18 | - public function get_wp_headers() { |
|
| 19 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 20 | - $is_plugin_subscription = apply_filters( 'wl_feature__enable__entity-types-professional', false ) || |
|
| 21 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 22 | - apply_filters( 'wl_feature__enable__entity-types-business', false ) || |
|
| 23 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 24 | - apply_filters( 'wl_feature__enable__entity-types-starter', false ); |
|
| 25 | - return $is_plugin_subscription ? array( |
|
| 26 | - 'X-Wordlift-Plugin-Wp-Admin' => untrailingslashit( get_admin_url() ), |
|
| 27 | - 'X-Wordlift-Plugin-Wp-Json' => untrailingslashit( get_rest_url() ), |
|
| 28 | - ) : array(); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @return Api_Headers_Service |
|
| 33 | - */ |
|
| 34 | - public static function get_instance() { |
|
| 35 | - if ( null === self::$instance ) { |
|
| 36 | - self::$instance = new Api_Headers_Service(); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - return self::$instance; |
|
| 40 | - } |
|
| 7 | + private static $instance = null; |
|
| 8 | + |
|
| 9 | + protected function __construct() { |
|
| 10 | + |
|
| 11 | + } |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * This function is used to append WordPress endpoint data to every request made. |
|
| 15 | + * |
|
| 16 | + * @return array |
|
| 17 | + */ |
|
| 18 | + public function get_wp_headers() { |
|
| 19 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 20 | + $is_plugin_subscription = apply_filters( 'wl_feature__enable__entity-types-professional', false ) || |
|
| 21 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 22 | + apply_filters( 'wl_feature__enable__entity-types-business', false ) || |
|
| 23 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
| 24 | + apply_filters( 'wl_feature__enable__entity-types-starter', false ); |
|
| 25 | + return $is_plugin_subscription ? array( |
|
| 26 | + 'X-Wordlift-Plugin-Wp-Admin' => untrailingslashit( get_admin_url() ), |
|
| 27 | + 'X-Wordlift-Plugin-Wp-Json' => untrailingslashit( get_rest_url() ), |
|
| 28 | + ) : array(); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @return Api_Headers_Service |
|
| 33 | + */ |
|
| 34 | + public static function get_instance() { |
|
| 35 | + if ( null === self::$instance ) { |
|
| 36 | + self::$instance = new Api_Headers_Service(); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + return self::$instance; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | 42 | } |
@@ -17,14 +17,14 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | public function get_wp_headers() { |
| 19 | 19 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 20 | - $is_plugin_subscription = apply_filters( 'wl_feature__enable__entity-types-professional', false ) || |
|
| 20 | + $is_plugin_subscription = apply_filters('wl_feature__enable__entity-types-professional', false) || |
|
| 21 | 21 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 22 | - apply_filters( 'wl_feature__enable__entity-types-business', false ) || |
|
| 22 | + apply_filters('wl_feature__enable__entity-types-business', false) || |
|
| 23 | 23 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 24 | - apply_filters( 'wl_feature__enable__entity-types-starter', false ); |
|
| 24 | + apply_filters('wl_feature__enable__entity-types-starter', false); |
|
| 25 | 25 | return $is_plugin_subscription ? array( |
| 26 | - 'X-Wordlift-Plugin-Wp-Admin' => untrailingslashit( get_admin_url() ), |
|
| 27 | - 'X-Wordlift-Plugin-Wp-Json' => untrailingslashit( get_rest_url() ), |
|
| 26 | + 'X-Wordlift-Plugin-Wp-Admin' => untrailingslashit(get_admin_url()), |
|
| 27 | + 'X-Wordlift-Plugin-Wp-Json' => untrailingslashit(get_rest_url()), |
|
| 28 | 28 | ) : array(); |
| 29 | 29 | } |
| 30 | 30 | |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * @return Api_Headers_Service |
| 33 | 33 | */ |
| 34 | 34 | public static function get_instance() { |
| 35 | - if ( null === self::$instance ) { |
|
| 35 | + if (null === self::$instance) { |
|
| 36 | 36 | self::$instance = new Api_Headers_Service(); |
| 37 | 37 | } |
| 38 | 38 | |
@@ -11,105 +11,105 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Recipe_Maker_Warning { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @var Recipe_Maker_Validation_Service |
|
| 16 | - */ |
|
| 17 | - private $recipe_maker_validation_service; |
|
| 18 | - |
|
| 19 | - public function __construct( $recipe_maker_validation_service ) { |
|
| 20 | - $this->recipe_maker_validation_service = $recipe_maker_validation_service; |
|
| 21 | - add_action( |
|
| 22 | - 'load-post.php', |
|
| 23 | - function () { |
|
| 24 | - add_action( 'wordlift_admin_notices', array( $this, 'display_image_size_warning' ) ); |
|
| 25 | - } |
|
| 26 | - ); |
|
| 27 | - |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Show the warning after applying the conditions. |
|
| 32 | - */ |
|
| 33 | - public function display_image_size_warning() { |
|
| 34 | - |
|
| 35 | - // Check if we are on the post. |
|
| 36 | - if ( ! get_post() instanceof \WP_Post ) { |
|
| 37 | - return false; |
|
| 38 | - } |
|
| 39 | - if ( ! $this->recipe_maker_validation_service->is_wp_recipe_maker_available() ) { |
|
| 40 | - return false; |
|
| 41 | - } |
|
| 42 | - $post_id = get_the_ID(); |
|
| 43 | - |
|
| 44 | - // Dont show notification if there is no recipes referred by the post. |
|
| 45 | - if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post( $post_id ) ) { |
|
| 46 | - return false; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $warnings = $this->get_warnings( $post_id ); |
|
| 50 | - |
|
| 51 | - if ( count( $warnings ) > 0 ) { |
|
| 52 | - // Show notification. |
|
| 53 | - ?> |
|
| 14 | + /** |
|
| 15 | + * @var Recipe_Maker_Validation_Service |
|
| 16 | + */ |
|
| 17 | + private $recipe_maker_validation_service; |
|
| 18 | + |
|
| 19 | + public function __construct( $recipe_maker_validation_service ) { |
|
| 20 | + $this->recipe_maker_validation_service = $recipe_maker_validation_service; |
|
| 21 | + add_action( |
|
| 22 | + 'load-post.php', |
|
| 23 | + function () { |
|
| 24 | + add_action( 'wordlift_admin_notices', array( $this, 'display_image_size_warning' ) ); |
|
| 25 | + } |
|
| 26 | + ); |
|
| 27 | + |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Show the warning after applying the conditions. |
|
| 32 | + */ |
|
| 33 | + public function display_image_size_warning() { |
|
| 34 | + |
|
| 35 | + // Check if we are on the post. |
|
| 36 | + if ( ! get_post() instanceof \WP_Post ) { |
|
| 37 | + return false; |
|
| 38 | + } |
|
| 39 | + if ( ! $this->recipe_maker_validation_service->is_wp_recipe_maker_available() ) { |
|
| 40 | + return false; |
|
| 41 | + } |
|
| 42 | + $post_id = get_the_ID(); |
|
| 43 | + |
|
| 44 | + // Dont show notification if there is no recipes referred by the post. |
|
| 45 | + if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post( $post_id ) ) { |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $warnings = $this->get_warnings( $post_id ); |
|
| 50 | + |
|
| 51 | + if ( count( $warnings ) > 0 ) { |
|
| 52 | + // Show notification. |
|
| 53 | + ?> |
|
| 54 | 54 | <div class="notice notice-warning is-dismissible"> |
| 55 | 55 | <h3><?php esc_html_e( 'WordLift', 'wordlift' ); ?></h3> |
| 56 | 56 | <p><?php esc_html_e( 'The following recipes don\'t have minimum image size of 1200 x 1200 px. This size is required for channels like Google Discover', 'wordlift' ); ?></p> |
| 57 | 57 | <ol> |
| 58 | 58 | <?php |
| 59 | - foreach ( $warnings as $warning ) { |
|
| 60 | - $image_link = get_edit_post_link( $warning['image_id'] ); |
|
| 61 | - ?> |
|
| 59 | + foreach ( $warnings as $warning ) { |
|
| 60 | + $image_link = get_edit_post_link( $warning['image_id'] ); |
|
| 61 | + ?> |
|
| 62 | 62 | <li><?php echo esc_html( get_the_title( $warning['recipe_id'] ) ); ?> <a href="<?php echo esc_attr( $image_link ); ?>"><?php esc_html_e( '[edit image]', 'wordlift' ); ?></a></li> |
| 63 | 63 | <?php |
| 64 | - } |
|
| 65 | - ?> |
|
| 64 | + } |
|
| 65 | + ?> |
|
| 66 | 66 | </ol> |
| 67 | 67 | </div> |
| 68 | 68 | <?php |
| 69 | - } |
|
| 70 | - |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @param $post_id |
|
| 75 | - * |
|
| 76 | - * @return array |
|
| 77 | - */ |
|
| 78 | - private function get_warnings( $post_id ) { |
|
| 79 | - |
|
| 80 | - $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
| 81 | - |
|
| 82 | - // Dont show duplicate warnings. |
|
| 83 | - $recipe_ids = array_unique( $recipe_ids ); |
|
| 84 | - |
|
| 85 | - $recipe_with_image_warnings = array(); |
|
| 86 | - |
|
| 87 | - foreach ( $recipe_ids as $recipe_id ) { |
|
| 88 | - $recipe = \WPRM_Recipe_Manager::get_recipe( $recipe_id ); |
|
| 89 | - if ( ! $recipe ) { |
|
| 90 | - continue; |
|
| 91 | - } |
|
| 92 | - $image_id = $recipe->image_id(); |
|
| 93 | - $image_data = wp_get_attachment_image_src( $image_id, array( 1200, 1200 ) ); |
|
| 94 | - if ( ! is_array( $image_data ) ) { |
|
| 95 | - continue; |
|
| 96 | - } |
|
| 97 | - $image_width = array_key_exists( 1, $image_data ) ? $image_data [1] : false; |
|
| 98 | - $image_height = array_key_exists( 2, $image_data ) ? $image_data [2] : false; |
|
| 99 | - if ( ! ( $image_height && $image_width ) ) { |
|
| 100 | - continue; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - if ( $image_height < 1200 || $image_width < 1200 ) { |
|
| 104 | - // Image size not present in 1200 * 1200, show a warning. |
|
| 105 | - $recipe_with_image_warnings[] = array( |
|
| 106 | - 'recipe_id' => $recipe_id, |
|
| 107 | - 'image_id' => $image_id, |
|
| 108 | - ); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return $recipe_with_image_warnings; |
|
| 113 | - } |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @param $post_id |
|
| 75 | + * |
|
| 76 | + * @return array |
|
| 77 | + */ |
|
| 78 | + private function get_warnings( $post_id ) { |
|
| 79 | + |
|
| 80 | + $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
| 81 | + |
|
| 82 | + // Dont show duplicate warnings. |
|
| 83 | + $recipe_ids = array_unique( $recipe_ids ); |
|
| 84 | + |
|
| 85 | + $recipe_with_image_warnings = array(); |
|
| 86 | + |
|
| 87 | + foreach ( $recipe_ids as $recipe_id ) { |
|
| 88 | + $recipe = \WPRM_Recipe_Manager::get_recipe( $recipe_id ); |
|
| 89 | + if ( ! $recipe ) { |
|
| 90 | + continue; |
|
| 91 | + } |
|
| 92 | + $image_id = $recipe->image_id(); |
|
| 93 | + $image_data = wp_get_attachment_image_src( $image_id, array( 1200, 1200 ) ); |
|
| 94 | + if ( ! is_array( $image_data ) ) { |
|
| 95 | + continue; |
|
| 96 | + } |
|
| 97 | + $image_width = array_key_exists( 1, $image_data ) ? $image_data [1] : false; |
|
| 98 | + $image_height = array_key_exists( 2, $image_data ) ? $image_data [2] : false; |
|
| 99 | + if ( ! ( $image_height && $image_width ) ) { |
|
| 100 | + continue; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + if ( $image_height < 1200 || $image_width < 1200 ) { |
|
| 104 | + // Image size not present in 1200 * 1200, show a warning. |
|
| 105 | + $recipe_with_image_warnings[] = array( |
|
| 106 | + 'recipe_id' => $recipe_id, |
|
| 107 | + 'image_id' => $image_id, |
|
| 108 | + ); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return $recipe_with_image_warnings; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | 115 | } |
@@ -16,12 +16,12 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | private $recipe_maker_validation_service; |
| 18 | 18 | |
| 19 | - public function __construct( $recipe_maker_validation_service ) { |
|
| 19 | + public function __construct($recipe_maker_validation_service) { |
|
| 20 | 20 | $this->recipe_maker_validation_service = $recipe_maker_validation_service; |
| 21 | 21 | add_action( |
| 22 | 22 | 'load-post.php', |
| 23 | - function () { |
|
| 24 | - add_action( 'wordlift_admin_notices', array( $this, 'display_image_size_warning' ) ); |
|
| 23 | + function() { |
|
| 24 | + add_action('wordlift_admin_notices', array($this, 'display_image_size_warning')); |
|
| 25 | 25 | } |
| 26 | 26 | ); |
| 27 | 27 | |
@@ -33,33 +33,33 @@ discard block |
||
| 33 | 33 | public function display_image_size_warning() { |
| 34 | 34 | |
| 35 | 35 | // Check if we are on the post. |
| 36 | - if ( ! get_post() instanceof \WP_Post ) { |
|
| 36 | + if ( ! get_post() instanceof \WP_Post) { |
|
| 37 | 37 | return false; |
| 38 | 38 | } |
| 39 | - if ( ! $this->recipe_maker_validation_service->is_wp_recipe_maker_available() ) { |
|
| 39 | + if ( ! $this->recipe_maker_validation_service->is_wp_recipe_maker_available()) { |
|
| 40 | 40 | return false; |
| 41 | 41 | } |
| 42 | 42 | $post_id = get_the_ID(); |
| 43 | 43 | |
| 44 | 44 | // Dont show notification if there is no recipes referred by the post. |
| 45 | - if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post( $post_id ) ) { |
|
| 45 | + if ( ! $this->recipe_maker_validation_service->is_atleast_once_recipe_present_in_the_post($post_id)) { |
|
| 46 | 46 | return false; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - $warnings = $this->get_warnings( $post_id ); |
|
| 49 | + $warnings = $this->get_warnings($post_id); |
|
| 50 | 50 | |
| 51 | - if ( count( $warnings ) > 0 ) { |
|
| 51 | + if (count($warnings) > 0) { |
|
| 52 | 52 | // Show notification. |
| 53 | 53 | ?> |
| 54 | 54 | <div class="notice notice-warning is-dismissible"> |
| 55 | - <h3><?php esc_html_e( 'WordLift', 'wordlift' ); ?></h3> |
|
| 56 | - <p><?php esc_html_e( 'The following recipes don\'t have minimum image size of 1200 x 1200 px. This size is required for channels like Google Discover', 'wordlift' ); ?></p> |
|
| 55 | + <h3><?php esc_html_e('WordLift', 'wordlift'); ?></h3> |
|
| 56 | + <p><?php esc_html_e('The following recipes don\'t have minimum image size of 1200 x 1200 px. This size is required for channels like Google Discover', 'wordlift'); ?></p> |
|
| 57 | 57 | <ol> |
| 58 | 58 | <?php |
| 59 | - foreach ( $warnings as $warning ) { |
|
| 60 | - $image_link = get_edit_post_link( $warning['image_id'] ); |
|
| 59 | + foreach ($warnings as $warning) { |
|
| 60 | + $image_link = get_edit_post_link($warning['image_id']); |
|
| 61 | 61 | ?> |
| 62 | - <li><?php echo esc_html( get_the_title( $warning['recipe_id'] ) ); ?> <a href="<?php echo esc_attr( $image_link ); ?>"><?php esc_html_e( '[edit image]', 'wordlift' ); ?></a></li> |
|
| 62 | + <li><?php echo esc_html(get_the_title($warning['recipe_id'])); ?> <a href="<?php echo esc_attr($image_link); ?>"><?php esc_html_e('[edit image]', 'wordlift'); ?></a></li> |
|
| 63 | 63 | <?php |
| 64 | 64 | } |
| 65 | 65 | ?> |
@@ -75,32 +75,32 @@ discard block |
||
| 75 | 75 | * |
| 76 | 76 | * @return array |
| 77 | 77 | */ |
| 78 | - private function get_warnings( $post_id ) { |
|
| 78 | + private function get_warnings($post_id) { |
|
| 79 | 79 | |
| 80 | - $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
| 80 | + $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post($post_id); |
|
| 81 | 81 | |
| 82 | 82 | // Dont show duplicate warnings. |
| 83 | - $recipe_ids = array_unique( $recipe_ids ); |
|
| 83 | + $recipe_ids = array_unique($recipe_ids); |
|
| 84 | 84 | |
| 85 | 85 | $recipe_with_image_warnings = array(); |
| 86 | 86 | |
| 87 | - foreach ( $recipe_ids as $recipe_id ) { |
|
| 88 | - $recipe = \WPRM_Recipe_Manager::get_recipe( $recipe_id ); |
|
| 89 | - if ( ! $recipe ) { |
|
| 87 | + foreach ($recipe_ids as $recipe_id) { |
|
| 88 | + $recipe = \WPRM_Recipe_Manager::get_recipe($recipe_id); |
|
| 89 | + if ( ! $recipe) { |
|
| 90 | 90 | continue; |
| 91 | 91 | } |
| 92 | 92 | $image_id = $recipe->image_id(); |
| 93 | - $image_data = wp_get_attachment_image_src( $image_id, array( 1200, 1200 ) ); |
|
| 94 | - if ( ! is_array( $image_data ) ) { |
|
| 93 | + $image_data = wp_get_attachment_image_src($image_id, array(1200, 1200)); |
|
| 94 | + if ( ! is_array($image_data)) { |
|
| 95 | 95 | continue; |
| 96 | 96 | } |
| 97 | - $image_width = array_key_exists( 1, $image_data ) ? $image_data [1] : false; |
|
| 98 | - $image_height = array_key_exists( 2, $image_data ) ? $image_data [2] : false; |
|
| 99 | - if ( ! ( $image_height && $image_width ) ) { |
|
| 97 | + $image_width = array_key_exists(1, $image_data) ? $image_data [1] : false; |
|
| 98 | + $image_height = array_key_exists(2, $image_data) ? $image_data [2] : false; |
|
| 99 | + if ( ! ($image_height && $image_width)) { |
|
| 100 | 100 | continue; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - if ( $image_height < 1200 || $image_width < 1200 ) { |
|
| 103 | + if ($image_height < 1200 || $image_width < 1200) { |
|
| 104 | 104 | // Image size not present in 1200 * 1200, show a warning. |
| 105 | 105 | $recipe_with_image_warnings[] = array( |
| 106 | 106 | 'recipe_id' => $recipe_id, |
@@ -14,60 +14,60 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Background_Task_Factory { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Create a {@link Background_Task} given the provided task and route. |
|
| 19 | - * |
|
| 20 | - * @param Task $task The target task. |
|
| 21 | - * @param string $route The route name. |
|
| 22 | - * @param string $page_id The page id. |
|
| 23 | - * @param string $page_title The page title. |
|
| 24 | - * |
|
| 25 | - * @return Background_Task |
|
| 26 | - * @throws Exception in case of invalid arguments. |
|
| 27 | - */ |
|
| 28 | - public static function create( $task, $route, $page_id, $page_title ) { |
|
| 29 | - self::assertions( $task, $route ); |
|
| 30 | - $background_task = Background_Task::create( $task ); |
|
| 31 | - $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 32 | - Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 33 | - return $background_task; |
|
| 34 | - } |
|
| 17 | + /** |
|
| 18 | + * Create a {@link Background_Task} given the provided task and route. |
|
| 19 | + * |
|
| 20 | + * @param Task $task The target task. |
|
| 21 | + * @param string $route The route name. |
|
| 22 | + * @param string $page_id The page id. |
|
| 23 | + * @param string $page_title The page title. |
|
| 24 | + * |
|
| 25 | + * @return Background_Task |
|
| 26 | + * @throws Exception in case of invalid arguments. |
|
| 27 | + */ |
|
| 28 | + public static function create( $task, $route, $page_id, $page_title ) { |
|
| 29 | + self::assertions( $task, $route ); |
|
| 30 | + $background_task = Background_Task::create( $task ); |
|
| 31 | + $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 32 | + Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 33 | + return $background_task; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Create a {@link Background_Task} given the provided task and route. |
|
| 38 | - * |
|
| 39 | - * @param Task $task The target task. |
|
| 40 | - * @param string $route The route name. |
|
| 41 | - * @param string $page_id The page id. |
|
| 42 | - * @param string $page_title The page title. |
|
| 43 | - * |
|
| 44 | - * @return \Wordlift\Task\Action_Scheduler\Background_Task |
|
| 45 | - * @throws Exception in case of invalid arguments. |
|
| 46 | - */ |
|
| 47 | - public static function create_action_scheduler_task( $hook, $group, $task, $route, $page_id, $page_title, $batch_size = 5 ) { |
|
| 48 | - self::assertions( $task, $route ); |
|
| 49 | - $background_task = new \Wordlift\Task\Action_Scheduler\Background_Task( |
|
| 50 | - $hook, |
|
| 51 | - $group, |
|
| 52 | - $task, |
|
| 53 | - "_{$hook}_", |
|
| 54 | - $batch_size |
|
| 55 | - ); |
|
| 56 | - $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 57 | - Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 58 | - return $background_task; |
|
| 59 | - } |
|
| 36 | + /** |
|
| 37 | + * Create a {@link Background_Task} given the provided task and route. |
|
| 38 | + * |
|
| 39 | + * @param Task $task The target task. |
|
| 40 | + * @param string $route The route name. |
|
| 41 | + * @param string $page_id The page id. |
|
| 42 | + * @param string $page_title The page title. |
|
| 43 | + * |
|
| 44 | + * @return \Wordlift\Task\Action_Scheduler\Background_Task |
|
| 45 | + * @throws Exception in case of invalid arguments. |
|
| 46 | + */ |
|
| 47 | + public static function create_action_scheduler_task( $hook, $group, $task, $route, $page_id, $page_title, $batch_size = 5 ) { |
|
| 48 | + self::assertions( $task, $route ); |
|
| 49 | + $background_task = new \Wordlift\Task\Action_Scheduler\Background_Task( |
|
| 50 | + $hook, |
|
| 51 | + $group, |
|
| 52 | + $task, |
|
| 53 | + "_{$hook}_", |
|
| 54 | + $batch_size |
|
| 55 | + ); |
|
| 56 | + $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 57 | + Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 58 | + return $background_task; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @param Task $task |
|
| 63 | - * @param $route |
|
| 64 | - * |
|
| 65 | - * @return void |
|
| 66 | - * @throws Exception in case of invalid arguments. |
|
| 67 | - */ |
|
| 68 | - private static function assertions( Task $task, $route ) { |
|
| 69 | - Assertions::is_a( $task, 'Wordlift\Task\Task' ); |
|
| 70 | - Assertions::starts_with( $route, '/', __( 'The route must start with a slash.', 'wordlift' ) ); |
|
| 71 | - } |
|
| 61 | + /** |
|
| 62 | + * @param Task $task |
|
| 63 | + * @param $route |
|
| 64 | + * |
|
| 65 | + * @return void |
|
| 66 | + * @throws Exception in case of invalid arguments. |
|
| 67 | + */ |
|
| 68 | + private static function assertions( Task $task, $route ) { |
|
| 69 | + Assertions::is_a( $task, 'Wordlift\Task\Task' ); |
|
| 70 | + Assertions::starts_with( $route, '/', __( 'The route must start with a slash.', 'wordlift' ) ); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | } |
@@ -25,11 +25,11 @@ discard block |
||
| 25 | 25 | * @return Background_Task |
| 26 | 26 | * @throws Exception in case of invalid arguments. |
| 27 | 27 | */ |
| 28 | - public static function create( $task, $route, $page_id, $page_title ) { |
|
| 29 | - self::assertions( $task, $route ); |
|
| 30 | - $background_task = Background_Task::create( $task ); |
|
| 31 | - $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 32 | - Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 28 | + public static function create($task, $route, $page_id, $page_title) { |
|
| 29 | + self::assertions($task, $route); |
|
| 30 | + $background_task = Background_Task::create($task); |
|
| 31 | + $background_task_route = Background_Task_Route::create($background_task, $route); |
|
| 32 | + Background_Task_Page::create($page_title, $page_id, $background_task_route); |
|
| 33 | 33 | return $background_task; |
| 34 | 34 | } |
| 35 | 35 | |
@@ -44,17 +44,17 @@ discard block |
||
| 44 | 44 | * @return \Wordlift\Task\Action_Scheduler\Background_Task |
| 45 | 45 | * @throws Exception in case of invalid arguments. |
| 46 | 46 | */ |
| 47 | - public static function create_action_scheduler_task( $hook, $group, $task, $route, $page_id, $page_title, $batch_size = 5 ) { |
|
| 48 | - self::assertions( $task, $route ); |
|
| 49 | - $background_task = new \Wordlift\Task\Action_Scheduler\Background_Task( |
|
| 47 | + public static function create_action_scheduler_task($hook, $group, $task, $route, $page_id, $page_title, $batch_size = 5) { |
|
| 48 | + self::assertions($task, $route); |
|
| 49 | + $background_task = new \Wordlift\Task\Action_Scheduler\Background_Task( |
|
| 50 | 50 | $hook, |
| 51 | 51 | $group, |
| 52 | 52 | $task, |
| 53 | 53 | "_{$hook}_", |
| 54 | 54 | $batch_size |
| 55 | 55 | ); |
| 56 | - $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 57 | - Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 56 | + $background_task_route = Background_Task_Route::create($background_task, $route); |
|
| 57 | + Background_Task_Page::create($page_title, $page_id, $background_task_route); |
|
| 58 | 58 | return $background_task; |
| 59 | 59 | } |
| 60 | 60 | |
@@ -65,9 +65,9 @@ discard block |
||
| 65 | 65 | * @return void |
| 66 | 66 | * @throws Exception in case of invalid arguments. |
| 67 | 67 | */ |
| 68 | - private static function assertions( Task $task, $route ) { |
|
| 69 | - Assertions::is_a( $task, 'Wordlift\Task\Task' ); |
|
| 70 | - Assertions::starts_with( $route, '/', __( 'The route must start with a slash.', 'wordlift' ) ); |
|
| 68 | + private static function assertions(Task $task, $route) { |
|
| 69 | + Assertions::is_a($task, 'Wordlift\Task\Task'); |
|
| 70 | + Assertions::starts_with($route, '/', __('The route must start with a slash.', 'wordlift')); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | } |