Skip to content

Commit 93f6c92

Browse files
committed
feat(Functions): Added wpfs function
1 parent 8733400 commit 93f6c92

File tree

3 files changed

+75
-19
lines changed

3 files changed

+75
-19
lines changed

Hook_Remover.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace XWP\Helper\Functions;
44

55
class Hook_Remover {
6-
private static function get_classname( string|object|false|null $target = '' ): string|false {
6+
private static function get_classname( string|object|bool|null $target = '' ): string|bool {
77
$classname = match ( true ) {
88
\is_null( $target ),
99
false === $target => '',
@@ -15,7 +15,7 @@ private static function get_classname( string|object|false|null $target = '' ):
1515
return \class_exists( $classname ) ? $classname : false;
1616
}
1717

18-
private static function callback_matches( callable|array $callback, string $classname, string|false $method = false ): bool {
18+
private static function callback_matches( callable|array $callback, string $classname, string|bool $method = false ): bool {
1919
if ( ! \is_array( $callback['function'] ) ) {
2020
return false;
2121
}
@@ -27,7 +27,7 @@ private static function callback_matches( callable|array $callback, string $clas
2727
return ! $method || ! ( $method !== $callback['function'][1] ?? false );
2828
}
2929

30-
private static function get_callbacks( string $hook_name, int|false $priority = false ): array {
30+
private static function get_callbacks( string $hook_name, int|bool $priority = false ): array {
3131
return $priority
3232
? $GLOBALS['wp_filter'][ $hook_name ][ $priority ] ?? array()
3333
: $GLOBALS['wp_filter'][ $hook_name ]->callbacks ?? array();
@@ -57,9 +57,9 @@ final public static function get_callback_id( string $classname, string $method,
5757

5858
final public static function remove_callbacks(
5959
string $classname,
60-
string|false $target_hook = false,
61-
string|false $method = false,
62-
int|false $priority = false,
60+
string|bool $target_hook = false,
61+
string|bool $method = false,
62+
int|bool $priority = false,
6363
): array {
6464
$removed = array();
6565

WPFS.php

+33-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,44 @@
33
namespace XWP\Helper\Functions;
44

55
class WPFS {
6-
final public static function load( array|false $args = false, string|false $ctx = false, bool $ownr = false ): \WP_Filesystem_Base|false|null {
7-
require_once ABSPATH . 'wp-admin/includes/file.php';
6+
/**
7+
* Whether the class has been hooked.
8+
*
9+
* @var bool|null
10+
*/
11+
private static ?bool $hooked = null;
12+
13+
final public static function load(
14+
array|bool $args = false,
15+
string|bool $ctx = false,
16+
bool $ownr = false,
17+
): \WP_Filesystem_Base|bool|null {
18+
self::$hooked ??= self::hook();
19+
20+
if ( isset( $GLOBALS['wp_filesystem'] ) ) {
21+
return $GLOBALS['wp_filesystem'];
22+
}
823

924
return match ( \WP_Filesystem( $args, $ctx, $ownr ) ) {
1025
true => $GLOBALS['wp_filesystem'],
1126
false => false,
1227
default => null
1328
};
1429
}
30+
31+
private static function hook(): void {
32+
require_once ABSPATH . 'wp-admin/includes/file.php';
33+
\add_filter( 'filesystem_method', array( self::class, 'fs_method' ), 99, 2 );
34+
}
35+
36+
public static function fs_method( string $method, array|bool $args ) {
37+
if ( ! \is_array( $args ) || ! isset( $args['method'] ) ) {
38+
return $method;
39+
}
40+
41+
$base = \ucfirst( \str_replace( 'WP_Filesystem_', '', $args['method'] ) );
42+
$cname = 'WP_Filesystem_' . $base;
43+
44+
return \class_exists( $cname ) ? $base : $method;
45+
}
1546
}

xwp-helper-fns.php

+36-11
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,47 @@
88

99
use XWP\Helper\Functions as f;
1010

11+
if ( ! function_exists( 'xwp_wpfs' ) ) :
12+
/**
13+
* Loads the WordPress filesystem
14+
*
15+
* @template TFS of \WP_Filesystem_Base
16+
*
17+
* @param class-string<TFS> $method Optional. Filesystem method classname. Default null.
18+
* @param array|false $args Optional. Connection args, These are passed directly to the WP_Filesystem_*() classes. Default false.
19+
* @param string|false $context Optional. Context for get_filesystem_method(). Default false.
20+
* @return TFS|false|null
21+
*/
22+
function xwp_wpfs(
23+
string $method = null,
24+
array|bool $args = false,
25+
string|bool $context = false,
26+
): WP_Filesystem_Base|bool|null {
27+
//phpcs:ignore Universal.Operators.DisallowShortTernary.Found
28+
$args = array_filter( $args ?: array( 'method' => $method ) );
29+
30+
return f\WPFS::load( $args, $context );
31+
}
32+
endif;
33+
1134
if ( ! function_exists( 'wp_load_filesystem' ) ) :
1235
/**
1336
* Loads the WordPress filesystem
1437
*
15-
* @param array|false $args Optional. Connection args, These are passed directly to the WP_Filesystem_*() classes. Default false.
16-
* @param string|false $context Optional. Context for get_filesystem_method(). Default false.
17-
* @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. Default false.
38+
* @template TFS of \WP_Filesystem_Base
39+
*
40+
* @param array{method?: class-string<TFS>}|array<string,mixed>|false $args Optional. Connection args, These are passed directly to the WP_Filesystem_*() classes. Default false.
41+
* @param string|false $context Optional. Context for get_filesystem_method(). Default false.
1842
*
1943
* @return \WP_Filesystem_Base|false|null
44+
*
45+
* @deprecated 1.10.0 Use xwp_wpfs instead.
2046
*/
2147
function wp_load_filesystem(
22-
array|false $args = false,
23-
string|false $context = false,
24-
bool $allow_relaxed_file_ownership = false,
25-
): \WP_Filesystem_Base|false|null {
26-
return f\WPFS::load( $args, $context, $allow_relaxed_file_ownership );
48+
array|bool $args = false,
49+
string|bool $context = false,
50+
): WP_Filesystem_Base|bool|null {
51+
return xwp_wpfs( null, $args, $context );
2752
}
2853
endif;
2954

@@ -147,9 +172,9 @@ function xwp_deregister_blocks(): array {
147172
*/
148173
function xwp_remove_hook_callbacks(
149174
string $classname,
150-
string|false $target_hook = false,
151-
string|false $method = false,
152-
int|false $priority = false,
175+
string|bool $target_hook = false,
176+
string|bool $method = false,
177+
int|bool $priority = false,
153178
): array {
154179
return f\Hook_Remover::remove_callbacks( $classname, $target_hook, $method, $priority );
155180
}

0 commit comments

Comments
 (0)