| @@ -1,29 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\Variables; |
| 4 | 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 | 5 | use Elementor\Plugin; |
| 15 | 6 | use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 16 | 7 | use Elementor\Modules\Variables\Classes\CSS_Renderer as Variables_CSS_Renderer; |
| 17 | 8 | use Elementor\Modules\Variables\Classes\Fonts; |
| 18 | 9 | use Elementor\Modules\Variables\Classes\Rest_Api as Variables_API; |
| 10 | +use Elementor\Modules\Variables\Classes\Variables; | |
| 11 | +use Elementor\Modules\Variables\Storage\Repository as Variables_Repository; | |
| 19 | 12 | use Elementor\Modules\Variables\Classes\Style_Schema; |
| 20 | -use Elementor\Modules\Variables\Classes\Size_Style_Schema; | |
| 21 | 13 | use Elementor\Modules\Variables\Classes\Style_Transformers; |
| 22 | -use Elementor\Modules\Variables\Classes\Variables; | |
| 23 | 14 | |
| 24 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 25 | - exit; | |
| 16 | + exit; // Exit if accessed directly. | |
| 26 | 17 | } |
| 27 | 18 | |
| 28 | 19 | class Hooks { |
| 29 | 20 | const PACKAGES = [ |
| @@ -31,25 +22,31 @@ | ||
| 31 | 22 | ]; |
| 32 | 23 | |
| 33 | 24 | public function register() { |
| 34 | 25 | $this->register_styles_transformers() |
| 26 | + ->register_packages() | |
| 27 | + ->filter_for_style_schema() | |
| 35 | 28 | ->register_css_renderer() |
| 36 | - ->register_packages() | |
| 37 | 29 | ->register_fonts() |
| 38 | - ->register_api_endpoints() | |
| 39 | - ->filter_for_style_schema() | |
| 40 | - ->register_variable_types() | |
| 41 | - ->register_template_library_import(); | |
| 30 | + ->register_api_endpoints(); | |
| 42 | 31 | |
| 32 | + // TODO: Remove this, later, temporary solution | |
| 33 | + $this->filter_for_stored_variables(); | |
| 34 | + | |
| 43 | 35 | return $this; |
| 44 | 36 | } |
| 45 | 37 | |
| 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() ); | |
| 38 | + private function filter_for_stored_variables() { | |
| 39 | + add_filter( Variables::FILTER, function ( $variables ) { | |
| 40 | + $db_record = ( new Variables_Repository( | |
| 41 | + Plugin::$instance->kits_manager->get_active_kit() | |
| 42 | + ) )->load(); | |
| 43 | + | |
| 44 | + foreach ( $db_record['data'] as $id => $variable ) { | |
| 45 | + $variables[ $id ] = $variable; | |
| 46 | + } | |
| 47 | + | |
| 48 | + return $variables; | |
| 52 | 49 | } ); |
| 53 | 50 | |
| 54 | 51 | return $this; |
| 55 | 52 | } |
| @@ -63,9 +60,8 @@ | ||
| 63 | 60 | } |
| 64 | 61 | |
| 65 | 62 | private function register_styles_transformers() { |
| 66 | 63 | add_action( 'elementor/atomic-widgets/styles/transformers/register', function ( $registry ) { |
| 67 | - Variables::init( $this->variables_service() ); | |
| 68 | 64 | ( new Style_Transformers() )->append_to( $registry ); |
| 69 | 65 | } ); |
| 70 | 66 | |
| 71 | 67 | return $this; |
| @@ -75,19 +71,11 @@ | ||
| 75 | 71 | add_filter( 'elementor/atomic-widgets/styles/schema', function ( array $schema ) { |
| 76 | 72 | return ( new Style_Schema() )->augment( $schema ); |
| 77 | 73 | } ); |
| 78 | 74 | |
| 79 | - add_filter( 'elementor/atomic-widgets/styles/schema', function ( array $schema ) { | |
| 80 | - return ( new Size_Style_Schema() )->augment( $schema ); | |
| 81 | - } ); | |
| 82 | - | |
| 83 | 75 | return $this; |
| 84 | 76 | } |
| 85 | 77 | |
| 86 | - private function css_renderer() { | |
| 87 | - return new Variables_CSS_Renderer( $this->variables_service() ); | |
| 88 | - } | |
| 89 | - | |
| 90 | 78 | private function register_css_renderer() { |
| 91 | 79 | add_action( 'elementor/css-file/post/parse', function ( Post_CSS $post_css ) { |
| 92 | 80 | if ( ! Plugin::$instance->kits_manager->is_kit( $post_css->get_post_id() ) ) { |
| 93 | 81 | return; |
| @@ -93,9 +81,9 @@ | ||
| 93 | 81 | return; |
| 94 | 82 | } |
| 95 | 83 | |
| 96 | 84 | $post_css->get_stylesheet()->add_raw_css( |
| 97 | - $this->css_renderer()->raw_css() | |
| 85 | + ( new Variables_CSS_Renderer( new Variables() ) )->raw_css() | |
| 98 | 86 | ); |
| 99 | 87 | } ); |
| 100 | 88 | |
| 101 | 89 | return $this; |
| @@ -100,15 +88,11 @@ | ||
| 100 | 88 | |
| 101 | 89 | return $this; |
| 102 | 90 | } |
| 103 | 91 | |
| 104 | - private function fonts() { | |
| 105 | - return new Fonts( $this->variables_service() ); | |
| 106 | - } | |
| 107 | - | |
| 108 | 92 | private function register_fonts() { |
| 109 | 93 | add_action( 'elementor/css-file/post/parse', function ( $post_css ) { |
| 110 | - $this->fonts()->append_to( $post_css ); | |
| 94 | + ( new Fonts() )->append_to( $post_css ); | |
| 111 | 95 | } ); |
| 112 | 96 | |
| 113 | 97 | return $this; |
| 114 | 98 | } |
| @@ -113,9 +97,13 @@ | ||
| 113 | 97 | return $this; |
| 114 | 98 | } |
| 115 | 99 | |
| 116 | 100 | private function rest_api() { |
| 117 | - return new Variables_API( $this->variables_service() ); | |
| 101 | + return new Variables_API( | |
| 102 | + new Variables_Repository( | |
| 103 | + Plugin::$instance->kits_manager->get_active_kit() | |
| 104 | + ) | |
| 105 | + ); | |
| 118 | 106 | } |
| 119 | 107 | |
| 120 | 108 | private function register_api_endpoints() { |
| 121 | 109 | add_action( 'rest_api_init', function () { |
| @@ -121,47 +109,25 @@ | ||
| 121 | 109 | add_action( 'rest_api_init', function () { |
| 122 | 110 | $this->rest_api()->register_routes(); |
| 123 | 111 | } ); |
| 124 | 112 | |
| 125 | - return $this; | |
| 126 | - } | |
| 113 | + // TODO: Remove this, when there are API-endpoints available to access the list of variables | |
| 114 | + add_action( 'elementor/editor/before_enqueue_scripts', function () { | |
| 115 | + // We must enqueue a random script, so that localize will be triggered as well... | |
| 116 | + wp_enqueue_script( | |
| 117 | + 'e-variables', | |
| 118 | + ELEMENTOR_ASSETS_URL . '/variables-' . md5( microtime() ) . '.js', | |
| 119 | + [], | |
| 120 | + ELEMENTOR_VERSION, | |
| 121 | + true | |
| 122 | + ); | |
| 127 | 123 | |
| 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 | - ); | |
| 124 | + wp_localize_script( | |
| 125 | + 'e-variables', | |
| 126 | + 'ElementorV4Variables', | |
| 127 | + ( new Variables() )->get_all() | |
| 128 | + ); | |
| 129 | + } ); | |
| 164 | 130 | |
| 165 | 131 | return $this; |
| 166 | 132 | } |
| 167 | 133 | } |