| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Onboarding\Routes; |
| 4 |
|
| 5 |
use Give\API\RestRoute; |
| 6 |
use Give\Onboarding\Helpers\Currency; |
| 7 |
use Give\Onboarding\SettingsRepositoryFactory; |
| 8 |
use WP_REST_Request; |
| 9 |
|
| 10 |
/** |
| 11 |
* @since 2.8.0 |
| 12 |
*/ |
| 13 |
class AddonsRoute implements RestRoute |
| 14 |
{ |
| 15 |
|
| 16 |
/** @var string */ |
| 17 |
protected $endpoint = 'onboarding/settings/addons'; |
| 18 |
|
| 19 |
/** @var SettingsRepository */ |
| 20 |
protected $settingsRepository; |
| 21 |
|
| 22 |
/** |
| 23 |
* @since 2.8.0 |
| 24 |
* |
| 25 |
* @param SettingsRepository $settingsRepository |
| 26 |
* |
| 27 |
*/ |
| 28 |
public function __construct(SettingsRepositoryFactory $settingsRepositoryFactory) |
| 29 |
{ |
| 30 |
$this->settingsRepository = $settingsRepositoryFactory->make('give_onboarding'); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @since 2.8.0 |
| 35 |
* |
| 36 |
* @param WP_REST_Request $request |
| 37 |
* |
| 38 |
* @return array |
| 39 |
* |
| 40 |
*/ |
| 41 |
public function handleRequest(WP_REST_Request $request) |
| 42 |
{ |
| 43 |
$value = json_decode($request->get_param('value')); |
| 44 |
|
| 45 |
$this->settingsRepository->set('addons', $value); |
| 46 |
$this->settingsRepository->save(); |
| 47 |
|
| 48 |
return [ |
| 49 |
'data' => [ |
| 50 |
'setting' => 'addons', |
| 51 |
'value' => $value, |
| 52 |
], |
| 53 |
]; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @inheritDoc |
| 58 |
*/ |
| 59 |
public function registerRoute() |
| 60 |
{ |
| 61 |
register_rest_route( |
| 62 |
'give-api/v2', |
| 63 |
$this->endpoint, |
| 64 |
[ |
| 65 |
[ |
| 66 |
'methods' => 'POST', |
| 67 |
'callback' => [$this, 'handleRequest'], |
| 68 |
'permission_callback' => function () { |
| 69 |
return current_user_can('manage_options'); |
| 70 |
}, |
| 71 |
'args' => [ |
| 72 |
'value' => [ |
| 73 |
'type' => 'string', |
| 74 |
'required' => false, |
| 75 |
// 'validate_callback' => [ $this, 'validateValue' ], |
| 76 |
'sanitize_callback' => 'sanitize_text_field', |
| 77 |
], |
| 78 |
], |
| 79 |
], |
| 80 |
'schema' => [$this, 'getSchema'], |
| 81 |
] |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @since 2.8.0 |
| 87 |
* @return array |
| 88 |
* |
| 89 |
*/ |
| 90 |
public function getSchema() |
| 91 |
{ |
| 92 |
return [ |
| 93 |
// This tells the spec of JSON Schema we are using which is draft 4. |
| 94 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 95 |
// The title property marks the identity of the resource. |
| 96 |
'title' => 'onboarding', |
| 97 |
'type' => 'object', |
| 98 |
// In JSON Schema you can specify object properties in the properties attribute. |
| 99 |
'properties' => [ |
| 100 |
'setting' => [ |
| 101 |
'description' => esc_html__('The reference name for the setting being updated.', 'give'), |
| 102 |
'type' => 'string', |
| 103 |
], |
| 104 |
'value' => [ |
| 105 |
'description' => esc_html__('The value of the setting being updated.', 'give'), |
| 106 |
'type' => 'string', |
| 107 |
], |
| 108 |
], |
| 109 |
]; |
| 110 |
} |
| 111 |
} |
| 112 |
|