Changeset 2103110
- Timestamp:
- 06/10/2019 05:02:44 AM (7 years ago)
- Location:
- wp-pleasewait/trunk
- Files:
-
- 2 edited
-
pleasewait-settings.php (modified) (12 diffs)
-
pleasewait.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-pleasewait/trunk/pleasewait-settings.php
r2071453 r2103110 2 2 class WpPleaseWait_SettingsPage 3 3 { 4 const CURRENT_VERSION = ' 1.0.4';4 const CURRENT_VERSION = '2.0'; 5 5 const GITHUB_URL = 'https://github.com/lbngoc/wp-please-wait'; // null|string 6 6 const PLUGIN_URL = 'https://wordpress.org/support/plugin/wp-pleasewait'; // null|string … … 14 14 15 15 private $default_options = array( 16 'auto_mode' => true, 17 'test_mode' => false, 16 18 'use_cdn' => false, 17 19 'bg_color' => '#f46d3b', … … 96 98 </div>', 97 99 ), 98 'custom_message_poss' => array( 100 'custom_message' => '<em style="font-size: x-large">Welcome</em>', 101 'custom_message_pos' => 'above', 102 'custom_message_pos_list' => array( 99 103 'above' => 'Above the spinner', 100 104 'below' => 'Below the spinner', … … 104 108 'timeout' => 10, // s 105 109 'delay' => 100, //ms 106 'allow_tags' => '< p><a><strong><em><span><img>',110 'allow_tags' => '<h1><h2><h3><p><a><strong><em><span><img>', 107 111 ); 108 112 … … 115 119 $this->default_options['hook_name'] = $this->get_hook_name(); 116 120 if (is_admin()) { 117 wp_enqueue_style('wp-color-picker'); 118 wp_enqueue_script('wp-color-picker'); 121 add_action('admin_enqueue_scripts', array($this, 'load_admin_assets'), 9 ); 119 122 add_action('admin_menu', array($this, 'add_plugin_page')); 120 123 add_action('admin_init', array($this, 'page_init')); … … 138 141 public function clear_settings() { 139 142 delete_option('wppleasewait_settings'); 143 } 144 145 /** 146 * Assets file for WP Pleasewait Settings 147 */ 148 public function load_admin_assets() { 149 wp_enqueue_style('wp-color-picker'); 150 wp_enqueue_script('wp-color-picker'); 140 151 } 141 152 … … 176 187 <div class="stuffbox"> 177 188 <form method="post" action="options.php"> 178 <?php 179 // This prints out all hidden setting fields 180 settings_fields('wppleasewait'); 181 do_settings_sections('wppleasewait-setting-admin'); 182 ?> 189 <p class="submit"> 190 <?php 191 submit_button("Reset to defaults", 'secondary', null, false, ['name' => 'reset']); 192 submit_button("Save changes", 'primary pull-right', 'submit', false); 193 ?> 194 </p> 195 <hr/> 196 <?php 197 // This prints out all hidden setting fields 198 settings_fields('wppleasewait'); 199 do_settings_sections('wppleasewait-setting-admin'); 200 ?> 183 201 <hr/> 184 202 <p class="submit"> 185 203 <?php 186 submit_button("Reset to defaults", 'secondary', 'reset', false);204 submit_button("Reset to defaults", 'secondary', null, false, ['name' => 'reset']); 187 205 submit_button("Save changes", 'primary pull-right', 'submit', false); 188 206 ?> … … 228 246 <script type="text/javascript">(function($){ 229 247 $(document).ready(function() { 230 $('.wp-color-picker').wpColorPicker() 248 $.fn.wpColorPicker && $('.wp-color-picker').wpColorPicker(); 249 $('.button[name="reset"]').click(function(e) { 250 if (!window.confirm('Are you sure you want to reset all settings?')) { 251 e.preventDefault(); 252 } 253 }); 254 $('input#auto_mode').change(function(e) { 255 if ($(e.target).is(':checked')) { 256 $('tr.hook_name').fadeOut(500); 257 } else { 258 $('tr.hook_name').fadeIn(500); 259 } 260 }); 231 261 }); 232 262 })(jQuery);</script> … … 253 283 254 284 add_settings_field( 285 'auto_mode', 286 'Auto Mode', 287 array($this, 'auto_mode_callback'), 288 'wppleasewait-setting-admin', 289 'setting_section_id' 290 ); 291 292 add_settings_field( 293 'hook_name', 294 'Hook Name', 295 array($this, 'hook_name_callback'), 296 'wppleasewait-setting-admin', 297 'setting_section_id', 298 array('class' => 'hook_name ' . ($this->options['auto_mode'] ? 'hidden' : '')) 299 ); 300 301 add_settings_field( 302 'test_mode', 303 'Testing Mode', 304 array($this, 'test_mode_callback'), 305 'wppleasewait-setting-admin', 306 'setting_section_id' 307 ); 308 309 add_settings_field( 255 310 'use_cdn', 256 311 'Use CDN', 257 312 array($this, 'use_cdn_callback'), 258 'wppleasewait-setting-admin',259 'setting_section_id'260 );261 262 add_settings_field(263 'hook_name',264 'Hook Name',265 array($this, 'hook_name_callback'),266 313 'wppleasewait-setting-admin', 267 314 'setting_section_id' … … 353 400 $new_input = array(); 354 401 402 if (isset($input['auto_mode'])) { // checked 403 $new_input['auto_mode'] = ($input['auto_mode'] === 'yes'); 404 } else { 405 $new_input['auto_mode'] = false; 406 } 407 408 if (isset($input['test_mode'])) { // checked 409 $new_input['test_mode'] = ($input['test_mode'] === 'yes'); 410 } else { 411 $new_input['test_mode'] = false; 412 } 413 355 414 if (isset($input['use_cdn'])) { 356 $new_input['use_cdn'] = $input['use_cdn'] === 'yes'; 415 $new_input['use_cdn'] = ($input['use_cdn'] === 'yes'); 416 } else { 417 $new_input['use_cdn'] = false; 357 418 } 358 419 … … 409 470 410 471 /** 472 * Get the auto_mode option and print its input control 473 */ 474 public function auto_mode_callback() 475 { 476 $current = isset($this->options['auto_mode']) ? $this->options['auto_mode'] : $this->default_options['auto_mode']; 477 printf( 478 '<input type="checkbox" id="auto_mode" name="wppleasewait_settings[auto_mode]" value="yes" %s />', 479 checked($current, true, false) 480 ); 481 print '<label for="auto_mode"><p class="description" style="display:inline;vertical-align:bottom">Auto add plugin scripts after <code><body></code> tag.</p></label>'; 482 } 483 484 /** 485 * Get the test_mode option and print its input control 486 */ 487 public function test_mode_callback() 488 { 489 $current = isset($this->options['test_mode']) ? $this->options['test_mode'] : $this->default_options['test_mode']; 490 printf( 491 '<input type="checkbox" id="test_mode" name="wppleasewait_settings[test_mode]" value="yes" %s />', 492 checked($current, true, false) 493 ); 494 print '<label for="test_mode"><p class="description" style="display:inline;vertical-align:bottom">Force show loading screen for admin user.</p></label>'; 495 } 496 497 /** 411 498 * Get the use_cdn option and print its input control 412 499 */ 413 500 public function use_cdn_callback() 414 501 { 502 $current = isset($this->options['use_cdn']) ? $this->options['use_cdn'] : $this->default_options['use_cdn']; 415 503 printf( 416 504 '<input type="checkbox" id="use_cdn" name="wppleasewait_settings[use_cdn]" value="yes" %s />', 417 checked($ this->options['use_cdn'], true, false)418 ); 419 print '< span class="description">Check if you want to load assets from CDN</span>';505 checked($current, true, false) 506 ); 507 print '<label for="use_cdn"><p class="description" style="display:inline;vertical-align:bottom">Load plugin assets from CDN instead of your server.</p></label>'; 420 508 } 421 509 … … 475 563 $value = isset($this->options['custom_message_pos']) ? $this->options['custom_message_pos'] : $this->default_options['custom_message_pos']; 476 564 print('<select type="text" id="custom_message_pos" name="wppleasewait_settings[custom_message_pos]">'); 477 foreach ($this->default_options['custom_message_pos s'] as $key => $desc) {565 foreach ($this->default_options['custom_message_pos_list'] as $key => $desc) { 478 566 printf('<option value="%s" %s>%s</option>', 479 567 $key, -
wp-pleasewait/trunk/pleasewait.php
r2071453 r2103110 5 5 Plugin URI: https://ngoclb.com/project/wp-please-wait 6 6 Description: Add PleaseWait loading screen to currrent theme 7 Version: 1.0.47 Version: 2.0 8 8 Author: Ngoc LB 9 9 Author URI: https://ngoclb.com/ … … 26 26 public function __construct() { 27 27 $this->options = WpPleaseWait_SettingsPage::getInstance()->get_options(); 28 // var_dump($this->options); die; 28 29 $this->load_actions(); 29 30 } … … 44 45 45 46 function load_actions() { 46 $hook_name = $this->options['hook_name'];47 47 // Hook 48 48 add_action( 'wp_enqueue_scripts', array( $this, 'add_styles_scripts' ) ); 49 add_action( 'wp_head', array( $this, 'add_inline_styles' ) ); 50 add_action( $hook_name, array( $this, 'add_inline_scripts' ) ); 49 if ($this->options['auto_mode']) { 50 add_action( 'wp_head', array( $this, 'add_inline_styles' ), 999 ); 51 add_action( 'wp_footer', array($this, 'add_inline_scripts'), 999 ); 52 } else { 53 $hook_name = $this->options['hook_name']; 54 add_action( $hook_name, array( $this, 'add_inline_styles' ) ); 55 add_action( $hook_name, array( $this, 'add_inline_scripts' ) ); 56 } 51 57 } 52 58 … … 73 79 } 74 80 75 function print_plugin_info() {81 function get_plugin_info() { 76 82 $ver = WpPleaseWait_SettingsPage::CURRENT_VERSION; 77 83 $url = WpPleaseWait_SettingsPage::GITHUB_URL; 78 echo"<!-- Generated by WP PleaseWait v${ver} - ${url} -->\n";84 return "<!-- Generated by WP PleaseWait v${ver} - ${url} -->\n"; 79 85 } 80 86 … … 99 105 $scale_value = $this->options['spinner_scale']; 100 106 $text_color = $this->options['text_color']; 107 // $bg_color = $this->options['bg_color']; 101 108 $css = <<<CSS 102 109 html.pg-loading { … … 113 120 } 114 121 .pg-loading-html .sk-rotating-plane, 115 .pg-loading-html .sk-rect, 116 .pg-loading-html .sk-cube, 117 .pg-loading-html .sk-circle, 118 .pg-loading-html .sk-child { background: {$text_color}} 122 .pg-loading-html .sk-double-bounce .sk-child, 123 .pg-loading-html .sk-wave .sk-rect, 124 .pg-loading-html .sk-wandering-cubes .sk-cube, 125 .pg-loading-html .sk-spinner-pulse, 126 .pg-loading-html .sk-chasing-dots .sk-child, 127 .pg-loading-html .sk-three-bounce .sk-child, 128 .pg-loading-html .sk-circle .sk-child:before, 129 .pg-loading-html .sk-fading-circle .sk-circle:before, 130 .pg-loading-html .sk-folding-cube .sk-cube:before { background: {$text_color}; } 119 131 .loading-message { color: {$text_color} } 120 132 CSS; 121 $this->print_plugin_info();133 // echo $this->get_plugin_info(); 122 134 echo sprintf("<style type='text/css'>%s</style>", $this->combine_to_oneline($css)); 135 // Start get document source code 136 if ($this->options['auto_mode']) { 137 ob_start(); 138 } 123 139 } 124 140 … … 126 142 $template = addslashes_gpc($this->combine_to_oneline($this->get_loading_template())); 127 143 $spinner_style = addslashes_gpc($this->combine_to_oneline($this->options['spinner_styles'][$this->options['spinner_style']])); 144 $delayMs = $this->options['delay']; 145 $timeoutMs = $this->options['timeout']; 146 $isTestMode = $this->options['test_mode'] && current_user_can('administrator') ? 'true' : 'false'; 128 147 $js = <<<JS 129 var rootelem = document.querySelector('html'), 130 loading_screen = pleaseWait({ 131 logo: false, 132 template: "{$template}", 133 backgroundColor: '{$this->options['bg_color']}', 134 loadingHtml: "{$spinner_style}", 135 onLoadedCallback: function() { setTimeout(function(){rootelem.className = rootelem.className.replace('pg-loading', 'pg-loaded').trim();} , 200); } 136 }); 137 rootelem.className += ' pg-loading'; 138 function hide_loading_screen() {loading_screen.finish();} 139 document.addEventListener("DOMContentLoaded", function() { setTimeout(hide_loading_screen, {$this->options['delay']}) }); 148 var rootelem = document.querySelector('html'), 149 isTestMode = ${isTestMode}; 150 if (window.pleaseWait) { 151 loadingScreen = pleaseWait({ 152 logo: false, 153 template: "{$template}", 154 backgroundColor: "{$this->options['bg_color']}", 155 loadingHtml: "{$spinner_style}", 156 onLoadedCallback: function() { setTimeout(function(){rootelem.className = rootelem.className.replace('pg-loading', 'pg-loaded').trim();} , 200); } 157 }); 158 rootelem.className += ' pg-loading'; 159 function hideLoadingScreen() { !isTestMode && loadingScreen.finish(); } 160 document.addEventListener("DOMContentLoaded", function() { setTimeout(hideLoadingScreen, {$delayMs}) }); 161 !!(${timeoutMs}) && setTimeout(hideLoadingScreen, {$timeoutMs}*1000); 162 } 140 163 JS; 141 if ($this->options['timeout']) { 142 $js .= "setTimeout(hide_loading_screen, {$this->options['timeout']}*1000);"; 164 $plugin_info = $this->get_plugin_info(); 165 $js_code = sprintf("<script type='text/javascript'>%s</script>", $this->combine_to_oneline($js)); 166 if ($this->options['auto_mode']) { 167 $content = ob_get_clean(); 168 $content = preg_replace('/<body([^>]*)>/i', '<body$1>' . "\n{$plugin_info}{$js_code}", $content); 169 echo $content; 170 } else { 171 echo $plugin_info; 172 echo $js_code; 143 173 } 144 $this->print_plugin_info();145 echo sprintf("<script type='text/javascript'>%s</script>", $this->combine_to_oneline($js));146 174 } 147 175 }
Note: See TracChangeset
for help on using the changeset viewer.