/** * Process data sent from the edit page */ private function process_actions() { /* Check for a valid nonce */ if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'save_snippet')) { return; } if (isset($_POST['save_snippet']) || isset($_POST['save_snippet_activate']) || isset($_POST['save_snippet_deactivate'])) { $this->save_posted_snippet(); } if (isset($_POST['snippet_id'])) { /* Delete the snippet if the button was clicked */ if (isset($_POST['delete_snippet'])) { delete_snippet($_POST['snippet_id']); wp_redirect(add_query_arg('result', 'delete', code_snippets_get_menu_url('manage'))); exit; } /* Export the snippet if the button was clicked */ if (isset($_POST['export_snippet'])) { export_snippets($_POST['snippet_id']); } } }
/** * Processes a bulk action * * @uses activate_snippet() To activate snippets * @uses deactivate_snippet() To deactivate snippets * @uses delete_snippet() To delete snippets * @uses export_snippets() To export selected snippets * @uses wp_redirect() To pass the results to the current page * @uses add_query_arg() To append the results to the current URI */ function process_bulk_actions() { $network = get_current_screen()->is_network; if (isset($_GET['action'], $_GET['id'])) { $id = absint($_GET['id']); $action = sanitize_key($_GET['action']); $_SERVER['REQUEST_URI'] = remove_query_arg(array('action', 'id')); if ('activate' === $action) { activate_snippet($id, $network); } elseif ('deactivate' === $action) { deactivate_snippet($id, $network); } elseif ('delete' === $action) { delete_snippet($id, $network); } elseif ('export' === $action) { export_snippets($id, $network); } elseif ('export-php' === $action) { export_snippets($id, $network, 'php'); } if (!in_array($action, array('export', 'export-php'))) { wp_redirect(apply_filters("code_snippets/{$action}_redirect", esc_url_raw(add_query_arg($action, true)))); } } if (!isset($_POST['ids'])) { return; } $ids = $_POST['ids']; $_SERVER['REQUEST_URI'] = remove_query_arg(array('activate', 'deactivate', 'delete', 'activate-multi', 'deactivate-multi', 'delete-multi')); switch ($this->current_action()) { case 'activate-selected': foreach ($ids as $id) { activate_snippet($id, $network); } wp_redirect(esc_url_raw(add_query_arg('activate-multi', true))); break; case 'deactivate-selected': foreach ($ids as $id) { deactivate_snippet($id, $network); } wp_redirect(esc_url_raw(add_query_arg('deactivate-multi', true))); break; case 'export-selected': export_snippets($ids, $network); break; case 'export-php-selected': export_snippets($ids, $network, 'php'); break; case 'delete-selected': foreach ($ids as $id) { delete_snippet($id, $network); } wp_redirect(esc_url_raw(add_query_arg('delete-multi', true))); break; case 'clear-recent-list': if ($network) { update_site_option('recently_activated_snippets', array()); } else { update_option('recently_activated_snippets', array()); } break; } }
/** * Processes a bulk action * * @uses activate_snippet() to activate snippets * @uses deactivate_snippet() to deactivate snippets * @uses delete_snippet() to delete snippets * @uses export_snippets() to export selected snippets * @uses wp_redirect() to pass the results to the current page * @uses add_query_arg() to append the results to the current URI */ public function process_bulk_actions() { if (isset($_GET['action'], $_GET['id'])) { $id = absint($_GET['id']); $action = sanitize_key($_GET['action']); $_SERVER['REQUEST_URI'] = remove_query_arg(array('action', 'id')); if ('activate' === $action) { activate_snippet($id, $this->is_network); $result = 'activated'; } elseif ('deactivate' === $action) { deactivate_snippet($id, $this->is_network); $result = 'deactivated'; } elseif ('activate-shared' === $action) { $active_shared_snippets = get_option('active_shared_network_snippets', array()); if (!in_array($id, $active_shared_snippets)) { $active_shared_snippets[] = $id; update_option('active_shared_network_snippets', $active_shared_snippets); } $result = 'activated'; } elseif ('deactivate-shared' === $action) { $active_shared_snippets = get_option('active_shared_network_snippets', array()); update_option('active_shared_network_snippets', array_diff($active_shared_snippets, array($id))); $result = 'deactivated'; } elseif ('delete' === $action) { delete_snippet($id, $this->is_network); $result = 'deleted'; } elseif ('export' === $action) { export_snippets(array($id), $this->is_network); } elseif ('export-php' === $action) { export_snippets(array($id), $this->is_network, 'php'); } if (isset($result)) { wp_redirect(esc_url_raw(add_query_arg('result', $result))); exit; } } if (!isset($_POST['ids']) && !isset($_POST['shared_ids'])) { return; } $ids = isset($_POST['ids']) ? $_POST['ids'] : array(); $_SERVER['REQUEST_URI'] = remove_query_arg('action'); switch ($this->current_action()) { case 'activate-selected': foreach ($ids as $id) { activate_snippet($id, $this->is_network); } /* Process the shared network snippets */ if (isset($_POST['shared_ids']) && is_multisite() && !$this->is_network) { $active_shared_snippets = get_option('active_shared_network_snippets', array()); foreach ($_POST['shared_ids'] as $id) { if (!in_array($id, $active_shared_snippets)) { $active_shared_snippets[] = $id; } } update_option('active_shared_network_snippets', $active_shared_snippets); } wp_redirect(esc_url_raw(add_query_arg('result', 'activated-multi'))); exit; case 'deactivate-selected': foreach ($ids as $id) { deactivate_snippet($id, $this->is_network); } /* Process the shared network snippets */ if (isset($_POST['shared_ids']) && is_multisite() && !$this->is_network) { $active_shared_snippets = get_option('active_shared_network_snippets', array()); $active_shared_snippets = array_diff($active_shared_snippets, $_POST['shared_ids']); update_option('active_shared_network_snippets', ${$active_shared_snippets}); } wp_redirect(esc_url_raw(add_query_arg('result', 'deactivated-multi'))); exit; case 'export-selected': export_snippets($ids, $this->is_network); break; case 'export-php-selected': export_snippets($ids, $this->is_network, 'php'); break; case 'delete-selected': foreach ($ids as $id) { delete_snippet($id, $this->is_network); } wp_redirect(esc_url_raw(add_query_arg('result', 'deleted-multi'))); exit; case 'clear-recent-list': if ($this->is_network) { update_site_option('recently_activated_snippets', array()); } else { update_option('recently_activated_snippets', array()); } break; } }
/** * 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']); } }
/** * Save the posted snippet to the database * @uses wp_redirect() to pass the results to the page * @uses save_snippet() to save the snippet to the database */ private function save_posted_snippet() { /* 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'])) { /* Build snippet object from fields with 'snippet_' prefix */ $snippet = new Snippet(); foreach ($_POST as $field => $value) { if ('snippet_' === substr($field, 0, 8)) { /* Remove 'snippet_' prefix from field name */ $field = substr($field, 8); $snippet->{$field} = stripslashes($value); } } /* Activate or deactivate the snippet before saving if we clicked the button */ // Shared network snippets cannot be network activated if (isset($_POST['snippet_sharing']) && 'on' === $_POST['snippet_sharing']) { $snippet->active = 0; unset($_POST['save_snippet_activate'], $_POST['save_snippet_deactivate']); } elseif (isset($_POST['save_snippet_activate'])) { $snippet->active = 1; } elseif (isset($_POST['save_snippet_deactivate'])) { $snippet->active = 0; } /* Save the snippet to the database */ $snippet_id = save_snippet($snippet); /* Update the shared network snippets if necessary */ if ($snippet_id && get_current_screen()->in_admin('network')) { $shared_snippets = get_site_option('shared_network_snippets', array()); if (isset($_POST['snippet_sharing']) && 'on' === $_POST['snippet_sharing']) { /* Add the snippet ID to the array if it isn't already */ if (!in_array($snippet_id, $shared_snippets)) { $shared_snippets[] = $snippet_id; update_site_option('shared_network_snippets', array_values($shared_snippets)); } } elseif (in_array($snippet_id, $shared_snippets)) { /* Remove the snippet ID from the array */ $shared_snippets = array_diff($shared_snippets, array($snippet_id)); update_site_option('shared_network_snippets', array_values($shared_snippets)); /* Deactivate on all sites */ global $wpdb; if ($sites = $wpdb->get_col("SELECT blog_id FROM {$wpdb->blogs}")) { foreach ($sites as $site) { switch_to_blog($site); $active_shared_snippets = get_option('active_shared_network_snippets'); if (is_array($active_shared_snippets)) { $active_shared_snippets = array_diff($active_shared_snippets, array($snippet_id)); update_option('active_shared_network_snippets', $active_shared_snippets); } } restore_current_blog(); } } } /* If the saved snippet ID is invalid, display an error message */ if (!$snippet_id || $snippet_id < 1) { /* An error occurred */ wp_redirect(add_query_arg('result', 'error', code_snippets_get_menu_url('add'))); exit; } /* Set the result depending on if the snippet was just added */ $result = isset($_POST['snippet_id']) ? 'updated' : 'added'; /* Append a suffix if the snippet was activated or deactivated */ if (isset($_POST['save_snippet_activate'])) { $result .= '-and-activated'; } elseif (isset($_POST['save_snippet_deactivate'])) { $result .= '-and-deactivated'; } /* Redirect to edit snippet page */ wp_redirect(add_query_arg(array('id' => $snippet_id, 'result' => $result), code_snippets_get_menu_url('edit'))); exit; } elseif (isset($_POST['snippet_id'], $_POST['delete_snippet'])) { delete_snippet($_POST['snippet_id']); wp_redirect(add_query_arg('result', 'delete', code_snippets_get_menu_url('manage'))); exit; } elseif (isset($_POST['snippet_id'], $_POST['export_snippet'])) { export_snippets($_POST['snippet_id']); } }