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