__( 'Subscription', 'frontend-admin' ), //singular name of the listed records 'plural' => __( 'Subscriptions', 'frontend-admin' ), //plural name of the listed records 'ajax' => false //does this table support ajax? ] ); } /** * Retrieve subscriptions data from the database * * @param int $per_page * @param int $page_number * * @return mixed */ public static function get_subscriptions( $per_page = 20, $page_number = 1 ) { global $wpdb; $sql = "SELECT * FROM {$wpdb->prefix}fea_subscriptions"; // the column name is reduced to letters, digits and underscores before it is used as an identifier $orderby = ! empty( $_REQUEST['orderby'] ) ? str_replace( '-', '', sanitize_key( wp_unslash( $_REQUEST['orderby'] ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only list sorting. if ( $orderby ) { $order = ! empty( $_REQUEST['order'] ) && 'desc' === sanitize_key( wp_unslash( $_REQUEST['order'] ) ) ? 'DESC' : 'ASC'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only list sorting. $sql .= " ORDER BY `$orderby` $order"; } else { $sql .= ' ORDER BY created_at DESC'; } $sql .= $wpdb->prepare( ' LIMIT %d OFFSET %d', $per_page, ( $page_number - 1 ) * $per_page ); $result = $wpdb->get_results( $sql, 'ARRAY_A' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- values are added with $wpdb->prepare() and identifiers come from an allowlist above. return $result; } /** * Delete a subscription record. * * @param int $id subscription ID */ public static function delete_subscription( $id ) { global $wpdb; $wpdb->delete( "{$wpdb->prefix}fea_subscriptions", ['id' => $id ], ['%d'] ); } /** * Returns the count of records in the database. * * @return null|string */ public static function record_count() { global $wpdb; $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}fea_subscriptions"; return $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- only the table prefix is interpolated. } /** Text displayed when no subscription data is available */ public function no_items() { esc_html_e( 'No subscriptions avaliable.', 'frontend-admin' ); } /** * Render a column when no column specific method exist. * * @param array $item * @param string $column_name * * @return mixed */ public function column_default( $item, $column_name ) { switch( $column_name ){ case 'created_at': $time_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); return gmdate( $time_format, strtotime( $item[ $column_name ] ) ); case 'expires_after': if( $item['expires_after'] ){ $time_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); return gmdate( $time_format, strtotime( $item[ $column_name ] ) ); }else{ return __( 'Never', 'frontend-admin' ); } default: return $item[ $column_name ]; } } /** * Gets the name of the default primary column. * * @since 4.3.0 * * @return string Name of the default primary column, in this case, 'title'. */ protected function get_default_primary_column_name() { return 'title'; } /** * Associative array of columns * * @return array */ function get_columns() { $columns = [ 'title' => __( 'Title', 'frontend-admin' ), 'description' => __( 'Description', 'frontend-admin' ), ]; return $columns; } /** * Handles data query and filter, sorting, and pagination. */ public function prepare_items() { $this->_column_headers = $this->get_column_info(); /** Process bulk action */ $this->process_bulk_action(); $perpage = $this->get_items_per_page( 'subscriptions_per_page', 20 ); $current_page = $this->get_pagenum(); $total_items = self::record_count(); $this->set_pagination_args( [ 'total_items' => $total_items, //WE have to calculate the total number of items 'per_page' => $perpage //WE have to determine how many items to show on a page ] ); $this->items = self::get_subscriptions( $perpage, $current_page ); $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array($columns, $hidden, $sortable); } public function process_bulk_action() { //Detect when a bulk action is being triggered... if ( 'delete' === $this->current_action() ) { // In our file that handles the request, verify the nonce. $nonce = esc_attr( $_REQUEST['_wpnonce'] ); if ( ! wp_verify_nonce( $nonce, 'sp_delete_subscription' ) ) { die( 'Go get a life script kiddies' ); } else { self::delete_subscription( absint( $_GET['subscription'] ) ); // esc_url_raw() is used to prevent converting ampersand in url to "#038;" // add_query_arg() return the current url wp_redirect( esc_url_raw(add_query_arg()) ); exit; } } // If the delete bulk action is triggered if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' ) ) { $delete_ids = esc_sql( $_POST['bulk-delete'] ); // loop over the array of record IDs and delete them foreach ( $delete_ids as $id ) { self::delete_subscription( $id ); } // esc_url_raw() is used to prevent converting ampersand in url to "#038;" // add_query_arg() return the current url wp_redirect( esc_url_raw(add_query_arg()) ); exit; } } } fea_instance()->subscriptions_list = new Subscriptions_List; endif;