PluginProbe
Writesonic / 1.0.5
Writesonic v1.0.5
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.5 1.0.6 2.0.0
writesonic / writesonic.php

writesonic.php in Writesonic 1.0.5, at writesonic.php

917 lines 26.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Name: Writesonic
5 * Description: Writesonic WordPress plugin
6 * Version: 1.0.5
7 * Author: <a href="https://writesonic.com/">Writesonic</a>
8 * Author URI: https://writesonic.com/
9 * Text Domain: writesonic
10 */
11
12 if (!defined('ABSPATH')) {
13 exit; // Exit if accessed directly.
14 }
15
16 const WRITESONIC_API_KEY_OPTION = 'writesonic_api_key';
17
18 const WRITESONIC_CONNECT_URL = 'https://app.writesonic.com/wordpress-authentication/';
19 // const WRITESONIC_CONNECT_URL = 'http://localhost:3000/wordpress-authentication/';
20 // const WRITESONIC_CONNECT_URL = 'https://staging.writesonic.com/wordpress-authentication/';
21
22 const WRITESONIC_CHECK_AUTH_URL = 'https://api.writesonic.com/v1/thirdparty/wordpress-org-authorization-status';
23 // const WRITESONIC_CHECK_AUTH_URL = 'http://localhost:8081/v1/thirdparty/wordpress-org-authorization-status';
24 // const WRITESONIC_CHECK_AUTH_URL = 'https://dev-backend.writesonic.com/v1/thirdparty/wordpress-org-authorization-status';
25
26 if (!class_exists('WPM_Writesonic_Integration')) {
27 class WPM_Writesonic_Integration
28 {
29 /**
30 * Plugin init, filters, hooks
31 */
32 public static function init()
33 {
34 // Initialize option with empty string
35 add_option(WRITESONIC_API_KEY_OPTION, '');
36
37 add_action('admin_menu', array('WPM_Writesonic_Integration', 'create_settings_menu'));
38 add_action('rest_api_init', array('WPM_Writesonic_Integration', 'register_api_endpoints'));
39 register_deactivation_hook(__FILE__, array('WPM_Writesonic_Integration', 'deactivation'));
40 add_action('admin_init', array('WPM_Writesonic_Integration', 'register_settings'));
41
42 // Get users w/o posts published
43 add_filter('rest_user_query', array('WPM_Writesonic_Integration', 'remove_has_published_posts_from_wp_api_user_query'), 10, 2);
44
45 // Force custom posts to be visible in REST API
46 add_filter('register_post_type_args', array('WPM_Writesonic_Integration', 'custom_post_types_show_in_rest_filter'), 10, 2);
47 }
48
49 /**
50 * Deactivation hook
51 */
52 public static function deactivation()
53 {
54 // We can delete key on deactivation, but it's not needed now
55 // delete_option(WRITESONIC_API_KEY_OPTION);
56 }
57
58 /**
59 * WP settings registration
60 */
61 public static function register_settings()
62 {
63 register_setting('writesonic', WRITESONIC_API_KEY_OPTION);
64 }
65
66 /**
67 * Settings menu registration
68 */
69 public static function create_settings_menu()
70 {
71 add_options_page('Writesonic Settings', 'Writesonic', 'manage_options', 'writesonic', array('WPM_Writesonic_Integration', 'create_settings_page'));
72 }
73
74 /**
75 * Settings page registration
76 */
77 public static function create_settings_page()
78 {
79 include plugin_dir_path(__FILE__) . '/templates/settings.php';
80 }
81
82 /**
83 * @param $args
84 * @param $post_type
85 *
86 * @return mixed
87 */
88 public static function custom_post_types_show_in_rest_filter($args, $post_type)
89 {
90 $args['show_in_rest'] = true;
91
92 return $args;
93 }
94
95 /**
96 * Removes `has_published_posts` from the query args so even users who have not
97 * published content are returned by the request.
98 *
99 * @see https://developer.wordpress.org/reference/classes/wp_user_query/
100 *
101 * @param array $prepared_args Array of arguments for WP_User_Query.
102 * @param WP_REST_Request $request The current request.
103 *
104 * @return array
105 */
106 function remove_has_published_posts_from_wp_api_user_query($prepared_args, $request)
107 {
108 unset($prepared_args['has_published_posts']);
109
110 return $prepared_args;
111 }
112
113 /**
114 * Register VidApp custom REST API endpoints
115 */
116 public static function register_api_endpoints()
117 {
118 /**
119 * Categories
120 */
121 $categories_controller = new WP_REST_Terms_Controller('category');
122 register_rest_route('writesonic/v2', '/categories', array(
123 'methods' => WP_REST_Server::READABLE,
124 'callback' => array('WPM_Writesonic_Integration', 'get_categories'),
125 'permission_callback' => array('WPM_Writesonic_Integration', 'get_categories_permissions_check'),
126 'args' => $categories_controller->get_collection_params()
127 ));
128
129 $tags_controller = new WP_REST_Terms_Controller('post_tag');
130 register_rest_route('writesonic/v2', '/tags', array(
131 'methods' => WP_REST_Server::READABLE,
132 'callback' => array('WPM_Writesonic_Integration', 'get_tags'),
133 'permission_callback' => array('WPM_Writesonic_Integration', 'get_tags_permissions_check'),
134 'args' => $tags_controller->get_collection_params()
135 ));
136
137 /**
138 * Posts
139 */
140 $posts_controller = new WP_REST_Posts_Controller('post');
141 register_rest_route('writesonic/v2', '/posts', array(
142 array(
143 'methods' => WP_REST_Server::READABLE,
144 'callback' => array('WPM_Writesonic_Integration', 'get_posts'),
145 'permission_callback' => array('WPM_Writesonic_Integration', 'get_posts_permission_check'),
146 'args' => $posts_controller->get_collection_params()
147 ),
148 array(
149 'methods' => WP_REST_Server::CREATABLE,
150 'callback' => array('WPM_Writesonic_Integration', 'create_post'),
151 'permission_callback' => array('WPM_Writesonic_Integration', 'create_post_permissions_check'),
152 'args' => array(
153 'title' => array(
154 'required' => false,
155 'sanitize_callback' => 'sanitize_text_field',
156 ),
157 'content' => array(
158 'required' => false,
159 ),
160 'status' => array(
161 'required' => false,
162 'sanitize_callback' => 'sanitize_text_field',
163 ),
164 'slug' => array(
165 'required' => false,
166 'sanitize_callback' => 'sanitize_text_field',
167 ),
168 'categories' => array(
169 'required' => false,
170 'type' => 'array',
171 ),
172 'tags' => array(
173 'required' => false,
174 'type' => 'array',
175 ),
176 'meta_description' => array(
177 'required' => false,
178 'sanitize_callback' => 'sanitize_text_field',
179 ),
180 'featured_image' => array(
181 'required' => false,
182 'type' => 'string',
183 ),
184 ),
185 ),
186 'schema' => array('WPM_Writesonic_Integration', 'get_public_item_schema'),
187 ));
188
189 /**
190 * Posts update
191 */
192 register_rest_route(
193 'writesonic/v2',
194 '/posts/(?P<id>\d+)',
195 array(
196 array(
197 'methods' => WP_REST_Server::READABLE,
198 'callback' => array('WPM_Writesonic_Integration', 'get_post'),
199 'permission_callback' => array('WPM_Writesonic_Integration', 'get_post_permission_check'),
200 ),
201 array(
202 'methods' => WP_REST_Server::EDITABLE,
203 'callback' => array('WPM_Writesonic_Integration', 'update_post'),
204 'permission_callback' => array('WPM_Writesonic_Integration', 'update_post_permissions_check'),
205 'args' => array(
206 'title' => array(
207 'required' => false,
208 'sanitize_callback' => 'sanitize_text_field',
209 ),
210 'content' => array(
211 'required' => false,
212 ),
213 'status' => array(
214 'required' => false,
215 'sanitize_callback' => 'sanitize_text_field',
216 ),
217 'slug' => array(
218 'required' => false,
219 'sanitize_callback' => 'sanitize_text_field',
220 ),
221 'categories' => array(
222 'required' => false,
223 'type' => 'array',
224 ),
225 'tags' => array(
226 'required' => false,
227 'type' => 'array',
228 ),
229 'meta_description' => array(
230 'required' => false,
231 'sanitize_callback' => 'sanitize_text_field',
232 ),
233 'featured_image' => array(
234 'required' => false,
235 'type' => 'string',
236 ),
237 ),
238 'schema' => array('WPM_Writesonic_Integration', 'get_public_item_schema'),
239 ),
240 )
241 );
242
243 /**
244 * Media
245 */
246 $attachment_controller = new WP_REST_Attachments_Controller('attachment');
247 register_rest_route('writesonic/v2', '/media', array(
248 array(
249 'methods' => WP_REST_Server::READABLE,
250 'callback' => array('WPM_Writesonic_Integration', 'get_media'),
251 'permission_callback' => array('WPM_Writesonic_Integration', 'get_media_permission_check'),
252 'args' => $attachment_controller->get_collection_params()
253
254 ),
255 array(
256 'methods' => WP_REST_Server::CREATABLE,
257 'callback' => array('WPM_Writesonic_Integration', 'create_media'),
258 'permission_callback' => array('WPM_Writesonic_Integration', 'create_media_permissions_check'),
259 'args' => $attachment_controller->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE),
260 ),
261 'schema' => array('WPM_Writesonic_Integration', 'get_public_item_schema'),
262 ));
263
264 /**
265 * Comments
266 */
267 $comment_controller = new WP_REST_Comments_Controller();
268 register_rest_route('writesonic/v2', '/comments', array(
269 array(
270 'methods' => WP_REST_Server::READABLE,
271 'callback' => array('WPM_Writesonic_Integration', 'get_comments'),
272 'permission_callback' => array('WPM_Writesonic_Integration', 'get_comments_permission_check'),
273 'args' => $comment_controller->get_collection_params()
274
275 ),
276 array(
277 'methods' => WP_REST_Server::CREATABLE,
278 'callback' => array('WPM_Writesonic_Integration', 'create_comment'),
279 'permission_callback' => array('WPM_Writesonic_Integration', 'create_comment_permissions_check'),
280 'args' => $comment_controller->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE),
281 ),
282 'schema' => array('WPM_Writesonic_Integration', 'get_public_item_schema'),
283 ));
284
285 /**
286 * Users
287 */
288 $users_controller = new WP_REST_Users_Controller();
289 register_rest_route('writesonic/v2', '/users', array(
290 'methods' => WP_REST_Server::READABLE,
291 'callback' => array('WPM_Writesonic_Integration', 'get_users'),
292 'permission_callback' => array('WPM_Writesonic_Integration', 'get_users_permissions_check'),
293 'args' => $users_controller->get_collection_params()
294 ));
295
296 register_rest_route('writesonic/v2', '/password', array(
297 'methods' => WP_REST_Server::CREATABLE,
298 'callback' => array('WPM_Writesonic_Integration', 'validate_user_password'),
299 'permission_callback' => array('WPM_Writesonic_Integration', 'validate_password_permissions_check'),
300 'args' => array(
301 'password' => array(
302 'default' => null, // значение параметра по умолчанию
303 'required' => true, // является ли параметр обязательным. Может быть только true
304 'sanitize_callback' => 'sanitize_text_field', // функция очистки значения параметра. Должна вернуть очищенное значение
305 )
306 )
307 ));
308
309 register_rest_route('writesonic/v2', '/authors', array(
310 'methods' => WP_REST_Server::READABLE,
311 'callback' => array('WPM_Writesonic_Integration', 'get_authors'),
312 'permission_callback' => array('WPM_Writesonic_Integration', 'get_authors_permissions_check'),
313 'args' => $users_controller->get_collection_params()
314 ));
315 }
316
317 public static function get_user_by_token($token, $user)
318 {
319 $wpb_writesonic_tokens = get_option(WRITESONIC_API_KEY_OPTION);
320
321 if (!is_array($wpb_writesonic_tokens)) {
322 return $user;
323 }
324
325 $user_email = array_search($token, $wpb_writesonic_tokens);
326
327 if ($user_email) {
328 $user = get_user_by('email', $user_email);
329
330 return $user->ID;
331 }
332
333 return $user;
334 }
335
336 public static function get_public_item_schema()
337 {
338 $posts_controller = new WP_REST_Posts_Controller('post');
339
340 return $posts_controller->get_public_item_schema();
341 }
342
343 /**
344 * @param WP_REST_Request $request
345 *
346 * @return bool
347 */
348 public static function get_users_permissions_check(WP_REST_Request $request)
349 {
350 return self::checkAPIKeyAuth($request);
351 }
352
353 /**
354 * @param WP_REST_Request $request
355 *
356 * @return bool
357 */
358 public static function get_categories_permissions_check(WP_REST_Request $request)
359 {
360 return self::checkAPIKeyAuth($request);
361 }
362
363 /**
364 * @param WP_REST_Request $request
365 *
366 * @return mixed
367 */
368 public static function get_users(WP_REST_Request $request)
369 {
370 $controller = new WP_REST_Users_Controller();
371
372 $response = $controller->get_items($request);
373
374 return $response;
375 }
376
377 /**
378 * Get users who can create or edit posts
379 *
380 * @param WP_REST_Request $request
381 * @return WP_REST_Response
382 */
383 public static function get_authors(WP_REST_Request $request)
384 {
385 // Define the query arguments to get users with post creation/editing capabilities
386 $args = array(
387 'role__in' => array('administrator', 'editor', 'author'), // Roles capable of creating/editing posts
388 );
389
390 // Fetch the users
391 $users = get_users($args);
392
393 // Prepare the response data
394 $author_data = array_map(function ($user) {
395 return array(
396 'ID' => $user->ID,
397 'username' => $user->user_login,
398 'name' => $user->display_name,
399 'email' => $user->user_email,
400 );
401 }, $users);
402
403 // Return the response
404 return rest_ensure_response($author_data);
405 }
406
407 /**
408 * Permissions check for getting authors
409 *
410 * @param WP_REST_Request $request
411 * @return bool
412 */
413 public static function get_authors_permissions_check(WP_REST_Request $request)
414 {
415 return self::checkAPIKeyAuth($request);
416 }
417
418
419 /**
420 * Get all categories
421 *
422 * @param WP_REST_Request $request
423 * @return WP_REST_Response
424 */
425 public static function get_categories(WP_REST_Request $request)
426 {
427 // Define arguments to fetch all categories
428 $args = array(
429 'hide_empty' => false, // Include categories even if they have no posts
430 );
431
432 // Fetch all categories
433 $categories = get_categories($args);
434
435 // Prepare the response data
436 $category_data = array_map(function ($category) {
437 return array(
438 'ID' => $category->term_id,
439 'name' => $category->name,
440 'slug' => $category->slug,
441 'count' => $category->count, // Number of posts in the category
442 );
443 }, $categories);
444
445 // Return the response
446 return rest_ensure_response($category_data);
447 }
448
449 /**
450 * Get all tags
451 *
452 * @param WP_REST_Request $request
453 * @return WP_REST_Response
454 */
455 public static function get_tags(WP_REST_Request $request)
456 {
457 // Define arguments to fetch all tags
458 $args = array(
459 'hide_empty' => false, // Include tags even if they have no posts
460 );
461
462 // Fetch all tags
463 $tags = get_tags($args);
464
465 // Prepare the response data
466 $tag_data = array_map(function ($tag) {
467 return array(
468 'ID' => $tag->term_id,
469 'name' => $tag->name,
470 'slug' => $tag->slug,
471 );
472 }, $tags);
473
474 // Return the response
475 return rest_ensure_response($tag_data);
476 }
477
478 /**
479 * Permissions check for getting tags
480 *
481 * @param WP_REST_Request $request
482 * @return bool
483 */
484 public static function get_tags_permissions_check(WP_REST_Request $request)
485 {
486 return self::checkAPIKeyAuth($request);
487 }
488
489
490
491 /**
492 * @param WP_REST_Request $request
493 *
494 * @return mixed
495 */
496 public static function get_posts_permission_check(WP_REST_Request $request)
497 {
498 return self::checkAPIKeyAuth($request);
499 }
500
501 /**
502 * @param WP_REST_Request $request
503 *
504 * @return mixed
505 */
506 public static function get_comments(WP_REST_Request $request)
507 {
508 $controller = new WP_REST_Comments_Controller();
509 $response = $controller->get_items($request);
510
511 return $response;
512 }
513
514 /**
515 * @param WP_REST_Request $request
516 *
517 * @return bool
518 */
519 public static function get_comments_permission_check(WP_REST_Request $request)
520 {
521 return self::checkAPIKeyAuth($request);
522 }
523
524 /**
525 * Get posts of any status and include tag and category names
526 *
527 * @param WP_REST_Request $request
528 * @return WP_REST_Response
529 */
530 public static function get_posts(WP_REST_Request $request)
531 {
532 // Determine the post type from the request, default to 'post'
533 $post_type = isset($request['post_type']) ? sanitize_text_field($request['post_type']) : 'post';
534
535 // Set the status parameter to 'any' to fetch posts of any status
536 $request['status'] = 'any';
537
538 // Create a new WP_REST_Posts_Controller for the specified post type
539 $controller = new WP_REST_Posts_Controller($post_type);
540
541 // Fetch the posts using the modified request parameters
542 $response = $controller->get_items($request);
543
544 // Check if the response is successful
545 if ($response instanceof WP_REST_Response && $response->is_error()) {
546 return $response;
547 }
548
549 // Get the posts from the response
550 $posts = $response->get_data();
551
552 // Loop through each post to fetch and append category and tag names
553 foreach ($posts as &$post) {
554 // Fetch category names
555 $categories = get_the_category($post['id']);
556 $category_names = array_map(function ($cat) {
557 return $cat->name;
558 }, $categories);
559
560 // Fetch tag names
561 $tags = get_the_tags($post['id']);
562 $tag_names = array();
563 if ($tags) {
564 $tag_names = array_map(function ($tag) {
565 return $tag->name;
566 }, $tags);
567 }
568
569 // Replace the IDs with names in the post data
570 $post['categories'] = $category_names;
571 $post['tags'] = $tag_names;
572
573 // * get the meta description and add it to the response
574 $meta_description = get_post_meta($post['id'], 'description', true);
575 // * if it's not empty, add it to the response
576 if (!empty($meta_description)) {
577 $post['meta_description'] = $meta_description;
578 }
579
580 if (!empty($post['featured_media'])) {
581 // * we need to get the link to the image
582 $featured_image = wp_get_attachment_image_src($post['featured_media'], 'full');
583 $post['featured_image'] = $featured_image[0];
584 }
585 }
586
587 // Return the modified response
588 return rest_ensure_response($posts);
589 }
590
591 public static function get_post(WP_REST_Request $request)
592 {
593 $controller = new WP_REST_Posts_Controller('post');
594 $response = $controller->get_item($request);
595
596 return $response;
597 }
598
599 public static function get_post_permission_check(WP_REST_Request $request)
600 {
601 return self::checkAPIKeyAuth($request);
602 }
603
604 /**
605 * Check if correct API key provided in request
606 *
607 * @param WP_REST_Request $request
608 *
609 * @return bool
610 */
611 public static function checkAPIKeyAuth(WP_REST_Request $request)
612 {
613 $auth = isset($_SERVER['HTTP_TOKEN']) ? sanitize_text_field($_SERVER['HTTP_TOKEN']) : false;
614 $writesonic_token_key = get_option(WRITESONIC_API_KEY_OPTION, true);
615
616 wp_set_current_user(self::get_user_by_token($auth, $user));
617
618 if (is_array($writesonic_token_key) && in_array($auth, $writesonic_token_key)) {
619 return true;
620 }
621
622 return false;
623 }
624
625 /**
626 * Handle the creation or updating of a post, including handling mixed input for tags and categories
627 *
628 * @param WP_REST_Request $request
629 * @param bool $is_update Whether this is an update operation
630 * @return WP_REST_Response
631 */
632 private static function handle_post_creation_or_update(WP_REST_Request $request, $is_update = false)
633 {
634 // Determine the post type from the request, default to 'post'
635 $post_type = isset($request['post_type']) ? sanitize_text_field($request['post_type']) : 'post';
636
637 // Prepare the parameters for creating or updating the post
638 $params = array(
639 'post_type' => $post_type,
640 'post_title' => isset($request['title']) ? sanitize_text_field($request['title']) : '',
641 'post_content' => isset($request['content']) ? $request['content'] : '',
642 'post_status' => isset($request['status']) ? sanitize_text_field($request['status']) : 'draft',
643 );
644
645 // Handle the optional slug parameter
646 if (isset($request['slug'])) {
647 $params['post_name'] = sanitize_text_field($request['slug']);
648 }
649
650 // Process categories (names as strings)
651 if (isset($request['categories']) && is_array($request['categories'])) {
652 $category_ids = array();
653 foreach ($request['categories'] as $category_name) {
654 // Try to get the category by name
655 $category = get_term_by('name', sanitize_text_field($category_name), 'category');
656 if ($category) {
657 // Existing category, use its ID
658 $category_ids[] = $category->term_id;
659 } else {
660 // New category, create it
661 $new_category = wp_insert_term(sanitize_text_field($category_name), 'category');
662 if (!is_wp_error($new_category)) {
663 $category_ids[] = $new_category['term_id'];
664 }
665 }
666 }
667 $params['post_category'] = $category_ids;
668 }
669
670 // Process tags (names as strings)
671 if (isset($request['tags']) && is_array($request['tags'])) {
672 $tag_ids = array();
673 foreach ($request['tags'] as $tag_name) {
674 // Try to get the tag by name
675 $tag = get_term_by('name', sanitize_text_field($tag_name), 'post_tag');
676 if ($tag) {
677 // Existing tag, use its ID
678 $tag_ids[] = $tag->term_id;
679 } else {
680 // New tag, create it
681 $new_tag = wp_insert_term(sanitize_text_field($tag_name), 'post_tag');
682 if (!is_wp_error($new_tag)) {
683 $tag_ids[] = $new_tag['term_id'];
684 }
685 }
686 }
687 $params['tax_input'] = array('post_tag' => $tag_ids);
688 }
689
690 if (isset($request['author']) && is_numeric($request['author'])) {
691 $params['post_author'] = (int) $request['author'];
692 }
693
694
695 // If this is an update, include the post ID
696 if ($is_update) {
697 $post_id = (int) $request['id'];
698 if (!get_post($post_id)) {
699 return new WP_REST_Response(array(
700 'message' => 'Post not found'
701 ), 404);
702 }
703 $params['ID'] = $post_id;
704 }
705
706 // Create or update the post
707 $result = $is_update ? wp_update_post($params, true) : wp_insert_post($params, true);
708
709 if (is_wp_error($result)) {
710 return new WP_REST_Response(array(
711 'message' => $result->get_error_message()
712 ), 400);
713 }
714
715 // * update the meta description.
716 if (isset($request['meta_description'])) {
717 update_post_meta($result, 'description', sanitize_text_field($request['meta_description']));
718 }
719 if (isset($request['featured_image'])) {
720 $attachment_id = (int) $request['featured_image'];
721 if ($attachment_id) {
722 set_post_thumbnail($result, $attachment_id);
723 }
724 }
725
726 // Fetch the newly created or updated post to return as a response
727 $controller = new WP_REST_Posts_Controller($post_type);
728 $request->set_param('id', $result);
729 $response = $controller->prepare_item_for_response(get_post($result), $request);
730
731 return $response;
732 }
733
734
735
736 /**
737 * @param WP_REST_Request $request
738 *
739 * @return bool
740 */
741 public static function create_post_permissions_check(WP_REST_Request $request)
742 {
743 return self::checkAPIKeyAuth($request);
744 }
745
746 /**
747 * @param WP_REST_Request $request
748 *
749 * @return mixed
750 */
751 public static function create_post(WP_REST_Request $request)
752 {
753 return self::handle_post_creation_or_update($request, false);
754 }
755
756 /**
757 * Update an existing post
758 *
759 * @param WP_REST_Request $request
760 * @return WP_REST_Response
761 */
762 public static function update_post(WP_REST_Request $request)
763 {
764 return self::handle_post_creation_or_update($request, true);
765 }
766
767 /**
768 * Permissions check for updating a post
769 *
770 * @param WP_REST_Request $request
771 * @return bool
772 */
773 public static function update_post_permissions_check(WP_REST_Request $request)
774 {
775 return self::checkAPIKeyAuth($request);
776 }
777
778
779 /**
780 * @param WP_REST_Request $request
781 *
782 * @return mixed
783 */
784 public static function get_media_permission_check(WP_REST_Request $request)
785 {
786 return self::checkAPIKeyAuth($request);
787 }
788
789 /**
790 * @param WP_REST_Request $request
791 *
792 * @return mixed
793 */
794 public static function get_media(WP_REST_Request $request)
795 {
796 $controller = new WP_REST_Attachments_Controller('attachment');
797 $response = $controller->get_items($request);
798
799 return $response;
800 }
801
802 /**
803 * @param WP_REST_Request $request
804 *
805 * @return mixed
806 */
807 public static function create_media_permissions_check(WP_REST_Request $request)
808 {
809 return self::checkAPIKeyAuth($request);
810 }
811
812 /**
813 * @param WP_REST_Request $request
814 *
815 * @return mixed
816 */
817 public static function create_media(WP_REST_Request $request)
818 {
819 $controller = new WP_REST_Attachments_Controller('attachment');
820 $response = $controller->create_item($request);
821
822 return $response;
823 }
824
825 /**
826 * @param WP_REST_Request $request
827 *
828 * @return mixed
829 */
830 public static function create_comment_permissions_check(WP_REST_Request $request)
831 {
832 return self::checkAPIKeyAuth($request);
833 }
834
835 /**
836 * @param WP_REST_Request $request
837 *
838 * @return mixed
839 */
840 public static function create_comment(WP_REST_Request $request)
841 {
842 $controller = new WP_REST_Comments_Controller();
843 $response = $controller->create_item($request);
844
845 return $response;
846 }
847
848 /**
849 * @param WP_REST_Request $request
850 *
851 * @return bool
852 */
853 public static function validate_password_permissions_check(WP_REST_Request $request)
854 {
855 return self::checkAPIKeyAuth($request);
856 }
857
858 /**
859 * Validate user's password
860 *
861 * @param WP_REST_Request $request
862 * @return string
863 */
864 public static function validate_user_password(WP_REST_Request $request)
865 {
866 if (!isset($request['password']) || !isset($request['user_id'])) {
867 return [];
868 }
869
870 $password = $request['password'];
871 $user = get_user_by('id', $request['user_id']);
872 if (wp_check_password($password, $user->user_pass)) {
873 $data = array('result' => 'true');
874 } else {
875 $data = array('result' => 'false');
876 }
877
878 $response = rest_ensure_response($data);
879
880 wp_send_json($response);
881 }
882
883 /**
884 * Check authorization
885 *
886 * @param string $token
887 * @param string $domain
888 * @return bool
889 */
890 public static function checkAuthorization($token, $domain)
891 {
892 $body = [
893 'token' => $token,
894 'domain' => $domain,
895 ];
896
897 $body = wp_json_encode($body);
898
899 $options = [
900 'body' => $body,
901 'headers' => [
902 'Content-Type' => 'application/json',
903 ],
904 'data_format' => 'body',
905 ];
906
907 $request = wp_remote_post(WRITESONIC_CHECK_AUTH_URL, $options);
908
909 $tmp = json_decode(wp_remote_retrieve_body($request), true);
910
911 return (bool) $tmp;
912 }
913 }
914
915 WPM_Writesonic_Integration::init();
916 }
917