| @@ -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 | 13 | public $chmod_dir = 0755; |
| 37 | - | |
| 38 | - /** | |
| 39 | - * @var int | |
| 40 | - */ | |
| 41 | 14 | public $chmod_file = 0644; |
| 42 | - | |
| 43 | - /** | |
| 44 | - * @var bool | |
| 45 | - */ | |
| 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,10 +61,8 @@ | ||
| 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 ) { |
| @@ -126,9 +88,8 @@ | ||
| 126 | 88 | */ |
| 127 | 89 | public function combine_files( $file_names ) { |
| 128 | 90 | if ( $this->has_permission ) { |
| 129 | 91 | $content = ''; |
| 130 | - | |
| 131 | 92 | foreach ( $file_names as $file_name ) { |
| 132 | 93 | $content .= $this->get_contents( $file_name ) . "\n"; |
| 133 | 94 | } |
| 134 | 95 | $this->create_file( $content ); |
| @@ -136,10 +97,8 @@ | ||
| 136 | 97 | } |
| 137 | 98 | |
| 138 | 99 | /** |
| 139 | 100 | * @since 3.0 |
| 140 | - * | |
| 141 | - * @return string | |
| 142 | 101 | */ |
| 143 | 102 | public function get_file_contents() { |
| 144 | 103 | $content = ''; |
| 145 | 104 | |
| @@ -151,16 +110,11 @@ | ||
| 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 | 117 | if ( empty( $file ) ) { |
| 164 | 118 | $file = $this->new_file_path; |
| 165 | 119 | } |
| 166 | 120 | |
| @@ -175,9 +129,8 @@ | ||
| 175 | 129 | private function check_permission() { |
| 176 | 130 | $creds = $this->get_creds(); |
| 177 | 131 | |
| 178 | 132 | $this->has_permission = true; |
| 179 | - | |
| 180 | 133 | if ( empty( $creds ) || ! WP_Filesystem( $creds ) ) { |
| 181 | 134 | // initialize the API - any problems and we exit |
| 182 | 135 | $this->show_error_message(); |
| 183 | 136 | $this->has_permission = false; |
| @@ -192,9 +145,8 @@ | ||
| 192 | 145 | private function create_directories( &$dirs_exist ) { |
| 193 | 146 | global $wp_filesystem; |
| 194 | 147 | |
| 195 | 148 | $needed_dirs = $this->get_needed_dirs(); |
| 196 | - | |
| 197 | 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'; |
| @@ -212,9 +164,8 @@ | ||
| 212 | 164 | $dir_names = explode( '/', $this->folder_name ); |
| 213 | 165 | $needed_dirs = array(); |
| 214 | 166 | |
| 215 | 167 | $next_dir = ''; |
| 216 | - | |
| 217 | 168 | foreach ( $dir_names as $dir ) { |
| 218 | 169 | $next_dir .= '/' . $dir; |
| 219 | 170 | $needed_dirs[] = $this->uploads['basedir'] . $next_dir; |
| 220 | 171 | } |
| @@ -221,11 +172,8 @@ | ||
| 221 | 172 | |
| 222 | 173 | return $needed_dirs; |
| 223 | 174 | } |
| 224 | 175 | |
| 225 | - /** | |
| 226 | - * @return array|bool | |
| 227 | - */ | |
| 228 | 176 | private function get_creds() { |
| 229 | 177 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
| 230 | 178 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 231 | 179 | } |
| @@ -230,9 +178,8 @@ | ||
| 230 | 178 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 231 | 179 | } |
| 232 | 180 | |
| 233 | 181 | $access_type = get_filesystem_method(); |
| 234 | - | |
| 235 | 182 | if ( $access_type === 'direct' ) { |
| 236 | 183 | $creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() ); |
| 237 | 184 | } else { |
| 238 | 185 | $creds = $this->get_ftp_creds( $access_type ); |
| @@ -242,10 +189,8 @@ | ||
| 242 | 189 | } |
| 243 | 190 | |
| 244 | 191 | /** |
| 245 | 192 | * @param string $type |
| 246 | - * | |
| 247 | - * @return array|false | |
| 248 | 193 | */ |
| 249 | 194 | private function get_ftp_creds( $type ) { |
| 250 | 195 | $credentials = get_option( |
| 251 | 196 | 'ftp_credentials', |
| @@ -268,9 +213,8 @@ | ||
| 268 | 213 | $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); |
| 269 | 214 | |
| 270 | 215 | if ( strpos( $credentials['hostname'], ':' ) ) { |
| 271 | 216 | list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 ); |
| 272 | - | |
| 273 | 217 | if ( ! is_numeric( $credentials['port'] ) ) { |
| 274 | 218 | unset( $credentials['port'] ); |
| 275 | 219 | } |
| 276 | 220 | } else { |
| @@ -287,13 +231,11 @@ | ||
| 287 | 231 | $credentials['connection_type'] = 'ftp'; |
| 288 | 232 | } |
| 289 | 233 | |
| 290 | 234 | $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 | - | |
| 235 | + $can_ssh = ( 'ssh' == $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) ); | |
| 293 | 236 | if ( $has_creds || $can_ssh ) { |
| 294 | 237 | $stored_credentials = $credentials; |
| 295 | - | |
| 296 | 238 | if ( ! empty( $stored_credentials['port'] ) ) { |
| 297 | 239 | // Save port as part of hostname to simplify above code. |
| 298 | 240 | $stored_credentials['hostname'] .= ':' . $stored_credentials['port']; |
| 299 | 241 | } |