PluginProbe
WP 2FA – Two-factor authentication for WordPress / 2.2.1
WP 2FA – Two-factor authentication for WordPress v2.2.1
4.1.0 4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 All 42 releases
wp-2fa / includes / classes / Admin / Helpers / class-wp-helper.php
class-wp-helper.php
194 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Responsible for the WP core functionalities
4 *
5 * @package wp2fa
6 * @subpackage helpers
7 * @since 2.2.0
8 * @copyright 2022 WP White Security
9 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
10 * @link https://wordpress.org/plugins/wp-2fa/
11 */
12
13 namespace WP2FA\Admin\Helpers;
14
15 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
16
17 /**
18 * WP helper class
19 */
20 if ( ! class_exists( '\WP2FA\Admin\Helpers\WP_Helper' ) ) {
21
22 /**
23 * All the WP functionality must go trough this class
24 *
25 * @since 2.2.0
26 */
27 class WP_Helper {
28
29 /**
30 * Hold the user roles as array - Human readable is used for key of the array, and the internal role name is the value.
31 *
32 * @var array
33 *
34 * @since 2.2.0
35 */
36 private static $user_roles = array();
37
38 /**
39 * Hold the user roles as array - Internal role name is used for key of the array, and the human readable format is the value.
40 *
41 * @var array
42 *
43 * @since 2.2.0
44 */
45 private static $user_roles_wp = array();
46
47 /**
48 * Keeps the value of the multisite install of the WP
49 *
50 * @var bool
51 *
52 * @since 2.2.0
53 */
54 private static $is_multisite = null;
55
56 /**
57 * Holds array with all the sites in multisite WP installation
58 *
59 * @var array
60 */
61 private static $sites = array();
62
63 /**
64 * Inits the class, and fires all the necessarily methods
65 *
66 * @return void
67 *
68 * @since 2.2.0
69 */
70 public static function init() {
71 if ( self::is_multisite() ) {
72 \add_action( 'network_admin_notices', array( __CLASS__, 'show_critical_admin_notice' ) );
73 } else {
74 \add_action( 'admin_notices', array( __CLASS__, 'show_critical_admin_notice' ) );
75 }
76 }
77
78 /**
79 * Checks if specific role exists
80 *
81 * @param string $role - The name of the role to check.
82 *
83 * @return boolean
84 *
85 * @since 2.2.0
86 */
87 public static function is_role_exists( string $role ): bool {
88 self::set_roles();
89
90 if ( in_array( $role, self::$user_roles, true ) ) {
91 return true;
92 }
93
94 return false;
95 }
96
97 /**
98 * Returns the currently available WP roles - the Human readable format is the key
99 *
100 * @return array
101 *
102 * @since 2.2.0
103 */
104 public static function get_roles() {
105 self::set_roles();
106
107 return self::$user_roles;
108 }
109
110 /**
111 * Returns the currently available WP roles
112 *
113 * @return array
114 *
115 * @since 2.2.0
116 */
117 public static function get_roles_wp() {
118 if ( empty( self::$user_roles_wp ) ) {
119 self::set_roles();
120 self::$user_roles_wp = array_flip( self::$user_roles );
121 }
122
123 return self::$user_roles_wp;
124 }
125
126 /**
127 * Shows critical notices to the admin
128 *
129 * @return void
130 *
131 * @since 2.2.0
132 */
133 public static function show_critical_admin_notice() {
134 if ( User_Helper::is_admin() ) {
135 /**
136 * Gives the ability to show notices to the admins
137 */
138 \do_action( WP_2FA_PREFIX . 'critical_notice' );
139 }
140 }
141
142 /**
143 * Check is this is a multisite setup.
144 *
145 * @return boolean
146 *
147 * @since 2.2.0
148 */
149 public static function is_multisite() {
150 if ( null === self::$is_multisite ) {
151 self::$is_multisite = function_exists( 'is_multisite' ) && is_multisite();
152 }
153 return self::$is_multisite;
154 }
155
156 /**
157 * Collects all the sites from multisite WP installation
158 *
159 * @return array
160 */
161 public static function get_multi_sites(): array {
162 if ( self::is_multisite() ) {
163 if ( empty( self::$sites ) ) {
164
165 self::$sites = \get_sites();
166 }
167
168 return self::$sites;
169 }
170
171 return array();
172 }
173
174 /**
175 * Sets the internal variable with all the existing WP roles
176 *
177 * @return void
178 *
179 * @since 2.2.0
180 */
181 private static function set_roles() {
182 if ( empty( self::$user_roles ) ) {
183 global $wp_roles;
184
185 if ( null === $wp_roles ) {
186 wp_roles();
187 }
188
189 self::$user_roles = array_flip( $wp_roles->get_names() );
190 }
191 }
192 }
193 }
194