Initial commit: WordPress wp-content (themes, plugins, languages)
- Theme: momentry (custom theme with REST API routes) - Plugins: code-snippets (contains all API proxies) - Languages: zh_TW translations - Excludes: cache, backups, uploads, logs
This commit is contained in:
207
plugins/code-snippets/php/admin-menus/class-admin-menu.php
Normal file
207
plugins/code-snippets/php/admin-menus/class-admin-menu.php
Normal file
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
namespace Code_Snippets;
|
||||
|
||||
/**
|
||||
* Base class for a plugin admin menu.
|
||||
*/
|
||||
abstract class Admin_Menu {
|
||||
|
||||
/**
|
||||
* The snippet page short name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public string $name;
|
||||
|
||||
/**
|
||||
* The label shown in the admin menu.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public string $label;
|
||||
|
||||
/**
|
||||
* The text used for the page title.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public string $title;
|
||||
|
||||
/**
|
||||
* The base slug for the top-level admin menu.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected string $base_slug;
|
||||
|
||||
/**
|
||||
* The slug for this admin menu.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected string $slug;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $name The snippet page short name.
|
||||
* @param string $label The label shown in the admin menu.
|
||||
* @param string $title The text used for the page title.
|
||||
*/
|
||||
public function __construct( string $name, string $label, string $title ) {
|
||||
$this->name = $name;
|
||||
$this->label = $label;
|
||||
$this->title = $title;
|
||||
|
||||
$this->base_slug = code_snippets()->get_menu_slug();
|
||||
$this->slug = code_snippets()->get_menu_slug( $name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register action and filter hooks.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run() {
|
||||
if ( ! code_snippets()->is_compact_menu() ) {
|
||||
add_action( 'admin_menu', array( $this, 'register' ) );
|
||||
add_action( 'network_admin_menu', array( $this, 'register' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a sub-menu to the Snippets menu.
|
||||
*
|
||||
* @param string $slug Menu slug.
|
||||
* @param string $label Label shown in admin menu.
|
||||
* @param string $title Page title.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add_menu( string $slug, string $label, string $title ) {
|
||||
$hook = add_submenu_page(
|
||||
$this->base_slug,
|
||||
$title,
|
||||
$label,
|
||||
code_snippets()->get_cap(),
|
||||
$slug,
|
||||
array( $this, 'render' )
|
||||
);
|
||||
|
||||
add_action( 'load-' . $hook, array( $this, 'load' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the admin menu
|
||||
*/
|
||||
public function register() {
|
||||
$this->add_menu( $this->slug, $this->label, $this->title );
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the content of a vew template
|
||||
*
|
||||
* @param string $name Name of view template to render.
|
||||
*/
|
||||
protected function render_view( string $name ) {
|
||||
include dirname( PLUGIN_FILE ) . '/php/views/' . $name . '.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the menu
|
||||
*/
|
||||
public function render() {
|
||||
$this->render_view( $this->name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the status and error messages
|
||||
*/
|
||||
protected function print_messages() {
|
||||
// None required by default.
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed when the admin page is loaded
|
||||
*/
|
||||
public function load() {
|
||||
// Make sure the user has permission to be here.
|
||||
if ( ! current_user_can( code_snippets()->get_cap() ) ) {
|
||||
wp_die( esc_html__( 'You are not authorized to access this page.', 'code-snippets' ) );
|
||||
}
|
||||
|
||||
// Create the snippet tables if they are missing.
|
||||
$db = code_snippets()->db;
|
||||
|
||||
if ( is_multisite() ) {
|
||||
$db->create_missing_table( $db->ms_table );
|
||||
}
|
||||
$db->create_missing_table( $db->table );
|
||||
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts and stylesheets for the admin page, if necessary
|
||||
*/
|
||||
abstract public function enqueue_assets();
|
||||
|
||||
/**
|
||||
* Generate a list of page title links for passing to React.
|
||||
*
|
||||
* @param array<string> $actions List of actions to convert into links, as array values.
|
||||
*
|
||||
* @return array<string, string> Link labels keyed to link URLs.
|
||||
*/
|
||||
public function page_title_action_links( array $actions ): array {
|
||||
$plugin = code_snippets();
|
||||
$links = [];
|
||||
|
||||
foreach ( $actions as $action ) {
|
||||
if ( 'settings' === $action && ! isset( $plugin->admin->menus['settings'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$url = $plugin->get_menu_url( $action );
|
||||
|
||||
if ( isset( $_GET['type'] ) && in_array( $_GET['type'], Snippet::get_types(), true ) ) {
|
||||
$url = add_query_arg( 'type', sanitize_key( wp_unslash( $_GET['type'] ) ), $url );
|
||||
}
|
||||
|
||||
switch ( $action ) {
|
||||
case 'manage':
|
||||
$label = _x( 'Manage', 'snippets', 'code-snippets' );
|
||||
break;
|
||||
case 'add':
|
||||
$label = _x( 'Add New', 'snippet', 'code-snippets' );
|
||||
break;
|
||||
case 'import':
|
||||
$label = _x( 'Import', 'snippets', 'code-snippets' );
|
||||
break;
|
||||
case 'settings':
|
||||
$label = _x( 'Settings', 'snippets', 'code-snippets' );
|
||||
break;
|
||||
default:
|
||||
$label = '';
|
||||
}
|
||||
|
||||
if ( $label && $url ) {
|
||||
$links[ $label ] = $url;
|
||||
}
|
||||
}
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a list of links to other pages in the page title
|
||||
*
|
||||
* @param array<string> $actions List of actions to render as links, as array values.
|
||||
*/
|
||||
public function render_page_title_actions( array $actions ) {
|
||||
foreach ( $this->page_title_action_links( $actions ) as $label => $url ) {
|
||||
printf( '<a href="%s" class="page-title-action">%s</a>', esc_url( $url ), esc_html( $label ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
240
plugins/code-snippets/php/admin-menus/class-edit-menu.php
Normal file
240
plugins/code-snippets/php/admin-menus/class-edit-menu.php
Normal file
@@ -0,0 +1,240 @@
|
||||
<?php
|
||||
|
||||
namespace Code_Snippets;
|
||||
|
||||
use function Code_Snippets\Settings\get_setting;
|
||||
|
||||
/**
|
||||
* This class handles the add/edit menu.
|
||||
*/
|
||||
class Edit_Menu extends Admin_Menu {
|
||||
|
||||
/**
|
||||
* Handle for JavaScript asset file.
|
||||
*/
|
||||
public const JS_HANDLE = 'code-snippets-edit-menu';
|
||||
|
||||
/**
|
||||
* Handle for CSS asset file.
|
||||
*/
|
||||
public const CSS_HANDLE = 'code-snippets-edit';
|
||||
|
||||
/**
|
||||
* The snippet object currently being edited
|
||||
*
|
||||
* @var Snippet|null
|
||||
* @see Edit_Menu::load_snippet_data()
|
||||
*/
|
||||
protected ?Snippet $snippet = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'edit',
|
||||
_x( 'Edit Snippet', 'menu label', 'code-snippets' ),
|
||||
__( 'Edit Snippet', 'code-snippets' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register action and filter hooks.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run() {
|
||||
parent::run();
|
||||
$this->remove_debug_bar_codemirror();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the admin menu
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register() {
|
||||
parent::register();
|
||||
|
||||
// Only preserve the edit menu if we are currently editing a snippet.
|
||||
if ( ! isset( $_REQUEST['page'] ) || $_REQUEST['page'] !== $this->slug ) {
|
||||
remove_submenu_page( $this->base_slug, $this->slug );
|
||||
}
|
||||
|
||||
// Add New Snippet menu.
|
||||
$this->add_menu(
|
||||
code_snippets()->get_menu_slug( 'add' ),
|
||||
_x( 'Add New', 'menu label', 'code-snippets' ),
|
||||
__( 'Create New Snippet', 'code-snippets' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed when the menu is loaded.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load() {
|
||||
parent::load();
|
||||
|
||||
$this->load_snippet_data();
|
||||
$this->ensure_correct_page();
|
||||
|
||||
$contextual_help = new Contextual_Help( 'edit' );
|
||||
$contextual_help->load();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disallow vising the Edit Snippet page without a valid ID.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function ensure_correct_page() {
|
||||
$screen = get_current_screen();
|
||||
$edit_hook = get_plugin_page_hookname( $this->slug, $this->base_slug );
|
||||
$edit_hook .= $screen->in_admin( 'network' ) ? '-network' : '';
|
||||
|
||||
// Disallow visiting the edit snippet page without a valid ID.
|
||||
if (
|
||||
$screen->base === $edit_hook
|
||||
&& ( empty( $_REQUEST['id'] ) || 0 === $this->snippet->id || null === $this->snippet->id )
|
||||
&& ! isset( $_REQUEST['preview'] )
|
||||
) {
|
||||
wp_safe_redirect( code_snippets()->get_menu_url( 'add' ) );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the edit menu interface.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function render() {
|
||||
printf(
|
||||
'<div id="edit-snippet-form-container"><small style="position: relative; top: 14px;">%s</small></div>',
|
||||
esc_html__( 'Loading edit page…', 'code-snippets' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the data for the snippet currently being edited.
|
||||
*/
|
||||
public function load_snippet_data() {
|
||||
$edit_id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
|
||||
|
||||
$this->snippet = get_snippet( $edit_id );
|
||||
|
||||
if ( 0 === $edit_id && isset( $_GET['type'] ) && sanitize_key( $_GET['type'] ) !== $this->snippet->type ) {
|
||||
$type = sanitize_key( $_GET['type'] );
|
||||
|
||||
$default_scopes = [
|
||||
'php' => 'global',
|
||||
'css' => 'site-css',
|
||||
'html' => 'content',
|
||||
'js' => 'site-head-js',
|
||||
'cond' => 'condition',
|
||||
];
|
||||
|
||||
if ( isset( $default_scopes[ $type ] ) ) {
|
||||
$this->snippet->scope = $default_scopes[ $type ];
|
||||
}
|
||||
}
|
||||
|
||||
$this->snippet = apply_filters( 'code_snippets/admin/load_snippet_data', $this->snippet );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue assets for the edit menu
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue_assets() {
|
||||
$plugin = code_snippets();
|
||||
|
||||
$settings = Settings\get_settings_values();
|
||||
$tags_enabled = $settings['general']['enable_tags'];
|
||||
$desc_enabled = $settings['general']['enable_description'];
|
||||
|
||||
enqueue_code_editor( $this->snippet->type );
|
||||
|
||||
wp_enqueue_style(
|
||||
self::CSS_HANDLE,
|
||||
plugins_url( 'dist/edit.css', $plugin->file ),
|
||||
[
|
||||
'code-editor',
|
||||
'wp-components',
|
||||
],
|
||||
$plugin->version
|
||||
);
|
||||
|
||||
wp_enqueue_script(
|
||||
self::JS_HANDLE,
|
||||
plugins_url( 'dist/edit.js', $plugin->file ),
|
||||
[
|
||||
'code-snippets-code-editor',
|
||||
'react',
|
||||
'react-dom',
|
||||
'wp-url',
|
||||
'wp-i18n',
|
||||
'wp-element',
|
||||
'wp-components',
|
||||
],
|
||||
$plugin->version,
|
||||
true
|
||||
);
|
||||
|
||||
wp_set_script_translations( self::JS_HANDLE, 'code-snippets' );
|
||||
|
||||
if ( $desc_enabled ) {
|
||||
remove_editor_styles();
|
||||
wp_enqueue_editor();
|
||||
}
|
||||
|
||||
$plugin->localize_script( self::JS_HANDLE );
|
||||
|
||||
wp_localize_script(
|
||||
self::JS_HANDLE,
|
||||
'CODE_SNIPPETS_EDIT',
|
||||
[
|
||||
'snippet' => $this->snippet->get_fields(),
|
||||
'pageTitleActions' => $plugin->is_compact_menu() ? $this->page_title_action_links( [ 'manage', 'import', 'settings' ] ) : [],
|
||||
'isPreview' => isset( $_REQUEST['preview'] ),
|
||||
'activateByDefault' => get_setting( 'general', 'activate_by_default' ),
|
||||
'editorTheme' => get_setting( 'editor', 'theme' ),
|
||||
'enableDownloads' => apply_filters( 'code_snippets/enable_downloads', true ),
|
||||
'enableDescription' => $desc_enabled,
|
||||
'hideUpsell' => get_setting( 'general', 'hide_upgrade_menu' ),
|
||||
'tagOptions' => apply_filters(
|
||||
'code_snippets/tag_editor_options',
|
||||
[
|
||||
'enabled' => $tags_enabled,
|
||||
'allowSpaces' => true,
|
||||
'availableTags' => $tags_enabled ? get_all_snippet_tags() : [],
|
||||
]
|
||||
),
|
||||
'descEditorOptions' => [
|
||||
'rows' => $settings['general']['visual_editor_rows'],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the old CodeMirror version used by the Debug Bar Console plugin that is messing up the snippet editor.
|
||||
*/
|
||||
public function remove_debug_bar_codemirror() {
|
||||
// Try to discern if we are on the single snippet page as good as we can at this early time.
|
||||
$is_codemirror_page =
|
||||
is_admin() && 'admin.php' === $GLOBALS['pagenow'] && isset( $_GET['page'] ) && (
|
||||
code_snippets()->get_menu_slug( 'edit' ) === $_GET['page'] ||
|
||||
code_snippets()->get_menu_slug( 'settings' ) === $_GET['page']
|
||||
);
|
||||
|
||||
if ( $is_codemirror_page ) {
|
||||
remove_action( 'debug_bar_enqueue_scripts', 'debug_bar_console_scripts' );
|
||||
}
|
||||
}
|
||||
}
|
||||
85
plugins/code-snippets/php/admin-menus/class-import-menu.php
Normal file
85
plugins/code-snippets/php/admin-menus/class-import-menu.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Code_Snippets;
|
||||
|
||||
/**
|
||||
* This class handles the import admin menu.
|
||||
*
|
||||
* @since 2.4.0
|
||||
* @package Code_Snippets
|
||||
*/
|
||||
class Import_Menu extends Admin_Menu {
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'import',
|
||||
_x( 'Import', 'menu label', 'code-snippets' ),
|
||||
__( 'Import Snippets', 'code-snippets' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register action and filter hooks
|
||||
*/
|
||||
public function run() {
|
||||
parent::run();
|
||||
add_action( 'admin_init', array( $this, 'register_importer' ) );
|
||||
add_action( 'load-importer-code-snippets', array( $this, 'load' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed when the menu is loaded
|
||||
*/
|
||||
public function load() {
|
||||
parent::load();
|
||||
|
||||
$contextual_help = new Contextual_Help( 'import' );
|
||||
$contextual_help->load();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the importer to the Tools > Import menu
|
||||
*/
|
||||
public function register_importer() {
|
||||
|
||||
/* Only register the importer if the current user can manage snippets */
|
||||
if ( ! defined( 'WP_LOAD_IMPORTERS' ) || ! code_snippets()->current_user_can() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Register the Code Snippets importer with WordPress */
|
||||
register_importer(
|
||||
'code-snippets',
|
||||
__( 'Code Snippets', 'code-snippets' ),
|
||||
__( 'Import snippets from a code snippets export file', 'code-snippets' ),
|
||||
array( $this, 'render' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue assets for the import menu.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue_assets() {
|
||||
$plugin = code_snippets();
|
||||
|
||||
wp_enqueue_script(
|
||||
'code-snippets-import',
|
||||
plugins_url( 'dist/import.js', $plugin->file ),
|
||||
[
|
||||
'react',
|
||||
'react-dom',
|
||||
'wp-i18n',
|
||||
'wp-components',
|
||||
],
|
||||
$plugin->version,
|
||||
true
|
||||
);
|
||||
|
||||
$plugin->localize_script( 'code-snippets-import' );
|
||||
}
|
||||
}
|
||||
337
plugins/code-snippets/php/admin-menus/class-manage-menu.php
Normal file
337
plugins/code-snippets/php/admin-menus/class-manage-menu.php
Normal file
@@ -0,0 +1,337 @@
|
||||
<?php
|
||||
|
||||
namespace Code_Snippets;
|
||||
|
||||
use Code_Snippets\Cloud\Cloud_Search_List_Table;
|
||||
use function Code_Snippets\Settings\get_setting;
|
||||
|
||||
/**
|
||||
* This class handles the manage snippets menu
|
||||
*
|
||||
* @since 2.4.0
|
||||
* @package Code_Snippets
|
||||
*/
|
||||
class Manage_Menu extends Admin_Menu {
|
||||
|
||||
/**
|
||||
* Instance of the list table class.
|
||||
*
|
||||
* @var List_Table
|
||||
*/
|
||||
public List_Table $list_table;
|
||||
|
||||
/**
|
||||
* Instance of the cloud list table class for search results.
|
||||
*
|
||||
* @var Cloud_Search_List_Table
|
||||
*/
|
||||
public Cloud_Search_List_Table $cloud_search_list_table;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'manage',
|
||||
_x( 'All Snippets', 'menu label', 'code-snippets' ),
|
||||
__( 'Snippets', 'code-snippets' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register action and filter hooks
|
||||
*/
|
||||
public function run() {
|
||||
parent::run();
|
||||
|
||||
if ( code_snippets()->is_compact_menu() ) {
|
||||
add_action( 'admin_menu', array( $this, 'register_compact_menu' ), 2 );
|
||||
add_action( 'network_admin_menu', array( $this, 'register_compact_menu' ), 2 );
|
||||
}
|
||||
|
||||
add_action( 'admin_menu', array( $this, 'register_upgrade_menu' ), 500 );
|
||||
add_filter( 'set-screen-option', array( $this, 'save_screen_option' ), 10, 3 );
|
||||
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_menu_css' ] );
|
||||
add_action( 'wp_ajax_update_code_snippet', array( $this, 'ajax_callback' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the top-level 'Snippets' menu and associated 'Manage' subpage
|
||||
*/
|
||||
public function register() {
|
||||
add_menu_page(
|
||||
__( 'Snippets', 'code-snippets' ),
|
||||
_x( 'Snippets', 'top-level menu label', 'code-snippets' ),
|
||||
code_snippets()->get_cap(),
|
||||
code_snippets()->get_menu_slug(),
|
||||
array( $this, 'render' ),
|
||||
'none', // Added through CSS as a mask to prevent loading 'blinking'.
|
||||
apply_filters( 'code_snippets/admin/menu_position', is_network_admin() ? 21 : 67 )
|
||||
);
|
||||
|
||||
// Register the sub-menu.
|
||||
parent::register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the 'upgrade' menu item.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_upgrade_menu() {
|
||||
if ( code_snippets()->licensing->is_licensed() || get_setting( 'general', 'hide_upgrade_menu' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$menu_title = sprintf(
|
||||
'<span class="button button-primary code-snippets-upgrade-button">%s %s</span>',
|
||||
_x( 'Go Pro', 'top-level menu label', 'code-snippets' ),
|
||||
'<span class="dashicons dashicons-external"></span>'
|
||||
);
|
||||
|
||||
$hook = add_submenu_page(
|
||||
code_snippets()->get_menu_slug(),
|
||||
__( 'Upgrade to Pro', 'code-snippets' ),
|
||||
$menu_title,
|
||||
code_snippets()->get_cap(),
|
||||
'code_snippets_upgrade',
|
||||
'__return_empty_string',
|
||||
100
|
||||
);
|
||||
|
||||
add_action( "load-$hook", [ $this, 'load_upgrade_menu' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Print CSS required for the admin menu icon.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue_menu_css() {
|
||||
wp_enqueue_style(
|
||||
'code-snippets-menu',
|
||||
plugins_url( 'dist/menu.css', PLUGIN_FILE ),
|
||||
[],
|
||||
PLUGIN_VERSION
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect the user upon opening the upgrade menu.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_upgrade_menu() {
|
||||
wp_safe_redirect( 'https://snipco.de/JE2f' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add menu pages for the compact menu
|
||||
*/
|
||||
public function register_compact_menu() {
|
||||
|
||||
if ( ! code_snippets()->is_compact_menu() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sub = code_snippets()->get_menu_slug( isset( $_GET['sub'] ) ? sanitize_key( $_GET['sub'] ) : 'snippets' );
|
||||
|
||||
$classmap = array(
|
||||
'snippets' => 'manage',
|
||||
'add-snippet' => 'edit',
|
||||
'edit-snippet' => 'edit',
|
||||
'import-code-snippets' => 'import',
|
||||
'snippets-settings' => 'settings',
|
||||
);
|
||||
|
||||
$menus = code_snippets()->admin->menus;
|
||||
$class = isset( $classmap[ $sub ], $menus[ $classmap[ $sub ] ] ) ? $menus[ $classmap[ $sub ] ] : $this;
|
||||
|
||||
/* Add a submenu to the Tools menu */
|
||||
$hook = add_submenu_page(
|
||||
'tools.php',
|
||||
__( 'Snippets', 'code-snippets' ),
|
||||
_x( 'Snippets', 'tools submenu label', 'code-snippets' ),
|
||||
code_snippets()->get_cap(),
|
||||
code_snippets()->get_menu_slug(),
|
||||
array( $class, 'render' )
|
||||
);
|
||||
|
||||
add_action( 'load-' . $hook, array( $class, 'load' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed when the admin page is loaded
|
||||
*/
|
||||
public function load() {
|
||||
parent::load();
|
||||
|
||||
$contextual_help = new Contextual_Help( 'manage' );
|
||||
$contextual_help->load();
|
||||
|
||||
$this->cloud_search_list_table = new Cloud_Search_List_Table();
|
||||
$this->cloud_search_list_table->prepare_items();
|
||||
|
||||
$this->list_table = new List_Table();
|
||||
$this->list_table->prepare_items();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts and stylesheets for the admin page.
|
||||
*/
|
||||
public function enqueue_assets() {
|
||||
$plugin = code_snippets();
|
||||
|
||||
wp_enqueue_style(
|
||||
'code-snippets-manage',
|
||||
plugins_url( 'dist/manage.css', $plugin->file ),
|
||||
[],
|
||||
$plugin->version
|
||||
);
|
||||
|
||||
wp_enqueue_script(
|
||||
'code-snippets-manage-js',
|
||||
plugins_url( 'dist/manage.js', $plugin->file ),
|
||||
[ 'wp-i18n' ],
|
||||
$plugin->version,
|
||||
true
|
||||
);
|
||||
|
||||
wp_set_script_translations( 'code-snippets-manage-js', 'code-snippets' );
|
||||
|
||||
if ( 'cloud' === $this->get_current_type() || 'cloud_search' === $this->get_current_type() ) {
|
||||
Front_End::enqueue_all_prism_themes();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the currently displayed snippet type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_current_type(): string {
|
||||
$types = Plugin::get_types();
|
||||
$current_type = isset( $_GET['type'] ) ? sanitize_key( wp_unslash( $_GET['type'] ) ) : 'all';
|
||||
return isset( $types[ $current_type ] ) ? $current_type : 'all';
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the status and error messages
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function print_messages() {
|
||||
$this->render_view( 'partials/list-table-notices' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles saving the user's snippets per page preference
|
||||
*
|
||||
* @param mixed $status Current screen option status.
|
||||
* @param string $option The screen option name.
|
||||
* @param mixed $value Screen option value.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function save_screen_option( $status, string $option, $value ) {
|
||||
return 'snippets_per_page' === $option ? $value : $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the priority value for a snippet.
|
||||
*
|
||||
* @param Snippet $snippet Snippet to update.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function update_snippet_priority( Snippet $snippet ) {
|
||||
global $wpdb;
|
||||
$table = code_snippets()->db->get_table_name( $snippet->network );
|
||||
|
||||
$wpdb->update(
|
||||
$table,
|
||||
array( 'priority' => $snippet->priority ),
|
||||
array( 'id' => $snippet->id ),
|
||||
array( '%d' ),
|
||||
array( '%d' )
|
||||
);
|
||||
|
||||
clean_snippets_cache( $table );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle AJAX requests
|
||||
*/
|
||||
public function ajax_callback() {
|
||||
check_ajax_referer( 'code_snippets_manage_ajax' );
|
||||
|
||||
if ( ! isset( $_POST['field'], $_POST['snippet'] ) ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'type' => 'param_error',
|
||||
'message' => 'incomplete request',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
$snippet_data = array_map( 'sanitize_text_field', json_decode( wp_unslash( $_POST['snippet'] ), true ) );
|
||||
|
||||
$snippet = new Snippet( $snippet_data );
|
||||
$field = sanitize_key( $_POST['field'] );
|
||||
|
||||
if ( 'priority' === $field ) {
|
||||
|
||||
if ( ! isset( $snippet_data['priority'] ) || ! is_numeric( $snippet_data['priority'] ) ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'type' => 'param_error',
|
||||
'message' => 'missing snippet priority data',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$this->update_snippet_priority( $snippet );
|
||||
|
||||
} elseif ( 'active' === $field ) {
|
||||
|
||||
if ( ! isset( $snippet_data['active'] ) ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'type' => 'param_error',
|
||||
'message' => 'missing snippet active data',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( $snippet->shared_network ) {
|
||||
$active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
|
||||
|
||||
if ( in_array( $snippet->id, $active_shared_snippets, true ) !== $snippet->active ) {
|
||||
|
||||
$active_shared_snippets = $snippet->active ?
|
||||
array_merge( $active_shared_snippets, array( $snippet->id ) ) :
|
||||
array_diff( $active_shared_snippets, array( $snippet->id ) );
|
||||
|
||||
update_option( 'active_shared_network_snippets', $active_shared_snippets );
|
||||
clean_active_snippets_cache( code_snippets()->db->ms_table );
|
||||
}
|
||||
} elseif ( $snippet->active ) {
|
||||
$result = activate_snippet( $snippet->id, $snippet->network );
|
||||
if ( is_string( $result ) ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'type' => 'action_error',
|
||||
'message' => $result,
|
||||
)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
deactivate_snippet( $snippet->id, $snippet->network );
|
||||
}
|
||||
}
|
||||
|
||||
wp_send_json_success();
|
||||
}
|
||||
}
|
||||
239
plugins/code-snippets/php/admin-menus/class-settings-menu.php
Normal file
239
plugins/code-snippets/php/admin-menus/class-settings-menu.php
Normal file
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
|
||||
namespace Code_Snippets;
|
||||
|
||||
use const Code_Snippets\Settings\CACHE_KEY;
|
||||
use const Code_Snippets\Settings\OPTION_GROUP;
|
||||
use const Code_Snippets\Settings\OPTION_NAME;
|
||||
|
||||
/**
|
||||
* This class handles the settings admin menu
|
||||
*
|
||||
* @since 2.4.0
|
||||
* @package Code_Snippets
|
||||
*/
|
||||
class Settings_Menu extends Admin_Menu {
|
||||
|
||||
/**
|
||||
* Settings page name as registered with the Settings API.
|
||||
*/
|
||||
public const SETTINGS_PAGE = 'code-snippets';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
parent::__construct(
|
||||
'settings',
|
||||
_x( 'Settings', 'menu label', 'code-snippets' ),
|
||||
__( 'Snippets Settings', 'code-snippets' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed when the admin page is loaded
|
||||
*/
|
||||
public function load() {
|
||||
parent::load();
|
||||
|
||||
if ( is_network_admin() ) {
|
||||
if ( Settings\are_settings_unified() ) {
|
||||
$this->update_network_options();
|
||||
} else {
|
||||
wp_safe_redirect( code_snippets()->get_menu_url( 'settings', 'admin' ) );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue the stylesheet for the settings menu
|
||||
*/
|
||||
public function enqueue_assets() {
|
||||
$plugin = code_snippets();
|
||||
|
||||
Settings\enqueue_editor_preview_assets();
|
||||
|
||||
wp_enqueue_style(
|
||||
'code-snippets-settings',
|
||||
plugins_url( 'dist/settings.css', $plugin->file ),
|
||||
[ 'code-editor' ],
|
||||
$plugin->version
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the list of settings sections.
|
||||
*
|
||||
* @return array<string, array<string, mixed>>
|
||||
*/
|
||||
private function get_sections(): array {
|
||||
global $wp_settings_sections;
|
||||
|
||||
if ( ! isset( $wp_settings_sections[ self::SETTINGS_PAGE ] ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
return (array) $wp_settings_sections[ self::SETTINGS_PAGE ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the name of the settings section currently being viewed.
|
||||
*
|
||||
* @param string $default_section Name of the default tab displayed.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_current_section( string $default_section = 'general' ): string {
|
||||
$sections = $this->get_sections();
|
||||
|
||||
if ( ! $sections ) {
|
||||
return $default_section;
|
||||
}
|
||||
|
||||
$active_tab = isset( $_REQUEST['section'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['section'] ) ) : $default_section;
|
||||
return isset( $sections[ $active_tab ] ) ? $active_tab : $default_section;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the admin screen
|
||||
*/
|
||||
public function render() {
|
||||
$update_url = is_network_admin() ? add_query_arg( 'update_site_option', true ) : admin_url( 'options.php' );
|
||||
$current_section = $this->get_current_section();
|
||||
|
||||
?>
|
||||
<div class="code-snippets-settings wrap" data-active-tab="<?php echo esc_attr( $current_section ); ?>">
|
||||
<h1>
|
||||
<?php
|
||||
esc_html_e( 'Settings', 'code-snippets' );
|
||||
|
||||
if ( code_snippets()->is_compact_menu() ) {
|
||||
$actions = [
|
||||
_x( 'Manage', 'snippets', 'code-snippets' ) => code_snippets()->get_menu_url(),
|
||||
_x( 'Add New', 'snippet', 'code-snippets' ) => code_snippets()->get_menu_url( 'add' ),
|
||||
_X( 'Import', 'snippets', 'code-snippets' ) => code_snippets()->get_menu_url( 'import' ),
|
||||
];
|
||||
|
||||
foreach ( $actions as $label => $url ) {
|
||||
printf(
|
||||
'<a href="%s" class="page-title-action">%s</a>',
|
||||
esc_url( $url ),
|
||||
esc_html( $label )
|
||||
);
|
||||
}
|
||||
}
|
||||
?>
|
||||
</h1>
|
||||
|
||||
<?php settings_errors( OPTION_NAME ); ?>
|
||||
|
||||
<form action="<?php echo esc_url( $update_url ); ?>" method="post">
|
||||
<input type="hidden" name="section" value="<?php echo esc_attr( $current_section ); ?>">
|
||||
<?php
|
||||
|
||||
settings_fields( OPTION_GROUP );
|
||||
$this->do_settings_tabs();
|
||||
?>
|
||||
<p class="submit">
|
||||
<?php
|
||||
submit_button( null, 'primary', 'submit', false );
|
||||
|
||||
submit_button(
|
||||
__( 'Reset to Default', 'code-snippets' ),
|
||||
'secondary',
|
||||
sprintf( '%s[%s]', OPTION_NAME, 'reset_settings' ),
|
||||
false
|
||||
);
|
||||
?>
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Output snippet settings in tabs
|
||||
*/
|
||||
protected function do_settings_tabs() {
|
||||
$sections = $this->get_sections();
|
||||
$active_tab = $this->get_current_section();
|
||||
|
||||
echo '<h2 class="nav-tab-wrapper" id="settings-sections-tabs">';
|
||||
|
||||
foreach ( $sections as $section ) {
|
||||
printf(
|
||||
'<a class="nav-tab%s" data-section="%s" href="%s">%s</a>',
|
||||
esc_attr( $active_tab ) === $section['id'] ? ' nav-tab-active' : '',
|
||||
esc_attr( $section['id'] ),
|
||||
esc_url( add_query_arg( 'section', $section['id'] ) ),
|
||||
esc_html( $section['title'] )
|
||||
);
|
||||
}
|
||||
|
||||
echo '</h2>';
|
||||
|
||||
foreach ( $sections as $section ) {
|
||||
if ( 'license' === $section['id'] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $section['title'] ) {
|
||||
printf(
|
||||
'<h2 id="%s-settings" class="settings-section-title">%s</h2>' . "\n",
|
||||
esc_attr( $section['id'] ),
|
||||
esc_html( $section['title'] )
|
||||
);
|
||||
}
|
||||
|
||||
if ( $section['callback'] ) {
|
||||
call_user_func( $section['callback'], $section );
|
||||
}
|
||||
|
||||
printf( '<div class="settings-section %s-settings"><table class="form-table">', esc_attr( $section['id'] ) );
|
||||
|
||||
do_settings_fields( self::SETTINGS_PAGE, $section['id'] );
|
||||
echo '</table></div>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill in for the Settings API in the Network Admin
|
||||
*/
|
||||
public function update_network_options() {
|
||||
|
||||
// Ensure the settings have been saved.
|
||||
if ( empty( $_GET['update_site_option'] ) || empty( $_POST[ OPTION_NAME ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
check_admin_referer( 'code-snippets-options' );
|
||||
|
||||
// Retrieve the saved options and save them to the database.
|
||||
$value = map_deep( wp_unslash( $_POST[ OPTION_NAME ] ), 'sanitize_key' );
|
||||
update_site_option( OPTION_NAME, $value );
|
||||
wp_cache_delete( CACHE_KEY );
|
||||
|
||||
// Add an updated notice.
|
||||
if ( ! count( get_settings_errors() ) ) {
|
||||
add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'code-snippets' ), 'updated' );
|
||||
}
|
||||
|
||||
set_transient( 'settings_errors', get_settings_errors(), 30 );
|
||||
|
||||
// Redirect back to the settings menu.
|
||||
$redirect = add_query_arg( 'settings-updated', 'true', remove_query_arg( 'update_site_option', wp_get_referer() ) );
|
||||
wp_safe_redirect( esc_url_raw( $redirect ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty implementation for print_messages.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function print_messages() {
|
||||
// none required.
|
||||
}
|
||||
}
|
||||
88
plugins/code-snippets/php/admin-menus/class-welcome-menu.php
Normal file
88
plugins/code-snippets/php/admin-menus/class-welcome-menu.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Code_Snippets;
|
||||
|
||||
/**
|
||||
* This class handles the welcome menu.
|
||||
*
|
||||
* @since 3.7.0
|
||||
* @package Code_Snippets
|
||||
*/
|
||||
class Welcome_Menu extends Admin_Menu {
|
||||
|
||||
/**
|
||||
* Instance of Welcome_API class.
|
||||
*
|
||||
* @var Welcome_API
|
||||
*/
|
||||
protected Welcome_API $api;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param Welcome_API $api Instance of API class.
|
||||
*/
|
||||
public function __construct( $api ) {
|
||||
parent::__construct(
|
||||
'welcome',
|
||||
_x( "What's New", 'menu label', 'code-snippets' ),
|
||||
__( 'Welcome to Code Snippets', 'code-snippets' )
|
||||
);
|
||||
|
||||
$this->api = $api;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue assets necessary for the welcome menu.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue_assets() {
|
||||
wp_enqueue_style(
|
||||
'code-snippets-welcome',
|
||||
plugins_url( 'dist/welcome.css', PLUGIN_FILE ),
|
||||
[],
|
||||
PLUGIN_VERSION
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of links to display in the page header.
|
||||
*
|
||||
* @return array<string, array{url: string, icon: string, label: string}>
|
||||
*/
|
||||
protected function get_header_links(): array {
|
||||
$links = [
|
||||
'cloud' => [
|
||||
'url' => 'https://codesnippets.cloud',
|
||||
'icon' => 'cloud',
|
||||
'label' => __( 'Cloud', 'code-snippets' ),
|
||||
],
|
||||
'resources' => [
|
||||
'url' => 'https://codesnippets.pro/support/',
|
||||
'icon' => 'sos',
|
||||
'label' => __( 'Support', 'code-snippets' ),
|
||||
],
|
||||
'facebook' => [
|
||||
'url' => 'https://www.facebook.com/groups/282962095661875/',
|
||||
'icon' => 'facebook',
|
||||
'label' => __( 'Community', 'code-snippets' ),
|
||||
],
|
||||
'discord' => [
|
||||
'url' => 'https://snipco.de/discord',
|
||||
'icon' => 'discord',
|
||||
'label' => __( 'Discord', 'code-snippets' ),
|
||||
],
|
||||
];
|
||||
|
||||
if ( ! code_snippets()->licensing->is_licensed() ) {
|
||||
$links['pro'] = [
|
||||
'url' => 'https://codesnippets.pro/pricing/',
|
||||
'icon' => 'cart',
|
||||
'label' => __( 'Upgrade to Pro', 'code-snippets' ),
|
||||
];
|
||||
}
|
||||
|
||||
return $links;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user