示例#1
0
 public static function find($args = '')
 {
     global $wpdb;
     $query = "SELECT *, COUNT(url) as total FROM {$wpdb->fortyfour_logs}";
     $per_page = 10;
     $where = '1=1';
     $orderby = !empty($_GET["orderby"]) ? $args["orderby"] : 'id';
     $order = !empty($_GET["order"]) ? $args["order"] : 'DESC';
     if (isset($_GET['s']) && !empty($_GET['s'])) {
         $where .= fortyfourwp_search_sql(sanitize_text_field($_GET['s']), array('url', 'redirect_url'));
     }
     $query .= " WHERE {$where}";
     $query .= ' GROUP BY (url)';
     if (!empty($orderby) & !empty($order)) {
         $query .= ' ORDER BY ' . $orderby . ' ' . $order;
     }
     self::$found_items = $found_items = $wpdb->query($query);
     $paged = !empty($_GET["paged"]) ? sanitize_text_field($_GET["paged"]) : '';
     //Page Number
     if (empty($paged) || !is_numeric($paged) || $paged <= 0) {
         $paged = 1;
     }
     //How many pages do we have in total?
     $totalpages = ceil($found_items / $per_page);
     //adjust the query to take pagination into account
     if (!empty($paged) && !empty($per_page)) {
         $offset = ($paged - 1) * $per_page;
         $query .= ' LIMIT ' . (int) $offset . ',' . (int) $per_page;
     }
     // echo $query;
     $posts = $wpdb->get_results($query);
     // echo $query;
     $objs = array();
     foreach ((array) $posts as $post) {
         $objs[] = new self($post);
     }
     // print_r($objs);
     return $objs;
 }
 function prepare_items()
 {
     $per_page = 10;
     $columns = $this->get_columns();
     $hidden = array();
     $sortable = $this->get_sortable_columns();
     $this->_column_headers = array($columns, $hidden, $sortable);
     $args = array('posts_per_page' => $per_page, 'orderby' => 'id', 'order' => 'DESC', 'offset' => ($this->get_pagenum() - 1) * $per_page);
     if (!empty($_REQUEST['s'])) {
         $args['s'] = sanitize_text_field($_REQUEST['s']);
     }
     if (!empty($_REQUEST['orderby'])) {
         $args['orderby'] = sanitize_text_field($_REQUEST['orderby']);
     }
     if (!empty($_REQUEST['order'])) {
         if ('asc' == strtolower($_REQUEST['order'])) {
             $args['order'] = 'ASC';
         } elseif ('desc' == strtolower($_REQUEST['order'])) {
             $args['order'] = 'DESC';
         }
     }
     $this->items = FORTYFOURWP_VISITS::find($args);
     $total_items = FORTYFOURWP_VISITS::$found_items;
     $total_pages = ceil($total_items / $per_page);
     $this->set_pagination_args(array('total_items' => $total_items, 'total_pages' => $total_pages, 'per_page' => $per_page));
 }