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