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

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