PluginProbe
Code Snippets / 3.8.1
Code Snippets v3.8.1
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 3.0.1 All 64 releases
code-snippets / php / admin-menus / class-edit-menu.php

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

239 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 ( $screen->base === $edit_hook && ( empty( $_REQUEST['id'] ) || 0 === $this->snippet->id || null === $this->snippet->id ) &&
101 ! isset( $_REQUEST['preview'] ) ) {
102 wp_safe_redirect( code_snippets()->get_menu_url( 'add' ) );
103 exit;
104 }
105 }
106
107 /**
108 * Render the edit menu interface.
109 *
110 * @return void
111 */
112 public function render() {
113 printf(
114 '<div id="edit-snippet-form-container">%s</div>',
115 esc_html__( 'Loading edit page…', 'code-snippets' )
116 );
117 }
118
119 /**
120 * Load the data for the snippet currently being edited.
121 */
122 public function load_snippet_data() {
123 $edit_id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
124
125 $this->snippet = get_snippet( $edit_id );
126
127 if ( 0 === $edit_id && isset( $_GET['type'] ) && sanitize_key( $_GET['type'] ) !== $this->snippet->type ) {
128 $type = sanitize_key( $_GET['type'] );
129
130 $default_scopes = [
131 'php' => 'global',
132 'css' => 'site-css',
133 'html' => 'content',
134 'js' => 'site-head-js',
135 'cond' => 'condition',
136 ];
137
138 if ( isset( $default_scopes[ $type ] ) ) {
139 $this->snippet->scope = $default_scopes[ $type ];
140 }
141 }
142
143 $this->snippet = apply_filters( 'code_snippets/admin/load_snippet_data', $this->snippet );
144 }
145
146 /**
147 * Enqueue assets for the edit menu
148 *
149 * @return void
150 */
151 public function enqueue_assets() {
152 $plugin = code_snippets();
153 $rtl = is_rtl() ? '-rtl' : '';
154
155 $settings = Settings\get_settings_values();
156 $tags_enabled = $settings['general']['enable_tags'];
157 $desc_enabled = $settings['general']['enable_description'];
158
159 enqueue_code_editor( $this->snippet->type );
160
161 wp_enqueue_style(
162 self::CSS_HANDLE,
163 plugins_url( "dist/edit$rtl.css", $plugin->file ),
164 [
165 'code-editor',
166 'wp-components',
167 ],
168 $plugin->version
169 );
170
171 wp_enqueue_script(
172 self::JS_HANDLE,
173 plugins_url( 'dist/edit.js', $plugin->file ),
174 [
175 'code-snippets-code-editor',
176 'react',
177 'react-dom',
178 'wp-url',
179 'wp-i18n',
180 'wp-element',
181 'wp-components',
182 ],
183 $plugin->version,
184 true
185 );
186
187 wp_set_script_translations( self::JS_HANDLE, 'code-snippets' );
188
189 if ( $desc_enabled ) {
190 remove_editor_styles();
191 wp_enqueue_editor();
192 }
193
194 $plugin->localize_script( self::JS_HANDLE );
195
196 wp_localize_script(
197 self::JS_HANDLE,
198 'CODE_SNIPPETS_EDIT',
199 [
200 'snippet' => $this->snippet->get_fields(),
201 'pageTitleActions' => $plugin->is_compact_menu() ? $this->page_title_action_links( [ 'manage', 'import', 'settings' ] ) : [],
202 'isPreview' => isset( $_REQUEST['preview'] ),
203 'activateByDefault' => get_setting( 'general', 'activate_by_default' ),
204 'editorTheme' => get_setting( 'editor', 'theme' ),
205 'enableDownloads' => apply_filters( 'code_snippets/enable_downloads', true ),
206 'enableDescription' => $desc_enabled,
207 'hideUpsell' => get_setting( 'general', 'hide_upgrade_menu' ),
208 'tagOptions' => apply_filters(
209 'code_snippets/tag_editor_options',
210 [
211 'enabled' => $tags_enabled,
212 'allowSpaces' => true,
213 'availableTags' => $tags_enabled ? get_all_snippet_tags() : [],
214 ]
215 ),
216 'descEditorOptions' => [
217 'rows' => $settings['general']['visual_editor_rows'],
218 ],
219 ]
220 );
221 }
222
223 /**
224 * Remove the old CodeMirror version used by the Debug Bar Console plugin that is messing up the snippet editor.
225 */
226 public function remove_debug_bar_codemirror() {
227 // Try to discern if we are on the single snippet page as good as we can at this early time.
228 $is_codemirror_page =
229 is_admin() && 'admin.php' === $GLOBALS['pagenow'] && isset( $_GET['page'] ) && (
230 code_snippets()->get_menu_slug( 'edit' ) === $_GET['page'] ||
231 code_snippets()->get_menu_slug( 'settings' ) === $_GET['page']
232 );
233
234 if ( $is_codemirror_page ) {
235 remove_action( 'debug_bar_enqueue_scripts', 'debug_bar_console_scripts' );
236 }
237 }
238 }
239