| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class WpAdapter. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Framework; |
| 11 |
|
| 12 |
use DateTimeZone; |
| 13 |
use WP_Error; |
| 14 |
use WP_Screen; |
| 15 |
use WP_Term; |
| 16 |
|
| 17 |
use function is_string; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class WpAdapter. |
| 21 |
* |
| 22 |
* @package Packetery |
| 23 |
*/ |
| 24 |
class WpAdapter { |
| 25 |
use AssetTrait; |
| 26 |
use EscapingTrait; |
| 27 |
use HookTrait; |
| 28 |
use HttpTrait; |
| 29 |
use OptionTrait; |
| 30 |
use TransientTrait; |
| 31 |
use TranslationTrait; |
| 32 |
use PostTrait; |
| 33 |
|
| 34 |
/** |
| 35 |
* Retrieves a modified URL query string. |
| 36 |
* |
| 37 |
* @param mixed ...$args Arguments. |
| 38 |
* |
| 39 |
* @return string New URL query string (unescaped). |
| 40 |
*/ |
| 41 |
public function addQueryArg( ...$args ): string { |
| 42 |
return add_query_arg( ...$args ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Gets WP term. |
| 47 |
* |
| 48 |
* @param int $termId Term id. |
| 49 |
* |
| 50 |
* @return WP_Term|WP_Error|null |
| 51 |
*/ |
| 52 |
public function getTerm( int $termId ) { |
| 53 |
return get_term( $termId ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Checks whether the given variable is an instance of the `WP_Error` class. |
| 58 |
* |
| 59 |
* @param mixed $thing Variable to check. |
| 60 |
* |
| 61 |
* @return bool |
| 62 |
*/ |
| 63 |
public function isWpError( $thing ): bool { |
| 64 |
return is_wp_error( $thing ); |
| 65 |
} |
| 66 |
|
| 67 |
public function getCurrentScree(): ?WP_Screen { |
| 68 |
return get_current_screen(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return false|string |
| 73 |
*/ |
| 74 |
public function date( string $format, ?int $timestamp = null, ?DateTimeZone $timezone = null ) { |
| 75 |
return wp_date( $format, $timestamp, $timezone ); |
| 76 |
} |
| 77 |
|
| 78 |
public function adminUrl( string $path = '', string $scheme = 'admin' ): ?string { |
| 79 |
return admin_url( $path, $scheme ); |
| 80 |
} |
| 81 |
|
| 82 |
public function doingAjax(): bool { |
| 83 |
return wp_doing_ajax(); |
| 84 |
} |
| 85 |
|
| 86 |
public function pluginBasename( string $file ): string { |
| 87 |
return plugin_basename( $file ); |
| 88 |
} |
| 89 |
|
| 90 |
public function isAdmin(): bool { |
| 91 |
return is_admin(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Phpdoc is not reliable. |
| 96 |
* |
| 97 |
* @return string|false |
| 98 |
*/ |
| 99 |
public function createNonce( string $action ) { |
| 100 |
return wp_create_nonce( $action ); |
| 101 |
} |
| 102 |
|
| 103 |
public function sendJson( array $settings ): void { |
| 104 |
wp_send_json( $settings ); |
| 105 |
} |
| 106 |
|
| 107 |
public function isUserLoggedIn(): bool { |
| 108 |
return is_user_logged_in(); |
| 109 |
} |
| 110 |
|
| 111 |
public function getSessionToken(): string { |
| 112 |
return wp_get_session_token(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @param mixed $data |
| 117 |
* |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
public function jsonEncode( $data ): string { |
| 121 |
$encodedData = wp_json_encode( $data ); |
| 122 |
|
| 123 |
return is_string( $encodedData ) ? $encodedData : ''; |
| 124 |
} |
| 125 |
|
| 126 |
public function isMultisite(): bool { |
| 127 |
return is_multisite(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @return array<string, array<string, string|bool>> |
| 132 |
*/ |
| 133 |
public function getMuPlugins(): array { |
| 134 |
return get_mu_plugins(); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @return false|mixed |
| 139 |
*/ |
| 140 |
public function getSiteOption( string $option ) { |
| 141 |
return get_site_option( $option ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns non-negative int. |
| 146 |
* |
| 147 |
* @param mixed $maybeInt |
| 148 |
* |
| 149 |
* @return int |
| 150 |
*/ |
| 151 |
public function absint( $maybeInt ): int { |
| 152 |
return absint( $maybeInt ); |
| 153 |
} |
| 154 |
|
| 155 |
public function getAdminUrl( ?int $blogId = null, string $path = '', string $scheme = 'admin' ): string { |
| 156 |
return get_admin_url( $blogId, $path, $scheme ); |
| 157 |
} |
| 158 |
} |
| 159 |
|