PluginProbe ʕ •ᴥ•ʔ
Spider Elements – Premium Elementor Widgets & Addons Library / 1.5.0
Spider Elements – Premium Elementor Widgets & Addons Library v1.5.0
trunk 1.0.0 1.1.0 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.7.0 1.8.0 1.9.0
spider-elements / includes / template_library / templates / Api.php
spider-elements / includes / template_library / templates Last commit date
assets 1 year ago views 1 year ago Api.php 1 year ago Import.php 1 year ago Init.php 1 year ago Load.php 1 year ago
Api.php
189 lines
1 <?php
2 namespace SPEL\includes\template_library;
3
4 use Elementor\Plugin;
5 use Elementor\TemplateLibrary\Source_Base;
6
7 if ( ! defined( 'ABSPATH' ) ) {exit;}
8
9 class Api extends Source_Base {
10
11 const LIBRARY_OPTION_KEY = 'docy_library_info';
12
13 const LIBRARY_TIMESTAMP_CACHE_KEY = 'docy_remote_update_timestamp';
14 const API_INFO_URL = 'https://wordpress-theme.spider-themes.net/elementor-template-library/docy/';
15
16 public function get_id(): string
17 {
18 return 'template_library';
19 }
20
21 public function get_title(): string
22 {
23 return esc_html__( 'Docy Library', 'spider-elements' );
24 }
25
26 public function register_data() {}
27
28 public function get_items( $args = [] ): array
29 {
30 $library_data = self::get_library_data();
31 $templates = [];
32 if ( ! empty( $library_data['templates'] ) ) {
33 foreach ( $library_data['templates'] as $template_data ) {
34 $templates[] = $this->prepare_template( $template_data );
35 }
36 }
37 return $templates;
38 }
39
40 public function get_tags() {
41 $library_data = self::get_library_data();
42 return ( ! empty( $library_data['tags'] ) ? $library_data['tags'] : [] );
43 }
44
45 public function get_type_tags() {
46 $library_data = self::get_library_data();
47 return ( ! empty( $library_data['type_tags'] ) ? $library_data['type_tags'] : [] );
48 }
49
50
51 private function prepare_template( array $template_data ): array
52 {
53 return [
54 'template_id' => $template_data['template_id'],
55 'title' => $template_data['title'],
56 'type' => $template_data['type'],
57 'thumbnail' => $template_data['thumbnail'],
58 'date' => $template_data['modified'],
59 'tags' => $template_data['tags'],
60 'url' => $template_data['preview'],
61 'liveurl' => $template_data['liveurl'],
62 'favorite' => ! empty( $template_data['template_id'] ),
63 ];
64 }
65
66
67 private static function request_library_data( $force_update = false ): void
68 {
69 $data = get_option( self::LIBRARY_OPTION_KEY );
70
71 $elementor_update_timestamp = get_option( '_transient_timeout_elementor_remote_info_api_data_' . ELEMENTOR_VERSION );
72 $update_timestamp = get_transient( self::LIBRARY_TIMESTAMP_CACHE_KEY );
73
74 if ( $force_update || false === $data || ! $update_timestamp || $update_timestamp != $elementor_update_timestamp ) {
75 $timeout = ( $force_update ) ? 25 : 8;
76
77 $apiUrl = self::API_INFO_URL .'?' . http_build_query([
78 'action' => 'get_layouts',
79 'tab' => ''
80 ]);
81 $response = wp_remote_get( $apiUrl, [
82 'timeout' => $timeout,
83 ] );
84
85 if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
86 update_option( self::LIBRARY_OPTION_KEY, [] );
87 return;
88 }
89
90 $data = json_decode( wp_remote_retrieve_body( $response ), true );
91
92 if ( empty( $data ) || ! is_array( $data ) ) {
93 update_option( self::LIBRARY_OPTION_KEY, [] );
94 set_transient( self::LIBRARY_TIMESTAMP_CACHE_KEY, [], 2 * HOUR_IN_SECONDS );
95 return;
96 }
97
98 //Update when reload
99 update_option( self::LIBRARY_OPTION_KEY, $data, 'yes' );
100 }
101 }
102
103 public static function get_library_data( $force_update = false ) {
104 self::request_library_data( $force_update );
105 $library_data = get_option( self::LIBRARY_OPTION_KEY );
106 if ( empty( $library_data ) ) {
107 return [];
108 }
109 return $library_data;
110 }
111
112 public function get_item( $template_id ) {
113 $templates = $this->get_items();
114
115 return $templates[ $template_id ];
116 }
117
118 public function save_item( $template_data ): \WP_Error
119 {
120 return new \WP_Error( 'invalid_request', 'Cannot save template to a docy elementor addons library' );
121 }
122
123 public function update_item( $new_data ): \WP_Error
124 {
125 return new \WP_Error( 'invalid_request', 'Cannot update template to a docy elementor addons library' );
126 }
127
128
129 public function delete_template( $template_id ): \WP_Error
130 {
131 return new \WP_Error( 'invalid_request', 'Cannot delete template from a docy elementor addons library' );
132 }
133
134 public function export_template( $template_id ): \WP_Error
135 {
136 return new \WP_Error( 'invalid_request', 'Cannot export template from a docy elementor addons library' );
137 }
138
139 public static function request_template_data( $template_id ): string
140 {
141 if ( empty( $template_id ) ) {
142 return $template_id;
143 }
144
145 $body = [
146 'site_lang' => get_bloginfo( 'language' ),
147 'home_url' => trailingslashit( home_url() ),
148 'template_version' => '1.0.0',
149 ];
150
151 $body_args = apply_filters( 'elementor/api/get_templates/body_args', $body );
152
153 $apiUrl = self::API_INFO_URL .'?' . http_build_query([
154 'action' => 'get_layout_data',
155 'id' => $template_id,
156 ]);
157
158 $response = wp_remote_get(
159 $apiUrl,
160 [
161 'body' => $body_args,
162 'timeout' => 10
163 ]
164 );
165
166 return wp_remote_retrieve_body( $response );
167 }
168
169 public function get_data( array $args, $context = 'display' ) {
170 $data = self::request_template_data( $args['template_id'] );
171
172 $data = json_decode( $data, true );
173 if ( empty( $data ) || empty( $data['content'] ) ) {
174 throw new \Exception( __( 'Template does not have any content', 'spider-elements' ) );
175 }
176
177 $data['content'] = $this->replace_elements_ids( $data['content'] );
178 $data['content'] = $this->process_export_import_content( $data['content'], 'on_import' );
179
180 $post_id = $args['editor_post_id'];
181 $document = Plugin::$instance->documents->get( $post_id );
182 if ( $document ) {
183 $data['content'] = $document->get_elements_raw_data( $data['content'], true );
184 }
185 return $data;
186 }
187
188 }
189