ru-se.com

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

Template.php (12883B)


      1 <?php
      2 
      3 namespace Materialis\Customizer;
      4 
      5 class Template
      6 {
      7     
      8     public static function load($companion)
      9     {
     10         $themeWA      = $companion->getCustomizerData('data:widgets_areas');
     11         $widgetsAreas = apply_filters('cloudpress\template\widgets_areas', $themeWA);
     12         
     13         if (is_array($widgetsAreas)) {
     14             foreach ($widgetsAreas as $data) {
     15                 self::addWidgetsArea($data);
     16             }
     17         }
     18         
     19         add_filter('cloudpress\customizer\global_data', array(__CLASS__, '__prepareStaticSections'));
     20         
     21         add_filter('the_content', array(__CLASS__, 'filterContent'), 0);
     22         
     23         add_filter('template_include', array(__CLASS__, 'filterTemplateFile'));
     24         
     25         // add_filter('do_shortcode_tag', array(__CLASS__, 'shortcodeTagFilter'), 10, 4);
     26         
     27         // parse shortcodes make the clean for preview
     28         // add_filter('the_content', array(__CLASS__, 'decorateShortcodesInHTMLTags'), PHP_INT_MAX);
     29     }
     30     
     31     
     32     public static function shortcodeTagFilter($output, $tag, $attr, $m)
     33     {
     34         if (is_customize_preview()) {
     35             $inAttribute = false;
     36             // try to see where is the tag called from by
     37             // triggering a fake Exception
     38             try {
     39                 throw new \Exception('Look For Shortcode Type');
     40             } catch (\Exception $e) {
     41                 $trace = $e->getTrace();
     42                 if (isset($trace[6])) {
     43                     if ($trace[6]['function'] === "do_shortcodes_in_html_tags") {
     44                         $inAttribute = true;
     45                     }
     46                 }
     47             }
     48             $shortcode = $m[0];
     49             if ($inAttribute) {
     50                 $output = "CPSHORTCODE___" . bin2hex($shortcode) . "___" . $output;
     51             } else {
     52                 $uid         = uniqid(md5($shortcode));
     53                 $commentText = "cp-shortcode:{$uid}:{$shortcode}";
     54                 $output      = "<!--$commentText-->" . $output . "<!--$commentText-->";
     55             }
     56         }
     57         
     58         return $output;
     59     }
     60     
     61     public static function decorateShortcodesInHTMLTags($content)
     62     {
     63         if (is_customize_preview()) {
     64             // match inside an attribute
     65             $content = preg_replace_callback("/ (.*?)\=[\"\']CPSHORTCODE___([a-zA-Z0-9]+)___/m", function ($matches) {
     66                 $attr         = $matches[1];
     67                 $shortcodeBin = $matches[2];
     68                 $match        = $matches[0];
     69                 
     70                 
     71                 $match = str_replace("CPSHORTCODE___{$shortcodeBin}___", "", $match);
     72                 
     73                 $shortcode = hex2bin($shortcodeBin);
     74                 $shortcode = esc_attr($shortcode);
     75                 
     76                 $shortcodeAttr = " data-shortcode-{$attr}=\"{$shortcode}\" ";
     77                 
     78                 return $shortcodeAttr . $match;
     79             }, $content);
     80         }
     81         
     82         return $content;
     83     }
     84     
     85     public static function removeGutenberg()
     86     {
     87         $prioriries = apply_filters('cloudpress\companion\gutenberg_autop_filters', array(6, 8));
     88         foreach ($prioriries as $priority) {
     89             remove_filter('the_content', 'gutenberg_wpautop', $priority);
     90         }
     91         do_action('cloudpress\companion\remove_gutenberg');
     92     }
     93     
     94     public static function filterContent($content)
     95     {
     96         $companion = \Materialis\Companion::instance();
     97         if ($companion->isMaintainable()) {
     98             remove_filter('the_content', 'wpautop');
     99             static::removeGutenberg();
    100             
    101             return Template::content($content, false);
    102         }
    103         
    104         return $content;
    105     }
    106     
    107     public static function filterTemplateFile($template)
    108     {
    109         global $post;
    110         $companion = \Materialis\Companion::instance();
    111         
    112         $template = apply_filters('cloudpress\companion\template', $template, $companion, $post);
    113         
    114         if ($post && $companion->isMaintainable($post->ID)) {
    115             $companion->loadMaintainablePageAssets($post, $template);
    116         }
    117         
    118         return $template;
    119     }
    120     
    121     
    122     public static function __prepareStaticSections($globalData)
    123     {
    124         $globalData['contentSections'] = array();
    125         
    126         foreach ($globalData['data']['sections'] as $section) {
    127             $section['content'] = isset($section['content'])? $section['content'] : false;
    128             $section['content']                            = apply_filters('cloudpress\template\page_content', $section['content']);
    129             $globalData['contentSections'][$section['id']] = $section;
    130         }
    131         
    132         return $globalData;
    133     }
    134     
    135     public static function header($slug = "", $isMod = false, $modDefault = null)
    136     {
    137         if ($isMod) {
    138             $slug = get_theme_mod($slug, $modDefault);
    139         }
    140         
    141         if (is_callable($slug)) {
    142             call_user_func($slug);
    143         } else {
    144             $slug = str_replace(".php", "", $slug);
    145             
    146             if (locate_template("header-{$slug}.php", false)) {
    147                 get_header($slug);
    148             } else {
    149                 $slug = $slug . ".php";
    150                 
    151                 if (file_exists(\Materialis\Companion::instance()->themeDataPath($slug))) {
    152                     require_once \Materialis\Companion::instance()->themeDataPath($slug);
    153                 } else {
    154                     get_header();
    155                 }
    156             }
    157         }
    158     }
    159     
    160     
    161     public static function content($content = null, $echo = true)
    162     {
    163         if ($content === null) {
    164             // directly call for the page content
    165             ob_start();
    166             remove_filter('the_content', 'wpautop');
    167             static::removeGutenberg();
    168             the_content();
    169             $content = ob_get_clean();
    170         } else {
    171             // inside the filter
    172             
    173             if (is_customize_preview()) {
    174                 $settingContent = get_theme_mod('page_content', array());
    175                 if ($settingContent && is_string($settingContent) && !empty($settingContent)) {
    176                     $settingContent = json_decode(urldecode($settingContent), true);
    177                 }
    178                 
    179                 $pageId = get_the_ID();
    180                 if ($settingContent && !empty($settingContent) && isset($settingContent[$pageId])) {
    181                     $content = $settingContent[$pageId];
    182                     $content = preg_replace(\Materialis\Customizer\Settings\ContentSetting::$pageIDRegex, "", $content);
    183                 }
    184                 
    185                 // add a data-cpid attr to all nodes inside the page.
    186                 // the unmkared nodes will be removed on save
    187                 $parts = wp_html_split($content);
    188                 
    189                 $index = 0;
    190                 foreach ($parts as &$part) {
    191                     $part2 = trim($part);
    192                     if (strpos($part2, '<') === 0 && strpos($part2, '/') !== 1) {
    193                         $part = preg_replace('/(\<[a-zA-Z0-9\-]+)/', "$1 data-cpid=\"cp_node_{$index}\" ", $part);
    194                         $index++;
    195                     }
    196                 }
    197                 
    198                 $content = implode('', $parts);
    199             }
    200             
    201             $content = apply_filters('cloudpress\template\page_content', $content);
    202             if (is_customize_preview()) {
    203                 $content = "<style id='cp_customizer_content_area_start'></style>" . $content;
    204             }
    205         }
    206         
    207         if ($echo) {
    208             echo $content;
    209         } else {
    210             return $content;
    211         }
    212     }
    213     
    214     public static function footer($slug = "", $isMod = false, $modDefault = null)
    215     {
    216         if ($isMod) {
    217             $slug = get_theme_mod($slug, $modDefault);
    218         }
    219         
    220         if (is_callable($slug)) {
    221             call_user_func($slug);
    222         } else {
    223             $slug = str_replace(".php", "", $slug);
    224             
    225             if (locate_template("footer-{$slug}.php", false)) {
    226                 get_footer($slug);
    227             } else {
    228                 
    229                 $slug = $slug . ".php";
    230                 
    231                 if (file_exists(\Materialis\Companion::instance()->themeDataPath($slug))) {
    232                     require_once \Materialis\Companion::instance()->themeDataPath($slug);
    233                 } else {
    234                     get_footer();
    235                 }
    236             }
    237         }
    238     }
    239     
    240     private static function preSetWidget($sidebar, $name, $args = array())
    241     {
    242         if (!$sidebars = get_option('sidebars_widgets')) {
    243             $sidebars = array();
    244         }
    245         
    246         // Create the sidebar if it doesn't exist.
    247         if (!isset($sidebars[$sidebar])) {
    248             $sidebars[$sidebar] = array();
    249         }
    250         
    251         // Check for existing saved widgets.
    252         if ($widget_opts = get_option("widget_$name")) {
    253             // Get next insert id.
    254             ksort($widget_opts);
    255             end($widget_opts);
    256             $insert_id = key($widget_opts);
    257         } else {
    258             // None existing, start fresh.
    259             $widget_opts = array('_multiwidget' => 1);
    260             $insert_id   = 0;
    261         }
    262         // Add our settings to the stack.
    263         $widget_opts[++$insert_id] = $args;
    264         // Add our widget!
    265         $sidebars[$sidebar][] = "$name-$insert_id";
    266         
    267         update_option('sidebars_widgets', $sidebars);
    268         update_option("widget_$name", $widget_opts);
    269     }
    270     
    271     public static function addWidgetsArea($data)
    272     {
    273         add_action('widgets_init', function () use ($data) {
    274             register_sidebar(array(
    275                 'name'          => $data['name'],
    276                 'id'            => $data['id'],
    277                 'before_widget' => '<div id="%1$s" class="widget %2$s">',
    278                 'after_widget'  => '</div>',
    279                 'before_title'  => '<h4>',
    280                 'after_title'   => '</h4>',
    281             ));
    282             
    283             $active_widgets = get_option('sidebars_widgets');
    284             $index          = count($active_widgets) + 1;
    285             if (empty($active_widgets[$data['id']]) && get_theme_mod('first_time_widget_' . $data['id'], true)) {
    286                 set_theme_mod('first_time_widget_' . $data['id'], false);
    287                 
    288                 $widget_content = array(
    289                     'title'  => __($data['title'], 'cloudpress-companion-companion'),
    290                     'text'   => '<ul><li><a href="http://#">Documentation</a></li><li><a href="http://#">Forum</a></li><li><a href="http://#">FAQ</a></li><li><a href="http://#">Contact</a></li></ul>',
    291                     'filter' => false,
    292                 );
    293                 
    294                 self::preSetWidget($data['id'], 'text', $widget_content);
    295             }
    296             
    297         });
    298     }
    299     
    300     public static function getWidgetsArea($id)
    301     {
    302         ob_start(); ?>
    303         <div data-widgets-area="<?php echo $id; ?>">
    304             <?php dynamic_sidebar($id); ?>
    305         </div>
    306         <?php ;
    307         $content = ob_get_clean();
    308         
    309         return trim($content);
    310     }
    311     
    312     public static function getModsData($mods)
    313     {
    314         $results = array();
    315         foreach ($mods as $mod => $default) {
    316             $value         = \Materialis\Companion::getThemeMod($mod, $default);
    317             $value         = \Materialis\Companion::filterDefault($value);
    318             $results[$mod] = $value;
    319         }
    320         
    321         return $results;
    322     }
    323     
    324     public static function loadThemeModPartial($mod, $default = '', $data = null, $once = true)
    325     {
    326         if (empty($default)) {
    327             $default = '[tag_companion_dir]/partials/header/default';
    328         }
    329         
    330         $template_file = \Materialis\Companion::getThemeMod($mod, $default);
    331         $template_file = \Materialis\Companion::filterDefault($template_file);
    332         $template_file = str_replace('.php', '', $template_file) . ".php";
    333         
    334         if (is_array($data)) {
    335             extract($data);
    336         }
    337         
    338         // wordpess date_defaults
    339         global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
    340         
    341         if (is_array($wp_query->query_vars)) {
    342             extract($wp_query->query_vars, EXTR_SKIP);
    343         }
    344         
    345         if (isset($s)) {
    346             $s = esc_attr($s);
    347         }
    348         
    349         if ($once) {
    350             require_once($template_file);
    351         } else {
    352             require($template_file);
    353         }
    354     }
    355 }