PluginProbe
User Access Manager / 2.3.15
User Access Manager v2.3.15
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Wrapper / Wordpress.php

Wordpress.php in User Access Manager 2.3.15, at src/Wrapper/Wordpress.php

952 lines 21.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Wrapper;
6
7 use JetBrains\PhpStorm\NoReturn;
8 use WP_Error;
9 use WP_Hook;
10 use WP_Post;
11 use WP_Post_Type;
12 use WP_Query;
13 use WP_Roles;
14 use WP_Taxonomy;
15 use WP_Term;
16 use WP_User;
17 use wpdb;
18 use function add_action;
19 use function add_filter;
20 use function add_menu_page;
21 use function add_meta_box;
22 use function add_option;
23 use function add_query_arg;
24 use function add_shortcode;
25 use function add_submenu_page;
26 use function attachment_url_to_postid;
27 use function current_time;
28 use function current_user_can;
29 use function date_i18n;
30 use function dbDelta;
31 use function delete_option;
32 use function do_action;
33 use function do_shortcode;
34 use function esc_html;
35 use function function_exists;
36 use function get_allowed_mime_types;
37 use function get_bloginfo;
38 use function get_home_path;
39 use function get_option;
40 use function get_page_by_path;
41 use function get_page_link;
42 use function get_pages;
43 use function get_post;
44 use function get_post_type_object;
45 use function get_post_types;
46 use function get_queried_object_id;
47 use function get_sites;
48 use function get_taxonomies;
49 use function get_taxonomy;
50 use function get_term;
51 use function get_userdata;
52 use function get_users;
53 use function got_mod_rewrite;
54 use function has_filter;
55 use function home_url;
56 use function is_admin;
57 use function is_feed;
58 use function is_multisite;
59 use function is_page;
60 use function is_post_type_hierarchical;
61 use function is_single;
62 use function is_super_admin;
63 use function is_taxonomy_hierarchical;
64 use function is_user_logged_in;
65 use function plugin_basename;
66 use function plugins_url;
67 use function register_widget;
68 use function remove_action;
69 use function remove_filter;
70 use function site_url;
71 use function switch_to_blog;
72 use function update_option;
73 use function wp_attachment_is_image;
74 use function wp_create_nonce;
75 use function wp_die;
76 use function wp_doing_ajax;
77 use function wp_enqueue_script;
78 use function wp_enqueue_style;
79 use function wp_get_current_user;
80 use function wp_login_url;
81 use function wp_logout_url;
82 use function wp_lostpassword_url;
83 use function wp_nonce_field;
84 use function wp_parse_id_list;
85 use function wp_redirect;
86 use function wp_register_script;
87 use function wp_register_style;
88 use function wp_registration_url;
89 use function wp_upload_dir;
90 use function wp_verify_nonce;
91
92 class Wordpress
93 {
94 public const REST_READING_METHODS = ['GET', 'HEAD'];
95
96 private bool $restRequestDispatched = false;
97 private bool $editingRestRequestDetected = false;
98
99 public function getDatabase(): wpdb
100 {
101 global $wpdb;
102 return $wpdb;
103 }
104
105 public function isNginx(): bool
106 {
107 global $is_nginx;
108 return $is_nginx;
109 }
110
111 /**
112 * @see wp_using_ext_object_cache
113 */
114 public function isUsingExtObjectCache(): bool
115 {
116 return function_exists('wp_using_ext_object_cache') && wp_using_ext_object_cache() === true;
117 }
118
119 /**
120 * @see apache_get_modules
121 */
122 public function isApacheModuleLoaded(string $module): bool
123 {
124 return function_exists('apache_get_modules') && in_array($module, apache_get_modules(), true);
125 }
126
127 /**
128 * @see is_post_type_hierarchical
129 */
130 public function isPostTypeHierarchical(string $postType): bool
131 {
132 return is_post_type_hierarchical($postType);
133 }
134
135 /**
136 * @see is_taxonomy_hierarchical
137 */
138 public function isTaxonomyHierarchical(string $taxonomy): bool
139 {
140 return is_taxonomy_hierarchical($taxonomy);
141 }
142
143 /**
144 * @see get_userdata
145 */
146 public function getUserData(int|string|null $id): WP_User|bool
147 {
148 return get_userdata($id);
149 }
150
151 /**
152 * @see get_post_types
153 */
154 public function getPostTypes(
155 array|string|int $arguments = [],
156 string $output = 'names',
157 string $operator = 'and'
158 ): array {
159 return get_post_types($arguments, $output, $operator);
160 }
161
162 /**
163 * @return string[]|WP_Taxonomy[]
164 * @see get_taxonomies
165 */
166 public function getTaxonomies(array $arguments = [], string $output = 'names', string $operator = 'and'): array
167 {
168 return get_taxonomies($arguments, $output, $operator);
169 }
170
171 /**
172 * @see get_taxonomy
173 */
174 public function getTaxonomy(string $taxonomy): WP_Taxonomy|bool
175 {
176 return get_taxonomy($taxonomy);
177 }
178
179 /**
180 * @see get_post
181 */
182 public function getPost(
183 int|string|null $id,
184 string $output = OBJECT,
185 string $filter = 'raw'
186 ): WP_Post|array|bool|null {
187 return get_post($id, $output, $filter);
188 }
189
190 /**
191 * @see get_post_type_object
192 */
193 public function getPostTypeObject(int|string $postType): ?WP_Post_Type
194 {
195 return get_post_type_object($postType);
196 }
197
198 /**
199 * @see get_term
200 */
201 public function getTerm(
202 int|string $id,
203 string $taxonomy = '',
204 string $output = OBJECT,
205 string $filter = 'raw'
206 ): WP_Term|WP_Error|array|bool|null {
207 return get_term($id, $taxonomy, $output, $filter);
208 }
209
210
211 /**
212 * @see dbDelta
213 */
214 public function dbDelta(array|string $queries = '', bool $execute = true): array
215 {
216 if (function_exists('\dbDelta') === false) {
217 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
218 }
219
220 return dbDelta($queries, $execute);
221 }
222
223 /**
224 * @see switch_to_blog
225 */
226 public function switchToBlog(int|string|null $blogId): bool
227 {
228 if (function_exists('\switch_to_blog') === true) {
229 return switch_to_blog($blogId);
230 }
231
232 return true;
233 }
234
235 /**
236 * @see \restore_current_blog()
237 */
238 public function restoreCurrentBlog(): bool
239 {
240 if (function_exists('\restore_current_blog') === true) {
241 return restore_current_blog();
242 }
243
244 return true;
245 }
246
247 /**
248 * @see is_multisite
249 */
250 public function isMultiSite(): bool
251 {
252 return is_multisite();
253 }
254
255 /**
256 * @see do_action
257 */
258 public function doAction(string $tag, mixed $arguments = ''): void
259 {
260 do_action($tag, $arguments);
261 }
262
263 /**
264 * @see add_action
265 */
266 public function addAction(
267 string $tag,
268 callable $functionToAdd,
269 int $priority = 10,
270 int $acceptedArguments = 1
271 ): bool {
272 return add_action($tag, $functionToAdd, $priority, $acceptedArguments);
273 }
274
275 /**
276 * @see remove_action
277 */
278 public function removeAction(string $hookName, callable $callback, int $priority = 10): bool
279 {
280 return remove_action($hookName, $callback, $priority);
281 }
282
283 /**
284 * @see has_filter
285 */
286 public function hasFilter(string $tag, callable|bool $functionToCheck = false): bool|int
287 {
288 return has_filter($tag, $functionToCheck);
289 }
290
291 /**
292 * @see add_filter
293 */
294 public function addFilter(
295 string $tag,
296 callable $functionToAdd,
297 int $priority = 10,
298 int $acceptedArguments = 1
299 ): bool {
300 return add_filter($tag, $functionToAdd, $priority, $acceptedArguments);
301 }
302
303 /**
304 * @see WP_Error
305 */
306 public function getWpError(string|int $code = '', string $message = '', mixed $data = ''): WP_Error
307 {
308 return new WP_Error($code, $message, $data);
309 }
310
311 /**
312 * @see remove_filter
313 */
314 public function removeFilter(string $tag, callable $functionToRemove, int $priority = 10): bool
315 {
316 return remove_filter($tag, $functionToRemove, $priority);
317 }
318
319 /**
320 * @see add_option
321 */
322 public function addOption(
323 string $option,
324 mixed $value = '',
325 string $deprecated = '',
326 bool|string $autoload = 'yes'
327 ): bool {
328 return add_option($option, $value, $deprecated, $autoload);
329 }
330
331 /**
332 * @see delete_option
333 */
334 public function deleteOption(string $option): bool
335 {
336 return delete_option($option);
337 }
338
339 /**
340 * @see update_option
341 */
342 public function updateOption(string $option, mixed $value = '', bool|string $autoload = 'yes'): bool
343 {
344 return update_option($option, $value, $autoload);
345 }
346
347 /**
348 * @see get_option
349 */
350 public function getOption(string $option, mixed $default = false)
351 {
352 return get_option($option, $default);
353 }
354
355 /**
356 * @see wp_cache_set
357 */
358 public function wpCacheSet(string $key, mixed $data, string $group = '', int $expire = 0): bool
359 {
360 return wp_cache_set($key, $data, $group, $expire);
361 }
362
363 /**
364 * @see wp_cache_get
365 */
366 public function wpCacheGet(string $key, string $group = ''): mixed
367 {
368 return wp_cache_get($key, $group);
369 }
370
371 /**
372 * @see wp_cache_delete
373 */
374 public function wpCacheDelete(string $key, string $group = ''): bool
375 {
376 return wp_cache_delete($key, $group);
377 }
378
379 /**
380 * @see is_super_admin
381 */
382 public function isSuperAdmin(bool|int|string|null $userId = false): bool
383 {
384 return is_super_admin($userId);
385 }
386
387 /**
388 * @see wp_get_current_user
389 */
390 public function getCurrentUser(): WP_User
391 {
392 if (function_exists('\wp_get_current_user') === false) {
393 require_once(ABSPATH . 'wp-includes/pluggable.php');
394 }
395
396 return wp_get_current_user();
397 }
398
399 /**
400 * @see get_allowed_mime_types
401 */
402 public function getAllowedMimeTypes(WP_User|int|string|null $user = null): array
403 {
404 return get_allowed_mime_types($user);
405 }
406
407 /**
408 * @see wp_upload_dir
409 */
410 public function getUploadDir(?string $time = null, bool $createDir = true, bool $refreshCache = false): array
411 {
412 return wp_upload_dir($time, $createDir, $refreshCache);
413 }
414
415 /**
416 * @see home_url
417 */
418 public function getHomeUrl(string $path = '', ?string $scheme = null): string
419 {
420 return home_url($path, $scheme);
421 }
422
423 /**
424 * @see site_url
425 */
426 public function getSiteUrl(string $path = '', ?string $scheme = null): string
427 {
428 return site_url($path, $scheme);
429 }
430
431 /**
432 * @see add_query_arg
433 */
434 public function addQueryArg(array $arguments, string $url): string
435 {
436 return add_query_arg($arguments, $url);
437 }
438
439 /**
440 * @see get_home_path
441 */
442 public function getHomePath(): string
443 {
444 if (function_exists('\get_home_path') === false) {
445 require_once(ABSPATH . 'wp-admin/includes/file.php');
446 }
447
448 return get_home_path();
449 }
450
451 /**
452 * @see wp_parse_id_list
453 */
454 public function parseIdList(array|string|int $list): array
455 {
456 return wp_parse_id_list($list);
457 }
458
459 /**
460 * @see wp_die
461 */
462 #[NoReturn]
463 public function wpDie(string $message = '', string $title = '', array $arguments = []): void
464 {
465 wp_die($message, $title, $arguments);
466 }
467
468 /**
469 * @see is_feed
470 */
471 public function isFeed(array|string|int $feeds = ''): bool
472 {
473 return is_feed($feeds);
474 }
475
476 /**
477 * @see is_user_logged_in
478 */
479 public function isUserLoggedIn(): bool
480 {
481 return is_user_logged_in();
482 }
483
484 /**
485 * @see get_page_by_path
486 */
487 public function getPageByPath(
488 string $pagePath,
489 string $output = OBJECT,
490 string $postType = 'page'
491 ): array|WP_Post|null {
492 return get_page_by_path($pagePath, $output, $postType);
493 }
494
495 /**
496 * @see wp_redirect
497 */
498 public function wpRedirect(string $location, int $status = 302): bool
499 {
500 return wp_redirect($location, $status);
501 }
502
503 /**
504 * @see wp_get_referer
505 */
506 public function getReferer(): string|false
507 {
508 return wp_get_referer();
509 }
510
511 public function getPageLink(
512 bool|int|string|WP_Post $post = false,
513 bool $leaveName = false,
514 bool $sample = false
515 ): string {
516 return get_page_link($post, $leaveName, $sample);
517 }
518
519 public function getWpQuery(): WP_Query
520 {
521 global $wp_query;
522 return $wp_query;
523 }
524
525 public function isRestRequest(): bool
526 {
527 return defined('REST_REQUEST') === true && REST_REQUEST === true;
528 }
529
530 public function setRestRequestContext(bool $isEditingRequest): void
531 {
532 $this->restRequestDispatched = true;
533 //Nested requests, like the ones for embedded objects, must not lower the context again.
534 $this->editingRestRequestDetected = $this->editingRestRequestDetected || $isEditingRequest;
535 }
536
537 private function isReadingRestMethod(string $method): bool
538 {
539 return in_array(strtoupper($method), self::REST_READING_METHODS, true);
540 }
541
542 private function isEditingRestRequest(): bool
543 {
544 if ($this->isRestRequest() === false) {
545 return false;
546 }
547
548 if ($this->restRequestDispatched === true) {
549 return $this->editingRestRequestDetected;
550 }
551
552 //Undispatched requests only expose the raw data, so an editing request is assumed.
553 return $this->isReadingRestMethod((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')) === false
554 || ($_REQUEST['context'] ?? '') === 'edit';
555 }
556
557 /**
558 * @see is_admin
559 */
560 public function isAdmin(): bool
561 {
562 //Ajax request are always identified as an administrative interface page
563 if (wp_doing_ajax() === true || $this->isRestRequest() === true) {
564 //So let's check if we are calling the ajax data for the frontend or backend
565 //If the referer is an admin url we are requesting the data for the backend
566 $adminUrl = get_admin_url();
567 return str_starts_with((string)($_SERVER['HTTP_REFERER'] ?? ''), $adminUrl)
568 || $this->isEditingRestRequest() === true;
569 }
570
571 //No ajax request just uses the normal function
572 return is_admin();
573 }
574
575 /**
576 * @see wp_create_nonce
577 */
578 public function createNonce(int|string $action): string
579 {
580 return wp_create_nonce($action);
581 }
582
583 /**
584 * @see wp_nonce_field
585 */
586 public function getNonceField(
587 int|string $action = -1,
588 string $name = '_wpnonce',
589 bool $referrer = true,
590 bool $echo = true
591 ): string {
592 return wp_nonce_field($action, $name, $referrer, $echo);
593 }
594
595 /**
596 * @see wp_verify_nonce
597 */
598 public function verifyNonce(?string $nonce, int|string $action = -1): bool|int
599 {
600 return wp_verify_nonce($nonce, $action);
601 }
602
603 public function getRoles(): WP_Roles
604 {
605 global $wp_roles;
606 return $wp_roles;
607 }
608
609 /**
610 * @see add_menu_page
611 */
612 public function addMenuPage(
613 string $pageTitle,
614 string $menuTitle,
615 string $capability,
616 string $menuSlug,
617 mixed $function = '',
618 string $iconUrl = '',
619 int|float|null $position = null
620 ): string {
621 return add_menu_page($pageTitle, $menuTitle, $capability, $menuSlug, $function, $iconUrl, $position);
622 }
623
624 /**
625 * @see add_submenu_page
626 */
627 public function addSubMenuPage(
628 string $parentSlug,
629 string $pageTitle,
630 string $menuTitle,
631 string $capability,
632 string $menuSlug,
633 callable|string $function = ''
634 ): bool|string {
635 return add_submenu_page($parentSlug, $pageTitle, $menuTitle, $capability, $menuSlug, $function);
636 }
637
638 /**
639 * @see add_meta_box
640 */
641 public function addMetaBox(
642 string $id,
643 string $title,
644 callable $callback,
645 $screen = null,
646 string $context = 'advanced',
647 string $priority = 'default',
648 $callbackArguments = null
649 ): void {
650 add_meta_box($id, $title, $callback, $screen, $context, $priority, $callbackArguments);
651 }
652
653 /**
654 * @see get_pages
655 */
656 public function getPages(array|string|int $arguments): bool|array
657 {
658 return get_pages($arguments);
659 }
660
661 /**
662 * @see wp_register_style
663 */
664 public function registerStyle(
665 string $handle,
666 string $source,
667 array $depends = [],
668 bool|string|null $version = false,
669 string $media = 'all'
670 ): bool {
671 return wp_register_style($handle, $source, $depends, $version, $media);
672 }
673
674 /**
675 * @see wp_register_script
676 */
677 public function registerScript(
678 string $handle,
679 string $source,
680 array $depends = [],
681 bool|string|null $version = false,
682 bool $inFooter = false
683 ): bool {
684 return wp_register_script($handle, $source, $depends, $version, $inFooter);
685 }
686
687 /**
688 * @see wp_enqueue_style
689 */
690 public function enqueueStyle(
691 string $handle,
692 string $source = '',
693 array $depends = [],
694 bool|string|null $version = false,
695 string $media = 'all'
696 ): void {
697 wp_enqueue_style($handle, $source, $depends, $version, $media);
698 }
699
700 /**
701 * @see wp_enqueue_script
702 */
703 public function enqueueScript(
704 string $handle,
705 string $source = '',
706 array $depends = [],
707 bool|string|null $version = false,
708 bool $inFooter = false
709 ): void {
710 wp_enqueue_script($handle, $source, $depends, $version, $inFooter);
711 }
712
713 public function getMetaBoxes(): array
714 {
715 global $wp_meta_boxes;
716 return $wp_meta_boxes;
717 }
718
719 public function setMetaBoxes(array $wpMetaBoxes): void
720 {
721 global $wp_meta_boxes;
722 $wp_meta_boxes = $wpMetaBoxes;
723 }
724
725 /**
726 * @see get_sites
727 */
728 public function getSites(array $arguments = []): array
729 {
730 if (function_exists('\get_sites') === true && class_exists('\WP_Site_Query') === true) {
731 return get_sites($arguments);
732 }
733
734 return [];
735 }
736
737 /**
738 * @see apply_filters()
739 */
740 public function applyFilters(string $tag, mixed $value): mixed
741 {
742 return call_user_func_array('\apply_filters', func_get_args());
743 }
744
745 /**
746 * @see get_bloginfo
747 */
748 public function getBlogInfo(string $show = '', string $filter = 'raw'): string
749 {
750 return get_bloginfo($show, $filter);
751 }
752
753 /**
754 * @see esc_html
755 */
756 public function escHtml(string $text): string
757 {
758 return esc_html($text);
759 }
760
761 /**
762 * @see is_single
763 */
764 public function isSingle(array|string|int $post = ''): bool
765 {
766 return is_single($post);
767 }
768
769 /**
770 * @see is_page
771 */
772 public function isPage(array|string|int $page = ''): bool
773 {
774 return is_page($page);
775 }
776
777 /**
778 * @see \WP_PLUGIN_DIR
779 */
780 public function getPluginDir(): string
781 {
782 return WP_PLUGIN_DIR;
783 }
784
785 /**
786 * @see plugins_url
787 */
788 public function pluginsUrl(string $path = '', string $plugin = ''): string
789 {
790 return plugins_url($path, $plugin);
791 }
792
793 /**
794 * @see plugin_basename
795 */
796 public function pluginBasename(string $file): string
797 {
798 return plugin_basename($file);
799 }
800
801 /**
802 * @see wp_attachment_is_image
803 */
804 public function attachmentIsImage(int|WP_Post $post): bool
805 {
806 return wp_attachment_is_image($post);
807 }
808
809 /**
810 * @see current_user_can
811 */
812 public function currentUserCan(string $capability): bool
813 {
814 return current_user_can($capability);
815 }
816
817 /**
818 * @see get_users
819 */
820 public function getUsers(array $arguments = []): array
821 {
822 return get_users($arguments);
823 }
824
825 /**
826 * @return WP_Hook[]
827 */
828 public function getFilters(): array
829 {
830 global $wp_filter;
831 return $wp_filter;
832 }
833
834 public function setFilters(array $filters): void
835 {
836 global $wp_filter;
837 $wp_filter = $filters;
838 }
839
840 /**
841 * @see current_time
842 */
843 public function currentTime(string $type, int $gmt = 0): int|string
844 {
845 return current_time($type, $gmt);
846 }
847
848 public function formatDate(string $date): string
849 {
850 $dateFormat = __('M j, Y @ H:i');
851 return date_i18n($dateFormat, strtotime($date));
852 }
853
854 /**
855 * @see register_widget
856 */
857 public function registerWidget(mixed $widgetClass): void
858 {
859 register_widget($widgetClass);
860 }
861
862 /**
863 * @see wp_login_url
864 */
865 public function wpLoginUrl(string $redirect = '', bool $forceReauth = false): string
866 {
867 return wp_login_url($redirect, $forceReauth);
868 }
869
870 /**
871 * @see wp_logout_url
872 */
873 public function wpLogoutUrl(string $redirect = ''): string
874 {
875 return wp_logout_url($redirect);
876 }
877
878 /**
879 * @see wp_registration_url
880 */
881 public function wpRegistrationUrl(): string
882 {
883 return wp_registration_url();
884 }
885
886 /**
887 * @see wp_lostpassword_url
888 */
889 public function wpLostPasswordUrl(string $redirect = ''): string
890 {
891 return wp_lostpassword_url($redirect);
892 }
893
894 /**
895 * @see add_shortcode
896 */
897 public function addShortCode(string $tag, callable $function): void
898 {
899 add_shortcode($tag, $function);
900 }
901
902 /**
903 * @see do_shortcode
904 */
905 public function doShortCode(string $content, bool $ignoreHtml = false): string
906 {
907 return do_shortcode($content, $ignoreHtml);
908 }
909
910 /**
911 * @see attachment_url_to_postid
912 */
913 public function attachmentUrlToPostId(string $url): int
914 {
915 return attachment_url_to_postid($url);
916 }
917
918 /**
919 * @see got_mod_rewrite
920 */
921 public function gotModRewrite(): bool
922 {
923 if (function_exists('\got_mod_rewirte') === false) {
924 require_once(ABSPATH . 'wp-admin/includes/misc.php');
925 }
926
927 return got_mod_rewrite();
928 }
929
930 /**
931 * @see is_user_member_of_blog()
932 */
933 public function isUserMemberOfBlog(): bool
934 {
935 return is_user_member_of_blog();
936 }
937
938 /**
939 * @see get_queried_object_id
940 */
941 public function getQueriedObjectId(): int
942 {
943 return get_queried_object_id();
944 }
945
946 public function getCurrentPost(): array|WP_Post|null
947 {
948 global $post;
949 return $post;
950 }
951 }
952