| @@ -32,14 +32,14 @@ | ||
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * @var int |
| 35 | 35 | */ |
| 36 | - public $chmod_dir = 0755; | |
| 36 | + public $chmod_dir = 0755; | |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | 39 | * @var int |
| 40 | 40 | */ |
| 41 | - public $chmod_file = 0644; | |
| 41 | + public $chmod_file = 0644; | |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | 44 | * @var bool |
| 45 | 45 | */ |
| @@ -80,20 +80,24 @@ | ||
| 80 | 80 | * |
| 81 | 81 | * @return void |
| 82 | 82 | */ |
| 83 | 83 | public function create_file( $file_content ) { |
| 84 | - if ( $this->has_permission ) { | |
| 85 | - $dirs_exist = true; | |
| 84 | + if ( ! $this->has_permission ) { | |
| 85 | + return; | |
| 86 | + } | |
| 86 | 87 | |
| 87 | - // Create the directories if need be. | |
| 88 | - $this->create_directories( $dirs_exist ); | |
| 88 | + $dirs_exist = true; | |
| 89 | 89 | |
| 90 | - // Only write the file if the folders exist. | |
| 91 | - if ( $dirs_exist ) { | |
| 92 | - global $wp_filesystem; | |
| 93 | - $wp_filesystem->put_contents( $this->new_file_path, $file_content, $this->chmod_file ); | |
| 94 | - } | |
| 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; | |
| 95 | 96 | } |
| 97 | + | |
| 98 | + global $wp_filesystem; | |
| 99 | + $wp_filesystem->put_contents( $this->new_file_path, $file_content, $this->chmod_file ); | |
| 96 | 100 | } |
| 97 | 101 | |
| 98 | 102 | /** |
| 99 | 103 | * @since 3.0 |
| @@ -102,18 +106,18 @@ | ||
| 102 | 106 | * |
| 103 | 107 | * @return void |
| 104 | 108 | */ |
| 105 | 109 | public function append_file( $file_content ) { |
| 106 | - if ( $this->has_permission ) { | |
| 110 | + if ( ! $this->has_permission ) { | |
| 111 | + return; | |
| 112 | + } | |
| 107 | 113 | |
| 108 | - if ( file_exists( $this->new_file_path ) ) { | |
| 114 | + if ( file_exists( $this->new_file_path ) ) { | |
| 115 | + $existing_content = $this->get_contents(); | |
| 116 | + $file_content = $existing_content . $file_content; | |
| 117 | + } | |
| 109 | 118 | |
| 110 | - $existing_content = $this->get_contents(); | |
| 111 | - $file_content = $existing_content . $file_content; | |
| 112 | - } | |
| 113 | - | |
| 114 | - $this->create_file( $file_content ); | |
| 115 | - } | |
| 119 | + $this->create_file( $file_content ); | |
| 116 | 120 | } |
| 117 | 121 | |
| 118 | 122 | /** |
| 119 | 123 | * Combine an array of files into one |
| @@ -124,16 +128,18 @@ | ||
| 124 | 128 | * |
| 125 | 129 | * @return void |
| 126 | 130 | */ |
| 127 | 131 | public function combine_files( $file_names ) { |
| 128 | - if ( $this->has_permission ) { | |
| 129 | - $content = ''; | |
| 132 | + if ( ! $this->has_permission ) { | |
| 133 | + return; | |
| 134 | + } | |
| 130 | 135 | |
| 131 | - foreach ( $file_names as $file_name ) { | |
| 132 | - $content .= $this->get_contents( $file_name ) . "\n"; | |
| 133 | - } | |
| 134 | - $this->create_file( $content ); | |
| 136 | + $content = ''; | |
| 137 | + | |
| 138 | + foreach ( $file_names as $file_name ) { | |
| 139 | + $content .= $this->get_contents( $file_name ) . "\n"; | |
| 135 | 140 | } |
| 141 | + $this->create_file( $content ); | |
| 136 | 142 | } |
| 137 | 143 | |
| 138 | 144 | /** |
| 139 | 145 | * @since 3.0 |
| @@ -140,15 +146,9 @@ | ||
| 140 | 146 | * |
| 141 | 147 | * @return string |
| 142 | 148 | */ |
| 143 | 149 | public function get_file_contents() { |
| 144 | - $content = ''; | |
| 145 | - | |
| 146 | - if ( $this->has_permission ) { | |
| 147 | - $content = $this->get_contents(); | |
| 148 | - } | |
| 149 | - | |
| 150 | - return $content; | |
| 150 | + return $this->has_permission ? $this->get_contents() : ''; | |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
| 154 | 154 | * @since 3.0 |
| @@ -159,9 +159,9 @@ | ||
| 159 | 159 | */ |
| 160 | 160 | private function get_contents( $file = '' ) { |
| 161 | 161 | global $wp_filesystem; |
| 162 | 162 | |
| 163 | - if ( empty( $file ) ) { | |
| 163 | + if ( ! $file ) { | |
| 164 | 164 | $file = $this->new_file_path; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | return $wp_filesystem->get_contents( $file ); |
| @@ -176,13 +176,15 @@ | ||
| 176 | 176 | $creds = $this->get_creds(); |
| 177 | 177 | |
| 178 | 178 | $this->has_permission = true; |
| 179 | 179 | |
| 180 | - if ( empty( $creds ) || ! WP_Filesystem( $creds ) ) { | |
| 181 | - // initialize the API - any problems and we exit | |
| 182 | - $this->show_error_message(); | |
| 183 | - $this->has_permission = false; | |
| 180 | + if ( $creds && WP_Filesystem( $creds ) ) { | |
| 181 | + return; | |
| 184 | 182 | } |
| 183 | + | |
| 184 | + // Initialize the API - any problems and we exit | |
| 185 | + $this->show_error_message(); | |
| 186 | + $this->has_permission = false; | |
| 185 | 187 | } |
| 186 | 188 | |
| 187 | 189 | /** |
| 188 | 190 | * @param true $dirs_exist |
| @@ -191,11 +193,9 @@ | ||
| 191 | 193 | */ |
| 192 | 194 | private function create_directories( &$dirs_exist ) { |
| 193 | 195 | global $wp_filesystem; |
| 194 | 196 | |
| 195 | - $needed_dirs = $this->get_needed_dirs(); | |
| 196 | - | |
| 197 | - foreach ( $needed_dirs as $_dir ) { | |
| 197 | + foreach ( $this->get_needed_dirs() as $_dir ) { | |
| 198 | 198 | // Only check to see if the Dir exists upon creation failure. Less I/O this way. |
| 199 | 199 | if ( $wp_filesystem->mkdir( $_dir, $this->chmod_dir ) ) { |
| 200 | 200 | $index_path = $_dir . '/index.php'; |
| 201 | 201 | $wp_filesystem->put_contents( $index_path, "<?php\n// Silence is golden.\n?>", $this->chmod_file ); |
| @@ -210,11 +210,10 @@ | ||
| 210 | 210 | */ |
| 211 | 211 | private function get_needed_dirs() { |
| 212 | 212 | $dir_names = explode( '/', $this->folder_name ); |
| 213 | 213 | $needed_dirs = array(); |
| 214 | + $next_dir = ''; | |
| 214 | 215 | |
| 215 | - $next_dir = ''; | |
| 216 | - | |
| 217 | 216 | foreach ( $dir_names as $dir ) { |
| 218 | 217 | $next_dir .= '/' . $dir; |
| 219 | 218 | $needed_dirs[] = $this->uploads['basedir'] . $next_dir; |
| 220 | 219 | } |
| @@ -232,14 +231,12 @@ | ||
| 232 | 231 | |
| 233 | 232 | $access_type = get_filesystem_method(); |
| 234 | 233 | |
| 235 | 234 | if ( $access_type === 'direct' ) { |
| 236 | - $creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() ); | |
| 237 | - } else { | |
| 238 | - $creds = $this->get_ftp_creds( $access_type ); | |
| 235 | + return request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() ); | |
| 239 | 236 | } |
| 240 | 237 | |
| 241 | - return $creds; | |
| 238 | + return $this->get_ftp_creds( $access_type ); | |
| 242 | 239 | } |
| 243 | 240 | |
| 244 | 241 | /** |
| 245 | 242 | * @param string $type |
| @@ -266,9 +263,9 @@ | ||
| 266 | 263 | // Sanitize the hostname, Some people might pass in odd-data. |
| 267 | 264 | // Strip any schemes off. |
| 268 | 265 | $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); |
| 269 | 266 | |
| 270 | - if ( strpos( $credentials['hostname'], ':' ) ) { | |
| 267 | + if ( str_contains( $credentials['hostname'], ':' ) ) { | |
| 271 | 268 | list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 ); |
| 272 | 269 | |
| 273 | 270 | if ( ! is_numeric( $credentials['port'] ) ) { |
| 274 | 271 | unset( $credentials['port'] ); |
| @@ -276,11 +273,11 @@ | ||
| 276 | 273 | } else { |
| 277 | 274 | unset( $credentials['port'] ); |
| 278 | 275 | } |
| 279 | 276 | |
| 280 | - if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) { | |
| 277 | + if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' === FS_METHOD ) ) { | |
| 281 | 278 | $credentials['connection_type'] = 'ssh'; |
| 282 | - } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) { | |
| 279 | + } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) { | |
| 283 | 280 | // Only the FTP Extension understands SSL. |
| 284 | 281 | $credentials['connection_type'] = 'ftps'; |
| 285 | 282 | } elseif ( ! isset( $credentials['connection_type'] ) ) { |
| 286 | 283 | // All else fails (And it's not defaulted to something else saved), Default to FTP. |
| @@ -286,10 +283,10 @@ | ||
| 286 | 283 | // All else fails (And it's not defaulted to something else saved), Default to FTP. |
| 287 | 284 | $credentials['connection_type'] = 'ftp'; |
| 288 | 285 | } |
| 289 | 286 | |
| 290 | - $has_creds = ( ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ) ); | |
| 291 | - $can_ssh = ( 'ssh' === $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) ); | |
| 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'] ); | |
| 292 | 289 | |
| 293 | 290 | if ( $has_creds || $can_ssh ) { |
| 294 | 291 | $stored_credentials = $credentials; |
| 295 | 292 | |
| @@ -309,9 +306,9 @@ | ||
| 309 | 306 | /** |
| 310 | 307 | * @return void |
| 311 | 308 | */ |
| 312 | 309 | private function show_error_message() { |
| 313 | - if ( ! empty( $this->error_message ) ) { | |
| 310 | + if ( $this->error_message ) { | |
| 314 | 311 | echo '<div class="message">' . esc_html( $this->error_message ) . '</div>'; |
| 315 | 312 | } |
| 316 | 313 | } |
| 317 | 314 | } |