| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Variables; |
| 4 |
|
| 5 |
use Elementor\Modules\Variables\Adapters\Prop_Type_Adapter; |
| 6 |
use Elementor\Modules\Variables\Classes\Variable_Types_Registry; |
| 7 |
use Elementor\Modules\Variables\PropTypes\Color_Variable_Prop_Type; |
| 8 |
use Elementor\Modules\Variables\PropTypes\Font_Variable_Prop_Type; |
| 9 |
use Elementor\Modules\Variables\PropTypes\Size_Variable_Prop_Type; |
| 10 |
use Elementor\Modules\Variables\Services\Batch_Operations\Batch_Processor; |
| 11 |
use Elementor\Modules\Variables\Services\Variables_Service; |
| 12 |
use Elementor\Modules\Variables\Storage\Variables_Repository; |
| 13 |
use Elementor\Modules\Variables\Utils\Template_Library_Variables; |
| 14 |
use Elementor\Plugin; |
| 15 |
use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 16 |
use Elementor\Modules\Variables\Classes\CSS_Renderer as Variables_CSS_Renderer; |
| 17 |
use Elementor\Modules\Variables\Classes\Fonts; |
| 18 |
use Elementor\Modules\Variables\Classes\Rest_Api as Variables_API; |
| 19 |
use Elementor\Modules\Variables\Classes\Style_Schema; |
| 20 |
use Elementor\Modules\Variables\Classes\Size_Style_Schema; |
| 21 |
use Elementor\Modules\Variables\Classes\Style_Transformers; |
| 22 |
use Elementor\Modules\Variables\Classes\Variables; |
| 23 |
|
| 24 |
if ( ! defined( 'ABSPATH' ) ) { |
| 25 |
exit; |
| 26 |
} |
| 27 |
|
| 28 |
class Hooks { |
| 29 |
const PACKAGES = [ |
| 30 |
'editor-variables', |
| 31 |
]; |
| 32 |
|
| 33 |
public function register() { |
| 34 |
$this->register_styles_transformers() |
| 35 |
->register_css_renderer() |
| 36 |
->register_packages() |
| 37 |
->register_fonts() |
| 38 |
->register_api_endpoints() |
| 39 |
->filter_for_style_schema() |
| 40 |
->register_variable_types() |
| 41 |
->register_template_library_import(); |
| 42 |
|
| 43 |
return $this; |
| 44 |
} |
| 45 |
|
| 46 |
private function register_variable_types() { |
| 47 |
add_action( 'elementor/variables/register', function ( Variable_Types_Registry $registry ) { |
| 48 |
$registry->register( Color_Variable_Prop_Type::get_key(), new Color_Variable_Prop_Type() ); |
| 49 |
$registry->register( Font_Variable_Prop_Type::get_key(), new Font_Variable_Prop_Type() ); |
| 50 |
$registry->register( Prop_Type_Adapter::GLOBAL_CUSTOM_SIZE_VARIABLE_KEY, new Size_Variable_Prop_Type() ); |
| 51 |
$registry->register( Size_Variable_Prop_Type::get_key(), new Size_Variable_Prop_Type() ); |
| 52 |
} ); |
| 53 |
|
| 54 |
return $this; |
| 55 |
} |
| 56 |
|
| 57 |
private function register_packages() { |
| 58 |
add_filter( 'elementor/editor/v2/packages', function ( $packages ) { |
| 59 |
return array_merge( $packages, self::PACKAGES ); |
| 60 |
} ); |
| 61 |
|
| 62 |
return $this; |
| 63 |
} |
| 64 |
|
| 65 |
private function register_styles_transformers() { |
| 66 |
add_action( 'elementor/atomic-widgets/styles/transformers/register', function ( $registry ) { |
| 67 |
Variables::init( $this->variables_service() ); |
| 68 |
( new Style_Transformers() )->append_to( $registry ); |
| 69 |
} ); |
| 70 |
|
| 71 |
return $this; |
| 72 |
} |
| 73 |
|
| 74 |
private function filter_for_style_schema() { |
| 75 |
add_filter( 'elementor/atomic-widgets/styles/schema', function ( array $schema ) { |
| 76 |
return ( new Style_Schema() )->augment( $schema ); |
| 77 |
} ); |
| 78 |
|
| 79 |
add_filter( 'elementor/atomic-widgets/styles/schema', function ( array $schema ) { |
| 80 |
return ( new Size_Style_Schema() )->augment( $schema ); |
| 81 |
} ); |
| 82 |
|
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
private function css_renderer() { |
| 87 |
return new Variables_CSS_Renderer( $this->variables_service() ); |
| 88 |
} |
| 89 |
|
| 90 |
private function register_css_renderer() { |
| 91 |
add_action( 'elementor/css-file/post/parse', function ( Post_CSS $post_css ) { |
| 92 |
if ( ! Plugin::$instance->kits_manager->is_kit( $post_css->get_post_id() ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
$post_css->get_stylesheet()->add_raw_css( |
| 97 |
$this->css_renderer()->raw_css() |
| 98 |
); |
| 99 |
} ); |
| 100 |
|
| 101 |
return $this; |
| 102 |
} |
| 103 |
|
| 104 |
private function fonts() { |
| 105 |
return new Fonts( $this->variables_service() ); |
| 106 |
} |
| 107 |
|
| 108 |
private function register_fonts() { |
| 109 |
add_action( 'elementor/css-file/post/parse', function ( $post_css ) { |
| 110 |
$this->fonts()->append_to( $post_css ); |
| 111 |
} ); |
| 112 |
|
| 113 |
return $this; |
| 114 |
} |
| 115 |
|
| 116 |
private function rest_api() { |
| 117 |
return new Variables_API( $this->variables_service() ); |
| 118 |
} |
| 119 |
|
| 120 |
private function register_api_endpoints() { |
| 121 |
add_action( 'rest_api_init', function () { |
| 122 |
$this->rest_api()->register_routes(); |
| 123 |
} ); |
| 124 |
|
| 125 |
return $this; |
| 126 |
} |
| 127 |
|
| 128 |
private function variables_service() { |
| 129 |
$repository = new Variables_Repository( |
| 130 |
Plugin::$instance->kits_manager->get_active_kit() |
| 131 |
); |
| 132 |
|
| 133 |
return new Variables_Service( $repository, new Batch_Processor() ); |
| 134 |
} |
| 135 |
|
| 136 |
private function register_template_library_import() { |
| 137 |
add_filter( |
| 138 |
'elementor/template_library/export/build_snapshots', |
| 139 |
[ Template_Library_Variables::class, 'add_variables_snapshot' ], |
| 140 |
20, |
| 141 |
4 |
| 142 |
); |
| 143 |
|
| 144 |
add_filter( |
| 145 |
'elementor/template_library/get_data/extract_snapshots', |
| 146 |
[ Template_Library_Variables::class, 'extract_variables_from_data' ], |
| 147 |
10, |
| 148 |
3 |
| 149 |
); |
| 150 |
|
| 151 |
add_filter( |
| 152 |
'elementor/template_library/import/process_content', |
| 153 |
[ Template_Library_Variables::class, 'process_variables_import' ], |
| 154 |
10, |
| 155 |
3 |
| 156 |
); |
| 157 |
|
| 158 |
add_filter( |
| 159 |
'elementor/global_classes/import/transform_snapshot', |
| 160 |
[ Template_Library_Variables::class, 'transform_variables_in_classes_snapshot' ], |
| 161 |
10, |
| 162 |
4 |
| 163 |
); |
| 164 |
|
| 165 |
return $this; |
| 166 |
} |
| 167 |
} |
| 168 |
|