| 1 |
<?php |
| 2 |
|
| 3 |
namespace FloatingButton\Dashboard; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
// Exit if accessed directly. |
| 8 |
use FloatingButton\Optimization\Obfuscator; |
| 9 |
use FloatingButton\Optimization\CSSMinifier; |
| 10 |
use FloatingButton\WOW_Plugin; |
| 11 |
|
| 12 |
class FolderManager { |
| 13 |
|
| 14 |
public static function create(): void { |
| 15 |
$basedir = self::path_upload_dir(); |
| 16 |
|
| 17 |
if ( ! file_exists( $basedir ) ) { |
| 18 |
wp_mkdir_p( $basedir ); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
public static function path_upload_dir(): string { |
| 23 |
$upload = wp_upload_dir(); |
| 24 |
|
| 25 |
return $upload['basedir'] . '/' . WOW_Plugin::FOLDER . '/'; |
| 26 |
} |
| 27 |
|
| 28 |
public static function path_upload_url(): string { |
| 29 |
$upload = wp_upload_dir(); |
| 30 |
|
| 31 |
return $upload['baseurl'] . '/' . WOW_Plugin::FOLDER . '/'; |
| 32 |
} |
| 33 |
|
| 34 |
public static function get_style_url( $result ): string { |
| 35 |
$path_style = self::path_upload_dir() . 'style-' . $result->id . '.css'; |
| 36 |
$param = ! empty( $data['param'] ) ? maybe_unserialize( $data['param'] ) : []; |
| 37 |
$css = $result->css_code; |
| 38 |
|
| 39 |
|
| 40 |
if ( ! empty( $param['minified_css'] ) ) { |
| 41 |
$css = CSSMinifier::minify( $css ); |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! file_exists( $path_style ) ) { |
| 45 |
file_put_contents( $path_style, $css ); |
| 46 |
} |
| 47 |
|
| 48 |
return self::path_upload_url() . 'style-' . $result->id . '.css'; |
| 49 |
} |
| 50 |
|
| 51 |
public static function get_js_url( $result ): string { |
| 52 |
|
| 53 |
$path_js = self::path_upload_dir() . 'script-' . $result->id . '.js'; |
| 54 |
|
| 55 |
if ( ! file_exists( $path_js ) ) { |
| 56 |
file_put_contents( $path_js, $result->js_code ); |
| 57 |
} |
| 58 |
|
| 59 |
return self::path_upload_url() . 'script-' . $result->id . '.js'; |
| 60 |
} |
| 61 |
|
| 62 |
public static function update_style( $data, $id ) { |
| 63 |
$path_style = self::path_upload_dir() . 'style-' . $id . '.css'; |
| 64 |
$css = $data['css_code']; |
| 65 |
$param = ! empty( $data['param'] ) ? maybe_unserialize( $data['param'] ) : []; |
| 66 |
|
| 67 |
if ( ! empty( $param['minified_css'] ) ) { |
| 68 |
$css = CSSMinifier::minify( $css ); |
| 69 |
} |
| 70 |
|
| 71 |
return file_put_contents( $path_style, $css ); |
| 72 |
} |
| 73 |
|
| 74 |
public static function update_script( $data, $id ) { |
| 75 |
if ( ! is_numeric( $id ) ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
$path_js = self::path_upload_dir() . 'script-' . $id . '.js'; |
| 80 |
|
| 81 |
$param = ! empty( $data['param'] ) ? maybe_unserialize( $data['param'] ) : []; |
| 82 |
|
| 83 |
$js_code = wp_specialchars_decode( $data['js_code'], ENT_QUOTES ); |
| 84 |
|
| 85 |
$minified = ! empty( $param['minified_js'] ) ? $param['minified_js'] : 'obfuscate'; |
| 86 |
|
| 87 |
if ( $minified === 'obfuscate' ) { |
| 88 |
$packer = new Obfuscator( $js_code, 'Normal', true, false ); |
| 89 |
$js_code = $packer->pack(); |
| 90 |
} |
| 91 |
if ( $minified === 'minify' ) { |
| 92 |
$packer = new Obfuscator( $js_code, 'None', true, false ); |
| 93 |
$js_code = $packer->pack(); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
return file_put_contents( $path_js, $js_code ); |
| 98 |
} |
| 99 |
|
| 100 |
public static function update_files( $data, $id ): void { |
| 101 |
if ( ! empty( $data['css_code'] ) ) { |
| 102 |
self::update_style( $data, $id ); |
| 103 |
} |
| 104 |
if ( ! empty( $data['js_code'] ) ) { |
| 105 |
self::update_script( $data, $id ); |
| 106 |
} |
| 107 |
} |
| 108 |
} |