| 1 |
<?php |
| 2 |
/** |
| 3 |
* Settings management and UI |
| 4 |
* |
| 5 |
* @since 0.2.0 |
| 6 |
*/ |
| 7 |
namespace wpCloud\StatelessMedia { |
| 8 |
|
| 9 |
if( !class_exists( 'wpCloud\StatelessMedia\Settings' ) ) { |
| 10 |
|
| 11 |
final class Settings extends \UDX\Settings { |
| 12 |
|
| 13 |
const DEFAULT_CACHE_CONTROL = 'public, max-age=36000, must-revalidate'; |
| 14 |
const SETUP_MESSAGE_KEY = 'finish-setup'; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var false|null|string |
| 18 |
*/ |
| 19 |
public $setup_wizard_ui = null; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var Array |
| 23 |
*/ |
| 24 |
public $wildcards = array(); |
| 25 |
|
| 26 |
/** |
| 27 |
* @var false|null|string |
| 28 |
*/ |
| 29 |
public $stateless_settings = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
private $plugin_file = ''; |
| 35 |
|
| 36 |
/** |
| 37 |
* Instance of |
| 38 |
* - ud_get_stateless_media |
| 39 |
* - wpCloud\StatelessMedia\Bootstrap |
| 40 |
* @var false|null|string |
| 41 |
*/ |
| 42 |
public $bootstrap = null; |
| 43 |
|
| 44 |
private $settings = array( |
| 45 |
'mode' => array('WP_STATELESS_MEDIA_MODE', 'ephemeral'), |
| 46 |
'body_rewrite' => array('WP_STATELESS_MEDIA_BODY_REWRITE', 'false'), |
| 47 |
'body_rewrite_types' => array('WP_STATELESS_MEDIA_BODY_REWRITE_TYPES', 'jpg jpeg png gif pdf'), |
| 48 |
'bucket' => array('WP_STATELESS_MEDIA_BUCKET', ''), |
| 49 |
'root_dir' => array('WP_STATELESS_MEDIA_ROOT_DIR', ['/%date_year/date_month%/', '/sites/%site_id%/%date_year/date_month%/']), |
| 50 |
'key_json' => array('WP_STATELESS_MEDIA_JSON_KEY', ''), |
| 51 |
'cache_control' => array('WP_STATELESS_MEDIA_CACHE_CONTROL', ''), |
| 52 |
'delete_remote' => array('WP_STATELESS_MEDIA_DELETE_REMOTE', 'true'), |
| 53 |
'custom_domain' => array('WP_STATELESS_MEDIA_CUSTOM_DOMAIN', ''), |
| 54 |
'organize_media' => array('', 'true'), |
| 55 |
'hashify_file_name' => array(['WP_STATELESS_MEDIA_HASH_FILENAME' => 'WP_STATELESS_MEDIA_CACHE_BUSTING'], 'false'), |
| 56 |
'dynamic_image_support' => array(['WP_STATELESS_MEDIA_ON_FLY' => 'WP_STATELESS_DYNAMIC_IMAGE_SUPPORT'], 'false'), |
| 57 |
'status_email_type' => array('', 'true'), |
| 58 |
'status_email_address' => array('', ''), |
| 59 |
'use_postmeta' => array('WP_STATELESS_POSTMETA', ['false', '']), |
| 60 |
'use_api_siteurl' => array('WP_STATELESS_API_SITEURL', ['WP_HOME', '']), |
| 61 |
); |
| 62 |
|
| 63 |
private $network_only_settings = array( |
| 64 |
'hide_settings_panel' => array('WP_STATELESS_MEDIA_HIDE_SETTINGS_PANEL', false), |
| 65 |
'hide_setup_assistant' => array('WP_STATELESS_MEDIA_HIDE_SETUP_ASSISTANT', false), |
| 66 |
); |
| 67 |
|
| 68 |
private $strings = array( |
| 69 |
'network' => 'Currently configured via Network Settings.', |
| 70 |
'constant' => 'Currently configured via a constant.', |
| 71 |
'environment' => 'Currently configured via an environment variable.', |
| 72 |
); |
| 73 |
|
| 74 |
/** |
| 75 |
* |
| 76 |
* Settings constructor. |
| 77 |
* @param null $bootstrap |
| 78 |
*/ |
| 79 |
public function __construct($bootstrap = null) { |
| 80 |
$this->bootstrap = $bootstrap ? $bootstrap : ud_get_stateless_media(); |
| 81 |
|
| 82 |
// Defile the main plugin file |
| 83 |
$realpath = realpath( __DIR__ . '/../..'); |
| 84 |
$this->plugin_file = basename( $realpath ) . '/wp-stateless-media.php'; |
| 85 |
|
| 86 |
/* Add 'Settings' link for SM plugin on plugins page. */ |
| 87 |
$_basename = plugin_basename( $this->bootstrap->boot_file ); |
| 88 |
|
| 89 |
parent::__construct( array( |
| 90 |
'store' => 'options', |
| 91 |
'format' => 'json', |
| 92 |
'data' => array( |
| 93 |
'sm' => array() |
| 94 |
) |
| 95 |
)); |
| 96 |
|
| 97 |
add_action('activated_plugin', array( $this, 'check_setup' )); |
| 98 |
add_action('admin_notices', array( $this, 'documentation_message' ), 1); |
| 99 |
|
| 100 |
// Setting sm variable |
| 101 |
$this->refresh(); |
| 102 |
|
| 103 |
$this->set('page_url.stateless_setup', $this->bootstrap->get_settings_page_url('?page=stateless-setup')); |
| 104 |
$this->set('page_url.stateless_settings', $this->bootstrap->get_settings_page_url('?page=stateless-settings')); |
| 105 |
|
| 106 |
/** Register options */ |
| 107 |
add_action( 'init', array( $this, 'init' ), 3 ); |
| 108 |
|
| 109 |
// additional links on plugins page |
| 110 |
add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2); |
| 111 |
add_filter( 'network_admin_plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2); |
| 112 |
add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 ); |
| 113 |
|
| 114 |
// apply wildcard to root dir. |
| 115 |
add_filter( 'wp_stateless_handle_root_dir', array( $this, 'root_dir_wildcards' ), 10, 3); |
| 116 |
|
| 117 |
// Parse root dir by wildcards |
| 118 |
add_filter( 'wp_stateless_unhandle_root_dir', array( $this, 'parse_root_dir_wildcards' ), 10, 3); |
| 119 |
|
| 120 |
// Settings page content |
| 121 |
add_action('wp_stateless_settings_tab_content', array($this, 'settings_tab_content')); |
| 122 |
add_action('wp_stateless_processing_tab_content', array($this, 'processing_tab_content')); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Init |
| 127 |
*/ |
| 128 |
public function init(){ |
| 129 |
$this->save_media_settings(); |
| 130 |
|
| 131 |
add_action('admin_menu', array( $this, 'admin_menu' )); |
| 132 |
|
| 133 |
/** |
| 134 |
* Manage specific Network Settings |
| 135 |
*/ |
| 136 |
if( is_network_admin() ) { |
| 137 |
add_action( 'network_admin_menu', array( $this, 'network_admin_menu' )); |
| 138 |
} |
| 139 |
|
| 140 |
$site_url = parse_url( site_url() ); |
| 141 |
$site_url['path'] = isset($site_url['path']) ? $site_url['path'] : ''; |
| 142 |
$this->wildcards = array( |
| 143 |
'sites' => [ |
| 144 |
'sites', |
| 145 |
__("sites", $this->bootstrap->domain), |
| 146 |
__("Sites uses for multisite.", $this->bootstrap->domain), |
| 147 |
], |
| 148 |
'%site_id%' => [ |
| 149 |
get_current_blog_id(), |
| 150 |
__("site id", $this->bootstrap->domain), |
| 151 |
__("Site ID, for example 1.", $this->bootstrap->domain), |
| 152 |
], |
| 153 |
'%site_url%' => [ |
| 154 |
trim( $site_url['host'] . $site_url['path'], '/ ' ), |
| 155 |
__("site url", $this->bootstrap->domain), |
| 156 |
__("Site URL, for example example.com/site-1.", $this->bootstrap->domain), |
| 157 |
], |
| 158 |
'%site_url_host%' => [ |
| 159 |
trim( $site_url['host'], '/ ' ), |
| 160 |
__("host name", $this->bootstrap->domain), |
| 161 |
__("Host name, for example example.com.", $this->bootstrap->domain), |
| 162 |
], |
| 163 |
'%site_url_path%' => [ |
| 164 |
trim( $site_url['path'], '/ ' ), |
| 165 |
__("site path", $this->bootstrap->domain), |
| 166 |
__("Site path, for example site-1.", $this->bootstrap->domain), |
| 167 |
], |
| 168 |
'%date_year/date_month%' => [ |
| 169 |
date('Y').'/'.date('m'), |
| 170 |
__("year and monthnum", $this->bootstrap->domain), |
| 171 |
__("The year of the post, four digits, for example 2004. Month of the year, for example 05", $this->bootstrap->domain), |
| 172 |
"\d{4}\/\d{2}" |
| 173 |
] |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Refresh settings |
| 179 |
*/ |
| 180 |
public function refresh() { |
| 181 |
$this->set( "sm.readonly", []); |
| 182 |
$google_app_key_file = getenv('GOOGLE_APPLICATION_CREDENTIALS') ?: getenv('GOOGLE_APPLICATION_CREDENTIALS'); |
| 183 |
|
| 184 |
foreach ($this->settings as $option => $array) { |
| 185 |
$value = ''; |
| 186 |
$_option = 'sm_' . $option; |
| 187 |
$constant = $array[0]; // Constant name |
| 188 |
$default = is_array($array[1]) ? $array[1] : array($array[1], $array[1]); // Default value |
| 189 |
|
| 190 |
// Getting settings |
| 191 |
$value = get_option($_option, $default[0]); |
| 192 |
|
| 193 |
if ($option == 'body_rewrite_types' && empty($value) && !is_multisite()) { |
| 194 |
$value = $default[0]; |
| 195 |
} |
| 196 |
|
| 197 |
if ($option == 'hashify_file_name' && in_array($this->get("sm.mode"), array( 'stateless', 'ephemeral' ) )) { |
| 198 |
$value = true; |
| 199 |
} |
| 200 |
|
| 201 |
// If constant is set then override by constant |
| 202 |
if(is_array($constant)){ |
| 203 |
foreach($constant as $old_const => $new_const){ |
| 204 |
if(defined($new_const)){ |
| 205 |
$value = constant($new_const); |
| 206 |
$this->set( "sm.readonly.{$option}", "constant" ); |
| 207 |
break; |
| 208 |
} |
| 209 |
if(is_string($old_const) && defined($old_const)){ |
| 210 |
$value = constant($old_const); |
| 211 |
$this->bootstrap->errors->add( array( |
| 212 |
'key' => $new_const, |
| 213 |
'title' => sprintf( __( "%s: Deprecated Notice (%s)", $this->bootstrap->domain ), $this->bootstrap->name, $new_const ), |
| 214 |
'message' => sprintf(__("<i>%s</i> constant is deprecated, please use <i>%s</i> instead.", $this->bootstrap->domain), $old_const, $new_const), |
| 215 |
), 'notice' ); |
| 216 |
$this->set( "sm.readonly.{$option}", "constant" ); |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
elseif(defined($constant)){ |
| 222 |
$value = constant($constant); |
| 223 |
$this->set( "sm.readonly.{$option}", "constant" ); |
| 224 |
} |
| 225 |
|
| 226 |
// Getting network settings |
| 227 |
if(is_multisite() && $option != 'organize_media' && !$this->get( "sm.readonly.{$option}")){ |
| 228 |
|
| 229 |
$network = get_site_option( $_option, $default[1] ); |
| 230 |
// If network settings available then override by network settings. |
| 231 |
if($network || is_network_admin()){ |
| 232 |
$value = $network; |
| 233 |
if(!is_network_admin()) |
| 234 |
$this->set( "sm.readonly.{$option}", "network" ); |
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
// Converting to string true false for angular. |
| 240 |
if(is_bool($value)){ |
| 241 |
$value = $value === true ? "true" : "false"; |
| 242 |
} |
| 243 |
|
| 244 |
$this->set( "sm.$option", $value); |
| 245 |
} |
| 246 |
|
| 247 |
// Network only settings, to hide settings page |
| 248 |
foreach ($this->network_only_settings as $option => $array) { |
| 249 |
$value = ''; |
| 250 |
$_option = 'sm_' . $option; |
| 251 |
$constant = $array[0]; // Constant name |
| 252 |
$default = $array[1]; // Default value |
| 253 |
|
| 254 |
// If constant is set then override by constant |
| 255 |
if(is_array($constant)){ |
| 256 |
foreach($constant as $old_const => $new_const){ |
| 257 |
if(defined($new_const)){ |
| 258 |
$value = constant($new_const); |
| 259 |
break; |
| 260 |
} |
| 261 |
if(is_string($old_const) && defined($old_const)){ |
| 262 |
$value = constant($old_const); |
| 263 |
trigger_error(__(sprintf("<i>%s</i> constant is deprecated, please use <i>%s</i> instead.", $old_const, $new_const)), E_USER_WARNING); |
| 264 |
break; |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
elseif(defined($constant)){ |
| 269 |
$value = constant($constant); |
| 270 |
} |
| 271 |
// Getting network settings |
| 272 |
elseif(is_multisite()){ |
| 273 |
$value = get_site_option( $_option, $default ); |
| 274 |
} |
| 275 |
|
| 276 |
// Converting to string true false for angular. |
| 277 |
if(is_bool($value)){ |
| 278 |
$value = $value === true ? "true" : "false"; |
| 279 |
} |
| 280 |
|
| 281 |
$this->set( "sm.$option", $value); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* JSON key file path |
| 286 |
*/ |
| 287 |
/* Use constant value for JSON key file path, if set. */ |
| 288 |
if (defined('WP_STATELESS_MEDIA_KEY_FILE_PATH') || $google_app_key_file !== false) { |
| 289 |
/* Maybe fix the path to p12 file. */ |
| 290 |
$key_file_path = (defined('WP_STATELESS_MEDIA_KEY_FILE_PATH')) ? WP_STATELESS_MEDIA_KEY_FILE_PATH : $google_app_key_file; |
| 291 |
|
| 292 |
if( !empty( $key_file_path ) ) { |
| 293 |
$upload_dir = wp_upload_dir(); |
| 294 |
/* Check if file exists */ |
| 295 |
switch( true ) { |
| 296 |
/* Determine if default path is correct */ |
| 297 |
case (file_exists($key_file_path)): |
| 298 |
/* Path is correct. Do nothing */ |
| 299 |
break; |
| 300 |
/* Look using WP root. */ |
| 301 |
case (file_exists( ABSPATH . $key_file_path ) ): |
| 302 |
$key_file_path = ABSPATH . $key_file_path; |
| 303 |
break; |
| 304 |
/* Look in wp-content dir */ |
| 305 |
case (file_exists( WP_CONTENT_DIR . $key_file_path ) ): |
| 306 |
$key_file_path = WP_CONTENT_DIR . $key_file_path; |
| 307 |
break; |
| 308 |
/* Look in uploads dir */ |
| 309 |
case (file_exists( wp_normalize_path( $upload_dir[ 'basedir' ] ) . '/' . $key_file_path ) ): |
| 310 |
$key_file_path = wp_normalize_path( $upload_dir[ 'basedir' ] ) . '/' . $key_file_path; |
| 311 |
break; |
| 312 |
/* Look using Plugin root */ |
| 313 |
case (file_exists($this->bootstrap->path( $key_file_path, 'dir') ) ): |
| 314 |
$key_file_path = $this->bootstrap->path( $key_file_path, 'dir' ); |
| 315 |
break; |
| 316 |
|
| 317 |
} |
| 318 |
if(is_readable($key_file_path)) { |
| 319 |
$this->set( 'sm.key_json', file_get_contents($key_file_path) ); |
| 320 |
if(defined('WP_STATELESS_MEDIA_KEY_FILE_PATH')) |
| 321 |
$this->set( "sm.readonly.key_json", "constant" ); |
| 322 |
else |
| 323 |
$this->set("sm.readonly.key_json", "environment"); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
$this->set( 'sm.strings', $this->strings ); |
| 329 |
|
| 330 |
do_action('wp_stateless_settings_refresh', $this); |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Remove settings |
| 335 |
* @param bool $network |
| 336 |
*/ |
| 337 |
public function reset($network = false) { |
| 338 |
foreach ($this->settings as $option => $array) { |
| 339 |
if($option == 'organize_media') |
| 340 |
continue; |
| 341 |
$_option = 'sm_' . $option; |
| 342 |
|
| 343 |
if($network && current_user_can('manage_network')){ |
| 344 |
delete_site_option($_option); |
| 345 |
delete_option($_option); |
| 346 |
} |
| 347 |
else{ |
| 348 |
delete_option($_option); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
foreach ($this->network_only_settings as $option => $array) { |
| 353 |
$_option = 'sm_' . $option; |
| 354 |
if($network && current_user_can('manage_network')){ |
| 355 |
delete_site_option($_option); |
| 356 |
delete_option($_option); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
$this->set('sm', []); |
| 361 |
$this->refresh(); |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Replacing wildcards with real values |
| 366 |
* @param $root_dir |
| 367 |
* @param bool $regex |
| 368 |
* @param array $current_values |
| 369 |
* @return mixed|null|string|string[] |
| 370 |
* |
| 371 |
*/ |
| 372 |
public function root_dir_wildcards( $root_dir, $regex = false, $current_values = [] ) { |
| 373 |
|
| 374 |
$not_allowed_char = '/[^A-Za-z0-9\/_.\.\-]/'; |
| 375 |
$wildcards = apply_filters('wp_stateless_root_dir_wildcard', $this->wildcards); |
| 376 |
|
| 377 |
if($regex){ |
| 378 |
$root_dir = preg_quote($root_dir); |
| 379 |
$not_allowed_char = '/[^A-Za-z0-9\/_.\.\-\\\\{}]/'; |
| 380 |
} |
| 381 |
|
| 382 |
if ( is_array( $wildcards ) && !empty( $wildcards ) ) { |
| 383 |
foreach ($wildcards as $wildcard => $values) { |
| 384 |
if (!empty($wildcard)) { |
| 385 |
$replace = $values[0]; |
| 386 |
if($regex){ |
| 387 |
$replace = isset($values[3]) ? $values[3] : preg_quote($values[0]); |
| 388 |
} |
| 389 |
if ( isset($current_values[$wildcard]) ) { |
| 390 |
$replace = $current_values[$wildcard]; |
| 391 |
} |
| 392 |
$root_dir = str_replace($wildcard, $replace, $root_dir); |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
//removing all special chars except slash |
| 398 |
$root_dir = preg_replace($not_allowed_char, '', $root_dir); |
| 399 |
$root_dir = preg_replace('/(\/+)/', '/', $root_dir); |
| 400 |
$root_dir = trim( $root_dir, '/ ' ); // Remove any forward slash and empty space. |
| 401 |
|
| 402 |
return $root_dir; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
/** |
| 407 |
* Parse path by wildcards and return array ('wildcard' => 'value') |
| 408 |
* The perpose of this filter is to return Y/M or other dynamic fields from the file path. |
| 409 |
* For now only Y/M is dynamic. We will get it via using regex. |
| 410 |
* @param $path |
| 411 |
* @return array |
| 412 |
*/ |
| 413 |
public function parse_root_dir_wildcards( $path ) { |
| 414 |
$result = []; |
| 415 |
|
| 416 |
/** |
| 417 |
* removing GS host from path |
| 418 |
*/ |
| 419 |
$gs_url = $this->bootstrap->get_gs_host(); |
| 420 |
if( 0 === strpos( $path, $gs_url . '/' ) ) { |
| 421 |
$path = substr( $path, strlen( $gs_url . '/' ) ); |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* removing filename and last slash |
| 426 |
*/ |
| 427 |
$path = untrailingslashit( str_replace(basename($path), '', $path) ); |
| 428 |
$wildcards = apply_filters('wp_stateless_root_dir_wildcard', $this->wildcards); |
| 429 |
|
| 430 |
/** |
| 431 |
* Checking if a wildcard have regex field in it. |
| 432 |
* Then return the matching value using regex. |
| 433 |
*/ |
| 434 |
foreach ($wildcards as $key => $value) { |
| 435 |
if(isset($value[3])){ |
| 436 |
if(preg_match("@" . $value[3] . "@", $path, $matches)){ |
| 437 |
$result[$key] = $matches[0]; |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
return $result; |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Add menu options |
| 447 |
*/ |
| 448 |
public function admin_menu() { |
| 449 |
$key_json = $this->get('sm.key_json'); |
| 450 |
if($this->get('sm.hide_setup_assistant') != 'true' && empty($key_json) ){ |
| 451 |
// #152 |
| 452 |
// $this->setup_wizard_ui = add_media_page( __( 'Stateless Setup', $this->bootstrap->domain ), __( 'Stateless Setup', $this->bootstrap->domain ), 'manage_options', 'stateless-setup', array($this, 'setup_wizard_interface') ); |
| 453 |
} |
| 454 |
|
| 455 |
if($this->get('sm.hide_settings_panel') != 'true'){ |
| 456 |
$this->stateless_settings = add_media_page( __( 'Stateless Settings', $this->bootstrap->domain ), __( 'Stateless Settings', $this->bootstrap->domain ), 'manage_options', 'stateless-settings', array($this, 'settings_interface') ); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Add menu options |
| 462 |
* @param $slug |
| 463 |
*/ |
| 464 |
public function network_admin_menu($slug) { |
| 465 |
// #152 |
| 466 |
// $this->setup_wizard_ui = add_submenu_page( 'settings.php', __( 'Stateless Setup', $this->bootstrap->domain ), __( 'Stateless Setup', $this->bootstrap->domain ), 'manage_options', 'stateless-setup', array($this, 'setup_wizard_interface') ); |
| 467 |
$this->stateless_settings = add_submenu_page( 'settings.php', __( 'Stateless Settings', $this->bootstrap->domain ), __( 'Stateless Settings', $this->bootstrap->domain ), 'manage_options', 'stateless-settings', array($this, 'settings_interface') ); |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* Draw interface |
| 472 |
*/ |
| 473 |
public function settings_interface() { |
| 474 |
$tab = isset($_GET['tab']) && !empty($_GET['tab']) ? $_GET['tab'] : 'stless_settings_tab'; |
| 475 |
|
| 476 |
include $this->bootstrap->path( '/static/views/settings_interface.php', 'dir' ); |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* Draw interface |
| 481 |
*/ |
| 482 |
public function regenerate_interface() { |
| 483 |
include $this->bootstrap->path( '/static/views/regenerate_interface.php', 'dir' ); |
| 484 |
} |
| 485 |
|
| 486 |
/** |
| 487 |
* Draw interface |
| 488 |
*/ |
| 489 |
public function setup_wizard_interface() { |
| 490 |
include ud_get_stateless_media()->path( '/static/views/setup_wizard_interface.php', 'dir' ); |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* Handles saving SM data. |
| 495 |
* |
| 496 |
* @author alim@UD |
| 497 |
*/ |
| 498 |
public function save_media_settings(){ |
| 499 |
if(isset($_POST['action']) && $_POST['action'] == 'stateless_settings' && wp_verify_nonce( $_POST['_smnonce'], 'wp-stateless-settings' )){ |
| 500 |
|
| 501 |
$settings = apply_filters('stateless::settings::save', $_POST['sm']); |
| 502 |
$root_dir_value = false; |
| 503 |
|
| 504 |
foreach ( $settings as $name => $value ) { |
| 505 |
/** |
| 506 |
* Sanitize POST data |
| 507 |
*/ |
| 508 |
if (!is_array($value)) { |
| 509 |
$value = sanitize_text_field($value); |
| 510 |
} |
| 511 |
/** |
| 512 |
* root_dir settings |
| 513 |
*/ |
| 514 |
if ( 'root_dir' == $name && is_array($value) ) { |
| 515 |
//managed in WP-Stateless settings (via Bucket Folder control) |
| 516 |
if ( in_array('%date_year/date_month%', $value)) { |
| 517 |
update_option( 'uploads_use_yearmonth_folders', '1' ); |
| 518 |
} else { |
| 519 |
update_option( 'uploads_use_yearmonth_folders', '0' ); |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* preparing path from tags |
| 524 |
*/ |
| 525 |
$value = implode('/', $value); |
| 526 |
$root_dir_value = true; |
| 527 |
} |
| 528 |
|
| 529 |
$option = 'sm_'. $name; |
| 530 |
|
| 531 |
if($name == 'organize_media'){ |
| 532 |
$option = 'uploads_use_yearmonth_folders'; |
| 533 |
} |
| 534 |
elseif($name == 'key_json'){ |
| 535 |
$value = stripslashes($value); |
| 536 |
} |
| 537 |
|
| 538 |
// Be sure to cleanup values before saving |
| 539 |
$value = trim($value); |
| 540 |
|
| 541 |
if(is_network_admin()){ |
| 542 |
update_site_option( $option, $value ); |
| 543 |
} |
| 544 |
else{ |
| 545 |
update_option( $option, $value ); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
if ( !$root_dir_value ) { |
| 550 |
if(is_network_admin()){ |
| 551 |
update_site_option( 'sm_root_dir', '' ); |
| 552 |
} |
| 553 |
else{ |
| 554 |
update_option( 'sm_root_dir', '' ); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
$this->bootstrap->flush_transients(); |
| 559 |
$this->refresh(); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
/** |
| 564 |
* Wrapper for setting value. |
| 565 |
* @param string $key |
| 566 |
* @param bool $value |
| 567 |
* @param bool $bypass_validation |
| 568 |
* @return \UDX\Settings |
| 569 |
*/ |
| 570 |
public function set( $key = '', $value = false, $bypass_validation = false ) { |
| 571 |
return parent::set( $key, $value, $bypass_validation ); |
| 572 |
} |
| 573 |
|
| 574 |
/** |
| 575 |
* Outputs 'Compatibility' tab content on the settings page. |
| 576 |
* |
| 577 |
*/ |
| 578 |
public function settings_tab_content() { |
| 579 |
$wildcards = apply_filters('wp_stateless_root_dir_wildcard', $this->wildcards); |
| 580 |
$wildcard_year_month = '%date_year/date_month%'; |
| 581 |
$root_dir = $this->get( 'sm.root_dir' ); |
| 582 |
|
| 583 |
$use_year_month = (strpos($root_dir, $wildcard_year_month) !== false) ?: false; |
| 584 |
|
| 585 |
/** |
| 586 |
* removing year/month wildcard |
| 587 |
*/ |
| 588 |
if ($use_year_month) { |
| 589 |
$root_dir = str_replace($wildcard_year_month, '%YM%', $root_dir); |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* preparing array with wildcards |
| 594 |
*/ |
| 595 |
$root_dir_values = explode('/', $root_dir); |
| 596 |
|
| 597 |
/** |
| 598 |
* adding year/month wildcard |
| 599 |
*/ |
| 600 |
if ($use_year_month) { |
| 601 |
if ( !empty($root_dir_values) ) { |
| 602 |
foreach( $root_dir_values as $k=>$root_dir_value ) { |
| 603 |
if ( $root_dir_value == '%YM%' ) { |
| 604 |
$root_dir_values[$k] = $wildcard_year_month; |
| 605 |
} |
| 606 |
} |
| 607 |
} else { |
| 608 |
$root_dir_values[] = $wildcard_year_month; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
/** |
| 613 |
* first slash |
| 614 |
*/ |
| 615 |
array_unshift($root_dir_values , '/'); |
| 616 |
|
| 617 |
/** |
| 618 |
* removing empty values |
| 619 |
*/ |
| 620 |
$root_dir_values = array_filter($root_dir_values); |
| 621 |
|
| 622 |
foreach ($root_dir_values as $k => $v) { |
| 623 |
$root_dir_values[$k] = trim($v); |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* merging user's wildcards with default values |
| 628 |
*/ |
| 629 |
$wildcards = array_keys($wildcards); |
| 630 |
|
| 631 |
if (!empty($root_dir_values)) { |
| 632 |
$wildcards = array_unique( array_merge($root_dir_values, $wildcards) ); |
| 633 |
} |
| 634 |
|
| 635 |
$sm = (object)$this->get('sm'); |
| 636 |
|
| 637 |
include ud_get_stateless_media()->path('static/views/settings-tab.php', 'dir'); |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* Outputs 'Sync' tab content on the settings page. |
| 642 |
* |
| 643 |
*/ |
| 644 |
public function processing_tab_content() { |
| 645 |
// Drop non-public properties |
| 646 |
$processes = json_encode(Utility::get_available_sync_classes()); |
| 647 |
$processes = json_decode($processes, true); |
| 648 |
|
| 649 |
$processes = Helper::array_of_objects($processes); |
| 650 |
|
| 651 |
include ud_get_stateless_media()->path('static/views/processing_interface.php', 'dir'); |
| 652 |
} |
| 653 |
|
| 654 |
/** |
| 655 |
* Getter for settings |
| 656 |
* |
| 657 |
* @param string|bool $key |
| 658 |
* @param mixed $default |
| 659 |
* @return mixed |
| 660 |
*/ |
| 661 |
public function get( $key = false, $default = false ) { |
| 662 |
$value = parent::get( $key, $default ); |
| 663 |
|
| 664 |
if ( $key === 'sm' && is_array($value) ) { |
| 665 |
foreach ( $value as $key => $val ) { |
| 666 |
$value[$key] = apply_filters("wp_stateless_get_setting_$key", $val, $default); |
| 667 |
} |
| 668 |
} else if ( is_string($key) ) { |
| 669 |
$key = str_replace('sm.', '', $key); |
| 670 |
$value = apply_filters("wp_stateless_get_setting_$key", $value, $default); |
| 671 |
} |
| 672 |
|
| 673 |
return $value; |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* Override Cache Control |
| 678 |
* |
| 679 |
* @param $value |
| 680 |
* @return mixed |
| 681 |
*/ |
| 682 |
public function override_cache_control($value) { |
| 683 |
if ( !empty($value) && $value !== self::DEFAULT_CACHE_CONTROL ) { |
| 684 |
return $value; |
| 685 |
} |
| 686 |
|
| 687 |
$cache_control = trim($this->get('sm.cache_control')); |
| 688 |
|
| 689 |
return empty($cache_control) ? self::DEFAULT_CACHE_CONTROL : $cache_control; |
| 690 |
} |
| 691 |
|
| 692 |
/** |
| 693 |
* Add 'Settings' on Plugins page |
| 694 |
* |
| 695 |
* @param array $actions |
| 696 |
* @param string $file |
| 697 |
* |
| 698 |
* @return array |
| 699 |
*/ |
| 700 |
public function plugin_action_links($actions, $file) { |
| 701 |
if ( $file !== $this->plugin_file ) { |
| 702 |
return $actions; |
| 703 |
} |
| 704 |
|
| 705 |
$url = ud_get_stateless_media()->get_settings_page_url('?page=stateless-settings'); |
| 706 |
|
| 707 |
$settings = [ |
| 708 |
'settings' => sprintf( '<a href="%s">%s</a>', $url, __('Settings', ud_get_stateless_media()->domain) ), |
| 709 |
]; |
| 710 |
|
| 711 |
$actions['addons'] = sprintf( '<a href="%s" style="color: #f05323" target="_blank">%s</a>', ud_get_stateless_media()->get_docs_page_url('addons'), __('Addons', ud_get_stateless_media()->domain) ); |
| 712 |
|
| 713 |
return $settings + $actions; |
| 714 |
} |
| 715 |
|
| 716 |
/** |
| 717 |
* Add link to docs on Plugins page |
| 718 |
* |
| 719 |
* @param $meta |
| 720 |
* @param $file |
| 721 |
* |
| 722 |
* @return array |
| 723 |
*/ |
| 724 |
public function plugin_row_meta($meta, $file) { |
| 725 |
if ( $file !== $this->plugin_file ) { |
| 726 |
return $meta; |
| 727 |
} |
| 728 |
|
| 729 |
$meta['documentation'] = sprintf( '<a href="%s" target="_blank">%s</a>', ud_get_stateless_media()->get_docs_page_url(), __('Documentation', ud_get_stateless_media()->domain) ); |
| 730 |
|
| 731 |
return $meta; |
| 732 |
} |
| 733 |
|
| 734 |
/** |
| 735 |
* Upon plugin activation check if configuration is complete and show setup message if needed |
| 736 |
*/ |
| 737 |
public function check_setup() { |
| 738 |
if ( json_decode($this->get('sm.key_json')) ) { |
| 739 |
return; |
| 740 |
} |
| 741 |
|
| 742 |
delete_option('dismissed_notice_' . self::SETUP_MESSAGE_KEY); |
| 743 |
} |
| 744 |
|
| 745 |
/** |
| 746 |
* Check if configuration is complete and show setup message if needed |
| 747 |
*/ |
| 748 |
public function documentation_message() { |
| 749 |
if ( json_decode($this->get('sm.key_json')) ) { |
| 750 |
return; |
| 751 |
} |
| 752 |
|
| 753 |
$documentation_url = sprintf( |
| 754 |
'<a href="%s" target="_blank">%s</a>', |
| 755 |
$this->bootstrap->get_docs_page_url('/setup/'), |
| 756 |
__('Refer to the setup manual', ud_get_stateless_media()->domain), |
| 757 |
); |
| 758 |
|
| 759 |
$this->bootstrap->errors->add([ |
| 760 |
'title' => __('WP-Stateless: Finish Setup', ud_get_stateless_media()->domain), |
| 761 |
'message' => sprintf( |
| 762 |
__( |
| 763 |
'WP-Stateless has been successfully activated. %s for guidance on completing the setup.', |
| 764 |
ud_get_stateless_media()->domain, |
| 765 |
), |
| 766 |
$documentation_url, |
| 767 |
), |
| 768 |
'key' => self::SETUP_MESSAGE_KEY, |
| 769 |
], 'message'); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
} |
| 774 |
|
| 775 |
} |
| 776 |
|