PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.6.6
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.6.6
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Core / Platform / Elementor.php

Elementor.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.6.6, at includes/Core/Platform/Elementor.php

398 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Templately\Core\Platform;
3
4 use WP_Post;
5 use WP_Error;
6 use WP_Query;
7 use WP_REST_Response;
8
9 use Templately\API\Import;
10 use Templately\Core\Module;
11 use Templately\Utils\Helper;
12 use Templately\Utils\Options;
13 use Templately\Core\Platform;
14
15 use Elementor\Plugin;
16 use Elementor\Core\Kits\Documents\Kit;
17 use Elementor\Core\Settings\Manager as SettingsManager;
18 use Templately\Core\Importer\Elementor as ElementorImporter;
19 use Templately\Core\Importer\Utils\Utils;
20 use Elementor\TemplateLibrary\Source_Local as ElementorLocal;
21
22 class Elementor extends Platform {
23 private $id = 'elementor';
24
25 public function __construct() {
26 Module::get_instance()->add( (object) [
27 'id' => $this->id,
28 'object' => $this
29 ] );
30
31 $this->hooks();
32 }
33
34 /**
35 * Initializing Hooks
36 * @return void
37 */
38 public function hooks() {
39 if ( class_exists( '\Elementor\Plugin' ) ) {
40 add_action( 'elementor/preview/enqueue_styles', [ $this, 'styles' ] );
41 add_action( 'elementor/editor/before_enqueue_styles', [ $this, 'styles' ] );
42 add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'scripts' ] );
43 }
44 }
45
46 /**
47 * Assets Enqueueing
48 * @return void
49 */
50 public function styles() {
51 templately()->assets->enqueue( 'templately-elementor-preview', 'css/elementor.css' );
52 templately()->assets->enqueue( 'templately-tailwind', 'css/tailwind.css', ['templately-elementor-preview'] );
53 }
54
55 /**
56 * Assets Enqueueing
57 * @return void
58 */
59 public function scripts() {
60 templately()->assets->enqueue( 'templately-elementor', 'css/elementor.css' );
61 templately()->assets->enqueue( 'templately-tailwind', 'css/tailwind.css', ['templately-elementor'] );
62 templately()->assets->enqueue( 'templately-elementor', 'js/elementor.js', [ 'jquery' ], true );
63 templately()->admin->scripts( 'elementor' );
64 }
65
66 /**
67 * Determine Active UI Theme
68 * @return string
69 */
70 public function ui_theme() {
71 $ui_theme = SettingsManager::get_settings_managers( 'editorPreferences' )->get_model()->get_settings( 'ui_theme' );
72
73 return 'light' !== $ui_theme ? 'dark' : 'light';
74 }
75
76 /**
77 * Checking the template eligibility for import.
78 *
79 * @return WP_Error|void
80 */
81 protected function is_eligible( $template, $types = [ 'page', 'section', 'block' ] ) {
82 if ( ! Helper::is_plugin_active( 'elementor-pro/elementor-pro.php' ) &&
83 isset( $template['type'] ) &&
84 ! in_array( $template['type'], $types, true )
85 ) {
86 $message = sprintf(
87 __( 'You have to install/activate %s to import %s Template.', 'templately' ),
88 sprintf(
89 '<a target="_blank" href="%s">%s</a>',
90 'https://wpdeveloper.com/go/elementor',
91 'Elementor PRO'
92 ),
93 $template['type']
94 );
95
96 return Helper::error( 'required_elementor_pro', $message, 'import/page', 404 );
97 }
98 }
99
100 public function get_saved_templates( $params = [] ) {
101 if ( ! function_exists( 'is_plugin_active' ) ){
102 require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
103 }
104
105 $_page_number = isset( $params['page'] ) ? intval( $params['page'] ) : 1;
106
107 $args = [
108 'post_type' => 'elementor_library',
109 'post_status' => [ 'publish', 'draft' ],
110 'paged' => $_page_number,
111 'page' => $_page_number,
112 'posts_per_page' => isset( $params['per_page'] ) ? intval( $params['per_page'] ) : 15
113 ];
114
115 $templates = new WP_Query( $args );
116 $templates_list = $templates->posts;
117 $templateList = [];
118 $_templates = Options::get_instance()->get( 'templates_in_clouds', [] );
119
120 if ( count( $templates_list ) > 0 ) :
121 $date_format = get_option( 'date_format', 'F j, Y' );
122 foreach ( $templates_list as $post ) {
123 $templateList[] = [
124 'id' => $post->ID,
125 'title' => $post->post_title,
126 'date' => date( $date_format, strtotime( $post->post_date ) ),
127 'type' => ucwords( get_post_meta( $post->ID, '_elementor_template_type', true ) ),
128 'created_by' => get_the_author_meta( 'user_nicename', $post->post_author ),
129 'preview_url' => get_permalink( $post->ID ),
130 'export_url' => self::get_export_link( $post->ID ),
131 'is_pushed' => array_key_exists( $post->ID, $_templates )
132 ];
133 }
134 endif;
135 wp_reset_postdata();
136 wp_reset_query();
137
138 return array(
139 'current_page' => $templates->query_vars['paged'],
140 'total_page' => $templates->max_num_pages,
141 'items' => $templateList,
142 'has_elementor' => rest_sanitize_boolean( \is_plugin_active( 'elementor/elementor.php' ) ),
143 'has_elementor_pro' => rest_sanitize_boolean( \is_plugin_active( 'elementor-pro/elementor-pro.php' ) ),
144 );
145 }
146
147 private function get_export_link( $template_id ) {
148 return add_query_arg(
149 [
150 'action' => 'elementor_library_direct_actions',
151 'library_action' => 'export_template',
152 'source' => 'local',
153 '_nonce' => wp_create_nonce( 'elementor_ajax' ),
154 'template_id' => $template_id,
155 ],
156 admin_url( 'admin-ajax.php' )
157 );
158 }
159
160 public function is_kit( $post_id ) {
161 $document = Plugin::$instance->documents->get( $post_id );
162
163 return $document instanceof Kit && ! $document->is_revision();
164 }
165
166 /**
167 * Delete an item from Elementor Templates Library.
168 * Which also refers as Saved Templates.
169 *
170 * @param $params
171 *
172 * @return WP_Error|WP_REST_Response
173 */
174 public function delete( $params = [] ) {
175 $id = isset( $params['id'] ) ? intval( $params['id'] ) : false;
176 $force_delete_kit = isset( $params['force_delete_kit'] ) && $params['force_delete_kit'];
177
178 $is_kit = false;
179
180 if ( $this->is_kit( $id ) ) {
181 $is_kit = true;
182 if ( ! $force_delete_kit ) {
183 return Helper::error( 'cant_delete_kit', 'Cannot delete kit.', 'saved-templates/delete', 500, [ 'id' => $id ] );
184 } else {
185 $_GET['force_delete_kit'] = 1; // Fallback GET Ready!
186 }
187 }
188
189 $post = wp_trash_post( $id );
190 if ( $post instanceof WP_Post ) {
191 return Helper::success( [
192 'is_kit' => $is_kit,
193 'status' => 'success',
194 'message' => __( 'Successfully Deleted.', 'templately' )
195 ] );
196 }
197
198 return Helper::error(
199 'cant_delete_kit',
200 __( 'Something must went wrong.', 'templately' ),
201 'saved-templates/delete', 500,
202 [ 'id' => $id ]
203 );
204
205 // $cloud_map = DB::get_option( '_templately_cloud_map', [] );
206 // $new_cloud_map = [];
207 // array_walk( $cloud_map, function ( $cloud ) use ( &$cloud_map, $id, &$new_cloud_map ) {
208 // if ( $cloud['template_id'] !== $id ) {
209 // $new_cloud_map[] = $cloud;
210 // }
211 // } );
212 // DB::update_option( '_templately_cloud_map', $new_cloud_map );
213 // Helper::send_success( __( 'Successfully Deleted.', 'templately' ) );
214 }
215
216 protected function get_template_from_library( $id ) {
217 $local = new ElementorLocal();
218
219 return $local->get_data( [ 'template_id' => $id ] );
220 }
221
222 public function get_template_data( $id ) {
223 $data = $this->get_template_from_library( $id );
224 if ( ! empty( $data ) && isset( $data['content'] ) ) {
225 $name = get_the_title( $id );
226
227 return [
228 'name' => $name,
229 'file_content' => $data
230 ];
231 }
232
233 return null;
234 }
235
236 /**
237 * Creating an elementor page
238 *
239 * @param integer $id
240 * @param string $title
241 * @param Import $importer
242 *
243 * @since 2.0.0
244 *
245 * @return array|WP_Error array on success, WP_Error on failure.
246 */
247 public function create_page( $id, $title, $importer = null, $settings = [] ) {
248 $template_data = $importer->get_content( $id );
249
250 if ( is_wp_error( $template_data ) ) {
251 return $template_data;
252 }
253
254 if ( ! empty( $settings ) && ! empty( $template_data['content'] ) && is_array( $template_data['content'] ) ) {
255 $template_data['content'] = \Templately\Core\Importer\Utils\ElementorSettingsMerger::merge( $template_data['content'], $settings );
256 }
257
258 $importer = new ElementorImporter;
259 $template_data = $importer->get_data( $template_data );
260
261 if ( ! empty( $title ) ) {
262 $template_data['page_title'] = $title;
263 }
264 /**
265 * Checking the template eligibility for import.
266 */
267 $eligible = $this->is_eligible( $template_data );
268 if( is_wp_error( $eligible ) ) {
269 return $eligible;
270 }
271
272 $inserted_ID = $importer->create_page( $template_data );
273
274 if ( is_wp_error( $inserted_ID ) ) {
275 return Helper::error(
276 'import_failed',
277 $inserted_ID->get_error_message(),
278 'import/page',
279 $inserted_ID->get_error_code(),
280 [ 'code' => $inserted_ID->get_error_code() ]
281 );
282 }
283
284 return [
285 'post_id' => $inserted_ID,
286 'edit_link' => get_edit_post_link( $inserted_ID, 'internal' ),
287 'elementor_edit_link' => Plugin::$instance->documents->get( $inserted_ID )->get_edit_url(),
288 'visit' => get_permalink( $inserted_ID )
289 ];
290 }
291
292 public function import_in_library( $id, $importer = null, $settings = [], $template_type = '', $type = '' ) {
293 $template_data = $importer->get_content( $id, 'elementor', 'remote' );
294
295 if ( is_wp_error( $template_data ) ) {
296 return $template_data;
297 }
298
299 if ( ! empty( $settings ) && ! empty( $template_data['content'] ) && is_array( $template_data['content'] ) ) {
300 $template_data['content'] = \Templately\Core\Importer\Utils\ElementorSettingsMerger::merge( $template_data['content'], $settings );
301 }
302
303 // Frontend-provided template_type (from item details API) takes precedence over
304 // whatever the fetched content JSON reports.
305 if ( ! empty( $template_type ) ) {
306 $template_data['template_type'] = $template_type;
307 }
308
309 // Resolve the correct Builder Type using template_type before passing to the importer.
310 try {
311 $template_data['type'] = static::resolve_library_type( $template_data );
312 } catch ( \InvalidArgumentException $e ) {
313 return Helper::error( 'unsupported_template_type', $e->getMessage(), 'import', 400 );
314 }
315
316 $importer = new ElementorImporter;
317 $imported_template = $importer->import_in_library( $template_data );
318
319 if( is_wp_error( $imported_template ) ) {
320 return $imported_template;
321 }
322
323 $post_id = $imported_template['template_id'];
324
325 // Companion content for archive/fluent types — same side effects (Shop page,
326 // page_for_posts, LearnDash courses slug, fluent single-page template) the FSI
327 // Templates runner performs when importing these template types.
328 Utils::create_companion_content( $template_data['type'], $template_data['title'] ?? '', 'elementor' );
329
330 // Mark user as having imported a template
331 \Templately\Utils\Options::get_instance()->set( 'has_imported_template', true );
332
333 $document = Plugin::$instance->documents->get( $post_id );
334 $elementor_edit_link = $document ? $document->get_edit_url() : admin_url( 'post.php?post=' . $post_id . '&action=elementor' );
335
336 return [
337 'post_id' => $post_id,
338 'edit_link' => get_edit_post_link( $post_id, 'internal' ),
339 'elementor_edit_link' => $elementor_edit_link,
340 'visit' => $imported_template['url']
341 ];
342 }
343
344 public function get_content( $id ) {
345 $local = new ElementorLocal();
346 $content = $local->get_data( [
347 'template_id' => $id
348 ] );
349
350 $_response = [ 'status' => 'success' ];
351 $_response['data'] = $content;
352
353 if ( is_wp_error( $content ) ) {
354 /**
355 * @var WP_Error $content
356 */
357 unset( $_response['data'] );
358 $_response['status'] = 'error';
359 $_response['message'] = $content->get_error_message();
360 }
361
362 return $_response;
363 }
364
365 public function insert( $data ) {
366 $importer = new ElementorImporter();
367
368 return $importer->get_data( $data );
369 }
370
371 /**
372 * Filter Child Type
373 *
374 * @param $child_type
375 * @param $element_data
376 * @param $element
377 *
378 * @return mixed
379 */
380 public static function filter_child_type( $child_type, $element_data, $element ) {
381 // Fix for Elementor Pro Promotion Widget
382 if ( is_a( $element, 'Elementor\Modules\Promotions\Widgets\Pro_Widget_Promotion' ) || ( is_object( $element ) && 'pro-promotion' === $element->get_name() ) ) {
383 $el_types = array_keys( Plugin::$instance->elements_manager->get_element_types() );
384
385 if ( isset( $element_data['elType'] ) && in_array( $element_data['elType'], $el_types, true ) ) {
386 return Plugin::$instance->elements_manager->get_element_types( $element_data['elType'] );
387 }
388
389 if ( ! isset( $element_data['widgetType'] ) ) {
390 return null;
391 }
392
393 return Plugin::$instance->widgets_manager->get_widget_types( $element_data['widgetType'] );
394 }
395
396 return $child_type;
397 }
398 }