| @@ -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'] ) ) { |
| @@ -97,15 +61,15 @@ | ||
| 97 | 61 | |
| 98 | 62 | /** |
| 99 | 63 | * @since 3.0 |
| 100 | 64 | * |
| 101 | - * @param string $file_content File content. | |
| 102 | - * | |
| 103 | 65 | * @return void |
| 104 | 66 | */ |
| 105 | 67 | public function append_file( $file_content ) { |
| 106 | 68 | if ( $this->has_permission ) { |
| 69 | + | |
| 107 | 70 | if ( file_exists( $this->new_file_path ) ) { |
| 71 | + | |
| 108 | 72 | $existing_content = $this->get_contents(); |
| 109 | 73 | $file_content = $existing_content . $file_content; |
| 110 | 74 | } |
| 111 | 75 | |
| @@ -124,9 +88,8 @@ | ||
| 124 | 88 | */ |
| 125 | 89 | public function combine_files( $file_names ) { |
| 126 | 90 | if ( $this->has_permission ) { |
| 127 | 91 | $content = ''; |
| 128 | - | |
| 129 | 92 | foreach ( $file_names as $file_name ) { |
| 130 | 93 | $content .= $this->get_contents( $file_name ) . "\n"; |
| 131 | 94 | } |
| 132 | 95 | $this->create_file( $content ); |
| @@ -134,26 +97,25 @@ | ||
| 134 | 97 | } |
| 135 | 98 | |
| 136 | 99 | /** |
| 137 | 100 | * @since 3.0 |
| 138 | - * | |
| 139 | - * @return string | |
| 140 | 101 | */ |
| 141 | 102 | public function get_file_contents() { |
| 142 | - 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; | |
| 143 | 110 | } |
| 144 | 111 | |
| 145 | 112 | /** |
| 146 | 113 | * @since 3.0 |
| 147 | - * | |
| 148 | - * @param string $file File. | |
| 149 | - * | |
| 150 | - * @return string | |
| 151 | 114 | */ |
| 152 | 115 | private function get_contents( $file = '' ) { |
| 153 | 116 | global $wp_filesystem; |
| 154 | - | |
| 155 | - if ( ! $file ) { | |
| 117 | + if ( empty( $file ) ) { | |
| 156 | 118 | $file = $this->new_file_path; |
| 157 | 119 | } |
| 158 | 120 | |
| 159 | 121 | return $wp_filesystem->get_contents( $file ); |
| @@ -167,10 +129,9 @@ | ||
| 167 | 129 | private function check_permission() { |
| 168 | 130 | $creds = $this->get_creds(); |
| 169 | 131 | |
| 170 | 132 | $this->has_permission = true; |
| 171 | - | |
| 172 | - if ( ! $creds || ! WP_Filesystem( $creds ) ) { | |
| 133 | + if ( empty( $creds ) || ! WP_Filesystem( $creds ) ) { | |
| 173 | 134 | // initialize the API - any problems and we exit |
| 174 | 135 | $this->show_error_message(); |
| 175 | 136 | $this->has_permission = false; |
| 176 | 137 | } |
| @@ -184,9 +145,8 @@ | ||
| 184 | 145 | private function create_directories( &$dirs_exist ) { |
| 185 | 146 | global $wp_filesystem; |
| 186 | 147 | |
| 187 | 148 | $needed_dirs = $this->get_needed_dirs(); |
| 188 | - | |
| 189 | 149 | foreach ( $needed_dirs as $_dir ) { |
| 190 | 150 | // Only check to see if the Dir exists upon creation failure. Less I/O this way. |
| 191 | 151 | if ( $wp_filesystem->mkdir( $_dir, $this->chmod_dir ) ) { |
| 192 | 152 | $index_path = $_dir . '/index.php'; |
| @@ -202,10 +162,10 @@ | ||
| 202 | 162 | */ |
| 203 | 163 | private function get_needed_dirs() { |
| 204 | 164 | $dir_names = explode( '/', $this->folder_name ); |
| 205 | 165 | $needed_dirs = array(); |
| 206 | - $next_dir = ''; | |
| 207 | 166 | |
| 167 | + $next_dir = ''; | |
| 208 | 168 | foreach ( $dir_names as $dir ) { |
| 209 | 169 | $next_dir .= '/' . $dir; |
| 210 | 170 | $needed_dirs[] = $this->uploads['basedir'] . $next_dir; |
| 211 | 171 | } |
| @@ -212,11 +172,8 @@ | ||
| 212 | 172 | |
| 213 | 173 | return $needed_dirs; |
| 214 | 174 | } |
| 215 | 175 | |
| 216 | - /** | |
| 217 | - * @return array|bool | |
| 218 | - */ | |
| 219 | 176 | private function get_creds() { |
| 220 | 177 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
| 221 | 178 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 222 | 179 | } |
| @@ -221,20 +178,19 @@ | ||
| 221 | 178 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 222 | 179 | } |
| 223 | 180 | |
| 224 | 181 | $access_type = get_filesystem_method(); |
| 225 | - | |
| 226 | 182 | if ( $access_type === 'direct' ) { |
| 227 | - 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 ); | |
| 228 | 186 | } |
| 229 | 187 | |
| 230 | - return $this->get_ftp_creds( $access_type ); | |
| 188 | + return $creds; | |
| 231 | 189 | } |
| 232 | 190 | |
| 233 | 191 | /** |
| 234 | 192 | * @param string $type |
| 235 | - * | |
| 236 | - * @return array|false | |
| 237 | 193 | */ |
| 238 | 194 | private function get_ftp_creds( $type ) { |
| 239 | 195 | $credentials = get_option( |
| 240 | 196 | 'ftp_credentials', |
| @@ -255,11 +211,10 @@ | ||
| 255 | 211 | // Sanitize the hostname, Some people might pass in odd-data. |
| 256 | 212 | // Strip any schemes off. |
| 257 | 213 | $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); |
| 258 | 214 | |
| 259 | - if ( str_contains( $credentials['hostname'], ':' ) ) { | |
| 215 | + if ( strpos( $credentials['hostname'], ':' ) ) { | |
| 260 | 216 | list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 ); |
| 261 | - | |
| 262 | 217 | if ( ! is_numeric( $credentials['port'] ) ) { |
| 263 | 218 | unset( $credentials['port'] ); |
| 264 | 219 | } |
| 265 | 220 | } else { |
| @@ -265,11 +220,11 @@ | ||
| 265 | 220 | } else { |
| 266 | 221 | unset( $credentials['port'] ); |
| 267 | 222 | } |
| 268 | 223 | |
| 269 | - 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 ) ) { | |
| 270 | 225 | $credentials['connection_type'] = 'ssh'; |
| 271 | - } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) { | |
| 226 | + } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) { | |
| 272 | 227 | // Only the FTP Extension understands SSL. |
| 273 | 228 | $credentials['connection_type'] = 'ftps'; |
| 274 | 229 | } elseif ( ! isset( $credentials['connection_type'] ) ) { |
| 275 | 230 | // All else fails (And it's not defaulted to something else saved), Default to FTP. |
| @@ -275,14 +230,12 @@ | ||
| 275 | 230 | // All else fails (And it's not defaulted to something else saved), Default to FTP. |
| 276 | 231 | $credentials['connection_type'] = 'ftp'; |
| 277 | 232 | } |
| 278 | 233 | |
| 279 | - $has_creds = ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ); | |
| 280 | - $can_ssh = 'ssh' === $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ); | |
| 281 | - | |
| 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'] ) ); | |
| 282 | 236 | if ( $has_creds || $can_ssh ) { |
| 283 | 237 | $stored_credentials = $credentials; |
| 284 | - | |
| 285 | 238 | if ( ! empty( $stored_credentials['port'] ) ) { |
| 286 | 239 | // Save port as part of hostname to simplify above code. |
| 287 | 240 | $stored_credentials['hostname'] .= ':' . $stored_credentials['port']; |
| 288 | 241 | } |