@@ -26,182 +26,182 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | class Wordlift_Entity_Link_Service { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The entity type service. |
|
| 31 | - * |
|
| 32 | - * @since 3.6.0 |
|
| 33 | - * @access private |
|
| 34 | - * @var Wordlift_Entity_Post_Type_Service $entity_type_service The entity type service. |
|
| 35 | - */ |
|
| 36 | - private $entity_type_service; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The entity post type slug. |
|
| 40 | - * |
|
| 41 | - * @since 3.6.0 |
|
| 42 | - * @access private |
|
| 43 | - * @var string $slug The entity post type slug. |
|
| 44 | - */ |
|
| 45 | - private $slug; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * A logger instance. |
|
| 49 | - * |
|
| 50 | - * @since 3.6.0 |
|
| 51 | - * @access private |
|
| 52 | - * @var Wordlift_Log_Service |
|
| 53 | - */ |
|
| 54 | - private $log; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Wordlift_Entity_Link_Service constructor. |
|
| 58 | - * |
|
| 59 | - * @since 3.6.0 |
|
| 60 | - * |
|
| 61 | - * @param Wordlift_Entity_Post_Type_Service $entity_type_service |
|
| 62 | - * @param string $slug The entity post type slug. |
|
| 63 | - */ |
|
| 64 | - public function __construct( $entity_type_service, $slug ) { |
|
| 65 | - |
|
| 66 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' ); |
|
| 67 | - |
|
| 68 | - $this->entity_type_service = $entity_type_service; |
|
| 69 | - $this->slug = $slug; |
|
| 70 | - |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Intercept link generation to posts in order to customize links to entities. |
|
| 75 | - * |
|
| 76 | - * @since 3.6.0 |
|
| 77 | - * |
|
| 78 | - * @param string $post_link The post's permalink. |
|
| 79 | - * @param WP_Post $post The post in question. |
|
| 80 | - * @param bool $leavename Whether to keep the post name. |
|
| 81 | - * @param bool $sample Is it a sample permalink. |
|
| 82 | - * |
|
| 83 | - * @return string The link to the post. |
|
| 84 | - */ |
|
| 85 | - public function post_type_link( $post_link, $post, $leavename, $sample ) { |
|
| 86 | - |
|
| 87 | - // Return the post link if this is not our post type. |
|
| 88 | - if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) { |
|
| 89 | - return $post_link; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // Replace /slug/post_name/ with /post_name/ |
|
| 93 | - // The slug comes from the Entity Type Service since that service is responsible for registering the default |
|
| 94 | - // slug. |
|
| 95 | - return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link ); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Alter the query to look for our own custom type. |
|
| 100 | - * |
|
| 101 | - * @since 3.6.0 |
|
| 102 | - * |
|
| 103 | - * @param WP_Query $query |
|
| 104 | - */ |
|
| 105 | - public function pre_get_posts( $query ) { |
|
| 106 | - |
|
| 107 | - // If a slug has been set, we don't need to alter the query. |
|
| 108 | - if ( ! empty( $this->slug ) ) { |
|
| 109 | - return; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // Check if it's a query we should extend with our own custom post type. |
|
| 113 | - // |
|
| 114 | - // The `$query->query` count could be > 2 if the preview parameter is passed too. |
|
| 115 | - // |
|
| 116 | - // See https://github.com/insideout10/wordlift-plugin/issues/439 |
|
| 117 | - if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) { |
|
| 118 | - return; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // Add our own post type to the query. |
|
| 122 | - $post_type = is_array( $query->get( 'post_type' ) ) ? $query->get( 'post_type' ) : array(); |
|
| 123 | - $query->set( 'post_type', array_merge( $post_type, array( |
|
| 124 | - 'post', |
|
| 125 | - $this->entity_type_service->get_post_type(), |
|
| 126 | - 'page', |
|
| 127 | - ) ) ); |
|
| 128 | - |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Hook to WordPress' wp_unique_post_slug_is_bad_flat_slug filter. This is called when a page is saved. |
|
| 133 | - * |
|
| 134 | - * @since 3.6.0 |
|
| 135 | - * |
|
| 136 | - * @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|
| 137 | - * @param string $slug The post slug. |
|
| 138 | - * @param string $post_type Post type. |
|
| 139 | - * |
|
| 140 | - * @return bool Whether the slug is bad. |
|
| 141 | - */ |
|
| 142 | - public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) { |
|
| 143 | - |
|
| 144 | - // The list of post types that might have conflicting slugs. |
|
| 145 | - $post_types = array( |
|
| 146 | - 'post', |
|
| 147 | - 'page', |
|
| 148 | - $this->entity_type_service->get_post_type(), |
|
| 149 | - ); |
|
| 150 | - |
|
| 151 | - // Ignore post types different from the ones we need to check. |
|
| 152 | - if ( ! in_array( $post_type, $post_types ) ) { |
|
| 153 | - return $bad_slug; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) ); |
|
| 157 | - |
|
| 158 | - $this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? "yes" : "no" ) . " ]" ); |
|
| 159 | - |
|
| 160 | - return $exists; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Hook to WordPress' wp_unique_post_slug_is_bad_hierarchical_slug filter. This is called when a page is saved. |
|
| 165 | - * |
|
| 166 | - * @since 3.6.0 |
|
| 167 | - * |
|
| 168 | - * @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|
| 169 | - * @param string $slug The post slug. |
|
| 170 | - * @param string $post_type Post type. |
|
| 171 | - * @param int $post_parent |
|
| 172 | - * |
|
| 173 | - * @return bool Whether the slug is bad. |
|
| 174 | - */ |
|
| 175 | - public function wp_unique_post_slug_is_bad_hierarchical_slug( $bad_slug, $slug, $post_type, $post_parent ) { |
|
| 176 | - |
|
| 177 | - // We only care about pages here. |
|
| 178 | - if ( 'page' !== $post_type ) { |
|
| 179 | - return $bad_slug; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - // We redirect the call to the flat hook, this means that this check is going to solve also the 6-years old issue |
|
| 183 | - // about overlapping slugs among pages and posts: |
|
| 184 | - // https://core.trac.wordpress.org/ticket/13459 |
|
| 185 | - return $this->wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Check whether a slug exists already for the specified post types. |
|
| 190 | - * |
|
| 191 | - * @since 3.6.0 |
|
| 192 | - * |
|
| 193 | - * @param string $slug The slug. |
|
| 194 | - * @param array $post_types An array of post types. |
|
| 195 | - * |
|
| 196 | - * @return bool True if the slug exists, otherwise false. |
|
| 197 | - */ |
|
| 198 | - private function slug_exists( $slug, $post_types ) { |
|
| 199 | - global $wpdb; |
|
| 200 | - |
|
| 201 | - // Post slugs must be unique across all posts. |
|
| 202 | - $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ('" . implode( "', '", array_map( 'esc_sql', $post_types ) ) . "') LIMIT 1"; |
|
| 203 | - |
|
| 204 | - return null !== $wpdb->get_var( $wpdb->prepare( $check_sql, $slug ) ); |
|
| 205 | - } |
|
| 29 | + /** |
|
| 30 | + * The entity type service. |
|
| 31 | + * |
|
| 32 | + * @since 3.6.0 |
|
| 33 | + * @access private |
|
| 34 | + * @var Wordlift_Entity_Post_Type_Service $entity_type_service The entity type service. |
|
| 35 | + */ |
|
| 36 | + private $entity_type_service; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The entity post type slug. |
|
| 40 | + * |
|
| 41 | + * @since 3.6.0 |
|
| 42 | + * @access private |
|
| 43 | + * @var string $slug The entity post type slug. |
|
| 44 | + */ |
|
| 45 | + private $slug; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * A logger instance. |
|
| 49 | + * |
|
| 50 | + * @since 3.6.0 |
|
| 51 | + * @access private |
|
| 52 | + * @var Wordlift_Log_Service |
|
| 53 | + */ |
|
| 54 | + private $log; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Wordlift_Entity_Link_Service constructor. |
|
| 58 | + * |
|
| 59 | + * @since 3.6.0 |
|
| 60 | + * |
|
| 61 | + * @param Wordlift_Entity_Post_Type_Service $entity_type_service |
|
| 62 | + * @param string $slug The entity post type slug. |
|
| 63 | + */ |
|
| 64 | + public function __construct( $entity_type_service, $slug ) { |
|
| 65 | + |
|
| 66 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' ); |
|
| 67 | + |
|
| 68 | + $this->entity_type_service = $entity_type_service; |
|
| 69 | + $this->slug = $slug; |
|
| 70 | + |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Intercept link generation to posts in order to customize links to entities. |
|
| 75 | + * |
|
| 76 | + * @since 3.6.0 |
|
| 77 | + * |
|
| 78 | + * @param string $post_link The post's permalink. |
|
| 79 | + * @param WP_Post $post The post in question. |
|
| 80 | + * @param bool $leavename Whether to keep the post name. |
|
| 81 | + * @param bool $sample Is it a sample permalink. |
|
| 82 | + * |
|
| 83 | + * @return string The link to the post. |
|
| 84 | + */ |
|
| 85 | + public function post_type_link( $post_link, $post, $leavename, $sample ) { |
|
| 86 | + |
|
| 87 | + // Return the post link if this is not our post type. |
|
| 88 | + if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) { |
|
| 89 | + return $post_link; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // Replace /slug/post_name/ with /post_name/ |
|
| 93 | + // The slug comes from the Entity Type Service since that service is responsible for registering the default |
|
| 94 | + // slug. |
|
| 95 | + return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link ); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Alter the query to look for our own custom type. |
|
| 100 | + * |
|
| 101 | + * @since 3.6.0 |
|
| 102 | + * |
|
| 103 | + * @param WP_Query $query |
|
| 104 | + */ |
|
| 105 | + public function pre_get_posts( $query ) { |
|
| 106 | + |
|
| 107 | + // If a slug has been set, we don't need to alter the query. |
|
| 108 | + if ( ! empty( $this->slug ) ) { |
|
| 109 | + return; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // Check if it's a query we should extend with our own custom post type. |
|
| 113 | + // |
|
| 114 | + // The `$query->query` count could be > 2 if the preview parameter is passed too. |
|
| 115 | + // |
|
| 116 | + // See https://github.com/insideout10/wordlift-plugin/issues/439 |
|
| 117 | + if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) { |
|
| 118 | + return; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // Add our own post type to the query. |
|
| 122 | + $post_type = is_array( $query->get( 'post_type' ) ) ? $query->get( 'post_type' ) : array(); |
|
| 123 | + $query->set( 'post_type', array_merge( $post_type, array( |
|
| 124 | + 'post', |
|
| 125 | + $this->entity_type_service->get_post_type(), |
|
| 126 | + 'page', |
|
| 127 | + ) ) ); |
|
| 128 | + |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Hook to WordPress' wp_unique_post_slug_is_bad_flat_slug filter. This is called when a page is saved. |
|
| 133 | + * |
|
| 134 | + * @since 3.6.0 |
|
| 135 | + * |
|
| 136 | + * @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|
| 137 | + * @param string $slug The post slug. |
|
| 138 | + * @param string $post_type Post type. |
|
| 139 | + * |
|
| 140 | + * @return bool Whether the slug is bad. |
|
| 141 | + */ |
|
| 142 | + public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) { |
|
| 143 | + |
|
| 144 | + // The list of post types that might have conflicting slugs. |
|
| 145 | + $post_types = array( |
|
| 146 | + 'post', |
|
| 147 | + 'page', |
|
| 148 | + $this->entity_type_service->get_post_type(), |
|
| 149 | + ); |
|
| 150 | + |
|
| 151 | + // Ignore post types different from the ones we need to check. |
|
| 152 | + if ( ! in_array( $post_type, $post_types ) ) { |
|
| 153 | + return $bad_slug; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) ); |
|
| 157 | + |
|
| 158 | + $this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? "yes" : "no" ) . " ]" ); |
|
| 159 | + |
|
| 160 | + return $exists; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Hook to WordPress' wp_unique_post_slug_is_bad_hierarchical_slug filter. This is called when a page is saved. |
|
| 165 | + * |
|
| 166 | + * @since 3.6.0 |
|
| 167 | + * |
|
| 168 | + * @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|
| 169 | + * @param string $slug The post slug. |
|
| 170 | + * @param string $post_type Post type. |
|
| 171 | + * @param int $post_parent |
|
| 172 | + * |
|
| 173 | + * @return bool Whether the slug is bad. |
|
| 174 | + */ |
|
| 175 | + public function wp_unique_post_slug_is_bad_hierarchical_slug( $bad_slug, $slug, $post_type, $post_parent ) { |
|
| 176 | + |
|
| 177 | + // We only care about pages here. |
|
| 178 | + if ( 'page' !== $post_type ) { |
|
| 179 | + return $bad_slug; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + // We redirect the call to the flat hook, this means that this check is going to solve also the 6-years old issue |
|
| 183 | + // about overlapping slugs among pages and posts: |
|
| 184 | + // https://core.trac.wordpress.org/ticket/13459 |
|
| 185 | + return $this->wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Check whether a slug exists already for the specified post types. |
|
| 190 | + * |
|
| 191 | + * @since 3.6.0 |
|
| 192 | + * |
|
| 193 | + * @param string $slug The slug. |
|
| 194 | + * @param array $post_types An array of post types. |
|
| 195 | + * |
|
| 196 | + * @return bool True if the slug exists, otherwise false. |
|
| 197 | + */ |
|
| 198 | + private function slug_exists( $slug, $post_types ) { |
|
| 199 | + global $wpdb; |
|
| 200 | + |
|
| 201 | + // Post slugs must be unique across all posts. |
|
| 202 | + $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ('" . implode( "', '", array_map( 'esc_sql', $post_types ) ) . "') LIMIT 1"; |
|
| 203 | + |
|
| 204 | + return null !== $wpdb->get_var( $wpdb->prepare( $check_sql, $slug ) ); |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | 207 | } |
@@ -61,9 +61,9 @@ discard block |
||
| 61 | 61 | * @param Wordlift_Entity_Post_Type_Service $entity_type_service |
| 62 | 62 | * @param string $slug The entity post type slug. |
| 63 | 63 | */ |
| 64 | - public function __construct( $entity_type_service, $slug ) { |
|
| 64 | + public function __construct($entity_type_service, $slug) { |
|
| 65 | 65 | |
| 66 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' ); |
|
| 66 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Link_Service'); |
|
| 67 | 67 | |
| 68 | 68 | $this->entity_type_service = $entity_type_service; |
| 69 | 69 | $this->slug = $slug; |
@@ -82,17 +82,17 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @return string The link to the post. |
| 84 | 84 | */ |
| 85 | - public function post_type_link( $post_link, $post, $leavename, $sample ) { |
|
| 85 | + public function post_type_link($post_link, $post, $leavename, $sample) { |
|
| 86 | 86 | |
| 87 | 87 | // Return the post link if this is not our post type. |
| 88 | - if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) { |
|
| 88 | + if ( ! empty($this->slug) || $this->entity_type_service->get_post_type() !== get_post_type($post)) { |
|
| 89 | 89 | return $post_link; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | // Replace /slug/post_name/ with /post_name/ |
| 93 | 93 | // The slug comes from the Entity Type Service since that service is responsible for registering the default |
| 94 | 94 | // slug. |
| 95 | - return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link ); |
|
| 95 | + return str_replace("/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
@@ -102,10 +102,10 @@ discard block |
||
| 102 | 102 | * |
| 103 | 103 | * @param WP_Query $query |
| 104 | 104 | */ |
| 105 | - public function pre_get_posts( $query ) { |
|
| 105 | + public function pre_get_posts($query) { |
|
| 106 | 106 | |
| 107 | 107 | // If a slug has been set, we don't need to alter the query. |
| 108 | - if ( ! empty( $this->slug ) ) { |
|
| 108 | + if ( ! empty($this->slug)) { |
|
| 109 | 109 | return; |
| 110 | 110 | } |
| 111 | 111 | |
@@ -114,17 +114,17 @@ discard block |
||
| 114 | 114 | // The `$query->query` count could be > 2 if the preview parameter is passed too. |
| 115 | 115 | // |
| 116 | 116 | // See https://github.com/insideout10/wordlift-plugin/issues/439 |
| 117 | - if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) { |
|
| 117 | + if ( ! $query->is_main_query() || 2 > count($query->query) || ! isset($query->query['page']) || empty($query->query['name'])) { |
|
| 118 | 118 | return; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | // Add our own post type to the query. |
| 122 | - $post_type = is_array( $query->get( 'post_type' ) ) ? $query->get( 'post_type' ) : array(); |
|
| 123 | - $query->set( 'post_type', array_merge( $post_type, array( |
|
| 122 | + $post_type = is_array($query->get('post_type')) ? $query->get('post_type') : array(); |
|
| 123 | + $query->set('post_type', array_merge($post_type, array( |
|
| 124 | 124 | 'post', |
| 125 | 125 | $this->entity_type_service->get_post_type(), |
| 126 | 126 | 'page', |
| 127 | - ) ) ); |
|
| 127 | + ))); |
|
| 128 | 128 | |
| 129 | 129 | } |
| 130 | 130 | |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | * |
| 140 | 140 | * @return bool Whether the slug is bad. |
| 141 | 141 | */ |
| 142 | - public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) { |
|
| 142 | + public function wp_unique_post_slug_is_bad_flat_slug($bad_slug, $slug, $post_type) { |
|
| 143 | 143 | |
| 144 | 144 | // The list of post types that might have conflicting slugs. |
| 145 | 145 | $post_types = array( |
@@ -149,13 +149,13 @@ discard block |
||
| 149 | 149 | ); |
| 150 | 150 | |
| 151 | 151 | // Ignore post types different from the ones we need to check. |
| 152 | - if ( ! in_array( $post_type, $post_types ) ) { |
|
| 152 | + if ( ! in_array($post_type, $post_types)) { |
|
| 153 | 153 | return $bad_slug; |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - $exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) ); |
|
| 156 | + $exists = $this->slug_exists($slug, array_diff($post_types, array($post_type))); |
|
| 157 | 157 | |
| 158 | - $this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? "yes" : "no" ) . " ]" ); |
|
| 158 | + $this->log->debug("Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: ".($exists ? "yes" : "no")." ]"); |
|
| 159 | 159 | |
| 160 | 160 | return $exists; |
| 161 | 161 | } |
@@ -172,17 +172,17 @@ discard block |
||
| 172 | 172 | * |
| 173 | 173 | * @return bool Whether the slug is bad. |
| 174 | 174 | */ |
| 175 | - public function wp_unique_post_slug_is_bad_hierarchical_slug( $bad_slug, $slug, $post_type, $post_parent ) { |
|
| 175 | + public function wp_unique_post_slug_is_bad_hierarchical_slug($bad_slug, $slug, $post_type, $post_parent) { |
|
| 176 | 176 | |
| 177 | 177 | // We only care about pages here. |
| 178 | - if ( 'page' !== $post_type ) { |
|
| 178 | + if ('page' !== $post_type) { |
|
| 179 | 179 | return $bad_slug; |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | // We redirect the call to the flat hook, this means that this check is going to solve also the 6-years old issue |
| 183 | 183 | // about overlapping slugs among pages and posts: |
| 184 | 184 | // https://core.trac.wordpress.org/ticket/13459 |
| 185 | - return $this->wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ); |
|
| 185 | + return $this->wp_unique_post_slug_is_bad_flat_slug($bad_slug, $slug, $post_type); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /** |
@@ -195,13 +195,13 @@ discard block |
||
| 195 | 195 | * |
| 196 | 196 | * @return bool True if the slug exists, otherwise false. |
| 197 | 197 | */ |
| 198 | - private function slug_exists( $slug, $post_types ) { |
|
| 198 | + private function slug_exists($slug, $post_types) { |
|
| 199 | 199 | global $wpdb; |
| 200 | 200 | |
| 201 | 201 | // Post slugs must be unique across all posts. |
| 202 | - $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ('" . implode( "', '", array_map( 'esc_sql', $post_types ) ) . "') LIMIT 1"; |
|
| 202 | + $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ('".implode("', '", array_map('esc_sql', $post_types))."') LIMIT 1"; |
|
| 203 | 203 | |
| 204 | - return null !== $wpdb->get_var( $wpdb->prepare( $check_sql, $slug ) ); |
|
| 204 | + return null !== $wpdb->get_var($wpdb->prepare($check_sql, $slug)); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | } |
@@ -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 create |
|
| 74 | - // 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 create |
|
| 74 | + // 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_Entity_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_Entity_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,13 +186,13 @@ discard block |
||
| 186 | 186 | */ |
| 187 | 187 | function wordlift_save_post_add_default_schema_type( $entity_id ) { |
| 188 | 188 | |
| 189 | - $entity = get_post( $entity_id ); |
|
| 190 | - $entity_type = wl_schema_get_types( $entity_id ); |
|
| 189 | + $entity = get_post( $entity_id ); |
|
| 190 | + $entity_type = wl_schema_get_types( $entity_id ); |
|
| 191 | 191 | |
| 192 | - // Assign type 'Thing' if we are dealing with an entity without type |
|
| 193 | - if ( $entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null( $entity_type ) ) { |
|
| 194 | - wl_schema_set_types( $entity_id, 'Thing' ); |
|
| 195 | - } |
|
| 192 | + // Assign type 'Thing' if we are dealing with an entity without type |
|
| 193 | + if ( $entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null( $entity_type ) ) { |
|
| 194 | + wl_schema_set_types( $entity_id, 'Thing' ); |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | 197 | } |
| 198 | 198 | |
@@ -218,180 +218,180 @@ discard block |
||
| 218 | 218 | */ |
| 219 | 219 | function wl_save_entity( $entity_data ) { |
| 220 | 220 | |
| 221 | - $uri = $entity_data['uri']; |
|
| 222 | - $label = $entity_data['label']; |
|
| 223 | - $type_uri = $entity_data['main_type']; |
|
| 224 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 225 | - $description = $entity_data['description']; |
|
| 226 | - $images = isset( $entity_data['image'] ) ? wl_force_to_array( $entity_data['image'] ) : array(); |
|
| 227 | - $same_as = isset( $entity_data['sameas'] ) ? wl_force_to_array( $entity_data['sameas'] ) : array(); |
|
| 228 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 229 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 230 | - |
|
| 231 | - // Check whether an entity already exists with the provided URI. |
|
| 232 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 233 | - return $post; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - // Prepare properties of the new entity. |
|
| 237 | - $params = array( |
|
| 238 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 239 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 240 | - 'post_title' => $label, |
|
| 241 | - 'post_content' => $description, |
|
| 242 | - 'post_excerpt' => '', |
|
| 243 | - // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 244 | - // entity with a post_name already set, since this call is made only for |
|
| 245 | - // new entities. |
|
| 246 | - // |
|
| 247 | - // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 248 | - 'post_name' => sanitize_title( $label ), |
|
| 249 | - ); |
|
| 250 | - |
|
| 251 | - // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 252 | - // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 253 | - // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 254 | - // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 255 | - // is created when saving a post. |
|
| 256 | - global $wpseo_metabox, $seo_ultimate; |
|
| 257 | - if ( isset( $wpseo_metabox ) ) { |
|
| 258 | - remove_action( 'wp_insert_post', array( |
|
| 259 | - $wpseo_metabox, |
|
| 260 | - 'save_postdata', |
|
| 261 | - ) ); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - if ( isset( $seo_ultimate ) ) { |
|
| 265 | - remove_action( 'save_post', array( |
|
| 266 | - $seo_ultimate, |
|
| 267 | - 'save_postmeta_box', |
|
| 268 | - ) ); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 272 | - // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 273 | - // to the save_post and restore them after we saved the entity. |
|
| 274 | - // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 275 | - // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 276 | - // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 277 | - global $wp_filter; |
|
| 278 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 279 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 280 | - |
|
| 281 | - // create or update the post. |
|
| 282 | - $post_id = wp_insert_post( $params, true ); |
|
| 283 | - |
|
| 284 | - // Restore all the existing filters. |
|
| 285 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 286 | - |
|
| 287 | - // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 288 | - if ( isset( $wpseo_metabox ) ) { |
|
| 289 | - add_action( 'wp_insert_post', array( |
|
| 290 | - $wpseo_metabox, |
|
| 291 | - 'save_postdata', |
|
| 292 | - ) ); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 296 | - if ( isset( $seo_ultimate ) ) { |
|
| 297 | - add_action( 'save_post', array( |
|
| 298 | - $seo_ultimate, |
|
| 299 | - 'save_postmeta_box', |
|
| 300 | - ), 10, 2 ); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - // TODO: handle errors. |
|
| 304 | - if ( is_wp_error( $post_id ) ) { |
|
| 305 | - wl_write_log( ': error occurred' ); |
|
| 306 | - |
|
| 307 | - // inform an error occurred. |
|
| 308 | - return null; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 312 | - |
|
| 313 | - // Save the entity types. |
|
| 314 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 315 | - |
|
| 316 | - // Get a dataset URI for the entity. |
|
| 317 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 318 | - |
|
| 319 | - // Save the entity URI. |
|
| 320 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 321 | - |
|
| 322 | - // Add the uri to the sameAs data if it's not a local URI. |
|
| 323 | - if ( $wl_uri !== $uri ) { |
|
| 324 | - array_push( $same_as, $uri ); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 328 | - |
|
| 329 | - // Save the sameAs data for the entity. |
|
| 330 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 331 | - |
|
| 332 | - // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 333 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 334 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - // Call hooks. |
|
| 338 | - do_action( 'wl_save_entity', $post_id ); |
|
| 339 | - |
|
| 340 | - wl_write_log( "[ post id :: $post_id ][ uri :: $uri ][ label :: $label ][ wl uri :: $wl_uri ][ types :: " . implode( ',', $entity_types ) . " ][ images count :: " . count( $images ) . " ][ same_as count :: " . count( $same_as ) . " ]" ); |
|
| 341 | - |
|
| 342 | - foreach ( $images as $image_remote_url ) { |
|
| 343 | - |
|
| 344 | - // Check if image is already present in local DB |
|
| 345 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 346 | - // Do nothing. |
|
| 347 | - continue; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - // Check if there is an existing attachment for this post ID and source URL. |
|
| 351 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 352 | - |
|
| 353 | - // Skip if an existing image is found. |
|
| 354 | - if ( null !== $existing_image ) { |
|
| 355 | - continue; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - // Save the image and get the local path. |
|
| 359 | - $image = wl_save_image( $image_remote_url ); |
|
| 360 | - |
|
| 361 | - // Get the local URL. |
|
| 362 | - $filename = $image['path']; |
|
| 363 | - $url = $image['url']; |
|
| 364 | - $content_type = $image['content_type']; |
|
| 365 | - |
|
| 366 | - $attachment = array( |
|
| 367 | - 'guid' => $url, |
|
| 368 | - // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 369 | - 'post_title' => $label, |
|
| 370 | - // Set the title to the post title. |
|
| 371 | - 'post_content' => '', |
|
| 372 | - 'post_status' => 'inherit', |
|
| 373 | - 'post_mime_type' => $content_type, |
|
| 374 | - ); |
|
| 375 | - |
|
| 376 | - // Create the attachment in WordPress and generate the related metadata. |
|
| 377 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 221 | + $uri = $entity_data['uri']; |
|
| 222 | + $label = $entity_data['label']; |
|
| 223 | + $type_uri = $entity_data['main_type']; |
|
| 224 | + $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 225 | + $description = $entity_data['description']; |
|
| 226 | + $images = isset( $entity_data['image'] ) ? wl_force_to_array( $entity_data['image'] ) : array(); |
|
| 227 | + $same_as = isset( $entity_data['sameas'] ) ? wl_force_to_array( $entity_data['sameas'] ) : array(); |
|
| 228 | + $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 229 | + $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 230 | + |
|
| 231 | + // Check whether an entity already exists with the provided URI. |
|
| 232 | + if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 233 | + return $post; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + // Prepare properties of the new entity. |
|
| 237 | + $params = array( |
|
| 238 | + 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 239 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 240 | + 'post_title' => $label, |
|
| 241 | + 'post_content' => $description, |
|
| 242 | + 'post_excerpt' => '', |
|
| 243 | + // Ensure we're using a valid slug. We're not overwriting an existing |
|
| 244 | + // entity with a post_name already set, since this call is made only for |
|
| 245 | + // new entities. |
|
| 246 | + // |
|
| 247 | + // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
| 248 | + 'post_name' => sanitize_title( $label ), |
|
| 249 | + ); |
|
| 250 | + |
|
| 251 | + // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
| 252 | + // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 253 | + // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
| 254 | + // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
| 255 | + // is created when saving a post. |
|
| 256 | + global $wpseo_metabox, $seo_ultimate; |
|
| 257 | + if ( isset( $wpseo_metabox ) ) { |
|
| 258 | + remove_action( 'wp_insert_post', array( |
|
| 259 | + $wpseo_metabox, |
|
| 260 | + 'save_postdata', |
|
| 261 | + ) ); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + if ( isset( $seo_ultimate ) ) { |
|
| 265 | + remove_action( 'save_post', array( |
|
| 266 | + $seo_ultimate, |
|
| 267 | + 'save_postmeta_box', |
|
| 268 | + ) ); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
| 272 | + // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
| 273 | + // to the save_post and restore them after we saved the entity. |
|
| 274 | + // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
| 275 | + // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
| 276 | + // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
| 277 | + global $wp_filter; |
|
| 278 | + $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 279 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 280 | + |
|
| 281 | + // create or update the post. |
|
| 282 | + $post_id = wp_insert_post( $params, true ); |
|
| 283 | + |
|
| 284 | + // Restore all the existing filters. |
|
| 285 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 286 | + |
|
| 287 | + // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
| 288 | + if ( isset( $wpseo_metabox ) ) { |
|
| 289 | + add_action( 'wp_insert_post', array( |
|
| 290 | + $wpseo_metabox, |
|
| 291 | + 'save_postdata', |
|
| 292 | + ) ); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
| 296 | + if ( isset( $seo_ultimate ) ) { |
|
| 297 | + add_action( 'save_post', array( |
|
| 298 | + $seo_ultimate, |
|
| 299 | + 'save_postmeta_box', |
|
| 300 | + ), 10, 2 ); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + // TODO: handle errors. |
|
| 304 | + if ( is_wp_error( $post_id ) ) { |
|
| 305 | + wl_write_log( ': error occurred' ); |
|
| 306 | + |
|
| 307 | + // inform an error occurred. |
|
| 308 | + return null; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 312 | + |
|
| 313 | + // Save the entity types. |
|
| 314 | + wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 315 | + |
|
| 316 | + // Get a dataset URI for the entity. |
|
| 317 | + $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 318 | + |
|
| 319 | + // Save the entity URI. |
|
| 320 | + wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 321 | + |
|
| 322 | + // Add the uri to the sameAs data if it's not a local URI. |
|
| 323 | + if ( $wl_uri !== $uri ) { |
|
| 324 | + array_push( $same_as, $uri ); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + $new_uri = wl_get_entity_uri( $post_id ); |
|
| 328 | + |
|
| 329 | + // Save the sameAs data for the entity. |
|
| 330 | + wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 331 | + |
|
| 332 | + // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
| 333 | + foreach ( $other_properties as $property_name => $property_value ) { |
|
| 334 | + wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + // Call hooks. |
|
| 338 | + do_action( 'wl_save_entity', $post_id ); |
|
| 339 | + |
|
| 340 | + wl_write_log( "[ post id :: $post_id ][ uri :: $uri ][ label :: $label ][ wl uri :: $wl_uri ][ types :: " . implode( ',', $entity_types ) . " ][ images count :: " . count( $images ) . " ][ same_as count :: " . count( $same_as ) . " ]" ); |
|
| 341 | + |
|
| 342 | + foreach ( $images as $image_remote_url ) { |
|
| 343 | + |
|
| 344 | + // Check if image is already present in local DB |
|
| 345 | + if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 346 | + // Do nothing. |
|
| 347 | + continue; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + // Check if there is an existing attachment for this post ID and source URL. |
|
| 351 | + $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 352 | + |
|
| 353 | + // Skip if an existing image is found. |
|
| 354 | + if ( null !== $existing_image ) { |
|
| 355 | + continue; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + // Save the image and get the local path. |
|
| 359 | + $image = wl_save_image( $image_remote_url ); |
|
| 360 | + |
|
| 361 | + // Get the local URL. |
|
| 362 | + $filename = $image['path']; |
|
| 363 | + $url = $image['url']; |
|
| 364 | + $content_type = $image['content_type']; |
|
| 365 | + |
|
| 366 | + $attachment = array( |
|
| 367 | + 'guid' => $url, |
|
| 368 | + // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
| 369 | + 'post_title' => $label, |
|
| 370 | + // Set the title to the post title. |
|
| 371 | + 'post_content' => '', |
|
| 372 | + 'post_status' => 'inherit', |
|
| 373 | + 'post_mime_type' => $content_type, |
|
| 374 | + ); |
|
| 375 | + |
|
| 376 | + // Create the attachment in WordPress and generate the related metadata. |
|
| 377 | + $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 378 | 378 | |
| 379 | - // Set the source URL for the image. |
|
| 380 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 379 | + // Set the source URL for the image. |
|
| 380 | + wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 381 | 381 | |
| 382 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 383 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 382 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 383 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 384 | 384 | |
| 385 | - // Set it as the featured image. |
|
| 386 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 387 | - } |
|
| 385 | + // Set it as the featured image. |
|
| 386 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
| 387 | + } |
|
| 388 | 388 | |
| 389 | - // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 390 | - // save the entity in the triple store. |
|
| 391 | - wl_linked_data_push_to_redlink( $post_id ); |
|
| 389 | + // The entity is pushed to Redlink on save by the function hooked to save_post. |
|
| 390 | + // save the entity in the triple store. |
|
| 391 | + wl_linked_data_push_to_redlink( $post_id ); |
|
| 392 | 392 | |
| 393 | - // finally return the entity post. |
|
| 394 | - return get_post( $post_id ); |
|
| 393 | + // finally return the entity post. |
|
| 394 | + return get_post( $post_id ); |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | /** |
@@ -405,42 +405,42 @@ discard block |
||
| 405 | 405 | */ |
| 406 | 406 | function wl_linked_data_content_get_embedded_entities( $content ) { |
| 407 | 407 | |
| 408 | - // Remove quote escapes. |
|
| 409 | - $content = str_replace( '\\"', '"', $content ); |
|
| 408 | + // Remove quote escapes. |
|
| 409 | + $content = str_replace( '\\"', '"', $content ); |
|
| 410 | 410 | |
| 411 | - // Match all itemid attributes. |
|
| 412 | - $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 411 | + // Match all itemid attributes. |
|
| 412 | + $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
|
| 413 | 413 | |
| 414 | - // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 414 | + // wl_write_log( "Getting entities embedded into content [ pattern :: $pattern ]" ); |
|
| 415 | 415 | |
| 416 | - // Remove the pattern while it is found (match nested annotations). |
|
| 417 | - $matches = array(); |
|
| 416 | + // Remove the pattern while it is found (match nested annotations). |
|
| 417 | + $matches = array(); |
|
| 418 | 418 | |
| 419 | - // In case of errors, return an empty array. |
|
| 420 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 421 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 419 | + // In case of errors, return an empty array. |
|
| 420 | + if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 421 | + wl_write_log( "Found no entities embedded in content" ); |
|
| 422 | 422 | |
| 423 | - return array(); |
|
| 424 | - } |
|
| 423 | + return array(); |
|
| 424 | + } |
|
| 425 | 425 | |
| 426 | 426 | // wl_write_log("wl_update_related_entities [ content :: $content ][ data :: " . var_export($data, true). " ][ matches :: " . var_export($matches, true) . " ]"); |
| 427 | 427 | |
| 428 | - // Collect the entities. |
|
| 429 | - $entities = array(); |
|
| 430 | - foreach ( $matches[1] as $uri ) { |
|
| 431 | - $uri_d = html_entity_decode( $uri ); |
|
| 428 | + // Collect the entities. |
|
| 429 | + $entities = array(); |
|
| 430 | + foreach ( $matches[1] as $uri ) { |
|
| 431 | + $uri_d = html_entity_decode( $uri ); |
|
| 432 | 432 | |
| 433 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 433 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 434 | 434 | |
| 435 | - if ( null !== $entity ) { |
|
| 436 | - array_push( $entities, $entity->ID ); |
|
| 437 | - } |
|
| 438 | - } |
|
| 435 | + if ( null !== $entity ) { |
|
| 436 | + array_push( $entities, $entity->ID ); |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | 439 | |
| 440 | - // $count = sizeof( $entities ); |
|
| 441 | - // wl_write_log( "Found $count entities embedded in content" ); |
|
| 440 | + // $count = sizeof( $entities ); |
|
| 441 | + // wl_write_log( "Found $count entities embedded in content" ); |
|
| 442 | 442 | |
| 443 | - return $entities; |
|
| 443 | + return $entities; |
|
| 444 | 444 | } |
| 445 | 445 | |
| 446 | 446 | /** |
@@ -452,22 +452,22 @@ discard block |
||
| 452 | 452 | */ |
| 453 | 453 | function wl_linked_data_push_to_redlink( $post_id ) { |
| 454 | 454 | |
| 455 | - // Get the post. |
|
| 456 | - $post = get_post( $post_id ); |
|
| 455 | + // Get the post. |
|
| 456 | + $post = get_post( $post_id ); |
|
| 457 | 457 | |
| 458 | - // wl_write_log( "wl_linked_data_push_to_redlink [ post id :: $post_id ][ post type :: $post->post_type ]" ); |
|
| 458 | + // wl_write_log( "wl_linked_data_push_to_redlink [ post id :: $post_id ][ post type :: $post->post_type ]" ); |
|
| 459 | 459 | |
| 460 | - // Call the method on behalf of the post type. |
|
| 461 | - switch ( $post->post_type ) { |
|
| 462 | - case 'entity': |
|
| 463 | - wl_push_entity_post_to_redlink( $post ); |
|
| 464 | - break; |
|
| 465 | - default: |
|
| 466 | - wl_push_post_to_redlink( $post ); |
|
| 467 | - } |
|
| 460 | + // Call the method on behalf of the post type. |
|
| 461 | + switch ( $post->post_type ) { |
|
| 462 | + case 'entity': |
|
| 463 | + wl_push_entity_post_to_redlink( $post ); |
|
| 464 | + break; |
|
| 465 | + default: |
|
| 466 | + wl_push_post_to_redlink( $post ); |
|
| 467 | + } |
|
| 468 | 468 | |
| 469 | - // Reindex the triple store if buffering is turned off. |
|
| 470 | - if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 471 | - wordlift_reindex_triple_store(); |
|
| 472 | - } |
|
| 469 | + // Reindex the triple store if buffering is turned off. |
|
| 470 | + if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 471 | + wordlift_reindex_triple_store(); |
|
| 472 | + } |
|
| 473 | 473 | } |
@@ -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 create |
| 74 | 74 | // 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_Entity_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,54 +151,54 @@ 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 ); |
|
| 190 | - $entity_type = wl_schema_get_types( $entity_id ); |
|
| 189 | + $entity = get_post($entity_id); |
|
| 190 | + $entity_type = wl_schema_get_types($entity_id); |
|
| 191 | 191 | |
| 192 | 192 | // Assign type 'Thing' if we are dealing with an entity without type |
| 193 | - if ( $entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null( $entity_type ) ) { |
|
| 194 | - wl_schema_set_types( $entity_id, 'Thing' ); |
|
| 193 | + if ($entity->post_type == Wordlift_Entity_Service::TYPE_NAME && is_null($entity_type)) { |
|
| 194 | + wl_schema_set_types($entity_id, 'Thing'); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | // Priority 1 (default is 10) because we want the default type to be set as soon as possible |
| 200 | 200 | // Attatched to save_post because *wl_linked_data_save_post* does not always fire |
| 201 | -add_action( 'save_post', 'wordlift_save_post_add_default_schema_type', 1 ); |
|
| 201 | +add_action('save_post', 'wordlift_save_post_add_default_schema_type', 1); |
|
| 202 | 202 | |
| 203 | 203 | /** |
| 204 | 204 | * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is |
@@ -216,26 +216,26 @@ discard block |
||
| 216 | 216 | * |
| 217 | 217 | * @return null|WP_Post A post instance or null in case of failure. |
| 218 | 218 | */ |
| 219 | -function wl_save_entity( $entity_data ) { |
|
| 219 | +function wl_save_entity($entity_data) { |
|
| 220 | 220 | |
| 221 | 221 | $uri = $entity_data['uri']; |
| 222 | 222 | $label = $entity_data['label']; |
| 223 | 223 | $type_uri = $entity_data['main_type']; |
| 224 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
| 224 | + $entity_types = isset($entity_data['type']) ? $entity_data['type'] : array(); |
|
| 225 | 225 | $description = $entity_data['description']; |
| 226 | - $images = isset( $entity_data['image'] ) ? wl_force_to_array( $entity_data['image'] ) : array(); |
|
| 227 | - $same_as = isset( $entity_data['sameas'] ) ? wl_force_to_array( $entity_data['sameas'] ) : array(); |
|
| 228 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
| 229 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
| 226 | + $images = isset($entity_data['image']) ? wl_force_to_array($entity_data['image']) : array(); |
|
| 227 | + $same_as = isset($entity_data['sameas']) ? wl_force_to_array($entity_data['sameas']) : array(); |
|
| 228 | + $related_post_id = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null; |
|
| 229 | + $other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array(); |
|
| 230 | 230 | |
| 231 | 231 | // Check whether an entity already exists with the provided URI. |
| 232 | - if ( null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ) ) { |
|
| 232 | + if (null !== $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri)) { |
|
| 233 | 233 | return $post; |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | // Prepare properties of the new entity. |
| 237 | 237 | $params = array( |
| 238 | - 'post_status' => ( is_numeric( $related_post_id ) ? get_post_status( $related_post_id ) : 'draft' ), |
|
| 238 | + 'post_status' => (is_numeric($related_post_id) ? get_post_status($related_post_id) : 'draft'), |
|
| 239 | 239 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
| 240 | 240 | 'post_title' => $label, |
| 241 | 241 | 'post_content' => $description, |
@@ -245,7 +245,7 @@ discard block |
||
| 245 | 245 | // new entities. |
| 246 | 246 | // |
| 247 | 247 | // See https://github.com/insideout10/wordlift-plugin/issues/282 |
| 248 | - 'post_name' => sanitize_title( $label ), |
|
| 248 | + 'post_name' => sanitize_title($label), |
|
| 249 | 249 | ); |
| 250 | 250 | |
| 251 | 251 | // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
@@ -254,18 +254,18 @@ discard block |
||
| 254 | 254 | // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
| 255 | 255 | // is created when saving a post. |
| 256 | 256 | global $wpseo_metabox, $seo_ultimate; |
| 257 | - if ( isset( $wpseo_metabox ) ) { |
|
| 258 | - remove_action( 'wp_insert_post', array( |
|
| 257 | + if (isset($wpseo_metabox)) { |
|
| 258 | + remove_action('wp_insert_post', array( |
|
| 259 | 259 | $wpseo_metabox, |
| 260 | 260 | 'save_postdata', |
| 261 | - ) ); |
|
| 261 | + )); |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | - if ( isset( $seo_ultimate ) ) { |
|
| 265 | - remove_action( 'save_post', array( |
|
| 264 | + if (isset($seo_ultimate)) { |
|
| 265 | + remove_action('save_post', array( |
|
| 266 | 266 | $seo_ultimate, |
| 267 | 267 | 'save_postmeta_box', |
| 268 | - ) ); |
|
| 268 | + )); |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
@@ -275,88 +275,88 @@ discard block |
||
| 275 | 275 | // see https://github.com/insideout10/wordlift-plugin/issues/156 |
| 276 | 276 | // see https://github.com/insideout10/wordlift-plugin/issues/148 |
| 277 | 277 | global $wp_filter; |
| 278 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 279 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 278 | + $save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
| 279 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); |
|
| 280 | 280 | |
| 281 | 281 | // create or update the post. |
| 282 | - $post_id = wp_insert_post( $params, true ); |
|
| 282 | + $post_id = wp_insert_post($params, true); |
|
| 283 | 283 | |
| 284 | 284 | // Restore all the existing filters. |
| 285 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 285 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; |
|
| 286 | 286 | |
| 287 | 287 | // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
| 288 | - if ( isset( $wpseo_metabox ) ) { |
|
| 289 | - add_action( 'wp_insert_post', array( |
|
| 288 | + if (isset($wpseo_metabox)) { |
|
| 289 | + add_action('wp_insert_post', array( |
|
| 290 | 290 | $wpseo_metabox, |
| 291 | 291 | 'save_postdata', |
| 292 | - ) ); |
|
| 292 | + )); |
|
| 293 | 293 | } |
| 294 | 294 | |
| 295 | 295 | // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
| 296 | - if ( isset( $seo_ultimate ) ) { |
|
| 297 | - add_action( 'save_post', array( |
|
| 296 | + if (isset($seo_ultimate)) { |
|
| 297 | + add_action('save_post', array( |
|
| 298 | 298 | $seo_ultimate, |
| 299 | 299 | 'save_postmeta_box', |
| 300 | - ), 10, 2 ); |
|
| 300 | + ), 10, 2); |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | // TODO: handle errors. |
| 304 | - if ( is_wp_error( $post_id ) ) { |
|
| 305 | - wl_write_log( ': error occurred' ); |
|
| 304 | + if (is_wp_error($post_id)) { |
|
| 305 | + wl_write_log(': error occurred'); |
|
| 306 | 306 | |
| 307 | 307 | // inform an error occurred. |
| 308 | 308 | return null; |
| 309 | 309 | } |
| 310 | 310 | |
| 311 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
| 311 | + wl_set_entity_main_type($post_id, $type_uri); |
|
| 312 | 312 | |
| 313 | 313 | // Save the entity types. |
| 314 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
| 314 | + wl_set_entity_rdf_types($post_id, $entity_types); |
|
| 315 | 315 | |
| 316 | 316 | // Get a dataset URI for the entity. |
| 317 | - $wl_uri = wl_build_entity_uri( $post_id ); |
|
| 317 | + $wl_uri = wl_build_entity_uri($post_id); |
|
| 318 | 318 | |
| 319 | 319 | // Save the entity URI. |
| 320 | - wl_set_entity_uri( $post_id, $wl_uri ); |
|
| 320 | + wl_set_entity_uri($post_id, $wl_uri); |
|
| 321 | 321 | |
| 322 | 322 | // Add the uri to the sameAs data if it's not a local URI. |
| 323 | - if ( $wl_uri !== $uri ) { |
|
| 324 | - array_push( $same_as, $uri ); |
|
| 323 | + if ($wl_uri !== $uri) { |
|
| 324 | + array_push($same_as, $uri); |
|
| 325 | 325 | } |
| 326 | 326 | |
| 327 | - $new_uri = wl_get_entity_uri( $post_id ); |
|
| 327 | + $new_uri = wl_get_entity_uri($post_id); |
|
| 328 | 328 | |
| 329 | 329 | // Save the sameAs data for the entity. |
| 330 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
| 330 | + wl_schema_set_value($post_id, 'sameAs', $same_as); |
|
| 331 | 331 | |
| 332 | 332 | // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
| 333 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
| 334 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
| 333 | + foreach ($other_properties as $property_name => $property_value) { |
|
| 334 | + wl_schema_set_value($post_id, $property_name, $property_value); |
|
| 335 | 335 | } |
| 336 | 336 | |
| 337 | 337 | // Call hooks. |
| 338 | - do_action( 'wl_save_entity', $post_id ); |
|
| 338 | + do_action('wl_save_entity', $post_id); |
|
| 339 | 339 | |
| 340 | - wl_write_log( "[ post id :: $post_id ][ uri :: $uri ][ label :: $label ][ wl uri :: $wl_uri ][ types :: " . implode( ',', $entity_types ) . " ][ images count :: " . count( $images ) . " ][ same_as count :: " . count( $same_as ) . " ]" ); |
|
| 340 | + wl_write_log("[ post id :: $post_id ][ uri :: $uri ][ label :: $label ][ wl uri :: $wl_uri ][ types :: ".implode(',', $entity_types)." ][ images count :: ".count($images)." ][ same_as count :: ".count($same_as)." ]"); |
|
| 341 | 341 | |
| 342 | - foreach ( $images as $image_remote_url ) { |
|
| 342 | + foreach ($images as $image_remote_url) { |
|
| 343 | 343 | |
| 344 | 344 | // Check if image is already present in local DB |
| 345 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
| 345 | + if (strpos($image_remote_url, site_url()) !== false) { |
|
| 346 | 346 | // Do nothing. |
| 347 | 347 | continue; |
| 348 | 348 | } |
| 349 | 349 | |
| 350 | 350 | // Check if there is an existing attachment for this post ID and source URL. |
| 351 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
| 351 | + $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url); |
|
| 352 | 352 | |
| 353 | 353 | // Skip if an existing image is found. |
| 354 | - if ( null !== $existing_image ) { |
|
| 354 | + if (null !== $existing_image) { |
|
| 355 | 355 | continue; |
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | // Save the image and get the local path. |
| 359 | - $image = wl_save_image( $image_remote_url ); |
|
| 359 | + $image = wl_save_image($image_remote_url); |
|
| 360 | 360 | |
| 361 | 361 | // Get the local URL. |
| 362 | 362 | $filename = $image['path']; |
@@ -374,24 +374,24 @@ discard block |
||
| 374 | 374 | ); |
| 375 | 375 | |
| 376 | 376 | // Create the attachment in WordPress and generate the related metadata. |
| 377 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
| 377 | + $attachment_id = wp_insert_attachment($attachment, $filename, $post_id); |
|
| 378 | 378 | |
| 379 | 379 | // Set the source URL for the image. |
| 380 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
| 380 | + wl_set_source_url($attachment_id, $image_remote_url); |
|
| 381 | 381 | |
| 382 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 383 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 382 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
| 383 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
| 384 | 384 | |
| 385 | 385 | // Set it as the featured image. |
| 386 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
| 386 | + set_post_thumbnail($post_id, $attachment_id); |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | // The entity is pushed to Redlink on save by the function hooked to save_post. |
| 390 | 390 | // save the entity in the triple store. |
| 391 | - wl_linked_data_push_to_redlink( $post_id ); |
|
| 391 | + wl_linked_data_push_to_redlink($post_id); |
|
| 392 | 392 | |
| 393 | 393 | // finally return the entity post. |
| 394 | - return get_post( $post_id ); |
|
| 394 | + return get_post($post_id); |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | /** |
@@ -403,10 +403,10 @@ discard block |
||
| 403 | 403 | * |
| 404 | 404 | * @return array An array of entity posts. |
| 405 | 405 | */ |
| 406 | -function wl_linked_data_content_get_embedded_entities( $content ) { |
|
| 406 | +function wl_linked_data_content_get_embedded_entities($content) { |
|
| 407 | 407 | |
| 408 | 408 | // Remove quote escapes. |
| 409 | - $content = str_replace( '\\"', '"', $content ); |
|
| 409 | + $content = str_replace('\\"', '"', $content); |
|
| 410 | 410 | |
| 411 | 411 | // Match all itemid attributes. |
| 412 | 412 | $pattern = '/<\w+[^>]*\sitemid="([^"]+)"[^>]*>/im'; |
@@ -417,8 +417,8 @@ discard block |
||
| 417 | 417 | $matches = array(); |
| 418 | 418 | |
| 419 | 419 | // In case of errors, return an empty array. |
| 420 | - if ( false === preg_match_all( $pattern, $content, $matches ) ) { |
|
| 421 | - wl_write_log( "Found no entities embedded in content" ); |
|
| 420 | + if (false === preg_match_all($pattern, $content, $matches)) { |
|
| 421 | + wl_write_log("Found no entities embedded in content"); |
|
| 422 | 422 | |
| 423 | 423 | return array(); |
| 424 | 424 | } |
@@ -427,13 +427,13 @@ discard block |
||
| 427 | 427 | |
| 428 | 428 | // Collect the entities. |
| 429 | 429 | $entities = array(); |
| 430 | - foreach ( $matches[1] as $uri ) { |
|
| 431 | - $uri_d = html_entity_decode( $uri ); |
|
| 430 | + foreach ($matches[1] as $uri) { |
|
| 431 | + $uri_d = html_entity_decode($uri); |
|
| 432 | 432 | |
| 433 | - $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri_d ); |
|
| 433 | + $entity = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri_d); |
|
| 434 | 434 | |
| 435 | - if ( null !== $entity ) { |
|
| 436 | - array_push( $entities, $entity->ID ); |
|
| 435 | + if (null !== $entity) { |
|
| 436 | + array_push($entities, $entity->ID); |
|
| 437 | 437 | } |
| 438 | 438 | } |
| 439 | 439 | |
@@ -450,24 +450,24 @@ discard block |
||
| 450 | 450 | * |
| 451 | 451 | * @param int $post_id The post ID. |
| 452 | 452 | */ |
| 453 | -function wl_linked_data_push_to_redlink( $post_id ) { |
|
| 453 | +function wl_linked_data_push_to_redlink($post_id) { |
|
| 454 | 454 | |
| 455 | 455 | // Get the post. |
| 456 | - $post = get_post( $post_id ); |
|
| 456 | + $post = get_post($post_id); |
|
| 457 | 457 | |
| 458 | 458 | // wl_write_log( "wl_linked_data_push_to_redlink [ post id :: $post_id ][ post type :: $post->post_type ]" ); |
| 459 | 459 | |
| 460 | 460 | // Call the method on behalf of the post type. |
| 461 | - switch ( $post->post_type ) { |
|
| 461 | + switch ($post->post_type) { |
|
| 462 | 462 | case 'entity': |
| 463 | - wl_push_entity_post_to_redlink( $post ); |
|
| 463 | + wl_push_entity_post_to_redlink($post); |
|
| 464 | 464 | break; |
| 465 | 465 | default: |
| 466 | - wl_push_post_to_redlink( $post ); |
|
| 466 | + wl_push_post_to_redlink($post); |
|
| 467 | 467 | } |
| 468 | 468 | |
| 469 | 469 | // Reindex the triple store if buffering is turned off. |
| 470 | - if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 470 | + if (false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING) { |
|
| 471 | 471 | wordlift_reindex_triple_store(); |
| 472 | 472 | } |
| 473 | 473 | } |