| @@ -1,0 +1,156 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Classes; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | + | |
| 9 | +class FileUpload { | |
| 10 | + | |
| 11 | + public function upload_file( $file, $supported_file_types = [] ) { | |
| 12 | + global $wp_filesystem; | |
| 13 | + if ( empty( $wp_filesystem ) ) { | |
| 14 | + require_once ABSPATH . '/wp-admin/includes/file.php'; | |
| 15 | + WP_Filesystem(); | |
| 16 | + } | |
| 17 | + | |
| 18 | + if ( ! empty( $file ) && ! empty( $file['name'] ) ) { | |
| 19 | + $filename = $file['name']; | |
| 20 | + do_action( 'ablocks/before_upload_file', $filename ); | |
| 21 | + } | |
| 22 | + | |
| 23 | + $this->create_folder(); | |
| 24 | + | |
| 25 | + $results = array( | |
| 26 | + 'error' => apply_filters( 'ablocks/file_upload_error_message', __( 'Error occurred, please try again', 'ablocks' ) ), | |
| 27 | + 'path' => '', | |
| 28 | + 'url' => '', | |
| 29 | + 'file_name' => '' | |
| 30 | + ); | |
| 31 | + | |
| 32 | + $path = ( isset( $file['name'] ) ) ? sanitize_text_field( $file['name'] ) : ''; | |
| 33 | + $ext = pathinfo( $path, PATHINFO_EXTENSION ); | |
| 34 | + | |
| 35 | + if ( count( $supported_file_types ) && ! in_array( $ext, $supported_file_types, true ) ) { | |
| 36 | + return apply_filters( 'ablocks/not_supported_upload_file_error_message', __( 'Invalid file extension', 'ablocks' ) ); | |
| 37 | + } | |
| 38 | + | |
| 39 | + $filename = md5( time() ) . basename( $path ); | |
| 40 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 41 | + $file = ( isset( $file['tmp_name'] ) ) ? file_get_contents( sanitize_text_field( $file['tmp_name'] ) ) : ''; | |
| 42 | + $upload_file = wp_upload_bits( $filename, null, $file ); | |
| 43 | + | |
| 44 | + if ( $upload_file['error'] ) { | |
| 45 | + $results['error'] = $upload_file['error']; | |
| 46 | + return $results; | |
| 47 | + } | |
| 48 | + | |
| 49 | + $wp_filesystem->move( $upload_file['file'], $destination_path ); | |
| 50 | + | |
| 51 | + $file_data = $this->get_file_data( $filename ); | |
| 52 | + $results['error'] = ''; | |
| 53 | + $results['path'] = $file_data['path']; | |
| 54 | + $results['url'] = $file_data['url']; | |
| 55 | + $results['file_name'] = $filename; | |
| 56 | + | |
| 57 | + return $results; | |
| 58 | + } | |
| 59 | + | |
| 60 | + public function create_file( $file_name, $content = '' ) { | |
| 61 | + do_action( 'ablocks/before_create_file', $file_name ); | |
| 62 | + $this->create_folder(); | |
| 63 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents | |
| 64 | + return file_put_contents( $this->get_file_path( $file_name ), $content ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + public function get_file_data( $filename ) { | |
| 68 | + return array( | |
| 69 | + 'path' => $this->get_file_path( $filename ), | |
| 70 | + 'url' => $this->get_file_url( $filename ), | |
| 71 | + ); | |
| 72 | + } | |
| 73 | + | |
| 74 | + public function get_file_path( $filename ) { | |
| 75 | + return $this->get_upload_dir() . '/' . $filename; | |
| 76 | + } | |
| 77 | + | |
| 78 | + public function get_file_url( $filename ) { | |
| 79 | + return $this->get_upload_url() . '/' . $filename; | |
| 80 | + } | |
| 81 | + | |
| 82 | + public function get_upload_url() { | |
| 83 | + $upload = wp_upload_dir(); | |
| 84 | + $upload_url = $upload['baseurl']; | |
| 85 | + $upload_url = $upload_url . '/ablocks_uploads'; | |
| 86 | + return set_url_scheme( $upload_url ); | |
| 87 | + } | |
| 88 | + | |
| 89 | + public function get_upload_dir() { | |
| 90 | + $upload = wp_upload_dir(); | |
| 91 | + $upload_dir = $upload['basedir']; | |
| 92 | + $upload_dir = $upload_dir . '/ablocks_uploads'; | |
| 93 | + return $upload_dir; | |
| 94 | + } | |
| 95 | + public function create_folder() { | |
| 96 | + global $wp_filesystem; | |
| 97 | + if ( empty( $wp_filesystem ) ) { | |
| 98 | + require_once ABSPATH . '/wp-admin/includes/file.php'; | |
| 99 | + WP_Filesystem(); | |
| 100 | + } | |
| 101 | + $upload_dir = $this->get_upload_dir(); | |
| 102 | + | |
| 103 | + if ( ! $wp_filesystem->is_dir( $upload_dir ) ) { | |
| 104 | + wp_mkdir_p( $upload_dir ); | |
| 105 | + } | |
| 106 | + } | |
| 107 | + public function has_upload_file( $filename ) { | |
| 108 | + global $wp_filesystem; | |
| 109 | + if ( empty( $wp_filesystem ) ) { | |
| 110 | + require_once ABSPATH . '/wp-admin/includes/file.php'; | |
| 111 | + WP_Filesystem(); | |
| 112 | + } | |
| 113 | + $upload_dir = $this->get_upload_dir(); | |
| 114 | + | |
| 115 | + return $wp_filesystem->is_file( $upload_dir . '/' . $filename ); | |
| 116 | + } | |
| 117 | + public function delete_file( $file_name ) { | |
| 118 | + global $wp_filesystem; | |
| 119 | + if ( empty( $wp_filesystem ) ) { | |
| 120 | + require_once ABSPATH . '/wp-admin/includes/file.php'; | |
| 121 | + WP_Filesystem(); | |
| 122 | + } | |
| 123 | + $upload_dir = $this->get_upload_dir(); | |
| 124 | + $file_path = $upload_dir . '/' . $file_name; | |
| 125 | + | |
| 126 | + if ( $wp_filesystem->is_file( $file_path ) ) { | |
| 127 | + return $wp_filesystem->delete( $file_path ); | |
| 128 | + } elseif ( $wp_filesystem->is_dir( $file_path ) ) { | |
| 129 | + return $wp_filesystem->rmdir( $file_path, true ); | |
| 130 | + } | |
| 131 | + return false; | |
| 132 | + } | |
| 133 | + public function delete_files() { | |
| 134 | + global $wp_filesystem; | |
| 135 | + if ( empty( $wp_filesystem ) ) { | |
| 136 | + require_once ABSPATH . '/wp-admin/includes/file.php'; | |
| 137 | + WP_Filesystem(); | |
| 138 | + } | |
| 139 | + | |
| 140 | + $upload_dir = $this->get_upload_dir(); | |
| 141 | + | |
| 142 | + // Get all files in the directory | |
| 143 | + $files = $wp_filesystem->dirlist( $upload_dir ); | |
| 144 | + if ( is_array( $files ) && count( $files ) ) { | |
| 145 | + foreach ( $files as $file => $fileinfo ) { | |
| 146 | + $file_path = $upload_dir . '/' . $file; | |
| 147 | + if ( $wp_filesystem->is_file( $file_path ) ) { | |
| 148 | + $wp_filesystem->delete( $file_path ); | |
| 149 | + } elseif ( $wp_filesystem->is_dir( $file_path ) ) { | |
| 150 | + $wp_filesystem->rmdir( $file_path, true ); | |
| 151 | + } | |
| 152 | + } | |
| 153 | + } | |
| 154 | + return true; | |
| 155 | + } | |
| 156 | +} | |