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