PluginProbe
Code Snippets / 4.0.0-beta.2
Code Snippets v4.0.0-beta.2
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / Integration / Evaluate_Functions.php

Evaluate_Functions.php in Code Snippets 4.0.0-beta.2, at php/Integration/Evaluate_Functions.php

271 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets\Integration;
4
5 use Code_Snippets\Core\DB;
6 use Code_Snippets\Flat_Files\Snippet_Files;
7 use Code_Snippets\REST_API\Snippets\Snippets_REST_Controller;
8 use function Code_Snippets\clean_active_snippets_cache;
9 use function Code_Snippets\clean_snippets_cache;
10 use function Code_Snippets\code_snippets;
11 use function Code_Snippets\execute_snippet;
12 use function Code_Snippets\execute_snippet_from_flat_file;
13
14 /**
15 * Class for evaluating functions snippets.
16 *
17 * @package Code_Snippets
18 */
19 class Evaluate_Functions {
20
21 /**
22 * Database class.
23 *
24 * @var DB
25 */
26 private DB $db;
27
28 /**
29 * Class constructor.
30 *
31 * @param DB $db Database class instance.
32 */
33 public function __construct( DB $db ) {
34 $this->db = $db;
35 add_action( 'plugins_loaded', [ $this, 'evaluate_early' ], 1 );
36 add_filter( 'code_snippets/execute_snippets', [ $this, 'disable_snippet_execution' ], 5 );
37
38 // Only the query var is inspected here. This constructor runs while
39 // plugins are still being included, which is before WordPress loads
40 // pluggable.php, so a capability check at this point would call an
41 // undefined wp_get_current_user() and take the whole request down.
42 // The capability is checked in the callback instead, which never runs
43 // before URLs are being generated.
44 if ( $this->is_safe_mode_query_var_set() ) {
45 add_filter( 'home_url', [ $this, 'add_safe_mode_query_var' ] );
46 add_filter( 'admin_url', [ $this, 'add_safe_mode_query_var' ] );
47 }
48 }
49
50 /**
51 * Check whether the safe mode query var is present on this request.
52 *
53 * Safe to call at any point, as it reads nothing but the request.
54 *
55 * @return bool
56 */
57 public function is_safe_mode_query_var_set(): bool {
58 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
59 return ! empty( $_REQUEST['snippets-safe-mode'] );
60 }
61
62 /**
63 * Check if safe mode has been requested via query var.
64 *
65 * Performs a capability check, so must not be called before pluggable
66 * functions are available.
67 *
68 * @return bool
69 */
70 public function is_safe_mode_requested(): bool {
71 return $this->is_safe_mode_query_var_set() && code_snippets()->current_user_can();
72 }
73
74 /**
75 * Inject the safe mode query var into URLs
76 *
77 * @param mixed $url Original URL, from an unknown earlier callback.
78 *
79 * @return string Modified URL.
80 */
81 public function add_safe_mode_query_var( $url ): string {
82 $url = is_string( $url ) ? $url : '';
83
84 return $this->is_safe_mode_requested() ?
85 add_query_arg( 'snippets-safe-mode', true, $url ) :
86 $url;
87 }
88
89 /**
90 * Retrieve details about the currently edited snippet, if any.
91 *
92 * @return ?array{id: int, table: string}
93 */
94 private function get_currently_editing_snippet(): ?array {
95 if ( wp_is_json_request() && ! empty( $_SERVER['REQUEST_URI'] ) ) {
96 $url = wp_parse_url( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
97
98 if ( isset( $url['path'] ) && false !== strpos( $url['path'], Snippets_REST_Controller::get_prefixed_base_route() ) ) {
99 $path_parts = explode( '/', $url['path'] );
100 $edit_id = intval( end( $path_parts ) );
101
102 if ( ! empty( $url['query'] ) ) {
103 wp_parse_str( $url['query'], $path_params );
104 $edit_table = isset( $path_params['network'] ) && rest_sanitize_boolean( $path_params['network'] )
105 ? $this->db->ms_table
106 : $this->db->table;
107 }
108
109 return [
110 'id' => $edit_id,
111 'table' => $edit_table ?? $this->db->table,
112 ];
113 }
114 }
115
116 return null;
117 }
118
119 /**
120 * Check if the plugin is running in safe mode.
121 *
122 * @return bool
123 *
124 * @noinspection PhpUndefinedConstantInspection
125 */
126 public static function is_safe_mode_active(): bool {
127 return ( defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) ||
128 ! apply_filters( 'code_snippets/execute_snippets', true );
129 }
130
131 /**
132 * Disable snippet execution if the necessary query var is set.
133 *
134 * @param mixed $execute_snippets Current filter value, from an unknown
135 * earlier callback.
136 *
137 * @return bool New filter value.
138 */
139 public function disable_snippet_execution( $execute_snippets ): bool {
140 return $execute_snippets && ! $this->is_safe_mode_requested();
141 }
142
143 /**
144 * Quickly deactivate a snippet with minimal overhead.
145 *
146 * @param int $snippet_id ID of the snippet to deactivate.
147 * @param string $table_name Name of the table where the snippet is stored.
148 *
149 * @return void
150 */
151 private function quick_deactivate_snippet( int $snippet_id, string $table_name ) {
152 global $wpdb;
153
154 $active_shared_ids = get_option( 'active_shared_network_snippets', [] );
155 $active_shared_ids = is_array( $active_shared_ids )
156 ? array_map( 'intval', $active_shared_ids )
157 : [];
158
159 if ( $table_name === $this->db->ms_table && in_array( $snippet_id, $active_shared_ids, true ) ) {
160 unset( $active_shared_ids[ array_search( $snippet_id, $active_shared_ids, true ) ] );
161 $active_shared_ids = array_values( $active_shared_ids );
162 update_option( 'active_shared_network_snippets', $active_shared_ids );
163 clean_active_snippets_cache( $table_name );
164 } else {
165 $wpdb->update(
166 $table_name,
167 [ 'active' => '0' ],
168 [ 'id' => $snippet_id ],
169 [ '%d' ],
170 [ '%d' ]
171 );
172 clean_snippets_cache( $table_name );
173
174 $network = $table_name === $this->db->ms_table;
175 do_action( 'code_snippets/deactivate_snippet', $snippet_id, $network );
176 }
177 }
178
179 /**
180 * Evaluate a snippet.
181 *
182 * @param array $snippet Snippet data.
183 * @param string|null $file_path Path to the snippet file, or null if executing from the database.
184 * @param array|null $edit_snippet Data of snippet currently being edited, if applicable.
185 *
186 * @return void
187 */
188 private function evaluate_snippet( array $snippet, ?string $file_path, ?array $edit_snippet ) {
189 $snippet_id = $snippet['id'];
190 $code = $snippet['code'];
191 $table_name = $snippet['table'];
192
193 // If the snippet is a single-use snippet, deactivate it before execution to ensure that the process always happens.
194 if ( 'single-use' === $snippet['scope'] ) {
195 $this->quick_deactivate_snippet( $snippet_id, $table_name );
196 }
197
198 if ( ! is_null( $edit_snippet ) && $edit_snippet['id'] === $snippet_id && $edit_snippet['table'] === $table_name ) {
199 return;
200 }
201
202 if ( apply_filters( 'code_snippets/allow_execute_snippet', true, $snippet_id, $table_name ) ) {
203 if ( is_null( $file_path ) ) {
204 execute_snippet( $code, $snippet_id );
205 } else {
206 execute_snippet_from_flat_file( $code, $file_path, $snippet_id );
207 }
208 }
209 }
210
211 /**
212 * Evaluate applicable active snippets as early as possible.
213 *
214 * @return bool True if snippets were evaluated, false if safe mode is active.
215 */
216 public function evaluate_early(): bool {
217 if ( self::is_safe_mode_active() ) {
218 return false;
219 }
220
221 if ( Snippet_Files::is_active() ) {
222 return $this->evaluate_file_snippets();
223 }
224
225 return $this->evaluate_db_snippets();
226 }
227
228 /**
229 * Evaluate active snippets stored in the database.
230 *
231 * @return bool True on completion.
232 */
233 public function evaluate_db_snippets(): bool {
234 $scopes = [ 'global', 'single-use', is_admin() ? 'admin' : 'front-end' ];
235 $active_snippets = $this->db->fetch_active_snippets( $scopes );
236 $edit_snippet = $this->get_currently_editing_snippet();
237
238 foreach ( $active_snippets as $snippet ) {
239 if ( 'condition' !== $snippet['scope'] ) {
240 $this->evaluate_snippet( $snippet, null, $edit_snippet );
241 }
242 }
243
244 return true;
245 }
246
247 /**
248 * Evaluate active snippets stored in flat files.
249 *
250 * @return bool True on completion.
251 */
252 private function evaluate_file_snippets(): bool {
253 $type = 'php';
254 $scopes = [ 'global', 'single-use', is_admin() ? 'admin' : 'front-end' ];
255 $snippets = Snippet_Files::get_active_snippets_from_flat_files( $scopes, $type );
256 $edit_snippet = $this->get_currently_editing_snippet();
257
258 foreach ( $snippets as $snippet ) {
259 if ( 'condition' !== $snippet['scope'] ) {
260 $table_name = Snippet_Files::get_hashed_table_name( $snippet['table'] );
261 $base_path = Snippet_Files::get_base_dir( $table_name, $type );
262 $file = $base_path . '/' . $snippet['id'] . '.' . $type;
263
264 $this->evaluate_snippet( $snippet, $file, $edit_snippet );
265 }
266 }
267
268 return true;
269 }
270 }
271