| @@ -394,5 +394,55 @@ | ||
| 394 | 394 | } |
| 395 | 395 | return reset( $array ); |
| 396 | 396 | } |
| 397 | 397 | |
| 398 | + /** | |
| 399 | + * Gets/creates the JS where user-specific customizations can be/have been added. | |
| 400 | + */ | |
| 401 | + protected function get_user_customization_js() { | |
| 402 | + // use this as the JS file in case we are not able to create the file in uploads. | |
| 403 | + $default = VISUALIZER_ABSURL . 'js/customization.js'; | |
| 404 | + | |
| 405 | + $uploads = wp_get_upload_dir(); | |
| 406 | + $specific = $uploads['baseurl'] . '/visualizer/customization.js'; | |
| 407 | + | |
| 408 | + // for testing on user sites (before we send them the correctly customized file). | |
| 409 | + if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) { | |
| 410 | + return $default; | |
| 411 | + } | |
| 412 | + | |
| 413 | + require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
| 414 | + WP_Filesystem(); | |
| 415 | + global $wp_filesystem; | |
| 416 | + | |
| 417 | + $dir = $wp_filesystem->wp_content_dir() . 'uploads/visualizer'; | |
| 418 | + $file = $wp_filesystem->wp_content_dir() . 'uploads/visualizer/customization.js'; | |
| 419 | + | |
| 420 | + if ( $wp_filesystem->is_readable( $file ) ) { | |
| 421 | + return $specific; | |
| 422 | + } | |
| 423 | + | |
| 424 | + if ( $wp_filesystem->exists( $file ) && ! $wp_filesystem->is_readable( $file ) ) { | |
| 425 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to read file %s', $file ), 'error', __FILE__, __LINE__ ); | |
| 426 | + return $default; | |
| 427 | + } | |
| 428 | + | |
| 429 | + if ( ! $wp_filesystem->exists( $dir ) ) { | |
| 430 | + if ( ( $done = $wp_filesystem->mkdir( $dir ) ) === false ) { | |
| 431 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ ); | |
| 432 | + return $default; | |
| 433 | + } | |
| 434 | + } | |
| 435 | + | |
| 436 | + // if file does not exist, copy. | |
| 437 | + if ( ! $wp_filesystem->exists( $file ) ) { | |
| 438 | + $src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' ); | |
| 439 | + if ( ( $done = $wp_filesystem->copy( $src, $file ) ) === false ) { | |
| 440 | + do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ ); | |
| 441 | + return $default; | |
| 442 | + } | |
| 443 | + } | |
| 444 | + | |
| 445 | + return $specific; | |
| 446 | + } | |
| 447 | + | |
| 398 | 448 | } |