| 1 |
<?php |
| 2 |
/** |
| 3 |
* Offline Settings - REST API for offline donation configuration |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SureDonation\Inc\Payments\Offline; |
| 9 |
|
| 10 |
use SureDonation\Inc\Payments\Payment_Helper; |
| 11 |
use SureDonation\Inc\Traits\Get_Instance; |
| 12 |
use WP_REST_Request; |
| 13 |
use WP_REST_Response; |
| 14 |
use WP_REST_Server; |
| 15 |
|
| 16 |
// Exit if accessed directly. |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Offline_Settings class |
| 23 |
* Manages offline donation REST API endpoints |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
*/ |
| 27 |
class Offline_Settings { |
| 28 |
use Get_Instance; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor |
| 32 |
* |
| 33 |
* @since 1.0.0 |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
add_action( 'rest_api_init', [ $this, 'register_routes' ] ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Register REST API routes |
| 41 |
* |
| 42 |
* @return void |
| 43 |
* @since 1.0.0 |
| 44 |
*/ |
| 45 |
public function register_routes() { |
| 46 |
// Get offline settings. |
| 47 |
register_rest_route( |
| 48 |
'suredonation/v1', |
| 49 |
'/payments/offline/settings', |
| 50 |
[ |
| 51 |
'methods' => WP_REST_Server::READABLE, |
| 52 |
'callback' => [ $this, 'get_settings' ], |
| 53 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 54 |
] |
| 55 |
); |
| 56 |
|
| 57 |
// Update offline settings. |
| 58 |
register_rest_route( |
| 59 |
'suredonation/v1', |
| 60 |
'/payments/offline/settings', |
| 61 |
[ |
| 62 |
'methods' => WP_REST_Server::EDITABLE, |
| 63 |
'callback' => [ $this, 'update_settings' ], |
| 64 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 65 |
'args' => [ |
| 66 |
'enabled' => [ |
| 67 |
'type' => 'boolean', |
| 68 |
'sanitize_callback' => 'rest_sanitize_boolean', |
| 69 |
], |
| 70 |
'instructions' => [ |
| 71 |
'type' => 'string', |
| 72 |
'sanitize_callback' => 'wp_kses_post', |
| 73 |
], |
| 74 |
], |
| 75 |
] |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get offline settings |
| 81 |
* |
| 82 |
* @param WP_REST_Request $request Request object. |
| 83 |
* @return WP_REST_Response Response object. |
| 84 |
* @since 1.0.0 |
| 85 |
*/ |
| 86 |
public function get_settings( $request ) { |
| 87 |
unset( $request ); // Unused parameter. |
| 88 |
|
| 89 |
$settings = Offline_Helper::get_all_offline_settings(); |
| 90 |
|
| 91 |
return new WP_REST_Response( |
| 92 |
[ |
| 93 |
'success' => true, |
| 94 |
'settings' => $settings, |
| 95 |
], |
| 96 |
200 |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Update offline settings |
| 102 |
* |
| 103 |
* @param WP_REST_Request $request Request object. |
| 104 |
* @return WP_REST_Response Response object. |
| 105 |
* @since 1.0.0 |
| 106 |
*/ |
| 107 |
public function update_settings( $request ) { |
| 108 |
$params = $request->get_json_params(); |
| 109 |
|
| 110 |
$settings = Offline_Helper::get_all_offline_settings(); |
| 111 |
|
| 112 |
if ( isset( $params['enabled'] ) ) { |
| 113 |
$settings['enabled'] = rest_sanitize_boolean( (bool) $params['enabled'] ); |
| 114 |
} |
| 115 |
|
| 116 |
if ( isset( $params['instructions'] ) ) { |
| 117 |
$instructions = is_string( $params['instructions'] ) ? $params['instructions'] : ''; |
| 118 |
$settings['instructions'] = wp_kses_post( $instructions ); |
| 119 |
} |
| 120 |
|
| 121 |
$updated = Payment_Helper::update_gateway_settings( 'offline', $settings ); |
| 122 |
|
| 123 |
if ( ! $updated ) { |
| 124 |
return new WP_REST_Response( |
| 125 |
[ |
| 126 |
'success' => false, |
| 127 |
'message' => __( 'Failed to update offline donation settings.', 'suredonation' ), |
| 128 |
], |
| 129 |
500 |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
return new WP_REST_Response( |
| 134 |
[ |
| 135 |
'success' => true, |
| 136 |
'message' => __( 'Offline donation settings updated successfully.', 'suredonation' ), |
| 137 |
'settings' => $settings, |
| 138 |
], |
| 139 |
200 |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Check if user has permission |
| 145 |
* |
| 146 |
* @return bool True if user has permission. |
| 147 |
* @since 1.0.0 |
| 148 |
*/ |
| 149 |
public function check_permissions() { |
| 150 |
return current_user_can( 'manage_options' ); |
| 151 |
} |
| 152 |
} |
| 153 |
|