| 1 |
<?php |
| 2 |
/** |
| 3 |
* Public site discovery REST API controller. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\API\V2 |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\API\V2; |
| 9 |
|
| 10 |
use WCPOS\WooCommercePOS\Services\Settings as SettingsService; |
| 11 |
use WCPOS\WooCommercePOS\Template_Router; |
| 12 |
use WP_REST_Response; |
| 13 |
use const WCPOS\WooCommercePOS\VERSION; |
| 14 |
|
| 15 |
/** |
| 16 |
* Serves the lightweight public site discovery response. |
| 17 |
*/ |
| 18 |
final class Site { |
| 19 |
private const ROUTE = '/wcpos/v2/site'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Register the site discovery route. |
| 23 |
*/ |
| 24 |
public function register_routes(): void { |
| 25 |
register_rest_route( |
| 26 |
'wcpos/v2', |
| 27 |
'/site', |
| 28 |
array( |
| 29 |
'methods' => 'GET', |
| 30 |
'callback' => array( $this, 'get_site' ), |
| 31 |
'permission_callback' => '__return_true', |
| 32 |
) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Classify the discovery route as public. |
| 38 |
* |
| 39 |
* @return array<string, string[]> |
| 40 |
*/ |
| 41 |
public function wcpos_route_classifications(): array { |
| 42 |
return array( 'public' => array( self::ROUTE ) ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Return the site discovery response. |
| 47 |
*/ |
| 48 |
public function get_site(): WP_REST_Response { |
| 49 |
$data = array( |
| 50 |
'uuid' => wcpos_get_site_uuid(), |
| 51 |
'name' => get_bloginfo( 'name' ), |
| 52 |
'description' => get_bloginfo( 'description' ), |
| 53 |
'url' => get_option( 'siteurl' ), |
| 54 |
'home' => home_url(), |
| 55 |
'gmt_offset' => get_option( 'gmt_offset' ), |
| 56 |
'timezone_string' => get_option( 'timezone_string' ), |
| 57 |
'site_logo' => (int) get_theme_mod( 'custom_logo', 0 ), |
| 58 |
'site_icon_url' => get_site_icon_url(), |
| 59 |
'wp_version' => get_bloginfo( 'version' ), |
| 60 |
'wcpos_version' => VERSION, |
| 61 |
'use_jwt_as_param' => SettingsService::instance()->use_jwt_as_param_enabled(), |
| 62 |
'namespaces' => array( 'wcpos/v2' ), |
| 63 |
'authentication' => array( |
| 64 |
'wcpos' => array( |
| 65 |
'endpoints' => array( |
| 66 |
'authorization' => Template_Router::get_auth_url(), |
| 67 |
), |
| 68 |
), |
| 69 |
), |
| 70 |
); |
| 71 |
|
| 72 |
if ( \function_exists( 'WC' ) ) { |
| 73 |
$data['wc_version'] = WC()->version; |
| 74 |
$data['namespaces'][] = 'wc/v3'; |
| 75 |
} |
| 76 |
|
| 77 |
if ( \defined( 'WCPOS\WooCommercePOSPro\VERSION' ) ) { |
| 78 |
$data['wcpos_pro_version'] = \constant( 'WCPOS\WooCommercePOSPro\VERSION' ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Filters the public site discovery data. |
| 83 |
* |
| 84 |
* @param array $data Site discovery data. |
| 85 |
*/ |
| 86 |
$data = apply_filters( 'wcpos_rest_site_info', $data ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Public WCPOS site discovery filter. |
| 87 |
|
| 88 |
return new WP_REST_Response( $data, 200 ); |
| 89 |
} |
| 90 |
} |
| 91 |
|