@@ -11,132 +11,132 @@ |
||
11 | 11 | // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledClassName |
12 | 12 | class Wordpress_Post_Content_Legacy_Service extends Abstract_Wordpress_Content_Legacy_Service { |
13 | 13 | |
14 | - private static $instance = null; |
|
15 | - |
|
16 | - /** |
|
17 | - * The singleton instance. We use this only to provide this instance to those classes where we have no access to |
|
18 | - * the constructor. |
|
19 | - * |
|
20 | - * @return Wordpress_Post_Content_Legacy_Service |
|
21 | - */ |
|
22 | - public static function get_instance() { |
|
23 | - |
|
24 | - if ( ! isset( self::$instance ) ) { |
|
25 | - self::$instance = new self( Object_Type_Enum::POST, 'get_post_meta' ); |
|
26 | - } |
|
27 | - |
|
28 | - return self::$instance; |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * @param string $uri An absolute or relative URI. When absolute it must be within the dataset URI scope. |
|
33 | - * |
|
34 | - * @return Wordpress_Content|null |
|
35 | - * @throws Exception in case of error. when the URI is not within the dataset URI. |
|
36 | - */ |
|
37 | - public function get_by_entity_id( $uri ) { |
|
38 | - Assertions::is_string( $uri, '`uri` must be a string.' ); |
|
39 | - Assertions::not_empty( $uri, '`uri` cannot be empty.' ); |
|
40 | - Assertions::not_empty( $this->get_dataset_uri(), '`dataset_uri` cannot be empty.' ); |
|
41 | - |
|
42 | - $abs_uri = $this->make_absolute( $uri ); |
|
43 | - |
|
44 | - Assertions::starts_with( $abs_uri, $this->get_dataset_uri(), '`uri` must start with dataset URI.' ); |
|
45 | - |
|
46 | - // Look in sameAs. |
|
47 | - $query_args = array( |
|
48 | - // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
49 | - 'ignore_sticky_posts' => 1, |
|
50 | - 'posts_per_page' => 1, |
|
51 | - 'post_status' => 'any', |
|
52 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
53 | - 'meta_query' => array( |
|
54 | - array( |
|
55 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
56 | - 'value' => $abs_uri, |
|
57 | - 'compare' => '=', |
|
58 | - ), |
|
59 | - ), |
|
60 | - ); |
|
61 | - |
|
62 | - $posts = get_posts( $query_args ); |
|
63 | - |
|
64 | - // Get the current post or allow 3rd parties to provide a replacement. |
|
65 | - $post = current( $posts ); |
|
66 | - $post = $post ? $post : apply_filters( 'wl_content_service__post__not_found', null, $uri ); |
|
67 | - |
|
68 | - if ( is_a( $post, 'WP_Post' ) ) { |
|
69 | - return new Wordpress_Content( current( $posts ) ); |
|
70 | - } |
|
71 | - |
|
72 | - return null; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @throws Exception in case of error. when `$uri` is not a string. |
|
77 | - */ |
|
78 | - public function get_by_entity_id_or_same_as( $uri ) { |
|
79 | - // ATM we're too strict with the assertions here so we return null if the uri isn't provided. |
|
80 | - // Assertions::is_string( $uri, '`uri` must be a string.' ); |
|
81 | - // Assertions::not_empty( '`uri` cannot be empty.' ); |
|
82 | - if ( ! is_string( $uri ) || empty( $uri ) ) { |
|
83 | - return null; |
|
84 | - } |
|
85 | - |
|
86 | - // If it's a relative URI, or it's an internal URI, look in entity ID. |
|
87 | - if ( ! $this->is_absolute( $uri ) || $this->is_internal( $uri ) ) { |
|
88 | - return $this->get_by_entity_id( $uri ); |
|
89 | - } |
|
90 | - |
|
91 | - // Look in sameAs. |
|
92 | - $query_args = array( |
|
93 | - // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
94 | - 'ignore_sticky_posts' => 1, |
|
95 | - 'posts_per_page' => 1, |
|
96 | - 'post_status' => 'any', |
|
97 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
98 | - 'meta_query' => array( |
|
99 | - array( |
|
100 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
101 | - 'value' => $uri, |
|
102 | - 'compare' => '=', |
|
103 | - ), |
|
104 | - ), |
|
105 | - ); |
|
106 | - |
|
107 | - $posts = get_posts( $query_args ); |
|
108 | - |
|
109 | - // Get the current post or allow 3rd parties to provide a replacement. |
|
110 | - $post = current( $posts ); |
|
111 | - $post = $post ? $post : apply_filters( 'wl_content_service__post__not_found', null, $uri ); |
|
112 | - |
|
113 | - if ( is_a( $post, '\WP_Post' ) ) { |
|
114 | - return new Wordpress_Content( current( $posts ) ); |
|
115 | - } |
|
116 | - |
|
117 | - return null; |
|
118 | - } |
|
119 | - |
|
120 | - public function set_entity_id( $content_id, $uri ) { |
|
121 | - Assertions::equals( $content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.' ); |
|
122 | - Assertions::not_empty( $uri, "`uri` can't be empty" ); |
|
123 | - |
|
124 | - if ( $this->is_absolute( $uri ) && ! $this->is_internal( $uri ) ) { |
|
125 | - throw new Exception( '`uri` must be within the dataset URI scope.' ); |
|
126 | - } |
|
127 | - |
|
128 | - $abs_url = $this->make_absolute( $uri ); |
|
129 | - |
|
130 | - update_post_meta( $content_id->get_id(), WL_ENTITY_URL_META_NAME, $abs_url ); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @param Wordpress_Content_Id $content_id |
|
135 | - * |
|
136 | - * @return bool |
|
137 | - */ |
|
138 | - public function supports( $content_id ) { |
|
139 | - return $content_id->get_type() === Object_Type_Enum::POST; |
|
140 | - } |
|
14 | + private static $instance = null; |
|
15 | + |
|
16 | + /** |
|
17 | + * The singleton instance. We use this only to provide this instance to those classes where we have no access to |
|
18 | + * the constructor. |
|
19 | + * |
|
20 | + * @return Wordpress_Post_Content_Legacy_Service |
|
21 | + */ |
|
22 | + public static function get_instance() { |
|
23 | + |
|
24 | + if ( ! isset( self::$instance ) ) { |
|
25 | + self::$instance = new self( Object_Type_Enum::POST, 'get_post_meta' ); |
|
26 | + } |
|
27 | + |
|
28 | + return self::$instance; |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * @param string $uri An absolute or relative URI. When absolute it must be within the dataset URI scope. |
|
33 | + * |
|
34 | + * @return Wordpress_Content|null |
|
35 | + * @throws Exception in case of error. when the URI is not within the dataset URI. |
|
36 | + */ |
|
37 | + public function get_by_entity_id( $uri ) { |
|
38 | + Assertions::is_string( $uri, '`uri` must be a string.' ); |
|
39 | + Assertions::not_empty( $uri, '`uri` cannot be empty.' ); |
|
40 | + Assertions::not_empty( $this->get_dataset_uri(), '`dataset_uri` cannot be empty.' ); |
|
41 | + |
|
42 | + $abs_uri = $this->make_absolute( $uri ); |
|
43 | + |
|
44 | + Assertions::starts_with( $abs_uri, $this->get_dataset_uri(), '`uri` must start with dataset URI.' ); |
|
45 | + |
|
46 | + // Look in sameAs. |
|
47 | + $query_args = array( |
|
48 | + // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
49 | + 'ignore_sticky_posts' => 1, |
|
50 | + 'posts_per_page' => 1, |
|
51 | + 'post_status' => 'any', |
|
52 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
53 | + 'meta_query' => array( |
|
54 | + array( |
|
55 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
56 | + 'value' => $abs_uri, |
|
57 | + 'compare' => '=', |
|
58 | + ), |
|
59 | + ), |
|
60 | + ); |
|
61 | + |
|
62 | + $posts = get_posts( $query_args ); |
|
63 | + |
|
64 | + // Get the current post or allow 3rd parties to provide a replacement. |
|
65 | + $post = current( $posts ); |
|
66 | + $post = $post ? $post : apply_filters( 'wl_content_service__post__not_found', null, $uri ); |
|
67 | + |
|
68 | + if ( is_a( $post, 'WP_Post' ) ) { |
|
69 | + return new Wordpress_Content( current( $posts ) ); |
|
70 | + } |
|
71 | + |
|
72 | + return null; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @throws Exception in case of error. when `$uri` is not a string. |
|
77 | + */ |
|
78 | + public function get_by_entity_id_or_same_as( $uri ) { |
|
79 | + // ATM we're too strict with the assertions here so we return null if the uri isn't provided. |
|
80 | + // Assertions::is_string( $uri, '`uri` must be a string.' ); |
|
81 | + // Assertions::not_empty( '`uri` cannot be empty.' ); |
|
82 | + if ( ! is_string( $uri ) || empty( $uri ) ) { |
|
83 | + return null; |
|
84 | + } |
|
85 | + |
|
86 | + // If it's a relative URI, or it's an internal URI, look in entity ID. |
|
87 | + if ( ! $this->is_absolute( $uri ) || $this->is_internal( $uri ) ) { |
|
88 | + return $this->get_by_entity_id( $uri ); |
|
89 | + } |
|
90 | + |
|
91 | + // Look in sameAs. |
|
92 | + $query_args = array( |
|
93 | + // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
94 | + 'ignore_sticky_posts' => 1, |
|
95 | + 'posts_per_page' => 1, |
|
96 | + 'post_status' => 'any', |
|
97 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
98 | + 'meta_query' => array( |
|
99 | + array( |
|
100 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
101 | + 'value' => $uri, |
|
102 | + 'compare' => '=', |
|
103 | + ), |
|
104 | + ), |
|
105 | + ); |
|
106 | + |
|
107 | + $posts = get_posts( $query_args ); |
|
108 | + |
|
109 | + // Get the current post or allow 3rd parties to provide a replacement. |
|
110 | + $post = current( $posts ); |
|
111 | + $post = $post ? $post : apply_filters( 'wl_content_service__post__not_found', null, $uri ); |
|
112 | + |
|
113 | + if ( is_a( $post, '\WP_Post' ) ) { |
|
114 | + return new Wordpress_Content( current( $posts ) ); |
|
115 | + } |
|
116 | + |
|
117 | + return null; |
|
118 | + } |
|
119 | + |
|
120 | + public function set_entity_id( $content_id, $uri ) { |
|
121 | + Assertions::equals( $content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.' ); |
|
122 | + Assertions::not_empty( $uri, "`uri` can't be empty" ); |
|
123 | + |
|
124 | + if ( $this->is_absolute( $uri ) && ! $this->is_internal( $uri ) ) { |
|
125 | + throw new Exception( '`uri` must be within the dataset URI scope.' ); |
|
126 | + } |
|
127 | + |
|
128 | + $abs_url = $this->make_absolute( $uri ); |
|
129 | + |
|
130 | + update_post_meta( $content_id->get_id(), WL_ENTITY_URL_META_NAME, $abs_url ); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @param Wordpress_Content_Id $content_id |
|
135 | + * |
|
136 | + * @return bool |
|
137 | + */ |
|
138 | + public function supports( $content_id ) { |
|
139 | + return $content_id->get_type() === Object_Type_Enum::POST; |
|
140 | + } |
|
141 | 141 | |
142 | 142 | } |
@@ -21,8 +21,8 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public static function get_instance() { |
23 | 23 | |
24 | - if ( ! isset( self::$instance ) ) { |
|
25 | - self::$instance = new self( Object_Type_Enum::POST, 'get_post_meta' ); |
|
24 | + if ( ! isset(self::$instance)) { |
|
25 | + self::$instance = new self(Object_Type_Enum::POST, 'get_post_meta'); |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | return self::$instance; |
@@ -34,14 +34,14 @@ discard block |
||
34 | 34 | * @return Wordpress_Content|null |
35 | 35 | * @throws Exception in case of error. when the URI is not within the dataset URI. |
36 | 36 | */ |
37 | - public function get_by_entity_id( $uri ) { |
|
38 | - Assertions::is_string( $uri, '`uri` must be a string.' ); |
|
39 | - Assertions::not_empty( $uri, '`uri` cannot be empty.' ); |
|
40 | - Assertions::not_empty( $this->get_dataset_uri(), '`dataset_uri` cannot be empty.' ); |
|
37 | + public function get_by_entity_id($uri) { |
|
38 | + Assertions::is_string($uri, '`uri` must be a string.'); |
|
39 | + Assertions::not_empty($uri, '`uri` cannot be empty.'); |
|
40 | + Assertions::not_empty($this->get_dataset_uri(), '`dataset_uri` cannot be empty.'); |
|
41 | 41 | |
42 | - $abs_uri = $this->make_absolute( $uri ); |
|
42 | + $abs_uri = $this->make_absolute($uri); |
|
43 | 43 | |
44 | - Assertions::starts_with( $abs_uri, $this->get_dataset_uri(), '`uri` must start with dataset URI.' ); |
|
44 | + Assertions::starts_with($abs_uri, $this->get_dataset_uri(), '`uri` must start with dataset URI.'); |
|
45 | 45 | |
46 | 46 | // Look in sameAs. |
47 | 47 | $query_args = array( |
@@ -59,14 +59,14 @@ discard block |
||
59 | 59 | ), |
60 | 60 | ); |
61 | 61 | |
62 | - $posts = get_posts( $query_args ); |
|
62 | + $posts = get_posts($query_args); |
|
63 | 63 | |
64 | 64 | // Get the current post or allow 3rd parties to provide a replacement. |
65 | - $post = current( $posts ); |
|
66 | - $post = $post ? $post : apply_filters( 'wl_content_service__post__not_found', null, $uri ); |
|
65 | + $post = current($posts); |
|
66 | + $post = $post ? $post : apply_filters('wl_content_service__post__not_found', null, $uri); |
|
67 | 67 | |
68 | - if ( is_a( $post, 'WP_Post' ) ) { |
|
69 | - return new Wordpress_Content( current( $posts ) ); |
|
68 | + if (is_a($post, 'WP_Post')) { |
|
69 | + return new Wordpress_Content(current($posts)); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | return null; |
@@ -75,17 +75,17 @@ discard block |
||
75 | 75 | /** |
76 | 76 | * @throws Exception in case of error. when `$uri` is not a string. |
77 | 77 | */ |
78 | - public function get_by_entity_id_or_same_as( $uri ) { |
|
78 | + public function get_by_entity_id_or_same_as($uri) { |
|
79 | 79 | // ATM we're too strict with the assertions here so we return null if the uri isn't provided. |
80 | 80 | // Assertions::is_string( $uri, '`uri` must be a string.' ); |
81 | 81 | // Assertions::not_empty( '`uri` cannot be empty.' ); |
82 | - if ( ! is_string( $uri ) || empty( $uri ) ) { |
|
82 | + if ( ! is_string($uri) || empty($uri)) { |
|
83 | 83 | return null; |
84 | 84 | } |
85 | 85 | |
86 | 86 | // If it's a relative URI, or it's an internal URI, look in entity ID. |
87 | - if ( ! $this->is_absolute( $uri ) || $this->is_internal( $uri ) ) { |
|
88 | - return $this->get_by_entity_id( $uri ); |
|
87 | + if ( ! $this->is_absolute($uri) || $this->is_internal($uri)) { |
|
88 | + return $this->get_by_entity_id($uri); |
|
89 | 89 | } |
90 | 90 | |
91 | 91 | // Look in sameAs. |
@@ -104,30 +104,30 @@ discard block |
||
104 | 104 | ), |
105 | 105 | ); |
106 | 106 | |
107 | - $posts = get_posts( $query_args ); |
|
107 | + $posts = get_posts($query_args); |
|
108 | 108 | |
109 | 109 | // Get the current post or allow 3rd parties to provide a replacement. |
110 | - $post = current( $posts ); |
|
111 | - $post = $post ? $post : apply_filters( 'wl_content_service__post__not_found', null, $uri ); |
|
110 | + $post = current($posts); |
|
111 | + $post = $post ? $post : apply_filters('wl_content_service__post__not_found', null, $uri); |
|
112 | 112 | |
113 | - if ( is_a( $post, '\WP_Post' ) ) { |
|
114 | - return new Wordpress_Content( current( $posts ) ); |
|
113 | + if (is_a($post, '\WP_Post')) { |
|
114 | + return new Wordpress_Content(current($posts)); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | return null; |
118 | 118 | } |
119 | 119 | |
120 | - public function set_entity_id( $content_id, $uri ) { |
|
121 | - Assertions::equals( $content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.' ); |
|
122 | - Assertions::not_empty( $uri, "`uri` can't be empty" ); |
|
120 | + public function set_entity_id($content_id, $uri) { |
|
121 | + Assertions::equals($content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.'); |
|
122 | + Assertions::not_empty($uri, "`uri` can't be empty"); |
|
123 | 123 | |
124 | - if ( $this->is_absolute( $uri ) && ! $this->is_internal( $uri ) ) { |
|
125 | - throw new Exception( '`uri` must be within the dataset URI scope.' ); |
|
124 | + if ($this->is_absolute($uri) && ! $this->is_internal($uri)) { |
|
125 | + throw new Exception('`uri` must be within the dataset URI scope.'); |
|
126 | 126 | } |
127 | 127 | |
128 | - $abs_url = $this->make_absolute( $uri ); |
|
128 | + $abs_url = $this->make_absolute($uri); |
|
129 | 129 | |
130 | - update_post_meta( $content_id->get_id(), WL_ENTITY_URL_META_NAME, $abs_url ); |
|
130 | + update_post_meta($content_id->get_id(), WL_ENTITY_URL_META_NAME, $abs_url); |
|
131 | 131 | } |
132 | 132 | |
133 | 133 | /** |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | * |
136 | 136 | * @return bool |
137 | 137 | */ |
138 | - public function supports( $content_id ) { |
|
138 | + public function supports($content_id) { |
|
139 | 139 | return $content_id->get_type() === Object_Type_Enum::POST; |
140 | 140 | } |
141 | 141 |
@@ -16,184 +16,184 @@ |
||
16 | 16 | // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledClassName |
17 | 17 | class Wordpress_Post_Content_Table_Service extends Abstract_Wordpress_Content_Service { |
18 | 18 | |
19 | - private static $instance = null; |
|
20 | - |
|
21 | - /** |
|
22 | - * The singleton instance. We use this only to provide this instance to those classes where we have no access to |
|
23 | - * the constructor. |
|
24 | - * |
|
25 | - * @return Wordpress_Post_Content_Table_Service |
|
26 | - */ |
|
27 | - public static function get_instance() { |
|
28 | - |
|
29 | - if ( ! isset( self::$instance ) ) { |
|
30 | - self::$instance = new self(); |
|
31 | - } |
|
32 | - |
|
33 | - return self::$instance; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * @param string $uri An absolute or relative URI. When absolute it must be within the dataset URI scope. |
|
38 | - * |
|
39 | - * @return Wordpress_Content|null |
|
40 | - * @throws Exception in case of error. when the URI is not within the dataset URI. |
|
41 | - */ |
|
42 | - public function get_by_entity_id( $uri ) { |
|
43 | - Assertions::is_string( $uri, '`uri` must be a string.' ); |
|
44 | - Assertions::not_empty( $uri, '`uri` cannot be empty.' ); |
|
45 | - Assertions::not_empty( $this->get_dataset_uri(), '`dataset_uri` cannot be empty.' ); |
|
46 | - |
|
47 | - if ( $this->is_absolute( $uri ) && ! $this->is_internal( $uri ) ) { |
|
48 | - throw new Exception( '`uri` must be within the dataset URI scope.' ); |
|
49 | - } |
|
50 | - |
|
51 | - $rel_uri = $this->make_relative( $uri ); |
|
52 | - |
|
53 | - global $wpdb; |
|
54 | - $row = $wpdb->get_row( |
|
55 | - $wpdb->prepare( |
|
56 | - " |
|
19 | + private static $instance = null; |
|
20 | + |
|
21 | + /** |
|
22 | + * The singleton instance. We use this only to provide this instance to those classes where we have no access to |
|
23 | + * the constructor. |
|
24 | + * |
|
25 | + * @return Wordpress_Post_Content_Table_Service |
|
26 | + */ |
|
27 | + public static function get_instance() { |
|
28 | + |
|
29 | + if ( ! isset( self::$instance ) ) { |
|
30 | + self::$instance = new self(); |
|
31 | + } |
|
32 | + |
|
33 | + return self::$instance; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * @param string $uri An absolute or relative URI. When absolute it must be within the dataset URI scope. |
|
38 | + * |
|
39 | + * @return Wordpress_Content|null |
|
40 | + * @throws Exception in case of error. when the URI is not within the dataset URI. |
|
41 | + */ |
|
42 | + public function get_by_entity_id( $uri ) { |
|
43 | + Assertions::is_string( $uri, '`uri` must be a string.' ); |
|
44 | + Assertions::not_empty( $uri, '`uri` cannot be empty.' ); |
|
45 | + Assertions::not_empty( $this->get_dataset_uri(), '`dataset_uri` cannot be empty.' ); |
|
46 | + |
|
47 | + if ( $this->is_absolute( $uri ) && ! $this->is_internal( $uri ) ) { |
|
48 | + throw new Exception( '`uri` must be within the dataset URI scope.' ); |
|
49 | + } |
|
50 | + |
|
51 | + $rel_uri = $this->make_relative( $uri ); |
|
52 | + |
|
53 | + global $wpdb; |
|
54 | + $row = $wpdb->get_row( |
|
55 | + $wpdb->prepare( |
|
56 | + " |
|
57 | 57 | SELECT content_type, content_id |
58 | 58 | FROM {$wpdb->prefix}wl_entities |
59 | 59 | WHERE rel_uri = %s |
60 | 60 | ", |
61 | - $rel_uri |
|
62 | - ) |
|
63 | - ); |
|
64 | - |
|
65 | - if ( ! isset( $row ) || Object_Type_Enum::POST !== (int) $row->content_type ) { |
|
66 | - return null; |
|
67 | - } |
|
68 | - |
|
69 | - return new Wordpress_Content( get_post( $row->content_id ) ); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * @throws Exception in case of error. when `$uri` is not a string. |
|
74 | - */ |
|
75 | - public function get_by_entity_id_or_same_as( $uri ) { |
|
76 | - Assertions::is_string( $uri, '`uri` must be a string.' ); |
|
77 | - Assertions::not_empty( '`uri` cannot be empty.' ); |
|
78 | - |
|
79 | - // If it's a relative URI, or it's an internal URI, look in entity ID. |
|
80 | - if ( ! $this->is_absolute( $uri ) || $this->is_internal( $uri ) ) { |
|
81 | - return $this->get_by_entity_id( $uri ); |
|
82 | - } |
|
83 | - |
|
84 | - // Look in sameAs. |
|
85 | - $query_args = array( |
|
86 | - // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
87 | - 'ignore_sticky_posts' => 1, |
|
88 | - 'posts_per_page' => 1, |
|
89 | - 'post_status' => 'any', |
|
90 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
91 | - 'meta_query' => array( |
|
92 | - array( |
|
93 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
94 | - 'value' => $uri, |
|
95 | - 'compare' => '=', |
|
96 | - ), |
|
97 | - ), |
|
98 | - ); |
|
99 | - |
|
100 | - $posts = get_posts( $query_args ); |
|
101 | - |
|
102 | - // Get the current post or allow 3rd parties to provide a replacement. |
|
103 | - $post = current( $posts ); |
|
104 | - $post = $post ? $post : apply_filters( 'wl_content_service__post__not_found', null, $uri ); |
|
105 | - |
|
106 | - if ( is_a( $post, 'WP_Post' ) ) { |
|
107 | - return new Wordpress_Content( current( $posts ) ); |
|
108 | - } |
|
109 | - |
|
110 | - return null; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @param Wordpress_Content_Id $content_id |
|
115 | - * |
|
116 | - * @return string|null The entity ID. |
|
117 | - * @throws Exception in case of error. |
|
118 | - */ |
|
119 | - public function get_entity_id( $content_id ) { |
|
120 | - Assertions::equals( $content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.' ); |
|
121 | - |
|
122 | - global $wpdb; |
|
123 | - $rel_uri = $wpdb->get_var( |
|
124 | - $wpdb->prepare( |
|
125 | - " |
|
61 | + $rel_uri |
|
62 | + ) |
|
63 | + ); |
|
64 | + |
|
65 | + if ( ! isset( $row ) || Object_Type_Enum::POST !== (int) $row->content_type ) { |
|
66 | + return null; |
|
67 | + } |
|
68 | + |
|
69 | + return new Wordpress_Content( get_post( $row->content_id ) ); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * @throws Exception in case of error. when `$uri` is not a string. |
|
74 | + */ |
|
75 | + public function get_by_entity_id_or_same_as( $uri ) { |
|
76 | + Assertions::is_string( $uri, '`uri` must be a string.' ); |
|
77 | + Assertions::not_empty( '`uri` cannot be empty.' ); |
|
78 | + |
|
79 | + // If it's a relative URI, or it's an internal URI, look in entity ID. |
|
80 | + if ( ! $this->is_absolute( $uri ) || $this->is_internal( $uri ) ) { |
|
81 | + return $this->get_by_entity_id( $uri ); |
|
82 | + } |
|
83 | + |
|
84 | + // Look in sameAs. |
|
85 | + $query_args = array( |
|
86 | + // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
87 | + 'ignore_sticky_posts' => 1, |
|
88 | + 'posts_per_page' => 1, |
|
89 | + 'post_status' => 'any', |
|
90 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
91 | + 'meta_query' => array( |
|
92 | + array( |
|
93 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
94 | + 'value' => $uri, |
|
95 | + 'compare' => '=', |
|
96 | + ), |
|
97 | + ), |
|
98 | + ); |
|
99 | + |
|
100 | + $posts = get_posts( $query_args ); |
|
101 | + |
|
102 | + // Get the current post or allow 3rd parties to provide a replacement. |
|
103 | + $post = current( $posts ); |
|
104 | + $post = $post ? $post : apply_filters( 'wl_content_service__post__not_found', null, $uri ); |
|
105 | + |
|
106 | + if ( is_a( $post, 'WP_Post' ) ) { |
|
107 | + return new Wordpress_Content( current( $posts ) ); |
|
108 | + } |
|
109 | + |
|
110 | + return null; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @param Wordpress_Content_Id $content_id |
|
115 | + * |
|
116 | + * @return string|null The entity ID. |
|
117 | + * @throws Exception in case of error. |
|
118 | + */ |
|
119 | + public function get_entity_id( $content_id ) { |
|
120 | + Assertions::equals( $content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.' ); |
|
121 | + |
|
122 | + global $wpdb; |
|
123 | + $rel_uri = $wpdb->get_var( |
|
124 | + $wpdb->prepare( |
|
125 | + " |
|
126 | 126 | SELECT rel_uri |
127 | 127 | FROM {$wpdb->prefix}wl_entities |
128 | 128 | WHERE content_id = %d AND content_type = %d |
129 | 129 | ", |
130 | - $content_id->get_id(), |
|
131 | - $content_id->get_type() |
|
132 | - ) |
|
133 | - ); |
|
134 | - |
|
135 | - return $rel_uri ? $this->make_absolute( $rel_uri ) : null; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @param Wordpress_Content_Id $content_id |
|
140 | - * @param string $uri |
|
141 | - * |
|
142 | - * @return void |
|
143 | - * @throws Exception in case of error. |
|
144 | - */ |
|
145 | - public function set_entity_id( $content_id, $uri ) { |
|
146 | - Assertions::equals( $content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.' ); |
|
147 | - Assertions::not_empty( $uri, "`uri` can't be empty" ); |
|
148 | - |
|
149 | - if ( $this->is_absolute( $uri ) && ! $this->is_internal( $uri ) ) { |
|
150 | - throw new Exception( '`uri` must be within the dataset URI scope.' ); |
|
151 | - } |
|
152 | - |
|
153 | - $rel_url = $this->make_relative( $uri ); |
|
154 | - |
|
155 | - global $wpdb; |
|
156 | - $wpdb->query( |
|
157 | - $wpdb->prepare( |
|
158 | - " |
|
130 | + $content_id->get_id(), |
|
131 | + $content_id->get_type() |
|
132 | + ) |
|
133 | + ); |
|
134 | + |
|
135 | + return $rel_uri ? $this->make_absolute( $rel_uri ) : null; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @param Wordpress_Content_Id $content_id |
|
140 | + * @param string $uri |
|
141 | + * |
|
142 | + * @return void |
|
143 | + * @throws Exception in case of error. |
|
144 | + */ |
|
145 | + public function set_entity_id( $content_id, $uri ) { |
|
146 | + Assertions::equals( $content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.' ); |
|
147 | + Assertions::not_empty( $uri, "`uri` can't be empty" ); |
|
148 | + |
|
149 | + if ( $this->is_absolute( $uri ) && ! $this->is_internal( $uri ) ) { |
|
150 | + throw new Exception( '`uri` must be within the dataset URI scope.' ); |
|
151 | + } |
|
152 | + |
|
153 | + $rel_url = $this->make_relative( $uri ); |
|
154 | + |
|
155 | + global $wpdb; |
|
156 | + $wpdb->query( |
|
157 | + $wpdb->prepare( |
|
158 | + " |
|
159 | 159 | INSERT INTO {$wpdb->prefix}wl_entities( content_id, content_type, rel_uri, rel_uri_hash ) |
160 | 160 | VALUES( %d, %d, %s, SHA1( %s ) ) |
161 | 161 | ON DUPLICATE KEY UPDATE rel_uri = VALUES( rel_uri ), rel_uri_hash = SHA1( VALUES( rel_uri ) ); |
162 | 162 | ", |
163 | - $content_id->get_id(), |
|
164 | - $content_id->get_type(), |
|
165 | - $rel_url, |
|
166 | - $rel_url |
|
167 | - ) |
|
168 | - ); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * @param Wordpress_Content_Id $content_id |
|
173 | - * |
|
174 | - * @return bool |
|
175 | - */ |
|
176 | - public function supports( $content_id ) { |
|
177 | - return $content_id->get_type() === Object_Type_Enum::POST; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @param Wordpress_Content_Id $content_id |
|
182 | - * |
|
183 | - * @return void |
|
184 | - */ |
|
185 | - public function delete( $content_id ) { |
|
186 | - global $wpdb; |
|
187 | - |
|
188 | - $wpdb->query( |
|
189 | - $wpdb->prepare( |
|
190 | - " |
|
163 | + $content_id->get_id(), |
|
164 | + $content_id->get_type(), |
|
165 | + $rel_url, |
|
166 | + $rel_url |
|
167 | + ) |
|
168 | + ); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * @param Wordpress_Content_Id $content_id |
|
173 | + * |
|
174 | + * @return bool |
|
175 | + */ |
|
176 | + public function supports( $content_id ) { |
|
177 | + return $content_id->get_type() === Object_Type_Enum::POST; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @param Wordpress_Content_Id $content_id |
|
182 | + * |
|
183 | + * @return void |
|
184 | + */ |
|
185 | + public function delete( $content_id ) { |
|
186 | + global $wpdb; |
|
187 | + |
|
188 | + $wpdb->query( |
|
189 | + $wpdb->prepare( |
|
190 | + " |
|
191 | 191 | DELETE FROM {$wpdb->prefix}wl_entities |
192 | 192 | WHERE content_id = %d AND content_type = %d |
193 | 193 | ", |
194 | - $content_id->get_id(), |
|
195 | - $content_id->get_type() |
|
196 | - ) |
|
197 | - ); |
|
198 | - } |
|
194 | + $content_id->get_id(), |
|
195 | + $content_id->get_type() |
|
196 | + ) |
|
197 | + ); |
|
198 | + } |
|
199 | 199 | } |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | */ |
27 | 27 | public static function get_instance() { |
28 | 28 | |
29 | - if ( ! isset( self::$instance ) ) { |
|
29 | + if ( ! isset(self::$instance)) { |
|
30 | 30 | self::$instance = new self(); |
31 | 31 | } |
32 | 32 | |
@@ -39,16 +39,16 @@ discard block |
||
39 | 39 | * @return Wordpress_Content|null |
40 | 40 | * @throws Exception in case of error. when the URI is not within the dataset URI. |
41 | 41 | */ |
42 | - public function get_by_entity_id( $uri ) { |
|
43 | - Assertions::is_string( $uri, '`uri` must be a string.' ); |
|
44 | - Assertions::not_empty( $uri, '`uri` cannot be empty.' ); |
|
45 | - Assertions::not_empty( $this->get_dataset_uri(), '`dataset_uri` cannot be empty.' ); |
|
42 | + public function get_by_entity_id($uri) { |
|
43 | + Assertions::is_string($uri, '`uri` must be a string.'); |
|
44 | + Assertions::not_empty($uri, '`uri` cannot be empty.'); |
|
45 | + Assertions::not_empty($this->get_dataset_uri(), '`dataset_uri` cannot be empty.'); |
|
46 | 46 | |
47 | - if ( $this->is_absolute( $uri ) && ! $this->is_internal( $uri ) ) { |
|
48 | - throw new Exception( '`uri` must be within the dataset URI scope.' ); |
|
47 | + if ($this->is_absolute($uri) && ! $this->is_internal($uri)) { |
|
48 | + throw new Exception('`uri` must be within the dataset URI scope.'); |
|
49 | 49 | } |
50 | 50 | |
51 | - $rel_uri = $this->make_relative( $uri ); |
|
51 | + $rel_uri = $this->make_relative($uri); |
|
52 | 52 | |
53 | 53 | global $wpdb; |
54 | 54 | $row = $wpdb->get_row( |
@@ -62,23 +62,23 @@ discard block |
||
62 | 62 | ) |
63 | 63 | ); |
64 | 64 | |
65 | - if ( ! isset( $row ) || Object_Type_Enum::POST !== (int) $row->content_type ) { |
|
65 | + if ( ! isset($row) || Object_Type_Enum::POST !== (int) $row->content_type) { |
|
66 | 66 | return null; |
67 | 67 | } |
68 | 68 | |
69 | - return new Wordpress_Content( get_post( $row->content_id ) ); |
|
69 | + return new Wordpress_Content(get_post($row->content_id)); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | /** |
73 | 73 | * @throws Exception in case of error. when `$uri` is not a string. |
74 | 74 | */ |
75 | - public function get_by_entity_id_or_same_as( $uri ) { |
|
76 | - Assertions::is_string( $uri, '`uri` must be a string.' ); |
|
77 | - Assertions::not_empty( '`uri` cannot be empty.' ); |
|
75 | + public function get_by_entity_id_or_same_as($uri) { |
|
76 | + Assertions::is_string($uri, '`uri` must be a string.'); |
|
77 | + Assertions::not_empty('`uri` cannot be empty.'); |
|
78 | 78 | |
79 | 79 | // If it's a relative URI, or it's an internal URI, look in entity ID. |
80 | - if ( ! $this->is_absolute( $uri ) || $this->is_internal( $uri ) ) { |
|
81 | - return $this->get_by_entity_id( $uri ); |
|
80 | + if ( ! $this->is_absolute($uri) || $this->is_internal($uri)) { |
|
81 | + return $this->get_by_entity_id($uri); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | // Look in sameAs. |
@@ -97,14 +97,14 @@ discard block |
||
97 | 97 | ), |
98 | 98 | ); |
99 | 99 | |
100 | - $posts = get_posts( $query_args ); |
|
100 | + $posts = get_posts($query_args); |
|
101 | 101 | |
102 | 102 | // Get the current post or allow 3rd parties to provide a replacement. |
103 | - $post = current( $posts ); |
|
104 | - $post = $post ? $post : apply_filters( 'wl_content_service__post__not_found', null, $uri ); |
|
103 | + $post = current($posts); |
|
104 | + $post = $post ? $post : apply_filters('wl_content_service__post__not_found', null, $uri); |
|
105 | 105 | |
106 | - if ( is_a( $post, 'WP_Post' ) ) { |
|
107 | - return new Wordpress_Content( current( $posts ) ); |
|
106 | + if (is_a($post, 'WP_Post')) { |
|
107 | + return new Wordpress_Content(current($posts)); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | return null; |
@@ -116,8 +116,8 @@ discard block |
||
116 | 116 | * @return string|null The entity ID. |
117 | 117 | * @throws Exception in case of error. |
118 | 118 | */ |
119 | - public function get_entity_id( $content_id ) { |
|
120 | - Assertions::equals( $content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.' ); |
|
119 | + public function get_entity_id($content_id) { |
|
120 | + Assertions::equals($content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.'); |
|
121 | 121 | |
122 | 122 | global $wpdb; |
123 | 123 | $rel_uri = $wpdb->get_var( |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | ) |
133 | 133 | ); |
134 | 134 | |
135 | - return $rel_uri ? $this->make_absolute( $rel_uri ) : null; |
|
135 | + return $rel_uri ? $this->make_absolute($rel_uri) : null; |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
@@ -142,15 +142,15 @@ discard block |
||
142 | 142 | * @return void |
143 | 143 | * @throws Exception in case of error. |
144 | 144 | */ |
145 | - public function set_entity_id( $content_id, $uri ) { |
|
146 | - Assertions::equals( $content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.' ); |
|
147 | - Assertions::not_empty( $uri, "`uri` can't be empty" ); |
|
145 | + public function set_entity_id($content_id, $uri) { |
|
146 | + Assertions::equals($content_id->get_type(), Object_Type_Enum::POST, '`content_id` must be of type post.'); |
|
147 | + Assertions::not_empty($uri, "`uri` can't be empty"); |
|
148 | 148 | |
149 | - if ( $this->is_absolute( $uri ) && ! $this->is_internal( $uri ) ) { |
|
150 | - throw new Exception( '`uri` must be within the dataset URI scope.' ); |
|
149 | + if ($this->is_absolute($uri) && ! $this->is_internal($uri)) { |
|
150 | + throw new Exception('`uri` must be within the dataset URI scope.'); |
|
151 | 151 | } |
152 | 152 | |
153 | - $rel_url = $this->make_relative( $uri ); |
|
153 | + $rel_url = $this->make_relative($uri); |
|
154 | 154 | |
155 | 155 | global $wpdb; |
156 | 156 | $wpdb->query( |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | * |
174 | 174 | * @return bool |
175 | 175 | */ |
176 | - public function supports( $content_id ) { |
|
176 | + public function supports($content_id) { |
|
177 | 177 | return $content_id->get_type() === Object_Type_Enum::POST; |
178 | 178 | } |
179 | 179 | |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | * |
183 | 183 | * @return void |
184 | 184 | */ |
185 | - public function delete( $content_id ) { |
|
185 | + public function delete($content_id) { |
|
186 | 186 | global $wpdb; |
187 | 187 | |
188 | 188 | $wpdb->query( |
@@ -4,39 +4,39 @@ |
||
4 | 4 | |
5 | 5 | interface Content { |
6 | 6 | |
7 | - /** |
|
8 | - * Get the actual content. |
|
9 | - * |
|
10 | - * @return mixed Get the actual content. |
|
11 | - */ |
|
12 | - public function get_bag(); |
|
13 | - |
|
14 | - /** |
|
15 | - * Get the content id. |
|
16 | - * |
|
17 | - * @return mixed |
|
18 | - */ |
|
19 | - public function get_id(); |
|
20 | - |
|
21 | - /** |
|
22 | - * Get the content type. |
|
23 | - * |
|
24 | - * @return mixed |
|
25 | - */ |
|
26 | - public function get_object_type_enum(); |
|
27 | - |
|
28 | - /** |
|
29 | - * Get the permalink. |
|
30 | - * |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function get_permalink(); |
|
34 | - |
|
35 | - /** |
|
36 | - * Get the edit link. |
|
37 | - * |
|
38 | - * @return string |
|
39 | - */ |
|
40 | - public function get_edit_link(); |
|
7 | + /** |
|
8 | + * Get the actual content. |
|
9 | + * |
|
10 | + * @return mixed Get the actual content. |
|
11 | + */ |
|
12 | + public function get_bag(); |
|
13 | + |
|
14 | + /** |
|
15 | + * Get the content id. |
|
16 | + * |
|
17 | + * @return mixed |
|
18 | + */ |
|
19 | + public function get_id(); |
|
20 | + |
|
21 | + /** |
|
22 | + * Get the content type. |
|
23 | + * |
|
24 | + * @return mixed |
|
25 | + */ |
|
26 | + public function get_object_type_enum(); |
|
27 | + |
|
28 | + /** |
|
29 | + * Get the permalink. |
|
30 | + * |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function get_permalink(); |
|
34 | + |
|
35 | + /** |
|
36 | + * Get the edit link. |
|
37 | + * |
|
38 | + * @return string |
|
39 | + */ |
|
40 | + public function get_edit_link(); |
|
41 | 41 | |
42 | 42 | } |
@@ -4,46 +4,46 @@ |
||
4 | 4 | |
5 | 5 | class Content_Migration { |
6 | 6 | |
7 | - public function __construct() { |
|
8 | - add_action( 'init', array( $this, 'migrate' ), 100 ); |
|
9 | - } |
|
7 | + public function __construct() { |
|
8 | + add_action( 'init', array( $this, 'migrate' ), 100 ); |
|
9 | + } |
|
10 | 10 | |
11 | - public function migrate() { |
|
12 | - if ( get_option( '_wl_content_migration__migrated' ) |
|
13 | - || version_compare( get_option( 'wl_db_version', '0.0.0' ), '3.33.9', '<' ) ) { |
|
14 | - return; |
|
15 | - } |
|
11 | + public function migrate() { |
|
12 | + if ( get_option( '_wl_content_migration__migrated' ) |
|
13 | + || version_compare( get_option( 'wl_db_version', '0.0.0' ), '3.33.9', '<' ) ) { |
|
14 | + return; |
|
15 | + } |
|
16 | 16 | |
17 | - $this->migrate_entity_url(); |
|
18 | - $this->delete_legacy_fields_from_postmeta(); |
|
17 | + $this->migrate_entity_url(); |
|
18 | + $this->delete_legacy_fields_from_postmeta(); |
|
19 | 19 | |
20 | - update_option( '_wl_content_migration__migrated', true, true ); |
|
21 | - } |
|
20 | + update_option( '_wl_content_migration__migrated', true, true ); |
|
21 | + } |
|
22 | 22 | |
23 | - private function migrate_entity_url() { |
|
24 | - global $wpdb; |
|
23 | + private function migrate_entity_url() { |
|
24 | + global $wpdb; |
|
25 | 25 | |
26 | - $wpdb->query( |
|
27 | - "INSERT INTO {$wpdb->prefix}wl_entities( content_id, content_type, rel_uri, rel_uri_hash ) |
|
26 | + $wpdb->query( |
|
27 | + "INSERT INTO {$wpdb->prefix}wl_entities( content_id, content_type, rel_uri, rel_uri_hash ) |
|
28 | 28 | SELECT post_id AS content_id, 0 AS content_type, |
29 | 29 | SUBSTR( meta_value, LENGTH( SUBSTRING_INDEX( meta_value, '/', 4 ) ) + 2 ) AS rel_uri, |
30 | 30 | SHA1( SUBSTR( meta_value, LENGTH( SUBSTRING_INDEX( meta_value, '/', 4 ) ) + 2 ) ) AS rel_uri_hash |
31 | 31 | FROM $wpdb->postmeta |
32 | 32 | WHERE meta_key = 'entity_url' AND post_id IN (SELECT ID FROM $wpdb->posts) |
33 | 33 | ON DUPLICATE KEY UPDATE rel_uri = VALUES( rel_uri ), rel_uri_hash = VALUES( rel_uri_hash );" |
34 | - ); |
|
34 | + ); |
|
35 | 35 | |
36 | - } |
|
36 | + } |
|
37 | 37 | |
38 | - private function delete_legacy_fields_from_postmeta() { |
|
39 | - global $wpdb; |
|
38 | + private function delete_legacy_fields_from_postmeta() { |
|
39 | + global $wpdb; |
|
40 | 40 | |
41 | - $wpdb->query( |
|
42 | - "DELETE |
|
41 | + $wpdb->query( |
|
42 | + "DELETE |
|
43 | 43 | FROM $wpdb->postmeta |
44 | 44 | WHERE meta_key = 'entity_url';" |
45 | - ); |
|
45 | + ); |
|
46 | 46 | |
47 | - } |
|
47 | + } |
|
48 | 48 | |
49 | 49 | } |
@@ -5,19 +5,19 @@ |
||
5 | 5 | class Content_Migration { |
6 | 6 | |
7 | 7 | public function __construct() { |
8 | - add_action( 'init', array( $this, 'migrate' ), 100 ); |
|
8 | + add_action('init', array($this, 'migrate'), 100); |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | public function migrate() { |
12 | - if ( get_option( '_wl_content_migration__migrated' ) |
|
13 | - || version_compare( get_option( 'wl_db_version', '0.0.0' ), '3.33.9', '<' ) ) { |
|
12 | + if (get_option('_wl_content_migration__migrated') |
|
13 | + || version_compare(get_option('wl_db_version', '0.0.0'), '3.33.9', '<')) { |
|
14 | 14 | return; |
15 | 15 | } |
16 | 16 | |
17 | 17 | $this->migrate_entity_url(); |
18 | 18 | $this->delete_legacy_fields_from_postmeta(); |
19 | 19 | |
20 | - update_option( '_wl_content_migration__migrated', true, true ); |
|
20 | + update_option('_wl_content_migration__migrated', true, true); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | private function migrate_entity_url() { |
@@ -4,54 +4,54 @@ |
||
4 | 4 | |
5 | 5 | class Features_Registry { |
6 | 6 | |
7 | - /** |
|
8 | - * @var array<Feature> |
|
9 | - */ |
|
10 | - private $features = array(); |
|
11 | - |
|
12 | - private static $instance = null; |
|
13 | - |
|
14 | - public static function get_instance() { |
|
15 | - if ( null === self::$instance ) { |
|
16 | - self::$instance = new Features_Registry(); |
|
17 | - } |
|
18 | - |
|
19 | - return self::$instance; |
|
20 | - } |
|
21 | - |
|
22 | - /** |
|
23 | - * @param $feature Feature |
|
24 | - */ |
|
25 | - public function register_feature( $feature ) { |
|
26 | - $this->features[] = $feature; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * @param $feature_slug string |
|
31 | - * @param $default_value bool |
|
32 | - * @param $callback callable |
|
33 | - */ |
|
34 | - public function register_feature_from_slug( $feature_slug, $default_value, $callback ) { |
|
35 | - $this->features[] = new Feature( |
|
36 | - $feature_slug, |
|
37 | - $default_value, |
|
38 | - $callback |
|
39 | - ); |
|
40 | - } |
|
41 | - |
|
42 | - public function initialize_all_features() { |
|
43 | - foreach ( $this->features as $feature ) { |
|
44 | - /** |
|
45 | - * @var $feature Feature |
|
46 | - */ |
|
47 | - $feature_slug = $feature->feature_slug; |
|
48 | - if ( apply_filters( "wl_feature__enable__${feature_slug}", $feature->default_value ) ) { |
|
49 | - call_user_func( $feature->callback ); |
|
50 | - } |
|
51 | - } |
|
52 | - } |
|
53 | - |
|
54 | - public function clear_all() { |
|
55 | - $this->features = array(); |
|
56 | - } |
|
7 | + /** |
|
8 | + * @var array<Feature> |
|
9 | + */ |
|
10 | + private $features = array(); |
|
11 | + |
|
12 | + private static $instance = null; |
|
13 | + |
|
14 | + public static function get_instance() { |
|
15 | + if ( null === self::$instance ) { |
|
16 | + self::$instance = new Features_Registry(); |
|
17 | + } |
|
18 | + |
|
19 | + return self::$instance; |
|
20 | + } |
|
21 | + |
|
22 | + /** |
|
23 | + * @param $feature Feature |
|
24 | + */ |
|
25 | + public function register_feature( $feature ) { |
|
26 | + $this->features[] = $feature; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * @param $feature_slug string |
|
31 | + * @param $default_value bool |
|
32 | + * @param $callback callable |
|
33 | + */ |
|
34 | + public function register_feature_from_slug( $feature_slug, $default_value, $callback ) { |
|
35 | + $this->features[] = new Feature( |
|
36 | + $feature_slug, |
|
37 | + $default_value, |
|
38 | + $callback |
|
39 | + ); |
|
40 | + } |
|
41 | + |
|
42 | + public function initialize_all_features() { |
|
43 | + foreach ( $this->features as $feature ) { |
|
44 | + /** |
|
45 | + * @var $feature Feature |
|
46 | + */ |
|
47 | + $feature_slug = $feature->feature_slug; |
|
48 | + if ( apply_filters( "wl_feature__enable__${feature_slug}", $feature->default_value ) ) { |
|
49 | + call_user_func( $feature->callback ); |
|
50 | + } |
|
51 | + } |
|
52 | + } |
|
53 | + |
|
54 | + public function clear_all() { |
|
55 | + $this->features = array(); |
|
56 | + } |
|
57 | 57 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | private static $instance = null; |
13 | 13 | |
14 | 14 | public static function get_instance() { |
15 | - if ( null === self::$instance ) { |
|
15 | + if (null === self::$instance) { |
|
16 | 16 | self::$instance = new Features_Registry(); |
17 | 17 | } |
18 | 18 | |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | /** |
23 | 23 | * @param $feature Feature |
24 | 24 | */ |
25 | - public function register_feature( $feature ) { |
|
25 | + public function register_feature($feature) { |
|
26 | 26 | $this->features[] = $feature; |
27 | 27 | } |
28 | 28 | |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * @param $default_value bool |
32 | 32 | * @param $callback callable |
33 | 33 | */ |
34 | - public function register_feature_from_slug( $feature_slug, $default_value, $callback ) { |
|
34 | + public function register_feature_from_slug($feature_slug, $default_value, $callback) { |
|
35 | 35 | $this->features[] = new Feature( |
36 | 36 | $feature_slug, |
37 | 37 | $default_value, |
@@ -40,13 +40,13 @@ discard block |
||
40 | 40 | } |
41 | 41 | |
42 | 42 | public function initialize_all_features() { |
43 | - foreach ( $this->features as $feature ) { |
|
43 | + foreach ($this->features as $feature) { |
|
44 | 44 | /** |
45 | 45 | * @var $feature Feature |
46 | 46 | */ |
47 | 47 | $feature_slug = $feature->feature_slug; |
48 | - if ( apply_filters( "wl_feature__enable__${feature_slug}", $feature->default_value ) ) { |
|
49 | - call_user_func( $feature->callback ); |
|
48 | + if (apply_filters("wl_feature__enable__${feature_slug}", $feature->default_value)) { |
|
49 | + call_user_func($feature->callback); |
|
50 | 50 | } |
51 | 51 | } |
52 | 52 | } |
@@ -3,108 +3,108 @@ |
||
3 | 3 | namespace Wordlift\Features; |
4 | 4 | |
5 | 5 | class Response_Adapter { |
6 | - const WL_FEATURES = '_wl_features'; |
|
7 | - const WL_1 = 'wl1'; |
|
8 | - |
|
9 | - public function __construct() { |
|
10 | - |
|
11 | - // Filter responses from the API calls to update the enabled features. |
|
12 | - add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
13 | - |
|
14 | - // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
|
15 | - // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
|
16 | - // $this->init_from_env(); |
|
17 | - |
|
18 | - // Register the `wl_features__enable__{feature-name}` filters. |
|
19 | - $this->register_filters(); |
|
20 | - |
|
21 | - // Hook to the updates to the features setting to refresh the features' filters. |
|
22 | - add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
23 | - |
|
24 | - } |
|
25 | - |
|
26 | - public function response( $response ) { |
|
27 | - |
|
28 | - $headers = wp_remote_retrieve_headers( $response ); |
|
29 | - |
|
30 | - // Bail out if the `wl1` header isn't defined. |
|
31 | - if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
32 | - return $response; |
|
33 | - } |
|
34 | - $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
35 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
36 | - $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
37 | - |
|
38 | - $updated_features = $wl1['features']; |
|
39 | - |
|
40 | - $existing_features = get_option( self::WL_FEATURES, array() ); |
|
41 | - |
|
42 | - // Loop through updated features. |
|
43 | - foreach ( $updated_features as $feature_slug => $new_value ) { |
|
44 | - |
|
45 | - // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
|
46 | - // not set before. |
|
47 | - $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
48 | - |
|
49 | - if ( $old_value !== $new_value ) { |
|
50 | - /** |
|
51 | - * @param $feature_slug string The feature slug. |
|
52 | - * @param $old_value null | boolean Null represents the feature flag was not set before. |
|
53 | - * @param $new_value boolean True or false. |
|
54 | - * |
|
55 | - * @since 3.32.1 |
|
56 | - * Hook : `wl_feature__change__{feature_slug}` |
|
57 | - * Action hook to be fired when there is a change in feature state. |
|
58 | - */ |
|
59 | - do_action( "wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug ); |
|
60 | - } |
|
61 | - } |
|
62 | - |
|
63 | - if ( isset( $updated_features ) ) { |
|
64 | - |
|
65 | - if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
66 | - $this->register_filters(); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - return $response; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Registers the feature filters. |
|
75 | - */ |
|
76 | - public function register_filters() { |
|
77 | - |
|
78 | - foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
79 | - // Remove previous filters. |
|
80 | - remove_filter( "wl_feature__enable__${name}", '__return_true' ); |
|
81 | - remove_filter( "wl_feature__enable__${name}", '__return_false' ); |
|
82 | - |
|
83 | - $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
84 | - add_filter( "wl_feature__enable__${name}", $callback ); |
|
85 | - } |
|
86 | - |
|
87 | - } |
|
88 | - |
|
89 | - private function init_from_env() { |
|
90 | - $features = array_reduce( |
|
91 | - array_filter( |
|
92 | - array_keys( $_ENV ), |
|
93 | - function ( $key ) { |
|
94 | - return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
95 | - } |
|
96 | - ), |
|
97 | - function ( $features, $env ) { |
|
98 | - $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
99 | - $value = wp_validate_boolean( getenv( $env ) ); |
|
100 | - $features[ $name ] = $value; |
|
101 | - |
|
102 | - return $features; |
|
103 | - }, |
|
104 | - array() |
|
105 | - ); |
|
106 | - |
|
107 | - update_option( self::WL_FEATURES, (array) $features, true ); |
|
108 | - } |
|
6 | + const WL_FEATURES = '_wl_features'; |
|
7 | + const WL_1 = 'wl1'; |
|
8 | + |
|
9 | + public function __construct() { |
|
10 | + |
|
11 | + // Filter responses from the API calls to update the enabled features. |
|
12 | + add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
13 | + |
|
14 | + // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
|
15 | + // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
|
16 | + // $this->init_from_env(); |
|
17 | + |
|
18 | + // Register the `wl_features__enable__{feature-name}` filters. |
|
19 | + $this->register_filters(); |
|
20 | + |
|
21 | + // Hook to the updates to the features setting to refresh the features' filters. |
|
22 | + add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
23 | + |
|
24 | + } |
|
25 | + |
|
26 | + public function response( $response ) { |
|
27 | + |
|
28 | + $headers = wp_remote_retrieve_headers( $response ); |
|
29 | + |
|
30 | + // Bail out if the `wl1` header isn't defined. |
|
31 | + if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
32 | + return $response; |
|
33 | + } |
|
34 | + $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
35 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
36 | + $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
37 | + |
|
38 | + $updated_features = $wl1['features']; |
|
39 | + |
|
40 | + $existing_features = get_option( self::WL_FEATURES, array() ); |
|
41 | + |
|
42 | + // Loop through updated features. |
|
43 | + foreach ( $updated_features as $feature_slug => $new_value ) { |
|
44 | + |
|
45 | + // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
|
46 | + // not set before. |
|
47 | + $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
48 | + |
|
49 | + if ( $old_value !== $new_value ) { |
|
50 | + /** |
|
51 | + * @param $feature_slug string The feature slug. |
|
52 | + * @param $old_value null | boolean Null represents the feature flag was not set before. |
|
53 | + * @param $new_value boolean True or false. |
|
54 | + * |
|
55 | + * @since 3.32.1 |
|
56 | + * Hook : `wl_feature__change__{feature_slug}` |
|
57 | + * Action hook to be fired when there is a change in feature state. |
|
58 | + */ |
|
59 | + do_action( "wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug ); |
|
60 | + } |
|
61 | + } |
|
62 | + |
|
63 | + if ( isset( $updated_features ) ) { |
|
64 | + |
|
65 | + if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
66 | + $this->register_filters(); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + return $response; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Registers the feature filters. |
|
75 | + */ |
|
76 | + public function register_filters() { |
|
77 | + |
|
78 | + foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
79 | + // Remove previous filters. |
|
80 | + remove_filter( "wl_feature__enable__${name}", '__return_true' ); |
|
81 | + remove_filter( "wl_feature__enable__${name}", '__return_false' ); |
|
82 | + |
|
83 | + $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
84 | + add_filter( "wl_feature__enable__${name}", $callback ); |
|
85 | + } |
|
86 | + |
|
87 | + } |
|
88 | + |
|
89 | + private function init_from_env() { |
|
90 | + $features = array_reduce( |
|
91 | + array_filter( |
|
92 | + array_keys( $_ENV ), |
|
93 | + function ( $key ) { |
|
94 | + return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
95 | + } |
|
96 | + ), |
|
97 | + function ( $features, $env ) { |
|
98 | + $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
99 | + $value = wp_validate_boolean( getenv( $env ) ); |
|
100 | + $features[ $name ] = $value; |
|
101 | + |
|
102 | + return $features; |
|
103 | + }, |
|
104 | + array() |
|
105 | + ); |
|
106 | + |
|
107 | + update_option( self::WL_FEATURES, (array) $features, true ); |
|
108 | + } |
|
109 | 109 | |
110 | 110 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | public function __construct() { |
10 | 10 | |
11 | 11 | // Filter responses from the API calls to update the enabled features. |
12 | - add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
12 | + add_filter('wl_api_service__response', array($this, 'response'), 10, 1); |
|
13 | 13 | |
14 | 14 | // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
15 | 15 | // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
@@ -19,34 +19,34 @@ discard block |
||
19 | 19 | $this->register_filters(); |
20 | 20 | |
21 | 21 | // Hook to the updates to the features setting to refresh the features' filters. |
22 | - add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
22 | + add_action('update_option_'.self::WL_FEATURES, array($this, 'register_filters'), 10, 0); |
|
23 | 23 | |
24 | 24 | } |
25 | 25 | |
26 | - public function response( $response ) { |
|
26 | + public function response($response) { |
|
27 | 27 | |
28 | - $headers = wp_remote_retrieve_headers( $response ); |
|
28 | + $headers = wp_remote_retrieve_headers($response); |
|
29 | 29 | |
30 | 30 | // Bail out if the `wl1` header isn't defined. |
31 | - if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
31 | + if ( ! isset($headers[self::WL_1])) { |
|
32 | 32 | return $response; |
33 | 33 | } |
34 | - $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
34 | + $wl1_as_base64_string = $headers[self::WL_1]; |
|
35 | 35 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
36 | - $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
36 | + $wl1 = json_decode(base64_decode($wl1_as_base64_string), true); |
|
37 | 37 | |
38 | 38 | $updated_features = $wl1['features']; |
39 | 39 | |
40 | - $existing_features = get_option( self::WL_FEATURES, array() ); |
|
40 | + $existing_features = get_option(self::WL_FEATURES, array()); |
|
41 | 41 | |
42 | 42 | // Loop through updated features. |
43 | - foreach ( $updated_features as $feature_slug => $new_value ) { |
|
43 | + foreach ($updated_features as $feature_slug => $new_value) { |
|
44 | 44 | |
45 | 45 | // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
46 | 46 | // not set before. |
47 | - $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
47 | + $old_value = array_key_exists($feature_slug, $existing_features) ? $existing_features[$feature_slug] : null; |
|
48 | 48 | |
49 | - if ( $old_value !== $new_value ) { |
|
49 | + if ($old_value !== $new_value) { |
|
50 | 50 | /** |
51 | 51 | * @param $feature_slug string The feature slug. |
52 | 52 | * @param $old_value null | boolean Null represents the feature flag was not set before. |
@@ -56,13 +56,13 @@ discard block |
||
56 | 56 | * Hook : `wl_feature__change__{feature_slug}` |
57 | 57 | * Action hook to be fired when there is a change in feature state. |
58 | 58 | */ |
59 | - do_action( "wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug ); |
|
59 | + do_action("wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug); |
|
60 | 60 | } |
61 | 61 | } |
62 | 62 | |
63 | - if ( isset( $updated_features ) ) { |
|
63 | + if (isset($updated_features)) { |
|
64 | 64 | |
65 | - if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
65 | + if (update_option(self::WL_FEATURES, (array) $updated_features, true)) { |
|
66 | 66 | $this->register_filters(); |
67 | 67 | } |
68 | 68 | } |
@@ -75,13 +75,13 @@ discard block |
||
75 | 75 | */ |
76 | 76 | public function register_filters() { |
77 | 77 | |
78 | - foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
78 | + foreach ((array) get_option(self::WL_FEATURES, array()) as $name => $enabled) { |
|
79 | 79 | // Remove previous filters. |
80 | - remove_filter( "wl_feature__enable__${name}", '__return_true' ); |
|
81 | - remove_filter( "wl_feature__enable__${name}", '__return_false' ); |
|
80 | + remove_filter("wl_feature__enable__${name}", '__return_true'); |
|
81 | + remove_filter("wl_feature__enable__${name}", '__return_false'); |
|
82 | 82 | |
83 | - $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
84 | - add_filter( "wl_feature__enable__${name}", $callback ); |
|
83 | + $callback = ($enabled ? '__return_true' : '__return_false'); |
|
84 | + add_filter("wl_feature__enable__${name}", $callback); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | } |
@@ -89,22 +89,22 @@ discard block |
||
89 | 89 | private function init_from_env() { |
90 | 90 | $features = array_reduce( |
91 | 91 | array_filter( |
92 | - array_keys( $_ENV ), |
|
93 | - function ( $key ) { |
|
94 | - return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
92 | + array_keys($_ENV), |
|
93 | + function($key) { |
|
94 | + return preg_match('~^WL_FEATURES__.*~', $key); |
|
95 | 95 | } |
96 | 96 | ), |
97 | - function ( $features, $env ) { |
|
98 | - $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
99 | - $value = wp_validate_boolean( getenv( $env ) ); |
|
100 | - $features[ $name ] = $value; |
|
97 | + function($features, $env) { |
|
98 | + $name = strtolower(str_replace('_', '-', substr($env, strlen('WL_FEATURES__')))); |
|
99 | + $value = wp_validate_boolean(getenv($env)); |
|
100 | + $features[$name] = $value; |
|
101 | 101 | |
102 | 102 | return $features; |
103 | 103 | }, |
104 | 104 | array() |
105 | 105 | ); |
106 | 106 | |
107 | - update_option( self::WL_FEATURES, (array) $features, true ); |
|
107 | + update_option(self::WL_FEATURES, (array) $features, true); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | } |
@@ -17,88 +17,88 @@ |
||
17 | 17 | * @package Wordlift\Term |
18 | 18 | */ |
19 | 19 | class Type_Service extends Singleton { |
20 | - /** |
|
21 | - * @var \Wordlift_Schema_Service |
|
22 | - */ |
|
23 | - private $schema_service; |
|
24 | - |
|
25 | - public function __construct() { |
|
26 | - parent::__construct(); |
|
27 | - $this->schema_service = \Wordlift_Schema_Service::get_instance(); |
|
28 | - } |
|
29 | - |
|
30 | - public function get_entity_types_labels( $term_id ) { |
|
31 | - $entity_types = $this->get_entity_types( $term_id ); |
|
32 | - return array_map( |
|
33 | - function ( $entity_type ) { |
|
34 | - return $entity_type->name; |
|
35 | - }, |
|
36 | - $entity_types |
|
37 | - ); |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Returns the entity types selected for the term. |
|
42 | - * |
|
43 | - * @param $term_id int |
|
44 | - * |
|
45 | - * @return \WP_Term[] |
|
46 | - */ |
|
47 | - public function get_entity_types( $term_id ) { |
|
48 | - |
|
49 | - $entity_type_slugs = get_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
50 | - |
|
51 | - $types = array_filter( |
|
52 | - array_map( |
|
53 | - function ( $type_slug ) { |
|
54 | - $term = get_term_by( 'slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
55 | - if ( ! $term ) { |
|
56 | - return false; |
|
57 | - } |
|
58 | - |
|
59 | - return $term; |
|
60 | - }, |
|
61 | - $entity_type_slugs |
|
62 | - ) |
|
63 | - ); |
|
64 | - |
|
65 | - $types = array_filter( $types ); |
|
66 | - |
|
67 | - if ( 0 !== count( $types ) ) { |
|
68 | - return $types; |
|
69 | - } |
|
70 | - |
|
71 | - return array( get_term_by( 'slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @param $term_id int |
|
76 | - * |
|
77 | - * @return array|null |
|
78 | - */ |
|
79 | - public function get_schema( $term_id ) { |
|
80 | - $entity_types = $this->get_entity_types( $term_id ); |
|
81 | - foreach ( $entity_types as $entity_type ) { |
|
82 | - $schema = $this->schema_service->get_schema( $entity_type->slug ); |
|
83 | - if ( ! $schema ) { |
|
84 | - break; |
|
85 | - } |
|
86 | - } |
|
87 | - |
|
88 | - return $this->schema_service->get_schema( 'thing' ); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Removes all the existing entity types and set the entity types for the term. |
|
93 | - * |
|
94 | - * @param $term_id int |
|
95 | - * @param $entity_types array |
|
96 | - */ |
|
97 | - public function set_entity_types( $term_id, $entity_types ) { |
|
98 | - delete_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
99 | - foreach ( $entity_types as $entity_type ) { |
|
100 | - add_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $entity_type ); |
|
101 | - } |
|
102 | - } |
|
20 | + /** |
|
21 | + * @var \Wordlift_Schema_Service |
|
22 | + */ |
|
23 | + private $schema_service; |
|
24 | + |
|
25 | + public function __construct() { |
|
26 | + parent::__construct(); |
|
27 | + $this->schema_service = \Wordlift_Schema_Service::get_instance(); |
|
28 | + } |
|
29 | + |
|
30 | + public function get_entity_types_labels( $term_id ) { |
|
31 | + $entity_types = $this->get_entity_types( $term_id ); |
|
32 | + return array_map( |
|
33 | + function ( $entity_type ) { |
|
34 | + return $entity_type->name; |
|
35 | + }, |
|
36 | + $entity_types |
|
37 | + ); |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Returns the entity types selected for the term. |
|
42 | + * |
|
43 | + * @param $term_id int |
|
44 | + * |
|
45 | + * @return \WP_Term[] |
|
46 | + */ |
|
47 | + public function get_entity_types( $term_id ) { |
|
48 | + |
|
49 | + $entity_type_slugs = get_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
50 | + |
|
51 | + $types = array_filter( |
|
52 | + array_map( |
|
53 | + function ( $type_slug ) { |
|
54 | + $term = get_term_by( 'slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
55 | + if ( ! $term ) { |
|
56 | + return false; |
|
57 | + } |
|
58 | + |
|
59 | + return $term; |
|
60 | + }, |
|
61 | + $entity_type_slugs |
|
62 | + ) |
|
63 | + ); |
|
64 | + |
|
65 | + $types = array_filter( $types ); |
|
66 | + |
|
67 | + if ( 0 !== count( $types ) ) { |
|
68 | + return $types; |
|
69 | + } |
|
70 | + |
|
71 | + return array( get_term_by( 'slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @param $term_id int |
|
76 | + * |
|
77 | + * @return array|null |
|
78 | + */ |
|
79 | + public function get_schema( $term_id ) { |
|
80 | + $entity_types = $this->get_entity_types( $term_id ); |
|
81 | + foreach ( $entity_types as $entity_type ) { |
|
82 | + $schema = $this->schema_service->get_schema( $entity_type->slug ); |
|
83 | + if ( ! $schema ) { |
|
84 | + break; |
|
85 | + } |
|
86 | + } |
|
87 | + |
|
88 | + return $this->schema_service->get_schema( 'thing' ); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Removes all the existing entity types and set the entity types for the term. |
|
93 | + * |
|
94 | + * @param $term_id int |
|
95 | + * @param $entity_types array |
|
96 | + */ |
|
97 | + public function set_entity_types( $term_id, $entity_types ) { |
|
98 | + delete_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
99 | + foreach ( $entity_types as $entity_type ) { |
|
100 | + add_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $entity_type ); |
|
101 | + } |
|
102 | + } |
|
103 | 103 | |
104 | 104 | } |
@@ -27,10 +27,10 @@ discard block |
||
27 | 27 | $this->schema_service = \Wordlift_Schema_Service::get_instance(); |
28 | 28 | } |
29 | 29 | |
30 | - public function get_entity_types_labels( $term_id ) { |
|
31 | - $entity_types = $this->get_entity_types( $term_id ); |
|
30 | + public function get_entity_types_labels($term_id) { |
|
31 | + $entity_types = $this->get_entity_types($term_id); |
|
32 | 32 | return array_map( |
33 | - function ( $entity_type ) { |
|
33 | + function($entity_type) { |
|
34 | 34 | return $entity_type->name; |
35 | 35 | }, |
36 | 36 | $entity_types |
@@ -44,15 +44,15 @@ discard block |
||
44 | 44 | * |
45 | 45 | * @return \WP_Term[] |
46 | 46 | */ |
47 | - public function get_entity_types( $term_id ) { |
|
47 | + public function get_entity_types($term_id) { |
|
48 | 48 | |
49 | - $entity_type_slugs = get_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
49 | + $entity_type_slugs = get_term_meta($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
50 | 50 | |
51 | 51 | $types = array_filter( |
52 | 52 | array_map( |
53 | - function ( $type_slug ) { |
|
54 | - $term = get_term_by( 'slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
55 | - if ( ! $term ) { |
|
53 | + function($type_slug) { |
|
54 | + $term = get_term_by('slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
55 | + if ( ! $term) { |
|
56 | 56 | return false; |
57 | 57 | } |
58 | 58 | |
@@ -62,13 +62,13 @@ discard block |
||
62 | 62 | ) |
63 | 63 | ); |
64 | 64 | |
65 | - $types = array_filter( $types ); |
|
65 | + $types = array_filter($types); |
|
66 | 66 | |
67 | - if ( 0 !== count( $types ) ) { |
|
67 | + if (0 !== count($types)) { |
|
68 | 68 | return $types; |
69 | 69 | } |
70 | 70 | |
71 | - return array( get_term_by( 'slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ); |
|
71 | + return array(get_term_by('slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME)); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -76,16 +76,16 @@ discard block |
||
76 | 76 | * |
77 | 77 | * @return array|null |
78 | 78 | */ |
79 | - public function get_schema( $term_id ) { |
|
80 | - $entity_types = $this->get_entity_types( $term_id ); |
|
81 | - foreach ( $entity_types as $entity_type ) { |
|
82 | - $schema = $this->schema_service->get_schema( $entity_type->slug ); |
|
83 | - if ( ! $schema ) { |
|
79 | + public function get_schema($term_id) { |
|
80 | + $entity_types = $this->get_entity_types($term_id); |
|
81 | + foreach ($entity_types as $entity_type) { |
|
82 | + $schema = $this->schema_service->get_schema($entity_type->slug); |
|
83 | + if ( ! $schema) { |
|
84 | 84 | break; |
85 | 85 | } |
86 | 86 | } |
87 | 87 | |
88 | - return $this->schema_service->get_schema( 'thing' ); |
|
88 | + return $this->schema_service->get_schema('thing'); |
|
89 | 89 | } |
90 | 90 | |
91 | 91 | /** |
@@ -94,10 +94,10 @@ discard block |
||
94 | 94 | * @param $term_id int |
95 | 95 | * @param $entity_types array |
96 | 96 | */ |
97 | - public function set_entity_types( $term_id, $entity_types ) { |
|
98 | - delete_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
99 | - foreach ( $entity_types as $entity_type ) { |
|
100 | - add_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $entity_type ); |
|
97 | + public function set_entity_types($term_id, $entity_types) { |
|
98 | + delete_term_meta($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
99 | + foreach ($entity_types as $entity_type) { |
|
100 | + add_term_meta($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $entity_type); |
|
101 | 101 | } |
102 | 102 | } |
103 | 103 |
@@ -11,8 +11,8 @@ |
||
11 | 11 | |
12 | 12 | class Synonyms_Service extends Singleton { |
13 | 13 | |
14 | - public function get_synonyms( $term_id ) { |
|
15 | - return get_term_meta( $term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ); |
|
16 | - } |
|
14 | + public function get_synonyms( $term_id ) { |
|
15 | + return get_term_meta( $term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ); |
|
16 | + } |
|
17 | 17 | |
18 | 18 | } |
@@ -11,8 +11,8 @@ |
||
11 | 11 | |
12 | 12 | class Synonyms_Service extends Singleton { |
13 | 13 | |
14 | - public function get_synonyms( $term_id ) { |
|
15 | - return get_term_meta( $term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ); |
|
14 | + public function get_synonyms($term_id) { |
|
15 | + return get_term_meta($term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY); |
|
16 | 16 | } |
17 | 17 | |
18 | 18 | } |
@@ -20,378 +20,378 @@ |
||
20 | 20 | |
21 | 21 | class Post_Adapter { |
22 | 22 | |
23 | - /** |
|
24 | - * A {@link Wordlift_Log_Service} logging instance. |
|
25 | - * |
|
26 | - * @access private |
|
27 | - * @var \Wordlift_Log_Service A {@link Wordlift_Log_Service} logging instance. |
|
28 | - */ |
|
29 | - private $log; |
|
30 | - |
|
31 | - public function __construct() { |
|
32 | - |
|
33 | - // Bail out if block editor's functions aren't available. |
|
34 | - if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
35 | - return; |
|
36 | - } |
|
37 | - |
|
38 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
39 | - |
|
40 | - add_action( 'init', array( $this, 'init' ) ); |
|
41 | - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10 ); |
|
42 | - |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Initialize by registering our block type `wordlift/classification`, required for {@link parse_blocks) to work |
|
47 | - * correctly. |
|
48 | - */ |
|
49 | - public function init() { |
|
50 | - |
|
51 | - register_block_type( |
|
52 | - 'wordlift/classification', |
|
53 | - array( |
|
54 | - 'editor_script' => 'wl-block-editor', |
|
55 | - 'attributes' => array( |
|
56 | - 'entities' => array( 'type' => 'array' ), |
|
57 | - ), |
|
58 | - ) |
|
59 | - ); |
|
60 | - |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * A sample structure: |
|
65 | - * |
|
66 | - * { |
|
67 | - * "entities": [ |
|
68 | - * { |
|
69 | - * "annotations": { |
|
70 | - * "urn:enhancement-7e8e66fc": { |
|
71 | - * "start": 3480, |
|
72 | - * "end": 3486, |
|
73 | - * "text": "libero" |
|
74 | - * } |
|
75 | - * }, |
|
76 | - * "description": "Le libero ou libéro est un poste défensif du volley-ball. Des règles particulières le concernant ont été introduites à la fin des années 1990. De par sa spécificité, le libéro a un statut à part au sein d’une équipe de volley-ball. Pour être identifié, il doit porter un uniforme qui contraste avec ceux des autres membres de son équipe, titulaires ou remplaçants.", |
|
77 | - * "id": "http://fr.dbpedia.org/resource/Libero_(volley-ball)", |
|
78 | - * "label": "Libero (volley-ball)", |
|
79 | - * "mainType": "other", |
|
80 | - * "occurrences": ["urn:enhancement-7e8e66fc"], |
|
81 | - * "sameAs": null, |
|
82 | - * "synonyms": [], |
|
83 | - * "types": ["other"] |
|
84 | - * } |
|
85 | - * ] |
|
86 | - * } |
|
87 | - * |
|
88 | - * @param array $data An array of slashed post data. |
|
89 | - * |
|
90 | - * @return array The data array. |
|
91 | - */ |
|
92 | - public function wp_insert_post_data( $data ) { |
|
93 | - $post_status = $data['post_status']; |
|
94 | - if ( 'auto-draft' === $post_status || 'inherit' === $post_status ) { |
|
95 | - return $data; |
|
96 | - } |
|
97 | - |
|
98 | - try { |
|
99 | - $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
100 | - |
|
101 | - foreach ( $entities as $entity ) { |
|
102 | - |
|
103 | - $entity_id = array_key_exists( 'id', $entity ) ? $entity['id'] : ''; |
|
104 | - |
|
105 | - if ( ! $this->entity_id_valid( $entity_id ) ) { |
|
106 | - continue; |
|
107 | - } |
|
108 | - |
|
109 | - $entity_uris = $this->get_entity_uris( $entity ); |
|
110 | - |
|
111 | - if ( $this->get_first_matching_entity_by_uri( $entity_uris ) === null && |
|
112 | - Post_Entities_Validator::is_local_entity_uri_exist( Wordlift_Entity_Uri_Service::get_instance(), $entity_uris ) ) { |
|
113 | - // Skip the entity |
|
114 | - continue; |
|
115 | - } |
|
116 | - |
|
117 | - // If 'entity auto publish' is false, we set the status to `draft` by default. |
|
118 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
119 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
120 | - ? $data['post_status'] : 'draft'; |
|
121 | - $this->create_or_update_entity( $entity, $post_status ); |
|
122 | - |
|
123 | - } |
|
124 | - } catch ( \Exception $e ) { |
|
125 | - $this->log->error( $e->getMessage() ); |
|
126 | - } |
|
127 | - |
|
128 | - return $data; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Parse the post content to find the `wordlift/classification` block and return the entities' data. |
|
133 | - * |
|
134 | - * @param string $post_content The post content. |
|
135 | - * |
|
136 | - * @return array An array of entities' structures. |
|
137 | - * @throws \Exception when an error occurs. |
|
138 | - */ |
|
139 | - private function parse_content( $post_content ) { |
|
140 | - |
|
141 | - $all_blocks = parse_blocks( $post_content ); |
|
142 | - |
|
143 | - $blocks = array_filter( |
|
144 | - $all_blocks, |
|
145 | - function ( $item ) { |
|
146 | - return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
147 | - } |
|
148 | - ); |
|
149 | - |
|
150 | - // Bail out if the blocks' array is empty. |
|
151 | - if ( empty( $blocks ) ) { |
|
152 | - return array(); |
|
153 | - } |
|
154 | - |
|
155 | - $block = current( $blocks ); |
|
156 | - |
|
157 | - // Bail out if the entities array is empty. |
|
158 | - if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
159 | - return array(); |
|
160 | - } |
|
161 | - |
|
162 | - return $block['attrs']['entities']; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Collect entity labels from the entity array. |
|
167 | - * |
|
168 | - * This function expects an array with the following keys: |
|
169 | - * |
|
170 | - * array( |
|
171 | - * 'label' => ..., |
|
172 | - * 'synonyms' => array( ... ), |
|
173 | - * 'annotations' => array( |
|
174 | - * ...id... => array( text => ... ), |
|
175 | - * ), |
|
176 | - * 'occurrences' => array( ... ), |
|
177 | - * ) |
|
178 | - * |
|
179 | - * and it is going to output an array with all the labels, keeping the `label` at first position: |
|
180 | - * |
|
181 | - * array( |
|
182 | - * ...label..., |
|
183 | - * ...synonyms..., |
|
184 | - * ...texts..., |
|
185 | - * ) |
|
186 | - * |
|
187 | - * This function is going to collect the label from the `label` property, from the `synonyms` property and from |
|
188 | - * `annotations` property. Since the `annotations` property contains all the annotations including those that |
|
189 | - * haven't been selected, this function is going to only get the `text` for the annotations property listed in |
|
190 | - * `occurrences`. |
|
191 | - * |
|
192 | - * @param array $entity { |
|
193 | - * The entity data. |
|
194 | - * |
|
195 | - * @type string $label The entity label. |
|
196 | - * @type array $synonyms The entity synonyms. |
|
197 | - * @type array $occurrences The selected occurrences. |
|
198 | - * @type array $annotations The annotations. |
|
199 | - * } |
|
200 | - * |
|
201 | - * @return array An array of labels. |
|
202 | - */ |
|
203 | - public function get_labels( $entity ) { |
|
204 | - |
|
205 | - $args = wp_parse_args( |
|
206 | - $entity, |
|
207 | - array( |
|
208 | - 'label' => array(), |
|
209 | - 'synonyms' => array(), |
|
210 | - 'annotations' => array(), |
|
211 | - 'occurrences' => array(), |
|
212 | - ) |
|
213 | - ); |
|
214 | - |
|
215 | - // We gather all the labels, occurrences texts and synonyms into one array. |
|
216 | - $initial = array_merge( |
|
217 | - (array) $args['label'], |
|
218 | - (array) $args['synonyms'] |
|
219 | - ); |
|
220 | - |
|
221 | - $annotations = $args['annotations']; |
|
222 | - |
|
223 | - return array_reduce( |
|
224 | - $args['occurrences'], |
|
225 | - function ( $carry, $item ) use ( $annotations ) { |
|
226 | - |
|
227 | - // Bail out if occurrences->$item->text isn't set or its contents are already |
|
228 | - // in `$carry`. |
|
229 | - if ( ! isset( $annotations[ $item ]['text'] ) |
|
230 | - || in_array( $annotations[ $item ]['text'], $carry, true ) ) { |
|
231 | - return $carry; |
|
232 | - } |
|
233 | - |
|
234 | - // Push the label. |
|
235 | - $carry[] = $annotations[ $item ]['text']; |
|
236 | - |
|
237 | - return $carry; |
|
238 | - }, |
|
239 | - $initial |
|
240 | - ); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Create or update the entity. |
|
245 | - * |
|
246 | - * An entity lookup is performed on the local vocabulary using the `id` and `sameAs` URIs. If an entity is found |
|
247 | - * the {@link Entity_Store} update function is called to update the `labels` and the `sameAs` values. |
|
248 | - * |
|
249 | - * If an entity is not found the {@link Entity_Store} create function is called to create a new entity. |
|
250 | - * |
|
251 | - * @param array $entity { |
|
252 | - * The entity parameters. |
|
253 | - * |
|
254 | - * @type string The entity item id URI. |
|
255 | - * @type string|array The entity sameAs URI(s). |
|
256 | - * @type string $description The entity description. |
|
257 | - * } |
|
258 | - * |
|
259 | - * @param $string $post_status The post status, default 'draft'. |
|
260 | - * |
|
261 | - * @return int|\WP_Error |
|
262 | - * @throws \Exception when an error occurs. |
|
263 | - */ |
|
264 | - private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
265 | - |
|
266 | - // Get only valid IDs. |
|
267 | - $uris = $this->get_entity_uris( $entity ); |
|
268 | - |
|
269 | - $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
270 | - |
|
271 | - // Get the labels. |
|
272 | - $labels = $this->get_labels( $entity ); |
|
273 | - |
|
274 | - if ( empty( $post ) ) { |
|
275 | - $entity_description = array_key_exists( 'description', $entity ) ? $entity['description'] : ''; |
|
276 | - // Create the entity if it doesn't exist. |
|
277 | - $post_id = Entity_Store::get_instance()->create( |
|
278 | - array( |
|
279 | - 'labels' => $labels, |
|
280 | - 'description' => $entity_description, |
|
281 | - 'same_as' => $uris, |
|
282 | - ), |
|
283 | - $post_status |
|
284 | - ); |
|
285 | - |
|
286 | - // Return the WP_Error if we got one. |
|
287 | - if ( is_wp_error( $post_id ) ) { |
|
288 | - return $post_id; |
|
289 | - } |
|
290 | - |
|
291 | - // Add the entity type. |
|
292 | - if ( isset( $entity['mainType'] ) ) { |
|
293 | - wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
294 | - } |
|
295 | - |
|
296 | - if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
297 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
298 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
299 | - } |
|
300 | - } else { |
|
301 | - // Update the entity otherwise. |
|
302 | - $post_id = Entity_Store::get_instance()->update( |
|
303 | - array( |
|
304 | - 'ID' => $post->ID, |
|
305 | - 'labels' => $labels, |
|
306 | - 'same_as' => $uris, |
|
307 | - ) |
|
308 | - ); |
|
309 | - |
|
310 | - // Add the entity type. |
|
311 | - if ( isset( $entity['mainType'] ) ) { |
|
312 | - wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
313 | - } |
|
314 | - |
|
315 | - // see https://github.com/insideout10/wordlift-plugin/issues/1304 |
|
316 | - // Set the post status, we need to set that in order to support entities |
|
317 | - // created using rest endpoint on block editor, so that they get published |
|
318 | - // when the post is published. |
|
319 | - // Once the entity is published don't update the post status. |
|
320 | - if ( 'publish' !== $post->post_status ) { |
|
321 | - wp_update_post( |
|
322 | - array( |
|
323 | - 'ID' => $post->ID, |
|
324 | - 'post_status' => $post_status, |
|
325 | - ) |
|
326 | - ); |
|
327 | - } |
|
328 | - } |
|
329 | - |
|
330 | - return $post_id; |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Get the first matching entity for the provided URI array. |
|
335 | - * |
|
336 | - * Entities IDs and sameAs are searched. |
|
337 | - * |
|
338 | - * @param array $uris An array of URIs. |
|
339 | - * |
|
340 | - * @return \WP_Post|null The entity WP_Post if found or null if not found. |
|
341 | - */ |
|
342 | - private function get_first_matching_entity_by_uri( $uris ) { |
|
343 | - |
|
344 | - foreach ( $uris as $uri ) { |
|
345 | - |
|
346 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
347 | - |
|
348 | - if ( ! $content ) { |
|
349 | - continue; |
|
350 | - } |
|
351 | - |
|
352 | - $existing_entity = $content->get_bag(); |
|
353 | - |
|
354 | - if ( is_a( $existing_entity, 'WP_Post' ) ) { |
|
355 | - return $existing_entity; |
|
356 | - } |
|
357 | - } |
|
358 | - |
|
359 | - return null; |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * @param $entity |
|
364 | - * |
|
365 | - * @return array |
|
366 | - */ |
|
367 | - private function filter_valid_entity_ids( $entity ) { |
|
368 | - $id = $entity['id']; |
|
369 | - |
|
370 | - return array_filter( |
|
371 | - (array) $id, |
|
372 | - function ( $id ) { |
|
373 | - return preg_match( '|^https?://|', $id ); |
|
374 | - } |
|
375 | - ); |
|
376 | - } |
|
377 | - |
|
378 | - /** |
|
379 | - * @param array $entity |
|
380 | - * |
|
381 | - * @return array |
|
382 | - */ |
|
383 | - private function get_entity_uris( $entity ) { |
|
384 | - $ids = $this->filter_valid_entity_ids( $entity ); |
|
385 | - $same_as = array_key_exists( 'sameAs', $entity ) ? $entity['sameAs'] : array(); |
|
386 | - |
|
387 | - return array_merge( |
|
388 | - (array) $ids, |
|
389 | - (array) $same_as |
|
390 | - ); |
|
391 | - } |
|
392 | - |
|
393 | - private function entity_id_valid( $entity_id ) { |
|
394 | - return preg_match( '#^https?://#i', $entity_id ) === 1; |
|
395 | - } |
|
23 | + /** |
|
24 | + * A {@link Wordlift_Log_Service} logging instance. |
|
25 | + * |
|
26 | + * @access private |
|
27 | + * @var \Wordlift_Log_Service A {@link Wordlift_Log_Service} logging instance. |
|
28 | + */ |
|
29 | + private $log; |
|
30 | + |
|
31 | + public function __construct() { |
|
32 | + |
|
33 | + // Bail out if block editor's functions aren't available. |
|
34 | + if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
35 | + return; |
|
36 | + } |
|
37 | + |
|
38 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
39 | + |
|
40 | + add_action( 'init', array( $this, 'init' ) ); |
|
41 | + add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10 ); |
|
42 | + |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Initialize by registering our block type `wordlift/classification`, required for {@link parse_blocks) to work |
|
47 | + * correctly. |
|
48 | + */ |
|
49 | + public function init() { |
|
50 | + |
|
51 | + register_block_type( |
|
52 | + 'wordlift/classification', |
|
53 | + array( |
|
54 | + 'editor_script' => 'wl-block-editor', |
|
55 | + 'attributes' => array( |
|
56 | + 'entities' => array( 'type' => 'array' ), |
|
57 | + ), |
|
58 | + ) |
|
59 | + ); |
|
60 | + |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * A sample structure: |
|
65 | + * |
|
66 | + * { |
|
67 | + * "entities": [ |
|
68 | + * { |
|
69 | + * "annotations": { |
|
70 | + * "urn:enhancement-7e8e66fc": { |
|
71 | + * "start": 3480, |
|
72 | + * "end": 3486, |
|
73 | + * "text": "libero" |
|
74 | + * } |
|
75 | + * }, |
|
76 | + * "description": "Le libero ou libéro est un poste défensif du volley-ball. Des règles particulières le concernant ont été introduites à la fin des années 1990. De par sa spécificité, le libéro a un statut à part au sein d’une équipe de volley-ball. Pour être identifié, il doit porter un uniforme qui contraste avec ceux des autres membres de son équipe, titulaires ou remplaçants.", |
|
77 | + * "id": "http://fr.dbpedia.org/resource/Libero_(volley-ball)", |
|
78 | + * "label": "Libero (volley-ball)", |
|
79 | + * "mainType": "other", |
|
80 | + * "occurrences": ["urn:enhancement-7e8e66fc"], |
|
81 | + * "sameAs": null, |
|
82 | + * "synonyms": [], |
|
83 | + * "types": ["other"] |
|
84 | + * } |
|
85 | + * ] |
|
86 | + * } |
|
87 | + * |
|
88 | + * @param array $data An array of slashed post data. |
|
89 | + * |
|
90 | + * @return array The data array. |
|
91 | + */ |
|
92 | + public function wp_insert_post_data( $data ) { |
|
93 | + $post_status = $data['post_status']; |
|
94 | + if ( 'auto-draft' === $post_status || 'inherit' === $post_status ) { |
|
95 | + return $data; |
|
96 | + } |
|
97 | + |
|
98 | + try { |
|
99 | + $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
100 | + |
|
101 | + foreach ( $entities as $entity ) { |
|
102 | + |
|
103 | + $entity_id = array_key_exists( 'id', $entity ) ? $entity['id'] : ''; |
|
104 | + |
|
105 | + if ( ! $this->entity_id_valid( $entity_id ) ) { |
|
106 | + continue; |
|
107 | + } |
|
108 | + |
|
109 | + $entity_uris = $this->get_entity_uris( $entity ); |
|
110 | + |
|
111 | + if ( $this->get_first_matching_entity_by_uri( $entity_uris ) === null && |
|
112 | + Post_Entities_Validator::is_local_entity_uri_exist( Wordlift_Entity_Uri_Service::get_instance(), $entity_uris ) ) { |
|
113 | + // Skip the entity |
|
114 | + continue; |
|
115 | + } |
|
116 | + |
|
117 | + // If 'entity auto publish' is false, we set the status to `draft` by default. |
|
118 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
119 | + $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
120 | + ? $data['post_status'] : 'draft'; |
|
121 | + $this->create_or_update_entity( $entity, $post_status ); |
|
122 | + |
|
123 | + } |
|
124 | + } catch ( \Exception $e ) { |
|
125 | + $this->log->error( $e->getMessage() ); |
|
126 | + } |
|
127 | + |
|
128 | + return $data; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Parse the post content to find the `wordlift/classification` block and return the entities' data. |
|
133 | + * |
|
134 | + * @param string $post_content The post content. |
|
135 | + * |
|
136 | + * @return array An array of entities' structures. |
|
137 | + * @throws \Exception when an error occurs. |
|
138 | + */ |
|
139 | + private function parse_content( $post_content ) { |
|
140 | + |
|
141 | + $all_blocks = parse_blocks( $post_content ); |
|
142 | + |
|
143 | + $blocks = array_filter( |
|
144 | + $all_blocks, |
|
145 | + function ( $item ) { |
|
146 | + return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
147 | + } |
|
148 | + ); |
|
149 | + |
|
150 | + // Bail out if the blocks' array is empty. |
|
151 | + if ( empty( $blocks ) ) { |
|
152 | + return array(); |
|
153 | + } |
|
154 | + |
|
155 | + $block = current( $blocks ); |
|
156 | + |
|
157 | + // Bail out if the entities array is empty. |
|
158 | + if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
159 | + return array(); |
|
160 | + } |
|
161 | + |
|
162 | + return $block['attrs']['entities']; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Collect entity labels from the entity array. |
|
167 | + * |
|
168 | + * This function expects an array with the following keys: |
|
169 | + * |
|
170 | + * array( |
|
171 | + * 'label' => ..., |
|
172 | + * 'synonyms' => array( ... ), |
|
173 | + * 'annotations' => array( |
|
174 | + * ...id... => array( text => ... ), |
|
175 | + * ), |
|
176 | + * 'occurrences' => array( ... ), |
|
177 | + * ) |
|
178 | + * |
|
179 | + * and it is going to output an array with all the labels, keeping the `label` at first position: |
|
180 | + * |
|
181 | + * array( |
|
182 | + * ...label..., |
|
183 | + * ...synonyms..., |
|
184 | + * ...texts..., |
|
185 | + * ) |
|
186 | + * |
|
187 | + * This function is going to collect the label from the `label` property, from the `synonyms` property and from |
|
188 | + * `annotations` property. Since the `annotations` property contains all the annotations including those that |
|
189 | + * haven't been selected, this function is going to only get the `text` for the annotations property listed in |
|
190 | + * `occurrences`. |
|
191 | + * |
|
192 | + * @param array $entity { |
|
193 | + * The entity data. |
|
194 | + * |
|
195 | + * @type string $label The entity label. |
|
196 | + * @type array $synonyms The entity synonyms. |
|
197 | + * @type array $occurrences The selected occurrences. |
|
198 | + * @type array $annotations The annotations. |
|
199 | + * } |
|
200 | + * |
|
201 | + * @return array An array of labels. |
|
202 | + */ |
|
203 | + public function get_labels( $entity ) { |
|
204 | + |
|
205 | + $args = wp_parse_args( |
|
206 | + $entity, |
|
207 | + array( |
|
208 | + 'label' => array(), |
|
209 | + 'synonyms' => array(), |
|
210 | + 'annotations' => array(), |
|
211 | + 'occurrences' => array(), |
|
212 | + ) |
|
213 | + ); |
|
214 | + |
|
215 | + // We gather all the labels, occurrences texts and synonyms into one array. |
|
216 | + $initial = array_merge( |
|
217 | + (array) $args['label'], |
|
218 | + (array) $args['synonyms'] |
|
219 | + ); |
|
220 | + |
|
221 | + $annotations = $args['annotations']; |
|
222 | + |
|
223 | + return array_reduce( |
|
224 | + $args['occurrences'], |
|
225 | + function ( $carry, $item ) use ( $annotations ) { |
|
226 | + |
|
227 | + // Bail out if occurrences->$item->text isn't set or its contents are already |
|
228 | + // in `$carry`. |
|
229 | + if ( ! isset( $annotations[ $item ]['text'] ) |
|
230 | + || in_array( $annotations[ $item ]['text'], $carry, true ) ) { |
|
231 | + return $carry; |
|
232 | + } |
|
233 | + |
|
234 | + // Push the label. |
|
235 | + $carry[] = $annotations[ $item ]['text']; |
|
236 | + |
|
237 | + return $carry; |
|
238 | + }, |
|
239 | + $initial |
|
240 | + ); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Create or update the entity. |
|
245 | + * |
|
246 | + * An entity lookup is performed on the local vocabulary using the `id` and `sameAs` URIs. If an entity is found |
|
247 | + * the {@link Entity_Store} update function is called to update the `labels` and the `sameAs` values. |
|
248 | + * |
|
249 | + * If an entity is not found the {@link Entity_Store} create function is called to create a new entity. |
|
250 | + * |
|
251 | + * @param array $entity { |
|
252 | + * The entity parameters. |
|
253 | + * |
|
254 | + * @type string The entity item id URI. |
|
255 | + * @type string|array The entity sameAs URI(s). |
|
256 | + * @type string $description The entity description. |
|
257 | + * } |
|
258 | + * |
|
259 | + * @param $string $post_status The post status, default 'draft'. |
|
260 | + * |
|
261 | + * @return int|\WP_Error |
|
262 | + * @throws \Exception when an error occurs. |
|
263 | + */ |
|
264 | + private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
265 | + |
|
266 | + // Get only valid IDs. |
|
267 | + $uris = $this->get_entity_uris( $entity ); |
|
268 | + |
|
269 | + $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
270 | + |
|
271 | + // Get the labels. |
|
272 | + $labels = $this->get_labels( $entity ); |
|
273 | + |
|
274 | + if ( empty( $post ) ) { |
|
275 | + $entity_description = array_key_exists( 'description', $entity ) ? $entity['description'] : ''; |
|
276 | + // Create the entity if it doesn't exist. |
|
277 | + $post_id = Entity_Store::get_instance()->create( |
|
278 | + array( |
|
279 | + 'labels' => $labels, |
|
280 | + 'description' => $entity_description, |
|
281 | + 'same_as' => $uris, |
|
282 | + ), |
|
283 | + $post_status |
|
284 | + ); |
|
285 | + |
|
286 | + // Return the WP_Error if we got one. |
|
287 | + if ( is_wp_error( $post_id ) ) { |
|
288 | + return $post_id; |
|
289 | + } |
|
290 | + |
|
291 | + // Add the entity type. |
|
292 | + if ( isset( $entity['mainType'] ) ) { |
|
293 | + wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
294 | + } |
|
295 | + |
|
296 | + if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
297 | + add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
298 | + add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
299 | + } |
|
300 | + } else { |
|
301 | + // Update the entity otherwise. |
|
302 | + $post_id = Entity_Store::get_instance()->update( |
|
303 | + array( |
|
304 | + 'ID' => $post->ID, |
|
305 | + 'labels' => $labels, |
|
306 | + 'same_as' => $uris, |
|
307 | + ) |
|
308 | + ); |
|
309 | + |
|
310 | + // Add the entity type. |
|
311 | + if ( isset( $entity['mainType'] ) ) { |
|
312 | + wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
313 | + } |
|
314 | + |
|
315 | + // see https://github.com/insideout10/wordlift-plugin/issues/1304 |
|
316 | + // Set the post status, we need to set that in order to support entities |
|
317 | + // created using rest endpoint on block editor, so that they get published |
|
318 | + // when the post is published. |
|
319 | + // Once the entity is published don't update the post status. |
|
320 | + if ( 'publish' !== $post->post_status ) { |
|
321 | + wp_update_post( |
|
322 | + array( |
|
323 | + 'ID' => $post->ID, |
|
324 | + 'post_status' => $post_status, |
|
325 | + ) |
|
326 | + ); |
|
327 | + } |
|
328 | + } |
|
329 | + |
|
330 | + return $post_id; |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Get the first matching entity for the provided URI array. |
|
335 | + * |
|
336 | + * Entities IDs and sameAs are searched. |
|
337 | + * |
|
338 | + * @param array $uris An array of URIs. |
|
339 | + * |
|
340 | + * @return \WP_Post|null The entity WP_Post if found or null if not found. |
|
341 | + */ |
|
342 | + private function get_first_matching_entity_by_uri( $uris ) { |
|
343 | + |
|
344 | + foreach ( $uris as $uri ) { |
|
345 | + |
|
346 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
347 | + |
|
348 | + if ( ! $content ) { |
|
349 | + continue; |
|
350 | + } |
|
351 | + |
|
352 | + $existing_entity = $content->get_bag(); |
|
353 | + |
|
354 | + if ( is_a( $existing_entity, 'WP_Post' ) ) { |
|
355 | + return $existing_entity; |
|
356 | + } |
|
357 | + } |
|
358 | + |
|
359 | + return null; |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * @param $entity |
|
364 | + * |
|
365 | + * @return array |
|
366 | + */ |
|
367 | + private function filter_valid_entity_ids( $entity ) { |
|
368 | + $id = $entity['id']; |
|
369 | + |
|
370 | + return array_filter( |
|
371 | + (array) $id, |
|
372 | + function ( $id ) { |
|
373 | + return preg_match( '|^https?://|', $id ); |
|
374 | + } |
|
375 | + ); |
|
376 | + } |
|
377 | + |
|
378 | + /** |
|
379 | + * @param array $entity |
|
380 | + * |
|
381 | + * @return array |
|
382 | + */ |
|
383 | + private function get_entity_uris( $entity ) { |
|
384 | + $ids = $this->filter_valid_entity_ids( $entity ); |
|
385 | + $same_as = array_key_exists( 'sameAs', $entity ) ? $entity['sameAs'] : array(); |
|
386 | + |
|
387 | + return array_merge( |
|
388 | + (array) $ids, |
|
389 | + (array) $same_as |
|
390 | + ); |
|
391 | + } |
|
392 | + |
|
393 | + private function entity_id_valid( $entity_id ) { |
|
394 | + return preg_match( '#^https?://#i', $entity_id ) === 1; |
|
395 | + } |
|
396 | 396 | |
397 | 397 | } |
@@ -31,14 +31,14 @@ discard block |
||
31 | 31 | public function __construct() { |
32 | 32 | |
33 | 33 | // Bail out if block editor's functions aren't available. |
34 | - if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
34 | + if ( ! function_exists('register_block_type') || ! function_exists('parse_blocks')) { |
|
35 | 35 | return; |
36 | 36 | } |
37 | 37 | |
38 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
38 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
39 | 39 | |
40 | - add_action( 'init', array( $this, 'init' ) ); |
|
41 | - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10 ); |
|
40 | + add_action('init', array($this, 'init')); |
|
41 | + add_filter('wp_insert_post_data', array($this, 'wp_insert_post_data'), 10); |
|
42 | 42 | |
43 | 43 | } |
44 | 44 | |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | array( |
54 | 54 | 'editor_script' => 'wl-block-editor', |
55 | 55 | 'attributes' => array( |
56 | - 'entities' => array( 'type' => 'array' ), |
|
56 | + 'entities' => array('type' => 'array'), |
|
57 | 57 | ), |
58 | 58 | ) |
59 | 59 | ); |
@@ -89,40 +89,40 @@ discard block |
||
89 | 89 | * |
90 | 90 | * @return array The data array. |
91 | 91 | */ |
92 | - public function wp_insert_post_data( $data ) { |
|
92 | + public function wp_insert_post_data($data) { |
|
93 | 93 | $post_status = $data['post_status']; |
94 | - if ( 'auto-draft' === $post_status || 'inherit' === $post_status ) { |
|
94 | + if ('auto-draft' === $post_status || 'inherit' === $post_status) { |
|
95 | 95 | return $data; |
96 | 96 | } |
97 | 97 | |
98 | 98 | try { |
99 | - $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
99 | + $entities = $this->parse_content(wp_unslash($data['post_content'])); |
|
100 | 100 | |
101 | - foreach ( $entities as $entity ) { |
|
101 | + foreach ($entities as $entity) { |
|
102 | 102 | |
103 | - $entity_id = array_key_exists( 'id', $entity ) ? $entity['id'] : ''; |
|
103 | + $entity_id = array_key_exists('id', $entity) ? $entity['id'] : ''; |
|
104 | 104 | |
105 | - if ( ! $this->entity_id_valid( $entity_id ) ) { |
|
105 | + if ( ! $this->entity_id_valid($entity_id)) { |
|
106 | 106 | continue; |
107 | 107 | } |
108 | 108 | |
109 | - $entity_uris = $this->get_entity_uris( $entity ); |
|
109 | + $entity_uris = $this->get_entity_uris($entity); |
|
110 | 110 | |
111 | - if ( $this->get_first_matching_entity_by_uri( $entity_uris ) === null && |
|
112 | - Post_Entities_Validator::is_local_entity_uri_exist( Wordlift_Entity_Uri_Service::get_instance(), $entity_uris ) ) { |
|
111 | + if ($this->get_first_matching_entity_by_uri($entity_uris) === null && |
|
112 | + Post_Entities_Validator::is_local_entity_uri_exist(Wordlift_Entity_Uri_Service::get_instance(), $entity_uris)) { |
|
113 | 113 | // Skip the entity |
114 | 114 | continue; |
115 | 115 | } |
116 | 116 | |
117 | 117 | // If 'entity auto publish' is false, we set the status to `draft` by default. |
118 | 118 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
119 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
119 | + $post_status = apply_filters('wl_feature__enable__entity-auto-publish', true) |
|
120 | 120 | ? $data['post_status'] : 'draft'; |
121 | - $this->create_or_update_entity( $entity, $post_status ); |
|
121 | + $this->create_or_update_entity($entity, $post_status); |
|
122 | 122 | |
123 | 123 | } |
124 | - } catch ( \Exception $e ) { |
|
125 | - $this->log->error( $e->getMessage() ); |
|
124 | + } catch (\Exception $e) { |
|
125 | + $this->log->error($e->getMessage()); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | return $data; |
@@ -136,26 +136,26 @@ discard block |
||
136 | 136 | * @return array An array of entities' structures. |
137 | 137 | * @throws \Exception when an error occurs. |
138 | 138 | */ |
139 | - private function parse_content( $post_content ) { |
|
139 | + private function parse_content($post_content) { |
|
140 | 140 | |
141 | - $all_blocks = parse_blocks( $post_content ); |
|
141 | + $all_blocks = parse_blocks($post_content); |
|
142 | 142 | |
143 | 143 | $blocks = array_filter( |
144 | 144 | $all_blocks, |
145 | - function ( $item ) { |
|
146 | - return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
145 | + function($item) { |
|
146 | + return ! empty($item['blockName']) && 'wordlift/classification' === $item['blockName']; |
|
147 | 147 | } |
148 | 148 | ); |
149 | 149 | |
150 | 150 | // Bail out if the blocks' array is empty. |
151 | - if ( empty( $blocks ) ) { |
|
151 | + if (empty($blocks)) { |
|
152 | 152 | return array(); |
153 | 153 | } |
154 | 154 | |
155 | - $block = current( $blocks ); |
|
155 | + $block = current($blocks); |
|
156 | 156 | |
157 | 157 | // Bail out if the entities array is empty. |
158 | - if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
158 | + if (empty($block['attrs']) && empty($block['attrs']['entities'])) { |
|
159 | 159 | return array(); |
160 | 160 | } |
161 | 161 | |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | * |
201 | 201 | * @return array An array of labels. |
202 | 202 | */ |
203 | - public function get_labels( $entity ) { |
|
203 | + public function get_labels($entity) { |
|
204 | 204 | |
205 | 205 | $args = wp_parse_args( |
206 | 206 | $entity, |
@@ -222,17 +222,17 @@ discard block |
||
222 | 222 | |
223 | 223 | return array_reduce( |
224 | 224 | $args['occurrences'], |
225 | - function ( $carry, $item ) use ( $annotations ) { |
|
225 | + function($carry, $item) use ($annotations) { |
|
226 | 226 | |
227 | 227 | // Bail out if occurrences->$item->text isn't set or its contents are already |
228 | 228 | // in `$carry`. |
229 | - if ( ! isset( $annotations[ $item ]['text'] ) |
|
230 | - || in_array( $annotations[ $item ]['text'], $carry, true ) ) { |
|
229 | + if ( ! isset($annotations[$item]['text']) |
|
230 | + || in_array($annotations[$item]['text'], $carry, true)) { |
|
231 | 231 | return $carry; |
232 | 232 | } |
233 | 233 | |
234 | 234 | // Push the label. |
235 | - $carry[] = $annotations[ $item ]['text']; |
|
235 | + $carry[] = $annotations[$item]['text']; |
|
236 | 236 | |
237 | 237 | return $carry; |
238 | 238 | }, |
@@ -261,18 +261,18 @@ discard block |
||
261 | 261 | * @return int|\WP_Error |
262 | 262 | * @throws \Exception when an error occurs. |
263 | 263 | */ |
264 | - private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
264 | + private function create_or_update_entity($entity, $post_status = 'draft') { |
|
265 | 265 | |
266 | 266 | // Get only valid IDs. |
267 | - $uris = $this->get_entity_uris( $entity ); |
|
267 | + $uris = $this->get_entity_uris($entity); |
|
268 | 268 | |
269 | - $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
269 | + $post = $this->get_first_matching_entity_by_uri($uris); |
|
270 | 270 | |
271 | 271 | // Get the labels. |
272 | - $labels = $this->get_labels( $entity ); |
|
272 | + $labels = $this->get_labels($entity); |
|
273 | 273 | |
274 | - if ( empty( $post ) ) { |
|
275 | - $entity_description = array_key_exists( 'description', $entity ) ? $entity['description'] : ''; |
|
274 | + if (empty($post)) { |
|
275 | + $entity_description = array_key_exists('description', $entity) ? $entity['description'] : ''; |
|
276 | 276 | // Create the entity if it doesn't exist. |
277 | 277 | $post_id = Entity_Store::get_instance()->create( |
278 | 278 | array( |
@@ -284,18 +284,18 @@ discard block |
||
284 | 284 | ); |
285 | 285 | |
286 | 286 | // Return the WP_Error if we got one. |
287 | - if ( is_wp_error( $post_id ) ) { |
|
287 | + if (is_wp_error($post_id)) { |
|
288 | 288 | return $post_id; |
289 | 289 | } |
290 | 290 | |
291 | 291 | // Add the entity type. |
292 | - if ( isset( $entity['mainType'] ) ) { |
|
293 | - wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
292 | + if (isset($entity['mainType'])) { |
|
293 | + wp_set_object_terms($post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
294 | 294 | } |
295 | 295 | |
296 | - if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
297 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
298 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
296 | + if (isset($entity['properties']) && isset($entity['properties']['latitude']) && isset($entity['properties']['longitude'])) { |
|
297 | + add_post_meta($post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude']); |
|
298 | + add_post_meta($post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude']); |
|
299 | 299 | } |
300 | 300 | } else { |
301 | 301 | // Update the entity otherwise. |
@@ -308,8 +308,8 @@ discard block |
||
308 | 308 | ); |
309 | 309 | |
310 | 310 | // Add the entity type. |
311 | - if ( isset( $entity['mainType'] ) ) { |
|
312 | - wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
311 | + if (isset($entity['mainType'])) { |
|
312 | + wp_add_object_terms($post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
313 | 313 | } |
314 | 314 | |
315 | 315 | // see https://github.com/insideout10/wordlift-plugin/issues/1304 |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | // created using rest endpoint on block editor, so that they get published |
318 | 318 | // when the post is published. |
319 | 319 | // Once the entity is published don't update the post status. |
320 | - if ( 'publish' !== $post->post_status ) { |
|
320 | + if ('publish' !== $post->post_status) { |
|
321 | 321 | wp_update_post( |
322 | 322 | array( |
323 | 323 | 'ID' => $post->ID, |
@@ -339,19 +339,19 @@ discard block |
||
339 | 339 | * |
340 | 340 | * @return \WP_Post|null The entity WP_Post if found or null if not found. |
341 | 341 | */ |
342 | - private function get_first_matching_entity_by_uri( $uris ) { |
|
342 | + private function get_first_matching_entity_by_uri($uris) { |
|
343 | 343 | |
344 | - foreach ( $uris as $uri ) { |
|
344 | + foreach ($uris as $uri) { |
|
345 | 345 | |
346 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
346 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as($uri); |
|
347 | 347 | |
348 | - if ( ! $content ) { |
|
348 | + if ( ! $content) { |
|
349 | 349 | continue; |
350 | 350 | } |
351 | 351 | |
352 | 352 | $existing_entity = $content->get_bag(); |
353 | 353 | |
354 | - if ( is_a( $existing_entity, 'WP_Post' ) ) { |
|
354 | + if (is_a($existing_entity, 'WP_Post')) { |
|
355 | 355 | return $existing_entity; |
356 | 356 | } |
357 | 357 | } |
@@ -364,13 +364,13 @@ discard block |
||
364 | 364 | * |
365 | 365 | * @return array |
366 | 366 | */ |
367 | - private function filter_valid_entity_ids( $entity ) { |
|
367 | + private function filter_valid_entity_ids($entity) { |
|
368 | 368 | $id = $entity['id']; |
369 | 369 | |
370 | 370 | return array_filter( |
371 | 371 | (array) $id, |
372 | - function ( $id ) { |
|
373 | - return preg_match( '|^https?://|', $id ); |
|
372 | + function($id) { |
|
373 | + return preg_match('|^https?://|', $id); |
|
374 | 374 | } |
375 | 375 | ); |
376 | 376 | } |
@@ -380,9 +380,9 @@ discard block |
||
380 | 380 | * |
381 | 381 | * @return array |
382 | 382 | */ |
383 | - private function get_entity_uris( $entity ) { |
|
384 | - $ids = $this->filter_valid_entity_ids( $entity ); |
|
385 | - $same_as = array_key_exists( 'sameAs', $entity ) ? $entity['sameAs'] : array(); |
|
383 | + private function get_entity_uris($entity) { |
|
384 | + $ids = $this->filter_valid_entity_ids($entity); |
|
385 | + $same_as = array_key_exists('sameAs', $entity) ? $entity['sameAs'] : array(); |
|
386 | 386 | |
387 | 387 | return array_merge( |
388 | 388 | (array) $ids, |
@@ -390,8 +390,8 @@ discard block |
||
390 | 390 | ); |
391 | 391 | } |
392 | 392 | |
393 | - private function entity_id_valid( $entity_id ) { |
|
394 | - return preg_match( '#^https?://#i', $entity_id ) === 1; |
|
393 | + private function entity_id_valid($entity_id) { |
|
394 | + return preg_match('#^https?://#i', $entity_id) === 1; |
|
395 | 395 | } |
396 | 396 | |
397 | 397 | } |