core
4 years ago
editor-rtl.css
7 months ago
editor.css
7 months ago
index.asset.php
1 year ago
index.js
1 year ago
index.php
1 year ago
manifest.php
1 week ago
style-rtl.css
7 months ago
style.css
7 months ago
index.php
217 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core; |
| 4 | |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | use Kubio\Core\StyleManager\StyleManager; |
| 7 | use Kubio\Core\StyleManager\StyleRender; |
| 8 | |
| 9 | |
| 10 | class ThirdPartySupportRegistry { |
| 11 | const KUBIO_STYLE_SUPPORT = 'kubio-style'; |
| 12 | private static $instance; |
| 13 | private $supported_blocks = array(); |
| 14 | |
| 15 | public function __construct() { |
| 16 | add_filter( 'register_block_type_args', array( $this, 'addBlockAttrsAndStyleSupport' ), 10, 2 ); |
| 17 | add_filter( 'plugins_loaded', array( $this, 'loadSupportedBlocks' ) ); |
| 18 | add_action( 'enqueue_block_editor_assets', array( $this, 'loadEditorAssets' ) ); |
| 19 | add_action( 'wp_enqueue_scripts', array( $this, 'loadFrontendAssets' ) ); |
| 20 | $this->registerKubioStyleSupport(); |
| 21 | } |
| 22 | |
| 23 | private function registerKubioStyleSupport() { |
| 24 | \WP_Block_Supports::get_instance()->register( |
| 25 | ThirdPartySupportRegistry::KUBIO_STYLE_SUPPORT, |
| 26 | array( |
| 27 | 'apply' => array( $this, 'applyKubioStyleSupport' ), |
| 28 | ) |
| 29 | ); |
| 30 | |
| 31 | add_filter( 'pre_render_block', array( $this, 'apply_style_on_renderless_blocks' ), 3, 10 ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * This method is meant to be used in `pre_render_block` where we need to apply our Kubio style on blocks that |
| 36 | * don't have a render in php. |
| 37 | * |
| 38 | * @param $null |
| 39 | * @param $parsed_block |
| 40 | */ |
| 41 | function apply_style_on_renderless_blocks( $pre_render, $parsed_block ) { |
| 42 | foreach ( $parsed_block as $key => $block ) { |
| 43 | if ( ! is_array( $block ) || empty( $block ) ) { |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | if ( empty( $block['blockName'] ) ) { |
| 48 | $this->apply_style_on_renderless_blocks( null, $block ); |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | $block_type = \WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
| 53 | |
| 54 | if ( ! isset( $block_type->render_callback ) && is_array( $block ) ) { |
| 55 | $this->applyKubioStyleSupport( $block_type, Arr::get( $block, 'attrs', array() ) ); |
| 56 | } |
| 57 | |
| 58 | if ( empty( $block['innerBlocks'] ) ) { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | $this->apply_style_on_renderless_blocks( null, $block['innerBlocks'] ); |
| 63 | } |
| 64 | |
| 65 | return $pre_render; |
| 66 | } |
| 67 | |
| 68 | public static function load() { |
| 69 | static::getInstance(); |
| 70 | } |
| 71 | |
| 72 | public static function getInstance() { |
| 73 | if ( ! static::$instance ) { |
| 74 | static::$instance = new ThirdPartySupportRegistry(); |
| 75 | } |
| 76 | |
| 77 | return static::$instance; |
| 78 | } |
| 79 | |
| 80 | public function loadEditorAssets() { |
| 81 | $this->loadFrontendAssets(); |
| 82 | wp_enqueue_style( 'kubio-third-party-blocks-editor' ); |
| 83 | } |
| 84 | |
| 85 | public function loadFrontendAssets() { |
| 86 | wp_enqueue_style( 'kubio-third-party-blocks' ); |
| 87 | } |
| 88 | |
| 89 | public function applyKubioStyleSupport( $block_type, $block_attributes ) { |
| 90 | if ( ! block_has_support( $block_type, array( ThirdPartySupportRegistry::KUBIO_STYLE_SUPPORT ), false ) ) { |
| 91 | return array(); |
| 92 | } |
| 93 | |
| 94 | if ( $this->isRestRerender() ) { |
| 95 | return array(); |
| 96 | } |
| 97 | |
| 98 | $name = $block_type->name; |
| 99 | $kubio_support = $this->supported_blocks[ $name ]; |
| 100 | |
| 101 | $main_attr = Arr::get( $block_attributes, 'kubio', null ); |
| 102 | $style_ref = Arr::get( $main_attr, 'styleRef', '' ); |
| 103 | $style_id = Arr::get( $main_attr, 'id', '' ); |
| 104 | |
| 105 | $elements_by_name = Arr::get( $kubio_support, 'elementsByName', array() ); |
| 106 | $elements_enum = Arr::get( $kubio_support, 'elementsEnum', array() ); |
| 107 | |
| 108 | $wrapper_element = ''; |
| 109 | foreach ( $elements_by_name as $elementName => $props ) { |
| 110 | if ( Arr::get( $props, 'wrapper', false ) ) { |
| 111 | $wrapper_element = $elementName; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | $style_prefix = apply_filters( 'kubio/element-style-class-prefix', 'style-' ); |
| 117 | $classes = array( |
| 118 | "{$style_prefix}{$style_ref}-{$wrapper_element}", |
| 119 | "{$style_prefix}local-{$style_id}-{$wrapper_element}", |
| 120 | 'wp-block-kubio-' . str_replace( '/', '-', $name ) . '__' . $wrapper_element, // bem class |
| 121 | ); |
| 122 | |
| 123 | // don't register style on rest requests ( reduce the load time ) |
| 124 | $normalized = StyleRender::normalizeData( $main_attr, $elements_by_name, $elements_enum ); |
| 125 | |
| 126 | $style_render = new StyleRender( |
| 127 | array( |
| 128 | 'styledElementsByName' => Arr::get( $normalized, 'styledElementsByName', array() ), |
| 129 | 'styledElementsEnum' => Arr::get( $normalized, 'styledElementsEnum', array() ), |
| 130 | 'wrapperElement' => $wrapper_element, |
| 131 | 'prefixParents' => array(), |
| 132 | 'useParentPrefix' => false, |
| 133 | 'model' => (object) Arr::get( $normalized, 'model', array() ), |
| 134 | ) |
| 135 | ); |
| 136 | |
| 137 | $styleByType = $style_render->export(); |
| 138 | StyleManager::getInstance()->registerBlockStyle( $styleByType ); |
| 139 | |
| 140 | $hidden = array( |
| 141 | 'desktop' => Arr::get( $main_attr, 'props.isHidden', false ), |
| 142 | 'tablet' => Arr::get( $main_attr, 'props.media.tablet.isHidden', false ), |
| 143 | 'mobile' => Arr::get( $main_attr, 'props.media.mobile.isHidden', false ), |
| 144 | ); |
| 145 | |
| 146 | foreach ( $hidden as $media => $is_hidden ) { |
| 147 | if ( $is_hidden ) { |
| 148 | $classes[] = "kubio-hide-on-{$media}"; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return array( |
| 153 | 'class' => implode( ' ', $classes ), |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | private function isRestRerender() { |
| 158 | if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 159 | /** @var $wp \WP */ |
| 160 | global $wp; |
| 161 | $route = (string) Arr::get( $wp->query_vars, 'rest_route', '' ); |
| 162 | |
| 163 | if ( strpos( $route, '/block-renderer/' ) !== false ) { |
| 164 | return true; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | public function loadSupportedBlocks() { |
| 172 | $manifest_file = __DIR__ . '/manifest.php'; // manifest file is autogenerated by webpack at build; |
| 173 | if ( file_exists( $manifest_file ) ) { |
| 174 | $supported_blocks = require_once $manifest_file; |
| 175 | |
| 176 | foreach ( $supported_blocks as $meta_file ) { |
| 177 | if ( file_exists( __DIR__ . '/' . $meta_file ) ) { |
| 178 | $settings = json_decode( file_get_contents( __DIR__ . '/' . $meta_file ), true ); |
| 179 | |
| 180 | if ( is_array( $settings ) ) { |
| 181 | $name = Arr::get( $settings, 'name', null ); |
| 182 | $kubio_support = Arr::get( $settings, 'kubioSupport', array() ); |
| 183 | $this->supported_blocks[ $name ] = $kubio_support; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | $this->supported_blocks = apply_filters( 'kubio/third-party-style/register-support', $this->supported_blocks ); |
| 190 | } |
| 191 | |
| 192 | public function addBlockAttrsAndStyleSupport( $args, $block_name ) { |
| 193 | |
| 194 | $has_support = ! ! Arr::get( $this->supported_blocks, $block_name, null ); |
| 195 | |
| 196 | if ( $has_support ) { |
| 197 | |
| 198 | // register the kubio attribute for supported blocks |
| 199 | $attributes = Arr::get( $args, 'attributes', array() ); |
| 200 | $attributes = is_array( $attributes ) ? $attributes : array(); |
| 201 | $attributes['kubio'] = array( 'type' => 'object' ); |
| 202 | |
| 203 | // enable the kubio-style support to add the block classes and rennder the style |
| 204 | $supports = Arr::get( $args, 'supports', array() ); |
| 205 | $supports = is_array( $supports ) ? $supports : array(); |
| 206 | $supports[ ThirdPartySupportRegistry::KUBIO_STYLE_SUPPORT ] = true; |
| 207 | |
| 208 | Arr::set( $args, 'attributes', $attributes ); |
| 209 | Arr::set( $args, 'supports', $supports ); |
| 210 | } |
| 211 | |
| 212 | return $args; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | ThirdPartySupportRegistry::load(); |
| 217 |