Plugin Directory

Changeset 2103110


Ignore:
Timestamp:
06/10/2019 05:02:44 AM (7 years ago)
Author:
ngoclb
Message:

Up to 2.0

Location:
wp-pleasewait/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-pleasewait/trunk/pleasewait-settings.php

    r2071453 r2103110  
    22class WpPleaseWait_SettingsPage
    33{
    4     const CURRENT_VERSION = '1.0.4';
     4    const CURRENT_VERSION = '2.0';
    55    const GITHUB_URL = 'https://github.com/lbngoc/wp-please-wait'; // null|string
    66    const PLUGIN_URL = 'https://wordpress.org/support/plugin/wp-pleasewait'; // null|string
     
    1414
    1515    private $default_options = array(
     16        'auto_mode'           => true,
     17        'test_mode'           => false,
    1618        'use_cdn'             => false,
    1719        'bg_color'            => '#f46d3b',
     
    9698        </div>',
    9799        ),
    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(
    99103            'above' => 'Above the spinner',
    100104            'below' => 'Below the spinner',
     
    104108        'timeout'             => 10, // s
    105109        'delay'               => 100, //ms
    106         'allow_tags'          => '<p><a><strong><em><span><img>',
     110        'allow_tags'          => '<h1><h2><h3><p><a><strong><em><span><img>',
    107111    );
    108112
     
    115119        $this->default_options['hook_name'] = $this->get_hook_name();
    116120        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 );
    119122            add_action('admin_menu', array($this, 'add_plugin_page'));
    120123            add_action('admin_init', array($this, 'page_init'));
     
    138141    public function clear_settings() {
    139142        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');
    140151    }
    141152
     
    176187                  <div class="stuffbox">
    177188                    <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                        ?>
    183201                      <hr/>
    184202                      <p class="submit">
    185203                        <?php
    186                             submit_button("Reset to defaults", 'secondary', 'reset', false);
     204                            submit_button("Reset to defaults", 'secondary', null, false, ['name' => 'reset']);
    187205                            submit_button("Save changes", 'primary pull-right', 'submit', false);
    188206                        ?>
     
    228246        <script type="text/javascript">(function($){
    229247          $(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            });
    231261          });
    232262        })(jQuery);</script>
     
    253283
    254284        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(
    255310            'use_cdn',
    256311            'Use CDN',
    257312            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'),
    266313            'wppleasewait-setting-admin',
    267314            'setting_section_id'
     
    353400        $new_input = array();
    354401
     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
    355414        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;
    357418        }
    358419
     
    409470
    410471    /**
     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>&lt;body&gt;</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    /**
    411498     * Get the use_cdn option and print its input control
    412499     */
    413500    public function use_cdn_callback()
    414501    {
     502        $current = isset($this->options['use_cdn']) ? $this->options['use_cdn'] : $this->default_options['use_cdn'];
    415503        printf(
    416504            '<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>';
    420508    }
    421509
     
    475563        $value = isset($this->options['custom_message_pos']) ? $this->options['custom_message_pos'] : $this->default_options['custom_message_pos'];
    476564        print('<select type="text" id="custom_message_pos" name="wppleasewait_settings[custom_message_pos]">');
    477         foreach ($this->default_options['custom_message_poss'] as $key => $desc) {
     565        foreach ($this->default_options['custom_message_pos_list'] as $key => $desc) {
    478566            printf('<option value="%s" %s>%s</option>',
    479567                $key,
  • wp-pleasewait/trunk/pleasewait.php

    r2071453 r2103110  
    55Plugin URI:   https://ngoclb.com/project/wp-please-wait
    66Description:  Add PleaseWait loading screen to currrent theme
    7 Version:      1.0.4
     7Version:      2.0
    88Author:       Ngoc LB
    99Author URI:   https://ngoclb.com/
     
    2626  public function __construct() {
    2727    $this->options = WpPleaseWait_SettingsPage::getInstance()->get_options();
     28    // var_dump($this->options); die;
    2829    $this->load_actions();
    2930  }
     
    4445
    4546  function load_actions() {
    46     $hook_name = $this->options['hook_name'];
    4747    // Hook
    4848    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    }
    5157  }
    5258
     
    7379  }
    7480
    75   function print_plugin_info() {
     81  function get_plugin_info() {
    7682    $ver = WpPleaseWait_SettingsPage::CURRENT_VERSION;
    7783    $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";
    7985  }
    8086
     
    99105    $scale_value = $this->options['spinner_scale'];
    100106    $text_color = $this->options['text_color'];
     107    // $bg_color = $this->options['bg_color'];
    101108    $css = <<<CSS
    102109    html.pg-loading {
     
    113120    }
    114121    .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}; }
    119131    .loading-message { color: {$text_color} }
    120132CSS;
    121     $this->print_plugin_info();
     133    // echo $this->get_plugin_info();
    122134    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    }
    123139  }
    124140
     
    126142    $template = addslashes_gpc($this->combine_to_oneline($this->get_loading_template()));
    127143    $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';
    128147    $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  }
    140163JS;
    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;
    143173    }
    144     $this->print_plugin_info();
    145     echo sprintf("<script type='text/javascript'>%s</script>", $this->combine_to_oneline($js));
    146174  }
    147175}
Note: See TracChangeset for help on using the changeset viewer.