PluginProbe
WP-Stateless – Google Cloud Storage / trunk
WP-Stateless – Google Cloud Storage vtrunk
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
← All changes | lib/classes/class-settings.php +487 -90 2.2.3trunk View file →
@@ -7,10 +7,13 @@
7 7 namespace wpCloud\StatelessMedia {
8 8
9 9 if( !class_exists( 'wpCloud\StatelessMedia\Settings' ) ) {
10 10
11 - final class Settings extends \UsabilityDynamics\Settings {
11 + final class Settings extends \UDX\Settings {
12 12
13 + const DEFAULT_CACHE_CONTROL = 'public, max-age=36000, must-revalidate';
14 + const SETUP_MESSAGE_KEY = 'finish-setup';
15 +
13 16 /**
14 17 * @var false|null|string
15 18 */
16 19 public $setup_wizard_ui = null;
@@ -15,50 +18,74 @@
15 18 */
16 19 public $setup_wizard_ui = null;
17 20
18 21 /**
22 + * @var Array
23 + */
24 + public $wildcards = array();
25 +
26 + /**
19 27 * @var false|null|string
20 28 */
21 29 public $stateless_settings = null;
22 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 +
23 44 private $settings = array(
24 - 'mode' => array('WP_STATELESS_MEDIA_MODE', 'cdn'),
25 - 'body_rewrite' => array('WP_STATELESS_MEDIA_BODY_REWRITE', 'false'),
26 - 'body_rewrite_types' => array('WP_STATELESS_MEDIA_BODY_REWRITE_TYPES', 'jpg jpeg png gif pdf'),
27 - 'bucket' => array('WP_STATELESS_MEDIA_BUCKET', ''),
28 - 'root_dir' => array('WP_STATELESS_MEDIA_ROOT_DIR', ''),
29 - 'key_json' => array('WP_STATELESS_MEDIA_JSON_KEY', ''),
30 - 'cache_control' => array('WP_STATELESS_MEDIA_CACHE_CONTROL', ''),
31 - 'delete_remote' => array('WP_STATELESS_MEDIA_DELETE_REMOTE', 'true'),
32 - 'custom_domain' => array('WP_STATELESS_MEDIA_CUSTOM_DOMAIN', ''),
33 - 'organize_media' => array('', 'true'),
34 - 'hashify_file_name' => array(['WP_STATELESS_MEDIA_HASH_FILENAME' => 'WP_STATELESS_MEDIA_CACHE_BUSTING'], 'true'),
35 - );
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 + );
36 62
37 63 private $network_only_settings = array(
38 - 'hide_settings_panel' => array('WP_STATELESS_MEDIA_HIDE_SETTINGS_PANEL', false),
39 - 'hide_setup_assistant' => array('WP_STATELESS_MEDIA_HIDE_SETUP_ASSISTANT', false),
40 - );
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 + );
41 67
42 68 private $strings = array(
43 - 'network' => 'Currently configured via Network Settings.',
44 - 'constant' => 'Currently configured via a constant.',
45 - 'environment' => 'Currently configured via an environment variable.',
46 - );
69 + 'network' => 'Currently configured via Network Settings.',
70 + 'constant' => 'Currently configured via a constant.',
71 + 'environment' => 'Currently configured via an environment variable.',
72 + );
47 73
48 74 /**
49 - * Overridden construct
75 + *
76 + * Settings constructor.
77 + * @param null $bootstrap
50 78 */
51 - public function __construct() {
79 + public function __construct($bootstrap = null) {
80 + $this->bootstrap = $bootstrap ? $bootstrap : ud_get_stateless_media();
52 81
53 - add_action('admin_menu', array( $this, 'admin_menu' ));
82 + // Defile the main plugin file
83 + $realpath = realpath( __DIR__ . '/../..');
84 + $this->plugin_file = basename( $realpath ) . '/wp-stateless-media.php';
54 85
55 -
56 - $this->save_media_settings();
57 -
58 -
59 86 /* Add 'Settings' link for SM plugin on plugins page. */
60 - $_basename = plugin_basename( ud_get_stateless_media()->boot_file );
87 + $_basename = plugin_basename( $this->bootstrap->boot_file );
61 88
62 89 parent::__construct( array(
63 90 'store' => 'options',
64 91 'format' => 'json',
@@ -65,12 +92,45 @@
65 92 'data' => array(
66 93 'sm' => array()
67 94 )
68 95 ));
69 -
96 +
97 + add_action('activated_plugin', array( $this, 'check_setup' ));
98 + add_action('admin_notices', array( $this, 'documentation_message' ), 1);
99 +
70 100 // Setting sm variable
71 101 $this->refresh();
72 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 +
73 133 /**
74 134 * Manage specific Network Settings
75 135 */
76 136 if( is_network_admin() ) {
@@ -76,25 +136,50 @@
76 136 if( is_network_admin() ) {
77 137 add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ));
78 138 }
79 139
80 - $this->set('page_url.stateless_setup', ud_get_stateless_media()->get_settings_page_url('?page=stateless-setup'));
81 - $this->set('page_url.stateless_settings', ud_get_stateless_media()->get_settings_page_url('?page=stateless-settings'));
82 -
83 - /** Register options */
84 - add_action( 'init', array( $this, 'init' ), 3 );
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 + );
85 175 }
86 176
87 - public function init(){
88 -
89 - }
90 -
91 177 /**
92 178 * Refresh settings
93 179 */
94 180 public function refresh() {
95 - $constant_mode = false;
96 - $upload_data = wp_upload_dir();
181 + $this->set( "sm.readonly", []);
97 182 $google_app_key_file = getenv('GOOGLE_APPLICATION_CREDENTIALS') ?: getenv('GOOGLE_APPLICATION_CREDENTIALS');
98 183
99 184 foreach ($this->settings as $option => $array) {
100 185 $value = '';
@@ -99,22 +184,18 @@
99 184 foreach ($this->settings as $option => $array) {
100 185 $value = '';
101 186 $_option = 'sm_' . $option;
102 187 $constant = $array[0]; // Constant name
103 - $default = $array[1]; // Default value
188 + $default = is_array($array[1]) ? $array[1] : array($array[1], $array[1]); // Default value
104 189
105 - if($option == 'organize_media'){
106 - $_option = 'uploads_use_yearmonth_folders';
107 - }
190 + // Getting settings
191 + $value = get_option($_option, $default[0]);
108 192
109 - // Getting settings
110 - $value = get_option($_option, $default);
111 -
112 193 if ($option == 'body_rewrite_types' && empty($value) && !is_multisite()) {
113 - $value = $default;
194 + $value = $default[0];
114 195 }
115 -
116 - if ($option == 'hashify_file_name' && $this->get("sm.mode") == 'stateless') {
196 +
197 + if ($option == 'hashify_file_name' && in_array($this->get("sm.mode"), array( 'stateless', 'ephemeral' ) )) {
117 198 $value = true;
118 199 }
119 200
120 201 // If constant is set then override by constant
@@ -120,21 +201,21 @@
120 201 // If constant is set then override by constant
121 202 if(is_array($constant)){
122 203 foreach($constant as $old_const => $new_const){
123 204 if(defined($new_const)){
124 - $value = constant($new_const);
125 - $this->set( "sm.readonly.{$option}", "constant" );
205 + $value = constant($new_const);
206 + $this->set( "sm.readonly.{$option}", "constant" );
126 207 break;
127 208 }
128 209 if(is_string($old_const) && defined($old_const)){
129 - $value = constant($old_const);
130 - ud_get_stateless_media()->errors->add( array(
131 - 'key' => $new_const,
132 - 'title' => sprintf( __( "%s: Deprecated Notice (%s)", ud_get_stateless_media()->domain ), ud_get_stateless_media()->name, $new_const ),
133 - 'message' => sprintf(__("<i>%s</i> constant is deprecated, please use <i>%s</i> instead.", ud_get_stateless_media()->domain), $old_const, $new_const),
134 - ), 'notice' );
135 - $this->set( "sm.readonly.{$option}", "constant" );
136 - break;
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;
137 218 }
138 219 }
139 220 }
140 221 elseif(defined($constant)){
@@ -140,11 +221,13 @@
140 221 elseif(defined($constant)){
141 222 $value = constant($constant);
142 223 $this->set( "sm.readonly.{$option}", "constant" );
143 224 }
225 +
144 226 // Getting network settings
145 - elseif(is_multisite() && $option != 'organize_media'){
146 - $network = get_site_option( $_option );
227 + if(is_multisite() && $option != 'organize_media' && !$this->get( "sm.readonly.{$option}")){
228 +
229 + $network = get_site_option( $_option, $default[1] );
147 230 // If network settings available then override by network settings.
148 231 if($network || is_network_admin()){
149 232 $value = $network;
150 233 if(!is_network_admin())
@@ -151,9 +234,9 @@
151 234 $this->set( "sm.readonly.{$option}", "network" );
152 235 }
153 236
154 237 }
155 -
238 +
156 239 // Converting to string true false for angular.
157 240 if(is_bool($value)){
158 241 $value = $value === true ? "true" : "false";
159 242 }
@@ -188,10 +271,10 @@
188 271 // Getting network settings
189 272 elseif(is_multisite()){
190 273 $value = get_site_option( $_option, $default );
191 274 }
192 -
193 - // Converting to string true flase for angular.
275 +
276 + // Converting to string true false for angular.
194 277 if(is_bool($value)){
195 278 $value = $value === true ? "true" : "false";
196 279 }
197 280
@@ -196,9 +279,9 @@
196 279 }
197 280
198 281 $this->set( "sm.$option", $value);
199 282 }
200 -
283 +
201 284 /**
202 285 * JSON key file path
203 286 */
204 287 /* Use constant value for JSON key file path, if set. */
@@ -226,10 +309,10 @@
226 309 case (file_exists( wp_normalize_path( $upload_dir[ 'basedir' ] ) . '/' . $key_file_path ) ):
227 310 $key_file_path = wp_normalize_path( $upload_dir[ 'basedir' ] ) . '/' . $key_file_path;
228 311 break;
229 312 /* Look using Plugin root */
230 - case (file_exists(ud_get_stateless_media()->path( $key_file_path, 'dir') ) ):
231 - $key_file_path = ud_get_stateless_media()->path( $key_file_path, 'dir' );
313 + case (file_exists($this->bootstrap->path( $key_file_path, 'dir') ) ):
314 + $key_file_path = $this->bootstrap->path( $key_file_path, 'dir' );
232 315 break;
233 316
234 317 }
235 318 if(is_readable($key_file_path)) {
@@ -242,12 +325,15 @@
242 325 }
243 326 }
244 327
245 328 $this->set( 'sm.strings', $this->strings );
329 +
330 + do_action('wp_stateless_settings_refresh', $this);
246 331 }
247 332
248 333 /**
249 334 * Remove settings
335 + * @param bool $network
250 336 */
251 337 public function reset($network = false) {
252 338 foreach ($this->settings as $option => $array) {
253 339 if($option == 'organize_media')
@@ -269,33 +355,117 @@
269 355 delete_site_option($_option);
270 356 delete_option($_option);
271 357 }
272 358 }
273 -
359 +
274 360 $this->set('sm', []);
275 361 $this->refresh();
276 362 }
277 363
278 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 + /**
279 446 * Add menu options
280 447 */
281 448 public function admin_menu() {
282 449 $key_json = $this->get('sm.key_json');
283 450 if($this->get('sm.hide_setup_assistant') != 'true' && empty($key_json) ){
284 - $this->setup_wizard_ui = add_media_page( __( 'Stateless Setup', ud_get_stateless_media()->domain ), __( 'Stateless Setup', ud_get_stateless_media()->domain ), 'manage_options', 'stateless-setup', array($this, 'setup_wizard_interface') );
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') );
285 453 }
286 454
287 455 if($this->get('sm.hide_settings_panel') != 'true'){
288 - $this->stateless_settings = add_media_page( __( 'Stateless Settings', ud_get_stateless_media()->domain ), __( 'Stateless Settings', ud_get_stateless_media()->domain ), 'manage_options', 'stateless-settings', array($this, 'settings_interface') );
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') );
289 457 }
290 458 }
291 459
292 460 /**
293 461 * Add menu options
462 + * @param $slug
294 463 */
295 464 public function network_admin_menu($slug) {
296 - $this->setup_wizard_ui = add_submenu_page( 'settings.php', __( 'Stateless Setup', ud_get_stateless_media()->domain ), __( 'Stateless Setup', ud_get_stateless_media()->domain ), 'manage_options', 'stateless-setup', array($this, 'setup_wizard_interface') );
297 - $this->stateless_settings = add_submenu_page( 'settings.php', __( 'Stateless Settings', ud_get_stateless_media()->domain ), __( 'Stateless Settings', ud_get_stateless_media()->domain ), 'manage_options', 'stateless-settings', array($this, 'settings_interface') );
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') );
298 468 }
299 469
300 470 /**
301 471 * Draw interface
@@ -300,9 +470,11 @@
300 470 /**
301 471 * Draw interface
302 472 */
303 473 public function settings_interface() {
304 - include ud_get_stateless_media()->path( '/static/views/settings_interface.php', 'dir' );
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' );
305 477 }
306 478
307 479 /**
308 480 * Draw interface
@@ -307,9 +479,9 @@
307 479 /**
308 480 * Draw interface
309 481 */
310 482 public function regenerate_interface() {
311 - include ud_get_stateless_media()->path( '/static/views/regenerate_interface.php', 'dir' );
483 + include $this->bootstrap->path( '/static/views/regenerate_interface.php', 'dir' );
312 484 }
313 485
314 486 /**
315 487 * Draw interface
@@ -314,20 +486,9 @@
314 486 /**
315 487 * Draw interface
316 488 */
317 489 public function setup_wizard_interface() {
318 - $step = !empty($_GET['step'])?$_GET['step']:'';
319 - switch ($step) {
320 - case 'google-login':
321 - case 'setup-project':
322 - case 'finish':
323 - include ud_get_stateless_media()->path( '/static/views/setup_wizard_interface.php', 'dir' );
324 - break;
325 -
326 - default:
327 - include ud_get_stateless_media()->path( '/static/views/stateless_splash_screen.php', 'dir' );
328 - break;
329 - }
490 + include ud_get_stateless_media()->path( '/static/views/setup_wizard_interface.php', 'dir' );
330 491 }
331 492
332 493 /**
333 494 * Handles saving SM data.
@@ -334,17 +495,46 @@
334 495 *
335 496 * @author alim@UD
336 497 */
337 498 public function save_media_settings(){
338 - if(isset($_POST['action']) && $_POST['action'] == 'stateless_settings' && wp_verify_nonce( $_POST['_smnonce'], 'wp-stateless-settings' )){
499 + if(isset($_POST['action']) && $_POST['action'] == 'stateless_settings' && wp_verify_nonce( $_POST['_smnonce'], 'wp-stateless-settings' )){
339 500
340 501 $settings = apply_filters('stateless::settings::save', $_POST['sm']);
502 + $root_dir_value = false;
503 +
341 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 +
342 529 $option = 'sm_'. $name;
343 530
344 531 if($name == 'organize_media'){
345 532 $option = 'uploads_use_yearmonth_folders';
346 533 }
534 + elseif($name == 'key_json'){
535 + $value = stripslashes($value);
536 + }
347 537
348 538 // Be sure to cleanup values before saving
349 539 $value = trim($value);
350 540
@@ -355,9 +545,19 @@
355 545 update_option( $option, $value );
356 546 }
357 547 }
358 548
359 - ud_get_stateless_media()->flush_transients();
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();
360 560 }
361 561 }
362 562
363 563 /**
@@ -364,14 +564,211 @@
364 564 * Wrapper for setting value.
365 565 * @param string $key
366 566 * @param bool $value
367 567 * @param bool $bypass_validation
368 - * @return \UsabilityDynamics\Settings
568 + * @return \UDX\Settings
369 569 */
370 570 public function set( $key = '', $value = false, $bypass_validation = false ) {
371 571 return parent::set( $key, $value, $bypass_validation );
372 572 }
373 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 + }
374 771 }
375 772
376 773 }
377 774