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