From 0af3262496108403c7587e83da16ded79fec6599 Mon Sep 17 00:00:00 2001 From: Nikita Date: Thu, 10 Oct 2024 11:46:18 +0300 Subject: [PATCH] move handlebars methods to separate class --- classes/class-blocks.php | 261 +------------------------------- classes/class-handlebars.php | 279 +++++++++++++++++++++++++++++++++++ lazy-blocks.php | 26 +++- 3 files changed, 301 insertions(+), 265 deletions(-) create mode 100644 classes/class-handlebars.php diff --git a/classes/class-blocks.php b/classes/class-blocks.php index 33b73c18..12edb2db 100644 --- a/classes/class-blocks.php +++ b/classes/class-blocks.php @@ -14,13 +14,6 @@ * LazyBlocks_Blocks class. Class to work with LazyBlocks CPT. */ class LazyBlocks_Blocks { - /** - * Handlebars engine. - * - * @var null|object - */ - private $handlebars = null; - /** * Rules to sanitize SVG * @@ -57,8 +50,6 @@ class LazyBlocks_Blocks { * LazyBlocks_Blocks constructor. */ public function __construct() { - add_action( 'init', array( $this, 'prepare_handlebars' ) ); - add_action( 'init', array( $this, 'register_post_type' ) ); add_action( 'init', array( $this, 'remove_custom_fields_support' ), 150 ); @@ -131,256 +122,6 @@ public function normalize_lazyblocks_post_status( $post_id, $post ) { } } - /** - * Check if the attribute is a handlebars options attribute. - * - * @param mixed $attribute The attribute to check. - * @return bool True if the attribute is a handlebars options attribute, false otherwise. - */ - public function is_handlebars_options_attribute( $attribute ) { - return is_array( $attribute ) && isset( $attribute['data']['index'] ) && isset( $attribute['context'] ); - } - - /** - * Handlebars php. - */ - public function prepare_handlebars() { - require_once lazyblocks()->plugin_path() . 'vendors/Handlebars/Autoloader.php'; - - Handlebars\Autoloader::register(); - - $this->handlebars = new Handlebars\Handlebars(); - - // truncate - // {{truncate 'string' 2 'true'}}. - $this->handlebars->registerHelper( - 'truncate', - function( $str, $len, $ellipsis = 'true' ) { - if ( $str && $len && mb_strlen( $str, 'UTF-8' ) > $len ) { - $new_str = mb_substr( $str, 0, $len + 1, 'UTF-8' ); - $count = mb_strlen( $new_str, 'UTF-8' ); - - while ( $count > 0 ) { - $ch = mb_substr( $new_str, -1, null, 'UTF-8' ); - $new_str = mb_substr( $new_str, 0, -1, 'UTF-8' ); - - $count--; - - if ( ' ' === $ch ) { - break; - } - } - - if ( '' === $new_str ) { - $new_str = mb_substr( $str, 0, $len, 'UTF-8' ); - } - - return new \Handlebars\SafeString( $new_str . ( 'true' === $ellipsis ? '...' : '' ) ); - } - return $str; - } - ); - - // compare. - // {{#compare 1 '===' 2}} Show if true {{/compare}} - // slightly changed https://gist.github.com/doginthehat/1890659. - $this->handlebars->registerHelper( - 'compare', - function( $lvalue, $operator, $rvalue = null, $options = null ) { - if ( null === $rvalue ) { - return $options['inverse'](); - } - - if ( null === $options ) { - $options = $rvalue; - $rvalue = $operator; - $operator = '==='; - } - - $result = false; - - switch ( $operator ) { - case '==': - // phpcs:ignore - $result = $lvalue == $rvalue; - break; - case '===': - $result = $lvalue === $rvalue; - break; - case '!=': - // phpcs:ignore - $result = $lvalue != $rvalue; - break; - case '!==': - $result = $lvalue !== $rvalue; - break; - case '<': - $result = $lvalue < $rvalue; - break; - case '>': - $result = $lvalue > $rvalue; - break; - case '<=': - $result = $lvalue <= $rvalue; - break; - case '>=': - $result = $lvalue >= $rvalue; - break; - case '&&': - $result = $lvalue && $rvalue; - break; - case '||': - $result = $lvalue || $rvalue; - break; - case 'typeof': - $result = gettype( $lvalue ) === $rvalue; - break; - } - - if ( $result ) { - return $options['fn'](); - } - - return $options['inverse'](); - } - ); - - // math. - // {{math 1 '+' 2}} - // https://stackoverflow.com/questions/33059203/error-missing-helper-in-handlebars-js/46317662#46317662. - $this->handlebars->registerHelper( - 'math', - function( $lvalue, $operator, $rvalue ) { - $result = ''; - - switch ( $operator ) { - case '+': - $result = $lvalue + $rvalue; - break; - case '-': - $result = $lvalue - $rvalue; - break; - case '*': - $result = $lvalue * $rvalue; - break; - case '/': - $result = $lvalue / $rvalue; - break; - case '%': - $result = $lvalue % $rvalue; - break; - } - - return $result; - } - ); - - // do_shortcode. - // {{{do_shortcode 'my_shortcode' this}}}. - $this->handlebars->registerHelper( - 'do_shortcode', - function( $shortcode_name, $attributes ) { - $result = '[' . $shortcode_name; - - // prepare attributes. - if ( isset( $attributes ) && ! empty( $attributes ) ) { - foreach ( $attributes as $name => $val ) { - if ( - 'content' === $name - || 'lazyblock_code_frontend_html' === $name - || 'lazyblock_code_backend_html' === $name - || 'data' === $name - || 'hash' === $name - ) { - continue; - } - - if ( is_array( $val ) ) { - $val = wp_json_encode( $val ); - } - - if ( - ! is_numeric( $val ) - && ! is_string( $val ) - && ! is_bool( $val ) - ) { - continue; - } - - if ( is_bool( $val ) ) { - $val = $val ? '1' : '0'; - } - - $result .= ' ' . esc_attr( $name ) . '="' . esc_attr( $val ) . '"'; - } - - // content. - if ( isset( $attributes['content'] ) ) { - $result .= ']' . $attributes['content'] . '[/' . $shortcode_name; - } - } - - $result .= ']'; - - return do_shortcode( $result ); - } - ); - - // date_i18n. - // {{date_i18n 'F j, Y H:i' '2018-09-16 15:35'}}. - $this->handlebars->registerHelper( - 'date_i18n', - function( $format, $time ) { - return date_i18n( $format, strtotime( $time ) ); - } - ); - - // var_dump. - // {{var_dump 'test'}}. - $this->handlebars->registerHelper( - 'var_dump', - function( $val ) { - ob_start(); - var_dump( $val ); - return ob_get_clean(); - } - ); - - // wp_get_attachment_image. - // {{wp_get_attachment_image 123 'thumbnail'}}. - $this->handlebars->registerHelper( - 'wp_get_attachment_image', - function( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) { - // Prevent options from being passed. - // Add default values if options are not set. - if ( ! isset( $size ) || $this->is_handlebars_options_attribute( $size ) ) { - $size = 'thumbnail'; - } - if ( ! isset( $icon ) || $this->is_handlebars_options_attribute( $icon ) ) { - $icon = false; - } - if ( ! isset( $attr ) || $this->is_handlebars_options_attribute( $attr ) ) { - $attr = ''; - } - - if ( is_array( $attachment_id ) && isset( $attachment_id['id'] ) ) { - if ( ! empty( $attachment_id['id'] ) ) { - return wp_get_attachment_image( $attachment_id['id'], $size, $icon, $attr ); - } elseif ( isset( $attachment_id['url'] ) ) { - return '' . esc_attr( $attr ) . ''; - } - } elseif ( is_numeric( $attachment_id ) || is_string( $attachment_id ) && ! empty( $attachment_id ) ) { - return wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); - } - - return ''; - } - ); - - // custom action for extending default helpers by 3rd-party. - do_action( 'lzb/handlebars/object', $this->handlebars ); - } - /** * Register CPT. */ @@ -1637,7 +1378,7 @@ public function render_callback( $attributes, $content = null, $context = 'front // Handlebars. } else { - $result = $this->handlebars->render( $code[ $custom_render_name ], $attributes ); + $result = lazyblocks()->handlebars()->object->render( $code[ $custom_render_name ], $attributes ); } } } diff --git a/classes/class-handlebars.php b/classes/class-handlebars.php new file mode 100644 index 00000000..9811533f --- /dev/null +++ b/classes/class-handlebars.php @@ -0,0 +1,279 @@ +plugin_path() . 'vendors/Handlebars/Autoloader.php'; + + Handlebars\Autoloader::register(); + + $this->object = new Handlebars\Handlebars(); + + // truncate + // {{truncate 'string' 2 'true'}}. + $this->object->registerHelper( + 'truncate', + function( $str, $len, $ellipsis = 'true' ) { + if ( $str && $len && mb_strlen( $str, 'UTF-8' ) > $len ) { + $new_str = mb_substr( $str, 0, $len + 1, 'UTF-8' ); + $count = mb_strlen( $new_str, 'UTF-8' ); + + while ( $count > 0 ) { + $ch = mb_substr( $new_str, -1, null, 'UTF-8' ); + $new_str = mb_substr( $new_str, 0, -1, 'UTF-8' ); + + $count--; + + if ( ' ' === $ch ) { + break; + } + } + + if ( '' === $new_str ) { + $new_str = mb_substr( $str, 0, $len, 'UTF-8' ); + } + + return new \Handlebars\SafeString( $new_str . ( 'true' === $ellipsis ? '...' : '' ) ); + } + return $str; + } + ); + + // compare. + // {{#compare 1 '===' 2}} Show if true {{/compare}} + // slightly changed https://gist.github.com/doginthehat/1890659. + $this->object->registerHelper( + 'compare', + function( $lvalue, $operator, $rvalue = null, $options = null ) { + if ( null === $rvalue ) { + return $options['inverse'](); + } + + if ( null === $options ) { + $options = $rvalue; + $rvalue = $operator; + $operator = '==='; + } + + $result = false; + + switch ( $operator ) { + case '==': + // phpcs:ignore + $result = $lvalue == $rvalue; + break; + case '===': + $result = $lvalue === $rvalue; + break; + case '!=': + // phpcs:ignore + $result = $lvalue != $rvalue; + break; + case '!==': + $result = $lvalue !== $rvalue; + break; + case '<': + $result = $lvalue < $rvalue; + break; + case '>': + $result = $lvalue > $rvalue; + break; + case '<=': + $result = $lvalue <= $rvalue; + break; + case '>=': + $result = $lvalue >= $rvalue; + break; + case '&&': + $result = $lvalue && $rvalue; + break; + case '||': + $result = $lvalue || $rvalue; + break; + case 'typeof': + $result = gettype( $lvalue ) === $rvalue; + break; + } + + if ( $result ) { + return $options['fn'](); + } + + return $options['inverse'](); + } + ); + + // math. + // {{math 1 '+' 2}} + // https://stackoverflow.com/questions/33059203/error-missing-helper-in-handlebars-js/46317662#46317662. + $this->object->registerHelper( + 'math', + function( $lvalue, $operator, $rvalue ) { + $result = ''; + + switch ( $operator ) { + case '+': + $result = $lvalue + $rvalue; + break; + case '-': + $result = $lvalue - $rvalue; + break; + case '*': + $result = $lvalue * $rvalue; + break; + case '/': + $result = $lvalue / $rvalue; + break; + case '%': + $result = $lvalue % $rvalue; + break; + } + + return $result; + } + ); + + // do_shortcode. + // {{{do_shortcode 'my_shortcode' this}}}. + $this->object->registerHelper( + 'do_shortcode', + function( $shortcode_name, $attributes ) { + $result = '[' . $shortcode_name; + + // prepare attributes. + if ( isset( $attributes ) && ! empty( $attributes ) ) { + foreach ( $attributes as $name => $val ) { + if ( + 'content' === $name + || 'lazyblock_code_frontend_html' === $name + || 'lazyblock_code_backend_html' === $name + || 'data' === $name + || 'hash' === $name + ) { + continue; + } + + if ( is_array( $val ) ) { + $val = wp_json_encode( $val ); + } + + if ( + ! is_numeric( $val ) + && ! is_string( $val ) + && ! is_bool( $val ) + ) { + continue; + } + + if ( is_bool( $val ) ) { + $val = $val ? '1' : '0'; + } + + $result .= ' ' . esc_attr( $name ) . '="' . esc_attr( $val ) . '"'; + } + + // content. + if ( isset( $attributes['content'] ) ) { + $result .= ']' . $attributes['content'] . '[/' . $shortcode_name; + } + } + + $result .= ']'; + + return do_shortcode( $result ); + } + ); + + // date_i18n. + // {{date_i18n 'F j, Y H:i' '2018-09-16 15:35'}}. + $this->object->registerHelper( + 'date_i18n', + function( $format, $time ) { + return date_i18n( $format, strtotime( $time ) ); + } + ); + + // var_dump. + // {{var_dump 'test'}}. + $this->object->registerHelper( + 'var_dump', + function( $val ) { + ob_start(); + var_dump( $val ); + return ob_get_clean(); + } + ); + + // wp_get_attachment_image. + // {{wp_get_attachment_image 123 'thumbnail'}}. + $this->object->registerHelper( + 'wp_get_attachment_image', + function( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) { + // Prevent options from being passed. + // Add default values if options are not set. + if ( ! isset( $size ) || $this->is_handlebars_options_attribute( $size ) ) { + $size = 'thumbnail'; + } + if ( ! isset( $icon ) || $this->is_handlebars_options_attribute( $icon ) ) { + $icon = false; + } + if ( ! isset( $attr ) || $this->is_handlebars_options_attribute( $attr ) ) { + $attr = ''; + } + + if ( is_array( $attachment_id ) && isset( $attachment_id['id'] ) ) { + if ( ! empty( $attachment_id['id'] ) ) { + return wp_get_attachment_image( $attachment_id['id'], $size, $icon, $attr ); + } elseif ( isset( $attachment_id['url'] ) ) { + return '' . esc_attr( $attr ) . ''; + } + } elseif ( is_numeric( $attachment_id ) || is_string( $attachment_id ) && ! empty( $attachment_id ) ) { + return wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); + } + + return ''; + } + ); + + // custom action for extending default helpers by 3rd-party. + do_action( 'lzb/handlebars/object', $this->object ); + } +} diff --git a/lazy-blocks.php b/lazy-blocks.php index 5b47671c..b266bc96 100644 --- a/lazy-blocks.php +++ b/lazy-blocks.php @@ -79,6 +79,13 @@ public static function instance() { */ private $controls; + /** + * Handlebars class object. + * + * @var LazyBlocks_Handlebars + */ + private $handlebars; + /** * Blocks class object. * @@ -130,11 +137,12 @@ public function init() { $this->load_text_domain(); $this->include_dependencies(); - $this->icons = new LazyBlocks_Icons(); - $this->controls = new LazyBlocks_Controls(); - $this->blocks = new LazyBlocks_Blocks(); - $this->templates = new LazyBlocks_Templates(); - $this->tools = new LazyBlocks_Tools(); + $this->icons = new LazyBlocks_Icons(); + $this->controls = new LazyBlocks_Controls(); + $this->handlebars = new LazyBlocks_Handlebars(); + $this->blocks = new LazyBlocks_Blocks(); + $this->templates = new LazyBlocks_Templates(); + $this->tools = new LazyBlocks_Tools(); add_action( 'init', array( $this, 'init_hook' ), 5 ); } @@ -228,6 +236,7 @@ private function include_dependencies() { require_once $this->plugin_path() . '/classes/class-admin.php'; require_once $this->plugin_path() . '/classes/class-icons.php'; require_once $this->plugin_path() . '/classes/class-controls.php'; + require_once $this->plugin_path() . '/classes/class-handlebars.php'; require_once $this->plugin_path() . '/classes/class-blocks.php'; require_once $this->plugin_path() . '/classes/class-templates.php'; require_once $this->plugin_path() . '/classes/class-tools.php'; @@ -254,6 +263,13 @@ public function controls() { return $this->controls; } + /** + * Get lazyblocks handlebars object. + */ + public function handlebars() { + return $this->handlebars; + } + /** * Get lazyblocks blocks object. */