PluginProbe ʕ •ᴥ•ʔ
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin / 4.7.0
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin v4.7.0
0.9.6 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.2 1.7.0 1.7.1 1.8.0 1.8.1 1.9.0 2.0.0 2.0.1 2.1.1 2.2.1 2.3.1 2.4.0 2.5.0 2.5.1 2.6.0 2.7.0 2.8.0 2.9.0 3.0.1 3.0.2 3.0.3 3.1.0 3.10.0 3.11.0 3.11.1 3.2.0 3.2.1 3.3.0 3.4.0 3.5.0 3.5.1 3.5.2 3.6.1 3.7.0 3.8.0 3.8.2 3.9.0 4.0.1 4.1.0 4.1.1 4.2.0 4.3.0 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.8.0 trunk 0.10.0 0.10.1 0.11.1 0.11.2 0.3.1 0.3.2 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.5.2 0.6 0.7 0.8 0.8.2 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5
wp-mail-smtp / src / Uploads.php
wp-mail-smtp / src Last commit date
Admin 6 months ago Compatibility 6 months ago Helpers 6 months ago Providers 6 months ago Queue 6 months ago Reports 6 months ago Tasks 6 months ago UsageTracking 6 months ago AbstractConnection.php 6 months ago Conflicts.php 6 months ago Connect.php 6 months ago Connection.php 6 months ago ConnectionInterface.php 6 months ago ConnectionsManager.php 6 months ago Core.php 6 months ago DBRepair.php 6 months ago Debug.php 6 months ago Geo.php 6 months ago MailCatcher.php 6 months ago MailCatcherInterface.php 6 months ago MailCatcherTrait.php 6 months ago MailCatcherV6.php 6 months ago Migration.php 6 months ago MigrationAbstract.php 6 months ago Migrations.php 6 months ago OptimizedEmailSending.php 6 months ago Options.php 6 months ago Processor.php 6 months ago SiteHealth.php 6 months ago Upgrade.php 6 months ago Uploads.php 6 months ago WP.php 6 months ago WPMailArgs.php 6 months ago WPMailInitiator.php 6 months ago
Uploads.php
238 lines
1 <?php
2
3 namespace WPMailSMTP;
4
5 use WP_Error;
6 use WP_Filesystem_Direct;
7
8 /**
9 * WPMailSMTP uploads.
10 *
11 * @since 2.8.0
12 */
13 class Uploads {
14
15 /**
16 * Uploads dir name.
17 *
18 * @since 4.1.1
19 */
20 const ROOT_FOLDER_NAME = 'wp-mail-smtp';
21
22 /**
23 * Get WPMailSMTP upload root path (e.g. /wp-content/uploads/wp-mail-smtp).
24 *
25 * @since 2.8.0
26 *
27 * @return array|WP_Error WPMailSMTP upload root path (no trailing slash).
28 */
29 public static function upload_dir() {
30
31 $upload_dir = wp_upload_dir();
32
33 if ( ! empty( $upload_dir['error'] ) ) {
34 return new WP_Error( 'wp_upload_dir_error', $upload_dir['error'] );
35 }
36
37 $dir = self::ROOT_FOLDER_NAME;
38
39 $upload_root = trailingslashit( realpath( $upload_dir['basedir'] ) ) . $dir;
40
41 /**
42 * Filters upload dir path.
43 *
44 * @since 2.8.0
45 *
46 * @param string $upload_root Upload dir path.
47 */
48 $custom_uploads_root = apply_filters( 'wp_mail_smtp_uploads_upload_dir_root', $upload_root );
49 if ( wp_is_writable( $custom_uploads_root ) ) {
50 $upload_root = $custom_uploads_root;
51 }
52
53 if ( ! file_exists( $upload_root ) && ! wp_mkdir_p( $custom_uploads_root ) ) {
54 return new WP_Error(
55 'wp_mail_smtp_upload_dir_unable_create',
56 sprintf(
57 /* translators: %s: Directory path. */
58 __( 'Unable to create directory %s. Is its parent directory writable by the server?', 'wp-mail-smtp' ),
59 esc_html( $upload_root )
60 )
61 );
62 }
63
64 if ( ! wp_is_writable( $custom_uploads_root ) ) {
65 return new WP_Error(
66 'wp_mail_smtp_upload_dir_not_writable',
67 sprintf(
68 /* translators: %s: Directory path. */
69 __( 'Unable to write in WPMailSMTP upload directory %s. Is it writable by the server?', 'wp-mail-smtp' ),
70 esc_html( $upload_root )
71 )
72 );
73 }
74
75 return [
76 'path' => $upload_root,
77 'url' => trailingslashit( $upload_dir['baseurl'] ) . $dir,
78 ];
79 }
80
81
82 /**
83 * Create .htaccess file in the WPMailSMTP upload directory.
84 *
85 * @since 2.8.0
86 *
87 * @return bool True when the .htaccess file exists, false on failure.
88 */
89 public static function create_upload_dir_htaccess_file() {
90
91 /**
92 * Filters create upload dir htaccess file.
93 *
94 * @since 2.8.0
95 *
96 * @param bool $is_create Creates upload dir htaccess file.
97 */
98 if ( ! apply_filters( 'wp_mail_smtp_uploads_create_upload_dir_htaccess_file', true ) ) {
99 return false;
100 }
101
102 $upload_dir = self::upload_dir();
103
104 if ( is_wp_error( $upload_dir ) ) {
105 return false;
106 }
107
108 $htaccess_file = wp_normalize_path( trailingslashit( $upload_dir['path'] ) . '.htaccess' );
109 $cache_key = 'wp_mail_smtp_upload_dir_htaccess_file';
110
111 if ( is_file( $htaccess_file ) ) {
112 $cached_stat = get_transient( $cache_key );
113 $stat = array_intersect_key(
114 stat( $htaccess_file ),
115 [
116 'size' => 0,
117 'mtime' => 0,
118 'ctime' => 0,
119 ]
120 );
121
122 if ( $cached_stat === $stat ) {
123 return true;
124 }
125
126 @unlink( $htaccess_file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
127 }
128
129 if ( ! function_exists( 'insert_with_markers' ) ) {
130 require_once ABSPATH . 'wp-admin/includes/misc.php';
131 }
132
133 /**
134 * Filters upload dir htaccess file content.
135 *
136 * @since 2.8.0
137 *
138 * @param bool $content Upload dir htaccess file content.
139 */
140 $contents = apply_filters(
141 'wp_mail_smtp_uploads_create_upload_dir_htaccess_file_content',
142 '# Disable PHP and Python scripts parsing.
143 <Files *>
144 SetHandler none
145 SetHandler default-handler
146 RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
147 RemoveType .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
148 </Files>
149 <IfModule mod_php5.c>
150 php_flag engine off
151 </IfModule>
152 <IfModule mod_php7.c>
153 php_flag engine off
154 </IfModule>
155 <IfModule mod_php8.c>
156 php_flag engine off
157 </IfModule>
158 <IfModule headers_module>
159 Header set X-Robots-Tag "noindex"
160 </IfModule>'
161 );
162
163 $created = insert_with_markers( $htaccess_file, 'WPMailSMTP', $contents );
164
165 if ( $created ) {
166 clearstatcache( true, $htaccess_file );
167 $stat = array_intersect_key(
168 stat( $htaccess_file ),
169 [
170 'size' => 0,
171 'mtime' => 0,
172 'ctime' => 0,
173 ]
174 );
175
176 set_transient( $cache_key, $stat );
177 }
178
179 return $created;
180 }
181
182 /**
183 * Create index.html file in the specified directory if it doesn't exist.
184 *
185 * @since 2.8.0
186 *
187 * @param string $path Path to the directory.
188 *
189 * @return int|false Number of bytes that were written to the file, or false on failure.
190 */
191 public static function create_index_html_file( $path ) {
192
193 if ( ! is_dir( $path ) || is_link( $path ) ) {
194 return false;
195 }
196
197 $index_file = wp_normalize_path( trailingslashit( $path ) . 'index.html' );
198
199 // Do nothing if index.html exists in the directory.
200 if ( file_exists( $index_file ) ) {
201 return false;
202 }
203
204 // Create empty index.html.
205 return file_put_contents( $index_file, '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions
206 }
207
208 /**
209 * Delete the WPMailSMTP uploads directory.
210 *
211 * @since 4.1.1
212 *
213 * @return void
214 */
215 public static function delete_upload_dir() {
216
217 // Get the upload dir.
218 $upload_dir = self::upload_dir();
219
220 // If there is an error, return.
221 if ( is_wp_error( $upload_dir ) ) {
222 return;
223 }
224
225 $upload_root = $upload_dir['path'];
226
227 // Get WP Filesystembase files.
228 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
229 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
230
231 // Initialize WP_Filesystem_Direct.
232 $wp_filesystem = new WP_Filesystem_Direct( false );
233
234 // Delete the directory.
235 $wp_filesystem->delete( $upload_root, true );
236 }
237 }
238