@@ -17,233 +17,233 @@ |
||
17 | 17 | * @package Wordlift\FAQ |
18 | 18 | */ |
19 | 19 | class Faq_Rest_Controller { |
20 | - const FAQ_META_KEY = '_wl_faq'; |
|
21 | - /** |
|
22 | - * Enumerations to determine the field to be deleted on |
|
23 | - * the DELETE request in API. |
|
24 | - */ |
|
25 | - const QUESTION = 'question'; |
|
26 | - const ANSWER = 'answer'; |
|
27 | - |
|
28 | - public static function register_routes() { |
|
29 | - add_action( 'rest_api_init', 'Wordlift\FAQ\FAQ_Rest_Controller::register_route_callback' ); |
|
30 | - } |
|
31 | - |
|
32 | - public static function register_route_callback() { |
|
33 | - /** |
|
34 | - * Specifies the list of arguments to be present for update request. |
|
35 | - */ |
|
36 | - $post_id_validation_settings = array( |
|
37 | - 'required' => true, |
|
38 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
39 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
40 | - return is_numeric( $param ); |
|
41 | - }, |
|
42 | - ); |
|
43 | - $faq_items_validation_settings = array( |
|
44 | - 'required' => true, |
|
45 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
46 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
47 | - return is_array( $param ) && count( $param ) > 0; |
|
48 | - }, |
|
49 | - ); |
|
50 | - /** |
|
51 | - * Array of args to be present in order to |
|
52 | - * make create, update or delete request. |
|
53 | - */ |
|
54 | - $create_or_update_or_delete_args = array( |
|
55 | - 'post_id' => $post_id_validation_settings, |
|
56 | - 'faq_items' => $faq_items_validation_settings, |
|
57 | - ); |
|
58 | - |
|
59 | - /** |
|
60 | - * Rest route for creating new faq item. |
|
61 | - */ |
|
62 | - register_rest_route( |
|
63 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
64 | - '/faq', |
|
65 | - array( |
|
66 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
67 | - 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::add_faq_items', |
|
68 | - 'permission_callback' => function () { |
|
69 | - return current_user_can( 'publish_posts' ); |
|
70 | - }, |
|
71 | - 'args' => $create_or_update_or_delete_args, |
|
72 | - ) |
|
73 | - ); |
|
74 | - /** |
|
75 | - * Rest route for updating faq items. |
|
76 | - */ |
|
77 | - register_rest_route( |
|
78 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
79 | - '/faq', |
|
80 | - array( |
|
81 | - 'methods' => \WP_REST_Server::EDITABLE, |
|
82 | - 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::update_faq_items', |
|
83 | - 'permission_callback' => function () { |
|
84 | - return current_user_can( 'publish_posts' ); |
|
85 | - }, |
|
86 | - 'args' => $create_or_update_or_delete_args, |
|
87 | - ) |
|
88 | - ); |
|
89 | - /** |
|
90 | - * Rest route for getting the faq items. |
|
91 | - */ |
|
92 | - register_rest_route( |
|
93 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
94 | - '/faq/(?P<post_id>\d+)', |
|
95 | - array( |
|
96 | - 'methods' => \WP_REST_Server::READABLE, |
|
97 | - 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::get_faq_items', |
|
98 | - 'permission_callback' => function () { |
|
99 | - return current_user_can( 'publish_posts' ); |
|
100 | - }, |
|
101 | - 'args' => array( 'post_id' => $post_id_validation_settings ), |
|
102 | - ) |
|
103 | - ); |
|
104 | - /** |
|
105 | - * Rest route for deleting faq item. |
|
106 | - */ |
|
107 | - register_rest_route( |
|
108 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
109 | - '/faq', |
|
110 | - array( |
|
111 | - 'methods' => \WP_REST_Server::DELETABLE, |
|
112 | - 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::delete_faq_items', |
|
113 | - 'permission_callback' => function () { |
|
114 | - return current_user_can( 'publish_posts' ); |
|
115 | - }, |
|
116 | - 'args' => $create_or_update_or_delete_args, |
|
117 | - ) |
|
118 | - ); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Get all FAQ items for a post id. |
|
123 | - * |
|
124 | - * @param $request $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
125 | - * |
|
126 | - * @return array Result array, if post id is not given then error array is returned. |
|
127 | - */ |
|
128 | - public static function get_faq_items( $request ) { |
|
129 | - $data = $request->get_params(); |
|
130 | - $post_id = (int) $data['post_id']; |
|
131 | - |
|
132 | - return get_post_meta( $post_id, self::FAQ_META_KEY ); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Update all FAQ items for a post id. |
|
137 | - * |
|
138 | - * @param $request $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
139 | - * |
|
140 | - * @return array Result array, if post id is not given then error array is returned. |
|
141 | - */ |
|
142 | - public static function update_faq_items( $request ) { |
|
143 | - $data = $request->get_params(); |
|
144 | - $post_id = (int) $data['post_id']; |
|
145 | - $faq_items = (array) $data['faq_items']; |
|
146 | - foreach ( $faq_items as $faq_item ) { |
|
147 | - $previous_value = array( |
|
148 | - 'question' => (string) $faq_item['previous_question_value'], |
|
149 | - 'answer' => (string) $faq_item['previous_answer_value'], |
|
150 | - 'id' => (int) $faq_item['id'], |
|
151 | - ); |
|
152 | - $new_value = array( |
|
153 | - 'question' => (string) $faq_item['question'], |
|
154 | - 'answer' => (string) $faq_item['answer'], |
|
155 | - 'id' => (int) $faq_item['id'], |
|
156 | - ); |
|
157 | - update_post_meta( $post_id, self::FAQ_META_KEY, $new_value, $previous_value ); |
|
158 | - } |
|
159 | - |
|
160 | - return array( |
|
161 | - 'status' => 'success', |
|
162 | - 'message' => __( 'Faq Items updated successfully', 'wordlift' ), |
|
163 | - ); |
|
164 | - |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Delete Faq items. |
|
169 | - * |
|
170 | - * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
171 | - * |
|
172 | - * @return array Associative array whether the faq item is deleted or not. |
|
173 | - */ |
|
174 | - public static function delete_faq_items( $request ) { |
|
175 | - $post_data = $request->get_params(); |
|
176 | - $post_id = $post_data['post_id']; |
|
177 | - $faq_items = $post_data['faq_items']; |
|
178 | - foreach ( $faq_items as $faq_item ) { |
|
179 | - /** |
|
180 | - * Note: the order of keys is important in order to delete it properly |
|
181 | - * If the order change, delete operation will fail since it is converted |
|
182 | - * in to a string when it was stored. |
|
183 | - * we cant rely on client to post it in correct order, so we create an array |
|
184 | - * in correct order. |
|
185 | - */ |
|
186 | - $deleted_faq_item = array( |
|
187 | - 'question' => $faq_item['question'], |
|
188 | - 'answer' => $faq_item['answer'], |
|
189 | - 'id' => (int) $faq_item['id'], |
|
190 | - ); |
|
191 | - /** |
|
192 | - * If the field to be deleted is answer, then don't delete the faq item, |
|
193 | - * if it is question then delete the faq item. |
|
194 | - */ |
|
195 | - if ( self::ANSWER === $faq_item['field_to_be_deleted'] ) { |
|
196 | - $previous_value = $deleted_faq_item; |
|
197 | - // then don't delete the faq item, set the answer as empty. |
|
198 | - $deleted_faq_item['answer'] = ''; |
|
199 | - $new_value = $deleted_faq_item; |
|
200 | - update_post_meta( $post_id, self::FAQ_META_KEY, $new_value, $previous_value ); |
|
201 | - } elseif ( self::QUESTION === $faq_item['field_to_be_deleted'] ) { |
|
202 | - /** |
|
203 | - * If the question is deleted, then delete the faq item. |
|
204 | - */ |
|
205 | - delete_post_meta( $post_id, self::FAQ_META_KEY, $deleted_faq_item ); |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * We are returning only the first item id, since the user can select only one text at a time. |
|
211 | - */ |
|
212 | - return array( |
|
213 | - 'status' => 'success', |
|
214 | - 'message' => __( 'Faq item successfully deleted.', 'wordlift' ), |
|
215 | - ); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Insert or update FAQ items. |
|
220 | - * |
|
221 | - * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
222 | - * |
|
223 | - * @return array Associative array whether the faq item is inserted or not |
|
224 | - */ |
|
225 | - public static function add_faq_items( $request ) { |
|
226 | - $post_data = $request->get_params(); |
|
227 | - $post_id = $post_data['post_id']; |
|
228 | - $faq_items = $post_data['faq_items']; |
|
229 | - foreach ( $faq_items as &$faq_item ) { |
|
230 | - // Add an identifier id to the faq item, it helps to prevent duplication problem. |
|
231 | - /** |
|
232 | - * We are using time() and a random integer to prevent |
|
233 | - * duplication problem. |
|
234 | - */ |
|
235 | - $faq_item['id'] = time() + wp_rand( 1, 100 ) + wp_rand( 1, 100 ); |
|
236 | - add_post_meta( (int) $post_id, self::FAQ_META_KEY, $faq_item ); |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * We are returning only the first item id, since the user can select only one text at a time. |
|
241 | - */ |
|
242 | - return array( |
|
243 | - 'status' => 'success', |
|
244 | - 'message' => __( 'Question successfully added.', 'wordlift' ), |
|
245 | - 'id' => (int) $faq_items[0]['id'], |
|
246 | - ); |
|
247 | - |
|
248 | - } |
|
20 | + const FAQ_META_KEY = '_wl_faq'; |
|
21 | + /** |
|
22 | + * Enumerations to determine the field to be deleted on |
|
23 | + * the DELETE request in API. |
|
24 | + */ |
|
25 | + const QUESTION = 'question'; |
|
26 | + const ANSWER = 'answer'; |
|
27 | + |
|
28 | + public static function register_routes() { |
|
29 | + add_action( 'rest_api_init', 'Wordlift\FAQ\FAQ_Rest_Controller::register_route_callback' ); |
|
30 | + } |
|
31 | + |
|
32 | + public static function register_route_callback() { |
|
33 | + /** |
|
34 | + * Specifies the list of arguments to be present for update request. |
|
35 | + */ |
|
36 | + $post_id_validation_settings = array( |
|
37 | + 'required' => true, |
|
38 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
39 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
40 | + return is_numeric( $param ); |
|
41 | + }, |
|
42 | + ); |
|
43 | + $faq_items_validation_settings = array( |
|
44 | + 'required' => true, |
|
45 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
46 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
47 | + return is_array( $param ) && count( $param ) > 0; |
|
48 | + }, |
|
49 | + ); |
|
50 | + /** |
|
51 | + * Array of args to be present in order to |
|
52 | + * make create, update or delete request. |
|
53 | + */ |
|
54 | + $create_or_update_or_delete_args = array( |
|
55 | + 'post_id' => $post_id_validation_settings, |
|
56 | + 'faq_items' => $faq_items_validation_settings, |
|
57 | + ); |
|
58 | + |
|
59 | + /** |
|
60 | + * Rest route for creating new faq item. |
|
61 | + */ |
|
62 | + register_rest_route( |
|
63 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
64 | + '/faq', |
|
65 | + array( |
|
66 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
67 | + 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::add_faq_items', |
|
68 | + 'permission_callback' => function () { |
|
69 | + return current_user_can( 'publish_posts' ); |
|
70 | + }, |
|
71 | + 'args' => $create_or_update_or_delete_args, |
|
72 | + ) |
|
73 | + ); |
|
74 | + /** |
|
75 | + * Rest route for updating faq items. |
|
76 | + */ |
|
77 | + register_rest_route( |
|
78 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
79 | + '/faq', |
|
80 | + array( |
|
81 | + 'methods' => \WP_REST_Server::EDITABLE, |
|
82 | + 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::update_faq_items', |
|
83 | + 'permission_callback' => function () { |
|
84 | + return current_user_can( 'publish_posts' ); |
|
85 | + }, |
|
86 | + 'args' => $create_or_update_or_delete_args, |
|
87 | + ) |
|
88 | + ); |
|
89 | + /** |
|
90 | + * Rest route for getting the faq items. |
|
91 | + */ |
|
92 | + register_rest_route( |
|
93 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
94 | + '/faq/(?P<post_id>\d+)', |
|
95 | + array( |
|
96 | + 'methods' => \WP_REST_Server::READABLE, |
|
97 | + 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::get_faq_items', |
|
98 | + 'permission_callback' => function () { |
|
99 | + return current_user_can( 'publish_posts' ); |
|
100 | + }, |
|
101 | + 'args' => array( 'post_id' => $post_id_validation_settings ), |
|
102 | + ) |
|
103 | + ); |
|
104 | + /** |
|
105 | + * Rest route for deleting faq item. |
|
106 | + */ |
|
107 | + register_rest_route( |
|
108 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
109 | + '/faq', |
|
110 | + array( |
|
111 | + 'methods' => \WP_REST_Server::DELETABLE, |
|
112 | + 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::delete_faq_items', |
|
113 | + 'permission_callback' => function () { |
|
114 | + return current_user_can( 'publish_posts' ); |
|
115 | + }, |
|
116 | + 'args' => $create_or_update_or_delete_args, |
|
117 | + ) |
|
118 | + ); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Get all FAQ items for a post id. |
|
123 | + * |
|
124 | + * @param $request $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
125 | + * |
|
126 | + * @return array Result array, if post id is not given then error array is returned. |
|
127 | + */ |
|
128 | + public static function get_faq_items( $request ) { |
|
129 | + $data = $request->get_params(); |
|
130 | + $post_id = (int) $data['post_id']; |
|
131 | + |
|
132 | + return get_post_meta( $post_id, self::FAQ_META_KEY ); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Update all FAQ items for a post id. |
|
137 | + * |
|
138 | + * @param $request $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
139 | + * |
|
140 | + * @return array Result array, if post id is not given then error array is returned. |
|
141 | + */ |
|
142 | + public static function update_faq_items( $request ) { |
|
143 | + $data = $request->get_params(); |
|
144 | + $post_id = (int) $data['post_id']; |
|
145 | + $faq_items = (array) $data['faq_items']; |
|
146 | + foreach ( $faq_items as $faq_item ) { |
|
147 | + $previous_value = array( |
|
148 | + 'question' => (string) $faq_item['previous_question_value'], |
|
149 | + 'answer' => (string) $faq_item['previous_answer_value'], |
|
150 | + 'id' => (int) $faq_item['id'], |
|
151 | + ); |
|
152 | + $new_value = array( |
|
153 | + 'question' => (string) $faq_item['question'], |
|
154 | + 'answer' => (string) $faq_item['answer'], |
|
155 | + 'id' => (int) $faq_item['id'], |
|
156 | + ); |
|
157 | + update_post_meta( $post_id, self::FAQ_META_KEY, $new_value, $previous_value ); |
|
158 | + } |
|
159 | + |
|
160 | + return array( |
|
161 | + 'status' => 'success', |
|
162 | + 'message' => __( 'Faq Items updated successfully', 'wordlift' ), |
|
163 | + ); |
|
164 | + |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Delete Faq items. |
|
169 | + * |
|
170 | + * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
171 | + * |
|
172 | + * @return array Associative array whether the faq item is deleted or not. |
|
173 | + */ |
|
174 | + public static function delete_faq_items( $request ) { |
|
175 | + $post_data = $request->get_params(); |
|
176 | + $post_id = $post_data['post_id']; |
|
177 | + $faq_items = $post_data['faq_items']; |
|
178 | + foreach ( $faq_items as $faq_item ) { |
|
179 | + /** |
|
180 | + * Note: the order of keys is important in order to delete it properly |
|
181 | + * If the order change, delete operation will fail since it is converted |
|
182 | + * in to a string when it was stored. |
|
183 | + * we cant rely on client to post it in correct order, so we create an array |
|
184 | + * in correct order. |
|
185 | + */ |
|
186 | + $deleted_faq_item = array( |
|
187 | + 'question' => $faq_item['question'], |
|
188 | + 'answer' => $faq_item['answer'], |
|
189 | + 'id' => (int) $faq_item['id'], |
|
190 | + ); |
|
191 | + /** |
|
192 | + * If the field to be deleted is answer, then don't delete the faq item, |
|
193 | + * if it is question then delete the faq item. |
|
194 | + */ |
|
195 | + if ( self::ANSWER === $faq_item['field_to_be_deleted'] ) { |
|
196 | + $previous_value = $deleted_faq_item; |
|
197 | + // then don't delete the faq item, set the answer as empty. |
|
198 | + $deleted_faq_item['answer'] = ''; |
|
199 | + $new_value = $deleted_faq_item; |
|
200 | + update_post_meta( $post_id, self::FAQ_META_KEY, $new_value, $previous_value ); |
|
201 | + } elseif ( self::QUESTION === $faq_item['field_to_be_deleted'] ) { |
|
202 | + /** |
|
203 | + * If the question is deleted, then delete the faq item. |
|
204 | + */ |
|
205 | + delete_post_meta( $post_id, self::FAQ_META_KEY, $deleted_faq_item ); |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * We are returning only the first item id, since the user can select only one text at a time. |
|
211 | + */ |
|
212 | + return array( |
|
213 | + 'status' => 'success', |
|
214 | + 'message' => __( 'Faq item successfully deleted.', 'wordlift' ), |
|
215 | + ); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Insert or update FAQ items. |
|
220 | + * |
|
221 | + * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
222 | + * |
|
223 | + * @return array Associative array whether the faq item is inserted or not |
|
224 | + */ |
|
225 | + public static function add_faq_items( $request ) { |
|
226 | + $post_data = $request->get_params(); |
|
227 | + $post_id = $post_data['post_id']; |
|
228 | + $faq_items = $post_data['faq_items']; |
|
229 | + foreach ( $faq_items as &$faq_item ) { |
|
230 | + // Add an identifier id to the faq item, it helps to prevent duplication problem. |
|
231 | + /** |
|
232 | + * We are using time() and a random integer to prevent |
|
233 | + * duplication problem. |
|
234 | + */ |
|
235 | + $faq_item['id'] = time() + wp_rand( 1, 100 ) + wp_rand( 1, 100 ); |
|
236 | + add_post_meta( (int) $post_id, self::FAQ_META_KEY, $faq_item ); |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * We are returning only the first item id, since the user can select only one text at a time. |
|
241 | + */ |
|
242 | + return array( |
|
243 | + 'status' => 'success', |
|
244 | + 'message' => __( 'Question successfully added.', 'wordlift' ), |
|
245 | + 'id' => (int) $faq_items[0]['id'], |
|
246 | + ); |
|
247 | + |
|
248 | + } |
|
249 | 249 | } |
@@ -26,25 +26,25 @@ discard block |
||
26 | 26 | const ANSWER = 'answer'; |
27 | 27 | |
28 | 28 | public static function register_routes() { |
29 | - add_action( 'rest_api_init', 'Wordlift\FAQ\FAQ_Rest_Controller::register_route_callback' ); |
|
29 | + add_action('rest_api_init', 'Wordlift\FAQ\FAQ_Rest_Controller::register_route_callback'); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | public static function register_route_callback() { |
33 | 33 | /** |
34 | 34 | * Specifies the list of arguments to be present for update request. |
35 | 35 | */ |
36 | - $post_id_validation_settings = array( |
|
36 | + $post_id_validation_settings = array( |
|
37 | 37 | 'required' => true, |
38 | 38 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
39 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
40 | - return is_numeric( $param ); |
|
39 | + 'validate_callback' => function($param, $request, $key) { |
|
40 | + return is_numeric($param); |
|
41 | 41 | }, |
42 | 42 | ); |
43 | 43 | $faq_items_validation_settings = array( |
44 | 44 | 'required' => true, |
45 | 45 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
46 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
47 | - return is_array( $param ) && count( $param ) > 0; |
|
46 | + 'validate_callback' => function($param, $request, $key) { |
|
47 | + return is_array($param) && count($param) > 0; |
|
48 | 48 | }, |
49 | 49 | ); |
50 | 50 | /** |
@@ -65,8 +65,8 @@ discard block |
||
65 | 65 | array( |
66 | 66 | 'methods' => \WP_REST_Server::CREATABLE, |
67 | 67 | 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::add_faq_items', |
68 | - 'permission_callback' => function () { |
|
69 | - return current_user_can( 'publish_posts' ); |
|
68 | + 'permission_callback' => function() { |
|
69 | + return current_user_can('publish_posts'); |
|
70 | 70 | }, |
71 | 71 | 'args' => $create_or_update_or_delete_args, |
72 | 72 | ) |
@@ -80,8 +80,8 @@ discard block |
||
80 | 80 | array( |
81 | 81 | 'methods' => \WP_REST_Server::EDITABLE, |
82 | 82 | 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::update_faq_items', |
83 | - 'permission_callback' => function () { |
|
84 | - return current_user_can( 'publish_posts' ); |
|
83 | + 'permission_callback' => function() { |
|
84 | + return current_user_can('publish_posts'); |
|
85 | 85 | }, |
86 | 86 | 'args' => $create_or_update_or_delete_args, |
87 | 87 | ) |
@@ -95,10 +95,10 @@ discard block |
||
95 | 95 | array( |
96 | 96 | 'methods' => \WP_REST_Server::READABLE, |
97 | 97 | 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::get_faq_items', |
98 | - 'permission_callback' => function () { |
|
99 | - return current_user_can( 'publish_posts' ); |
|
98 | + 'permission_callback' => function() { |
|
99 | + return current_user_can('publish_posts'); |
|
100 | 100 | }, |
101 | - 'args' => array( 'post_id' => $post_id_validation_settings ), |
|
101 | + 'args' => array('post_id' => $post_id_validation_settings), |
|
102 | 102 | ) |
103 | 103 | ); |
104 | 104 | /** |
@@ -110,8 +110,8 @@ discard block |
||
110 | 110 | array( |
111 | 111 | 'methods' => \WP_REST_Server::DELETABLE, |
112 | 112 | 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::delete_faq_items', |
113 | - 'permission_callback' => function () { |
|
114 | - return current_user_can( 'publish_posts' ); |
|
113 | + 'permission_callback' => function() { |
|
114 | + return current_user_can('publish_posts'); |
|
115 | 115 | }, |
116 | 116 | 'args' => $create_or_update_or_delete_args, |
117 | 117 | ) |
@@ -125,11 +125,11 @@ discard block |
||
125 | 125 | * |
126 | 126 | * @return array Result array, if post id is not given then error array is returned. |
127 | 127 | */ |
128 | - public static function get_faq_items( $request ) { |
|
128 | + public static function get_faq_items($request) { |
|
129 | 129 | $data = $request->get_params(); |
130 | 130 | $post_id = (int) $data['post_id']; |
131 | 131 | |
132 | - return get_post_meta( $post_id, self::FAQ_META_KEY ); |
|
132 | + return get_post_meta($post_id, self::FAQ_META_KEY); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | /** |
@@ -139,27 +139,27 @@ discard block |
||
139 | 139 | * |
140 | 140 | * @return array Result array, if post id is not given then error array is returned. |
141 | 141 | */ |
142 | - public static function update_faq_items( $request ) { |
|
142 | + public static function update_faq_items($request) { |
|
143 | 143 | $data = $request->get_params(); |
144 | 144 | $post_id = (int) $data['post_id']; |
145 | 145 | $faq_items = (array) $data['faq_items']; |
146 | - foreach ( $faq_items as $faq_item ) { |
|
146 | + foreach ($faq_items as $faq_item) { |
|
147 | 147 | $previous_value = array( |
148 | 148 | 'question' => (string) $faq_item['previous_question_value'], |
149 | 149 | 'answer' => (string) $faq_item['previous_answer_value'], |
150 | 150 | 'id' => (int) $faq_item['id'], |
151 | 151 | ); |
152 | - $new_value = array( |
|
152 | + $new_value = array( |
|
153 | 153 | 'question' => (string) $faq_item['question'], |
154 | 154 | 'answer' => (string) $faq_item['answer'], |
155 | 155 | 'id' => (int) $faq_item['id'], |
156 | 156 | ); |
157 | - update_post_meta( $post_id, self::FAQ_META_KEY, $new_value, $previous_value ); |
|
157 | + update_post_meta($post_id, self::FAQ_META_KEY, $new_value, $previous_value); |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | return array( |
161 | 161 | 'status' => 'success', |
162 | - 'message' => __( 'Faq Items updated successfully', 'wordlift' ), |
|
162 | + 'message' => __('Faq Items updated successfully', 'wordlift'), |
|
163 | 163 | ); |
164 | 164 | |
165 | 165 | } |
@@ -171,11 +171,11 @@ discard block |
||
171 | 171 | * |
172 | 172 | * @return array Associative array whether the faq item is deleted or not. |
173 | 173 | */ |
174 | - public static function delete_faq_items( $request ) { |
|
174 | + public static function delete_faq_items($request) { |
|
175 | 175 | $post_data = $request->get_params(); |
176 | 176 | $post_id = $post_data['post_id']; |
177 | 177 | $faq_items = $post_data['faq_items']; |
178 | - foreach ( $faq_items as $faq_item ) { |
|
178 | + foreach ($faq_items as $faq_item) { |
|
179 | 179 | /** |
180 | 180 | * Note: the order of keys is important in order to delete it properly |
181 | 181 | * If the order change, delete operation will fail since it is converted |
@@ -192,17 +192,17 @@ discard block |
||
192 | 192 | * If the field to be deleted is answer, then don't delete the faq item, |
193 | 193 | * if it is question then delete the faq item. |
194 | 194 | */ |
195 | - if ( self::ANSWER === $faq_item['field_to_be_deleted'] ) { |
|
195 | + if (self::ANSWER === $faq_item['field_to_be_deleted']) { |
|
196 | 196 | $previous_value = $deleted_faq_item; |
197 | 197 | // then don't delete the faq item, set the answer as empty. |
198 | 198 | $deleted_faq_item['answer'] = ''; |
199 | 199 | $new_value = $deleted_faq_item; |
200 | - update_post_meta( $post_id, self::FAQ_META_KEY, $new_value, $previous_value ); |
|
201 | - } elseif ( self::QUESTION === $faq_item['field_to_be_deleted'] ) { |
|
200 | + update_post_meta($post_id, self::FAQ_META_KEY, $new_value, $previous_value); |
|
201 | + } elseif (self::QUESTION === $faq_item['field_to_be_deleted']) { |
|
202 | 202 | /** |
203 | 203 | * If the question is deleted, then delete the faq item. |
204 | 204 | */ |
205 | - delete_post_meta( $post_id, self::FAQ_META_KEY, $deleted_faq_item ); |
|
205 | + delete_post_meta($post_id, self::FAQ_META_KEY, $deleted_faq_item); |
|
206 | 206 | } |
207 | 207 | } |
208 | 208 | |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | */ |
212 | 212 | return array( |
213 | 213 | 'status' => 'success', |
214 | - 'message' => __( 'Faq item successfully deleted.', 'wordlift' ), |
|
214 | + 'message' => __('Faq item successfully deleted.', 'wordlift'), |
|
215 | 215 | ); |
216 | 216 | } |
217 | 217 | |
@@ -222,18 +222,18 @@ discard block |
||
222 | 222 | * |
223 | 223 | * @return array Associative array whether the faq item is inserted or not |
224 | 224 | */ |
225 | - public static function add_faq_items( $request ) { |
|
225 | + public static function add_faq_items($request) { |
|
226 | 226 | $post_data = $request->get_params(); |
227 | 227 | $post_id = $post_data['post_id']; |
228 | 228 | $faq_items = $post_data['faq_items']; |
229 | - foreach ( $faq_items as &$faq_item ) { |
|
229 | + foreach ($faq_items as &$faq_item) { |
|
230 | 230 | // Add an identifier id to the faq item, it helps to prevent duplication problem. |
231 | 231 | /** |
232 | 232 | * We are using time() and a random integer to prevent |
233 | 233 | * duplication problem. |
234 | 234 | */ |
235 | - $faq_item['id'] = time() + wp_rand( 1, 100 ) + wp_rand( 1, 100 ); |
|
236 | - add_post_meta( (int) $post_id, self::FAQ_META_KEY, $faq_item ); |
|
235 | + $faq_item['id'] = time() + wp_rand(1, 100) + wp_rand(1, 100); |
|
236 | + add_post_meta((int) $post_id, self::FAQ_META_KEY, $faq_item); |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | /** |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | */ |
242 | 242 | return array( |
243 | 243 | 'status' => 'success', |
244 | - 'message' => __( 'Question successfully added.', 'wordlift' ), |
|
244 | + 'message' => __('Question successfully added.', 'wordlift'), |
|
245 | 245 | 'id' => (int) $faq_items[0]['id'], |
246 | 246 | ); |
247 | 247 |
@@ -13,41 +13,41 @@ |
||
13 | 13 | |
14 | 14 | class Faq_Tinymce_Adapter { |
15 | 15 | |
16 | - const FAQ_TINYMCE_PLUGIN_NAME = 'wl_faq_tinymce'; |
|
17 | - |
|
18 | - const FAQ_TINYMCE_ADD_BUTTON_ID = 'wl-faq-toolbar-button'; |
|
19 | - |
|
20 | - /** |
|
21 | - * Add a list of custom tags which is to be used by our highlighting program. |
|
22 | - * |
|
23 | - * @param $init_array |
|
24 | - * |
|
25 | - * @return array |
|
26 | - */ |
|
27 | - public function register_custom_tags( $init_array ) { |
|
28 | - $opts = '~wl-faq-question,~wl-faq-answer'; |
|
29 | - $init_array['custom_elements'] .= ( empty( $init_array['custom_elements'] ) ? '' : ',' ) . $opts; |
|
30 | - $init_array['extended_valid_elements'] .= ( empty( $init_array['extended_valid_elements'] ) ? '' : ',' ) . $opts; |
|
31 | - |
|
32 | - return $init_array; |
|
33 | - } |
|
34 | - |
|
35 | - public function register_faq_tinymce_plugin( $plugins ) { |
|
36 | - /** |
|
37 | - * Registering the tinymce plugin for FAQ here. |
|
38 | - * |
|
39 | - * @since 3.26.0 |
|
40 | - */ |
|
41 | - $version = Wordlift::get_instance()->get_version(); |
|
42 | - $plugins[ self::FAQ_TINYMCE_PLUGIN_NAME ] = plugin_dir_url( dirname( __DIR__ ) ) . 'js/dist/tinymce-faq-plugin.full.js?ver=' . $version; |
|
43 | - |
|
44 | - return $plugins; |
|
45 | - } |
|
46 | - |
|
47 | - public function register_faq_toolbar_button( $buttons ) { |
|
48 | - array_push( $buttons, self::FAQ_TINYMCE_ADD_BUTTON_ID ); |
|
49 | - |
|
50 | - return $buttons; |
|
51 | - } |
|
16 | + const FAQ_TINYMCE_PLUGIN_NAME = 'wl_faq_tinymce'; |
|
17 | + |
|
18 | + const FAQ_TINYMCE_ADD_BUTTON_ID = 'wl-faq-toolbar-button'; |
|
19 | + |
|
20 | + /** |
|
21 | + * Add a list of custom tags which is to be used by our highlighting program. |
|
22 | + * |
|
23 | + * @param $init_array |
|
24 | + * |
|
25 | + * @return array |
|
26 | + */ |
|
27 | + public function register_custom_tags( $init_array ) { |
|
28 | + $opts = '~wl-faq-question,~wl-faq-answer'; |
|
29 | + $init_array['custom_elements'] .= ( empty( $init_array['custom_elements'] ) ? '' : ',' ) . $opts; |
|
30 | + $init_array['extended_valid_elements'] .= ( empty( $init_array['extended_valid_elements'] ) ? '' : ',' ) . $opts; |
|
31 | + |
|
32 | + return $init_array; |
|
33 | + } |
|
34 | + |
|
35 | + public function register_faq_tinymce_plugin( $plugins ) { |
|
36 | + /** |
|
37 | + * Registering the tinymce plugin for FAQ here. |
|
38 | + * |
|
39 | + * @since 3.26.0 |
|
40 | + */ |
|
41 | + $version = Wordlift::get_instance()->get_version(); |
|
42 | + $plugins[ self::FAQ_TINYMCE_PLUGIN_NAME ] = plugin_dir_url( dirname( __DIR__ ) ) . 'js/dist/tinymce-faq-plugin.full.js?ver=' . $version; |
|
43 | + |
|
44 | + return $plugins; |
|
45 | + } |
|
46 | + |
|
47 | + public function register_faq_toolbar_button( $buttons ) { |
|
48 | + array_push( $buttons, self::FAQ_TINYMCE_ADD_BUTTON_ID ); |
|
49 | + |
|
50 | + return $buttons; |
|
51 | + } |
|
52 | 52 | |
53 | 53 | } |
@@ -24,28 +24,28 @@ |
||
24 | 24 | * |
25 | 25 | * @return array |
26 | 26 | */ |
27 | - public function register_custom_tags( $init_array ) { |
|
27 | + public function register_custom_tags($init_array) { |
|
28 | 28 | $opts = '~wl-faq-question,~wl-faq-answer'; |
29 | - $init_array['custom_elements'] .= ( empty( $init_array['custom_elements'] ) ? '' : ',' ) . $opts; |
|
30 | - $init_array['extended_valid_elements'] .= ( empty( $init_array['extended_valid_elements'] ) ? '' : ',' ) . $opts; |
|
29 | + $init_array['custom_elements'] .= (empty($init_array['custom_elements']) ? '' : ',').$opts; |
|
30 | + $init_array['extended_valid_elements'] .= (empty($init_array['extended_valid_elements']) ? '' : ',').$opts; |
|
31 | 31 | |
32 | 32 | return $init_array; |
33 | 33 | } |
34 | 34 | |
35 | - public function register_faq_tinymce_plugin( $plugins ) { |
|
35 | + public function register_faq_tinymce_plugin($plugins) { |
|
36 | 36 | /** |
37 | 37 | * Registering the tinymce plugin for FAQ here. |
38 | 38 | * |
39 | 39 | * @since 3.26.0 |
40 | 40 | */ |
41 | 41 | $version = Wordlift::get_instance()->get_version(); |
42 | - $plugins[ self::FAQ_TINYMCE_PLUGIN_NAME ] = plugin_dir_url( dirname( __DIR__ ) ) . 'js/dist/tinymce-faq-plugin.full.js?ver=' . $version; |
|
42 | + $plugins[self::FAQ_TINYMCE_PLUGIN_NAME] = plugin_dir_url(dirname(__DIR__)).'js/dist/tinymce-faq-plugin.full.js?ver='.$version; |
|
43 | 43 | |
44 | 44 | return $plugins; |
45 | 45 | } |
46 | 46 | |
47 | - public function register_faq_toolbar_button( $buttons ) { |
|
48 | - array_push( $buttons, self::FAQ_TINYMCE_ADD_BUTTON_ID ); |
|
47 | + public function register_faq_toolbar_button($buttons) { |
|
48 | + array_push($buttons, self::FAQ_TINYMCE_ADD_BUTTON_ID); |
|
49 | 49 | |
50 | 50 | return $buttons; |
51 | 51 | } |
@@ -14,70 +14,70 @@ |
||
14 | 14 | |
15 | 15 | class Post_Handler { |
16 | 16 | |
17 | - /** |
|
18 | - * Process post. |
|
19 | - * |
|
20 | - * @param int $post_id |
|
21 | - */ |
|
22 | - public static function fix( $post_id ) { |
|
23 | - // 3 cases: |
|
24 | - // 1. item id w/o the base URI: itemid="/entity/marketing" |
|
25 | - // 2. item id w/ domain outside the scope of dataset URI: itemid="https://data.wordlift.io/wl95583/entity/emailing" |
|
26 | - // 3. item id w/ domain within the scope of the dataset URI, but non existent. |
|
27 | - // 4. should we manipulate also the `wp:wordlift/classification` block? |
|
28 | - |
|
29 | - $post = get_post( $post_id ); |
|
30 | - $new_post_content = preg_replace_callback( |
|
31 | - '@<(\w+)[^<]*class="([^"]*)"\sitemid="([^"]+)"[^>]*>(.*?)</\1>@i', |
|
32 | - array( get_class(), 'fix_annotations' ), |
|
33 | - $post->post_content, |
|
34 | - - 1, |
|
35 | - $count |
|
36 | - ); |
|
37 | - |
|
38 | - if ( 0 === $count || $new_post_content === $post->post_content ) { |
|
39 | - // Bail out if the regex doesn't match or if the post content didn't change. |
|
40 | - return; |
|
41 | - } |
|
42 | - |
|
43 | - wp_update_post( |
|
44 | - array( |
|
45 | - 'ID' => $post_id, |
|
46 | - 'post_content' => $new_post_content, |
|
47 | - ) |
|
48 | - ); |
|
49 | - |
|
50 | - } |
|
51 | - |
|
52 | - public static function fix_annotations( $args ) { |
|
53 | - // Make the item id relative. |
|
54 | - if ( 0 === preg_match( '@(?:https?://[^/]+/[^/]+)?/?(.*)@', $args[3], $matches ) ) { |
|
55 | - return $args[4]; |
|
56 | - } |
|
57 | - |
|
58 | - $item_id = $matches[1]; |
|
59 | - |
|
60 | - // Bail out if the item id is empty. |
|
61 | - if ( empty( $item_id ) ) { |
|
62 | - return $args[4]; |
|
63 | - } |
|
64 | - |
|
65 | - // Find a matching content. |
|
66 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
67 | - $content = $content_service->get_by_entity_id_or_same_as( $item_id ); |
|
68 | - |
|
69 | - if ( ! isset( $content ) ) { |
|
70 | - // No content found return only label. |
|
71 | - return $args[4]; |
|
72 | - } else { |
|
73 | - // Get the actual entity id. |
|
74 | - $new_item_id = $content_service->get_entity_id( |
|
75 | - new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ) |
|
76 | - ); |
|
77 | - |
|
78 | - // Replace the incoming entity id with the actual entity id. |
|
79 | - return str_replace( $args[3], $new_item_id, $args[0] ); |
|
80 | - } |
|
81 | - } |
|
17 | + /** |
|
18 | + * Process post. |
|
19 | + * |
|
20 | + * @param int $post_id |
|
21 | + */ |
|
22 | + public static function fix( $post_id ) { |
|
23 | + // 3 cases: |
|
24 | + // 1. item id w/o the base URI: itemid="/entity/marketing" |
|
25 | + // 2. item id w/ domain outside the scope of dataset URI: itemid="https://data.wordlift.io/wl95583/entity/emailing" |
|
26 | + // 3. item id w/ domain within the scope of the dataset URI, but non existent. |
|
27 | + // 4. should we manipulate also the `wp:wordlift/classification` block? |
|
28 | + |
|
29 | + $post = get_post( $post_id ); |
|
30 | + $new_post_content = preg_replace_callback( |
|
31 | + '@<(\w+)[^<]*class="([^"]*)"\sitemid="([^"]+)"[^>]*>(.*?)</\1>@i', |
|
32 | + array( get_class(), 'fix_annotations' ), |
|
33 | + $post->post_content, |
|
34 | + - 1, |
|
35 | + $count |
|
36 | + ); |
|
37 | + |
|
38 | + if ( 0 === $count || $new_post_content === $post->post_content ) { |
|
39 | + // Bail out if the regex doesn't match or if the post content didn't change. |
|
40 | + return; |
|
41 | + } |
|
42 | + |
|
43 | + wp_update_post( |
|
44 | + array( |
|
45 | + 'ID' => $post_id, |
|
46 | + 'post_content' => $new_post_content, |
|
47 | + ) |
|
48 | + ); |
|
49 | + |
|
50 | + } |
|
51 | + |
|
52 | + public static function fix_annotations( $args ) { |
|
53 | + // Make the item id relative. |
|
54 | + if ( 0 === preg_match( '@(?:https?://[^/]+/[^/]+)?/?(.*)@', $args[3], $matches ) ) { |
|
55 | + return $args[4]; |
|
56 | + } |
|
57 | + |
|
58 | + $item_id = $matches[1]; |
|
59 | + |
|
60 | + // Bail out if the item id is empty. |
|
61 | + if ( empty( $item_id ) ) { |
|
62 | + return $args[4]; |
|
63 | + } |
|
64 | + |
|
65 | + // Find a matching content. |
|
66 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
67 | + $content = $content_service->get_by_entity_id_or_same_as( $item_id ); |
|
68 | + |
|
69 | + if ( ! isset( $content ) ) { |
|
70 | + // No content found return only label. |
|
71 | + return $args[4]; |
|
72 | + } else { |
|
73 | + // Get the actual entity id. |
|
74 | + $new_item_id = $content_service->get_entity_id( |
|
75 | + new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ) |
|
76 | + ); |
|
77 | + |
|
78 | + // Replace the incoming entity id with the actual entity id. |
|
79 | + return str_replace( $args[3], $new_item_id, $args[0] ); |
|
80 | + } |
|
81 | + } |
|
82 | 82 | |
83 | 83 | } |
@@ -19,23 +19,23 @@ discard block |
||
19 | 19 | * |
20 | 20 | * @param int $post_id |
21 | 21 | */ |
22 | - public static function fix( $post_id ) { |
|
22 | + public static function fix($post_id) { |
|
23 | 23 | // 3 cases: |
24 | 24 | // 1. item id w/o the base URI: itemid="/entity/marketing" |
25 | 25 | // 2. item id w/ domain outside the scope of dataset URI: itemid="https://data.wordlift.io/wl95583/entity/emailing" |
26 | 26 | // 3. item id w/ domain within the scope of the dataset URI, but non existent. |
27 | 27 | // 4. should we manipulate also the `wp:wordlift/classification` block? |
28 | 28 | |
29 | - $post = get_post( $post_id ); |
|
29 | + $post = get_post($post_id); |
|
30 | 30 | $new_post_content = preg_replace_callback( |
31 | 31 | '@<(\w+)[^<]*class="([^"]*)"\sitemid="([^"]+)"[^>]*>(.*?)</\1>@i', |
32 | - array( get_class(), 'fix_annotations' ), |
|
32 | + array(get_class(), 'fix_annotations'), |
|
33 | 33 | $post->post_content, |
34 | 34 | - 1, |
35 | 35 | $count |
36 | 36 | ); |
37 | 37 | |
38 | - if ( 0 === $count || $new_post_content === $post->post_content ) { |
|
38 | + if (0 === $count || $new_post_content === $post->post_content) { |
|
39 | 39 | // Bail out if the regex doesn't match or if the post content didn't change. |
40 | 40 | return; |
41 | 41 | } |
@@ -49,34 +49,34 @@ discard block |
||
49 | 49 | |
50 | 50 | } |
51 | 51 | |
52 | - public static function fix_annotations( $args ) { |
|
52 | + public static function fix_annotations($args) { |
|
53 | 53 | // Make the item id relative. |
54 | - if ( 0 === preg_match( '@(?:https?://[^/]+/[^/]+)?/?(.*)@', $args[3], $matches ) ) { |
|
54 | + if (0 === preg_match('@(?:https?://[^/]+/[^/]+)?/?(.*)@', $args[3], $matches)) { |
|
55 | 55 | return $args[4]; |
56 | 56 | } |
57 | 57 | |
58 | 58 | $item_id = $matches[1]; |
59 | 59 | |
60 | 60 | // Bail out if the item id is empty. |
61 | - if ( empty( $item_id ) ) { |
|
61 | + if (empty($item_id)) { |
|
62 | 62 | return $args[4]; |
63 | 63 | } |
64 | 64 | |
65 | 65 | // Find a matching content. |
66 | 66 | $content_service = Wordpress_Content_Service::get_instance(); |
67 | - $content = $content_service->get_by_entity_id_or_same_as( $item_id ); |
|
67 | + $content = $content_service->get_by_entity_id_or_same_as($item_id); |
|
68 | 68 | |
69 | - if ( ! isset( $content ) ) { |
|
69 | + if ( ! isset($content)) { |
|
70 | 70 | // No content found return only label. |
71 | 71 | return $args[4]; |
72 | 72 | } else { |
73 | 73 | // Get the actual entity id. |
74 | 74 | $new_item_id = $content_service->get_entity_id( |
75 | - new Wordpress_Content_Id( $content->get_id(), $content->get_object_type_enum() ) |
|
75 | + new Wordpress_Content_Id($content->get_id(), $content->get_object_type_enum()) |
|
76 | 76 | ); |
77 | 77 | |
78 | 78 | // Replace the incoming entity id with the actual entity id. |
79 | - return str_replace( $args[3], $new_item_id, $args[0] ); |
|
79 | + return str_replace($args[3], $new_item_id, $args[0]); |
|
80 | 80 | } |
81 | 81 | } |
82 | 82 |
@@ -15,112 +15,112 @@ |
||
15 | 15 | // @@todo: add a hook to clear the cached files now and then. |
16 | 16 | class Cacheable_Http_Client extends Simple_Http_Client { |
17 | 17 | |
18 | - /** |
|
19 | - * The TTL of cached responses in seconds. |
|
20 | - * |
|
21 | - * @var int $ttl The TTL in seconds. |
|
22 | - * @access private |
|
23 | - * @since 1.0.0 |
|
24 | - */ |
|
25 | - private $ttl; |
|
26 | - |
|
27 | - /** |
|
28 | - * The cache dir where the cached responses are written. |
|
29 | - * |
|
30 | - * @since 1.0.0 |
|
31 | - * @access private |
|
32 | - * @var string $cache_dir The cache dir where the cached responses are written. |
|
33 | - */ |
|
34 | - private $cache_dir; |
|
35 | - |
|
36 | - /** |
|
37 | - * A {@link Wordlift_Log_Service} instance. |
|
38 | - * |
|
39 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
40 | - * @access private |
|
41 | - * @since 1.0.0 |
|
42 | - */ |
|
43 | - private $log; |
|
44 | - |
|
45 | - /** |
|
46 | - * Create a {@link Cacheable_Http_Client} with the specified TTL, default 900 secs. |
|
47 | - * |
|
48 | - * @param int $ttl The cache TTL, default 900 secs. |
|
49 | - * |
|
50 | - * @since 1.0.0 |
|
51 | - */ |
|
52 | - public function __construct( $ttl = 900 ) { |
|
53 | - |
|
54 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
55 | - |
|
56 | - $this->ttl = $ttl; |
|
57 | - |
|
58 | - // Get the temp dir and add the directory separator if missing. |
|
59 | - $temp_dir = get_temp_dir(); |
|
60 | - if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
61 | - $temp_dir .= DIRECTORY_SEPARATOR; |
|
62 | - } |
|
63 | - $this->cache_dir = $temp_dir . 'wlfb-http-cache'; |
|
64 | - |
|
65 | - $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
66 | - wp_mkdir_p( $this->cache_dir ); |
|
67 | - |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @inheritDoc |
|
72 | - */ |
|
73 | - public function request( $url, $options = array() ) { |
|
74 | - |
|
75 | - // Create a hash and a path to the cache file. |
|
76 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
77 | - $hash = md5( $url ) . '-' . md5( serialize( $options ) ); |
|
78 | - $filename = $this->get_path( $hash ); |
|
79 | - |
|
80 | - // If the cache file exists and it's not too old, then return it. |
|
81 | - if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
82 | - $this->log->trace( "Cache HIT.\n" ); |
|
83 | - |
|
84 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
85 | - return json_decode( file_get_contents( $filename ), true ); |
|
86 | - } |
|
87 | - |
|
88 | - $this->log->trace( "Cache MISS for URL $url, hash $hash.\n" ); |
|
89 | - |
|
90 | - // Get a fresh response and return it. |
|
91 | - $response = parent::request( $url, $options ); |
|
92 | - |
|
93 | - // Return immediately, do not cache. |
|
94 | - if ( is_wp_error( $response ) ) { |
|
95 | - return $response; |
|
96 | - } |
|
97 | - |
|
98 | - // Do not cache response with invalid status codes or status code different from 2xx. |
|
99 | - $code = wp_remote_retrieve_response_code( $response ); |
|
100 | - if ( ! is_numeric( $code ) || 2 !== intval( $code ) / 100 ) { |
|
101 | - return $response; |
|
102 | - } |
|
103 | - |
|
104 | - // Cache. |
|
105 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
106 | - @unlink( $filename ); |
|
107 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.json_encode_json_encode,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
108 | - @file_put_contents( $filename, json_encode( $response ) ); |
|
109 | - |
|
110 | - return $response; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get the full path for the given `$hash`. The file is not checked for its existence. |
|
115 | - * |
|
116 | - * @param string $hash A file hash. |
|
117 | - * |
|
118 | - * @return string The full path to the file. |
|
119 | - * @since 1.0.0 |
|
120 | - */ |
|
121 | - private function get_path( $hash ) { |
|
122 | - |
|
123 | - return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
124 | - } |
|
18 | + /** |
|
19 | + * The TTL of cached responses in seconds. |
|
20 | + * |
|
21 | + * @var int $ttl The TTL in seconds. |
|
22 | + * @access private |
|
23 | + * @since 1.0.0 |
|
24 | + */ |
|
25 | + private $ttl; |
|
26 | + |
|
27 | + /** |
|
28 | + * The cache dir where the cached responses are written. |
|
29 | + * |
|
30 | + * @since 1.0.0 |
|
31 | + * @access private |
|
32 | + * @var string $cache_dir The cache dir where the cached responses are written. |
|
33 | + */ |
|
34 | + private $cache_dir; |
|
35 | + |
|
36 | + /** |
|
37 | + * A {@link Wordlift_Log_Service} instance. |
|
38 | + * |
|
39 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
40 | + * @access private |
|
41 | + * @since 1.0.0 |
|
42 | + */ |
|
43 | + private $log; |
|
44 | + |
|
45 | + /** |
|
46 | + * Create a {@link Cacheable_Http_Client} with the specified TTL, default 900 secs. |
|
47 | + * |
|
48 | + * @param int $ttl The cache TTL, default 900 secs. |
|
49 | + * |
|
50 | + * @since 1.0.0 |
|
51 | + */ |
|
52 | + public function __construct( $ttl = 900 ) { |
|
53 | + |
|
54 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
55 | + |
|
56 | + $this->ttl = $ttl; |
|
57 | + |
|
58 | + // Get the temp dir and add the directory separator if missing. |
|
59 | + $temp_dir = get_temp_dir(); |
|
60 | + if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
61 | + $temp_dir .= DIRECTORY_SEPARATOR; |
|
62 | + } |
|
63 | + $this->cache_dir = $temp_dir . 'wlfb-http-cache'; |
|
64 | + |
|
65 | + $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
66 | + wp_mkdir_p( $this->cache_dir ); |
|
67 | + |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @inheritDoc |
|
72 | + */ |
|
73 | + public function request( $url, $options = array() ) { |
|
74 | + |
|
75 | + // Create a hash and a path to the cache file. |
|
76 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
77 | + $hash = md5( $url ) . '-' . md5( serialize( $options ) ); |
|
78 | + $filename = $this->get_path( $hash ); |
|
79 | + |
|
80 | + // If the cache file exists and it's not too old, then return it. |
|
81 | + if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
82 | + $this->log->trace( "Cache HIT.\n" ); |
|
83 | + |
|
84 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
85 | + return json_decode( file_get_contents( $filename ), true ); |
|
86 | + } |
|
87 | + |
|
88 | + $this->log->trace( "Cache MISS for URL $url, hash $hash.\n" ); |
|
89 | + |
|
90 | + // Get a fresh response and return it. |
|
91 | + $response = parent::request( $url, $options ); |
|
92 | + |
|
93 | + // Return immediately, do not cache. |
|
94 | + if ( is_wp_error( $response ) ) { |
|
95 | + return $response; |
|
96 | + } |
|
97 | + |
|
98 | + // Do not cache response with invalid status codes or status code different from 2xx. |
|
99 | + $code = wp_remote_retrieve_response_code( $response ); |
|
100 | + if ( ! is_numeric( $code ) || 2 !== intval( $code ) / 100 ) { |
|
101 | + return $response; |
|
102 | + } |
|
103 | + |
|
104 | + // Cache. |
|
105 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
106 | + @unlink( $filename ); |
|
107 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.json_encode_json_encode,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
108 | + @file_put_contents( $filename, json_encode( $response ) ); |
|
109 | + |
|
110 | + return $response; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get the full path for the given `$hash`. The file is not checked for its existence. |
|
115 | + * |
|
116 | + * @param string $hash A file hash. |
|
117 | + * |
|
118 | + * @return string The full path to the file. |
|
119 | + * @since 1.0.0 |
|
120 | + */ |
|
121 | + private function get_path( $hash ) { |
|
122 | + |
|
123 | + return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
124 | + } |
|
125 | 125 | |
126 | 126 | } |
@@ -49,63 +49,63 @@ discard block |
||
49 | 49 | * |
50 | 50 | * @since 1.0.0 |
51 | 51 | */ |
52 | - public function __construct( $ttl = 900 ) { |
|
52 | + public function __construct($ttl = 900) { |
|
53 | 53 | |
54 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
54 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
55 | 55 | |
56 | 56 | $this->ttl = $ttl; |
57 | 57 | |
58 | 58 | // Get the temp dir and add the directory separator if missing. |
59 | 59 | $temp_dir = get_temp_dir(); |
60 | - if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
60 | + if (DIRECTORY_SEPARATOR !== substr($temp_dir, - strlen(DIRECTORY_SEPARATOR))) { |
|
61 | 61 | $temp_dir .= DIRECTORY_SEPARATOR; |
62 | 62 | } |
63 | - $this->cache_dir = $temp_dir . 'wlfb-http-cache'; |
|
63 | + $this->cache_dir = $temp_dir.'wlfb-http-cache'; |
|
64 | 64 | |
65 | - $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
66 | - wp_mkdir_p( $this->cache_dir ); |
|
65 | + $this->log->trace("Creating the cache folder {$this->cache_dir}..."); |
|
66 | + wp_mkdir_p($this->cache_dir); |
|
67 | 67 | |
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
71 | 71 | * @inheritDoc |
72 | 72 | */ |
73 | - public function request( $url, $options = array() ) { |
|
73 | + public function request($url, $options = array()) { |
|
74 | 74 | |
75 | 75 | // Create a hash and a path to the cache file. |
76 | 76 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
77 | - $hash = md5( $url ) . '-' . md5( serialize( $options ) ); |
|
78 | - $filename = $this->get_path( $hash ); |
|
77 | + $hash = md5($url).'-'.md5(serialize($options)); |
|
78 | + $filename = $this->get_path($hash); |
|
79 | 79 | |
80 | 80 | // If the cache file exists and it's not too old, then return it. |
81 | - if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
82 | - $this->log->trace( "Cache HIT.\n" ); |
|
81 | + if (file_exists($filename) && $this->ttl >= time() - filemtime($filename)) { |
|
82 | + $this->log->trace("Cache HIT.\n"); |
|
83 | 83 | |
84 | 84 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
85 | - return json_decode( file_get_contents( $filename ), true ); |
|
85 | + return json_decode(file_get_contents($filename), true); |
|
86 | 86 | } |
87 | 87 | |
88 | - $this->log->trace( "Cache MISS for URL $url, hash $hash.\n" ); |
|
88 | + $this->log->trace("Cache MISS for URL $url, hash $hash.\n"); |
|
89 | 89 | |
90 | 90 | // Get a fresh response and return it. |
91 | - $response = parent::request( $url, $options ); |
|
91 | + $response = parent::request($url, $options); |
|
92 | 92 | |
93 | 93 | // Return immediately, do not cache. |
94 | - if ( is_wp_error( $response ) ) { |
|
94 | + if (is_wp_error($response)) { |
|
95 | 95 | return $response; |
96 | 96 | } |
97 | 97 | |
98 | 98 | // Do not cache response with invalid status codes or status code different from 2xx. |
99 | - $code = wp_remote_retrieve_response_code( $response ); |
|
100 | - if ( ! is_numeric( $code ) || 2 !== intval( $code ) / 100 ) { |
|
99 | + $code = wp_remote_retrieve_response_code($response); |
|
100 | + if ( ! is_numeric($code) || 2 !== intval($code) / 100) { |
|
101 | 101 | return $response; |
102 | 102 | } |
103 | 103 | |
104 | 104 | // Cache. |
105 | 105 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
106 | - @unlink( $filename ); |
|
106 | + @unlink($filename); |
|
107 | 107 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.json_encode_json_encode,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
108 | - @file_put_contents( $filename, json_encode( $response ) ); |
|
108 | + @file_put_contents($filename, json_encode($response)); |
|
109 | 109 | |
110 | 110 | return $response; |
111 | 111 | } |
@@ -118,9 +118,9 @@ discard block |
||
118 | 118 | * @return string The full path to the file. |
119 | 119 | * @since 1.0.0 |
120 | 120 | */ |
121 | - private function get_path( $hash ) { |
|
121 | + private function get_path($hash) { |
|
122 | 122 | |
123 | - return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
123 | + return $this->cache_dir.DIRECTORY_SEPARATOR.$hash; |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | } |
@@ -15,26 +15,26 @@ |
||
15 | 15 | */ |
16 | 16 | interface Http_Client { |
17 | 17 | |
18 | - /** |
|
19 | - * Perform a `GET` operation to the specified `$url`. |
|
20 | - * |
|
21 | - * @param string $url The URL. |
|
22 | - * @param array $options An array of options to pass to WordPress {@link wp_remote_request} function, default: array(). |
|
23 | - * |
|
24 | - * @return \WP_Error|array The response or WP_Error on failure. |
|
25 | - * @since 1.0.0 |
|
26 | - */ |
|
27 | - public function get( $url, $options = array() ); |
|
18 | + /** |
|
19 | + * Perform a `GET` operation to the specified `$url`. |
|
20 | + * |
|
21 | + * @param string $url The URL. |
|
22 | + * @param array $options An array of options to pass to WordPress {@link wp_remote_request} function, default: array(). |
|
23 | + * |
|
24 | + * @return \WP_Error|array The response or WP_Error on failure. |
|
25 | + * @since 1.0.0 |
|
26 | + */ |
|
27 | + public function get( $url, $options = array() ); |
|
28 | 28 | |
29 | - /** |
|
30 | - * Perform a request to the specified `$url`. |
|
31 | - * |
|
32 | - * @param string $url The URL. |
|
33 | - * @param array $options An array of options to pass to WordPress {@link wp_remote_request} function, default: array(). |
|
34 | - * |
|
35 | - * @return \WP_Error|array The response or WP_Error on failure. |
|
36 | - * @since 1.0.0 |
|
37 | - */ |
|
38 | - public function request( $url, $options = array() ); |
|
29 | + /** |
|
30 | + * Perform a request to the specified `$url`. |
|
31 | + * |
|
32 | + * @param string $url The URL. |
|
33 | + * @param array $options An array of options to pass to WordPress {@link wp_remote_request} function, default: array(). |
|
34 | + * |
|
35 | + * @return \WP_Error|array The response or WP_Error on failure. |
|
36 | + * @since 1.0.0 |
|
37 | + */ |
|
38 | + public function request( $url, $options = array() ); |
|
39 | 39 | |
40 | 40 | } |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * @return \WP_Error|array The response or WP_Error on failure. |
25 | 25 | * @since 1.0.0 |
26 | 26 | */ |
27 | - public function get( $url, $options = array() ); |
|
27 | + public function get($url, $options = array()); |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * Perform a request to the specified `$url`. |
@@ -35,6 +35,6 @@ discard block |
||
35 | 35 | * @return \WP_Error|array The response or WP_Error on failure. |
36 | 36 | * @since 1.0.0 |
37 | 37 | */ |
38 | - public function request( $url, $options = array() ); |
|
38 | + public function request($url, $options = array()); |
|
39 | 39 | |
40 | 40 | } |
@@ -14,59 +14,59 @@ |
||
14 | 14 | use Wordlift_Entity_Type_Taxonomy_Service; |
15 | 15 | |
16 | 16 | class Entity_Type_Change_Handler { |
17 | - /** |
|
18 | - * @var Wordlift_Entity_Service |
|
19 | - */ |
|
20 | - private $entity_service; |
|
21 | - /** |
|
22 | - * @var Wordlift_Entity_Type_Service |
|
23 | - */ |
|
24 | - private $entity_type_service; |
|
25 | - |
|
26 | - /** |
|
27 | - * Entity_Type_Change_Handler constructor. |
|
28 | - * |
|
29 | - * @param $entity_service Wordlift_Entity_Service |
|
30 | - * @param $entity_type_service Wordlift_Entity_Type_Service |
|
31 | - */ |
|
32 | - public function __construct( $entity_service, $entity_type_service ) { |
|
33 | - |
|
34 | - $this->entity_service = $entity_service; |
|
35 | - |
|
36 | - $this->entity_type_service = $entity_type_service; |
|
37 | - |
|
38 | - // Takes a performance toll, do we really need it? |
|
39 | - // add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 4 ); |
|
40 | - } |
|
41 | - |
|
42 | - public function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy ) { |
|
43 | - |
|
44 | - if ( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME !== $taxonomy ) { |
|
45 | - return; |
|
46 | - } |
|
47 | - |
|
48 | - if ( count( $terms ) !== 1 ) { |
|
49 | - // Unable to determine which entity type or multiple entity types. |
|
50 | - return; |
|
51 | - } |
|
52 | - |
|
53 | - // This taxonomy is registered only for post, so the object id would |
|
54 | - // be the post id. |
|
55 | - $types = $this->entity_type_service->get_names( $object_id ); |
|
56 | - |
|
57 | - if ( count( $types ) !== 1 ) { |
|
58 | - // Unable to determine which entity type or multiple entity types. |
|
59 | - return; |
|
60 | - } |
|
61 | - |
|
62 | - // Check if set to Article or one of its descendants |
|
63 | - if ( ! in_array( $types[0], Jsonld_Article_Wrapper::$article_types, true ) ) { |
|
64 | - return; |
|
65 | - } |
|
66 | - |
|
67 | - // clear the labels. |
|
68 | - $this->entity_service->set_alternative_labels( $object_id, array() ); |
|
69 | - |
|
70 | - } |
|
17 | + /** |
|
18 | + * @var Wordlift_Entity_Service |
|
19 | + */ |
|
20 | + private $entity_service; |
|
21 | + /** |
|
22 | + * @var Wordlift_Entity_Type_Service |
|
23 | + */ |
|
24 | + private $entity_type_service; |
|
25 | + |
|
26 | + /** |
|
27 | + * Entity_Type_Change_Handler constructor. |
|
28 | + * |
|
29 | + * @param $entity_service Wordlift_Entity_Service |
|
30 | + * @param $entity_type_service Wordlift_Entity_Type_Service |
|
31 | + */ |
|
32 | + public function __construct( $entity_service, $entity_type_service ) { |
|
33 | + |
|
34 | + $this->entity_service = $entity_service; |
|
35 | + |
|
36 | + $this->entity_type_service = $entity_type_service; |
|
37 | + |
|
38 | + // Takes a performance toll, do we really need it? |
|
39 | + // add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 4 ); |
|
40 | + } |
|
41 | + |
|
42 | + public function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy ) { |
|
43 | + |
|
44 | + if ( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME !== $taxonomy ) { |
|
45 | + return; |
|
46 | + } |
|
47 | + |
|
48 | + if ( count( $terms ) !== 1 ) { |
|
49 | + // Unable to determine which entity type or multiple entity types. |
|
50 | + return; |
|
51 | + } |
|
52 | + |
|
53 | + // This taxonomy is registered only for post, so the object id would |
|
54 | + // be the post id. |
|
55 | + $types = $this->entity_type_service->get_names( $object_id ); |
|
56 | + |
|
57 | + if ( count( $types ) !== 1 ) { |
|
58 | + // Unable to determine which entity type or multiple entity types. |
|
59 | + return; |
|
60 | + } |
|
61 | + |
|
62 | + // Check if set to Article or one of its descendants |
|
63 | + if ( ! in_array( $types[0], Jsonld_Article_Wrapper::$article_types, true ) ) { |
|
64 | + return; |
|
65 | + } |
|
66 | + |
|
67 | + // clear the labels. |
|
68 | + $this->entity_service->set_alternative_labels( $object_id, array() ); |
|
69 | + |
|
70 | + } |
|
71 | 71 | |
72 | 72 | } |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * @param $entity_service Wordlift_Entity_Service |
30 | 30 | * @param $entity_type_service Wordlift_Entity_Type_Service |
31 | 31 | */ |
32 | - public function __construct( $entity_service, $entity_type_service ) { |
|
32 | + public function __construct($entity_service, $entity_type_service) { |
|
33 | 33 | |
34 | 34 | $this->entity_service = $entity_service; |
35 | 35 | |
@@ -39,33 +39,33 @@ discard block |
||
39 | 39 | // add_action( 'set_object_terms', array( $this, 'set_object_terms' ), 10, 4 ); |
40 | 40 | } |
41 | 41 | |
42 | - public function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy ) { |
|
42 | + public function set_object_terms($object_id, $terms, $tt_ids, $taxonomy) { |
|
43 | 43 | |
44 | - if ( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME !== $taxonomy ) { |
|
44 | + if (Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME !== $taxonomy) { |
|
45 | 45 | return; |
46 | 46 | } |
47 | 47 | |
48 | - if ( count( $terms ) !== 1 ) { |
|
48 | + if (count($terms) !== 1) { |
|
49 | 49 | // Unable to determine which entity type or multiple entity types. |
50 | 50 | return; |
51 | 51 | } |
52 | 52 | |
53 | 53 | // This taxonomy is registered only for post, so the object id would |
54 | 54 | // be the post id. |
55 | - $types = $this->entity_type_service->get_names( $object_id ); |
|
55 | + $types = $this->entity_type_service->get_names($object_id); |
|
56 | 56 | |
57 | - if ( count( $types ) !== 1 ) { |
|
57 | + if (count($types) !== 1) { |
|
58 | 58 | // Unable to determine which entity type or multiple entity types. |
59 | 59 | return; |
60 | 60 | } |
61 | 61 | |
62 | 62 | // Check if set to Article or one of its descendants |
63 | - if ( ! in_array( $types[0], Jsonld_Article_Wrapper::$article_types, true ) ) { |
|
63 | + if ( ! in_array($types[0], Jsonld_Article_Wrapper::$article_types, true)) { |
|
64 | 64 | return; |
65 | 65 | } |
66 | 66 | |
67 | 67 | // clear the labels. |
68 | - $this->entity_service->set_alternative_labels( $object_id, array() ); |
|
68 | + $this->entity_service->set_alternative_labels($object_id, array()); |
|
69 | 69 | |
70 | 70 | } |
71 | 71 |
@@ -10,208 +10,208 @@ |
||
10 | 10 | |
11 | 11 | class Entity_Type_Setter { |
12 | 12 | |
13 | - const STARTER_PLAN = 'entity-types-starter'; |
|
14 | - |
|
15 | - const PROFESSIONAL_PLAN = 'entity-types-professional'; |
|
16 | - |
|
17 | - const BUSINESS_PLAN = 'entity-types-business'; |
|
18 | - |
|
19 | - public function __construct() { |
|
20 | - add_action( |
|
21 | - 'wl_feature__change__entity-types-starter', |
|
22 | - array( |
|
23 | - $this, |
|
24 | - 'wl_entity_types_feature_changed', |
|
25 | - ), |
|
26 | - 10, |
|
27 | - 3 |
|
28 | - ); |
|
29 | - add_action( |
|
30 | - 'wl_feature__change__entity-types-professional', |
|
31 | - array( |
|
32 | - $this, |
|
33 | - 'wl_entity_types_feature_changed', |
|
34 | - ), |
|
35 | - 10, |
|
36 | - 3 |
|
37 | - ); |
|
38 | - add_action( |
|
39 | - 'wl_feature__change__entity-types-business', |
|
40 | - array( |
|
41 | - $this, |
|
42 | - 'wl_entity_types_feature_changed', |
|
43 | - ), |
|
44 | - 10, |
|
45 | - 3 |
|
46 | - ); |
|
47 | - } |
|
48 | - |
|
49 | - public function wl_entity_types_feature_changed( $new_value, $old_value, $feature_slug ) { |
|
50 | - |
|
51 | - // If the entity types is not set by server, then return early. |
|
52 | - if ( ! $new_value ) { |
|
53 | - return; |
|
54 | - } |
|
55 | - |
|
56 | - $entity_types_data = self::get_entity_types_by_feature_flag( $feature_slug ); |
|
57 | - |
|
58 | - // If we dont have entity types returned, then dont reset the entity types, return early. |
|
59 | - if ( ! $entity_types_data ) { |
|
60 | - return; |
|
61 | - } |
|
62 | - // Repopulate the ones returned by package type. |
|
63 | - foreach ( $entity_types_data as $entity_type_data ) { |
|
64 | - |
|
65 | - $schema_label = $entity_type_data['label']; |
|
66 | - |
|
67 | - $term_exists = get_term_by( 'name', $schema_label, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) instanceof \WP_Term; |
|
68 | - |
|
69 | - if ( $term_exists ) { |
|
70 | - // Dont create term if it already exists. |
|
71 | - continue; |
|
72 | - } |
|
73 | - |
|
74 | - $term_data = wp_insert_term( |
|
75 | - $schema_label, |
|
76 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
77 | - array( |
|
78 | - 'description' => $entity_type_data['description'], |
|
79 | - 'slug' => $entity_type_data['slug'], |
|
80 | - ) |
|
81 | - ); |
|
82 | - |
|
83 | - $term_id = $term_data['term_id']; |
|
84 | - |
|
85 | - update_term_meta( $term_id, '_wl_uri', 'http://schema.org/' . $schema_label ); |
|
86 | - update_term_meta( $term_id, '_wl_name', $schema_label ); |
|
87 | - } |
|
88 | - |
|
89 | - } |
|
90 | - |
|
91 | - public static function get_starter_entity_types() { |
|
92 | - return array( |
|
93 | - array( |
|
94 | - 'label' => 'Person', |
|
95 | - 'description' => 'A person (or a music artist).', |
|
96 | - 'slug' => 'person', |
|
97 | - ), |
|
98 | - array( |
|
99 | - 'label' => 'Thing', |
|
100 | - 'description' => 'A generic thing (something that doesn\'t fit in the previous definitions.', |
|
101 | - 'slug' => 'thing', |
|
102 | - ), |
|
103 | - |
|
104 | - array( |
|
105 | - 'label' => 'Place', |
|
106 | - 'description' => 'A place.', |
|
107 | - 'slug' => 'place', |
|
108 | - ), |
|
109 | - |
|
110 | - array( |
|
111 | - 'label' => 'CreativeWork', |
|
112 | - 'description' => 'A creative work (or a Music Album).', |
|
113 | - 'slug' => 'creative-work', |
|
114 | - ), |
|
115 | - array( |
|
116 | - 'label' => 'Organization', |
|
117 | - 'description' => 'An organization, including a government or a newspaper.', |
|
118 | - 'slug' => 'organization', |
|
119 | - ), |
|
120 | - |
|
121 | - array( |
|
122 | - 'label' => 'Article', |
|
123 | - 'description' => 'An article, such as a news article or piece of investigative report. Newspapers and magazines have articles of many different types and this is intended to cover them all.', |
|
124 | - 'slug' => 'article', |
|
125 | - ), |
|
126 | - |
|
127 | - array( |
|
128 | - 'label' => 'WebSite', |
|
129 | - 'description' => 'A WebSite is a set of related web pages and other items typically served from a single web domain and accessible via URLs.', |
|
130 | - 'slug' => 'web-site', |
|
131 | - ), |
|
132 | - |
|
133 | - array( |
|
134 | - 'label' => 'NewsArticle', |
|
135 | - 'description' => 'A NewsArticle is an article whose content reports news, or provides background context and supporting materials for understanding the news.', |
|
136 | - 'slug' => 'news-article', |
|
137 | - ), |
|
138 | - |
|
139 | - array( |
|
140 | - 'label' => 'AboutPage', |
|
141 | - 'description' => 'An About page.', |
|
142 | - 'slug' => 'about-page', |
|
143 | - ), |
|
144 | - |
|
145 | - array( |
|
146 | - 'label' => 'ContactPage', |
|
147 | - 'description' => 'A Contact Page.', |
|
148 | - 'slug' => 'contact-page', |
|
149 | - ), |
|
150 | - |
|
151 | - ); |
|
152 | - } |
|
153 | - |
|
154 | - public static function get_professional_entity_types() { |
|
155 | - return array( |
|
156 | - |
|
157 | - array( |
|
158 | - 'label' => 'FAQPage', |
|
159 | - 'description' => 'A FAQPage is a WebPage presenting one or more "Frequently asked questions".', |
|
160 | - 'slug' => 'faq-page', |
|
161 | - ), |
|
162 | - array( |
|
163 | - 'label' => 'LocalBusiness', |
|
164 | - 'description' => 'A particular physical business or branch of an organization. Examples of LocalBusiness include a restaurant, a particular branch of a restaurant chain, a branch of a bank, a medical practice, a club, a bowling alley, etc.', |
|
165 | - 'slug' => 'local-business', |
|
166 | - |
|
167 | - ), |
|
168 | - array( |
|
169 | - 'label' => 'Recipe', |
|
170 | - 'description' => 'A recipe', |
|
171 | - 'slug' => 'recipe', |
|
172 | - ), |
|
173 | - array( |
|
174 | - 'label' => 'PodcastEpisode', |
|
175 | - 'description' => 'A single episode of a podcast series.', |
|
176 | - 'slug' => 'podcast-episode', |
|
177 | - ), |
|
178 | - array( |
|
179 | - 'label' => 'Course', |
|
180 | - 'description' => 'A description of an educational course which may be offered as distinct instances at which take place at different times or take place at different locations, or be offered through different media or modes of study.', |
|
181 | - 'slug' => 'course', |
|
182 | - ), |
|
183 | - array( |
|
184 | - 'label' => 'Event', |
|
185 | - 'description' => 'An event happening at a certain time and location, such as a concert, lecture, or festival.', |
|
186 | - 'slug' => 'event', |
|
187 | - ), |
|
188 | - array( |
|
189 | - 'label' => 'Review', |
|
190 | - 'description' => 'A review of an item - for example, of a restaurant, movie, or store.', |
|
191 | - 'slug' => 'review', |
|
192 | - ), |
|
193 | - |
|
194 | - ); |
|
195 | - } |
|
196 | - |
|
197 | - private static function get_entity_types_by_feature_flag( $package_type ) { |
|
198 | - |
|
199 | - switch ( $package_type ) { |
|
200 | - case self::STARTER_PLAN: |
|
201 | - return self::get_starter_entity_types(); |
|
202 | - case self::BUSINESS_PLAN: |
|
203 | - case self::PROFESSIONAL_PLAN: |
|
204 | - // We return same entity types for professional and business plans. |
|
205 | - // Business plan should have sync schema ui feature enabled, to sync all the entity types. |
|
206 | - return array_merge( |
|
207 | - self::get_starter_entity_types(), |
|
208 | - self::get_professional_entity_types() |
|
209 | - ); |
|
210 | - default: |
|
211 | - return array(); |
|
212 | - |
|
213 | - } |
|
214 | - |
|
215 | - } |
|
13 | + const STARTER_PLAN = 'entity-types-starter'; |
|
14 | + |
|
15 | + const PROFESSIONAL_PLAN = 'entity-types-professional'; |
|
16 | + |
|
17 | + const BUSINESS_PLAN = 'entity-types-business'; |
|
18 | + |
|
19 | + public function __construct() { |
|
20 | + add_action( |
|
21 | + 'wl_feature__change__entity-types-starter', |
|
22 | + array( |
|
23 | + $this, |
|
24 | + 'wl_entity_types_feature_changed', |
|
25 | + ), |
|
26 | + 10, |
|
27 | + 3 |
|
28 | + ); |
|
29 | + add_action( |
|
30 | + 'wl_feature__change__entity-types-professional', |
|
31 | + array( |
|
32 | + $this, |
|
33 | + 'wl_entity_types_feature_changed', |
|
34 | + ), |
|
35 | + 10, |
|
36 | + 3 |
|
37 | + ); |
|
38 | + add_action( |
|
39 | + 'wl_feature__change__entity-types-business', |
|
40 | + array( |
|
41 | + $this, |
|
42 | + 'wl_entity_types_feature_changed', |
|
43 | + ), |
|
44 | + 10, |
|
45 | + 3 |
|
46 | + ); |
|
47 | + } |
|
48 | + |
|
49 | + public function wl_entity_types_feature_changed( $new_value, $old_value, $feature_slug ) { |
|
50 | + |
|
51 | + // If the entity types is not set by server, then return early. |
|
52 | + if ( ! $new_value ) { |
|
53 | + return; |
|
54 | + } |
|
55 | + |
|
56 | + $entity_types_data = self::get_entity_types_by_feature_flag( $feature_slug ); |
|
57 | + |
|
58 | + // If we dont have entity types returned, then dont reset the entity types, return early. |
|
59 | + if ( ! $entity_types_data ) { |
|
60 | + return; |
|
61 | + } |
|
62 | + // Repopulate the ones returned by package type. |
|
63 | + foreach ( $entity_types_data as $entity_type_data ) { |
|
64 | + |
|
65 | + $schema_label = $entity_type_data['label']; |
|
66 | + |
|
67 | + $term_exists = get_term_by( 'name', $schema_label, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) instanceof \WP_Term; |
|
68 | + |
|
69 | + if ( $term_exists ) { |
|
70 | + // Dont create term if it already exists. |
|
71 | + continue; |
|
72 | + } |
|
73 | + |
|
74 | + $term_data = wp_insert_term( |
|
75 | + $schema_label, |
|
76 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
77 | + array( |
|
78 | + 'description' => $entity_type_data['description'], |
|
79 | + 'slug' => $entity_type_data['slug'], |
|
80 | + ) |
|
81 | + ); |
|
82 | + |
|
83 | + $term_id = $term_data['term_id']; |
|
84 | + |
|
85 | + update_term_meta( $term_id, '_wl_uri', 'http://schema.org/' . $schema_label ); |
|
86 | + update_term_meta( $term_id, '_wl_name', $schema_label ); |
|
87 | + } |
|
88 | + |
|
89 | + } |
|
90 | + |
|
91 | + public static function get_starter_entity_types() { |
|
92 | + return array( |
|
93 | + array( |
|
94 | + 'label' => 'Person', |
|
95 | + 'description' => 'A person (or a music artist).', |
|
96 | + 'slug' => 'person', |
|
97 | + ), |
|
98 | + array( |
|
99 | + 'label' => 'Thing', |
|
100 | + 'description' => 'A generic thing (something that doesn\'t fit in the previous definitions.', |
|
101 | + 'slug' => 'thing', |
|
102 | + ), |
|
103 | + |
|
104 | + array( |
|
105 | + 'label' => 'Place', |
|
106 | + 'description' => 'A place.', |
|
107 | + 'slug' => 'place', |
|
108 | + ), |
|
109 | + |
|
110 | + array( |
|
111 | + 'label' => 'CreativeWork', |
|
112 | + 'description' => 'A creative work (or a Music Album).', |
|
113 | + 'slug' => 'creative-work', |
|
114 | + ), |
|
115 | + array( |
|
116 | + 'label' => 'Organization', |
|
117 | + 'description' => 'An organization, including a government or a newspaper.', |
|
118 | + 'slug' => 'organization', |
|
119 | + ), |
|
120 | + |
|
121 | + array( |
|
122 | + 'label' => 'Article', |
|
123 | + 'description' => 'An article, such as a news article or piece of investigative report. Newspapers and magazines have articles of many different types and this is intended to cover them all.', |
|
124 | + 'slug' => 'article', |
|
125 | + ), |
|
126 | + |
|
127 | + array( |
|
128 | + 'label' => 'WebSite', |
|
129 | + 'description' => 'A WebSite is a set of related web pages and other items typically served from a single web domain and accessible via URLs.', |
|
130 | + 'slug' => 'web-site', |
|
131 | + ), |
|
132 | + |
|
133 | + array( |
|
134 | + 'label' => 'NewsArticle', |
|
135 | + 'description' => 'A NewsArticle is an article whose content reports news, or provides background context and supporting materials for understanding the news.', |
|
136 | + 'slug' => 'news-article', |
|
137 | + ), |
|
138 | + |
|
139 | + array( |
|
140 | + 'label' => 'AboutPage', |
|
141 | + 'description' => 'An About page.', |
|
142 | + 'slug' => 'about-page', |
|
143 | + ), |
|
144 | + |
|
145 | + array( |
|
146 | + 'label' => 'ContactPage', |
|
147 | + 'description' => 'A Contact Page.', |
|
148 | + 'slug' => 'contact-page', |
|
149 | + ), |
|
150 | + |
|
151 | + ); |
|
152 | + } |
|
153 | + |
|
154 | + public static function get_professional_entity_types() { |
|
155 | + return array( |
|
156 | + |
|
157 | + array( |
|
158 | + 'label' => 'FAQPage', |
|
159 | + 'description' => 'A FAQPage is a WebPage presenting one or more "Frequently asked questions".', |
|
160 | + 'slug' => 'faq-page', |
|
161 | + ), |
|
162 | + array( |
|
163 | + 'label' => 'LocalBusiness', |
|
164 | + 'description' => 'A particular physical business or branch of an organization. Examples of LocalBusiness include a restaurant, a particular branch of a restaurant chain, a branch of a bank, a medical practice, a club, a bowling alley, etc.', |
|
165 | + 'slug' => 'local-business', |
|
166 | + |
|
167 | + ), |
|
168 | + array( |
|
169 | + 'label' => 'Recipe', |
|
170 | + 'description' => 'A recipe', |
|
171 | + 'slug' => 'recipe', |
|
172 | + ), |
|
173 | + array( |
|
174 | + 'label' => 'PodcastEpisode', |
|
175 | + 'description' => 'A single episode of a podcast series.', |
|
176 | + 'slug' => 'podcast-episode', |
|
177 | + ), |
|
178 | + array( |
|
179 | + 'label' => 'Course', |
|
180 | + 'description' => 'A description of an educational course which may be offered as distinct instances at which take place at different times or take place at different locations, or be offered through different media or modes of study.', |
|
181 | + 'slug' => 'course', |
|
182 | + ), |
|
183 | + array( |
|
184 | + 'label' => 'Event', |
|
185 | + 'description' => 'An event happening at a certain time and location, such as a concert, lecture, or festival.', |
|
186 | + 'slug' => 'event', |
|
187 | + ), |
|
188 | + array( |
|
189 | + 'label' => 'Review', |
|
190 | + 'description' => 'A review of an item - for example, of a restaurant, movie, or store.', |
|
191 | + 'slug' => 'review', |
|
192 | + ), |
|
193 | + |
|
194 | + ); |
|
195 | + } |
|
196 | + |
|
197 | + private static function get_entity_types_by_feature_flag( $package_type ) { |
|
198 | + |
|
199 | + switch ( $package_type ) { |
|
200 | + case self::STARTER_PLAN: |
|
201 | + return self::get_starter_entity_types(); |
|
202 | + case self::BUSINESS_PLAN: |
|
203 | + case self::PROFESSIONAL_PLAN: |
|
204 | + // We return same entity types for professional and business plans. |
|
205 | + // Business plan should have sync schema ui feature enabled, to sync all the entity types. |
|
206 | + return array_merge( |
|
207 | + self::get_starter_entity_types(), |
|
208 | + self::get_professional_entity_types() |
|
209 | + ); |
|
210 | + default: |
|
211 | + return array(); |
|
212 | + |
|
213 | + } |
|
214 | + |
|
215 | + } |
|
216 | 216 | |
217 | 217 | } |
@@ -46,27 +46,27 @@ discard block |
||
46 | 46 | ); |
47 | 47 | } |
48 | 48 | |
49 | - public function wl_entity_types_feature_changed( $new_value, $old_value, $feature_slug ) { |
|
49 | + public function wl_entity_types_feature_changed($new_value, $old_value, $feature_slug) { |
|
50 | 50 | |
51 | 51 | // If the entity types is not set by server, then return early. |
52 | - if ( ! $new_value ) { |
|
52 | + if ( ! $new_value) { |
|
53 | 53 | return; |
54 | 54 | } |
55 | 55 | |
56 | - $entity_types_data = self::get_entity_types_by_feature_flag( $feature_slug ); |
|
56 | + $entity_types_data = self::get_entity_types_by_feature_flag($feature_slug); |
|
57 | 57 | |
58 | 58 | // If we dont have entity types returned, then dont reset the entity types, return early. |
59 | - if ( ! $entity_types_data ) { |
|
59 | + if ( ! $entity_types_data) { |
|
60 | 60 | return; |
61 | 61 | } |
62 | 62 | // Repopulate the ones returned by package type. |
63 | - foreach ( $entity_types_data as $entity_type_data ) { |
|
63 | + foreach ($entity_types_data as $entity_type_data) { |
|
64 | 64 | |
65 | 65 | $schema_label = $entity_type_data['label']; |
66 | 66 | |
67 | - $term_exists = get_term_by( 'name', $schema_label, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) instanceof \WP_Term; |
|
67 | + $term_exists = get_term_by('name', $schema_label, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME) instanceof \WP_Term; |
|
68 | 68 | |
69 | - if ( $term_exists ) { |
|
69 | + if ($term_exists) { |
|
70 | 70 | // Dont create term if it already exists. |
71 | 71 | continue; |
72 | 72 | } |
@@ -82,8 +82,8 @@ discard block |
||
82 | 82 | |
83 | 83 | $term_id = $term_data['term_id']; |
84 | 84 | |
85 | - update_term_meta( $term_id, '_wl_uri', 'http://schema.org/' . $schema_label ); |
|
86 | - update_term_meta( $term_id, '_wl_name', $schema_label ); |
|
85 | + update_term_meta($term_id, '_wl_uri', 'http://schema.org/'.$schema_label); |
|
86 | + update_term_meta($term_id, '_wl_name', $schema_label); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | } |
@@ -194,9 +194,9 @@ discard block |
||
194 | 194 | ); |
195 | 195 | } |
196 | 196 | |
197 | - private static function get_entity_types_by_feature_flag( $package_type ) { |
|
197 | + private static function get_entity_types_by_feature_flag($package_type) { |
|
198 | 198 | |
199 | - switch ( $package_type ) { |
|
199 | + switch ($package_type) { |
|
200 | 200 | case self::STARTER_PLAN: |
201 | 201 | return self::get_starter_entity_types(); |
202 | 202 | case self::BUSINESS_PLAN: |
@@ -7,152 +7,152 @@ |
||
7 | 7 | |
8 | 8 | class Default_Api_Service implements Api_Service, Api_Service_Ext { |
9 | 9 | |
10 | - /** |
|
11 | - * @var string |
|
12 | - */ |
|
13 | - private $wordlift_key; |
|
14 | - /** |
|
15 | - * @var int |
|
16 | - */ |
|
17 | - private $timeout; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - private $user_agent; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - private $headers; |
|
28 | - /** |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - private $base_url; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var \Wordlift_Log_Service |
|
35 | - */ |
|
36 | - private $log; |
|
37 | - |
|
38 | - /** |
|
39 | - * Default_Api_Service constructor. |
|
40 | - * |
|
41 | - * @param string $base_url |
|
42 | - * @param int $timeout |
|
43 | - * @param string $user_agent |
|
44 | - * @param string $wordlift_key |
|
45 | - */ |
|
46 | - protected function __construct( $base_url, $timeout, $user_agent, $wordlift_key ) { |
|
47 | - |
|
48 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
49 | - |
|
50 | - $this->base_url = untrailingslashit( $base_url ); |
|
51 | - $this->timeout = $timeout; |
|
52 | - $this->user_agent = $user_agent; |
|
53 | - $this->wordlift_key = $wordlift_key; |
|
54 | - |
|
55 | - $this->headers = array( |
|
56 | - 'Content-Type' => 'application/json', |
|
57 | - 'Authorization' => "Key $wordlift_key", |
|
58 | - 'Expect' => '', |
|
59 | - ); |
|
60 | - |
|
61 | - self::$instance = $this; |
|
62 | - } |
|
63 | - |
|
64 | - private static $instance; |
|
65 | - |
|
66 | - /** |
|
67 | - * @return Default_Api_Service |
|
68 | - */ |
|
69 | - public static function get_instance() { |
|
70 | - if ( ! isset( self::$instance ) ) { |
|
71 | - self::$instance = new self( |
|
72 | - apply_filters( 'wl_api_base_url', WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE ), |
|
73 | - 60, |
|
74 | - User_Agent::get_user_agent(), |
|
75 | - Wordlift_Configuration_Service::get_instance()->get_key() |
|
76 | - ); |
|
77 | - } |
|
78 | - |
|
79 | - return self::$instance; |
|
80 | - } |
|
81 | - |
|
82 | - public function request( $method, $path, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array() ) { |
|
83 | - |
|
84 | - // Get the timeout for this request. |
|
85 | - $request_timeout = isset( $timeout ) ? $timeout : $this->timeout; |
|
86 | - |
|
87 | - // Set the time limit if lesser than our request timeout. |
|
88 | - $max_execution_time = ini_get( 'max_execution_time' ); |
|
89 | - if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) |
|
90 | - && ( 0 !== intval( $max_execution_time ) ) |
|
91 | - && ( $max_execution_time < $request_timeout ) ) { |
|
92 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
93 | - @set_time_limit( $request_timeout ); |
|
94 | - } |
|
95 | - |
|
96 | - $request_url = $this->base_url . $path; |
|
97 | - // Create the request args in the following order: |
|
98 | - // 1. use `$args` as base if provided. |
|
99 | - // 2. set the custom timeout if provided. |
|
100 | - // 3. set the custom user-agent if provided. |
|
101 | - // 4. merge the API headers to the provided headers. |
|
102 | - // 5. add the body. |
|
103 | - // |
|
104 | - // In this way the user can fully control the request if wanted (using `$args`) and we can add our defaults. |
|
105 | - $request_args = apply_filters( |
|
106 | - 'wl_api_service__request', |
|
107 | - $args + array( |
|
108 | - 'method' => $method, |
|
109 | - 'timeout' => $request_timeout, |
|
110 | - 'user-agent' => isset( $user_agent ) ? $user_agent : $this->user_agent, |
|
111 | - 'headers' => $headers + $this->headers + Api_Headers_Service::get_instance()->get_wp_headers(), |
|
112 | - 'body' => $body, |
|
113 | - ) |
|
114 | - ); |
|
115 | - |
|
116 | - /** |
|
117 | - * Allow 3rd parties to process the response. |
|
118 | - */ |
|
119 | - $response = apply_filters( |
|
120 | - 'wl_api_service__response', |
|
121 | - wp_remote_request( $request_url, $request_args ), |
|
122 | - $request_url, |
|
123 | - $request_args |
|
124 | - ); |
|
125 | - |
|
126 | - if ( defined( 'WL_DEBUG' ) && WL_DEBUG ) { |
|
127 | - $this->log->trace( |
|
128 | - "=== REQUEST ===========================\n" |
|
129 | - . "=== URL: $request_url ===========================\n" |
|
130 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
|
131 | - . var_export( $request_args, true ) |
|
132 | - . "=== RESPONSE ===========================\n" |
|
133 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
|
134 | - . var_export( $response, true ) |
|
135 | - ); |
|
136 | - } |
|
137 | - |
|
138 | - return new Response( $response ); |
|
139 | - } |
|
140 | - |
|
141 | - public function get( $path, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array() ) { |
|
142 | - |
|
143 | - return $this->request( 'GET', $path, $headers, $body, $timeout, $user_agent, $args ); |
|
144 | - } |
|
145 | - |
|
146 | - public function get_base_url() { |
|
147 | - return $this->base_url; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @return Me_Response |
|
152 | - * @throws Exception when an error occurs. |
|
153 | - */ |
|
154 | - public function me() { |
|
155 | - return json_decode( $this->get( '/me' )->get_body() ); |
|
156 | - } |
|
10 | + /** |
|
11 | + * @var string |
|
12 | + */ |
|
13 | + private $wordlift_key; |
|
14 | + /** |
|
15 | + * @var int |
|
16 | + */ |
|
17 | + private $timeout; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + private $user_agent; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + private $headers; |
|
28 | + /** |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + private $base_url; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var \Wordlift_Log_Service |
|
35 | + */ |
|
36 | + private $log; |
|
37 | + |
|
38 | + /** |
|
39 | + * Default_Api_Service constructor. |
|
40 | + * |
|
41 | + * @param string $base_url |
|
42 | + * @param int $timeout |
|
43 | + * @param string $user_agent |
|
44 | + * @param string $wordlift_key |
|
45 | + */ |
|
46 | + protected function __construct( $base_url, $timeout, $user_agent, $wordlift_key ) { |
|
47 | + |
|
48 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
49 | + |
|
50 | + $this->base_url = untrailingslashit( $base_url ); |
|
51 | + $this->timeout = $timeout; |
|
52 | + $this->user_agent = $user_agent; |
|
53 | + $this->wordlift_key = $wordlift_key; |
|
54 | + |
|
55 | + $this->headers = array( |
|
56 | + 'Content-Type' => 'application/json', |
|
57 | + 'Authorization' => "Key $wordlift_key", |
|
58 | + 'Expect' => '', |
|
59 | + ); |
|
60 | + |
|
61 | + self::$instance = $this; |
|
62 | + } |
|
63 | + |
|
64 | + private static $instance; |
|
65 | + |
|
66 | + /** |
|
67 | + * @return Default_Api_Service |
|
68 | + */ |
|
69 | + public static function get_instance() { |
|
70 | + if ( ! isset( self::$instance ) ) { |
|
71 | + self::$instance = new self( |
|
72 | + apply_filters( 'wl_api_base_url', WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE ), |
|
73 | + 60, |
|
74 | + User_Agent::get_user_agent(), |
|
75 | + Wordlift_Configuration_Service::get_instance()->get_key() |
|
76 | + ); |
|
77 | + } |
|
78 | + |
|
79 | + return self::$instance; |
|
80 | + } |
|
81 | + |
|
82 | + public function request( $method, $path, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array() ) { |
|
83 | + |
|
84 | + // Get the timeout for this request. |
|
85 | + $request_timeout = isset( $timeout ) ? $timeout : $this->timeout; |
|
86 | + |
|
87 | + // Set the time limit if lesser than our request timeout. |
|
88 | + $max_execution_time = ini_get( 'max_execution_time' ); |
|
89 | + if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) |
|
90 | + && ( 0 !== intval( $max_execution_time ) ) |
|
91 | + && ( $max_execution_time < $request_timeout ) ) { |
|
92 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
93 | + @set_time_limit( $request_timeout ); |
|
94 | + } |
|
95 | + |
|
96 | + $request_url = $this->base_url . $path; |
|
97 | + // Create the request args in the following order: |
|
98 | + // 1. use `$args` as base if provided. |
|
99 | + // 2. set the custom timeout if provided. |
|
100 | + // 3. set the custom user-agent if provided. |
|
101 | + // 4. merge the API headers to the provided headers. |
|
102 | + // 5. add the body. |
|
103 | + // |
|
104 | + // In this way the user can fully control the request if wanted (using `$args`) and we can add our defaults. |
|
105 | + $request_args = apply_filters( |
|
106 | + 'wl_api_service__request', |
|
107 | + $args + array( |
|
108 | + 'method' => $method, |
|
109 | + 'timeout' => $request_timeout, |
|
110 | + 'user-agent' => isset( $user_agent ) ? $user_agent : $this->user_agent, |
|
111 | + 'headers' => $headers + $this->headers + Api_Headers_Service::get_instance()->get_wp_headers(), |
|
112 | + 'body' => $body, |
|
113 | + ) |
|
114 | + ); |
|
115 | + |
|
116 | + /** |
|
117 | + * Allow 3rd parties to process the response. |
|
118 | + */ |
|
119 | + $response = apply_filters( |
|
120 | + 'wl_api_service__response', |
|
121 | + wp_remote_request( $request_url, $request_args ), |
|
122 | + $request_url, |
|
123 | + $request_args |
|
124 | + ); |
|
125 | + |
|
126 | + if ( defined( 'WL_DEBUG' ) && WL_DEBUG ) { |
|
127 | + $this->log->trace( |
|
128 | + "=== REQUEST ===========================\n" |
|
129 | + . "=== URL: $request_url ===========================\n" |
|
130 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
|
131 | + . var_export( $request_args, true ) |
|
132 | + . "=== RESPONSE ===========================\n" |
|
133 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
|
134 | + . var_export( $response, true ) |
|
135 | + ); |
|
136 | + } |
|
137 | + |
|
138 | + return new Response( $response ); |
|
139 | + } |
|
140 | + |
|
141 | + public function get( $path, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array() ) { |
|
142 | + |
|
143 | + return $this->request( 'GET', $path, $headers, $body, $timeout, $user_agent, $args ); |
|
144 | + } |
|
145 | + |
|
146 | + public function get_base_url() { |
|
147 | + return $this->base_url; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @return Me_Response |
|
152 | + * @throws Exception when an error occurs. |
|
153 | + */ |
|
154 | + public function me() { |
|
155 | + return json_decode( $this->get( '/me' )->get_body() ); |
|
156 | + } |
|
157 | 157 | |
158 | 158 | } |
@@ -43,11 +43,11 @@ discard block |
||
43 | 43 | * @param string $user_agent |
44 | 44 | * @param string $wordlift_key |
45 | 45 | */ |
46 | - protected function __construct( $base_url, $timeout, $user_agent, $wordlift_key ) { |
|
46 | + protected function __construct($base_url, $timeout, $user_agent, $wordlift_key) { |
|
47 | 47 | |
48 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
48 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
49 | 49 | |
50 | - $this->base_url = untrailingslashit( $base_url ); |
|
50 | + $this->base_url = untrailingslashit($base_url); |
|
51 | 51 | $this->timeout = $timeout; |
52 | 52 | $this->user_agent = $user_agent; |
53 | 53 | $this->wordlift_key = $wordlift_key; |
@@ -67,9 +67,9 @@ discard block |
||
67 | 67 | * @return Default_Api_Service |
68 | 68 | */ |
69 | 69 | public static function get_instance() { |
70 | - if ( ! isset( self::$instance ) ) { |
|
70 | + if ( ! isset(self::$instance)) { |
|
71 | 71 | self::$instance = new self( |
72 | - apply_filters( 'wl_api_base_url', WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE ), |
|
72 | + apply_filters('wl_api_base_url', WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE), |
|
73 | 73 | 60, |
74 | 74 | User_Agent::get_user_agent(), |
75 | 75 | Wordlift_Configuration_Service::get_instance()->get_key() |
@@ -79,21 +79,21 @@ discard block |
||
79 | 79 | return self::$instance; |
80 | 80 | } |
81 | 81 | |
82 | - public function request( $method, $path, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array() ) { |
|
82 | + public function request($method, $path, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array()) { |
|
83 | 83 | |
84 | 84 | // Get the timeout for this request. |
85 | - $request_timeout = isset( $timeout ) ? $timeout : $this->timeout; |
|
85 | + $request_timeout = isset($timeout) ? $timeout : $this->timeout; |
|
86 | 86 | |
87 | 87 | // Set the time limit if lesser than our request timeout. |
88 | - $max_execution_time = ini_get( 'max_execution_time' ); |
|
89 | - if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) |
|
90 | - && ( 0 !== intval( $max_execution_time ) ) |
|
91 | - && ( $max_execution_time < $request_timeout ) ) { |
|
88 | + $max_execution_time = ini_get('max_execution_time'); |
|
89 | + if ( ! (defined('WP_CLI') && WP_CLI) |
|
90 | + && (0 !== intval($max_execution_time)) |
|
91 | + && ($max_execution_time < $request_timeout)) { |
|
92 | 92 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
93 | - @set_time_limit( $request_timeout ); |
|
93 | + @set_time_limit($request_timeout); |
|
94 | 94 | } |
95 | 95 | |
96 | - $request_url = $this->base_url . $path; |
|
96 | + $request_url = $this->base_url.$path; |
|
97 | 97 | // Create the request args in the following order: |
98 | 98 | // 1. use `$args` as base if provided. |
99 | 99 | // 2. set the custom timeout if provided. |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | $args + array( |
108 | 108 | 'method' => $method, |
109 | 109 | 'timeout' => $request_timeout, |
110 | - 'user-agent' => isset( $user_agent ) ? $user_agent : $this->user_agent, |
|
110 | + 'user-agent' => isset($user_agent) ? $user_agent : $this->user_agent, |
|
111 | 111 | 'headers' => $headers + $this->headers + Api_Headers_Service::get_instance()->get_wp_headers(), |
112 | 112 | 'body' => $body, |
113 | 113 | ) |
@@ -118,29 +118,29 @@ discard block |
||
118 | 118 | */ |
119 | 119 | $response = apply_filters( |
120 | 120 | 'wl_api_service__response', |
121 | - wp_remote_request( $request_url, $request_args ), |
|
121 | + wp_remote_request($request_url, $request_args), |
|
122 | 122 | $request_url, |
123 | 123 | $request_args |
124 | 124 | ); |
125 | 125 | |
126 | - if ( defined( 'WL_DEBUG' ) && WL_DEBUG ) { |
|
126 | + if (defined('WL_DEBUG') && WL_DEBUG) { |
|
127 | 127 | $this->log->trace( |
128 | 128 | "=== REQUEST ===========================\n" |
129 | 129 | . "=== URL: $request_url ===========================\n" |
130 | 130 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
131 | - . var_export( $request_args, true ) |
|
131 | + . var_export($request_args, true) |
|
132 | 132 | . "=== RESPONSE ===========================\n" |
133 | 133 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
134 | - . var_export( $response, true ) |
|
134 | + . var_export($response, true) |
|
135 | 135 | ); |
136 | 136 | } |
137 | 137 | |
138 | - return new Response( $response ); |
|
138 | + return new Response($response); |
|
139 | 139 | } |
140 | 140 | |
141 | - public function get( $path, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array() ) { |
|
141 | + public function get($path, $headers = array(), $body = null, $timeout = null, $user_agent = null, $args = array()) { |
|
142 | 142 | |
143 | - return $this->request( 'GET', $path, $headers, $body, $timeout, $user_agent, $args ); |
|
143 | + return $this->request('GET', $path, $headers, $body, $timeout, $user_agent, $args); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | public function get_base_url() { |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | * @throws Exception when an error occurs. |
153 | 153 | */ |
154 | 154 | public function me() { |
155 | - return json_decode( $this->get( '/me' )->get_body() ); |
|
155 | + return json_decode($this->get('/me')->get_body()); |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | } |
@@ -4,33 +4,33 @@ |
||
4 | 4 | |
5 | 5 | class Api_Headers_Service { |
6 | 6 | |
7 | - private static $instance = null; |
|
8 | - |
|
9 | - protected function __construct() { |
|
10 | - |
|
11 | - } |
|
12 | - |
|
13 | - /** |
|
14 | - * This function is used to append WordPress endpoint data to every request made. |
|
15 | - * |
|
16 | - * @return array |
|
17 | - */ |
|
18 | - public function get_wp_headers() { |
|
19 | - return array( |
|
20 | - 'X-Wordlift-Plugin-Wp-Admin' => untrailingslashit( get_admin_url() ), |
|
21 | - 'X-Wordlift-Plugin-Wp-Json' => untrailingslashit( get_rest_url() ), |
|
22 | - ); |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * @return Api_Headers_Service |
|
27 | - */ |
|
28 | - public static function get_instance() { |
|
29 | - if ( null === self::$instance ) { |
|
30 | - self::$instance = new Api_Headers_Service(); |
|
31 | - } |
|
32 | - |
|
33 | - return self::$instance; |
|
34 | - } |
|
7 | + private static $instance = null; |
|
8 | + |
|
9 | + protected function __construct() { |
|
10 | + |
|
11 | + } |
|
12 | + |
|
13 | + /** |
|
14 | + * This function is used to append WordPress endpoint data to every request made. |
|
15 | + * |
|
16 | + * @return array |
|
17 | + */ |
|
18 | + public function get_wp_headers() { |
|
19 | + return array( |
|
20 | + 'X-Wordlift-Plugin-Wp-Admin' => untrailingslashit( get_admin_url() ), |
|
21 | + 'X-Wordlift-Plugin-Wp-Json' => untrailingslashit( get_rest_url() ), |
|
22 | + ); |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * @return Api_Headers_Service |
|
27 | + */ |
|
28 | + public static function get_instance() { |
|
29 | + if ( null === self::$instance ) { |
|
30 | + self::$instance = new Api_Headers_Service(); |
|
31 | + } |
|
32 | + |
|
33 | + return self::$instance; |
|
34 | + } |
|
35 | 35 | |
36 | 36 | } |
@@ -17,8 +17,8 @@ discard block |
||
17 | 17 | */ |
18 | 18 | public function get_wp_headers() { |
19 | 19 | return array( |
20 | - 'X-Wordlift-Plugin-Wp-Admin' => untrailingslashit( get_admin_url() ), |
|
21 | - 'X-Wordlift-Plugin-Wp-Json' => untrailingslashit( get_rest_url() ), |
|
20 | + 'X-Wordlift-Plugin-Wp-Admin' => untrailingslashit(get_admin_url()), |
|
21 | + 'X-Wordlift-Plugin-Wp-Json' => untrailingslashit(get_rest_url()), |
|
22 | 22 | ); |
23 | 23 | } |
24 | 24 | |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | * @return Api_Headers_Service |
27 | 27 | */ |
28 | 28 | public static function get_instance() { |
29 | - if ( null === self::$instance ) { |
|
29 | + if (null === self::$instance) { |
|
30 | 30 | self::$instance = new Api_Headers_Service(); |
31 | 31 | } |
32 | 32 |