PluginProbe
Core Framework / trunk
Core Framework vtrunk
2.0.2 2.0.1 2.0.0 1.10.4 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.3.10 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.1.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.8.0 All 32 releases
core-framework / wp / Common / Traits / Requester.php

Requester.php in Core Framework trunk, at wp/Common/Traits/Requester.php

141 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CoreFramework
4 *
5 * @package CoreFramework
6 * @author Core Framework <hello@coreframework.com>
7 * @copyright 2023 Core Framework
8 * @license MIT
9 * @link https://coreframework.com
10 */
11
12 declare (strict_types = 1);
13
14 namespace CoreFramework\Common\Traits;
15
16 use CoreFramework\Common\Utils\Errors;
17 use CoreFramework\Config\Plugin;
18
19 /**
20 * The requester trait to determine what we request; used to determine
21 * which classes we instantiate in the Scaffold class
22 *
23 * @see Scaffold
24 *
25 * @package CoreFramework\Common\Traits
26 * @since 0.0.0
27 */
28 trait Requester {
29
30 /**
31 * Get the plugin data in static form
32 *
33 * @since 0.0.0
34 */
35 public static function getPluginData(): array {
36 return Plugin::init()->data();
37 }
38
39 /**
40 * What type of request is this?
41 *
42 * @param string $type admin, cron, cli, amp or frontend.
43 * @since 0.0.0
44 */
45 public function request( string $type ): bool {
46 switch ( $type ) {
47 case 'installing_wp':
48 return $this->isInstallingWp();
49 case 'frontend':
50 return $this->isFrontend();
51 case 'backend':
52 return $this->isAdminBackend();
53 case 'rest':
54 return $this->isRest();
55 case 'cron':
56 return $this->isCron();
57 case 'cli':
58 return $this->isCli();
59 case 'oxygen':
60 return CoreFrameworkOxygen()->is_oxygen();
61 case 'bricks':
62 return CoreFrameworkBricks()->is_bricks();
63 case 'gutenberg':
64 return CoreFrameworkGutenberg()->is_gutenberg() && ! CoreFrameworkOxygen()->is_oxygen() && ! CoreFrameworkBricks()->is_bricks();
65 default:
66 Errors::wpDie(
67 sprintf(
68 /* translators: %s: request function */
69 \__( 'Unknown request type: %s', 'core-framework' ),
70 $type
71 ),
72 \__( 'Classes are not being correctly requested', 'core-framework' ),
73 __FILE__
74 );
75 return false;
76 }
77 }
78
79 /**
80 * Is installing WP
81 *
82 * @since 0.0.0
83 */
84 public function isInstallingWp(): bool {
85 return defined( 'WP_INSTALLING' );
86 }
87
88 /**
89 * Is frontend
90 *
91 * @since 0.0.0
92 */
93 public function isFrontend(): bool {
94 return ! $this->isAdminBackend() && ! $this->isCron() && ! $this->isRest();
95 }
96
97 /**
98 * Is admin
99 *
100 * @since 0.0.0
101 */
102 public function isAdminBackend(): bool {
103 return \is_user_logged_in() && \is_admin();
104 }
105
106 /**
107 * Is rest
108 *
109 * @since 0.0.0
110 */
111 public function isRest(): bool {
112 // rest_url_prefix is a WordPress core filter, not a plugin-owned hook.
113 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
114 $rest_prefix = (string) apply_filters( 'rest_url_prefix', 'wp-json' );
115 $request_uri = isset( $_SERVER['REQUEST_URI'] )
116 ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) )
117 : '';
118 $is_plain_rest_request = 1 === preg_match( '/(?:\?|&)rest_route=/', $request_uri );
119
120 return false !== strpos( $request_uri, $rest_prefix ) || $is_plain_rest_request || defined( 'REST_REQUEST' );
121 }
122
123 /**
124 * Is cron
125 *
126 * @since 0.0.0
127 */
128 public function isCron(): bool {
129 return ( function_exists( 'wp_doing_cron' ) && \wp_doing_cron() ) || defined( 'DOING_CRON' );
130 }
131
132 /**
133 * Is cli
134 *
135 * @since 0.0.0
136 */
137 public function isCli(): bool {
138 return defined( 'WP_CLI' ) && \WP_CLI;
139 }
140 }
141