first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,188 @@
<?php
namespace Elementor\TemplateLibrary\Classes;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor template library import images.
*
* Elementor template library import images handler class is responsible for
* importing remote images used by the template library.
*
* @since 1.0.0
*/
class Import_Images {
/**
* Replaced images IDs.
*
* The IDs of all the new imported images. An array containing the old
* attachment ID and the new attachment ID generated after the import.
*
* @since 1.0.0
* @access private
*
* @var array
*/
private $_replace_image_ids = [];
/**
* Get image hash.
*
* Retrieve the sha1 hash of the image URL.
*
* @since 2.0.0
* @access private
*
* @param string $attachment_url The attachment URL.
*
* @return string Image hash.
*/
private function get_hash_image( $attachment_url ) {
return sha1( $attachment_url );
}
/**
* Get saved image.
*
* Retrieve new image ID, if the image has a new ID after the import.
*
* @since 2.0.0
* @access private
*
* @param array $attachment The attachment.
*
* @return false|array New image ID or false.
*/
private function get_saved_image( $attachment ) {
global $wpdb;
if ( isset( $this->_replace_image_ids[ $attachment['id'] ] ) ) {
return $this->_replace_image_ids[ $attachment['id'] ];
}
$post_id = $wpdb->get_var(
$wpdb->prepare(
'SELECT `post_id` FROM `' . $wpdb->postmeta . '`
WHERE `meta_key` = \'_elementor_source_image_hash\'
AND `meta_value` = %s
;',
$this->get_hash_image( $attachment['url'] )
)
);
if ( $post_id ) {
$new_attachment = [
'id' => $post_id,
'url' => wp_get_attachment_url( $post_id ),
];
$this->_replace_image_ids[ $attachment['id'] ] = $new_attachment;
return $new_attachment;
}
return false;
}
/**
* Import image.
*
* Import a single image from a remote server, upload the image WordPress
* uploads folder, create a new attachment in the database and updates the
* attachment metadata.
*
* @since 1.0.0
* @since 3.2.0 New `$parent_post_id` option added
* @access public
*
* @param array $attachment The attachment.
* @param int $parent_post_id Optional
*
* @return false|array Imported image data, or false.
*/
public function import( $attachment, $parent_post_id = null ) {
if ( ! empty( $attachment['id'] ) ) {
$saved_image = $this->get_saved_image( $attachment );
if ( $saved_image ) {
return $saved_image;
}
}
// Extract the file name and extension from the url.
$filename = basename( $attachment['url'] );
$file_content = wp_remote_retrieve_body( wp_safe_remote_get( $attachment['url'] ) );
if ( empty( $file_content ) ) {
return false;
}
$upload = wp_upload_bits(
$filename,
null,
$file_content
);
$post = [
'post_title' => $filename,
'guid' => $upload['url'],
];
$info = wp_check_filetype( $upload['file'] );
if ( $info ) {
$post['post_mime_type'] = $info['type'];
} else {
// For now just return the origin attachment
return $attachment;
// return new \WP_Error( 'attachment_processing_error', __( 'Invalid file type.', 'elementor' ) );
}
$post_id = wp_insert_attachment( $post, $upload['file'], $parent_post_id );
// On REST requests.
if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
require_once ABSPATH . '/wp-admin/includes/image.php';
}
if ( ! function_exists( 'wp_read_video_metadata' ) ) {
require_once ABSPATH . '/wp-admin/includes/media.php';
}
wp_update_attachment_metadata(
$post_id,
wp_generate_attachment_metadata( $post_id, $upload['file'] )
);
update_post_meta( $post_id, '_elementor_source_image_hash', $this->get_hash_image( $attachment['url'] ) );
$new_attachment = [
'id' => $post_id,
'url' => $upload['url'],
];
if ( ! empty( $attachment['id'] ) ) {
$this->_replace_image_ids[ $attachment['id'] ] = $new_attachment;
}
return $new_attachment;
}
/**
* Template library import images constructor.
*
* Initializing the images import class used by the template library through
* the WordPress Filesystem API.
*
* @since 1.0.0
* @access public
*/
public function __construct() {
if ( ! function_exists( 'WP_Filesystem' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
WP_Filesystem();
}
}

View File

@@ -0,0 +1,671 @@
<?php
namespace Elementor\TemplateLibrary;
use Elementor\Api;
use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
use Elementor\Core\Settings\Manager as SettingsManager;
use Elementor\TemplateLibrary\Classes\Import_Images;
use Elementor\Plugin;
use Elementor\User;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor template library manager.
*
* Elementor template library manager handler class is responsible for
* initializing the template library.
*
* @since 1.0.0
*/
class Manager {
/**
* Registered template sources.
*
* Holds a list of all the supported sources with their instances.
*
* @access protected
*
* @var Source_Base[]
*/
protected $_registered_sources = [];
/**
* Imported template images.
*
* Holds an instance of `Import_Images` class.
*
* @access private
*
* @var Import_Images
*/
private $_import_images = null;
/**
* Template library manager constructor.
*
* Initializing the template library manager by registering default template
* sources and initializing ajax calls.
*
* @since 1.0.0
* @access public
*/
public function __construct() {
$this->register_default_sources();
$this->add_actions();
}
/**
* @since 2.3.0
* @access public
*/
public function add_actions() {
add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
add_action( 'wp_ajax_elementor_library_direct_actions', [ $this, 'handle_direct_actions' ] );
// TODO: bc since 2.3.0
add_action( 'wp_ajax_elementor_update_templates', function() {
if ( ! isset( $_POST['templates'] ) ) {
return;
}
foreach ( $_POST['templates'] as & $template ) {
if ( ! isset( $template['content'] ) ) {
return;
}
$template['content'] = stripslashes( $template['content'] );
}
wp_send_json_success( $this->handle_ajax_request( 'update_templates', $_POST ) );
} );
}
/**
* Get `Import_Images` instance.
*
* Retrieve the instance of the `Import_Images` class.
*
* @since 1.0.0
* @access public
*
* @return Import_Images Imported images instance.
*/
public function get_import_images_instance() {
if ( null === $this->_import_images ) {
$this->_import_images = new Import_Images();
}
return $this->_import_images;
}
/**
* Register template source.
*
* Used to register new template sources displayed in the template library.
*
* @since 1.0.0
* @access public
*
* @param string $source_class The name of source class.
* @param array $args Optional. Class arguments. Default is an
* empty array.
*
* @return \WP_Error|true True if the source was registered, `WP_Error`
* otherwise.
*/
public function register_source( $source_class, $args = [] ) {
if ( ! class_exists( $source_class ) ) {
return new \WP_Error( 'source_class_name_not_exists' );
}
$source_instance = new $source_class( $args );
if ( ! $source_instance instanceof Source_Base ) {
return new \WP_Error( 'wrong_instance_source' );
}
$source_id = $source_instance->get_id();
if ( isset( $this->_registered_sources[ $source_id ] ) ) {
return new \WP_Error( 'source_exists' );
}
$this->_registered_sources[ $source_id ] = $source_instance;
return true;
}
/**
* Unregister template source.
*
* Remove an existing template sources from the list of registered template
* sources.
*
* @deprecated 2.7.0
*
* @since 1.0.0
* @access public
*
* @param string $id The source ID.
*
* @return bool Whether the source was unregistered.
*/
public function unregister_source( $id ) {
return true;
}
/**
* Get registered template sources.
*
* Retrieve registered template sources.
*
* @since 1.0.0
* @access public
*
* @return Source_Base[] Registered template sources.
*/
public function get_registered_sources() {
return $this->_registered_sources;
}
/**
* Get template source.
*
* Retrieve single template sources for a given template ID.
*
* @since 1.0.0
* @access public
*
* @param string $id The source ID.
*
* @return false|Source_Base Template sources if one exist, False otherwise.
*/
public function get_source( $id ) {
$sources = $this->get_registered_sources();
if ( ! isset( $sources[ $id ] ) ) {
return false;
}
return $sources[ $id ];
}
/**
* Get templates.
*
* Retrieve all the templates from all the registered sources.
*
* @since 1.0.0
* @access public
*
* @return array Templates array.
*/
public function get_templates() {
$templates = [];
foreach ( $this->get_registered_sources() as $source ) {
$templates = array_merge( $templates, $source->get_items() );
}
return $templates;
}
/**
* Get library data.
*
* Retrieve the library data.
*
* @since 1.9.0
* @access public
*
* @param array $args Library arguments.
*
* @return array Library data.
*/
public function get_library_data( array $args ) {
$library_data = Api::get_library_data( ! empty( $args['sync'] ) );
// Ensure all document are registered.
Plugin::$instance->documents->get_document_types();
return [
'templates' => $this->get_templates(),
'config' => $library_data['types_data'],
];
}
/**
* Save template.
*
* Save new or update existing template on the database.
*
* @since 1.0.0
* @access public
*
* @param array $args Template arguments.
*
* @return \WP_Error|int The ID of the saved/updated template.
*/
public function save_template( array $args ) {
$validate_args = $this->ensure_args( [ 'post_id', 'source', 'content', 'type' ], $args );
if ( is_wp_error( $validate_args ) ) {
return $validate_args;
}
$source = $this->get_source( $args['source'] );
if ( ! $source ) {
return new \WP_Error( 'template_error', 'Template source not found.' );
}
$args['content'] = json_decode( $args['content'], true );
$page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['post_id'] );
$args['page_settings'] = $page->get_data( 'settings' );
$template_id = $source->save_item( $args );
if ( is_wp_error( $template_id ) ) {
return $template_id;
}
return $source->get_item( $template_id );
}
/**
* Update template.
*
* Update template on the database.
*
* @since 1.0.0
* @access public
*
* @param array $template_data New template data.
*
* @return \WP_Error|Source_Base Template sources instance if the templates
* was updated, `WP_Error` otherwise.
*/
public function update_template( array $template_data ) {
$validate_args = $this->ensure_args( [ 'source', 'content', 'type' ], $template_data );
if ( is_wp_error( $validate_args ) ) {
return $validate_args;
}
$source = $this->get_source( $template_data['source'] );
if ( ! $source ) {
return new \WP_Error( 'template_error', 'Template source not found.' );
}
$template_data['content'] = json_decode( $template_data['content'], true );
$update = $source->update_item( $template_data );
if ( is_wp_error( $update ) ) {
return $update;
}
return $source->get_item( $template_data['id'] );
}
/**
* Update templates.
*
* Update template on the database.
*
* @since 1.0.0
* @access public
*
* @param array $args Template arguments.
*
* @return \WP_Error|true True if templates updated, `WP_Error` otherwise.
*/
public function update_templates( array $args ) {
foreach ( $args['templates'] as $template_data ) {
$result = $this->update_template( $template_data );
if ( is_wp_error( $result ) ) {
return $result;
}
}
return true;
}
/**
* Get template data.
*
* Retrieve the template data.
*
* @since 1.5.0
* @access public
*
* @param array $args Template arguments.
*
* @return \WP_Error|bool|array ??
*/
public function get_template_data( array $args ) {
$validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
if ( is_wp_error( $validate_args ) ) {
return $validate_args;
}
if ( isset( $args['edit_mode'] ) ) {
Plugin::$instance->editor->set_edit_mode( $args['edit_mode'] );
}
$source = $this->get_source( $args['source'] );
if ( ! $source ) {
return new \WP_Error( 'template_error', 'Template source not found.' );
}
do_action( 'elementor/template-library/before_get_source_data', $args, $source );
$data = $source->get_data( $args );
do_action( 'elementor/template-library/after_get_source_data', $args, $source );
return $data;
}
/**
* Delete template.
*
* Delete template from the database.
*
* @since 1.0.0
* @access public
*
* @param array $args Template arguments.
*
* @return \WP_Post|\WP_Error|false|null Post data on success, false or null
* or 'WP_Error' on failure.
*/
public function delete_template( array $args ) {
$validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
if ( is_wp_error( $validate_args ) ) {
return $validate_args;
}
$source = $this->get_source( $args['source'] );
if ( ! $source ) {
return new \WP_Error( 'template_error', 'Template source not found.' );
}
return $source->delete_template( $args['template_id'] );
}
/**
* Export template.
*
* Export template to a file.
*
* @since 1.0.0
* @access public
*
* @param array $args Template arguments.
*
* @return mixed Whether the export succeeded or failed.
*/
public function export_template( array $args ) {
$validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
if ( is_wp_error( $validate_args ) ) {
return $validate_args;
}
$source = $this->get_source( $args['source'] );
if ( ! $source ) {
return new \WP_Error( 'template_error', 'Template source not found' );
}
return $source->export_template( $args['template_id'] );
}
/**
* @since 2.3.0
* @access public
*/
public function direct_import_template() {
/** @var Source_Local $source */
$source = $this->get_source( 'local' );
return $source->import_template( $_FILES['file']['name'], $_FILES['file']['tmp_name'] );
}
/**
* Import template.
*
* Import template from a file.
*
* @since 1.0.0
* @access public
*
* @param array $data
*
* @return mixed Whether the export succeeded or failed.
*/
public function import_template( array $data ) {
/** @var Source_Local $source */
$file_content = base64_decode( $data['fileData'] );
$tmp_file = tmpfile();
fwrite( $tmp_file, $file_content );
$source = $this->get_source( 'local' );
$result = $source->import_template( $data['fileName'], stream_get_meta_data( $tmp_file )['uri'] );
fclose( $tmp_file );
return $result;
}
/**
* Mark template as favorite.
*
* Add the template to the user favorite templates.
*
* @since 1.9.0
* @access public
*
* @param array $args Template arguments.
*
* @return mixed Whether the template marked as favorite.
*/
public function mark_template_as_favorite( $args ) {
$validate_args = $this->ensure_args( [ 'source', 'template_id', 'favorite' ], $args );
if ( is_wp_error( $validate_args ) ) {
return $validate_args;
}
$source = $this->get_source( $args['source'] );
return $source->mark_as_favorite( $args['template_id'], filter_var( $args['favorite'], FILTER_VALIDATE_BOOLEAN ) );
}
/**
* Register default template sources.
*
* Register the 'local' and 'remote' template sources that Elementor use by
* default.
*
* @since 1.0.0
* @access private
*/
private function register_default_sources() {
$sources = [
'local',
'remote',
];
foreach ( $sources as $source_filename ) {
$class_name = ucwords( $source_filename );
$class_name = str_replace( '-', '_', $class_name );
$this->register_source( __NAMESPACE__ . '\Source_' . $class_name );
}
}
/**
* Handle ajax request.
*
* Fire authenticated ajax actions for any given ajax request.
*
* @since 1.0.0
* @access private
*
* @param string $ajax_request Ajax request.
*
* @param array $data
*
* @return mixed
* @throws \Exception
*/
private function handle_ajax_request( $ajax_request, array $data ) {
if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
throw new \Exception( 'Access Denied' );
}
if ( ! empty( $data['editor_post_id'] ) ) {
$editor_post_id = absint( $data['editor_post_id'] );
if ( ! get_post( $editor_post_id ) ) {
throw new \Exception( __( 'Post not found.', 'elementor' ) );
}
Plugin::$instance->db->switch_to_post( $editor_post_id );
}
$result = call_user_func( [ $this, $ajax_request ], $data );
if ( is_wp_error( $result ) ) {
throw new \Exception( $result->get_error_message() );
}
return $result;
}
/**
* Init ajax calls.
*
* Initialize template library ajax calls for allowed ajax requests.
*
* @since 2.3.0
* @access public
*
* @param Ajax $ajax
*/
public function register_ajax_actions( Ajax $ajax ) {
$library_ajax_requests = [
'get_library_data',
'get_template_data',
'save_template',
'update_templates',
'delete_template',
'import_template',
'mark_template_as_favorite',
];
foreach ( $library_ajax_requests as $ajax_request ) {
$ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) {
return $this->handle_ajax_request( $ajax_request, $data );
} );
}
}
/**
* @since 2.3.0
* @access public
*/
public function handle_direct_actions() {
if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
return;
}
/** @var Ajax $ajax */
$ajax = Plugin::$instance->common->get_component( 'ajax' );
if ( ! $ajax->verify_request_nonce() ) {
$this->handle_direct_action_error( 'Access Denied' );
}
$action = $_REQUEST['library_action'];
$result = $this->$action( $_REQUEST );
if ( is_wp_error( $result ) ) {
/** @var \WP_Error $result */
$this->handle_direct_action_error( $result->get_error_message() . '.' );
}
$callback = "on_{$action}_success";
if ( method_exists( $this, $callback ) ) {
$this->$callback( $result );
}
die;
}
/**
* On successful template import.
*
* Redirect the user to the template library after template import was
* successful finished.
*
* @since 2.3.0
* @access private
*/
private function on_direct_import_template_success() {
wp_safe_redirect( admin_url( Source_Local::ADMIN_MENU_SLUG ) );
}
/**
* @since 2.3.0
* @access private
*/
private function handle_direct_action_error( $message ) {
_default_wp_die_handler( $message, 'Elementor Library' );
}
/**
* Ensure arguments exist.
*
* Checks whether the required arguments exist in the specified arguments.
*
* @since 1.0.0
* @access private
*
* @param array $required_args Required arguments to check whether they
* exist.
* @param array $specified_args The list of all the specified arguments to
* check against.
*
* @return \WP_Error|true True on success, 'WP_Error' otherwise.
*/
private function ensure_args( array $required_args, array $specified_args ) {
$not_specified_args = array_diff( $required_args, array_keys( array_filter( $specified_args ) ) );
if ( $not_specified_args ) {
return new \WP_Error( 'arguments_not_specified', sprintf( 'The required argument(s) "%s" not specified.', implode( ', ', $not_specified_args ) ) );
}
return true;
}
}

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;
}
}

View File

@@ -0,0 +1,233 @@
<?php
namespace Elementor\TemplateLibrary;
use Elementor\Api;
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 {
/**
* 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 __( '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. Nou used in remote source.
*
* @return array Remote templates.
*/
public function get_items( $args = [] ) {
$library_data = Api::get_library_data();
$templates = [];
if ( ! empty( $library_data['templates'] ) ) {
foreach ( $library_data['templates'] 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;
}
// 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 );
}
return $data;
}
/**
* @since 2.2.0
* @access private
*/
private function prepare_template( array $template_data ) {
$favorite_templates = $this->get_user_meta( 'favorites' );
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'],
'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'] ] ),
];
}
}