tenberg_Fields_Transformer::instance()->prepare_blocks_fields( array_keys( self::$gutenberg_blocks ) ); } /** * @return array */ public static function gutenberg_sticky_fields() { if ( ! class_exists( 'BF_Fields_To_Gutenberg' ) ) { require BF_PATH . 'gutenberg/class-bf-fields-to-gutenberg.php'; } $fields = $stds = array(); apply_filters_ref_array( 'better-framework/gutenberg/sticky-fields', array( &$fields ) ); apply_filters_ref_array( 'better-framework/gutenberg/sticky-stds', array( &$stds ) ); if ( empty( $fields ) ) { return array(); } $converter = new BF_Fields_To_Gutenberg( $fields, $stds ); self::$additional_attributes = array_merge( $converter->list_attributes(), self::$additional_attributes ); return $converter->transform(); } /** * @return array */ public static function gutenberg_blocks_attributes() { if ( ! class_exists( 'BF_Gutenberg_Fields_Transformer' ) ) { require BF_PATH . 'gutenberg/class-bf-gutenberg-fields-transformer.php'; } $transformer = BF_Gutenberg_Fields_Transformer::instance(); if ( bf_is( 'dev' ) ) { // Don't use cache in development return $transformer->prepare_blocks_attributes( array_keys( self::$gutenberg_blocks ) ); } $blocks_attributes = []; $expiration = HOUR_IN_SECONDS * 6; foreach ( self::$gutenberg_blocks as $block_id => $options ) { $version = $options['_version'] ?? '1'; $cache_key = sprintf( '%s_%s', $block_id, $version ); $attributes = bf_cache_get( $cache_key, 'bf-block-attributes' ); $should_update_cache = $attributes === false; if ( $should_update_cache ) { $attributes = $transformer->block_attributes( $block_id ); } $blocks_attributes[ $block_id ] = $attributes; $should_update_cache && bf_cache_set( $cache_key, $attributes, 'bf-block-attributes', $expiration ); } return $blocks_attributes; } /** * @param string $shortcode the shortcode unique ID */ public static function register_block( $shortcode ) { if ( ! function_exists('register_block_type') ) { return; } $block_id = str_replace( '_', '-', $shortcode ); $block_id = strtolower( $block_id ); // uppercase letters are not allowed if ( ! self::$blocks_attributes ) { self::$blocks_attributes = self::gutenberg_blocks_attributes(); } if ( ! class_exists( 'BF_Gutenberg_Shortcode_Render' ) ) { require BF_PATH . 'gutenberg/class-bf-gutenberg-shortcode-render.php'; } $render_callback = "BF_Gutenberg_Shortcode_Render::$shortcode"; $attributes = isset( self::$blocks_attributes[ $shortcode ] ) ? self::$blocks_attributes[ $shortcode ] : array(); // if ( ! empty( $attributes ) ) { $attributes['className'] = array( 'type' => 'string' ); } $attributes['_updated'] = [ 'type' => 'integer' ]; $args = array_filter( compact( 'render_callback', 'attributes' ) ); register_block_type( "better-studio/$block_id", apply_filters( 'better-framework/gutenberg/block-type-args', $args, $shortcode ) ); } /** * @param array $categories * * @return array */ public function register_custom_categories( $categories ) { $pushed_categories = array(); foreach ( $categories as $category ) { $pushed_categories[] = $category['slug']; } foreach ( self::$gutenberg_blocks as $block ) { if ( empty( $block['category'] ) ) { continue; } $title = $block['category']; $slug = self::sanitize_gutenberg_category( $title ); if ( ! in_array( $slug, $pushed_categories ) ) { array_push( $categories, compact( 'slug', 'title' ) ); $pushed_categories[] = $slug; } } // Register default category if ( ! in_array( 'betterstudio', $pushed_categories ) ) { array_push( $categories, array( 'slug' => 'betterstudio', 'title' => __( 'BetterStudio', 'publisher' ), ) ); } return $categories; } /** * @param string $category * * @return string */ public static function sanitize_gutenberg_category( $category ) { $slug = sanitize_title_with_dashes( trim( $category ) ); if ( $slug === 'embeds' ) { return 'embed'; } return $slug; } public function early_load_all_shortcodes() { if ( bf_is_block_render_request() ) { BF_Shortcodes_Manager::init_shortcodes( true ); } } public function render_term_select_items( $items ) { if ( ! class_exists( 'BF_Term_Select' ) ) { require BF_PATH . '/core/field-generator/class-bf-term-select.php'; } $results = array(); foreach ( $items as $id => $item ) { if ( ! isset( $item['taxonomy'] ) ) { continue; } ob_start(); wp_list_categories( array( 'style' => 'list', 'title_li' => false, 'taxonomy' => $item['taxonomy'], 'walker' => new BF_Term_Select, 'selected_terms' => isset( $item['value'] ) ? $item['value'] : '', 'input_name' => isset( $item['input_name'] ) ? $item['input_name'] : 'bf-term-select', 'hide_empty' => false, ) ); $results[ $id ] = ob_get_contents(); ob_end_clean(); } wp_send_json_success( $results ); } public function gutenberg_additional_attributes() { } }