PluginProbe
Code Snippets / 3.9.6
Code Snippets v3.9.6
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 / class-edit-menu.php

class-edit-menu.php in Code Snippets 3.9.6, at php/admin-menus/class-edit-menu.php

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