wpsy/Twig/WordpressTwigExtension.php line 170

Open in your IDE?
  1. <?php
  2. /**
  3.  * Class TwigExtension
  4.  *
  5.  * Provide a set of methods which can be used in template engine
  6.  *
  7.  */
  8. namespace Metabolism\WordpressBundle\Twig;
  9. use Metabolism\WordpressBundle\Entity\Image;
  10. use Metabolism\WordpressBundle\Factory\Factory;
  11. use Metabolism\WordpressBundle\Factory\PostFactory;
  12. use Metabolism\WordpressBundle\Factory\TermFactory;
  13. use Twig\Extension\AbstractExtension,
  14.     Twig\TwigFilter,
  15.     Twig\TwigFunction;
  16. class WordpressTwigExtension extends AbstractExtension{
  17.     public function getFilters()
  18.     {
  19.         return [
  20.             new TwigFilter'handle''sanitize_title' ),
  21.             new TwigFilter'placeholder', [$this'placeholder'] ),
  22.             new TwigFilter'more', [$this'more'] ),
  23.             new TwigFilter'resize', [$this'resize'] ),
  24.             new TwigFilter'picture', [$this'picture'] ),
  25.             new TwigFilter'stripshortcodes','strip_shortcodes' ),
  26.             new TwigFilter'function', [$this'execFunction'] ),
  27.             new TwigFilter'excerpt','wp_trim_words' ),
  28.             new TwigFilter'sanitize','sanitize_title' ),
  29.             new TwigFilter'shortcodes','do_shortcode' ),
  30.             new TwigFilter'wpautop','wpautop' ),
  31.             new TwigFilter'array',[$this'to_array'] ),
  32.         ];
  33.     }
  34.     /**
  35.      * @return array
  36.      */
  37.     public function getFunctions()
  38.     {
  39.         return [
  40.             new TwigFunction'pixel', [$this'generatePixel'] ),
  41.             new TwigFunction'fn', [$this'execFunction'] ),
  42.             new TwigFunction'function', [$this'execFunction'] ),
  43.             new TwigFunction'action', [$this'doAction'] ),
  44.             new TwigFunction'shortcode''do_shortcode' ),
  45.             new TwigFunction'login_url''wp_login_url' ),
  46.             new TwigFunction'search_form''get_search_form' ),
  47.             new TwigFunction'archive_url''get_post_type_archive_link' ),
  48.             new TwigFunction'attachment_url''wp_get_attachment_url' ),
  49.             new TwigFunction'post_url', [$this'getPermalink'] ),
  50.             new TwigFunction'term_url', [$this'getTermLink'] ),
  51.             new TwigFunction'bloginfo''get_bloginfo' ),
  52.             new TwigFunction'dynamic_sidebar', function($id){ return $this->getOutput('dynamic_sidebar', [$id]); }, ['is_safe' => array('html')]  ),
  53.             new TwigFunction'comment_form', function($post_id$args=[]){ return $this->getOutput('comment_form', [$args$post_id]); }, ['is_safe' => array('html')]  ),
  54.             new TwigFunction'is_active_sidebar''is_active_sidebar' ),
  55.             new TwigFunction'_e''translate' ),
  56.             new TwigFunction'_x''_x' ),
  57.             new TwigFunction'_ex''_ex' ),
  58.             new TwigFunction'_nx''_nx' ),
  59.             new TwigFunction'_n_noop''_n_noop' ),
  60.             new TwigFunction'_nx_noop''_nx_noop' ),
  61.             new TwigFunction'_n''_n' ),
  62.             new TwigFunction'__''translate' ),
  63.             new TwigFunction'translate''translate' ),
  64.             new TwigFunction'translate_nooped_plural''translate_nooped_plural' ),
  65.             new TwigFunction'wp_head', function(){ return @$this->getOutput('wp_head'); }, ['is_safe' => array('html')]  ),
  66.             new TwigFunction'wp_footer', function(){ return @$this->getOutput('wp_footer'); }, ['is_safe' => array('html')]  ),
  67.             new TwigFunction'Post', function($id){ return PostFactory::create($id); } ),
  68.             new TwigFunction'User', function($id){ return Factory::create($id'user'); } ),
  69.             new TwigFunction'Term', function($id){ return TermFactory::create($id); } ),
  70.             new TwigFunction'Image', function($id){ return Factory::create($id'image'); } )
  71.         ];
  72.     }
  73.     public function to_array$arr ) {
  74.         return (array)$arr;
  75.     }
  76.     public function generatePixel($w 1$h 1) {
  77.         ob_start();
  78.         $img imagecreatetruecolor($w$h);
  79.         imagetruecolortopalette($imgfalse1);
  80.         imagesavealpha($imgtrue);
  81.         $color imagecolorallocatealpha($img000127);
  82.         imagefill($img00$color);
  83.         imagepng($imgnull9);
  84.         imagedestroy($img);
  85.         $imagedata ob_get_contents();
  86.         ob_end_clean();
  87.        return 'data:image/png;base64,' base64_encode($imagedata);
  88.     }
  89.     /**
  90.      * Return resized image
  91.      *
  92.      * @param $image
  93.      * @param $width
  94.      * @param int $height
  95.      * @param array $args
  96.      * @return string
  97.      */
  98.     public function resize($image$width$height=0$args=[])
  99.     {
  100.         if( is_string($image) )
  101.             $image = new Image($image);
  102.         elseif( is_array($image) && isset($image['url']) )
  103.             $image = new Image($image['url']);
  104.         if( !$image instanceof Image )
  105.             $image = new Image();
  106.         $args['resize'] = [$width$height];
  107.         return $image->edit($args);
  108.     }
  109.     /**
  110.      * Return resized picture
  111.      *
  112.      * @param $image
  113.      * @param $width
  114.      * @param int $height
  115.      * @param array $sources
  116.      * @param bool $alt
  117.      * @param string $loading
  118.      * @return string
  119.      */
  120.     public function picture($image$width$height=0$sources=[], $alt=false$loading='lazy')
  121.     {
  122.         if( is_string($image) ){
  123.             $image = new Image($image);
  124.         }
  125.         elseif( is_array($image) && isset($image['url']) ){
  126.             if( !$alt && isset($image['alt']) )
  127.                 $alt $image['alt'];
  128.             $image = new Image($image['url']);
  129.         }
  130.         if( !$image instanceof Image )
  131.             $image = new Image();
  132.         return $image->toHTML($width$height$sources$alt$loading);
  133.     }
  134.     /**
  135.      * Return function echo
  136.      * @param $function
  137.      * @param array $args
  138.      * @return string
  139.      */
  140.     private function getOutput($function$args=[])
  141.     {
  142.         ob_start();
  143.         call_user_func_array($function$args);
  144.         $data ob_get_contents();
  145.         ob_end_clean();
  146.         return $data;
  147.     }
  148.     /**
  149.      * @param object|int|string $term
  150.      * @param string $taxonomy
  151.      * @param bool|string $meta
  152.      * @return false|string
  153.      */
  154.     public function getTermLink$term$taxonomy 'category'$meta=false )
  155.     {
  156.         if( $meta ){
  157.             $args = array(
  158.                 'meta_query' => array(
  159.                     array(
  160.                         'key'       => $meta,
  161.                         'value'     => $term,
  162.                         'compare'   => 'LIKE'
  163.                     )
  164.                 ),
  165.                 'number'  => 1,
  166.                 'taxonomy'  => $taxonomy,
  167.             );
  168.             $terms get_terms$args );
  169.             if( count($terms) )
  170.                 $term $terms[0];
  171.         }
  172.         $link get_term_link($term$taxonomy);
  173.         if( !is_string($link) )
  174.             return false;
  175.         return $link;
  176.     }
  177.     /**
  178.      * @param $content
  179.      * @return array
  180.      */
  181.     public function more$content )
  182.     {
  183.         if ( preg_match'/<!--more(.*?)?-->/'$content$matches ) ) {
  184.             if ( has_block'more'$content ) ) {
  185.                 // Remove the core/more block delimiters. They will be left over after $content is split up.
  186.                 $content preg_replace'/<!-- \/?wp:more(.*?) -->/'''$content );
  187.             }
  188.             $content explode$matches[0], $content);
  189.         } else {
  190.             $content = array( $content );
  191.         }
  192.         foreach ($content as &$paragraph)
  193.             $paragraph force_balance_tags($paragraph);
  194.         return $content;
  195.     }
  196.     /**
  197.      * @param $function_name
  198.      * @return mixed
  199.      */
  200.     public function execFunction$function_name )
  201.     {
  202.         $args func_get_args();
  203.         array_shift($args);
  204.         if ( is_string($function_name) )
  205.             $function_name trim($function_name);
  206.         return call_user_func_array($function_name, ($args));
  207.     }
  208.     /**
  209.      * @param $action_name
  210.      * @param mixed ...$args
  211.      * @return void
  212.      */
  213.     public function doAction$action_name, ...$args )
  214.     {
  215.         do_action_ref_array$action_name$args );
  216.     }
  217.     /**
  218.      * @param $page
  219.      * @param bool $by
  220.      * @return false|string
  221.      */
  222.     public function getPermalink$page$by=false )
  223.     {
  224.         switch ( $by ){
  225.             case 'state':
  226.                 $page get_page_by_state($page);
  227.                 break;
  228.             case 'path':
  229.                 $page get_page_by_path($page);
  230.                 break;
  231.             case 'title':
  232.                 $page get_page_by_title($page);
  233.                 break;
  234.         }
  235.         if( $page ){
  236.             $link get_permalink($page);
  237.             if( !is_string($link) )
  238.                 return false;
  239.             return $link;
  240.         }
  241.         else
  242.             return false;
  243.     }
  244.     /**
  245.      * @param $image
  246.      * @param bool $params
  247.      * @return Image
  248.      */
  249.     public function placeholder($image$params=false)
  250.     {
  251.         if(!$image instanceof Image)
  252.             return new Image();
  253.         return $image;
  254.     }
  255. }