Advertisement
Guest User

Untitled

a guest
Sep 5th, 2021
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.01 KB | None | 0 0
  1. <?php
  2.  
  3. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  4.  
  5. add_action( 'admin_action_delete', 'delete_article' );
  6.  
  7. class Articles_List extends WP_List_Table {
  8.  
  9.     public function __construct() {
  10.         // Set parent defaults.
  11.         parent::__construct( array(
  12.             'singular' => 'article',     // Singular name of the listed records.
  13.             'plural'   => 'articles',    // Plural name of the listed records.
  14.             'ajax'     => false,       // Does this table support ajax?
  15.         ) );
  16.     }
  17.  
  18.     function get_all_articles(){
  19.         global $wpdb;
  20.         $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);
  21.         return $query;
  22.     }
  23.  
  24.     function prepare_items()
  25.     {
  26.         global $wpdb;
  27.         $columns = $this->get_columns();
  28.         $hidden = $this->get_hidden_columns();
  29.         $sortable = $this->get_sortable_columns();
  30.  
  31.         $data = $this->get_all_articles();
  32.         usort( $data, array( $this, 'sort_data' ) );
  33.  
  34.          $perPage = 10;
  35.          $currentPage = $this->get_pagenum();
  36.          $totalItems = count($data);
  37.  
  38.          $this->set_pagination_args( array(
  39.              'total_items' => $totalItems,
  40.              'per_page'    => $perPage
  41.          ) );
  42.  
  43.          $data = array_slice($data,(($currentPage-1)*$perPage),$perPage);
  44.  
  45.         $this->_column_headers = array($columns, $hidden, $sortable);
  46.         $this->items = $data;
  47.     }
  48.  
  49.     function record_count() {
  50.         global $wpdb;
  51.  
  52.         $query = "SELECT COUNT(*) FROM wp_copywriter_articles";
  53.  
  54.         return $wpdb->get_var( $query );
  55. }
  56.  
  57.  
  58.     function no_articles() {
  59.     _e( 'Brak artykułów', 'sp' );
  60. }
  61.  
  62.  function column_default( $item, $column_name ) {
  63.     switch ( $column_name ) {
  64.         case 'article_id':
  65.         case 'article_name':
  66.         case 'article_source':
  67.         case 'category_name':
  68.             return $item[ $column_name ];
  69.         default:
  70.             return print_r( $item, true );
  71.     }
  72. }
  73.  
  74.  function column_name( $item ) {
  75.     $delete_nonce = wp_create_nonce( 'delete_article' );
  76.     $title = '<strong>' . $item['article_name'] . '</strong>';
  77.     $actions = [
  78.         'edit' => sprintf('<a href="?page=%s&id=%s">Edit</a>', 'edytuj-artykul', $item['article_id']),
  79.         '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)
  80.     ];
  81.     return $title. $this->row_actions( $actions );
  82. }
  83.  
  84.  
  85. function delete_article(){
  86.     global $wpdb;
  87.  
  88.     // Ensure we can verify our nonce.
  89.     if( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'delete_article' ) ) {
  90.         wp_die( esc_html__( 'Could not verify request. Please try again.' ) );
  91.  
  92.     // Ensure we have an article ID provided.
  93.     } else if( ! isset( $_REQUEST['article_id'] ) ) {
  94.         wp_die( esc_html__( 'Could not find provided article ID. Please try again.' ) );
  95.  
  96.     // Ensure we're able to delete the actual article.
  97.     } else if( false === $wpdb->delete( 'wp_copywriter_articles', array( 'article_id' => $_REQUEST['article_id'] ) ) ) {
  98.         wp_die( esc_html__( 'Could not delete the provided article. Please try again.' ) );
  99.     }
  100.  
  101.     // Redirect back to original page with 'success' $_GET
  102.     wp_safe_redirect( add_query_arg( array(
  103.         'page' => ( isset( $_REQUEST['page'] ) ) ? $_REQUEST['page'] : '404',
  104.         'success' => 1,
  105.     ), admin_url( 'admin.php' ) ) );
  106.     exit();
  107.    
  108.     // $article_id = $item['article_id'];
  109.     // global $wpdb;
  110.     //  $table = 'wp_copywriter_articles';
  111.     //  $wpdb->query("DELETE FROM $table WHERE article_id = %d", $article_id);
  112. }
  113.  
  114. function column_cb( $item ) {
  115.     return sprintf(
  116.     '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['article_id']
  117.     );
  118.     }
  119.  
  120. function get_columns() {
  121.     $columns = array(
  122.         'cb'      => '<input type="checkbox" />',
  123.         'name' => 'Nazwa',
  124.         'article_source' => 'Źródło',
  125.         'category_name' => 'Kategoria',
  126.     );
  127.     return $columns;
  128. }
  129.  
  130. function get_hidden_columns()
  131. {
  132.     return array();
  133. }
  134.  
  135. function get_sortable_columns()
  136. {
  137.     return array(
  138.         'name' => array('article_name', false),
  139.         'article_source' => array('article_source', false),
  140.         'category_name' => array('category_name', false)
  141.     );
  142. }
  143.  
  144. private function sort_data($a, $b)
  145. {
  146.     $orderby = 'article_name';
  147.     $order = 'asc';
  148.  
  149.             // If orderby is set, use this as the sort column
  150.             if(!empty($_GET['orderby']))
  151.             {
  152.                 $orderby = $_GET['orderby'];
  153.             }
  154.             // If order is set use this as the order
  155.             if(!empty($_GET['order']))
  156.             {
  157.                 $order = $_GET['order'];
  158.             }
  159.             $result = strcmp( $a[$orderby], $b[$orderby] );
  160.  
  161.             if($order === 'asc')
  162.             {
  163.                 return $result;
  164.             }
  165.             return -$result;
  166.     }
  167. }
  168. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement