PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.0.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.0.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / utils / fs.php

fs.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.0.0, at includes/utils/fs.php

117 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Utils;
4
5 use WP_Error;
6 use WP_Filesystem_Base;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 class Fs {
13 protected static $fs_loaded = null;
14
15 public static function load_fs() {
16 if ( null !== self::$fs_loaded ) {
17 return self::$fs_loaded;
18 }
19
20 // Allow us to easily interact with the filesystem.
21 if ( ! function_exists( 'WP_Filesystem' ) ) {
22 require_once ABSPATH . 'wp-admin/includes/file.php';
23 }
24
25 ob_start();
26 $credentials = request_filesystem_credentials( self_admin_url() );
27 ob_end_clean();
28
29 if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
30 global $wp_filesystem;
31 $errorCode = 'fs_unavailable';
32 $errorMessage = esc_html__( 'Unable to connect to the filesystem. Please confirm your credentials.', 'storeengine' );
33
34 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { // @phpstan-ignore-line
35 $errorMessage = esc_html( $wp_filesystem->errors->get_error_message() );
36 }
37
38 self::$fs_loaded = new WP_Error( $errorCode, $errorMessage, [ 'status' => 500 ] );
39 } else {
40 self::$fs_loaded = true;
41 }
42
43 return self::$fs_loaded;
44 }
45
46 /**
47 * Initializes and connects the WordPress Filesystem.
48 *
49 * @return WP_Error|WP_Filesystem_Base
50 */
51 public static function load() {
52 $loaded = self::load_fs();
53
54 if ( is_wp_error( $loaded ) ) {
55 return $loaded;
56 }
57
58 global $wp_filesystem;
59
60 if ( ! is_object( $wp_filesystem ) ) {
61 return new WP_Error( 'fs_unavailable', esc_html__( 'Could not access filesystem.', 'storeengine' ), [ 'status' => 500 ] );
62 }
63
64 if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
65 return new WP_Error( 'fs_error', esc_html__( 'Filesystem error.', 'storeengine' ), $wp_filesystem->errors );
66 }
67
68 return $wp_filesystem;
69 }
70
71 /**
72 * Downloads a URL to a local temporary file using the WordPress HTTP API.
73 * Please note that the calling function must delete or move the file.
74 *
75 *
76 * @param string $url The URL of the file to download.
77 * @param int $timeout The timeout for the request to download the file.
78 * Default 300 seconds.
79 *
80 * @return string|WP_Error Temp filename (path) on success, WP_Error on failure.
81 */
82 public static function download_url( string $url, int $timeout = 300 ) {
83 if ( ! function_exists( 'download_url' ) ) {
84 require_once ABSPATH . 'wp-admin/includes/file.php';
85 }
86
87 return download_url( esc_url_raw( $url ), $timeout );
88 }
89
90 /**
91 * Reads entire file into a string.
92 *
93 * @param string $file Name of the file to read.
94 *
95 * @return string|false Read data on success, false on failure.
96 * @since 1.6.4
97 */
98 public static function get_contents( string $file ) {
99 $fs = self::load();
100
101 // load() is documented to return WP_Error on failure (line 49 above).
102 // Honour the get_contents() contract — return false on FS unavailable
103 // — instead of dispatching a method on a WP_Error and fataling.
104 if ( $fs instanceof WP_Error ) {
105 return false;
106 }
107
108 if ( ! $fs->exists( $file ) || ! $fs->is_readable( $file ) ) {
109 return false;
110 }
111
112 return $fs->get_contents( $file );
113 }
114 }
115
116 // End of file filesystem.php.
117