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

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