| 1 |
<?php |
| 2 |
|
| 3 |
namespace AffiliateX\Modules; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use WP_REST_Request; |
| 8 |
|
| 9 |
/** |
| 10 |
* Modules REST API |
| 11 |
* |
| 12 |
* @package AffiliateX |
| 13 |
*/ |
| 14 |
class ModulesAPI { |
| 15 |
use \AffiliateX\Helpers\ResponseHelper; |
| 16 |
|
| 17 |
/** |
| 18 |
* Default module states |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
/** |
| 23 |
* Get default module states. |
| 24 |
* Pro modules default to true when pro is active. |
| 25 |
* |
| 26 |
* @return array |
| 27 |
*/ |
| 28 |
private static function get_defaults(): array { |
| 29 |
$is_pro = function_exists( 'affiliatex_fs' ) && affiliatex_fs()->is__premium_only(); |
| 30 |
|
| 31 |
return array( |
| 32 |
'analytics' => true, |
| 33 |
'api_integration' => $is_pro, |
| 34 |
'email_reports' => $is_pro, |
| 35 |
'broken_link_checker' => true, |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Register REST routes |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function register_routes(): void { |
| 45 |
register_rest_route( |
| 46 |
'affiliatex/v1/modules', |
| 47 |
'/status', |
| 48 |
array( |
| 49 |
'methods' => 'GET', |
| 50 |
'callback' => array( $this, 'get_status' ), |
| 51 |
'permission_callback' => function () { |
| 52 |
return current_user_can( 'manage_options' ); |
| 53 |
}, |
| 54 |
) |
| 55 |
); |
| 56 |
|
| 57 |
register_rest_route( |
| 58 |
'affiliatex/v1/modules', |
| 59 |
'/toggle', |
| 60 |
array( |
| 61 |
'methods' => 'POST', |
| 62 |
'callback' => array( $this, 'toggle_module' ), |
| 63 |
'permission_callback' => function () { |
| 64 |
return current_user_can( 'manage_options' ); |
| 65 |
}, |
| 66 |
) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get all module statuses |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
public function get_status(): void { |
| 76 |
$saved = get_option( 'affiliatex_modules', array() ); |
| 77 |
$statuses = wp_parse_args( $saved, $this->get_defaults() ); |
| 78 |
$is_pro = function_exists( 'affiliatex_fs' ) && affiliatex_fs()->is__premium_only(); |
| 79 |
|
| 80 |
if ( ! $is_pro ) { |
| 81 |
$statuses['api_integration'] = false; |
| 82 |
$statuses['email_reports'] = false; |
| 83 |
} |
| 84 |
|
| 85 |
$this->send_json_plain_success( |
| 86 |
array( |
| 87 |
'modules' => $statuses, |
| 88 |
'is_pro' => $is_pro, |
| 89 |
) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Toggle a module on/off |
| 95 |
* |
| 96 |
* @param WP_REST_Request $request Request object. |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
public function toggle_module( WP_REST_Request $request ): void { |
| 100 |
$body = $request->get_json_params(); |
| 101 |
$module = isset( $body['module'] ) ? sanitize_text_field( $body['module'] ) : ''; |
| 102 |
$enabled = isset( $body['enabled'] ) ? (bool) $body['enabled'] : false; |
| 103 |
|
| 104 |
if ( empty( $module ) || ! array_key_exists( $module, $this->get_defaults() ) ) { |
| 105 |
$this->send_json_error( __( 'Invalid module', 'affiliatex' ) ); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
$saved = get_option( 'affiliatex_modules', array() ); |
| 110 |
$saved[ $module ] = $enabled; |
| 111 |
update_option( 'affiliatex_modules', $saved ); |
| 112 |
|
| 113 |
$this->send_json_plain_success( |
| 114 |
array( |
| 115 |
'module' => $module, |
| 116 |
'enabled' => $enabled, |
| 117 |
) |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Check if a module is enabled |
| 123 |
* |
| 124 |
* @param string $module_id Module identifier. |
| 125 |
* @return bool |
| 126 |
*/ |
| 127 |
public static function is_enabled( string $module_id ): bool { |
| 128 |
$saved = get_option( 'affiliatex_modules', array() ); |
| 129 |
$statuses = wp_parse_args( $saved, self::get_defaults() ); |
| 130 |
$is_pro = function_exists( 'affiliatex_fs' ) && affiliatex_fs()->is__premium_only(); |
| 131 |
|
| 132 |
$pro_only = array( 'api_integration', 'email_reports' ); |
| 133 |
if ( ! $is_pro && in_array( $module_id, $pro_only, true ) ) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
return ! empty( $statuses[ $module_id ] ); |
| 138 |
} |
| 139 |
} |
| 140 |
|