Issues (19)

inc/namespace.php (1 issue)

1
<?php
2
/**
3
 * User_Approval Namespace.
4
 *
5
 * @package user-approval
6
 */
7
8
namespace User_Approval;
9
10
use WP_User;
11
12
const STATUS_PRE_APPROVED   = 'pre-approved';
13
const STATUS_PENDING        = 'pending';
14
const STATUS_APPROVED       = 'approved';
15
const STATUS_BLOCKED        = 'blocked';
16
const STATUS_META_KEY       = 'aj_user_status';
17
const STATUS_APPROVED_NONCE = 'aj-user-approve';
18 1
const STATUS_BLOCKED_NONCE  = 'aj-user-blocked';
19 1
20
/**
21
 * Hook up all the filters and actions.
22
 */
23
function bootstrap() {
24
25
	// Load text-domain for language translation.
26
	add_action( 'plugins_loaded', __NAMESPACE__ . '\\load_textdomain' );
27
}
28
29
/**
30
 * Load plugin text domain for text translation.
31
 *
32
 * @codeCoverageIgnore
33
 */
34
function load_textdomain() {
35
36
	load_plugin_textdomain(
37
		'user-approval',
38
		false,
39
		basename( plugin_dir_url( __DIR__ ) ) . '/languages'
40
	);
41 8
}
42
43
/**
44
 * Get default user role to which new registered user will be assigned.
45
 *
46
 * @return string
47
 */
48
function get_default_user_role() {
49
	return apply_filters( 'user_approval_default_user_role', get_option( 'default_role' ) );
50
}
51
52
/**
53
 * Check if given user is with default user role which need user status verification.
54 2
 *
55 2
 * @param WP_User $user WP_User object.
56
 *
57
 * @return bool
58
 */
59
function is_default_role_user( $user ) {
60
61
	return (
62
		$user instanceof WP_User
63
		&& in_array( get_default_user_role(), $user->roles, true )
64
	);
65 3
}
66
67 3
/**
68
 * Get all user role names array.
69
 *
70
 * @return array|string[]
71
 */
72
function get_role_names() {
73
	$user_roles_obj = wp_roles();
74
75
	return $user_roles_obj->role_names ?? [];
76 2
}
77
78
/**
79 2
 * Get user role which should be pre-approved.
80
 *
81 2
 * @return array
82
 */
83
function get_pre_approved_user_roles() {
84
	$user_roles = get_role_names();
85
86
	// Remove default role.
87
	unset( $user_roles[ get_default_user_role() ] );
88
89
	return $user_roles;
90
}
91
92
/**
93
 * Get all user status.
94 5
 *
95 5
 * @param string $status Status key to get label of respective user status.
96 5
 *
97 5
 * @return string|array
98
 */
99
function get_user_status( $status = '' ) {
100 5
101 4
	$user_status = [
102
		STATUS_PRE_APPROVED => esc_html__( 'Pre Approved', 'user-approval' ),
103
		STATUS_PENDING      => esc_html__( 'Pending', 'user-approval' ),
104 2
		STATUS_APPROVED     => esc_html__( 'Approved', 'user-approval' ),
105
		STATUS_BLOCKED      => esc_html__( 'Blocked', 'user-approval' ),
106
	];
107
108
	if ( isset( $user_status[ $status ] ) ) {
109
		return $user_status[ $status ];
110
	}
111
112
	return empty( $status ) ? $user_status[ STATUS_PENDING ] : $user_status;
113
}
114
115
/**
116
 * This method is an improved version of PHP's filter_input() and
117
 * works well on PHP Cli as well which PHP default method does not.
118
 *
119
 * @param int    $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.
120
 * @param string $variable_name Name of a variable to get.
121
 * @param int    $filter The ID of the filter to apply.
122
 * @param mixed  $options filter to apply.
123
 *
124
 * @codeCoverageIgnore
125
 *
126
 * @return mixed Value of the requested variable on success, FALSE if the filter fails, or NULL if the variable_name variable is not set.
127
 */
128
function filter_input( $type, $variable_name, $filter = FILTER_DEFAULT, $options = null ) {
129
130
	if ( php_sapi_name() !== 'cli' ) {
131
		/*
132
		 * Code is not running on PHP Cli and we are in clear.
133
		 * Use the PHP method and bail out.
134
		 */
135
		switch ( $filter ) {
136
			case FILTER_SANITIZE_STRING:
137
				$sanitized_variable = sanitize_text_field( \filter_input( $type, $variable_name, $filter ) );
138
				break;
139
			default:
140
				$sanitized_variable = \filter_input( $type, $variable_name, $filter, $options );
141
				break;
142
		}
143
144
		return $sanitized_variable;
145
	}
146
147
	$allowed_html_tags = wp_kses_allowed_html( 'post' );
148
149
	/**
150
	 * Marking the switch() block below to be ignored by PHPCS
151
	 * because PHPCS squawks on using superglobals like $_POST or $_GET
152
	 * directly but it can't be helped in this case as this code
153
	 * is running on Cli.
154
	 */
155
156
	// @codingStandardsIgnoreStart
157
	switch ( $type ) {
158
159
		case INPUT_GET:
160
			if ( ! isset( $_GET[ $variable_name ] ) ) {
161
				return null;
162
			}
163
			$input = wp_kses( $_GET[ $variable_name ], $allowed_html_tags );
164
			break;
165
166
		case INPUT_POST:
167
			if ( ! isset( $_POST[ $variable_name ] ) ) {
168
				return null;
169
			}
170
171
			$input = wp_kses( $_POST[ $variable_name ], $allowed_html_tags );
172
			break;
173
174
		case INPUT_COOKIE:
175
			if ( ! isset( $_COOKIE[ $variable_name ] ) ) {
176
				return null;
177
			}
178
			$input = wp_kses( $_COOKIE[ $variable_name ], $allowed_html_tags );
179
			break;
180
181
		case INPUT_SERVER:
182
			if ( ! isset( $_SERVER[ $variable_name ] ) ) {
183
				return null;
184
			}
185
186
			$input = wp_kses( $_SERVER[ $variable_name ], $allowed_html_tags );
187
			break;
188
189
		case INPUT_ENV:
190
			if ( ! isset( $_ENV[ $variable_name ] ) ) {
191
				return null;
192
			}
193
194
			$input = wp_kses( $_ENV[ $variable_name ], $allowed_html_tags );
195
			break;
196
197
		default:
198
			return null;
199
			break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
200
201
	}
202
	// @codingStandardsIgnoreEnd​
203
204
	return filter_var( $input, $filter );
205
}
206