PluginProbe
Extendify / 3.1.5
Extendify v3.1.5
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / app / HelpCenter / Controllers / SupportArticlesController.php

SupportArticlesController.php in Extendify 3.1.5, at app/HelpCenter/Controllers/SupportArticlesController.php

116 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Controls Support Articles
5 */
6
7 namespace Extendify\HelpCenter\Controllers;
8
9 defined('ABSPATH') || die('No direct access.');
10
11 use Extendify\Constants;
12 use Extendify\Shared\Services\Sanitizer;
13
14 /**
15 * The controller for fetching support articles
16 */
17
18 class SupportArticlesController
19 {
20 /**
21 * Return support articles from source.
22 *
23 * @return \WP_REST_Response
24 */
25 public static function fetchArticles()
26 {
27 if (!defined('EXTENDIFY_PARTNER_ID')) {
28 return new \WP_REST_Response([], 200);
29 }
30
31 $response = wp_remote_get(sprintf('%s/api/posts?lang=%s', Constants::KB_HOST, \get_locale()));
32
33 if (is_wp_error($response)) {
34 return new \WP_REST_Response([]);
35 }
36
37 $body = json_decode(\wp_remote_retrieve_body($response), true);
38 return new \WP_REST_Response($body);
39 }
40
41 /**
42 * Return the selected support article from source.
43 *
44 * @param \WP_REST_Request $request - The request.
45 * @return \WP_REST_Response
46 */
47 public static function article($request)
48 {
49 $response = wp_remote_get(
50 sprintf(
51 '%s/api/posts/%s?lang=%s',
52 Constants::KB_HOST,
53 $request->get_param('slug'),
54 \get_locale()
55 )
56 );
57
58 if (is_wp_error($response)) {
59 return new \WP_REST_Response([]);
60 }
61
62 return new \WP_REST_Response(wp_remote_retrieve_body($response));
63 }
64
65 /**
66 * Return the data
67 *
68 * @return \WP_REST_Response
69 */
70 public static function get()
71 {
72 $data = get_option('extendify_assist_support_articles', []);
73 return new \WP_REST_Response($data);
74 }
75
76 /**
77 * Persist the data
78 *
79 * @param \WP_REST_Request $request - The request.
80 * @return \WP_REST_Response
81 */
82 public static function store($request)
83 {
84 $data = $request->get_param('state');
85 update_option('extendify_assist_support_articles', [
86 'state' => Sanitizer::sanitizeArray($data),
87 ]);
88 return new \WP_REST_Response($data);
89 }
90
91 /**
92 * Attempts to find a redirect URL from the old docs site
93 *
94 * @param \WP_REST_Request $request - The request.
95 * @return \WP_REST_Response
96 */
97 public static function getRedirect($request)
98 {
99 $url = 'https://wordpress.org' . $request->get_param('path');
100 $response = \wp_remote_head($url);
101 $location = \wp_remote_retrieve_header($response, 'location');
102 if (\is_wp_error($response)) {
103 \wp_send_json_error(\__('Page not found', 'extendify-local'), 404);
104 }
105
106 // No redirect, we're done.
107 if (empty($location)) {
108 return new \WP_REST_Response($url, 200);
109 }
110
111 // Keep going until no more redirects.
112 $request->set_param('path', \wp_parse_url($location, PHP_URL_PATH));
113 return self::getRedirect($request);
114 }
115 }
116