PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.6.2
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.6.2
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.2, at includes/Core/Platform/Elementor.php

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