s' => '\ThemeIsle\GutenbergBlocks\Render\Plugin_Card_Block',
'posts-grid' => '\ThemeIsle\GutenbergBlocks\Render\Posts_Grid_Block',
'review' => '\ThemeIsle\GutenbergBlocks\Render\Review_Block',
'sharing-icons' => '\ThemeIsle\GutenbergBlocks\Render\Sharing_Icons_Block',
'stripe-checkout' => '\ThemeIsle\GutenbergBlocks\Render\Stripe_Checkout_Block',
'form-multiple-choice' => '\ThemeIsle\GutenbergBlocks\Render\Form_Multiple_Choice_Block',
);
$dynamic_blocks = apply_filters( 'otter_blocks_register_dynamic_blocks', $dynamic_blocks );
self::$blocks = array(
'about-author',
'accordion',
'accordion-item',
'advanced-column',
'advanced-columns',
'advanced-heading',
'button',
'button-group',
'circle-counter',
'content-slider',
'countdown',
'flip',
'font-awesome-icons',
'form',
'form-captcha',
'form-input',
'form-nonce',
'form-textarea',
'form-multiple-choice',
'google-map',
'icon-list',
'icon-list-item',
'leaflet-map',
'lottie',
'plugin-cards',
'popup',
'modal',
'posts-grid',
'pricing',
'progress-bar',
'review',
'service',
'sharing-icons',
'slider',
'stripe-checkout',
'tabs',
'tabs-item',
'testimonials',
'timeline',
'timeline-item',
);
self::$blocks = apply_filters( 'otter_blocks_register_blocks', self::$blocks );
$this->enqueue_assets();
self::$block_dependencies = array(
'leaflet-map' => array( 'leaflet', 'leaflet-gesture-handling' ),
'slider' => array( 'glidejs-core', 'glidejs-theme' ),
);
$local_dependencies = array_merge(
self::$block_dependencies,
array(
'button-group' => array( 'font-awesome-5', 'font-awesome-4-shims' ),
'font-awesome-icons' => array( 'font-awesome-5', 'font-awesome-4-shims' ),
'icon-list-item' => array( 'font-awesome-5', 'font-awesome-4-shims' ),
'plugin-cards' => array( 'font-awesome-5', 'font-awesome-4-shims' ),
'timeline-item' => array( 'font-awesome-5', 'font-awesome-4-shims' ),
)
);
// Shared asset file (version/deps) for all blocks — read once, not per block.
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/blocks.asset.php';
foreach ( self::$blocks as $block ) {
$block_path = OTTER_BLOCKS_PATH . '/build/blocks/' . $block;
$editor_style = OTTER_BLOCKS_URL . 'build/blocks/' . $block . '/editor.css';
if ( ! file_exists( $block_path ) && defined( 'OTTER_PRO_BUILD_PATH' ) ) {
$block_path = OTTER_PRO_BUILD_PATH . $block;
$editor_style = OTTER_PRO_BUILD_URL . $block . '/editor.css';
}
$metadata_file = trailingslashit( $block_path ) . 'block.json';
$editor_style_path = trailingslashit( $block_path ) . 'editor.css';
$metadata = $this->get_metadata( $metadata_file );
if ( false === $metadata ) {
continue;
}
$deps = array();
if ( isset( $local_dependencies[ $block ] ) ) {
$deps = $local_dependencies[ $block ];
}
if ( file_exists( $editor_style_path ) && ! empty( $metadata['editorStyle'] ) ) {
wp_register_style(
$metadata['editorStyle'],
$editor_style,
$deps,
$asset_file['version']
);
}
$renderer = isset( $dynamic_blocks[ $block ] ) ? Loader::instantiate( $dynamic_blocks[ $block ] ) : null;
if ( null !== $renderer && method_exists( $renderer, 'render' ) ) {
register_block_type_from_metadata(
$metadata_file,
array(
'render_callback' => array( $renderer, 'render' ),
)
);
continue;
}
register_block_type_from_metadata( $metadata_file );
}
}
/**
* Initialize AMP blocks.
*
* @since 1.0.0
* @access public
*/
public function init_amp_blocks() {
$classnames = array(
'\ThemeIsle\GutenbergBlocks\Render\AMP\Circle_Counter_Block',
'\ThemeIsle\GutenbergBlocks\Render\AMP\Lottie_Block',
'\ThemeIsle\GutenbergBlocks\Render\AMP\Slider_Block',
);
foreach ( $classnames as $classname ) {
Loader::boot( $classname );
}
}
/**
* Subscribe to FA enqueue.
*
* @param string $block_content Block content parsed.
* @param array $block Block details.
*
* @return mixed
*/
public function subscribe_fa( $block_content, $block ) {
if ( ! isset( $block['blockName'] ) ) {
return $block_content;
}
if ( self::$is_fa_loaded ) {
return $block_content;
}
// always load for those.
static $always_load = [
'themeisle-blocks/plugin-cards' => true,
];
if ( isset( $always_load[ $block['blockName'] ] ) ) {
self::$is_fa_loaded = true;
return $block_content;
}
if ( 'themeisle-blocks/button' === $block['blockName'] ) {
if ( isset( $block['attrs']['library'] ) && 'themeisle-icons' === $block['attrs']['library'] ) {
return $block_content;
}
if ( isset( $block['attrs']['iconType'] ) ) {
self::$is_fa_loaded = true;
return $block_content;
}
}
if ( 'themeisle-blocks/font-awesome-icons' === $block['blockName'] ) {
if ( ! isset( $block['attrs']['library'] ) ) {
self::$is_fa_loaded = true;
return $block_content;
}
}
if ( 'themeisle-blocks/icon-list-item' === $block['blockName'] ) {
if ( ! isset( $block['attrs']['library'] ) ) {
self::$is_fa_loaded = true;
return $block_content;
}
if ( 'fontawesome' === $block['attrs']['library'] ) {
self::$is_fa_loaded = true;
return $block_content;
}
}
$has_navigation_block = \WP_Block_Type_Registry::get_instance()->is_registered( 'core/navigation' );
if ( $has_navigation_block && ( 'core/navigation-link' === $block['blockName'] || 'core/navigation-submenu' === $block['blockName'] ) ) {
if ( strpos( self::get_class_name( isset( $block['attrs'] ) ? $block['attrs'] : array() ), 'fa-' ) !== false ) {
self::$is_fa_loaded = true;
// See the src/blocks/plugins/menu-icons/inline.css file for where this comes from.
$styles = '.fab.wp-block-navigation-item,.far.wp-block-navigation-item,.fas.wp-block-navigation-item{-moz-osx-font-smoothing:inherit;-webkit-font-smoothing:inherit;font-weight:inherit;font-family: inherit}.fab.wp-block-navigation-item:before,.far.wp-block-navigation-item:before,.fas.wp-block-navigation-item:before{font-family:Font Awesome\ 5 Free;margin-right:5px}.fab.wp-block-navigation-item:before,.far.wp-block-navigation-item:before{font-weight:400;padding-right:5px}.fas.wp-block-navigation-item:before{font-weight:900;padding-right:5px}.fab.wp-block-navigation-item:before{font-family:Font Awesome\ 5 Brands}';
wp_add_inline_style( 'font-awesome-5', $styles );
return $block_content;
}
}
if ( 'themeisle-blocks/accordion' === $block['blockName'] ) {
if ( isset( $block['attrs']['icon'] ) || isset( $block['attrs']['openItemIcon'] ) ) {
self::$is_fa_loaded = true;
return $block_content;
}
}
if ( 'themeisle-blocks/timeline-item' === $block['blockName'] &&
( isset( $block['innerHTML'] ) && false !== strpos( $block['innerHTML'], 'fa-' ) )
) {
self::$is_fa_loaded = true;
return $block_content;
}
return $block_content;
}
/**
* Load assets in frontend.
*
* @param string $block_content Content of block.
* @param array $block Block Attributes.
* @return string
* @since 2.0.5
*/
public function load_sticky( $block_content, $block ) {
if ( ( did_action( 'parse_request' ) && function_exists( 'amp_is_request' ) && amp_is_request() ) || self::$scripts_loaded['sticky'] ) {
return $block_content;
}
if ( false !== strpos( self::get_class_name( isset( $block['attrs'] ) ? $block['attrs'] : array() ), 'o-sticky' ) ) {
$asset_file = include OTTER_BLOCKS_PATH . '/build/blocks/sticky.asset.php';
wp_enqueue_script(
'otter-sticky',
OTTER_BLOCKS_URL . 'build/blocks/sticky.js',
$asset_file['dependencies'],
$asset_file['version'],
true
);
wp_script_add_data( 'otter-sticky', 'defer', true );
add_action( 'wp_head', array( $this, 'sticky_style' ) );
self::$scripts_loaded['sticky'] = true;
}
return $block_content;
}
/**
* Get styles for sticky blocks.
*
* @static
* @since 2.0.14
* @access public
*/
public static function sticky_style() {
echo '';
}
/**
* Load the Hide on condition styles.
*
* @param string $block_content The block content.
* @param array $block The block data.
* @return string
*/
public function load_condition_hide_on_styles( $block_content, $block ) {
if ( self::$scripts_loaded['condition_hide_on'] ) {
return $block_content;
}
if ( empty( $block['attrs']['otterConditions'] ) || ! is_array( $block['attrs']['otterConditions'] ) ) {
return $block_content;
}
$has_condition = false;
foreach ( $block['attrs']['otterConditions'] as $group ) {
foreach ( $group as $condition ) {
if ( array_key_exists( 'type', $condition ) && 'screenSize' === $condition['type'] && isset( $condition['screen_sizes'] ) && is_array( $condition['screen_sizes'] ) ) {
$has_condition = true;
break;
}
}
if ( $has_condition ) {
break;
}
}
if ( ! $has_condition ) {
return $block_content;
}
add_action( 'wp_footer', array( $this, 'condition_hide_on_style' ) );
self::$scripts_loaded['condition_hide_on'] = true;
return $block_content;
}
/**
* Get the styles for Hide on condition.
*
* @static
* @since 2.4
* @access public
*/
public static function condition_hide_on_style() {
echo '';
}
/**
* Get the content of all active widgets.
*
* @return string
*/
public static function get_active_widgets_content() {
$content = '';
if ( 0 === count( self::$widget_used ) ) {
return $content;
}
$valid_widgets = array();
$widget_data = get_option( 'widget_block', array() );
foreach ( self::$widget_used as $widget_id ) {
$widget_id = str_replace( 'block-', '', $widget_id );
if ( isset( $widget_data[ $widget_id ] ) ) {
$valid_widgets[] = (object) $widget_data[ $widget_id ];
}
}
foreach ( $valid_widgets as $widget ) {
if ( isset( $widget->content ) ) {
$content .= $widget->content;
}
}
return $content;
}
/**
* Collect the IDs of every reusable block (synced pattern) in a block tree.
*
* Synced patterns can be nested inside other blocks, so the whole tree is
* walked instead of only its top level. Without this, a pattern placed inside
* a Group block is never found and the blocks it holds get none of their
* assets registered.
*
* @param array> $blocks Parsed blocks.
* @return array List of reusable block IDs.
* @since 3.2.3
* @access public
*/
public static function get_reusable_block_ids( $blocks ) {
$ids = array();
foreach ( $blocks as $block ) {
if ( 'core/block' === $block['blockName'] && isset( $block['attrs']['ref'] ) ) {
$ids[] = (int) $block['attrs']['ref'];
}
if ( ! empty( $block['innerBlocks'] ) ) {
$ids = array_merge( $ids, self::get_reusable_block_ids( $block['innerBlocks'] ) );
}
}
return array_values( array_unique( $ids ) );
}
/**
* Watch and save the used widgets.
*
* @param array $params The widget params.
* @return mixed
*/
public function watch_used_widgets( $params ) {
if ( isset( $params[0]['widget_id'] ) && ! in_array( $params[0]['widget_id'], self::$widget_used ) ) {
self::$widget_used[] = $params[0]['widget_id'];
}
return $params;
}
/**
* The instance method for the static class.
* Defines and returns the instance of the static class.
*
* @static
* @since 1.0.0
* @access public
* @return Registration
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
self::$instance->init();
}
return self::$instance;
}
/**
* Throw error on object clone
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @access public
* @since 1.0.0
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'otter-blocks' ), '1.0.0' );
}
/**
* Disable unserializing of the class
*
* @access public
* @since 1.0.0
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'otter-blocks' ), '1.0.0' );
}
}