name = $name; $this->description = __( 'A set of awesome Gutenberg Blocks!', 'themeisle-companion' ); } /** * Method to define hooks needed. * * @since 1.0.0 * @access public */ public function init() { if ( ! defined( 'THEMEISLE_BLOCKS_VERSION' ) ) { define( 'THEMEISLE_BLOCKS_VERSION', '1.6.1' ); define( 'THEMEISLE_BLOCKS_DEV', false ); } if ( THEMEISLE_BLOCKS_DEV ) { self::$assets_version = time(); } else { self::$assets_version = THEMEISLE_BLOCKS_VERSION; } $allow_json = get_option( 'themeisle_allow_json_upload' ); add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_frontend_assets' ) ); add_action( 'init', array( $this, 'autoload_classes' ), 11 ); add_action( 'init', array( $this, 'load_server_side_blocks' ), 11 ); add_action( 'block_categories', array( $this, 'block_categories' ) ); add_filter( 'render_block', array( $this, 'render_amp' ), 10, 3 ); if ( isset( $allow_json ) && true === (bool) $allow_json && ! function_exists( 'is_wpcom_vip' ) ) { add_filter( 'upload_mimes', array( $this, 'allow_json' ) ); //phpcs:ignore WordPressVIPMinimum.Filters.RestrictedHook.UploadMimes add_filter( 'wp_check_filetype_and_ext', array( $this, 'fix_mime_type_json' ), 75, 4 ); } } /** * Load Gutenberg blocks. * * @since 1.0.0 * @access public */ public function enqueue_block_editor_assets() { if ( defined( 'THEMEISLE_GUTENBERG_GOOGLE_MAPS_API' ) ) { $api = THEMEISLE_GUTENBERG_GOOGLE_MAPS_API; } else { $api = false; } wp_enqueue_script( 'themeisle-gutenberg-blocks-vendor', plugin_dir_url( $this->get_dir() ) . 'build/chunk-vendor.js', array( 'react', 'react-dom' ), self::$assets_version, true ); wp_enqueue_script( 'themeisle-gutenberg-blocks', plugin_dir_url( $this->get_dir() ) . 'build/blocks.js', array( 'lodash', 'wp-api', 'wp-i18n', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-editor', 'wp-edit-post', 'wp-element', 'wp-keycodes', 'wp-plugins', 'wp-primitives', 'wp-rich-text', 'wp-server-side-render', 'wp-url', 'wp-viewport', 'themeisle-gutenberg-blocks-vendor', 'glidejs', 'lottie-player' ), self::$assets_version, true ); wp_enqueue_script( 'glidejs', plugin_dir_url( $this->get_dir() ) . 'assets/glide/glide.min.js', [], self::$assets_version, true ); wp_enqueue_script( 'lottie-player', plugin_dir_url( $this->get_dir() ) . 'assets/lottie/lottie-player.min.js', [], self::$assets_version, true ); wp_set_script_translations( 'themeisle-gutenberg-blocks', 'textdomain' ); wp_localize_script( 'themeisle-gutenberg-blocks', 'themeisleGutenberg', array( 'isCompatible' => $this->is_compatible(), 'packagePath' => plugin_dir_url( $this->get_dir() ) . 'build/', 'assetsPath' => plugin_dir_url( $this->get_dir() ) . 'assets', 'updatePath' => admin_url( 'update-core.php' ), 'optionsPath' => admin_url( 'options-general.php?page=otter' ), 'mapsAPI' => $api, 'themeDefaults' => $this->get_global_defaults(), 'isWPVIP' => function_exists( 'is_wpcom_vip' ), 'canTrack' => 'yes' === get_option( 'otter_blocks_logger_flag', false ) ? true : false, ) ); wp_enqueue_style( 'themeisle-gutenberg-blocks-editor', plugin_dir_url( $this->get_dir() ) . 'build/editor.css', array( 'wp-edit-blocks' ), self::$assets_version ); wp_enqueue_style( 'glidejs-core', plugin_dir_url( $this->get_dir() ) . 'assets/glide/glide.core.min.css', [], self::$assets_version ); wp_enqueue_style( 'glidejs-theme', plugin_dir_url( $this->get_dir() ) . 'assets/glide/glide.theme.min.css', [], self::$assets_version ); wp_enqueue_script( 'themeisle-gutenberg-map-block', plugin_dir_url( $this->get_dir() ) . 'assets/leaflet/leaflet.js', array( 'wp-dom-ready' ), self::$assets_version, true ); wp_enqueue_style( 'leaflet-theme', plugin_dir_url( $this->get_dir() ) . 'assets/leaflet/leaflet.css', [], self::$assets_version ); wp_enqueue_script( 'themeisle-gutenberg-map-block-gesture', plugin_dir_url( $this->get_dir() ) . 'assets/leaflet/leaflet-gesture-handling.min.js', array( 'wp-dom-ready' ), self::$assets_version, true ); wp_enqueue_style( 'leaflet-theme-gesture', plugin_dir_url( $this->get_dir() ) . 'assets/leaflet/leaflet-gesture-handling.min.css', [], self::$assets_version ); } /** * Loop through block content to find specified blocks. * * @param array $blocks Parsed array of blocks. * @param string $block_name name of the block. * @param array $target Target variable. */ public function loop_blocks( $blocks, $block_name, $target = array() ) { if ( is_array( $block_name ) ) { foreach ( $block_name as $name ) { $target = $this->loop_blocks( $blocks, $name, $target ); } } else { foreach ( $blocks as $block ) { if ( $block_name === $block['blockName'] ) { array_push( $target, $block ); } if ( count( $block['innerBlocks'] ) ) { $target = $this->loop_blocks( $block['innerBlocks'], $block_name, $target ); } } } return $target; } /** * Handler which checks the blocks used and enqueue the assets which needs. * * @param null $post Current post. */ public function enqueue_dependencies( $post = null ) { $content = get_the_content( $post ); if ( strpos( $content, '