| 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 |
|
| 56 |
Style_Manager::instance(); |
| 57 |
|
| 58 |
$this->compiler = new Css_Compiler_Manager(); |
| 59 |
|
| 60 |
\WP_Block_Supports::get_instance()->register( |
| 61 |
self::SUPPORT_NAME, |
| 62 |
array( |
| 63 |
'register_attribute' => array( $this, 'register_support' ), |
| 64 |
'apply' => array( $this, 'apply_support' ), |
| 65 |
) |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
public function on_uninstall() { |
| 70 |
unset( $this->compiler ); |
| 71 |
|
| 72 |
\WP_Block_Supports::get_instance()->register( self::SUPPORT_NAME, array() ); |
| 73 |
} |
| 74 |
|
| 75 |
public function init_hooks() { |
| 76 |
add_action( |
| 77 |
'jet-form-builder/editor-assets/before', |
| 78 |
array( $this, 'register_scripts' ), |
| 79 |
0 |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
public function remove_hooks() { |
| 84 |
remove_action( |
| 85 |
'jet-form-builder/editor-assets/before', |
| 86 |
array( $this, 'register_scripts' ), |
| 87 |
0 |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
public function register_scripts() { |
| 92 |
$script_asset = require_once $this->get_dir( 'assets/build/editor.asset.php' ); |
| 93 |
|
| 94 |
if ( true === $script_asset ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
wp_enqueue_script( |
| 99 |
$this->get_handle(), |
| 100 |
$this->get_url( 'assets/build/editor.js' ), |
| 101 |
$script_asset['dependencies'], |
| 102 |
$script_asset['version'], |
| 103 |
true |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
public function register_support( \WP_Block_Type $block_type ) { |
| 108 |
// Setup attributes and styles within that if needed. |
| 109 |
if ( ! $block_type->attributes ) { |
| 110 |
$block_type->attributes = array(); |
| 111 |
} |
| 112 |
|
| 113 |
if ( block_has_support( $block_type, array( self::SUPPORT_NAME ) ) && |
| 114 |
! array_key_exists( 'style', $block_type->attributes ) |
| 115 |
) { |
| 116 |
$block_type->attributes['style'] = array( |
| 117 |
'type' => 'object', |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
public function apply_support( \WP_Block_Type $block_type, array $block_attributes ): array { |
| 123 |
|
| 124 |
if ( ! is_array( $block_type->supports ) ) { |
| 125 |
return array(); |
| 126 |
} |
| 127 |
|
| 128 |
$support_config = Array_Tools::get( $block_type->supports, array( self::SUPPORT_NAME ), false ); |
| 129 |
$root_styles = $block_attributes['style'] ?? array(); |
| 130 |
|
| 131 |
if ( |
| 132 |
! is_array( $support_config ) || |
| 133 |
empty( $root_styles ) |
| 134 |
) { |
| 135 |
return array(); |
| 136 |
} |
| 137 |
|
| 138 |
return $this->get_compiler()->compile( $root_styles, $support_config ); |
| 139 |
} |
| 140 |
|
| 141 |
public function get_compiler(): Css_Compiler_Manager { |
| 142 |
return $this->compiler; |
| 143 |
} |
| 144 |
} |
| 145 |
|