| 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 |
* Render a PHP template file and return its output. |
| 117 |
* |
| 118 |
* Used for the block-markup page-content templates: they carry a |
| 119 |
* `defined( 'ABSPATH' ) || exit;` direct-access guard (required by Plugin |
| 120 |
* Check), so they must be executed via include — not read raw — otherwise |
| 121 |
* the guard line would leak into the generated page content. |
| 122 |
* |
| 123 |
* @param string $file Absolute path to the template file. |
| 124 |
* @return string Rendered markup, or '' if the file is missing. |
| 125 |
*/ |
| 126 |
public static function render_template( string $file ): string { |
| 127 |
if ( ! is_readable( $file ) ) { |
| 128 |
return ''; |
| 129 |
} |
| 130 |
ob_start(); |
| 131 |
include $file; |
| 132 |
return (string) ob_get_clean(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
// End of file filesystem.php. |
| 137 |
|