-
Notifications
You must be signed in to change notification settings - Fork 1
/
Is.php
137 lines (127 loc) · 4.35 KB
/
Is.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Helper functions to determine entry point.
*
* @author Viktor Szépe <viktor@szepe.net>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/szepeviktor/toolkit4wp
*/
declare(strict_types=1);
namespace Toolkit4WP;
use WP_User;
/**
* Various request helpers.
*
* @see https://github.com/chesio/bc-security/blob/develop/classes/BlueChip/Security/Helpers/Is.php
*/
class Is
{
/**
* Whether we are in a live environment.
*
* @return bool
*/
public static function live(): bool
{
// Consider both production and staging environment as live.
return \defined('WP_ENV') && \in_array(\WP_ENV, ['production', 'staging'], true);
}
/**
* Whether given user is an administrator.
*
* @param \WP_User $user The given user.
* @return bool
*/
public static function admin(WP_User $user): bool
{
return \is_multisite() ? \user_can($user, 'manage_network') : \user_can($user, 'manage_options');
}
/**
* Whether the current user is not logged in.
*
* @return bool
*/
public static function anonymousUsers(): bool
{
return ! \is_user_logged_in();
}
/**
* Whether the current user is a comment author.
*
* @return bool
*/
public static function commentAuthor(): bool
{
// phpcs:ignore WordPress.VIP.RestrictedVariables.cache_constraints___COOKIE
return isset($_COOKIE['comment_author_' . \COOKIEHASH]);
}
/**
* Whether current webserver interface is CLI.
*
* @return bool
*/
public static function cli(): bool
{
return \php_sapi_name() === 'cli';
}
/**
* Whether current request is of the given type.
*
* All of them are available even before 'muplugins_loaded' action,
* exceptions are commented.
*
* @param string $type Type of request.
* @return bool
* phpcs:disable NeutronStandard.Functions.LongFunction.LongFunction
*/
public static function request(string $type): bool
{
// phpcs:disable Squiz.PHP.CommentedOutCode.Found
switch ($type) {
case 'installing':
return \defined('WP_INSTALLING') && \WP_INSTALLING === true;
case 'index':
return \defined('WP_USE_THEMES') && \WP_USE_THEMES === true;
case 'frontend':
// Use !request('frontend') for admin pages.
return (! \is_admin() || \wp_doing_ajax() ) && ! \wp_doing_cron();
case 'admin':
// Includes admin-ajax :(
return \is_admin();
case 'login':
return isset($_SERVER['REQUEST_URI'])
&& \explode('?', $_SERVER['REQUEST_URI'])[0]
=== \wp_parse_url(\wp_login_url('', true), \PHP_URL_PATH);
case 'async-upload':
return isset($_SERVER['SCRIPT_FILENAME'])
&& \ABSPATH . 'wp-admin/async-upload.php' === $_SERVER['SCRIPT_FILENAME'];
case 'preview': // in 'parse_query' action if (is_main_query())
return \is_preview() || \is_customize_preview();
case 'autosave': // After 'heartbeat_received', 500 action
// Autosave post while editing and Heartbeat.
return \defined('DOING_AUTOSAVE') && \DOING_AUTOSAVE === true;
case 'rest': // After 'parse_request' action
return \defined('REST_REQUEST') && \REST_REQUEST === true;
case 'ajax':
return \wp_doing_ajax();
case 'xmlrpc':
return \defined('XMLRPC_REQUEST') && \XMLRPC_REQUEST === true;
case 'trackback': // In 'parse_query'
return \is_trackback();
case 'search': // In 'parse_query'
return \is_search();
case 'feed': // In 'parse_query'
return \is_feed();
case 'robots': // In 'parse_query'
return \is_robots();
case 'cron':
return \wp_doing_cron();
case 'wp-cli':
return \defined('WP_CLI') && \WP_CLI === true;
default:
\_doing_it_wrong(__METHOD__, \esc_html(\sprintf('Unknown request type: %s', $type)), '0.1.0');
return false;
}
// phpcs:enable
}
}