예제 #1
0
 /**
  * Extend blade
  * @return void
  */
 protected function extend()
 {
     // add @acfrepeater
     $this->blade->getCompiler()->extend(function ($view, $compiler) {
         if (!function_exists('get_field')) {
             return $view;
         }
         $pattern = '/(\\s*)@acf\\(((\\s*)(.+))\\)/';
         $replacement = '$1<?php if ( have_rows( $2 ) ) : ';
         $replacement .= 'while ( have_rows( $2 ) ) : the_row(); ?>';
         return preg_replace($pattern, $replacement, $view);
     });
     // add @acfempty
     $this->blade->getCompiler()->extend(function ($view, $compiler) {
         return str_replace('@acfempty', '<?php endwhile; ?><?php else: ?>', $view);
     });
     // add @acfend
     $this->blade->getCompiler()->extend(function ($view, $compiler) {
         if (!function_exists('get_field')) {
             return $view;
         }
         return str_replace('@acfend', '<?php endif; ?>', $view);
     });
     // add @subfield
     $this->blade->getCompiler()->extend(function ($view, $compiler) {
         if (!function_exists('get_field')) {
             return $view;
         }
         $pattern = '/(\\s*)@subfield\\(((\\s*)(.+))\\)/';
         $replacement = '$1<?php if ( get_sub_field( $2 ) ) : ';
         $replacement .= 'the_sub_field($2); endif; ?>';
         return preg_replace($pattern, $replacement, $view);
     });
     // add @field
     $this->blade->getCompiler()->extend(function ($view, $compiler) {
         if (!function_exists('get_field')) {
             return $view;
         }
         $pattern = '/(\\s*)@field\\(((\\s*)(.+))\\)/';
         $replacement = '$1<?php if ( get_field( $2 ) ) : ';
         $replacement .= 'the_field($2); endif; ?>';
         return preg_replace($pattern, $replacement, $view);
     });
     // add @hasfield
     $this->blade->getCompiler()->extend(function ($view, $compiler) {
         if (!function_exists('get_field')) {
             return $view;
         }
         $pattern = '/(\\s*)@hasfield\\(((\\s*)(.+))\\)/';
         $replacement = '$1<?php if ( get_field( $2 ) ) : ?>';
         return preg_replace($pattern, $replacement, $view);
     });
     // add @wpposts
     $this->blade->getCompiler()->extend(function ($view, $compiler) {
         return str_replace('@wpposts', '<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>', $view);
     });
     // add @wpquery
     $this->blade->getCompiler()->extend(function ($view, $compiler) {
         $pattern = '/(\\s*)@wpquery(\\s*\\(.*\\))/';
         $replacement = '$1<?php $bladequery = new WP_Query$2; ';
         $replacement .= 'if ( $bladequery->have_posts() ) : ';
         $replacement .= 'while ( $bladequery->have_posts() ) : ';
         $replacement .= '$bladequery->the_post(); ?> ';
         return preg_replace($pattern, $replacement, $view);
     });
     // add @wpempty
     $this->blade->getCompiler()->extend(function ($view, $compiler) {
         return str_replace('@wpempty', '<?php endwhile; ?><?php else: ?>', $view);
     });
     // add @wpend
     $this->blade->getCompiler()->extend(function ($view, $compiler) {
         return str_replace('@wpend', '<?php endif; wp_reset_postdata(); ?>', $view);
     });
     // add @scripts
     $self = $this;
     $this->blade->getCompiler()->directive('scripts', function ($expression) use($self) {
         return '<?php ' . get_class($self) . '::add_scripts(' . $expression . '); ?>';
     });
 }