Skip to content

Commit 17f6308

Browse files
committed
Ensure that snippets currently being saved are not executed twice.
1 parent c931d92 commit 17f6308

File tree

3 files changed

+28
-35
lines changed

3 files changed

+28
-35
lines changed

Diff for: php/class-admin.php

-34
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ public function run() {
5656
add_filter( 'plugin_row_meta', array( $this, 'plugin_meta_links' ), 10, 2 );
5757
add_filter( 'debug_information', array( $this, 'debug_information' ) );
5858
add_action( 'code_snippets/admin/manage', array( $this, 'print_notices' ) );
59-
60-
if ( ! empty( $_POST['save_snippet'] ) ) {
61-
add_action( 'code_snippets/allow_execute_snippet', array( $this, 'prevent_exec_on_save' ), 10, 3 );
62-
}
6359
}
6460

6561
/**
@@ -82,36 +78,6 @@ public function mu_menu_items( array $menu_items ): array {
8278
return $menu_items;
8379
}
8480

85-
/**
86-
* Prevent the snippet currently being saved from being executed
87-
* so that it is not run twice (once normally, once when validated)
88-
*
89-
* @param bool $exec Whether the snippet will be executed.
90-
* @param int $exec_id ID of the snippet being executed.
91-
* @param string $table_name Name of the database table the snippet is stored in.
92-
*
93-
* @return bool Whether the snippet will be executed.
94-
*/
95-
public function prevent_exec_on_save( bool $exec, int $exec_id, string $table_name ): bool {
96-
97-
// TODO: make this work for AJAX method.
98-
if ( ! isset( $_POST['save_snippet'], $_POST['snippet_id'] ) ) {
99-
return $exec;
100-
}
101-
102-
if ( code_snippets()->db->get_table_name() !== $table_name ) {
103-
return $exec;
104-
}
105-
106-
$id = intval( $_POST['snippet_id'] );
107-
108-
if ( $id === $exec_id ) {
109-
return false;
110-
}
111-
112-
return $exec;
113-
}
114-
11581
/**
11682
* Adds a link pointing to the Manage Snippets page
11783
*

Diff for: php/rest-api/class-snippets-rest-controller.php

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ public static function get_base_route(): string {
5959
return REST_API_NAMESPACE . self::VERSION . '/' . self::BASE_ROUTE;
6060
}
6161

62+
/**
63+
* Retrieve the full base route including the REST API prefix.
64+
*
65+
* @return string
66+
*/
67+
public static function get_prefixed_base_route(): string {
68+
return '/' . rtrim( rest_get_url_prefix(), '/\\' ) . '/' . self::get_base_route();
69+
}
70+
6271
/**
6372
* Register REST routes.
6473
*/

Diff for: php/snippet-ops.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Code_Snippets;
99

10+
use Code_Snippets\REST_API\Snippets_REST_Controller;
1011
use ParseError;
1112

1213
/**
@@ -610,6 +611,22 @@ function execute_active_snippets(): bool {
610611
$scopes = array( 'global', 'single-use', is_admin() ? 'admin' : 'front-end' );
611612
$data = $db->fetch_active_snippets( $scopes );
612613

614+
// Detect if a snippet is currently being edited, and if so, spare it from execution.
615+
$edit_id = 0;
616+
$edit_table = '';
617+
618+
if ( wp_is_json_request() && ! empty( $_SERVER['REQUEST_URI'] ) ) {
619+
$url = wp_parse_url( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
620+
621+
if ( false !== strpos( $url['path'], Snippets_REST_Controller::get_prefixed_base_route() ) ) {
622+
$path_parts = explode( '/', $url['path'] );
623+
wp_parse_str( $url['query'], $path_params );
624+
$edit_id = intval( end( $path_parts ) );
625+
$edit_table = isset( $path_params['network'] ) && rest_sanitize_boolean( $path_params['network'] ) ?
626+
$db->ms_table : $db->table;
627+
}
628+
}
629+
613630
foreach ( $data as $table_name => $active_snippets ) {
614631

615632
// Loop through the returned snippets and execute the PHP code.
@@ -638,7 +655,8 @@ function execute_active_snippets(): bool {
638655
}
639656
}
640657

641-
if ( apply_filters( 'code_snippets/allow_execute_snippet', true, $snippet_id, $table_name ) ) {
658+
if ( apply_filters( 'code_snippets/allow_execute_snippet', true, $snippet_id, $table_name ) &&
659+
! ( $edit_id === $snippet_id && $table_name === $edit_table ) ) {
642660
execute_snippet( $code, $snippet_id );
643661
}
644662
}

0 commit comments

Comments
 (0)