first commit

This commit is contained in:
Ryan Ariana
2024-05-06 11:04:37 +07:00
commit aee061ddba
7322 changed files with 2918816 additions and 0 deletions

View File

@@ -0,0 +1,345 @@
<?php
namespace Elementor\Core\Page_Assets\Data_Managers;
use Elementor\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor Assets Data.
*
* @since 3.3.0
*/
abstract class Base {
const ASSETS_DATA_KEY = '_elementor_assets_data';
/**
* @var array
*/
protected $assets_data;
/**
* @var string
*/
protected $content_type;
/**
* @var string
*/
protected $assets_category;
/**
* @var array
*/
private $assets_config;
/**
* @var array
*/
private $files_data;
/**
* Get Asset Content.
*
* Responsible for extracting the asset data from a certain file.
* Will be triggered automatically when the asset data does not exist, or when the asset version was changed.
*
* @since 3.3.0
* @access public
*
* @return string
*/
abstract protected function get_asset_content();
/**
* Get Asset Key.
*
* The asset data will be saved in the DB under this key.
*
* @since 3.3.0
* @access protected
*
* @return string
*/
protected function get_key() {
return $this->assets_config['key'];
}
/**
* Get Relative Version.
*
* The asset data will be re-evaluated according the version number.
*
* @since 3.3.0
* @access protected
*
* @return string
*/
protected function get_version() {
return $this->assets_config['version'];
}
/**
* Get Asset Path.
*
* The asset data will be extracted from the file path.
*
* @since 3.3.0
* @access protected
*
* @return string
*/
protected function get_file_path() {
return $this->assets_config['file_path'];
}
/**
* Get Config Data.
*
* Holds a unique data relevant for the specific assets category type.
*
* @since 3.3.0
* @access protected
*
* @return string|array
*/
protected function get_config_data( $key = '' ) {
if ( isset( $this->assets_config['data'] ) ) {
if ( $key ) {
if ( isset( $this->assets_config['data'][ $key ] ) ) {
return $this->assets_config['data'][ $key ];
}
return '';
}
return $this->assets_config['data'];
}
return [];
}
/**
* Set Asset Data.
*
* Responsible for setting the current asset data.
*
* @since 3.3.0
* @access protected
*
* @return void
*/
protected function set_asset_data( $asset_key ) {
if ( ! isset( $this->assets_data[ $asset_key ] ) ) {
$this->assets_data[ $asset_key ] = [];
}
$this->assets_data[ $asset_key ]['content'] = $this->get_asset_content();
$this->assets_data[ $asset_key ]['version'] = $this->get_version();
$this->save_asset_data( $asset_key );
}
/**
* Save Asset Data.
*
* Responsible for saving the asset data in the DB.
*
* @since 3.3.0
* @access protected
*
* @param string $asset_key
*
* @return void
*/
protected function save_asset_data( $asset_key ) {
$assets_data = $this->get_saved_assets_data();
$content_type = $this->content_type;
$assets_category = $this->assets_category;
$assets_data[ $content_type ][ $assets_category ][ $asset_key ] = $this->assets_data[ $asset_key ];
update_option( self::ASSETS_DATA_KEY, $assets_data );
}
/**
* Is Asset Version Changed.
*
* Responsible for comparing the saved asset data version to the current relative version.
*
* @since 3.3.0
* @access protected
*
* @param string $asset_key
*
* @return boolean
*/
protected function is_asset_version_changed( $version ) {
return $this->get_version() !== $version;
}
/**
* Get File Data.
*
* Getting a file content or size.
*
* @since 3.3.0
* @access protected
*
* @param string $data_type (content|size)
* @param string $file_key - In case that the same file data is needed for multiple assets (like a JSON file), the file data key should be the same for all shared assets to make sure that the file is being read only once.
*
* @return string|number
*/
protected function get_file_data( $data_type, $file_key = '' ) {
$asset_key = $file_key ? $file_key : $this->get_key();
if ( isset( $this->files_data[ $asset_key ][ $data_type ] ) ) {
return $this->files_data[ $asset_key ][ $data_type ];
}
if ( ! isset( $this->files_data[ $asset_key ] ) ) {
$this->files_data[ $asset_key ] = [];
}
$asset_path = $this->get_file_path();
if ( 'content' === $data_type ) {
$data = Utils::file_get_contents( $asset_path );
if ( ! $data ) {
$data = '';
}
} elseif ( 'size' === $data_type ) {
$data = file_exists( $asset_path ) ? filesize( $asset_path ) : 0;
}
$this->files_data[ $asset_key ][ $data_type ] = $data;
return $data;
}
/**
* Get Saved Assets Data.
*
* Getting the assets data from the DB.
*
* @since 3.3.0
* @access protected
*
* @return array
*/
protected function get_saved_assets_data() {
$assets_data = get_option( self::ASSETS_DATA_KEY, [] );
$content_type = $this->content_type;
$assets_category = $this->assets_category;
if ( ! isset( $assets_data[ $content_type ] ) ) {
$assets_data[ $content_type ] = [];
}
if ( ! isset( $assets_data[ $content_type ][ $assets_category ] ) ) {
$assets_data[ $content_type ][ $assets_category ] = [];
}
return $assets_data;
}
/**
* Get Config.
*
* Getting the assets data config.
*
* @since 3.5.0
* @access protected
*
* @return array
*/
protected function get_config( $data ) {
return [];
}
/**
* Init Asset Data.
*
* Initialize the asset data and handles the asset content updates when needed.
*
* @since 3.3.0
* @access public
*
* @param array $config {
* @type string 'key'
* @type string 'version'
* @type string 'file_path'
* @type array 'data'
* }
*
* @return void
*/
public function init_asset_data( $config ) {
$this->assets_config = $config;
$asset_key = $config['key'];
$asset_data = isset( $this->assets_data[ $asset_key ] ) ? $this->assets_data[ $asset_key ] : [];
if ( ! $asset_data || $this->is_asset_version_changed( $asset_data['version'] ) ) {
$this->set_asset_data( $asset_key );
}
}
/**
* Get Asset Data From Config.
*
* Getting the asset data content from config.
*
* @since 3.3.0
* @access public
*
* @param array $config {
* @type string 'key'
* @type string 'version'
* @type string 'file_path'
* @type array 'data'
* }
*
* @return mixed
*/
public function get_asset_data_from_config( array $config ) {
$this->init_asset_data( $config );
$asset_key = $config['key'];
return $this->assets_data[ $asset_key ]['content'];
}
/**
* Get Asset Data.
*
* Getting the asset data content.
*
* @since 3.5.0
* @access public
*
* @param array $data
*
* @return mixed
*/
public function get_asset_data( array $data ) {
$config = $this->get_config( $data );
return $this->get_asset_data_from_config( $config );
}
public function __construct() {
$assets_data = $this->get_saved_assets_data();
$content_type = $this->content_type;
$assets_category = $this->assets_category;
$this->assets_data = $assets_data[ $content_type ][ $assets_category ];
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Elementor\Core\Page_Assets\Data_Managers\Font_Icon_Svg;
use Elementor\Core\Page_Assets\Data_Managers\Base as Data_Managers_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor Font Icon Svg Base.
*
* @since 3.4.0
*/
class Base extends Data_Managers_Base {
protected $content_type = 'svg';
protected $assets_category = 'font-icon';
protected function get_asset_content() {}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Elementor\Core\Page_Assets\Data_Managers\Font_Icon_Svg;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* E-Icons Svg.
*
* @since 3.5.0
*/
class E_Icons extends Base {
const LIBRARY_CURRENT_VERSION = '5.13.0';
protected function get_config( $icon ) {
return [
'key' => $icon['value'],
'version' => self::LIBRARY_CURRENT_VERSION,
'file_path' => ELEMENTOR_ASSETS_PATH . 'lib/eicons/eicons.json',
'data' => [
'icon_data' => [
'name' => $icon['value'],
'library' => $icon['library'],
],
],
];
}
protected function get_asset_content() {
$icon_data = $this->get_config_data( 'icon_data' );
$file_data = json_decode( $this->get_file_data( 'content', $icon_data['library'] ), true );
$icon_name = str_replace( 'eicon-', '', $icon_data['name'] );
$svg_data = $file_data[ $icon_name ];
return [
'width' => $svg_data['width'],
'height' => $svg_data['height'],
'path' => $svg_data['path'],
'key' => $this->get_key(),
];
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Elementor\Core\Page_Assets\Data_Managers\Font_Icon_Svg;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Font Awesome Icon Svg.
*
* @since 3.4.0
*/
class Font_Awesome extends Base {
const LIBRARY_CURRENT_VERSION = '5.15.3';
protected function get_config( $icon ) {
preg_match( '/fa(.*) fa-/', $icon['value'], $icon_name_matches );
$icon_name = str_replace( $icon_name_matches[0], '', $icon['value'] );
$icon_key = str_replace( ' fa-', '-', $icon['value'] );
$icon_file_name = str_replace( 'fa-', '', $icon['library'] );
return [
'key' => $icon_key,
'version' => self::LIBRARY_CURRENT_VERSION,
'file_path' => ELEMENTOR_ASSETS_PATH . 'lib/font-awesome/json/' . $icon_file_name . '.json',
'data' => [
'icon_data' => [
'name' => $icon_name,
'library' => $icon['library'],
],
],
];
}
protected function get_asset_content() {
$icon_data = $this->get_config_data( 'icon_data' );
$file_data = json_decode( $this->get_file_data( 'content', $icon_data['library'] ), true );
$icon_name = $icon_data['name'];
$svg_data = $file_data['icons'][ $icon_name ];
return [
'width' => $svg_data[0],
'height' => $svg_data[1],
'path' => $svg_data[4],
'key' => $this->get_key(),
];
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Elementor\Core\Page_Assets\Data_Managers\Font_Icon_Svg;
use Elementor\Core\Base\Base_Object;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor Font Icon Svg Manager.
*
* @since 3.4.0
*/
class Manager extends Base_Object {
private static $data = [];
private static function get_data() {
if ( ! self::$data ) {
self::$data = [
'font-awesome' => [
'regex' => '/^fa-/',
'manager' => new Font_Awesome(),
],
'eicons' => [
'regex' => '/^eicons$/',
'manager' => new E_Icons(),
],
];
}
return self::$data;
}
public static function get_font_icon_svg_data( $icon ) {
$data = self::get_data();
$font_family = $icon['font_family'];
$font_family_manager = $data[ $font_family ]['manager'];
return $font_family_manager->get_asset_data( $icon );
}
public static function get_font_family( $icon_library ) {
foreach ( self::get_data() as $family => $data ) {
if ( preg_match( $data['regex'], $icon_library ) ) {
return $family;
}
}
return '';
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Elementor\Core\Page_Assets\Data_Managers;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor Responsive Widgets Data.
*
* @since 3.5.0
*/
class Responsive_Widgets extends Base {
const RESPONSIVE_WIDGETS_DATABASE_KEY = 'responsive-widgets';
const RESPONSIVE_WIDGETS_FILE_PATH = 'data/responsive-widgets.json';
protected $content_type = 'json';
protected $assets_category = 'widgets';
protected function get_asset_content() {
$data = $this->get_file_data( 'content' );
if ( $data ) {
$data = json_decode( $data, true );
}
return $data;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Elementor\Core\Page_Assets\Data_Managers;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor Assets Data.
*
* @since 3.3.0
*/
class Widgets_Css extends Base {
protected $content_type = 'css';
protected $assets_category = 'widgets';
protected function get_asset_content() {
$asset_css_file_size = $this->get_file_data( 'size' );
$widget_css = '';
if ( $asset_css_file_size ) {
// If the file size is larger than 8KB then calling the external CSS file, otherwise, printing inline CSS.
if ( $asset_css_file_size > 8000 ) {
$asset_url = $this->get_config_data( 'file_url' );
$widget_css = sprintf( '<link rel="stylesheet" href="%s">', $asset_url );
} else {
$widget_css = $this->get_file_data( 'content' );
$widget_css = sprintf( '<style>%s</style>', $widget_css );
}
}
return $widget_css;
}
}

View File

@@ -0,0 +1,114 @@
<?php
namespace Elementor\Core\Page_Assets;
use Elementor\Core\Base\Module;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Elementor assets loader.
*
* A class that is responsible for conditionally enqueuing styles and script assets that were pre-enabled.
*
* @since 3.3.0
*/
class Loader extends Module {
private $assets;
public function get_name() {
return 'assets-loader';
}
private function init_assets() {
$this->assets = [
'styles' => [
'e-animations' => [
'src' => $this->get_css_assets_url( 'animations', 'assets/lib/animations/', true ),
'version' => ELEMENTOR_VERSION,
'dependencies' => [],
],
],
'scripts' => [],
];
}
public function get_assets() {
if ( ! $this->assets ) {
$this->init_assets();
}
return $this->assets;
}
/**
* @param array $assets {
* @type array 'styles'
* @type array 'scripts'
* }
*/
public function enable_assets( array $assets_data ) {
if ( ! $this->assets ) {
$this->init_assets();
}
foreach ( $assets_data as $assets_type => $assets_list ) {
foreach ( $assets_list as $asset_name ) {
$this->assets[ $assets_type ][ $asset_name ]['enabled'] = true;
}
}
}
/**
* @param array $assets {
* @type array 'styles'
* @type array 'scripts'
* }
*/
public function add_assets( array $assets ) {
if ( ! $this->assets ) {
$this->init_assets();
}
$this->assets = array_replace_recursive( $this->assets, $assets );
}
public function enqueue_assets() {
$assets = $this->get_assets();
$is_preview_mode = Plugin::$instance->preview->is_preview_mode();
foreach ( $assets as $assets_type => $assets_type_data ) {
foreach ( $assets_type_data as $asset_name => $asset_data ) {
if ( ! empty( $asset_data['enabled'] ) || $is_preview_mode ) {
if ( 'scripts' === $assets_type ) {
wp_enqueue_script( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'], true );
} else {
wp_enqueue_style( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'] );
}
}
}
}
}
private function register_assets() {
$assets = $this->get_assets();
foreach ( $assets as $assets_type => $assets_type_data ) {
foreach ( $assets_type_data as $asset_name => $asset_data ) {
if ( 'scripts' === $assets_type ) {
wp_register_script( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'], true );
} else {
wp_register_style( $asset_name, $asset_data['src'], $asset_data['dependencies'], $asset_data['version'] );
}
}
}
}
public function __construct() {
parent::__construct();
$this->register_assets();
}
}