| @@ -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,16 +106,18 @@ | ||
| 102 | 106 | * |
| 103 | 107 | * @return void |
| 104 | 108 | */ |
| 105 | 109 | public function append_file( $file_content ) { |
| 106 | - if ( $this->has_permission ) { | |
| 107 | - if ( file_exists( $this->new_file_path ) ) { | |
| 108 | - $existing_content = $this->get_contents(); | |
| 109 | - $file_content = $existing_content . $file_content; | |
| 110 | - } | |
| 110 | + if ( ! $this->has_permission ) { | |
| 111 | + return; | |
| 112 | + } | |
| 111 | 113 | |
| 112 | - $this->create_file( $file_content ); | |
| 114 | + if ( file_exists( $this->new_file_path ) ) { | |
| 115 | + $existing_content = $this->get_contents(); | |
| 116 | + $file_content = $existing_content . $file_content; | |
| 113 | 117 | } |
| 118 | + | |
| 119 | + $this->create_file( $file_content ); | |
| 114 | 120 | } |
| 115 | 121 | |
| 116 | 122 | /** |
| 117 | 123 | * Combine an array of files into one |
| @@ -122,16 +128,18 @@ | ||
| 122 | 128 | * |
| 123 | 129 | * @return void |
| 124 | 130 | */ |
| 125 | 131 | public function combine_files( $file_names ) { |
| 126 | - if ( $this->has_permission ) { | |
| 127 | - $content = ''; | |
| 132 | + if ( ! $this->has_permission ) { | |
| 133 | + return; | |
| 134 | + } | |
| 128 | 135 | |
| 129 | - foreach ( $file_names as $file_name ) { | |
| 130 | - $content .= $this->get_contents( $file_name ) . "\n"; | |
| 131 | - } | |
| 132 | - $this->create_file( $content ); | |
| 136 | + $content = ''; | |
| 137 | + | |
| 138 | + foreach ( $file_names as $file_name ) { | |
| 139 | + $content .= $this->get_contents( $file_name ) . "\n"; | |
| 133 | 140 | } |
| 141 | + $this->create_file( $content ); | |
| 134 | 142 | } |
| 135 | 143 | |
| 136 | 144 | /** |
| 137 | 145 | * @since 3.0 |
| @@ -168,13 +176,15 @@ | ||
| 168 | 176 | $creds = $this->get_creds(); |
| 169 | 177 | |
| 170 | 178 | $this->has_permission = true; |
| 171 | 179 | |
| 172 | - if ( ! $creds || ! WP_Filesystem( $creds ) ) { | |
| 173 | - // initialize the API - any problems and we exit | |
| 174 | - $this->show_error_message(); | |
| 175 | - $this->has_permission = false; | |
| 180 | + if ( $creds && WP_Filesystem( $creds ) ) { | |
| 181 | + return; | |
| 176 | 182 | } |
| 183 | + | |
| 184 | + // Initialize the API - any problems and we exit | |
| 185 | + $this->show_error_message(); | |
| 186 | + $this->has_permission = false; | |
| 177 | 187 | } |
| 178 | 188 | |
| 179 | 189 | /** |
| 180 | 190 | * @param true $dirs_exist |
| @@ -183,11 +193,9 @@ | ||
| 183 | 193 | */ |
| 184 | 194 | private function create_directories( &$dirs_exist ) { |
| 185 | 195 | global $wp_filesystem; |
| 186 | 196 | |
| 187 | - $needed_dirs = $this->get_needed_dirs(); | |
| 188 | - | |
| 189 | - foreach ( $needed_dirs as $_dir ) { | |
| 197 | + foreach ( $this->get_needed_dirs() as $_dir ) { | |
| 190 | 198 | // Only check to see if the Dir exists upon creation failure. Less I/O this way. |
| 191 | 199 | if ( $wp_filesystem->mkdir( $_dir, $this->chmod_dir ) ) { |
| 192 | 200 | $index_path = $_dir . '/index.php'; |
| 193 | 201 | $wp_filesystem->put_contents( $index_path, "<?php\n// Silence is golden.\n?>", $this->chmod_file ); |
| @@ -298,9 +306,9 @@ | ||
| 298 | 306 | /** |
| 299 | 307 | * @return void |
| 300 | 308 | */ |
| 301 | 309 | private function show_error_message() { |
| 302 | - if ( ! empty( $this->error_message ) ) { | |
| 310 | + if ( $this->error_message ) { | |
| 303 | 311 | echo '<div class="message">' . esc_html( $this->error_message ) . '</div>'; |
| 304 | 312 | } |
| 305 | 313 | } |
| 306 | 314 | } |