first commit

This commit is contained in:
2024-07-31 13:12:38 +07:00
commit b4e8cbe182
10213 changed files with 3125839 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
<?php
namespace Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems;
use Elementor\Core\Admin\Menu\Interfaces\Admin_Menu_Item;
use Elementor\Core\Editor\Editor;
use Elementor\TemplateLibrary\Source_Local;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Add_New_Template_Menu_Item implements Admin_Menu_Item {
public function is_visible() {
return true;
}
public function get_parent_slug() {
return Source_Local::ADMIN_MENU_SLUG;
}
public function get_label() {
return esc_html__( 'Add New', 'elementor' );
}
public function get_capability() {
return Editor::EDITING_CAPABILITY;
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems;
use Elementor\Core\Admin\Menu\Interfaces\Admin_Menu_Item;
use Elementor\Core\Editor\Editor;
use Elementor\TemplateLibrary\Source_Local;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Saved_Templates_Menu_Item implements Admin_Menu_Item {
public function is_visible() {
return true;
}
public function get_parent_slug() {
return Source_Local::ADMIN_MENU_SLUG;
}
public function get_label() {
return esc_html__( 'Saved Templates', 'elementor' );
}
public function get_capability() {
return Editor::EDITING_CAPABILITY;
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems;
use Elementor\Core\Admin\Menu\Interfaces\Admin_Menu_Item;
use Elementor\Core\Editor\Editor;
use Elementor\TemplateLibrary\Source_Local;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Templates_Categories_Menu_Item implements Admin_Menu_Item {
public function is_visible() {
return true;
}
public function get_parent_slug() {
return Source_Local::ADMIN_MENU_SLUG;
}
public function get_label() {
return esc_html__( 'Categories', 'elementor' );
}
public function get_capability() {
return 'manage_categories';
}
}

View File

@@ -0,0 +1,368 @@
<?php
namespace Elementor\TemplateLibrary;
use Elementor\Controls_Stack;
use Elementor\Plugin;
use Elementor\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor template library source base.
*
* Elementor template library source base handler class is responsible for
* initializing all the methods controlling the source of Elementor templates.
*
* @since 1.0.0
* @abstract
*/
abstract class Source_Base {
/**
* User meta.
*
* Holds the current user meta data.
*
* @access private
*
* @var array
*/
private $user_meta;
/**
* Get template ID.
*
* Retrieve the template ID.
*
* @since 1.0.0
* @access public
* @abstract
*/
abstract public function get_id();
/**
* Get template title.
*
* Retrieve the template title.
*
* @since 1.0.0
* @access public
* @abstract
*/
abstract public function get_title();
/**
* Register template data.
*
* Used to register custom template data like a post type, a taxonomy or any
* other data.
*
* @since 1.0.0
* @access public
* @abstract
*/
abstract public function register_data();
/**
* Get templates.
*
* Retrieve templates from the template library.
*
* @since 1.0.0
* @access public
* @abstract
*
* @param array $args Optional. Filter templates list based on a set of
* arguments. Default is an empty array.
*/
abstract public function get_items( $args = [] );
/**
* Get template.
*
* Retrieve a single template from the template library.
*
* @since 1.0.0
* @access public
* @abstract
*
* @param int $template_id The template ID.
*/
abstract public function get_item( $template_id );
/**
* Get template data.
*
* Retrieve a single template data from the template library.
*
* @since 1.5.0
* @access public
* @abstract
*
* @param array $args Custom template arguments.
*/
abstract public function get_data( array $args );
/**
* Delete template.
*
* Delete template from the database.
*
* @since 1.0.0
* @access public
* @abstract
*
* @param int $template_id The template ID.
*/
abstract public function delete_template( $template_id );
/**
* Save template.
*
* Save new or update existing template on the database.
*
* @since 1.0.0
* @access public
* @abstract
*
* @param array $template_data The template data.
*/
abstract public function save_item( $template_data );
/**
* Update template.
*
* Update template on the database.
*
* @since 1.0.0
* @access public
* @abstract
*
* @param array $new_data New template data.
*/
abstract public function update_item( $new_data );
/**
* Export template.
*
* Export template to a file.
*
* @since 1.0.0
* @access public
* @abstract
*
* @param int $template_id The template ID.
*/
abstract public function export_template( $template_id );
/**
* Template library source base constructor.
*
* Initializing the template library source base by registering custom
* template data.
*
* @since 1.0.0
* @access public
*/
public function __construct() {
$this->register_data();
}
/**
* Mark template as favorite.
*
* Update user meta containing his favorite templates. For a given template
* ID, add the template to the favorite templates or remove it from the
* favorites, based on the `favorite` parameter.
*
* @since 1.9.0
* @access public
*
* @param int $template_id The template ID.
* @param bool $favorite Optional. Whether the template is marked as
* favorite, or not. Default is true.
*
* @return int|bool User meta ID if the key didn't exist, true on successful
* update, false on failure.
*/
public function mark_as_favorite( $template_id, $favorite = true ) {
$favorites_templates = $this->get_user_meta( 'favorites' );
if ( ! $favorites_templates ) {
$favorites_templates = [];
}
if ( $favorite ) {
$favorites_templates[ $template_id ] = $favorite;
} elseif ( isset( $favorites_templates[ $template_id ] ) ) {
unset( $favorites_templates[ $template_id ] );
}
return $this->update_user_meta( 'favorites', $favorites_templates );
}
/**
* Get current user meta.
*
* Retrieve Elementor meta data for the current user.
*
* @since 1.9.0
* @access public
*
* @param string $item Optional. User meta key. Default is null.
*
* @return null|array An array of user meta data, or null otherwise.
*/
public function get_user_meta( $item = null ) {
if ( null === $this->user_meta ) {
$this->user_meta = get_user_meta( get_current_user_id(), $this->get_user_meta_prefix(), true );
}
if ( ! $this->user_meta ) {
$this->user_meta = [];
}
if ( $item ) {
if ( isset( $this->user_meta[ $item ] ) ) {
return $this->user_meta[ $item ];
}
return null;
}
return $this->user_meta;
}
/**
* Update current user meta.
*
* Update user meta data based on meta key an value.
*
* @since 1.9.0
* @access public
*
* @param string $key Optional. User meta key.
* @param mixed $value Optional. User meta value.
*
* @return int|bool User meta ID if the key didn't exist, true on successful
* update, false on failure.
*/
public function update_user_meta( $key, $value ) {
$meta = $this->get_user_meta();
$meta[ $key ] = $value;
$this->user_meta = $meta;
return update_user_meta( get_current_user_id(), $this->get_user_meta_prefix(), $meta );
}
/**
* Replace elements IDs.
*
* For any given Elementor content/data, replace the IDs with new randomly
* generated IDs.
*
* @since 1.0.0
* @access protected
*
* @param array $content Any type of Elementor data.
*
* @return mixed Iterated data.
*/
protected function replace_elements_ids( $content ) {
return Plugin::$instance->db->iterate_data( $content, function( $element ) {
$element['id'] = Utils::generate_random_string();
return $element;
} );
}
/**
* Get Elementor library user meta prefix.
*
* Retrieve user meta prefix used to save Elementor data.
*
* @since 1.9.0
* @access protected
*
* @return string User meta prefix.
*/
protected function get_user_meta_prefix() {
return 'elementor_library_' . $this->get_id();
}
/**
* Process content for export/import.
*
* Process the content and all the inner elements, and prepare all the
* elements data for export/import.
*
* @since 1.5.0
* @access protected
*
* @param array $content A set of elements.
* @param string $method Accepts either `on_export` to export data or
* `on_import` to import data.
*
* @return mixed Processed content data.
*/
protected function process_export_import_content( $content, $method ) {
return Plugin::$instance->db->iterate_data(
$content, function( $element_data ) use ( $method ) {
$element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
// If the widget/element isn't exist, like a plugin that creates a widget but deactivated
if ( ! $element ) {
return null;
}
return $this->process_element_export_import_content( $element, $method );
}
);
}
/**
* Process single element content for export/import.
*
* Process any given element and prepare the element data for export/import.
*
* @since 1.5.0
* @access protected
*
* @param Controls_Stack $element
* @param string $method
*
* @return array Processed element data.
*/
protected function process_element_export_import_content( Controls_Stack $element, $method ) {
$element_data = $element->get_data();
if ( method_exists( $element, $method ) ) {
// TODO: Use the internal element data without parameters.
$element_data = $element->{$method}( $element_data );
}
foreach ( $element->get_controls() as $control ) {
$control_class = Plugin::$instance->controls_manager->get_control( $control['type'] );
// If the control isn't exist, like a plugin that creates the control but deactivated.
if ( ! $control_class ) {
return $element_data;
}
if ( method_exists( $control_class, $method ) ) {
$element_data['settings'][ $control['name'] ] = $control_class->{$method}( $element->get_settings( $control['name'] ), $control );
}
// On Export, check if the control has an argument 'export' => false.
if ( 'on_export' === $method && isset( $control['export'] ) && false === $control['export'] ) {
unset( $element_data['settings'][ $control['name'] ] );
}
}
return $element_data;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,343 @@
<?php
namespace Elementor\TemplateLibrary;
use Elementor\Api;
use Elementor\Core\Common\Modules\Connect\Module as ConnectModule;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor template library remote source.
*
* Elementor template library remote source handler class is responsible for
* handling remote templates from Elementor.com servers.
*
* @since 1.0.0
*/
class Source_Remote extends Source_Base {
const API_TEMPLATES_URL = 'https://my.elementor.com/api/connect/v1/library/templates';
const TEMPLATES_DATA_TRANSIENT_KEY_PREFIX = 'elementor_remote_templates_data_';
public function __construct() {
parent::__construct();
$this->add_actions();
}
public function add_actions() {
add_action( 'elementor/experiments/feature-state-change/container', [ $this, 'clear_cache' ], 10, 0 );
}
/**
* Get remote template ID.
*
* Retrieve the remote template ID.
*
* @since 1.0.0
* @access public
*
* @return string The remote template ID.
*/
public function get_id() {
return 'remote';
}
/**
* Get remote template title.
*
* Retrieve the remote template title.
*
* @since 1.0.0
* @access public
*
* @return string The remote template title.
*/
public function get_title() {
return esc_html__( 'Remote', 'elementor' );
}
/**
* Register remote template data.
*
* Used to register custom template data like a post type, a taxonomy or any
* other data.
*
* @since 1.0.0
* @access public
*/
public function register_data() {}
/**
* Get remote templates.
*
* Retrieve remote templates from Elementor.com servers.
*
* @since 1.0.0
* @access public
*
* @param array $args Optional. Not used in remote source.
*
* @return array Remote templates.
*/
public function get_items( $args = [] ) {
$force_update = ! empty( $args['force_update'] ) && is_bool( $args['force_update'] );
$templates_data = $this->get_templates_data( $force_update );
$templates = [];
foreach ( $templates_data as $template_data ) {
$templates[] = $this->prepare_template( $template_data );
}
return $templates;
}
/**
* Get remote template.
*
* Retrieve a single remote template from Elementor.com servers.
*
* @since 1.0.0
* @access public
*
* @param int $template_id The template ID.
*
* @return array Remote template.
*/
public function get_item( $template_id ) {
$templates = $this->get_items();
return $templates[ $template_id ];
}
/**
* Save remote template.
*
* Remote template from Elementor.com servers cannot be saved on the
* database as they are retrieved from remote servers.
*
* @since 1.0.0
* @access public
*
* @param array $template_data Remote template data.
*
* @return \WP_Error
*/
public function save_item( $template_data ) {
return new \WP_Error( 'invalid_request', 'Cannot save template to a remote source' );
}
/**
* Update remote template.
*
* Remote template from Elementor.com servers cannot be updated on the
* database as they are retrieved from remote servers.
*
* @since 1.0.0
* @access public
*
* @param array $new_data New template data.
*
* @return \WP_Error
*/
public function update_item( $new_data ) {
return new \WP_Error( 'invalid_request', 'Cannot update template to a remote source' );
}
/**
* Delete remote template.
*
* Remote template from Elementor.com servers cannot be deleted from the
* database as they are retrieved from remote servers.
*
* @since 1.0.0
* @access public
*
* @param int $template_id The template ID.
*
* @return \WP_Error
*/
public function delete_template( $template_id ) {
return new \WP_Error( 'invalid_request', 'Cannot delete template from a remote source' );
}
/**
* Export remote template.
*
* Remote template from Elementor.com servers cannot be exported from the
* database as they are retrieved from remote servers.
*
* @since 1.0.0
* @access public
*
* @param int $template_id The template ID.
*
* @return \WP_Error
*/
public function export_template( $template_id ) {
return new \WP_Error( 'invalid_request', 'Cannot export template from a remote source' );
}
/**
* Get remote template data.
*
* Retrieve the data of a single remote template from Elementor.com servers.
*
* @since 1.5.0
* @access public
*
* @param array $args Custom template arguments.
* @param string $context Optional. The context. Default is `display`.
*
* @return array|\WP_Error Remote Template data.
*/
public function get_data( array $args, $context = 'display' ) {
$data = Api::get_template_content( $args['template_id'] );
if ( is_wp_error( $data ) ) {
return $data;
}
// Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
// BC.
$data = (array) $data;
$data['content'] = $this->replace_elements_ids( $data['content'] );
$data['content'] = $this->process_export_import_content( $data['content'], 'on_import' );
$post_id = $args['editor_post_id'];
$document = Plugin::$instance->documents->get( $post_id );
if ( $document ) {
$data['content'] = $document->get_elements_raw_data( $data['content'], true );
}
// After the upload complete, set the elementor upload state back to false
Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
return $data;
}
/**
* Get templates data from a transient or from a remote request.
* In any of the following 2 conditions, the remote request will be triggered:
* 1. Force update - "$force_update = true" parameter was passed.
* 2. The data saved in the transient is empty or not exist.
*
* @param bool $force_update
* @return array
*/
private function get_templates_data( bool $force_update ) : array {
$templates_data_cache_key = static::TEMPLATES_DATA_TRANSIENT_KEY_PREFIX . ELEMENTOR_VERSION;
$experiments_manager = Plugin::$instance->experiments;
$editor_layout_type = $experiments_manager->is_feature_active( 'container' ) ? 'container_flexbox' : '';
if ( $force_update ) {
return $this->get_templates( $editor_layout_type );
}
$templates_data = get_transient( $templates_data_cache_key );
if ( empty( $templates_data ) ) {
return $this->get_templates( $editor_layout_type );
}
return $templates_data;
}
/**
* Get the templates from a remote server and set a transient.
*
* @param string $editor_layout_type
* @return array
*/
private function get_templates( string $editor_layout_type ): array {
$templates_data_cache_key = static::TEMPLATES_DATA_TRANSIENT_KEY_PREFIX . ELEMENTOR_VERSION;
$templates_data = $this->get_templates_remotely( $editor_layout_type );
if ( empty( $templates_data ) ) {
return [];
}
set_transient( $templates_data_cache_key, $templates_data, 12 * HOUR_IN_SECONDS );
return $templates_data;
}
/**
* Fetch templates from the remote server.
*
* @param string $editor_layout_type
* @return array|false
*/
private function get_templates_remotely( string $editor_layout_type ) {
$response = wp_remote_get( static::API_TEMPLATES_URL, [
'body' => [
'plugin_version' => ELEMENTOR_VERSION,
'editor_layout_type' => $editor_layout_type,
],
] );
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
return false;
}
$templates_data = json_decode( wp_remote_retrieve_body( $response ), true );
if ( empty( $templates_data ) || ! is_array( $templates_data ) ) {
return [];
}
return $templates_data;
}
/**
* @since 2.2.0
* @access private
*/
private function prepare_template( array $template_data ) {
$favorite_templates = $this->get_user_meta( 'favorites' );
// BC: Support legacy APIs that don't have access tiers.
if ( isset( $template_data['access_tier'] ) ) {
$access_tier = $template_data['access_tier'];
} else {
$access_tier = 0 === $template_data['access_level']
? ConnectModule::ACCESS_TIER_FREE
: ConnectModule::ACCESS_TIER_ESSENTIAL;
}
return [
'template_id' => $template_data['id'],
'source' => $this->get_id(),
'type' => $template_data['type'],
'subtype' => $template_data['subtype'],
'title' => $template_data['title'],
'thumbnail' => $template_data['thumbnail'],
'date' => $template_data['tmpl_created'],
'author' => $template_data['author'],
'tags' => json_decode( $template_data['tags'] ),
'isPro' => ( '1' === $template_data['is_pro'] ),
'accessLevel' => $template_data['access_level'],
'accessTier' => $access_tier,
'popularityIndex' => (int) $template_data['popularity_index'],
'trendIndex' => (int) $template_data['trend_index'],
'hasPageSettings' => ( '1' === $template_data['has_page_settings'] ),
'url' => $template_data['url'],
'favorite' => ! empty( $favorite_templates[ $template_data['id'] ] ),
];
}
public function clear_cache() {
delete_transient( static::TEMPLATES_DATA_TRANSIENT_KEY_PREFIX . ELEMENTOR_VERSION );
}
}