PluginProbe
User Access Manager / 2.3.13
User Access Manager v2.3.13
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.13, at src/Wrapper/Wordpress.php

865 lines 19.5 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 remove_filter
299 */
300 public function removeFilter(string $tag, callable $functionToRemove, int $priority = 10): bool
301 {
302 return remove_filter($tag, $functionToRemove, $priority);
303 }
304
305 /**
306 * @see add_option
307 */
308 public function addOption(
309 string $option,
310 mixed $value = '',
311 string $deprecated = '',
312 bool|string $autoload = 'yes'
313 ): bool {
314 return add_option($option, $value, $deprecated, $autoload);
315 }
316
317 /**
318 * @see delete_option
319 */
320 public function deleteOption(string $option): bool
321 {
322 return delete_option($option);
323 }
324
325 /**
326 * @see update_option
327 */
328 public function updateOption(string $option, mixed $value = '', bool|string $autoload = 'yes'): bool
329 {
330 return update_option($option, $value, $autoload);
331 }
332
333 /**
334 * @see get_option
335 */
336 public function getOption(string $option, mixed $default = false)
337 {
338 return get_option($option, $default);
339 }
340
341 /**
342 * @see is_super_admin
343 */
344 public function isSuperAdmin(bool|int|string|null $userId = false): bool
345 {
346 return is_super_admin($userId);
347 }
348
349 /**
350 * @see wp_get_current_user
351 */
352 public function getCurrentUser(): WP_User
353 {
354 if (function_exists('\wp_get_current_user') === false) {
355 require_once(ABSPATH . 'wp-includes/pluggable.php');
356 }
357
358 return wp_get_current_user();
359 }
360
361 /**
362 * @see get_allowed_mime_types
363 */
364 public function getAllowedMimeTypes(WP_User|int|string|null $user = null): array
365 {
366 return get_allowed_mime_types($user);
367 }
368
369 /**
370 * @see wp_upload_dir
371 */
372 public function getUploadDir(?string $time = null, bool $createDir = true, bool $refreshCache = false): array
373 {
374 return wp_upload_dir($time, $createDir, $refreshCache);
375 }
376
377 /**
378 * @see home_url
379 */
380 public function getHomeUrl(string $path = '', ?string $scheme = null): string
381 {
382 return home_url($path, $scheme);
383 }
384
385 /**
386 * @see site_url
387 */
388 public function getSiteUrl(string $path = '', ?string $scheme = null): string
389 {
390 return site_url($path, $scheme);
391 }
392
393 /**
394 * @see get_home_path
395 */
396 public function getHomePath(): string
397 {
398 if (function_exists('\get_home_path') === false) {
399 require_once(ABSPATH . 'wp-admin/includes/file.php');
400 }
401
402 return get_home_path();
403 }
404
405 /**
406 * @see wp_parse_id_list
407 */
408 public function parseIdList(array|string|int $list): array
409 {
410 return wp_parse_id_list($list);
411 }
412
413 /**
414 * @see wp_die
415 */
416 #[NoReturn]
417 public function wpDie(string $message = '', string $title = '', array $arguments = []): void
418 {
419 wp_die($message, $title, $arguments);
420 }
421
422 /**
423 * @see is_feed
424 */
425 public function isFeed(array|string|int $feeds = ''): bool
426 {
427 return is_feed($feeds);
428 }
429
430 /**
431 * @see is_user_logged_in
432 */
433 public function isUserLoggedIn(): bool
434 {
435 return is_user_logged_in();
436 }
437
438 /**
439 * @see get_page_by_path
440 */
441 public function getPageByPath(
442 string $pagePath,
443 string $output = OBJECT,
444 string $postType = 'page'
445 ): array|WP_Post|null {
446 return get_page_by_path($pagePath, $output, $postType);
447 }
448
449 /**
450 * @see wp_redirect
451 */
452 public function wpRedirect(string $location, int $status = 302): bool
453 {
454 return wp_redirect($location, $status);
455 }
456
457 public function getPageLink(
458 bool|int|string|WP_Post $post = false,
459 bool $leaveName = false,
460 bool $sample = false
461 ): string {
462 return get_page_link($post, $leaveName, $sample);
463 }
464
465 public function getWpQuery(): WP_Query
466 {
467 global $wp_query;
468 return $wp_query;
469 }
470
471 /**
472 * @see is_admin
473 */
474 public function isAdmin(): bool
475 {
476 //Ajax request are always identified as an administrative interface page
477 if (wp_doing_ajax() === true || defined('REST_REQUEST') && REST_REQUEST) {
478 //So let's check if we are calling the ajax data for the frontend or backend
479 //If the referer is an admin url we are requesting the data for the backend
480 $adminUrl = get_admin_url();
481 return str_starts_with((string)($_SERVER['HTTP_REFERER'] ?? ''), $adminUrl);
482 }
483
484 //No ajax request just uses the normal function
485 return is_admin();
486 }
487
488 /**
489 * @see wp_create_nonce
490 */
491 public function createNonce(int|string $action): string
492 {
493 return wp_create_nonce($action);
494 }
495
496 /**
497 * @see wp_nonce_field
498 */
499 public function getNonceField(
500 int|string $action = -1,
501 string $name = '_wpnonce',
502 bool $referrer = true,
503 bool $echo = true
504 ): string {
505 return wp_nonce_field($action, $name, $referrer, $echo);
506 }
507
508 /**
509 * @see wp_verify_nonce
510 */
511 public function verifyNonce(?string $nonce, int|string $action = -1): bool|int
512 {
513 return wp_verify_nonce($nonce, $action);
514 }
515
516 public function getRoles(): WP_Roles
517 {
518 global $wp_roles;
519 return $wp_roles;
520 }
521
522 /**
523 * @see add_menu_page
524 */
525 public function addMenuPage(
526 string $pageTitle,
527 string $menuTitle,
528 string $capability,
529 string $menuSlug,
530 mixed $function = '',
531 string $iconUrl = '',
532 int|float|null $position = null
533 ): string {
534 return add_menu_page($pageTitle, $menuTitle, $capability, $menuSlug, $function, $iconUrl, $position);
535 }
536
537 /**
538 * @see add_submenu_page
539 */
540 public function addSubMenuPage(
541 string $parentSlug,
542 string $pageTitle,
543 string $menuTitle,
544 string $capability,
545 string $menuSlug,
546 callable|string $function = ''
547 ): bool|string {
548 return add_submenu_page($parentSlug, $pageTitle, $menuTitle, $capability, $menuSlug, $function);
549 }
550
551 /**
552 * @see add_meta_box
553 */
554 public function addMetaBox(
555 string $id,
556 string $title,
557 callable $callback,
558 $screen = null,
559 string $context = 'advanced',
560 string $priority = 'default',
561 $callbackArguments = null
562 ): void {
563 add_meta_box($id, $title, $callback, $screen, $context, $priority, $callbackArguments);
564 }
565
566 /**
567 * @see get_pages
568 */
569 public function getPages(array|string|int $arguments): bool|array
570 {
571 return get_pages($arguments);
572 }
573
574 /**
575 * @see wp_register_style
576 */
577 public function registerStyle(
578 string $handle,
579 string $source,
580 array $depends = [],
581 bool|string|null $version = false,
582 string $media = 'all'
583 ): bool {
584 return wp_register_style($handle, $source, $depends, $version, $media);
585 }
586
587 /**
588 * @see wp_register_script
589 */
590 public function registerScript(
591 string $handle,
592 string $source,
593 array $depends = [],
594 bool|string|null $version = false,
595 bool $inFooter = false
596 ): bool {
597 return wp_register_script($handle, $source, $depends, $version, $inFooter);
598 }
599
600 /**
601 * @see wp_enqueue_style
602 */
603 public function enqueueStyle(
604 string $handle,
605 string $source = '',
606 array $depends = [],
607 bool|string|null $version = false,
608 string $media = 'all'
609 ): void {
610 wp_enqueue_style($handle, $source, $depends, $version, $media);
611 }
612
613 /**
614 * @see wp_enqueue_script
615 */
616 public function enqueueScript(
617 string $handle,
618 string $source = '',
619 array $depends = [],
620 bool|string|null $version = false,
621 bool $inFooter = false
622 ): void {
623 wp_enqueue_script($handle, $source, $depends, $version, $inFooter);
624 }
625
626 public function getMetaBoxes(): array
627 {
628 global $wp_meta_boxes;
629 return $wp_meta_boxes;
630 }
631
632 public function setMetaBoxes(array $wpMetaBoxes): void
633 {
634 global $wp_meta_boxes;
635 $wp_meta_boxes = $wpMetaBoxes;
636 }
637
638 /**
639 * @see get_sites
640 */
641 public function getSites(array $arguments = []): array
642 {
643 if (function_exists('\get_sites') === true && class_exists('\WP_Site_Query') === true) {
644 return get_sites($arguments);
645 }
646
647 return [];
648 }
649
650 /**
651 * @see apply_filters()
652 */
653 public function applyFilters(string $tag, mixed $value): mixed
654 {
655 return call_user_func_array('\apply_filters', func_get_args());
656 }
657
658 /**
659 * @see get_bloginfo
660 */
661 public function getBlogInfo(string $show = '', string $filter = 'raw'): string
662 {
663 return get_bloginfo($show, $filter);
664 }
665
666 /**
667 * @see esc_html
668 */
669 public function escHtml(string $text): string
670 {
671 return esc_html($text);
672 }
673
674 /**
675 * @see is_single
676 */
677 public function isSingle(array|string|int $post = ''): bool
678 {
679 return is_single($post);
680 }
681
682 /**
683 * @see is_page
684 */
685 public function isPage(array|string|int $page = ''): bool
686 {
687 return is_page($page);
688 }
689
690 /**
691 * @see \WP_PLUGIN_DIR
692 */
693 public function getPluginDir(): string
694 {
695 return WP_PLUGIN_DIR;
696 }
697
698 /**
699 * @see plugins_url
700 */
701 public function pluginsUrl(string $path = '', string $plugin = ''): string
702 {
703 return plugins_url($path, $plugin);
704 }
705
706 /**
707 * @see plugin_basename
708 */
709 public function pluginBasename(string $file): string
710 {
711 return plugin_basename($file);
712 }
713
714 /**
715 * @see wp_attachment_is_image
716 */
717 public function attachmentIsImage(int|WP_Post $post): bool
718 {
719 return wp_attachment_is_image($post);
720 }
721
722 /**
723 * @see current_user_can
724 */
725 public function currentUserCan(string $capability): bool
726 {
727 return current_user_can($capability);
728 }
729
730 /**
731 * @see get_users
732 */
733 public function getUsers(array $arguments = []): array
734 {
735 return get_users($arguments);
736 }
737
738 /**
739 * @return WP_Hook[]
740 */
741 public function getFilters(): array
742 {
743 global $wp_filter;
744 return $wp_filter;
745 }
746
747 public function setFilters(array $filters): void
748 {
749 global $wp_filter;
750 $wp_filter = $filters;
751 }
752
753 /**
754 * @see current_time
755 */
756 public function currentTime(string $type, int $gmt = 0): int|string
757 {
758 return current_time($type, $gmt);
759 }
760
761 public function formatDate(string $date): string
762 {
763 $dateFormat = __('M j, Y @ H:i');
764 return date_i18n($dateFormat, strtotime($date));
765 }
766
767 /**
768 * @see register_widget
769 */
770 public function registerWidget(mixed $widgetClass): void
771 {
772 register_widget($widgetClass);
773 }
774
775 /**
776 * @see wp_login_url
777 */
778 public function wpLoginUrl(string $redirect = '', bool $forceReauth = false): string
779 {
780 return wp_login_url($redirect, $forceReauth);
781 }
782
783 /**
784 * @see wp_logout_url
785 */
786 public function wpLogoutUrl(string $redirect = ''): string
787 {
788 return wp_logout_url($redirect);
789 }
790
791 /**
792 * @see wp_registration_url
793 */
794 public function wpRegistrationUrl(): string
795 {
796 return wp_registration_url();
797 }
798
799 /**
800 * @see wp_lostpassword_url
801 */
802 public function wpLostPasswordUrl(string $redirect = ''): string
803 {
804 return wp_lostpassword_url($redirect);
805 }
806
807 /**
808 * @see add_shortcode
809 */
810 public function addShortCode(string $tag, callable $function): void
811 {
812 add_shortcode($tag, $function);
813 }
814
815 /**
816 * @see do_shortcode
817 */
818 public function doShortCode(string $content, bool $ignoreHtml = false): string
819 {
820 return do_shortcode($content, $ignoreHtml);
821 }
822
823 /**
824 * @see attachment_url_to_postid
825 */
826 public function attachmentUrlToPostId(string $url): int
827 {
828 return attachment_url_to_postid($url);
829 }
830
831 /**
832 * @see got_mod_rewrite
833 */
834 public function gotModRewrite(): bool
835 {
836 if (function_exists('\got_mod_rewirte') === false) {
837 require_once(ABSPATH . 'wp-admin/includes/misc.php');
838 }
839
840 return got_mod_rewrite();
841 }
842
843 /**
844 * @see is_user_member_of_blog()
845 */
846 public function isUserMemberOfBlog(): bool
847 {
848 return is_user_member_of_blog();
849 }
850
851 /**
852 * @see get_queried_object_id
853 */
854 public function getQueriedObjectId(): int
855 {
856 return get_queried_object_id();
857 }
858
859 public function getCurrentPost(): array|WP_Post|null
860 {
861 global $post;
862 return $post;
863 }
864 }
865