| 1 |
<?php |
| 2 |
namespace Better_Payment\Lite\Blocks; |
| 3 |
|
| 4 |
class AssetGeneration extends ThirdPartyIntegration |
| 5 |
{ |
| 6 |
|
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
$this->add_ajax( |
| 10 |
[ |
| 11 |
'eb_regenerate_assets' => [ |
| 12 |
'callback' => 'regenerate_assets', |
| 13 |
'public' => false |
| 14 |
] |
| 15 |
] |
| 16 |
); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* regenerate_assets |
| 21 |
*/ |
| 22 |
public function regenerate_assets() |
| 23 |
{ |
| 24 |
$response_data = [ 'messsage' => __( 'Sorry, you are not allowed to do this operation.', 'essential-blocks' ) ]; |
| 25 |
|
| 26 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 27 |
wp_send_json_error( $response_data ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Nonce verification |
| 32 |
*/ |
| 33 |
if ( ! isset( $_POST[ 'admin_nonce' ] ) || ! wp_verify_nonce( sanitize_key( $_POST[ 'admin_nonce' ] ), 'admin-nonce' ) ) { |
| 34 |
die( esc_html__( 'Nonce did not match', 'essential-blocks' ) ); |
| 35 |
} |
| 36 |
|
| 37 |
if ( empty( $_POST ) ) { |
| 38 |
$response_data = [ 'messsage' => __( 'No post data found!', 'essential-blocks' ) ]; |
| 39 |
wp_send_json_error( $response_data ); |
| 40 |
} |
| 41 |
|
| 42 |
$upload_dir = wp_upload_dir(); |
| 43 |
$base_path = $upload_dir[ 'basedir' ]; |
| 44 |
self::remove_directory_files( $base_path . '/eb-style' ); |
| 45 |
$response_data = [ |
| 46 |
'messsage' => __( 'Successfully saved data!', 'essential-blocks' ) |
| 47 |
]; |
| 48 |
wp_send_json_success( $response_data ); |
| 49 |
} |
| 50 |
|
| 51 |
public static function remove_directory_files( $src ) |
| 52 |
{ |
| 53 |
if ( is_dir( $src ) ) { |
| 54 |
$dir = opendir( $src ); |
| 55 |
while ( false !== ( $file = readdir( $dir ) ) ) { |
| 56 |
if ( ( $file != '.' ) && ( $file != '..' ) ) { |
| 57 |
$full = $src . '/' . $file; |
| 58 |
if ( is_dir( $full ) ) { |
| 59 |
self::remove_directory_files( $full ); |
| 60 |
} else { |
| 61 |
wp_delete_file( $full ); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
closedir( $dir ); |
| 66 |
rmdir( $src ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public static function remove_file( $src ) |
| 71 |
{ |
| 72 |
if ( file_exists( $src ) ) { |
| 73 |
unlink( $src ); |
| 74 |
} |
| 75 |
} |
| 76 |
} |