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

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