ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

custom-style.php (2469B)


      1 <?php
      2 
      3 function materialis_sprintf_style_array($data, $media = false)
      4 {
      5     $style = "";
      6 
      7     if ( ! is_array($data) || empty($data)) {
      8         return $style;
      9     }
     10 
     11     foreach ($data as $selector => $props) {
     12         $propsText = "";
     13         foreach ($props as $prop => $value) {
     14             $propText = "\t{$prop}:{$value};\n";
     15             if ($media) {
     16                 $propText = "\t{$propText}";
     17             }
     18             $propsText .= $propText;
     19         }
     20 
     21         if ($media) {
     22             $selector = "\t{$selector}";
     23         }
     24 
     25         $style .= "$selector{\n{$propsText}\n}";
     26     }
     27 
     28     if ($media) {
     29         $style = "$media{\n{$style}\n}";
     30     }
     31 
     32     return $style . "\n";
     33 
     34 }
     35 
     36 
     37 function materialis_print_content_custom_style()
     38 {
     39     global $post;
     40 
     41     if ( ! $post) {
     42         return;
     43     }
     44 
     45     $mediaMap = array(
     46         "mobile"  => "@media screen and (max-width:767)",
     47         "tablet"  => "@media screen and (min-width:768)",
     48         "desktop" => "@media screen and (min-width:1024)",
     49         "nomedia" => false,
     50     );
     51 
     52     $mod  = 'custom_content_style_' . $post->ID;
     53     $data = get_theme_mod($mod, array(
     54         "mobile"  => array(),
     55         "tablet"  => array(),
     56         "desktop" => array(),
     57         "nomedia" => array(),
     58     ));
     59 
     60     $outputOrder = array('nomedia', 'mobile', 'tablet', 'desktop');
     61 
     62     $style = "";
     63     foreach ($outputOrder as $media) {
     64         $mediaQuery  = $mediaMap[$media];
     65         $mediaStyles = isset($data[$media]) ? $data[$media] : array();
     66         $style       .= materialis_sprintf_style_array($mediaStyles, $mediaQuery);
     67     }
     68 
     69     ?>
     70     <style id="page-content-custom-styles">
     71         <?php echo $style; ?>
     72     </style>
     73     <?php
     74 }
     75 
     76 add_action('wp_head', "materialis_print_content_custom_style", PHP_INT_MAX);
     77 
     78 function materialis_pro_page_custom_styles_filter($value)
     79 {
     80 
     81     global $post;
     82 
     83     $default = array(
     84         "mobile"  => new stdClass(),
     85         "tablet"  => new stdClass(),
     86         "desktop" => new stdClass(),
     87         "nomedia" => new stdClass(),
     88     );
     89 
     90     if ($post) {
     91         $mod  = 'custom_content_style_' . $post->ID;
     92         $data = get_theme_mod($mod, $default);
     93 
     94         $value['content_style'] = $data;
     95     } else {
     96         $value['content_style'] = $default;
     97     }
     98 
     99 
    100     return $value;
    101 }
    102 
    103 add_filter('cloudpress\customizer\preview_data', 'materialis_pro_page_custom_styles_filter');