Changeset 686557
- Timestamp:
- 03/24/2013 10:27:08 AM (13 years ago)
- Location:
- color-scheme-every-theme
- Files:
-
- 10 edited
- 8 copied
-
tags/2.0 (copied) (copied from color-scheme-every-theme/trunk)
-
tags/2.0/classes (copied) (copied from color-scheme-every-theme/trunk/classes)
-
tags/2.0/classes/ColorExtractor.class.php (copied) (copied from color-scheme-every-theme/trunk/classes/ColorExtractor.class.php) (3 diffs)
-
tags/2.0/classes/Cset.class.php (modified) (3 diffs)
-
tags/2.0/classes/Options.class.php (modified) (5 diffs)
-
tags/2.0/color-scheme-every-theme.php (copied) (copied from color-scheme-every-theme/trunk/color-scheme-every-theme.php) (1 diff)
-
tags/2.0/languages (copied) (copied from color-scheme-every-theme/trunk/languages)
-
tags/2.0/readme.txt (copied) (copied from color-scheme-every-theme/trunk/readme.txt) (4 diffs)
-
tags/2.0/views (copied) (copied from color-scheme-every-theme/trunk/views)
-
tags/2.0/views/color_scheme_control.view.php (modified) (1 diff)
-
tags/2.0/views/options.view.php (copied) (copied from color-scheme-every-theme/trunk/views/options.view.php) (1 diff)
-
trunk/classes/ColorExtractor.class.php (modified) (3 diffs)
-
trunk/classes/Cset.class.php (modified) (3 diffs)
-
trunk/classes/Options.class.php (modified) (5 diffs)
-
trunk/color-scheme-every-theme.php (modified) (1 diff)
-
trunk/readme.txt (modified) (4 diffs)
-
trunk/views/color_scheme_control.view.php (modified) (1 diff)
-
trunk/views/options.view.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
color-scheme-every-theme/tags/2.0/classes/ColorExtractor.class.php
r678985 r686557 3 3 class Cset_ColorExtractor { 4 4 5 var $current_css_tmpl = false;6 7 public function __construct() {8 $uploads = wp_upload_dir( );9 $current_theme = wp_get_theme( );10 $this->current_css_tmpl = $uploads['basedir'].'/color-scheme-'.$current_theme->Name.'-'.$current_theme->Version.'.tss';11 }12 13 5 14 6 private function rglob($pattern='*', $flags = 0, $path='') { 15 $paths = glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT); 16 $files = glob($path.$pattern, $flags); 17 18 if (!is_array($paths) || !is_array($files)) return array(); 19 7 $paths=glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT); 8 $files=glob($path.$pattern, $flags); 20 9 foreach ($paths as $path) { $files=array_merge($files,$this->rglob($pattern, $flags, $path)); } 21 22 10 return $files; 23 11 } … … 42 30 43 31 44 public function get_custom_styles() { 45 if (file_exists($this->current_css_tmpl)) { 46 $styles = file_get_contents($this->current_css_tmpl); 47 $styles = preg_replace('/\/\*(.|\\n|\\r)+\*\//',"",$styles); 48 return $styles; 49 } 50 return ''; 51 } 52 53 54 public function color_scheme_exists() { 55 if (!file_exists($this->current_css_tmpl)) { 56 return false; 57 } 58 return true; 59 } 60 61 62 public function get_color_scheme() { 63 if ($this->color_scheme_exists()) { 64 $scheme = file_get_contents($this->current_css_tmpl); 65 if (preg_match_all('/\$color-[0-9]+/',$scheme, $colors)) { 66 return $colors; 67 } 68 } 69 return false; 70 } 71 72 73 public function get_default_scheme() { 74 if ($this->color_scheme_exists()) { 75 $scheme = file_get_contents($this->current_css_tmpl); 76 $start = strpos($scheme,"/*"); 77 $stop = strpos($scheme,"*/"); 78 if ($start >= 0 && $stop >= 0) { 79 return substr($scheme,$start,$stop-$start+2); 80 } 81 } 82 return false; 83 } 84 85 86 public function extract_color_scheme() { 32 public function extract_color_scheme( $theme_dir, $css_tmpl ) { 87 33 $regex = array( 88 34 "`^([\t\s]+)`ism"=>'', … … 92 38 "`(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+`ism"=>"\n" 93 39 ); 40 94 41 $replace_colors = array(); 95 42 $color_rules = array(); 96 $default_colors = array(); 97 $css_files = $this->rglob('*.css',0,get_stylesheet_directory()); 98 foreach ($css_files as $css_file) { 99 $clean_css = preg_replace(array_keys($regex),$regex,file_get_contents($css_file)); 100 $clean_css = str_replace(array("\r\n","\r","\n"),"",$clean_css); 101 foreach ($this->parse_css($clean_css) as $selector => $rules) { 102 foreach ($rules as $property => $value) { 103 if (preg_match_all('/#[a-f0-9]{3,6}/i',$value, $colors)) { 43 $default_colors = array(); 44 45 foreach ( $this->rglob( '*.css', 0, $theme_dir ) as $css_file ) { 46 47 $clean_css = preg_replace( array_keys( $regex ), $regex, file_get_contents($css_file) ); 48 $clean_css = str_replace( array( "\r\n", "\r", "\n" ), "", $clean_css ); 49 50 foreach ( $this->parse_css( $clean_css ) as $selector => $rules ) { 51 52 foreach ( $rules as $property => $value ) { 53 54 if ( preg_match_all( '/#[a-f0-9]{3,6}/i',$value, $colors ) ) { 55 104 56 foreach ($colors as $color) { 105 $color_id = (isset($replace_colors[$color[0]])) ? $replace_colors[$color[0]] : '$color-'.(count($replace_colors)+1); 57 58 $color_id = (isset($replace_colors[$color[0]])) ? $replace_colors[ $color[0] ] : '$color-'.( count( $replace_colors ) + 1 ); 106 59 $replace_colors[$color[0]] = $color_id; 107 60 $default_colors[$color_id] = $color[0]; 61 108 62 } 109 $color_rules[$property.":".$value][] = $selector; 63 64 $color_rules[$property.":".$value][] = '$scheme_name '.$selector; 65 110 66 } 111 if (preg_match_all('/(rgba|rgb|hsl|hsla)\([0-9\,\.]+?\)/',$value, $colors)) { 67 68 if ( preg_match_all( '/(rgba|rgb|hsl|hsla)\([0-9\,\.]+?\)/', $value, $colors ) ) { 69 112 70 foreach ($colors[0] as $color) { 71 113 72 $color_id = (isset($replace_colors[$color[0]])) ? $replace_colors[$color[0]] : '$color-'.(count($replace_colors)+1); 114 73 $replace_colors[$color] = $color_id; 115 74 $default_colors[$color_id] = $color; 75 116 76 } 117 $color_rules[$property.":".$value][] = $selector; 77 78 $color_rules[$property.":".$value][] = '$scheme_name '.$selector; 118 79 } 80 119 81 } 120 82 } 121 83 } 84 85 $css_template = ""; 86 87 foreach ($color_rules as $color_rule => $selectors) { 88 $css_template .= implode(",\n",$selectors)."{\n\t".$color_rule.";\n}\n"; 89 } 122 90 123 if (!empty($css_files)) { 124 $default_colors_template = "/**\n * Default colors\n *\n"; 125 foreach ($default_colors as $color_id => $default_color) { 126 $default_colors_template .= " * ".$color_id.": ".$default_color.";\n"; 127 } 128 $default_colors_template .= " */\n\n\n"; 129 130 $css_template = ""; 131 foreach ($color_rules as $color_rule => $selectors) { 132 $css_template .= implode(",\n",$selectors)."{\n\t".$color_rule.";\n}\n"; 133 } 134 $css_template = str_replace(array_keys($replace_colors),$replace_colors,$css_template); 135 file_put_contents($this->current_css_tmpl,$default_colors_template.$css_template); 136 } else { 137 if (file_exists($this->current_css_tmpl)) { 138 unlink($this->current_css_tmpl); 139 } 140 } 91 $css_template = str_replace( array_keys( $replace_colors ), $replace_colors, $css_template ); 92 file_put_contents( $css_tmpl, $css_template ); 93 94 return $default_colors; 141 95 } 142 96 } -
color-scheme-every-theme/tags/2.0/classes/Cset.class.php
r673507 r686557 13 13 $this->options = new Cset_Options(); 14 14 15 $this->schemes = array_merge( 16 $this->options->get_schemes(), 17 $this->schemes 18 ); 19 15 20 add_action( 'customize_register', array($this,'customize_register') ); 16 add_action( 'wp_head', array($this,'customize_css' ) ); 21 add_action( 'wp_head', array($this,'customize_css' ) ); 17 22 18 23 } … … 53 58 if (isset($this->schemes[get_theme_mod('color_scheme')])) { 54 59 $customized_script = new Cset_View("custom_styles"); 55 $css_extractor = new Cset_ColorExtractor(); 56 $styles = $css_extractor->get_custom_styles(); 60 61 $styles = ""; 62 if (file_exists(($current_css_tmpl = $this->options->get_css_templ()))) { 63 $styles = file_get_contents($current_css_tmpl); 64 } 65 57 66 $styles = str_replace( 58 67 array_reverse(array_keys($this->schemes[get_theme_mod('color_scheme')])), … … 60 69 $styles 61 70 ); 71 72 $styles = str_replace('$scheme_name ','',$styles); 73 62 74 $customized_script->set('styles', $styles); 63 75 echo $customized_script->render(); 64 76 } 77 78 if (get_option('dynamic_theme_colors')) { 79 80 $customized_js = new Cset_View("js_schemes_array"); 81 $all_styles = ""; 82 $styles = file_get_contents($current_css_tmpl); 83 $schemes = array(); 84 85 foreach ($this->schemes as $name => $colors) { 86 $name = 'cset-scheme-'.$name; 87 $mod_styles = str_replace( 88 array_reverse(array_keys($colors)), 89 array_reverse($colors), 90 $styles 91 ); 92 $all_styles .= str_replace('$scheme_name','.'.$name,$mod_styles); 93 $schemes[] = $name; 94 } 95 96 $customized_script->set('styles', $all_styles); 97 echo $customized_script->render(); 98 99 $customized_js->set('schemes',$schemes); 100 echo $customized_js->render(); 101 } 65 102 } 66 103 } -
color-scheme-every-theme/tags/2.0/classes/Options.class.php
r673507 r686557 6 6 class Cset_Options { 7 7 8 private $plugin_path = ""; 9 8 10 private $options = array(); 9 11 … … 12 14 private $css_extractor = null; 13 15 16 private $current_css_tmpl = ""; 17 18 private $current_theme_dir = ""; 19 20 private $current_theme = ""; 21 14 22 15 23 public function __construct() { 24 25 $this->plugin_path = "/wp-content/plugins/".plugin_basename(dirname(dirname(__FILE__))); 26 27 if ( !( $this->options = get_option( 'cset_schemes' ) ) ) { 28 $this->options = array(); 29 } 30 16 31 add_action('admin_menu',array(&$this,'add_admin_options_menu')); 17 32 add_action('admin_init',array(&$this,'register_admin_options')); 18 33 34 $uploads = wp_upload_dir( ); 35 $this->current_theme = wp_get_theme( ); 36 $this->current_css_tmpl = $uploads['basedir'].'/color-scheme-'.$this->current_theme->Name.'-'.$this->current_theme->Version.'.tss'; 37 $this->current_theme_dir = get_stylesheet_directory(); 38 19 39 $this->options_view = new Cset_View("options"); 20 40 $this->css_extractor = new Cset_ColorExtractor(); 21 41 22 42 add_filter('plugin_action_links_color-scheme-every-theme/color-scheme-every-theme.php',array(&$this,'settings_link')); 23 24 if (isset($_REQUEST['generate_color_scheme']) && $_REQUEST['generate_color_scheme'] == '1') { 25 $this->css_extractor->extract_color_scheme(); 43 44 add_action( 'admin_enqueue_scripts', array( $this,'enqueue_css_js' ) ); 45 46 $this->generate_scheme(); 47 $this->save_schemes(); 48 49 } 50 51 52 public function generate_scheme() { 53 if (isset($_REQUEST['generate_color_scheme'])) { 54 55 if ( !array_key_exists( $this->current_theme->Name, $this->options ) ) { 56 $this->options[$this->current_theme->Name] = array(); 57 } 58 59 $this->options[$this->current_theme->Name][0] = $this->css_extractor->extract_color_scheme( $this->current_theme_dir, $this->current_css_tmpl ); 60 update_option( "cset_schemes", $this->options); 61 62 } 63 } 64 65 public function save_schemes() { 66 if (array_key_exists('schemes',$_REQUEST) && array_key_exists($this->current_theme->Name,$_REQUEST['schemes'])) { 67 foreach ($_REQUEST['schemes'][$this->current_theme->Name] as $scheme_idx => $scheme_colors) { 68 $scheme_idx++; 69 $this->options[$this->current_theme->Name][$scheme_idx] = array(); 70 foreach ($scheme_colors as $color_idx => $color) { 71 $this->options[$this->current_theme->Name][$scheme_idx]["$".$color_idx] = $color; 72 } 73 74 } 26 75 } 27 76 77 if (array_key_exists('delete_schemes',$_REQUEST) && array_key_exists($this->current_theme->Name,$_REQUEST['delete_schemes'])) { 78 foreach ($_REQUEST['delete_schemes'][$this->current_theme->Name] as $scheme_idx => $delete) { 79 $scheme_idx++; 80 if ($delete == 1) { 81 unset($this->options[$this->current_theme->Name][$scheme_idx]); 82 } 83 } 84 } 85 86 if ( array_key_exists('new_scheme_changed',$_REQUEST) && $_REQUEST['new_scheme_changed'] && array_key_exists("new_scheme",$_REQUEST) && is_array($_REQUEST["new_scheme"])) { 87 $scheme = array(); 88 foreach ($_REQUEST["new_scheme"] as $color_idx => $color) { 89 $scheme["$".$color_idx] = $color; 90 } 91 $this->options[$this->current_theme->Name][] = $scheme; 92 } 93 94 update_option( "cset_schemes", $this->options); 95 96 } 97 98 99 public function get_css_templ() { 100 return $this->current_css_tmpl; 101 } 102 103 104 public function get_schemes() { 105 $schemes = array(); 106 if (array_key_exists($this->current_theme->Name,$this->options)) { 107 foreach ($this->options[$this->current_theme->Name] as $scheme_idx => $scheme) { 108 $schemes["custom-scheme-"+$scheme_idx] = $scheme; 109 } 110 } 111 return $schemes; 28 112 } 29 113 … … 36 120 37 121 122 public function enqueue_css_js() { 123 wp_enqueue_style('cset-options',site_url().$this->plugin_path.'/views/css/options.css'); 124 wp_enqueue_script('cset-options',site_url().$this->plugin_path.'/views/js/options.js'); 125 } 126 127 38 128 public function register_admin_options() { 39 register_setting('color-scheme-every-theme','gmaps_api_key'); 129 register_setting('cset','default_scheme'); 130 register_setting('cset','custom_schemes'); 131 register_setting('cset','dynamic_theme_colors'); 40 132 } 41 133 … … 46 138 'Color Scheme every Theme', 47 139 'manage_options', 48 'c olor-scheme-every-theme',140 'cset', 49 141 array($this,'print_admin_options_page') 50 142 ); … … 56 148 wp_die(__('You do not have sufficient permissions to access this page.', 'cset')); 57 149 } 58 59 $this->options_view->set('template_exists', $this->css_extractor->color_scheme_exists()); 60 $this->options_view->set('colors', $this->css_extractor->get_color_scheme()); 61 $this->options_view->set('default_colors', $this->css_extractor->get_default_scheme()); 150 $this->options_view->set('template_exists', array_key_exists( $this->current_theme->Name, $this->options ) ); 151 $this->options_view->set('colors', $this->options); 152 $this->options_view->set('theme', $this->current_theme->Name); 62 153 echo $this->options_view->render(); 63 154 } -
color-scheme-every-theme/tags/2.0/color-scheme-every-theme.php
r678985 r686557 4 4 Plugin URI: http://www.danielauener.com/color-scheme-every-theme 5 5 Description: This plugin lets you change the entire color scheme of the current theme via the theme customizer. 6 Version: 1.26 Version: 2.0 7 7 Author: @danielauener 8 8 Author URI: http://www.danielauener.se -
color-scheme-every-theme/tags/2.0/readme.txt
r678985 r686557 5 5 Requires at least: 3.4 6 6 Tested up to: 3.5 7 Stable tag: 1.27 Stable tag: 2.0 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 28 28 A more detailed description is available on my blog [http://www.danielauener.com/color-scheme-every-theme/](http://www.danielauener.com/color-scheme-every-theme/). You are wellcome to give feedback/ask questions directly on my blog as well. 29 29 30 Read about the new features in version 2.0 here: [http://www.danielauener.com/color-scheme-every-theme/](http://www.danielauener.com/color-scheme-every-theme/). You are wellcome to give feedback/ask questions directly on my blog as well. 31 30 32 There is even a github-repository on: [https://github.com/danielauener/color-scheme-every-theme/](http://github.com/danielauener/color-scheme-every-theme/) 31 33 … … 45 47 46 48 1. Theme customizer 49 2. Edit color schemes directly on the settings page 47 50 48 51 == Changelog == … … 54 57 * Fixed a bug that resulted in a blank page after generating the color scheme - thanks to Stiaan for reporting the bug and Ulrich P for helping me to reproduce it! 55 58 56 = 1.11 = 57 * Handle situations where no theme is activated or the current theme has no css file in it. 59 = 2.0 = 60 * Testet for multisite environment 61 * Making schemes editable on the settings page 62 * Adding an option to make all color schemes available on the front-end, could 63 be used for a javascript color scheme switcher -
color-scheme-every-theme/tags/2.0/views/color_scheme_control.view.php
r673507 r686557 3 3 <input type="radio" name="color_scheme" <?php echo $link; ?> value="<?php echo $name; ?>" /><?php echo $name; ?> 4 4 <div style="height:50px;margin:10px 0;position:relative;"><?php 5 asort($scheme);6 5 $color_count = count($scheme); 7 6 foreach ($scheme as $color) : ?> -
color-scheme-every-theme/tags/2.0/views/options.view.php
r678985 r686557 3 3 <h2><?php _e("Color Scheme every Theme settings", 'cset'); ?></h2> 4 4 <form method="post" action="options.php"> 5 <?php settings_fields('color-scheme-every-theme'); ?> 5 <?php settings_fields('cset'); ?> 6 <h3><?php _e("Existing schemes"); ?></h3> 7 <table class="form-table"> 8 <?php if (!$template_exists || count($colors[$theme]) == 0) : ?> 9 <tr valign="top"> 10 <th scope="row"><?php _e('Color Schemes', 'cset') ?></th> 11 <td> 12 <p><?php _e("There is no color scheme generatad for your current theme.", 'cset'); ?></p> 13 </td> 14 </tr> 15 <?php else: ?> 16 <tr valign="top"> 17 <th scope="row"><?php _e('Color Schemes', 'cset') ?></th> 18 <td><?php 19 foreach ($colors[$theme] as $scheme_idx => $theme_colors) : ?> 20 <div class="scheme"> 21 <input type="hidden" class="delete-flag" name="delete_schemes[<?php echo $theme; ?>][<?php echo $scheme_idx-1; ?>]" value="0" /> 22 <div class="scheme-name"> 23 <b>Scheme <?php echo ($scheme_idx == 0) ? __('default') : $scheme_idx; ?></b> 24 </div> 25 <div class="scheme-colors"><?php 26 $width = 100/count($theme_colors); 27 $color_idx = 1; 28 foreach ($theme_colors as $color) : ?> 29 <div style="width:<?php echo $width; ?>%;background:<?php echo $color; ?>;" class="scheme-color-<?php echo $color_idx; ?> scheme-color"> 30 <?php if ($scheme_idx > 0): ?> 31 <input type="hidden" name="schemes[<?php echo $theme; ?>][<?php echo $scheme_idx-1; ?>][color-<?php echo $color_idx; ?>]" value="<?php echo $color; ?>" /> 32 <?php endif; ?> 33 </div><?php 34 $color_idx++; 35 endforeach; ?> 36 </div><?php 37 if ($scheme_idx == 0): ?> 38 <p class="description"><?php _e("The default colors, extracted from your current theme."); ?></p><?php 39 else: ?> 40 <p> 41 <a class="color-scheme-embed" href="#" ><?php _e('< embedd in theme >'); ?></a> 42 | <a class="color-scheme-delete" href="#" ><?php _e('delete'); ?></a> 43 </p> 44 <p class="embed"> 45 <label><?php _e('Add this to the <em>functions.php</em> of your theme:'); ?></label> 46 <textarea class="embed-code" rows="10" style="width:100%;">if ( function_exists( 'cset_add_color_scheme' ) ) { 47 cset_add_color_scheme( 'cset_scheme-<?php echo $scheme_idx; ?>', array( <?php 48 echo "\n"; 49 50 foreach ($theme_colors as $color_idx => $color): 51 echo "\t\t".'\''.$color_idx.'\' => \''.$color.'\','."\n"; 52 endforeach; ?> 53 ) ); 54 }</textarea></p><?php 55 endif; ?> 56 </div><?php 57 endforeach; ?> 58 </td> 59 </tr> 60 </table> 61 <h3><?php _e("Add a new scheme"); ?></h3> 62 <table class="form-table"> 63 <tr valign="top"> 64 <th scope="row"><?php _e('New color scheme', 'cset') ?></th> 65 <td> 66 <div id="new-scheme" class="scheme-colors new-scheme"> 67 <input type="hidden" name="new_scheme_changed" value="0" id="new-scheme-changed" /><?php 68 $width = 100/count($theme_colors); 69 $color_idx = 1; 70 foreach ($colors[$theme][0] as $color) : ?> 71 <div style="width:<?php echo $width; ?>%;background:<?php echo $color; ?>;" id="scheme-color-<?php echo $color_idx; ?>" class="scheme-color"> 72 <input type="hidden" name="new_scheme[color-<?php echo $color_idx; ?>]" value="<?php echo $color; ?>" /> 73 </div><?php 74 $color_idx++; 75 endforeach; ?> 76 <div class="new-color"> 77 <div class="input"> 78 <label><?php _e('Change color: '); ?></label><input type="text" id="new-color" class="regular-text" /> 79 </div> 80 </div> 81 </div> 82 </td> 83 </tr> 84 <?php endif; ?> 85 </table> 86 <h3><?php _e("Options"); ?></h3> 6 87 <table class="form-table"> 7 88 <tr valign="top"> 8 <th scope="row"><?php _e('Color Schemes', 'cset') ?></th> 9 <td><?php 10 if (!$template_exists) : ?> 11 <p><?php _e("There is no color scheme generatad for your current theme.", 'cset'); ?></p><?php 12 if (isset($_GET['settings-updated']) && $_GET['settings-updated'] == 'true') : ?> 13 <p style="color:#990000;"><?php _e("There was an error while generating the color scheme. <br />Check if you have an active and working theme and if there is any css-file in your active theme!"); ?></p><?php 14 endif; 15 elseif ($colors): ?> 16 <textarea rows="10" style="width:100%;">if ( function_exists( 'cset_add_color_scheme' ) ) { 17 cset_add_color_scheme( 'scheme-name', array( <?php 18 echo "\n"; 19 20 foreach (array_unique($colors[0]) as $color): 21 echo "\t\t".'\''.$color.'\' => \'#111111\','."\n"; 22 endforeach; ?> 23 ) ); 24 }</textarea> 25 <p class="description"><?php _e('Add this code to your <b>functions.php</b>, for every color scheme you want to add, and modify the color codes as you whish.', 'cset'); ?></p><br /><br /> 26 <textarea rows="10" style="width:100%;"><?php 27 echo $default_colors; 28 ?></textarea> 29 <p class="description"><?php _e('These are the default colors, extracted from your current theme.', 'cset'); ?></p><?php 30 endif; ?> 89 <th scope="row"><?php _e('Dynamic theme colors', 'cset') ?></th> 90 <td> 91 <label for="dynamic_theme_colors"> 92 <input name="dynamic_theme_colors" type="checkbox" id="dynamic_theme_colors" value="1" <?php checked('1', get_option('dynamic_theme_colors')); ?> /> 93 <?php _e("Embed css for all color schemes in the current theme, so they can be changed dynamically via javascript.", 'cset'); ?> 94 </label> 31 95 </td> 32 </tr> 33 </table> 96 </tr> 97 </table> 34 98 <p class="submit"> 35 <input type="hidden" name="generate_color_scheme" value="1" />36 <input type="submit" class="button-primary" value="<?php echo ($template_exists) ? _e('Regenerate Color Scheme', 'cset') : _e('Generate Color Scheme', 'cset'); ?>" />99 <input id="save-color-schemes" type="submit" name="save_color_schemes" class="button-primary" value="<?php _e('Save Schemes'); ?>" /> 100 <input type="submit" name="generate_color_scheme" class="<?php echo ($template_exists) ? 'button' : 'button-primary'; ?>" value="<?php echo ($template_exists) ? _e('Regenerate Color Scheme', 'cset') : _e('Generate Color Scheme', 'cset'); ?>" /> 37 101 </p> 38 102 </form> -
color-scheme-every-theme/trunk/classes/ColorExtractor.class.php
r678985 r686557 3 3 class Cset_ColorExtractor { 4 4 5 var $current_css_tmpl = false;6 7 public function __construct() {8 $uploads = wp_upload_dir( );9 $current_theme = wp_get_theme( );10 $this->current_css_tmpl = $uploads['basedir'].'/color-scheme-'.$current_theme->Name.'-'.$current_theme->Version.'.tss';11 }12 13 5 14 6 private function rglob($pattern='*', $flags = 0, $path='') { 15 $paths = glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT); 16 $files = glob($path.$pattern, $flags); 17 18 if (!is_array($paths) || !is_array($files)) return array(); 19 7 $paths=glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT); 8 $files=glob($path.$pattern, $flags); 20 9 foreach ($paths as $path) { $files=array_merge($files,$this->rglob($pattern, $flags, $path)); } 21 22 10 return $files; 23 11 } … … 42 30 43 31 44 public function get_custom_styles() { 45 if (file_exists($this->current_css_tmpl)) { 46 $styles = file_get_contents($this->current_css_tmpl); 47 $styles = preg_replace('/\/\*(.|\\n|\\r)+\*\//',"",$styles); 48 return $styles; 49 } 50 return ''; 51 } 52 53 54 public function color_scheme_exists() { 55 if (!file_exists($this->current_css_tmpl)) { 56 return false; 57 } 58 return true; 59 } 60 61 62 public function get_color_scheme() { 63 if ($this->color_scheme_exists()) { 64 $scheme = file_get_contents($this->current_css_tmpl); 65 if (preg_match_all('/\$color-[0-9]+/',$scheme, $colors)) { 66 return $colors; 67 } 68 } 69 return false; 70 } 71 72 73 public function get_default_scheme() { 74 if ($this->color_scheme_exists()) { 75 $scheme = file_get_contents($this->current_css_tmpl); 76 $start = strpos($scheme,"/*"); 77 $stop = strpos($scheme,"*/"); 78 if ($start >= 0 && $stop >= 0) { 79 return substr($scheme,$start,$stop-$start+2); 80 } 81 } 82 return false; 83 } 84 85 86 public function extract_color_scheme() { 32 public function extract_color_scheme( $theme_dir, $css_tmpl ) { 87 33 $regex = array( 88 34 "`^([\t\s]+)`ism"=>'', … … 92 38 "`(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+`ism"=>"\n" 93 39 ); 40 94 41 $replace_colors = array(); 95 42 $color_rules = array(); 96 $default_colors = array(); 97 $css_files = $this->rglob('*.css',0,get_stylesheet_directory()); 98 foreach ($css_files as $css_file) { 99 $clean_css = preg_replace(array_keys($regex),$regex,file_get_contents($css_file)); 100 $clean_css = str_replace(array("\r\n","\r","\n"),"",$clean_css); 101 foreach ($this->parse_css($clean_css) as $selector => $rules) { 102 foreach ($rules as $property => $value) { 103 if (preg_match_all('/#[a-f0-9]{3,6}/i',$value, $colors)) { 43 $default_colors = array(); 44 45 foreach ( $this->rglob( '*.css', 0, $theme_dir ) as $css_file ) { 46 47 $clean_css = preg_replace( array_keys( $regex ), $regex, file_get_contents($css_file) ); 48 $clean_css = str_replace( array( "\r\n", "\r", "\n" ), "", $clean_css ); 49 50 foreach ( $this->parse_css( $clean_css ) as $selector => $rules ) { 51 52 foreach ( $rules as $property => $value ) { 53 54 if ( preg_match_all( '/#[a-f0-9]{3,6}/i',$value, $colors ) ) { 55 104 56 foreach ($colors as $color) { 105 $color_id = (isset($replace_colors[$color[0]])) ? $replace_colors[$color[0]] : '$color-'.(count($replace_colors)+1); 57 58 $color_id = (isset($replace_colors[$color[0]])) ? $replace_colors[ $color[0] ] : '$color-'.( count( $replace_colors ) + 1 ); 106 59 $replace_colors[$color[0]] = $color_id; 107 60 $default_colors[$color_id] = $color[0]; 61 108 62 } 109 $color_rules[$property.":".$value][] = $selector; 63 64 $color_rules[$property.":".$value][] = '$scheme_name '.$selector; 65 110 66 } 111 if (preg_match_all('/(rgba|rgb|hsl|hsla)\([0-9\,\.]+?\)/',$value, $colors)) { 67 68 if ( preg_match_all( '/(rgba|rgb|hsl|hsla)\([0-9\,\.]+?\)/', $value, $colors ) ) { 69 112 70 foreach ($colors[0] as $color) { 71 113 72 $color_id = (isset($replace_colors[$color[0]])) ? $replace_colors[$color[0]] : '$color-'.(count($replace_colors)+1); 114 73 $replace_colors[$color] = $color_id; 115 74 $default_colors[$color_id] = $color; 75 116 76 } 117 $color_rules[$property.":".$value][] = $selector; 77 78 $color_rules[$property.":".$value][] = '$scheme_name '.$selector; 118 79 } 80 119 81 } 120 82 } 121 83 } 84 85 $css_template = ""; 86 87 foreach ($color_rules as $color_rule => $selectors) { 88 $css_template .= implode(",\n",$selectors)."{\n\t".$color_rule.";\n}\n"; 89 } 122 90 123 if (!empty($css_files)) { 124 $default_colors_template = "/**\n * Default colors\n *\n"; 125 foreach ($default_colors as $color_id => $default_color) { 126 $default_colors_template .= " * ".$color_id.": ".$default_color.";\n"; 127 } 128 $default_colors_template .= " */\n\n\n"; 129 130 $css_template = ""; 131 foreach ($color_rules as $color_rule => $selectors) { 132 $css_template .= implode(",\n",$selectors)."{\n\t".$color_rule.";\n}\n"; 133 } 134 $css_template = str_replace(array_keys($replace_colors),$replace_colors,$css_template); 135 file_put_contents($this->current_css_tmpl,$default_colors_template.$css_template); 136 } else { 137 if (file_exists($this->current_css_tmpl)) { 138 unlink($this->current_css_tmpl); 139 } 140 } 91 $css_template = str_replace( array_keys( $replace_colors ), $replace_colors, $css_template ); 92 file_put_contents( $css_tmpl, $css_template ); 93 94 return $default_colors; 141 95 } 142 96 } -
color-scheme-every-theme/trunk/classes/Cset.class.php
r673507 r686557 13 13 $this->options = new Cset_Options(); 14 14 15 $this->schemes = array_merge( 16 $this->options->get_schemes(), 17 $this->schemes 18 ); 19 15 20 add_action( 'customize_register', array($this,'customize_register') ); 16 add_action( 'wp_head', array($this,'customize_css' ) ); 21 add_action( 'wp_head', array($this,'customize_css' ) ); 17 22 18 23 } … … 53 58 if (isset($this->schemes[get_theme_mod('color_scheme')])) { 54 59 $customized_script = new Cset_View("custom_styles"); 55 $css_extractor = new Cset_ColorExtractor(); 56 $styles = $css_extractor->get_custom_styles(); 60 61 $styles = ""; 62 if (file_exists(($current_css_tmpl = $this->options->get_css_templ()))) { 63 $styles = file_get_contents($current_css_tmpl); 64 } 65 57 66 $styles = str_replace( 58 67 array_reverse(array_keys($this->schemes[get_theme_mod('color_scheme')])), … … 60 69 $styles 61 70 ); 71 72 $styles = str_replace('$scheme_name ','',$styles); 73 62 74 $customized_script->set('styles', $styles); 63 75 echo $customized_script->render(); 64 76 } 77 78 if (get_option('dynamic_theme_colors')) { 79 80 $customized_js = new Cset_View("js_schemes_array"); 81 $all_styles = ""; 82 $styles = file_get_contents($current_css_tmpl); 83 $schemes = array(); 84 85 foreach ($this->schemes as $name => $colors) { 86 $name = 'cset-scheme-'.$name; 87 $mod_styles = str_replace( 88 array_reverse(array_keys($colors)), 89 array_reverse($colors), 90 $styles 91 ); 92 $all_styles .= str_replace('$scheme_name','.'.$name,$mod_styles); 93 $schemes[] = $name; 94 } 95 96 $customized_script->set('styles', $all_styles); 97 echo $customized_script->render(); 98 99 $customized_js->set('schemes',$schemes); 100 echo $customized_js->render(); 101 } 65 102 } 66 103 } -
color-scheme-every-theme/trunk/classes/Options.class.php
r673507 r686557 6 6 class Cset_Options { 7 7 8 private $plugin_path = ""; 9 8 10 private $options = array(); 9 11 … … 12 14 private $css_extractor = null; 13 15 16 private $current_css_tmpl = ""; 17 18 private $current_theme_dir = ""; 19 20 private $current_theme = ""; 21 14 22 15 23 public function __construct() { 24 25 $this->plugin_path = "/wp-content/plugins/".plugin_basename(dirname(dirname(__FILE__))); 26 27 if ( !( $this->options = get_option( 'cset_schemes' ) ) ) { 28 $this->options = array(); 29 } 30 16 31 add_action('admin_menu',array(&$this,'add_admin_options_menu')); 17 32 add_action('admin_init',array(&$this,'register_admin_options')); 18 33 34 $uploads = wp_upload_dir( ); 35 $this->current_theme = wp_get_theme( ); 36 $this->current_css_tmpl = $uploads['basedir'].'/color-scheme-'.$this->current_theme->Name.'-'.$this->current_theme->Version.'.tss'; 37 $this->current_theme_dir = get_stylesheet_directory(); 38 19 39 $this->options_view = new Cset_View("options"); 20 40 $this->css_extractor = new Cset_ColorExtractor(); 21 41 22 42 add_filter('plugin_action_links_color-scheme-every-theme/color-scheme-every-theme.php',array(&$this,'settings_link')); 23 24 if (isset($_REQUEST['generate_color_scheme']) && $_REQUEST['generate_color_scheme'] == '1') { 25 $this->css_extractor->extract_color_scheme(); 43 44 add_action( 'admin_enqueue_scripts', array( $this,'enqueue_css_js' ) ); 45 46 $this->generate_scheme(); 47 $this->save_schemes(); 48 49 } 50 51 52 public function generate_scheme() { 53 if (isset($_REQUEST['generate_color_scheme'])) { 54 55 if ( !array_key_exists( $this->current_theme->Name, $this->options ) ) { 56 $this->options[$this->current_theme->Name] = array(); 57 } 58 59 $this->options[$this->current_theme->Name][0] = $this->css_extractor->extract_color_scheme( $this->current_theme_dir, $this->current_css_tmpl ); 60 update_option( "cset_schemes", $this->options); 61 62 } 63 } 64 65 public function save_schemes() { 66 if (array_key_exists('schemes',$_REQUEST) && array_key_exists($this->current_theme->Name,$_REQUEST['schemes'])) { 67 foreach ($_REQUEST['schemes'][$this->current_theme->Name] as $scheme_idx => $scheme_colors) { 68 $scheme_idx++; 69 $this->options[$this->current_theme->Name][$scheme_idx] = array(); 70 foreach ($scheme_colors as $color_idx => $color) { 71 $this->options[$this->current_theme->Name][$scheme_idx]["$".$color_idx] = $color; 72 } 73 74 } 26 75 } 27 76 77 if (array_key_exists('delete_schemes',$_REQUEST) && array_key_exists($this->current_theme->Name,$_REQUEST['delete_schemes'])) { 78 foreach ($_REQUEST['delete_schemes'][$this->current_theme->Name] as $scheme_idx => $delete) { 79 $scheme_idx++; 80 if ($delete == 1) { 81 unset($this->options[$this->current_theme->Name][$scheme_idx]); 82 } 83 } 84 } 85 86 if ( array_key_exists('new_scheme_changed',$_REQUEST) && $_REQUEST['new_scheme_changed'] && array_key_exists("new_scheme",$_REQUEST) && is_array($_REQUEST["new_scheme"])) { 87 $scheme = array(); 88 foreach ($_REQUEST["new_scheme"] as $color_idx => $color) { 89 $scheme["$".$color_idx] = $color; 90 } 91 $this->options[$this->current_theme->Name][] = $scheme; 92 } 93 94 update_option( "cset_schemes", $this->options); 95 96 } 97 98 99 public function get_css_templ() { 100 return $this->current_css_tmpl; 101 } 102 103 104 public function get_schemes() { 105 $schemes = array(); 106 if (array_key_exists($this->current_theme->Name,$this->options)) { 107 foreach ($this->options[$this->current_theme->Name] as $scheme_idx => $scheme) { 108 $schemes["custom-scheme-"+$scheme_idx] = $scheme; 109 } 110 } 111 return $schemes; 28 112 } 29 113 … … 36 120 37 121 122 public function enqueue_css_js() { 123 wp_enqueue_style('cset-options',site_url().$this->plugin_path.'/views/css/options.css'); 124 wp_enqueue_script('cset-options',site_url().$this->plugin_path.'/views/js/options.js'); 125 } 126 127 38 128 public function register_admin_options() { 39 register_setting('color-scheme-every-theme','gmaps_api_key'); 129 register_setting('cset','default_scheme'); 130 register_setting('cset','custom_schemes'); 131 register_setting('cset','dynamic_theme_colors'); 40 132 } 41 133 … … 46 138 'Color Scheme every Theme', 47 139 'manage_options', 48 'c olor-scheme-every-theme',140 'cset', 49 141 array($this,'print_admin_options_page') 50 142 ); … … 56 148 wp_die(__('You do not have sufficient permissions to access this page.', 'cset')); 57 149 } 58 59 $this->options_view->set('template_exists', $this->css_extractor->color_scheme_exists()); 60 $this->options_view->set('colors', $this->css_extractor->get_color_scheme()); 61 $this->options_view->set('default_colors', $this->css_extractor->get_default_scheme()); 150 $this->options_view->set('template_exists', array_key_exists( $this->current_theme->Name, $this->options ) ); 151 $this->options_view->set('colors', $this->options); 152 $this->options_view->set('theme', $this->current_theme->Name); 62 153 echo $this->options_view->render(); 63 154 } -
color-scheme-every-theme/trunk/color-scheme-every-theme.php
r678985 r686557 4 4 Plugin URI: http://www.danielauener.com/color-scheme-every-theme 5 5 Description: This plugin lets you change the entire color scheme of the current theme via the theme customizer. 6 Version: 1.26 Version: 2.0 7 7 Author: @danielauener 8 8 Author URI: http://www.danielauener.se -
color-scheme-every-theme/trunk/readme.txt
r678985 r686557 5 5 Requires at least: 3.4 6 6 Tested up to: 3.5 7 Stable tag: 1.27 Stable tag: 2.0 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 28 28 A more detailed description is available on my blog [http://www.danielauener.com/color-scheme-every-theme/](http://www.danielauener.com/color-scheme-every-theme/). You are wellcome to give feedback/ask questions directly on my blog as well. 29 29 30 Read about the new features in version 2.0 here: [http://www.danielauener.com/color-scheme-every-theme/](http://www.danielauener.com/color-scheme-every-theme/). You are wellcome to give feedback/ask questions directly on my blog as well. 31 30 32 There is even a github-repository on: [https://github.com/danielauener/color-scheme-every-theme/](http://github.com/danielauener/color-scheme-every-theme/) 31 33 … … 45 47 46 48 1. Theme customizer 49 2. Edit color schemes directly on the settings page 47 50 48 51 == Changelog == … … 54 57 * Fixed a bug that resulted in a blank page after generating the color scheme - thanks to Stiaan for reporting the bug and Ulrich P for helping me to reproduce it! 55 58 56 = 1.11 = 57 * Handle situations where no theme is activated or the current theme has no css file in it. 59 = 2.0 = 60 * Testet for multisite environment 61 * Making schemes editable on the settings page 62 * Adding an option to make all color schemes available on the front-end, could 63 be used for a javascript color scheme switcher -
color-scheme-every-theme/trunk/views/color_scheme_control.view.php
r673507 r686557 3 3 <input type="radio" name="color_scheme" <?php echo $link; ?> value="<?php echo $name; ?>" /><?php echo $name; ?> 4 4 <div style="height:50px;margin:10px 0;position:relative;"><?php 5 asort($scheme);6 5 $color_count = count($scheme); 7 6 foreach ($scheme as $color) : ?> -
color-scheme-every-theme/trunk/views/options.view.php
r678985 r686557 3 3 <h2><?php _e("Color Scheme every Theme settings", 'cset'); ?></h2> 4 4 <form method="post" action="options.php"> 5 <?php settings_fields('color-scheme-every-theme'); ?> 5 <?php settings_fields('cset'); ?> 6 <h3><?php _e("Existing schemes"); ?></h3> 7 <table class="form-table"> 8 <?php if (!$template_exists || count($colors[$theme]) == 0) : ?> 9 <tr valign="top"> 10 <th scope="row"><?php _e('Color Schemes', 'cset') ?></th> 11 <td> 12 <p><?php _e("There is no color scheme generatad for your current theme.", 'cset'); ?></p> 13 </td> 14 </tr> 15 <?php else: ?> 16 <tr valign="top"> 17 <th scope="row"><?php _e('Color Schemes', 'cset') ?></th> 18 <td><?php 19 foreach ($colors[$theme] as $scheme_idx => $theme_colors) : ?> 20 <div class="scheme"> 21 <input type="hidden" class="delete-flag" name="delete_schemes[<?php echo $theme; ?>][<?php echo $scheme_idx-1; ?>]" value="0" /> 22 <div class="scheme-name"> 23 <b>Scheme <?php echo ($scheme_idx == 0) ? __('default') : $scheme_idx; ?></b> 24 </div> 25 <div class="scheme-colors"><?php 26 $width = 100/count($theme_colors); 27 $color_idx = 1; 28 foreach ($theme_colors as $color) : ?> 29 <div style="width:<?php echo $width; ?>%;background:<?php echo $color; ?>;" class="scheme-color-<?php echo $color_idx; ?> scheme-color"> 30 <?php if ($scheme_idx > 0): ?> 31 <input type="hidden" name="schemes[<?php echo $theme; ?>][<?php echo $scheme_idx-1; ?>][color-<?php echo $color_idx; ?>]" value="<?php echo $color; ?>" /> 32 <?php endif; ?> 33 </div><?php 34 $color_idx++; 35 endforeach; ?> 36 </div><?php 37 if ($scheme_idx == 0): ?> 38 <p class="description"><?php _e("The default colors, extracted from your current theme."); ?></p><?php 39 else: ?> 40 <p> 41 <a class="color-scheme-embed" href="#" ><?php _e('< embedd in theme >'); ?></a> 42 | <a class="color-scheme-delete" href="#" ><?php _e('delete'); ?></a> 43 </p> 44 <p class="embed"> 45 <label><?php _e('Add this to the <em>functions.php</em> of your theme:'); ?></label> 46 <textarea class="embed-code" rows="10" style="width:100%;">if ( function_exists( 'cset_add_color_scheme' ) ) { 47 cset_add_color_scheme( 'cset_scheme-<?php echo $scheme_idx; ?>', array( <?php 48 echo "\n"; 49 50 foreach ($theme_colors as $color_idx => $color): 51 echo "\t\t".'\''.$color_idx.'\' => \''.$color.'\','."\n"; 52 endforeach; ?> 53 ) ); 54 }</textarea></p><?php 55 endif; ?> 56 </div><?php 57 endforeach; ?> 58 </td> 59 </tr> 60 </table> 61 <h3><?php _e("Add a new scheme"); ?></h3> 62 <table class="form-table"> 63 <tr valign="top"> 64 <th scope="row"><?php _e('New color scheme', 'cset') ?></th> 65 <td> 66 <div id="new-scheme" class="scheme-colors new-scheme"> 67 <input type="hidden" name="new_scheme_changed" value="0" id="new-scheme-changed" /><?php 68 $width = 100/count($theme_colors); 69 $color_idx = 1; 70 foreach ($colors[$theme][0] as $color) : ?> 71 <div style="width:<?php echo $width; ?>%;background:<?php echo $color; ?>;" id="scheme-color-<?php echo $color_idx; ?>" class="scheme-color"> 72 <input type="hidden" name="new_scheme[color-<?php echo $color_idx; ?>]" value="<?php echo $color; ?>" /> 73 </div><?php 74 $color_idx++; 75 endforeach; ?> 76 <div class="new-color"> 77 <div class="input"> 78 <label><?php _e('Change color: '); ?></label><input type="text" id="new-color" class="regular-text" /> 79 </div> 80 </div> 81 </div> 82 </td> 83 </tr> 84 <?php endif; ?> 85 </table> 86 <h3><?php _e("Options"); ?></h3> 6 87 <table class="form-table"> 7 88 <tr valign="top"> 8 <th scope="row"><?php _e('Color Schemes', 'cset') ?></th> 9 <td><?php 10 if (!$template_exists) : ?> 11 <p><?php _e("There is no color scheme generatad for your current theme.", 'cset'); ?></p><?php 12 if (isset($_GET['settings-updated']) && $_GET['settings-updated'] == 'true') : ?> 13 <p style="color:#990000;"><?php _e("There was an error while generating the color scheme. <br />Check if you have an active and working theme and if there is any css-file in your active theme!"); ?></p><?php 14 endif; 15 elseif ($colors): ?> 16 <textarea rows="10" style="width:100%;">if ( function_exists( 'cset_add_color_scheme' ) ) { 17 cset_add_color_scheme( 'scheme-name', array( <?php 18 echo "\n"; 19 20 foreach (array_unique($colors[0]) as $color): 21 echo "\t\t".'\''.$color.'\' => \'#111111\','."\n"; 22 endforeach; ?> 23 ) ); 24 }</textarea> 25 <p class="description"><?php _e('Add this code to your <b>functions.php</b>, for every color scheme you want to add, and modify the color codes as you whish.', 'cset'); ?></p><br /><br /> 26 <textarea rows="10" style="width:100%;"><?php 27 echo $default_colors; 28 ?></textarea> 29 <p class="description"><?php _e('These are the default colors, extracted from your current theme.', 'cset'); ?></p><?php 30 endif; ?> 89 <th scope="row"><?php _e('Dynamic theme colors', 'cset') ?></th> 90 <td> 91 <label for="dynamic_theme_colors"> 92 <input name="dynamic_theme_colors" type="checkbox" id="dynamic_theme_colors" value="1" <?php checked('1', get_option('dynamic_theme_colors')); ?> /> 93 <?php _e("Embed css for all color schemes in the current theme, so they can be changed dynamically via javascript.", 'cset'); ?> 94 </label> 31 95 </td> 32 </tr> 33 </table> 96 </tr> 97 </table> 34 98 <p class="submit"> 35 <input type="hidden" name="generate_color_scheme" value="1" />36 <input type="submit" class="button-primary" value="<?php echo ($template_exists) ? _e('Regenerate Color Scheme', 'cset') : _e('Generate Color Scheme', 'cset'); ?>" />99 <input id="save-color-schemes" type="submit" name="save_color_schemes" class="button-primary" value="<?php _e('Save Schemes'); ?>" /> 100 <input type="submit" name="generate_color_scheme" class="<?php echo ($template_exists) ? 'button' : 'button-primary'; ?>" value="<?php echo ($template_exists) ? _e('Regenerate Color Scheme', 'cset') : _e('Generate Color Scheme', 'cset'); ?>" /> 37 101 </p> 38 102 </form>
Note: See TracChangeset
for help on using the changeset viewer.