first commit
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
namespace Elementor\TemplateLibrary\Classes;
|
||||
|
||||
use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
|
||||
use Elementor\Core\Files\Uploads_Manager;
|
||||
use Elementor\Plugin;
|
||||
use Elementor\Utils;
|
||||
|
||||
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 ( isset( $attachment['tmp_name'] ) ) {
|
||||
// Used when called to import a directly-uploaded file.
|
||||
$filename = $attachment['name'];
|
||||
$file_content = false;
|
||||
|
||||
// security validation in case the tmp_name has been tampered with
|
||||
if ( is_uploaded_file( $attachment['tmp_name'] ) ) {
|
||||
$file_content = Utils::file_get_contents( $attachment['tmp_name'] );
|
||||
}
|
||||
} else {
|
||||
// Used when attachment information is passed to this method.
|
||||
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'] );
|
||||
|
||||
$request = wp_safe_remote_get( $attachment['url'] );
|
||||
|
||||
// Make sure the request returns a valid result.
|
||||
if ( is_wp_error( $request ) || ( ! empty( $request['response']['code'] ) && 200 !== (int) $request['response']['code'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$file_content = wp_remote_retrieve_body( $request );
|
||||
}
|
||||
|
||||
if ( empty( $file_content ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$filetype = wp_check_filetype( $filename );
|
||||
|
||||
// If the file type is not recognized by WordPress, exit here to avoid creation of an empty attachment document.
|
||||
if ( ! $filetype['ext'] ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( 'svg' === $filetype['ext'] ) {
|
||||
// In case that unfiltered-files upload is not enabled, SVG images should not be imported.
|
||||
if ( ! Uploads_Manager::are_unfiltered_uploads_enabled() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$svg_handler = Plugin::$instance->uploads_manager->get_file_type_handlers( 'svg' );
|
||||
|
||||
$file_content = $svg_handler->sanitizer( $file_content );
|
||||
};
|
||||
|
||||
$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', esc_html__( 'Invalid file type.', 'elementor' ) );
|
||||
}
|
||||
|
||||
$post_id = wp_insert_attachment( $post, $upload['file'], $parent_post_id );
|
||||
|
||||
apply_filters( 'elementor/template_library/import_images/new_attachment', $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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace Elementor\Includes\TemplateLibrary\Data;
|
||||
|
||||
use Elementor\User;
|
||||
use Elementor\TemplateLibrary\Source_Local;
|
||||
use Elementor\Data\V2\Base\Controller as Controller_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Controller extends Controller_Base {
|
||||
|
||||
public function get_name() {
|
||||
return 'template-library';
|
||||
}
|
||||
|
||||
public function register_endpoints() {
|
||||
$this->register_endpoint( new Endpoints\Templates( $this ) );
|
||||
}
|
||||
|
||||
protected function register_index_endpoint() {
|
||||
// Bypass, currently does not required.
|
||||
}
|
||||
|
||||
public function get_permission_callback( $request ) {
|
||||
return User::is_current_user_can_edit_post_type( Source_Local::CPT );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
namespace Elementor\Includes\TemplateLibrary\Data\Endpoints;
|
||||
|
||||
use Elementor\Data\V2\Base\Endpoint;
|
||||
use Elementor\Plugin;
|
||||
use Elementor\TemplateLibrary\Source_Local;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Templates extends Endpoint {
|
||||
|
||||
protected function register() {
|
||||
parent::register();
|
||||
|
||||
$document_types = Plugin::$instance->documents->get_document_types( [
|
||||
'show_in_library' => true,
|
||||
] );
|
||||
|
||||
$this->register_route( '', \WP_REST_Server::CREATABLE, [
|
||||
'is_multi' => true,
|
||||
'title' => [
|
||||
'required' => false,
|
||||
'type' => 'string',
|
||||
'description' => 'The title of the document',
|
||||
],
|
||||
'type' => [
|
||||
'required' => true,
|
||||
'description' => 'The document type.',
|
||||
'type' => 'string',
|
||||
'enum' => array_keys( $document_types ),
|
||||
],
|
||||
'content' => [
|
||||
'required' => false,
|
||||
'description' => 'Elementor data object',
|
||||
'type' => 'object',
|
||||
],
|
||||
] );
|
||||
}
|
||||
|
||||
public function get_name() {
|
||||
return 'templates';
|
||||
}
|
||||
|
||||
public function get_format() {
|
||||
return 'template-library/templates';
|
||||
}
|
||||
|
||||
public function get_items( $request ) {
|
||||
return $this->reorder_categories( Plugin::$instance->templates_manager->get_library_data( [ 'filter_sources' => [ $request->get_param( 'source' ) ] ] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Move the '404 page' category to the end of the list
|
||||
*
|
||||
* @param array $library_data
|
||||
* @return array
|
||||
*/
|
||||
private function reorder_categories( array $library_data ): array {
|
||||
$not_found_category = '404 page';
|
||||
|
||||
$key = array_search( $not_found_category, $library_data['config']['block']['categories'] );
|
||||
if ( false === $key ) {
|
||||
return $library_data;
|
||||
}
|
||||
|
||||
array_splice( $library_data['config']['block']['categories'], $key, 1 );
|
||||
$library_data['config']['block']['categories'][] = $not_found_category;
|
||||
|
||||
return $library_data;
|
||||
}
|
||||
|
||||
public function create_items( $request ) {
|
||||
/** @var Source_Local $source */
|
||||
$source = Plugin::$instance->templates_manager->get_source( 'local' );
|
||||
|
||||
$result = $source->save_item( [
|
||||
'title' => $request->get_param( 'title' ),
|
||||
'type' => $request->get_param( 'type' ),
|
||||
'content' => $request->get_param( 'content' ),
|
||||
'page_settings' => $request->get_param( 'page_settings' ),
|
||||
] );
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $source->get_item( $result );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace Elementor\TemplateLibrary\Forms;
|
||||
|
||||
use Elementor\Controls_Manager;
|
||||
use Elementor\Controls_Stack;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class New_Template_Form extends Controls_Stack {
|
||||
|
||||
public function get_name() {
|
||||
return 'add-template-form';
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception Exception Throws an exception if the control type is not supported.
|
||||
*/
|
||||
public function render() {
|
||||
foreach ( $this->get_controls() as $control ) {
|
||||
switch ( $control['type'] ) {
|
||||
case Controls_Manager::SELECT:
|
||||
$this->render_select( $control );
|
||||
break;
|
||||
default:
|
||||
throw new \Exception( sprintf( "'%s' control type is not supported.", esc_html( $control['type'] ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function render_select( $control_settings ) {
|
||||
$control_id = "elementor-new-template__form__{$control_settings['name']}";
|
||||
$wrapper_class = isset( $control_settings['wrapper_class'] ) ? $control_settings['wrapper_class'] : '';
|
||||
?>
|
||||
<div id="<?php echo esc_attr( $control_id ); ?>__wrapper" class="elementor-form-field <?php echo esc_attr( $wrapper_class ); ?>">
|
||||
<label for="<?php echo esc_attr( $control_id ); ?>" class="elementor-form-field__label">
|
||||
<?php echo esc_html( $control_settings['label'] ); ?>
|
||||
</label>
|
||||
<div class="elementor-form-field__select__wrapper">
|
||||
<select id="<?php echo esc_attr( $control_id ); ?>" class="elementor-form-field__select" name="meta[<?php echo esc_html( $control_settings['name'] ); ?>]">
|
||||
<?php
|
||||
foreach ( $control_settings['options'] as $key => $value ) {
|
||||
printf( '<option value="%1$s">%2$s</option>', esc_html( $key ), esc_html( $value ) );
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
1301
wp-content/plugins/elementor/includes/template-library/manager.php
Normal file
1301
wp-content/plugins/elementor/includes/template-library/manager.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,543 @@
|
||||
<?php
|
||||
namespace Elementor\TemplateLibrary;
|
||||
|
||||
use Elementor\Controls_Stack;
|
||||
use Elementor\Core\Settings\Page\Model;
|
||||
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 apply_filters( 'elementor/document/element/replace_id', $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;
|
||||
}
|
||||
|
||||
public function get_item_children( array $args = [] ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function search_templates( array $args = [] ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function save_folder( array $folder_data = [] ) {
|
||||
return new \WP_Error( 'template_error', 'Folders cannot be created in this source' );
|
||||
}
|
||||
|
||||
public function move_template_to_folder( array $folder_data = [] ) {
|
||||
return new \WP_Error( 'template_error', 'Templates cannot be moved in this source' );
|
||||
}
|
||||
|
||||
public function move_bulk_templates_to_folder( array $folder_data = [] ) {
|
||||
return new \WP_Error( 'template_error', 'Templates cannot be moved in this source' );
|
||||
}
|
||||
|
||||
public function save_bulk_items( array $args = [] ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function get_bulk_items( array $args = [] ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $template_id
|
||||
* @return \Elementor\Core\Base\Document|\WP_Error
|
||||
*/
|
||||
public function create_document_for_preview( int $template_id ) {
|
||||
return new \WP_Error( 'template_error', 'Can not generate preview for this source' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $template_id
|
||||
* @param mixed $data
|
||||
* @return string|\WP_Error
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function save_item_preview( int $template_id, $data ) {
|
||||
return new \WP_Error( 'template_error', 'Cannot save previews for this source' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $template_id
|
||||
* @param string $error
|
||||
* @return string|\WP_Error
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function mark_preview_as_failed( int $template_id, string $error ) {
|
||||
return new \WP_Error( 'template_error', 'Cannot mark preview as failed for this source' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $template_ids
|
||||
* @return bool|\WP_Error
|
||||
*/
|
||||
public function bulk_delete_items( array $template_ids ) {
|
||||
return new \WP_Error( 'template_error', 'Bulk delete action is not supported for this source' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $template_ids
|
||||
* @return bool|\WP_Error
|
||||
*/
|
||||
public function bulk_undo_delete_items( array $template_ids ) {
|
||||
return new \WP_Error( 'template_error', 'Undo delete action is not supported for this source' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|\WP_Error
|
||||
*/
|
||||
public function get_quota() {
|
||||
return new \WP_Error( 'template_error', 'This source does not support quotas' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|\WP_Error
|
||||
*/
|
||||
public function import_template( $name, $path ) {
|
||||
return new \WP_Error( 'template_error', 'This source does not support import' );
|
||||
}
|
||||
|
||||
public function supports_quota(): bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function validate_quota( array $items ) {
|
||||
return new \WP_Error( 'quota_error', 'This source does not support quota validation' );
|
||||
}
|
||||
|
||||
public function prepare_import_template_data( $file_path ) {
|
||||
$data = json_decode( Utils::file_get_contents( $file_path ), true );
|
||||
|
||||
if ( empty( $data ) ) {
|
||||
return new \WP_Error( 'file_error', 'Invalid File' );
|
||||
}
|
||||
|
||||
$content = $data['content'];
|
||||
|
||||
if ( ! is_array( $content ) ) {
|
||||
return new \WP_Error( 'file_error', 'Invalid Content In File' );
|
||||
}
|
||||
|
||||
$content = $this->process_export_import_content( $content, 'on_import' );
|
||||
|
||||
$content = apply_filters(
|
||||
"elementor/template_library/sources/{$this->get_id()}/import/elements",
|
||||
$content
|
||||
);
|
||||
|
||||
$page_settings = [];
|
||||
|
||||
if ( ! empty( $data['page_settings'] ) ) {
|
||||
$page = new Model( [
|
||||
'id' => 0,
|
||||
'settings' => $data['page_settings'],
|
||||
] );
|
||||
|
||||
$page_settings_data = $this->process_element_export_import_content( $page, 'on_import' );
|
||||
|
||||
if ( ! empty( $page_settings_data['settings'] ) ) {
|
||||
$page_settings = $page_settings_data['settings'];
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'content' => $content,
|
||||
'page_settings' => $page_settings,
|
||||
'title' => $data['title'],
|
||||
'type' => $data['type'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Send file headers.
|
||||
*
|
||||
* Set the file header when export template data to a file.
|
||||
*
|
||||
* @since 1.6.0
|
||||
* @access protected
|
||||
*
|
||||
* @param string $file_name File name.
|
||||
* @param int $file_size File size.
|
||||
*/
|
||||
protected function send_file_headers( $file_name, $file_size ) {
|
||||
header( 'Content-Type: application/octet-stream' );
|
||||
header( 'Content-Disposition: attachment; filename=' . $file_name );
|
||||
header( 'Expires: 0' );
|
||||
header( 'Cache-Control: must-revalidate' );
|
||||
header( 'Pragma: public' );
|
||||
header( 'Content-Length: ' . $file_size );
|
||||
}
|
||||
|
||||
protected function filesize( $path ) {
|
||||
return filesize( $path );
|
||||
}
|
||||
|
||||
protected function serve_zip( $zip_complete_path ): void {
|
||||
@ob_end_flush();
|
||||
@readfile( $zip_complete_path );
|
||||
}
|
||||
|
||||
protected function serve_file( string $file_content ): void {
|
||||
@ob_end_clean();
|
||||
flush();
|
||||
|
||||
// PHPCS - Export widget json
|
||||
echo $file_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,478 @@
|
||||
<?php
|
||||
namespace Elementor\TemplateLibrary;
|
||||
|
||||
use Elementor\Core\Base\Document;
|
||||
use Elementor\Core\Utils\Exceptions;
|
||||
use Elementor\Modules\CloudLibrary\Connect\Cloud_Library;
|
||||
use Elementor\Modules\CloudLibrary\Documents\Cloud_Template_Preview;
|
||||
use Elementor\Plugin;
|
||||
use Elementor\DB;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Source_Cloud extends Source_Base {
|
||||
const FOLDER_RESOURCE_TYPE = 'FOLDER';
|
||||
const TEMPLATE_RESOURCE_TYPE = 'TEMPLATE';
|
||||
|
||||
protected function get_app(): Cloud_Library {
|
||||
$cloud_library_app = Plugin::$instance->common->get_component( 'connect' )->get_app( 'cloud-library' );
|
||||
|
||||
if ( ! $cloud_library_app ) {
|
||||
$error_message = esc_html__( 'Cloud-Library is not instantiated.', 'elementor' );
|
||||
|
||||
throw new \Exception( $error_message, Exceptions::FORBIDDEN ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
|
||||
}
|
||||
|
||||
return $cloud_library_app;
|
||||
}
|
||||
|
||||
public function get_id(): string {
|
||||
return 'cloud';
|
||||
}
|
||||
|
||||
public function get_title(): string {
|
||||
return esc_html__( 'Cloud Library', 'elementor' );
|
||||
}
|
||||
|
||||
public function register_data() {}
|
||||
|
||||
public function supports_quota(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function get_items( $args = [] ) {
|
||||
return $this->get_app()->get_resources( $args );
|
||||
}
|
||||
|
||||
public function get_item_children( array $args = [] ) {
|
||||
return $this->get_app()->get_resources( [ 'parentId' => $args['template_id'] ] );
|
||||
}
|
||||
|
||||
public function get_item( $id ) {
|
||||
return $this->get_app()->get_resource( [ 'id' => $id ] );
|
||||
}
|
||||
|
||||
public function get_data( array $args ) {
|
||||
$data = $this->get_app()->get_resource( [ 'id' => $args['template_id'] ] );
|
||||
|
||||
if ( is_wp_error( $data ) || empty( $data['content'] ) ) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$decoded_data = json_decode( $data['content'], true );
|
||||
$data['content'] = $decoded_data['content'];
|
||||
|
||||
Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
|
||||
|
||||
$data['content'] = $this->replace_elements_ids( $data['content'] );
|
||||
$data['content'] = $this->process_export_import_content( $data['content'], 'on_import' );
|
||||
|
||||
if ( ! empty( $args['editor_post_id'] ) ) {
|
||||
$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 );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $args['with_page_settings'] ) ) {
|
||||
$data['page_settings'] = $decoded_data['page_settings'];
|
||||
}
|
||||
|
||||
// After the upload complete, set the elementor upload state back to false
|
||||
Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function delete_template( $template_id ) {
|
||||
return $this->get_app()->delete_resource( $template_id );
|
||||
}
|
||||
|
||||
public function save_item( $template_data ): int {
|
||||
$app = $this->get_app();
|
||||
|
||||
$resource_data = $this->format_resource_item_for_create( $template_data );
|
||||
|
||||
$response = $app->post_resource( $resource_data );
|
||||
|
||||
return (int) $response['id'];
|
||||
}
|
||||
|
||||
private function format_resource_item_for_create( $template_data ) {
|
||||
return [
|
||||
'title' => $template_data['title'] ?? esc_html__( '(no title)', 'elementor' ),
|
||||
'type' => self::TEMPLATE_RESOURCE_TYPE,
|
||||
'templateType' => $template_data['type'],
|
||||
'parentId' => ! empty( $template_data['parentId'] ) ? (int) $template_data['parentId'] : null,
|
||||
'content' => wp_json_encode( [
|
||||
'content' => $template_data['content'],
|
||||
'page_settings' => $template_data['page_settings'],
|
||||
] ),
|
||||
'hasPageSettings' => ! empty( $template_data['page_settings'] ),
|
||||
];
|
||||
}
|
||||
|
||||
public function save_folder( array $folder_data = [] ) {
|
||||
$app = $this->get_app();
|
||||
|
||||
$resource_data = [
|
||||
'title' => $folder_data['title'] ?? esc_html__( 'New Folder', 'elementor' ),
|
||||
'type' => self::FOLDER_RESOURCE_TYPE,
|
||||
'templateType' => 'folder',
|
||||
'parentId' => null,
|
||||
];
|
||||
|
||||
$response = $app->post_resource( $resource_data );
|
||||
|
||||
return (int) $response['id'];
|
||||
}
|
||||
|
||||
public function update_item( $template_data ) {
|
||||
return $this->get_app()->update_resource( $template_data );
|
||||
}
|
||||
|
||||
public function search_templates( array $args = [] ) {
|
||||
return $this->get_app()->get_resources( $args );
|
||||
}
|
||||
|
||||
public function export_template( $id ) {
|
||||
$data = $this->get_app()->get_resource( [ 'id' => $id ] );
|
||||
|
||||
if ( is_wp_error( $data ) ) {
|
||||
return new \WP_Error( 'export_template_error', 'An error has occured' );
|
||||
}
|
||||
|
||||
if ( static::TEMPLATE_RESOURCE_TYPE === $data['type'] ) {
|
||||
$this->handle_export_file( $data );
|
||||
}
|
||||
|
||||
if ( static::FOLDER_RESOURCE_TYPE === $data['type'] ) {
|
||||
$this->handle_export_folder( $id );
|
||||
}
|
||||
}
|
||||
|
||||
protected function handle_export_file( array $data ): void {
|
||||
$file_data = $this->prepare_template_export( $data );
|
||||
|
||||
if ( is_wp_error( $file_data ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->send_file_headers( $file_data['name'], strlen( $file_data['content'] ) );
|
||||
|
||||
$this->serve_file( $file_data['content'] );
|
||||
}
|
||||
|
||||
protected function handle_export_folder( int $folder_id ) {
|
||||
$data = $this->get_item_children( [ 'template_id' => $folder_id ] );
|
||||
|
||||
if ( empty( $data['templates'] ) ) {
|
||||
throw new \Exception( 'Folder does not have any templates to export' );
|
||||
}
|
||||
|
||||
$template_ids = array_map( fn( $template ) => $template['template_id'], $data['templates'] );
|
||||
|
||||
$this->export_multiple_templates( $template_ids );
|
||||
}
|
||||
|
||||
private function prepare_template_export( $data ) {
|
||||
if ( empty( $data['content'] ) ) {
|
||||
throw new \Exception( 'Template data not found' );
|
||||
}
|
||||
|
||||
$data['content'] = json_decode( $data['content'], true );
|
||||
|
||||
if ( empty( $data['content']['content'] ) ) {
|
||||
throw new \Exception( 'The template is empty' );
|
||||
}
|
||||
|
||||
$export_data = [
|
||||
'content' => $data['content']['content'],
|
||||
'page_settings' => $data['content']['page_settings'] ?? [],
|
||||
'version' => DB::DB_VERSION,
|
||||
'title' => $data['title'],
|
||||
'type' => $data['templateType'],
|
||||
];
|
||||
|
||||
return [
|
||||
'name' => 'elementor-' . $data['id'] . '-' . gmdate( 'Y-m-d' ) . '.json',
|
||||
'content' => wp_json_encode( $export_data ),
|
||||
];
|
||||
}
|
||||
|
||||
public function export_multiple_templates( array $template_ids ) {
|
||||
$files = [];
|
||||
$temp_path = Plugin::$instance->uploads_manager->create_unique_dir();
|
||||
|
||||
foreach ( $template_ids as $template_id ) {
|
||||
$files[] = $this->get_file_item( $template_id, $temp_path );
|
||||
}
|
||||
|
||||
if ( empty( $files ) ) {
|
||||
throw new \Exception( 'There are no files to export (probably all the requested templates are empty).' );
|
||||
}
|
||||
|
||||
list( $zip_archive_filename, $zip_complete_path ) = $this->handle_zip_file( $temp_path, $files );
|
||||
|
||||
$this->send_file_headers( $zip_archive_filename, $this->filesize( $zip_complete_path ) );
|
||||
|
||||
$this->serve_zip( $zip_complete_path );
|
||||
|
||||
Plugin::$instance->uploads_manager->remove_file_or_dir( $temp_path );
|
||||
}
|
||||
|
||||
protected function handle_zip_file( string $temp_path, array $files ): array {
|
||||
if ( ! class_exists( 'ZipArchive' ) ) {
|
||||
throw new \Error( 'ZipArchive module missing' );
|
||||
}
|
||||
|
||||
$zip_archive_filename = 'elementor-templates-' . gmdate( 'Y-m-d' ) . '.zip';
|
||||
|
||||
$zip_archive = new \ZipArchive();
|
||||
|
||||
$zip_complete_path = $temp_path . '/' . $zip_archive_filename;
|
||||
|
||||
$zip_archive->open( $zip_complete_path, \ZipArchive::CREATE );
|
||||
|
||||
foreach ( $files as $file ) {
|
||||
$zip_archive->addFile( $file['path'], $file['name'] );
|
||||
}
|
||||
|
||||
$zip_archive->close();
|
||||
|
||||
return [ $zip_archive_filename, $zip_complete_path ];
|
||||
}
|
||||
|
||||
private function get_file_item( $template_id, string $temp_path ) {
|
||||
$data = $this->get_app()->get_resource( [ 'id' => $template_id ] );
|
||||
$file_data = $this->prepare_template_export( $data );
|
||||
|
||||
if ( is_wp_error( $file_data ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$complete_path = $temp_path . $file_data['name'];
|
||||
|
||||
$put_contents = file_put_contents( $complete_path, $file_data['content'] );
|
||||
|
||||
if ( ! $put_contents ) {
|
||||
throw new \Exception( sprintf( 'Cannot create file "%s".', esc_html( $file_data['name'] ) ) );
|
||||
}
|
||||
|
||||
return [
|
||||
'path' => $complete_path,
|
||||
'name' => $file_data['name'],
|
||||
];
|
||||
}
|
||||
|
||||
public function move_template_to_folder( array $args = [] ) {
|
||||
$move_args = [
|
||||
'title' => $args['title'],
|
||||
'id' => $args['from_template_id'],
|
||||
'parentId' => ! empty( $args['parentId'] ) ? (int) $args['parentId'] : '',
|
||||
];
|
||||
|
||||
return $this->update_item( $move_args );
|
||||
}
|
||||
|
||||
public function move_bulk_templates_to_folder( array $args = [] ) {
|
||||
$move_args = [
|
||||
'ids' => $args['from_template_id'],
|
||||
'parentId' => ! empty( $args['parentId'] ) ? (int) $args['parentId'] : null,
|
||||
];
|
||||
|
||||
return $this->get_app()->bulk_move_templates( $move_args );
|
||||
}
|
||||
|
||||
public function save_item_preview( $template_id, $data ) {
|
||||
return $this->get_app()->update_resource_preview( $template_id, $data );
|
||||
}
|
||||
|
||||
public function mark_preview_as_failed( $template_id, $data ) {
|
||||
return $this->get_app()->mark_preview_as_failed( $template_id, $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $template_id
|
||||
* @return Document|\WP_Error
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function create_document_for_preview( int $template_id ) {
|
||||
if ( ! current_user_can( 'edit_posts' ) ) {
|
||||
return new \WP_Error( Exceptions::FORBIDDEN, esc_html__( 'You do not have permission to create preview documents.', 'elementor' ) );
|
||||
}
|
||||
|
||||
$cloud_library_app = $this->get_app();
|
||||
|
||||
$template = $cloud_library_app->get_resource( [ 'id' => $template_id ] );
|
||||
|
||||
if ( is_wp_error( $template ) ) {
|
||||
$error_message = $template->get_error_message();
|
||||
throw new \Exception( esc_html( $error_message ), Exceptions::FORBIDDEN ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
|
||||
}
|
||||
|
||||
$decoded_content = json_decode( $template['content'], true );
|
||||
|
||||
return $this->save_document_for_preview( [
|
||||
'content' => $decoded_content['content'],
|
||||
'page_settings' => $decoded_content['page_settings'],
|
||||
] );
|
||||
}
|
||||
|
||||
protected function save_document_for_preview( $template_content ) {
|
||||
$template_data = [
|
||||
'title' => esc_html__( '(no title)', 'elementor' ),
|
||||
'page_settings' => $template_content['page_settings'] ?? [],
|
||||
'status' => 'draft',
|
||||
'type' => 'container',
|
||||
];
|
||||
|
||||
$document = Plugin::$instance->documents->create(
|
||||
Cloud_Template_Preview::TYPE,
|
||||
[
|
||||
'post_title' => $template_data['title'],
|
||||
'post_status' => $template_data['status'],
|
||||
]
|
||||
);
|
||||
|
||||
if ( is_wp_error( $document ) ) {
|
||||
wp_die();
|
||||
}
|
||||
|
||||
$template_data['content'] = $this->replace_elements_ids( $template_content['content'] );
|
||||
|
||||
$document->save( [
|
||||
'elements' => $template_data['content'],
|
||||
'settings' => $template_data['page_settings'],
|
||||
] );
|
||||
|
||||
do_action( 'elementor/template-library/after_save_template', $document->get_main_id(), $template_data );
|
||||
do_action( 'elementor/template-library/after_update_template', $document->get_main_id(), $template_data );
|
||||
|
||||
return $document;
|
||||
}
|
||||
|
||||
public function bulk_delete_items( array $template_ids ) {
|
||||
return $this->get_app()->bulk_delete_resources( $template_ids );
|
||||
}
|
||||
|
||||
|
||||
public function bulk_undo_delete_items( array $template_ids ) {
|
||||
return $this->get_app()->bulk_undo_delete_resources( $template_ids );
|
||||
}
|
||||
|
||||
public function save_bulk_items( array $data = [] ) {
|
||||
$items = [];
|
||||
|
||||
foreach ( $data as $template_data ) {
|
||||
$items[] = $this->format_resource_item_for_create( $template_data );
|
||||
}
|
||||
|
||||
return $this->get_app()->post_bulk_resources( $items );
|
||||
}
|
||||
|
||||
public function get_bulk_items( array $args = [] ) {
|
||||
return $this->get_app()->get_bulk_resources_with_content( $args );
|
||||
}
|
||||
|
||||
public function get_quota() {
|
||||
return $this->get_app()->get_quota();
|
||||
}
|
||||
|
||||
public function import_template( $name, $path ) {
|
||||
if ( empty( $path ) ) {
|
||||
return new \WP_Error( 'file_error', 'Please upload a file to import' );
|
||||
}
|
||||
|
||||
Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
|
||||
|
||||
$items = [];
|
||||
|
||||
$quota = $this->get_quota();
|
||||
|
||||
if ( is_wp_error( $quota ) ) {
|
||||
return $quota;
|
||||
}
|
||||
|
||||
if ( $quota['currentUsage'] >= $quota['threshold'] ) {
|
||||
return new \WP_Error( 'quota_error', 'The upload failed because you’ve saved the maximum templates already.' );
|
||||
}
|
||||
|
||||
if ( 'zip' === pathinfo( $name, PATHINFO_EXTENSION ) ) {
|
||||
$extracted_files = Plugin::$instance->uploads_manager->extract_and_validate_zip( $path, [ 'json' ] );
|
||||
|
||||
if ( is_wp_error( $extracted_files ) ) {
|
||||
Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
|
||||
|
||||
return $extracted_files;
|
||||
}
|
||||
|
||||
$items_to_save = [];
|
||||
|
||||
foreach ( $extracted_files['files'] as $file_path ) {
|
||||
$prepared = $this->prepare_import_template_data( $file_path );
|
||||
|
||||
if ( is_wp_error( $prepared ) ) {
|
||||
Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
|
||||
|
||||
return $prepared;
|
||||
}
|
||||
|
||||
$items_to_save[] = $this->format_resource_item_for_create( $prepared );
|
||||
}
|
||||
|
||||
$is_quota_valid = $this->validate_quota( $items_to_save );
|
||||
|
||||
if ( is_wp_error( $is_quota_valid ) ) {
|
||||
return $is_quota_valid;
|
||||
}
|
||||
|
||||
if ( ! $is_quota_valid ) {
|
||||
return new \WP_Error( 'quota_error', 'The upload failed because it will pass the maximum templates you can save.' );
|
||||
}
|
||||
|
||||
$items = $this->get_app()->post_bulk_resources( $items_to_save );
|
||||
|
||||
Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
|
||||
} else {
|
||||
$prepared = $this->prepare_import_template_data( $path );
|
||||
|
||||
if ( is_wp_error( $prepared ) ) {
|
||||
return $prepared;
|
||||
}
|
||||
|
||||
$is_quota_valid = $this->validate_quota( [ $prepared ] );
|
||||
|
||||
if ( is_wp_error( $is_quota_valid ) ) {
|
||||
return $is_quota_valid;
|
||||
}
|
||||
|
||||
if ( ! $is_quota_valid ) {
|
||||
return new \WP_Error( 'quota_error', 'The upload failed because it will pass the maximum templates you can save.' );
|
||||
}
|
||||
|
||||
$item = $this->get_app()->post_resource( $this->format_resource_item_for_create( $prepared ) );
|
||||
|
||||
if ( is_wp_error( $item ) ) {
|
||||
return $item;
|
||||
}
|
||||
|
||||
$items[] = $item;
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
public function validate_quota( $items ) {
|
||||
$quota = $this->get_quota();
|
||||
|
||||
if ( is_wp_error( $quota ) ) {
|
||||
return $quota;
|
||||
}
|
||||
|
||||
return $quota['currentUsage'] + count( $items ) <= $quota['threshold'];
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,324 @@
|
||||
<?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_';
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
protected function get_templates_data( bool $force_update ): array {
|
||||
$experiments_manager = Plugin::$instance->experiments;
|
||||
$editor_layout_type = $experiments_manager->is_feature_active( 'container' ) ? 'container_flexbox' : '';
|
||||
|
||||
return $this->get_templates( $editor_layout_type );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the templates from a remote server and set a transient.
|
||||
*
|
||||
* @param string $editor_layout_type
|
||||
* @return array
|
||||
*/
|
||||
protected function get_templates( string $editor_layout_type ): array {
|
||||
$templates_data = $this->get_templates_remotely( $editor_layout_type );
|
||||
|
||||
return empty( $templates_data ) ? [] : $templates_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch templates from the remote server.
|
||||
*
|
||||
* @param string $editor_layout_type
|
||||
* @return array|false
|
||||
*/
|
||||
protected function get_templates_remotely( string $editor_layout_type ) {
|
||||
$response = wp_remote_get( static::API_TEMPLATES_URL, [
|
||||
'body' => $this->get_templates_body_args( $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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the body arguments for the remote request.
|
||||
*
|
||||
* @param string $editor_layout_type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_templates_body_args( string $editor_layout_type ): array {
|
||||
return [
|
||||
'plugin_version' => ELEMENTOR_VERSION,
|
||||
'editor_layout_type' => $editor_layout_type,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.2.0
|
||||
* @access private
|
||||
*/
|
||||
protected 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 );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user