Plugin Directory

Changeset 2508518


Ignore:
Timestamp:
04/03/2021 08:28:20 AM (5 years ago)
Author:
jojoee
Message:

Release 1.2.6

Location:
grayscale-body/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • grayscale-body/trunk/css/main.css

    r1515781 r2508518  
    2424  box-sizing: border-box;
    2525  z-index: 999;
     26  overflow: hidden;
     27  text-overflow: ellipsis;
    2628}
    2729
  • grayscale-body/trunk/grayscale-body.php

    r2217464 r2508518  
    44Plugin URI: https://wordpress.org/plugins/grayscale-body/
    55Description: Automatically turn the site to grayscale
    6 Version: 1.2.4
     6Version: 1.2.6
    77Author: Nathachai Thongniran
    88Author URI: http://jojoee.com/
     
    1717
    1818  public function __construct() {
    19     $this->is_debug = false;
    20     $this->menu_page = 'grayscale-body';
     19    $this->is_debug          = FALSE;
     20    $this->menu_page         = 'grayscale-body';
    2121    $this->option_group_name = 'gsb_option_group';
    2222    $this->option_field_name = 'gsb_option_field';
    23     $this->options = get_option( $this->option_field_name );
     23    $this->options           = get_option( $this->option_field_name );
    2424
    2525    // set default prop
     
    3333
    3434    // add plugin link
    35     add_filter( 'plugin_action_links', array( $this, 'gsb_plugin_action_links' ), 10, 4 );
     35    add_filter( 'plugin_action_links', array(
     36      $this,
     37      'gsb_plugin_action_links',
     38    ), 10, 4 );
    3639
    3740    // hook
     
    4043  }
    4144
     45  /*================================================================ Util
     46   */
     47
     48  /**
     49   * Check is null or empty string
     50   *
     51   * @see http://stackoverflow.com/questions/381265/better-way-to-check-variable-for-null-or-empty-string
     52   *
     53   * @param string $str
     54   *
     55   * @return boolean
     56   */
     57  private function is_null_or_empty_string( $str ) {
     58    return ( ! isset( $str ) || trim( $str ) === '' );
     59  }
     60
    4261  /*================================================================ Debug
    4362   */
    4463
    45   private function dd( $var = null, $is_die = true ) {
     64  private function dd( $var = NULL, $is_die = TRUE ) {
    4665    echo '<pre>';
    4766    print_r( $var );
    4867    echo '</pre>';
    4968
    50     if ( $is_die ) die();
    51   }
    52 
    53   private function da( $var = null ) {
    54     $this->dd( $var, false );
    55   }
    56 
    57   private function dhead( $head, $var, $is_die = false ) {
     69    if ( $is_die ) {
     70      die();
     71    }
     72  }
     73
     74  private function da( $var = NULL ) {
     75    $this->dd( $var, FALSE );
     76  }
     77
     78  private function dhead( $head, $var, $is_die = FALSE ) {
    5879    echo '<div class="debug-box">';
    5980    echo '================';
     
    6586  }
    6687
    67   private function dump( $is_die = false ) {
     88  private function dump( $is_die = FALSE ) {
    6889    $this->da( $this->options, $is_die );
    6990  }
     
    7596  /*================================================================ Public
    7697   */
    77  
    78   public function gsb_head() { ?>
     98  public function gsb_head() {
     99    $is_enabled = $this->options['gsb_field_is_enabled'];
     100    $custom_css = $this->options['gsb_field_custom_css'];
     101
     102    // custom css
     103    if ( $is_enabled && ! $this->is_null_or_empty_string( $custom_css ) ) {
     104      printf( '<style>%s</style>', $custom_css );
     105    }
     106    ?>
     107
    79108    <script>
    80       var gsbOption = '<?php echo json_encode( $this->options ); ?>';
     109      var gsbOption = '<?php echo json_encode( $this->options ); ?>'
    81110    </script>
    82111    <?php
     
    84113
    85114  public function gsb_enqueue_scripts() {
    86     $is_enabled = $this->options['gsb_field_is_enabled'];
     115    global $post;
     116
     117    $is_enabled         = $this->options['gsb_field_is_enabled'];
    87118    $is_enable_switcher = $this->options['gsb_field_is_enable_switcher'];
    88 
    89     if ( $is_enabled ) {
     119    $ignored_post_ids   = $this->options['gsb_field_ignored_post_ids'];
     120    $ignored_post_ids   = explode( ",", $ignored_post_ids );
     121    $post_id            = $post->ID;
     122    $is_ignored_post    = in_array( $post_id, $ignored_post_ids );
     123
     124    if ( $is_enabled && ! $is_ignored_post ) {
    90125      if ( $is_enable_switcher ) {
    91126        wp_enqueue_style( 'gsb-main-style', plugins_url( 'css/main.css', __FILE__ ) );
    92         wp_enqueue_script( 'gsb-main-script', plugins_url('js/main.js', __FILE__), array(), '120', true);
     127        wp_enqueue_script( 'gsb-main-script', plugins_url( 'js/main.js', __FILE__ ), array(), '120', TRUE );
    93128
    94129      } else {
     
    100135  /*================================================================ Callback
    101136   */
    102  
     137
    103138  public function gsb_field_is_enabled_callback() {
    104     $field_id = 'gsb_field_is_enabled';
    105     $field_name = $this->option_field_name . "[$field_id]";
     139    $field_id    = 'gsb_field_is_enabled';
     140    $field_name  = $this->option_field_name . "[$field_id]";
    106141    $field_value = 1;
    107     $check_attr = checked( 1, $this->options[ $field_id ], false );
     142    $check_attr  = checked( 1, $this->options[ $field_id ], FALSE );
    108143
    109144    printf(
     
    117152
    118153  public function gsb_field_is_enable_switcher_callback() {
    119     $field_id = 'gsb_field_is_enable_switcher';
    120     $field_name = $this->option_field_name . "[$field_id]";
     154    $field_id    = 'gsb_field_is_enable_switcher';
     155    $field_name  = $this->option_field_name . "[$field_id]";
    121156    $field_value = 1;
    122     $check_attr = checked( 1, $this->options[ $field_id ], false );
     157    $check_attr  = checked( 1, $this->options[ $field_id ], FALSE );
    123158
    124159    printf(
     
    131166  }
    132167
    133   public function gsb_field_switcher_position_callback() {
    134     $field_id = 'gsb_field_switcher_position';
     168  public function gsb_field_default_mode_callback() {
     169    $field_id   = 'gsb_field_default_mode';
    135170    $field_name = $this->option_field_name . "[$field_id]";
    136     $positions = array(
    137       array(
    138         'value'     => 'top-left',
    139         'name'      => 'Top left'
    140       ),
    141       array(
    142         'value'     => 'top-right',
    143         'name'      => 'Top right'
    144       ),
    145       array(
    146         'value'     => 'bottom-left',
    147         'name'      => 'Bottom left'
    148       ),
    149       array(
    150         'value'     => 'bottom-right',
    151         'name'      => 'Bottom right'
    152       )
     171    $positions  = array(
     172      array(
     173        'value' => 'color',
     174        'name'  => 'Color',
     175      ),
     176      array(
     177        'value' => 'grayscale',
     178        'name'  => 'Grayscale',
     179      ),
    153180    );
    154181
    155182    printf( '<select id="%s" name="%s">', $field_id, $field_name );
    156183    foreach ( $positions as $position ) {
    157       $value = $position['value'];
    158       $name = $position['name'];
    159       $select_attr = selected( $this->options[ $field_id ], $value, false );
     184      $value       = $position['value'];
     185      $name        = $position['name'];
     186      $select_attr = selected( $this->options[ $field_id ], $value, FALSE );
    160187
    161188      printf( '<option value="%s" %s>%s</option>',
     
    166193    }
    167194    echo '</select>';
     195  }
     196
     197  public function gsb_field_switcher_position_callback() {
     198    $field_id   = 'gsb_field_switcher_position';
     199    $field_name = $this->option_field_name . "[$field_id]";
     200    $positions  = array(
     201      array(
     202        'value' => 'top-left',
     203        'name'  => 'Top left',
     204      ),
     205      array(
     206        'value' => 'top-right',
     207        'name'  => 'Top right',
     208      ),
     209      array(
     210        'value' => 'bottom-left',
     211        'name'  => 'Bottom left',
     212      ),
     213      array(
     214        'value' => 'bottom-right',
     215        'name'  => 'Bottom right',
     216      ),
     217    );
     218
     219    printf( '<select id="%s" name="%s">', $field_id, $field_name );
     220    foreach ( $positions as $position ) {
     221      $value       = $position['value'];
     222      $name        = $position['name'];
     223      $select_attr = selected( $this->options[ $field_id ], $value, FALSE );
     224
     225      printf( '<option value="%s" %s>%s</option>',
     226        $value,
     227        $select_attr,
     228        $name
     229      );
     230    }
     231    echo '</select>';
     232  }
     233
     234  public function gsb_field_ignored_post_ids_callback() {
     235    $field_id    = 'gsb_field_ignored_post_ids';
     236    $field_name  = $this->option_field_name . "[$field_id]";
     237    $field_value = $this->options[ $field_id ];
     238
     239    printf(
     240      '<input type="text" id="%s" name="%s" value="%s" placeholder="%s">',
     241      $field_id,
     242      $field_name,
     243      $field_value,
     244      "ignored post ids for examples: 1,3,4"
     245    );
     246  }
     247
     248  public function gsb_field_custom_css_callback() {
     249    $field_id    = 'gsb_field_custom_css';
     250    $field_name  = $this->option_field_name . "[$field_id]";
     251    $field_value = $this->options[ $field_id ];
     252
     253    printf(
     254      '<textarea id="%s" name="%s" type="textarea">%s</textarea>',
     255      $field_id,
     256      $field_name,
     257      $field_value
     258    );
    168259  }
    169260
     
    177268    //   'gsb_field_is_enabled'             => 1
    178269    //   'gsb_field_is_enable_switcher'     => 0
     270    //   'gsb_field_default_mode'           => 'grayscale'
    179271    //   'gsb_field_switcher_position'      => 'top-right'
     272    //   'gsb_field_ignored_post_ids'       => ''
     273    //   'gsb_field_custom_css'             => ''
    180274    // ]
    181275
    182276    $options = $this->options;
    183277
    184     if ( ! isset( $options['gsb_field_is_enabled'] ) )              $options['gsb_field_is_enabled'] = 1;
    185     if ( ! isset( $options['gsb_field_is_enable_switcher'] ) )      $options['gsb_field_is_enable_switcher'] = 0;
    186 
     278    if ( ! isset( $options['gsb_field_is_enabled'] ) ) {
     279      $options['gsb_field_is_enabled'] = 1;
     280    }
     281    if ( ! isset( $options['gsb_field_is_enable_switcher'] ) ) {
     282      $options['gsb_field_is_enable_switcher'] = 0;
     283    }
     284    if ( ! isset( $options['gsb_field_default_mode'] ) || ( $options['gsb_field_default_mode'] === '' ) ) {
     285      $options['gsb_field_default_mode'] = 'grayscale';
     286    }
    187287    if ( ! isset( $options['gsb_field_switcher_position'] ) || ( $options['gsb_field_switcher_position'] === '' ) ) {
    188288      $options['gsb_field_switcher_position'] = 'top-right';
     289    }
     290    if ( ! isset( $options['gsb_field_ignored_post_ids'] ) ) {
     291      $options['gsb_field_ignored_post_ids'] = '';
     292    }
     293    if ( ! isset( $options['gsb_field_custom_css'] ) ) {
     294      $options['gsb_field_custom_css'] = '';
    189295    }
    190296
     
    210316  /**
    211317   * Options page callback
    212    * 
     318   *
    213319   * TODO: relocate style
    214320   */
    215321  public function gsb_admin_page() { ?>
    216     <?php if ( $this->is_debug ) $this->dump(); ?>
     322    <?php if ( $this->is_debug ) {
     323      $this->dump();
     324    } ?>
    217325    <div class="wrap">
    218326      <h1>Grayscale Body</h1>
    219327      <form method="post" action="options.php">
    220328        <?php
    221           settings_fields( $this->option_group_name );
    222           do_settings_sections( $this->menu_page );
    223           submit_button();
     329        settings_fields( $this->option_group_name );
     330        do_settings_sections( $this->menu_page );
     331        submit_button();
    224332        ?>
    225333      </form>
    226334    </div>
    227335    <style>
    228     .debug-box {
    229       padding: 12px 0;
    230     }
    231     .form-table th,
    232     .form-table td {
    233       padding: 0;
    234       line-height: 30px;
    235       height: 30px;
    236     }
     336      .debug-box {
     337        padding: 12px 0;
     338      }
     339
     340      .form-table th,
     341      .form-table td {
     342        padding: 0;
     343        padding-bottom: 6px;
     344        line-height: 30px;
     345        height: 30px;
     346      }
     347
     348      #gsb_field_ignored_post_ids {
     349
     350      }
     351
     352      #gsb_field_custom_css {
     353
     354      }
     355
     356      @media (min-width: 768px) {
     357        #gsb_field_ignored_post_ids {
     358          min-width: 600px;
     359        }
     360
     361        #gsb_field_custom_css {
     362          min-width: 600px;
     363          min-height: 300px;
     364        }
     365      }
    237366    </style>
    238367    <?php
     
    259388    // - is_enabled
    260389    // - is_enable_switcher
     390    // - default_mode
    261391    // - switcher_position
     392    // - ignored_post_ids
     393    // - custom_css
    262394    add_settings_field(
    263395      'gsb_field_is_enabled',
    264       'Enable',
     396      'Enable the plugin',
    265397      array( $this, 'gsb_field_is_enabled_callback' ),
    266398      $this->menu_page,
     
    270402    add_settings_field(
    271403      'gsb_field_is_enable_switcher',
    272       'Enable switcher',
     404      'Enable the switcher',
    273405      array( $this, 'gsb_field_is_enable_switcher_callback' ),
    274406      $this->menu_page,
     
    277409
    278410    add_settings_field(
     411      'gsb_field_default_mode',
     412      'Default mode',
     413      array( $this, 'gsb_field_default_mode_callback' ),
     414      $this->menu_page,
     415      $section_id
     416    );
     417
     418    add_settings_field(
    279419      'gsb_field_switcher_position',
    280       'Switcher: position',
     420      'Switcher position',
    281421      array( $this, 'gsb_field_switcher_position_callback' ),
    282422      $this->menu_page,
    283423      $section_id
    284424    );
     425
     426    add_settings_field(
     427      'gsb_field_ignored_post_ids',
     428      'Ignored Post IDs',
     429      array( $this, 'gsb_field_ignored_post_ids_callback' ),
     430      $this->menu_page,
     431      $section_id
     432    );
     433
     434    add_settings_field(
     435      'gsb_field_custom_css',
     436      'Custom CSS',
     437      array( $this, 'gsb_field_custom_css_callback' ),
     438      $this->menu_page,
     439      $section_id
     440    );
     441
    285442  }
    286443
     
    299456    // text
    300457    $text_input_ids = array(
    301       'gsb_field_switcher_position'
     458      'gsb_field_default_mode',
     459      'gsb_field_switcher_position',
     460      'gsb_field_ignored_post_ids',
     461      'gsb_field_custom_css',
     462
    302463    );
    303464    foreach ( $text_input_ids as $text_input_id ) {
     
    310471    $number_input_ids = array(
    311472      'gsb_field_is_enabled',
    312       'gsb_field_is_enable_switcher'
     473      'gsb_field_is_enable_switcher',
    313474    );
    314475    foreach ( $number_input_ids as $number_input_id ) {
     
    330491    return array_merge( $links, $plugin_link );
    331492  }
     493
    332494}
    333495
  • grayscale-body/trunk/js/main.js

    r1515781 r2508518  
    44 */
    55
    6 var gsbDebug = false;
     6var gsbDebug = false
    77
    8 var gsbIsIE = function() {
    9   var myNav = navigator.userAgent.toLowerCase();
     8var gsbIsIE = function () {
     9  var myNav = navigator.userAgent.toLowerCase()
    1010
    11   return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
    12 };
     11  return (myNav.indexOf('msie') != -1)
     12    ? parseInt(myNav.split('msie')[1])
     13    : false
     14}
    1315
    14 var GrayscaleBody = function() {
    15   this.bodyEle;
    16   this.bodyClassNamePrefix = 'gsb-';
    17   this.switcherEle;
    18   this.grayscaleStateName = 'grayscale';
    19   this.colorStateName = 'color';
    20   this.localStorageStateKey = 'gsbState';
    21   this.prevState;
    22   this.currentState;
    23   this.option;
     16var GrayscaleBody = function () {
     17  this.bodyEle
     18  this.bodyClassNamePrefix = 'gsb-'
     19  this.switcherEle
     20  this.grayscaleStateName = 'grayscale'
     21  this.colorStateName = 'color'
     22  this.localStorageStateKey = 'gsbState'
     23  this.prevState
     24  this.currentState
     25  this.option
    2426
    2527  /*================================================================ Local storage
    2628   */
    2729
    28   this.getCurrentStateFromLocalStorage = function() {
    29     var state = localStorage.getItem(this.localStorageStateKey);
     30  this.getCurrentStateFromLocalStorage = function () {
     31    var initState = this.option.gsb_field_default_mode
     32    var state = localStorage.getItem(this.localStorageStateKey)
    3033
    31     if (!state) state = this.grayscaleStateName;
     34    if (!state) {
     35      state = initState || this.grayscaleStateName
     36    }
    3237
    33     return state;
    34   };
     38    return state
     39  }
    3540
    36   this.setCurrentStateToLocalStorage = function() {
    37     return localStorage.setItem(this.localStorageStateKey, this.currentState);
    38   };
     41  this.setCurrentStateToLocalStorage = function () {
     42    return localStorage.setItem(this.localStorageStateKey, this.currentState)
     43  }
    3944
    4045  /*================================================================ Others
    4146   */
    4247
    43   this.updateBodyCurrentState = function() {
    44     this.bodyEle.classList.remove(this.bodyClassNamePrefix + this.prevState);
    45     this.bodyEle.classList.add(this.bodyClassNamePrefix + this.currentState);
    46   };
     48  this.updateBodyCurrentState = function () {
     49    this.bodyEle.classList.remove(this.bodyClassNamePrefix + this.prevState)
     50    this.bodyEle.classList.add(this.bodyClassNamePrefix + this.currentState)
     51  }
    4752
    48   this.initSwitcher = function() {
     53  this.initSwitcher = function () {
    4954    var swticherEle = document.createElement('div'),
    5055      swticherName = 'gsb-switcher',
    51       self = this;
     56      self = this
    5257
    5358    // setup switcher
    54     swticherEle.id = swticherName;
    55     swticherEle.className = swticherName;
    56     swticherEle.classList.add(this.currentState);
    57     swticherEle.classList.add(this.option.gsb_field_switcher_position);
    58     swticherEle.onclick = function(event) {
     59    swticherEle.id = swticherName
     60    swticherEle.className = swticherName
     61    swticherEle.classList.add(this.currentState)
     62    swticherEle.classList.add(this.option.gsb_field_switcher_position)
     63    swticherEle.onclick = function (event) {
    5964      var newCurrentState = (self.currentState === self.grayscaleStateName) ?
    6065        self.colorStateName :
    61         self.grayscaleStateName;
     66        self.grayscaleStateName
    6267
    63       event.preventDefault();
     68      event.preventDefault()
    6469
    6570      // add update swticher
    66       this.classList.remove(self.currentState);
    67       this.classList.add(newCurrentState);
     71      this.classList.remove(self.currentState)
     72      this.classList.add(newCurrentState)
    6873
    6974      // update state (global)
    70       self.prevState = self.currentState;
    71       self.currentState = newCurrentState;
     75      self.prevState = self.currentState
     76      self.currentState = newCurrentState
    7277
    7378      // update body
    74       self.updateBodyCurrentState();
     79      self.updateBodyCurrentState()
    7580
    7681      // set localStorage
    77       self.setCurrentStateToLocalStorage();
     82      self.setCurrentStateToLocalStorage()
    7883
    7984      if (gsbDebug) {
    80         console.log('================ gsb - switcher is clicked');
    81         console.log('self.prevState', self.prevState);
    82         console.log('self.currentState', self.currentState);
     85        console.log('================ gsb - switcher is clicked')
     86        console.log('self.prevState', self.prevState)
     87        console.log('self.currentState', self.currentState)
    8388      }
    84     };
     89    }
    8590
    8691    // add to body
    87     this.bodyEle.appendChild(swticherEle);
     92    this.bodyEle.appendChild(swticherEle)
    8893
    8994    // init gsbSwitcherEle
    90     this.switcherEle = document.getElementById(swticherName);
    91   };
     95    this.switcherEle = document.getElementById(swticherName)
     96  }
    9297
    93   this.init = function() {
    94     this.bodyEle = document.getElementsByTagName('body')[0];
    95     this.prevState = this.getCurrentStateFromLocalStorage();
    96     this.currentState = this.getCurrentStateFromLocalStorage();
    97     this.option = JSON.parse(gsbOption);
     98  this.init = function () {
     99    this.option = JSON.parse(gsbOption)
     100    this.bodyEle = document.getElementsByTagName('body')[0]
     101    this.prevState = this.getCurrentStateFromLocalStorage()
     102    this.currentState = this.getCurrentStateFromLocalStorage()
    98103
    99     this.updateBodyCurrentState();
    100     this.initSwitcher();
     104    this.updateBodyCurrentState()
     105    this.initSwitcher()
    101106
    102107    if (gsbDebug) {
    103       console.log('================ gsb - init');
    104       console.log('this.prevState: ', this.prevState);
    105       console.log('this.currentState: ', this.currentState);
     108      console.log('================ gsb - init')
     109      console.log('this.prevState: ', this.prevState)
     110      console.log('this.currentState: ', this.currentState)
    106111    }
    107   };
     112  }
    108113}
    109114
    110 function iniGrayscaleBody() {
    111   var gsb = new GrayscaleBody();
     115function iniGrayscaleBody () {
     116  var gsb = new GrayscaleBody()
    112117
    113   gsb.init();
     118  gsb.init()
    114119}
    115120
    116 function iniGrayscaleBodyUnsupportedBrowser() {
     121function iniGrayscaleBodyUnsupportedBrowser () {
    117122  // hard code for unsupported browser
    118123  // `document.addEventListener`
    119   var bodyEle = document.getElementsByTagName('body')[0];
    120   bodyEle.className += (' ' + 'gsb-grayscale');
     124  var bodyEle = document.getElementsByTagName('body')[0]
     125  bodyEle.className += (' ' + 'gsb-grayscale')
    121126}
    122127
    123128if (gsbIsIE()) {
    124   iniGrayscaleBodyUnsupportedBrowser();
     129  iniGrayscaleBodyUnsupportedBrowser()
    125130
    126 } else {
     131}
     132else {
    127133  try {
    128     document.addEventListener('DOMContentLoaded', iniGrayscaleBody);
     134    document.addEventListener('DOMContentLoaded', iniGrayscaleBody)
    129135
    130   } catch (err) {
    131     if (gsbDebug) console.log(err.message);
     136  }
     137  catch (err) {
     138    if (gsbDebug) {
     139      console.log(err.message)
     140    }
    132141
    133     iniGrayscaleBodyUnsupportedBrowser();
     142    iniGrayscaleBodyUnsupportedBrowser()
    134143  }
    135144}
  • grayscale-body/trunk/readme.txt

    r2352052 r2508518  
    55Tags: grayscale, black, white, black and white, site, convert, conversion, CSS, CSS3, filter
    66Requires at least: 3.0.1
    7 Tested up to: 5.4.2
     7Tested up to: 5.7
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1717* Grayscale / Color switcher (for non-IE)
    1818* Theme option to enable / disable switcher and select switcher position
     19* Default mode, ability to set default which either "grayscale" or "color"
     20* Ignored Post IDs, user can display this plugin for specific posts by post IDs
     21* Custom css
    1922
    2023Compatible with all browsers:
     
    5053
    5154== Upgrade Notice ==
     55
     56= 1.2.6 =
     57* Fix bug, frontend is not working
     58
     59= 1.2.5 =
     60* Retest the plugin against WordPress 5.7
     61* Default mode
     62* Ignored Post IDs
     63* Custom css
    5264
    5365= 1.2.4 =
Note: See TracChangeset for help on using the changeset viewer.