| 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 timezone(): DateTimeZone { |
| 79 |
return wp_timezone(); |
| 80 |
} |
| 81 |
|
| 82 |
public function adminUrl( string $path = '', string $scheme = 'admin' ): ?string { |
| 83 |
return admin_url( $path, $scheme ); |
| 84 |
} |
| 85 |
|
| 86 |
public function doingAjax(): bool { |
| 87 |
return wp_doing_ajax(); |
| 88 |
} |
| 89 |
|
| 90 |
public function pluginBasename( string $file ): string { |
| 91 |
return plugin_basename( $file ); |
| 92 |
} |
| 93 |
|
| 94 |
public function isAdmin(): bool { |
| 95 |
return is_admin(); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Phpdoc is not reliable. |
| 100 |
* |
| 101 |
* @return string|false |
| 102 |
*/ |
| 103 |
public function createNonce( string $action ) { |
| 104 |
return wp_create_nonce( $action ); |
| 105 |
} |
| 106 |
|
| 107 |
public function sendJson( array $settings ): void { |
| 108 |
wp_send_json( $settings ); |
| 109 |
} |
| 110 |
|
| 111 |
public function isUserLoggedIn(): bool { |
| 112 |
return is_user_logged_in(); |
| 113 |
} |
| 114 |
|
| 115 |
public function getSessionToken(): string { |
| 116 |
return wp_get_session_token(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @param mixed $data |
| 121 |
* |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
public function jsonEncode( $data ): string { |
| 125 |
$encodedData = wp_json_encode( $data ); |
| 126 |
|
| 127 |
return is_string( $encodedData ) ? $encodedData : ''; |
| 128 |
} |
| 129 |
|
| 130 |
public function isMultisite(): bool { |
| 131 |
return is_multisite(); |
| 132 |
} |
| 133 |
|
| 134 |
public function switchToBlog( int $site ): bool { |
| 135 |
return switch_to_blog( $site ); |
| 136 |
} |
| 137 |
|
| 138 |
public function restoreCurrentBlog(): bool { |
| 139 |
return restore_current_blog(); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @return array<string, array<string, string|bool>> |
| 144 |
*/ |
| 145 |
public function getMuPlugins(): array { |
| 146 |
return get_mu_plugins(); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* @return false|mixed |
| 151 |
*/ |
| 152 |
public function getSiteOption( string $option ) { |
| 153 |
return get_site_option( $option ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Returns non-negative int. |
| 158 |
* |
| 159 |
* @param mixed $maybeInt |
| 160 |
* |
| 161 |
* @return int |
| 162 |
*/ |
| 163 |
public function absint( $maybeInt ): int { |
| 164 |
return absint( $maybeInt ); |
| 165 |
} |
| 166 |
|
| 167 |
public function getAdminUrl( ?int $blogId = null, string $path = '', string $scheme = 'admin' ): string { |
| 168 |
return get_admin_url( $blogId, $path, $scheme ); |
| 169 |
} |
| 170 |
|
| 171 |
public function addSubmenuPage( |
| 172 |
string $parentSlug, |
| 173 |
string $pageTitle, |
| 174 |
string $menuTitle, |
| 175 |
string $capability, |
| 176 |
string $menuSlug, |
| 177 |
callable $callback, |
| 178 |
?int $position = null |
| 179 |
): void { |
| 180 |
add_submenu_page( |
| 181 |
$parentSlug, |
| 182 |
$pageTitle, |
| 183 |
$menuTitle, |
| 184 |
$capability, |
| 185 |
$menuSlug, |
| 186 |
$callback, |
| 187 |
$position |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
public function addMenuPage( |
| 192 |
string $pageTitle, |
| 193 |
string $menuTitle, |
| 194 |
string $capability, |
| 195 |
string $menuSlug, |
| 196 |
callable $callback, |
| 197 |
string $iconUrl |
| 198 |
): void { |
| 199 |
add_menu_page( |
| 200 |
$pageTitle, |
| 201 |
$menuTitle, |
| 202 |
$capability, |
| 203 |
$menuSlug, |
| 204 |
$callback, |
| 205 |
$iconUrl |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
public function addShortcode( string $tag, callable $callback ): void { |
| 210 |
add_shortcode( $tag, $callback ); |
| 211 |
} |
| 212 |
|
| 213 |
public function doShortcode( string $content ): string { |
| 214 |
return do_shortcode( $content ); |
| 215 |
} |
| 216 |
|
| 217 |
public function sanitizeEmail( string $email ): string { |
| 218 |
return sanitize_email( $email ); |
| 219 |
} |
| 220 |
|
| 221 |
public function wpKsesPost( string $content ): string { |
| 222 |
return wp_kses_post( $content ); |
| 223 |
} |
| 224 |
|
| 225 |
public function wpStripAllTags( string $content ): string { |
| 226 |
return wp_strip_all_tags( $content ); |
| 227 |
} |
| 228 |
|
| 229 |
public function enqueueEditor(): void { |
| 230 |
wp_enqueue_editor(); |
| 231 |
} |
| 232 |
|
| 233 |
public function getBlogInfo( string $show, string $filter ): string { |
| 234 |
return get_bloginfo( $show, $filter ); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* @param string|string[] $to |
| 239 |
* @param string $subject |
| 240 |
* @param string $message |
| 241 |
* @param string[] $headers |
| 242 |
* @param string[] $attachments |
| 243 |
*/ |
| 244 |
public function wpMail( $to, string $subject, string $message, array $headers, array $attachments ): bool { |
| 245 |
return wp_mail( $to, $subject, $message, $headers, $attachments ); |
| 246 |
} |
| 247 |
|
| 248 |
public function currentTime( string $type, bool $gmt ): string { |
| 249 |
return (string) current_time( $type, $gmt ); |
| 250 |
} |
| 251 |
} |
| 252 |
|