PluginProbe
User Access Manager / 2.3.14
User Access Manager v2.3.14
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.14, at src/Wrapper/Wordpress.php

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