PluginProbe
WP-Stateless – Google Cloud Storage / 2.1.4
WP-Stateless – Google Cloud Storage v2.1.4
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
wp-stateless / lib / classes / class-settings.php

class-settings.php in WP-Stateless – Google Cloud Storage 2.1.4, at lib/classes/class-settings.php

336 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 \UsabilityDynamics\Settings {
12
13 /**
14 * @var false|null|string
15 */
16 public $setup_wizard_ui = null;
17
18 /**
19 * @var false|null|string
20 */
21 public $stateless_settings = null;
22
23 private $settings = array(
24 'mode' => array('WP_STATELESS_MEDIA_MODE', 'cdn'),
25 'body_rewrite' => array('WP_STATELESS_MEDIA_BODY_REWRITE', 'true'),
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', 'true'),
35 );
36
37 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 );
41
42 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 );
47
48 /**
49 * Overriden construct
50 */
51 public function __construct() {
52
53 add_action('admin_menu', array( $this, 'admin_menu' ));
54
55
56 $this->save_media_settings();
57
58
59 /* Add 'Settings' link for SM plugin on plugins page. */
60 $_basename = plugin_basename( ud_get_stateless_media()->boot_file );
61
62 parent::__construct( array(
63 'store' => 'options',
64 'format' => 'json',
65 'data' => array(
66 'sm' => array()
67 )
68 ));
69
70 // Setting sm variable
71 $this->refresh();
72
73 /**
74 * Manage specific Network Settings
75 */
76 if( is_network_admin() ) {
77 add_action( 'update_wpmu_options', array( $this, 'save_network_settings' ) );
78 add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ));
79 }
80
81 $this->set('page_url.stateless_setup', ud_get_stateless_media()->get_settings_page_url('?page=stateless-setup'));
82 $this->set('page_url.stateless_settings', ud_get_stateless_media()->get_settings_page_url('?page=stateless-settings'));
83
84 /** Register options */
85 add_action( 'init', array( $this, 'init' ), 3 );
86 }
87
88 public function init(){
89
90 }
91
92 /**
93 * Refresh settings
94 */
95 public function refresh() {
96 $constant_mode = false;
97 $upload_data = wp_upload_dir();
98 $google_app_key_file = getenv('GOOGLE_APPLICATION_CREDENTIALS') ?: getenv('GOOGLE_APPLICATION_CREDENTIALS');
99
100 foreach ($this->settings as $option => $array) {
101 $value = '';
102 $_option = 'sm_' . $option;
103 $constant = $array[0]; // Constant name
104 $default = $array[1]; // Default value
105
106 if($option == 'organize_media'){
107 $_option = 'uploads_use_yearmonth_folders';
108 }
109
110 // Getting settings
111 $value = get_option($_option, $default);
112
113 if ($option == 'body_rewrite_types' && empty($value) && !is_multisite()) {
114 $value = $default;
115 }
116
117 // If constant is set then override by constant
118 if(defined($constant)){
119 $value = constant($constant);
120 $this->set( "sm.readonly.{$option}", "constant" );
121 }
122 // Getting network settings
123 elseif(is_multisite() && $option != 'organize_media'){
124 $network = get_site_option( $_option );
125 // If network settings available then override by network settings.
126 if($network || is_network_admin()){
127 $value = $network;
128 if(!is_network_admin())
129 $this->set( "sm.readonly.{$option}", "network" );
130 }
131
132 }
133
134 $this->set( "sm.$option", $value);
135 }
136
137 foreach ($this->network_only_settings as $option => $array) {
138 $value = '';
139 $_option = 'sm_' . $option;
140 $constant = $array[0]; // Constant name
141 $default = $array[1]; // Default value
142
143 // If constant is set then override by constant
144 if(defined($constant)){
145 $value = constant($constant);
146 }
147 // Getting network settings
148 elseif(is_multisite()){
149 $value = get_site_option( $_option, $default );
150 }
151
152 $this->set( "sm.$option", $value);
153 }
154
155 /**
156 * JSON key file path
157 */
158 /* Use constant value for JSON key file path, if set. */
159 if (defined('WP_STATELESS_MEDIA_KEY_FILE_PATH') || $google_app_key_file !== false) {
160 /* Maybe fix the path to p12 file. */
161 $key_file_path = (defined('WP_STATELESS_MEDIA_KEY_FILE_PATH')) ? WP_STATELESS_MEDIA_KEY_FILE_PATH : $google_app_key_file;
162
163 if( !empty( $key_file_path ) ) {
164 $upload_dir = wp_upload_dir();
165 /* Check if file exists */
166 switch( true ) {
167 /* Determine if default path is correct */
168 case (file_exists($key_file_path)):
169 /* Path is correct. Do nothing */
170 break;
171 /* Look using WP root. */
172 case (file_exists( ABSPATH . $key_file_path ) ):
173 $key_file_path = ABSPATH . $key_file_path;
174 break;
175 /* Look in wp-content dir */
176 case (file_exists( WP_CONTENT_DIR . $key_file_path ) ):
177 $key_file_path = WP_CONTENT_DIR . $key_file_path;
178 break;
179 /* Look in uploads dir */
180 case (file_exists( wp_normalize_path( $upload_dir[ 'basedir' ] ) . '/' . $key_file_path ) ):
181 $key_file_path = wp_normalize_path( $upload_dir[ 'basedir' ] ) . '/' . $key_file_path;
182 break;
183 /* Look using Plugin root */
184 case (file_exists(ud_get_stateless_media()->path( $key_file_path, 'dir') ) ):
185 $key_file_path = ud_get_stateless_media()->path( $key_file_path, 'dir' );
186 break;
187
188 }
189 if(is_readable($key_file_path)) {
190 $this->set( 'sm.key_json', file_get_contents($key_file_path) );
191 if(defined('WP_STATELESS_MEDIA_KEY_FILE_PATH'))
192 $this->set( "sm.readonly.key_json", "constant" );
193 else
194 $this->set("sm.readonly.key_json", "environment");
195 }
196 }
197 }
198
199 $this->set( 'sm.strings', $this->strings );
200 }
201
202 /**
203 * Remove settings
204 */
205 public function reset($network = false) {
206 foreach ($this->settings as $option => $array) {
207 if($option == 'organize_media')
208 continue;
209 $_option = 'sm_' . $option;
210
211 if($network && current_user_can('manage_network')){
212 delete_site_option($_option);
213 delete_option($_option);
214 }
215 else{
216 delete_option($_option);
217 }
218 }
219
220 foreach ($this->network_only_settings as $option => $array) {
221 $_option = 'sm_' . $option;
222 if($network && current_user_can('manage_network')){
223 delete_site_option($_option);
224 delete_option($_option);
225 }
226 }
227
228 $this->set('sm', []);
229 $this->refresh();
230 }
231
232 /**
233 * Add menu options
234 */
235 public function admin_menu() {
236 $key_json = $this->get('sm.key_json');
237 if($this->get('sm.hide_setup_assistant') != 'true' && empty($key_json) ){
238 $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') );
239 }
240
241 if($this->get('sm.hide_settings_panel') != 'true'){
242 $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') );
243 }
244 }
245
246 /**
247 * Add menu options
248 */
249 public function network_admin_menu($slug) {
250 $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') );
251 $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') );
252 }
253
254 /**
255 * Draw interface
256 */
257 public function settings_interface() {
258 include ud_get_stateless_media()->path( '/static/views/settings_interface.php', 'dir' );
259 }
260
261 /**
262 * Draw interface
263 */
264 public function regenerate_interface() {
265 include ud_get_stateless_media()->path( '/static/views/regenerate_interface.php', 'dir' );
266 }
267
268 /**
269 * Draw interface
270 */
271 public function setup_wizard_interface() {
272 $step = !empty($_GET['step'])?$_GET['step']:'';
273 switch ($step) {
274 case 'google-login':
275 case 'setup-project':
276 case 'finish':
277 include ud_get_stateless_media()->path( '/static/views/setup_wizard_interface.php', 'dir' );
278 break;
279
280 default:
281 include ud_get_stateless_media()->path( '/static/views/stateless_splash_screen.php', 'dir' );
282 break;
283 }
284 }
285
286 /**
287 * Handles saving SM data.
288 *
289 * @author alim@UD
290 */
291 public function save_media_settings(){
292 if(isset($_POST['action']) && $_POST['action'] == 'stateless_settings' && wp_verify_nonce( $_POST['_smnonce'], 'wp-stateless-settings' )){
293
294 $settings = apply_filters('stateless::settings::save', $_POST['sm']);
295 foreach ( $settings as $name => $value ) {
296 $option = 'sm_'. $name;
297
298 if($name == 'organize_media'){
299 $option = 'uploads_use_yearmonth_folders';
300 }
301
302 if(is_network_admin()){
303 update_site_option( $option, $value );
304 }
305 else{
306 update_option( $option, $value );
307 }
308 }
309
310 ud_get_stateless_media()->flush_transients();
311 }
312 }
313
314 /**
315 * Wrapper for setting value.
316 * @param string $key
317 * @param bool $value
318 * @param bool $bypass_validation
319 * @return \UsabilityDynamics\Settings
320 */
321 public function set( $key = '', $value = false, $bypass_validation = false ) {
322
323 //if ( $value !== false ) {
324 // update_option( str_replace( '.', '_', $key ), $value );
325 //}
326
327 return parent::set( $key, $value, $bypass_validation );
328
329 }
330
331 }
332
333 }
334
335 }
336