PluginProbe
User Access Manager / 2.3.17
User Access Manager v2.3.17
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.17, at src/Wrapper/Wordpress.php

960 lines 21.8 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_current_user;
81 use function wp_login_url;
82 use function wp_logout_url;
83 use function wp_lostpassword_url;
84 use function wp_nonce_field;
85 use function wp_parse_id_list;
86 use function wp_redirect;
87 use function wp_register_script;
88 use function wp_register_style;
89 use function wp_registration_url;
90 use function wp_upload_dir;
91 use function wp_verify_nonce;
92
93 class Wordpress
94 {
95 public const REST_READING_METHODS = ['GET', 'HEAD'];
96
97 private bool $restRequestDispatched = false;
98 private bool $editingRestRequestDetected = false;
99
100 public function getDatabase(): wpdb
101 {
102 global $wpdb;
103 return $wpdb;
104 }
105
106 public function isNginx(): bool
107 {
108 global $is_nginx;
109 return $is_nginx;
110 }
111
112 /**
113 * @see wp_using_ext_object_cache
114 */
115 public function isUsingExtObjectCache(): bool
116 {
117 return function_exists('wp_using_ext_object_cache') && wp_using_ext_object_cache() === true;
118 }
119
120 /**
121 * @see apache_get_modules
122 */
123 public function isApacheModuleLoaded(string $module): bool
124 {
125 return function_exists('apache_get_modules') && in_array($module, apache_get_modules(), true);
126 }
127
128 /**
129 * @see is_post_type_hierarchical
130 */
131 public function isPostTypeHierarchical(string $postType): bool
132 {
133 return is_post_type_hierarchical($postType);
134 }
135
136 /**
137 * @see is_taxonomy_hierarchical
138 */
139 public function isTaxonomyHierarchical(string $taxonomy): bool
140 {
141 return is_taxonomy_hierarchical($taxonomy);
142 }
143
144 /**
145 * @see get_userdata
146 */
147 public function getUserData(int|string|null $id): WP_User|bool
148 {
149 return get_userdata($id);
150 }
151
152 /**
153 * @see get_post_types
154 */
155 public function getPostTypes(
156 array|string|int $arguments = [],
157 string $output = 'names',
158 string $operator = 'and'
159 ): array {
160 return get_post_types($arguments, $output, $operator);
161 }
162
163 /**
164 * @return string[]|WP_Taxonomy[]
165 * @see get_taxonomies
166 */
167 public function getTaxonomies(array $arguments = [], string $output = 'names', string $operator = 'and'): array
168 {
169 return get_taxonomies($arguments, $output, $operator);
170 }
171
172 /**
173 * @see get_taxonomy
174 */
175 public function getTaxonomy(string $taxonomy): WP_Taxonomy|bool
176 {
177 return get_taxonomy($taxonomy);
178 }
179
180 /**
181 * @see get_post
182 */
183 public function getPost(
184 int|string|null $id,
185 string $output = OBJECT,
186 string $filter = 'raw'
187 ): WP_Post|array|bool|null {
188 return get_post($id, $output, $filter);
189 }
190
191 /**
192 * @see get_post_type_object
193 */
194 public function getPostTypeObject(int|string $postType): ?WP_Post_Type
195 {
196 return get_post_type_object($postType);
197 }
198
199 /**
200 * @see get_term
201 */
202 public function getTerm(
203 int|string $id,
204 string $taxonomy = '',
205 string $output = OBJECT,
206 string $filter = 'raw'
207 ): WP_Term|WP_Error|array|bool|null {
208 return get_term($id, $taxonomy, $output, $filter);
209 }
210
211
212 /**
213 * @see dbDelta
214 */
215 public function dbDelta(array|string $queries = '', bool $execute = true): array
216 {
217 if (function_exists('\dbDelta') === false) {
218 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
219 }
220
221 return dbDelta($queries, $execute);
222 }
223
224 /**
225 * @see switch_to_blog
226 */
227 public function switchToBlog(int|string|null $blogId): bool
228 {
229 if (function_exists('\switch_to_blog') === true) {
230 return (bool) switch_to_blog($blogId);
231 }
232
233 return true;
234 }
235
236 /**
237 * @see \restore_current_blog()
238 */
239 public function restoreCurrentBlog(): bool
240 {
241 if (function_exists('\restore_current_blog') === true) {
242 return restore_current_blog();
243 }
244
245 return true;
246 }
247
248 /**
249 * @see is_multisite
250 */
251 public function isMultiSite(): bool
252 {
253 return is_multisite();
254 }
255
256 /**
257 * @see do_action
258 */
259 public function doAction(string $tag, mixed $arguments = ''): void
260 {
261 do_action($tag, $arguments);
262 }
263
264 /**
265 * @see add_action
266 */
267 public function addAction(
268 string $tag,
269 callable $functionToAdd,
270 int $priority = 10,
271 int $acceptedArguments = 1
272 ): bool {
273 return add_action($tag, $functionToAdd, $priority, $acceptedArguments);
274 }
275
276 /**
277 * @see remove_action
278 */
279 public function removeAction(string $hookName, callable $callback, int $priority = 10): bool
280 {
281 return remove_action($hookName, $callback, $priority);
282 }
283
284 /**
285 * @see has_filter
286 */
287 public function hasFilter(string $tag, callable|bool $functionToCheck = false): bool|int
288 {
289 return has_filter($tag, $functionToCheck);
290 }
291
292 /**
293 * @see add_filter
294 */
295 public function addFilter(
296 string $tag,
297 callable $functionToAdd,
298 int $priority = 10,
299 int $acceptedArguments = 1
300 ): bool {
301 return add_filter($tag, $functionToAdd, $priority, $acceptedArguments);
302 }
303
304 /**
305 * @see WP_Error
306 */
307 public function getWpError(string|int $code = '', string $message = '', mixed $data = ''): WP_Error
308 {
309 return new WP_Error($code, $message, $data);
310 }
311
312 /**
313 * @see remove_filter
314 */
315 public function removeFilter(string $tag, callable $functionToRemove, int $priority = 10): bool
316 {
317 return remove_filter($tag, $functionToRemove, $priority);
318 }
319
320 /**
321 * @see add_option
322 */
323 public function addOption(
324 string $option,
325 mixed $value = '',
326 string $deprecated = '',
327 bool|string $autoload = 'yes'
328 ): bool {
329 return add_option($option, $value, $deprecated, $autoload);
330 }
331
332 /**
333 * @see delete_option
334 */
335 public function deleteOption(string $option): bool
336 {
337 return delete_option($option);
338 }
339
340 /**
341 * @see update_option
342 */
343 public function updateOption(string $option, mixed $value = '', bool|string $autoload = 'yes'): bool
344 {
345 return update_option($option, $value, $autoload);
346 }
347
348 /**
349 * @see get_option
350 */
351 public function getOption(string $option, mixed $default = false)
352 {
353 return get_option($option, $default);
354 }
355
356 /**
357 * @see wp_cache_set
358 */
359 public function wpCacheSet(string $key, mixed $data, string $group = '', int $expire = 0): bool
360 {
361 return wp_cache_set($key, $data, $group, $expire);
362 }
363
364 /**
365 * @see wp_cache_get
366 */
367 public function wpCacheGet(string $key, string $group = ''): mixed
368 {
369 return wp_cache_get($key, $group);
370 }
371
372 /**
373 * @see wp_cache_delete
374 */
375 public function wpCacheDelete(string $key, string $group = ''): bool
376 {
377 return wp_cache_delete($key, $group);
378 }
379
380 /**
381 * @see is_super_admin
382 */
383 public function isSuperAdmin(bool|int|string|null $userId = false): bool
384 {
385 return is_super_admin($userId);
386 }
387
388 /**
389 * @see wp_get_current_user
390 */
391 public function getCurrentUser(): WP_User
392 {
393 if (function_exists('\wp_get_current_user') === false) {
394 require_once(ABSPATH . 'wp-includes/pluggable.php');
395 }
396
397 return wp_get_current_user();
398 }
399
400 /**
401 * @see get_allowed_mime_types
402 */
403 public function getAllowedMimeTypes(WP_User|int|string|null $user = null): array
404 {
405 return get_allowed_mime_types($user);
406 }
407
408 /**
409 * @see wp_upload_dir
410 */
411 public function getUploadDir(?string $time = null, bool $createDir = true, bool $refreshCache = false): array
412 {
413 return wp_upload_dir($time, $createDir, $refreshCache);
414 }
415
416 /**
417 * @see home_url
418 */
419 public function getHomeUrl(string $path = '', ?string $scheme = null): string
420 {
421 return home_url($path, $scheme);
422 }
423
424 /**
425 * @see site_url
426 */
427 public function getSiteUrl(string $path = '', ?string $scheme = null): string
428 {
429 return site_url($path, $scheme);
430 }
431
432 /**
433 * @see add_query_arg
434 */
435 public function addQueryArg(array $arguments, string $url): string
436 {
437 return add_query_arg($arguments, $url);
438 }
439
440 /**
441 * @see get_home_path
442 */
443 public function getHomePath(): string
444 {
445 if (function_exists('\get_home_path') === false) {
446 require_once(ABSPATH . 'wp-admin/includes/file.php');
447 }
448
449 return get_home_path();
450 }
451
452 /**
453 * @see wp_parse_id_list
454 */
455 public function parseIdList(array|string|int $list): array
456 {
457 return wp_parse_id_list($list);
458 }
459
460 /**
461 * @see wp_die
462 */
463 #[NoReturn]
464 public function wpDie(string $message = '', string $title = '', array $arguments = []): void
465 {
466 wp_die($message, $title, $arguments);
467 }
468
469 /**
470 * @see is_feed
471 */
472 public function isFeed(array|string|int $feeds = ''): bool
473 {
474 return is_feed($feeds);
475 }
476
477 /**
478 * @see is_user_logged_in
479 */
480 public function isUserLoggedIn(): bool
481 {
482 return is_user_logged_in();
483 }
484
485 /**
486 * @see get_page_by_path
487 */
488 public function getPageByPath(
489 string $pagePath,
490 string $output = OBJECT,
491 string $postType = 'page'
492 ): array|WP_Post|null {
493 return get_page_by_path($pagePath, $output, $postType);
494 }
495
496 /**
497 * @see wp_redirect
498 */
499 public function wpRedirect(string $location, int $status = 302): bool
500 {
501 return wp_redirect($location, $status);
502 }
503
504 /**
505 * @see wp_get_referer
506 */
507 public function getReferer(): string|false
508 {
509 return wp_get_referer();
510 }
511
512 public function getPageLink(
513 bool|int|string|WP_Post $post = false,
514 bool $leaveName = false,
515 bool $sample = false
516 ): string {
517 return get_page_link($post, $leaveName, $sample);
518 }
519
520 public function getWpQuery(): WP_Query
521 {
522 global $wp_query;
523 return $wp_query;
524 }
525
526 public function isRestRequest(): bool
527 {
528 return defined('REST_REQUEST') === true && REST_REQUEST === true;
529 }
530
531 public function setRestRequestContext(bool $isEditingRequest): void
532 {
533 $this->restRequestDispatched = true;
534 //Nested requests, like the ones for embedded objects, must not lower the context again.
535 $this->editingRestRequestDetected = $this->editingRestRequestDetected || $isEditingRequest;
536 }
537
538 private function isReadingRestMethod(string $method): bool
539 {
540 return in_array(strtoupper($method), self::REST_READING_METHODS, true);
541 }
542
543 private function isEditingRestRequest(): bool
544 {
545 if ($this->isRestRequest() === false) {
546 return false;
547 }
548
549 if ($this->restRequestDispatched === true) {
550 return $this->editingRestRequestDetected;
551 }
552
553 //Undispatched requests only expose the raw data, so an editing request is assumed.
554 return $this->isReadingRestMethod((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')) === false
555 || ($_REQUEST['context'] ?? '') === 'edit';
556 }
557
558 /**
559 * @see is_admin
560 */
561 public function isAdmin(): bool
562 {
563 if ($this->isRestRequest() === true) {
564 return $this->isEditingRestRequest();
565 }
566
567 if (wp_doing_ajax() === true) {
568 return is_user_logged_in() === true
569 && str_starts_with((string)($_SERVER['HTTP_REFERER'] ?? ''), get_admin_url());
570 }
571
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 get_attached_file
811 */
812 public function getAttachedFile(int $attachmentId): string|false
813 {
814 return get_attached_file($attachmentId);
815 }
816
817 /**
818 * @see current_user_can
819 */
820 public function currentUserCan(string $capability): bool
821 {
822 return current_user_can($capability);
823 }
824
825 /**
826 * @see get_users
827 */
828 public function getUsers(array $arguments = []): array
829 {
830 return get_users($arguments);
831 }
832
833 /**
834 * @return WP_Hook[]
835 */
836 public function getFilters(): array
837 {
838 global $wp_filter;
839 return $wp_filter;
840 }
841
842 public function setFilters(array $filters): void
843 {
844 global $wp_filter;
845 $wp_filter = $filters;
846 }
847
848 /**
849 * @see current_time
850 */
851 public function currentTime(string $type, int $gmt = 0): int|string
852 {
853 return current_time($type, $gmt);
854 }
855
856 public function formatDate(string $date): string
857 {
858 $dateFormat = __('M j, Y @ H:i');
859 return date_i18n($dateFormat, strtotime($date));
860 }
861
862 /**
863 * @see register_widget
864 */
865 public function registerWidget(mixed $widgetClass): void
866 {
867 register_widget($widgetClass);
868 }
869
870 /**
871 * @see wp_login_url
872 */
873 public function wpLoginUrl(string $redirect = '', bool $forceReauth = false): string
874 {
875 return wp_login_url($redirect, $forceReauth);
876 }
877
878 /**
879 * @see wp_logout_url
880 */
881 public function wpLogoutUrl(string $redirect = ''): string
882 {
883 return wp_logout_url($redirect);
884 }
885
886 /**
887 * @see wp_registration_url
888 */
889 public function wpRegistrationUrl(): string
890 {
891 return wp_registration_url();
892 }
893
894 /**
895 * @see wp_lostpassword_url
896 */
897 public function wpLostPasswordUrl(string $redirect = ''): string
898 {
899 return wp_lostpassword_url($redirect);
900 }
901
902 /**
903 * @see add_shortcode
904 */
905 public function addShortCode(string $tag, callable $function): void
906 {
907 add_shortcode($tag, $function);
908 }
909
910 /**
911 * @see do_shortcode
912 */
913 public function doShortCode(string $content, bool $ignoreHtml = false): string
914 {
915 return do_shortcode($content, $ignoreHtml);
916 }
917
918 /**
919 * @see attachment_url_to_postid
920 */
921 public function attachmentUrlToPostId(string $url): int
922 {
923 return attachment_url_to_postid($url);
924 }
925
926 /**
927 * @see got_mod_rewrite
928 */
929 public function gotModRewrite(): bool
930 {
931 if (function_exists('\got_mod_rewirte') === false) {
932 require_once(ABSPATH . 'wp-admin/includes/misc.php');
933 }
934
935 return got_mod_rewrite();
936 }
937
938 /**
939 * @see is_user_member_of_blog()
940 */
941 public function isUserMemberOfBlog(): bool
942 {
943 return is_user_member_of_blog();
944 }
945
946 /**
947 * @see get_queried_object_id
948 */
949 public function getQueriedObjectId(): int
950 {
951 return get_queried_object_id();
952 }
953
954 public function getCurrentPost(): array|WP_Post|null
955 {
956 global $post;
957 return $post;
958 }
959 }
960