/** * Add the importer to the Tools > Import menu */ function register_importer() { /* Only register the importer if the current user can manage snippets */ if (!defined('WP_LOAD_IMPORTERS') || !current_user_can(get_snippets_cap())) { 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')); }
/** * Register the top-level 'Snippets' menu and associated 'Manage' subpage * * @uses add_menu_page() to register a top-level menu * @uses add_submenu_page() to register a sub-menu */ function register() { /* Register the top-level menu */ add_menu_page(__('Snippets', 'code-snippets'), __('Snippets', 'code-snippets'), get_snippets_cap(), code_snippets_get_menu_slug(), array($this, 'render'), 'div', is_network_admin() ? 21 : 67); /* Register the sub-menu */ parent::register(); }
/** * Executed when the admin page is loaded */ public function load() { /* Make sure the user has permission to be here */ if (!current_user_can(get_snippets_cap())) { wp_die(__('You are not authorized to access this page.', 'code-snippets')); } /* Create the snippet tables if they don't exist */ create_code_snippets_tables(); }
/** * Initializes the list table class and loads the help tabs * for the Manage Snippets page * * @since 1.0 * @access private */ function code_snippets_load_manage_menu() { /* Make sure the user has permission to be here */ if (!current_user_can(get_snippets_cap())) { wp_die(__('You are not authorized to access this page.', 'code-snippets')); } /* Create the snippet tables if they don't exist */ create_code_snippets_tables(); /* Load the screen help tabs */ require plugin_dir_path(__FILE__) . 'admin-help.php'; /* Initialize the snippet table class */ require_once plugin_dir_path(__FILE__) . 'class-list-table.php'; global $code_snippets_list_table; $code_snippets_list_table = new Code_Snippets_List_Table(); $code_snippets_list_table->prepare_items(); }
/** * Processes import files and loads the help tabs for the Import Snippets page * * @since 1.3 * * @uses import_snippets() To process the import file * @uses wp_redirect() To pass the import results to the page * @uses add_query_arg() To append the results to the current URI */ function code_snippets_load_import_menu() { $network = get_current_screen()->is_network; /* Make sure the user has permission to be here */ if (!current_user_can(get_snippets_cap())) { wp_die(__('You are not access this page.', 'code-snippets')); } /* Create the snippet tables if they don't exist */ create_code_snippets_tables(); /* Process import files */ if (isset($_FILES['code_snippets_import_file']['tmp_name'])) { /* Import the snippets. The result is the number of snippets that were imported */ $result = import_snippets($_FILES['code_snippets_import_file']['tmp_name'], $network); /* Send the amount of imported snippets to the page */ $url = add_query_arg(false === $result ? array('error' => true) : array('imported' => $result)); wp_redirect(esc_url_raw($url)); } /* Load the screen help tabs */ require plugin_dir_path(__FILE__) . 'admin-help.php'; }
/** * Run the active snippets * * @since 2.0 * * @return bool true on success, false on failure */ function execute_active_snippets() { /* Bail early if safe mode is active */ if (defined('CODE_SNIPPETS_SAFE_MODE') && CODE_SNIPPETS_SAFE_MODE) { return false; } if (isset($_GET['code_snippets_safe_mode']) && $_GET['code_snippets_safe_mode'] && current_user_can(get_snippets_cap())) { return false; } /** @var wpdb $wpdb */ global $wpdb; $current_scope = is_admin() ? 1 : 2; /* Check if the snippets tables exist */ $table_exists = $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->snippets}'") === $wpdb->snippets; $ms_table_exists = is_multisite() && $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->ms_snippets}'") === $wpdb->ms_snippets; $sql = ''; /* Fetch snippets from site table */ if ($table_exists) { $sql = $wpdb->prepare("SELECT id, code FROM {$wpdb->snippets} WHERE active=1 AND (scope=0 OR scope=%d)", $current_scope); } /* Fetch snippets from the network table */ if ($ms_table_exists) { if (!empty($sql)) { $sql .= ' UNION ALL '; } /* Only select snippets in the current scope */ $sql .= $wpdb->prepare("SELECT id, code FROM {$wpdb->ms_snippets} WHERE active=1 AND (scope=0 OR scope=%d)", $current_scope); /* Add shared network snippets */ if ($active_shared_ids = get_option('active_shared_network_snippets', false)) { $sql .= ' UNION ALL '; $sql .= $wpdb->prepare(sprintf("SELECT id, code FROM {$wpdb->ms_snippets} WHERE id IN (%s)", implode(',', array_fill(0, count($active_shared_ids), '%d'))), $active_shared_ids); } } /* Return false if there is no query */ if (empty($sql)) { return false; } /* Grab the snippets from the database */ $active_snippets = $wpdb->get_results($sql, OBJECT_K); /* Loop through the returned snippets and execute the PHP code */ foreach ($active_snippets as $snippet_id => $snippet) { if (apply_filters('code_snippets/allow_execute_snippet', true, $snippet_id)) { execute_snippet($snippet->code, $snippet_id); } } return true; }
/** * Loads the help tabs for the Edit Snippets page * * @since 1.0 * @access private * @uses wp_redirect To pass the results to the page */ function code_snippets_load_single_menu() { /* Make sure the user has permission to be here */ if (!current_user_can(get_snippets_cap())) { wp_die(__('You are not authorized to access this page.', 'code-snippets')); } /* Create the snippet tables if they don't exist */ create_code_snippets_tables(); /* Load the screen help tabs */ require plugin_dir_path(__FILE__) . 'admin-help.php'; /* Enqueue the code editor and other scripts and styles */ add_action('admin_enqueue_scripts', 'code_snippets_enqueue_codemirror', 9); /* Don't allow visiting the edit snippet page without a valid ID */ if (code_snippets_get_menu_slug('edit') === $_REQUEST['page']) { if (!isset($_REQUEST['id']) || 0 == $_REQUEST['id']) { wp_redirect(code_snippets_get_menu_url('add')); exit; } } /* Make sure the nonce validates before we do any snippet ops */ if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'save_snippet')) { return; } /* Save the snippet if one has been submitted */ if (isset($_POST['save_snippet']) || isset($_POST['save_snippet_activate']) || isset($_POST['save_snippet_deactivate'])) { /* Activate or deactivate the snippet before saving if we clicked the button */ if (isset($_POST['save_snippet_activate'])) { $_POST['snippet_active'] = 1; } elseif (isset($_POST['save_snippet_deactivate'])) { $_POST['snippet_active'] = 0; } /* Save the snippet to the database */ $result = save_snippet(stripslashes_deep($_POST)); /* Build the status message and redirect */ $query_args = array(); if ($result && isset($_POST['save_snippet_activate'])) { /* Snippet was activated addition to saving*/ $query_args['activated'] = true; } elseif ($result && isset($_POST['save_snippet_deactivate'])) { /* Snippet was deactivated addition to saving*/ $query_args['deactivated'] = true; } if (!$result || $result < 1) { /* An error occurred */ $query_args['invalid'] = true; } elseif (isset($_POST['snippet_id'])) { /* Existing snippet was updated */ $query_args['id'] = $result; $query_args['updated'] = true; } else { /* New snippet was added */ $query_args['id'] = $result; $query_args['added'] = true; } /* Redirect to edit snippet page */ wp_redirect(add_query_arg($query_args, code_snippets_get_menu_url('edit'))); } elseif (isset($_POST['snippet_id'], $_POST['delete_snippet'])) { delete_snippet($_POST['snippet_id']); wp_redirect(add_query_arg('delete', true, code_snippets_get_menu_url('manage'))); } elseif (isset($_POST['snippet_id'], $_POST['export_snippet'])) { export_snippets($_POST['snippet_id']); } }
/** * Register the setting sub-menu * * @since 2.0 * @access private * * @uses add_submenu_page() To register a sub-menu */ function code_snippets_add_settings_menu() { add_submenu_page(code_snippets_get_menu_slug(), __('Snippets Settings', 'code-snippets'), __('Settings', 'code-snippets'), get_snippets_cap(), code_snippets_get_menu_slug('settings'), 'code_snippets_render_settings_menu'); }