| 1 |
<?php |
| 2 |
namespace Elementor\TemplateLibrary; |
| 3 |
|
| 4 |
use Elementor\Api; |
| 5 |
use Elementor\Plugin; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Elementor template library remote source. |
| 13 |
* |
| 14 |
* Elementor template library remote source handler class is responsible for |
| 15 |
* handling remote templates from Elementor.com servers. |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
class Source_Remote extends Source_Base { |
| 20 |
|
| 21 |
/** |
| 22 |
* Get remote template ID. |
| 23 |
* |
| 24 |
* Retrieve the remote template ID. |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
* @access public |
| 28 |
* |
| 29 |
* @return string The remote template ID. |
| 30 |
*/ |
| 31 |
public function get_id() { |
| 32 |
return 'remote'; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get remote template title. |
| 37 |
* |
| 38 |
* Retrieve the remote template title. |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
* @access public |
| 42 |
* |
| 43 |
* @return string The remote template title. |
| 44 |
*/ |
| 45 |
public function get_title() { |
| 46 |
return esc_html__( 'Remote', 'elementor' ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Register remote template data. |
| 51 |
* |
| 52 |
* Used to register custom template data like a post type, a taxonomy or any |
| 53 |
* other data. |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* @access public |
| 57 |
*/ |
| 58 |
public function register_data() {} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get remote templates. |
| 62 |
* |
| 63 |
* Retrieve remote templates from Elementor.com servers. |
| 64 |
* |
| 65 |
* @since 1.0.0 |
| 66 |
* @access public |
| 67 |
* |
| 68 |
* @param array $args Optional. Nou used in remote source. |
| 69 |
* |
| 70 |
* @return array Remote templates. |
| 71 |
*/ |
| 72 |
public function get_items( $args = [] ) { |
| 73 |
$library_data = Api::get_library_data(); |
| 74 |
|
| 75 |
$templates = []; |
| 76 |
|
| 77 |
if ( ! empty( $library_data['templates'] ) ) { |
| 78 |
foreach ( $library_data['templates'] as $template_data ) { |
| 79 |
$templates[] = $this->prepare_template( $template_data ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return $templates; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get remote template. |
| 88 |
* |
| 89 |
* Retrieve a single remote template from Elementor.com servers. |
| 90 |
* |
| 91 |
* @since 1.0.0 |
| 92 |
* @access public |
| 93 |
* |
| 94 |
* @param int $template_id The template ID. |
| 95 |
* |
| 96 |
* @return array Remote template. |
| 97 |
*/ |
| 98 |
public function get_item( $template_id ) { |
| 99 |
$templates = $this->get_items(); |
| 100 |
|
| 101 |
return $templates[ $template_id ]; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Save remote template. |
| 106 |
* |
| 107 |
* Remote template from Elementor.com servers cannot be saved on the |
| 108 |
* database as they are retrieved from remote servers. |
| 109 |
* |
| 110 |
* @since 1.0.0 |
| 111 |
* @access public |
| 112 |
* |
| 113 |
* @param array $template_data Remote template data. |
| 114 |
* |
| 115 |
* @return \WP_Error |
| 116 |
*/ |
| 117 |
public function save_item( $template_data ) { |
| 118 |
return new \WP_Error( 'invalid_request', 'Cannot save template to a remote source' ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Update remote template. |
| 123 |
* |
| 124 |
* Remote template from Elementor.com servers cannot be updated on the |
| 125 |
* database as they are retrieved from remote servers. |
| 126 |
* |
| 127 |
* @since 1.0.0 |
| 128 |
* @access public |
| 129 |
* |
| 130 |
* @param array $new_data New template data. |
| 131 |
* |
| 132 |
* @return \WP_Error |
| 133 |
*/ |
| 134 |
public function update_item( $new_data ) { |
| 135 |
return new \WP_Error( 'invalid_request', 'Cannot update template to a remote source' ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Delete remote template. |
| 140 |
* |
| 141 |
* Remote template from Elementor.com servers cannot be deleted from the |
| 142 |
* database as they are retrieved from remote servers. |
| 143 |
* |
| 144 |
* @since 1.0.0 |
| 145 |
* @access public |
| 146 |
* |
| 147 |
* @param int $template_id The template ID. |
| 148 |
* |
| 149 |
* @return \WP_Error |
| 150 |
*/ |
| 151 |
public function delete_template( $template_id ) { |
| 152 |
return new \WP_Error( 'invalid_request', 'Cannot delete template from a remote source' ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Export remote template. |
| 157 |
* |
| 158 |
* Remote template from Elementor.com servers cannot be exported from the |
| 159 |
* database as they are retrieved from remote servers. |
| 160 |
* |
| 161 |
* @since 1.0.0 |
| 162 |
* @access public |
| 163 |
* |
| 164 |
* @param int $template_id The template ID. |
| 165 |
* |
| 166 |
* @return \WP_Error |
| 167 |
*/ |
| 168 |
public function export_template( $template_id ) { |
| 169 |
return new \WP_Error( 'invalid_request', 'Cannot export template from a remote source' ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get remote template data. |
| 174 |
* |
| 175 |
* Retrieve the data of a single remote template from Elementor.com servers. |
| 176 |
* |
| 177 |
* @since 1.5.0 |
| 178 |
* @access public |
| 179 |
* |
| 180 |
* @param array $args Custom template arguments. |
| 181 |
* @param string $context Optional. The context. Default is `display`. |
| 182 |
* |
| 183 |
* @return array|\WP_Error Remote Template data. |
| 184 |
*/ |
| 185 |
public function get_data( array $args, $context = 'display' ) { |
| 186 |
$data = Api::get_template_content( $args['template_id'] ); |
| 187 |
|
| 188 |
if ( is_wp_error( $data ) ) { |
| 189 |
return $data; |
| 190 |
} |
| 191 |
|
| 192 |
// BC. |
| 193 |
$data = (array) $data; |
| 194 |
|
| 195 |
$data['content'] = $this->replace_elements_ids( $data['content'] ); |
| 196 |
$data['content'] = $this->process_export_import_content( $data['content'], 'on_import' ); |
| 197 |
|
| 198 |
$post_id = $args['editor_post_id']; |
| 199 |
$document = Plugin::$instance->documents->get( $post_id ); |
| 200 |
if ( $document ) { |
| 201 |
$data['content'] = $document->get_elements_raw_data( $data['content'], true ); |
| 202 |
} |
| 203 |
|
| 204 |
return $data; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* @since 2.2.0 |
| 209 |
* @access private |
| 210 |
*/ |
| 211 |
private function prepare_template( array $template_data ) { |
| 212 |
$favorite_templates = $this->get_user_meta( 'favorites' ); |
| 213 |
|
| 214 |
return [ |
| 215 |
'template_id' => $template_data['id'], |
| 216 |
'source' => $this->get_id(), |
| 217 |
'type' => $template_data['type'], |
| 218 |
'subtype' => $template_data['subtype'], |
| 219 |
'title' => $template_data['title'], |
| 220 |
'thumbnail' => $template_data['thumbnail'], |
| 221 |
'date' => $template_data['tmpl_created'], |
| 222 |
'author' => $template_data['author'], |
| 223 |
'tags' => json_decode( $template_data['tags'] ), |
| 224 |
'isPro' => ( '1' === $template_data['is_pro'] ), |
| 225 |
'accessLevel' => $template_data['access_level'], |
| 226 |
'popularityIndex' => (int) $template_data['popularity_index'], |
| 227 |
'trendIndex' => (int) $template_data['trend_index'], |
| 228 |
'hasPageSettings' => ( '1' === $template_data['has_page_settings'] ), |
| 229 |
'url' => $template_data['url'], |
| 230 |
'favorite' => ! empty( $favorite_templates[ $template_data['id'] ] ), |
| 231 |
]; |
| 232 |
} |
| 233 |
} |
| 234 |
|