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