| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\API; |
| 4 |
|
| 5 |
use Templately\Core\Developer; |
| 6 |
use WP_REST_Request; |
| 7 |
|
| 8 |
/** |
| 9 |
* Developer Settings API |
| 10 |
* |
| 11 |
* Handles developer-only settings that are available when TEMPLATELY_DEVELOPER_MODE is enabled |
| 12 |
* |
| 13 |
* @since 3.3.4 |
| 14 |
*/ |
| 15 |
class DeveloperSettings extends API { |
| 16 |
|
| 17 |
/** |
| 18 |
* @param $request WP_REST_Request |
| 19 |
* @return bool |
| 20 |
*/ |
| 21 |
public function permission_check( WP_REST_Request $request ) { |
| 22 |
// Only allow access if developer mode is enabled and user has manage_options capability |
| 23 |
return Developer::is_developer_mode_enabled() && current_user_can( 'manage_options' ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Register API routes |
| 28 |
*/ |
| 29 |
public function register_routes() { |
| 30 |
$this->get( 'developer-settings', [ $this, 'get_settings' ] ); |
| 31 |
$this->post( 'developer-settings', [ $this, 'update_settings' ] ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get current developer settings |
| 36 |
* |
| 37 |
* @return array |
| 38 |
*/ |
| 39 |
public function get_settings() { |
| 40 |
$settings = Developer::get_settings(); |
| 41 |
$config = Developer::get_available_constants(); |
| 42 |
$override_status = Developer::get_constant_override_status(); |
| 43 |
|
| 44 |
return $this->success( [ |
| 45 |
'success' => true, |
| 46 |
'data' => $settings, |
| 47 |
'config' => $config, |
| 48 |
'override_status' => $override_status, |
| 49 |
] ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Update developer settings |
| 54 |
* |
| 55 |
* Note: This method saves settings to the database but they won't take effect |
| 56 |
* until the constants are updated in wp-config.php or the master constant is used. |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public function update_settings() { |
| 61 |
$settings = $this->get_param( 'settings', [] ); |
| 62 |
|
| 63 |
if ( empty( $settings ) ) { |
| 64 |
return $this->error( 'invalid_settings', __( 'No settings provided.', 'templately' ), 'update_settings', 400 ); |
| 65 |
} |
| 66 |
|
| 67 |
// Use Developer class to update settings |
| 68 |
$success = Developer::update_settings( $settings ); |
| 69 |
|
| 70 |
if ( ! $success ) { |
| 71 |
return $this->error( 'update_failed', __( 'Failed to update developer settings.', 'templately' ), 'update_settings', 500 ); |
| 72 |
} |
| 73 |
|
| 74 |
// Get the sanitized settings |
| 75 |
$sanitized_settings = Developer::sanitize_settings( $settings ); |
| 76 |
|
| 77 |
return $this->success( [ |
| 78 |
'success' => true, |
| 79 |
'data' => [ |
| 80 |
'message' => __( 'Developer settings saved successfully.', 'templately' ), |
| 81 |
'settings' => $sanitized_settings, |
| 82 |
'note' => __( 'Settings have been saved to the database. To activate them, add the generated constants to your wp-config.php file or ensure TEMPLATELY_DEVELOPER_MODE is enabled.', 'templately' ) |
| 83 |
], |
| 84 |
] ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get all available developer constants and their descriptions |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public function get_available_constants() { |
| 93 |
$constants = Developer::get_available_constants(); |
| 94 |
return $this->success( $constants ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Check if network admin functionality is available |
| 99 |
* |
| 100 |
* @return array |
| 101 |
*/ |
| 102 |
public function get_network_admin_status() { |
| 103 |
$status = Developer::get_network_admin_status(); |
| 104 |
return $this->success( $status ); |
| 105 |
} |
| 106 |
} |
| 107 |
|