| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\API; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use StoreEngine\Admin\Notices; |
| 10 |
use StoreEngine\Classes\Countries; |
| 11 |
use StoreEngine\Classes\Exceptions\StoreEngineException; |
| 12 |
use StoreEngine\Payment_Gateways; |
| 13 |
use StoreEngine\Utils\Formatting; |
| 14 |
use StoreEngine\Utils\Geolocation; |
| 15 |
use StoreEngine\Utils\Helper; |
| 16 |
use WP_Error; |
| 17 |
use WP_REST_Controller; |
| 18 |
use WP_REST_Request; |
| 19 |
use WP_REST_Response; |
| 20 |
use WP_REST_Server; |
| 21 |
|
| 22 |
class Settings extends WP_REST_Controller { |
| 23 |
|
| 24 |
public static function init() { |
| 25 |
$self = new self(); |
| 26 |
$self->namespace = STOREENGINE_PLUGIN_SLUG . '/v1'; |
| 27 |
$self->rest_base = 'settings'; |
| 28 |
add_action( 'rest_api_init', array( $self, 'register_routes' ) ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Register the routes for the objects of the controller. |
| 33 |
*/ |
| 34 |
public function register_routes() { |
| 35 |
register_rest_route( $this->namespace, '/' . $this->rest_base, [ |
| 36 |
[ |
| 37 |
'methods' => WP_REST_Server::READABLE, |
| 38 |
'callback' => [ $this, 'get_items' ], |
| 39 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 40 |
'args' => $this->get_collection_params(), |
| 41 |
], |
| 42 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 43 |
] ); |
| 44 |
|
| 45 |
$gateway_args = $this->get_gateway_args(); |
| 46 |
|
| 47 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/payment-gateways', [ |
| 48 |
[ |
| 49 |
'methods' => WP_REST_Server::EDITABLE, |
| 50 |
'callback' => [ $this, 'update_payment_gateway_settings' ], |
| 51 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 52 |
'args' => [ |
| 53 |
'context' => [ |
| 54 |
'type' => 'string', |
| 55 |
'sanitize_callback' => 'sanitize_key', |
| 56 |
'validate_callback' => 'rest_validate_request_arg', |
| 57 |
'default' => 'edit', |
| 58 |
], |
| 59 |
'config' => [ |
| 60 |
'type' => 'object', |
| 61 |
'required' => true, |
| 62 |
'properties' => $gateway_args, |
| 63 |
], |
| 64 |
], |
| 65 |
], |
| 66 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 67 |
] ); |
| 68 |
|
| 69 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/verify-payment-gateways', [ |
| 70 |
[ |
| 71 |
'methods' => WP_REST_Server::EDITABLE, |
| 72 |
'callback' => [ $this, 'verify_payment_gateway_settings' ], |
| 73 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 74 |
'args' => [ |
| 75 |
'context' => [ |
| 76 |
'type' => 'string', |
| 77 |
'sanitize_callback' => 'sanitize_key', |
| 78 |
'validate_callback' => 'rest_validate_request_arg', |
| 79 |
'default' => 'edit', |
| 80 |
], |
| 81 |
'method' => [ |
| 82 |
'title' => __( 'Payment method identifier.', 'storeengine' ), |
| 83 |
'type' => 'string', |
| 84 |
'sanitize_callback' => 'sanitize_key', |
| 85 |
'validate_callback' => 'rest_validate_request_arg', |
| 86 |
'required' => true, |
| 87 |
], |
| 88 |
'config' => [ |
| 89 |
'title' => __( 'Payment method config.', 'storeengine' ), |
| 90 |
'description' => __( 'Payment method settings data.', 'storeengine' ), |
| 91 |
'type' => 'object', |
| 92 |
'required' => true, |
| 93 |
'properties' => array_merge( ...array_values( $gateway_args ) )['properties'], |
| 94 |
], |
| 95 |
], |
| 96 |
], |
| 97 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 98 |
] ); |
| 99 |
} |
| 100 |
|
| 101 |
protected function get_gateway_args(): array { |
| 102 |
$type_mapping = [ |
| 103 |
'text' => [ |
| 104 |
'type' => 'string', |
| 105 |
'sanitize_callback' => 'sanitize_text_field', |
| 106 |
'validate_callback' => 'rest_validate_request_arg', |
| 107 |
], |
| 108 |
'password' => [ |
| 109 |
'type' => 'string', |
| 110 |
'sanitize_callback' => 'sanitize_text_field', |
| 111 |
'validate_callback' => 'rest_validate_request_arg', |
| 112 |
], |
| 113 |
'checkbox' => [ |
| 114 |
'type' => 'boolean', |
| 115 |
'validate_callback' => 'rest_validate_request_arg', |
| 116 |
], |
| 117 |
'safe_text' => [ |
| 118 |
'type' => 'string', |
| 119 |
'sanitize_callback' => [ Formatting::class, 'sanitize_safe_text_field' ], |
| 120 |
'validate_callback' => [ Formatting::class, 'sanitize_safe_text_field' ], |
| 121 |
], |
| 122 |
'textarea' => [ |
| 123 |
'type' => 'string', |
| 124 |
'sanitize_callback' => 'wp_kses_post', |
| 125 |
'validate_callback' => 'wp_kses_post', |
| 126 |
], |
| 127 |
'select' => [ |
| 128 |
'type' => 'string', |
| 129 |
'sanitize_callback' => 'sanitize_text_field', |
| 130 |
'validate_callback' => 'rest_validate_request_arg', |
| 131 |
], |
| 132 |
'multiselect' => [ |
| 133 |
'type' => 'array', |
| 134 |
'items' => [ 'type' => 'string' ], |
| 135 |
'sanitize_callback' => 'sanitize_text_field', |
| 136 |
'validate_callback' => 'rest_validate_request_arg', |
| 137 |
], |
| 138 |
]; |
| 139 |
|
| 140 |
$fields = []; |
| 141 |
|
| 142 |
foreach ( Payment_Gateways::init()->get_gateways() as $gateway ) { |
| 143 |
$properties = [ |
| 144 |
'is_enabled' => [ |
| 145 |
'type' => 'boolean', |
| 146 |
'validate_callback' => 'rest_validate_request_arg', |
| 147 |
'default' => false, |
| 148 |
], |
| 149 |
'index' => [ |
| 150 |
'type' => 'integer', |
| 151 |
'sanitize_callback' => 'absint', |
| 152 |
'validate_callback' => 'rest_validate_request_arg', |
| 153 |
'default' => 0, |
| 154 |
], |
| 155 |
]; |
| 156 |
|
| 157 |
foreach ( $gateway->get_admin_fields_sorted() as $field => $cfg ) { |
| 158 |
$type = $cfg['type'] ?? 'text'; |
| 159 |
if ( 'repeater' === $type ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf |
| 160 |
$items = []; |
| 161 |
foreach ( $cfg['fields'] as $name => $repeated ) { |
| 162 |
$repeated_type = $repeated['type'] ?? 'text'; |
| 163 |
$item = [ |
| 164 |
'description' => $repeated['description'] ?? ( $repeated['tooltip'] ?? '' ), |
| 165 |
'type' => $type_mapping[ $repeated_type ]['type'] ?? 'string', |
| 166 |
'default' => $repeated['default'] ?? '', |
| 167 |
'sanitize_callback' => 'sanitize_text_field', |
| 168 |
'validate_callback' => 'rest_validate_request_arg', |
| 169 |
]; |
| 170 |
|
| 171 |
if ( ! empty( $type_mapping[ $type ] ) ) { |
| 172 |
$item = array_merge( $item, $type_mapping[ $type ] ); |
| 173 |
} |
| 174 |
|
| 175 |
$items[ $name ] = $item; |
| 176 |
} |
| 177 |
$schema = [ |
| 178 |
'type' => 'array', |
| 179 |
'description' => $cfg['tooltip'] ?? '', |
| 180 |
'items' => [ |
| 181 |
'type' => 'object', |
| 182 |
'properties' => $items, |
| 183 |
], |
| 184 |
]; |
| 185 |
} else { |
| 186 |
$schema = [ |
| 187 |
'type' => $type_mapping[ $type ]['type'] ?? 'string', |
| 188 |
'description' => $cfg['tooltip'] ?? '', |
| 189 |
'default' => $cfg['default'] ?? '', |
| 190 |
'sanitize_callback' => 'sanitize_text_field', |
| 191 |
'validate_callback' => 'rest_validate_request_arg', |
| 192 |
]; |
| 193 |
if ( ! empty( $type_mapping[ $type ] ) ) { |
| 194 |
$schema = array_merge( $schema, $type_mapping[ $type ] ); |
| 195 |
} |
| 196 |
} |
| 197 |
$properties[ $field ] = $schema; |
| 198 |
} |
| 199 |
|
| 200 |
$fields[ $gateway->id ] = [ |
| 201 |
'type' => 'object', |
| 202 |
'label' => $gateway->method_title, |
| 203 |
'description' => $gateway->method_description, |
| 204 |
'properties' => $properties, |
| 205 |
]; |
| 206 |
} |
| 207 |
|
| 208 |
return $fields; |
| 209 |
} |
| 210 |
|
| 211 |
public function get_items( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass |
| 212 |
global $storeengine_settings; |
| 213 |
|
| 214 |
// Clone object or it mutates. |
| 215 |
$settings = clone $storeengine_settings; |
| 216 |
Notices::init()->dispatch_notices(); |
| 217 |
$settings->admin_notices = array_values( Notices::get_notices() ); |
| 218 |
|
| 219 |
if ( Formatting::string_to_bool( $request->get_param( 'is_setup' ) ) ) { |
| 220 |
if ( empty( $settings->store_country ) ) { |
| 221 |
$user_location = Geolocation::geolocate_ip( '', true, true ); |
| 222 |
|
| 223 |
$settings->store_country = $user_location['country']; |
| 224 |
$settings->store_state = $user_location['state']; |
| 225 |
|
| 226 |
if ( empty( $settings->store_currency ) ) { |
| 227 |
$settings->store_currency = 'USD'; |
| 228 |
$locale_info = include STOREENGINE_ROOT_DIR_PATH . '/i18n/locale-info.php'; |
| 229 |
if ( $settings->store_country && ! empty( $locale_info[ $settings->store_country ] ) ) { |
| 230 |
$info = $locale_info[ $settings->store_country ]; |
| 231 |
$settings->store_currency = $info['currency_code']; |
| 232 |
$settings->store_currency_position = $info['currency_pos']; |
| 233 |
$settings->store_currency_thousand_separator = $info['thousand_sep']; |
| 234 |
$settings->store_currency_decimal_separator = $info['decimal_sep']; |
| 235 |
$settings->store_currency_decimal_limit = $info['num_decimals']; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
// Return settings in response. |
| 242 |
return rest_ensure_response( apply_filters( 'storeengine/api/settings', $settings ) ); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* @param WP_REST_Request $request Full details about the request. |
| 247 |
* |
| 248 |
* @return WP_Error|WP_REST_Response |
| 249 |
*/ |
| 250 |
public function update_payment_gateway_settings( WP_REST_Request $request ) { |
| 251 |
// @TODO make separate endpoints for each gateway to minimize processing time during saving. |
| 252 |
$error = new WP_Error(); |
| 253 |
foreach ( $request->get_param( 'config' ) as $gateway => $payload ) { |
| 254 |
if ( ! Payment_Gateways::init()->get_gateway( $gateway ) ) { |
| 255 |
continue; |
| 256 |
} |
| 257 |
|
| 258 |
try { |
| 259 |
do_action( "storeengine/api/settings/payment-gateways/update/$gateway", $payload ); |
| 260 |
} catch ( StoreEngineException $e ) { |
| 261 |
$error->add( $e->get_wp_error_code(), $e->getMessage(), $e->get_data() ); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
return $error->has_errors() ? $error : rest_ensure_response( true ); |
| 266 |
} |
| 267 |
|
| 268 |
public function verify_payment_gateway_settings( WP_REST_Request $request ) { |
| 269 |
if ( ! Payment_Gateways::init()->get_gateway( $request->get_param( 'method' ) ) ) { |
| 270 |
return new WP_Error( |
| 271 |
'invalid_gateway', |
| 272 |
__( 'Payment gateway doesnt exists.', 'storeengine' ), |
| 273 |
[ |
| 274 |
'status' => 404, |
| 275 |
'gateway' => $request->get_param( 'method' ), |
| 276 |
] |
| 277 |
); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
try { |
| 282 |
$gateway = $request->get_param( 'method' ); |
| 283 |
do_action( "storeengine/api/settings/payment-gateways/verify/$gateway", $request->get_param( 'config' ) ); |
| 284 |
|
| 285 |
return rest_ensure_response( true ); |
| 286 |
} catch ( StoreEngineException $e ) { |
| 287 |
return $e->toWpError(); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
public function permissions_check() { |
| 292 |
return Helper::check_rest_user_cap( 'manage_options' ); |
| 293 |
} |
| 294 |
} |
| 295 |
|