PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tab_class.php

https://gitlab.com/mohsentm/downloadchibashi
PHP | 346 lines | 203 code | 69 blank | 74 comment | 94 complexity | 23009b6e0c3c7ab4ddf96e742119e2ac MD5 | raw file
  1. <?php
  2. if ( ! class_exists( 'WP_List_Table' ) ) {
  3. require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  4. }
  5. class Download_Status extends WP_List_Table {
  6. /** Class constructor */
  7. public function __construct() {
  8. parent::__construct( [
  9. 'singular' => __( 'Download', 'sp' ), //singular name of the listed records
  10. 'plural' => __( 'Downloads', 'sp' ), //plural name of the listed records
  11. 'ajax' => false //does this table support ajax?
  12. ] );
  13. }
  14. /**
  15. * Retrieve downloads data from the database
  16. *
  17. * @param int $per_page
  18. * @param int $page_number
  19. *
  20. * @return mixed
  21. */
  22. /*
  23. تعداد دانلود های موفق به تفکیک روز
  24. select count(*),f_datetime from wp_downloadchibashi_files_list group by DATE(f_datetime)
  25. تعداد دانلود های ناموفق با تفکیک روز
  26. select count(*),f_datetime from wp_downloadchibashi_files_list where f_fail =1 group by DATE(f_datetime);
  27. */
  28. public static function get_downloads( $per_page = 5, $page_number = 1 ) {
  29. global $wpdb;
  30. if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "all" )
  31. {
  32. $sql = "select * from {$wpdb->prefix}downloadchibashi_files_list";
  33. if ( ! empty( $_REQUEST['s'] ) ) {
  34. $search = trim($_REQUEST['s']);
  35. $sql .= " WHERE ip = '%$search%' OR f_url LIKE '%$search%' OR (`f_datetime` >= '$search 00:00:00' AND `f_datetime` <= '$search 23:59:59' )";
  36. }
  37. }
  38. else if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "success" )
  39. $sql = "select count(*),f_datetime from {$wpdb->prefix}downloadchibashi_files_list where f_fail IS NULL group by DATE(f_datetime)";
  40. else if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "failed" )
  41. $sql = "select count(*),f_datetime from {$wpdb->prefix}downloadchibashi_files_list where f_fail =1 group by DATE(f_datetime)";
  42. if ( ! empty( $_REQUEST['orderby'] ) ) {
  43. $sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] );
  44. $sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC';
  45. }
  46. else
  47. {
  48. $sql .= ' ORDER BY f_datetime DESC';
  49. }
  50. $sql .= " LIMIT $per_page";
  51. $sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
  52. $result = $wpdb->get_results( $sql, 'ARRAY_A' );
  53. return $result;
  54. }
  55. /**
  56. * Delete a download record.
  57. *
  58. * @param int $id download ID
  59. */
  60. public static function delete_download( $id ) {
  61. global $wpdb;
  62. if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "all" )
  63. $wpdb->delete(
  64. "{$wpdb->prefix}downloadchibashi_files_list",
  65. [ 'f_id' => $id ],
  66. [ '%d' ]
  67. );
  68. else if(!empty( $_REQUEST['status']) && ($_REQUEST['status'] == "success" || $_REQUEST['status'] == "failed" ))
  69. $wpdb->delete(
  70. "{$wpdb->prefix}downloadchibashi_files_list",
  71. [ 'f_datetime' => $id ],
  72. [ '%d' ]
  73. );
  74. }
  75. /**
  76. * Returns the count of records in the database.
  77. *
  78. * @return null|string
  79. */
  80. public static function record_count() {
  81. global $wpdb;
  82. if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "all" )
  83. $sql = "select count(*) from {$wpdb->prefix}downloadchibashi_files_list ";
  84. else if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "success" )
  85. $sql = "select count(*) from {$wpdb->prefix}downloadchibashi_files_list where f_fail IS NULL group by DATE(f_datetime)";
  86. else if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "failed" )
  87. $sql = "select count(*) from {$wpdb->prefix}downloadchibashi_files_list where f_fail =1 group by DATE(f_datetime)";
  88. return $wpdb->get_var( $sql );
  89. }
  90. /** Text displayed when no download data is available */
  91. public function no_items() {
  92. _e( 'No downloads avaliable.', 'sp' );
  93. }
  94. /**
  95. * Render a column when no column specific method exist.
  96. *
  97. * @param array $item
  98. * @param string $column_name
  99. *
  100. * @return mixed
  101. */
  102. public function column_default( $item, $column_name ) {
  103. if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "all" )
  104. switch ( $column_name ) {
  105. case 'f_id':
  106. case 'ip':
  107. case 'f_url':
  108. case 'f_fail ':
  109. case 'f_datetime':
  110. return $item[ $column_name ];
  111. default:
  112. return print_r( $item, true ); //Show the whole array for troubleshooting purposes
  113. }
  114. else if(!empty( $_REQUEST['status']) && ($_REQUEST['status'] == "success" || $_REQUEST['status'] == "failed" ))
  115. switch ( $column_name ) {
  116. case 'f_datetime':
  117. case 'count(*)':
  118. return $item[ $column_name ];
  119. default:
  120. return print_r( $item, true ); //Show the whole array for troubleshooting purposes
  121. }
  122. }
  123. /**
  124. * Render the bulk edit checkbox
  125. *
  126. * @param array $item
  127. *
  128. * @return string
  129. */
  130. function column_cb( $item ) {
  131. if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "all" )
  132. return sprintf(
  133. '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['f_id']
  134. );
  135. else if(!empty( $_REQUEST['status']) && ($_REQUEST['status'] == "success" || $_REQUEST['status'] == "failed" ))
  136. return sprintf(
  137. '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['f_datetime']
  138. );
  139. }
  140. function column_fil_status( $item ) {
  141. if($item['f_fail'] == 1)
  142. {
  143. return sprintf( 'failed' );
  144. }
  145. else
  146. return sprintf( 'successful' );
  147. //return $item['count(*)'] ;
  148. }
  149. /**
  150. * Method for name column
  151. *
  152. * @param array $item an array of DB data
  153. *
  154. * @return string
  155. */
  156. function column_name( $item ) {
  157. $delete_nonce = wp_create_nonce( 'sp_delete_download' );
  158. if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "all" )
  159. {
  160. $title = '<strong>' . $item['ip'] . '</strong>';
  161. $actions = [
  162. 'delete' => sprintf( '<a href="?page=%s&action=%s&download=%s&_wpnonce=%s">Delete</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['f_id'] ), $delete_nonce )
  163. ];
  164. }
  165. else if(!empty( $_REQUEST['status']) && ($_REQUEST['status'] == "success" || $_REQUEST['status'] == "failed" ))
  166. {
  167. $title = '<strong>' . $item['f_datetime'] . '</strong>';
  168. $actions = [
  169. 'delete' => sprintf( '<a href="?page=%s&action=%s&download=%s&_wpnonce=%s">Delete</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['f_datetime'] ), $delete_nonce )
  170. ];
  171. }
  172. return $title . $this->row_actions( $actions );
  173. }
  174. /**
  175. * Associative array of columns
  176. *
  177. * @return array
  178. */
  179. function get_columns() {
  180. if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "all" )
  181. $columns = [
  182. 'cb' => '<input type="checkbox" />',
  183. 'ip' => __( 'IP Address', 'sp' ),
  184. 'f_datetime' => __( 'Date&Time', 'sp' ),
  185. 'f_url' => __( 'Url', 'sp' ),
  186. 'fil_status' => 'Status'
  187. ];
  188. else if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "success")
  189. $columns = [
  190. 'cb' => '<input type="checkbox" />',
  191. 'f_datetime' => __( 'Date&Time', 'sp' ),
  192. 'count(*)' => __( 'Download Success', 'sp' )
  193. ];
  194. else if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "failed" )
  195. $columns = [
  196. 'cb' => '<input type="checkbox" />',
  197. 'f_datetime' => __( 'Date&Time', 'sp' ),
  198. 'count(*)' => __( 'Download Failed', 'sp' )
  199. ];
  200. return $columns;
  201. }
  202. /**
  203. * Columns to make sortable.
  204. *
  205. * @return array
  206. */
  207. public function get_sortable_columns() {
  208. if(!empty( $_REQUEST['status']) && $_REQUEST['status'] == "all" )
  209. $sortable_columns = array(
  210. 'ip' => array( 'ip', true ),
  211. 'f_datetime' => array( 'f_datetime', false ),
  212. 'f_url' => array( 'f_url', false ),
  213. 'f_fail' => array( 'f_fail', false )
  214. );
  215. else if(!empty( $_REQUEST['status']) && ($_REQUEST['status'] == "success" || $_REQUEST['status'] == "failed" ))
  216. $sortable_columns = array(
  217. 'f_datetime' => array( 'f_datetime', false ),
  218. 'count(*)' => array( 'f_url', false ),
  219. );
  220. return $sortable_columns;
  221. }
  222. /**
  223. * Returns an associative array containing the bulk action
  224. *
  225. * @return array
  226. */
  227. public function get_bulk_actions() {
  228. $actions = [
  229. 'bulk-delete' => 'حذف'
  230. ];
  231. return $actions;
  232. }
  233. /**
  234. * Handles data query and filter, sorting, and pagination.
  235. */
  236. public function prepare_items() {
  237. $this->_column_headers = $this->get_column_info();
  238. /** Process bulk action */
  239. $this->process_bulk_action();
  240. $per_page = $this->get_items_per_page( 'downloads_per_page', 20 );
  241. $current_page = $this->get_pagenum();
  242. $total_items = self::record_count();
  243. $this->set_pagination_args( [
  244. 'total_items' => $total_items, //WE have to calculate the total number of items
  245. 'per_page' => $per_page //WE have to determine how many items to show on a page
  246. ] );
  247. $this->items = self::get_downloads( $per_page, $current_page );
  248. }
  249. public function process_bulk_action() {
  250. //Detect when a bulk action is being triggered...
  251. if ( 'delete' === $this->current_action() ) {
  252. // In our file that handles the request, verify the nonce.
  253. $nonce = esc_attr( $_REQUEST['_wpnonce'] );
  254. if ( ! wp_verify_nonce( $nonce, 'sp_delete_download' ) ) {
  255. die( 'Go get a life script kiddies' );
  256. }
  257. else {
  258. self::delete_download( absint( $_GET['download'] ) );
  259. wp_redirect( esc_url( add_query_arg() ) );
  260. exit;
  261. }
  262. }
  263. // If the delete bulk action is triggered
  264. if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' )
  265. || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' )
  266. ) {
  267. $delete_ids = esc_sql( $_POST['bulk-delete'] );
  268. // loop over the array of record IDs and delete them
  269. foreach ( $delete_ids as $id ) {
  270. self::delete_download( $id );
  271. }
  272. //wp_redirect( esc_url( add_query_arg() ) );
  273. //exit;
  274. }
  275. }
  276. }
  277. ?>