Пример #1
0
 /**
  * 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();
 }
Пример #2
0
/**
 * 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();
}
Пример #3
0
/**
 * 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';
}
Пример #4
0
/**
 * Preform upgrade tasks such as deleting and updating options
 * @since 2.0
 */
function code_snippets_upgrader()
{
    /* Get the current plugin version from the database */
    $prev_version = get_option('code_snippets_version');
    /* Check if this is the first plugin run */
    if (!$prev_version) {
        /* Register capabilities */
        $role = get_role(apply_filters('code_snippets_role', 'administrator'));
        $role->add_cap(apply_filters('code_snippets_cap', 'manage_snippets'));
    }
    /* Check if we have upgraded from an older version */
    if (version_compare($prev_version, CODE_SNIPPETS_VERSION, '<')) {
        /* Upgrade the database tables */
        create_code_snippets_tables(true);
        /* Update the plugin version stored in the database */
        update_option('code_snippets_version', CODE_SNIPPETS_VERSION);
    }
    /* Run multisite-only upgrades */
    if (is_multisite() && is_main_site()) {
        /* Get the current plugin version from the database */
        $prev_ms_version = get_site_option('code_snippets_version');
        /* Check if this is the first plugin run */
        if (!$prev_ms_version) {
            /* Register multisite capabilities */
            $network_cap = apply_filters('code_snippets_network_cap', 'manage_network_snippets');
            $supers = get_super_admins();
            foreach ($supers as $admin) {
                $user = new WP_User(0, $admin);
                $user->add_cap($network_cap);
            }
        }
        /* Check if we have upgraded from an older version */
        if (version_compare($prev_ms_version, CODE_SNIPPETS_VERSION, '<')) {
            /* Update the plugin version stored in the database */
            update_site_option('code_snippets_version', CODE_SNIPPETS_VERSION);
        }
    }
}
Пример #5
0
/**
 * 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']);
    }
}