PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.4.2
WP 2FA – Two-factor authentication for WordPress v2.4.2
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 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / includes / classes / Admin / Helpers / class-wp-helper.php
wp-2fa / includes / classes / Admin / Helpers Last commit date
class-classes-helper.php 3 years ago class-file-writer.php 3 years ago class-php-helper.php 3 years ago class-user-helper.php 3 years ago class-wp-helper.php 3 years ago
class-wp-helper.php
215 lines
1 <?php
2 /**
3 * Responsible for the WP core functionalities
4 *
5 * @package wp2fa
6 * @subpackage helpers
7 * @since 2.2.0
8 * @copyright 2023 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 use WP2FA\Admin\Helpers\User_Helper;
16
17 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
18
19 /**
20 * WP helper class
21 */
22 if ( ! class_exists( '\WP2FA\Admin\Helpers\WP_Helper' ) ) {
23
24 /**
25 * All the WP functionality must go trough this class
26 *
27 * @since 2.2.0
28 */
29 class WP_Helper {
30
31 /**
32 * Hold the user roles as array - Human readable is used for key of the array, and the internal role name is the value.
33 *
34 * @var array
35 *
36 * @since 2.2.0
37 */
38 private static $user_roles = array();
39
40 /**
41 * Hold the user roles as array - Internal role name is used for key of the array, and the human readable format is the value.
42 *
43 * @var array
44 *
45 * @since 2.2.0
46 */
47 private static $user_roles_wp = array();
48
49 /**
50 * Keeps the value of the multisite install of the WP
51 *
52 * @var bool
53 *
54 * @since 2.2.0
55 */
56 private static $is_multisite = null;
57
58 /**
59 * Holds array with all the sites in multisite WP installation
60 *
61 * @var array
62 */
63 private static $sites = array();
64
65 /**
66 * Inits the class, and fires all the necessarily methods
67 *
68 * @return void
69 *
70 * @since 2.2.0
71 */
72 public static function init() {
73 if ( self::is_multisite() ) {
74 \add_action( 'network_admin_notices', array( __CLASS__, 'show_critical_admin_notice' ) );
75 } else {
76 \add_action( 'admin_notices', array( __CLASS__, 'show_critical_admin_notice' ) );
77 }
78 }
79
80 /**
81 * Checks if specific role exists
82 *
83 * @param string $role - The name of the role to check.
84 *
85 * @return boolean
86 *
87 * @since 2.2.0
88 */
89 public static function is_role_exists( string $role ): bool {
90 self::set_roles();
91
92 if ( in_array( $role, self::$user_roles, true ) ) {
93 return true;
94 }
95
96 return false;
97 }
98
99 /**
100 * Returns the currently available WP roles - the Human readable format is the key
101 *
102 * @return array
103 *
104 * @since 2.2.0
105 */
106 public static function get_roles() {
107 self::set_roles();
108
109 return self::$user_roles;
110 }
111
112 /**
113 * Returns the currently available WP roles
114 *
115 * @return array
116 *
117 * @since 2.2.0
118 */
119 public static function get_roles_wp() {
120 if ( empty( self::$user_roles_wp ) ) {
121 self::set_roles();
122 self::$user_roles_wp = array_flip( self::$user_roles );
123 }
124
125 return self::$user_roles_wp;
126 }
127
128 /**
129 * Shows critical notices to the admin
130 *
131 * @return void
132 *
133 * @since 2.2.0
134 */
135 public static function show_critical_admin_notice() {
136 if ( User_Helper::is_admin() ) {
137 /**
138 * Gives the ability to show notices to the admins
139 */
140 \do_action( WP_2FA_PREFIX . 'critical_notice' );
141 }
142 }
143
144 /**
145 * Check is this is a multisite setup.
146 *
147 * @return boolean
148 *
149 * @since 2.2.0
150 */
151 public static function is_multisite() {
152 if ( null === self::$is_multisite ) {
153 self::$is_multisite = function_exists( 'is_multisite' ) && is_multisite();
154 }
155 return self::$is_multisite;
156 }
157
158 /**
159 * Collects all the sites from multisite WP installation
160 *
161 * @return array
162 */
163 public static function get_multi_sites(): array {
164 if ( self::is_multisite() ) {
165 if ( empty( self::$sites ) ) {
166
167 self::$sites = \get_sites();
168 }
169
170 return self::$sites;
171 }
172
173 return array();
174 }
175
176 /**
177 * Calculating the signature.
178 *
179 * @param array $data - Array with data to create a signature for.
180 *
181 * @return string
182 *
183 * @since 2.2.2
184 */
185 public static function calculate_api_signature( array $data ): string {
186 $now = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
187 $nonce = $now->getTimestamp();
188
189 $pk_hash = hash( 'sha512', $data['license_key'] . '|' . $nonce );
190 $authentication_string = base64_encode( $pk_hash . '|' . $nonce );
191
192 return $authentication_string;
193 }
194
195 /**
196 * Sets the internal variable with all the existing WP roles
197 *
198 * @return void
199 *
200 * @since 2.2.0
201 */
202 private static function set_roles() {
203 if ( empty( self::$user_roles ) ) {
204 global $wp_roles;
205
206 if ( null === $wp_roles ) {
207 wp_roles();
208 }
209
210 self::$user_roles = array_flip( $wp_roles->get_names() );
211 }
212 }
213 }
214 }
215