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

359 lines 16.2 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 private $regenerate_ui = null;
17
18 /**
19 * Overriden construct
20 */
21 public function __construct() {
22
23 add_action('admin_menu', array( $this, 'admin_menu' ));
24
25 /* Add 'Settings' link for SM plugin on plugins page. */
26 $_basename = plugin_basename( ud_get_stateless_media()->boot_file );
27
28 add_filter( "plugin_action_links_" . $_basename, function( $links ) {
29 $settings_link = '<a href="options-media.php#stateless-media">' . __( 'Settings', ud_get_stateless_media()->domain ) . '</a>';
30 array_unshift($links, $settings_link);
31 return $links;
32 });
33
34 parent::__construct( array(
35 'store' => 'options',
36 'format' => 'json',
37 'data' => array(
38 'sm' => array(
39 'mode' => get_option( 'sm_mode', 'disabled' ),
40 'bucket' => get_option( 'sm_bucket' ),
41 'root_dir' => get_option( 'sm_root_dir' ),
42 'key_json' => get_option( 'sm_key_json' ),
43 'body_rewrite' => get_option( 'sm_body_rewrite' ),
44 'on_fly' => get_option( 'sm_on_fly' ),
45 'delete_remote' => get_option( 'sm_delete_remote' ),
46 'hashify_file_name' => get_option( 'sm_hashify_file_name' ),
47 'override_cache_control' => get_option( 'sm_override_cache_control' ),
48 'cache_control' => get_option( 'sm_cache_control' )
49 )
50 )
51 ));
52
53 /* Use constant value for mode, if set. */
54 if( defined( 'WP_STATELESS_MEDIA_MODE' ) ) {
55 $this->set( 'sm.mode', WP_STATELESS_MEDIA_MODE );
56 }
57
58 /* Use constant value for Bucket, if set. */
59 if( defined( 'WP_STATELESS_MEDIA_BUCKET' ) ) {
60 $this->set( 'sm.bucket', WP_STATELESS_MEDIA_BUCKET );
61 }
62
63 /* Use constant value for Root Dir, if set. */
64 if( defined( 'WP_STATELESS_MEDIA_ROOT_DIR' ) ) {
65 $this->set( 'sm.root_dir', WP_STATELESS_MEDIA_ROOT_DIR );
66 }
67
68 /* Set default cacheControl in case it is empty */
69 $cache_control = trim( $this->get( 'sm.cache_control' ) );
70 if ( empty( $cache_control ) ) {
71 $this->set( 'sm.cache_control', 'public, max-age=36000, must-revalidate' );
72 }
73
74 /**
75 * Manage specific Network Settings
76 */
77 if( ud_get_stateless_media()->is_network_detected() || !is_multisite() ) {
78
79 add_filter( 'wpmu_options', array( $this, 'register_network_settings' ) );
80 add_action( 'update_wpmu_options', array( $this, 'save_network_settings' ) );
81 }
82
83 /** Register options */
84 add_action( 'admin_init', array( $this, 'register_settings' ) );
85 }
86
87 /**
88 * Refresh settings
89 */
90 public function refresh() {
91 $this->set('sm', array(
92 'mode' => get_option( 'sm_mode', 'disabled' ),
93 'bucket' => get_option( 'sm_bucket' ),
94 'root_dir' => get_option( 'sm_root_dir' ),
95 'key_json' => get_option( 'sm_key_json' ),
96 'body_rewrite' => get_option( 'sm_body_rewrite' ),
97 'on_fly' => get_option( 'sm_on_fly' ),
98 'delete_remote' => get_option( 'sm_delete_remote' ),
99 'hashify_file_name' => get_option( 'sm_hashify_file_name' ),
100 'override_cache_control' => get_option( 'sm_override_cache_control' ),
101 'cache_control' => get_option( 'sm_cache_control' )
102 ));
103 }
104
105 /**
106 * Add menu options
107 */
108 public function admin_menu() {
109 $this->regenerate_ui = add_management_page( __( 'Stateless Images Synchronisation', ud_get_stateless_media()->domain ), __( 'Stateless Sync', ud_get_stateless_media()->domain ), 'manage_options', 'stateless-regenerate', array($this, 'regenerate_interface') );
110 }
111
112 /**
113 * Draw interface
114 */
115 public function regenerate_interface() {
116 include ud_get_stateless_media()->path( '/static/views/regenerate_interface.php', 'dir' );
117 }
118
119 /**
120 * Handles saving network SM data.
121 *
122 * @action update_wpmu_options
123 * @author peshkov@UD
124 */
125 public function save_network_settings() {
126 $settings = $_POST['sm'];
127 foreach ( $settings as $name => $value ) {
128 update_site_option( 'sm_'. $name, stripslashes($value) );
129 }
130 }
131
132 /**
133 * Registers Network Settings in case plugin is Network Enabled.
134 *
135 * @action wpmu_options
136 * @author peshkov@UD
137 */
138 public function register_network_settings() {
139 ?>
140 <h3><?php _e( 'Stateless Media Settings', ud_get_stateless_media()->domain ); ?></h3>
141 <p><?php $this->section_callback(); ?></p>
142 <div class="key_type"><label><b><?php _e('Service Account JSON', ud_get_stateless_media()->domain) ?></b></label>
143 <div class="_key_type _sm_key_json">
144 <textarea id="sm_key_json" class="field regular-textarea sm_key_json" type="text" name="sm[key_json]"><?php echo get_site_option( 'sm_key_json' ); ?></textarea>
145 </div>
146 </div>
147 <?php
148 }
149
150 /**
151 * Adds options
152 */
153 public function register_settings() {
154
155 //** Register Setting */
156 register_setting( 'media', 'sm', array( $this, 'validate' ) );
157
158 //** Add Section */
159 add_settings_section( 'sm', __( 'Stateless Media', ud_get_stateless_media()->domain ),array( $this, 'section_callback' ), 'media' );
160
161 //** Add Fields */
162 add_settings_field( 'sm.mode', __( 'Mode', ud_get_stateless_media()->domain ), array( $this, 'sm_fields_mode_callback' ), 'media', 'sm' );
163
164 //** Add Fields */
165 add_settings_field( 'sm.credentials', __( 'Credentials', ud_get_stateless_media()->domain ), array( $this, 'sm_fields_credentials_callback' ), 'media', 'sm' );
166 add_settings_field( 'sm.advanced', __( 'Advanced', ud_get_stateless_media()->domain ), array( $this, 'sm_fields_advanced_callback' ), 'media', 'sm' );
167
168 if( defined( 'WP_DEBUG' ) && WP_DEBUG === true || WP_DEBUG === 'true' ) {
169 add_settings_field( 'sm.debug', __( 'Debug', ud_get_stateless_media()->domain ), array( $this, 'sm_fields_debug_callback' ), 'media', 'sm' );
170 }
171
172 }
173
174 /**
175 * Before save filter
176 * Used to sync options with options table
177 *
178 * @param type $input
179 * @return type
180 */
181 public function validate( $input ) {
182
183 if ( !empty( $input ) && is_array( $input ) ) {
184
185 $_has_updates = false;
186
187 foreach( $input as $_field => $_value ) {
188 if ( update_option( "sm_{$_field}", $_value ) ) {
189 $_has_updates = true;
190 }
191 }
192
193 if ( $_has_updates ) {
194 /* Reset all plugin's transients. */
195 ud_get_stateless_media()->flush_transients();
196 }
197
198 }
199
200 return $input;
201 }
202
203 /**
204 * Advanced Media Options
205 *
206 * @author potanin@UD
207 */
208 public function sm_fields_advanced_callback() {
209
210 $inputs = array();
211
212 // override cache control
213 $inputs[] = '<input type="hidden" name="sm[override_cache_control]" value="false" />';
214 $inputs[] = '<label for="sm_override_cache_control"><input id="sm_override_cache_control" type="checkbox" name="sm[override_cache_control]" value="true" '. checked( 'true', $this->get( 'sm.override_cache_control' ), false ) .'/>'.__( 'Override default Cache Control', ud_get_stateless_media()->domain ).'<small> '.__( '(Use input bellow to change cache control)', ud_get_stateless_media()->domain ).'</small></label>';
215
216 // cache control input
217 $inputs[] = '<input id="sm_cache_control" class="regular-text" type="text" name="sm[cache_control]" value="'.$this->get( 'sm.cache_control' ).'" />';
218
219 // body content rewrite
220 $inputs[] = '<input type="hidden" name="sm[body_rewrite]" value="false" />';
221 $inputs[] = '<label for="sm_body_rewrite"><input id="sm_body_rewrite" type="checkbox" name="sm[body_rewrite]" value="true" '. checked( 'true', $this->get( 'sm.body_rewrite' ), false ) .'/>'.__( 'Body content media URL rewrite.', ud_get_stateless_media()->domain ).'</label>';
222
223 // on fly generate
224 $inputs[] = '<input type="hidden" name="sm[on_fly]" value="false" />';
225 $inputs[] = '<label for="sm_on_fly"><input id="sm_on_fly" type="checkbox" name="sm[on_fly]" value="true" '. checked( 'true', $this->get( 'sm.on_fly' ), false ) .'/>'.__( 'Upload on-fly generated (by third-party scripts) images to GCS.', ud_get_stateless_media()->domain ).'<small> '.__( '(This option may slow down file upload processes)', ud_get_stateless_media()->domain ).'</small></label>';
226
227 // delete remote
228 $inputs[] = '<input type="hidden" name="sm[delete_remote]" value="false" />';
229 $inputs[] = '<label for="sm_delete_remote"><input id="sm_delete_remote" type="checkbox" name="sm[delete_remote]" value="true" '. checked( 'true', $this->get( 'sm.delete_remote' ), false ) .'/>'.__( 'Delete media from GCS when media is deleted from the site.', ud_get_stateless_media()->domain ).'<small> '.__( '(This option may slow down media deletion process)', ud_get_stateless_media()->domain ).'</small></label>';
230
231 // hashify
232 $inputs[] = '<input type="hidden" name="sm[hashify_file_name]" value="false" />';
233 $inputs[] = '<label for="sm_hashify_file_name"><input id="sm_hashify_file_name" type="checkbox" name="sm[hashify_file_name]" value="true" '. checked( 'true', $this->get( 'sm.hashify_file_name' ), false ) .'/>'.__( 'Randomize the filename of newly uploaded media files.', ud_get_stateless_media()->domain ).'<small> '.__( '(May help to avoid unwanted GCS caching)', ud_get_stateless_media()->domain ).'</small></label>';
234
235 echo '<section class="wp-stateless-media-options wp-stateless-media-advanced-options"><p>' . implode( "</p>\n<p>", (array) apply_filters( 'sm::settings::advanced', $inputs ) ) . '</p></section>';
236
237 }
238
239 /**
240 * Debug output
241 * @author potanin@UD
242 */
243 public function sm_fields_debug_callback() {
244
245 echo( '<pre style="width:600px;overflow:scroll;">' . print_r( json_decode($this->get()), true ) . '</pre>' );
246
247 }
248
249 /**
250 * Render Credential Inputs
251 *
252 */
253 public function sm_fields_credentials_callback() {
254
255 $inputs = array( '<section class="wp-stateless-media-options wp-stateless-credentials-options">' );
256
257 if( !defined( 'WP_STATELESS_MEDIA_BUCKET' ) ) {
258 $inputs[ ] = '<p><label for="sm_bucket">'.__( 'Bucket', ud_get_stateless_media()->domain ).'</label><div><input id="sm_bucket" class="regular-text" type="text" name="sm[bucket]" value="'. esc_attr( $this->get( 'sm.bucket' ) ) .'" /></div></p>';
259 } else {
260 $inputs[ ] = '<p><label for="sm_bucket">'.__( 'Bucket', ud_get_stateless_media()->domain ).'</label><div><input id="sm_bucket" class="regular-text" readonly="readonly" type="text" name="sm[bucket]" value="'. esc_attr( $this->get( 'sm.bucket' ) ) .'" /></div></p>';
261 }
262
263 if( !defined( 'WP_STATELESS_MEDIA_ROOT_DIR' ) ) {
264 $inputs[ ] = '<p><label for="sm_bucket">'.__( 'Root Directory', ud_get_stateless_media()->domain ).'<small> '.__('(With trailing slash!)', ud_get_stateless_media()->domain).'</small></label><div><input id="sm_bucket" class="regular-text" type="text" name="sm[root_dir]" value="'. esc_attr( $this->get( 'sm.root_dir' ) ) .'" /></div></p>';
265 } else {
266 $inputs[ ] = '<p><label for="sm_bucket">'.__( 'Root Directory', ud_get_stateless_media()->domain ).'<small> '.__('(With trailing slash!)', ud_get_stateless_media()->domain).'</small></label><div><input id="sm_bucket" class="regular-text" readonly="readonly" type="text" name="sm[root_dir]" value="'. esc_attr( $this->get( 'sm.root_dir' ) ) .'" /></div></p>';
267 }
268
269 if( ud_get_stateless_media()->is_network_detected() ) {
270
271 if( is_super_admin() ) {
272
273 $kjsn_readonly = get_site_option( 'sm_key_json' ) || defined( 'WP_STATELESS_MEDIA_KEY_FILE_PATH' ) ? 'readonly="readonly"' : '';
274
275 $inputs[] = '<div class="key_type"><label>'.__('Service Account JSON', ud_get_stateless_media()->domain).'</label>';
276 $inputs[] = '<div class="_key_type _sm_key_json">';
277 $inputs[] = '<textarea '.$kjsn_readonly.' id="sm_key_json" class="field regular-textarea sm_key_json" type="text" name="sm[key_json]" >'. esc_attr( $this->get( 'sm.key_json' ) ) .'</textarea>';
278 $inputs[] = '</div>';
279 $inputs[] = '</div>';
280
281 if( $kjsn_readonly ) {
282 $inputs[ ] = '<p class="description">' . sprintf( __( 'The account name can not be changed because it is set via <a href="%s">Network Settings.</a>' ), network_admin_url( 'settings.php' ) ) . '</p>';
283 }
284
285 }
286
287 } else {
288
289 $inputs[] = '<div class="key_type"><label>'.__('Service Account JSON', ud_get_stateless_media()->domain).'</label>';
290 $inputs[] = '<div class="_key_type _sm_key_json">';
291 $inputs[] = '<textarea id="sm_key_json" class="field regular-textarea sm_key_json" type="text" name="sm[key_json]" >'. esc_attr( $this->get( 'sm.key_json' ) ) .'</textarea>';
292 $inputs[] = '</div>';
293 $inputs[] = '</div>';
294 }
295
296 $inputs[] = '</section>';
297
298 echo implode( "\n", (array) apply_filters( 'sm::settings::credentials', $inputs ) );
299
300 }
301
302 /**
303 * Render inputs
304 */
305 public function sm_fields_mode_callback() {
306
307 $inputs = array(
308 '<p class="sm-mode"><label for="sm_mode_disabled"><input id="sm_mode_disabled" '. checked( 'disabled', $this->get( 'sm.mode' ), false ) .' type="radio" name="sm[mode]" value="disabled" />'.__( 'Disabled', ud_get_stateless_media()->domain ).''
309 . '<small class="description">'.__('Disable Stateless Media.', ud_get_stateless_media()->domain).'</small></label></p>',
310 '<p class="sm-mode"><label for="sm_mode_backup"><input id="sm_mode_backup" '. checked( 'backup', $this->get( 'sm.mode' ), false ) .' type="radio" name="sm[mode]" value="backup" />'.__( 'Backup', ud_get_stateless_media()->domain ).''
311 . '<small class="description">'.__('Push media files to Google Storage but keep using local ones.', ud_get_stateless_media()->domain).'</small></label></p>',
312 '<p class="sm-mode"><label for="sm_mode_cdn"><input id="sm_mode_cdn" '. checked( 'cdn', $this->get( 'sm.mode' ), false ) .' type="radio" name="sm[mode]" value="cdn" />'.__( 'CDN', ud_get_stateless_media()->domain ).''
313 . '<small class="description">'.__('Push media files to Google Storage and use them directly from there.', ud_get_stateless_media()->domain).'</small></label></p>'
314 );
315
316 echo implode( "\n", (array)apply_filters( 'sm::settings::mode', $inputs ) );
317
318 }
319
320 /**
321 * Description callback
322 */
323 public function section_callback() {
324 echo '<p id="stateless-media">' . __( 'Google Storage credentials and settings.', ud_get_stateless_media()->domain ) . '</p>';
325
326 //Imagick is installed
327 if( !extension_loaded('imagick') || !class_exists("Imagick" ) ) {
328 echo '<p id="stateless-media">' . __( 'Be advised, Imagick does not seem to be installed, thumbnails will not be generated not uploaded..', ud_get_stateless_media()->domain ) . '</p>';
329 }
330
331 // Check GD library.
332 if ( !extension_loaded('gd') || !function_exists('gd_info') ) {
333 echo '<p id="stateless-media">' . __( 'Be advised, GD does not seem to be installed, thumbnails will not be generated not uploaded..', ud_get_stateless_media()->domain ) . '</p>';
334 }
335
336 }
337
338 /**
339 * Wrapper for setting value.
340 * @param string $key
341 * @param bool $value
342 * @param bool $bypass_validation
343 * @return \UsabilityDynamics\Settings
344 */
345 public function set( $key = '', $value = false, $bypass_validation = false ) {
346
347 if ( $value !== false ) {
348 update_option( str_replace( '.', '_', $key ), $value );
349 }
350
351 return parent::set( $key, $value, $bypass_validation );
352
353 }
354
355 }
356
357 }
358
359 }