Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
- add_action( 'admin_action_delete', 'delete_article' );
- class Articles_List extends WP_List_Table {
- public function __construct() {
- // Set parent defaults.
- parent::__construct( array(
- 'singular' => 'article', // Singular name of the listed records.
- 'plural' => 'articles', // Plural name of the listed records.
- 'ajax' => false, // Does this table support ajax?
- ) );
- }
- function get_all_articles(){
- global $wpdb;
- $query = $wpdb->get_results("select * from wp_copywriter_articles as articles join wp_copywriter_categories as categories on categories.category_id = articles.category_id", ARRAY_A);
- return $query;
- }
- function prepare_items()
- {
- global $wpdb;
- $columns = $this->get_columns();
- $hidden = $this->get_hidden_columns();
- $sortable = $this->get_sortable_columns();
- $data = $this->get_all_articles();
- usort( $data, array( $this, 'sort_data' ) );
- $perPage = 10;
- $currentPage = $this->get_pagenum();
- $totalItems = count($data);
- $this->set_pagination_args( array(
- 'total_items' => $totalItems,
- 'per_page' => $perPage
- ) );
- $data = array_slice($data,(($currentPage-1)*$perPage),$perPage);
- $this->_column_headers = array($columns, $hidden, $sortable);
- $this->items = $data;
- }
- function record_count() {
- global $wpdb;
- $query = "SELECT COUNT(*) FROM wp_copywriter_articles";
- return $wpdb->get_var( $query );
- }
- function no_articles() {
- _e( 'Brak artykułów', 'sp' );
- }
- function column_default( $item, $column_name ) {
- switch ( $column_name ) {
- case 'article_id':
- case 'article_name':
- case 'article_source':
- case 'category_name':
- return $item[ $column_name ];
- default:
- return print_r( $item, true );
- }
- }
- function column_name( $item ) {
- $delete_nonce = wp_create_nonce( 'delete_article' );
- $title = '<strong>' . $item['article_name'] . '</strong>';
- $actions = [
- 'edit' => sprintf('<a href="?page=%s&id=%s">Edit</a>', 'edytuj-artykul', $item['article_id']),
- 'delete' => sprintf('<a href="?page=%s&action=%s&article_id=%s&_wpnonce=%s">Delete</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint($item['article_id']), $delete_nonce)
- ];
- return $title. $this->row_actions( $actions );
- }
- function delete_article(){
- global $wpdb;
- // Ensure we can verify our nonce.
- if( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'delete_article' ) ) {
- wp_die( esc_html__( 'Could not verify request. Please try again.' ) );
- // Ensure we have an article ID provided.
- } else if( ! isset( $_REQUEST['article_id'] ) ) {
- wp_die( esc_html__( 'Could not find provided article ID. Please try again.' ) );
- // Ensure we're able to delete the actual article.
- } else if( false === $wpdb->delete( 'wp_copywriter_articles', array( 'article_id' => $_REQUEST['article_id'] ) ) ) {
- wp_die( esc_html__( 'Could not delete the provided article. Please try again.' ) );
- }
- // Redirect back to original page with 'success' $_GET
- wp_safe_redirect( add_query_arg( array(
- 'page' => ( isset( $_REQUEST['page'] ) ) ? $_REQUEST['page'] : '404',
- 'success' => 1,
- ), admin_url( 'admin.php' ) ) );
- exit();
- // $article_id = $item['article_id'];
- // global $wpdb;
- // $table = 'wp_copywriter_articles';
- // $wpdb->query("DELETE FROM $table WHERE article_id = %d", $article_id);
- }
- function column_cb( $item ) {
- return sprintf(
- '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['article_id']
- );
- }
- function get_columns() {
- $columns = array(
- 'cb' => '<input type="checkbox" />',
- 'name' => 'Nazwa',
- 'article_source' => 'Źródło',
- 'category_name' => 'Kategoria',
- );
- return $columns;
- }
- function get_hidden_columns()
- {
- return array();
- }
- function get_sortable_columns()
- {
- return array(
- 'name' => array('article_name', false),
- 'article_source' => array('article_source', false),
- 'category_name' => array('category_name', false)
- );
- }
- private function sort_data($a, $b)
- {
- $orderby = 'article_name';
- $order = 'asc';
- // If orderby is set, use this as the sort column
- if(!empty($_GET['orderby']))
- {
- $orderby = $_GET['orderby'];
- }
- // If order is set use this as the order
- if(!empty($_GET['order']))
- {
- $order = $_GET['order'];
- }
- $result = strcmp( $a[$orderby], $b[$orderby] );
- if($order === 'asc')
- {
- return $result;
- }
- return -$result;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement