PluginProbe
User Access Manager / 2.3.10
User Access Manager v2.3.10
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.10, at src/Wrapper/Wordpress.php

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