| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPCoder\Dashboard; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
// Exit if accessed directly. |
| 8 |
use WPCoder\Optimization\Obfuscator; |
| 9 |
use WPCoder\Optimization\CSSMinifier; |
| 10 |
use WPCoder\WPCoder; |
| 11 |
|
| 12 |
class FolderManager { |
| 13 |
|
| 14 |
public static function create(): void { |
| 15 |
$basedir = self::path_upload_dir(); |
| 16 |
$index_file = $basedir . 'index.php'; |
| 17 |
|
| 18 |
if ( ! file_exists( $basedir ) ) { |
| 19 |
wp_mkdir_p( $basedir ); |
| 20 |
} |
| 21 |
|
| 22 |
if ( is_dir( $basedir ) && ! file_exists( $index_file ) ) { |
| 23 |
file_put_contents($index_file, '<?php // Silence is golden.'); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
public static function path_upload_dir(): string { |
| 29 |
$upload = wp_upload_dir(); |
| 30 |
|
| 31 |
return $upload['basedir'] . '/' . WPCoder::FOLDER . '/'; |
| 32 |
} |
| 33 |
|
| 34 |
public static function path_upload_url(): string { |
| 35 |
$upload = wp_upload_dir(); |
| 36 |
|
| 37 |
return $upload['baseurl'] . '/' . WPCoder::FOLDER . '/'; |
| 38 |
} |
| 39 |
|
| 40 |
public static function get_style_url( $result ): string { |
| 41 |
$path_style = self::path_upload_dir() . 'style-' . $result->id . '.css'; |
| 42 |
$param = ! empty( $data['param'] ) ? maybe_unserialize( $data['param'] ) : []; |
| 43 |
$css = $result->css_code; |
| 44 |
|
| 45 |
|
| 46 |
if ( ! empty( $param['minified_css'] ) ) { |
| 47 |
$css = CSSMinifier::minify( $css ); |
| 48 |
} |
| 49 |
|
| 50 |
if ( ! file_exists( $path_style ) ) { |
| 51 |
file_put_contents( $path_style, $css ); |
| 52 |
} |
| 53 |
|
| 54 |
return self::path_upload_url() . 'style-' . $result->id . '.css'; |
| 55 |
} |
| 56 |
|
| 57 |
public static function get_js_url( $result ): string { |
| 58 |
|
| 59 |
$path_js = self::path_upload_dir() . 'script-' . $result->id . '.js'; |
| 60 |
|
| 61 |
if ( ! file_exists( $path_js ) ) { |
| 62 |
file_put_contents( $path_js, $result->js_code ); |
| 63 |
} |
| 64 |
|
| 65 |
return self::path_upload_url() . 'script-' . $result->id . '.js'; |
| 66 |
} |
| 67 |
|
| 68 |
public static function update_style( $data, $id ) { |
| 69 |
$path_style = self::path_upload_dir() . 'style-' . $id . '.css'; |
| 70 |
$css = $data['css_code']; |
| 71 |
$param = ! empty( $data['param'] ) ? maybe_unserialize( $data['param'] ) : []; |
| 72 |
|
| 73 |
if ( ! empty( $param['minified_css'] ) ) { |
| 74 |
$css = CSSMinifier::minify( $css ); |
| 75 |
} |
| 76 |
|
| 77 |
return file_put_contents( $path_style, $css ); |
| 78 |
} |
| 79 |
|
| 80 |
public static function update_script( $data, $id ) { |
| 81 |
if ( ! is_numeric( $id ) ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
$path_js = self::path_upload_dir() . 'script-' . $id . '.js'; |
| 86 |
|
| 87 |
$param = ! empty( $data['param'] ) ? maybe_unserialize( $data['param'] ) : []; |
| 88 |
|
| 89 |
$js_code = wp_specialchars_decode( $data['js_code'], ENT_QUOTES ); |
| 90 |
|
| 91 |
$minified = ! empty( $param['minified_js'] ) ? $param['minified_js'] : 'obfuscate'; |
| 92 |
|
| 93 |
if ( $minified === 'obfuscate' ) { |
| 94 |
$packer = new Obfuscator( $js_code, 'Normal', true, false ); |
| 95 |
$js_code = $packer->pack(); |
| 96 |
} |
| 97 |
if ( $minified === 'minify' ) { |
| 98 |
$packer = new Obfuscator( $js_code, 'None', true, false ); |
| 99 |
$js_code = $packer->pack(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
return file_put_contents( $path_js, $js_code ); |
| 104 |
} |
| 105 |
|
| 106 |
public static function code_path( $result ): string { |
| 107 |
|
| 108 |
$path = self::path_upload_dir() . 'wp-code-' . $result->id . '.php'; |
| 109 |
|
| 110 |
if ( ! file_exists( $path ) ) { |
| 111 |
$content = "<?php\n\n defined( 'ABSPATH' ) || exit;\n"; |
| 112 |
$content .= $result->php_code; |
| 113 |
file_put_contents( $path, $content ); |
| 114 |
} |
| 115 |
|
| 116 |
return self::path_upload_dir() . 'wp-code-' . $result->id . '.php'; |
| 117 |
} |
| 118 |
|
| 119 |
public static function update_code( $data, $id) { |
| 120 |
|
| 121 |
if ( ! is_numeric( $id ) ) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
$path = self::path_upload_dir() . 'wp-code-' . $id . '.php'; |
| 126 |
|
| 127 |
$content = "<?php\n\n defined( 'ABSPATH' ) || exit;\n"; |
| 128 |
$content .= $data['php_code']; |
| 129 |
|
| 130 |
return file_put_contents( $path, $content ); |
| 131 |
} |
| 132 |
|
| 133 |
public static function update_files( $data, $id ): void { |
| 134 |
if ( ! empty( $data['css_code'] ) ) { |
| 135 |
self::update_style( $data, $id ); |
| 136 |
} |
| 137 |
if ( ! empty( $data['js_code'] ) ) { |
| 138 |
self::update_script( $data, $id ); |
| 139 |
} |
| 140 |
|
| 141 |
if ( ! empty( $data['php_code'] ) ) { |
| 142 |
self::update_code( $data, $id ); |
| 143 |
} |
| 144 |
} |
| 145 |
} |