@@ -14,19 +14,19 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | function wl_linked_data_save_post( $post_id ) { |
| 16 | 16 | |
| 17 | - // If it's not numeric exit from here. |
|
| 18 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 19 | - return; |
|
| 20 | - } |
|
| 17 | + // If it's not numeric exit from here. |
|
| 18 | + if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 19 | + return; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - // unhook this function so it doesn't loop infinitely |
|
| 23 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 22 | + // unhook this function so it doesn't loop infinitely |
|
| 23 | + remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 24 | 24 | |
| 25 | - // raise the *wl_linked_data_save_post* event. |
|
| 26 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 25 | + // raise the *wl_linked_data_save_post* event. |
|
| 26 | + do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 27 | 27 | |
| 28 | - // re-hook this function |
|
| 29 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 28 | + // re-hook this function |
|
| 29 | + add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | add_action( 'save_post', 'wl_linked_data_save_post' ); |
@@ -40,143 +40,143 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | function wl_linked_data_save_post_and_related_entities( $post_id ) { |
| 42 | 42 | |
| 43 | - // Ignore auto-saves |
|
| 44 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 45 | - return; |
|
| 46 | - } |
|
| 43 | + // Ignore auto-saves |
|
| 44 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 45 | + return; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - // get the current post. |
|
| 49 | - $post = get_post( $post_id ); |
|
| 48 | + // get the current post. |
|
| 49 | + $post = get_post( $post_id ); |
|
| 50 | 50 | |
| 51 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 51 | + remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 52 | 52 | |
| 53 | - // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 53 | + // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
| 54 | 54 | |
| 55 | - // Store mapping between tmp new entities uris and real new entities uri |
|
| 56 | - $entities_uri_mapping = array(); |
|
| 55 | + // Store mapping between tmp new entities uris and real new entities uri |
|
| 56 | + $entities_uri_mapping = array(); |
|
| 57 | 57 | |
| 58 | - // Save the entities coming with POST data. |
|
| 59 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 58 | + // Save the entities coming with POST data. |
|
| 59 | + if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 60 | 60 | |
| 61 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 62 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 63 | - wl_write_log( "]" ); |
|
| 64 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 65 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 66 | - wl_write_log( "]" ); |
|
| 61 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 62 | + wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 63 | + wl_write_log( "]" ); |
|
| 64 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 65 | + wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 66 | + wl_write_log( "]" ); |
|
| 67 | 67 | |
| 68 | - $entities_via_post = $_POST['wl_entities']; |
|
| 69 | - $boxes_via_post = $_POST['wl_boxes']; |
|
| 68 | + $entities_via_post = $_POST['wl_entities']; |
|
| 69 | + $boxes_via_post = $_POST['wl_boxes']; |
|
| 70 | 70 | |
| 71 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 71 | + foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 72 | 72 | |
| 73 | - // Only if the current entity is created from scratch let's avoid to |
|
| 74 | - // create more than one entity with same label & entity type. |
|
| 75 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 76 | - $entity['main_type'] : null; |
|
| 77 | - |
|
| 78 | - // Look if current entity uri matches an internal existing entity, meaning: |
|
| 79 | - // 1. when $entity_uri is an internal uri |
|
| 80 | - // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 81 | - $ie = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $entity_uri ); |
|
| 73 | + // Only if the current entity is created from scratch let's avoid to |
|
| 74 | + // create more than one entity with same label & entity type. |
|
| 75 | + $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 76 | + $entity['main_type'] : null; |
|
| 77 | + |
|
| 78 | + // Look if current entity uri matches an internal existing entity, meaning: |
|
| 79 | + // 1. when $entity_uri is an internal uri |
|
| 80 | + // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
| 81 | + $ie = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $entity_uri ); |
|
| 82 | 82 | |
| 83 | - // Detect the uri depending if is an existing or a new entity |
|
| 84 | - $uri = ( null === $ie ) ? |
|
| 85 | - Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 86 | - $entity['label'], |
|
| 87 | - Wordlift_Entity_Service::TYPE_NAME, |
|
| 88 | - $entity_type |
|
| 89 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 83 | + // Detect the uri depending if is an existing or a new entity |
|
| 84 | + $uri = ( null === $ie ) ? |
|
| 85 | + Wordlift_Uri_Service::get_instance()->build_uri( |
|
| 86 | + $entity['label'], |
|
| 87 | + Wordlift_Entity_Service::TYPE_NAME, |
|
| 88 | + $entity_type |
|
| 89 | + ) : wl_get_entity_uri( $ie->ID ); |
|
| 90 | 90 | |
| 91 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 92 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 91 | + wl_write_log( "Map $entity_uri on $uri" ); |
|
| 92 | + $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 93 | 93 | |
| 94 | - // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 95 | - // These uris need to be rewritten here and replaced in the content |
|
| 96 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 97 | - // Override the entity obj |
|
| 98 | - $entity['uri'] = $uri; |
|
| 99 | - } |
|
| 94 | + // Local entities have a tmp uri with 'local-entity-' prefix |
|
| 95 | + // These uris need to be rewritten here and replaced in the content |
|
| 96 | + if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 97 | + // Override the entity obj |
|
| 98 | + $entity['uri'] = $uri; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - // Update entity data with related post |
|
| 102 | - $entity['related_post_id'] = $post_id; |
|
| 101 | + // Update entity data with related post |
|
| 102 | + $entity['related_post_id'] = $post_id; |
|
| 103 | 103 | |
| 104 | - // Save the entity if is a new entity |
|
| 105 | - if ( null === $ie ) { |
|
| 106 | - wl_save_entity( $entity ); |
|
| 107 | - } |
|
| 104 | + // Save the entity if is a new entity |
|
| 105 | + if ( null === $ie ) { |
|
| 106 | + wl_save_entity( $entity ); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - } |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - } |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - // Replace tmp uris in content post if needed |
|
| 114 | - $updated_post_content = $post->post_content; |
|
| 115 | - // Save each entity and store the post id. |
|
| 116 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 117 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 118 | - } |
|
| 113 | + // Replace tmp uris in content post if needed |
|
| 114 | + $updated_post_content = $post->post_content; |
|
| 115 | + // Save each entity and store the post id. |
|
| 116 | + foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 117 | + $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - // Update the post content |
|
| 121 | - wp_update_post( array( |
|
| 122 | - 'ID' => $post->ID, |
|
| 123 | - 'post_content' => $updated_post_content, |
|
| 124 | - ) ); |
|
| 120 | + // Update the post content |
|
| 121 | + wp_update_post( array( |
|
| 122 | + 'ID' => $post->ID, |
|
| 123 | + 'post_content' => $updated_post_content, |
|
| 124 | + ) ); |
|
| 125 | 125 | |
| 126 | - // Extract related/referenced entities from text. |
|
| 127 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 126 | + // Extract related/referenced entities from text. |
|
| 127 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 128 | 128 | |
| 129 | - // Reset previously saved instances |
|
| 130 | - wl_core_delete_relation_instances( $post_id ); |
|
| 129 | + // Reset previously saved instances |
|
| 130 | + wl_core_delete_relation_instances( $post_id ); |
|
| 131 | 131 | |
| 132 | - // Save relation instances |
|
| 133 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 132 | + // Save relation instances |
|
| 133 | + foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 134 | 134 | |
| 135 | - wl_core_add_relation_instance( |
|
| 136 | - $post_id, |
|
| 137 | - Wordlift_Entity_Service::get_instance()->get_classification_scope_for( $referenced_entity_id ), |
|
| 138 | - $referenced_entity_id |
|
| 139 | - ); |
|
| 135 | + wl_core_add_relation_instance( |
|
| 136 | + $post_id, |
|
| 137 | + Wordlift_Entity_Service::get_instance()->get_classification_scope_for( $referenced_entity_id ), |
|
| 138 | + $referenced_entity_id |
|
| 139 | + ); |
|
| 140 | 140 | |
| 141 | - } |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 144 | - // Save post metadata if available |
|
| 145 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 146 | - $_POST['wl_metadata'] : array(); |
|
| 143 | + if ( isset( $_POST['wl_entities'] ) ) { |
|
| 144 | + // Save post metadata if available |
|
| 145 | + $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 146 | + $_POST['wl_metadata'] : array(); |
|
| 147 | 147 | |
| 148 | - $fields = array( |
|
| 149 | - Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 150 | - Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 151 | - ); |
|
| 152 | - |
|
| 153 | - // Unlink topic taxonomy terms |
|
| 154 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 155 | - |
|
| 156 | - foreach ( $fields as $field ) { |
|
| 157 | - |
|
| 158 | - // Delete current values |
|
| 159 | - delete_post_meta( $post->ID, $field ); |
|
| 160 | - // Retrieve the entity uri |
|
| 161 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 162 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 163 | - |
|
| 164 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 165 | - |
|
| 166 | - if ( $entity ) { |
|
| 167 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 168 | - // Set also the topic taxonomy |
|
| 169 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 170 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // Push the post to Redlink. |
|
| 177 | - wl_linked_data_push_to_redlink( $post->ID ); |
|
| 178 | - |
|
| 179 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 148 | + $fields = array( |
|
| 149 | + Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
| 150 | + Wordlift_Schema_Service::FIELD_TOPIC, |
|
| 151 | + ); |
|
| 152 | + |
|
| 153 | + // Unlink topic taxonomy terms |
|
| 154 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 155 | + |
|
| 156 | + foreach ( $fields as $field ) { |
|
| 157 | + |
|
| 158 | + // Delete current values |
|
| 159 | + delete_post_meta( $post->ID, $field ); |
|
| 160 | + // Retrieve the entity uri |
|
| 161 | + $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 162 | + stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 163 | + |
|
| 164 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 165 | + |
|
| 166 | + if ( $entity ) { |
|
| 167 | + add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 168 | + // Set also the topic taxonomy |
|
| 169 | + if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 170 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // Push the post to Redlink. |
|
| 177 | + wl_linked_data_push_to_redlink( $post->ID ); |
|
| 178 | + |
|
| 179 | + add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
@@ -186,20 +186,20 @@ discard block |
||
| 186 | 186 | */ |
| 187 | 187 | function wordlift_save_post_add_default_schema_type( $entity_id ) { |
| 188 | 188 | |
| 189 | - $entity = get_post( $entity_id ); |
|
| 189 | + $entity = get_post( $entity_id ); |
|
| 190 | 190 | |
| 191 | - // Avoid doing anything if post is autosave or a revision. |
|
| 191 | + // Avoid doing anything if post is autosave or a revision. |
|
| 192 | 192 | |
| 193 | - if ( wp_is_post_autosave( $entity ) || wp_is_post_revision( $entity ) ) { |
|
| 194 | - return; |
|
| 195 | - } |
|
| 193 | + if ( wp_is_post_autosave( $entity ) || wp_is_post_revision( $entity ) ) { |
|
| 194 | + return; |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - $entity_type = wl_schema_get_types( $entity_id ); |
|
| 197 | + $entity_type = wl_schema_get_types( $entity_id ); |
|
| 198 | 198 | |
| 199 | - // Assign type 'Thing' if we are dealing with an entity without type |
|
| 200 | - if ( $entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null( $entity_type ) ) { |
|
| 201 | - wl_schema_set_types( $entity_id, 'Thing' ); |
|
| 202 | - } |
|
| 199 | + // Assign type 'Thing' if we are dealing with an entity without type |
|
| 200 | + if ( $entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null( $entity_type ) ) { |
|
| 201 | + wl_schema_set_types( $entity_id, 'Thing' ); |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | 204 | } |
| 205 | 205 | |
@@ -225,178 +225,178 @@ discard block |
||
| 225 | 225 | */ |
| 226 | 226 | function wl_save_entity( $entity_data ) { |
| 227 | 227 | |
| 228 | - $uri = $entity_data['uri']; |
|
| 229 | - $label = $entity_data['label']; |
|
| 230 | - $type_uri = $entity_data['main_type']; |
|
| 231 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 232 | - $description = $entity_data['description']; |
|
| 233 | - $images = isset( $entity_data['image'] ) ? wl_force_to_array( $entity_data['image'] ) : array(); |
|
| 234 | - $same_as = isset( $entity_data['sameas'] ) ? wl_force_to_array( $entity_data['sameas'] ) : array(); |
|
| 235 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 236 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 237 | - |
|
| 238 | - // Check whether an entity already exists with the provided URI. |
|
| 239 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 240 | - return $post; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - // Prepare properties of the new entity. |
|
| 244 | - $params = array( |
|
| 245 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 246 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 247 | - 'post_title' => $label, |
|
| 248 | - 'post_content' => $description, |
|
| 249 | - 'post_excerpt' => '', |
|
| 250 | - // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 251 | - // entity with a post_name already set, since this call is made only for |
|
| 252 | - // new entities. |
|
| 253 | - // |
|
| 254 | - // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 255 | - 'post_name' => sanitize_title( $label ), |
|
| 256 | - ); |
|
| 257 | - |
|
| 258 | - // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 259 | - // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 260 | - // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 261 | - // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 262 | - // is created when saving a post. |
|
| 263 | - global $wpseo_metabox, $seo_ultimate; |
|
| 264 | - if ( isset( $wpseo_metabox ) ) { |
|
| 265 | - remove_action( 'wp_insert_post', array( |
|
| 266 | - $wpseo_metabox, |
|
| 267 | - 'save_postdata', |
|
| 268 | - ) ); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - if ( isset( $seo_ultimate ) ) { |
|
| 272 | - remove_action( 'save_post', array( |
|
| 273 | - $seo_ultimate, |
|
| 274 | - 'save_postmeta_box', |
|
| 275 | - ) ); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 279 | - // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 280 | - // to the save_post and restore them after we saved the entity. |
|
| 281 | - // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 282 | - // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 283 | - // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 284 | - global $wp_filter; |
|
| 285 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 286 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 287 | - |
|
| 288 | - // create or update the post. |
|
| 289 | - $post_id = wp_insert_post( $params, true ); |
|
| 290 | - |
|
| 291 | - // Restore all the existing filters. |
|
| 292 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 293 | - |
|
| 294 | - // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 295 | - if ( isset( $wpseo_metabox ) ) { |
|
| 296 | - add_action( 'wp_insert_post', array( |
|
| 297 | - $wpseo_metabox, |
|
| 298 | - 'save_postdata', |
|
| 299 | - ) ); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 303 | - if ( isset( $seo_ultimate ) ) { |
|
| 304 | - add_action( 'save_post', array( |
|
| 305 | - $seo_ultimate, |
|
| 306 | - 'save_postmeta_box', |
|
| 307 | - ), 10, 2 ); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - // TODO: handle errors. |
|
| 311 | - if ( is_wp_error( $post_id ) ) { |
|
| 312 | - wl_write_log( ': error occurred' ); |
|
| 313 | - |
|
| 314 | - // inform an error occurred. |
|
| 315 | - return null; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 319 | - |
|
| 320 | - // Save the entity types. |
|
| 321 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 322 | - |
|
| 323 | - // Get a dataset URI for the entity. |
|
| 324 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 325 | - |
|
| 326 | - // Save the entity URI. |
|
| 327 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 328 | - |
|
| 329 | - // Add the uri to the sameAs data if it's not a local URI. |
|
| 330 | - if ( $wl_uri !== $uri ) { |
|
| 331 | - array_push( $same_as, $uri ); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 335 | - |
|
| 336 | - // Save the sameAs data for the entity. |
|
| 337 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 338 | - |
|
| 339 | - // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 340 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 341 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - // Call hooks. |
|
| 345 | - do_action( 'wl_save_entity', $post_id ); |
|
| 346 | - |
|
| 347 | - foreach ( $images as $image_remote_url ) { |
|
| 348 | - |
|
| 349 | - // Check if image is already present in local DB |
|
| 350 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 351 | - // Do nothing. |
|
| 352 | - continue; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - // Check if there is an existing attachment for this post ID and source URL. |
|
| 356 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 357 | - |
|
| 358 | - // Skip if an existing image is found. |
|
| 359 | - if ( null !== $existing_image ) { |
|
| 360 | - continue; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // Save the image and get the local path. |
|
| 364 | - $image = wl_save_image( $image_remote_url ); |
|
| 365 | - |
|
| 366 | - // Get the local URL. |
|
| 367 | - $filename = $image['path']; |
|
| 368 | - $url = $image['url']; |
|
| 369 | - $content_type = $image['content_type']; |
|
| 370 | - |
|
| 371 | - $attachment = array( |
|
| 372 | - 'guid' => $url, |
|
| 373 | - // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 374 | - 'post_title' => $label, |
|
| 375 | - // Set the title to the post title. |
|
| 376 | - 'post_content' => '', |
|
| 377 | - 'post_status' => 'inherit', |
|
| 378 | - 'post_mime_type' => $content_type, |
|
| 379 | - ); |
|
| 380 | - |
|
| 381 | - // Create the attachment in WordPress and generate the related metadata. |
|
| 382 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 383 | - |
|
| 384 | - // Set the source URL for the image. |
|
| 385 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 228 | + $uri = $entity_data['uri']; |
|
| 229 | + $label = $entity_data['label']; |
|
| 230 | + $type_uri = $entity_data['main_type']; |
|
| 231 | + $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 232 | + $description = $entity_data['description']; |
|
| 233 | + $images = isset( $entity_data['image'] ) ? wl_force_to_array( $entity_data['image'] ) : array(); |
|
| 234 | + $same_as = isset( $entity_data['sameas'] ) ? wl_force_to_array( $entity_data['sameas'] ) : array(); |
|
| 235 | + $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 236 | + $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 237 | + |
|
| 238 | + // Check whether an entity already exists with the provided URI. |
|
| 239 | + if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 240 | + return $post; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + // Prepare properties of the new entity. |
|
| 244 | + $params = array( |
|
| 245 | + 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 246 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 247 | + 'post_title' => $label, |
|
| 248 | + 'post_content' => $description, |
|
| 249 | + 'post_excerpt' => '', |
|
| 250 | + // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 251 | + // entity with a post_name already set, since this call is made only for |
|
| 252 | + // new entities. |
|
| 253 | + // |
|
| 254 | + // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 255 | + 'post_name' => sanitize_title( $label ), |
|
| 256 | + ); |
|
| 257 | + |
|
| 258 | + // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 259 | + // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 260 | + // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 261 | + // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 262 | + // is created when saving a post. |
|
| 263 | + global $wpseo_metabox, $seo_ultimate; |
|
| 264 | + if ( isset( $wpseo_metabox ) ) { |
|
| 265 | + remove_action( 'wp_insert_post', array( |
|
| 266 | + $wpseo_metabox, |
|
| 267 | + 'save_postdata', |
|
| 268 | + ) ); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + if ( isset( $seo_ultimate ) ) { |
|
| 272 | + remove_action( 'save_post', array( |
|
| 273 | + $seo_ultimate, |
|
| 274 | + 'save_postmeta_box', |
|
| 275 | + ) ); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 279 | + // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 280 | + // to the save_post and restore them after we saved the entity. |
|
| 281 | + // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 282 | + // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 283 | + // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 284 | + global $wp_filter; |
|
| 285 | + $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 286 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 287 | + |
|
| 288 | + // create or update the post. |
|
| 289 | + $post_id = wp_insert_post( $params, true ); |
|
| 290 | + |
|
| 291 | + // Restore all the existing filters. |
|
| 292 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 293 | + |
|
| 294 | + // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 295 | + if ( isset( $wpseo_metabox ) ) { |
|
| 296 | + add_action( 'wp_insert_post', array( |
|
| 297 | + $wpseo_metabox, |
|
| 298 | + 'save_postdata', |
|
| 299 | + ) ); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 303 | + if ( isset( $seo_ultimate ) ) { |
|
| 304 | + add_action( 'save_post', array( |
|
| 305 | + $seo_ultimate, |
|
| 306 | + 'save_postmeta_box', |
|
| 307 | + ), 10, 2 ); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + // TODO: handle errors. |
|
| 311 | + if ( is_wp_error( $post_id ) ) { |
|
| 312 | + wl_write_log( ': error occurred' ); |
|
| 313 | + |
|
| 314 | + // inform an error occurred. |
|
| 315 | + return null; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 319 | + |
|
| 320 | + // Save the entity types. |
|
| 321 | + wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 322 | + |
|
| 323 | + // Get a dataset URI for the entity. |
|
| 324 | + $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 325 | + |
|
| 326 | + // Save the entity URI. |
|
| 327 | + wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 328 | + |
|
| 329 | + // Add the uri to the sameAs data if it's not a local URI. |
|
| 330 | + if ( $wl_uri !== $uri ) { |
|
| 331 | + array_push( $same_as, $uri ); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $new_uri = wl_get_entity_uri( $post_id ); |
|
| 335 | + |
|
| 336 | + // Save the sameAs data for the entity. |
|
| 337 | + wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 338 | + |
|
| 339 | + // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 340 | + foreach ( $other_properties as $property_name => $property_value ) { |
|
| 341 | + wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + // Call hooks. |
|
| 345 | + do_action( 'wl_save_entity', $post_id ); |
|
| 346 | + |
|
| 347 | + foreach ( $images as $image_remote_url ) { |
|
| 348 | + |
|
| 349 | + // Check if image is already present in local DB |
|
| 350 | + if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 351 | + // Do nothing. |
|
| 352 | + continue; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + // Check if there is an existing attachment for this post ID and source URL. |
|
| 356 | + $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 357 | + |
|
| 358 | + // Skip if an existing image is found. |
|
| 359 | + if ( null !== $existing_image ) { |
|
| 360 | + continue; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // Save the image and get the local path. |
|
| 364 | + $image = wl_save_image( $image_remote_url ); |
|
| 365 | + |
|
| 366 | + // Get the local URL. |
|
| 367 | + $filename = $image['path']; |
|
| 368 | + $url = $image['url']; |
|
| 369 | + $content_type = $image['content_type']; |
|
| 370 | + |
|
| 371 | + $attachment = array( |
|
| 372 | + 'guid' => $url, |
|
| 373 | + // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 374 | + 'post_title' => $label, |
|
| 375 | + // Set the title to the post title. |
|
| 376 | + 'post_content' => '', |
|
| 377 | + 'post_status' => 'inherit', |
|
| 378 | + 'post_mime_type' => $content_type, |
|
| 379 | + ); |
|
| 380 | + |
|
| 381 | + // Create the attachment in WordPress and generate the related metadata. |
|
| 382 | + $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 383 | + |
|
| 384 | + // Set the source URL for the image. |
|
| 385 | + wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 386 | 386 | |
| 387 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 388 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 387 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 388 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 389 | 389 | |
| 390 | - // Set it as the featured image. |
|
| 391 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 392 | - } |
|
| 390 | + // Set it as the featured image. |
|
| 391 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
| 392 | + } |
|
| 393 | 393 | |
| 394 | - // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 395 | - // save the entity in the triple store. |
|
| 396 | - wl_linked_data_push_to_redlink( $post_id ); |
|
| 394 | + // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 395 | + // save the entity in the triple store. |
|
| 396 | + wl_linked_data_push_to_redlink( $post_id ); |
|
| 397 | 397 | |
| 398 | - // finally return the entity post. |
|
| 399 | - return get_post( $post_id ); |
|
| 398 | + // finally return the entity post. |
|
| 399 | + return get_post( $post_id ); |
|
| 400 | 400 | } |
| 401 | 401 | |
| 402 | 402 | /** |
@@ -410,42 +410,42 @@ discard block |
||
| 410 | 410 | */ |
| 411 | 411 | function wl_linked_data_content_get_embedded_entities( $content ) { |
| 412 | 412 | |
| 413 | - // Remove quote escapes. |
|
| 414 | - $content = str_replace( '\\"', '"', $content ); |
|
| 413 | + // Remove quote escapes. |
|
| 414 | + $content = str_replace( '\\"', '"', $content ); |
|
| 415 | 415 | |
| 416 | - // Match all itemid attributes. |
|
| 417 | - $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 416 | + // Match all itemid attributes. |
|
| 417 | + $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 418 | 418 | |
| 419 | - // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 419 | + // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 420 | 420 | |
| 421 | - // Remove the pattern while it is found (match nested annotations). |
|
| 422 | - $matches = array(); |
|
| 421 | + // Remove the pattern while it is found (match nested annotations). |
|
| 422 | + $matches = array(); |
|
| 423 | 423 | |
| 424 | - // In case of errors, return an empty array. |
|
| 425 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 426 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 424 | + // In case of errors, return an empty array. |
|
| 425 | + if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 426 | + wl_write_log( "Found no entities embedded in content" ); |
|
| 427 | 427 | |
| 428 | - return array(); |
|
| 429 | - } |
|
| 428 | + return array(); |
|
| 429 | + } |
|
| 430 | 430 | |
| 431 | 431 | // wl_write_log("wl_update_related_entities [ content :: $content ][ data :: " . var_export($data, true). " ][ matches :: " . var_export($matches, true) . " ]"); |
| 432 | 432 | |
| 433 | - // Collect the entities. |
|
| 434 | - $entities = array(); |
|
| 435 | - foreach ( $matches[1] as $uri ) { |
|
| 436 | - $uri_d = html_entity_decode( $uri ); |
|
| 433 | + // Collect the entities. |
|
| 434 | + $entities = array(); |
|
| 435 | + foreach ( $matches[1] as $uri ) { |
|
| 436 | + $uri_d = html_entity_decode( $uri ); |
|
| 437 | 437 | |
| 438 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 438 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 439 | 439 | |
| 440 | - if ( null !== $entity ) { |
|
| 441 | - array_push( $entities, $entity->ID ); |
|
| 442 | - } |
|
| 443 | - } |
|
| 440 | + if ( null !== $entity ) { |
|
| 441 | + array_push( $entities, $entity->ID ); |
|
| 442 | + } |
|
| 443 | + } |
|
| 444 | 444 | |
| 445 | - // $count = sizeof( $entities ); |
|
| 446 | - // wl_write_log( "Found $count entities embedded in content" ); |
|
| 445 | + // $count = sizeof( $entities ); |
|
| 446 | + // wl_write_log( "Found $count entities embedded in content" ); |
|
| 447 | 447 | |
| 448 | - return $entities; |
|
| 448 | + return $entities; |
|
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | /** |
@@ -457,22 +457,22 @@ discard block |
||
| 457 | 457 | */ |
| 458 | 458 | function wl_linked_data_push_to_redlink( $post_id ) { |
| 459 | 459 | |
| 460 | - // Get the post. |
|
| 461 | - $post = get_post( $post_id ); |
|
| 460 | + // Get the post. |
|
| 461 | + $post = get_post( $post_id ); |
|
| 462 | 462 | |
| 463 | - // wl_write_log( "wl_linked_data_push_to_redlink [ post id :: $post_id ][ post type :: $post->post_type ]" ); |
|
| 463 | + // wl_write_log( "wl_linked_data_push_to_redlink [ post id :: $post_id ][ post type :: $post->post_type ]" ); |
|
| 464 | 464 | |
| 465 | - // Call the method on behalf of the post type. |
|
| 466 | - switch ( $post->post_type ) { |
|
| 467 | - case 'entity': |
|
| 468 | - wl_push_entity_post_to_redlink( $post ); |
|
| 469 | - break; |
|
| 470 | - default: |
|
| 471 | - wl_push_post_to_redlink( $post ); |
|
| 472 | - } |
|
| 465 | + // Call the method on behalf of the post type. |
|
| 466 | + switch ( $post->post_type ) { |
|
| 467 | + case 'entity': |
|
| 468 | + wl_push_entity_post_to_redlink( $post ); |
|
| 469 | + break; |
|
| 470 | + default: |
|
| 471 | + wl_push_post_to_redlink( $post ); |
|
| 472 | + } |
|
| 473 | 473 | |
| 474 | - // Reindex the triple store if buffering is turned off. |
|
| 475 | - if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 476 | - wordlift_reindex_triple_store(); |
|
| 477 | - } |
|
| 474 | + // Reindex the triple store if buffering is turned off. |
|
| 475 | + if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 476 | + wordlift_reindex_triple_store(); |
|
| 477 | + } |
|
| 478 | 478 | } |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | * The Linked Data module provides synchronization of local WordPress data with the remote Linked Data store. |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | -require_once( 'wordlift_linked_data_images.php' ); |
|
| 6 | +require_once('wordlift_linked_data_images.php'); |
|
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * Receive events from post saves, and split them according to the post type. |
@@ -12,24 +12,24 @@ discard block |
||
| 12 | 12 | * |
| 13 | 13 | * @param int $post_id The post id. |
| 14 | 14 | */ |
| 15 | -function wl_linked_data_save_post( $post_id ) { |
|
| 15 | +function wl_linked_data_save_post($post_id) { |
|
| 16 | 16 | |
| 17 | 17 | // If it's not numeric exit from here. |
| 18 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
| 18 | + if ( ! is_numeric($post_id) || is_numeric(wp_is_post_revision($post_id))) { |
|
| 19 | 19 | return; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | // unhook this function so it doesn't loop infinitely |
| 23 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 23 | + remove_action('save_post', 'wl_linked_data_save_post'); |
|
| 24 | 24 | |
| 25 | 25 | // raise the *wl_linked_data_save_post* event. |
| 26 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
| 26 | + do_action('wl_linked_data_save_post', $post_id); |
|
| 27 | 27 | |
| 28 | 28 | // re-hook this function |
| 29 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 29 | + add_action('save_post', 'wl_linked_data_save_post'); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | -add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
| 32 | +add_action('save_post', 'wl_linked_data_save_post'); |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * Save the post to the triple store. Also saves the entities locally and on the triple store. |
@@ -38,17 +38,17 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @param int $post_id The post id being saved. |
| 40 | 40 | */ |
| 41 | -function wl_linked_data_save_post_and_related_entities( $post_id ) { |
|
| 41 | +function wl_linked_data_save_post_and_related_entities($post_id) { |
|
| 42 | 42 | |
| 43 | 43 | // Ignore auto-saves |
| 44 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 44 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
| 45 | 45 | return; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | // get the current post. |
| 49 | - $post = get_post( $post_id ); |
|
| 49 | + $post = get_post($post_id); |
|
| 50 | 50 | |
| 51 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 51 | + remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 52 | 52 | |
| 53 | 53 | // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
| 54 | 54 | |
@@ -56,44 +56,44 @@ discard block |
||
| 56 | 56 | $entities_uri_mapping = array(); |
| 57 | 57 | |
| 58 | 58 | // Save the entities coming with POST data. |
| 59 | - if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) { |
|
| 59 | + if (isset($_POST['wl_entities']) && isset($_POST['wl_boxes'])) { |
|
| 60 | 60 | |
| 61 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
| 62 | - wl_write_log( json_encode( $_POST['wl_entities'] ) ); |
|
| 63 | - wl_write_log( "]" ); |
|
| 64 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_boxes) :: " ); |
|
| 65 | - wl_write_log( json_encode( $_POST['wl_boxes'], true ) ); |
|
| 66 | - wl_write_log( "]" ); |
|
| 61 | + wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: "); |
|
| 62 | + wl_write_log(json_encode($_POST['wl_entities'])); |
|
| 63 | + wl_write_log("]"); |
|
| 64 | + wl_write_log("[ post id :: $post_id ][ POST(wl_boxes) :: "); |
|
| 65 | + wl_write_log(json_encode($_POST['wl_boxes'], true)); |
|
| 66 | + wl_write_log("]"); |
|
| 67 | 67 | |
| 68 | 68 | $entities_via_post = $_POST['wl_entities']; |
| 69 | 69 | $boxes_via_post = $_POST['wl_boxes']; |
| 70 | 70 | |
| 71 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
| 71 | + foreach ($entities_via_post as $entity_uri => $entity) { |
|
| 72 | 72 | |
| 73 | 73 | // Only if the current entity is created from scratch let's avoid to |
| 74 | 74 | // create more than one entity with same label & entity type. |
| 75 | - $entity_type = ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) ? |
|
| 75 | + $entity_type = (preg_match('/^local-entity-.+/', $entity_uri) > 0) ? |
|
| 76 | 76 | $entity['main_type'] : null; |
| 77 | 77 | |
| 78 | 78 | // Look if current entity uri matches an internal existing entity, meaning: |
| 79 | 79 | // 1. when $entity_uri is an internal uri |
| 80 | 80 | // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
| 81 | - $ie = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $entity_uri ); |
|
| 81 | + $ie = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($entity_uri); |
|
| 82 | 82 | |
| 83 | 83 | // Detect the uri depending if is an existing or a new entity |
| 84 | - $uri = ( null === $ie ) ? |
|
| 84 | + $uri = (null === $ie) ? |
|
| 85 | 85 | Wordlift_Uri_Service::get_instance()->build_uri( |
| 86 | 86 | $entity['label'], |
| 87 | 87 | Wordlift_Entity_Service::TYPE_NAME, |
| 88 | 88 | $entity_type |
| 89 | - ) : wl_get_entity_uri( $ie->ID ); |
|
| 89 | + ) : wl_get_entity_uri($ie->ID); |
|
| 90 | 90 | |
| 91 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
| 92 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
| 91 | + wl_write_log("Map $entity_uri on $uri"); |
|
| 92 | + $entities_uri_mapping[$entity_uri] = $uri; |
|
| 93 | 93 | |
| 94 | 94 | // Local entities have a tmp uri with 'local-entity-' prefix |
| 95 | 95 | // These uris need to be rewritten here and replaced in the content |
| 96 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) > 0 ) { |
|
| 96 | + if (preg_match('/^local-entity-.+/', $entity_uri) > 0) { |
|
| 97 | 97 | // Override the entity obj |
| 98 | 98 | $entity['uri'] = $uri; |
| 99 | 99 | } |
@@ -102,8 +102,8 @@ discard block |
||
| 102 | 102 | $entity['related_post_id'] = $post_id; |
| 103 | 103 | |
| 104 | 104 | // Save the entity if is a new entity |
| 105 | - if ( null === $ie ) { |
|
| 106 | - wl_save_entity( $entity ); |
|
| 105 | + if (null === $ie) { |
|
| 106 | + wl_save_entity($entity); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | } |
@@ -113,36 +113,36 @@ discard block |
||
| 113 | 113 | // Replace tmp uris in content post if needed |
| 114 | 114 | $updated_post_content = $post->post_content; |
| 115 | 115 | // Save each entity and store the post id. |
| 116 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
| 117 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
| 116 | + foreach ($entities_uri_mapping as $tmp_uri => $uri) { |
|
| 117 | + $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | // Update the post content |
| 121 | - wp_update_post( array( |
|
| 121 | + wp_update_post(array( |
|
| 122 | 122 | 'ID' => $post->ID, |
| 123 | 123 | 'post_content' => $updated_post_content, |
| 124 | - ) ); |
|
| 124 | + )); |
|
| 125 | 125 | |
| 126 | 126 | // Extract related/referenced entities from text. |
| 127 | - $disambiguated_entities = wl_linked_data_content_get_embedded_entities( $updated_post_content ); |
|
| 127 | + $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content); |
|
| 128 | 128 | |
| 129 | 129 | // Reset previously saved instances |
| 130 | - wl_core_delete_relation_instances( $post_id ); |
|
| 130 | + wl_core_delete_relation_instances($post_id); |
|
| 131 | 131 | |
| 132 | 132 | // Save relation instances |
| 133 | - foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) { |
|
| 133 | + foreach (array_unique($disambiguated_entities) as $referenced_entity_id) { |
|
| 134 | 134 | |
| 135 | 135 | wl_core_add_relation_instance( |
| 136 | 136 | $post_id, |
| 137 | - Wordlift_Entity_Service::get_instance()->get_classification_scope_for( $referenced_entity_id ), |
|
| 137 | + Wordlift_Entity_Service::get_instance()->get_classification_scope_for($referenced_entity_id), |
|
| 138 | 138 | $referenced_entity_id |
| 139 | 139 | ); |
| 140 | 140 | |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - if ( isset( $_POST['wl_entities'] ) ) { |
|
| 143 | + if (isset($_POST['wl_entities'])) { |
|
| 144 | 144 | // Save post metadata if available |
| 145 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? |
|
| 145 | + $metadata_via_post = (isset($_POST['wl_metadata'])) ? |
|
| 146 | 146 | $_POST['wl_metadata'] : array(); |
| 147 | 147 | |
| 148 | 148 | $fields = array( |
@@ -151,61 +151,61 @@ discard block |
||
| 151 | 151 | ); |
| 152 | 152 | |
| 153 | 153 | // Unlink topic taxonomy terms |
| 154 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
| 154 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID); |
|
| 155 | 155 | |
| 156 | - foreach ( $fields as $field ) { |
|
| 156 | + foreach ($fields as $field) { |
|
| 157 | 157 | |
| 158 | 158 | // Delete current values |
| 159 | - delete_post_meta( $post->ID, $field ); |
|
| 159 | + delete_post_meta($post->ID, $field); |
|
| 160 | 160 | // Retrieve the entity uri |
| 161 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
| 162 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
| 161 | + $uri = (isset($metadata_via_post[$field])) ? |
|
| 162 | + stripslashes($metadata_via_post[$field]) : ''; |
|
| 163 | 163 | |
| 164 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
| 164 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri); |
|
| 165 | 165 | |
| 166 | - if ( $entity ) { |
|
| 167 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
| 166 | + if ($entity) { |
|
| 167 | + add_post_meta($post->ID, $field, $entity->ID, true); |
|
| 168 | 168 | // Set also the topic taxonomy |
| 169 | - if ( $field === Wordlift_Schema_Service::FIELD_TOPIC ) { |
|
| 170 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
| 169 | + if ($field === Wordlift_Schema_Service::FIELD_TOPIC) { |
|
| 170 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity); |
|
| 171 | 171 | } |
| 172 | 172 | } |
| 173 | 173 | } |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | // Push the post to Redlink. |
| 177 | - wl_linked_data_push_to_redlink( $post->ID ); |
|
| 177 | + wl_linked_data_push_to_redlink($post->ID); |
|
| 178 | 178 | |
| 179 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 179 | + add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | -add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
| 182 | +add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
| 183 | 183 | |
| 184 | 184 | /** |
| 185 | 185 | * Adds default schema type "Thing" as soon as an entity is created. |
| 186 | 186 | */ |
| 187 | -function wordlift_save_post_add_default_schema_type( $entity_id ) { |
|
| 187 | +function wordlift_save_post_add_default_schema_type($entity_id) { |
|
| 188 | 188 | |
| 189 | - $entity = get_post( $entity_id ); |
|
| 189 | + $entity = get_post($entity_id); |
|
| 190 | 190 | |
| 191 | 191 | // Avoid doing anything if post is autosave or a revision. |
| 192 | 192 | |
| 193 | - if ( wp_is_post_autosave( $entity ) || wp_is_post_revision( $entity ) ) { |
|
| 193 | + if (wp_is_post_autosave($entity) || wp_is_post_revision($entity)) { |
|
| 194 | 194 | return; |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | - $entity_type = wl_schema_get_types( $entity_id ); |
|
| 197 | + $entity_type = wl_schema_get_types($entity_id); |
|
| 198 | 198 | |
| 199 | 199 | // Assign type 'Thing' if we are dealing with an entity without type |
| 200 | - if ( $entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null( $entity_type ) ) { |
|
| 201 | - wl_schema_set_types( $entity_id, 'Thing' ); |
|
| 200 | + if ($entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null($entity_type)) { |
|
| 201 | + wl_schema_set_types($entity_id, 'Thing'); |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | // Priority 1 (default is 10) because we want the default type to be set as soon as possible |
| 207 | 207 | // Attatched to save_post because *wl_linked_data_save_post* does not always fire |
| 208 | -add_action( 'save_post', 'wordlift_save_post_add_default_schema_type', 1 ); |
|
| 208 | +add_action('save_post', 'wordlift_save_post_add_default_schema_type', 1); |
|
| 209 | 209 | |
| 210 | 210 | /** |
| 211 | 211 | * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is |
@@ -223,26 +223,26 @@ discard block |
||
| 223 | 223 | * |
| 224 | 224 | * @return null|WP_Post A post instance or null in case of failure. |
| 225 | 225 | */ |
| 226 | -function wl_save_entity( $entity_data ) { |
|
| 226 | +function wl_save_entity($entity_data) { |
|
| 227 | 227 | |
| 228 | 228 | $uri = $entity_data['uri']; |
| 229 | 229 | $label = $entity_data['label']; |
| 230 | 230 | $type_uri = $entity_data['main_type']; |
| 231 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 231 | + $entity_types = isset($entity_data['type']) ? $entity_data['type'] : array(); |
|
| 232 | 232 | $description = $entity_data['description']; |
| 233 | - $images = isset( $entity_data['image'] ) ? wl_force_to_array( $entity_data['image'] ) : array(); |
|
| 234 | - $same_as = isset( $entity_data['sameas'] ) ? wl_force_to_array( $entity_data['sameas'] ) : array(); |
|
| 235 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 236 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 233 | + $images = isset($entity_data['image']) ? wl_force_to_array($entity_data['image']) : array(); |
|
| 234 | + $same_as = isset($entity_data['sameas']) ? wl_force_to_array($entity_data['sameas']) : array(); |
|
| 235 | + $related_post_id = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null; |
|
| 236 | + $other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array(); |
|
| 237 | 237 | |
| 238 | 238 | // Check whether an entity already exists with the provided URI. |
| 239 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 239 | + if (null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri)) { |
|
| 240 | 240 | return $post; |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | // Prepare properties of the new entity. |
| 244 | 244 | $params = array( |
| 245 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 245 | + 'post_status' => (is_numeric($related_post_id) ? get_post_status($related_post_id) : 'draft'), |
|
| 246 | 246 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
| 247 | 247 | 'post_title' => $label, |
| 248 | 248 | 'post_content' => $description, |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | // new entities. |
| 253 | 253 | // |
| 254 | 254 | // See https://github.com/insideout10/wordlift-plugin/issues/282 |
| 255 | - 'post_name' => sanitize_title( $label ), |
|
| 255 | + 'post_name' => sanitize_title($label), |
|
| 256 | 256 | ); |
| 257 | 257 | |
| 258 | 258 | // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
@@ -261,18 +261,18 @@ discard block |
||
| 261 | 261 | // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
| 262 | 262 | // is created when saving a post. |
| 263 | 263 | global $wpseo_metabox, $seo_ultimate; |
| 264 | - if ( isset( $wpseo_metabox ) ) { |
|
| 265 | - remove_action( 'wp_insert_post', array( |
|
| 264 | + if (isset($wpseo_metabox)) { |
|
| 265 | + remove_action('wp_insert_post', array( |
|
| 266 | 266 | $wpseo_metabox, |
| 267 | 267 | 'save_postdata', |
| 268 | - ) ); |
|
| 268 | + )); |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | - if ( isset( $seo_ultimate ) ) { |
|
| 272 | - remove_action( 'save_post', array( |
|
| 271 | + if (isset($seo_ultimate)) { |
|
| 272 | + remove_action('save_post', array( |
|
| 273 | 273 | $seo_ultimate, |
| 274 | 274 | 'save_postmeta_box', |
| 275 | - ) ); |
|
| 275 | + )); |
|
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
@@ -282,86 +282,86 @@ discard block |
||
| 282 | 282 | // see https://github.com/insideout10/wordlift-plugin/issues/156 |
| 283 | 283 | // see https://github.com/insideout10/wordlift-plugin/issues/148 |
| 284 | 284 | global $wp_filter; |
| 285 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 286 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 285 | + $save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 286 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 287 | 287 | |
| 288 | 288 | // create or update the post. |
| 289 | - $post_id = wp_insert_post( $params, true ); |
|
| 289 | + $post_id = wp_insert_post($params, true); |
|
| 290 | 290 | |
| 291 | 291 | // Restore all the existing filters. |
| 292 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 292 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 293 | 293 | |
| 294 | 294 | // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
| 295 | - if ( isset( $wpseo_metabox ) ) { |
|
| 296 | - add_action( 'wp_insert_post', array( |
|
| 295 | + if (isset($wpseo_metabox)) { |
|
| 296 | + add_action('wp_insert_post', array( |
|
| 297 | 297 | $wpseo_metabox, |
| 298 | 298 | 'save_postdata', |
| 299 | - ) ); |
|
| 299 | + )); |
|
| 300 | 300 | } |
| 301 | 301 | |
| 302 | 302 | // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
| 303 | - if ( isset( $seo_ultimate ) ) { |
|
| 304 | - add_action( 'save_post', array( |
|
| 303 | + if (isset($seo_ultimate)) { |
|
| 304 | + add_action('save_post', array( |
|
| 305 | 305 | $seo_ultimate, |
| 306 | 306 | 'save_postmeta_box', |
| 307 | - ), 10, 2 ); |
|
| 307 | + ), 10, 2); |
|
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | // TODO: handle errors. |
| 311 | - if ( is_wp_error( $post_id ) ) { |
|
| 312 | - wl_write_log( ': error occurred' ); |
|
| 311 | + if (is_wp_error($post_id)) { |
|
| 312 | + wl_write_log(': error occurred'); |
|
| 313 | 313 | |
| 314 | 314 | // inform an error occurred. |
| 315 | 315 | return null; |
| 316 | 316 | } |
| 317 | 317 | |
| 318 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 318 | + wl_set_entity_main_type($post_id, $type_uri); |
|
| 319 | 319 | |
| 320 | 320 | // Save the entity types. |
| 321 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 321 | + wl_set_entity_rdf_types($post_id, $entity_types); |
|
| 322 | 322 | |
| 323 | 323 | // Get a dataset URI for the entity. |
| 324 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 324 | + $wl_uri = wl_build_entity_uri($post_id); |
|
| 325 | 325 | |
| 326 | 326 | // Save the entity URI. |
| 327 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 327 | + wl_set_entity_uri($post_id, $wl_uri); |
|
| 328 | 328 | |
| 329 | 329 | // Add the uri to the sameAs data if it's not a local URI. |
| 330 | - if ( $wl_uri !== $uri ) { |
|
| 331 | - array_push( $same_as, $uri ); |
|
| 330 | + if ($wl_uri !== $uri) { |
|
| 331 | + array_push($same_as, $uri); |
|
| 332 | 332 | } |
| 333 | 333 | |
| 334 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 334 | + $new_uri = wl_get_entity_uri($post_id); |
|
| 335 | 335 | |
| 336 | 336 | // Save the sameAs data for the entity. |
| 337 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 337 | + wl_schema_set_value($post_id, 'sameAs', $same_as); |
|
| 338 | 338 | |
| 339 | 339 | // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
| 340 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 341 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 340 | + foreach ($other_properties as $property_name => $property_value) { |
|
| 341 | + wl_schema_set_value($post_id, $property_name, $property_value); |
|
| 342 | 342 | } |
| 343 | 343 | |
| 344 | 344 | // Call hooks. |
| 345 | - do_action( 'wl_save_entity', $post_id ); |
|
| 345 | + do_action('wl_save_entity', $post_id); |
|
| 346 | 346 | |
| 347 | - foreach ( $images as $image_remote_url ) { |
|
| 347 | + foreach ($images as $image_remote_url) { |
|
| 348 | 348 | |
| 349 | 349 | // Check if image is already present in local DB |
| 350 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 350 | + if (strpos($image_remote_url, site_url()) !== false) { |
|
| 351 | 351 | // Do nothing. |
| 352 | 352 | continue; |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | // Check if there is an existing attachment for this post ID and source URL. |
| 356 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 356 | + $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url); |
|
| 357 | 357 | |
| 358 | 358 | // Skip if an existing image is found. |
| 359 | - if ( null !== $existing_image ) { |
|
| 359 | + if (null !== $existing_image) { |
|
| 360 | 360 | continue; |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | 363 | // Save the image and get the local path. |
| 364 | - $image = wl_save_image( $image_remote_url ); |
|
| 364 | + $image = wl_save_image($image_remote_url); |
|
| 365 | 365 | |
| 366 | 366 | // Get the local URL. |
| 367 | 367 | $filename = $image['path']; |
@@ -379,24 +379,24 @@ discard block |
||
| 379 | 379 | ); |
| 380 | 380 | |
| 381 | 381 | // Create the attachment in WordPress and generate the related metadata. |
| 382 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 382 | + $attachment_id = wp_insert_attachment($attachment, $filename, $post_id); |
|
| 383 | 383 | |
| 384 | 384 | // Set the source URL for the image. |
| 385 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 385 | + wl_set_source_url($attachment_id, $image_remote_url); |
|
| 386 | 386 | |
| 387 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 388 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 387 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
| 388 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 389 | 389 | |
| 390 | 390 | // Set it as the featured image. |
| 391 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 391 | + set_post_thumbnail($post_id, $attachment_id); |
|
| 392 | 392 | } |
| 393 | 393 | |
| 394 | 394 | // The entity is pushed to Redlink on save by the function hooked to save_post. |
| 395 | 395 | // save the entity in the triple store. |
| 396 | - wl_linked_data_push_to_redlink( $post_id ); |
|
| 396 | + wl_linked_data_push_to_redlink($post_id); |
|
| 397 | 397 | |
| 398 | 398 | // finally return the entity post. |
| 399 | - return get_post( $post_id ); |
|
| 399 | + return get_post($post_id); |
|
| 400 | 400 | } |
| 401 | 401 | |
| 402 | 402 | /** |
@@ -408,10 +408,10 @@ discard block |
||
| 408 | 408 | * |
| 409 | 409 | * @return array An array of entity posts. |
| 410 | 410 | */ |
| 411 | -function wl_linked_data_content_get_embedded_entities( $content ) { |
|
| 411 | +function wl_linked_data_content_get_embedded_entities($content) { |
|
| 412 | 412 | |
| 413 | 413 | // Remove quote escapes. |
| 414 | - $content = str_replace( '\\"', '"', $content ); |
|
| 414 | + $content = str_replace('\\"', '"', $content); |
|
| 415 | 415 | |
| 416 | 416 | // Match all itemid attributes. |
| 417 | 417 | $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
@@ -422,8 +422,8 @@ discard block |
||
| 422 | 422 | $matches = array(); |
| 423 | 423 | |
| 424 | 424 | // In case of errors, return an empty array. |
| 425 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 426 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 425 | + if (false === preg_match_all($pattern, $content, $matches)) { |
|
| 426 | + wl_write_log("Found no entities embedded in content"); |
|
| 427 | 427 | |
| 428 | 428 | return array(); |
| 429 | 429 | } |
@@ -432,13 +432,13 @@ discard block |
||
| 432 | 432 | |
| 433 | 433 | // Collect the entities. |
| 434 | 434 | $entities = array(); |
| 435 | - foreach ( $matches[1] as $uri ) { |
|
| 436 | - $uri_d = html_entity_decode( $uri ); |
|
| 435 | + foreach ($matches[1] as $uri) { |
|
| 436 | + $uri_d = html_entity_decode($uri); |
|
| 437 | 437 | |
| 438 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 438 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri_d); |
|
| 439 | 439 | |
| 440 | - if ( null !== $entity ) { |
|
| 441 | - array_push( $entities, $entity->ID ); |
|
| 440 | + if (null !== $entity) { |
|
| 441 | + array_push($entities, $entity->ID); |
|
| 442 | 442 | } |
| 443 | 443 | } |
| 444 | 444 | |
@@ -455,24 +455,24 @@ discard block |
||
| 455 | 455 | * |
| 456 | 456 | * @param int $post_id The post ID. |
| 457 | 457 | */ |
| 458 | -function wl_linked_data_push_to_redlink( $post_id ) { |
|
| 458 | +function wl_linked_data_push_to_redlink($post_id) { |
|
| 459 | 459 | |
| 460 | 460 | // Get the post. |
| 461 | - $post = get_post( $post_id ); |
|
| 461 | + $post = get_post($post_id); |
|
| 462 | 462 | |
| 463 | 463 | // wl_write_log( "wl_linked_data_push_to_redlink [ post id :: $post_id ][ post type :: $post->post_type ]" ); |
| 464 | 464 | |
| 465 | 465 | // Call the method on behalf of the post type. |
| 466 | - switch ( $post->post_type ) { |
|
| 466 | + switch ($post->post_type) { |
|
| 467 | 467 | case 'entity': |
| 468 | - wl_push_entity_post_to_redlink( $post ); |
|
| 468 | + wl_push_entity_post_to_redlink($post); |
|
| 469 | 469 | break; |
| 470 | 470 | default: |
| 471 | - wl_push_post_to_redlink( $post ); |
|
| 471 | + wl_push_post_to_redlink($post); |
|
| 472 | 472 | } |
| 473 | 473 | |
| 474 | 474 | // Reindex the triple store if buffering is turned off. |
| 475 | - if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 475 | + if (false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING) { |
|
| 476 | 476 | wordlift_reindex_triple_store(); |
| 477 | 477 | } |
| 478 | 478 | } |
@@ -7,472 +7,472 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Entity_Service { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The Log service. |
|
| 12 | - * |
|
| 13 | - * @since 3.2.0 |
|
| 14 | - * @access private |
|
| 15 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | - */ |
|
| 17 | - private $log; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The UI service. |
|
| 21 | - * |
|
| 22 | - * @since 3.2.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | - */ |
|
| 26 | - private $ui_service; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The entity post type name. |
|
| 30 | - * |
|
| 31 | - * @since 3.1.0 |
|
| 32 | - */ |
|
| 33 | - const TYPE_NAME = 'entity'; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The alternative label meta key. |
|
| 37 | - * |
|
| 38 | - * @since 3.2.0 |
|
| 39 | - */ |
|
| 40 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The alternative label input template. |
|
| 44 | - * |
|
| 45 | - * @since 3.2.0 |
|
| 46 | - */ |
|
| 47 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 48 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 10 | + /** |
|
| 11 | + * The Log service. |
|
| 12 | + * |
|
| 13 | + * @since 3.2.0 |
|
| 14 | + * @access private |
|
| 15 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | + */ |
|
| 17 | + private $log; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The UI service. |
|
| 21 | + * |
|
| 22 | + * @since 3.2.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | + */ |
|
| 26 | + private $ui_service; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The entity post type name. |
|
| 30 | + * |
|
| 31 | + * @since 3.1.0 |
|
| 32 | + */ |
|
| 33 | + const TYPE_NAME = 'entity'; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The alternative label meta key. |
|
| 37 | + * |
|
| 38 | + * @since 3.2.0 |
|
| 39 | + */ |
|
| 40 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The alternative label input template. |
|
| 44 | + * |
|
| 45 | + * @since 3.2.0 |
|
| 46 | + */ |
|
| 47 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 48 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 49 | 49 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 50 | 50 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 51 | 51 | <button class="button wl-delete-button">%s</button> |
| 52 | 52 | </div>'; |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * A singleton instance of the Entity service. |
|
| 56 | - * |
|
| 57 | - * @since 3.2.0 |
|
| 58 | - * @access private |
|
| 59 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 60 | - */ |
|
| 61 | - private static $instance; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Create a Wordlift_Entity_Service instance. |
|
| 65 | - * |
|
| 66 | - * @since 3.2.0 |
|
| 67 | - * |
|
| 68 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 69 | - */ |
|
| 70 | - public function __construct( $ui_service ) { |
|
| 71 | - |
|
| 72 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 73 | - |
|
| 74 | - // Set the UI service. |
|
| 75 | - $this->ui_service = $ui_service; |
|
| 76 | - |
|
| 77 | - // Set the singleton instance. |
|
| 78 | - self::$instance = $this; |
|
| 79 | - |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Get the singleton instance of the Entity service. |
|
| 84 | - * |
|
| 85 | - * @since 3.2.0 |
|
| 86 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 87 | - */ |
|
| 88 | - public static function get_instance() { |
|
| 89 | - |
|
| 90 | - return self::$instance; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Determines whether a post is an entity or not. |
|
| 95 | - * |
|
| 96 | - * @since 3.1.0 |
|
| 97 | - * |
|
| 98 | - * @param int $post_id A post id. |
|
| 99 | - * |
|
| 100 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 101 | - */ |
|
| 102 | - public function is_entity( $post_id ) { |
|
| 103 | - |
|
| 104 | - return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Get the proper classification scope for a given entity post |
|
| 109 | - * |
|
| 110 | - * @since 3.5.0 |
|
| 111 | - * |
|
| 112 | - * @param integer $post_id An entity post id. |
|
| 113 | - * |
|
| 114 | - * @return string Returns an uri. |
|
| 115 | - */ |
|
| 116 | - public function get_classification_scope_for( $post_id ) { |
|
| 117 | - |
|
| 118 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 119 | - return null; |
|
| 120 | - } |
|
| 121 | - // Retrieve the entity type |
|
| 122 | - $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 124 | - // Retrieve classification boxes configuration |
|
| 125 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | - foreach ( $classification_boxes as $cb ) { |
|
| 127 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 128 | - return $cb['id']; |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // or null |
|
| 133 | - return null; |
|
| 134 | - |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - |
|
| 138 | - public function is_used( $post_id ) { |
|
| 139 | - |
|
| 140 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 141 | - return null; |
|
| 142 | - } |
|
| 143 | - // Retrieve the post |
|
| 144 | - $entity = get_post( $post_id ); |
|
| 145 | - |
|
| 146 | - global $wpdb; |
|
| 147 | - // Retrieve Wordlift relation instances table name |
|
| 148 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 149 | - |
|
| 150 | - // Check is it's referenced / related to another post / entity |
|
| 151 | - $stmt = $wpdb->prepare( |
|
| 152 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 153 | - $entity->ID |
|
| 154 | - ); |
|
| 155 | - |
|
| 156 | - // Perform the query |
|
| 157 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 158 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 159 | - if ( 0 < $relation_instances ) { |
|
| 160 | - return true; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // Check if the entity uri is used as meta_value |
|
| 164 | - $stmt = $wpdb->prepare( |
|
| 165 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 166 | - $entity->ID, |
|
| 167 | - wl_get_entity_uri( $entity->ID ) |
|
| 168 | - ); |
|
| 169 | - // Perform the query |
|
| 170 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 171 | - |
|
| 172 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 173 | - if ( 0 < $meta_instances ) { |
|
| 174 | - return true; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - // If we are here, it means the current entity is not used at the moment |
|
| 178 | - return false; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Determines whether a given uri is an internal uri or not. |
|
| 183 | - * |
|
| 184 | - * @since 3.3.2 |
|
| 185 | - * |
|
| 186 | - * @param int $uri An uri. |
|
| 187 | - * |
|
| 188 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 189 | - */ |
|
| 190 | - public function is_internal_uri( $uri ) { |
|
| 191 | - |
|
| 192 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 197 | - * |
|
| 198 | - * @since 3.2.0 |
|
| 199 | - * |
|
| 200 | - * @param string $uri The entity URI. |
|
| 201 | - * |
|
| 202 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 203 | - */ |
|
| 204 | - public function get_entity_post_by_uri( $uri ) { |
|
| 205 | - |
|
| 206 | - // Check if we've been provided with a value otherwise return null. |
|
| 207 | - if ( empty( $uri ) ) { |
|
| 208 | - return null; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $query_args = array( |
|
| 212 | - 'posts_per_page' => 1, |
|
| 213 | - 'post_status' => 'any', |
|
| 214 | - 'post_type' => self::TYPE_NAME, |
|
| 215 | - 'meta_query' => array( |
|
| 216 | - array( |
|
| 217 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 218 | - 'value' => $uri, |
|
| 219 | - 'compare' => '=', |
|
| 220 | - ), |
|
| 221 | - ), |
|
| 222 | - ); |
|
| 223 | - |
|
| 224 | - // Only if the current uri is not an internal uri, entity search is |
|
| 225 | - // performed also looking at sameAs values. |
|
| 226 | - // |
|
| 227 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 228 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 229 | - |
|
| 230 | - $query_args['meta_query']['relation'] = 'OR'; |
|
| 231 | - $query_args['meta_query'][] = array( |
|
| 232 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 233 | - 'value' => $uri, |
|
| 234 | - 'compare' => '=', |
|
| 235 | - ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $query = new WP_Query( $query_args ); |
|
| 239 | - |
|
| 240 | - // Get the matching entity posts. |
|
| 241 | - $posts = $query->get_posts(); |
|
| 242 | - |
|
| 243 | - // Return null if no post is found. |
|
| 244 | - if ( 0 === count( $posts ) ) { |
|
| 245 | - return null; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - // Return the found post. |
|
| 249 | - return $posts[0]; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 254 | - * we check that the post we're saving is the current post. |
|
| 255 | - * |
|
| 256 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 257 | - * |
|
| 258 | - * @since 3.2.0 |
|
| 259 | - * |
|
| 260 | - * @param int $post_id Post ID. |
|
| 261 | - * @param WP_Post $post Post object. |
|
| 262 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 263 | - */ |
|
| 264 | - public function save_post( $post_id, $post, $update ) { |
|
| 265 | - |
|
| 266 | - // Avoid doing anything if post is autosave or a revision. |
|
| 267 | - |
|
| 268 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 269 | - return; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - // We're setting the alternative label that have been provided via the UI |
|
| 273 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 274 | - // programmatically by some other function: we need to check therefore if |
|
| 275 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 276 | - // |
|
| 277 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 278 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 279 | - return; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - // Get the alt labels from the request (or empty array). |
|
| 283 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 284 | - |
|
| 285 | - // Set the alternative labels. |
|
| 286 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 287 | - |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Set the alternative labels. |
|
| 292 | - * |
|
| 293 | - * @since 3.2.0 |
|
| 294 | - * |
|
| 295 | - * @param int $post_id The post id. |
|
| 296 | - * @param array $alt_labels An array of labels. |
|
| 297 | - */ |
|
| 298 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 299 | - |
|
| 300 | - // Force $alt_labels to be an array |
|
| 301 | - if ( ! is_array( $alt_labels ) ) { |
|
| 302 | - $alt_labels = array( $alt_labels ); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 306 | - |
|
| 307 | - // Delete all the existing alternate labels. |
|
| 308 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 309 | - |
|
| 310 | - // Set the alternative labels. |
|
| 311 | - foreach ( $alt_labels as $alt_label ) { |
|
| 312 | - if ( ! empty( $alt_label ) ) { |
|
| 313 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 314 | - } |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Retrieve the alternate labels. |
|
| 321 | - * |
|
| 322 | - * @since 3.2.0 |
|
| 323 | - * |
|
| 324 | - * @param int $post_id Post id. |
|
| 325 | - * |
|
| 326 | - * @return mixed An array of alternative labels. |
|
| 327 | - */ |
|
| 328 | - public function get_alternative_labels( $post_id ) { |
|
| 329 | - |
|
| 330 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 335 | - * |
|
| 336 | - * @since 3.2.0 |
|
| 337 | - * |
|
| 338 | - * @param WP_Post $post Post object. |
|
| 339 | - */ |
|
| 340 | - public function edit_form_before_permalink( $post ) { |
|
| 341 | - |
|
| 342 | - // If it's not an entity, return. |
|
| 343 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 344 | - return; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - // Print the input template. |
|
| 348 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 349 | - |
|
| 350 | - // Print all the currently set alternative labels. |
|
| 351 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 352 | - |
|
| 353 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 354 | - |
|
| 355 | - }; |
|
| 356 | - |
|
| 357 | - // Print the button. |
|
| 358 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 359 | - |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Get the URI for the entity with the specified post id. |
|
| 364 | - * |
|
| 365 | - * @since 3.6.0 |
|
| 366 | - * |
|
| 367 | - * @param int $post_id The entity post id. |
|
| 368 | - * |
|
| 369 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 370 | - */ |
|
| 371 | - public function get_uri( $post_id ) { |
|
| 372 | - |
|
| 373 | - // If a null is given, nothing to do |
|
| 374 | - if ( null == $post_id ) { |
|
| 375 | - return null; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 379 | - |
|
| 380 | - // If the dataset uri is not properly configured, null is returned |
|
| 381 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 382 | - return null; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - // Set the URI if it isn't set yet. |
|
| 386 | - $post_status = get_post_status( $post_id ); |
|
| 387 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 388 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 389 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - return $uri; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Get the alternative label input HTML code. |
|
| 398 | - * |
|
| 399 | - * @since 3.2.0 |
|
| 400 | - * |
|
| 401 | - * @param string $value The input value. |
|
| 402 | - * |
|
| 403 | - * @return string The input HTML code. |
|
| 404 | - */ |
|
| 405 | - private function get_alternative_label_input( $value = '' ) { |
|
| 406 | - |
|
| 407 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Get the number of entity posts published in this blog. |
|
| 412 | - * |
|
| 413 | - * @since 3.6.0 |
|
| 414 | - * |
|
| 415 | - * @return int The number of published entity posts. |
|
| 416 | - */ |
|
| 417 | - public function count() { |
|
| 418 | - |
|
| 419 | - $count = wp_count_posts( self::TYPE_NAME ); |
|
| 420 | - |
|
| 421 | - return $count->publish; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Create a new entity. |
|
| 426 | - * |
|
| 427 | - * @since 3.9.0 |
|
| 428 | - * |
|
| 429 | - * @param string $name The entity name. |
|
| 430 | - * @param string $type_uri The entity's type URI. |
|
| 431 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 432 | - * @param string $status The post status, by default 'publish'. |
|
| 433 | - * |
|
| 434 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 435 | - */ |
|
| 436 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 437 | - |
|
| 438 | - // Create an entity for the publisher. |
|
| 439 | - $post_id = wp_insert_post( array( |
|
| 440 | - 'post_type' => self::TYPE_NAME, |
|
| 441 | - 'post_title' => $name, |
|
| 442 | - 'post_status' => $status, |
|
| 443 | - 'post_content' => '', |
|
| 444 | - ) ); |
|
| 445 | - |
|
| 446 | - // Return the error if any. |
|
| 447 | - if ( is_wp_error( $post_id ) ) { |
|
| 448 | - return $post_id; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - // Set the entity logo. |
|
| 452 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 453 | - set_post_thumbnail( $post_id, $logo ); |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - // Set the entity type. |
|
| 457 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 458 | - |
|
| 459 | - return $post_id; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * Get the entities related to the one with the specified id. By default only |
|
| 464 | - * published entities will be returned. |
|
| 465 | - * |
|
| 466 | - * @since 3.10.0 |
|
| 467 | - * |
|
| 468 | - * @param int $id The post id. |
|
| 469 | - * @param string $post_status The target post status (default = publish). |
|
| 470 | - * |
|
| 471 | - * @return array An array of post ids. |
|
| 472 | - */ |
|
| 473 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 474 | - |
|
| 475 | - return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 476 | - } |
|
| 54 | + /** |
|
| 55 | + * A singleton instance of the Entity service. |
|
| 56 | + * |
|
| 57 | + * @since 3.2.0 |
|
| 58 | + * @access private |
|
| 59 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 60 | + */ |
|
| 61 | + private static $instance; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Create a Wordlift_Entity_Service instance. |
|
| 65 | + * |
|
| 66 | + * @since 3.2.0 |
|
| 67 | + * |
|
| 68 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 69 | + */ |
|
| 70 | + public function __construct( $ui_service ) { |
|
| 71 | + |
|
| 72 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 73 | + |
|
| 74 | + // Set the UI service. |
|
| 75 | + $this->ui_service = $ui_service; |
|
| 76 | + |
|
| 77 | + // Set the singleton instance. |
|
| 78 | + self::$instance = $this; |
|
| 79 | + |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Get the singleton instance of the Entity service. |
|
| 84 | + * |
|
| 85 | + * @since 3.2.0 |
|
| 86 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 87 | + */ |
|
| 88 | + public static function get_instance() { |
|
| 89 | + |
|
| 90 | + return self::$instance; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Determines whether a post is an entity or not. |
|
| 95 | + * |
|
| 96 | + * @since 3.1.0 |
|
| 97 | + * |
|
| 98 | + * @param int $post_id A post id. |
|
| 99 | + * |
|
| 100 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 101 | + */ |
|
| 102 | + public function is_entity( $post_id ) { |
|
| 103 | + |
|
| 104 | + return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Get the proper classification scope for a given entity post |
|
| 109 | + * |
|
| 110 | + * @since 3.5.0 |
|
| 111 | + * |
|
| 112 | + * @param integer $post_id An entity post id. |
|
| 113 | + * |
|
| 114 | + * @return string Returns an uri. |
|
| 115 | + */ |
|
| 116 | + public function get_classification_scope_for( $post_id ) { |
|
| 117 | + |
|
| 118 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 119 | + return null; |
|
| 120 | + } |
|
| 121 | + // Retrieve the entity type |
|
| 122 | + $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 124 | + // Retrieve classification boxes configuration |
|
| 125 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | + foreach ( $classification_boxes as $cb ) { |
|
| 127 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 128 | + return $cb['id']; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // or null |
|
| 133 | + return null; |
|
| 134 | + |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + |
|
| 138 | + public function is_used( $post_id ) { |
|
| 139 | + |
|
| 140 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 141 | + return null; |
|
| 142 | + } |
|
| 143 | + // Retrieve the post |
|
| 144 | + $entity = get_post( $post_id ); |
|
| 145 | + |
|
| 146 | + global $wpdb; |
|
| 147 | + // Retrieve Wordlift relation instances table name |
|
| 148 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 149 | + |
|
| 150 | + // Check is it's referenced / related to another post / entity |
|
| 151 | + $stmt = $wpdb->prepare( |
|
| 152 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 153 | + $entity->ID |
|
| 154 | + ); |
|
| 155 | + |
|
| 156 | + // Perform the query |
|
| 157 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 158 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 159 | + if ( 0 < $relation_instances ) { |
|
| 160 | + return true; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // Check if the entity uri is used as meta_value |
|
| 164 | + $stmt = $wpdb->prepare( |
|
| 165 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 166 | + $entity->ID, |
|
| 167 | + wl_get_entity_uri( $entity->ID ) |
|
| 168 | + ); |
|
| 169 | + // Perform the query |
|
| 170 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 171 | + |
|
| 172 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 173 | + if ( 0 < $meta_instances ) { |
|
| 174 | + return true; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + // If we are here, it means the current entity is not used at the moment |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Determines whether a given uri is an internal uri or not. |
|
| 183 | + * |
|
| 184 | + * @since 3.3.2 |
|
| 185 | + * |
|
| 186 | + * @param int $uri An uri. |
|
| 187 | + * |
|
| 188 | + * @return true if the uri internal to the current dataset otherwise false. |
|
| 189 | + */ |
|
| 190 | + public function is_internal_uri( $uri ) { |
|
| 191 | + |
|
| 192 | + return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 197 | + * |
|
| 198 | + * @since 3.2.0 |
|
| 199 | + * |
|
| 200 | + * @param string $uri The entity URI. |
|
| 201 | + * |
|
| 202 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 203 | + */ |
|
| 204 | + public function get_entity_post_by_uri( $uri ) { |
|
| 205 | + |
|
| 206 | + // Check if we've been provided with a value otherwise return null. |
|
| 207 | + if ( empty( $uri ) ) { |
|
| 208 | + return null; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $query_args = array( |
|
| 212 | + 'posts_per_page' => 1, |
|
| 213 | + 'post_status' => 'any', |
|
| 214 | + 'post_type' => self::TYPE_NAME, |
|
| 215 | + 'meta_query' => array( |
|
| 216 | + array( |
|
| 217 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 218 | + 'value' => $uri, |
|
| 219 | + 'compare' => '=', |
|
| 220 | + ), |
|
| 221 | + ), |
|
| 222 | + ); |
|
| 223 | + |
|
| 224 | + // Only if the current uri is not an internal uri, entity search is |
|
| 225 | + // performed also looking at sameAs values. |
|
| 226 | + // |
|
| 227 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 228 | + if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 229 | + |
|
| 230 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 231 | + $query_args['meta_query'][] = array( |
|
| 232 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 233 | + 'value' => $uri, |
|
| 234 | + 'compare' => '=', |
|
| 235 | + ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $query = new WP_Query( $query_args ); |
|
| 239 | + |
|
| 240 | + // Get the matching entity posts. |
|
| 241 | + $posts = $query->get_posts(); |
|
| 242 | + |
|
| 243 | + // Return null if no post is found. |
|
| 244 | + if ( 0 === count( $posts ) ) { |
|
| 245 | + return null; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + // Return the found post. |
|
| 249 | + return $posts[0]; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 254 | + * we check that the post we're saving is the current post. |
|
| 255 | + * |
|
| 256 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 257 | + * |
|
| 258 | + * @since 3.2.0 |
|
| 259 | + * |
|
| 260 | + * @param int $post_id Post ID. |
|
| 261 | + * @param WP_Post $post Post object. |
|
| 262 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 263 | + */ |
|
| 264 | + public function save_post( $post_id, $post, $update ) { |
|
| 265 | + |
|
| 266 | + // Avoid doing anything if post is autosave or a revision. |
|
| 267 | + |
|
| 268 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 269 | + return; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + // We're setting the alternative label that have been provided via the UI |
|
| 273 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 274 | + // programmatically by some other function: we need to check therefore if |
|
| 275 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 276 | + // |
|
| 277 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 278 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 279 | + return; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + // Get the alt labels from the request (or empty array). |
|
| 283 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 284 | + |
|
| 285 | + // Set the alternative labels. |
|
| 286 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 287 | + |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Set the alternative labels. |
|
| 292 | + * |
|
| 293 | + * @since 3.2.0 |
|
| 294 | + * |
|
| 295 | + * @param int $post_id The post id. |
|
| 296 | + * @param array $alt_labels An array of labels. |
|
| 297 | + */ |
|
| 298 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 299 | + |
|
| 300 | + // Force $alt_labels to be an array |
|
| 301 | + if ( ! is_array( $alt_labels ) ) { |
|
| 302 | + $alt_labels = array( $alt_labels ); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 306 | + |
|
| 307 | + // Delete all the existing alternate labels. |
|
| 308 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 309 | + |
|
| 310 | + // Set the alternative labels. |
|
| 311 | + foreach ( $alt_labels as $alt_label ) { |
|
| 312 | + if ( ! empty( $alt_label ) ) { |
|
| 313 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 314 | + } |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Retrieve the alternate labels. |
|
| 321 | + * |
|
| 322 | + * @since 3.2.0 |
|
| 323 | + * |
|
| 324 | + * @param int $post_id Post id. |
|
| 325 | + * |
|
| 326 | + * @return mixed An array of alternative labels. |
|
| 327 | + */ |
|
| 328 | + public function get_alternative_labels( $post_id ) { |
|
| 329 | + |
|
| 330 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 335 | + * |
|
| 336 | + * @since 3.2.0 |
|
| 337 | + * |
|
| 338 | + * @param WP_Post $post Post object. |
|
| 339 | + */ |
|
| 340 | + public function edit_form_before_permalink( $post ) { |
|
| 341 | + |
|
| 342 | + // If it's not an entity, return. |
|
| 343 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 344 | + return; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + // Print the input template. |
|
| 348 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 349 | + |
|
| 350 | + // Print all the currently set alternative labels. |
|
| 351 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 352 | + |
|
| 353 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 354 | + |
|
| 355 | + }; |
|
| 356 | + |
|
| 357 | + // Print the button. |
|
| 358 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 359 | + |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Get the URI for the entity with the specified post id. |
|
| 364 | + * |
|
| 365 | + * @since 3.6.0 |
|
| 366 | + * |
|
| 367 | + * @param int $post_id The entity post id. |
|
| 368 | + * |
|
| 369 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 370 | + */ |
|
| 371 | + public function get_uri( $post_id ) { |
|
| 372 | + |
|
| 373 | + // If a null is given, nothing to do |
|
| 374 | + if ( null == $post_id ) { |
|
| 375 | + return null; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 379 | + |
|
| 380 | + // If the dataset uri is not properly configured, null is returned |
|
| 381 | + if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 382 | + return null; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + // Set the URI if it isn't set yet. |
|
| 386 | + $post_status = get_post_status( $post_id ); |
|
| 387 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 388 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 389 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + return $uri; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Get the alternative label input HTML code. |
|
| 398 | + * |
|
| 399 | + * @since 3.2.0 |
|
| 400 | + * |
|
| 401 | + * @param string $value The input value. |
|
| 402 | + * |
|
| 403 | + * @return string The input HTML code. |
|
| 404 | + */ |
|
| 405 | + private function get_alternative_label_input( $value = '' ) { |
|
| 406 | + |
|
| 407 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Get the number of entity posts published in this blog. |
|
| 412 | + * |
|
| 413 | + * @since 3.6.0 |
|
| 414 | + * |
|
| 415 | + * @return int The number of published entity posts. |
|
| 416 | + */ |
|
| 417 | + public function count() { |
|
| 418 | + |
|
| 419 | + $count = wp_count_posts( self::TYPE_NAME ); |
|
| 420 | + |
|
| 421 | + return $count->publish; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Create a new entity. |
|
| 426 | + * |
|
| 427 | + * @since 3.9.0 |
|
| 428 | + * |
|
| 429 | + * @param string $name The entity name. |
|
| 430 | + * @param string $type_uri The entity's type URI. |
|
| 431 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 432 | + * @param string $status The post status, by default 'publish'. |
|
| 433 | + * |
|
| 434 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 435 | + */ |
|
| 436 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 437 | + |
|
| 438 | + // Create an entity for the publisher. |
|
| 439 | + $post_id = wp_insert_post( array( |
|
| 440 | + 'post_type' => self::TYPE_NAME, |
|
| 441 | + 'post_title' => $name, |
|
| 442 | + 'post_status' => $status, |
|
| 443 | + 'post_content' => '', |
|
| 444 | + ) ); |
|
| 445 | + |
|
| 446 | + // Return the error if any. |
|
| 447 | + if ( is_wp_error( $post_id ) ) { |
|
| 448 | + return $post_id; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + // Set the entity logo. |
|
| 452 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 453 | + set_post_thumbnail( $post_id, $logo ); |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + // Set the entity type. |
|
| 457 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 458 | + |
|
| 459 | + return $post_id; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * Get the entities related to the one with the specified id. By default only |
|
| 464 | + * published entities will be returned. |
|
| 465 | + * |
|
| 466 | + * @since 3.10.0 |
|
| 467 | + * |
|
| 468 | + * @param int $id The post id. |
|
| 469 | + * @param string $post_status The target post status (default = publish). |
|
| 470 | + * |
|
| 471 | + * @return array An array of post ids. |
|
| 472 | + */ |
|
| 473 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 474 | + |
|
| 475 | + return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 476 | + } |
|
| 477 | 477 | |
| 478 | 478 | } |
@@ -67,9 +67,9 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @param \Wordlift_UI_Service $ui_service The UI service. |
| 69 | 69 | */ |
| 70 | - public function __construct( $ui_service ) { |
|
| 70 | + public function __construct($ui_service) { |
|
| 71 | 71 | |
| 72 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 72 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 73 | 73 | |
| 74 | 74 | // Set the UI service. |
| 75 | 75 | $this->ui_service = $ui_service; |
@@ -99,9 +99,9 @@ discard block |
||
| 99 | 99 | * |
| 100 | 100 | * @return bool Return true if the post is an entity otherwise false. |
| 101 | 101 | */ |
| 102 | - public function is_entity( $post_id ) { |
|
| 102 | + public function is_entity($post_id) { |
|
| 103 | 103 | |
| 104 | - return ( self::TYPE_NAME === get_post_type( $post_id ) ); |
|
| 104 | + return (self::TYPE_NAME === get_post_type($post_id)); |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -113,18 +113,18 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @return string Returns an uri. |
| 115 | 115 | */ |
| 116 | - public function get_classification_scope_for( $post_id ) { |
|
| 116 | + public function get_classification_scope_for($post_id) { |
|
| 117 | 117 | |
| 118 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 118 | + if (false === $this->is_entity($post_id)) { |
|
| 119 | 119 | return null; |
| 120 | 120 | } |
| 121 | 121 | // Retrieve the entity type |
| 122 | - $entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id ); |
|
| 123 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 122 | + $entity_type_arr = wl_entity_type_taxonomy_get_type($post_id); |
|
| 123 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 124 | 124 | // Retrieve classification boxes configuration |
| 125 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 126 | - foreach ( $classification_boxes as $cb ) { |
|
| 127 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 125 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 126 | + foreach ($classification_boxes as $cb) { |
|
| 127 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 128 | 128 | return $cb['id']; |
| 129 | 129 | } |
| 130 | 130 | } |
@@ -135,13 +135,13 @@ discard block |
||
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | |
| 138 | - public function is_used( $post_id ) { |
|
| 138 | + public function is_used($post_id) { |
|
| 139 | 139 | |
| 140 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 140 | + if (false === $this->is_entity($post_id)) { |
|
| 141 | 141 | return null; |
| 142 | 142 | } |
| 143 | 143 | // Retrieve the post |
| 144 | - $entity = get_post( $post_id ); |
|
| 144 | + $entity = get_post($post_id); |
|
| 145 | 145 | |
| 146 | 146 | global $wpdb; |
| 147 | 147 | // Retrieve Wordlift relation instances table name |
@@ -154,9 +154,9 @@ discard block |
||
| 154 | 154 | ); |
| 155 | 155 | |
| 156 | 156 | // Perform the query |
| 157 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 157 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 158 | 158 | // If there is at least one relation instance for the current entity, then it's used |
| 159 | - if ( 0 < $relation_instances ) { |
|
| 159 | + if (0 < $relation_instances) { |
|
| 160 | 160 | return true; |
| 161 | 161 | } |
| 162 | 162 | |
@@ -164,13 +164,13 @@ discard block |
||
| 164 | 164 | $stmt = $wpdb->prepare( |
| 165 | 165 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 166 | 166 | $entity->ID, |
| 167 | - wl_get_entity_uri( $entity->ID ) |
|
| 167 | + wl_get_entity_uri($entity->ID) |
|
| 168 | 168 | ); |
| 169 | 169 | // Perform the query |
| 170 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 170 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 171 | 171 | |
| 172 | 172 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 173 | - if ( 0 < $meta_instances ) { |
|
| 173 | + if (0 < $meta_instances) { |
|
| 174 | 174 | return true; |
| 175 | 175 | } |
| 176 | 176 | |
@@ -187,9 +187,9 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return true if the uri internal to the current dataset otherwise false. |
| 189 | 189 | */ |
| 190 | - public function is_internal_uri( $uri ) { |
|
| 190 | + public function is_internal_uri($uri) { |
|
| 191 | 191 | |
| 192 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 192 | + return (0 === strrpos($uri, wl_configuration_get_redlink_dataset_uri())); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | /** |
@@ -201,10 +201,10 @@ discard block |
||
| 201 | 201 | * |
| 202 | 202 | * @return WP_Post|null A WP_Post instance or null if not found. |
| 203 | 203 | */ |
| 204 | - public function get_entity_post_by_uri( $uri ) { |
|
| 204 | + public function get_entity_post_by_uri($uri) { |
|
| 205 | 205 | |
| 206 | 206 | // Check if we've been provided with a value otherwise return null. |
| 207 | - if ( empty( $uri ) ) { |
|
| 207 | + if (empty($uri)) { |
|
| 208 | 208 | return null; |
| 209 | 209 | } |
| 210 | 210 | |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | // performed also looking at sameAs values. |
| 226 | 226 | // |
| 227 | 227 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 228 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 228 | + if ( ! $this->is_internal_uri($uri)) { |
|
| 229 | 229 | |
| 230 | 230 | $query_args['meta_query']['relation'] = 'OR'; |
| 231 | 231 | $query_args['meta_query'][] = array( |
@@ -235,13 +235,13 @@ discard block |
||
| 235 | 235 | ); |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | - $query = new WP_Query( $query_args ); |
|
| 238 | + $query = new WP_Query($query_args); |
|
| 239 | 239 | |
| 240 | 240 | // Get the matching entity posts. |
| 241 | 241 | $posts = $query->get_posts(); |
| 242 | 242 | |
| 243 | 243 | // Return null if no post is found. |
| 244 | - if ( 0 === count( $posts ) ) { |
|
| 244 | + if (0 === count($posts)) { |
|
| 245 | 245 | return null; |
| 246 | 246 | } |
| 247 | 247 | |
@@ -261,11 +261,11 @@ discard block |
||
| 261 | 261 | * @param WP_Post $post Post object. |
| 262 | 262 | * @param bool $update Whether this is an existing post being updated or not. |
| 263 | 263 | */ |
| 264 | - public function save_post( $post_id, $post, $update ) { |
|
| 264 | + public function save_post($post_id, $post, $update) { |
|
| 265 | 265 | |
| 266 | 266 | // Avoid doing anything if post is autosave or a revision. |
| 267 | 267 | |
| 268 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 268 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 269 | 269 | return; |
| 270 | 270 | } |
| 271 | 271 | |
@@ -275,15 +275,15 @@ discard block |
||
| 275 | 275 | // the $post_id in the save_post call matches the post id set in the request. |
| 276 | 276 | // |
| 277 | 277 | // If this is not the current post being saved or if it's not an entity, return. |
| 278 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 278 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 279 | 279 | return; |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | // Get the alt labels from the request (or empty array). |
| 283 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 283 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 284 | 284 | |
| 285 | 285 | // Set the alternative labels. |
| 286 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 286 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 287 | 287 | |
| 288 | 288 | } |
| 289 | 289 | |
@@ -295,22 +295,22 @@ discard block |
||
| 295 | 295 | * @param int $post_id The post id. |
| 296 | 296 | * @param array $alt_labels An array of labels. |
| 297 | 297 | */ |
| 298 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 298 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 299 | 299 | |
| 300 | 300 | // Force $alt_labels to be an array |
| 301 | - if ( ! is_array( $alt_labels ) ) { |
|
| 302 | - $alt_labels = array( $alt_labels ); |
|
| 301 | + if ( ! is_array($alt_labels)) { |
|
| 302 | + $alt_labels = array($alt_labels); |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 305 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 306 | 306 | |
| 307 | 307 | // Delete all the existing alternate labels. |
| 308 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 308 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 309 | 309 | |
| 310 | 310 | // Set the alternative labels. |
| 311 | - foreach ( $alt_labels as $alt_label ) { |
|
| 312 | - if ( ! empty( $alt_label ) ) { |
|
| 313 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 311 | + foreach ($alt_labels as $alt_label) { |
|
| 312 | + if ( ! empty($alt_label)) { |
|
| 313 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 314 | 314 | } |
| 315 | 315 | } |
| 316 | 316 | |
@@ -325,9 +325,9 @@ discard block |
||
| 325 | 325 | * |
| 326 | 326 | * @return mixed An array of alternative labels. |
| 327 | 327 | */ |
| 328 | - public function get_alternative_labels( $post_id ) { |
|
| 328 | + public function get_alternative_labels($post_id) { |
|
| 329 | 329 | |
| 330 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 330 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | /** |
@@ -337,25 +337,25 @@ discard block |
||
| 337 | 337 | * |
| 338 | 338 | * @param WP_Post $post Post object. |
| 339 | 339 | */ |
| 340 | - public function edit_form_before_permalink( $post ) { |
|
| 340 | + public function edit_form_before_permalink($post) { |
|
| 341 | 341 | |
| 342 | 342 | // If it's not an entity, return. |
| 343 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 343 | + if ( ! $this->is_entity($post->ID)) { |
|
| 344 | 344 | return; |
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | // Print the input template. |
| 348 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 348 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 349 | 349 | |
| 350 | 350 | // Print all the currently set alternative labels. |
| 351 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 351 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 352 | 352 | |
| 353 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 353 | + echo $this->get_alternative_label_input($alt_label); |
|
| 354 | 354 | |
| 355 | 355 | }; |
| 356 | 356 | |
| 357 | 357 | // Print the button. |
| 358 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 358 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 359 | 359 | |
| 360 | 360 | } |
| 361 | 361 | |
@@ -368,25 +368,25 @@ discard block |
||
| 368 | 368 | * |
| 369 | 369 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
| 370 | 370 | */ |
| 371 | - public function get_uri( $post_id ) { |
|
| 371 | + public function get_uri($post_id) { |
|
| 372 | 372 | |
| 373 | 373 | // If a null is given, nothing to do |
| 374 | - if ( null == $post_id ) { |
|
| 374 | + if (null == $post_id) { |
|
| 375 | 375 | return null; |
| 376 | 376 | } |
| 377 | 377 | |
| 378 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 378 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 379 | 379 | |
| 380 | 380 | // If the dataset uri is not properly configured, null is returned |
| 381 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 381 | + if ('' === wl_configuration_get_redlink_dataset_uri()) { |
|
| 382 | 382 | return null; |
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | // Set the URI if it isn't set yet. |
| 386 | - $post_status = get_post_status( $post_id ); |
|
| 387 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 388 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 389 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 386 | + $post_status = get_post_status($post_id); |
|
| 387 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 388 | + $uri = wl_build_entity_uri($post_id); |
|
| 389 | + wl_set_entity_uri($post_id, $uri); |
|
| 390 | 390 | } |
| 391 | 391 | |
| 392 | 392 | return $uri; |
@@ -402,9 +402,9 @@ discard block |
||
| 402 | 402 | * |
| 403 | 403 | * @return string The input HTML code. |
| 404 | 404 | */ |
| 405 | - private function get_alternative_label_input( $value = '' ) { |
|
| 405 | + private function get_alternative_label_input($value = '') { |
|
| 406 | 406 | |
| 407 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 407 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | /** |
@@ -416,7 +416,7 @@ discard block |
||
| 416 | 416 | */ |
| 417 | 417 | public function count() { |
| 418 | 418 | |
| 419 | - $count = wp_count_posts( self::TYPE_NAME ); |
|
| 419 | + $count = wp_count_posts(self::TYPE_NAME); |
|
| 420 | 420 | |
| 421 | 421 | return $count->publish; |
| 422 | 422 | } |
@@ -433,28 +433,28 @@ discard block |
||
| 433 | 433 | * |
| 434 | 434 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
| 435 | 435 | */ |
| 436 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 436 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 437 | 437 | |
| 438 | 438 | // Create an entity for the publisher. |
| 439 | - $post_id = wp_insert_post( array( |
|
| 439 | + $post_id = wp_insert_post(array( |
|
| 440 | 440 | 'post_type' => self::TYPE_NAME, |
| 441 | 441 | 'post_title' => $name, |
| 442 | 442 | 'post_status' => $status, |
| 443 | 443 | 'post_content' => '', |
| 444 | - ) ); |
|
| 444 | + )); |
|
| 445 | 445 | |
| 446 | 446 | // Return the error if any. |
| 447 | - if ( is_wp_error( $post_id ) ) { |
|
| 447 | + if (is_wp_error($post_id)) { |
|
| 448 | 448 | return $post_id; |
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | // Set the entity logo. |
| 452 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 453 | - set_post_thumbnail( $post_id, $logo ); |
|
| 452 | + if ($logo && is_numeric($logo)) { |
|
| 453 | + set_post_thumbnail($post_id, $logo); |
|
| 454 | 454 | } |
| 455 | 455 | |
| 456 | 456 | // Set the entity type. |
| 457 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 457 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 458 | 458 | |
| 459 | 459 | return $post_id; |
| 460 | 460 | } |
@@ -470,9 +470,9 @@ discard block |
||
| 470 | 470 | * |
| 471 | 471 | * @return array An array of post ids. |
| 472 | 472 | */ |
| 473 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 473 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 474 | 474 | |
| 475 | - return wl_core_inner_get_related_entities( 'post_ids', $id, null, $post_status ); |
|
| 475 | + return wl_core_inner_get_related_entities('post_ids', $id, null, $post_status); |
|
| 476 | 476 | } |
| 477 | 477 | |
| 478 | 478 | } |
@@ -16,148 +16,148 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Entity_Post_Type_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The entity post type. |
|
| 21 | - * |
|
| 22 | - * @since 3.6.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var string $post_type The entity post type. |
|
| 25 | - */ |
|
| 26 | - private $post_type; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The entity type slug. |
|
| 30 | - * |
|
| 31 | - * @since 3.6.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var string $slug The entity type slug. |
|
| 34 | - */ |
|
| 35 | - private $slug; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * A singleton instance of the entity type service. |
|
| 39 | - * |
|
| 40 | - * @since 3.6.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var Wordlift_Entity_Post_Type_Service |
|
| 43 | - */ |
|
| 44 | - private static $instance; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Create an entity type service instance. |
|
| 48 | - * |
|
| 49 | - * @since 3.6.0 |
|
| 50 | - * |
|
| 51 | - * @param string $post_type The post type, e.g. entity. |
|
| 52 | - * @param string $slug The entity type slug, if the slug is empty, the default slug will be used. |
|
| 53 | - */ |
|
| 54 | - public function __construct( $post_type, $slug ) { |
|
| 55 | - |
|
| 56 | - $this->post_type = $post_type; |
|
| 57 | - |
|
| 58 | - // We cannot assign an empty slug to the register_post_type function, therefore if the slug is empty we default |
|
| 59 | - // to the type name. |
|
| 60 | - $this->slug = $slug ?: $post_type; |
|
| 61 | - |
|
| 62 | - self::$instance = $this; |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Get the entity type service singleton instance. |
|
| 68 | - * |
|
| 69 | - * @since 3.6.0 |
|
| 70 | - * |
|
| 71 | - * @return Wordlift_Entity_Post_Type_Service The entity type service singleton instance. |
|
| 72 | - */ |
|
| 73 | - public static function get_instance() { |
|
| 74 | - |
|
| 75 | - return self::$instance; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Get the entity type slug. |
|
| 80 | - * |
|
| 81 | - * @since 3.6.0 |
|
| 82 | - * |
|
| 83 | - * @return string The entity type slug. |
|
| 84 | - */ |
|
| 85 | - public function get_slug() { |
|
| 86 | - |
|
| 87 | - return $this->slug; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Get the entity post type name. |
|
| 92 | - * |
|
| 93 | - * @since 3.6.0 |
|
| 94 | - * |
|
| 95 | - * @return string The entity post type. |
|
| 96 | - */ |
|
| 97 | - public function get_post_type() { |
|
| 98 | - |
|
| 99 | - return $this->post_type; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Register the WordLift entity post type. This method is hooked to WordPress' init action. |
|
| 105 | - * |
|
| 106 | - * @since 3.6.0 |
|
| 107 | - */ |
|
| 108 | - public function register() { |
|
| 109 | - |
|
| 110 | - $labels = array( |
|
| 111 | - 'name' => _x( 'Vocabulary', 'post type general name', 'wordlift' ), |
|
| 112 | - 'singular_name' => _x( 'Entity', 'post type singular name', 'wordlift' ), |
|
| 113 | - 'add_new' => _x( 'Add New Entity', 'entity', 'wordlift' ), |
|
| 114 | - 'add_new_item' => __( 'Add New Entity', 'wordlift' ), |
|
| 115 | - 'edit_item' => __( 'Edit Entity', 'wordlift' ), |
|
| 116 | - 'new_item' => __( 'New Entity', 'wordlift' ), |
|
| 117 | - 'all_items' => __( 'All Entities', 'wordlift' ), |
|
| 118 | - 'view_item' => __( 'View Entity', 'wordlift' ), |
|
| 119 | - 'search_items' => __( 'Search in Vocabulary', 'wordlift' ), |
|
| 120 | - 'not_found' => __( 'No entities found', 'wordlift' ), |
|
| 121 | - 'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ), |
|
| 122 | - 'parent_item_colon' => '', |
|
| 123 | - 'menu_name' => __( 'Vocabulary', 'wordlift' ), |
|
| 124 | - ); |
|
| 125 | - |
|
| 126 | - $args = array( |
|
| 127 | - 'labels' => $labels, |
|
| 128 | - 'description' => 'Holds our vocabulary (set of entities) and entity specific data', |
|
| 129 | - 'public' => true, |
|
| 130 | - 'menu_position' => 20, |
|
| 131 | - // after the pages menu. |
|
| 132 | - // Add support for 'authors' and 'revisions': |
|
| 133 | - // * see https://github.com/insideout10/wordlift-plugin/issues/395 |
|
| 134 | - // * see https://github.com/insideout10/wordlift-plugin/issues/376 |
|
| 135 | - 'supports' => array( |
|
| 136 | - 'title', |
|
| 137 | - 'editor', |
|
| 138 | - 'thumbnail', |
|
| 139 | - 'excerpt', |
|
| 140 | - 'comments', |
|
| 141 | - 'author', |
|
| 142 | - 'revisions', |
|
| 143 | - ), |
|
| 144 | - 'has_archive' => true, |
|
| 145 | - 'menu_icon' => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
| 146 | - // Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service. |
|
| 147 | - 'rewrite' => array( 'slug' => $this->slug ), |
|
| 148 | - ); |
|
| 149 | - |
|
| 150 | - register_post_type( $this->post_type, $args ); |
|
| 151 | - |
|
| 152 | - // Enable WP's standard `category` taxonomy for entities. |
|
| 153 | - // |
|
| 154 | - // While this enables editors to bind entities to the WP posts' category |
|
| 155 | - // taxonomy, in Wordlift_Category_Taxonomy_Service we also need to alter |
|
| 156 | - // WP's main category query to include the `entity` post type. |
|
| 157 | - // |
|
| 158 | - // See https://github.com/insideout10/wordlift-plugin/issues/442 |
|
| 159 | - register_taxonomy_for_object_type( 'category', $this->post_type ); |
|
| 160 | - |
|
| 161 | - } |
|
| 19 | + /** |
|
| 20 | + * The entity post type. |
|
| 21 | + * |
|
| 22 | + * @since 3.6.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var string $post_type The entity post type. |
|
| 25 | + */ |
|
| 26 | + private $post_type; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The entity type slug. |
|
| 30 | + * |
|
| 31 | + * @since 3.6.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var string $slug The entity type slug. |
|
| 34 | + */ |
|
| 35 | + private $slug; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * A singleton instance of the entity type service. |
|
| 39 | + * |
|
| 40 | + * @since 3.6.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var Wordlift_Entity_Post_Type_Service |
|
| 43 | + */ |
|
| 44 | + private static $instance; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Create an entity type service instance. |
|
| 48 | + * |
|
| 49 | + * @since 3.6.0 |
|
| 50 | + * |
|
| 51 | + * @param string $post_type The post type, e.g. entity. |
|
| 52 | + * @param string $slug The entity type slug, if the slug is empty, the default slug will be used. |
|
| 53 | + */ |
|
| 54 | + public function __construct( $post_type, $slug ) { |
|
| 55 | + |
|
| 56 | + $this->post_type = $post_type; |
|
| 57 | + |
|
| 58 | + // We cannot assign an empty slug to the register_post_type function, therefore if the slug is empty we default |
|
| 59 | + // to the type name. |
|
| 60 | + $this->slug = $slug ?: $post_type; |
|
| 61 | + |
|
| 62 | + self::$instance = $this; |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Get the entity type service singleton instance. |
|
| 68 | + * |
|
| 69 | + * @since 3.6.0 |
|
| 70 | + * |
|
| 71 | + * @return Wordlift_Entity_Post_Type_Service The entity type service singleton instance. |
|
| 72 | + */ |
|
| 73 | + public static function get_instance() { |
|
| 74 | + |
|
| 75 | + return self::$instance; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Get the entity type slug. |
|
| 80 | + * |
|
| 81 | + * @since 3.6.0 |
|
| 82 | + * |
|
| 83 | + * @return string The entity type slug. |
|
| 84 | + */ |
|
| 85 | + public function get_slug() { |
|
| 86 | + |
|
| 87 | + return $this->slug; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Get the entity post type name. |
|
| 92 | + * |
|
| 93 | + * @since 3.6.0 |
|
| 94 | + * |
|
| 95 | + * @return string The entity post type. |
|
| 96 | + */ |
|
| 97 | + public function get_post_type() { |
|
| 98 | + |
|
| 99 | + return $this->post_type; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Register the WordLift entity post type. This method is hooked to WordPress' init action. |
|
| 105 | + * |
|
| 106 | + * @since 3.6.0 |
|
| 107 | + */ |
|
| 108 | + public function register() { |
|
| 109 | + |
|
| 110 | + $labels = array( |
|
| 111 | + 'name' => _x( 'Vocabulary', 'post type general name', 'wordlift' ), |
|
| 112 | + 'singular_name' => _x( 'Entity', 'post type singular name', 'wordlift' ), |
|
| 113 | + 'add_new' => _x( 'Add New Entity', 'entity', 'wordlift' ), |
|
| 114 | + 'add_new_item' => __( 'Add New Entity', 'wordlift' ), |
|
| 115 | + 'edit_item' => __( 'Edit Entity', 'wordlift' ), |
|
| 116 | + 'new_item' => __( 'New Entity', 'wordlift' ), |
|
| 117 | + 'all_items' => __( 'All Entities', 'wordlift' ), |
|
| 118 | + 'view_item' => __( 'View Entity', 'wordlift' ), |
|
| 119 | + 'search_items' => __( 'Search in Vocabulary', 'wordlift' ), |
|
| 120 | + 'not_found' => __( 'No entities found', 'wordlift' ), |
|
| 121 | + 'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ), |
|
| 122 | + 'parent_item_colon' => '', |
|
| 123 | + 'menu_name' => __( 'Vocabulary', 'wordlift' ), |
|
| 124 | + ); |
|
| 125 | + |
|
| 126 | + $args = array( |
|
| 127 | + 'labels' => $labels, |
|
| 128 | + 'description' => 'Holds our vocabulary (set of entities) and entity specific data', |
|
| 129 | + 'public' => true, |
|
| 130 | + 'menu_position' => 20, |
|
| 131 | + // after the pages menu. |
|
| 132 | + // Add support for 'authors' and 'revisions': |
|
| 133 | + // * see https://github.com/insideout10/wordlift-plugin/issues/395 |
|
| 134 | + // * see https://github.com/insideout10/wordlift-plugin/issues/376 |
|
| 135 | + 'supports' => array( |
|
| 136 | + 'title', |
|
| 137 | + 'editor', |
|
| 138 | + 'thumbnail', |
|
| 139 | + 'excerpt', |
|
| 140 | + 'comments', |
|
| 141 | + 'author', |
|
| 142 | + 'revisions', |
|
| 143 | + ), |
|
| 144 | + 'has_archive' => true, |
|
| 145 | + 'menu_icon' => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
| 146 | + // Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service. |
|
| 147 | + 'rewrite' => array( 'slug' => $this->slug ), |
|
| 148 | + ); |
|
| 149 | + |
|
| 150 | + register_post_type( $this->post_type, $args ); |
|
| 151 | + |
|
| 152 | + // Enable WP's standard `category` taxonomy for entities. |
|
| 153 | + // |
|
| 154 | + // While this enables editors to bind entities to the WP posts' category |
|
| 155 | + // taxonomy, in Wordlift_Category_Taxonomy_Service we also need to alter |
|
| 156 | + // WP's main category query to include the `entity` post type. |
|
| 157 | + // |
|
| 158 | + // See https://github.com/insideout10/wordlift-plugin/issues/442 |
|
| 159 | + register_taxonomy_for_object_type( 'category', $this->post_type ); |
|
| 160 | + |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | 163 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | * @param string $post_type The post type, e.g. entity. |
| 52 | 52 | * @param string $slug The entity type slug, if the slug is empty, the default slug will be used. |
| 53 | 53 | */ |
| 54 | - public function __construct( $post_type, $slug ) { |
|
| 54 | + public function __construct($post_type, $slug) { |
|
| 55 | 55 | |
| 56 | 56 | $this->post_type = $post_type; |
| 57 | 57 | |
@@ -108,19 +108,19 @@ discard block |
||
| 108 | 108 | public function register() { |
| 109 | 109 | |
| 110 | 110 | $labels = array( |
| 111 | - 'name' => _x( 'Vocabulary', 'post type general name', 'wordlift' ), |
|
| 112 | - 'singular_name' => _x( 'Entity', 'post type singular name', 'wordlift' ), |
|
| 113 | - 'add_new' => _x( 'Add New Entity', 'entity', 'wordlift' ), |
|
| 114 | - 'add_new_item' => __( 'Add New Entity', 'wordlift' ), |
|
| 115 | - 'edit_item' => __( 'Edit Entity', 'wordlift' ), |
|
| 116 | - 'new_item' => __( 'New Entity', 'wordlift' ), |
|
| 117 | - 'all_items' => __( 'All Entities', 'wordlift' ), |
|
| 118 | - 'view_item' => __( 'View Entity', 'wordlift' ), |
|
| 119 | - 'search_items' => __( 'Search in Vocabulary', 'wordlift' ), |
|
| 120 | - 'not_found' => __( 'No entities found', 'wordlift' ), |
|
| 121 | - 'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ), |
|
| 111 | + 'name' => _x('Vocabulary', 'post type general name', 'wordlift'), |
|
| 112 | + 'singular_name' => _x('Entity', 'post type singular name', 'wordlift'), |
|
| 113 | + 'add_new' => _x('Add New Entity', 'entity', 'wordlift'), |
|
| 114 | + 'add_new_item' => __('Add New Entity', 'wordlift'), |
|
| 115 | + 'edit_item' => __('Edit Entity', 'wordlift'), |
|
| 116 | + 'new_item' => __('New Entity', 'wordlift'), |
|
| 117 | + 'all_items' => __('All Entities', 'wordlift'), |
|
| 118 | + 'view_item' => __('View Entity', 'wordlift'), |
|
| 119 | + 'search_items' => __('Search in Vocabulary', 'wordlift'), |
|
| 120 | + 'not_found' => __('No entities found', 'wordlift'), |
|
| 121 | + 'not_found_in_trash' => __('No entities found in the Trash', 'wordlift'), |
|
| 122 | 122 | 'parent_item_colon' => '', |
| 123 | - 'menu_name' => __( 'Vocabulary', 'wordlift' ), |
|
| 123 | + 'menu_name' => __('Vocabulary', 'wordlift'), |
|
| 124 | 124 | ); |
| 125 | 125 | |
| 126 | 126 | $args = array( |
@@ -142,12 +142,12 @@ discard block |
||
| 142 | 142 | 'revisions', |
| 143 | 143 | ), |
| 144 | 144 | 'has_archive' => true, |
| 145 | - 'menu_icon' => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
| 145 | + 'menu_icon' => WP_CONTENT_URL.'/plugins/wordlift/images/svg/wl-vocabulary-icon.svg', |
|
| 146 | 146 | // Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service. |
| 147 | - 'rewrite' => array( 'slug' => $this->slug ), |
|
| 147 | + 'rewrite' => array('slug' => $this->slug), |
|
| 148 | 148 | ); |
| 149 | 149 | |
| 150 | - register_post_type( $this->post_type, $args ); |
|
| 150 | + register_post_type($this->post_type, $args); |
|
| 151 | 151 | |
| 152 | 152 | // Enable WP's standard `category` taxonomy for entities. |
| 153 | 153 | // |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | // WP's main category query to include the `entity` post type. |
| 157 | 157 | // |
| 158 | 158 | // See https://github.com/insideout10/wordlift-plugin/issues/442 |
| 159 | - register_taxonomy_for_object_type( 'category', $this->post_type ); |
|
| 159 | + register_taxonomy_for_object_type('category', $this->post_type); |
|
| 160 | 160 | |
| 161 | 161 | } |
| 162 | 162 | |