| @@ -4,55 +4,21 @@ | ||
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | class FrmCreateFile { |
| 7 | 7 | |
| 8 | - /** | |
| 9 | - * @var string | |
| 10 | - */ | |
| 11 | 8 | public $folder_name; |
| 12 | - | |
| 13 | - /** | |
| 14 | - * @var string | |
| 15 | - */ | |
| 16 | 9 | public $file_name; |
| 17 | - | |
| 18 | - /** | |
| 19 | - * @var string | |
| 20 | - */ | |
| 21 | 10 | public $error_message; |
| 22 | - | |
| 23 | - /** | |
| 24 | - * @var array | |
| 25 | - */ | |
| 26 | 11 | public $uploads; |
| 27 | - | |
| 28 | - /** | |
| 29 | - * @var string | |
| 30 | - */ | |
| 31 | 12 | private $new_file_path; |
| 32 | - | |
| 33 | - /** | |
| 34 | - * @var int | |
| 35 | - */ | |
| 36 | - public $chmod_dir = 0755; | |
| 37 | - | |
| 38 | - /** | |
| 39 | - * @var int | |
| 40 | - */ | |
| 41 | - public $chmod_file = 0644; | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * @var bool | |
| 45 | - */ | |
| 13 | + public $chmod_dir = 0755; | |
| 14 | + public $chmod_file = 0644; | |
| 46 | 15 | private $has_permission = false; |
| 47 | 16 | |
| 48 | - /** | |
| 49 | - * @param array $atts | |
| 50 | - */ | |
| 51 | 17 | public function __construct( $atts ) { |
| 52 | - $this->folder_name = $atts['folder_name'] ?? ''; | |
| 18 | + $this->folder_name = isset( $atts['folder_name'] ) ? $atts['folder_name'] : ''; | |
| 53 | 19 | $this->file_name = $atts['file_name']; |
| 54 | - $this->error_message = $atts['error_message'] ?? ''; | |
| 20 | + $this->error_message = isset( $atts['error_message'] ) ? $atts['error_message'] : ''; | |
| 55 | 21 | $this->uploads = wp_upload_dir(); |
| 56 | 22 | $this->set_new_file_path( $atts ); |
| 57 | 23 | $this->chmod_dir = defined( 'FS_CHMOD_DIR' ) ? FS_CHMOD_DIR : ( fileperms( ABSPATH ) & 0777 | 0755 ); |
| 58 | 24 | $this->chmod_file = defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ); |
| @@ -62,10 +28,8 @@ | ||
| 62 | 28 | |
| 63 | 29 | /** |
| 64 | 30 | * @since 3.0 |
| 65 | 31 | * |
| 66 | - * @param array $atts Attributes. | |
| 67 | - * | |
| 68 | 32 | * @return void |
| 69 | 33 | */ |
| 70 | 34 | private function set_new_file_path( $atts ) { |
| 71 | 35 | if ( isset( $atts['new_file_path'] ) ) { |
| @@ -80,44 +44,38 @@ | ||
| 80 | 44 | * |
| 81 | 45 | * @return void |
| 82 | 46 | */ |
| 83 | 47 | public function create_file( $file_content ) { |
| 84 | - if ( ! $this->has_permission ) { | |
| 85 | - return; | |
| 86 | - } | |
| 48 | + if ( $this->has_permission ) { | |
| 49 | + $dirs_exist = true; | |
| 87 | 50 | |
| 88 | - $dirs_exist = true; | |
| 51 | + // Create the directories if need be. | |
| 52 | + $this->create_directories( $dirs_exist ); | |
| 89 | 53 | |
| 90 | - // Create the directories if need be. | |
| 91 | - $this->create_directories( $dirs_exist ); | |
| 92 | - | |
| 93 | - // Only write the file if the folders exist. | |
| 94 | - if ( ! $dirs_exist ) { | |
| 95 | - return; | |
| 54 | + // Only write the file if the folders exist. | |
| 55 | + if ( $dirs_exist ) { | |
| 56 | + global $wp_filesystem; | |
| 57 | + $wp_filesystem->put_contents( $this->new_file_path, $file_content, $this->chmod_file ); | |
| 58 | + } | |
| 96 | 59 | } |
| 97 | - | |
| 98 | - global $wp_filesystem; | |
| 99 | - $wp_filesystem->put_contents( $this->new_file_path, $file_content, $this->chmod_file ); | |
| 100 | 60 | } |
| 101 | 61 | |
| 102 | 62 | /** |
| 103 | 63 | * @since 3.0 |
| 104 | 64 | * |
| 105 | - * @param string $file_content File content. | |
| 106 | - * | |
| 107 | 65 | * @return void |
| 108 | 66 | */ |
| 109 | 67 | public function append_file( $file_content ) { |
| 110 | - if ( ! $this->has_permission ) { | |
| 111 | - return; | |
| 112 | - } | |
| 68 | + if ( $this->has_permission ) { | |
| 113 | 69 | |
| 114 | - if ( file_exists( $this->new_file_path ) ) { | |
| 115 | - $existing_content = $this->get_contents(); | |
| 116 | - $file_content = $existing_content . $file_content; | |
| 70 | + if ( file_exists( $this->new_file_path ) ) { | |
| 71 | + | |
| 72 | + $existing_content = $this->get_contents(); | |
| 73 | + $file_content = $existing_content . $file_content; | |
| 74 | + } | |
| 75 | + | |
| 76 | + $this->create_file( $file_content ); | |
| 117 | 77 | } |
| 118 | - | |
| 119 | - $this->create_file( $file_content ); | |
| 120 | 78 | } |
| 121 | 79 | |
| 122 | 80 | /** |
| 123 | 81 | * Combine an array of files into one |
| @@ -128,40 +86,36 @@ | ||
| 128 | 86 | * |
| 129 | 87 | * @return void |
| 130 | 88 | */ |
| 131 | 89 | public function combine_files( $file_names ) { |
| 132 | - if ( ! $this->has_permission ) { | |
| 133 | - return; | |
| 90 | + if ( $this->has_permission ) { | |
| 91 | + $content = ''; | |
| 92 | + foreach ( $file_names as $file_name ) { | |
| 93 | + $content .= $this->get_contents( $file_name ) . "\n"; | |
| 94 | + } | |
| 95 | + $this->create_file( $content ); | |
| 134 | 96 | } |
| 135 | - | |
| 136 | - $content = ''; | |
| 137 | - | |
| 138 | - foreach ( $file_names as $file_name ) { | |
| 139 | - $content .= $this->get_contents( $file_name ) . "\n"; | |
| 140 | - } | |
| 141 | - $this->create_file( $content ); | |
| 142 | 97 | } |
| 143 | 98 | |
| 144 | 99 | /** |
| 145 | 100 | * @since 3.0 |
| 146 | - * | |
| 147 | - * @return string | |
| 148 | 101 | */ |
| 149 | 102 | public function get_file_contents() { |
| 150 | - return $this->has_permission ? $this->get_contents() : ''; | |
| 103 | + $content = ''; | |
| 104 | + | |
| 105 | + if ( $this->has_permission ) { | |
| 106 | + $content = $this->get_contents(); | |
| 107 | + } | |
| 108 | + | |
| 109 | + return $content; | |
| 151 | 110 | } |
| 152 | 111 | |
| 153 | 112 | /** |
| 154 | 113 | * @since 3.0 |
| 155 | - * | |
| 156 | - * @param string $file File. | |
| 157 | - * | |
| 158 | - * @return string | |
| 159 | 114 | */ |
| 160 | 115 | private function get_contents( $file = '' ) { |
| 161 | 116 | global $wp_filesystem; |
| 162 | - | |
| 163 | - if ( ! $file ) { | |
| 117 | + if ( empty( $file ) ) { | |
| 164 | 118 | $file = $this->new_file_path; |
| 165 | 119 | } |
| 166 | 120 | |
| 167 | 121 | return $wp_filesystem->get_contents( $file ); |
| @@ -175,16 +129,13 @@ | ||
| 175 | 129 | private function check_permission() { |
| 176 | 130 | $creds = $this->get_creds(); |
| 177 | 131 | |
| 178 | 132 | $this->has_permission = true; |
| 179 | - | |
| 180 | - if ( $creds && WP_Filesystem( $creds ) ) { | |
| 181 | - return; | |
| 133 | + if ( empty( $creds ) || ! WP_Filesystem( $creds ) ) { | |
| 134 | + // initialize the API - any problems and we exit | |
| 135 | + $this->show_error_message(); | |
| 136 | + $this->has_permission = false; | |
| 182 | 137 | } |
| 183 | - | |
| 184 | - // Initialize the API - any problems and we exit | |
| 185 | - $this->show_error_message(); | |
| 186 | - $this->has_permission = false; | |
| 187 | 138 | } |
| 188 | 139 | |
| 189 | 140 | /** |
| 190 | 141 | * @param true $dirs_exist |
| @@ -193,9 +144,10 @@ | ||
| 193 | 144 | */ |
| 194 | 145 | private function create_directories( &$dirs_exist ) { |
| 195 | 146 | global $wp_filesystem; |
| 196 | 147 | |
| 197 | - foreach ( $this->get_needed_dirs() as $_dir ) { | |
| 148 | + $needed_dirs = $this->get_needed_dirs(); | |
| 149 | + foreach ( $needed_dirs as $_dir ) { | |
| 198 | 150 | // Only check to see if the Dir exists upon creation failure. Less I/O this way. |
| 199 | 151 | if ( $wp_filesystem->mkdir( $_dir, $this->chmod_dir ) ) { |
| 200 | 152 | $index_path = $_dir . '/index.php'; |
| 201 | 153 | $wp_filesystem->put_contents( $index_path, "<?php\n// Silence is golden.\n?>", $this->chmod_file ); |
| @@ -210,10 +162,10 @@ | ||
| 210 | 162 | */ |
| 211 | 163 | private function get_needed_dirs() { |
| 212 | 164 | $dir_names = explode( '/', $this->folder_name ); |
| 213 | 165 | $needed_dirs = array(); |
| 214 | - $next_dir = ''; | |
| 215 | 166 | |
| 167 | + $next_dir = ''; | |
| 216 | 168 | foreach ( $dir_names as $dir ) { |
| 217 | 169 | $next_dir .= '/' . $dir; |
| 218 | 170 | $needed_dirs[] = $this->uploads['basedir'] . $next_dir; |
| 219 | 171 | } |
| @@ -220,11 +172,8 @@ | ||
| 220 | 172 | |
| 221 | 173 | return $needed_dirs; |
| 222 | 174 | } |
| 223 | 175 | |
| 224 | - /** | |
| 225 | - * @return array|bool | |
| 226 | - */ | |
| 227 | 176 | private function get_creds() { |
| 228 | 177 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
| 229 | 178 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 230 | 179 | } |
| @@ -229,20 +178,19 @@ | ||
| 229 | 178 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 230 | 179 | } |
| 231 | 180 | |
| 232 | 181 | $access_type = get_filesystem_method(); |
| 233 | - | |
| 234 | 182 | if ( $access_type === 'direct' ) { |
| 235 | - return request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() ); | |
| 183 | + $creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() ); | |
| 184 | + } else { | |
| 185 | + $creds = $this->get_ftp_creds( $access_type ); | |
| 236 | 186 | } |
| 237 | 187 | |
| 238 | - return $this->get_ftp_creds( $access_type ); | |
| 188 | + return $creds; | |
| 239 | 189 | } |
| 240 | 190 | |
| 241 | 191 | /** |
| 242 | 192 | * @param string $type |
| 243 | - * | |
| 244 | - * @return array|false | |
| 245 | 193 | */ |
| 246 | 194 | private function get_ftp_creds( $type ) { |
| 247 | 195 | $credentials = get_option( |
| 248 | 196 | 'ftp_credentials', |
| @@ -263,11 +211,10 @@ | ||
| 263 | 211 | // Sanitize the hostname, Some people might pass in odd-data. |
| 264 | 212 | // Strip any schemes off. |
| 265 | 213 | $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); |
| 266 | 214 | |
| 267 | - if ( str_contains( $credentials['hostname'], ':' ) ) { | |
| 215 | + if ( strpos( $credentials['hostname'], ':' ) ) { | |
| 268 | 216 | list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 ); |
| 269 | - | |
| 270 | 217 | if ( ! is_numeric( $credentials['port'] ) ) { |
| 271 | 218 | unset( $credentials['port'] ); |
| 272 | 219 | } |
| 273 | 220 | } else { |
| @@ -273,11 +220,11 @@ | ||
| 273 | 220 | } else { |
| 274 | 221 | unset( $credentials['port'] ); |
| 275 | 222 | } |
| 276 | 223 | |
| 277 | - if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' === FS_METHOD ) ) { | |
| 224 | + if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) { | |
| 278 | 225 | $credentials['connection_type'] = 'ssh'; |
| 279 | - } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) { | |
| 226 | + } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) { | |
| 280 | 227 | // Only the FTP Extension understands SSL. |
| 281 | 228 | $credentials['connection_type'] = 'ftps'; |
| 282 | 229 | } elseif ( ! isset( $credentials['connection_type'] ) ) { |
| 283 | 230 | // All else fails (And it's not defaulted to something else saved), Default to FTP. |
| @@ -283,14 +230,12 @@ | ||
| 283 | 230 | // All else fails (And it's not defaulted to something else saved), Default to FTP. |
| 284 | 231 | $credentials['connection_type'] = 'ftp'; |
| 285 | 232 | } |
| 286 | 233 | |
| 287 | - $has_creds = ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ); | |
| 288 | - $can_ssh = 'ssh' === $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ); | |
| 289 | - | |
| 234 | + $has_creds = ( ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ) ); | |
| 235 | + $can_ssh = ( 'ssh' == $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) ); | |
| 290 | 236 | if ( $has_creds || $can_ssh ) { |
| 291 | 237 | $stored_credentials = $credentials; |
| 292 | - | |
| 293 | 238 | if ( ! empty( $stored_credentials['port'] ) ) { |
| 294 | 239 | // Save port as part of hostname to simplify above code. |
| 295 | 240 | $stored_credentials['hostname'] .= ':' . $stored_credentials['port']; |
| 296 | 241 | } |
| @@ -306,9 +251,9 @@ | ||
| 306 | 251 | /** |
| 307 | 252 | * @return void |
| 308 | 253 | */ |
| 309 | 254 | private function show_error_message() { |
| 310 | - if ( $this->error_message ) { | |
| 255 | + if ( ! empty( $this->error_message ) ) { | |
| 311 | 256 | echo '<div class="message">' . esc_html( $this->error_message ) . '</div>'; |
| 312 | 257 | } |
| 313 | 258 | } |
| 314 | 259 | } |