@@ -441,6 +441,9 @@ discard block |
||
| 441 | 441 | return $uri; |
| 442 | 442 | } |
| 443 | 443 | |
| 444 | + /** |
|
| 445 | + * @param integer $type |
|
| 446 | + */ |
|
| 444 | 447 | public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
| 445 | 448 | |
| 446 | 449 | if ( Object_Type_Enum::POST === $type ) { |
@@ -566,7 +569,7 @@ discard block |
||
| 566 | 569 | * @param null $logo The entity logo id (or NULL if none). |
| 567 | 570 | * @param string $status The post status, by default 'publish'. |
| 568 | 571 | * |
| 569 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 572 | + * @return integer The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 570 | 573 | * @since 3.9.0 |
| 571 | 574 | * |
| 572 | 575 | */ |
@@ -18,534 +18,534 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Entity_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The Log service. |
|
| 23 | - * |
|
| 24 | - * @since 3.2.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 27 | - */ |
|
| 28 | - private $log; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The UI service. |
|
| 32 | - * |
|
| 33 | - * @since 3.2.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 36 | - */ |
|
| 37 | - private $ui_service; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.15.0 |
|
| 43 | - * @access private |
|
| 44 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 45 | - */ |
|
| 46 | - private $relation_service; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 50 | - * |
|
| 51 | - * @since 3.16.3 |
|
| 52 | - * @access private |
|
| 53 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 54 | - */ |
|
| 55 | - private $entity_uri_service; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The entity post type name. |
|
| 59 | - * |
|
| 60 | - * @since 3.1.0 |
|
| 61 | - */ |
|
| 62 | - const TYPE_NAME = 'entity'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * The alternative label meta key. |
|
| 66 | - * |
|
| 67 | - * @since 3.2.0 |
|
| 68 | - */ |
|
| 69 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The alternative label input template. |
|
| 73 | - * |
|
| 74 | - * @since 3.2.0 |
|
| 75 | - */ |
|
| 76 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 77 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 21 | + /** |
|
| 22 | + * The Log service. |
|
| 23 | + * |
|
| 24 | + * @since 3.2.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 27 | + */ |
|
| 28 | + private $log; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The UI service. |
|
| 32 | + * |
|
| 33 | + * @since 3.2.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 36 | + */ |
|
| 37 | + private $ui_service; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.15.0 |
|
| 43 | + * @access private |
|
| 44 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 45 | + */ |
|
| 46 | + private $relation_service; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 50 | + * |
|
| 51 | + * @since 3.16.3 |
|
| 52 | + * @access private |
|
| 53 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 54 | + */ |
|
| 55 | + private $entity_uri_service; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The entity post type name. |
|
| 59 | + * |
|
| 60 | + * @since 3.1.0 |
|
| 61 | + */ |
|
| 62 | + const TYPE_NAME = 'entity'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * The alternative label meta key. |
|
| 66 | + * |
|
| 67 | + * @since 3.2.0 |
|
| 68 | + */ |
|
| 69 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The alternative label input template. |
|
| 73 | + * |
|
| 74 | + * @since 3.2.0 |
|
| 75 | + */ |
|
| 76 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 77 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 78 | 78 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 79 | 79 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 80 | 80 | <button class="button wl-delete-button">%s</button> |
| 81 | 81 | </div>'; |
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * A singleton instance of the Entity service. |
|
| 85 | - * |
|
| 86 | - * @since 3.2.0 |
|
| 87 | - * @access private |
|
| 88 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 89 | - */ |
|
| 90 | - private static $instance; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Create a Wordlift_Entity_Service instance. |
|
| 94 | - * |
|
| 95 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 96 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 97 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 98 | - * |
|
| 99 | - * @since 3.2.0 |
|
| 100 | - * |
|
| 101 | - */ |
|
| 102 | - public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 103 | - |
|
| 104 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 105 | - |
|
| 106 | - $this->ui_service = $ui_service; |
|
| 107 | - $this->relation_service = $relation_service; |
|
| 108 | - $this->entity_uri_service = $entity_uri_service; |
|
| 109 | - |
|
| 110 | - // Set the singleton instance. |
|
| 111 | - self::$instance = $this; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Get the singleton instance of the Entity service. |
|
| 116 | - * |
|
| 117 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 118 | - * @since 3.2.0 |
|
| 119 | - */ |
|
| 120 | - public static function get_instance() { |
|
| 121 | - |
|
| 122 | - return self::$instance; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 127 | - * something which is not an article. |
|
| 128 | - * |
|
| 129 | - * @param int $post_id A post id. |
|
| 130 | - * |
|
| 131 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 132 | - * @since 3.1.0 |
|
| 133 | - * |
|
| 134 | - */ |
|
| 135 | - public function is_entity( $post_id ) { |
|
| 136 | - |
|
| 137 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 138 | - |
|
| 139 | - if ( is_wp_error( $terms ) ) { |
|
| 140 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 141 | - |
|
| 142 | - return false; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - if ( empty( $terms ) ) { |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /* |
|
| 83 | + /** |
|
| 84 | + * A singleton instance of the Entity service. |
|
| 85 | + * |
|
| 86 | + * @since 3.2.0 |
|
| 87 | + * @access private |
|
| 88 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 89 | + */ |
|
| 90 | + private static $instance; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Create a Wordlift_Entity_Service instance. |
|
| 94 | + * |
|
| 95 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 96 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 97 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 98 | + * |
|
| 99 | + * @since 3.2.0 |
|
| 100 | + * |
|
| 101 | + */ |
|
| 102 | + public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 103 | + |
|
| 104 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 105 | + |
|
| 106 | + $this->ui_service = $ui_service; |
|
| 107 | + $this->relation_service = $relation_service; |
|
| 108 | + $this->entity_uri_service = $entity_uri_service; |
|
| 109 | + |
|
| 110 | + // Set the singleton instance. |
|
| 111 | + self::$instance = $this; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Get the singleton instance of the Entity service. |
|
| 116 | + * |
|
| 117 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 118 | + * @since 3.2.0 |
|
| 119 | + */ |
|
| 120 | + public static function get_instance() { |
|
| 121 | + |
|
| 122 | + return self::$instance; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 127 | + * something which is not an article. |
|
| 128 | + * |
|
| 129 | + * @param int $post_id A post id. |
|
| 130 | + * |
|
| 131 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 132 | + * @since 3.1.0 |
|
| 133 | + * |
|
| 134 | + */ |
|
| 135 | + public function is_entity( $post_id ) { |
|
| 136 | + |
|
| 137 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 138 | + |
|
| 139 | + if ( is_wp_error( $terms ) ) { |
|
| 140 | + $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 141 | + |
|
| 142 | + return false; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + if ( empty( $terms ) ) { |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /* |
|
| 150 | 150 | * We don't consider an `article` to be an entity. |
| 151 | 151 | * |
| 152 | 152 | * @since 3.20.0 At least one associated mustn't be an `article`. |
| 153 | 153 | * |
| 154 | 154 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 155 | 155 | */ |
| 156 | - foreach ( $terms as $term ) { |
|
| 157 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 158 | - return true; |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return false; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get the proper classification scope for a given entity post |
|
| 167 | - * |
|
| 168 | - * @param integer $post_id An entity post id. |
|
| 169 | - * |
|
| 170 | - * @param string $default The default classification scope, `what` if not |
|
| 171 | - * provided. |
|
| 172 | - * |
|
| 173 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 174 | - * @since 3.5.0 |
|
| 175 | - * |
|
| 176 | - */ |
|
| 177 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 178 | - |
|
| 179 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 180 | - return $default; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - // Retrieve the entity type |
|
| 184 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 185 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 186 | - // Retrieve classification boxes configuration |
|
| 187 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 188 | - foreach ( $classification_boxes as $cb ) { |
|
| 189 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 190 | - return $cb['id']; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - return $default; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Check whether a {@link WP_Post} is used. |
|
| 199 | - * |
|
| 200 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 201 | - * |
|
| 202 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 203 | - */ |
|
| 204 | - public function is_used( $post_id ) { |
|
| 205 | - |
|
| 206 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 207 | - return null; |
|
| 208 | - } |
|
| 209 | - // Retrieve the post |
|
| 210 | - $entity = get_post( $post_id ); |
|
| 211 | - |
|
| 212 | - global $wpdb; |
|
| 213 | - // Retrieve Wordlift relation instances table name |
|
| 214 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 215 | - |
|
| 216 | - // Check is it's referenced / related to another post / entity |
|
| 217 | - $stmt = $wpdb->prepare( |
|
| 218 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 219 | - $entity->ID |
|
| 220 | - ); |
|
| 221 | - |
|
| 222 | - // Perform the query |
|
| 223 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 224 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 225 | - if ( 0 < $relation_instances ) { |
|
| 226 | - return true; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - // Check if the entity uri is used as meta_value |
|
| 230 | - $stmt = $wpdb->prepare( |
|
| 231 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 232 | - $entity->ID, |
|
| 233 | - wl_get_entity_uri( $entity->ID ) |
|
| 234 | - ); |
|
| 235 | - // Perform the query |
|
| 236 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 237 | - |
|
| 238 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 239 | - if ( 0 < $meta_instances ) { |
|
| 240 | - return true; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - // If we are here, it means the current entity is not used at the moment |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 249 | - * |
|
| 250 | - * @param string $uri The entity URI. |
|
| 251 | - * |
|
| 252 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 253 | - * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 254 | - * |
|
| 255 | - * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 256 | - * @since 3.2.0 |
|
| 257 | - * |
|
| 258 | - */ |
|
| 259 | - public function get_entity_post_by_uri( $uri ) { |
|
| 260 | - |
|
| 261 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 266 | - * we check that the post we're saving is the current post. |
|
| 267 | - * |
|
| 268 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 269 | - * |
|
| 270 | - * @since 3.2.0 |
|
| 271 | - * |
|
| 272 | - * @param int $post_id Post ID. |
|
| 273 | - * @param WP_Post $post Post object. |
|
| 274 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 275 | - */ |
|
| 276 | - public function save_post( $post_id, $post, $update ) { |
|
| 277 | - |
|
| 278 | - // Avoid doing anything if post is autosave or a revision. |
|
| 279 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 280 | - return; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - // We're setting the alternative label that have been provided via the UI |
|
| 284 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 285 | - // programmatically by some other function: we need to check therefore if |
|
| 286 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 287 | - // |
|
| 288 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 289 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 290 | - return; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - // Get the alt labels from the request (or empty array). |
|
| 294 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 295 | - |
|
| 296 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 297 | - // This is via classic editor, so set the alternative labels. |
|
| 298 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Set the alternative labels. |
|
| 306 | - * |
|
| 307 | - * @param int $post_id The post id. |
|
| 308 | - * @param array $alt_labels An array of labels. |
|
| 309 | - * |
|
| 310 | - * @since 3.2.0 |
|
| 311 | - * |
|
| 312 | - */ |
|
| 313 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 314 | - |
|
| 315 | - // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 316 | - // instead of post id. |
|
| 317 | - if ( ! is_numeric( $post_id ) ) { |
|
| 318 | - return; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - // Force $alt_labels to be an array |
|
| 322 | - if ( ! is_array( $alt_labels ) ) { |
|
| 323 | - $alt_labels = array( $alt_labels ); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 327 | - |
|
| 328 | - // Delete all the existing alternate labels. |
|
| 329 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 330 | - |
|
| 331 | - // Set the alternative labels. |
|
| 332 | - foreach ( $alt_labels as $alt_label ) { |
|
| 333 | - if ( ! empty( $alt_label ) ) { |
|
| 334 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Retrieve the alternate labels. |
|
| 342 | - * |
|
| 343 | - * @param int $post_id Post id. |
|
| 344 | - * |
|
| 345 | - * @return mixed An array of alternative labels. |
|
| 346 | - * @since 3.2.0 |
|
| 347 | - * |
|
| 348 | - */ |
|
| 349 | - public function get_alternative_labels( $post_id ) { |
|
| 350 | - |
|
| 351 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 356 | - * |
|
| 357 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 358 | - * |
|
| 359 | - * @return array An array with the entity title and labels. |
|
| 360 | - * @since 3.12.0 |
|
| 361 | - * |
|
| 362 | - */ |
|
| 363 | - public function get_labels( $post_id ) { |
|
| 364 | - |
|
| 365 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 370 | - * |
|
| 371 | - * @param WP_Post $post Post object. |
|
| 372 | - * |
|
| 373 | - * @since 3.2.0 |
|
| 374 | - * |
|
| 375 | - */ |
|
| 376 | - public function edit_form_before_permalink( $post ) { |
|
| 377 | - |
|
| 378 | - // If it's not an entity, return. |
|
| 379 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 380 | - return; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - // Print the input template. |
|
| 384 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 385 | - |
|
| 386 | - // Print all the currently set alternative labels. |
|
| 387 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 388 | - |
|
| 389 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 390 | - |
|
| 391 | - }; |
|
| 392 | - |
|
| 393 | - // Print the button. |
|
| 394 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 395 | - |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Get the URI for the entity with the specified post id. |
|
| 400 | - * |
|
| 401 | - * @param int $post_id The entity post id. |
|
| 402 | - * |
|
| 403 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 404 | - * @since 3.6.0 |
|
| 405 | - * |
|
| 406 | - */ |
|
| 407 | - private function get_uri_for_post( $post_id ) { |
|
| 408 | - |
|
| 409 | - $log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 410 | - |
|
| 411 | - // If a null is given, nothing to do |
|
| 412 | - if ( is_null( $post_id ) ) { |
|
| 413 | - return null; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
| 417 | - |
|
| 418 | - if ( empty( $dataset_uri ) ) { |
|
| 419 | - // Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri() |
|
| 420 | - $log->debug( 'Continuing, dataset uri not configured...' ); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 424 | - |
|
| 425 | - /* |
|
| 156 | + foreach ( $terms as $term ) { |
|
| 157 | + if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 158 | + return true; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return false; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get the proper classification scope for a given entity post |
|
| 167 | + * |
|
| 168 | + * @param integer $post_id An entity post id. |
|
| 169 | + * |
|
| 170 | + * @param string $default The default classification scope, `what` if not |
|
| 171 | + * provided. |
|
| 172 | + * |
|
| 173 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 174 | + * @since 3.5.0 |
|
| 175 | + * |
|
| 176 | + */ |
|
| 177 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 178 | + |
|
| 179 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 180 | + return $default; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + // Retrieve the entity type |
|
| 184 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 185 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 186 | + // Retrieve classification boxes configuration |
|
| 187 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 188 | + foreach ( $classification_boxes as $cb ) { |
|
| 189 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 190 | + return $cb['id']; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + return $default; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Check whether a {@link WP_Post} is used. |
|
| 199 | + * |
|
| 200 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 201 | + * |
|
| 202 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 203 | + */ |
|
| 204 | + public function is_used( $post_id ) { |
|
| 205 | + |
|
| 206 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 207 | + return null; |
|
| 208 | + } |
|
| 209 | + // Retrieve the post |
|
| 210 | + $entity = get_post( $post_id ); |
|
| 211 | + |
|
| 212 | + global $wpdb; |
|
| 213 | + // Retrieve Wordlift relation instances table name |
|
| 214 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 215 | + |
|
| 216 | + // Check is it's referenced / related to another post / entity |
|
| 217 | + $stmt = $wpdb->prepare( |
|
| 218 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 219 | + $entity->ID |
|
| 220 | + ); |
|
| 221 | + |
|
| 222 | + // Perform the query |
|
| 223 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 224 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 225 | + if ( 0 < $relation_instances ) { |
|
| 226 | + return true; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + // Check if the entity uri is used as meta_value |
|
| 230 | + $stmt = $wpdb->prepare( |
|
| 231 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 232 | + $entity->ID, |
|
| 233 | + wl_get_entity_uri( $entity->ID ) |
|
| 234 | + ); |
|
| 235 | + // Perform the query |
|
| 236 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 237 | + |
|
| 238 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 239 | + if ( 0 < $meta_instances ) { |
|
| 240 | + return true; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + // If we are here, it means the current entity is not used at the moment |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 249 | + * |
|
| 250 | + * @param string $uri The entity URI. |
|
| 251 | + * |
|
| 252 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 253 | + * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 254 | + * |
|
| 255 | + * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 256 | + * @since 3.2.0 |
|
| 257 | + * |
|
| 258 | + */ |
|
| 259 | + public function get_entity_post_by_uri( $uri ) { |
|
| 260 | + |
|
| 261 | + return $this->entity_uri_service->get_entity( $uri ); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 266 | + * we check that the post we're saving is the current post. |
|
| 267 | + * |
|
| 268 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 269 | + * |
|
| 270 | + * @since 3.2.0 |
|
| 271 | + * |
|
| 272 | + * @param int $post_id Post ID. |
|
| 273 | + * @param WP_Post $post Post object. |
|
| 274 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 275 | + */ |
|
| 276 | + public function save_post( $post_id, $post, $update ) { |
|
| 277 | + |
|
| 278 | + // Avoid doing anything if post is autosave or a revision. |
|
| 279 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 280 | + return; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + // We're setting the alternative label that have been provided via the UI |
|
| 284 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 285 | + // programmatically by some other function: we need to check therefore if |
|
| 286 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 287 | + // |
|
| 288 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 289 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 290 | + return; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + // Get the alt labels from the request (or empty array). |
|
| 294 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 295 | + |
|
| 296 | + if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 297 | + // This is via classic editor, so set the alternative labels. |
|
| 298 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Set the alternative labels. |
|
| 306 | + * |
|
| 307 | + * @param int $post_id The post id. |
|
| 308 | + * @param array $alt_labels An array of labels. |
|
| 309 | + * |
|
| 310 | + * @since 3.2.0 |
|
| 311 | + * |
|
| 312 | + */ |
|
| 313 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 314 | + |
|
| 315 | + // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 316 | + // instead of post id. |
|
| 317 | + if ( ! is_numeric( $post_id ) ) { |
|
| 318 | + return; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + // Force $alt_labels to be an array |
|
| 322 | + if ( ! is_array( $alt_labels ) ) { |
|
| 323 | + $alt_labels = array( $alt_labels ); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 327 | + |
|
| 328 | + // Delete all the existing alternate labels. |
|
| 329 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 330 | + |
|
| 331 | + // Set the alternative labels. |
|
| 332 | + foreach ( $alt_labels as $alt_label ) { |
|
| 333 | + if ( ! empty( $alt_label ) ) { |
|
| 334 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Retrieve the alternate labels. |
|
| 342 | + * |
|
| 343 | + * @param int $post_id Post id. |
|
| 344 | + * |
|
| 345 | + * @return mixed An array of alternative labels. |
|
| 346 | + * @since 3.2.0 |
|
| 347 | + * |
|
| 348 | + */ |
|
| 349 | + public function get_alternative_labels( $post_id ) { |
|
| 350 | + |
|
| 351 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 356 | + * |
|
| 357 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 358 | + * |
|
| 359 | + * @return array An array with the entity title and labels. |
|
| 360 | + * @since 3.12.0 |
|
| 361 | + * |
|
| 362 | + */ |
|
| 363 | + public function get_labels( $post_id ) { |
|
| 364 | + |
|
| 365 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 370 | + * |
|
| 371 | + * @param WP_Post $post Post object. |
|
| 372 | + * |
|
| 373 | + * @since 3.2.0 |
|
| 374 | + * |
|
| 375 | + */ |
|
| 376 | + public function edit_form_before_permalink( $post ) { |
|
| 377 | + |
|
| 378 | + // If it's not an entity, return. |
|
| 379 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 380 | + return; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + // Print the input template. |
|
| 384 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 385 | + |
|
| 386 | + // Print all the currently set alternative labels. |
|
| 387 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 388 | + |
|
| 389 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 390 | + |
|
| 391 | + }; |
|
| 392 | + |
|
| 393 | + // Print the button. |
|
| 394 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 395 | + |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Get the URI for the entity with the specified post id. |
|
| 400 | + * |
|
| 401 | + * @param int $post_id The entity post id. |
|
| 402 | + * |
|
| 403 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 404 | + * @since 3.6.0 |
|
| 405 | + * |
|
| 406 | + */ |
|
| 407 | + private function get_uri_for_post( $post_id ) { |
|
| 408 | + |
|
| 409 | + $log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 410 | + |
|
| 411 | + // If a null is given, nothing to do |
|
| 412 | + if ( is_null( $post_id ) ) { |
|
| 413 | + return null; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
| 417 | + |
|
| 418 | + if ( empty( $dataset_uri ) ) { |
|
| 419 | + // Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri() |
|
| 420 | + $log->debug( 'Continuing, dataset uri not configured...' ); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 424 | + |
|
| 425 | + /* |
|
| 426 | 426 | * Consider the URI invalid if it doesn't start with the dataset URI. |
| 427 | 427 | * |
| 428 | 428 | * @see https://github.com/insideout10/wordlift-plugin/issues/996 |
| 429 | 429 | */ |
| 430 | - if ( null === $dataset_uri || 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 431 | - $uri = null; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - // Set the URI if it isn't set yet. |
|
| 435 | - $post_status = get_post_status( $post_id ); |
|
| 436 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 437 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 438 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - return $uri; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 445 | - |
|
| 446 | - if ( Object_Type_Enum::POST === $type ) { |
|
| 447 | - return $this->get_uri_for_post( $object_id ); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - if ( Object_Type_Enum::USER === $type ) { |
|
| 451 | - $uri = get_user_meta( $object_id, 'entity_url', true ); |
|
| 452 | - if ( ! empty( $uri ) ) { |
|
| 453 | - return $uri; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
| 457 | - if ( empty( $dataset_uri ) ) { |
|
| 458 | - return get_author_posts_url( $object_id ) . '#author'; |
|
| 459 | - } else { |
|
| 460 | - $user = get_userdata( $object_id ); |
|
| 461 | - if ( empty( $user->user_nicename ) ) { |
|
| 462 | - return null; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - return sprintf( '%s/author/%s', $dataset_uri, $user->user_nicename ); |
|
| 466 | - } |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - /** |
|
| 472 | - * Get the alternative label input HTML code. |
|
| 473 | - * |
|
| 474 | - * @param string $value The input value. |
|
| 475 | - * |
|
| 476 | - * @return string The input HTML code. |
|
| 477 | - * @since 3.2.0 |
|
| 478 | - * |
|
| 479 | - */ |
|
| 480 | - private function get_alternative_label_input( $value = '' ) { |
|
| 481 | - |
|
| 482 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * Get the number of entity posts published in this blog. |
|
| 487 | - * |
|
| 488 | - * @return int The number of published entity posts. |
|
| 489 | - * @since 3.6.0 |
|
| 490 | - * |
|
| 491 | - */ |
|
| 492 | - public function count() { |
|
| 493 | - global $wpdb; |
|
| 494 | - |
|
| 495 | - // Try to get the count from the transient. |
|
| 496 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 497 | - if ( false !== $count ) { |
|
| 498 | - return $count; |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - // Query the count. |
|
| 502 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 503 | - "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 504 | - . " FROM {$wpdb->term_relationships} tr" |
|
| 505 | - . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 506 | - . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 507 | - . " INNER JOIN {$wpdb->terms} t" |
|
| 508 | - . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 509 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 510 | - 'article' |
|
| 511 | - ) ); |
|
| 512 | - |
|
| 513 | - // Store the count in cache. |
|
| 514 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 515 | - |
|
| 516 | - return $count; |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 521 | - * call. |
|
| 522 | - * |
|
| 523 | - * @param array $args The arguments for a `get_posts` call. |
|
| 524 | - * |
|
| 525 | - * @return array The arguments for a `get_posts` call. |
|
| 526 | - * @since 3.15.0 |
|
| 527 | - * |
|
| 528 | - */ |
|
| 529 | - public static function add_criterias( $args ) { |
|
| 530 | - |
|
| 531 | - // Build an optimal tax-query. |
|
| 532 | - $tax_query = array( |
|
| 533 | - 'relation' => 'AND', |
|
| 534 | - array( |
|
| 535 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 536 | - 'operator' => 'EXISTS', |
|
| 537 | - ), |
|
| 538 | - array( |
|
| 539 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 540 | - 'field' => 'slug', |
|
| 541 | - 'terms' => 'article', |
|
| 542 | - 'operator' => 'NOT IN', |
|
| 543 | - ), |
|
| 544 | - ); |
|
| 545 | - |
|
| 546 | - return $args + array( |
|
| 547 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 548 | - /* |
|
| 430 | + if ( null === $dataset_uri || 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 431 | + $uri = null; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + // Set the URI if it isn't set yet. |
|
| 435 | + $post_status = get_post_status( $post_id ); |
|
| 436 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 437 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 438 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + return $uri; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 445 | + |
|
| 446 | + if ( Object_Type_Enum::POST === $type ) { |
|
| 447 | + return $this->get_uri_for_post( $object_id ); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + if ( Object_Type_Enum::USER === $type ) { |
|
| 451 | + $uri = get_user_meta( $object_id, 'entity_url', true ); |
|
| 452 | + if ( ! empty( $uri ) ) { |
|
| 453 | + return $uri; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
| 457 | + if ( empty( $dataset_uri ) ) { |
|
| 458 | + return get_author_posts_url( $object_id ) . '#author'; |
|
| 459 | + } else { |
|
| 460 | + $user = get_userdata( $object_id ); |
|
| 461 | + if ( empty( $user->user_nicename ) ) { |
|
| 462 | + return null; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + return sprintf( '%s/author/%s', $dataset_uri, $user->user_nicename ); |
|
| 466 | + } |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + /** |
|
| 472 | + * Get the alternative label input HTML code. |
|
| 473 | + * |
|
| 474 | + * @param string $value The input value. |
|
| 475 | + * |
|
| 476 | + * @return string The input HTML code. |
|
| 477 | + * @since 3.2.0 |
|
| 478 | + * |
|
| 479 | + */ |
|
| 480 | + private function get_alternative_label_input( $value = '' ) { |
|
| 481 | + |
|
| 482 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * Get the number of entity posts published in this blog. |
|
| 487 | + * |
|
| 488 | + * @return int The number of published entity posts. |
|
| 489 | + * @since 3.6.0 |
|
| 490 | + * |
|
| 491 | + */ |
|
| 492 | + public function count() { |
|
| 493 | + global $wpdb; |
|
| 494 | + |
|
| 495 | + // Try to get the count from the transient. |
|
| 496 | + $count = get_transient( '_wl_entity_service__count' ); |
|
| 497 | + if ( false !== $count ) { |
|
| 498 | + return $count; |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + // Query the count. |
|
| 502 | + $count = $wpdb->get_var( $wpdb->prepare( |
|
| 503 | + "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 504 | + . " FROM {$wpdb->term_relationships} tr" |
|
| 505 | + . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 506 | + . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 507 | + . " INNER JOIN {$wpdb->terms} t" |
|
| 508 | + . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 509 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 510 | + 'article' |
|
| 511 | + ) ); |
|
| 512 | + |
|
| 513 | + // Store the count in cache. |
|
| 514 | + set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 515 | + |
|
| 516 | + return $count; |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 521 | + * call. |
|
| 522 | + * |
|
| 523 | + * @param array $args The arguments for a `get_posts` call. |
|
| 524 | + * |
|
| 525 | + * @return array The arguments for a `get_posts` call. |
|
| 526 | + * @since 3.15.0 |
|
| 527 | + * |
|
| 528 | + */ |
|
| 529 | + public static function add_criterias( $args ) { |
|
| 530 | + |
|
| 531 | + // Build an optimal tax-query. |
|
| 532 | + $tax_query = array( |
|
| 533 | + 'relation' => 'AND', |
|
| 534 | + array( |
|
| 535 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 536 | + 'operator' => 'EXISTS', |
|
| 537 | + ), |
|
| 538 | + array( |
|
| 539 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 540 | + 'field' => 'slug', |
|
| 541 | + 'terms' => 'article', |
|
| 542 | + 'operator' => 'NOT IN', |
|
| 543 | + ), |
|
| 544 | + ); |
|
| 545 | + |
|
| 546 | + return $args + array( |
|
| 547 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 548 | + /* |
|
| 549 | 549 | * Ensure compatibility with Polylang. |
| 550 | 550 | * |
| 551 | 551 | * @see https://github.com/insideout10/wordlift-plugin/issues/855. |
@@ -553,102 +553,102 @@ discard block |
||
| 553 | 553 | * |
| 554 | 554 | * @since 3.19.5 |
| 555 | 555 | */ |
| 556 | - 'lang' => '', |
|
| 557 | - 'tax_query' => $tax_query, |
|
| 558 | - ); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Create a new entity. |
|
| 563 | - * |
|
| 564 | - * @param string $name The entity name. |
|
| 565 | - * @param string $type_uri The entity's type URI. |
|
| 566 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 567 | - * @param string $status The post status, by default 'publish'. |
|
| 568 | - * |
|
| 569 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 570 | - * @since 3.9.0 |
|
| 571 | - * |
|
| 572 | - */ |
|
| 573 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 574 | - |
|
| 575 | - // Create an entity for the publisher. |
|
| 576 | - $post_id = @wp_insert_post( array( |
|
| 577 | - 'post_type' => self::TYPE_NAME, |
|
| 578 | - 'post_title' => $name, |
|
| 579 | - 'post_status' => $status, |
|
| 580 | - 'post_content' => '', |
|
| 581 | - ) ); |
|
| 582 | - |
|
| 583 | - // Return the error if any. |
|
| 584 | - if ( is_wp_error( $post_id ) ) { |
|
| 585 | - return $post_id; |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - // Set the entity logo. |
|
| 589 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 590 | - set_post_thumbnail( $post_id, $logo ); |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - // Set the entity type. |
|
| 594 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 595 | - |
|
| 596 | - return $post_id; |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - /** |
|
| 600 | - * Get the entities related to the one with the specified id. By default only |
|
| 601 | - * published entities will be returned. |
|
| 602 | - * |
|
| 603 | - * @param int $id The post id. |
|
| 604 | - * @param string $post_status The target post status (default = publish). |
|
| 605 | - * |
|
| 606 | - * @return array An array of post ids. |
|
| 607 | - * @since 3.10.0 |
|
| 608 | - * |
|
| 609 | - */ |
|
| 610 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 611 | - |
|
| 612 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Get the list of entities. |
|
| 617 | - * |
|
| 618 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 619 | - * |
|
| 620 | - * @return array An array of entity posts. |
|
| 621 | - * @since 3.12.2 |
|
| 622 | - * |
|
| 623 | - */ |
|
| 624 | - public function get( $params = array() ) { |
|
| 625 | - |
|
| 626 | - // Set the defaults. |
|
| 627 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 628 | - |
|
| 629 | - // Merge the defaults with the provided parameters. |
|
| 630 | - $args = wp_parse_args( $params, $defaults ); |
|
| 631 | - |
|
| 632 | - // Call the `get_posts` function. |
|
| 633 | - return get_posts( $args ); |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /** |
|
| 637 | - * The list of post type names which can be used for entities |
|
| 638 | - * |
|
| 639 | - * Criteria is that the post type is public. The list of valid post types |
|
| 640 | - * can be overridden with a filter. |
|
| 641 | - * |
|
| 642 | - * @return array Array containing the names of the valid post types. |
|
| 643 | - * @since 3.15.0 |
|
| 644 | - * |
|
| 645 | - */ |
|
| 646 | - static function valid_entity_post_types() { |
|
| 647 | - |
|
| 648 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 649 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 650 | - |
|
| 651 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 652 | - } |
|
| 556 | + 'lang' => '', |
|
| 557 | + 'tax_query' => $tax_query, |
|
| 558 | + ); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Create a new entity. |
|
| 563 | + * |
|
| 564 | + * @param string $name The entity name. |
|
| 565 | + * @param string $type_uri The entity's type URI. |
|
| 566 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 567 | + * @param string $status The post status, by default 'publish'. |
|
| 568 | + * |
|
| 569 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 570 | + * @since 3.9.0 |
|
| 571 | + * |
|
| 572 | + */ |
|
| 573 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 574 | + |
|
| 575 | + // Create an entity for the publisher. |
|
| 576 | + $post_id = @wp_insert_post( array( |
|
| 577 | + 'post_type' => self::TYPE_NAME, |
|
| 578 | + 'post_title' => $name, |
|
| 579 | + 'post_status' => $status, |
|
| 580 | + 'post_content' => '', |
|
| 581 | + ) ); |
|
| 582 | + |
|
| 583 | + // Return the error if any. |
|
| 584 | + if ( is_wp_error( $post_id ) ) { |
|
| 585 | + return $post_id; |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + // Set the entity logo. |
|
| 589 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 590 | + set_post_thumbnail( $post_id, $logo ); |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + // Set the entity type. |
|
| 594 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 595 | + |
|
| 596 | + return $post_id; |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * Get the entities related to the one with the specified id. By default only |
|
| 601 | + * published entities will be returned. |
|
| 602 | + * |
|
| 603 | + * @param int $id The post id. |
|
| 604 | + * @param string $post_status The target post status (default = publish). |
|
| 605 | + * |
|
| 606 | + * @return array An array of post ids. |
|
| 607 | + * @since 3.10.0 |
|
| 608 | + * |
|
| 609 | + */ |
|
| 610 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 611 | + |
|
| 612 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Get the list of entities. |
|
| 617 | + * |
|
| 618 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 619 | + * |
|
| 620 | + * @return array An array of entity posts. |
|
| 621 | + * @since 3.12.2 |
|
| 622 | + * |
|
| 623 | + */ |
|
| 624 | + public function get( $params = array() ) { |
|
| 625 | + |
|
| 626 | + // Set the defaults. |
|
| 627 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 628 | + |
|
| 629 | + // Merge the defaults with the provided parameters. |
|
| 630 | + $args = wp_parse_args( $params, $defaults ); |
|
| 631 | + |
|
| 632 | + // Call the `get_posts` function. |
|
| 633 | + return get_posts( $args ); |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /** |
|
| 637 | + * The list of post type names which can be used for entities |
|
| 638 | + * |
|
| 639 | + * Criteria is that the post type is public. The list of valid post types |
|
| 640 | + * can be overridden with a filter. |
|
| 641 | + * |
|
| 642 | + * @return array Array containing the names of the valid post types. |
|
| 643 | + * @since 3.15.0 |
|
| 644 | + * |
|
| 645 | + */ |
|
| 646 | + static function valid_entity_post_types() { |
|
| 647 | + |
|
| 648 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 649 | + $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 650 | + |
|
| 651 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 652 | + } |
|
| 653 | 653 | |
| 654 | 654 | } |
@@ -99,9 +99,9 @@ discard block |
||
| 99 | 99 | * @since 3.2.0 |
| 100 | 100 | * |
| 101 | 101 | */ |
| 102 | - public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 102 | + public function __construct($ui_service, $relation_service, $entity_uri_service) { |
|
| 103 | 103 | |
| 104 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 104 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 105 | 105 | |
| 106 | 106 | $this->ui_service = $ui_service; |
| 107 | 107 | $this->relation_service = $relation_service; |
@@ -132,17 +132,17 @@ discard block |
||
| 132 | 132 | * @since 3.1.0 |
| 133 | 133 | * |
| 134 | 134 | */ |
| 135 | - public function is_entity( $post_id ) { |
|
| 135 | + public function is_entity($post_id) { |
|
| 136 | 136 | |
| 137 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 137 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 138 | 138 | |
| 139 | - if ( is_wp_error( $terms ) ) { |
|
| 140 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 139 | + if (is_wp_error($terms)) { |
|
| 140 | + $this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message()); |
|
| 141 | 141 | |
| 142 | 142 | return false; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | - if ( empty( $terms ) ) { |
|
| 145 | + if (empty($terms)) { |
|
| 146 | 146 | return false; |
| 147 | 147 | } |
| 148 | 148 | |
@@ -153,8 +153,8 @@ discard block |
||
| 153 | 153 | * |
| 154 | 154 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 155 | 155 | */ |
| 156 | - foreach ( $terms as $term ) { |
|
| 157 | - if ( 1 !== preg_match( '~(^|-)article$~', $term->slug ) ) { |
|
| 156 | + foreach ($terms as $term) { |
|
| 157 | + if (1 !== preg_match('~(^|-)article$~', $term->slug)) { |
|
| 158 | 158 | return true; |
| 159 | 159 | } |
| 160 | 160 | } |
@@ -174,19 +174,19 @@ discard block |
||
| 174 | 174 | * @since 3.5.0 |
| 175 | 175 | * |
| 176 | 176 | */ |
| 177 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 177 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 178 | 178 | |
| 179 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 179 | + if (false === $this->is_entity($post_id)) { |
|
| 180 | 180 | return $default; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | // Retrieve the entity type |
| 184 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 185 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 184 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 185 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 186 | 186 | // Retrieve classification boxes configuration |
| 187 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 188 | - foreach ( $classification_boxes as $cb ) { |
|
| 189 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 187 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 188 | + foreach ($classification_boxes as $cb) { |
|
| 189 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 190 | 190 | return $cb['id']; |
| 191 | 191 | } |
| 192 | 192 | } |
@@ -201,13 +201,13 @@ discard block |
||
| 201 | 201 | * |
| 202 | 202 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 203 | 203 | */ |
| 204 | - public function is_used( $post_id ) { |
|
| 204 | + public function is_used($post_id) { |
|
| 205 | 205 | |
| 206 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 206 | + if (false === $this->is_entity($post_id)) { |
|
| 207 | 207 | return null; |
| 208 | 208 | } |
| 209 | 209 | // Retrieve the post |
| 210 | - $entity = get_post( $post_id ); |
|
| 210 | + $entity = get_post($post_id); |
|
| 211 | 211 | |
| 212 | 212 | global $wpdb; |
| 213 | 213 | // Retrieve Wordlift relation instances table name |
@@ -220,9 +220,9 @@ discard block |
||
| 220 | 220 | ); |
| 221 | 221 | |
| 222 | 222 | // Perform the query |
| 223 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 223 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 224 | 224 | // If there is at least one relation instance for the current entity, then it's used |
| 225 | - if ( 0 < $relation_instances ) { |
|
| 225 | + if (0 < $relation_instances) { |
|
| 226 | 226 | return true; |
| 227 | 227 | } |
| 228 | 228 | |
@@ -230,13 +230,13 @@ discard block |
||
| 230 | 230 | $stmt = $wpdb->prepare( |
| 231 | 231 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 232 | 232 | $entity->ID, |
| 233 | - wl_get_entity_uri( $entity->ID ) |
|
| 233 | + wl_get_entity_uri($entity->ID) |
|
| 234 | 234 | ); |
| 235 | 235 | // Perform the query |
| 236 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 236 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 237 | 237 | |
| 238 | 238 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 239 | - if ( 0 < $meta_instances ) { |
|
| 239 | + if (0 < $meta_instances) { |
|
| 240 | 240 | return true; |
| 241 | 241 | } |
| 242 | 242 | |
@@ -256,9 +256,9 @@ discard block |
||
| 256 | 256 | * @since 3.2.0 |
| 257 | 257 | * |
| 258 | 258 | */ |
| 259 | - public function get_entity_post_by_uri( $uri ) { |
|
| 259 | + public function get_entity_post_by_uri($uri) { |
|
| 260 | 260 | |
| 261 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 261 | + return $this->entity_uri_service->get_entity($uri); |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | /** |
@@ -273,10 +273,10 @@ discard block |
||
| 273 | 273 | * @param WP_Post $post Post object. |
| 274 | 274 | * @param bool $update Whether this is an existing post being updated or not. |
| 275 | 275 | */ |
| 276 | - public function save_post( $post_id, $post, $update ) { |
|
| 276 | + public function save_post($post_id, $post, $update) { |
|
| 277 | 277 | |
| 278 | 278 | // Avoid doing anything if post is autosave or a revision. |
| 279 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 279 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 280 | 280 | return; |
| 281 | 281 | } |
| 282 | 282 | |
@@ -286,16 +286,16 @@ discard block |
||
| 286 | 286 | // the $post_id in the save_post call matches the post id set in the request. |
| 287 | 287 | // |
| 288 | 288 | // If this is not the current post being saved or if it's not an entity, return. |
| 289 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 289 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 290 | 290 | return; |
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | // Get the alt labels from the request (or empty array). |
| 294 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 294 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 295 | 295 | |
| 296 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 296 | + if (( ! empty($_POST['content']) && ! empty($_POST['post_content'])) || isset($_REQUEST['wl_alternative_label'])) { |
|
| 297 | 297 | // This is via classic editor, so set the alternative labels. |
| 298 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 298 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | |
@@ -310,28 +310,28 @@ discard block |
||
| 310 | 310 | * @since 3.2.0 |
| 311 | 311 | * |
| 312 | 312 | */ |
| 313 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 313 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 314 | 314 | |
| 315 | 315 | // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
| 316 | 316 | // instead of post id. |
| 317 | - if ( ! is_numeric( $post_id ) ) { |
|
| 317 | + if ( ! is_numeric($post_id)) { |
|
| 318 | 318 | return; |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | // Force $alt_labels to be an array |
| 322 | - if ( ! is_array( $alt_labels ) ) { |
|
| 323 | - $alt_labels = array( $alt_labels ); |
|
| 322 | + if ( ! is_array($alt_labels)) { |
|
| 323 | + $alt_labels = array($alt_labels); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 326 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 327 | 327 | |
| 328 | 328 | // Delete all the existing alternate labels. |
| 329 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 329 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 330 | 330 | |
| 331 | 331 | // Set the alternative labels. |
| 332 | - foreach ( $alt_labels as $alt_label ) { |
|
| 333 | - if ( ! empty( $alt_label ) ) { |
|
| 334 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 332 | + foreach ($alt_labels as $alt_label) { |
|
| 333 | + if ( ! empty($alt_label)) { |
|
| 334 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 335 | 335 | } |
| 336 | 336 | } |
| 337 | 337 | |
@@ -346,9 +346,9 @@ discard block |
||
| 346 | 346 | * @since 3.2.0 |
| 347 | 347 | * |
| 348 | 348 | */ |
| 349 | - public function get_alternative_labels( $post_id ) { |
|
| 349 | + public function get_alternative_labels($post_id) { |
|
| 350 | 350 | |
| 351 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 351 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | /** |
@@ -360,9 +360,9 @@ discard block |
||
| 360 | 360 | * @since 3.12.0 |
| 361 | 361 | * |
| 362 | 362 | */ |
| 363 | - public function get_labels( $post_id ) { |
|
| 363 | + public function get_labels($post_id) { |
|
| 364 | 364 | |
| 365 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 365 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | /** |
@@ -373,25 +373,25 @@ discard block |
||
| 373 | 373 | * @since 3.2.0 |
| 374 | 374 | * |
| 375 | 375 | */ |
| 376 | - public function edit_form_before_permalink( $post ) { |
|
| 376 | + public function edit_form_before_permalink($post) { |
|
| 377 | 377 | |
| 378 | 378 | // If it's not an entity, return. |
| 379 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 379 | + if ( ! $this->is_entity($post->ID)) { |
|
| 380 | 380 | return; |
| 381 | 381 | } |
| 382 | 382 | |
| 383 | 383 | // Print the input template. |
| 384 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 384 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 385 | 385 | |
| 386 | 386 | // Print all the currently set alternative labels. |
| 387 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 387 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 388 | 388 | |
| 389 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 389 | + echo $this->get_alternative_label_input($alt_label); |
|
| 390 | 390 | |
| 391 | 391 | }; |
| 392 | 392 | |
| 393 | 393 | // Print the button. |
| 394 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 394 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 395 | 395 | |
| 396 | 396 | } |
| 397 | 397 | |
@@ -404,65 +404,65 @@ discard block |
||
| 404 | 404 | * @since 3.6.0 |
| 405 | 405 | * |
| 406 | 406 | */ |
| 407 | - private function get_uri_for_post( $post_id ) { |
|
| 407 | + private function get_uri_for_post($post_id) { |
|
| 408 | 408 | |
| 409 | - $log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 409 | + $log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 410 | 410 | |
| 411 | 411 | // If a null is given, nothing to do |
| 412 | - if ( is_null( $post_id ) ) { |
|
| 412 | + if (is_null($post_id)) { |
|
| 413 | 413 | return null; |
| 414 | 414 | } |
| 415 | 415 | |
| 416 | 416 | $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
| 417 | 417 | |
| 418 | - if ( empty( $dataset_uri ) ) { |
|
| 418 | + if (empty($dataset_uri)) { |
|
| 419 | 419 | // Continue even if the dataset uri is not properly configured. It is handled in function wl_build_entity_uri() |
| 420 | - $log->debug( 'Continuing, dataset uri not configured...' ); |
|
| 420 | + $log->debug('Continuing, dataset uri not configured...'); |
|
| 421 | 421 | } |
| 422 | 422 | |
| 423 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 423 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 424 | 424 | |
| 425 | 425 | /* |
| 426 | 426 | * Consider the URI invalid if it doesn't start with the dataset URI. |
| 427 | 427 | * |
| 428 | 428 | * @see https://github.com/insideout10/wordlift-plugin/issues/996 |
| 429 | 429 | */ |
| 430 | - if ( null === $dataset_uri || 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 430 | + if (null === $dataset_uri || 0 !== strpos($uri, $dataset_uri)) { |
|
| 431 | 431 | $uri = null; |
| 432 | 432 | } |
| 433 | 433 | |
| 434 | 434 | // Set the URI if it isn't set yet. |
| 435 | - $post_status = get_post_status( $post_id ); |
|
| 436 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 437 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 438 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 435 | + $post_status = get_post_status($post_id); |
|
| 436 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 437 | + $uri = wl_build_entity_uri($post_id); |
|
| 438 | + wl_set_entity_uri($post_id, $uri); |
|
| 439 | 439 | } |
| 440 | 440 | |
| 441 | 441 | return $uri; |
| 442 | 442 | } |
| 443 | 443 | |
| 444 | - public function get_uri( $object_id, $type = Object_Type_Enum::POST ) { |
|
| 444 | + public function get_uri($object_id, $type = Object_Type_Enum::POST) { |
|
| 445 | 445 | |
| 446 | - if ( Object_Type_Enum::POST === $type ) { |
|
| 447 | - return $this->get_uri_for_post( $object_id ); |
|
| 446 | + if (Object_Type_Enum::POST === $type) { |
|
| 447 | + return $this->get_uri_for_post($object_id); |
|
| 448 | 448 | } |
| 449 | 449 | |
| 450 | - if ( Object_Type_Enum::USER === $type ) { |
|
| 451 | - $uri = get_user_meta( $object_id, 'entity_url', true ); |
|
| 452 | - if ( ! empty( $uri ) ) { |
|
| 450 | + if (Object_Type_Enum::USER === $type) { |
|
| 451 | + $uri = get_user_meta($object_id, 'entity_url', true); |
|
| 452 | + if ( ! empty($uri)) { |
|
| 453 | 453 | return $uri; |
| 454 | 454 | } |
| 455 | 455 | |
| 456 | 456 | $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
| 457 | - if ( empty( $dataset_uri ) ) { |
|
| 458 | - return get_author_posts_url( $object_id ) . '#author'; |
|
| 457 | + if (empty($dataset_uri)) { |
|
| 458 | + return get_author_posts_url($object_id).'#author'; |
|
| 459 | 459 | } else { |
| 460 | - $user = get_userdata( $object_id ); |
|
| 461 | - if ( empty( $user->user_nicename ) ) { |
|
| 460 | + $user = get_userdata($object_id); |
|
| 461 | + if (empty($user->user_nicename)) { |
|
| 462 | 462 | return null; |
| 463 | 463 | } |
| 464 | 464 | |
| 465 | - return sprintf( '%s/author/%s', $dataset_uri, $user->user_nicename ); |
|
| 465 | + return sprintf('%s/author/%s', $dataset_uri, $user->user_nicename); |
|
| 466 | 466 | } |
| 467 | 467 | } |
| 468 | 468 | |
@@ -477,9 +477,9 @@ discard block |
||
| 477 | 477 | * @since 3.2.0 |
| 478 | 478 | * |
| 479 | 479 | */ |
| 480 | - private function get_alternative_label_input( $value = '' ) { |
|
| 480 | + private function get_alternative_label_input($value = '') { |
|
| 481 | 481 | |
| 482 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 482 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 483 | 483 | } |
| 484 | 484 | |
| 485 | 485 | /** |
@@ -493,13 +493,13 @@ discard block |
||
| 493 | 493 | global $wpdb; |
| 494 | 494 | |
| 495 | 495 | // Try to get the count from the transient. |
| 496 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 497 | - if ( false !== $count ) { |
|
| 496 | + $count = get_transient('_wl_entity_service__count'); |
|
| 497 | + if (false !== $count) { |
|
| 498 | 498 | return $count; |
| 499 | 499 | } |
| 500 | 500 | |
| 501 | 501 | // Query the count. |
| 502 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 502 | + $count = $wpdb->get_var($wpdb->prepare( |
|
| 503 | 503 | "SELECT COUNT( DISTINCT( tr.object_id ) )" |
| 504 | 504 | . " FROM {$wpdb->term_relationships} tr" |
| 505 | 505 | . " INNER JOIN {$wpdb->term_taxonomy} tt" |
@@ -508,10 +508,10 @@ discard block |
||
| 508 | 508 | . " ON t.term_id = tt.term_id AND t.name != %s", |
| 509 | 509 | Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 510 | 510 | 'article' |
| 511 | - ) ); |
|
| 511 | + )); |
|
| 512 | 512 | |
| 513 | 513 | // Store the count in cache. |
| 514 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 514 | + set_transient('_wl_entity_service__count', $count, 900); |
|
| 515 | 515 | |
| 516 | 516 | return $count; |
| 517 | 517 | } |
@@ -526,7 +526,7 @@ discard block |
||
| 526 | 526 | * @since 3.15.0 |
| 527 | 527 | * |
| 528 | 528 | */ |
| 529 | - public static function add_criterias( $args ) { |
|
| 529 | + public static function add_criterias($args) { |
|
| 530 | 530 | |
| 531 | 531 | // Build an optimal tax-query. |
| 532 | 532 | $tax_query = array( |
@@ -570,28 +570,28 @@ discard block |
||
| 570 | 570 | * @since 3.9.0 |
| 571 | 571 | * |
| 572 | 572 | */ |
| 573 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 573 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 574 | 574 | |
| 575 | 575 | // Create an entity for the publisher. |
| 576 | - $post_id = @wp_insert_post( array( |
|
| 576 | + $post_id = @wp_insert_post(array( |
|
| 577 | 577 | 'post_type' => self::TYPE_NAME, |
| 578 | 578 | 'post_title' => $name, |
| 579 | 579 | 'post_status' => $status, |
| 580 | 580 | 'post_content' => '', |
| 581 | - ) ); |
|
| 581 | + )); |
|
| 582 | 582 | |
| 583 | 583 | // Return the error if any. |
| 584 | - if ( is_wp_error( $post_id ) ) { |
|
| 584 | + if (is_wp_error($post_id)) { |
|
| 585 | 585 | return $post_id; |
| 586 | 586 | } |
| 587 | 587 | |
| 588 | 588 | // Set the entity logo. |
| 589 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 590 | - set_post_thumbnail( $post_id, $logo ); |
|
| 589 | + if ($logo && is_numeric($logo)) { |
|
| 590 | + set_post_thumbnail($post_id, $logo); |
|
| 591 | 591 | } |
| 592 | 592 | |
| 593 | 593 | // Set the entity type. |
| 594 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 594 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 595 | 595 | |
| 596 | 596 | return $post_id; |
| 597 | 597 | } |
@@ -607,9 +607,9 @@ discard block |
||
| 607 | 607 | * @since 3.10.0 |
| 608 | 608 | * |
| 609 | 609 | */ |
| 610 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 610 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 611 | 611 | |
| 612 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 612 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 613 | 613 | } |
| 614 | 614 | |
| 615 | 615 | /** |
@@ -621,16 +621,16 @@ discard block |
||
| 621 | 621 | * @since 3.12.2 |
| 622 | 622 | * |
| 623 | 623 | */ |
| 624 | - public function get( $params = array() ) { |
|
| 624 | + public function get($params = array()) { |
|
| 625 | 625 | |
| 626 | 626 | // Set the defaults. |
| 627 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 627 | + $defaults = array('post_type' => 'entity'); |
|
| 628 | 628 | |
| 629 | 629 | // Merge the defaults with the provided parameters. |
| 630 | - $args = wp_parse_args( $params, $defaults ); |
|
| 630 | + $args = wp_parse_args($params, $defaults); |
|
| 631 | 631 | |
| 632 | 632 | // Call the `get_posts` function. |
| 633 | - return get_posts( $args ); |
|
| 633 | + return get_posts($args); |
|
| 634 | 634 | } |
| 635 | 635 | |
| 636 | 636 | /** |
@@ -646,9 +646,9 @@ discard block |
||
| 646 | 646 | static function valid_entity_post_types() { |
| 647 | 647 | |
| 648 | 648 | // Ignore builtins in the call to avoid getting attachments. |
| 649 | - $post_types = array( 'post', 'page', self::TYPE_NAME, 'product' ); |
|
| 649 | + $post_types = array('post', 'page', self::TYPE_NAME, 'product'); |
|
| 650 | 650 | |
| 651 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 651 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 652 | 652 | } |
| 653 | 653 | |
| 654 | 654 | } |
@@ -47,11 +47,18 @@ |
||
| 47 | 47 | |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | + /** |
|
| 51 | + * @param string $meta_key |
|
| 52 | + * @param boolean $single |
|
| 53 | + */ |
|
| 50 | 54 | function get_meta( $meta_key, $single ) { |
| 51 | 55 | |
| 52 | 56 | return call_user_func( $this->get_meta, $this->object_id, $meta_key, $single ); |
| 53 | 57 | } |
| 54 | 58 | |
| 59 | + /** |
|
| 60 | + * @param string $meta_key |
|
| 61 | + */ |
|
| 55 | 62 | function update_meta( $meta_key, $meta_value ) { |
| 56 | 63 | |
| 57 | 64 | call_user_func( $this->update_meta, $this->object_id, $meta_key, $meta_value ); |
@@ -7,55 +7,55 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class Abstract_Sync_Object_Adapter implements Sync_Object_Adapter { |
| 9 | 9 | |
| 10 | - private $object_id; |
|
| 10 | + private $object_id; |
|
| 11 | 11 | |
| 12 | - private $type; |
|
| 12 | + private $type; |
|
| 13 | 13 | |
| 14 | - private $get_meta; |
|
| 14 | + private $get_meta; |
|
| 15 | 15 | |
| 16 | - private $update_meta; |
|
| 16 | + private $update_meta; |
|
| 17 | 17 | |
| 18 | - private static $meta_name = array( |
|
| 19 | - Object_Type_Enum::POST => 'post', |
|
| 20 | - Object_Type_Enum::TERM => 'term', |
|
| 21 | - Object_Type_Enum::USER => 'user', |
|
| 22 | - ); |
|
| 18 | + private static $meta_name = array( |
|
| 19 | + Object_Type_Enum::POST => 'post', |
|
| 20 | + Object_Type_Enum::TERM => 'term', |
|
| 21 | + Object_Type_Enum::USER => 'user', |
|
| 22 | + ); |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Sync_Object_Adapter constructor. |
|
| 26 | - * |
|
| 27 | - * @param int $type One of Object_Type_Enum. |
|
| 28 | - * @param int $object_id A post or term id. |
|
| 29 | - * @param Jsonld_Service |
|
| 30 | - * |
|
| 31 | - * @throws \Exception |
|
| 32 | - */ |
|
| 33 | - function __construct( $type, $object_id ) { |
|
| 24 | + /** |
|
| 25 | + * Sync_Object_Adapter constructor. |
|
| 26 | + * |
|
| 27 | + * @param int $type One of Object_Type_Enum. |
|
| 28 | + * @param int $object_id A post or term id. |
|
| 29 | + * @param Jsonld_Service |
|
| 30 | + * |
|
| 31 | + * @throws \Exception |
|
| 32 | + */ |
|
| 33 | + function __construct( $type, $object_id ) { |
|
| 34 | 34 | |
| 35 | - $this->type = filter_var( $type, FILTER_VALIDATE_INT ); |
|
| 36 | - $this->object_id = filter_var( $object_id, FILTER_VALIDATE_INT ); |
|
| 35 | + $this->type = filter_var( $type, FILTER_VALIDATE_INT ); |
|
| 36 | + $this->object_id = filter_var( $object_id, FILTER_VALIDATE_INT ); |
|
| 37 | 37 | |
| 38 | - if ( null === $this->type || ! isset( self::$meta_name[ $this->type ] ) ) { |
|
| 39 | - throw new \Exception( 'Invalid $type.' ); |
|
| 40 | - } |
|
| 41 | - if ( null === $this->object_id ) { |
|
| 42 | - throw new \Exception( 'Invalid $object.' ); |
|
| 43 | - } |
|
| 38 | + if ( null === $this->type || ! isset( self::$meta_name[ $this->type ] ) ) { |
|
| 39 | + throw new \Exception( 'Invalid $type.' ); |
|
| 40 | + } |
|
| 41 | + if ( null === $this->object_id ) { |
|
| 42 | + throw new \Exception( 'Invalid $object.' ); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - $this->get_meta = sprintf( 'get_%s_meta', self::$meta_name[ $this->type ] ); |
|
| 46 | - $this->update_meta = sprintf( 'update_%s_meta', self::$meta_name[ $this->type ] ); |
|
| 45 | + $this->get_meta = sprintf( 'get_%s_meta', self::$meta_name[ $this->type ] ); |
|
| 46 | + $this->update_meta = sprintf( 'update_%s_meta', self::$meta_name[ $this->type ] ); |
|
| 47 | 47 | |
| 48 | - } |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - function get_meta( $meta_key, $single ) { |
|
| 50 | + function get_meta( $meta_key, $single ) { |
|
| 51 | 51 | |
| 52 | - return call_user_func( $this->get_meta, $this->object_id, $meta_key, $single ); |
|
| 53 | - } |
|
| 52 | + return call_user_func( $this->get_meta, $this->object_id, $meta_key, $single ); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - function update_meta( $meta_key, $meta_value ) { |
|
| 55 | + function update_meta( $meta_key, $meta_value ) { |
|
| 56 | 56 | |
| 57 | - call_user_func( $this->update_meta, $this->object_id, $meta_key, $meta_value ); |
|
| 57 | + call_user_func( $this->update_meta, $this->object_id, $meta_key, $meta_value ); |
|
| 58 | 58 | |
| 59 | - } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | 61 | } |
@@ -30,31 +30,31 @@ |
||
| 30 | 30 | * |
| 31 | 31 | * @throws \Exception |
| 32 | 32 | */ |
| 33 | - function __construct( $type, $object_id ) { |
|
| 33 | + function __construct($type, $object_id) { |
|
| 34 | 34 | |
| 35 | - $this->type = filter_var( $type, FILTER_VALIDATE_INT ); |
|
| 36 | - $this->object_id = filter_var( $object_id, FILTER_VALIDATE_INT ); |
|
| 35 | + $this->type = filter_var($type, FILTER_VALIDATE_INT); |
|
| 36 | + $this->object_id = filter_var($object_id, FILTER_VALIDATE_INT); |
|
| 37 | 37 | |
| 38 | - if ( null === $this->type || ! isset( self::$meta_name[ $this->type ] ) ) { |
|
| 39 | - throw new \Exception( 'Invalid $type.' ); |
|
| 38 | + if (null === $this->type || ! isset(self::$meta_name[$this->type])) { |
|
| 39 | + throw new \Exception('Invalid $type.'); |
|
| 40 | 40 | } |
| 41 | - if ( null === $this->object_id ) { |
|
| 42 | - throw new \Exception( 'Invalid $object.' ); |
|
| 41 | + if (null === $this->object_id) { |
|
| 42 | + throw new \Exception('Invalid $object.'); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - $this->get_meta = sprintf( 'get_%s_meta', self::$meta_name[ $this->type ] ); |
|
| 46 | - $this->update_meta = sprintf( 'update_%s_meta', self::$meta_name[ $this->type ] ); |
|
| 45 | + $this->get_meta = sprintf('get_%s_meta', self::$meta_name[$this->type]); |
|
| 46 | + $this->update_meta = sprintf('update_%s_meta', self::$meta_name[$this->type]); |
|
| 47 | 47 | |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - function get_meta( $meta_key, $single ) { |
|
| 50 | + function get_meta($meta_key, $single) { |
|
| 51 | 51 | |
| 52 | - return call_user_func( $this->get_meta, $this->object_id, $meta_key, $single ); |
|
| 52 | + return call_user_func($this->get_meta, $this->object_id, $meta_key, $single); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - function update_meta( $meta_key, $meta_value ) { |
|
| 55 | + function update_meta($meta_key, $meta_value) { |
|
| 56 | 56 | |
| 57 | - call_user_func( $this->update_meta, $this->object_id, $meta_key, $meta_value ); |
|
| 57 | + call_user_func($this->update_meta, $this->object_id, $meta_key, $meta_value); |
|
| 58 | 58 | |
| 59 | 59 | } |
| 60 | 60 | |
@@ -88,7 +88,7 @@ |
||
| 88 | 88 | * @param int $type |
| 89 | 89 | * @param int $object_id |
| 90 | 90 | * |
| 91 | - * @return array|false |
|
| 91 | + * @return boolean |
|
| 92 | 92 | * @throws \Exception |
| 93 | 93 | */ |
| 94 | 94 | public function sync_one( $type, $object_id ) { |
@@ -7,187 +7,187 @@ discard block |
||
| 7 | 7 | use Wordlift\Object_Type_Enum; |
| 8 | 8 | |
| 9 | 9 | class Sync_Service { |
| 10 | - const JSONLD_HASH = '_wl_jsonld_hash'; |
|
| 11 | - const SYNCED_GMT = '_wl_synced_gmt'; |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * @var \Wordlift_Log_Service |
|
| 15 | - */ |
|
| 16 | - private $log; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @var Api_Service |
|
| 20 | - */ |
|
| 21 | - private $api_service; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @var Jsonld_Service |
|
| 25 | - */ |
|
| 26 | - private $jsonld_service; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var Sync_Background_Process |
|
| 30 | - */ |
|
| 31 | - private $sync_background_process; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * The number of posts processed in one call. |
|
| 35 | - * |
|
| 36 | - * @var int The batch size. |
|
| 37 | - */ |
|
| 38 | - private $batch_size; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var Sync_Object_Adapter_Factory |
|
| 42 | - */ |
|
| 43 | - private $sync_object_adapter_factory; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var Sync_Service |
|
| 47 | - */ |
|
| 48 | - private static $instance; |
|
| 49 | - private $entity_service; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Constructor. |
|
| 53 | - * |
|
| 54 | - * @param Api_Service $api_service The {@link Api_Service} used to communicate with the remote APIs. |
|
| 55 | - * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 56 | - * @param Jsonld_Service $jsonld_service |
|
| 57 | - * @param \Wordlift_Entity_Service $entity_service |
|
| 58 | - */ |
|
| 59 | - public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 60 | - |
|
| 61 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 62 | - |
|
| 63 | - $this->api_service = $api_service; |
|
| 64 | - $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 65 | - $this->jsonld_service = $jsonld_service; |
|
| 66 | - $this->entity_service = $entity_service; |
|
| 67 | - $this->batch_size = 10; |
|
| 68 | - |
|
| 69 | - // You need to initialize this early, otherwise the Background Process isn't registered in AJAX calls. |
|
| 70 | - $this->sync_background_process = new Sync_Background_Process( $this );; |
|
| 71 | - |
|
| 72 | - // Exclude the JSONLD_HASH meta key from those that require a resync. |
|
| 73 | - add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 74 | - $args[] = Sync_Service::JSONLD_HASH; |
|
| 75 | - $args[] = Sync_Service::SYNCED_GMT; |
|
| 76 | - |
|
| 77 | - return $args; |
|
| 78 | - } ); |
|
| 79 | - |
|
| 80 | - self::$instance = $this; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - public static function get_instance() { |
|
| 84 | - return self::$instance; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @param int $type |
|
| 89 | - * @param int $object_id |
|
| 90 | - * |
|
| 91 | - * @return array|false |
|
| 92 | - * @throws \Exception |
|
| 93 | - */ |
|
| 94 | - public function sync_one( $type, $object_id ) { |
|
| 95 | - |
|
| 96 | - $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 97 | - $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) ); |
|
| 98 | - $new_hash = sha1( $jsonld_as_string ); |
|
| 99 | - |
|
| 100 | - $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 101 | - $old_hash = $object->get_meta( self::JSONLD_HASH, true ); |
|
| 102 | - |
|
| 103 | - // JSON-LD hasn't changed, bail out. |
|
| 104 | - if ( $new_hash === $old_hash ) { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 109 | - |
|
| 110 | - // Entity URL isn't set, bail out. |
|
| 111 | - if ( empty( $uri ) ) { |
|
| 112 | - return false; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $response = $this->api_service->request( |
|
| 116 | - 'POST', '/middleware/dataset/batch', |
|
| 117 | - array( 'Content-Type' => 'application/json', ), |
|
| 118 | - wp_json_encode( array( |
|
| 119 | - array( |
|
| 120 | - 'uri' => $uri, |
|
| 121 | - 'model' => $jsonld_as_string, |
|
| 122 | - 'private' => ! $object->is_public(), |
|
| 123 | - ) |
|
| 124 | - ) ) ); |
|
| 125 | - |
|
| 126 | - // Update the sync date in case of success, otherwise log an error. |
|
| 127 | - if ( ! $response->is_success() ) { |
|
| 128 | - return false; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $object->update_meta( self::JSONLD_HASH, $new_hash ); |
|
| 132 | - $object->update_meta( self::SYNCED_GMT, current_time( 'mysql', true ) ); |
|
| 133 | - |
|
| 134 | - return true; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - public function delete_one( $type, $object_id ) { |
|
| 138 | - $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 139 | - $uri = $object->get_meta( 'entity_url', true ); |
|
| 140 | - |
|
| 141 | - // Entity URL isn't set, bail out. |
|
| 142 | - if ( empty( $uri ) ) { |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - $response = $this->api_service->request( |
|
| 147 | - 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 148 | - |
|
| 149 | - // Update the sync date in case of success, otherwise log an error. |
|
| 150 | - if ( ! $response->is_success() ) { |
|
| 151 | - return false; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - return true; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Starts a new synchronization. |
|
| 159 | - */ |
|
| 160 | - public function start() { |
|
| 161 | - |
|
| 162 | - // Create the Sync_Background_Process. |
|
| 163 | - $this->sync_background_process->start(); |
|
| 164 | - |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Request to cancel a background process. |
|
| 169 | - */ |
|
| 170 | - public function request_cancel() { |
|
| 171 | - |
|
| 172 | - $this->sync_background_process->request_cancel(); |
|
| 173 | - |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Get the next post IDs to synchronize. |
|
| 178 | - * |
|
| 179 | - * @return array An array of post IDs. |
|
| 180 | - */ |
|
| 181 | - public function next() { |
|
| 182 | - global $wpdb; |
|
| 183 | - |
|
| 184 | - $state = $this->info(); |
|
| 185 | - |
|
| 186 | - // Limit the query to the allowed post types. |
|
| 187 | - $post_type_in = implode( "','", array_map( 'esc_sql', \Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
| 188 | - |
|
| 189 | - // Get the next post ID. |
|
| 190 | - return $wpdb->get_col( " |
|
| 10 | + const JSONLD_HASH = '_wl_jsonld_hash'; |
|
| 11 | + const SYNCED_GMT = '_wl_synced_gmt'; |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * @var \Wordlift_Log_Service |
|
| 15 | + */ |
|
| 16 | + private $log; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @var Api_Service |
|
| 20 | + */ |
|
| 21 | + private $api_service; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @var Jsonld_Service |
|
| 25 | + */ |
|
| 26 | + private $jsonld_service; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var Sync_Background_Process |
|
| 30 | + */ |
|
| 31 | + private $sync_background_process; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * The number of posts processed in one call. |
|
| 35 | + * |
|
| 36 | + * @var int The batch size. |
|
| 37 | + */ |
|
| 38 | + private $batch_size; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var Sync_Object_Adapter_Factory |
|
| 42 | + */ |
|
| 43 | + private $sync_object_adapter_factory; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var Sync_Service |
|
| 47 | + */ |
|
| 48 | + private static $instance; |
|
| 49 | + private $entity_service; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Constructor. |
|
| 53 | + * |
|
| 54 | + * @param Api_Service $api_service The {@link Api_Service} used to communicate with the remote APIs. |
|
| 55 | + * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 56 | + * @param Jsonld_Service $jsonld_service |
|
| 57 | + * @param \Wordlift_Entity_Service $entity_service |
|
| 58 | + */ |
|
| 59 | + public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 60 | + |
|
| 61 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 62 | + |
|
| 63 | + $this->api_service = $api_service; |
|
| 64 | + $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 65 | + $this->jsonld_service = $jsonld_service; |
|
| 66 | + $this->entity_service = $entity_service; |
|
| 67 | + $this->batch_size = 10; |
|
| 68 | + |
|
| 69 | + // You need to initialize this early, otherwise the Background Process isn't registered in AJAX calls. |
|
| 70 | + $this->sync_background_process = new Sync_Background_Process( $this );; |
|
| 71 | + |
|
| 72 | + // Exclude the JSONLD_HASH meta key from those that require a resync. |
|
| 73 | + add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 74 | + $args[] = Sync_Service::JSONLD_HASH; |
|
| 75 | + $args[] = Sync_Service::SYNCED_GMT; |
|
| 76 | + |
|
| 77 | + return $args; |
|
| 78 | + } ); |
|
| 79 | + |
|
| 80 | + self::$instance = $this; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + public static function get_instance() { |
|
| 84 | + return self::$instance; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @param int $type |
|
| 89 | + * @param int $object_id |
|
| 90 | + * |
|
| 91 | + * @return array|false |
|
| 92 | + * @throws \Exception |
|
| 93 | + */ |
|
| 94 | + public function sync_one( $type, $object_id ) { |
|
| 95 | + |
|
| 96 | + $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 97 | + $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) ); |
|
| 98 | + $new_hash = sha1( $jsonld_as_string ); |
|
| 99 | + |
|
| 100 | + $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 101 | + $old_hash = $object->get_meta( self::JSONLD_HASH, true ); |
|
| 102 | + |
|
| 103 | + // JSON-LD hasn't changed, bail out. |
|
| 104 | + if ( $new_hash === $old_hash ) { |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 109 | + |
|
| 110 | + // Entity URL isn't set, bail out. |
|
| 111 | + if ( empty( $uri ) ) { |
|
| 112 | + return false; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $response = $this->api_service->request( |
|
| 116 | + 'POST', '/middleware/dataset/batch', |
|
| 117 | + array( 'Content-Type' => 'application/json', ), |
|
| 118 | + wp_json_encode( array( |
|
| 119 | + array( |
|
| 120 | + 'uri' => $uri, |
|
| 121 | + 'model' => $jsonld_as_string, |
|
| 122 | + 'private' => ! $object->is_public(), |
|
| 123 | + ) |
|
| 124 | + ) ) ); |
|
| 125 | + |
|
| 126 | + // Update the sync date in case of success, otherwise log an error. |
|
| 127 | + if ( ! $response->is_success() ) { |
|
| 128 | + return false; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $object->update_meta( self::JSONLD_HASH, $new_hash ); |
|
| 132 | + $object->update_meta( self::SYNCED_GMT, current_time( 'mysql', true ) ); |
|
| 133 | + |
|
| 134 | + return true; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + public function delete_one( $type, $object_id ) { |
|
| 138 | + $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 139 | + $uri = $object->get_meta( 'entity_url', true ); |
|
| 140 | + |
|
| 141 | + // Entity URL isn't set, bail out. |
|
| 142 | + if ( empty( $uri ) ) { |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + $response = $this->api_service->request( |
|
| 147 | + 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 148 | + |
|
| 149 | + // Update the sync date in case of success, otherwise log an error. |
|
| 150 | + if ( ! $response->is_success() ) { |
|
| 151 | + return false; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + return true; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Starts a new synchronization. |
|
| 159 | + */ |
|
| 160 | + public function start() { |
|
| 161 | + |
|
| 162 | + // Create the Sync_Background_Process. |
|
| 163 | + $this->sync_background_process->start(); |
|
| 164 | + |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Request to cancel a background process. |
|
| 169 | + */ |
|
| 170 | + public function request_cancel() { |
|
| 171 | + |
|
| 172 | + $this->sync_background_process->request_cancel(); |
|
| 173 | + |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Get the next post IDs to synchronize. |
|
| 178 | + * |
|
| 179 | + * @return array An array of post IDs. |
|
| 180 | + */ |
|
| 181 | + public function next() { |
|
| 182 | + global $wpdb; |
|
| 183 | + |
|
| 184 | + $state = $this->info(); |
|
| 185 | + |
|
| 186 | + // Limit the query to the allowed post types. |
|
| 187 | + $post_type_in = implode( "','", array_map( 'esc_sql', \Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
| 188 | + |
|
| 189 | + // Get the next post ID. |
|
| 190 | + return $wpdb->get_col( " |
|
| 191 | 191 | SELECT p.ID |
| 192 | 192 | FROM $wpdb->posts p |
| 193 | 193 | WHERE p.post_status = 'publish' |
@@ -195,118 +195,118 @@ discard block |
||
| 195 | 195 | ORDER BY p.ID |
| 196 | 196 | LIMIT {$state->index},{$this->batch_size} |
| 197 | 197 | " ); |
| 198 | - } |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - public function count() { |
|
| 201 | - global $wpdb; |
|
| 202 | - $post_type_in = implode( "','", array_map( 'esc_sql', \Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
| 200 | + public function count() { |
|
| 201 | + global $wpdb; |
|
| 202 | + $post_type_in = implode( "','", array_map( 'esc_sql', \Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
| 203 | 203 | |
| 204 | - return $wpdb->get_var( " |
|
| 204 | + return $wpdb->get_var( " |
|
| 205 | 205 | SELECT COUNT(1) |
| 206 | 206 | FROM $wpdb->posts p |
| 207 | 207 | WHERE p.post_status = 'publish' |
| 208 | 208 | AND p.post_type IN ('$post_type_in') |
| 209 | 209 | " ); |
| 210 | - } |
|
| 211 | - |
|
| 212 | - public function info() { |
|
| 213 | - return Sync_Background_Process::get_state(); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - public function sync_items( |
|
| 217 | - $post_ids, $clear = true |
|
| 218 | - ) { |
|
| 219 | - |
|
| 220 | - $this->log->debug( sprintf( 'Synchronizing post(s) %s...', implode( ', ', $post_ids ) ) ); |
|
| 221 | - |
|
| 222 | - // If we're starting the sync, try to clear the dataset. |
|
| 223 | - if ( $clear && 0 === $this->info()->index ) { |
|
| 224 | - $this->api_service->request( 'DELETE', '/middleware/dataset/delete' ); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - $that = $this; |
|
| 228 | - $request_body = array_filter( array_map( function ( $post_id ) use ( $that ) { |
|
| 229 | - // Check if the post type is public. |
|
| 230 | - $post_type = get_post_type( $post_id ); |
|
| 231 | - $post_type_obj = get_post_type_object( $post_type ); |
|
| 232 | - if ( ! $post_type_obj->public ) { |
|
| 233 | - return false; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - $is_private = ( 'publish' !== get_post_status( $post_id ) ); |
|
| 237 | - $uri = get_post_meta( $post_id, 'entity_url', true ); |
|
| 238 | - $object_adapter = $that->sync_object_adapter_factory->create( Object_Type_Enum::POST, $post_id ); |
|
| 239 | - $jsonld = $object_adapter->get_jsonld_and_update_hash(); |
|
| 240 | - $jsonld_as_string = wp_json_encode( $jsonld ); |
|
| 241 | - |
|
| 242 | - $that->log->trace( "Posting JSON-LD:\n$jsonld_as_string" ); |
|
| 243 | - |
|
| 244 | - return array( |
|
| 245 | - 'uri' => $uri, |
|
| 246 | - 'model' => $jsonld_as_string, |
|
| 247 | - 'private' => $is_private |
|
| 248 | - ); |
|
| 249 | - }, $post_ids ) ); |
|
| 250 | - |
|
| 251 | - // There's no point in making a request if the request is empty. |
|
| 252 | - if ( empty( $request_body ) ) { |
|
| 253 | - return true; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - // Make a request to the remote endpoint. |
|
| 257 | - $state = $this->info(); |
|
| 258 | - $state_header_value = str_replace( "\n", '', wp_json_encode( $state ) ); |
|
| 259 | - $response = $this->api_service->request( |
|
| 260 | - 'POST', '/middleware/dataset/batch', |
|
| 261 | - array( |
|
| 262 | - 'Content-Type' => 'application/json', |
|
| 263 | - 'X-Wordlift-Dataset-Sync-State-V1' => $state_header_value |
|
| 264 | - ), |
|
| 265 | - wp_json_encode( $request_body ) ); |
|
| 266 | - |
|
| 267 | - $this->log->debug( "Response received: " . ( $response->is_success() ? 'yes' : 'no' ) ); |
|
| 268 | - |
|
| 269 | - // Update the sync date in case of success, otherwise log an error. |
|
| 270 | - if ( $response->is_success() ) { |
|
| 271 | - |
|
| 272 | - foreach ( $post_ids as $post_id ) { |
|
| 273 | - update_post_meta( $post_id, '_wl_synced_gmt', current_time( 'mysql', true ) ); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - $this->log->debug( sprintf( 'Posts %s synchronized.', implode( ', ', $post_ids ) ) ); |
|
| 277 | - |
|
| 278 | - return true; |
|
| 279 | - } else { |
|
| 280 | - // @@todo: should we put a limit retry here? |
|
| 281 | - $response_dump = var_export( $response, true ); |
|
| 282 | - $this->log->error( |
|
| 283 | - sprintf( 'An error occurred while synchronizing the data for post IDs %s: %s', implode( ', ', $post_ids ), $response_dump ) ); |
|
| 284 | - |
|
| 285 | - return false; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * @param $post_id |
|
| 292 | - * |
|
| 293 | - * @todo Complete the delete item. |
|
| 294 | - */ |
|
| 295 | - public function delete_item( $post_id ) { |
|
| 296 | - $uri = get_post_meta( $post_id, 'entity_url', true ); |
|
| 297 | - // Make a request to the remote endpoint. |
|
| 298 | - $state_header_value = str_replace( wp_json_encode( $this->info() ), "\n", '' ); |
|
| 299 | - $response = $this->api_service->request( |
|
| 300 | - 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 301 | - array( |
|
| 302 | - 'Content-Type' => 'application/ld+json', |
|
| 303 | - 'X-Wordlift-Dataset-Sync-State-V1' => $state_header_value |
|
| 304 | - ) ); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - public function get_batch_size() { |
|
| 308 | - |
|
| 309 | - return $this->batch_size; |
|
| 310 | - } |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + public function info() { |
|
| 213 | + return Sync_Background_Process::get_state(); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + public function sync_items( |
|
| 217 | + $post_ids, $clear = true |
|
| 218 | + ) { |
|
| 219 | + |
|
| 220 | + $this->log->debug( sprintf( 'Synchronizing post(s) %s...', implode( ', ', $post_ids ) ) ); |
|
| 221 | + |
|
| 222 | + // If we're starting the sync, try to clear the dataset. |
|
| 223 | + if ( $clear && 0 === $this->info()->index ) { |
|
| 224 | + $this->api_service->request( 'DELETE', '/middleware/dataset/delete' ); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + $that = $this; |
|
| 228 | + $request_body = array_filter( array_map( function ( $post_id ) use ( $that ) { |
|
| 229 | + // Check if the post type is public. |
|
| 230 | + $post_type = get_post_type( $post_id ); |
|
| 231 | + $post_type_obj = get_post_type_object( $post_type ); |
|
| 232 | + if ( ! $post_type_obj->public ) { |
|
| 233 | + return false; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + $is_private = ( 'publish' !== get_post_status( $post_id ) ); |
|
| 237 | + $uri = get_post_meta( $post_id, 'entity_url', true ); |
|
| 238 | + $object_adapter = $that->sync_object_adapter_factory->create( Object_Type_Enum::POST, $post_id ); |
|
| 239 | + $jsonld = $object_adapter->get_jsonld_and_update_hash(); |
|
| 240 | + $jsonld_as_string = wp_json_encode( $jsonld ); |
|
| 241 | + |
|
| 242 | + $that->log->trace( "Posting JSON-LD:\n$jsonld_as_string" ); |
|
| 243 | + |
|
| 244 | + return array( |
|
| 245 | + 'uri' => $uri, |
|
| 246 | + 'model' => $jsonld_as_string, |
|
| 247 | + 'private' => $is_private |
|
| 248 | + ); |
|
| 249 | + }, $post_ids ) ); |
|
| 250 | + |
|
| 251 | + // There's no point in making a request if the request is empty. |
|
| 252 | + if ( empty( $request_body ) ) { |
|
| 253 | + return true; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + // Make a request to the remote endpoint. |
|
| 257 | + $state = $this->info(); |
|
| 258 | + $state_header_value = str_replace( "\n", '', wp_json_encode( $state ) ); |
|
| 259 | + $response = $this->api_service->request( |
|
| 260 | + 'POST', '/middleware/dataset/batch', |
|
| 261 | + array( |
|
| 262 | + 'Content-Type' => 'application/json', |
|
| 263 | + 'X-Wordlift-Dataset-Sync-State-V1' => $state_header_value |
|
| 264 | + ), |
|
| 265 | + wp_json_encode( $request_body ) ); |
|
| 266 | + |
|
| 267 | + $this->log->debug( "Response received: " . ( $response->is_success() ? 'yes' : 'no' ) ); |
|
| 268 | + |
|
| 269 | + // Update the sync date in case of success, otherwise log an error. |
|
| 270 | + if ( $response->is_success() ) { |
|
| 271 | + |
|
| 272 | + foreach ( $post_ids as $post_id ) { |
|
| 273 | + update_post_meta( $post_id, '_wl_synced_gmt', current_time( 'mysql', true ) ); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + $this->log->debug( sprintf( 'Posts %s synchronized.', implode( ', ', $post_ids ) ) ); |
|
| 277 | + |
|
| 278 | + return true; |
|
| 279 | + } else { |
|
| 280 | + // @@todo: should we put a limit retry here? |
|
| 281 | + $response_dump = var_export( $response, true ); |
|
| 282 | + $this->log->error( |
|
| 283 | + sprintf( 'An error occurred while synchronizing the data for post IDs %s: %s', implode( ', ', $post_ids ), $response_dump ) ); |
|
| 284 | + |
|
| 285 | + return false; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * @param $post_id |
|
| 292 | + * |
|
| 293 | + * @todo Complete the delete item. |
|
| 294 | + */ |
|
| 295 | + public function delete_item( $post_id ) { |
|
| 296 | + $uri = get_post_meta( $post_id, 'entity_url', true ); |
|
| 297 | + // Make a request to the remote endpoint. |
|
| 298 | + $state_header_value = str_replace( wp_json_encode( $this->info() ), "\n", '' ); |
|
| 299 | + $response = $this->api_service->request( |
|
| 300 | + 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 301 | + array( |
|
| 302 | + 'Content-Type' => 'application/ld+json', |
|
| 303 | + 'X-Wordlift-Dataset-Sync-State-V1' => $state_header_value |
|
| 304 | + ) ); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + public function get_batch_size() { |
|
| 308 | + |
|
| 309 | + return $this->batch_size; |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | 312 | } |
@@ -56,9 +56,9 @@ discard block |
||
| 56 | 56 | * @param Jsonld_Service $jsonld_service |
| 57 | 57 | * @param \Wordlift_Entity_Service $entity_service |
| 58 | 58 | */ |
| 59 | - public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 59 | + public function __construct($api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service) { |
|
| 60 | 60 | |
| 61 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 61 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 62 | 62 | |
| 63 | 63 | $this->api_service = $api_service; |
| 64 | 64 | $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
@@ -67,10 +67,10 @@ discard block |
||
| 67 | 67 | $this->batch_size = 10; |
| 68 | 68 | |
| 69 | 69 | // You need to initialize this early, otherwise the Background Process isn't registered in AJAX calls. |
| 70 | - $this->sync_background_process = new Sync_Background_Process( $this );; |
|
| 70 | + $this->sync_background_process = new Sync_Background_Process($this); ; |
|
| 71 | 71 | |
| 72 | 72 | // Exclude the JSONLD_HASH meta key from those that require a resync. |
| 73 | - add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 73 | + add_filter('wl_dataset__sync_hooks__ignored_meta_keys', function($args) { |
|
| 74 | 74 | $args[] = Sync_Service::JSONLD_HASH; |
| 75 | 75 | $args[] = Sync_Service::SYNCED_GMT; |
| 76 | 76 | |
@@ -91,63 +91,63 @@ discard block |
||
| 91 | 91 | * @return array|false |
| 92 | 92 | * @throws \Exception |
| 93 | 93 | */ |
| 94 | - public function sync_one( $type, $object_id ) { |
|
| 94 | + public function sync_one($type, $object_id) { |
|
| 95 | 95 | |
| 96 | - $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 97 | - $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) ); |
|
| 98 | - $new_hash = sha1( $jsonld_as_string ); |
|
| 96 | + $jsonld_as_string = wp_json_encode(apply_filters('wl_dataset__sync_service__sync_item__jsonld', |
|
| 97 | + $this->jsonld_service->get($type, $object_id), $type, $object_id)); |
|
| 98 | + $new_hash = sha1($jsonld_as_string); |
|
| 99 | 99 | |
| 100 | - $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 101 | - $old_hash = $object->get_meta( self::JSONLD_HASH, true ); |
|
| 100 | + $object = $this->sync_object_adapter_factory->create($type, $object_id); |
|
| 101 | + $old_hash = $object->get_meta(self::JSONLD_HASH, true); |
|
| 102 | 102 | |
| 103 | 103 | // JSON-LD hasn't changed, bail out. |
| 104 | - if ( $new_hash === $old_hash ) { |
|
| 104 | + if ($new_hash === $old_hash) { |
|
| 105 | 105 | return false; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 108 | + $uri = $this->entity_service->get_uri($object_id, $type); |
|
| 109 | 109 | |
| 110 | 110 | // Entity URL isn't set, bail out. |
| 111 | - if ( empty( $uri ) ) { |
|
| 111 | + if (empty($uri)) { |
|
| 112 | 112 | return false; |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | $response = $this->api_service->request( |
| 116 | 116 | 'POST', '/middleware/dataset/batch', |
| 117 | - array( 'Content-Type' => 'application/json', ), |
|
| 118 | - wp_json_encode( array( |
|
| 117 | + array('Content-Type' => 'application/json',), |
|
| 118 | + wp_json_encode(array( |
|
| 119 | 119 | array( |
| 120 | 120 | 'uri' => $uri, |
| 121 | 121 | 'model' => $jsonld_as_string, |
| 122 | 122 | 'private' => ! $object->is_public(), |
| 123 | 123 | ) |
| 124 | - ) ) ); |
|
| 124 | + )) ); |
|
| 125 | 125 | |
| 126 | 126 | // Update the sync date in case of success, otherwise log an error. |
| 127 | - if ( ! $response->is_success() ) { |
|
| 127 | + if ( ! $response->is_success()) { |
|
| 128 | 128 | return false; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - $object->update_meta( self::JSONLD_HASH, $new_hash ); |
|
| 132 | - $object->update_meta( self::SYNCED_GMT, current_time( 'mysql', true ) ); |
|
| 131 | + $object->update_meta(self::JSONLD_HASH, $new_hash); |
|
| 132 | + $object->update_meta(self::SYNCED_GMT, current_time('mysql', true)); |
|
| 133 | 133 | |
| 134 | 134 | return true; |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - public function delete_one( $type, $object_id ) { |
|
| 138 | - $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 139 | - $uri = $object->get_meta( 'entity_url', true ); |
|
| 137 | + public function delete_one($type, $object_id) { |
|
| 138 | + $object = $this->sync_object_adapter_factory->create($type, $object_id); |
|
| 139 | + $uri = $object->get_meta('entity_url', true); |
|
| 140 | 140 | |
| 141 | 141 | // Entity URL isn't set, bail out. |
| 142 | - if ( empty( $uri ) ) { |
|
| 142 | + if (empty($uri)) { |
|
| 143 | 143 | return false; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | $response = $this->api_service->request( |
| 147 | - 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 147 | + 'DELETE', sprintf('/middleware/dataset?uri=%s', rawurlencode($uri)) ); |
|
| 148 | 148 | |
| 149 | 149 | // Update the sync date in case of success, otherwise log an error. |
| 150 | - if ( ! $response->is_success() ) { |
|
| 150 | + if ( ! $response->is_success()) { |
|
| 151 | 151 | return false; |
| 152 | 152 | } |
| 153 | 153 | |
@@ -184,29 +184,29 @@ discard block |
||
| 184 | 184 | $state = $this->info(); |
| 185 | 185 | |
| 186 | 186 | // Limit the query to the allowed post types. |
| 187 | - $post_type_in = implode( "','", array_map( 'esc_sql', \Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
| 187 | + $post_type_in = implode("','", array_map('esc_sql', \Wordlift_Entity_Service::valid_entity_post_types())); |
|
| 188 | 188 | |
| 189 | 189 | // Get the next post ID. |
| 190 | - return $wpdb->get_col( " |
|
| 190 | + return $wpdb->get_col(" |
|
| 191 | 191 | SELECT p.ID |
| 192 | 192 | FROM $wpdb->posts p |
| 193 | 193 | WHERE p.post_status = 'publish' |
| 194 | 194 | AND p.post_type IN ('$post_type_in') |
| 195 | 195 | ORDER BY p.ID |
| 196 | 196 | LIMIT {$state->index},{$this->batch_size} |
| 197 | - " ); |
|
| 197 | + "); |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | public function count() { |
| 201 | 201 | global $wpdb; |
| 202 | - $post_type_in = implode( "','", array_map( 'esc_sql', \Wordlift_Entity_Service::valid_entity_post_types() ) ); |
|
| 202 | + $post_type_in = implode("','", array_map('esc_sql', \Wordlift_Entity_Service::valid_entity_post_types())); |
|
| 203 | 203 | |
| 204 | - return $wpdb->get_var( " |
|
| 204 | + return $wpdb->get_var(" |
|
| 205 | 205 | SELECT COUNT(1) |
| 206 | 206 | FROM $wpdb->posts p |
| 207 | 207 | WHERE p.post_status = 'publish' |
| 208 | 208 | AND p.post_type IN ('$post_type_in') |
| 209 | - " ); |
|
| 209 | + "); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | public function info() { |
@@ -217,70 +217,70 @@ discard block |
||
| 217 | 217 | $post_ids, $clear = true |
| 218 | 218 | ) { |
| 219 | 219 | |
| 220 | - $this->log->debug( sprintf( 'Synchronizing post(s) %s...', implode( ', ', $post_ids ) ) ); |
|
| 220 | + $this->log->debug(sprintf('Synchronizing post(s) %s...', implode(', ', $post_ids))); |
|
| 221 | 221 | |
| 222 | 222 | // If we're starting the sync, try to clear the dataset. |
| 223 | - if ( $clear && 0 === $this->info()->index ) { |
|
| 224 | - $this->api_service->request( 'DELETE', '/middleware/dataset/delete' ); |
|
| 223 | + if ($clear && 0 === $this->info()->index) { |
|
| 224 | + $this->api_service->request('DELETE', '/middleware/dataset/delete'); |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | $that = $this; |
| 228 | - $request_body = array_filter( array_map( function ( $post_id ) use ( $that ) { |
|
| 228 | + $request_body = array_filter(array_map(function($post_id) use ($that) { |
|
| 229 | 229 | // Check if the post type is public. |
| 230 | - $post_type = get_post_type( $post_id ); |
|
| 231 | - $post_type_obj = get_post_type_object( $post_type ); |
|
| 232 | - if ( ! $post_type_obj->public ) { |
|
| 230 | + $post_type = get_post_type($post_id); |
|
| 231 | + $post_type_obj = get_post_type_object($post_type); |
|
| 232 | + if ( ! $post_type_obj->public) { |
|
| 233 | 233 | return false; |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | - $is_private = ( 'publish' !== get_post_status( $post_id ) ); |
|
| 237 | - $uri = get_post_meta( $post_id, 'entity_url', true ); |
|
| 238 | - $object_adapter = $that->sync_object_adapter_factory->create( Object_Type_Enum::POST, $post_id ); |
|
| 236 | + $is_private = ('publish' !== get_post_status($post_id)); |
|
| 237 | + $uri = get_post_meta($post_id, 'entity_url', true); |
|
| 238 | + $object_adapter = $that->sync_object_adapter_factory->create(Object_Type_Enum::POST, $post_id); |
|
| 239 | 239 | $jsonld = $object_adapter->get_jsonld_and_update_hash(); |
| 240 | - $jsonld_as_string = wp_json_encode( $jsonld ); |
|
| 240 | + $jsonld_as_string = wp_json_encode($jsonld); |
|
| 241 | 241 | |
| 242 | - $that->log->trace( "Posting JSON-LD:\n$jsonld_as_string" ); |
|
| 242 | + $that->log->trace("Posting JSON-LD:\n$jsonld_as_string"); |
|
| 243 | 243 | |
| 244 | 244 | return array( |
| 245 | 245 | 'uri' => $uri, |
| 246 | 246 | 'model' => $jsonld_as_string, |
| 247 | 247 | 'private' => $is_private |
| 248 | 248 | ); |
| 249 | - }, $post_ids ) ); |
|
| 249 | + }, $post_ids)); |
|
| 250 | 250 | |
| 251 | 251 | // There's no point in making a request if the request is empty. |
| 252 | - if ( empty( $request_body ) ) { |
|
| 252 | + if (empty($request_body)) { |
|
| 253 | 253 | return true; |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | // Make a request to the remote endpoint. |
| 257 | 257 | $state = $this->info(); |
| 258 | - $state_header_value = str_replace( "\n", '', wp_json_encode( $state ) ); |
|
| 258 | + $state_header_value = str_replace("\n", '', wp_json_encode($state)); |
|
| 259 | 259 | $response = $this->api_service->request( |
| 260 | 260 | 'POST', '/middleware/dataset/batch', |
| 261 | 261 | array( |
| 262 | 262 | 'Content-Type' => 'application/json', |
| 263 | 263 | 'X-Wordlift-Dataset-Sync-State-V1' => $state_header_value |
| 264 | 264 | ), |
| 265 | - wp_json_encode( $request_body ) ); |
|
| 265 | + wp_json_encode($request_body) ); |
|
| 266 | 266 | |
| 267 | - $this->log->debug( "Response received: " . ( $response->is_success() ? 'yes' : 'no' ) ); |
|
| 267 | + $this->log->debug("Response received: ".($response->is_success() ? 'yes' : 'no')); |
|
| 268 | 268 | |
| 269 | 269 | // Update the sync date in case of success, otherwise log an error. |
| 270 | - if ( $response->is_success() ) { |
|
| 270 | + if ($response->is_success()) { |
|
| 271 | 271 | |
| 272 | - foreach ( $post_ids as $post_id ) { |
|
| 273 | - update_post_meta( $post_id, '_wl_synced_gmt', current_time( 'mysql', true ) ); |
|
| 272 | + foreach ($post_ids as $post_id) { |
|
| 273 | + update_post_meta($post_id, '_wl_synced_gmt', current_time('mysql', true)); |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | - $this->log->debug( sprintf( 'Posts %s synchronized.', implode( ', ', $post_ids ) ) ); |
|
| 276 | + $this->log->debug(sprintf('Posts %s synchronized.', implode(', ', $post_ids))); |
|
| 277 | 277 | |
| 278 | 278 | return true; |
| 279 | 279 | } else { |
| 280 | 280 | // @@todo: should we put a limit retry here? |
| 281 | - $response_dump = var_export( $response, true ); |
|
| 281 | + $response_dump = var_export($response, true); |
|
| 282 | 282 | $this->log->error( |
| 283 | - sprintf( 'An error occurred while synchronizing the data for post IDs %s: %s', implode( ', ', $post_ids ), $response_dump ) ); |
|
| 283 | + sprintf('An error occurred while synchronizing the data for post IDs %s: %s', implode(', ', $post_ids), $response_dump) ); |
|
| 284 | 284 | |
| 285 | 285 | return false; |
| 286 | 286 | } |
@@ -292,12 +292,12 @@ discard block |
||
| 292 | 292 | * |
| 293 | 293 | * @todo Complete the delete item. |
| 294 | 294 | */ |
| 295 | - public function delete_item( $post_id ) { |
|
| 296 | - $uri = get_post_meta( $post_id, 'entity_url', true ); |
|
| 295 | + public function delete_item($post_id) { |
|
| 296 | + $uri = get_post_meta($post_id, 'entity_url', true); |
|
| 297 | 297 | // Make a request to the remote endpoint. |
| 298 | - $state_header_value = str_replace( wp_json_encode( $this->info() ), "\n", '' ); |
|
| 298 | + $state_header_value = str_replace(wp_json_encode($this->info()), "\n", ''); |
|
| 299 | 299 | $response = $this->api_service->request( |
| 300 | - 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 300 | + 'DELETE', '/middleware/dataset?uri='.rawurlencode($uri), |
|
| 301 | 301 | array( |
| 302 | 302 | 'Content-Type' => 'application/ld+json', |
| 303 | 303 | 'X-Wordlift-Dataset-Sync-State-V1' => $state_header_value |
@@ -6,24 +6,24 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Sync_Object_Adapter_Factory { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @param $type |
|
| 11 | - * @param $object_id |
|
| 12 | - * |
|
| 13 | - * @return Abstract_Sync_Object_Adapter |
|
| 14 | - * @throws \Exception |
|
| 15 | - */ |
|
| 16 | - function create( $type, $object_id ) { |
|
| 9 | + /** |
|
| 10 | + * @param $type |
|
| 11 | + * @param $object_id |
|
| 12 | + * |
|
| 13 | + * @return Abstract_Sync_Object_Adapter |
|
| 14 | + * @throws \Exception |
|
| 15 | + */ |
|
| 16 | + function create( $type, $object_id ) { |
|
| 17 | 17 | |
| 18 | - switch ( $type ) { |
|
| 19 | - case Object_Type_Enum::POST: |
|
| 20 | - return new Sync_Post_Adapter( $object_id ); |
|
| 21 | - case Object_Type_Enum::USER: |
|
| 22 | - return new Sync_User_Adapter( $object_id ); |
|
| 23 | - default: |
|
| 24 | - throw new \Exception( "Unsupported type $type." ); |
|
| 25 | - } |
|
| 18 | + switch ( $type ) { |
|
| 19 | + case Object_Type_Enum::POST: |
|
| 20 | + return new Sync_Post_Adapter( $object_id ); |
|
| 21 | + case Object_Type_Enum::USER: |
|
| 22 | + return new Sync_User_Adapter( $object_id ); |
|
| 23 | + default: |
|
| 24 | + throw new \Exception( "Unsupported type $type." ); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - } |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | 29 | } |
@@ -13,15 +13,15 @@ |
||
| 13 | 13 | * @return Abstract_Sync_Object_Adapter |
| 14 | 14 | * @throws \Exception |
| 15 | 15 | */ |
| 16 | - function create( $type, $object_id ) { |
|
| 16 | + function create($type, $object_id) { |
|
| 17 | 17 | |
| 18 | - switch ( $type ) { |
|
| 18 | + switch ($type) { |
|
| 19 | 19 | case Object_Type_Enum::POST: |
| 20 | - return new Sync_Post_Adapter( $object_id ); |
|
| 20 | + return new Sync_Post_Adapter($object_id); |
|
| 21 | 21 | case Object_Type_Enum::USER: |
| 22 | - return new Sync_User_Adapter( $object_id ); |
|
| 22 | + return new Sync_User_Adapter($object_id); |
|
| 23 | 23 | default: |
| 24 | - throw new \Exception( "Unsupported type $type." ); |
|
| 24 | + throw new \Exception("Unsupported type $type."); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | } |
@@ -6,23 +6,23 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Sync_User_Adapter extends Abstract_Sync_Object_Adapter { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Sync_User_Adapter constructor. |
|
| 11 | - * |
|
| 12 | - * @param int $user_id |
|
| 13 | - * |
|
| 14 | - * @throws \Exception |
|
| 15 | - */ |
|
| 16 | - function __construct( $user_id ) { |
|
| 17 | - parent::__construct( Object_Type_Enum::USER, $user_id ); |
|
| 18 | - } |
|
| 9 | + /** |
|
| 10 | + * Sync_User_Adapter constructor. |
|
| 11 | + * |
|
| 12 | + * @param int $user_id |
|
| 13 | + * |
|
| 14 | + * @throws \Exception |
|
| 15 | + */ |
|
| 16 | + function __construct( $user_id ) { |
|
| 17 | + parent::__construct( Object_Type_Enum::USER, $user_id ); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - function is_published() { |
|
| 21 | - return true; |
|
| 22 | - } |
|
| 20 | + function is_published() { |
|
| 21 | + return true; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - function is_public() { |
|
| 25 | - return true; |
|
| 26 | - } |
|
| 24 | + function is_public() { |
|
| 25 | + return true; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | 28 | } |
@@ -13,8 +13,8 @@ |
||
| 13 | 13 | * |
| 14 | 14 | * @throws \Exception |
| 15 | 15 | */ |
| 16 | - function __construct( $user_id ) { |
|
| 17 | - parent::__construct( Object_Type_Enum::USER, $user_id ); |
|
| 16 | + function __construct($user_id) { |
|
| 17 | + parent::__construct(Object_Type_Enum::USER, $user_id); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | function is_published() { |
@@ -10,28 +10,28 @@ |
||
| 10 | 10 | use Wordlift\Jsonld\Jsonld_Service; |
| 11 | 11 | |
| 12 | 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | - exit; |
|
| 13 | + exit; |
|
| 14 | 14 | } |
| 15 | 15 | // Register the Dataset JSON Endpoint. The `$api_service` variable must be defined in the calling file (wordlift.php). |
| 16 | 16 | if ( apply_filters( 'wl_feature__enable__dataset-ng', false ) ) { |
| 17 | - /* |
|
| 17 | + /* |
|
| 18 | 18 | * Add Composer Autoload with Mozart support. |
| 19 | 19 | * |
| 20 | 20 | * @since 3.27.6 |
| 21 | 21 | */ |
| 22 | 22 | // require dirname( dirname( dirname( __FILE__ ) ) ) . '/vendor/autoload.php'; |
| 23 | 23 | |
| 24 | - $sync_object_adapter_factory = new Sync_Object_Adapter_Factory(); |
|
| 25 | - $sync_service = new Sync_Service( $api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
| 26 | - new Sync_Post_Hooks( $sync_service ); |
|
| 27 | - new Sync_User_Hooks( $sync_service ); |
|
| 24 | + $sync_object_adapter_factory = new Sync_Object_Adapter_Factory(); |
|
| 25 | + $sync_service = new Sync_Service( $api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
| 26 | + new Sync_Post_Hooks( $sync_service ); |
|
| 27 | + new Sync_User_Hooks( $sync_service ); |
|
| 28 | 28 | // new Sync_Wpjson_Endpoint( $sync_service ); |
| 29 | 29 | // new Sync_Page(); |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @since 3.28.0 |
|
| 33 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1186 |
|
| 34 | - */ |
|
| 35 | - new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() ); |
|
| 31 | + /** |
|
| 32 | + * @since 3.28.0 |
|
| 33 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1186 |
|
| 34 | + */ |
|
| 35 | + new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() ); |
|
| 36 | 36 | |
| 37 | 37 | } |
@@ -9,11 +9,11 @@ discard block |
||
| 9 | 9 | use Wordlift\Dataset\Sync_Wpjson_Endpoint; |
| 10 | 10 | use Wordlift\Jsonld\Jsonld_Service; |
| 11 | 11 | |
| 12 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 12 | +if ( ! defined('ABSPATH')) { |
|
| 13 | 13 | exit; |
| 14 | 14 | } |
| 15 | 15 | // Register the Dataset JSON Endpoint. The `$api_service` variable must be defined in the calling file (wordlift.php). |
| 16 | -if ( apply_filters( 'wl_feature__enable__dataset-ng', false ) ) { |
|
| 16 | +if (apply_filters('wl_feature__enable__dataset-ng', false)) { |
|
| 17 | 17 | /* |
| 18 | 18 | * Add Composer Autoload with Mozart support. |
| 19 | 19 | * |
@@ -22,9 +22,9 @@ discard block |
||
| 22 | 22 | // require dirname( dirname( dirname( __FILE__ ) ) ) . '/vendor/autoload.php'; |
| 23 | 23 | |
| 24 | 24 | $sync_object_adapter_factory = new Sync_Object_Adapter_Factory(); |
| 25 | - $sync_service = new Sync_Service( $api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
| 26 | - new Sync_Post_Hooks( $sync_service ); |
|
| 27 | - new Sync_User_Hooks( $sync_service ); |
|
| 25 | + $sync_service = new Sync_Service($api_service, $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance()); |
|
| 26 | + new Sync_Post_Hooks($sync_service); |
|
| 27 | + new Sync_User_Hooks($sync_service); |
|
| 28 | 28 | // new Sync_Wpjson_Endpoint( $sync_service ); |
| 29 | 29 | // new Sync_Page(); |
| 30 | 30 | |
@@ -32,6 +32,6 @@ discard block |
||
| 32 | 32 | * @since 3.28.0 |
| 33 | 33 | * @see https://github.com/insideout10/wordlift-plugin/issues/1186 |
| 34 | 34 | */ |
| 35 | - new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() ); |
|
| 35 | + new Sync_Hooks_Entity_Relation(Wordlift_Entity_Service::get_instance()); |
|
| 36 | 36 | |
| 37 | 37 | } |
@@ -5,34 +5,34 @@ |
||
| 5 | 5 | use Wordlift\Object_Type_Enum; |
| 6 | 6 | |
| 7 | 7 | class Sync_Post_Adapter extends Abstract_Sync_Object_Adapter { |
| 8 | - /** |
|
| 9 | - * @var int |
|
| 10 | - */ |
|
| 11 | - private $post_id; |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * Sync_User_Adapter constructor. |
|
| 15 | - * |
|
| 16 | - * @param int $post_id |
|
| 17 | - * |
|
| 18 | - * @throws \Exception |
|
| 19 | - */ |
|
| 20 | - function __construct( $post_id ) { |
|
| 21 | - parent::__construct( Object_Type_Enum::POST, $post_id ); |
|
| 22 | - |
|
| 23 | - $this->post_id = $post_id; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - function is_published() { |
|
| 27 | - return ( 'publish' === get_post_status( $this->post_id ) ); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - function is_public() { |
|
| 31 | - // Check if the post type is public. |
|
| 32 | - $post_type = get_post_type( $this->post_id ); |
|
| 33 | - $post_type_obj = get_post_type_object( $post_type ); |
|
| 34 | - |
|
| 35 | - return $post_type_obj->public; |
|
| 36 | - } |
|
| 8 | + /** |
|
| 9 | + * @var int |
|
| 10 | + */ |
|
| 11 | + private $post_id; |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * Sync_User_Adapter constructor. |
|
| 15 | + * |
|
| 16 | + * @param int $post_id |
|
| 17 | + * |
|
| 18 | + * @throws \Exception |
|
| 19 | + */ |
|
| 20 | + function __construct( $post_id ) { |
|
| 21 | + parent::__construct( Object_Type_Enum::POST, $post_id ); |
|
| 22 | + |
|
| 23 | + $this->post_id = $post_id; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + function is_published() { |
|
| 27 | + return ( 'publish' === get_post_status( $this->post_id ) ); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + function is_public() { |
|
| 31 | + // Check if the post type is public. |
|
| 32 | + $post_type = get_post_type( $this->post_id ); |
|
| 33 | + $post_type_obj = get_post_type_object( $post_type ); |
|
| 34 | + |
|
| 35 | + return $post_type_obj->public; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | 38 | } |
@@ -17,20 +17,20 @@ |
||
| 17 | 17 | * |
| 18 | 18 | * @throws \Exception |
| 19 | 19 | */ |
| 20 | - function __construct( $post_id ) { |
|
| 21 | - parent::__construct( Object_Type_Enum::POST, $post_id ); |
|
| 20 | + function __construct($post_id) { |
|
| 21 | + parent::__construct(Object_Type_Enum::POST, $post_id); |
|
| 22 | 22 | |
| 23 | 23 | $this->post_id = $post_id; |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | function is_published() { |
| 27 | - return ( 'publish' === get_post_status( $this->post_id ) ); |
|
| 27 | + return ('publish' === get_post_status($this->post_id)); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | function is_public() { |
| 31 | 31 | // Check if the post type is public. |
| 32 | - $post_type = get_post_type( $this->post_id ); |
|
| 33 | - $post_type_obj = get_post_type_object( $post_type ); |
|
| 32 | + $post_type = get_post_type($this->post_id); |
|
| 33 | + $post_type_obj = get_post_type_object($post_type); |
|
| 34 | 34 | |
| 35 | 35 | return $post_type_obj->public; |
| 36 | 36 | } |
@@ -5,91 +5,91 @@ |
||
| 5 | 5 | use Wordlift\Object_Type_Enum; |
| 6 | 6 | |
| 7 | 7 | class Sync_User_Hooks { |
| 8 | - /** |
|
| 9 | - * @var \Wordlift_Log_Service |
|
| 10 | - */ |
|
| 11 | - private $log; |
|
| 8 | + /** |
|
| 9 | + * @var \Wordlift_Log_Service |
|
| 10 | + */ |
|
| 11 | + private $log; |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var Sync_Service |
|
| 15 | - */ |
|
| 16 | - private $sync_service; |
|
| 13 | + /** |
|
| 14 | + * @var Sync_Service |
|
| 15 | + */ |
|
| 16 | + private $sync_service; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Sync_User_Hooks constructor. |
|
| 20 | - * |
|
| 21 | - * @param Sync_Service $sync_service |
|
| 22 | - */ |
|
| 23 | - function __construct( Sync_Service $sync_service ) { |
|
| 18 | + /** |
|
| 19 | + * Sync_User_Hooks constructor. |
|
| 20 | + * |
|
| 21 | + * @param Sync_Service $sync_service |
|
| 22 | + */ |
|
| 23 | + function __construct( Sync_Service $sync_service ) { |
|
| 24 | 24 | |
| 25 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 25 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 26 | 26 | |
| 27 | - $this->sync_service = $sync_service; |
|
| 27 | + $this->sync_service = $sync_service; |
|
| 28 | 28 | |
| 29 | - $this->register_hooks(); |
|
| 29 | + $this->register_hooks(); |
|
| 30 | 30 | |
| 31 | - } |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - private function register_hooks() { |
|
| 34 | - /** |
|
| 35 | - * Register hooks for user and meta. |
|
| 36 | - */ |
|
| 37 | - add_action( 'user_register', array( $this, 'changed_user' ) ); |
|
| 38 | - add_action( 'profile_update', array( $this, 'changed_user' ) ); |
|
| 39 | - add_action( 'added_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 40 | - add_action( 'updated_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 41 | - add_action( 'deleted_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 42 | - add_action( 'delete_user', array( $this, 'delete_user' ) ); |
|
| 33 | + private function register_hooks() { |
|
| 34 | + /** |
|
| 35 | + * Register hooks for user and meta. |
|
| 36 | + */ |
|
| 37 | + add_action( 'user_register', array( $this, 'changed_user' ) ); |
|
| 38 | + add_action( 'profile_update', array( $this, 'changed_user' ) ); |
|
| 39 | + add_action( 'added_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 40 | + add_action( 'updated_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 41 | + add_action( 'deleted_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 42 | + add_action( 'delete_user', array( $this, 'delete_user' ) ); |
|
| 43 | 43 | |
| 44 | - } |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function changed_user( $user_id ) { |
|
| 46 | + public function changed_user( $user_id ) { |
|
| 47 | 47 | |
| 48 | - $this->sync( $user_id ); |
|
| 48 | + $this->sync( $user_id ); |
|
| 49 | 49 | |
| 50 | - } |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - public function changed_user_meta( $meta_id, $user_id, $meta_key ) { |
|
| 52 | + public function changed_user_meta( $meta_id, $user_id, $meta_key ) { |
|
| 53 | 53 | |
| 54 | - if ( in_array( $meta_key, |
|
| 55 | - apply_filters( 'wl_dataset__sync_user_hooks__ignored_meta_keys', |
|
| 56 | - apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 57 | - array( |
|
| 58 | - 'rich_editing', |
|
| 59 | - 'comment_shortcuts', |
|
| 60 | - 'admin_color', |
|
| 61 | - 'use_ssl', |
|
| 62 | - 'show_admin_bar_front', |
|
| 63 | - 'wptests_capabilities', |
|
| 64 | - 'wptests_user_level', |
|
| 65 | - 'dismissed_wp_pointers', |
|
| 66 | - 'entity_url', |
|
| 67 | - ) ) ) ) ) { |
|
| 68 | - return; |
|
| 69 | - } |
|
| 54 | + if ( in_array( $meta_key, |
|
| 55 | + apply_filters( 'wl_dataset__sync_user_hooks__ignored_meta_keys', |
|
| 56 | + apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 57 | + array( |
|
| 58 | + 'rich_editing', |
|
| 59 | + 'comment_shortcuts', |
|
| 60 | + 'admin_color', |
|
| 61 | + 'use_ssl', |
|
| 62 | + 'show_admin_bar_front', |
|
| 63 | + 'wptests_capabilities', |
|
| 64 | + 'wptests_user_level', |
|
| 65 | + 'dismissed_wp_pointers', |
|
| 66 | + 'entity_url', |
|
| 67 | + ) ) ) ) ) { |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - $this->sync( $user_id ); |
|
| 71 | + $this->sync( $user_id ); |
|
| 72 | 72 | |
| 73 | - } |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - private function sync( $user_id ) { |
|
| 75 | + private function sync( $user_id ) { |
|
| 76 | 76 | |
| 77 | - try { |
|
| 78 | - $this->sync_service->sync_one( Object_Type_Enum::USER, (int) $user_id ); |
|
| 79 | - } catch ( \Exception $e ) { |
|
| 80 | - $this->log->error( "An error occurred while trying to sync user $user_id: " . $e->getMessage(), $e ); |
|
| 81 | - } |
|
| 77 | + try { |
|
| 78 | + $this->sync_service->sync_one( Object_Type_Enum::USER, (int) $user_id ); |
|
| 79 | + } catch ( \Exception $e ) { |
|
| 80 | + $this->log->error( "An error occurred while trying to sync user $user_id: " . $e->getMessage(), $e ); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - } |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - public function delete_user( $user_id ) { |
|
| 85 | + public function delete_user( $user_id ) { |
|
| 86 | 86 | |
| 87 | - try { |
|
| 88 | - $this->sync_service->delete_one( Object_Type_Enum::USER, $user_id ); |
|
| 89 | - } catch ( \Exception $e ) { |
|
| 90 | - $this->log->error( "An error occurred while trying to delete user $user_id: " . $e->getMessage(), $e ); |
|
| 91 | - } |
|
| 87 | + try { |
|
| 88 | + $this->sync_service->delete_one( Object_Type_Enum::USER, $user_id ); |
|
| 89 | + } catch ( \Exception $e ) { |
|
| 90 | + $this->log->error( "An error occurred while trying to delete user $user_id: " . $e->getMessage(), $e ); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - } |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | 95 | } |
| 96 | 96 | \ No newline at end of file |
@@ -20,9 +20,9 @@ discard block |
||
| 20 | 20 | * |
| 21 | 21 | * @param Sync_Service $sync_service |
| 22 | 22 | */ |
| 23 | - function __construct( Sync_Service $sync_service ) { |
|
| 23 | + function __construct(Sync_Service $sync_service) { |
|
| 24 | 24 | |
| 25 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 25 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 26 | 26 | |
| 27 | 27 | $this->sync_service = $sync_service; |
| 28 | 28 | |
@@ -34,26 +34,26 @@ discard block |
||
| 34 | 34 | /** |
| 35 | 35 | * Register hooks for user and meta. |
| 36 | 36 | */ |
| 37 | - add_action( 'user_register', array( $this, 'changed_user' ) ); |
|
| 38 | - add_action( 'profile_update', array( $this, 'changed_user' ) ); |
|
| 39 | - add_action( 'added_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 40 | - add_action( 'updated_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 41 | - add_action( 'deleted_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 42 | - add_action( 'delete_user', array( $this, 'delete_user' ) ); |
|
| 37 | + add_action('user_register', array($this, 'changed_user')); |
|
| 38 | + add_action('profile_update', array($this, 'changed_user')); |
|
| 39 | + add_action('added_user_meta', array($this, 'changed_user_meta'), 10, 3); |
|
| 40 | + add_action('updated_user_meta', array($this, 'changed_user_meta'), 10, 3); |
|
| 41 | + add_action('deleted_user_meta', array($this, 'changed_user_meta'), 10, 3); |
|
| 42 | + add_action('delete_user', array($this, 'delete_user')); |
|
| 43 | 43 | |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - public function changed_user( $user_id ) { |
|
| 46 | + public function changed_user($user_id) { |
|
| 47 | 47 | |
| 48 | - $this->sync( $user_id ); |
|
| 48 | + $this->sync($user_id); |
|
| 49 | 49 | |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - public function changed_user_meta( $meta_id, $user_id, $meta_key ) { |
|
| 52 | + public function changed_user_meta($meta_id, $user_id, $meta_key) { |
|
| 53 | 53 | |
| 54 | - if ( in_array( $meta_key, |
|
| 55 | - apply_filters( 'wl_dataset__sync_user_hooks__ignored_meta_keys', |
|
| 56 | - apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 54 | + if (in_array($meta_key, |
|
| 55 | + apply_filters('wl_dataset__sync_user_hooks__ignored_meta_keys', |
|
| 56 | + apply_filters('wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 57 | 57 | array( |
| 58 | 58 | 'rich_editing', |
| 59 | 59 | 'comment_shortcuts', |
@@ -64,30 +64,30 @@ discard block |
||
| 64 | 64 | 'wptests_user_level', |
| 65 | 65 | 'dismissed_wp_pointers', |
| 66 | 66 | 'entity_url', |
| 67 | - ) ) ) ) ) { |
|
| 67 | + ))))) { |
|
| 68 | 68 | return; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - $this->sync( $user_id ); |
|
| 71 | + $this->sync($user_id); |
|
| 72 | 72 | |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - private function sync( $user_id ) { |
|
| 75 | + private function sync($user_id) { |
|
| 76 | 76 | |
| 77 | 77 | try { |
| 78 | - $this->sync_service->sync_one( Object_Type_Enum::USER, (int) $user_id ); |
|
| 79 | - } catch ( \Exception $e ) { |
|
| 80 | - $this->log->error( "An error occurred while trying to sync user $user_id: " . $e->getMessage(), $e ); |
|
| 78 | + $this->sync_service->sync_one(Object_Type_Enum::USER, (int) $user_id); |
|
| 79 | + } catch (\Exception $e) { |
|
| 80 | + $this->log->error("An error occurred while trying to sync user $user_id: ".$e->getMessage(), $e); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - public function delete_user( $user_id ) { |
|
| 85 | + public function delete_user($user_id) { |
|
| 86 | 86 | |
| 87 | 87 | try { |
| 88 | - $this->sync_service->delete_one( Object_Type_Enum::USER, $user_id ); |
|
| 89 | - } catch ( \Exception $e ) { |
|
| 90 | - $this->log->error( "An error occurred while trying to delete user $user_id: " . $e->getMessage(), $e ); |
|
| 88 | + $this->sync_service->delete_one(Object_Type_Enum::USER, $user_id); |
|
| 89 | + } catch (\Exception $e) { |
|
| 90 | + $this->log->error("An error occurred while trying to delete user $user_id: ".$e->getMessage(), $e); |
|
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | } |
@@ -5,84 +5,84 @@ |
||
| 5 | 5 | use Wordlift\Object_Type_Enum; |
| 6 | 6 | |
| 7 | 7 | class Sync_Post_Hooks { |
| 8 | - /** |
|
| 9 | - * @var \Wordlift_Log_Service |
|
| 10 | - */ |
|
| 11 | - private $log; |
|
| 8 | + /** |
|
| 9 | + * @var \Wordlift_Log_Service |
|
| 10 | + */ |
|
| 11 | + private $log; |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var Sync_Service |
|
| 15 | - */ |
|
| 16 | - private $sync_service; |
|
| 13 | + /** |
|
| 14 | + * @var Sync_Service |
|
| 15 | + */ |
|
| 16 | + private $sync_service; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Sync_Post_Hooks constructor. |
|
| 20 | - * |
|
| 21 | - * @param Sync_Service $sync_service |
|
| 22 | - */ |
|
| 23 | - function __construct( $sync_service ) { |
|
| 18 | + /** |
|
| 19 | + * Sync_Post_Hooks constructor. |
|
| 20 | + * |
|
| 21 | + * @param Sync_Service $sync_service |
|
| 22 | + */ |
|
| 23 | + function __construct( $sync_service ) { |
|
| 24 | 24 | |
| 25 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 25 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 26 | 26 | |
| 27 | - $this->sync_service = $sync_service; |
|
| 27 | + $this->sync_service = $sync_service; |
|
| 28 | 28 | |
| 29 | - $this->register_hooks(); |
|
| 29 | + $this->register_hooks(); |
|
| 30 | 30 | |
| 31 | - } |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - private function register_hooks() { |
|
| 34 | - /** |
|
| 35 | - * Register hooks for post and meta. |
|
| 36 | - */ |
|
| 37 | - add_action( 'save_post', array( $this, 'save_post' ) ); |
|
| 38 | - add_action( 'added_post_meta', array( $this, 'changed_post_meta' ), 10, 4 ); |
|
| 39 | - add_action( 'updated_post_meta', array( $this, 'changed_post_meta' ), 10, 4 ); |
|
| 40 | - add_action( 'deleted_post_meta', array( $this, 'changed_post_meta' ), 10, 4 ); |
|
| 41 | - add_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
| 33 | + private function register_hooks() { |
|
| 34 | + /** |
|
| 35 | + * Register hooks for post and meta. |
|
| 36 | + */ |
|
| 37 | + add_action( 'save_post', array( $this, 'save_post' ) ); |
|
| 38 | + add_action( 'added_post_meta', array( $this, 'changed_post_meta' ), 10, 4 ); |
|
| 39 | + add_action( 'updated_post_meta', array( $this, 'changed_post_meta' ), 10, 4 ); |
|
| 40 | + add_action( 'deleted_post_meta', array( $this, 'changed_post_meta' ), 10, 4 ); |
|
| 41 | + add_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
| 42 | 42 | |
| 43 | - } |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - public function save_post( $post_id ) { |
|
| 45 | + public function save_post( $post_id ) { |
|
| 46 | 46 | |
| 47 | - $this->sync( $post_id ); |
|
| 47 | + $this->sync( $post_id ); |
|
| 48 | 48 | |
| 49 | - } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - public function changed_post_meta( $meta_id, $post_id, $meta_key, $_meta_value ) { |
|
| 51 | + public function changed_post_meta( $meta_id, $post_id, $meta_key, $_meta_value ) { |
|
| 52 | 52 | |
| 53 | - if ( in_array( $meta_key, |
|
| 54 | - apply_filters( 'wl_dataset__sync_post_hooks__ignored_meta_keys', |
|
| 55 | - apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 56 | - array( |
|
| 57 | - '_pingme', |
|
| 58 | - '_encloseme', |
|
| 59 | - 'entity_url', |
|
| 60 | - ) ) ) ) ) { |
|
| 61 | - return; |
|
| 62 | - } |
|
| 53 | + if ( in_array( $meta_key, |
|
| 54 | + apply_filters( 'wl_dataset__sync_post_hooks__ignored_meta_keys', |
|
| 55 | + apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 56 | + array( |
|
| 57 | + '_pingme', |
|
| 58 | + '_encloseme', |
|
| 59 | + 'entity_url', |
|
| 60 | + ) ) ) ) ) { |
|
| 61 | + return; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - $this->sync( $post_id ); |
|
| 64 | + $this->sync( $post_id ); |
|
| 65 | 65 | |
| 66 | - } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - private function sync( $post_id ) { |
|
| 68 | + private function sync( $post_id ) { |
|
| 69 | 69 | |
| 70 | - try { |
|
| 71 | - $this->sync_service->sync_one( Object_Type_Enum::POST, $post_id ); |
|
| 72 | - } catch ( \Exception $e ) { |
|
| 73 | - $this->log->error( "An error occurred while trying to sync post $post_id: " . $e->getMessage(), $e ); |
|
| 74 | - } |
|
| 70 | + try { |
|
| 71 | + $this->sync_service->sync_one( Object_Type_Enum::POST, $post_id ); |
|
| 72 | + } catch ( \Exception $e ) { |
|
| 73 | + $this->log->error( "An error occurred while trying to sync post $post_id: " . $e->getMessage(), $e ); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - } |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - public function delete_post( $post_id ) { |
|
| 78 | + public function delete_post( $post_id ) { |
|
| 79 | 79 | |
| 80 | - try { |
|
| 81 | - $this->sync_service->delete_one( Object_Type_Enum::POST, $post_id ); |
|
| 82 | - } catch ( \Exception $e ) { |
|
| 83 | - $this->log->error( "An error occurred while trying to delete post $post_id: " . $e->getMessage(), $e ); |
|
| 84 | - } |
|
| 80 | + try { |
|
| 81 | + $this->sync_service->delete_one( Object_Type_Enum::POST, $post_id ); |
|
| 82 | + } catch ( \Exception $e ) { |
|
| 83 | + $this->log->error( "An error occurred while trying to delete post $post_id: " . $e->getMessage(), $e ); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - } |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | 88 | } |
| 89 | 89 | \ No newline at end of file |
@@ -20,9 +20,9 @@ discard block |
||
| 20 | 20 | * |
| 21 | 21 | * @param Sync_Service $sync_service |
| 22 | 22 | */ |
| 23 | - function __construct( $sync_service ) { |
|
| 23 | + function __construct($sync_service) { |
|
| 24 | 24 | |
| 25 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 25 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 26 | 26 | |
| 27 | 27 | $this->sync_service = $sync_service; |
| 28 | 28 | |
@@ -34,53 +34,53 @@ discard block |
||
| 34 | 34 | /** |
| 35 | 35 | * Register hooks for post and meta. |
| 36 | 36 | */ |
| 37 | - add_action( 'save_post', array( $this, 'save_post' ) ); |
|
| 38 | - add_action( 'added_post_meta', array( $this, 'changed_post_meta' ), 10, 4 ); |
|
| 39 | - add_action( 'updated_post_meta', array( $this, 'changed_post_meta' ), 10, 4 ); |
|
| 40 | - add_action( 'deleted_post_meta', array( $this, 'changed_post_meta' ), 10, 4 ); |
|
| 41 | - add_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
| 37 | + add_action('save_post', array($this, 'save_post')); |
|
| 38 | + add_action('added_post_meta', array($this, 'changed_post_meta'), 10, 4); |
|
| 39 | + add_action('updated_post_meta', array($this, 'changed_post_meta'), 10, 4); |
|
| 40 | + add_action('deleted_post_meta', array($this, 'changed_post_meta'), 10, 4); |
|
| 41 | + add_action('delete_post', array($this, 'delete_post')); |
|
| 42 | 42 | |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - public function save_post( $post_id ) { |
|
| 45 | + public function save_post($post_id) { |
|
| 46 | 46 | |
| 47 | - $this->sync( $post_id ); |
|
| 47 | + $this->sync($post_id); |
|
| 48 | 48 | |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - public function changed_post_meta( $meta_id, $post_id, $meta_key, $_meta_value ) { |
|
| 51 | + public function changed_post_meta($meta_id, $post_id, $meta_key, $_meta_value) { |
|
| 52 | 52 | |
| 53 | - if ( in_array( $meta_key, |
|
| 54 | - apply_filters( 'wl_dataset__sync_post_hooks__ignored_meta_keys', |
|
| 55 | - apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 53 | + if (in_array($meta_key, |
|
| 54 | + apply_filters('wl_dataset__sync_post_hooks__ignored_meta_keys', |
|
| 55 | + apply_filters('wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 56 | 56 | array( |
| 57 | 57 | '_pingme', |
| 58 | 58 | '_encloseme', |
| 59 | 59 | 'entity_url', |
| 60 | - ) ) ) ) ) { |
|
| 60 | + ))))) { |
|
| 61 | 61 | return; |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - $this->sync( $post_id ); |
|
| 64 | + $this->sync($post_id); |
|
| 65 | 65 | |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - private function sync( $post_id ) { |
|
| 68 | + private function sync($post_id) { |
|
| 69 | 69 | |
| 70 | 70 | try { |
| 71 | - $this->sync_service->sync_one( Object_Type_Enum::POST, $post_id ); |
|
| 72 | - } catch ( \Exception $e ) { |
|
| 73 | - $this->log->error( "An error occurred while trying to sync post $post_id: " . $e->getMessage(), $e ); |
|
| 71 | + $this->sync_service->sync_one(Object_Type_Enum::POST, $post_id); |
|
| 72 | + } catch (\Exception $e) { |
|
| 73 | + $this->log->error("An error occurred while trying to sync post $post_id: ".$e->getMessage(), $e); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - public function delete_post( $post_id ) { |
|
| 78 | + public function delete_post($post_id) { |
|
| 79 | 79 | |
| 80 | 80 | try { |
| 81 | - $this->sync_service->delete_one( Object_Type_Enum::POST, $post_id ); |
|
| 82 | - } catch ( \Exception $e ) { |
|
| 83 | - $this->log->error( "An error occurred while trying to delete post $post_id: " . $e->getMessage(), $e ); |
|
| 81 | + $this->sync_service->delete_one(Object_Type_Enum::POST, $post_id); |
|
| 82 | + } catch (\Exception $e) { |
|
| 83 | + $this->log->error("An error occurred while trying to delete post $post_id: ".$e->getMessage(), $e); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | } |