WC_Admin_Notices::is_uploads_directory_protected
Check if uploads directory is protected.
Method of the class: WC_Admin_Notices{}
No Hooks.
Returns
true|false.
Usage
$result = WC_Admin_Notices::is_uploads_directory_protected();
Changelog
| Since 4.2.0 | Introduced. |
WC_Admin_Notices::is_uploads_directory_protected() WC Admin Notices::is uploads directory protected code WC 10.3.6
protected static function is_uploads_directory_protected() {
$cache_key = '_woocommerce_upload_directory_status';
$status = get_transient( $cache_key );
// Check for cache.
if ( false !== $status ) {
return 'protected' === $status;
}
// Get only data from the uploads directory.
$uploads = wp_get_upload_dir();
// Check for the "uploads/woocommerce_uploads" directory.
$response = wp_safe_remote_get(
esc_url_raw( $uploads['baseurl'] . '/woocommerce_uploads/' ),
array(
'redirection' => 0,
)
);
$response_code = intval( wp_remote_retrieve_response_code( $response ) );
$response_content = wp_remote_retrieve_body( $response );
// Check if returns 200 with empty content in case can open an index.html file,
// and check for non-200 codes in case the directory is protected.
$is_protected = ( 200 === $response_code && empty( $response_content ) ) || ( 200 !== $response_code );
set_transient( $cache_key, $is_protected ? 'protected' : 'unprotected', 1 * DAY_IN_SECONDS );
return $is_protected;
}