Settings'; array_unshift($links, $settings_link); return $links; } add_filter("plugin_action_links_$plugin_basename", 'randomtext_settings_link' ); 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_pagetitle($suffix='') { return '

Random Text '.$suffix.'

'; } function randomtext_error($text='An undefined error has occured.') { echo '
'.randomtext_pagetitle(' - ERROR!').'

'.$text.'

'; } function randomtext_list() { global $wpdb, $user_ID, $randomtext_adminurl; $table_name = $wpdb->prefix . 'randomtext'; $pageURL = $randomtext_adminurl; $cat = isset($_GET['cat']) ? $_GET['cat'] : false; $author_id = isset($_GET['author_id']) ? intval($_GET['author_id']) : 0; $where = $page_params = ''; if($cat) { $where = " WHERE category = '$cat'"; $page_params = '&cat='.urlencode($cat); } if($author_id) { $where = " WHERE user_id = $author_id"; $page_params .= '&author_id='.$author_id; } // pagination related $item_count = $wpdb->get_row("Select count(*) items FROM $table_name $where"); if(isset($item_count->items)) { $totalrows = $item_count->items; } else { echo '

Achtung! The expected database table "'.$table_name.'" does not appear to exist.

'; } $perpage = 20; $paged = isset($_GET['paged']) ? intval($_GET['paged']) : 0; $paged = $paged ? $paged : 1; $num_pages = 1+floor($totalrows/$perpage); if($paged > $num_pages) { $paged = $num_pages; } $del_paged = ($paged > 1) ? '&paged='.$paged : ''; // so we stay on the current page if we delete an item $paging = paginate_links( array( 'base' => $pageURL.$page_params.'%_%', // add_query_arg( 'paged', '%#%' ), 'format' => '&paged=%#%', 'prev_text' => __('«'), 'next_text' => __('»'), 'total' => $num_pages, 'current' => $paged )); // now load the data to display $startrow = ($paged-1)*$perpage; $rows = $wpdb->get_results("SELECT * FROM $table_name $where ORDER BY randomtext_id LIMIT $startrow, $perpage"); $item_range = count($rows); if($item_range>1) { $item_range = ($startrow+1).' - '.($startrow+$item_range); } $author = array(); ?>
Category:
Displaying
user_id])){ $user_info = get_userdata($row->user_id); $author[$row->user_id] = $user_info->display_name; } $status = ($row->visible=='yes') ? 'visible' : 'hidden'; $bytes = strlen($row->text); if(strlen($row->text) > 200) $row->text = trim(mb_substr($row->text,0,350,'UTF-8')).'...'; echo ' '; } echo '
ID Text Category Author Action
'.$row->randomtext_id.' '.esc_html($row->text).' '.$row->category.'
'.$status.'
'.$author[$row->user_id].'
'.$bytes.' bytes
Edit
'; $del_link = wp_nonce_url($pageURL.$del_paged.'&action=delete&id='.$row->randomtext_id, 'randomtext_delete' . $row->randomtext_id); echo 'Delete'; echo '
'; echo '
'; } function randomtext_edit($randomtext_id=0) { echo '
'; $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 = '

The requested entry was not found.

'; } else { $row = new stdClass(); $row->text = ''; $row->visible = 'yes'; } echo randomtext_pagetitle($title); if($randomtext_id && !$row) { echo '

The requested entry was not found.

'; } else { // display the add/edit form global $randomtext_adminurl; echo '
'.wp_nonce_field('randomtext_edit' . $randomtext_id).'

Text To Display

Category

Select a category from the list or enter a new one.

'; echo '

Is visible.

    

'; if(!$randomtext_id) { // don't offer Bulk Insert on edit echo '

Use Bulk Insert

Yes     No

Bulk insert will create a new record for each line (delimited by carriage return) within the text box above using the same category selected.
Empty lines will be ignored.
'; } echo '

Return to Random Text summary page.

'; } echo '
'; } function randomtext_save($data) { global $wpdb, $user_ID; $table_name = $wpdb->prefix . 'randomtext'; $randomtext_id = intval($data['randomtext_id']); check_admin_referer('randomtext_edit'.$randomtext_id); $sqldata = array(); $category_new = trim($data['randomtext_category_new']); $sqldata['category'] = ($category_new) ? $category_new : $data['randomtext_category']; $sqldata['user_id'] = $user_ID; $sqldata['visible'] = $data['randomtext_visible']; // check for "Bulk Insert" $do_bulkinsert = isset($data['randomtext_bulkinsert']) ? $data['randomtext_bulkinsert'] : 'no'; if ($do_bulkinsert == 'yes') { // Split the data by carriage returns $lines = preg_split("/[\n|\r]/", trim(stripslashes($data['randomtext_text']))); foreach ($lines as $key=>$value) { // Ignore empty lines if (!empty($value)) { // Set the datavalue and insert $sqldata['text'] = $value; $wpdb->insert($table_name, $sqldata); } } } else { // single record insert/update $sqldata['text'] = trim(stripslashes($data['randomtext_text'])); if($randomtext_id) $wpdb->update($table_name, $sqldata, array('randomtext_id'=>$randomtext_id)); else $wpdb->insert($table_name, $sqldata); } } function randomtext_delete($id) { global $wpdb; $table_name = $wpdb->prefix . 'randomtext'; $id = intval($id); $sql = "DELETE FROM $table_name WHERE randomtext_id = $id"; $wpdb->query($sql); } ?>