Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. <?php
  2.  
  3. function theme_get_color_styles(){
  4.     global $_theme_colors;
  5.     if (empty($_theme_colors)) {
  6.         $_theme_colors = array();
  7.         foreach(glob(get_theme_root() . '/' . get_template() . '/colors/*.css') as $color_file){
  8.             if(preg_match('/Style name: "(.*?)"/i', file_get_contents($color_file), $match)){
  9.                 $_theme_colors[basename($color_file, ".css")] = trim($match[1]);
  10.             }
  11.         }
  12.     }
  13.     return $_theme_colors;
  14. }
  15.  
  16. function theme_color_styles() {
  17.     wp_register_style('css_main', get_bloginfo('template_directory') . '/stylesheets/styles.css');
  18.     $_theme_colors = theme_get_color_styles();
  19.     foreach ($_theme_colors as $color => $name) {
  20.         wp_register_style("css_{$color}", get_bloginfo('template_directory') . "/colors/{$color}.css", array('css_main'), false, 'screen');
  21.     }
  22.     global $wp_styles;
  23.     reset($_theme_colors);
  24.     $default = get_option('default_theme_color', key($_theme_colors));
  25.     $preset = (get_option('show_switcher') && isset($_COOKIE['style'])) ? $_COOKIE['style'] : false;
  26.     $style_to_set = ($preset) ? $preset : $default;
  27.     foreach ($_theme_colors as $color => $name) {
  28.         if ($color != $style_to_set) {
  29.             $wp_styles->registered["css_{$color}"]->add_data('alt', true);
  30.         }
  31.         $wp_styles->registered["css_{$color}"]->add_data('title', $color);
  32.     }
  33. }
  34.  
  35. function theme_color_switcher($action) {
  36.     if (!get_option('show_switcher'))
  37.         return;
  38.     switch ($action) {
  39.         case 'init':
  40.  ?>
  41.     <!-- Stylesheet switcher built on jQuery -->
  42.     <script type="text/javascript">
  43.         jQuery(function($) {
  44.             var offset = $(".schemes").offset();
  45.             var topPadding = 50;
  46.             jQuery(window).scroll(function() {
  47.                 if (jQuery(window).scrollTop() > offset.top) {
  48.                     jQuery(".schemes").stop().animate({
  49.                         marginTop: $(window).scrollTop() - offset.top + topPadding
  50.                     });
  51.                 } else {
  52.                     jQuery(".schemes").stop().animate({
  53.                         marginTop: 0
  54.                     });
  55.                 };
  56.             });
  57.         });
  58.         jQuery(function($)
  59.             {
  60.                 $.stylesheetInit();
  61.                 $('.schemes a').bind(
  62.                     'click',
  63.                     function(e)
  64.                     {
  65.                         $.stylesheetSwitch(this.getAttribute('rel'));
  66.                         return false;
  67.                     }
  68.                 );
  69.             }
  70.         );
  71.     </script>
  72.  <?php
  73.         break;
  74.         case 'render':
  75.             echo '<div class="schemes">';
  76.             $url = home_url('/');
  77.             foreach (theme_get_color_styles() as $color => $name) {
  78.                 echo "<a href='{$url}?style={$color}' rel='{$color}' class='{$color}' title='{$name}'>{$name}</a>";
  79.             }
  80.             echo '</div>';
  81.         break;
  82.     }
  83. }
  84.  
  85. function enqueue_color_styles() {
  86.     $_theme_colors = theme_get_color_styles();
  87.     reset($_theme_colors);
  88.     $default = get_option('default_theme_color', key($_theme_colors));
  89.     $preset = (get_option('show_switcher') && isset($_COOKIE['style'])) ? $_COOKIE['style'] : false;
  90.     $style_to_set = ($preset) ? $preset : $default;
  91.     wp_enqueue_style("css_{$style_to_set}");
  92.     foreach ($_theme_colors as $color => $name) {
  93.         if ($color != $style_to_set) {
  94.             wp_enqueue_style("css_{$color}");
  95.         }
  96.     }
  97. }
  98.  
  99.  
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement