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