| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Variables; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 7 |
use Elementor\Modules\Variables\Classes\CSS_Renderer as Variables_CSS_Renderer; |
| 8 |
use Elementor\Modules\Variables\Classes\Fonts; |
| 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; |
| 12 |
use Elementor\Modules\Variables\Classes\Style_Schema; |
| 13 |
use Elementor\Modules\Variables\Classes\Style_Transformers; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Hooks { |
| 20 |
const PACKAGES = [ |
| 21 |
'editor-variables', |
| 22 |
]; |
| 23 |
|
| 24 |
public function register() { |
| 25 |
$this->register_styles_transformers() |
| 26 |
->register_packages() |
| 27 |
->filter_for_style_schema() |
| 28 |
->register_css_renderer() |
| 29 |
->register_fonts() |
| 30 |
->register_api_endpoints(); |
| 31 |
|
| 32 |
// TODO: Remove this, later, temporary solution |
| 33 |
$this->filter_for_stored_variables(); |
| 34 |
|
| 35 |
return $this; |
| 36 |
} |
| 37 |
|
| 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; |
| 49 |
} ); |
| 50 |
|
| 51 |
return $this; |
| 52 |
} |
| 53 |
|
| 54 |
private function register_packages() { |
| 55 |
add_filter( 'elementor/editor/v2/packages', function ( $packages ) { |
| 56 |
return array_merge( $packages, self::PACKAGES ); |
| 57 |
} ); |
| 58 |
|
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
private function register_styles_transformers() { |
| 63 |
add_action( 'elementor/atomic-widgets/styles/transformers/register', function ( $registry ) { |
| 64 |
( new Style_Transformers() )->append_to( $registry ); |
| 65 |
} ); |
| 66 |
|
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
private function filter_for_style_schema() { |
| 71 |
add_filter( 'elementor/atomic-widgets/styles/schema', function ( array $schema ) { |
| 72 |
return ( new Style_Schema() )->augment( $schema ); |
| 73 |
} ); |
| 74 |
|
| 75 |
return $this; |
| 76 |
} |
| 77 |
|
| 78 |
private function register_css_renderer() { |
| 79 |
add_action( 'elementor/css-file/post/parse', function ( Post_CSS $post_css ) { |
| 80 |
if ( ! Plugin::$instance->kits_manager->is_kit( $post_css->get_post_id() ) ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$post_css->get_stylesheet()->add_raw_css( |
| 85 |
( new Variables_CSS_Renderer( new Variables() ) )->raw_css() |
| 86 |
); |
| 87 |
} ); |
| 88 |
|
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
private function register_fonts() { |
| 93 |
add_action( 'elementor/css-file/post/parse', function ( $post_css ) { |
| 94 |
( new Fonts() )->append_to( $post_css ); |
| 95 |
} ); |
| 96 |
|
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
private function rest_api() { |
| 101 |
return new Variables_API( |
| 102 |
new Variables_Repository( |
| 103 |
Plugin::$instance->kits_manager->get_active_kit() |
| 104 |
) |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
private function register_api_endpoints() { |
| 109 |
add_action( 'rest_api_init', function () { |
| 110 |
$this->rest_api()->register_routes(); |
| 111 |
} ); |
| 112 |
|
| 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 |
); |
| 123 |
|
| 124 |
wp_localize_script( |
| 125 |
'e-variables', |
| 126 |
'ElementorV4Variables', |
| 127 |
( new Variables() )->get_all() |
| 128 |
); |
| 129 |
} ); |
| 130 |
|
| 131 |
return $this; |
| 132 |
} |
| 133 |
} |
| 134 |
|