| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Image SEO API Endpoints Class |
| 5 |
* |
| 6 |
* REST API endpoints for image SEO management. |
| 7 |
* |
| 8 |
* @package ThinkRank |
| 9 |
* @subpackage API |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
declare(strict_types=1); |
| 14 |
|
| 15 |
namespace ThinkRank\API; |
| 16 |
|
| 17 |
use ThinkRank\SEO\Image_SEO_Manager; |
| 18 |
use ThinkRank\API\Traits\CSRF_Protection; |
| 19 |
use WP_REST_Controller; |
| 20 |
use WP_REST_Request; |
| 21 |
use WP_REST_Response; |
| 22 |
use WP_Error; |
| 23 |
|
| 24 |
/** |
| 25 |
* Image SEO API Endpoints Class |
| 26 |
* |
| 27 |
* Provides REST API endpoints for image SEO settings management. |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
*/ |
| 31 |
class Image_SEO_Endpoint extends WP_REST_Controller { |
| 32 |
use CSRF_Protection; |
| 33 |
|
| 34 |
/** |
| 35 |
* Image SEO Manager instance |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* @var Image_SEO_Manager |
| 39 |
*/ |
| 40 |
private Image_SEO_Manager $image_manager; |
| 41 |
|
| 42 |
/** |
| 43 |
* API namespace |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
protected $namespace = 'thinkrank/v1'; |
| 49 |
|
| 50 |
/** |
| 51 |
* API resource base |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* @var string |
| 55 |
*/ |
| 56 |
protected $rest_base = 'image-seo'; |
| 57 |
|
| 58 |
/** |
| 59 |
* Constructor |
| 60 |
* |
| 61 |
* @since 1.0.0 |
| 62 |
*/ |
| 63 |
public function __construct() { |
| 64 |
if (!class_exists('ThinkRank\\SEO\\Image_SEO_Manager')) { |
| 65 |
require_once THINKRANK_PLUGIN_DIR . 'includes/seo/class-image-seo-manager.php'; |
| 66 |
} |
| 67 |
|
| 68 |
$this->image_manager = new Image_SEO_Manager(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Register API routes |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
*/ |
| 76 |
public function register_routes(): void { |
| 77 |
register_rest_route( |
| 78 |
$this->namespace, |
| 79 |
'/' . $this->rest_base . '/settings', |
| 80 |
[ |
| 81 |
[ |
| 82 |
'methods' => 'GET', |
| 83 |
'callback' => [$this, 'get_settings'], |
| 84 |
'permission_callback' => [$this, 'check_permissions'] |
| 85 |
], |
| 86 |
[ |
| 87 |
'methods' => 'POST', |
| 88 |
'callback' => [$this, 'update_settings'], |
| 89 |
'permission_callback' => [$this, 'check_permissions'], |
| 90 |
'args' => $this->get_settings_args() |
| 91 |
] |
| 92 |
] |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Check if user has required permissions |
| 98 |
* |
| 99 |
* @since 1.0.0 |
| 100 |
* @return bool True if authorized, false otherwise |
| 101 |
*/ |
| 102 |
public function check_permissions(): bool { |
| 103 |
return current_user_can('manage_options'); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get image SEO settings |
| 108 |
* |
| 109 |
* @since 1.0.0 |
| 110 |
* @param WP_REST_Request $request API request object |
| 111 |
* @return WP_REST_Response|WP_Error API response |
| 112 |
*/ |
| 113 |
public function get_settings(WP_REST_Request $request): WP_REST_Response { |
| 114 |
$settings = $this->image_manager->get_settings('site'); |
| 115 |
return new WP_REST_Response($settings, 200); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Update image SEO settings |
| 120 |
* |
| 121 |
* @since 1.0.0 |
| 122 |
* @param WP_REST_Request $request API request object |
| 123 |
* @return WP_REST_Response|WP_Error API response |
| 124 |
*/ |
| 125 |
public function update_settings(WP_REST_Request $request): WP_REST_Response { |
| 126 |
$settings = $request->get_params(); |
| 127 |
|
| 128 |
// Remove nonce from settings |
| 129 |
if (isset($settings['_wpnonce'])) { |
| 130 |
unset($settings['_wpnonce']); |
| 131 |
} |
| 132 |
|
| 133 |
$success = $this->image_manager->save_settings('site', 0, $settings); |
| 134 |
|
| 135 |
if ($success) { |
| 136 |
return new WP_REST_Response([ |
| 137 |
'success' => true, |
| 138 |
'message' => __('Settings updated successfully', 'thinkrank') |
| 139 |
], 200); |
| 140 |
} |
| 141 |
|
| 142 |
return new WP_REST_Response([ |
| 143 |
'success' => false, |
| 144 |
'message' => __('Failed to update settings', 'thinkrank') |
| 145 |
], 500); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Get settings schema arguments |
| 150 |
* |
| 151 |
* @since 1.0.0 |
| 152 |
* @return array API arguments array |
| 153 |
*/ |
| 154 |
private function get_settings_args(): array { |
| 155 |
$schema = $this->image_manager->get_settings_schema('site'); |
| 156 |
$args = []; |
| 157 |
|
| 158 |
foreach ($schema as $key => $config) { |
| 159 |
$args[$key] = [ |
| 160 |
'type' => $config['type'], |
| 161 |
'required' => false, |
| 162 |
'sanitize_callback' => $config['type'] === 'boolean' ? 'rest_sanitize_boolean' : 'sanitize_text_field' |
| 163 |
]; |
| 164 |
} |
| 165 |
|
| 166 |
return $args; |
| 167 |
} |
| 168 |
} |
| 169 |
|