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 +259 -74 3.0.4trunk 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;
@@ -25,8 +28,13 @@
25 28 */
26 29 public $stateless_settings = null;
27 30
28 31 /**
32 + * @var string
33 + */
34 + private $plugin_file = '';
35 +
36 + /**
29 37 * Instance of
30 38 * - ud_get_stateless_media
31 39 * - wpCloud\StatelessMedia\Bootstrap
32 40 * @var false|null|string
@@ -33,9 +41,9 @@
33 41 */
34 42 public $bootstrap = null;
35 43
36 44 private $settings = array(
37 - 'mode' => array('WP_STATELESS_MEDIA_MODE', 'cdn'),
45 + 'mode' => array('WP_STATELESS_MEDIA_MODE', 'ephemeral'),
38 46 'body_rewrite' => array('WP_STATELESS_MEDIA_BODY_REWRITE', 'false'),
39 47 'body_rewrite_types' => array('WP_STATELESS_MEDIA_BODY_REWRITE_TYPES', 'jpg jpeg png gif pdf'),
40 48 'bucket' => array('WP_STATELESS_MEDIA_BUCKET', ''),
41 49 'root_dir' => array('WP_STATELESS_MEDIA_ROOT_DIR', ['/%date_year/date_month%/', '/sites/%site_id%/%date_year/date_month%/']),
@@ -44,8 +52,13 @@
44 52 'delete_remote' => array('WP_STATELESS_MEDIA_DELETE_REMOTE', 'true'),
45 53 'custom_domain' => array('WP_STATELESS_MEDIA_CUSTOM_DOMAIN', ''),
46 54 'organize_media' => array('', 'true'),
47 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', '']),
48 61 );
49 62
50 63 private $network_only_settings = array(
51 64 'hide_settings_panel' => array('WP_STATELESS_MEDIA_HIDE_SETTINGS_PANEL', false),
@@ -65,8 +78,11 @@
65 78 */
66 79 public function __construct($bootstrap = null) {
67 80 $this->bootstrap = $bootstrap ? $bootstrap : ud_get_stateless_media();
68 81
82 + // Defile the main plugin file
83 + $realpath = realpath( __DIR__ . '/../..');
84 + $this->plugin_file = basename( $realpath ) . '/wp-stateless-media.php';
69 85
70 86 /* Add 'Settings' link for SM plugin on plugins page. */
71 87 $_basename = plugin_basename( $this->bootstrap->boot_file );
72 88
@@ -77,8 +93,11 @@
77 93 'sm' => array()
78 94 )
79 95 ));
80 96
97 + add_action('activated_plugin', array( $this, 'check_setup' ));
98 + add_action('admin_notices', array( $this, 'documentation_message' ), 1);
99 +
81 100 // Setting sm variable
82 101 $this->refresh();
83 102
84 103 $this->set('page_url.stateless_setup', $this->bootstrap->get_settings_page_url('?page=stateless-setup'));
@@ -85,8 +104,14 @@
85 104 $this->set('page_url.stateless_settings', $this->bootstrap->get_settings_page_url('?page=stateless-settings'));
86 105
87 106 /** Register options */
88 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 +
89 114 // apply wildcard to root dir.
90 115 add_filter( 'wp_stateless_handle_root_dir', array( $this, 'root_dir_wildcards' ), 10, 3);
91 116
92 117 // Parse root dir by wildcards
@@ -91,8 +116,28 @@
91 116
92 117 // Parse root dir by wildcards
93 118 add_filter( 'wp_stateless_unhandle_root_dir', array( $this, 'parse_root_dir_wildcards' ), 10, 3);
94 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 +
95 140 $site_url = parse_url( site_url() );
96 141 $site_url['path'] = isset($site_url['path']) ? $site_url['path'] : '';
97 142 $this->wildcards = array(
98 143 'sites' => [
@@ -129,24 +174,8 @@
129 174 );
130 175 }
131 176
132 177 /**
133 - * Init
134 - */
135 - public function init(){
136 - $this->save_media_settings();
137 -
138 - add_action('admin_menu', array( $this, 'admin_menu' ));
139 - /**
140 - * Manage specific Network Settings
141 - */
142 - if( is_network_admin() ) {
143 - add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ));
144 - }
145 -
146 - }
147 -
148 - /**
149 178 * Refresh settings
150 179 */
151 180 public function refresh() {
152 181 $this->set( "sm.readonly", []);
@@ -296,8 +325,10 @@
296 325 }
297 326 }
298 327
299 328 $this->set( 'sm.strings', $this->strings );
329 +
330 + do_action('wp_stateless_settings_refresh', $this);
300 331 }
301 332
302 333 /**
303 334 * Remove settings
@@ -378,9 +409,9 @@
378 409 * For now only Y/M is dynamic. We will get it via using regex.
379 410 * @param $path
380 411 * @return array
381 412 */
382 - public function parse_root_dir_wildcards ( $path ) {
413 + public function parse_root_dir_wildcards( $path ) {
383 414 $result = [];
384 415
385 416 /**
386 417 * removing GS host from path
@@ -410,10 +441,8 @@
410 441
411 442 return $result;
412 443 }
413 444
414 -
415 -
416 445 /**
417 446 * Add menu options
418 447 */
419 448 public function admin_menu() {
@@ -418,9 +447,10 @@
418 447 */
419 448 public function admin_menu() {
420 449 $key_json = $this->get('sm.key_json');
421 450 if($this->get('sm.hide_setup_assistant') != 'true' && empty($key_json) ){
422 - $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') );
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') );
423 453 }
424 454
425 455 if($this->get('sm.hide_settings_panel') != 'true'){
426 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') );
@@ -431,9 +461,10 @@
431 461 * Add menu options
432 462 * @param $slug
433 463 */
434 464 public function network_admin_menu($slug) {
435 - $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') );
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') );
436 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') );
437 468 }
438 469
439 470 /**
@@ -439,58 +470,10 @@
439 470 /**
440 471 * Draw interface
441 472 */
442 473 public function settings_interface() {
443 - $wildcards = apply_filters('wp_stateless_root_dir_wildcard', $this->wildcards);
444 - $wildcard_year_month = '%date_year/date_month%';
445 - $root_dir = $this->get( 'sm.root_dir' );
474 + $tab = isset($_GET['tab']) && !empty($_GET['tab']) ? $_GET['tab'] : 'stless_settings_tab';
446 475
447 - $use_year_month = (strpos($root_dir, $wildcard_year_month) !== false) ?: false;
448 -
449 - /**
450 - * removing year/month wildcard
451 - */
452 - if ($use_year_month) {
453 - $root_dir = str_replace($wildcard_year_month, '%YM%', $root_dir);
454 - }
455 -
456 - /**
457 - * preparing array with wildcards
458 - */
459 - $root_dir_values = explode('/', $root_dir);
460 -
461 - /**
462 - * adding year/month wildcard
463 - */
464 - if ($use_year_month) {
465 - if ( !empty($root_dir_values) ) {
466 - foreach( $root_dir_values as $k=>$root_dir_value ) {
467 - if ( $root_dir_value == '%YM%' ) {
468 - $root_dir_values[$k] = $wildcard_year_month;
469 - }
470 - }
471 - } else {
472 - $root_dir_values[] = $wildcard_year_month;
473 - }
474 - }
475 -
476 - /**
477 - * first slash
478 - */
479 - array_unshift($root_dir_values , '/');
480 -
481 - /**
482 - * removing empty values
483 - */
484 - $root_dir_values = array_filter($root_dir_values);
485 -
486 - /**
487 - * merging user's wildcards with default values
488 - */
489 - if (!empty($root_dir_values)) {
490 - $wildcards = array_merge(array_flip($root_dir_values), $wildcards);
491 - }
492 -
493 476 include $this->bootstrap->path( '/static/views/settings_interface.php', 'dir' );
494 477 }
495 478
496 479 /**
@@ -518,10 +501,15 @@
518 501 $settings = apply_filters('stateless::settings::save', $_POST['sm']);
519 502 $root_dir_value = false;
520 503
521 504 foreach ( $settings as $name => $value ) {
522 -
523 505 /**
506 + * Sanitize POST data
507 + */
508 + if (!is_array($value)) {
509 + $value = sanitize_text_field($value);
510 + }
511 + /**
524 512 * root_dir settings
525 513 */
526 514 if ( 'root_dir' == $name && is_array($value) ) {
527 515 //managed in WP-Stateless settings (via Bucket Folder control)
@@ -576,14 +564,211 @@
576 564 * Wrapper for setting value.
577 565 * @param string $key
578 566 * @param bool $value
579 567 * @param bool $bypass_validation
580 - * @return \UsabilityDynamics\Settings
568 + * @return \UDX\Settings
581 569 */
582 570 public function set( $key = '', $value = false, $bypass_validation = false ) {
583 571 return parent::set( $key, $value, $bypass_validation );
584 572 }
585 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 + }
586 771 }
587 772
588 773 }
589 774