PluginProbe
User Access Manager / 2.3.20
User Access Manager v2.3.20
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.20, at src/Wrapper/Wordpress.php

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