PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
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 3 weeks ago Apps.php 3 weeks ago Collection.php 1 month ago Comments.php 2 months ago DynamicContent.php 1 month ago ExportImport.php 3 weeks ago Form.php 3 weeks ago Media.php 3 weeks ago Page.php 3 weeks ago PageSettings.php 3 weeks ago RBAC.php 3 weeks ago Symbol.php 3 weeks ago Taxonomy.php 2 months ago TemplateExportImport.php 2 months ago UserData.php 3 weeks ago Users.php 2 months ago Walkthrough.php 2 months ago WordpressData.php 2 months ago WpAdmin.php 2 months ago
Apps.php
393 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 $app = HelperFunctions::sanitize_text( isset( $_POST['app'] ) ? $_POST['app'] : null );
111 $app = json_decode( stripslashes( $app ), true );
112
113 $app_slug = preg_replace( '/[^a-zA-Z0-9\-]/', '', $app['slug'] );
114
115 $src = $app['src'];
116
117 $version = isset( $app['version'] ) ? $app['version'] : '1.0.0';
118
119 // Get the installed apps data
120 $apps = HelperFunctions::get_global_data_using_key( 'kirki_installed_apps' );
121 if ( ! $apps ) {
122 $apps = array();
123 }
124
125 // Check if the app is already installed
126 if ( in_array( $app_slug, $apps ) ) {
127 wp_send_json_error( 'This app is already installed. Please check the kirki-apps directory in wp-content folders.' );
128 }
129
130 // Define the remote zip URL and generate a unique file name
131 $remote_zip_url = KIRKI_APPS_BASE_URL . $src;
132 $file_name_new = uniqid( '', true ) . '.zip'; // 'random.ext'
133
134 // Check if ZipArchive extension is loaded
135 if ( ! class_exists( 'ZipArchive' ) ) {
136 wp_send_json_error( 'ZipArchive extension is not installed or enabled on the server.' );
137 }
138
139 // Attempt to download the zip file
140 $zip_file_path = HelperFunctions::download_zip_from_remote( $remote_zip_url, $file_name_new );
141
142 try {
143 // If the file was successfully downloaded
144 if ( $zip_file_path ) {
145 $zip = new \ZipArchive();
146 $res = $zip->open( $zip_file_path );
147
148 if ( $res === true ) {
149 // Get the WordPress wp-content directory
150 $base_upload_dir = WP_CONTENT_DIR; // Absolute path to 'uploads'
151 $temp_folder = $base_upload_dir . '/kirki-apps';
152
153 // Check if the wp-content directory is writable
154 global $wp_filesystem;
155 if ( empty( $wp_filesystem ) ) {
156 require_once ABSPATH . 'wp-admin/includes/file.php';
157 WP_Filesystem();
158 }
159
160 if ( ! $wp_filesystem->is_writable( $base_upload_dir ) ) {
161 wp_delete_file( $zip_file_path );
162 wp_send_json_error( 'The wp-content directory is not writable. Please check folder permissions.' );
163 return;
164 }
165
166 // Create the 'kirki-apps' folder if it doesn't exist
167 if ( ! file_exists( $temp_folder ) ) {
168 if ( ! wp_mkdir_p( $temp_folder ) ) {
169 wp_delete_file( $zip_file_path );
170 wp_send_json_error( 'Failed to create directory: kirki-apps. Please check folder permissions.' );
171 return;
172 }
173 }
174
175 // Extract the zip file to the 'kirki-apps' folder
176 $zip->extractTo( $temp_folder );
177 $zip->close();
178
179 // Clean up the zip file
180 wp_delete_file( $zip_file_path );
181
182 // Update the list of installed apps
183 $apps[] = array(
184 'app_slug' => $app_slug,
185 'version' => $version,
186 );
187 HelperFunctions::update_global_data_using_key( 'kirki_installed_apps', $apps );
188
189 wp_send_json_success( 'App installed successfully!' );
190 } else {
191 wp_delete_file( $zip_file_path );
192 wp_send_json_error( 'Failed to extract the zip file. Please ensure the file is valid.' );
193 }
194 } else {
195 wp_send_json_error( 'Failed to download the app zip file. Please check the remote URL.' );
196 }
197 } catch ( \Throwable $th ) {
198 // Log the error or handle it as needed
199 wp_delete_file( $zip_file_path ); // Clean up the zip file if it exists
200 wp_send_json_error( 'An error occurred during the app installation: ' . $th->getMessage() );
201 }
202 } else {
203 wp_send_json_error( 'User authentication failed. Please log in.' );
204 }
205 die();
206 }
207
208
209 /**
210 * @deprecated
211 * @see Kirki\App\Services\AppsService::delete_app()
212 */
213 public static function delete_app_using_slug() {
214 $user_id = get_current_user_id();
215 if ( ! empty( $user_id ) ) {
216 $app_slug = HelperFunctions::sanitize_text( isset( $_POST['slug'] ) ? $_POST['slug'] : null );
217
218 $apps = HelperFunctions::get_global_data_using_key( 'kirki_installed_apps' );
219 if ( ! $apps ) {
220 $apps = array();
221 }
222
223 $is_app_present = false;
224
225 foreach ( $apps as $app ) {
226 if ( isset( $app['app_slug'] ) && $app['app_slug'] === $app_slug ) {
227 $is_app_present = true;
228 break;
229 }
230 }
231
232 if ( $is_app_present ) {
233 $base_upload_dir = WP_CONTENT_DIR; // Absolute path to 'uploads'
234 $app_folder = $base_upload_dir . '/kirki-apps/' . $app_slug;
235
236 $arr = array();
237 foreach ( $apps as $key => $a ) {
238 if ( isset( $a['app_slug'] ) && $a['app_slug'] !== $app_slug ) {
239 $arr[] = $a;
240 }
241 }
242
243 HelperFunctions::update_global_data_using_key( 'kirki_installed_apps', $arr );
244 HelperFunctions::delete_directory( $app_folder );
245 HelperFunctions::update_global_data_using_key( 'kirki_app_settings_' . $app_slug, false );
246 wp_send_json_success( 'success' );
247 }
248
249 wp_send_json_error( 'App not found!' );
250 }
251 die();
252 }
253
254 /**
255 * @deprecated
256 * @see Kirki\App\Services\AppsService::update_app()
257 */
258 public static function update_app() {
259 error_reporting( E_ALL );
260 ini_set( 'display_errors', 1 );
261
262 $user_id = get_current_user_id();
263 if ( empty( $user_id ) ) {
264 wp_send_json_error( 'User authentication failed. Please log in.' );
265 }
266
267 try {
268 $app = HelperFunctions::sanitize_text( isset( $_POST['app'] ) ? $_POST['app'] : null );
269 $app = json_decode( stripslashes( $app ), true );
270
271 if ( ! $app || ! isset( $app['slug'] ) || ! isset( $app['src'] ) || ! isset( $app['version'] ) ) {
272 wp_send_json_error( 'Invalid app data provided.' );
273 }
274
275 $app_slug = preg_replace( '/[^a-zA-Z0-9\-]/', '', $app['slug'] );
276 $src = $app['src'];
277 $new_version = $app['version'];
278
279 // Check if the app is installed
280 $apps = HelperFunctions::get_global_data_using_key( 'kirki_installed_apps' );
281 if ( ! $apps ) {
282 wp_send_json_error( 'No apps are currently installed.' );
283 }
284
285 $app_found = false;
286 $current_version = '';
287 foreach ( $apps as $installed_app ) {
288 if ( isset( $installed_app['app_slug'] ) && $installed_app['app_slug'] === $app_slug ) {
289 $app_found = true;
290 $current_version = $installed_app['version'];
291 break;
292 }
293 }
294
295 if ( ! $app_found ) {
296 wp_send_json_error( 'App not found in installed apps.' );
297 }
298
299 // Check if ZipArchive is available
300 if ( ! class_exists( 'ZipArchive' ) ) {
301 wp_send_json_error( 'ZipArchive extension is not installed or enabled on the server.' );
302 }
303
304 $base_upload_dir = WP_CONTENT_DIR;
305 $apps_folder = $base_upload_dir . '/kirki-apps';
306 $app_folder = $apps_folder . '/' . $app_slug;
307 $backup_folder = $app_folder . '_backup_' . time();
308
309 global $wp_filesystem;
310 if ( empty( $wp_filesystem ) ) {
311 require_once ABSPATH . 'wp-admin/includes/file.php';
312 WP_Filesystem();
313 }
314
315 // Create backup of current app
316 if ( ! $wp_filesystem->move( $app_folder, $backup_folder ) ) {
317 wp_send_json_error( 'Failed to create backup of current app version.' );
318 }
319
320 // Download and extract new version
321 $remote_zip_url = KIRKI_APPS_BASE_URL . $src;
322 $file_name_new = uniqid( '', true ) . '.zip';
323 $zip_file_path = HelperFunctions::download_zip_from_remote( $remote_zip_url, $file_name_new );
324
325 if ( ! $zip_file_path ) {
326 // Restore backup
327 $wp_filesystem->move( $backup_folder, $app_folder );
328 wp_send_json_error( 'Failed to download the new app version.' );
329 }
330
331 $zip = new \ZipArchive();
332 $res = $zip->open( $zip_file_path );
333
334 if ( $res !== true ) {
335 // Restore backup
336 $wp_filesystem->move( $backup_folder, $app_folder );
337 wp_delete_file( $zip_file_path );
338 wp_send_json_error( 'Failed to extract the zip file. Please ensure the file is valid.' );
339 }
340
341 // Extract new version
342 if ( ! $zip->extractTo( $apps_folder ) ) {
343 // Restore backup
344 $wp_filesystem->move( $backup_folder, $app_folder );
345 $zip->close();
346 wp_delete_file( $zip_file_path );
347 wp_send_json_error( 'Failed to extract new app version.' );
348 }
349
350 $zip->close();
351 wp_delete_file( $zip_file_path );
352
353 // Update version in installed apps array
354 foreach ( $apps as &$installed_app ) {
355 if ( isset( $installed_app['app_slug'] ) && $installed_app['app_slug'] === $app_slug ) {
356 $installed_app['version'] = $new_version;
357 break;
358 }
359 }
360
361 HelperFunctions::update_global_data_using_key( 'kirki_installed_apps', $apps );
362
363 // If everything succeeded, remove the backup
364 HelperFunctions::delete_directory( $backup_folder );
365
366 wp_send_json_success( 'App installed successfully!' );
367 } catch ( \Throwable $th ) {
368 global $wp_filesystem;
369 if ( empty( $wp_filesystem ) ) {
370 require_once ABSPATH . 'wp-admin/includes/file.php';
371 WP_Filesystem();
372 }
373
374 // If backup exists, restore it
375 if ( isset( $backup_folder ) && isset( $app_folder ) && $wp_filesystem->exists( $backup_folder ) ) {
376 if ( $wp_filesystem->exists( $app_folder ) ) {
377 HelperFunctions::delete_directory( $app_folder );
378 }
379 $wp_filesystem->move( $backup_folder, $app_folder );
380 }
381
382 // Clean up zip file if it exists
383 if ( isset( $zip_file_path ) && file_exists( $zip_file_path ) ) {
384 wp_delete_file( $zip_file_path );
385 }
386
387 wp_send_json_error( 'An error occurred during the app update: ' . $th->getMessage() );
388 }
389
390 die();
391 }
392 }
393