Changeset 2508518
- Timestamp:
- 04/03/2021 08:28:20 AM (5 years ago)
- Location:
- grayscale-body/trunk
- Files:
-
- 5 edited
-
css/main.css (modified) (1 diff)
-
grayscale-body.php (modified) (19 diffs)
-
js/main.js (modified) (1 diff)
-
readme.txt (modified) (3 diffs)
-
screenshot-3.jpg (modified) (previous)
Legend:
- Unmodified
- Added
- Removed
-
grayscale-body/trunk/css/main.css
r1515781 r2508518 24 24 box-sizing: border-box; 25 25 z-index: 999; 26 overflow: hidden; 27 text-overflow: ellipsis; 26 28 } 27 29 -
grayscale-body/trunk/grayscale-body.php
r2217464 r2508518 4 4 Plugin URI: https://wordpress.org/plugins/grayscale-body/ 5 5 Description: Automatically turn the site to grayscale 6 Version: 1.2. 46 Version: 1.2.6 7 7 Author: Nathachai Thongniran 8 8 Author URI: http://jojoee.com/ … … 17 17 18 18 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'; 21 21 $this->option_group_name = 'gsb_option_group'; 22 22 $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 ); 24 24 25 25 // set default prop … … 33 33 34 34 // 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 ); 36 39 37 40 // hook … … 40 43 } 41 44 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 42 61 /*================================================================ Debug 43 62 */ 44 63 45 private function dd( $var = null, $is_die = true) {64 private function dd( $var = NULL, $is_die = TRUE ) { 46 65 echo '<pre>'; 47 66 print_r( $var ); 48 67 echo '</pre>'; 49 68 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 ) { 58 79 echo '<div class="debug-box">'; 59 80 echo '================'; … … 65 86 } 66 87 67 private function dump( $is_die = false) {88 private function dump( $is_die = FALSE ) { 68 89 $this->da( $this->options, $is_die ); 69 90 } … … 75 96 /*================================================================ Public 76 97 */ 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 79 108 <script> 80 var gsbOption = '<?php echo json_encode( $this->options ); ?>' ;109 var gsbOption = '<?php echo json_encode( $this->options ); ?>' 81 110 </script> 82 111 <?php … … 84 113 85 114 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']; 87 118 $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 ) { 90 125 if ( $is_enable_switcher ) { 91 126 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 ); 93 128 94 129 } else { … … 100 135 /*================================================================ Callback 101 136 */ 102 137 103 138 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]"; 106 141 $field_value = 1; 107 $check_attr = checked( 1, $this->options[ $field_id ], false);142 $check_attr = checked( 1, $this->options[ $field_id ], FALSE ); 108 143 109 144 printf( … … 117 152 118 153 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]"; 121 156 $field_value = 1; 122 $check_attr = checked( 1, $this->options[ $field_id ], false);157 $check_attr = checked( 1, $this->options[ $field_id ], FALSE ); 123 158 124 159 printf( … … 131 166 } 132 167 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'; 135 170 $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 ), 153 180 ); 154 181 155 182 printf( '<select id="%s" name="%s">', $field_id, $field_name ); 156 183 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 ); 160 187 161 188 printf( '<option value="%s" %s>%s</option>', … … 166 193 } 167 194 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 ); 168 259 } 169 260 … … 177 268 // 'gsb_field_is_enabled' => 1 178 269 // 'gsb_field_is_enable_switcher' => 0 270 // 'gsb_field_default_mode' => 'grayscale' 179 271 // 'gsb_field_switcher_position' => 'top-right' 272 // 'gsb_field_ignored_post_ids' => '' 273 // 'gsb_field_custom_css' => '' 180 274 // ] 181 275 182 276 $options = $this->options; 183 277 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 } 187 287 if ( ! isset( $options['gsb_field_switcher_position'] ) || ( $options['gsb_field_switcher_position'] === '' ) ) { 188 288 $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'] = ''; 189 295 } 190 296 … … 210 316 /** 211 317 * Options page callback 212 * 318 * 213 319 * TODO: relocate style 214 320 */ 215 321 public function gsb_admin_page() { ?> 216 <?php if ( $this->is_debug ) $this->dump(); ?> 322 <?php if ( $this->is_debug ) { 323 $this->dump(); 324 } ?> 217 325 <div class="wrap"> 218 326 <h1>Grayscale Body</h1> 219 327 <form method="post" action="options.php"> 220 328 <?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(); 224 332 ?> 225 333 </form> 226 334 </div> 227 335 <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 } 237 366 </style> 238 367 <?php … … 259 388 // - is_enabled 260 389 // - is_enable_switcher 390 // - default_mode 261 391 // - switcher_position 392 // - ignored_post_ids 393 // - custom_css 262 394 add_settings_field( 263 395 'gsb_field_is_enabled', 264 'Enable ',396 'Enable the plugin', 265 397 array( $this, 'gsb_field_is_enabled_callback' ), 266 398 $this->menu_page, … … 270 402 add_settings_field( 271 403 'gsb_field_is_enable_switcher', 272 'Enable switcher',404 'Enable the switcher', 273 405 array( $this, 'gsb_field_is_enable_switcher_callback' ), 274 406 $this->menu_page, … … 277 409 278 410 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( 279 419 'gsb_field_switcher_position', 280 'Switcher :position',420 'Switcher position', 281 421 array( $this, 'gsb_field_switcher_position_callback' ), 282 422 $this->menu_page, 283 423 $section_id 284 424 ); 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 285 442 } 286 443 … … 299 456 // text 300 457 $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 302 463 ); 303 464 foreach ( $text_input_ids as $text_input_id ) { … … 310 471 $number_input_ids = array( 311 472 'gsb_field_is_enabled', 312 'gsb_field_is_enable_switcher' 473 'gsb_field_is_enable_switcher', 313 474 ); 314 475 foreach ( $number_input_ids as $number_input_id ) { … … 330 491 return array_merge( $links, $plugin_link ); 331 492 } 493 332 494 } 333 495 -
grayscale-body/trunk/js/main.js
r1515781 r2508518 4 4 */ 5 5 6 var gsbDebug = false ;6 var gsbDebug = false 7 7 8 var gsbIsIE = function () {9 var myNav = navigator.userAgent.toLowerCase() ;8 var gsbIsIE = function () { 9 var myNav = navigator.userAgent.toLowerCase() 10 10 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 } 13 15 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 ;16 var 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 24 26 25 27 /*================================================================ Local storage 26 28 */ 27 29 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) 30 33 31 if (!state) state = this.grayscaleStateName; 34 if (!state) { 35 state = initState || this.grayscaleStateName 36 } 32 37 33 return state ;34 } ;38 return state 39 } 35 40 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 } 39 44 40 45 /*================================================================ Others 41 46 */ 42 47 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 } 47 52 48 this.initSwitcher = function () {53 this.initSwitcher = function () { 49 54 var swticherEle = document.createElement('div'), 50 55 swticherName = 'gsb-switcher', 51 self = this ;56 self = this 52 57 53 58 // 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) { 59 64 var newCurrentState = (self.currentState === self.grayscaleStateName) ? 60 65 self.colorStateName : 61 self.grayscaleStateName ;66 self.grayscaleStateName 62 67 63 event.preventDefault() ;68 event.preventDefault() 64 69 65 70 // 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) 68 73 69 74 // update state (global) 70 self.prevState = self.currentState ;71 self.currentState = newCurrentState ;75 self.prevState = self.currentState 76 self.currentState = newCurrentState 72 77 73 78 // update body 74 self.updateBodyCurrentState() ;79 self.updateBodyCurrentState() 75 80 76 81 // set localStorage 77 self.setCurrentStateToLocalStorage() ;82 self.setCurrentStateToLocalStorage() 78 83 79 84 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) 83 88 } 84 } ;89 } 85 90 86 91 // add to body 87 this.bodyEle.appendChild(swticherEle) ;92 this.bodyEle.appendChild(swticherEle) 88 93 89 94 // init gsbSwitcherEle 90 this.switcherEle = document.getElementById(swticherName) ;91 } ;95 this.switcherEle = document.getElementById(swticherName) 96 } 92 97 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() 98 103 99 this.updateBodyCurrentState() ;100 this.initSwitcher() ;104 this.updateBodyCurrentState() 105 this.initSwitcher() 101 106 102 107 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) 106 111 } 107 } ;112 } 108 113 } 109 114 110 function iniGrayscaleBody () {111 var gsb = new GrayscaleBody() ;115 function iniGrayscaleBody () { 116 var gsb = new GrayscaleBody() 112 117 113 gsb.init() ;118 gsb.init() 114 119 } 115 120 116 function iniGrayscaleBodyUnsupportedBrowser () {121 function iniGrayscaleBodyUnsupportedBrowser () { 117 122 // hard code for unsupported browser 118 123 // `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') 121 126 } 122 127 123 128 if (gsbIsIE()) { 124 iniGrayscaleBodyUnsupportedBrowser() ;129 iniGrayscaleBodyUnsupportedBrowser() 125 130 126 } else { 131 } 132 else { 127 133 try { 128 document.addEventListener('DOMContentLoaded', iniGrayscaleBody) ;134 document.addEventListener('DOMContentLoaded', iniGrayscaleBody) 129 135 130 } catch (err) { 131 if (gsbDebug) console.log(err.message); 136 } 137 catch (err) { 138 if (gsbDebug) { 139 console.log(err.message) 140 } 132 141 133 iniGrayscaleBodyUnsupportedBrowser() ;142 iniGrayscaleBodyUnsupportedBrowser() 134 143 } 135 144 } -
grayscale-body/trunk/readme.txt
r2352052 r2508518 5 5 Tags: grayscale, black, white, black and white, site, convert, conversion, CSS, CSS3, filter 6 6 Requires at least: 3.0.1 7 Tested up to: 5. 4.27 Tested up to: 5.7 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 17 17 * Grayscale / Color switcher (for non-IE) 18 18 * 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 19 22 20 23 Compatible with all browsers: … … 50 53 51 54 == 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 52 64 53 65 = 1.2.4 =
Note: See TracChangeset
for help on using the changeset viewer.