PluginProbe
WP-Stateless – Google Cloud Storage / 2.2.0
WP-Stateless – Google Cloud Storage v2.2.0
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.2.0, at lib/classes/class-settings.php

382 lines 13.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', '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 );
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 * Overridden 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( 'network_admin_menu', array( $this, 'network_admin_menu' ));
78 }
79
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 );
85 }
86
87 public function init(){
88
89 }
90
91 /**
92 * Refresh settings
93 */
94 public function refresh() {
95 $constant_mode = false;
96 $upload_data = wp_upload_dir();
97 $google_app_key_file = getenv('GOOGLE_APPLICATION_CREDENTIALS') ?: getenv('GOOGLE_APPLICATION_CREDENTIALS');
98
99 foreach ($this->settings as $option => $array) {
100 $value = '';
101 $_option = 'sm_' . $option;
102 $constant = $array[0]; // Constant name
103 $default = $array[1]; // Default value
104
105 if($option == 'organize_media'){
106 $_option = 'uploads_use_yearmonth_folders';
107 }
108
109 // Getting settings
110 $value = get_option($_option, $default);
111
112 if ($option == 'body_rewrite_types' && empty($value) && !is_multisite()) {
113 $value = $default;
114 }
115
116 if ($option == 'hashify_file_name' && $this->get("sm.mode") == 'stateless') {
117 $value = true;
118 }
119
120 // If constant is set then override by constant
121 if(is_array($constant)){
122 foreach($constant as $old_const => $new_const){
123 if(defined($new_const)){
124 $value = constant($new_const);
125 $this->set( "sm.readonly.{$option}", "constant" );
126 break;
127 }
128 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;
137 }
138 }
139 }
140 elseif(defined($constant)){
141 $value = constant($constant);
142 $this->set( "sm.readonly.{$option}", "constant" );
143 }
144 // Getting network settings
145 elseif(is_multisite() && $option != 'organize_media'){
146 $network = get_site_option( $_option );
147 // If network settings available then override by network settings.
148 if($network || is_network_admin()){
149 $value = $network;
150 if(!is_network_admin())
151 $this->set( "sm.readonly.{$option}", "network" );
152 }
153
154 }
155
156 // Converting to string true false for angular.
157 if(is_bool($value)){
158 $value = $value === true ? "true" : "false";
159 }
160
161 $this->set( "sm.$option", $value);
162 }
163
164 // Network only settings, to hide settings page
165 foreach ($this->network_only_settings as $option => $array) {
166 $value = '';
167 $_option = 'sm_' . $option;
168 $constant = $array[0]; // Constant name
169 $default = $array[1]; // Default value
170
171 // If constant is set then override by constant
172 if(is_array($constant)){
173 foreach($constant as $old_const => $new_const){
174 if(defined($new_const)){
175 $value = constant($new_const);
176 break;
177 }
178 if(is_string($old_const) && defined($old_const)){
179 $value = constant($old_const);
180 trigger_error(__(sprintf("<i>%s</i> constant is deprecated, please use <i>%s</i> instead.", $old_const, $new_const)), E_USER_WARNING);
181 break;
182 }
183 }
184 }
185 elseif(defined($constant)){
186 $value = constant($constant);
187 }
188 // Getting network settings
189 elseif(is_multisite()){
190 $value = get_site_option( $_option, $default );
191 }
192
193 // Converting to string true flase for angular.
194 if(is_bool($value)){
195 $value = $value === true ? "true" : "false";
196 }
197
198 $this->set( "sm.$option", $value);
199 }
200
201 /**
202 * JSON key file path
203 */
204 /* Use constant value for JSON key file path, if set. */
205 if (defined('WP_STATELESS_MEDIA_KEY_FILE_PATH') || $google_app_key_file !== false) {
206 /* Maybe fix the path to p12 file. */
207 $key_file_path = (defined('WP_STATELESS_MEDIA_KEY_FILE_PATH')) ? WP_STATELESS_MEDIA_KEY_FILE_PATH : $google_app_key_file;
208
209 if( !empty( $key_file_path ) ) {
210 $upload_dir = wp_upload_dir();
211 /* Check if file exists */
212 switch( true ) {
213 /* Determine if default path is correct */
214 case (file_exists($key_file_path)):
215 /* Path is correct. Do nothing */
216 break;
217 /* Look using WP root. */
218 case (file_exists( ABSPATH . $key_file_path ) ):
219 $key_file_path = ABSPATH . $key_file_path;
220 break;
221 /* Look in wp-content dir */
222 case (file_exists( WP_CONTENT_DIR . $key_file_path ) ):
223 $key_file_path = WP_CONTENT_DIR . $key_file_path;
224 break;
225 /* Look in uploads dir */
226 case (file_exists( wp_normalize_path( $upload_dir[ 'basedir' ] ) . '/' . $key_file_path ) ):
227 $key_file_path = wp_normalize_path( $upload_dir[ 'basedir' ] ) . '/' . $key_file_path;
228 break;
229 /* 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' );
232 break;
233
234 }
235 if(is_readable($key_file_path)) {
236 $this->set( 'sm.key_json', file_get_contents($key_file_path) );
237 if(defined('WP_STATELESS_MEDIA_KEY_FILE_PATH'))
238 $this->set( "sm.readonly.key_json", "constant" );
239 else
240 $this->set("sm.readonly.key_json", "environment");
241 }
242 }
243 }
244
245 $this->set( 'sm.strings', $this->strings );
246 }
247
248 /**
249 * Remove settings
250 */
251 public function reset($network = false) {
252 foreach ($this->settings as $option => $array) {
253 if($option == 'organize_media')
254 continue;
255 $_option = 'sm_' . $option;
256
257 if($network && current_user_can('manage_network')){
258 delete_site_option($_option);
259 delete_option($_option);
260 }
261 else{
262 delete_option($_option);
263 }
264 }
265
266 foreach ($this->network_only_settings as $option => $array) {
267 $_option = 'sm_' . $option;
268 if($network && current_user_can('manage_network')){
269 delete_site_option($_option);
270 delete_option($_option);
271 }
272 }
273
274 $this->set('sm', []);
275 $this->refresh();
276 }
277
278 /**
279 * Add menu options
280 */
281 public function admin_menu() {
282 $key_json = $this->get('sm.key_json');
283 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') );
285 }
286
287 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') );
289 }
290 }
291
292 /**
293 * Add menu options
294 */
295 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') );
298 }
299
300 /**
301 * Draw interface
302 */
303 public function settings_interface() {
304 include ud_get_stateless_media()->path( '/static/views/settings_interface.php', 'dir' );
305 }
306
307 /**
308 * Draw interface
309 */
310 public function regenerate_interface() {
311 include ud_get_stateless_media()->path( '/static/views/regenerate_interface.php', 'dir' );
312 }
313
314 /**
315 * Draw interface
316 */
317 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 }
330 }
331
332 /**
333 * Handles saving SM data.
334 *
335 * @author alim@UD
336 */
337 public function save_media_settings(){
338 if(isset($_POST['action']) && $_POST['action'] == 'stateless_settings' && wp_verify_nonce( $_POST['_smnonce'], 'wp-stateless-settings' )){
339
340 $settings = apply_filters('stateless::settings::save', $_POST['sm']);
341 foreach ( $settings as $name => $value ) {
342 $option = 'sm_'. $name;
343
344 if($name == 'organize_media'){
345 $option = 'uploads_use_yearmonth_folders';
346 }
347
348 if(is_network_admin()){
349 update_site_option( $option, $value );
350 }
351 else{
352 update_option( $option, $value );
353 }
354 }
355
356 ud_get_stateless_media()->flush_transients();
357 }
358 }
359
360 /**
361 * Wrapper for setting value.
362 * @param string $key
363 * @param bool $value
364 * @param bool $bypass_validation
365 * @return \UsabilityDynamics\Settings
366 */
367 public function set( $key = '', $value = false, $bypass_validation = false ) {
368
369 //if ( $value !== false ) {
370 // update_option( str_replace( '.', '_', $key ), $value );
371 //}
372
373 return parent::set( $key, $value, $bypass_validation );
374
375 }
376
377 }
378
379 }
380
381 }
382