| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageHub; |
| 4 |
|
| 5 |
use ImageHub\Utils; |
| 6 |
|
| 7 |
class Settings |
| 8 |
{ |
| 9 |
|
| 10 |
public $settings = []; |
| 11 |
|
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
add_action('init', array($this, 'image_hub_register_settings')); |
| 15 |
add_action('init', array($this, 'image_hub_get_settings')); |
| 16 |
add_action('admin_menu', array($this, 'image_hub_add_settings_page')); |
| 17 |
add_action('admin_enqueue_scripts', array($this, 'image_hub_enqueue_admin_settings_assets')); |
| 18 |
add_action('wp_ajax_image_hub_update_settings', array($this, 'image_hub_update_settings')); |
| 19 |
|
| 20 |
$this->image_hub_get_settings(); |
| 21 |
} |
| 22 |
|
| 23 |
public function image_hub_update_settings() |
| 24 |
{ |
| 25 |
|
| 26 |
if ( |
| 27 |
!isset($_POST['nonce']) || |
| 28 |
!wp_verify_nonce(sanitize_key(wp_unslash($_POST['nonce'])), 'image_hub_nonce') |
| 29 |
) { |
| 30 |
wp_send_json_error('Nonce verification failed.'); |
| 31 |
} |
| 32 |
|
| 33 |
if (!isset($_POST['data'])) { |
| 34 |
wp_send_json_error('No data received.'); |
| 35 |
} |
| 36 |
|
| 37 |
$safeData = sanitize_text_field(wp_unslash($_POST['data'])); |
| 38 |
|
| 39 |
$updates = json_decode(stripslashes($safeData), true); |
| 40 |
if (!is_array($updates)) { |
| 41 |
wp_send_json_error('Invalid data format.'); |
| 42 |
} |
| 43 |
|
| 44 |
$valid_keys = array( |
| 45 |
'image_hub_api_keys_giphy_api_key', |
| 46 |
'image_hub_api_keys_openverse_client_id', |
| 47 |
'image_hub_api_keys_openverse_client_secret', |
| 48 |
'image_hub_api_keys_pexels_api_key', |
| 49 |
'image_hub_api_keys_pixabay_api_key', |
| 50 |
'image_hub_api_keys_unsplash_access_key', |
| 51 |
'image_hub_max_image_height', |
| 52 |
'image_hub_max_image_width', |
| 53 |
'image_hub_enable_unsplash', |
| 54 |
'image_hub_enable_openverse', |
| 55 |
'image_hub_enable_pixabay', |
| 56 |
'image_hub_enable_pexels', |
| 57 |
'image_hub_enable_giphy', |
| 58 |
'image_hub_enable_image_attribution', |
| 59 |
'image_hub_enable_media_modal', |
| 60 |
'image_hub_use_default_keys' |
| 61 |
); |
| 62 |
$updated = []; |
| 63 |
|
| 64 |
foreach ($updates as $key => $value) { |
| 65 |
if (in_array($key, $valid_keys)) { |
| 66 |
if (strpos($key, 'enable_') !== false) { |
| 67 |
$value = (bool)$value ? 1 : 0; |
| 68 |
} |
| 69 |
update_option($key, $value); |
| 70 |
$updated[] = $key; |
| 71 |
} |
| 72 |
} |
| 73 |
wp_send_json_success(['updated_keys' => $updated]); |
| 74 |
} |
| 75 |
|
| 76 |
public function image_hub_get_settings() |
| 77 |
{ |
| 78 |
$this->settings = array( |
| 79 |
'image_hub_api_keys_giphy_api_key' => get_option('image_hub_api_keys_giphy_api_key'), |
| 80 |
'image_hub_api_keys_openverse_client_id' => get_option('image_hub_api_keys_openverse_client_id'), |
| 81 |
'image_hub_api_keys_openverse_client_secret' => get_option('image_hub_api_keys_openverse_client_secret'), |
| 82 |
'image_hub_api_keys_pexels_api_key' => get_option('image_hub_api_keys_pexels_api_key'), |
| 83 |
'image_hub_api_keys_pixabay_api_key' => get_option('image_hub_api_keys_pixabay_api_key'), |
| 84 |
'image_hub_api_keys_unsplash_access_key' => get_option('image_hub_api_keys_unsplash_access_key'), |
| 85 |
'image_hub_max_image_height' => get_option('image_hub_max_image_height'), |
| 86 |
'image_hub_max_image_width' => get_option('image_hub_max_image_width'), |
| 87 |
'image_hub_enable_unsplash' => (bool) get_option('image_hub_enable_unsplash', true), |
| 88 |
'image_hub_enable_openverse' => (bool) get_option('image_hub_enable_openverse', true), |
| 89 |
'image_hub_enable_pixabay' => (bool) get_option('image_hub_enable_pixabay', true), |
| 90 |
'image_hub_enable_pexels' => (bool) get_option('image_hub_enable_pexels', true), |
| 91 |
'image_hub_enable_giphy' => (bool) get_option('image_hub_enable_giphy', true), |
| 92 |
'image_hub_enable_image_attribution' => (bool) get_option('image_hub_enable_image_attribution', false), |
| 93 |
'image_hub_enable_media_modal' => (bool) get_option('image_hub_enable_media_modal', true), |
| 94 |
'image_hub_use_default_keys' => (bool) get_option('image_hub_use_default_keys', true), |
| 95 |
); |
| 96 |
} |
| 97 |
function image_hub_force_store_boolean($value) |
| 98 |
{ |
| 99 |
$value = sanitize_text_field($value); |
| 100 |
return (bool) $value; |
| 101 |
} |
| 102 |
function image_hub_sanitize_number($value) |
| 103 |
{ |
| 104 |
$value = sanitize_text_field($value); |
| 105 |
return absint($value); |
| 106 |
} |
| 107 |
public function image_hub_register_settings() |
| 108 |
{ |
| 109 |
|
| 110 |
// phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 111 |
register_setting( |
| 112 |
'image_hub_options', |
| 113 |
'image_hub_api_keys_giphy_api_key', |
| 114 |
[ |
| 115 |
'type' => 'string', |
| 116 |
'sanitize_callback' => 'sanitize_text_field', |
| 117 |
'show_in_rest' => true, |
| 118 |
] |
| 119 |
); |
| 120 |
// phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 121 |
register_setting('image_hub_options', 'image_hub_api_keys_openverse_client_id', [ |
| 122 |
'type' => 'string', |
| 123 |
'sanitize_callback' => 'sanitize_text_field', |
| 124 |
'show_in_rest' => true, |
| 125 |
]); |
| 126 |
// phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 127 |
register_setting('image_hub_options', 'image_hub_api_keys_openverse_client_secret', [ |
| 128 |
'type' => 'string', |
| 129 |
'sanitize_callback' => 'sanitize_text_field', |
| 130 |
'show_in_rest' => true, |
| 131 |
]); |
| 132 |
// phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 133 |
register_setting('image_hub_options', 'image_hub_api_keys_pexels_api_key', [ |
| 134 |
'type' => 'string', |
| 135 |
'sanitize_callback' => 'sanitize_text_field', |
| 136 |
'show_in_rest' => true, |
| 137 |
]); |
| 138 |
// phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 139 |
register_setting('image_hub_options', 'image_hub_api_keys_pixabay_api_key', [ |
| 140 |
'type' => 'string', |
| 141 |
'sanitize_callback' => 'sanitize_text_field', |
| 142 |
'show_in_rest' => true, |
| 143 |
]); |
| 144 |
// phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 145 |
register_setting('image_hub_options', 'image_hub_api_keys_unsplash_access_key', [ |
| 146 |
'type' => 'string', |
| 147 |
'sanitize_callback' => 'sanitize_text_field', |
| 148 |
'show_in_rest' => true, |
| 149 |
]); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
// phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 157 |
register_setting( |
| 158 |
'image_hub_options', |
| 159 |
'image_hub_max_image_height', |
| 160 |
array( |
| 161 |
'type' => 'integer', |
| 162 |
'default' => 1200, |
| 163 |
'sanitize_callback' => array($this, 'image_hub_sanitize_number'), |
| 164 |
'show_in_rest' => true, |
| 165 |
) |
| 166 |
); |
| 167 |
// phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 168 |
register_setting( |
| 169 |
'image_hub_options', |
| 170 |
'image_hub_max_image_width', |
| 171 |
array( |
| 172 |
'type' => 'integer', |
| 173 |
'default' => 1600, |
| 174 |
'sanitize_callback' => array($this,'image_hub_sanitize_number'), |
| 175 |
'show_in_rest' => true, |
| 176 |
) |
| 177 |
); |
| 178 |
|
| 179 |
$providers = ['unsplash', 'openverse', 'pixabay', 'pexels', 'giphy']; |
| 180 |
|
| 181 |
foreach ($providers as $provider) { |
| 182 |
|
| 183 |
// phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 184 |
register_setting( |
| 185 |
'image_hub_options', |
| 186 |
'image_hub_enable_' . $provider, |
| 187 |
array( |
| 188 |
'type' => 'boolean', |
| 189 |
'default' => true, |
| 190 |
'sanitize_callback' => array($this, 'image_hub_force_store_boolean'), |
| 191 |
'show_in_rest' => true, |
| 192 |
) |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
// phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 197 |
register_setting( |
| 198 |
'image_hub_options', |
| 199 |
'image_hub_enable_image_attribution', |
| 200 |
array( |
| 201 |
'type' => 'boolean', |
| 202 |
'default' => false, |
| 203 |
'sanitize_callback' => array($this, 'image_hub_force_store_boolean'), |
| 204 |
'show_in_rest' => true, |
| 205 |
) |
| 206 |
); |
| 207 |
// phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 208 |
register_setting( |
| 209 |
'image_hub_options', |
| 210 |
'image_hub_enable_media_modal', |
| 211 |
array( |
| 212 |
'type' => 'boolean', |
| 213 |
'default' => true, |
| 214 |
'sanitize_callback' => array($this, 'image_hub_force_store_boolean'), |
| 215 |
'show_in_rest' => true, |
| 216 |
) |
| 217 |
); |
| 218 |
register_setting( |
| 219 |
'image_hub_options', |
| 220 |
'image_hub_use_default_keys', |
| 221 |
array( |
| 222 |
'type' => 'boolean', |
| 223 |
'default' => true, |
| 224 |
'sanitize_callback' => array($this, 'image_hub_force_store_boolean'), |
| 225 |
'show_in_rest' => true, |
| 226 |
) |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
public function image_hub_add_settings_page() |
| 232 |
{ |
| 233 |
add_options_page( |
| 234 |
"Image Hub Settings", |
| 235 |
"Image Hub", |
| 236 |
'manage_options', |
| 237 |
'image-hub-settings-page', |
| 238 |
array($this, 'image_hub_settings_page_renderer') |
| 239 |
); |
| 240 |
} |
| 241 |
|
| 242 |
public function image_hub_settings_page_renderer() |
| 243 |
{ |
| 244 |
?> |
| 245 |
<div class="wrap"> |
| 246 |
<div id="image-hub-settings-root"></div> |
| 247 |
</div> |
| 248 |
<?php |
| 249 |
} |
| 250 |
|
| 251 |
public function image_hub_enqueue_admin_settings_assets($hook) |
| 252 |
{ |
| 253 |
if ('settings_page_image-hub-settings-page' !== $hook) { |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
wp_enqueue_style( |
| 258 |
'image-hub-style', |
| 259 |
IMAGE_HUB_ROOT_URL . '/assets/css/settings.css', |
| 260 |
array(), |
| 261 |
Utils::get_version() |
| 262 |
); |
| 263 |
|
| 264 |
Utils::enque_image_hub_script('settings', true); |
| 265 |
wp_enqueue_style('wp-components'); |
| 266 |
|
| 267 |
wp_localize_script('image-hub-settings', 'image_hub_settings', $this->settings); |
| 268 |
Utils::localize_props('image-hub-settings'); |
| 269 |
|
| 270 |
wp_set_script_translations('image-hub-settings', 'image-hub'); |
| 271 |
} |
| 272 |
} |
| 273 |
|