PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.3.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.3.0
6.3.0 6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / includes / Ajax / Apps.php
kirki / includes / Ajax Last commit date
Collaboration 1 month ago Apps.php 2 days ago Collection.php 2 months ago Comments.php 4 months ago DynamicContent.php 4 weeks ago ExportImport.php 2 weeks ago Form.php 2 months ago Media.php 2 days ago Page.php 1 month ago PageSettings.php 2 months ago RBAC.php 2 months ago Symbol.php 2 days ago Taxonomy.php 4 months ago TemplateExportImport.php 2 weeks ago UserData.php 2 months ago Users.php 3 weeks ago Walkthrough.php 1 month ago WordpressData.php 4 months ago WpAdmin.php 1 month ago
Apps.php
405 lines
1 <?php
2
3 /**
4 * Manager User data
5 *
6 * @package kirki
7 */
8
9 namespace Kirki\Ajax;
10
11 use Kirki\HelperFunctions;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16
17 /**
18 * Apps API Class
19 */
20 class Apps {
21
22
23 /**
24 * get_user_installed_apps_list
25 *
26 * @return void wp_send_json
27 *
28 * @deprecated
29 * @see \Kirki\App\Services\AppsService::get_all_apps()
30 */
31 public static function get_app_list() {
32 $apps = HelperFunctions::get_global_data_using_key( 'kirki_installed_apps' );
33 if ( ! $apps ) {
34 $apps = array();
35 }
36 $apps = HelperFunctions::http_get( KIRKI_APPS_BASE_URL . '/apps.json', array( 'sslverify' => false ) );
37 wp_send_json_success( json_decode( $apps ) );
38 }
39
40 /**
41 * get_user_installed_apps_list
42 *
43 * @return void wp_send_json
44 *
45 * @deprecated
46 * @see \Kirki\App\Services\AppsService::get_installed_apps()
47 */
48 public static function get_installed_apps_list() {
49 $apps = HelperFunctions::get_global_data_using_key( 'kirki_installed_apps' );
50 if ( ! $apps ) {
51 $apps = array();
52 }
53 wp_send_json_success( $apps );
54 }
55
56 /**
57 * get_app_settings_using_slug
58 *
59 * @return void wp_send_json
60 *
61 * @deprecated
62 * @see Kirki\App\Services\AppsService::get_app_settings()
63 */
64 public static function get_app_settings_using_slug() {
65 $user_id = get_current_user_id();
66 if ( ! empty( $user_id ) ) {
67 $app_slug = HelperFunctions::sanitize_text( isset( $_GET['slug'] ) ? $_GET['slug'] : null );
68 $settings = HelperFunctions::get_global_data_using_key( 'kirki_app_settings_' . $app_slug );
69 if ( ! $settings ) {
70 $settings = array();
71 }
72 $settings = apply_filters( 'kirki_apps_configuration_' . $app_slug, $settings );
73 wp_send_json_success( $settings );
74 }
75 wp_send_json_error( 'User authentication failed. Please log in.' );
76 die();
77 }
78
79 /**
80 * get_app_settings_using_slug
81 *
82 * @return void wp_send_json
83 *
84 * @deprecated
85 * @see Kirki\App\Services\AppsService::save_app_settings()
86 */
87 public static function save_app_settings_using_slug() {
88 $user_id = get_current_user_id();
89 if ( ! empty( $user_id ) ) {
90 $app_slug = HelperFunctions::sanitize_text( isset( $_POST['slug'] ) ? $_POST['slug'] : null );
91 $settings = HelperFunctions::sanitize_text( isset( $_POST['settings'] ) ? $_POST['settings'] : null );
92 $settings = json_decode( stripslashes( $settings ), true );
93 HelperFunctions::update_global_data_using_key( 'kirki_app_settings_' . $app_slug, $settings );
94 wp_send_json_success( true );
95 }
96 wp_send_json_error( 'User authentication failed. Please log in.' );
97 die();
98 }
99
100 /**
101 * @deprecated
102 * @see Kirki\App\Services\AppsService::install_app()
103 */
104 public static function install_app() {
105 error_reporting( E_ALL );
106 ini_set( 'display_errors', 1 );
107
108 $user_id = get_current_user_id();
109 if ( ! empty( $user_id ) ) {
110 if ( ! HelperFunctions::has_access( KIRKI_ACCESS_LEVELS['FULL_ACCESS'] ) ) {
111 wp_send_json_error( 'Not authorized' );
112 }
113
114 $app = HelperFunctions::sanitize_text( isset( $_POST['app'] ) ? $_POST['app'] : null );
115 $app = json_decode( stripslashes( $app ), true );
116
117 $app_slug = preg_replace( '/[^a-zA-Z0-9\-]/', '', $app['slug'] );
118
119 $src = $app['src'];
120
121 $version = isset( $app['version'] ) ? $app['version'] : '1.0.0';
122
123 // Get the installed apps data
124 $apps = HelperFunctions::get_global_data_using_key( 'kirki_installed_apps' );
125 if ( ! $apps ) {
126 $apps = array();
127 }
128
129 // Check if the app is already installed
130 if ( in_array( $app_slug, $apps ) ) {
131 wp_send_json_error( 'This app is already installed. Please check the kirki-apps directory in wp-content folders.' );
132 }
133
134 // Define the remote zip URL and generate a unique file name
135 $remote_zip_url = KIRKI_APPS_BASE_URL . $src;
136 $file_name_new = uniqid( '', true ) . '.zip'; // 'random.ext'
137
138 // Check if ZipArchive extension is loaded
139 if ( ! class_exists( 'ZipArchive' ) ) {
140 wp_send_json_error( 'ZipArchive extension is not installed or enabled on the server.' );
141 }
142
143 // Attempt to download the zip file
144 $zip_file_path = HelperFunctions::download_zip_from_remote( $remote_zip_url, $file_name_new );
145
146 try {
147 // If the file was successfully downloaded
148 if ( $zip_file_path ) {
149 $zip = new \ZipArchive();
150 $res = $zip->open( $zip_file_path );
151
152 if ( $res === true ) {
153 // Get the WordPress wp-content directory
154 $base_upload_dir = WP_CONTENT_DIR; // Absolute path to 'uploads'
155 $temp_folder = $base_upload_dir . '/kirki-apps';
156
157 // Check if the wp-content directory is writable
158 global $wp_filesystem;
159 if ( empty( $wp_filesystem ) ) {
160 require_once ABSPATH . 'wp-admin/includes/file.php';
161 WP_Filesystem();
162 }
163
164 if ( ! $wp_filesystem->is_writable( $base_upload_dir ) ) {
165 wp_delete_file( $zip_file_path );
166 wp_send_json_error( 'The wp-content directory is not writable. Please check folder permissions.' );
167 return;
168 }
169
170 // Create the 'kirki-apps' folder if it doesn't exist
171 if ( ! file_exists( $temp_folder ) ) {
172 if ( ! wp_mkdir_p( $temp_folder ) ) {
173 wp_delete_file( $zip_file_path );
174 wp_send_json_error( 'Failed to create directory: kirki-apps. Please check folder permissions.' );
175 return;
176 }
177 }
178
179 // Extract the zip file to the 'kirki-apps' folder
180 $zip->extractTo( $temp_folder );
181 $zip->close();
182
183 // Clean up the zip file
184 wp_delete_file( $zip_file_path );
185
186 // Update the list of installed apps
187 $apps[] = array(
188 'app_slug' => $app_slug,
189 'version' => $version,
190 );
191 HelperFunctions::update_global_data_using_key( 'kirki_installed_apps', $apps );
192
193 wp_send_json_success( 'App installed successfully!' );
194 } else {
195 wp_delete_file( $zip_file_path );
196 wp_send_json_error( 'Failed to extract the zip file. Please ensure the file is valid.' );
197 }
198 } else {
199 wp_send_json_error( 'Failed to download the app zip file. Please check the remote URL.' );
200 }
201 } catch ( \Throwable $th ) {
202 // Log the error or handle it as needed
203 wp_delete_file( $zip_file_path ); // Clean up the zip file if it exists
204 wp_send_json_error( 'An error occurred during the app installation: ' . $th->getMessage() );
205 }
206 } else {
207 wp_send_json_error( 'User authentication failed. Please log in.' );
208 }
209 die();
210 }
211
212
213 /**
214 * @deprecated
215 * @see Kirki\App\Services\AppsService::delete_app()
216 */
217 public static function delete_app_using_slug() {
218 $user_id = get_current_user_id();
219 if ( ! empty( $user_id ) ) {
220 if ( ! HelperFunctions::has_access( KIRKI_ACCESS_LEVELS['FULL_ACCESS'] ) ) {
221 wp_send_json_error( 'Not authorized' );
222 }
223
224 $app_slug = HelperFunctions::sanitize_text( isset( $_POST['slug'] ) ? $_POST['slug'] : null );
225
226 $apps = HelperFunctions::get_global_data_using_key( 'kirki_installed_apps' );
227 if ( ! $apps ) {
228 $apps = array();
229 }
230
231 $is_app_present = false;
232
233 foreach ( $apps as $app ) {
234 if ( isset( $app['app_slug'] ) && $app['app_slug'] === $app_slug ) {
235 $is_app_present = true;
236 break;
237 }
238 }
239
240 if ( $is_app_present ) {
241 $base_upload_dir = WP_CONTENT_DIR; // Absolute path to 'uploads'
242 $app_folder = $base_upload_dir . '/kirki-apps/' . $app_slug;
243
244 $arr = array();
245 foreach ( $apps as $key => $a ) {
246 if ( isset( $a['app_slug'] ) && $a['app_slug'] !== $app_slug ) {
247 $arr[] = $a;
248 }
249 }
250
251 HelperFunctions::update_global_data_using_key( 'kirki_installed_apps', $arr );
252 HelperFunctions::delete_directory( $app_folder );
253 HelperFunctions::update_global_data_using_key( 'kirki_app_settings_' . $app_slug, false );
254 wp_send_json_success( 'success' );
255 }
256
257 wp_send_json_error( 'App not found!' );
258 }
259 die();
260 }
261
262 /**
263 * @deprecated
264 * @see Kirki\App\Services\AppsService::update_app()
265 */
266 public static function update_app() {
267 error_reporting( E_ALL );
268 ini_set( 'display_errors', 1 );
269
270 $user_id = get_current_user_id();
271 if ( empty( $user_id ) ) {
272 wp_send_json_error( 'User authentication failed. Please log in.' );
273 }
274
275 if ( ! HelperFunctions::has_access( KIRKI_ACCESS_LEVELS['FULL_ACCESS'] ) ) {
276 wp_send_json_error( 'Not authorized' );
277 }
278
279 try {
280 $app = HelperFunctions::sanitize_text( isset( $_POST['app'] ) ? $_POST['app'] : null );
281 $app = json_decode( stripslashes( $app ), true );
282
283 if ( ! $app || ! isset( $app['slug'] ) || ! isset( $app['src'] ) || ! isset( $app['version'] ) ) {
284 wp_send_json_error( 'Invalid app data provided.' );
285 }
286
287 $app_slug = preg_replace( '/[^a-zA-Z0-9\-]/', '', $app['slug'] );
288 $src = $app['src'];
289 $new_version = $app['version'];
290
291 // Check if the app is installed
292 $apps = HelperFunctions::get_global_data_using_key( 'kirki_installed_apps' );
293 if ( ! $apps ) {
294 wp_send_json_error( 'No apps are currently installed.' );
295 }
296
297 $app_found = false;
298 $current_version = '';
299 foreach ( $apps as $installed_app ) {
300 if ( isset( $installed_app['app_slug'] ) && $installed_app['app_slug'] === $app_slug ) {
301 $app_found = true;
302 $current_version = $installed_app['version'];
303 break;
304 }
305 }
306
307 if ( ! $app_found ) {
308 wp_send_json_error( 'App not found in installed apps.' );
309 }
310
311 // Check if ZipArchive is available
312 if ( ! class_exists( 'ZipArchive' ) ) {
313 wp_send_json_error( 'ZipArchive extension is not installed or enabled on the server.' );
314 }
315
316 $base_upload_dir = WP_CONTENT_DIR;
317 $apps_folder = $base_upload_dir . '/kirki-apps';
318 $app_folder = $apps_folder . '/' . $app_slug;
319 $backup_folder = $app_folder . '_backup_' . time();
320
321 global $wp_filesystem;
322 if ( empty( $wp_filesystem ) ) {
323 require_once ABSPATH . 'wp-admin/includes/file.php';
324 WP_Filesystem();
325 }
326
327 // Create backup of current app
328 if ( ! $wp_filesystem->move( $app_folder, $backup_folder ) ) {
329 wp_send_json_error( 'Failed to create backup of current app version.' );
330 }
331
332 // Download and extract new version
333 $remote_zip_url = KIRKI_APPS_BASE_URL . $src;
334 $file_name_new = uniqid( '', true ) . '.zip';
335 $zip_file_path = HelperFunctions::download_zip_from_remote( $remote_zip_url, $file_name_new );
336
337 if ( ! $zip_file_path ) {
338 // Restore backup
339 $wp_filesystem->move( $backup_folder, $app_folder );
340 wp_send_json_error( 'Failed to download the new app version.' );
341 }
342
343 $zip = new \ZipArchive();
344 $res = $zip->open( $zip_file_path );
345
346 if ( $res !== true ) {
347 // Restore backup
348 $wp_filesystem->move( $backup_folder, $app_folder );
349 wp_delete_file( $zip_file_path );
350 wp_send_json_error( 'Failed to extract the zip file. Please ensure the file is valid.' );
351 }
352
353 // Extract new version
354 if ( ! $zip->extractTo( $apps_folder ) ) {
355 // Restore backup
356 $wp_filesystem->move( $backup_folder, $app_folder );
357 $zip->close();
358 wp_delete_file( $zip_file_path );
359 wp_send_json_error( 'Failed to extract new app version.' );
360 }
361
362 $zip->close();
363 wp_delete_file( $zip_file_path );
364
365 // Update version in installed apps array
366 foreach ( $apps as &$installed_app ) {
367 if ( isset( $installed_app['app_slug'] ) && $installed_app['app_slug'] === $app_slug ) {
368 $installed_app['version'] = $new_version;
369 break;
370 }
371 }
372
373 HelperFunctions::update_global_data_using_key( 'kirki_installed_apps', $apps );
374
375 // If everything succeeded, remove the backup
376 HelperFunctions::delete_directory( $backup_folder );
377
378 wp_send_json_success( 'App installed successfully!' );
379 } catch ( \Throwable $th ) {
380 global $wp_filesystem;
381 if ( empty( $wp_filesystem ) ) {
382 require_once ABSPATH . 'wp-admin/includes/file.php';
383 WP_Filesystem();
384 }
385
386 // If backup exists, restore it
387 if ( isset( $backup_folder ) && isset( $app_folder ) && $wp_filesystem->exists( $backup_folder ) ) {
388 if ( $wp_filesystem->exists( $app_folder ) ) {
389 HelperFunctions::delete_directory( $app_folder );
390 }
391 $wp_filesystem->move( $backup_folder, $app_folder );
392 }
393
394 // Clean up zip file if it exists
395 if ( isset( $zip_file_path ) && file_exists( $zip_file_path ) ) {
396 wp_delete_file( $zip_file_path );
397 }
398
399 wp_send_json_error( 'An error occurred during the app update: ' . $th->getMessage() );
400 }
401
402 die();
403 }
404 }
405