| 1 |
<?php |
| 2 |
/** |
| 3 |
* Merchant Get Settings Handler. |
| 4 |
* |
| 5 |
* Handles the merchant/get-module-settings ability. |
| 6 |
* Returns a module's settings with optional field schemas. |
| 7 |
* |
| 8 |
* @package Merchant |
| 9 |
* @since 2.3.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Merchant_Get_Settings_Handler |
| 18 |
* |
| 19 |
* Reads module settings using the schema generator. |
| 20 |
* Works on both active and inactive modules (inactive |
| 21 |
* modules include is_active: false in the response). |
| 22 |
* |
| 23 |
* @since 2.3.0 |
| 24 |
*/ |
| 25 |
class Merchant_Get_Settings_Handler extends Merchant_Abstract_Handler { |
| 26 |
|
| 27 |
/** |
| 28 |
* Schema generator instance. |
| 29 |
* |
| 30 |
* @var Merchant_Schema_Generator |
| 31 |
*/ |
| 32 |
private $schema_generator; |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor. |
| 36 |
* |
| 37 |
* @param Merchant_Schema_Generator $schema_generator Schema generator instance. |
| 38 |
*/ |
| 39 |
public function __construct( $schema_generator ) { |
| 40 |
$this->schema_generator = $schema_generator; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Handle the get-module-settings request. |
| 45 |
* |
| 46 |
* @param array<string, mixed> $params { module_id: string, include_schema?: bool } |
| 47 |
* |
| 48 |
* @return array<string, mixed>|WP_Error Response envelope or WP_Error on failure. |
| 49 |
*/ |
| 50 |
public function handle( $params ) { |
| 51 |
$module_id = isset( $params['module_id'] ) ? $params['module_id'] : ''; |
| 52 |
$include_schema = isset( $params['include_schema'] ) ? $this->parse_bool( $params['include_schema'], true ) : true; |
| 53 |
|
| 54 |
$error = $this->preflight( $module_id ); |
| 55 |
if ( null !== $error ) { |
| 56 |
return $error; |
| 57 |
} |
| 58 |
|
| 59 |
// Check activation status. |
| 60 |
$active_modules = get_option( 'merchant-modules', array() ); |
| 61 |
$is_active = ! empty( $active_modules[ $module_id ] ); |
| 62 |
|
| 63 |
// Generate settings via schema generator. |
| 64 |
$settings = $this->schema_generator->generate( $module_id, $include_schema ); |
| 65 |
|
| 66 |
return array( |
| 67 |
'success' => true, |
| 68 |
'data' => array( |
| 69 |
'module_id' => $module_id, |
| 70 |
'is_active' => $is_active, |
| 71 |
'settings' => $settings, |
| 72 |
), |
| 73 |
); |
| 74 |
} |
| 75 |
} |
| 76 |
|