Example #1
0
function randomtext_options()
{
    if ($_POST) {
        // process the posted data and display summary page - not pretty :(
        randomtext_save($_POST);
    }
    $action = isset($_GET['action']) ? $_GET['action'] : false;
    switch ($action) {
        case 'new':
            randomtext_edit();
            break;
        case 'edit':
            $id = intval($_GET['id']);
            randomtext_edit($id);
            break;
        case 'delete':
            $id = intval($_GET['id']);
            check_admin_referer('randomtext_delete' . $id);
            randomtext_delete($id);
            // now display summary page
            randomtext_list();
            break;
        default:
            randomtext_list();
    }
}
function randomtext_edit($randomtext_id = 0)
{
    if ($_POST) {
        // process the posted data and display summary page - not pretty :(
        randomtext_save($_POST);
        randomtext_list();
        die;
    }
    echo '<div class="wrap">';
    $title = '- Add New';
    if ($randomtext_id) {
        $title = '- Edit';
        global $wpdb;
        $table_name = $wpdb->prefix . 'randomtext';
        $sql = "SELECT * from {$table_name} where randomtext_id={$randomtext_id}";
        $row = $wpdb->get_row($sql);
        if (!$row) {
            $error_text = '<h2>The requested entry was not found.</h2>';
        }
    } else {
        $row = new stdClass();
        $row->text = '';
        $row->visible = 'yes';
    }
    echo randomtext_pagetitle($title);
    if ($randomtext_id && !$row) {
        echo '<h3>The requested entry was not found.</h3>';
    } else {
        // display the add/edit form
        echo '<form method="post" action="' . $_SERVER["REQUEST_URI"] . '">
		' . wp_nonce_field('randomtext_edit' . $randomtext_id) . '
		<input type="hidden" id="randomtext_id" name="randomtext_id" value="' . $randomtext_id . '">
		<h3>Text To Display</h3>
		<textarea name="randomtext_text" style="width: 80%; height: 100px;">' . apply_filters('format_to_edit', $row->text) . '</textarea>
		<h3>Category</h3>
		<p>Select a category from the list or enter a new one.</p>
		<label for="randomtext_category">Category: </label><select id="randomtext_category" name="randomtext_category">';
        echo randomtext_get_category_options($row->category);
        echo '</select></p>
		<p><label for="randomtext_category_new">New Category: </label><input type="text" id="randomtext_category_new" name="randomtext_category_new"></p>';
        echo '<h3>Is visible.</h3>
			<p><label for="randomtext_visible_yes"><input type="radio" id="randomtext_visible_yes" name="randomtext_visible" value="yes" ' . checked($row->visible, 'yes', false) . ' /> Yes</label>&nbsp;&nbsp;&nbsp;&nbsp;
			<label for="randomtext_visible_no"><input type="radio" id="randomtext_visible_no" name="randomtext_visible" value="no" ' . checked($row->visible, 'no', false) . ' /> No</label></p>';
        if (!$randomtext_id) {
            // don't offer Bulk Insert on edit
            echo '<h3>Use Bulk Insert</h3>
			<p><input type="radio" name="randomtext_bulkinsert" value="yes" /> Yes&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="randomtext_bulkinsert" value="no" checked="checked" /> No</p>
			<small>Bulk insert will create a new record for each line (delimited by carriage return) within the text box above using the same category selected.<br />Empty lines will be ignored.</small>';
        }
        echo '<div class="submit">
			<input class="button-primary" type="submit" name="randomtext_Save" value="Save Changes" />
			</div>
			</form>
			
			<p>Return to <a href="options-general.php?page=randomtext">Random Text summary page</a>.</p>';
    }
    echo '</div>';
}