@@ -10,48 +10,48 @@ |
||
10 | 10 | namespace Wordlift\Entity; |
11 | 11 | |
12 | 12 | class Entity_Rest_Service { |
13 | - /** |
|
14 | - * @var \Wordlift_Entity_Type_Service |
|
15 | - */ |
|
16 | - private $entity_type_service; |
|
13 | + /** |
|
14 | + * @var \Wordlift_Entity_Type_Service |
|
15 | + */ |
|
16 | + private $entity_type_service; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Entity_Rest_Service constructor. |
|
20 | - * |
|
21 | - * @param $entity_type_service \Wordlift_Entity_Type_Service |
|
22 | - */ |
|
23 | - public function __construct( $entity_type_service ) { |
|
18 | + /** |
|
19 | + * Entity_Rest_Service constructor. |
|
20 | + * |
|
21 | + * @param $entity_type_service \Wordlift_Entity_Type_Service |
|
22 | + */ |
|
23 | + public function __construct( $entity_type_service ) { |
|
24 | 24 | |
25 | - $this->entity_type_service = $entity_type_service; |
|
25 | + $this->entity_type_service = $entity_type_service; |
|
26 | 26 | |
27 | - add_action( 'rest_insert_entity', array( $this, 'action_rest_insert_entity' ), 10, 3 ); |
|
27 | + add_action( 'rest_insert_entity', array( $this, 'action_rest_insert_entity' ), 10, 3 ); |
|
28 | 28 | |
29 | - } |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param $post \WP_Post |
|
33 | - * @param $request \WP_REST_Request |
|
34 | - * @param $creating bool |
|
35 | - */ |
|
36 | - public function action_rest_insert_entity( $post, $request, $creating ) { |
|
31 | + /** |
|
32 | + * @param $post \WP_Post |
|
33 | + * @param $request \WP_REST_Request |
|
34 | + * @param $creating bool |
|
35 | + */ |
|
36 | + public function action_rest_insert_entity( $post, $request, $creating ) { |
|
37 | 37 | |
38 | - // Set the type only on entity create. |
|
39 | - if ( ! $creating ) { |
|
40 | - return; |
|
41 | - } |
|
38 | + // Set the type only on entity create. |
|
39 | + if ( ! $creating ) { |
|
40 | + return; |
|
41 | + } |
|
42 | 42 | |
43 | - $entity_types = $request->get_param( 'wlEntityMainType' ); |
|
43 | + $entity_types = $request->get_param( 'wlEntityMainType' ); |
|
44 | 44 | |
45 | - if ( null === $entity_types ) { |
|
46 | - // Return early if entity types not set. |
|
47 | - return; |
|
48 | - } |
|
45 | + if ( null === $entity_types ) { |
|
46 | + // Return early if entity types not set. |
|
47 | + return; |
|
48 | + } |
|
49 | 49 | |
50 | - foreach ( $entity_types as $type_uri ) { |
|
51 | - // we don't replace since its entity creation and only one entity will be present. |
|
52 | - $this->entity_type_service->set( $post->ID, $type_uri ); |
|
53 | - } |
|
50 | + foreach ( $entity_types as $type_uri ) { |
|
51 | + // we don't replace since its entity creation and only one entity will be present. |
|
52 | + $this->entity_type_service->set( $post->ID, $type_uri ); |
|
53 | + } |
|
54 | 54 | |
55 | - } |
|
55 | + } |
|
56 | 56 | |
57 | 57 | } |
@@ -20,11 +20,11 @@ discard block |
||
20 | 20 | * |
21 | 21 | * @param $entity_type_service \Wordlift_Entity_Type_Service |
22 | 22 | */ |
23 | - public function __construct( $entity_type_service ) { |
|
23 | + public function __construct($entity_type_service) { |
|
24 | 24 | |
25 | 25 | $this->entity_type_service = $entity_type_service; |
26 | 26 | |
27 | - add_action( 'rest_insert_entity', array( $this, 'action_rest_insert_entity' ), 10, 3 ); |
|
27 | + add_action('rest_insert_entity', array($this, 'action_rest_insert_entity'), 10, 3); |
|
28 | 28 | |
29 | 29 | } |
30 | 30 | |
@@ -33,23 +33,23 @@ discard block |
||
33 | 33 | * @param $request \WP_REST_Request |
34 | 34 | * @param $creating bool |
35 | 35 | */ |
36 | - public function action_rest_insert_entity( $post, $request, $creating ) { |
|
36 | + public function action_rest_insert_entity($post, $request, $creating) { |
|
37 | 37 | |
38 | 38 | // Set the type only on entity create. |
39 | - if ( ! $creating ) { |
|
39 | + if ( ! $creating) { |
|
40 | 40 | return; |
41 | 41 | } |
42 | 42 | |
43 | - $entity_types = $request->get_param( 'wlEntityMainType' ); |
|
43 | + $entity_types = $request->get_param('wlEntityMainType'); |
|
44 | 44 | |
45 | - if ( null === $entity_types ) { |
|
45 | + if (null === $entity_types) { |
|
46 | 46 | // Return early if entity types not set. |
47 | 47 | return; |
48 | 48 | } |
49 | 49 | |
50 | - foreach ( $entity_types as $type_uri ) { |
|
50 | + foreach ($entity_types as $type_uri) { |
|
51 | 51 | // we don't replace since its entity creation and only one entity will be present. |
52 | - $this->entity_type_service->set( $post->ID, $type_uri ); |
|
52 | + $this->entity_type_service->set($post->ID, $type_uri); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | } |
@@ -8,46 +8,46 @@ |
||
8 | 8 | */ |
9 | 9 | class Entity_No_Index_Flag { |
10 | 10 | |
11 | - const YOAST_POST_NO_INDEX_FLAG = '_yoast_wpseo_meta-robots-noindex'; |
|
12 | - |
|
13 | - public function __construct() { |
|
14 | - |
|
15 | - $no_index_flag = self::YOAST_POST_NO_INDEX_FLAG; |
|
16 | - |
|
17 | - add_action( |
|
18 | - 'wp_insert_post', |
|
19 | - function ( $post_id, $post, $update ) use ( $no_index_flag ) { |
|
20 | - |
|
21 | - $post_type = get_post_type( $post_id ); |
|
22 | - |
|
23 | - if ( \Wordlift_Entity_Service::TYPE_NAME !== $post_type ) { |
|
24 | - // Don't set this flag for any other post types. |
|
25 | - return; |
|
26 | - } |
|
27 | - |
|
28 | - // We need to set this flag only on entity creation. |
|
29 | - if ( ! $update ) { |
|
30 | - update_post_meta( $post_id, $no_index_flag, 1 ); |
|
31 | - } |
|
32 | - |
|
33 | - }, |
|
34 | - PHP_INT_MAX, |
|
35 | - 3 |
|
36 | - ); |
|
37 | - |
|
38 | - add_action( |
|
39 | - 'post_updated', |
|
40 | - function ( $post_id ) use ( $no_index_flag ) { |
|
41 | - if ( get_post_type( $post_id ) !== \Wordlift_Entity_Service::TYPE_NAME ) { |
|
42 | - return; |
|
43 | - } |
|
44 | - // if the post is updated, remove this flag |
|
45 | - delete_post_meta( $post_id, $no_index_flag ); |
|
46 | - }, |
|
47 | - PHP_INT_MAX |
|
48 | - ); |
|
49 | - |
|
50 | - } |
|
11 | + const YOAST_POST_NO_INDEX_FLAG = '_yoast_wpseo_meta-robots-noindex'; |
|
12 | + |
|
13 | + public function __construct() { |
|
14 | + |
|
15 | + $no_index_flag = self::YOAST_POST_NO_INDEX_FLAG; |
|
16 | + |
|
17 | + add_action( |
|
18 | + 'wp_insert_post', |
|
19 | + function ( $post_id, $post, $update ) use ( $no_index_flag ) { |
|
20 | + |
|
21 | + $post_type = get_post_type( $post_id ); |
|
22 | + |
|
23 | + if ( \Wordlift_Entity_Service::TYPE_NAME !== $post_type ) { |
|
24 | + // Don't set this flag for any other post types. |
|
25 | + return; |
|
26 | + } |
|
27 | + |
|
28 | + // We need to set this flag only on entity creation. |
|
29 | + if ( ! $update ) { |
|
30 | + update_post_meta( $post_id, $no_index_flag, 1 ); |
|
31 | + } |
|
32 | + |
|
33 | + }, |
|
34 | + PHP_INT_MAX, |
|
35 | + 3 |
|
36 | + ); |
|
37 | + |
|
38 | + add_action( |
|
39 | + 'post_updated', |
|
40 | + function ( $post_id ) use ( $no_index_flag ) { |
|
41 | + if ( get_post_type( $post_id ) !== \Wordlift_Entity_Service::TYPE_NAME ) { |
|
42 | + return; |
|
43 | + } |
|
44 | + // if the post is updated, remove this flag |
|
45 | + delete_post_meta( $post_id, $no_index_flag ); |
|
46 | + }, |
|
47 | + PHP_INT_MAX |
|
48 | + ); |
|
49 | + |
|
50 | + } |
|
51 | 51 | |
52 | 52 | } |
53 | 53 |
@@ -16,18 +16,18 @@ discard block |
||
16 | 16 | |
17 | 17 | add_action( |
18 | 18 | 'wp_insert_post', |
19 | - function ( $post_id, $post, $update ) use ( $no_index_flag ) { |
|
19 | + function($post_id, $post, $update) use ($no_index_flag) { |
|
20 | 20 | |
21 | - $post_type = get_post_type( $post_id ); |
|
21 | + $post_type = get_post_type($post_id); |
|
22 | 22 | |
23 | - if ( \Wordlift_Entity_Service::TYPE_NAME !== $post_type ) { |
|
23 | + if (\Wordlift_Entity_Service::TYPE_NAME !== $post_type) { |
|
24 | 24 | // Don't set this flag for any other post types. |
25 | 25 | return; |
26 | 26 | } |
27 | 27 | |
28 | 28 | // We need to set this flag only on entity creation. |
29 | - if ( ! $update ) { |
|
30 | - update_post_meta( $post_id, $no_index_flag, 1 ); |
|
29 | + if ( ! $update) { |
|
30 | + update_post_meta($post_id, $no_index_flag, 1); |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | }, |
@@ -37,12 +37,12 @@ discard block |
||
37 | 37 | |
38 | 38 | add_action( |
39 | 39 | 'post_updated', |
40 | - function ( $post_id ) use ( $no_index_flag ) { |
|
41 | - if ( get_post_type( $post_id ) !== \Wordlift_Entity_Service::TYPE_NAME ) { |
|
40 | + function($post_id) use ($no_index_flag) { |
|
41 | + if (get_post_type($post_id) !== \Wordlift_Entity_Service::TYPE_NAME) { |
|
42 | 42 | return; |
43 | 43 | } |
44 | 44 | // if the post is updated, remove this flag |
45 | - delete_post_meta( $post_id, $no_index_flag ); |
|
45 | + delete_post_meta($post_id, $no_index_flag); |
|
46 | 46 | }, |
47 | 47 | PHP_INT_MAX |
48 | 48 | ); |
@@ -32,142 +32,142 @@ |
||
32 | 32 | */ |
33 | 33 | class Entity_Store { |
34 | 34 | |
35 | - protected function __construct() { |
|
36 | - } |
|
37 | - |
|
38 | - private static $instance = null; |
|
39 | - |
|
40 | - /** |
|
41 | - * Get the Entity_Store singleton, lazily initialized. |
|
42 | - * |
|
43 | - * @return Entity_Store The singleton. |
|
44 | - */ |
|
45 | - public static function get_instance() { |
|
46 | - if ( ! isset( self::$instance ) ) { |
|
47 | - self::$instance = new Entity_Store(); |
|
48 | - } |
|
49 | - |
|
50 | - return self::$instance; |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Create and persist an entity. |
|
55 | - * |
|
56 | - * @param array $params { |
|
57 | - * The entity parameters. |
|
58 | - * |
|
59 | - * @type string|array $labels A label, or an array of labels. The first label is set as post title. |
|
60 | - * @type string $description The entity description, stored in the post content. |
|
61 | - * @type string|array $same_as One or more entity URIs, stored in the sameAs post meta. |
|
62 | - * } |
|
63 | - * |
|
64 | - * @param string $post_status The post status, by default `draft`. |
|
65 | - * |
|
66 | - * @return int|\WP_Error |
|
67 | - * @throws \Exception when an error occurs. |
|
68 | - */ |
|
69 | - public function create( $params, $post_status = 'draft' ) { |
|
70 | - |
|
71 | - $args = wp_parse_args( |
|
72 | - $params, |
|
73 | - array( |
|
74 | - 'labels' => array(), |
|
75 | - 'description' => '', |
|
76 | - 'same_as' => array(), |
|
77 | - ) |
|
78 | - ); |
|
79 | - |
|
80 | - // Use the first label as `post_title`. |
|
81 | - $labels = (array) $args['labels']; |
|
82 | - $label = array_shift( $labels ); |
|
83 | - |
|
84 | - $post_id = wp_insert_post( |
|
85 | - array( |
|
86 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
87 | - 'post_status' => $post_status, |
|
88 | - 'post_title' => $label, |
|
89 | - 'post_name' => sanitize_title( $label ), |
|
90 | - 'post_content' => $args['description'], |
|
91 | - ) |
|
92 | - ); |
|
93 | - |
|
94 | - // Bail out if we've got an error. |
|
95 | - if ( empty( $post_id ) || is_wp_error( $post_id ) ) { |
|
96 | - throw new \Exception( 'An error occurred while creating an entity.' ); |
|
97 | - } |
|
98 | - |
|
99 | - Wordlift_Entity_Service::get_instance() |
|
100 | - ->set_alternative_labels( $post_id, array_diff( $labels, array( $label ) ) ); |
|
101 | - $this->merge_post_meta( |
|
102 | - $post_id, |
|
103 | - \Wordlift_Schema_Service::FIELD_SAME_AS, |
|
104 | - (array) $args['same_as'], |
|
105 | - (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
106 | - ); |
|
107 | - |
|
108 | - return $post_id; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Update an entity. |
|
113 | - * |
|
114 | - * @param array $params { |
|
115 | - * |
|
116 | - * @type int $ID The post ID. |
|
117 | - * @type string|array One or more labels to add to the synonyms. |
|
118 | - * @type string|array One or more URIs to add to the sameAs. |
|
119 | - * } |
|
120 | - * |
|
121 | - * @return int The post id. |
|
122 | - */ |
|
123 | - public function update( $params ) { |
|
124 | - |
|
125 | - $args = wp_parse_args( |
|
126 | - $params, |
|
127 | - array( |
|
128 | - 'ID' => 0, |
|
129 | - 'labels' => array(), |
|
130 | - 'same_as' => array(), |
|
131 | - ) |
|
132 | - ); |
|
133 | - |
|
134 | - $post_id = $args['ID']; |
|
135 | - |
|
136 | - // Save synonyms except title. |
|
137 | - Wordlift_Entity_Service::get_instance() |
|
138 | - ->append_alternative_labels( $post_id, array_diff( (array) $args['labels'], array( get_the_title( $post_id ) ) ) ); |
|
139 | - |
|
140 | - $this->merge_post_meta( |
|
141 | - $post_id, |
|
142 | - \Wordlift_Schema_Service::FIELD_SAME_AS, |
|
143 | - (array) $args['same_as'], |
|
144 | - (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
145 | - ); |
|
146 | - |
|
147 | - return $post_id; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Merge the post meta. |
|
152 | - * |
|
153 | - * @param int $post_id The post ID. |
|
154 | - * @param string $meta_key The post meta key. |
|
155 | - * @param string|array $values One or more values to add. |
|
156 | - * @param string|array $exclusions An additional list of values to exclude. |
|
157 | - */ |
|
158 | - private function merge_post_meta( $post_id, $meta_key, $values, $exclusions = array() ) { |
|
159 | - |
|
160 | - $existing = array_merge( |
|
161 | - (array) $exclusions, |
|
162 | - get_post_meta( $post_id, $meta_key ) |
|
163 | - ); |
|
164 | - |
|
165 | - foreach ( (array) $values as $value ) { |
|
166 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
|
167 | - if ( ! in_array( $value, $existing ) ) { |
|
168 | - add_post_meta( $post_id, $meta_key, $value ); |
|
169 | - } |
|
170 | - } |
|
171 | - } |
|
35 | + protected function __construct() { |
|
36 | + } |
|
37 | + |
|
38 | + private static $instance = null; |
|
39 | + |
|
40 | + /** |
|
41 | + * Get the Entity_Store singleton, lazily initialized. |
|
42 | + * |
|
43 | + * @return Entity_Store The singleton. |
|
44 | + */ |
|
45 | + public static function get_instance() { |
|
46 | + if ( ! isset( self::$instance ) ) { |
|
47 | + self::$instance = new Entity_Store(); |
|
48 | + } |
|
49 | + |
|
50 | + return self::$instance; |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Create and persist an entity. |
|
55 | + * |
|
56 | + * @param array $params { |
|
57 | + * The entity parameters. |
|
58 | + * |
|
59 | + * @type string|array $labels A label, or an array of labels. The first label is set as post title. |
|
60 | + * @type string $description The entity description, stored in the post content. |
|
61 | + * @type string|array $same_as One or more entity URIs, stored in the sameAs post meta. |
|
62 | + * } |
|
63 | + * |
|
64 | + * @param string $post_status The post status, by default `draft`. |
|
65 | + * |
|
66 | + * @return int|\WP_Error |
|
67 | + * @throws \Exception when an error occurs. |
|
68 | + */ |
|
69 | + public function create( $params, $post_status = 'draft' ) { |
|
70 | + |
|
71 | + $args = wp_parse_args( |
|
72 | + $params, |
|
73 | + array( |
|
74 | + 'labels' => array(), |
|
75 | + 'description' => '', |
|
76 | + 'same_as' => array(), |
|
77 | + ) |
|
78 | + ); |
|
79 | + |
|
80 | + // Use the first label as `post_title`. |
|
81 | + $labels = (array) $args['labels']; |
|
82 | + $label = array_shift( $labels ); |
|
83 | + |
|
84 | + $post_id = wp_insert_post( |
|
85 | + array( |
|
86 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
87 | + 'post_status' => $post_status, |
|
88 | + 'post_title' => $label, |
|
89 | + 'post_name' => sanitize_title( $label ), |
|
90 | + 'post_content' => $args['description'], |
|
91 | + ) |
|
92 | + ); |
|
93 | + |
|
94 | + // Bail out if we've got an error. |
|
95 | + if ( empty( $post_id ) || is_wp_error( $post_id ) ) { |
|
96 | + throw new \Exception( 'An error occurred while creating an entity.' ); |
|
97 | + } |
|
98 | + |
|
99 | + Wordlift_Entity_Service::get_instance() |
|
100 | + ->set_alternative_labels( $post_id, array_diff( $labels, array( $label ) ) ); |
|
101 | + $this->merge_post_meta( |
|
102 | + $post_id, |
|
103 | + \Wordlift_Schema_Service::FIELD_SAME_AS, |
|
104 | + (array) $args['same_as'], |
|
105 | + (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
106 | + ); |
|
107 | + |
|
108 | + return $post_id; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Update an entity. |
|
113 | + * |
|
114 | + * @param array $params { |
|
115 | + * |
|
116 | + * @type int $ID The post ID. |
|
117 | + * @type string|array One or more labels to add to the synonyms. |
|
118 | + * @type string|array One or more URIs to add to the sameAs. |
|
119 | + * } |
|
120 | + * |
|
121 | + * @return int The post id. |
|
122 | + */ |
|
123 | + public function update( $params ) { |
|
124 | + |
|
125 | + $args = wp_parse_args( |
|
126 | + $params, |
|
127 | + array( |
|
128 | + 'ID' => 0, |
|
129 | + 'labels' => array(), |
|
130 | + 'same_as' => array(), |
|
131 | + ) |
|
132 | + ); |
|
133 | + |
|
134 | + $post_id = $args['ID']; |
|
135 | + |
|
136 | + // Save synonyms except title. |
|
137 | + Wordlift_Entity_Service::get_instance() |
|
138 | + ->append_alternative_labels( $post_id, array_diff( (array) $args['labels'], array( get_the_title( $post_id ) ) ) ); |
|
139 | + |
|
140 | + $this->merge_post_meta( |
|
141 | + $post_id, |
|
142 | + \Wordlift_Schema_Service::FIELD_SAME_AS, |
|
143 | + (array) $args['same_as'], |
|
144 | + (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
145 | + ); |
|
146 | + |
|
147 | + return $post_id; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Merge the post meta. |
|
152 | + * |
|
153 | + * @param int $post_id The post ID. |
|
154 | + * @param string $meta_key The post meta key. |
|
155 | + * @param string|array $values One or more values to add. |
|
156 | + * @param string|array $exclusions An additional list of values to exclude. |
|
157 | + */ |
|
158 | + private function merge_post_meta( $post_id, $meta_key, $values, $exclusions = array() ) { |
|
159 | + |
|
160 | + $existing = array_merge( |
|
161 | + (array) $exclusions, |
|
162 | + get_post_meta( $post_id, $meta_key ) |
|
163 | + ); |
|
164 | + |
|
165 | + foreach ( (array) $values as $value ) { |
|
166 | + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
|
167 | + if ( ! in_array( $value, $existing ) ) { |
|
168 | + add_post_meta( $post_id, $meta_key, $value ); |
|
169 | + } |
|
170 | + } |
|
171 | + } |
|
172 | 172 | |
173 | 173 | } |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | * @return Entity_Store The singleton. |
44 | 44 | */ |
45 | 45 | public static function get_instance() { |
46 | - if ( ! isset( self::$instance ) ) { |
|
46 | + if ( ! isset(self::$instance)) { |
|
47 | 47 | self::$instance = new Entity_Store(); |
48 | 48 | } |
49 | 49 | |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | * @return int|\WP_Error |
67 | 67 | * @throws \Exception when an error occurs. |
68 | 68 | */ |
69 | - public function create( $params, $post_status = 'draft' ) { |
|
69 | + public function create($params, $post_status = 'draft') { |
|
70 | 70 | |
71 | 71 | $args = wp_parse_args( |
72 | 72 | $params, |
@@ -79,30 +79,30 @@ discard block |
||
79 | 79 | |
80 | 80 | // Use the first label as `post_title`. |
81 | 81 | $labels = (array) $args['labels']; |
82 | - $label = array_shift( $labels ); |
|
82 | + $label = array_shift($labels); |
|
83 | 83 | |
84 | 84 | $post_id = wp_insert_post( |
85 | 85 | array( |
86 | 86 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
87 | 87 | 'post_status' => $post_status, |
88 | 88 | 'post_title' => $label, |
89 | - 'post_name' => sanitize_title( $label ), |
|
89 | + 'post_name' => sanitize_title($label), |
|
90 | 90 | 'post_content' => $args['description'], |
91 | 91 | ) |
92 | 92 | ); |
93 | 93 | |
94 | 94 | // Bail out if we've got an error. |
95 | - if ( empty( $post_id ) || is_wp_error( $post_id ) ) { |
|
96 | - throw new \Exception( 'An error occurred while creating an entity.' ); |
|
95 | + if (empty($post_id) || is_wp_error($post_id)) { |
|
96 | + throw new \Exception('An error occurred while creating an entity.'); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | Wordlift_Entity_Service::get_instance() |
100 | - ->set_alternative_labels( $post_id, array_diff( $labels, array( $label ) ) ); |
|
100 | + ->set_alternative_labels($post_id, array_diff($labels, array($label))); |
|
101 | 101 | $this->merge_post_meta( |
102 | 102 | $post_id, |
103 | 103 | \Wordlift_Schema_Service::FIELD_SAME_AS, |
104 | 104 | (array) $args['same_as'], |
105 | - (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
105 | + (array) Wordpress_Content_Service::get_instance()->get_entity_id(Wordpress_Content_Id::create_post($post_id)) |
|
106 | 106 | ); |
107 | 107 | |
108 | 108 | return $post_id; |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | * |
121 | 121 | * @return int The post id. |
122 | 122 | */ |
123 | - public function update( $params ) { |
|
123 | + public function update($params) { |
|
124 | 124 | |
125 | 125 | $args = wp_parse_args( |
126 | 126 | $params, |
@@ -135,13 +135,13 @@ discard block |
||
135 | 135 | |
136 | 136 | // Save synonyms except title. |
137 | 137 | Wordlift_Entity_Service::get_instance() |
138 | - ->append_alternative_labels( $post_id, array_diff( (array) $args['labels'], array( get_the_title( $post_id ) ) ) ); |
|
138 | + ->append_alternative_labels($post_id, array_diff((array) $args['labels'], array(get_the_title($post_id)))); |
|
139 | 139 | |
140 | 140 | $this->merge_post_meta( |
141 | 141 | $post_id, |
142 | 142 | \Wordlift_Schema_Service::FIELD_SAME_AS, |
143 | 143 | (array) $args['same_as'], |
144 | - (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
144 | + (array) Wordpress_Content_Service::get_instance()->get_entity_id(Wordpress_Content_Id::create_post($post_id)) |
|
145 | 145 | ); |
146 | 146 | |
147 | 147 | return $post_id; |
@@ -155,17 +155,17 @@ discard block |
||
155 | 155 | * @param string|array $values One or more values to add. |
156 | 156 | * @param string|array $exclusions An additional list of values to exclude. |
157 | 157 | */ |
158 | - private function merge_post_meta( $post_id, $meta_key, $values, $exclusions = array() ) { |
|
158 | + private function merge_post_meta($post_id, $meta_key, $values, $exclusions = array()) { |
|
159 | 159 | |
160 | 160 | $existing = array_merge( |
161 | 161 | (array) $exclusions, |
162 | - get_post_meta( $post_id, $meta_key ) |
|
162 | + get_post_meta($post_id, $meta_key) |
|
163 | 163 | ); |
164 | 164 | |
165 | - foreach ( (array) $values as $value ) { |
|
165 | + foreach ((array) $values as $value) { |
|
166 | 166 | // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
167 | - if ( ! in_array( $value, $existing ) ) { |
|
168 | - add_post_meta( $post_id, $meta_key, $value ); |
|
167 | + if ( ! in_array($value, $existing)) { |
|
168 | + add_post_meta($post_id, $meta_key, $value); |
|
169 | 169 | } |
170 | 170 | } |
171 | 171 | } |
@@ -6,10 +6,10 @@ |
||
6 | 6 | |
7 | 7 | interface Remote_Entity_Importer { |
8 | 8 | |
9 | - /** |
|
10 | - * @return Wordpress_Content_Id|boolean |
|
11 | - * Returns content id or false. |
|
12 | - */ |
|
13 | - public function import(); |
|
9 | + /** |
|
10 | + * @return Wordpress_Content_Id|boolean |
|
11 | + * Returns content id or false. |
|
12 | + */ |
|
13 | + public function import(); |
|
14 | 14 | |
15 | 15 | } |
@@ -7,9 +7,9 @@ |
||
7 | 7 | */ |
8 | 8 | class Invalid_Remote_Entity_Importer implements Remote_Entity_Importer { |
9 | 9 | |
10 | - public function import() { |
|
11 | - // Cant import invalid entities. |
|
12 | - return false; |
|
13 | - } |
|
10 | + public function import() { |
|
11 | + // Cant import invalid entities. |
|
12 | + return false; |
|
13 | + } |
|
14 | 14 | |
15 | 15 | } |
@@ -8,39 +8,39 @@ |
||
8 | 8 | |
9 | 9 | class Valid_Remote_Entity_Importer implements Remote_Entity_Importer { |
10 | 10 | |
11 | - /** |
|
12 | - * @var Valid_Remote_Entity |
|
13 | - */ |
|
14 | - private $entity; |
|
15 | - |
|
16 | - /** |
|
17 | - * @param $entity Valid_Remote_Entity |
|
18 | - */ |
|
19 | - public function __construct( $entity ) { |
|
20 | - $this->entity = $entity; |
|
21 | - } |
|
22 | - |
|
23 | - public function import() { |
|
24 | - |
|
25 | - $entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
|
26 | - |
|
27 | - $post_id = wp_insert_post( |
|
28 | - array( |
|
29 | - 'post_title' => $this->entity->get_name(), |
|
30 | - 'post_content' => $this->entity->get_description(), |
|
31 | - 'post_status' => 'draft', |
|
32 | - 'post_type' => 'entity', |
|
33 | - ) |
|
34 | - ); |
|
35 | - |
|
36 | - foreach ( $this->entity->get_types() as $type ) { |
|
37 | - $entity_type_service->set( $post_id, "http://schema.org/$type", false ); |
|
38 | - } |
|
39 | - |
|
40 | - foreach ( $this->entity->get_same_as() as $same_as ) { |
|
41 | - add_post_meta( $post_id, 'entity_same_as', $same_as ); |
|
42 | - } |
|
43 | - |
|
44 | - return Wordpress_Content_Id::create_post( $post_id ); |
|
45 | - } |
|
11 | + /** |
|
12 | + * @var Valid_Remote_Entity |
|
13 | + */ |
|
14 | + private $entity; |
|
15 | + |
|
16 | + /** |
|
17 | + * @param $entity Valid_Remote_Entity |
|
18 | + */ |
|
19 | + public function __construct( $entity ) { |
|
20 | + $this->entity = $entity; |
|
21 | + } |
|
22 | + |
|
23 | + public function import() { |
|
24 | + |
|
25 | + $entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
|
26 | + |
|
27 | + $post_id = wp_insert_post( |
|
28 | + array( |
|
29 | + 'post_title' => $this->entity->get_name(), |
|
30 | + 'post_content' => $this->entity->get_description(), |
|
31 | + 'post_status' => 'draft', |
|
32 | + 'post_type' => 'entity', |
|
33 | + ) |
|
34 | + ); |
|
35 | + |
|
36 | + foreach ( $this->entity->get_types() as $type ) { |
|
37 | + $entity_type_service->set( $post_id, "http://schema.org/$type", false ); |
|
38 | + } |
|
39 | + |
|
40 | + foreach ( $this->entity->get_same_as() as $same_as ) { |
|
41 | + add_post_meta( $post_id, 'entity_same_as', $same_as ); |
|
42 | + } |
|
43 | + |
|
44 | + return Wordpress_Content_Id::create_post( $post_id ); |
|
45 | + } |
|
46 | 46 | } |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | /** |
17 | 17 | * @param $entity Valid_Remote_Entity |
18 | 18 | */ |
19 | - public function __construct( $entity ) { |
|
19 | + public function __construct($entity) { |
|
20 | 20 | $this->entity = $entity; |
21 | 21 | } |
22 | 22 | |
@@ -33,14 +33,14 @@ discard block |
||
33 | 33 | ) |
34 | 34 | ); |
35 | 35 | |
36 | - foreach ( $this->entity->get_types() as $type ) { |
|
37 | - $entity_type_service->set( $post_id, "http://schema.org/$type", false ); |
|
36 | + foreach ($this->entity->get_types() as $type) { |
|
37 | + $entity_type_service->set($post_id, "http://schema.org/$type", false); |
|
38 | 38 | } |
39 | 39 | |
40 | - foreach ( $this->entity->get_same_as() as $same_as ) { |
|
41 | - add_post_meta( $post_id, 'entity_same_as', $same_as ); |
|
40 | + foreach ($this->entity->get_same_as() as $same_as) { |
|
41 | + add_post_meta($post_id, 'entity_same_as', $same_as); |
|
42 | 42 | } |
43 | 43 | |
44 | - return Wordpress_Content_Id::create_post( $post_id ); |
|
44 | + return Wordpress_Content_Id::create_post($post_id); |
|
45 | 45 | } |
46 | 46 | } |
@@ -6,16 +6,16 @@ |
||
6 | 6 | |
7 | 7 | class Url_To_Remote_Entity_Converter { |
8 | 8 | |
9 | - /** |
|
10 | - * @param $url |
|
11 | - * |
|
12 | - * @return Remote_Entity |
|
13 | - */ |
|
14 | - public static function convert( $url ) { |
|
15 | - $target_path = '/id/' . preg_replace( '@^(https?)://@', '$1/', $url ); |
|
16 | - $response = Default_Api_Service::get_instance()->get( $target_path ); |
|
9 | + /** |
|
10 | + * @param $url |
|
11 | + * |
|
12 | + * @return Remote_Entity |
|
13 | + */ |
|
14 | + public static function convert( $url ) { |
|
15 | + $target_path = '/id/' . preg_replace( '@^(https?)://@', '$1/', $url ); |
|
16 | + $response = Default_Api_Service::get_instance()->get( $target_path ); |
|
17 | 17 | |
18 | - return Remote_Entity_Factory::from_response( $url, $response ); |
|
19 | - } |
|
18 | + return Remote_Entity_Factory::from_response( $url, $response ); |
|
19 | + } |
|
20 | 20 | |
21 | 21 | } |
@@ -11,11 +11,11 @@ |
||
11 | 11 | * |
12 | 12 | * @return Remote_Entity |
13 | 13 | */ |
14 | - public static function convert( $url ) { |
|
15 | - $target_path = '/id/' . preg_replace( '@^(https?)://@', '$1/', $url ); |
|
16 | - $response = Default_Api_Service::get_instance()->get( $target_path ); |
|
14 | + public static function convert($url) { |
|
15 | + $target_path = '/id/'.preg_replace('@^(https?)://@', '$1/', $url); |
|
16 | + $response = Default_Api_Service::get_instance()->get($target_path); |
|
17 | 17 | |
18 | - return Remote_Entity_Factory::from_response( $url, $response ); |
|
18 | + return Remote_Entity_Factory::from_response($url, $response); |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | } |
@@ -4,19 +4,19 @@ |
||
4 | 4 | |
5 | 5 | class Invalid_Remote_Entity implements Remote_Entity { |
6 | 6 | |
7 | - public function get_name() { |
|
8 | - // TODO: Implement getName() method. |
|
9 | - } |
|
7 | + public function get_name() { |
|
8 | + // TODO: Implement getName() method. |
|
9 | + } |
|
10 | 10 | |
11 | - public function get_description() { |
|
12 | - // TODO: Implement getDescription() method. |
|
13 | - } |
|
11 | + public function get_description() { |
|
12 | + // TODO: Implement getDescription() method. |
|
13 | + } |
|
14 | 14 | |
15 | - public function get_same_as() { |
|
16 | - // TODO: Implement getSameAs() method. |
|
17 | - } |
|
15 | + public function get_same_as() { |
|
16 | + // TODO: Implement getSameAs() method. |
|
17 | + } |
|
18 | 18 | |
19 | - public function get_types() { |
|
20 | - // TODO: Implement getTypes() method. |
|
21 | - } |
|
19 | + public function get_types() { |
|
20 | + // TODO: Implement getTypes() method. |
|
21 | + } |
|
22 | 22 | } |
@@ -4,47 +4,47 @@ |
||
4 | 4 | |
5 | 5 | class Remote_Entity_Factory { |
6 | 6 | |
7 | - /** |
|
8 | - * |
|
9 | - * @param $entity_url string |
|
10 | - * @param $response \Wordlift\Api\Response |
|
11 | - * |
|
12 | - * @return Remote_Entity |
|
13 | - */ |
|
14 | - public static function from_response( $entity_url, $response ) { |
|
15 | - |
|
16 | - if ( ! $response->is_success() ) { |
|
17 | - return new Invalid_Remote_Entity(); |
|
18 | - } |
|
19 | - |
|
20 | - $entity_data = json_decode( $response->get_body(), true ); |
|
21 | - |
|
22 | - if ( ! $entity_data ) { |
|
23 | - return new Invalid_Remote_Entity(); |
|
24 | - } |
|
25 | - |
|
26 | - if ( ! array_key_exists( '@type', $entity_data ) |
|
27 | - || ! array_key_exists( 'name', $entity_data ) |
|
28 | - || ! array_key_exists( 'description', $entity_data ) ) { |
|
29 | - |
|
30 | - return new Invalid_Remote_Entity(); |
|
31 | - } |
|
32 | - |
|
33 | - return new Valid_Remote_Entity( |
|
34 | - self::may_be_wrap_array( $entity_data['@type'] ), |
|
35 | - $entity_data['name'], |
|
36 | - $entity_data['description'], |
|
37 | - array_merge( array( $entity_url ), self::may_be_wrap_array( $entity_data['sameAs'] ) ) |
|
38 | - ); |
|
39 | - |
|
40 | - } |
|
41 | - |
|
42 | - private static function may_be_wrap_array( $el ) { |
|
43 | - if ( is_array( $el ) ) { |
|
44 | - return $el; |
|
45 | - } |
|
46 | - |
|
47 | - return array( $el ); |
|
48 | - } |
|
7 | + /** |
|
8 | + * |
|
9 | + * @param $entity_url string |
|
10 | + * @param $response \Wordlift\Api\Response |
|
11 | + * |
|
12 | + * @return Remote_Entity |
|
13 | + */ |
|
14 | + public static function from_response( $entity_url, $response ) { |
|
15 | + |
|
16 | + if ( ! $response->is_success() ) { |
|
17 | + return new Invalid_Remote_Entity(); |
|
18 | + } |
|
19 | + |
|
20 | + $entity_data = json_decode( $response->get_body(), true ); |
|
21 | + |
|
22 | + if ( ! $entity_data ) { |
|
23 | + return new Invalid_Remote_Entity(); |
|
24 | + } |
|
25 | + |
|
26 | + if ( ! array_key_exists( '@type', $entity_data ) |
|
27 | + || ! array_key_exists( 'name', $entity_data ) |
|
28 | + || ! array_key_exists( 'description', $entity_data ) ) { |
|
29 | + |
|
30 | + return new Invalid_Remote_Entity(); |
|
31 | + } |
|
32 | + |
|
33 | + return new Valid_Remote_Entity( |
|
34 | + self::may_be_wrap_array( $entity_data['@type'] ), |
|
35 | + $entity_data['name'], |
|
36 | + $entity_data['description'], |
|
37 | + array_merge( array( $entity_url ), self::may_be_wrap_array( $entity_data['sameAs'] ) ) |
|
38 | + ); |
|
39 | + |
|
40 | + } |
|
41 | + |
|
42 | + private static function may_be_wrap_array( $el ) { |
|
43 | + if ( is_array( $el ) ) { |
|
44 | + return $el; |
|
45 | + } |
|
46 | + |
|
47 | + return array( $el ); |
|
48 | + } |
|
49 | 49 | |
50 | 50 | } |
@@ -11,40 +11,40 @@ |
||
11 | 11 | * |
12 | 12 | * @return Remote_Entity |
13 | 13 | */ |
14 | - public static function from_response( $entity_url, $response ) { |
|
14 | + public static function from_response($entity_url, $response) { |
|
15 | 15 | |
16 | - if ( ! $response->is_success() ) { |
|
16 | + if ( ! $response->is_success()) { |
|
17 | 17 | return new Invalid_Remote_Entity(); |
18 | 18 | } |
19 | 19 | |
20 | - $entity_data = json_decode( $response->get_body(), true ); |
|
20 | + $entity_data = json_decode($response->get_body(), true); |
|
21 | 21 | |
22 | - if ( ! $entity_data ) { |
|
22 | + if ( ! $entity_data) { |
|
23 | 23 | return new Invalid_Remote_Entity(); |
24 | 24 | } |
25 | 25 | |
26 | - if ( ! array_key_exists( '@type', $entity_data ) |
|
27 | - || ! array_key_exists( 'name', $entity_data ) |
|
28 | - || ! array_key_exists( 'description', $entity_data ) ) { |
|
26 | + if ( ! array_key_exists('@type', $entity_data) |
|
27 | + || ! array_key_exists('name', $entity_data) |
|
28 | + || ! array_key_exists('description', $entity_data)) { |
|
29 | 29 | |
30 | 30 | return new Invalid_Remote_Entity(); |
31 | 31 | } |
32 | 32 | |
33 | 33 | return new Valid_Remote_Entity( |
34 | - self::may_be_wrap_array( $entity_data['@type'] ), |
|
34 | + self::may_be_wrap_array($entity_data['@type']), |
|
35 | 35 | $entity_data['name'], |
36 | 36 | $entity_data['description'], |
37 | - array_merge( array( $entity_url ), self::may_be_wrap_array( $entity_data['sameAs'] ) ) |
|
37 | + array_merge(array($entity_url), self::may_be_wrap_array($entity_data['sameAs'])) |
|
38 | 38 | ); |
39 | 39 | |
40 | 40 | } |
41 | 41 | |
42 | - private static function may_be_wrap_array( $el ) { |
|
43 | - if ( is_array( $el ) ) { |
|
42 | + private static function may_be_wrap_array($el) { |
|
43 | + if (is_array($el)) { |
|
44 | 44 | return $el; |
45 | 45 | } |
46 | 46 | |
47 | - return array( $el ); |
|
47 | + return array($el); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | } |