assets
2 years ago
css-compilers
2 years ago
css-compiler-manager.php
2 years ago
module.php
1 year ago
module.php
142 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Jet_Style; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 7 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 8 | use JFB_Components\Module\Base_Module_Dir_It; |
| 9 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 10 | use JFB_Components\Module\Base_Module_Handle_It; |
| 11 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 12 | use JFB_Components\Module\Base_Module_It; |
| 13 | use JFB_Components\Module\Base_Module_Url_It; |
| 14 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 15 | |
| 16 | // If this file is called directly, abort. |
| 17 | if ( ! defined( 'WPINC' ) ) { |
| 18 | die; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @since 3.1.0 |
| 23 | * |
| 24 | * Class Module |
| 25 | * @package JFB_Modules\Jet_Style |
| 26 | */ |
| 27 | class Module implements |
| 28 | Base_Module_It, |
| 29 | Base_Module_Handle_It, |
| 30 | Base_Module_Url_It, |
| 31 | Base_Module_After_Install_It, |
| 32 | Base_Module_Dir_It { |
| 33 | |
| 34 | use Base_Module_Url_Trait; |
| 35 | use Base_Module_Handle_Trait; |
| 36 | use Base_Module_Dir_Trait; |
| 37 | |
| 38 | const SUPPORT_NAME = 'jetStyle'; |
| 39 | |
| 40 | /** |
| 41 | * @var Css_Compiler_Manager |
| 42 | */ |
| 43 | private $compiler; |
| 44 | |
| 45 | public function rep_item_id() { |
| 46 | return 'jet-style'; |
| 47 | } |
| 48 | |
| 49 | public function condition(): bool { |
| 50 | // since WP 6.2 |
| 51 | return class_exists( '\WP_HTML_Tag_Processor' ); |
| 52 | } |
| 53 | |
| 54 | public function on_install() { |
| 55 | $this->compiler = new Css_Compiler_Manager(); |
| 56 | |
| 57 | \WP_Block_Supports::get_instance()->register( |
| 58 | self::SUPPORT_NAME, |
| 59 | array( |
| 60 | 'register_attribute' => array( $this, 'register_support' ), |
| 61 | 'apply' => array( $this, 'apply_support' ), |
| 62 | ) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | public function on_uninstall() { |
| 67 | unset( $this->compiler ); |
| 68 | |
| 69 | \WP_Block_Supports::get_instance()->register( self::SUPPORT_NAME, array() ); |
| 70 | } |
| 71 | |
| 72 | public function init_hooks() { |
| 73 | add_action( |
| 74 | 'jet-form-builder/editor-assets/before', |
| 75 | array( $this, 'register_scripts' ), |
| 76 | 0 |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | public function remove_hooks() { |
| 81 | remove_action( |
| 82 | 'jet-form-builder/editor-assets/before', |
| 83 | array( $this, 'register_scripts' ), |
| 84 | 0 |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | public function register_scripts() { |
| 89 | $script_asset = require_once $this->get_dir( 'assets/build/editor.asset.php' ); |
| 90 | |
| 91 | if ( true === $script_asset ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | wp_enqueue_script( |
| 96 | $this->get_handle(), |
| 97 | $this->get_url( 'assets/build/editor.js' ), |
| 98 | $script_asset['dependencies'], |
| 99 | $script_asset['version'], |
| 100 | true |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | public function register_support( \WP_Block_Type $block_type ) { |
| 105 | // Setup attributes and styles within that if needed. |
| 106 | if ( ! $block_type->attributes ) { |
| 107 | $block_type->attributes = array(); |
| 108 | } |
| 109 | |
| 110 | if ( block_has_support( $block_type, array( self::SUPPORT_NAME ) ) && |
| 111 | ! array_key_exists( 'style', $block_type->attributes ) |
| 112 | ) { |
| 113 | $block_type->attributes['style'] = array( |
| 114 | 'type' => 'object', |
| 115 | ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public function apply_support( \WP_Block_Type $block_type, array $block_attributes ): array { |
| 120 | |
| 121 | if ( ! is_array( $block_type->supports ) ) { |
| 122 | return array(); |
| 123 | } |
| 124 | |
| 125 | $support_config = Array_Tools::get( $block_type->supports, array( self::SUPPORT_NAME ), false ); |
| 126 | $root_styles = $block_attributes['style'] ?? array(); |
| 127 | |
| 128 | if ( |
| 129 | ! is_array( $support_config ) || |
| 130 | empty( $root_styles ) |
| 131 | ) { |
| 132 | return array(); |
| 133 | } |
| 134 | |
| 135 | return $this->get_compiler()->compile( $root_styles, $support_config ); |
| 136 | } |
| 137 | |
| 138 | public function get_compiler(): Css_Compiler_Manager { |
| 139 | return $this->compiler; |
| 140 | } |
| 141 | } |
| 142 |