| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows Common Settings Data Query. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace CartflowsAdmin\AdminCore\Api; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
use CartflowsAdmin\AdminCore\Api\ApiBase; |
| 16 |
use CartflowsAdmin\AdminCore\Inc\AdminHelper; |
| 17 |
use CartflowsAdmin\AdminCore\Inc\GlobalSettings; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class Admin_Query. |
| 21 |
*/ |
| 22 |
class CommonSettings extends ApiBase { |
| 23 |
|
| 24 |
/** |
| 25 |
* Route base. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $rest_base = '/admin/commonsettings/'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Instance |
| 33 |
* |
| 34 |
* @access private |
| 35 |
* @var object Class object. |
| 36 |
* @since 1.0.0 |
| 37 |
*/ |
| 38 |
private static $instance; |
| 39 |
|
| 40 |
/** |
| 41 |
* Initiator |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
* @return object initialized object of class. |
| 45 |
*/ |
| 46 |
public static function get_instance() { |
| 47 |
if ( ! isset( self::$instance ) ) { |
| 48 |
self::$instance = new self(); |
| 49 |
} |
| 50 |
return self::$instance; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Constructor |
| 55 |
*/ |
| 56 |
public function __construct() { |
| 57 |
|
| 58 |
add_filter( 'cartflows_admin_global_data_options', array( $this, 'add_other_option_data' ), 10, 1 ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Add flow meta options. |
| 63 |
* |
| 64 |
* @param array $options options. |
| 65 |
* @return array. |
| 66 |
*/ |
| 67 |
public function add_other_option_data( $options ) { |
| 68 |
|
| 69 |
$options['cartflows_delete_plugin_data'] = get_option( 'cartflows_delete_plugin_data' ); |
| 70 |
$options['cartflows_stats_report_emails'] = get_option( 'cartflows_stats_report_emails', 'enable' ); |
| 71 |
$options['cf_usage_optin'] = get_site_option( 'cf_usage_optin', 'no' ); |
| 72 |
$options['cartflows_stats_report_email_ids'] = get_option( 'cartflows_stats_report_email_ids', get_option( 'admin_email' ) ); |
| 73 |
|
| 74 |
// The toggle field expects an 'enable'/'disable' string (backComp). Older code paths |
| 75 |
// may have stored a boolean true (set by 3.0 migration); coerce to 'enable' so the |
| 76 |
// switch reflects the correct on-state without changing what is persisted. |
| 77 |
$legacy_admin_raw = get_option( 'cartflows-legacy-admin', 'disable' ); |
| 78 |
$options['cartflows-legacy-admin'] = in_array( $legacy_admin_raw, array( true, 1, '1', 'enable', 'yes' ), true ) ? 'enable' : 'disable'; |
| 79 |
|
| 80 |
return $options; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Init Hooks. |
| 85 |
* |
| 86 |
* @since 1.0.0 |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function register_routes() { |
| 90 |
|
| 91 |
$namespace = $this->get_api_namespace(); |
| 92 |
|
| 93 |
register_rest_route( |
| 94 |
$namespace, |
| 95 |
$this->rest_base, |
| 96 |
array( |
| 97 |
array( |
| 98 |
'methods' => \WP_REST_Server::READABLE, |
| 99 |
'callback' => array( $this, 'get_common_settings' ), |
| 100 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 101 |
'args' => array(), |
| 102 |
), |
| 103 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 104 |
) |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get common settings. |
| 110 |
* |
| 111 |
* @param WP_REST_Request $request Full details about the request. |
| 112 |
*/ |
| 113 |
public function get_common_settings( $request ) { |
| 114 |
|
| 115 |
$settings = GlobalSettings::get_global_settings_fields(); |
| 116 |
$options = AdminHelper::get_options(); |
| 117 |
|
| 118 |
$global_settings = array( |
| 119 |
'settings' => $settings, |
| 120 |
'options' => $options, |
| 121 |
); |
| 122 |
|
| 123 |
return $global_settings; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Check whether a given request has permission to read notes. |
| 128 |
* |
| 129 |
* @param WP_REST_Request $request Full details about the request. |
| 130 |
* @return WP_Error|boolean |
| 131 |
*/ |
| 132 |
public function get_items_permissions_check( $request ) { |
| 133 |
|
| 134 |
// Security: Settings endpoint requires cartflows_manage_settings to prevent sensitive data exposure. |
| 135 |
if ( ! current_user_can( 'cartflows_manage_settings' ) ) { |
| 136 |
return new \WP_Error( 'cartflows_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'cartflows' ), array( 'status' => rest_authorization_required_code() ) ); |
| 137 |
} |
| 138 |
|
| 139 |
return true; |
| 140 |
} |
| 141 |
} |
| 142 |
|