restRequestDispatched = true; //Nested requests, like the ones for embedded objects, must not lower the context again. $this->editingRestRequestDetected = $this->editingRestRequestDetected || $isEditingRequest; } private function isReadingRestMethod(string $method): bool { return in_array(strtoupper($method), self::REST_READING_METHODS, true); } private function isEditingRestRequest(): bool { if ($this->isRestRequest() === false) { return false; } if ($this->restRequestDispatched === true) { return $this->editingRestRequestDetected; } //Undispatched requests only expose the raw data, so an editing request is assumed. return $this->isReadingRestMethod((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')) === false || ($_REQUEST['context'] ?? '') === 'edit'; } /** * @see is_admin */ public function isAdmin(): bool { if ($this->isRestRequest() === true) { return $this->isEditingRestRequest(); } if (wp_doing_ajax() === true) { return is_user_logged_in() === true && str_starts_with((string)($_SERVER['HTTP_REFERER'] ?? ''), get_admin_url()); } return is_admin(); } /** * @see wp_create_nonce */ public function createNonce(int|string $action): string { return wp_create_nonce($action); } /** * @see wp_nonce_field */ public function getNonceField( int|string $action = -1, string $name = '_wpnonce', bool $referrer = true, bool $echo = true ): string { return wp_nonce_field($action, $name, $referrer, $echo); } /** * @see wp_verify_nonce */ public function verifyNonce(?string $nonce, int|string $action = -1): bool|int { return wp_verify_nonce($nonce, $action); } public function getRoles(): WP_Roles { global $wp_roles; return $wp_roles; } /** * @see add_menu_page */ public function addMenuPage( string $pageTitle, string $menuTitle, string $capability, string $menuSlug, mixed $function = '', string $iconUrl = '', int|float|null $position = null ): string { return add_menu_page($pageTitle, $menuTitle, $capability, $menuSlug, $function, $iconUrl, $position); } /** * @see add_submenu_page */ public function addSubMenuPage( string $parentSlug, string $pageTitle, string $menuTitle, string $capability, string $menuSlug, callable|string $function = '' ): bool|string { return add_submenu_page($parentSlug, $pageTitle, $menuTitle, $capability, $menuSlug, $function); } /** * @see add_meta_box */ public function addMetaBox( string $id, string $title, callable $callback, $screen = null, string $context = 'advanced', string $priority = 'default', $callbackArguments = null ): void { add_meta_box($id, $title, $callback, $screen, $context, $priority, $callbackArguments); } /** * @see get_pages */ public function getPages(array|string|int $arguments): bool|array { return get_pages($arguments); } /** * @see wp_register_style */ public function registerStyle( string $handle, string $source, array $depends = [], bool|string|null $version = false, string $media = 'all' ): bool { return wp_register_style($handle, $source, $depends, $version, $media); } /** * @see wp_register_script */ public function registerScript( string $handle, string $source, array $depends = [], bool|string|null $version = false, bool $inFooter = false ): bool { return wp_register_script($handle, $source, $depends, $version, $inFooter); } /** * @see wp_enqueue_style */ public function enqueueStyle( string $handle, string $source = '', array $depends = [], bool|string|null $version = false, string $media = 'all' ): void { wp_enqueue_style($handle, $source, $depends, $version, $media); } /** * @see wp_enqueue_script */ public function enqueueScript( string $handle, string $source = '', array $depends = [], bool|string|null $version = false, bool $inFooter = false ): void { wp_enqueue_script($handle, $source, $depends, $version, $inFooter); } public function getMetaBoxes(): array { global $wp_meta_boxes; return $wp_meta_boxes; } public function setMetaBoxes(array $wpMetaBoxes): void { global $wp_meta_boxes; $wp_meta_boxes = $wpMetaBoxes; } /** * @see get_sites */ public function getSites(array $arguments = []): array { if (function_exists('\get_sites') === true && class_exists('\WP_Site_Query') === true) { return get_sites($arguments); } return []; } /** * @see apply_filters() */ public function applyFilters(string $tag, mixed $value): mixed { return call_user_func_array('\apply_filters', func_get_args()); } /** * @see get_bloginfo */ public function getBlogInfo(string $show = '', string $filter = 'raw'): string { return get_bloginfo($show, $filter); } /** * @see esc_html */ public function escHtml(string $text): string { return esc_html($text); } /** * @see is_single */ public function isSingle(array|string|int $post = ''): bool { return is_single($post); } /** * @see is_page */ public function isPage(array|string|int $page = ''): bool { return is_page($page); } /** * @see \WP_PLUGIN_DIR */ public function getPluginDir(): string { return WP_PLUGIN_DIR; } /** * @see plugins_url */ public function pluginsUrl(string $path = '', string $plugin = ''): string { return plugins_url($path, $plugin); } /** * @see plugin_basename */ public function pluginBasename(string $file): string { return plugin_basename($file); } /** * @see wp_attachment_is_image */ public function attachmentIsImage(int|WP_Post $post): bool { return wp_attachment_is_image($post); } /** * @see get_attached_file */ public function getAttachedFile(int $attachmentId): string|false { return get_attached_file($attachmentId); } /** * The return value passes the wp_get_attachment_metadata filter, so any type can reach us. * * @see wp_get_attachment_metadata */ public function getAttachmentMetadata(int $attachmentId): mixed { return wp_get_attachment_metadata($attachmentId); } /** * @see current_user_can */ public function currentUserCan(string $capability): bool { return current_user_can($capability); } /** * @see get_users */ public function getUsers(array $arguments = []): array { return get_users($arguments); } /** * @return WP_Hook[] */ public function getFilters(): array { global $wp_filter; return $wp_filter; } public function setFilters(array $filters): void { global $wp_filter; $wp_filter = $filters; } /** * @see current_time */ public function currentTime(string $type, int $gmt = 0): int|string { return current_time($type, $gmt); } public function formatDate(string $date): string { $dateFormat = __('M j, Y @ H:i'); return date_i18n($dateFormat, strtotime($date)); } /** * @see register_widget */ public function registerWidget(mixed $widgetClass): void { register_widget($widgetClass); } /** * @see wp_login_url */ public function wpLoginUrl(string $redirect = '', bool $forceReauth = false): string { return wp_login_url($redirect, $forceReauth); } /** * @see wp_logout_url */ public function wpLogoutUrl(string $redirect = ''): string { return wp_logout_url($redirect); } /** * @see wp_registration_url */ public function wpRegistrationUrl(): string { return wp_registration_url(); } /** * @see wp_lostpassword_url */ public function wpLostPasswordUrl(string $redirect = ''): string { return wp_lostpassword_url($redirect); } /** * @see add_shortcode */ public function addShortCode(string $tag, callable $function): void { add_shortcode($tag, $function); } /** * @see do_shortcode */ public function doShortCode(string $content, bool $ignoreHtml = false): string { return do_shortcode($content, $ignoreHtml); } /** * @see attachment_url_to_postid */ public function attachmentUrlToPostId(string $url): int { return attachment_url_to_postid($url); } /** * @see got_mod_rewrite */ public function gotModRewrite(): bool { if (function_exists('\got_mod_rewirte') === false) { require_once(ABSPATH . 'wp-admin/includes/misc.php'); } return got_mod_rewrite(); } /** * @see is_user_member_of_blog() */ public function isUserMemberOfBlog(): bool { return is_user_member_of_blog(); } /** * @see get_queried_object_id */ public function getQueriedObjectId(): int { return get_queried_object_id(); } public function getCurrentPost(): array|WP_Post|null { global $post; return $post; } }