PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
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 / Admin / Menus / Edit_Menu.php

Edit_Menu.php in Code Snippets 4.0.0-beta.1, at php/Admin/Menus/Edit_Menu.php

252 lines 6.6 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\Admin\Menus;
4
5 use Code_Snippets\Admin\Contextual_Help;
6 use Code_Snippets\Model\Snippet;
7 use WP_Screen;
8 use function Code_Snippets\code_snippets;
9 use function Code_Snippets\get_all_snippet_tags;
10 use function Code_Snippets\get_snippet;
11 use function Code_Snippets\Settings\get_setting;
12 use function Code_Snippets\Settings\get_settings_values;
13 use function Code_Snippets\Utils\enqueue_code_editor;
14 use const Code_Snippets\PLUGIN_FILE;
15 use const Code_Snippets\PLUGIN_VERSION;
16
17 /**
18 * This class handles the add/edit menu.
19 */
20 class Edit_Menu extends Admin_Menu {
21
22 /**
23 * Handle for JavaScript asset file.
24 */
25 public const JS_HANDLE = 'code-snippets-edit-menu';
26
27 /**
28 * Handle for CSS asset file.
29 */
30 public const CSS_HANDLE = 'code-snippets-edit';
31
32 /**
33 * The snippet object currently being edited
34 *
35 * @var Snippet|null
36 * @see Edit_Menu::load_snippet_data()
37 */
38 protected ?Snippet $snippet = null;
39
40 /**
41 * Constructor.
42 *
43 * @return void
44 */
45 public function __construct() {
46 parent::__construct(
47 'edit',
48 _x( 'Edit Snippet', 'menu label', 'code-snippets' ),
49 __( 'Edit Snippet', 'code-snippets' )
50 );
51
52 add_action( 'current_screen', array( $this, 'maybe_hide_menu_item' ) );
53
54 $this->remove_debug_bar_codemirror();
55 }
56
57 /**
58 * Register the admin menu
59 *
60 * @return void
61 */
62 public function register() {
63 parent::register();
64
65 // Create New Snippet menu.
66 $this->add_menu(
67 code_snippets()->get_menu_slug( 'add' ),
68 _x( 'Add New', 'menu label', 'code-snippets' ),
69 __( 'Create New Snippet', 'code-snippets' )
70 );
71 }
72
73 /**
74 * Hide the static Edit Snippet menu item unless a specific snippet is being edited.
75 *
76 * @param WP_Screen $screen Current admin screen.
77 *
78 * @return void
79 */
80 public function maybe_hide_menu_item( WP_Screen $screen ) {
81 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin menu context.
82 $current_id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0;
83 $edit_hook = get_plugin_page_hookname( $this->slug, $this->base_slug );
84 $edit_hook .= $screen->in_admin( 'network' ) ? '-network' : '';
85
86 if ( ( $screen->id === $edit_hook || $screen->base === $edit_hook ) && 0 < $current_id ) {
87 return;
88 }
89
90 remove_submenu_page( $this->base_slug, $this->slug );
91 }
92
93 /**
94 * Executed when the menu is loaded.
95 *
96 * @return void
97 */
98 public function load() {
99 parent::load();
100
101 $this->load_snippet_data();
102 $this->ensure_correct_page();
103
104 $contextual_help = new Contextual_Help( 'edit' );
105 $contextual_help->load();
106 }
107
108 /**
109 * Disallow vising the Edit Snippet page without a valid ID.
110 *
111 * @return void
112 */
113 protected function ensure_correct_page() {
114 $screen = get_current_screen();
115 $edit_hook = get_plugin_page_hookname( $this->slug, $this->base_slug );
116 $edit_hook .= $screen->in_admin( 'network' ) ? '-network' : '';
117
118 // Disallow visiting the edit snippet page without a valid ID.
119 if (
120 $screen->base === $edit_hook
121 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
122 && ( empty( $_REQUEST['id'] ) || 0 === $this->snippet->id || null === $this->snippet->id )
123 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
124 && ! isset( $_REQUEST['preview'] )
125 ) {
126 wp_safe_redirect( code_snippets()->get_menu_url( 'add' ) );
127 exit;
128 }
129 }
130
131 /**
132 * Render the edit menu interface.
133 *
134 * @return void
135 */
136 public function render() {
137 echo '<div id="edit-snippet-container" class="wrap"></div>';
138 }
139
140 /**
141 * Load the data for the snippet currently being edited.
142 */
143 public function load_snippet_data() {
144 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
145 $edit_id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
146
147 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
148 $edit_type = isset( $_REQUEST['type'] ) ? sanitize_key( wp_unslash( $_REQUEST['type'] ) ) : '';
149
150 $this->snippet = get_snippet( $edit_id );
151
152 if ( 0 === $edit_id && $edit_type !== $this->snippet->type ) {
153 $default_scopes = [
154 'php' => 'global',
155 'css' => 'site-css',
156 'html' => 'content',
157 'js' => 'site-head-js',
158 'cond' => 'condition',
159 ];
160
161 if ( isset( $default_scopes[ $edit_type ] ) ) {
162 $this->snippet->scope = $default_scopes[ $edit_type ];
163 }
164 }
165
166 $this->snippet = apply_filters( 'code_snippets/admin/load_snippet_data', $this->snippet );
167 }
168
169 /**
170 * Enqueue assets for the edit menu
171 *
172 * @return void
173 */
174 public function enqueue_assets() {
175
176 $settings = get_settings_values();
177 $tags_enabled = $settings['general']['enable_tags'];
178 $desc_enabled = $settings['general']['enable_description'];
179
180 enqueue_code_editor( $this->snippet->type );
181
182 wp_enqueue_style(
183 self::CSS_HANDLE,
184 plugins_url( 'dist/edit.css', PLUGIN_FILE ),
185 [
186 'code-editor',
187 'wp-components',
188 ],
189 PLUGIN_VERSION
190 );
191
192 wp_enqueue_script(
193 self::JS_HANDLE,
194 plugins_url( 'dist/edit.js', PLUGIN_FILE ),
195 [ 'code-snippets-code-editor' ] + self::$script_deps,
196 PLUGIN_VERSION,
197 [ 'in_footer' => true ]
198 );
199
200 wp_set_script_translations( self::JS_HANDLE, 'code-snippets' );
201
202 if ( $desc_enabled ) {
203 remove_editor_styles();
204 wp_enqueue_editor();
205 }
206
207 code_snippets()->localize_script( self::JS_HANDLE );
208
209 wp_localize_script(
210 self::JS_HANDLE,
211 'CODE_SNIPPETS_EDIT',
212 [
213 'snippet' => $this->snippet->get_fields(),
214 'activateByDefault' => get_setting( 'general', 'activate_by_default' ),
215 'editorTheme' => get_setting( 'editor', 'theme' ),
216 'enableDownloads' => apply_filters( 'code_snippets/enable_downloads', true ),
217 'enableDescription' => $desc_enabled,
218 'tagOptions' => apply_filters(
219 'code_snippets/tag_editor_options',
220 [
221 'enabled' => $tags_enabled,
222 'allowSpaces' => true,
223 'availableTags' => $tags_enabled ? get_all_snippet_tags() : [],
224 ]
225 ),
226 'descEditorOptions' => [
227 'rows' => $settings['general']['visual_editor_rows'],
228 ],
229 ]
230 );
231 }
232
233 /**
234 * Remove the old CodeMirror version used by the Debug Bar Console plugin that is messing up the snippet editor.
235 */
236 public function remove_debug_bar_codemirror() {
237 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
238 $current_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
239
240 // Try to discern if we are on the single snippet page as good as we can at this early time.
241 $is_codemirror_page =
242 is_admin() && 'admin.php' === $GLOBALS['pagenow'] && $current_page && (
243 code_snippets()->get_menu_slug( 'edit' ) === $current_page ||
244 code_snippets()->get_menu_slug( 'settings' ) === $current_page
245 );
246
247 if ( $is_codemirror_page ) {
248 remove_action( 'debug_bar_enqueue_scripts', 'debug_bar_console_scripts' );
249 }
250 }
251 }
252