@@ -6,24 +6,24 @@ |
||
6 | 6 | |
7 | 7 | abstract class Abstract_Relation_Service implements Relation_Service_Interface { |
8 | 8 | |
9 | - protected function __construct() { |
|
9 | + protected function __construct() { |
|
10 | 10 | |
11 | - } |
|
11 | + } |
|
12 | 12 | |
13 | - /** |
|
14 | - * A default implementation for the `get_relations` method that creates `Relations` instance |
|
15 | - * and populates it with `Relation`s. |
|
16 | - * |
|
17 | - * @param Wordpress_Content_Id $content_id The WordPress content id. |
|
18 | - * |
|
19 | - * @return Relations_Interface A `Relations` instance. |
|
20 | - */ |
|
21 | - public function get_relations( $content_id ) { |
|
22 | - $relations = new Relations(); |
|
13 | + /** |
|
14 | + * A default implementation for the `get_relations` method that creates `Relations` instance |
|
15 | + * and populates it with `Relation`s. |
|
16 | + * |
|
17 | + * @param Wordpress_Content_Id $content_id The WordPress content id. |
|
18 | + * |
|
19 | + * @return Relations_Interface A `Relations` instance. |
|
20 | + */ |
|
21 | + public function get_relations( $content_id ) { |
|
22 | + $relations = new Relations(); |
|
23 | 23 | |
24 | - $this->add_relations( $content_id, $relations ); |
|
24 | + $this->add_relations( $content_id, $relations ); |
|
25 | 25 | |
26 | - return $relations; |
|
27 | - } |
|
26 | + return $relations; |
|
27 | + } |
|
28 | 28 | |
29 | 29 | } |
@@ -18,10 +18,10 @@ |
||
18 | 18 | * |
19 | 19 | * @return Relations_Interface A `Relations` instance. |
20 | 20 | */ |
21 | - public function get_relations( $content_id ) { |
|
21 | + public function get_relations($content_id) { |
|
22 | 22 | $relations = new Relations(); |
23 | 23 | |
24 | - $this->add_relations( $content_id, $relations ); |
|
24 | + $this->add_relations($content_id, $relations); |
|
25 | 25 | |
26 | 26 | return $relations; |
27 | 27 | } |
@@ -18,55 +18,55 @@ |
||
18 | 18 | |
19 | 19 | class Post_Content_Relation_Service extends Abstract_Relation_Service { |
20 | 20 | |
21 | - private static $instance = null; |
|
21 | + private static $instance = null; |
|
22 | 22 | |
23 | - /** |
|
24 | - * The singleton instance. |
|
25 | - * |
|
26 | - * @return Relation_Service_Interface |
|
27 | - */ |
|
28 | - public static function get_instance() { |
|
29 | - if ( ! isset( self::$instance ) ) { |
|
30 | - self::$instance = new self(); |
|
31 | - } |
|
23 | + /** |
|
24 | + * The singleton instance. |
|
25 | + * |
|
26 | + * @return Relation_Service_Interface |
|
27 | + */ |
|
28 | + public static function get_instance() { |
|
29 | + if ( ! isset( self::$instance ) ) { |
|
30 | + self::$instance = new self(); |
|
31 | + } |
|
32 | 32 | |
33 | - return self::$instance; |
|
34 | - } |
|
33 | + return self::$instance; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param Wordpress_Content_Id $content_id |
|
38 | - * @param Relations_Interface $relations |
|
39 | - */ |
|
40 | - public function add_relations( $content_id, $relations ) { |
|
41 | - if ( $content_id->get_type() !== Object_Type_Enum::POST ) { |
|
42 | - return; |
|
43 | - } |
|
36 | + /** |
|
37 | + * @param Wordpress_Content_Id $content_id |
|
38 | + * @param Relations_Interface $relations |
|
39 | + */ |
|
40 | + public function add_relations( $content_id, $relations ) { |
|
41 | + if ( $content_id->get_type() !== Object_Type_Enum::POST ) { |
|
42 | + return; |
|
43 | + } |
|
44 | 44 | |
45 | - $post = get_post( $content_id->get_id() ); |
|
46 | - if ( ! is_a( $post, 'WP_Post' ) ) { |
|
47 | - return; |
|
48 | - } |
|
45 | + $post = get_post( $content_id->get_id() ); |
|
46 | + if ( ! is_a( $post, 'WP_Post' ) ) { |
|
47 | + return; |
|
48 | + } |
|
49 | 49 | |
50 | - $content_filter_service = \Wordlift_Content_Filter_Service::get_instance(); |
|
50 | + $content_filter_service = \Wordlift_Content_Filter_Service::get_instance(); |
|
51 | 51 | |
52 | - $post_content = wp_unslash( $post->post_content ); |
|
53 | - $entity_uris = $content_filter_service->get_entity_uris( $post_content ); |
|
54 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
55 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
52 | + $post_content = wp_unslash( $post->post_content ); |
|
53 | + $entity_uris = $content_filter_service->get_entity_uris( $post_content ); |
|
54 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
55 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
56 | 56 | |
57 | - foreach ( $entity_uris as $entity_uri ) { |
|
58 | - $content = $content_service->get_by_entity_id( $entity_uri ); |
|
59 | - if ( ! isset( $content ) ) { |
|
60 | - continue; |
|
61 | - } |
|
57 | + foreach ( $entity_uris as $entity_uri ) { |
|
58 | + $content = $content_service->get_by_entity_id( $entity_uri ); |
|
59 | + if ( ! isset( $content ) ) { |
|
60 | + continue; |
|
61 | + } |
|
62 | 62 | |
63 | - $object_id = new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ); |
|
63 | + $object_id = new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ); |
|
64 | 64 | |
65 | - $predicate = $entity_service->get_classification_scope_for( $content->get_id() ); |
|
66 | - $relation = new Relation( $content_id, $object_id, $predicate ); |
|
67 | - $relations->add( $relation ); |
|
68 | - } |
|
65 | + $predicate = $entity_service->get_classification_scope_for( $content->get_id() ); |
|
66 | + $relation = new Relation( $content_id, $object_id, $predicate ); |
|
67 | + $relations->add( $relation ); |
|
68 | + } |
|
69 | 69 | |
70 | - } |
|
70 | + } |
|
71 | 71 | |
72 | 72 | } |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | * @return Relation_Service_Interface |
27 | 27 | */ |
28 | 28 | public static function get_instance() { |
29 | - if ( ! isset( self::$instance ) ) { |
|
29 | + if ( ! isset(self::$instance)) { |
|
30 | 30 | self::$instance = new self(); |
31 | 31 | } |
32 | 32 | |
@@ -37,34 +37,34 @@ discard block |
||
37 | 37 | * @param Wordpress_Content_Id $content_id |
38 | 38 | * @param Relations_Interface $relations |
39 | 39 | */ |
40 | - public function add_relations( $content_id, $relations ) { |
|
41 | - if ( $content_id->get_type() !== Object_Type_Enum::POST ) { |
|
40 | + public function add_relations($content_id, $relations) { |
|
41 | + if ($content_id->get_type() !== Object_Type_Enum::POST) { |
|
42 | 42 | return; |
43 | 43 | } |
44 | 44 | |
45 | - $post = get_post( $content_id->get_id() ); |
|
46 | - if ( ! is_a( $post, 'WP_Post' ) ) { |
|
45 | + $post = get_post($content_id->get_id()); |
|
46 | + if ( ! is_a($post, 'WP_Post')) { |
|
47 | 47 | return; |
48 | 48 | } |
49 | 49 | |
50 | 50 | $content_filter_service = \Wordlift_Content_Filter_Service::get_instance(); |
51 | 51 | |
52 | - $post_content = wp_unslash( $post->post_content ); |
|
53 | - $entity_uris = $content_filter_service->get_entity_uris( $post_content ); |
|
52 | + $post_content = wp_unslash($post->post_content); |
|
53 | + $entity_uris = $content_filter_service->get_entity_uris($post_content); |
|
54 | 54 | $content_service = Wordpress_Content_Service::get_instance(); |
55 | 55 | $entity_service = Wordlift_Entity_Service::get_instance(); |
56 | 56 | |
57 | - foreach ( $entity_uris as $entity_uri ) { |
|
58 | - $content = $content_service->get_by_entity_id( $entity_uri ); |
|
59 | - if ( ! isset( $content ) ) { |
|
57 | + foreach ($entity_uris as $entity_uri) { |
|
58 | + $content = $content_service->get_by_entity_id($entity_uri); |
|
59 | + if ( ! isset($content)) { |
|
60 | 60 | continue; |
61 | 61 | } |
62 | 62 | |
63 | - $object_id = new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ); |
|
63 | + $object_id = new Wordpress_Content_Id($content->get_id(), $content->get_object_type_enum()); |
|
64 | 64 | |
65 | - $predicate = $entity_service->get_classification_scope_for( $content->get_id() ); |
|
66 | - $relation = new Relation( $content_id, $object_id, $predicate ); |
|
67 | - $relations->add( $relation ); |
|
65 | + $predicate = $entity_service->get_classification_scope_for($content->get_id()); |
|
66 | + $relation = new Relation($content_id, $object_id, $predicate); |
|
67 | + $relations->add($relation); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | } |
@@ -16,142 +16,142 @@ |
||
16 | 16 | use Wordlift\Content\Wordpress\Wordpress_Content_Id; |
17 | 17 | |
18 | 18 | class Relation implements Hashable_Interface, JsonSerializable { |
19 | - /** |
|
20 | - * @var Wordpress_Content_Id |
|
21 | - */ |
|
22 | - private $subject; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - private $predicate; |
|
28 | - |
|
29 | - /** |
|
30 | - * @param Wordpress_Content_Id $subject |
|
31 | - * @param Wordpress_Content_Id $object |
|
32 | - * @param string $predicate |
|
33 | - */ |
|
34 | - public function __construct( $subject, $object, $predicate ) { |
|
35 | - $this->subject = $subject; |
|
36 | - $this->predicate = $predicate; |
|
37 | - $this->object = $object; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * @var Wordpress_Content_Id |
|
42 | - */ |
|
43 | - private $object; |
|
44 | - |
|
45 | - public static function from_json( $item ) { |
|
46 | - $subject = Wordpress_Content_Id::from_json( $item['subject'] ); |
|
47 | - $object = Wordpress_Content_Id::from_json( $item['object'] ); |
|
48 | - $predicate = $item['predicate']; |
|
49 | - |
|
50 | - return new self( $subject, $object, $predicate ); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * @return Wordpress_Content_Id |
|
55 | - */ |
|
56 | - public function get_subject() { |
|
57 | - return $this->subject; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * @return string |
|
62 | - */ |
|
63 | - public function get_predicate() { |
|
64 | - return $this->predicate; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @return Wordpress_Content_Id |
|
69 | - */ |
|
70 | - public function get_object() { |
|
71 | - return $this->object; |
|
72 | - } |
|
73 | - |
|
74 | - public function hash() { |
|
75 | - // Define the hash algorithm for your object |
|
76 | - // Here's an example using the md5 hash function |
|
77 | - return md5( |
|
78 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
79 | - serialize( |
|
80 | - array( |
|
81 | - $this->get_subject(), |
|
82 | - $this->get_predicate(), |
|
83 | - $this->get_object(), |
|
84 | - ) |
|
85 | - ) |
|
86 | - ); |
|
87 | - } |
|
88 | - |
|
89 | - public function equals( Hashable_Interface $obj ) { |
|
90 | - return $this->hash() === $obj->hash(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @return string |
|
95 | - * @deprecated used for legacy compatibilty |
|
96 | - */ |
|
97 | - public function get_relation_type() { |
|
98 | - return $this->predicate; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @return int |
|
103 | - * @deprecated used for legacy compatibility |
|
104 | - */ |
|
105 | - public function get_object_id() { |
|
106 | - return $this->object->get_id(); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Subject type. |
|
111 | - * |
|
112 | - * @deprecated used for legacy compatibility |
|
113 | - */ |
|
114 | - public function get_subject_type() { |
|
115 | - return $this->subject->get_type(); |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Object type. |
|
120 | - * |
|
121 | - * @deprecated used for legacy compatibility |
|
122 | - */ |
|
123 | - public function get_object_type() { |
|
124 | - return $this->object->get_type(); |
|
125 | - } |
|
126 | - |
|
127 | - public static function from_relation_instances( $instance ) { |
|
128 | - Assertions::is_set( $instance->subject_id ); |
|
129 | - Assertions::is_set( $instance->subject_type ); |
|
130 | - Assertions::is_set( $instance->predicate ); |
|
131 | - Assertions::is_set( $instance->object_id ); |
|
132 | - Assertions::is_set( $instance->object_type ); |
|
133 | - |
|
134 | - return new self( |
|
135 | - // Subject. |
|
136 | - new Wordpress_Content_Id( |
|
137 | - $instance->subject_id, |
|
138 | - $instance->subject_type |
|
139 | - ), |
|
140 | - // Object. |
|
141 | - new Wordpress_Content_Id( |
|
142 | - $instance->object_id, |
|
143 | - $instance->object_type |
|
144 | - ), |
|
145 | - // Predicate. |
|
146 | - $instance->predicate |
|
147 | - ); |
|
148 | - } |
|
149 | - |
|
150 | - public function jsonSerialize() { |
|
151 | - return array( |
|
152 | - 'subject' => $this->get_subject(), |
|
153 | - 'object' => $this->get_object(), |
|
154 | - 'predicate' => $this->get_predicate(), |
|
155 | - ); |
|
156 | - } |
|
19 | + /** |
|
20 | + * @var Wordpress_Content_Id |
|
21 | + */ |
|
22 | + private $subject; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + private $predicate; |
|
28 | + |
|
29 | + /** |
|
30 | + * @param Wordpress_Content_Id $subject |
|
31 | + * @param Wordpress_Content_Id $object |
|
32 | + * @param string $predicate |
|
33 | + */ |
|
34 | + public function __construct( $subject, $object, $predicate ) { |
|
35 | + $this->subject = $subject; |
|
36 | + $this->predicate = $predicate; |
|
37 | + $this->object = $object; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * @var Wordpress_Content_Id |
|
42 | + */ |
|
43 | + private $object; |
|
44 | + |
|
45 | + public static function from_json( $item ) { |
|
46 | + $subject = Wordpress_Content_Id::from_json( $item['subject'] ); |
|
47 | + $object = Wordpress_Content_Id::from_json( $item['object'] ); |
|
48 | + $predicate = $item['predicate']; |
|
49 | + |
|
50 | + return new self( $subject, $object, $predicate ); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * @return Wordpress_Content_Id |
|
55 | + */ |
|
56 | + public function get_subject() { |
|
57 | + return $this->subject; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * @return string |
|
62 | + */ |
|
63 | + public function get_predicate() { |
|
64 | + return $this->predicate; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @return Wordpress_Content_Id |
|
69 | + */ |
|
70 | + public function get_object() { |
|
71 | + return $this->object; |
|
72 | + } |
|
73 | + |
|
74 | + public function hash() { |
|
75 | + // Define the hash algorithm for your object |
|
76 | + // Here's an example using the md5 hash function |
|
77 | + return md5( |
|
78 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
79 | + serialize( |
|
80 | + array( |
|
81 | + $this->get_subject(), |
|
82 | + $this->get_predicate(), |
|
83 | + $this->get_object(), |
|
84 | + ) |
|
85 | + ) |
|
86 | + ); |
|
87 | + } |
|
88 | + |
|
89 | + public function equals( Hashable_Interface $obj ) { |
|
90 | + return $this->hash() === $obj->hash(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @return string |
|
95 | + * @deprecated used for legacy compatibilty |
|
96 | + */ |
|
97 | + public function get_relation_type() { |
|
98 | + return $this->predicate; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @return int |
|
103 | + * @deprecated used for legacy compatibility |
|
104 | + */ |
|
105 | + public function get_object_id() { |
|
106 | + return $this->object->get_id(); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Subject type. |
|
111 | + * |
|
112 | + * @deprecated used for legacy compatibility |
|
113 | + */ |
|
114 | + public function get_subject_type() { |
|
115 | + return $this->subject->get_type(); |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Object type. |
|
120 | + * |
|
121 | + * @deprecated used for legacy compatibility |
|
122 | + */ |
|
123 | + public function get_object_type() { |
|
124 | + return $this->object->get_type(); |
|
125 | + } |
|
126 | + |
|
127 | + public static function from_relation_instances( $instance ) { |
|
128 | + Assertions::is_set( $instance->subject_id ); |
|
129 | + Assertions::is_set( $instance->subject_type ); |
|
130 | + Assertions::is_set( $instance->predicate ); |
|
131 | + Assertions::is_set( $instance->object_id ); |
|
132 | + Assertions::is_set( $instance->object_type ); |
|
133 | + |
|
134 | + return new self( |
|
135 | + // Subject. |
|
136 | + new Wordpress_Content_Id( |
|
137 | + $instance->subject_id, |
|
138 | + $instance->subject_type |
|
139 | + ), |
|
140 | + // Object. |
|
141 | + new Wordpress_Content_Id( |
|
142 | + $instance->object_id, |
|
143 | + $instance->object_type |
|
144 | + ), |
|
145 | + // Predicate. |
|
146 | + $instance->predicate |
|
147 | + ); |
|
148 | + } |
|
149 | + |
|
150 | + public function jsonSerialize() { |
|
151 | + return array( |
|
152 | + 'subject' => $this->get_subject(), |
|
153 | + 'object' => $this->get_object(), |
|
154 | + 'predicate' => $this->get_predicate(), |
|
155 | + ); |
|
156 | + } |
|
157 | 157 | } |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * @param Wordpress_Content_Id $object |
32 | 32 | * @param string $predicate |
33 | 33 | */ |
34 | - public function __construct( $subject, $object, $predicate ) { |
|
34 | + public function __construct($subject, $object, $predicate) { |
|
35 | 35 | $this->subject = $subject; |
36 | 36 | $this->predicate = $predicate; |
37 | 37 | $this->object = $object; |
@@ -42,12 +42,12 @@ discard block |
||
42 | 42 | */ |
43 | 43 | private $object; |
44 | 44 | |
45 | - public static function from_json( $item ) { |
|
46 | - $subject = Wordpress_Content_Id::from_json( $item['subject'] ); |
|
47 | - $object = Wordpress_Content_Id::from_json( $item['object'] ); |
|
45 | + public static function from_json($item) { |
|
46 | + $subject = Wordpress_Content_Id::from_json($item['subject']); |
|
47 | + $object = Wordpress_Content_Id::from_json($item['object']); |
|
48 | 48 | $predicate = $item['predicate']; |
49 | 49 | |
50 | - return new self( $subject, $object, $predicate ); |
|
50 | + return new self($subject, $object, $predicate); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | /** |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | ); |
87 | 87 | } |
88 | 88 | |
89 | - public function equals( Hashable_Interface $obj ) { |
|
89 | + public function equals(Hashable_Interface $obj) { |
|
90 | 90 | return $this->hash() === $obj->hash(); |
91 | 91 | } |
92 | 92 | |
@@ -124,12 +124,12 @@ discard block |
||
124 | 124 | return $this->object->get_type(); |
125 | 125 | } |
126 | 126 | |
127 | - public static function from_relation_instances( $instance ) { |
|
128 | - Assertions::is_set( $instance->subject_id ); |
|
129 | - Assertions::is_set( $instance->subject_type ); |
|
130 | - Assertions::is_set( $instance->predicate ); |
|
131 | - Assertions::is_set( $instance->object_id ); |
|
132 | - Assertions::is_set( $instance->object_type ); |
|
127 | + public static function from_relation_instances($instance) { |
|
128 | + Assertions::is_set($instance->subject_id); |
|
129 | + Assertions::is_set($instance->subject_type); |
|
130 | + Assertions::is_set($instance->predicate); |
|
131 | + Assertions::is_set($instance->object_id); |
|
132 | + Assertions::is_set($instance->object_type); |
|
133 | 133 | |
134 | 134 | return new self( |
135 | 135 | // Subject. |
@@ -6,49 +6,49 @@ |
||
6 | 6 | */ |
7 | 7 | class Wordlift_Install_3_32_0 extends Wordlift_Install { |
8 | 8 | |
9 | - /** |
|
10 | - * {@inheritdoc} |
|
11 | - */ |
|
12 | - protected static $version = '3.32.0'; |
|
13 | - |
|
14 | - public static function is_column_exists( $column_name ) { |
|
15 | - global $wpdb; |
|
16 | - |
|
17 | - return $wpdb->get_results( |
|
18 | - $wpdb->prepare( |
|
19 | - "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='{$wpdb->prefix}wl_relation_instances' AND column_name = %s", |
|
20 | - $column_name |
|
21 | - ) |
|
22 | - ); |
|
23 | - |
|
24 | - } |
|
25 | - |
|
26 | - public function install() { |
|
27 | - global $wpdb; |
|
28 | - // Allocate only 8 bytes, represent 2 ^ 8 values in signed form ( -128 to 127 ) |
|
29 | - // we default to 0 here, because we are going to represent Object_Type_Enum in |
|
30 | - // this field |
|
31 | - // const POST = 0; |
|
32 | - // const TERM = 1; |
|
33 | - // const HOMEPAGE = 2; |
|
34 | - // const USER = 3; |
|
35 | - // we add 0 as default since we want to add compat between old and new values. |
|
36 | - |
|
37 | - if ( ! self::is_column_exists( 'object_type' ) ) { |
|
38 | - // Add object_type column |
|
39 | - $wpdb->query( |
|
40 | - "ALTER TABLE {$wpdb->prefix}wl_relation_instances |
|
9 | + /** |
|
10 | + * {@inheritdoc} |
|
11 | + */ |
|
12 | + protected static $version = '3.32.0'; |
|
13 | + |
|
14 | + public static function is_column_exists( $column_name ) { |
|
15 | + global $wpdb; |
|
16 | + |
|
17 | + return $wpdb->get_results( |
|
18 | + $wpdb->prepare( |
|
19 | + "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='{$wpdb->prefix}wl_relation_instances' AND column_name = %s", |
|
20 | + $column_name |
|
21 | + ) |
|
22 | + ); |
|
23 | + |
|
24 | + } |
|
25 | + |
|
26 | + public function install() { |
|
27 | + global $wpdb; |
|
28 | + // Allocate only 8 bytes, represent 2 ^ 8 values in signed form ( -128 to 127 ) |
|
29 | + // we default to 0 here, because we are going to represent Object_Type_Enum in |
|
30 | + // this field |
|
31 | + // const POST = 0; |
|
32 | + // const TERM = 1; |
|
33 | + // const HOMEPAGE = 2; |
|
34 | + // const USER = 3; |
|
35 | + // we add 0 as default since we want to add compat between old and new values. |
|
36 | + |
|
37 | + if ( ! self::is_column_exists( 'object_type' ) ) { |
|
38 | + // Add object_type column |
|
39 | + $wpdb->query( |
|
40 | + "ALTER TABLE {$wpdb->prefix}wl_relation_instances |
|
41 | 41 | ADD object_type TINYINT DEFAULT 0;" |
42 | - ); |
|
43 | - } |
|
42 | + ); |
|
43 | + } |
|
44 | 44 | |
45 | - if ( ! self::is_column_exists( 'subject_type' ) ) { |
|
46 | - // Add subject_type column. |
|
47 | - $wpdb->query( |
|
48 | - "ALTER TABLE {$wpdb->prefix}wl_relation_instances |
|
45 | + if ( ! self::is_column_exists( 'subject_type' ) ) { |
|
46 | + // Add subject_type column. |
|
47 | + $wpdb->query( |
|
48 | + "ALTER TABLE {$wpdb->prefix}wl_relation_instances |
|
49 | 49 | ADD subject_type TINYINT DEFAULT 0;" |
50 | - ); |
|
51 | - } |
|
52 | - } |
|
50 | + ); |
|
51 | + } |
|
52 | + } |
|
53 | 53 | |
54 | 54 | } |
@@ -23,61 +23,61 @@ discard block |
||
23 | 23 | */ |
24 | 24 | function wl_linked_data_save_post( $post_id ) { |
25 | 25 | |
26 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
27 | - |
|
28 | - $log->trace( "Saving post $post_id to Linked Data..." ); |
|
29 | - |
|
30 | - // If it's not numeric exit from here. |
|
31 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
32 | - $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
33 | - |
|
34 | - return; |
|
35 | - } |
|
36 | - |
|
37 | - // Get the post type and check whether it supports the editor. |
|
38 | - // |
|
39 | - // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
|
40 | - $post_type = get_post_type( $post_id ); |
|
41 | - /** |
|
42 | - * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
|
43 | - * |
|
44 | - * @since 3.19.4 |
|
45 | - * |
|
46 | - * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
47 | - */ |
|
48 | - $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
49 | - |
|
50 | - $is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ); |
|
51 | - // Bail out if it's not an entity. |
|
52 | - if ( ! $is_editor_supported |
|
53 | - && ! $is_no_editor_analysis_enabled ) { |
|
54 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
55 | - |
|
56 | - return; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Only process valid post types |
|
61 | - * |
|
62 | - * @since 3.25.6 |
|
63 | - */ |
|
64 | - $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
65 | - |
|
66 | - // Bail out if it's not a valid entity. |
|
67 | - if ( ! in_array( $post_type, $supported_types, true ) && ! $is_no_editor_analysis_enabled ) { |
|
68 | - $log->debug( "Skipping $post_id, because $post_type is not a valid entity." ); |
|
69 | - |
|
70 | - return; |
|
71 | - } |
|
72 | - |
|
73 | - // Unhook this function so it doesn't loop infinitely. |
|
74 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
75 | - |
|
76 | - // raise the *wl_linked_data_save_post* event. |
|
77 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
78 | - |
|
79 | - // Re-hook this function. |
|
80 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
26 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
27 | + |
|
28 | + $log->trace( "Saving post $post_id to Linked Data..." ); |
|
29 | + |
|
30 | + // If it's not numeric exit from here. |
|
31 | + if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
32 | + $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
33 | + |
|
34 | + return; |
|
35 | + } |
|
36 | + |
|
37 | + // Get the post type and check whether it supports the editor. |
|
38 | + // |
|
39 | + // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
|
40 | + $post_type = get_post_type( $post_id ); |
|
41 | + /** |
|
42 | + * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
|
43 | + * |
|
44 | + * @since 3.19.4 |
|
45 | + * |
|
46 | + * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
|
47 | + */ |
|
48 | + $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
49 | + |
|
50 | + $is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ); |
|
51 | + // Bail out if it's not an entity. |
|
52 | + if ( ! $is_editor_supported |
|
53 | + && ! $is_no_editor_analysis_enabled ) { |
|
54 | + $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
55 | + |
|
56 | + return; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Only process valid post types |
|
61 | + * |
|
62 | + * @since 3.25.6 |
|
63 | + */ |
|
64 | + $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
65 | + |
|
66 | + // Bail out if it's not a valid entity. |
|
67 | + if ( ! in_array( $post_type, $supported_types, true ) && ! $is_no_editor_analysis_enabled ) { |
|
68 | + $log->debug( "Skipping $post_id, because $post_type is not a valid entity." ); |
|
69 | + |
|
70 | + return; |
|
71 | + } |
|
72 | + |
|
73 | + // Unhook this function so it doesn't loop infinitely. |
|
74 | + remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
75 | + |
|
76 | + // raise the *wl_linked_data_save_post* event. |
|
77 | + do_action( 'wl_linked_data_save_post', $post_id ); |
|
78 | + |
|
79 | + // Re-hook this function. |
|
80 | + add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | add_action( 'save_post', 'wl_linked_data_save_post' ); |
@@ -93,204 +93,204 @@ discard block |
||
93 | 93 | */ |
94 | 94 | function wl_linked_data_save_post_and_related_entities( $post_id ) { |
95 | 95 | |
96 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
97 | - |
|
98 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
99 | - |
|
100 | - // Ignore auto-saves |
|
101 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
102 | - $log->trace( 'Doing autosave, skipping...' ); |
|
103 | - |
|
104 | - return; |
|
105 | - } |
|
106 | - |
|
107 | - // get the current post. |
|
108 | - $post = get_post( $post_id ); |
|
109 | - |
|
110 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
111 | - |
|
112 | - // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
113 | - |
|
114 | - // Get the entity service instance. |
|
115 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
116 | - $uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
117 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
118 | - |
|
119 | - // Store mapping between tmp new entities uris and real new entities uri |
|
120 | - $entities_uri_mapping = array(); |
|
121 | - |
|
122 | - // Save all the selected internal entity uris to this variable. |
|
123 | - $internal_entity_uris = array(); |
|
124 | - |
|
125 | - // Save the entities coming with POST data. |
|
126 | - if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
127 | - $data = filter_var_array( $_POST, array( 'wl_entities' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
128 | - $entities_via_post = $data['wl_entities']; |
|
129 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
130 | - wl_write_log( wp_json_encode( $entities_via_post ) ); |
|
131 | - wl_write_log( ']' ); |
|
132 | - |
|
133 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
134 | - |
|
135 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) { |
|
136 | - $existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() ); |
|
137 | - if ( isset( $existing_entity ) ) { |
|
138 | - $existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID ); |
|
139 | - // Type doesn't match, continue to create a new entity. |
|
140 | - if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) { |
|
141 | - $existing_entity = null; |
|
142 | - } |
|
143 | - } |
|
144 | - } else { |
|
145 | - // Look if current entity uri matches an internal existing entity, meaning: |
|
146 | - // 1. when $entity_uri is an internal uri |
|
147 | - // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
148 | - $existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
149 | - } |
|
150 | - |
|
151 | - // Don't save the entities which are not found, but also local. |
|
152 | - if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) { |
|
153 | - $internal_entity_uris[] = $entity_uri; |
|
154 | - continue; |
|
155 | - } |
|
156 | - |
|
157 | - if ( ! isset( $existing_entity ) ) { |
|
158 | - // Update entity data with related post |
|
159 | - $entity['related_post_id'] = $post_id; |
|
160 | - // New entity, save it. |
|
161 | - $existing_entity = wl_save_entity( $entity ); |
|
162 | - } else { |
|
163 | - // Existing entity, update post status. |
|
164 | - if ( $existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status ) { |
|
165 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
166 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
167 | - ? $post->post_status : 'draft'; |
|
168 | - wl_update_post_status( $existing_entity->ID, $post_status ); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - $uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) ); |
|
173 | - |
|
174 | - $internal_entity_uris[] = $uri; |
|
175 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
176 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
177 | - |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - // Replace tmp uris in content post if needed |
|
182 | - $updated_post_content = $post->post_content; |
|
183 | - |
|
184 | - // Update the post content if we found mappings of new entities. |
|
185 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
186 | - // Save each entity and store the post id. |
|
187 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
188 | - if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) { |
|
189 | - continue; |
|
190 | - } |
|
191 | - |
|
192 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
193 | - } |
|
194 | - |
|
195 | - // Update the post content. |
|
196 | - /** |
|
197 | - * Note: wp_update_post do stripslashes before saving the |
|
198 | - * content, so add the slashes to prevent back slash getting |
|
199 | - * removed. |
|
200 | - */ |
|
201 | - wp_update_post( |
|
202 | - array( |
|
203 | - 'ID' => $post->ID, |
|
204 | - 'post_content' => addslashes( $updated_post_content ), |
|
205 | - ) |
|
206 | - ); |
|
207 | - } |
|
208 | - |
|
209 | - // Reset previously saved instances. |
|
210 | - wl_core_delete_relation_instances( $post_id ); |
|
211 | - |
|
212 | - $content_id = Wordpress_Content_Id::create_post( $post->ID ); |
|
213 | - $relations = Relation_Service::get_instance()->get_relations( $content_id ); |
|
214 | - |
|
215 | - if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) { |
|
216 | - $relations->add( ...Relation_Service::get_relations_from_uris( $content_id, $internal_entity_uris ) ); |
|
217 | - } |
|
218 | - /** |
|
219 | - * Filter the relations, we dont want to create a relation |
|
220 | - * to uncategorized term, we are already filtering this on jsonld, |
|
221 | - * |
|
222 | - * @todo: do i need to move this to post-term-relation-service ? |
|
223 | - */ |
|
224 | - $filtered_relations = array_filter( |
|
225 | - $relations->toArray(), |
|
226 | - function ( $item ) { |
|
227 | - /** |
|
228 | - * @var $item Relation |
|
229 | - */ |
|
230 | - $object = $item->get_object(); |
|
231 | - |
|
232 | - return ! ( $object->get_type() === Object_Type_Enum::TERM |
|
233 | - && $object->get_id() === 1 ); |
|
234 | - } |
|
235 | - ); |
|
236 | - |
|
237 | - // Save relation instances |
|
238 | - /** @var Relation $relation */ |
|
239 | - foreach ( $filtered_relations as $relation ) { |
|
240 | - $subject = $relation->get_subject(); |
|
241 | - $object = $relation->get_object(); |
|
242 | - |
|
243 | - wl_core_add_relation_instance( |
|
244 | - // subject id. |
|
245 | - $subject->get_id(), |
|
246 | - // what, where, when, who |
|
247 | - $relation->get_predicate(), |
|
248 | - // object id. |
|
249 | - $object->get_id(), |
|
250 | - // Subject type. |
|
251 | - $subject->get_type(), |
|
252 | - // Object type. |
|
253 | - $object->get_type() |
|
254 | - ); |
|
255 | - |
|
256 | - } |
|
257 | - |
|
258 | - if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
259 | - // Save post metadata if available |
|
260 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? filter_input_array( INPUT_POST, array( 'wl_metadata' => FILTER_DEFAULT ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
261 | - |
|
262 | - $fields = array( |
|
263 | - Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
264 | - Wordlift_Schema_Service::FIELD_TOPIC, |
|
265 | - ); |
|
266 | - |
|
267 | - // Unlink topic taxonomy terms |
|
268 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
269 | - |
|
270 | - foreach ( $fields as $field ) { |
|
271 | - |
|
272 | - // Delete current values |
|
273 | - delete_post_meta( $post->ID, $field ); |
|
274 | - // Retrieve the entity uri |
|
275 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
276 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
277 | - |
|
278 | - if ( empty( $uri ) ) { |
|
279 | - continue; |
|
280 | - } |
|
281 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
282 | - |
|
283 | - if ( $entity ) { |
|
284 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
285 | - // Set also the topic taxonomy |
|
286 | - if ( Wordlift_Schema_Service::FIELD_TOPIC === $field ) { |
|
287 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
288 | - } |
|
289 | - } |
|
290 | - } |
|
291 | - } |
|
292 | - |
|
293 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
96 | + $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
97 | + |
|
98 | + $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
99 | + |
|
100 | + // Ignore auto-saves |
|
101 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
102 | + $log->trace( 'Doing autosave, skipping...' ); |
|
103 | + |
|
104 | + return; |
|
105 | + } |
|
106 | + |
|
107 | + // get the current post. |
|
108 | + $post = get_post( $post_id ); |
|
109 | + |
|
110 | + remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
111 | + |
|
112 | + // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
|
113 | + |
|
114 | + // Get the entity service instance. |
|
115 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
116 | + $uri_service = Wordlift_Entity_Uri_Service::get_instance(); |
|
117 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
118 | + |
|
119 | + // Store mapping between tmp new entities uris and real new entities uri |
|
120 | + $entities_uri_mapping = array(); |
|
121 | + |
|
122 | + // Save all the selected internal entity uris to this variable. |
|
123 | + $internal_entity_uris = array(); |
|
124 | + |
|
125 | + // Save the entities coming with POST data. |
|
126 | + if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
127 | + $data = filter_var_array( $_POST, array( 'wl_entities' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
128 | + $entities_via_post = $data['wl_entities']; |
|
129 | + wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
130 | + wl_write_log( wp_json_encode( $entities_via_post ) ); |
|
131 | + wl_write_log( ']' ); |
|
132 | + |
|
133 | + foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
134 | + |
|
135 | + if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) { |
|
136 | + $existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() ); |
|
137 | + if ( isset( $existing_entity ) ) { |
|
138 | + $existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID ); |
|
139 | + // Type doesn't match, continue to create a new entity. |
|
140 | + if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) { |
|
141 | + $existing_entity = null; |
|
142 | + } |
|
143 | + } |
|
144 | + } else { |
|
145 | + // Look if current entity uri matches an internal existing entity, meaning: |
|
146 | + // 1. when $entity_uri is an internal uri |
|
147 | + // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
|
148 | + $existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
149 | + } |
|
150 | + |
|
151 | + // Don't save the entities which are not found, but also local. |
|
152 | + if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) { |
|
153 | + $internal_entity_uris[] = $entity_uri; |
|
154 | + continue; |
|
155 | + } |
|
156 | + |
|
157 | + if ( ! isset( $existing_entity ) ) { |
|
158 | + // Update entity data with related post |
|
159 | + $entity['related_post_id'] = $post_id; |
|
160 | + // New entity, save it. |
|
161 | + $existing_entity = wl_save_entity( $entity ); |
|
162 | + } else { |
|
163 | + // Existing entity, update post status. |
|
164 | + if ( $existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status ) { |
|
165 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
166 | + $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
167 | + ? $post->post_status : 'draft'; |
|
168 | + wl_update_post_status( $existing_entity->ID, $post_status ); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + $uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) ); |
|
173 | + |
|
174 | + $internal_entity_uris[] = $uri; |
|
175 | + wl_write_log( "Map $entity_uri on $uri" ); |
|
176 | + $entities_uri_mapping[ $entity_uri ] = $uri; |
|
177 | + |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + // Replace tmp uris in content post if needed |
|
182 | + $updated_post_content = $post->post_content; |
|
183 | + |
|
184 | + // Update the post content if we found mappings of new entities. |
|
185 | + if ( ! empty( $entities_uri_mapping ) ) { |
|
186 | + // Save each entity and store the post id. |
|
187 | + foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
188 | + if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) { |
|
189 | + continue; |
|
190 | + } |
|
191 | + |
|
192 | + $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
193 | + } |
|
194 | + |
|
195 | + // Update the post content. |
|
196 | + /** |
|
197 | + * Note: wp_update_post do stripslashes before saving the |
|
198 | + * content, so add the slashes to prevent back slash getting |
|
199 | + * removed. |
|
200 | + */ |
|
201 | + wp_update_post( |
|
202 | + array( |
|
203 | + 'ID' => $post->ID, |
|
204 | + 'post_content' => addslashes( $updated_post_content ), |
|
205 | + ) |
|
206 | + ); |
|
207 | + } |
|
208 | + |
|
209 | + // Reset previously saved instances. |
|
210 | + wl_core_delete_relation_instances( $post_id ); |
|
211 | + |
|
212 | + $content_id = Wordpress_Content_Id::create_post( $post->ID ); |
|
213 | + $relations = Relation_Service::get_instance()->get_relations( $content_id ); |
|
214 | + |
|
215 | + if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) { |
|
216 | + $relations->add( ...Relation_Service::get_relations_from_uris( $content_id, $internal_entity_uris ) ); |
|
217 | + } |
|
218 | + /** |
|
219 | + * Filter the relations, we dont want to create a relation |
|
220 | + * to uncategorized term, we are already filtering this on jsonld, |
|
221 | + * |
|
222 | + * @todo: do i need to move this to post-term-relation-service ? |
|
223 | + */ |
|
224 | + $filtered_relations = array_filter( |
|
225 | + $relations->toArray(), |
|
226 | + function ( $item ) { |
|
227 | + /** |
|
228 | + * @var $item Relation |
|
229 | + */ |
|
230 | + $object = $item->get_object(); |
|
231 | + |
|
232 | + return ! ( $object->get_type() === Object_Type_Enum::TERM |
|
233 | + && $object->get_id() === 1 ); |
|
234 | + } |
|
235 | + ); |
|
236 | + |
|
237 | + // Save relation instances |
|
238 | + /** @var Relation $relation */ |
|
239 | + foreach ( $filtered_relations as $relation ) { |
|
240 | + $subject = $relation->get_subject(); |
|
241 | + $object = $relation->get_object(); |
|
242 | + |
|
243 | + wl_core_add_relation_instance( |
|
244 | + // subject id. |
|
245 | + $subject->get_id(), |
|
246 | + // what, where, when, who |
|
247 | + $relation->get_predicate(), |
|
248 | + // object id. |
|
249 | + $object->get_id(), |
|
250 | + // Subject type. |
|
251 | + $subject->get_type(), |
|
252 | + // Object type. |
|
253 | + $object->get_type() |
|
254 | + ); |
|
255 | + |
|
256 | + } |
|
257 | + |
|
258 | + if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
259 | + // Save post metadata if available |
|
260 | + $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? filter_input_array( INPUT_POST, array( 'wl_metadata' => FILTER_DEFAULT ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
261 | + |
|
262 | + $fields = array( |
|
263 | + Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
|
264 | + Wordlift_Schema_Service::FIELD_TOPIC, |
|
265 | + ); |
|
266 | + |
|
267 | + // Unlink topic taxonomy terms |
|
268 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
269 | + |
|
270 | + foreach ( $fields as $field ) { |
|
271 | + |
|
272 | + // Delete current values |
|
273 | + delete_post_meta( $post->ID, $field ); |
|
274 | + // Retrieve the entity uri |
|
275 | + $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
276 | + stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
277 | + |
|
278 | + if ( empty( $uri ) ) { |
|
279 | + continue; |
|
280 | + } |
|
281 | + $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
282 | + |
|
283 | + if ( $entity ) { |
|
284 | + add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
285 | + // Set also the topic taxonomy |
|
286 | + if ( Wordlift_Schema_Service::FIELD_TOPIC === $field ) { |
|
287 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
288 | + } |
|
289 | + } |
|
290 | + } |
|
291 | + } |
|
292 | + |
|
293 | + add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
294 | 294 | } |
295 | 295 | |
296 | 296 | add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
@@ -313,221 +313,221 @@ discard block |
||
313 | 313 | */ |
314 | 314 | function wl_save_entity( $entity_data ) { |
315 | 315 | |
316 | - // Required for REST API calls |
|
317 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
318 | - require_once ABSPATH . 'wp-admin/includes/image.php'; |
|
319 | - } |
|
316 | + // Required for REST API calls |
|
317 | + if ( ! function_exists( 'wp_crop_image' ) ) { |
|
318 | + require_once ABSPATH . 'wp-admin/includes/image.php'; |
|
319 | + } |
|
320 | 320 | |
321 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
321 | + $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
322 | 322 | |
323 | - /* |
|
323 | + /* |
|
324 | 324 | * Data is coming from a $_POST, sanitize it. |
325 | 325 | * |
326 | 326 | * @since 3.19.4 |
327 | 327 | * |
328 | 328 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
329 | 329 | */ |
330 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
331 | - $type_uri = $entity_data['main_type']; |
|
332 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
333 | - $description = $entity_data['description']; |
|
334 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
335 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
336 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
337 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
338 | - // Get the synonyms. |
|
339 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
340 | - |
|
341 | - // Check whether an entity already exists with the provided URI. |
|
342 | - $uri = $entity_data['uri']; |
|
343 | - if ( isset( $uri ) ) { |
|
344 | - $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
345 | - if ( isset( $post ) ) { |
|
346 | - $log->debug( "Post already exists for URI $uri." ); |
|
347 | - |
|
348 | - return $post; |
|
349 | - } |
|
350 | - } |
|
351 | - |
|
352 | - // Prepare properties of the new entity. |
|
353 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
354 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id ) |
|
355 | - ? get_post_status( $related_post_id ) : 'draft'; |
|
356 | - |
|
357 | - $params = array( |
|
358 | - // @@todo: we don't want an entity to be automatically published. |
|
359 | - 'post_status' => $post_status, |
|
360 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
361 | - 'post_title' => $label, |
|
362 | - 'post_content' => $description, |
|
363 | - 'post_excerpt' => '', |
|
364 | - // Ensure we're using a valid slug. We're not overwriting an existing |
|
365 | - // entity with a post_name already set, since this call is made only for |
|
366 | - // new entities. |
|
367 | - // |
|
368 | - // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
369 | - 'post_name' => sanitize_title( $label ), |
|
370 | - ); |
|
371 | - |
|
372 | - // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
373 | - // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
374 | - // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
375 | - // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
376 | - // is created when saving a post. |
|
377 | - global $wpseo_metabox, $seo_ultimate; |
|
378 | - if ( isset( $wpseo_metabox ) ) { |
|
379 | - remove_action( |
|
380 | - 'wp_insert_post', |
|
381 | - array( |
|
382 | - $wpseo_metabox, |
|
383 | - 'save_postdata', |
|
384 | - ) |
|
385 | - ); |
|
386 | - } |
|
387 | - |
|
388 | - if ( isset( $seo_ultimate ) ) { |
|
389 | - remove_action( |
|
390 | - 'save_post', |
|
391 | - array( |
|
392 | - $seo_ultimate, |
|
393 | - 'save_postmeta_box', |
|
394 | - ) |
|
395 | - ); |
|
396 | - } |
|
397 | - |
|
398 | - // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
399 | - // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
400 | - // to the save_post and restore them after we saved the entity. |
|
401 | - // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
402 | - // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
403 | - // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
404 | - global $wp_filter; |
|
405 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
406 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
407 | - |
|
408 | - $log->trace( 'Going to insert new post...' ); |
|
409 | - |
|
410 | - // create or update the post. |
|
411 | - $post_id = wp_insert_post( $params, true ); |
|
412 | - |
|
413 | - // Setting the alternative labels for this entity. |
|
414 | - Wordlift_Entity_Service::get_instance() |
|
415 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
416 | - |
|
417 | - // Restore all the existing filters. |
|
418 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
419 | - |
|
420 | - // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
421 | - if ( isset( $wpseo_metabox ) ) { |
|
422 | - add_action( |
|
423 | - 'wp_insert_post', |
|
424 | - array( |
|
425 | - $wpseo_metabox, |
|
426 | - 'save_postdata', |
|
427 | - ) |
|
428 | - ); |
|
429 | - } |
|
430 | - |
|
431 | - // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
432 | - if ( isset( $seo_ultimate ) ) { |
|
433 | - add_action( |
|
434 | - 'save_post', |
|
435 | - array( |
|
436 | - $seo_ultimate, |
|
437 | - 'save_postmeta_box', |
|
438 | - ), |
|
439 | - 10, |
|
440 | - 2 |
|
441 | - ); |
|
442 | - } |
|
443 | - |
|
444 | - // TODO: handle errors. |
|
445 | - if ( is_wp_error( $post_id ) ) { |
|
446 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
447 | - |
|
448 | - // inform an error occurred. |
|
449 | - return null; |
|
450 | - } |
|
451 | - |
|
452 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
453 | - |
|
454 | - // Save the entity types. |
|
455 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
456 | - |
|
457 | - // Get a dataset URI for the entity. |
|
458 | - $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id ); |
|
459 | - |
|
460 | - // Add the uri to the sameAs data if it's not a local URI. |
|
461 | - if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) && |
|
462 | - $wl_uri !== $uri && |
|
463 | - // Only remote entities |
|
464 | - 0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) |
|
465 | - ) { |
|
466 | - array_push( $same_as, $uri ); |
|
467 | - } |
|
468 | - |
|
469 | - // Save the sameAs data for the entity. |
|
470 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
471 | - |
|
472 | - // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
473 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
474 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
475 | - } |
|
476 | - |
|
477 | - // Call hooks. |
|
478 | - do_action( 'wl_save_entity', $post_id ); |
|
479 | - |
|
480 | - foreach ( $images as $image_remote_url ) { |
|
481 | - |
|
482 | - // Check if image is already present in local DB |
|
483 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
484 | - // Do nothing. |
|
485 | - continue; |
|
486 | - } |
|
487 | - |
|
488 | - // Check if there is an existing attachment for this post ID and source URL. |
|
489 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
490 | - |
|
491 | - // Skip if an existing image is found. |
|
492 | - if ( null !== $existing_image ) { |
|
493 | - continue; |
|
494 | - } |
|
495 | - |
|
496 | - // Save the image and get the local path. |
|
497 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
498 | - |
|
499 | - if ( false === $image || is_wp_error( $image ) ) { |
|
500 | - continue; |
|
501 | - } |
|
502 | - |
|
503 | - // Get the local URL. |
|
504 | - $filename = $image['path']; |
|
505 | - $url = $image['url']; |
|
506 | - $content_type = $image['content_type']; |
|
507 | - |
|
508 | - $attachment = array( |
|
509 | - 'guid' => $url, |
|
510 | - // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
511 | - 'post_title' => $label, |
|
512 | - // Set the title to the post title. |
|
513 | - 'post_content' => '', |
|
514 | - 'post_status' => 'inherit', |
|
515 | - 'post_mime_type' => $content_type, |
|
516 | - ); |
|
517 | - |
|
518 | - // Create the attachment in WordPress and generate the related metadata. |
|
519 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
520 | - |
|
521 | - // Set the source URL for the image. |
|
522 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
523 | - |
|
524 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
525 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
526 | - |
|
527 | - // Set it as the featured image. |
|
528 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
529 | - } |
|
530 | - |
|
531 | - // finally return the entity post. |
|
532 | - return get_post( $post_id ); |
|
330 | + $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
331 | + $type_uri = $entity_data['main_type']; |
|
332 | + $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
333 | + $description = $entity_data['description']; |
|
334 | + $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
335 | + $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
336 | + $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
337 | + $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
338 | + // Get the synonyms. |
|
339 | + $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
340 | + |
|
341 | + // Check whether an entity already exists with the provided URI. |
|
342 | + $uri = $entity_data['uri']; |
|
343 | + if ( isset( $uri ) ) { |
|
344 | + $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
345 | + if ( isset( $post ) ) { |
|
346 | + $log->debug( "Post already exists for URI $uri." ); |
|
347 | + |
|
348 | + return $post; |
|
349 | + } |
|
350 | + } |
|
351 | + |
|
352 | + // Prepare properties of the new entity. |
|
353 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
354 | + $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id ) |
|
355 | + ? get_post_status( $related_post_id ) : 'draft'; |
|
356 | + |
|
357 | + $params = array( |
|
358 | + // @@todo: we don't want an entity to be automatically published. |
|
359 | + 'post_status' => $post_status, |
|
360 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
361 | + 'post_title' => $label, |
|
362 | + 'post_content' => $description, |
|
363 | + 'post_excerpt' => '', |
|
364 | + // Ensure we're using a valid slug. We're not overwriting an existing |
|
365 | + // entity with a post_name already set, since this call is made only for |
|
366 | + // new entities. |
|
367 | + // |
|
368 | + // See https://github.com/insideout10/wordlift-plugin/issues/282 |
|
369 | + 'post_name' => sanitize_title( $label ), |
|
370 | + ); |
|
371 | + |
|
372 | + // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
|
373 | + // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
374 | + // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148) |
|
375 | + // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
|
376 | + // is created when saving a post. |
|
377 | + global $wpseo_metabox, $seo_ultimate; |
|
378 | + if ( isset( $wpseo_metabox ) ) { |
|
379 | + remove_action( |
|
380 | + 'wp_insert_post', |
|
381 | + array( |
|
382 | + $wpseo_metabox, |
|
383 | + 'save_postdata', |
|
384 | + ) |
|
385 | + ); |
|
386 | + } |
|
387 | + |
|
388 | + if ( isset( $seo_ultimate ) ) { |
|
389 | + remove_action( |
|
390 | + 'save_post', |
|
391 | + array( |
|
392 | + $seo_ultimate, |
|
393 | + 'save_postmeta_box', |
|
394 | + ) |
|
395 | + ); |
|
396 | + } |
|
397 | + |
|
398 | + // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to |
|
399 | + // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks |
|
400 | + // to the save_post and restore them after we saved the entity. |
|
401 | + // see https://github.com/insideout10/wordlift-plugin/issues/203 |
|
402 | + // see https://github.com/insideout10/wordlift-plugin/issues/156 |
|
403 | + // see https://github.com/insideout10/wordlift-plugin/issues/148 |
|
404 | + global $wp_filter; |
|
405 | + $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
406 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
407 | + |
|
408 | + $log->trace( 'Going to insert new post...' ); |
|
409 | + |
|
410 | + // create or update the post. |
|
411 | + $post_id = wp_insert_post( $params, true ); |
|
412 | + |
|
413 | + // Setting the alternative labels for this entity. |
|
414 | + Wordlift_Entity_Service::get_instance() |
|
415 | + ->set_alternative_labels( $post_id, $synonyms ); |
|
416 | + |
|
417 | + // Restore all the existing filters. |
|
418 | + is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
419 | + |
|
420 | + // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
|
421 | + if ( isset( $wpseo_metabox ) ) { |
|
422 | + add_action( |
|
423 | + 'wp_insert_post', |
|
424 | + array( |
|
425 | + $wpseo_metabox, |
|
426 | + 'save_postdata', |
|
427 | + ) |
|
428 | + ); |
|
429 | + } |
|
430 | + |
|
431 | + // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
|
432 | + if ( isset( $seo_ultimate ) ) { |
|
433 | + add_action( |
|
434 | + 'save_post', |
|
435 | + array( |
|
436 | + $seo_ultimate, |
|
437 | + 'save_postmeta_box', |
|
438 | + ), |
|
439 | + 10, |
|
440 | + 2 |
|
441 | + ); |
|
442 | + } |
|
443 | + |
|
444 | + // TODO: handle errors. |
|
445 | + if ( is_wp_error( $post_id ) ) { |
|
446 | + $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
447 | + |
|
448 | + // inform an error occurred. |
|
449 | + return null; |
|
450 | + } |
|
451 | + |
|
452 | + wl_set_entity_main_type( $post_id, $type_uri ); |
|
453 | + |
|
454 | + // Save the entity types. |
|
455 | + wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
456 | + |
|
457 | + // Get a dataset URI for the entity. |
|
458 | + $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id ); |
|
459 | + |
|
460 | + // Add the uri to the sameAs data if it's not a local URI. |
|
461 | + if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) && |
|
462 | + $wl_uri !== $uri && |
|
463 | + // Only remote entities |
|
464 | + 0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) |
|
465 | + ) { |
|
466 | + array_push( $same_as, $uri ); |
|
467 | + } |
|
468 | + |
|
469 | + // Save the sameAs data for the entity. |
|
470 | + wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
471 | + |
|
472 | + // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
|
473 | + foreach ( $other_properties as $property_name => $property_value ) { |
|
474 | + wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
475 | + } |
|
476 | + |
|
477 | + // Call hooks. |
|
478 | + do_action( 'wl_save_entity', $post_id ); |
|
479 | + |
|
480 | + foreach ( $images as $image_remote_url ) { |
|
481 | + |
|
482 | + // Check if image is already present in local DB |
|
483 | + if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
484 | + // Do nothing. |
|
485 | + continue; |
|
486 | + } |
|
487 | + |
|
488 | + // Check if there is an existing attachment for this post ID and source URL. |
|
489 | + $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
490 | + |
|
491 | + // Skip if an existing image is found. |
|
492 | + if ( null !== $existing_image ) { |
|
493 | + continue; |
|
494 | + } |
|
495 | + |
|
496 | + // Save the image and get the local path. |
|
497 | + $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
498 | + |
|
499 | + if ( false === $image || is_wp_error( $image ) ) { |
|
500 | + continue; |
|
501 | + } |
|
502 | + |
|
503 | + // Get the local URL. |
|
504 | + $filename = $image['path']; |
|
505 | + $url = $image['url']; |
|
506 | + $content_type = $image['content_type']; |
|
507 | + |
|
508 | + $attachment = array( |
|
509 | + 'guid' => $url, |
|
510 | + // post_title, post_content (the value for this key should be the empty string), post_status and post_mime_type |
|
511 | + 'post_title' => $label, |
|
512 | + // Set the title to the post title. |
|
513 | + 'post_content' => '', |
|
514 | + 'post_status' => 'inherit', |
|
515 | + 'post_mime_type' => $content_type, |
|
516 | + ); |
|
517 | + |
|
518 | + // Create the attachment in WordPress and generate the related metadata. |
|
519 | + $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
520 | + |
|
521 | + // Set the source URL for the image. |
|
522 | + wl_set_source_url( $attachment_id, $image_remote_url ); |
|
523 | + |
|
524 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
525 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
526 | + |
|
527 | + // Set it as the featured image. |
|
528 | + set_post_thumbnail( $post_id, $attachment_id ); |
|
529 | + } |
|
530 | + |
|
531 | + // finally return the entity post. |
|
532 | + return get_post( $post_id ); |
|
533 | 533 | } |
@@ -21,15 +21,15 @@ discard block |
||
21 | 21 | * |
22 | 22 | * @since 3.0.0 |
23 | 23 | */ |
24 | -function wl_linked_data_save_post( $post_id ) { |
|
24 | +function wl_linked_data_save_post($post_id) { |
|
25 | 25 | |
26 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post' ); |
|
26 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post'); |
|
27 | 27 | |
28 | - $log->trace( "Saving post $post_id to Linked Data..." ); |
|
28 | + $log->trace("Saving post $post_id to Linked Data..."); |
|
29 | 29 | |
30 | 30 | // If it's not numeric exit from here. |
31 | - if ( ! is_numeric( $post_id ) || is_numeric( wp_is_post_revision( $post_id ) ) ) { |
|
32 | - $log->debug( "Skipping $post_id, because id is not numeric or is a post revision." ); |
|
31 | + if ( ! is_numeric($post_id) || is_numeric(wp_is_post_revision($post_id))) { |
|
32 | + $log->debug("Skipping $post_id, because id is not numeric or is a post revision."); |
|
33 | 33 | |
34 | 34 | return; |
35 | 35 | } |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | // Get the post type and check whether it supports the editor. |
38 | 38 | // |
39 | 39 | // @see https://github.com/insideout10/wordlift-plugin/issues/659. |
40 | - $post_type = get_post_type( $post_id ); |
|
40 | + $post_type = get_post_type($post_id); |
|
41 | 41 | /** |
42 | 42 | * Use `wl_post_type_supports_editor` which calls a filter to allow 3rd parties to change the setting. |
43 | 43 | * |
@@ -45,13 +45,13 @@ discard block |
||
45 | 45 | * |
46 | 46 | * @see https://github.com/insideout10/wordlift-plugin/issues/847. |
47 | 47 | */ |
48 | - $is_editor_supported = wl_post_type_supports_editor( $post_type ); |
|
48 | + $is_editor_supported = wl_post_type_supports_editor($post_type); |
|
49 | 49 | |
50 | - $is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ); |
|
50 | + $is_no_editor_analysis_enabled = Wordlift\No_Editor_Analysis\No_Editor_Analysis_Feature::can_no_editor_analysis_be_used($post_id); |
|
51 | 51 | // Bail out if it's not an entity. |
52 | 52 | if ( ! $is_editor_supported |
53 | - && ! $is_no_editor_analysis_enabled ) { |
|
54 | - $log->debug( "Skipping $post_id, because $post_type doesn't support the editor (content)." ); |
|
53 | + && ! $is_no_editor_analysis_enabled) { |
|
54 | + $log->debug("Skipping $post_id, because $post_type doesn't support the editor (content)."); |
|
55 | 55 | |
56 | 56 | return; |
57 | 57 | } |
@@ -64,23 +64,23 @@ discard block |
||
64 | 64 | $supported_types = Wordlift_Entity_Service::valid_entity_post_types(); |
65 | 65 | |
66 | 66 | // Bail out if it's not a valid entity. |
67 | - if ( ! in_array( $post_type, $supported_types, true ) && ! $is_no_editor_analysis_enabled ) { |
|
68 | - $log->debug( "Skipping $post_id, because $post_type is not a valid entity." ); |
|
67 | + if ( ! in_array($post_type, $supported_types, true) && ! $is_no_editor_analysis_enabled) { |
|
68 | + $log->debug("Skipping $post_id, because $post_type is not a valid entity."); |
|
69 | 69 | |
70 | 70 | return; |
71 | 71 | } |
72 | 72 | |
73 | 73 | // Unhook this function so it doesn't loop infinitely. |
74 | - remove_action( 'save_post', 'wl_linked_data_save_post' ); |
|
74 | + remove_action('save_post', 'wl_linked_data_save_post'); |
|
75 | 75 | |
76 | 76 | // raise the *wl_linked_data_save_post* event. |
77 | - do_action( 'wl_linked_data_save_post', $post_id ); |
|
77 | + do_action('wl_linked_data_save_post', $post_id); |
|
78 | 78 | |
79 | 79 | // Re-hook this function. |
80 | - add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
80 | + add_action('save_post', 'wl_linked_data_save_post'); |
|
81 | 81 | } |
82 | 82 | |
83 | -add_action( 'save_post', 'wl_linked_data_save_post' ); |
|
83 | +add_action('save_post', 'wl_linked_data_save_post'); |
|
84 | 84 | |
85 | 85 | /** |
86 | 86 | * Save the post to the triple store. Also saves the entities locally and on the triple store. |
@@ -91,23 +91,23 @@ discard block |
||
91 | 91 | * |
92 | 92 | * @since 3.0.0 |
93 | 93 | */ |
94 | -function wl_linked_data_save_post_and_related_entities( $post_id ) { |
|
94 | +function wl_linked_data_save_post_and_related_entities($post_id) { |
|
95 | 95 | |
96 | - $log = Wordlift_Log_Service::get_logger( 'wl_linked_data_save_post_and_related_entities' ); |
|
96 | + $log = Wordlift_Log_Service::get_logger('wl_linked_data_save_post_and_related_entities'); |
|
97 | 97 | |
98 | - $log->trace( "Saving $post_id to Linked Data along with related entities..." ); |
|
98 | + $log->trace("Saving $post_id to Linked Data along with related entities..."); |
|
99 | 99 | |
100 | 100 | // Ignore auto-saves |
101 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
102 | - $log->trace( 'Doing autosave, skipping...' ); |
|
101 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
102 | + $log->trace('Doing autosave, skipping...'); |
|
103 | 103 | |
104 | 104 | return; |
105 | 105 | } |
106 | 106 | |
107 | 107 | // get the current post. |
108 | - $post = get_post( $post_id ); |
|
108 | + $post = get_post($post_id); |
|
109 | 109 | |
110 | - remove_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
110 | + remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
111 | 111 | |
112 | 112 | // wl_write_log( "[ post id :: $post_id ][ autosave :: false ][ post type :: $post->post_type ]" ); |
113 | 113 | |
@@ -123,21 +123,21 @@ discard block |
||
123 | 123 | $internal_entity_uris = array(); |
124 | 124 | |
125 | 125 | // Save the entities coming with POST data. |
126 | - if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
127 | - $data = filter_var_array( $_POST, array( 'wl_entities' => array( 'flags' => FILTER_REQUIRE_ARRAY ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
126 | + if (isset($_POST['wl_entities'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
127 | + $data = filter_var_array($_POST, array('wl_entities' => array('flags' => FILTER_REQUIRE_ARRAY))); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
128 | 128 | $entities_via_post = $data['wl_entities']; |
129 | - wl_write_log( "[ post id :: $post_id ][ POST(wl_entities) :: " ); |
|
130 | - wl_write_log( wp_json_encode( $entities_via_post ) ); |
|
131 | - wl_write_log( ']' ); |
|
129 | + wl_write_log("[ post id :: $post_id ][ POST(wl_entities) :: "); |
|
130 | + wl_write_log(wp_json_encode($entities_via_post)); |
|
131 | + wl_write_log(']'); |
|
132 | 132 | |
133 | - foreach ( $entities_via_post as $entity_uri => $entity ) { |
|
133 | + foreach ($entities_via_post as $entity_uri => $entity) { |
|
134 | 134 | |
135 | - if ( preg_match( '/^local-entity-.+/', $entity_uri ) ) { |
|
136 | - $existing_entity = get_page_by_title( $entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types() ); |
|
137 | - if ( isset( $existing_entity ) ) { |
|
138 | - $existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get( $existing_entity->ID ); |
|
135 | + if (preg_match('/^local-entity-.+/', $entity_uri)) { |
|
136 | + $existing_entity = get_page_by_title($entity['label'], OBJECT, Wordlift_Entity_Service::valid_entity_post_types()); |
|
137 | + if (isset($existing_entity)) { |
|
138 | + $existing_entity_type = Wordlift_Entity_Type_Service::get_instance()->get($existing_entity->ID); |
|
139 | 139 | // Type doesn't match, continue to create a new entity. |
140 | - if ( ! isset( $existing_entity_type ) || $existing_entity_type['css_class'] !== $entity['main_type'] ) { |
|
140 | + if ( ! isset($existing_entity_type) || $existing_entity_type['css_class'] !== $entity['main_type']) { |
|
141 | 141 | $existing_entity = null; |
142 | 142 | } |
143 | 143 | } |
@@ -145,35 +145,35 @@ discard block |
||
145 | 145 | // Look if current entity uri matches an internal existing entity, meaning: |
146 | 146 | // 1. when $entity_uri is an internal uri |
147 | 147 | // 2. when $entity_uri is an external uri used as sameAs of an internal entity |
148 | - $existing_entity = $entity_service->get_entity_post_by_uri( $entity_uri ); |
|
148 | + $existing_entity = $entity_service->get_entity_post_by_uri($entity_uri); |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | // Don't save the entities which are not found, but also local. |
152 | - if ( ! isset( $existing_entity ) && $uri_service->is_internal( $entity_uri ) ) { |
|
152 | + if ( ! isset($existing_entity) && $uri_service->is_internal($entity_uri)) { |
|
153 | 153 | $internal_entity_uris[] = $entity_uri; |
154 | 154 | continue; |
155 | 155 | } |
156 | 156 | |
157 | - if ( ! isset( $existing_entity ) ) { |
|
157 | + if ( ! isset($existing_entity)) { |
|
158 | 158 | // Update entity data with related post |
159 | 159 | $entity['related_post_id'] = $post_id; |
160 | 160 | // New entity, save it. |
161 | - $existing_entity = wl_save_entity( $entity ); |
|
161 | + $existing_entity = wl_save_entity($entity); |
|
162 | 162 | } else { |
163 | 163 | // Existing entity, update post status. |
164 | - if ( $existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status ) { |
|
164 | + if ($existing_entity instanceof WP_Post && 'publish' !== $existing_entity->post_status) { |
|
165 | 165 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
166 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
166 | + $post_status = apply_filters('wl_feature__enable__entity-auto-publish', true) |
|
167 | 167 | ? $post->post_status : 'draft'; |
168 | - wl_update_post_status( $existing_entity->ID, $post_status ); |
|
168 | + wl_update_post_status($existing_entity->ID, $post_status); |
|
169 | 169 | } |
170 | 170 | } |
171 | 171 | |
172 | - $uri = $content_service->get_entity_id( Wordpress_Content_Id::create_post( $existing_entity->ID ) ); |
|
172 | + $uri = $content_service->get_entity_id(Wordpress_Content_Id::create_post($existing_entity->ID)); |
|
173 | 173 | |
174 | 174 | $internal_entity_uris[] = $uri; |
175 | - wl_write_log( "Map $entity_uri on $uri" ); |
|
176 | - $entities_uri_mapping[ $entity_uri ] = $uri; |
|
175 | + wl_write_log("Map $entity_uri on $uri"); |
|
176 | + $entities_uri_mapping[$entity_uri] = $uri; |
|
177 | 177 | |
178 | 178 | } |
179 | 179 | } |
@@ -182,14 +182,14 @@ discard block |
||
182 | 182 | $updated_post_content = $post->post_content; |
183 | 183 | |
184 | 184 | // Update the post content if we found mappings of new entities. |
185 | - if ( ! empty( $entities_uri_mapping ) ) { |
|
185 | + if ( ! empty($entities_uri_mapping)) { |
|
186 | 186 | // Save each entity and store the post id. |
187 | - foreach ( $entities_uri_mapping as $tmp_uri => $uri ) { |
|
188 | - if ( 1 !== preg_match( '@^(https?://|local-entity-)@', $tmp_uri ) ) { |
|
187 | + foreach ($entities_uri_mapping as $tmp_uri => $uri) { |
|
188 | + if (1 !== preg_match('@^(https?://|local-entity-)@', $tmp_uri)) { |
|
189 | 189 | continue; |
190 | 190 | } |
191 | 191 | |
192 | - $updated_post_content = str_replace( $tmp_uri, $uri, $updated_post_content ); |
|
192 | + $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); |
|
193 | 193 | } |
194 | 194 | |
195 | 195 | // Update the post content. |
@@ -201,19 +201,19 @@ discard block |
||
201 | 201 | wp_update_post( |
202 | 202 | array( |
203 | 203 | 'ID' => $post->ID, |
204 | - 'post_content' => addslashes( $updated_post_content ), |
|
204 | + 'post_content' => addslashes($updated_post_content), |
|
205 | 205 | ) |
206 | 206 | ); |
207 | 207 | } |
208 | 208 | |
209 | 209 | // Reset previously saved instances. |
210 | - wl_core_delete_relation_instances( $post_id ); |
|
210 | + wl_core_delete_relation_instances($post_id); |
|
211 | 211 | |
212 | - $content_id = Wordpress_Content_Id::create_post( $post->ID ); |
|
213 | - $relations = Relation_Service::get_instance()->get_relations( $content_id ); |
|
212 | + $content_id = Wordpress_Content_Id::create_post($post->ID); |
|
213 | + $relations = Relation_Service::get_instance()->get_relations($content_id); |
|
214 | 214 | |
215 | - if ( No_Editor_Analysis_Feature::can_no_editor_analysis_be_used( $post_id ) ) { |
|
216 | - $relations->add( ...Relation_Service::get_relations_from_uris( $content_id, $internal_entity_uris ) ); |
|
215 | + if (No_Editor_Analysis_Feature::can_no_editor_analysis_be_used($post_id)) { |
|
216 | + $relations->add(...Relation_Service::get_relations_from_uris($content_id, $internal_entity_uris)); |
|
217 | 217 | } |
218 | 218 | /** |
219 | 219 | * Filter the relations, we dont want to create a relation |
@@ -223,20 +223,20 @@ discard block |
||
223 | 223 | */ |
224 | 224 | $filtered_relations = array_filter( |
225 | 225 | $relations->toArray(), |
226 | - function ( $item ) { |
|
226 | + function($item) { |
|
227 | 227 | /** |
228 | 228 | * @var $item Relation |
229 | 229 | */ |
230 | 230 | $object = $item->get_object(); |
231 | 231 | |
232 | - return ! ( $object->get_type() === Object_Type_Enum::TERM |
|
233 | - && $object->get_id() === 1 ); |
|
232 | + return ! ($object->get_type() === Object_Type_Enum::TERM |
|
233 | + && $object->get_id() === 1); |
|
234 | 234 | } |
235 | 235 | ); |
236 | 236 | |
237 | 237 | // Save relation instances |
238 | 238 | /** @var Relation $relation */ |
239 | - foreach ( $filtered_relations as $relation ) { |
|
239 | + foreach ($filtered_relations as $relation) { |
|
240 | 240 | $subject = $relation->get_subject(); |
241 | 241 | $object = $relation->get_object(); |
242 | 242 | |
@@ -255,9 +255,9 @@ discard block |
||
255 | 255 | |
256 | 256 | } |
257 | 257 | |
258 | - if ( isset( $_POST['wl_entities'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
258 | + if (isset($_POST['wl_entities'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
259 | 259 | // Save post metadata if available |
260 | - $metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ? filter_input_array( INPUT_POST, array( 'wl_metadata' => FILTER_DEFAULT ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
260 | + $metadata_via_post = (isset($_POST['wl_metadata'])) ? filter_input_array(INPUT_POST, array('wl_metadata' => FILTER_DEFAULT)) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
261 | 261 | |
262 | 262 | $fields = array( |
263 | 263 | Wordlift_Schema_Service::FIELD_LOCATION_CREATED, |
@@ -265,35 +265,35 @@ discard block |
||
265 | 265 | ); |
266 | 266 | |
267 | 267 | // Unlink topic taxonomy terms |
268 | - Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for( $post->ID ); |
|
268 | + Wordlift_Topic_Taxonomy_Service::get_instance()->unlink_topic_for($post->ID); |
|
269 | 269 | |
270 | - foreach ( $fields as $field ) { |
|
270 | + foreach ($fields as $field) { |
|
271 | 271 | |
272 | 272 | // Delete current values |
273 | - delete_post_meta( $post->ID, $field ); |
|
273 | + delete_post_meta($post->ID, $field); |
|
274 | 274 | // Retrieve the entity uri |
275 | - $uri = ( isset( $metadata_via_post[ $field ] ) ) ? |
|
276 | - stripslashes( $metadata_via_post[ $field ] ) : ''; |
|
275 | + $uri = (isset($metadata_via_post[$field])) ? |
|
276 | + stripslashes($metadata_via_post[$field]) : ''; |
|
277 | 277 | |
278 | - if ( empty( $uri ) ) { |
|
278 | + if (empty($uri)) { |
|
279 | 279 | continue; |
280 | 280 | } |
281 | - $entity = $entity_service->get_entity_post_by_uri( $uri ); |
|
281 | + $entity = $entity_service->get_entity_post_by_uri($uri); |
|
282 | 282 | |
283 | - if ( $entity ) { |
|
284 | - add_post_meta( $post->ID, $field, $entity->ID, true ); |
|
283 | + if ($entity) { |
|
284 | + add_post_meta($post->ID, $field, $entity->ID, true); |
|
285 | 285 | // Set also the topic taxonomy |
286 | - if ( Wordlift_Schema_Service::FIELD_TOPIC === $field ) { |
|
287 | - Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for( $post->ID, $entity ); |
|
286 | + if (Wordlift_Schema_Service::FIELD_TOPIC === $field) { |
|
287 | + Wordlift_Topic_Taxonomy_Service::get_instance()->set_topic_for($post->ID, $entity); |
|
288 | 288 | } |
289 | 289 | } |
290 | 290 | } |
291 | 291 | } |
292 | 292 | |
293 | - add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
293 | + add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
294 | 294 | } |
295 | 295 | |
296 | -add_action( 'wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities' ); |
|
296 | +add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); |
|
297 | 297 | |
298 | 298 | /** |
299 | 299 | * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is |
@@ -311,14 +311,14 @@ discard block |
||
311 | 311 | * |
312 | 312 | * @return null|WP_Post A post instance or null in case of failure. |
313 | 313 | */ |
314 | -function wl_save_entity( $entity_data ) { |
|
314 | +function wl_save_entity($entity_data) { |
|
315 | 315 | |
316 | 316 | // Required for REST API calls |
317 | - if ( ! function_exists( 'wp_crop_image' ) ) { |
|
318 | - require_once ABSPATH . 'wp-admin/includes/image.php'; |
|
317 | + if ( ! function_exists('wp_crop_image')) { |
|
318 | + require_once ABSPATH.'wp-admin/includes/image.php'; |
|
319 | 319 | } |
320 | 320 | |
321 | - $log = Wordlift_Log_Service::get_logger( 'wl_save_entity' ); |
|
321 | + $log = Wordlift_Log_Service::get_logger('wl_save_entity'); |
|
322 | 322 | |
323 | 323 | /* |
324 | 324 | * Data is coming from a $_POST, sanitize it. |
@@ -327,23 +327,23 @@ discard block |
||
327 | 327 | * |
328 | 328 | * @see https://github.com/insideout10/wordlift-plugin/issues/841 |
329 | 329 | */ |
330 | - $label = preg_replace( '/\xEF\xBB\xBF/', '', sanitize_text_field( $entity_data['label'] ) ); |
|
330 | + $label = preg_replace('/\xEF\xBB\xBF/', '', sanitize_text_field($entity_data['label'])); |
|
331 | 331 | $type_uri = $entity_data['main_type']; |
332 | - $entity_types = isset( $entity_data['type'] ) ? $entity_data['type'] : array(); |
|
332 | + $entity_types = isset($entity_data['type']) ? $entity_data['type'] : array(); |
|
333 | 333 | $description = $entity_data['description']; |
334 | - $images = isset( $entity_data['image'] ) ? (array) $entity_data['image'] : array(); |
|
335 | - $same_as = isset( $entity_data['sameas'] ) ? (array) $entity_data['sameas'] : array(); |
|
336 | - $related_post_id = isset( $entity_data['related_post_id'] ) ? $entity_data['related_post_id'] : null; |
|
337 | - $other_properties = isset( $entity_data['properties'] ) ? $entity_data['properties'] : array(); |
|
334 | + $images = isset($entity_data['image']) ? (array) $entity_data['image'] : array(); |
|
335 | + $same_as = isset($entity_data['sameas']) ? (array) $entity_data['sameas'] : array(); |
|
336 | + $related_post_id = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null; |
|
337 | + $other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array(); |
|
338 | 338 | // Get the synonyms. |
339 | - $synonyms = isset( $entity_data['synonym'] ) ? $entity_data['synonym'] : array(); |
|
339 | + $synonyms = isset($entity_data['synonym']) ? $entity_data['synonym'] : array(); |
|
340 | 340 | |
341 | 341 | // Check whether an entity already exists with the provided URI. |
342 | 342 | $uri = $entity_data['uri']; |
343 | - if ( isset( $uri ) ) { |
|
344 | - $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri( $uri ); |
|
345 | - if ( isset( $post ) ) { |
|
346 | - $log->debug( "Post already exists for URI $uri." ); |
|
343 | + if (isset($uri)) { |
|
344 | + $post = Wordlift_Entity_Service::get_instance()->get_entity_post_by_uri($uri); |
|
345 | + if (isset($post)) { |
|
346 | + $log->debug("Post already exists for URI $uri."); |
|
347 | 347 | |
348 | 348 | return $post; |
349 | 349 | } |
@@ -351,8 +351,8 @@ discard block |
||
351 | 351 | |
352 | 352 | // Prepare properties of the new entity. |
353 | 353 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
354 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) && is_numeric( $related_post_id ) |
|
355 | - ? get_post_status( $related_post_id ) : 'draft'; |
|
354 | + $post_status = apply_filters('wl_feature__enable__entity-auto-publish', true) && is_numeric($related_post_id) |
|
355 | + ? get_post_status($related_post_id) : 'draft'; |
|
356 | 356 | |
357 | 357 | $params = array( |
358 | 358 | // @@todo: we don't want an entity to be automatically published. |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | // new entities. |
367 | 367 | // |
368 | 368 | // See https://github.com/insideout10/wordlift-plugin/issues/282 |
369 | - 'post_name' => sanitize_title( $label ), |
|
369 | + 'post_name' => sanitize_title($label), |
|
370 | 370 | ); |
371 | 371 | |
372 | 372 | // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over" |
@@ -375,7 +375,7 @@ discard block |
||
375 | 375 | // This does NOT affect saving an entity from the entity admin page since this function is called when an entity |
376 | 376 | // is created when saving a post. |
377 | 377 | global $wpseo_metabox, $seo_ultimate; |
378 | - if ( isset( $wpseo_metabox ) ) { |
|
378 | + if (isset($wpseo_metabox)) { |
|
379 | 379 | remove_action( |
380 | 380 | 'wp_insert_post', |
381 | 381 | array( |
@@ -385,7 +385,7 @@ discard block |
||
385 | 385 | ); |
386 | 386 | } |
387 | 387 | |
388 | - if ( isset( $seo_ultimate ) ) { |
|
388 | + if (isset($seo_ultimate)) { |
|
389 | 389 | remove_action( |
390 | 390 | 'save_post', |
391 | 391 | array( |
@@ -402,23 +402,23 @@ discard block |
||
402 | 402 | // see https://github.com/insideout10/wordlift-plugin/issues/156 |
403 | 403 | // see https://github.com/insideout10/wordlift-plugin/issues/148 |
404 | 404 | global $wp_filter; |
405 | - $save_post_filters = is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
406 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
405 | + $save_post_filters = is_array($wp_filter['save_post']) ? $wp_filter['save_post'] : $wp_filter['save_post']->callbacks; |
|
406 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = array() : $wp_filter['save_post']->remove_all_filters(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
407 | 407 | |
408 | - $log->trace( 'Going to insert new post...' ); |
|
408 | + $log->trace('Going to insert new post...'); |
|
409 | 409 | |
410 | 410 | // create or update the post. |
411 | - $post_id = wp_insert_post( $params, true ); |
|
411 | + $post_id = wp_insert_post($params, true); |
|
412 | 412 | |
413 | 413 | // Setting the alternative labels for this entity. |
414 | 414 | Wordlift_Entity_Service::get_instance() |
415 | - ->set_alternative_labels( $post_id, $synonyms ); |
|
415 | + ->set_alternative_labels($post_id, $synonyms); |
|
416 | 416 | |
417 | 417 | // Restore all the existing filters. |
418 | - is_array( $wp_filter['save_post'] ) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
418 | + is_array($wp_filter['save_post']) ? $wp_filter['save_post'] = $save_post_filters : $wp_filter['save_post']->callbacks = $save_post_filters; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
|
419 | 419 | |
420 | 420 | // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156) |
421 | - if ( isset( $wpseo_metabox ) ) { |
|
421 | + if (isset($wpseo_metabox)) { |
|
422 | 422 | add_action( |
423 | 423 | 'wp_insert_post', |
424 | 424 | array( |
@@ -429,7 +429,7 @@ discard block |
||
429 | 429 | } |
430 | 430 | |
431 | 431 | // If SEO Ultimate is installed, add back the hook we removed a few lines above. |
432 | - if ( isset( $seo_ultimate ) ) { |
|
432 | + if (isset($seo_ultimate)) { |
|
433 | 433 | add_action( |
434 | 434 | 'save_post', |
435 | 435 | array( |
@@ -442,61 +442,61 @@ discard block |
||
442 | 442 | } |
443 | 443 | |
444 | 444 | // TODO: handle errors. |
445 | - if ( is_wp_error( $post_id ) ) { |
|
446 | - $log->error( 'An error occurred: ' . $post_id->get_error_message() ); |
|
445 | + if (is_wp_error($post_id)) { |
|
446 | + $log->error('An error occurred: '.$post_id->get_error_message()); |
|
447 | 447 | |
448 | 448 | // inform an error occurred. |
449 | 449 | return null; |
450 | 450 | } |
451 | 451 | |
452 | - wl_set_entity_main_type( $post_id, $type_uri ); |
|
452 | + wl_set_entity_main_type($post_id, $type_uri); |
|
453 | 453 | |
454 | 454 | // Save the entity types. |
455 | - wl_set_entity_rdf_types( $post_id, $entity_types ); |
|
455 | + wl_set_entity_rdf_types($post_id, $entity_types); |
|
456 | 456 | |
457 | 457 | // Get a dataset URI for the entity. |
458 | - $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri( $post_id ); |
|
458 | + $wl_uri = Wordlift_Entity_Service::get_instance()->get_uri($post_id); |
|
459 | 459 | |
460 | 460 | // Add the uri to the sameAs data if it's not a local URI. |
461 | - if ( isset( $uri ) && preg_match( '@https?://.*@', $uri ) && |
|
461 | + if (isset($uri) && preg_match('@https?://.*@', $uri) && |
|
462 | 462 | $wl_uri !== $uri && |
463 | 463 | // Only remote entities |
464 | - 0 !== strpos( $uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri() ) |
|
464 | + 0 !== strpos($uri, Wordlift_Configuration_Service::get_instance()->get_dataset_uri()) |
|
465 | 465 | ) { |
466 | - array_push( $same_as, $uri ); |
|
466 | + array_push($same_as, $uri); |
|
467 | 467 | } |
468 | 468 | |
469 | 469 | // Save the sameAs data for the entity. |
470 | - wl_schema_set_value( $post_id, 'sameAs', $same_as ); |
|
470 | + wl_schema_set_value($post_id, 'sameAs', $same_as); |
|
471 | 471 | |
472 | 472 | // Save the other properties (latitude, langitude, startDate, endDate, etc.) |
473 | - foreach ( $other_properties as $property_name => $property_value ) { |
|
474 | - wl_schema_set_value( $post_id, $property_name, $property_value ); |
|
473 | + foreach ($other_properties as $property_name => $property_value) { |
|
474 | + wl_schema_set_value($post_id, $property_name, $property_value); |
|
475 | 475 | } |
476 | 476 | |
477 | 477 | // Call hooks. |
478 | - do_action( 'wl_save_entity', $post_id ); |
|
478 | + do_action('wl_save_entity', $post_id); |
|
479 | 479 | |
480 | - foreach ( $images as $image_remote_url ) { |
|
480 | + foreach ($images as $image_remote_url) { |
|
481 | 481 | |
482 | 482 | // Check if image is already present in local DB |
483 | - if ( strpos( $image_remote_url, site_url() ) !== false ) { |
|
483 | + if (strpos($image_remote_url, site_url()) !== false) { |
|
484 | 484 | // Do nothing. |
485 | 485 | continue; |
486 | 486 | } |
487 | 487 | |
488 | 488 | // Check if there is an existing attachment for this post ID and source URL. |
489 | - $existing_image = wl_get_attachment_for_source_url( $post_id, $image_remote_url ); |
|
489 | + $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url); |
|
490 | 490 | |
491 | 491 | // Skip if an existing image is found. |
492 | - if ( null !== $existing_image ) { |
|
492 | + if (null !== $existing_image) { |
|
493 | 493 | continue; |
494 | 494 | } |
495 | 495 | |
496 | 496 | // Save the image and get the local path. |
497 | - $image = Wordlift_Remote_Image_Service::save_from_url( $image_remote_url ); |
|
497 | + $image = Wordlift_Remote_Image_Service::save_from_url($image_remote_url); |
|
498 | 498 | |
499 | - if ( false === $image || is_wp_error( $image ) ) { |
|
499 | + if (false === $image || is_wp_error($image)) { |
|
500 | 500 | continue; |
501 | 501 | } |
502 | 502 | |
@@ -516,18 +516,18 @@ discard block |
||
516 | 516 | ); |
517 | 517 | |
518 | 518 | // Create the attachment in WordPress and generate the related metadata. |
519 | - $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); |
|
519 | + $attachment_id = wp_insert_attachment($attachment, $filename, $post_id); |
|
520 | 520 | |
521 | 521 | // Set the source URL for the image. |
522 | - wl_set_source_url( $attachment_id, $image_remote_url ); |
|
522 | + wl_set_source_url($attachment_id, $image_remote_url); |
|
523 | 523 | |
524 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
525 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
524 | + $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename); |
|
525 | + wp_update_attachment_metadata($attachment_id, $attachment_data); |
|
526 | 526 | |
527 | 527 | // Set it as the featured image. |
528 | - set_post_thumbnail( $post_id, $attachment_id ); |
|
528 | + set_post_thumbnail($post_id, $attachment_id); |
|
529 | 529 | } |
530 | 530 | |
531 | 531 | // finally return the entity post. |
532 | - return get_post( $post_id ); |
|
532 | + return get_post($post_id); |
|
533 | 533 | } |
@@ -7,74 +7,74 @@ |
||
7 | 7 | |
8 | 8 | class Jsonld { |
9 | 9 | |
10 | - /** |
|
11 | - * @var Content_Service $content_service |
|
12 | - */ |
|
13 | - private $content_service; |
|
10 | + /** |
|
11 | + * @var Content_Service $content_service |
|
12 | + */ |
|
13 | + private $content_service; |
|
14 | 14 | |
15 | - public function __construct( Content_Service $content_service ) { |
|
16 | - $this->content_service = $content_service; |
|
17 | - } |
|
15 | + public function __construct( Content_Service $content_service ) { |
|
16 | + $this->content_service = $content_service; |
|
17 | + } |
|
18 | 18 | |
19 | - public function register_hooks() { |
|
20 | - add_action( 'wl_post_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
21 | - add_action( 'wl_entity_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
19 | + public function register_hooks() { |
|
20 | + add_action( 'wl_post_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
21 | + add_action( 'wl_entity_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
22 | 22 | |
23 | - } |
|
23 | + } |
|
24 | 24 | |
25 | - public function post_jsonld( $jsonld, $post_id ) { |
|
25 | + public function post_jsonld( $jsonld, $post_id ) { |
|
26 | 26 | |
27 | - $mentions = array(); |
|
28 | - $term_ids = $this->get_term_ids( $post_id ); |
|
27 | + $mentions = array(); |
|
28 | + $term_ids = $this->get_term_ids( $post_id ); |
|
29 | 29 | |
30 | - foreach ( $term_ids as $term_id ) { |
|
31 | - $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_term( $term_id ) ); |
|
32 | - } |
|
30 | + foreach ( $term_ids as $term_id ) { |
|
31 | + $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_term( $term_id ) ); |
|
32 | + } |
|
33 | 33 | |
34 | - // Add also the post jsonld. |
|
35 | - $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_post( $post_id ) ); |
|
34 | + // Add also the post jsonld. |
|
35 | + $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_post( $post_id ) ); |
|
36 | 36 | |
37 | - $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
37 | + $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
38 | 38 | |
39 | - if ( count( $mentions ) > 0 ) { |
|
40 | - $jsonld['mentions'] = array_merge( $existing_mentions, $mentions ); |
|
41 | - } |
|
42 | - return $jsonld; |
|
43 | - } |
|
39 | + if ( count( $mentions ) > 0 ) { |
|
40 | + $jsonld['mentions'] = array_merge( $existing_mentions, $mentions ); |
|
41 | + } |
|
42 | + return $jsonld; |
|
43 | + } |
|
44 | 44 | |
45 | - private function get_term_ids( $post_id ) { |
|
46 | - global $wpdb; |
|
47 | - $result = $wpdb->get_col( |
|
48 | - $wpdb->prepare( |
|
49 | - "SELECT r.term_id from {$wpdb->prefix}term_relationships r |
|
45 | + private function get_term_ids( $post_id ) { |
|
46 | + global $wpdb; |
|
47 | + $result = $wpdb->get_col( |
|
48 | + $wpdb->prepare( |
|
49 | + "SELECT r.term_id from {$wpdb->prefix}term_relationships r |
|
50 | 50 | INNER JOIN {$wpdb->prefix}term_taxonomy tt ON r.term_taxonomy_id=tt.term_taxonomy_id WHERE r.object_id= %d", |
51 | - $post_id |
|
52 | - ) |
|
53 | - ); |
|
54 | - |
|
55 | - return is_array( $result ) ? $result : array(); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param $list array Adds the jsonld to the list if valid. |
|
60 | - * @param $content_id \Wordlift\Content\Wordpress\Wordpress_Content_Id |
|
61 | - * |
|
62 | - * @return void |
|
63 | - */ |
|
64 | - private function add_about_jsonld( &$list, $content_id ) { |
|
65 | - $about_jsonld = $this->content_service->get_about_jsonld( |
|
66 | - $content_id |
|
67 | - ); |
|
68 | - $decoded_jsonld = json_decode( $about_jsonld, true ); |
|
69 | - |
|
70 | - if ( null === $decoded_jsonld ) { |
|
71 | - return; |
|
72 | - } |
|
73 | - |
|
74 | - $value = wp_is_numeric_array( $decoded_jsonld ) ? $decoded_jsonld : array( $decoded_jsonld ); |
|
75 | - |
|
76 | - $list = array_merge( $list, $value ); |
|
77 | - |
|
78 | - } |
|
51 | + $post_id |
|
52 | + ) |
|
53 | + ); |
|
54 | + |
|
55 | + return is_array( $result ) ? $result : array(); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param $list array Adds the jsonld to the list if valid. |
|
60 | + * @param $content_id \Wordlift\Content\Wordpress\Wordpress_Content_Id |
|
61 | + * |
|
62 | + * @return void |
|
63 | + */ |
|
64 | + private function add_about_jsonld( &$list, $content_id ) { |
|
65 | + $about_jsonld = $this->content_service->get_about_jsonld( |
|
66 | + $content_id |
|
67 | + ); |
|
68 | + $decoded_jsonld = json_decode( $about_jsonld, true ); |
|
69 | + |
|
70 | + if ( null === $decoded_jsonld ) { |
|
71 | + return; |
|
72 | + } |
|
73 | + |
|
74 | + $value = wp_is_numeric_array( $decoded_jsonld ) ? $decoded_jsonld : array( $decoded_jsonld ); |
|
75 | + |
|
76 | + $list = array_merge( $list, $value ); |
|
77 | + |
|
78 | + } |
|
79 | 79 | |
80 | 80 | } |
@@ -12,37 +12,37 @@ discard block |
||
12 | 12 | */ |
13 | 13 | private $content_service; |
14 | 14 | |
15 | - public function __construct( Content_Service $content_service ) { |
|
15 | + public function __construct(Content_Service $content_service) { |
|
16 | 16 | $this->content_service = $content_service; |
17 | 17 | } |
18 | 18 | |
19 | 19 | public function register_hooks() { |
20 | - add_action( 'wl_post_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
21 | - add_action( 'wl_entity_jsonld', array( $this, 'post_jsonld' ), 10, 2 ); |
|
20 | + add_action('wl_post_jsonld', array($this, 'post_jsonld'), 10, 2); |
|
21 | + add_action('wl_entity_jsonld', array($this, 'post_jsonld'), 10, 2); |
|
22 | 22 | |
23 | 23 | } |
24 | 24 | |
25 | - public function post_jsonld( $jsonld, $post_id ) { |
|
25 | + public function post_jsonld($jsonld, $post_id) { |
|
26 | 26 | |
27 | 27 | $mentions = array(); |
28 | - $term_ids = $this->get_term_ids( $post_id ); |
|
28 | + $term_ids = $this->get_term_ids($post_id); |
|
29 | 29 | |
30 | - foreach ( $term_ids as $term_id ) { |
|
31 | - $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_term( $term_id ) ); |
|
30 | + foreach ($term_ids as $term_id) { |
|
31 | + $this->add_about_jsonld($mentions, Wordpress_Content_Id::create_term($term_id)); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | // Add also the post jsonld. |
35 | - $this->add_about_jsonld( $mentions, Wordpress_Content_Id::create_post( $post_id ) ); |
|
35 | + $this->add_about_jsonld($mentions, Wordpress_Content_Id::create_post($post_id)); |
|
36 | 36 | |
37 | - $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
37 | + $existing_mentions = array_key_exists('mentions', $jsonld) ? $jsonld['mentions'] : array(); |
|
38 | 38 | |
39 | - if ( count( $mentions ) > 0 ) { |
|
40 | - $jsonld['mentions'] = array_merge( $existing_mentions, $mentions ); |
|
39 | + if (count($mentions) > 0) { |
|
40 | + $jsonld['mentions'] = array_merge($existing_mentions, $mentions); |
|
41 | 41 | } |
42 | 42 | return $jsonld; |
43 | 43 | } |
44 | 44 | |
45 | - private function get_term_ids( $post_id ) { |
|
45 | + private function get_term_ids($post_id) { |
|
46 | 46 | global $wpdb; |
47 | 47 | $result = $wpdb->get_col( |
48 | 48 | $wpdb->prepare( |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | ) |
53 | 53 | ); |
54 | 54 | |
55 | - return is_array( $result ) ? $result : array(); |
|
55 | + return is_array($result) ? $result : array(); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
@@ -61,19 +61,19 @@ discard block |
||
61 | 61 | * |
62 | 62 | * @return void |
63 | 63 | */ |
64 | - private function add_about_jsonld( &$list, $content_id ) { |
|
64 | + private function add_about_jsonld(&$list, $content_id) { |
|
65 | 65 | $about_jsonld = $this->content_service->get_about_jsonld( |
66 | 66 | $content_id |
67 | 67 | ); |
68 | - $decoded_jsonld = json_decode( $about_jsonld, true ); |
|
68 | + $decoded_jsonld = json_decode($about_jsonld, true); |
|
69 | 69 | |
70 | - if ( null === $decoded_jsonld ) { |
|
70 | + if (null === $decoded_jsonld) { |
|
71 | 71 | return; |
72 | 72 | } |
73 | 73 | |
74 | - $value = wp_is_numeric_array( $decoded_jsonld ) ? $decoded_jsonld : array( $decoded_jsonld ); |
|
74 | + $value = wp_is_numeric_array($decoded_jsonld) ? $decoded_jsonld : array($decoded_jsonld); |
|
75 | 75 | |
76 | - $list = array_merge( $list, $value ); |
|
76 | + $list = array_merge($list, $value); |
|
77 | 77 | |
78 | 78 | } |
79 | 79 |
@@ -18,42 +18,42 @@ |
||
18 | 18 | |
19 | 19 | class No_Annotation_Strategy extends Singleton implements Occurrences { |
20 | 20 | |
21 | - /** |
|
22 | - * @var Relation_Service_Interface |
|
23 | - */ |
|
24 | - private $relation_service; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var Content_Service |
|
28 | - */ |
|
29 | - private $content_service; |
|
30 | - |
|
31 | - protected function __construct() { |
|
32 | - parent::__construct(); |
|
33 | - $this->relation_service = Relation_Service::get_instance(); |
|
34 | - $this->content_service = Wordpress_Content_Service::get_instance(); |
|
35 | - } |
|
36 | - |
|
37 | - public function add_occurrences_to_entities( $occurrences, $json, $post_id ) { |
|
38 | - |
|
39 | - $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
40 | - $relation_service = Relation_Service::get_instance(); |
|
41 | - $relations = $relation_service->get_relations( $content_id ); |
|
42 | - |
|
43 | - /** @var Relation $relation */ |
|
44 | - foreach ( $relations->toArray() as $relation ) { |
|
45 | - $object = $relation->get_object(); |
|
46 | - // @@todo is this working okey with term id? |
|
47 | - $entity_uri = $this->content_service->get_entity_id( $relation->get_object() ); |
|
48 | - if ( ! $entity_uri ) { |
|
49 | - continue; |
|
50 | - } |
|
51 | - $entity_data = wl_serialize_entity( $object->get_id() ); |
|
52 | - $entity_data['occurrences'] = array( 'placeholder-occurrence' ); |
|
53 | - $json->entities->{$entity_uri} = $entity_data; |
|
54 | - } |
|
55 | - |
|
56 | - return $json; |
|
57 | - } |
|
21 | + /** |
|
22 | + * @var Relation_Service_Interface |
|
23 | + */ |
|
24 | + private $relation_service; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var Content_Service |
|
28 | + */ |
|
29 | + private $content_service; |
|
30 | + |
|
31 | + protected function __construct() { |
|
32 | + parent::__construct(); |
|
33 | + $this->relation_service = Relation_Service::get_instance(); |
|
34 | + $this->content_service = Wordpress_Content_Service::get_instance(); |
|
35 | + } |
|
36 | + |
|
37 | + public function add_occurrences_to_entities( $occurrences, $json, $post_id ) { |
|
38 | + |
|
39 | + $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
40 | + $relation_service = Relation_Service::get_instance(); |
|
41 | + $relations = $relation_service->get_relations( $content_id ); |
|
42 | + |
|
43 | + /** @var Relation $relation */ |
|
44 | + foreach ( $relations->toArray() as $relation ) { |
|
45 | + $object = $relation->get_object(); |
|
46 | + // @@todo is this working okey with term id? |
|
47 | + $entity_uri = $this->content_service->get_entity_id( $relation->get_object() ); |
|
48 | + if ( ! $entity_uri ) { |
|
49 | + continue; |
|
50 | + } |
|
51 | + $entity_data = wl_serialize_entity( $object->get_id() ); |
|
52 | + $entity_data['occurrences'] = array( 'placeholder-occurrence' ); |
|
53 | + $json->entities->{$entity_uri} = $entity_data; |
|
54 | + } |
|
55 | + |
|
56 | + return $json; |
|
57 | + } |
|
58 | 58 | |
59 | 59 | } |
@@ -34,22 +34,22 @@ |
||
34 | 34 | $this->content_service = Wordpress_Content_Service::get_instance(); |
35 | 35 | } |
36 | 36 | |
37 | - public function add_occurrences_to_entities( $occurrences, $json, $post_id ) { |
|
37 | + public function add_occurrences_to_entities($occurrences, $json, $post_id) { |
|
38 | 38 | |
39 | - $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
39 | + $content_id = Wordpress_Content_Id::create_post($post_id); |
|
40 | 40 | $relation_service = Relation_Service::get_instance(); |
41 | - $relations = $relation_service->get_relations( $content_id ); |
|
41 | + $relations = $relation_service->get_relations($content_id); |
|
42 | 42 | |
43 | 43 | /** @var Relation $relation */ |
44 | - foreach ( $relations->toArray() as $relation ) { |
|
44 | + foreach ($relations->toArray() as $relation) { |
|
45 | 45 | $object = $relation->get_object(); |
46 | 46 | // @@todo is this working okey with term id? |
47 | - $entity_uri = $this->content_service->get_entity_id( $relation->get_object() ); |
|
48 | - if ( ! $entity_uri ) { |
|
47 | + $entity_uri = $this->content_service->get_entity_id($relation->get_object()); |
|
48 | + if ( ! $entity_uri) { |
|
49 | 49 | continue; |
50 | 50 | } |
51 | - $entity_data = wl_serialize_entity( $object->get_id() ); |
|
52 | - $entity_data['occurrences'] = array( 'placeholder-occurrence' ); |
|
51 | + $entity_data = wl_serialize_entity($object->get_id()); |
|
52 | + $entity_data['occurrences'] = array('placeholder-occurrence'); |
|
53 | 53 | $json->entities->{$entity_uri} = $entity_data; |
54 | 54 | } |
55 | 55 |
@@ -11,111 +11,111 @@ |
||
11 | 11 | |
12 | 12 | class Post_Jsonld { |
13 | 13 | |
14 | - public function enhance_post_jsonld() { |
|
15 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
16 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11 ); |
|
17 | - } |
|
14 | + public function enhance_post_jsonld() { |
|
15 | + add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
16 | + add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11 ); |
|
17 | + } |
|
18 | 18 | |
19 | - public function wl_post_jsonld_array( $arr, $post_id ) { |
|
19 | + public function wl_post_jsonld_array( $arr, $post_id ) { |
|
20 | 20 | |
21 | - $jsonld = $arr['jsonld']; |
|
22 | - $references = $arr['references']; |
|
21 | + $jsonld = $arr['jsonld']; |
|
22 | + $references = $arr['references']; |
|
23 | 23 | |
24 | - $this->add_mentions( $post_id, $jsonld ); |
|
24 | + $this->add_mentions( $post_id, $jsonld ); |
|
25 | 25 | |
26 | - return array( |
|
27 | - 'jsonld' => $jsonld, |
|
28 | - 'references' => $references, |
|
29 | - ); |
|
26 | + return array( |
|
27 | + 'jsonld' => $jsonld, |
|
28 | + 'references' => $references, |
|
29 | + ); |
|
30 | 30 | |
31 | - } |
|
31 | + } |
|
32 | 32 | |
33 | - public function add_mentions( $post_id, &$jsonld ) { |
|
33 | + public function add_mentions( $post_id, &$jsonld ) { |
|
34 | 34 | |
35 | - $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
36 | - $terms = array(); |
|
35 | + $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
36 | + $terms = array(); |
|
37 | 37 | |
38 | - foreach ( $taxonomies as $taxonomy ) { |
|
39 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
40 | - if ( ! $taxonomy_terms ) { |
|
41 | - continue; |
|
42 | - } |
|
43 | - $terms = array_merge( $taxonomy_terms, $terms ); |
|
44 | - } |
|
38 | + foreach ( $taxonomies as $taxonomy ) { |
|
39 | + $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
40 | + if ( ! $taxonomy_terms ) { |
|
41 | + continue; |
|
42 | + } |
|
43 | + $terms = array_merge( $taxonomy_terms, $terms ); |
|
44 | + } |
|
45 | 45 | |
46 | - if ( ! $terms ) { |
|
47 | - return; |
|
48 | - } |
|
46 | + if ( ! $terms ) { |
|
47 | + return; |
|
48 | + } |
|
49 | 49 | |
50 | - if ( ! array_key_exists( 'mentions', $jsonld ) && count( $terms ) > 0 ) { |
|
51 | - $jsonld['mentions'] = array(); |
|
52 | - } |
|
50 | + if ( ! array_key_exists( 'mentions', $jsonld ) && count( $terms ) > 0 ) { |
|
51 | + $jsonld['mentions'] = array(); |
|
52 | + } |
|
53 | 53 | |
54 | - foreach ( $terms as $term ) { |
|
54 | + foreach ( $terms as $term ) { |
|
55 | 55 | |
56 | - $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
56 | + $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
57 | 57 | |
58 | - if ( ! $is_matched ) { |
|
59 | - continue; |
|
60 | - } |
|
58 | + if ( ! $is_matched ) { |
|
59 | + continue; |
|
60 | + } |
|
61 | 61 | |
62 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term->term_id ); |
|
62 | + $entities = Jsonld_Utils::get_matched_entities_for_term( $term->term_id ); |
|
63 | 63 | |
64 | - if ( count( $entities ) === 0 ) { |
|
65 | - continue; |
|
66 | - } |
|
64 | + if ( count( $entities ) === 0 ) { |
|
65 | + continue; |
|
66 | + } |
|
67 | 67 | |
68 | - $add_additional_attrs = self::add_additional_attrs( $term, $entities ); |
|
68 | + $add_additional_attrs = self::add_additional_attrs( $term, $entities ); |
|
69 | 69 | |
70 | - $jsonld['mentions'] = array_merge( $jsonld['mentions'], $add_additional_attrs ); |
|
71 | - } |
|
70 | + $jsonld['mentions'] = array_merge( $jsonld['mentions'], $add_additional_attrs ); |
|
71 | + } |
|
72 | 72 | |
73 | - } |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * @param $term \WP_Term |
|
77 | - * @param $entities |
|
78 | - * |
|
79 | - * @return array |
|
80 | - */ |
|
81 | - public static function add_additional_attrs( $term, $entities ) { |
|
75 | + /** |
|
76 | + * @param $term \WP_Term |
|
77 | + * @param $entities |
|
78 | + * |
|
79 | + * @return array |
|
80 | + */ |
|
81 | + public static function add_additional_attrs( $term, $entities ) { |
|
82 | 82 | |
83 | - return array_map( |
|
84 | - function ( $entity ) use ( $term ) { |
|
85 | - $entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
86 | - if ( ! empty( $term->description ) ) { |
|
87 | - $entity['description'] = $term->description; |
|
88 | - } |
|
83 | + return array_map( |
|
84 | + function ( $entity ) use ( $term ) { |
|
85 | + $entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
86 | + if ( ! empty( $term->description ) ) { |
|
87 | + $entity['description'] = $term->description; |
|
88 | + } |
|
89 | 89 | |
90 | - return $entity; |
|
90 | + return $entity; |
|
91 | 91 | |
92 | - }, |
|
93 | - $entities |
|
94 | - ); |
|
92 | + }, |
|
93 | + $entities |
|
94 | + ); |
|
95 | 95 | |
96 | - } |
|
96 | + } |
|
97 | 97 | |
98 | - public function wl_after_get_jsonld( $jsonld ) { |
|
98 | + public function wl_after_get_jsonld( $jsonld ) { |
|
99 | 99 | |
100 | - if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
101 | - return $jsonld; |
|
102 | - } |
|
100 | + if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
101 | + return $jsonld; |
|
102 | + } |
|
103 | 103 | |
104 | - foreach ( $jsonld as $key => $value ) { |
|
105 | - if ( 'Article' === $value['@type'] && isset( $value['image'] ) ) { |
|
106 | - $image = $value['image']; |
|
107 | - } |
|
108 | - if ( 'Recipe' === $value['@type'] && ! isset( $value['image'] ) ) { |
|
109 | - $index = $key; |
|
110 | - } |
|
111 | - } |
|
104 | + foreach ( $jsonld as $key => $value ) { |
|
105 | + if ( 'Article' === $value['@type'] && isset( $value['image'] ) ) { |
|
106 | + $image = $value['image']; |
|
107 | + } |
|
108 | + if ( 'Recipe' === $value['@type'] && ! isset( $value['image'] ) ) { |
|
109 | + $index = $key; |
|
110 | + } |
|
111 | + } |
|
112 | 112 | |
113 | - if ( isset( $index ) && ! empty( $image ) ) { |
|
114 | - $jsonld[ $index ]['image'] = $image; |
|
115 | - } |
|
113 | + if ( isset( $index ) && ! empty( $image ) ) { |
|
114 | + $jsonld[ $index ]['image'] = $image; |
|
115 | + } |
|
116 | 116 | |
117 | - return $jsonld; |
|
117 | + return $jsonld; |
|
118 | 118 | |
119 | - } |
|
119 | + } |
|
120 | 120 | |
121 | 121 | } |
@@ -12,16 +12,16 @@ discard block |
||
12 | 12 | class Post_Jsonld { |
13 | 13 | |
14 | 14 | public function enhance_post_jsonld() { |
15 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
16 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11 ); |
|
15 | + add_filter('wl_post_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 2); |
|
16 | + add_filter('wl_after_get_jsonld', array($this, 'wl_after_get_jsonld'), 11); |
|
17 | 17 | } |
18 | 18 | |
19 | - public function wl_post_jsonld_array( $arr, $post_id ) { |
|
19 | + public function wl_post_jsonld_array($arr, $post_id) { |
|
20 | 20 | |
21 | 21 | $jsonld = $arr['jsonld']; |
22 | 22 | $references = $arr['references']; |
23 | 23 | |
24 | - $this->add_mentions( $post_id, $jsonld ); |
|
24 | + $this->add_mentions($post_id, $jsonld); |
|
25 | 25 | |
26 | 26 | return array( |
27 | 27 | 'jsonld' => $jsonld, |
@@ -30,44 +30,44 @@ discard block |
||
30 | 30 | |
31 | 31 | } |
32 | 32 | |
33 | - public function add_mentions( $post_id, &$jsonld ) { |
|
33 | + public function add_mentions($post_id, &$jsonld) { |
|
34 | 34 | |
35 | 35 | $taxonomies = Terms_Compat::get_public_taxonomies(); |
36 | 36 | $terms = array(); |
37 | 37 | |
38 | - foreach ( $taxonomies as $taxonomy ) { |
|
39 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
40 | - if ( ! $taxonomy_terms ) { |
|
38 | + foreach ($taxonomies as $taxonomy) { |
|
39 | + $taxonomy_terms = get_the_terms($post_id, $taxonomy); |
|
40 | + if ( ! $taxonomy_terms) { |
|
41 | 41 | continue; |
42 | 42 | } |
43 | - $terms = array_merge( $taxonomy_terms, $terms ); |
|
43 | + $terms = array_merge($taxonomy_terms, $terms); |
|
44 | 44 | } |
45 | 45 | |
46 | - if ( ! $terms ) { |
|
46 | + if ( ! $terms) { |
|
47 | 47 | return; |
48 | 48 | } |
49 | 49 | |
50 | - if ( ! array_key_exists( 'mentions', $jsonld ) && count( $terms ) > 0 ) { |
|
50 | + if ( ! array_key_exists('mentions', $jsonld) && count($terms) > 0) { |
|
51 | 51 | $jsonld['mentions'] = array(); |
52 | 52 | } |
53 | 53 | |
54 | - foreach ( $terms as $term ) { |
|
54 | + foreach ($terms as $term) { |
|
55 | 55 | |
56 | - $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
56 | + $is_matched = intval(get_term_meta($term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true)) === 1; |
|
57 | 57 | |
58 | - if ( ! $is_matched ) { |
|
58 | + if ( ! $is_matched) { |
|
59 | 59 | continue; |
60 | 60 | } |
61 | 61 | |
62 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term->term_id ); |
|
62 | + $entities = Jsonld_Utils::get_matched_entities_for_term($term->term_id); |
|
63 | 63 | |
64 | - if ( count( $entities ) === 0 ) { |
|
64 | + if (count($entities) === 0) { |
|
65 | 65 | continue; |
66 | 66 | } |
67 | 67 | |
68 | - $add_additional_attrs = self::add_additional_attrs( $term, $entities ); |
|
68 | + $add_additional_attrs = self::add_additional_attrs($term, $entities); |
|
69 | 69 | |
70 | - $jsonld['mentions'] = array_merge( $jsonld['mentions'], $add_additional_attrs ); |
|
70 | + $jsonld['mentions'] = array_merge($jsonld['mentions'], $add_additional_attrs); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | } |
@@ -78,12 +78,12 @@ discard block |
||
78 | 78 | * |
79 | 79 | * @return array |
80 | 80 | */ |
81 | - public static function add_additional_attrs( $term, $entities ) { |
|
81 | + public static function add_additional_attrs($term, $entities) { |
|
82 | 82 | |
83 | 83 | return array_map( |
84 | - function ( $entity ) use ( $term ) { |
|
85 | - $entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
86 | - if ( ! empty( $term->description ) ) { |
|
84 | + function($entity) use ($term) { |
|
85 | + $entity['@id'] = get_term_link($term->term_id).'#id'; |
|
86 | + if ( ! empty($term->description)) { |
|
87 | 87 | $entity['description'] = $term->description; |
88 | 88 | } |
89 | 89 | |
@@ -95,23 +95,23 @@ discard block |
||
95 | 95 | |
96 | 96 | } |
97 | 97 | |
98 | - public function wl_after_get_jsonld( $jsonld ) { |
|
98 | + public function wl_after_get_jsonld($jsonld) { |
|
99 | 99 | |
100 | - if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
100 | + if ( ! is_array($jsonld) || count($jsonld) === 0) { |
|
101 | 101 | return $jsonld; |
102 | 102 | } |
103 | 103 | |
104 | - foreach ( $jsonld as $key => $value ) { |
|
105 | - if ( 'Article' === $value['@type'] && isset( $value['image'] ) ) { |
|
104 | + foreach ($jsonld as $key => $value) { |
|
105 | + if ('Article' === $value['@type'] && isset($value['image'])) { |
|
106 | 106 | $image = $value['image']; |
107 | 107 | } |
108 | - if ( 'Recipe' === $value['@type'] && ! isset( $value['image'] ) ) { |
|
108 | + if ('Recipe' === $value['@type'] && ! isset($value['image'])) { |
|
109 | 109 | $index = $key; |
110 | 110 | } |
111 | 111 | } |
112 | 112 | |
113 | - if ( isset( $index ) && ! empty( $image ) ) { |
|
114 | - $jsonld[ $index ]['image'] = $image; |
|
113 | + if (isset($index) && ! empty($image)) { |
|
114 | + $jsonld[$index]['image'] = $image; |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | return $jsonld; |
@@ -17,109 +17,109 @@ |
||
17 | 17 | |
18 | 18 | class Post_Jsonld { |
19 | 19 | |
20 | - public function init() { |
|
21 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 10, 2 ); |
|
22 | - } |
|
23 | - |
|
24 | - public function wl_post_jsonld_array( $data, $post_id ) { |
|
25 | - |
|
26 | - $term_relations = $this->get_term_relations( $post_id ); |
|
27 | - |
|
28 | - if ( ! $term_relations ) { |
|
29 | - return $data; |
|
30 | - } |
|
31 | - |
|
32 | - $references = $data['references']; |
|
33 | - $jsonld = $data['jsonld']; |
|
34 | - $references_infos = $data['references_infos']; |
|
35 | - /** |
|
36 | - * @var $relations Relations |
|
37 | - */ |
|
38 | - $relations = $data['relations']; |
|
39 | - $relations->add( ...$term_relations ); |
|
40 | - |
|
41 | - $term_mentions = $this->get_term_mentions( $term_relations ); |
|
42 | - if ( count( $term_mentions ) > 0 ) { |
|
43 | - $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
44 | - $jsonld['mentions'] = array_merge( $existing_mentions, $term_mentions ); |
|
45 | - } |
|
46 | - |
|
47 | - return array( |
|
48 | - 'jsonld' => $jsonld, |
|
49 | - 'references' => $references, |
|
50 | - 'references_infos' => $references_infos, |
|
51 | - 'relations' => $relations, |
|
52 | - ); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @param $post_id |
|
57 | - * @param $relations Relations |
|
58 | - * |
|
59 | - * @return array<Relation> Returns a list of term relations. |
|
60 | - */ |
|
61 | - private function get_term_relations( $post_id ) { |
|
62 | - |
|
63 | - /** @var WP_Taxonomy[] $taxonomies_for_post */ |
|
64 | - $taxonomies_for_post = get_object_taxonomies( get_post_type( $post_id ), 'objects' ); |
|
65 | - |
|
66 | - // now we need to collect all terms attached to this post. |
|
67 | - $terms = array(); |
|
68 | - |
|
69 | - foreach ( $taxonomies_for_post as $taxonomy ) { |
|
70 | - // Please note that `$taxonomy->publicly_queryable is only WP 4.7+ |
|
71 | - if ( 'wl_entity_type' === $taxonomy->name || ! $taxonomy->public ) { |
|
72 | - continue; |
|
73 | - } |
|
74 | - |
|
75 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy->name ); |
|
76 | - if ( is_array( $taxonomy_terms ) ) { |
|
77 | - $terms = array_merge( $terms, $taxonomy_terms ); |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - // Convert everything to the Term Reference. |
|
82 | - return array_filter( |
|
83 | - array_map( |
|
84 | - function ( $term ) use ( $post_id ) { |
|
85 | - /** |
|
86 | - * @var WP_Term $term |
|
87 | - */ |
|
88 | - if ( Wordpress_Term_Content_Legacy_Service::get_instance() |
|
89 | - ->get_entity_id( Wordpress_Content_Id::create_term( $term->term_id ) ) |
|
90 | - ) { |
|
91 | - return new Relation( |
|
92 | - Wordpress_Content_Id::create_post( $post_id ), |
|
93 | - Wordpress_Content_Id::create_term( $term->term_id ), |
|
94 | - WL_WHAT_RELATION |
|
95 | - ); |
|
96 | - } |
|
97 | - |
|
98 | - return false; |
|
99 | - }, |
|
100 | - $terms |
|
101 | - ) |
|
102 | - ); |
|
103 | - |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @param $term_relations array<Relation> |
|
108 | - * |
|
109 | - * @return array |
|
110 | - */ |
|
111 | - private function get_term_mentions( $term_relations ) { |
|
112 | - |
|
113 | - return array_map( |
|
114 | - function ( $term_relation ) { |
|
115 | - return array( |
|
116 | - '@id' => Wordpress_Term_Content_Legacy_Service::get_instance() |
|
117 | - ->get_entity_id( $term_relation->get_object() ), |
|
118 | - ); |
|
119 | - }, |
|
120 | - $term_relations |
|
121 | - ); |
|
122 | - |
|
123 | - } |
|
20 | + public function init() { |
|
21 | + add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 10, 2 ); |
|
22 | + } |
|
23 | + |
|
24 | + public function wl_post_jsonld_array( $data, $post_id ) { |
|
25 | + |
|
26 | + $term_relations = $this->get_term_relations( $post_id ); |
|
27 | + |
|
28 | + if ( ! $term_relations ) { |
|
29 | + return $data; |
|
30 | + } |
|
31 | + |
|
32 | + $references = $data['references']; |
|
33 | + $jsonld = $data['jsonld']; |
|
34 | + $references_infos = $data['references_infos']; |
|
35 | + /** |
|
36 | + * @var $relations Relations |
|
37 | + */ |
|
38 | + $relations = $data['relations']; |
|
39 | + $relations->add( ...$term_relations ); |
|
40 | + |
|
41 | + $term_mentions = $this->get_term_mentions( $term_relations ); |
|
42 | + if ( count( $term_mentions ) > 0 ) { |
|
43 | + $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
44 | + $jsonld['mentions'] = array_merge( $existing_mentions, $term_mentions ); |
|
45 | + } |
|
46 | + |
|
47 | + return array( |
|
48 | + 'jsonld' => $jsonld, |
|
49 | + 'references' => $references, |
|
50 | + 'references_infos' => $references_infos, |
|
51 | + 'relations' => $relations, |
|
52 | + ); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @param $post_id |
|
57 | + * @param $relations Relations |
|
58 | + * |
|
59 | + * @return array<Relation> Returns a list of term relations. |
|
60 | + */ |
|
61 | + private function get_term_relations( $post_id ) { |
|
62 | + |
|
63 | + /** @var WP_Taxonomy[] $taxonomies_for_post */ |
|
64 | + $taxonomies_for_post = get_object_taxonomies( get_post_type( $post_id ), 'objects' ); |
|
65 | + |
|
66 | + // now we need to collect all terms attached to this post. |
|
67 | + $terms = array(); |
|
68 | + |
|
69 | + foreach ( $taxonomies_for_post as $taxonomy ) { |
|
70 | + // Please note that `$taxonomy->publicly_queryable is only WP 4.7+ |
|
71 | + if ( 'wl_entity_type' === $taxonomy->name || ! $taxonomy->public ) { |
|
72 | + continue; |
|
73 | + } |
|
74 | + |
|
75 | + $taxonomy_terms = get_the_terms( $post_id, $taxonomy->name ); |
|
76 | + if ( is_array( $taxonomy_terms ) ) { |
|
77 | + $terms = array_merge( $terms, $taxonomy_terms ); |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + // Convert everything to the Term Reference. |
|
82 | + return array_filter( |
|
83 | + array_map( |
|
84 | + function ( $term ) use ( $post_id ) { |
|
85 | + /** |
|
86 | + * @var WP_Term $term |
|
87 | + */ |
|
88 | + if ( Wordpress_Term_Content_Legacy_Service::get_instance() |
|
89 | + ->get_entity_id( Wordpress_Content_Id::create_term( $term->term_id ) ) |
|
90 | + ) { |
|
91 | + return new Relation( |
|
92 | + Wordpress_Content_Id::create_post( $post_id ), |
|
93 | + Wordpress_Content_Id::create_term( $term->term_id ), |
|
94 | + WL_WHAT_RELATION |
|
95 | + ); |
|
96 | + } |
|
97 | + |
|
98 | + return false; |
|
99 | + }, |
|
100 | + $terms |
|
101 | + ) |
|
102 | + ); |
|
103 | + |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @param $term_relations array<Relation> |
|
108 | + * |
|
109 | + * @return array |
|
110 | + */ |
|
111 | + private function get_term_mentions( $term_relations ) { |
|
112 | + |
|
113 | + return array_map( |
|
114 | + function ( $term_relation ) { |
|
115 | + return array( |
|
116 | + '@id' => Wordpress_Term_Content_Legacy_Service::get_instance() |
|
117 | + ->get_entity_id( $term_relation->get_object() ), |
|
118 | + ); |
|
119 | + }, |
|
120 | + $term_relations |
|
121 | + ); |
|
122 | + |
|
123 | + } |
|
124 | 124 | |
125 | 125 | } |
@@ -18,14 +18,14 @@ discard block |
||
18 | 18 | class Post_Jsonld { |
19 | 19 | |
20 | 20 | public function init() { |
21 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 10, 2 ); |
|
21 | + add_filter('wl_post_jsonld_array', array($this, 'wl_post_jsonld_array'), 10, 2); |
|
22 | 22 | } |
23 | 23 | |
24 | - public function wl_post_jsonld_array( $data, $post_id ) { |
|
24 | + public function wl_post_jsonld_array($data, $post_id) { |
|
25 | 25 | |
26 | - $term_relations = $this->get_term_relations( $post_id ); |
|
26 | + $term_relations = $this->get_term_relations($post_id); |
|
27 | 27 | |
28 | - if ( ! $term_relations ) { |
|
28 | + if ( ! $term_relations) { |
|
29 | 29 | return $data; |
30 | 30 | } |
31 | 31 | |
@@ -36,12 +36,12 @@ discard block |
||
36 | 36 | * @var $relations Relations |
37 | 37 | */ |
38 | 38 | $relations = $data['relations']; |
39 | - $relations->add( ...$term_relations ); |
|
39 | + $relations->add(...$term_relations); |
|
40 | 40 | |
41 | - $term_mentions = $this->get_term_mentions( $term_relations ); |
|
42 | - if ( count( $term_mentions ) > 0 ) { |
|
43 | - $existing_mentions = array_key_exists( 'mentions', $jsonld ) ? $jsonld['mentions'] : array(); |
|
44 | - $jsonld['mentions'] = array_merge( $existing_mentions, $term_mentions ); |
|
41 | + $term_mentions = $this->get_term_mentions($term_relations); |
|
42 | + if (count($term_mentions) > 0) { |
|
43 | + $existing_mentions = array_key_exists('mentions', $jsonld) ? $jsonld['mentions'] : array(); |
|
44 | + $jsonld['mentions'] = array_merge($existing_mentions, $term_mentions); |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | return array( |
@@ -58,39 +58,39 @@ discard block |
||
58 | 58 | * |
59 | 59 | * @return array<Relation> Returns a list of term relations. |
60 | 60 | */ |
61 | - private function get_term_relations( $post_id ) { |
|
61 | + private function get_term_relations($post_id) { |
|
62 | 62 | |
63 | 63 | /** @var WP_Taxonomy[] $taxonomies_for_post */ |
64 | - $taxonomies_for_post = get_object_taxonomies( get_post_type( $post_id ), 'objects' ); |
|
64 | + $taxonomies_for_post = get_object_taxonomies(get_post_type($post_id), 'objects'); |
|
65 | 65 | |
66 | 66 | // now we need to collect all terms attached to this post. |
67 | 67 | $terms = array(); |
68 | 68 | |
69 | - foreach ( $taxonomies_for_post as $taxonomy ) { |
|
69 | + foreach ($taxonomies_for_post as $taxonomy) { |
|
70 | 70 | // Please note that `$taxonomy->publicly_queryable is only WP 4.7+ |
71 | - if ( 'wl_entity_type' === $taxonomy->name || ! $taxonomy->public ) { |
|
71 | + if ('wl_entity_type' === $taxonomy->name || ! $taxonomy->public) { |
|
72 | 72 | continue; |
73 | 73 | } |
74 | 74 | |
75 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy->name ); |
|
76 | - if ( is_array( $taxonomy_terms ) ) { |
|
77 | - $terms = array_merge( $terms, $taxonomy_terms ); |
|
75 | + $taxonomy_terms = get_the_terms($post_id, $taxonomy->name); |
|
76 | + if (is_array($taxonomy_terms)) { |
|
77 | + $terms = array_merge($terms, $taxonomy_terms); |
|
78 | 78 | } |
79 | 79 | } |
80 | 80 | |
81 | 81 | // Convert everything to the Term Reference. |
82 | 82 | return array_filter( |
83 | 83 | array_map( |
84 | - function ( $term ) use ( $post_id ) { |
|
84 | + function($term) use ($post_id) { |
|
85 | 85 | /** |
86 | 86 | * @var WP_Term $term |
87 | 87 | */ |
88 | - if ( Wordpress_Term_Content_Legacy_Service::get_instance() |
|
89 | - ->get_entity_id( Wordpress_Content_Id::create_term( $term->term_id ) ) |
|
88 | + if (Wordpress_Term_Content_Legacy_Service::get_instance() |
|
89 | + ->get_entity_id(Wordpress_Content_Id::create_term($term->term_id)) |
|
90 | 90 | ) { |
91 | 91 | return new Relation( |
92 | - Wordpress_Content_Id::create_post( $post_id ), |
|
93 | - Wordpress_Content_Id::create_term( $term->term_id ), |
|
92 | + Wordpress_Content_Id::create_post($post_id), |
|
93 | + Wordpress_Content_Id::create_term($term->term_id), |
|
94 | 94 | WL_WHAT_RELATION |
95 | 95 | ); |
96 | 96 | } |
@@ -108,13 +108,13 @@ discard block |
||
108 | 108 | * |
109 | 109 | * @return array |
110 | 110 | */ |
111 | - private function get_term_mentions( $term_relations ) { |
|
111 | + private function get_term_mentions($term_relations) { |
|
112 | 112 | |
113 | 113 | return array_map( |
114 | - function ( $term_relation ) { |
|
114 | + function($term_relation) { |
|
115 | 115 | return array( |
116 | 116 | '@id' => Wordpress_Term_Content_Legacy_Service::get_instance() |
117 | - ->get_entity_id( $term_relation->get_object() ), |
|
117 | + ->get_entity_id($term_relation->get_object()), |
|
118 | 118 | ); |
119 | 119 | }, |
120 | 120 | $term_relations |