| 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 |
private const JS_HANDLE = 'code-snippets-edit-menu'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Handle for CSS asset file. |
| 29 |
*/ |
| 30 |
private 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 |
* Retrieve every hookname registered by this menu, including the separate |
| 75 |
* "Add New" page, so screen-based checks recognise both editor views. |
| 76 |
* |
| 77 |
* @return string[] |
| 78 |
*/ |
| 79 |
public function get_hooknames(): array { |
| 80 |
return [ |
| 81 |
$this->get_hookname(), |
| 82 |
get_plugin_page_hookname( code_snippets()->get_menu_slug( 'add' ), $this->base_slug ), |
| 83 |
]; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Hide the static Edit Snippet menu item unless a specific snippet is being edited. |
| 88 |
* |
| 89 |
* @param WP_Screen $screen Current admin screen. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function maybe_hide_menu_item( WP_Screen $screen ) { |
| 94 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin menu context. |
| 95 |
$current_id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0; |
| 96 |
$edit_hook = get_plugin_page_hookname( $this->slug, $this->base_slug ); |
| 97 |
$edit_hook .= $screen->in_admin( 'network' ) ? '-network' : ''; |
| 98 |
|
| 99 |
if ( ( $screen->id === $edit_hook || $screen->base === $edit_hook ) && 0 < $current_id ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
remove_submenu_page( $this->base_slug, $this->slug ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Executed when the menu is loaded. |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function load() { |
| 112 |
parent::load(); |
| 113 |
|
| 114 |
$this->load_snippet_data(); |
| 115 |
$this->ensure_correct_page(); |
| 116 |
|
| 117 |
$contextual_help = new Contextual_Help( 'edit' ); |
| 118 |
$contextual_help->load(); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Disallow vising the Edit Snippet page without a valid ID. |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
protected function ensure_correct_page() { |
| 127 |
$screen = get_current_screen(); |
| 128 |
$edit_hook = get_plugin_page_hookname( $this->slug, $this->base_slug ); |
| 129 |
$edit_hook .= $screen->in_admin( 'network' ) ? '-network' : ''; |
| 130 |
|
| 131 |
// Disallow visiting the edit snippet page without a valid ID. |
| 132 |
if ( |
| 133 |
$screen->base === $edit_hook |
| 134 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 135 |
&& ( empty( $_REQUEST['id'] ) || 0 === $this->snippet->id || null === $this->snippet->id ) |
| 136 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 137 |
&& ! isset( $_REQUEST['preview'] ) |
| 138 |
) { |
| 139 |
wp_safe_redirect( code_snippets()->get_menu_url( 'add' ) ); |
| 140 |
exit; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Render the edit menu interface. |
| 146 |
* |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
public function render() { |
| 150 |
echo '<div id="edit-snippet-container" class="wrap"></div>'; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Load the data for the snippet currently being edited. |
| 155 |
*/ |
| 156 |
public function load_snippet_data() { |
| 157 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 158 |
$edit_id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0; |
| 159 |
|
| 160 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 161 |
$edit_type = isset( $_REQUEST['type'] ) ? sanitize_key( wp_unslash( $_REQUEST['type'] ) ) : ''; |
| 162 |
|
| 163 |
$this->snippet = get_snippet( $edit_id ); |
| 164 |
|
| 165 |
if ( 0 === $edit_id && $edit_type !== $this->snippet->type ) { |
| 166 |
$default_scopes = [ |
| 167 |
'php' => 'global', |
| 168 |
'css' => 'site-css', |
| 169 |
'html' => 'content', |
| 170 |
'js' => 'site-head-js', |
| 171 |
'cond' => 'condition', |
| 172 |
]; |
| 173 |
|
| 174 |
if ( isset( $default_scopes[ $edit_type ] ) ) { |
| 175 |
$this->snippet->scope = $default_scopes[ $edit_type ]; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
$this->snippet = apply_filters( 'code_snippets/admin/load_snippet_data', $this->snippet ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Enqueue assets for the edit menu |
| 184 |
* |
| 185 |
* @return void |
| 186 |
*/ |
| 187 |
public function enqueue_assets() { |
| 188 |
|
| 189 |
$settings = get_settings_values(); |
| 190 |
$tags_enabled = $settings['general']['enable_tags']; |
| 191 |
$desc_enabled = $settings['general']['enable_description']; |
| 192 |
|
| 193 |
enqueue_code_editor( $this->snippet->type ); |
| 194 |
|
| 195 |
wp_enqueue_style( |
| 196 |
self::CSS_HANDLE, |
| 197 |
plugins_url( 'dist/edit.css', PLUGIN_FILE ), |
| 198 |
[ |
| 199 |
'code-editor', |
| 200 |
'wp-components', |
| 201 |
], |
| 202 |
PLUGIN_VERSION |
| 203 |
); |
| 204 |
|
| 205 |
wp_enqueue_script( |
| 206 |
self::JS_HANDLE, |
| 207 |
plugins_url( 'dist/edit.js', PLUGIN_FILE ), |
| 208 |
array_merge( [ 'code-snippets-code-editor' ], self::$script_deps ), |
| 209 |
PLUGIN_VERSION, |
| 210 |
[ 'in_footer' => true ] |
| 211 |
); |
| 212 |
|
| 213 |
wp_set_script_translations( self::JS_HANDLE, 'code-snippets' ); |
| 214 |
|
| 215 |
if ( $desc_enabled ) { |
| 216 |
remove_editor_styles(); |
| 217 |
wp_enqueue_editor(); |
| 218 |
} |
| 219 |
|
| 220 |
code_snippets()->localize_script( self::JS_HANDLE ); |
| 221 |
|
| 222 |
wp_localize_script( |
| 223 |
self::JS_HANDLE, |
| 224 |
'CODE_SNIPPETS_EDIT', |
| 225 |
[ |
| 226 |
'snippet' => $this->snippet->get_fields(), |
| 227 |
'activateByDefault' => get_setting( 'general', 'activate_by_default' ), |
| 228 |
'editorTheme' => get_setting( 'editor', 'theme' ), |
| 229 |
'enableDownloads' => apply_filters( 'code_snippets/enable_downloads', true ), |
| 230 |
'enableDescription' => $desc_enabled, |
| 231 |
'tagOptions' => apply_filters( |
| 232 |
'code_snippets/tag_editor_options', |
| 233 |
[ |
| 234 |
'enabled' => $tags_enabled, |
| 235 |
'allowSpaces' => true, |
| 236 |
'availableTags' => $tags_enabled ? get_all_snippet_tags() : [], |
| 237 |
] |
| 238 |
), |
| 239 |
'descEditorOptions' => [ |
| 240 |
'rows' => $settings['general']['visual_editor_rows'], |
| 241 |
], |
| 242 |
] |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Remove the old CodeMirror version used by the Debug Bar Console plugin that is messing up the snippet editor. |
| 248 |
*/ |
| 249 |
public function remove_debug_bar_codemirror() { |
| 250 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 251 |
$current_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 252 |
|
| 253 |
// Try to discern if we are on the single snippet page as good as we can at this early time. |
| 254 |
$is_codemirror_page = |
| 255 |
is_admin() && 'admin.php' === $GLOBALS['pagenow'] && $current_page && ( |
| 256 |
code_snippets()->get_menu_slug( 'edit' ) === $current_page || |
| 257 |
code_snippets()->get_menu_slug( 'settings' ) === $current_page |
| 258 |
); |
| 259 |
|
| 260 |
if ( $is_codemirror_page ) { |
| 261 |
remove_action( 'debug_bar_enqueue_scripts', 'debug_bar_console_scripts' ); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|