1 <?php
2 /**
3 * Gateway Error Log View Class
4 *
5 * @package EDD
6 * @subpackage Admin/Reports
7 * @copyright Copyright (c) 2013, Pippin Williamson
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 */
10
11 // Exit if accessed directly
12 if ( ! defined( 'ABSPATH' ) ) exit;
13
14 // Load WP_List_Table if not loaded
15 if ( ! class_exists( 'WP_List_Table' ) ) {
16 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
17 }
18
19 /**
20 * EDD_Gateway_Error_Log_Table Class
21 *
22 * Renders the gateway errors list table
23 *
24 * @access private
25 * @since 1.4
26 */
27 class EDD_Gateway_Error_Log_Table extends WP_List_Table {
28 /**
29 * Number of items per page
30 *
31 * @var int
32 * @since 1.4
33 */
34 public $per_page = 30;
35
36 /**
37 * Get things started
38 *
39 * @access public
40 * @since 1.4
41 * @see WP_List_Table::__construct()
42 * @return void
43 */
44 public function __construct() {
45 global $status, $page;
46
47 // Set parent defaults
48 parent::__construct( array(
49 'singular' => edd_get_label_singular(), // Singular name of the listed records
50 'plural' => edd_get_label_plural(), // Plural name of the listed records
51 'ajax' => false // Does this table support ajax?
52 ) );
53 }
54
55 /**
56 * This function renders most of the columns in the list table.
57 *
58 * @access public
59 * @since 1.4
60 *
61 * @param array $item Contains all the data of the discount code
62 * @param string $column_name The name of the column
63 *
64 * @return string Column Name
65 */
66 public function column_default( $item, $column_name ) {
67 switch ( $column_name ) {
68 case 'error' :
69 return get_the_title( $item['ID'] ) ? get_the_title( $item['ID'] ) : __( 'Payment Error', 'edd' );
70 default:
71 return $item[ $column_name ];
72 }
73 }
74
75 /**
76 * Output Error Message Column
77 *
78 * @access public
79 * @since 1.4.4
80 * @param array $item Contains all the data of the log
81 * @return void
82 */
83 public function column_message( $item ) {
84 ?>
85 <a href="#TB_inline?width=640&inlineId=log-message-<?php echo $item['ID']; ?>" class="thickbox" title="<?php _e( 'View Log Message', 'edd' ); ?> "><?php _e( 'View Log Message', 'edd' ); ?></a>
86 <div id="log-message-<?php echo $item['ID']; ?>" style="display:none;">
87 <?php
88
89 $log_message = get_post_field( 'post_content', $item['ID'] );
90 $serialized = strpos( $log_message, '{"' );
91
92 // Check to see if the log message contains serialized information
93 if ( $serialized !== false ) {
94 $length = strlen( $log_message ) - $serialized;
95 $intro = substr( $log_message, 0, - $length );
96 $data = substr( $log_message, $serialized, strlen( $log_message ) - 1 );
97
98 echo wpautop( $intro );
99 echo wpautop( __( '<strong>Log data:</strong>', 'edd' ) );
100 echo '<div style="word-wrap: break-word;">' . wpautop( $data ) . '</div>';
101 } else {
102 // No serialized data found
103 echo wpautop( $log_message );
104 }
105 ?>
106 </div>
107 <?php
108 }
109
110 /**
111 * Retrieve the table columns
112 *
113 * @access public
114 * @since 1.4
115 * @return array $columns Array of all the list table columns
116 */
117 public function get_columns() {
118 $columns = array(
119 'ID' => __( 'Log ID', 'edd' ),
120 'payment_id' => __( 'Payment ID', 'edd' ),
121 'error' => __( 'Error', 'edd' ),
122 'message' => __( 'Error Message', 'edd' ),
123 'gateway' => __( 'Gateway', 'edd' ),
124 'date' => __( 'Date', 'edd' )
125 );
126
127 return $columns;
128 }
129
130 /**
131 * Retrieve the current page number
132 *
133 * @access public
134 * @since 1.4
135 * @return int Current page number
136 */
137 public function get_paged() {
138 return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
139 }
140
141 /**
142 * Outputs the log views
143 *
144 * @access public
145 * @since 1.4
146 * @return void
147 */
148 public function bulk_actions() {
149 // These aren't really bulk actions but this outputs the markup in the right place
150 edd_log_views();
151 }
152
153 /**
154 * Gets the log entries for the current view
155 *
156 * @access public
157 * @since 1.4
158 * @global object $edd_logs EDD Logs Object
159 * @return array $logs_data Array of all the Log entires
160 */
161 public function get_logs() {
162 global $edd_logs;
163
164 $logs_data = array();
165 $paged = $this->get_paged();
166 $log_query = array(
167 'log_type' => 'gateway_error',
168 'paged' => $paged
169 );
170
171 $logs = $edd_logs->get_connected_logs( $log_query );
172
173 if ( $logs ) {
174 foreach ( $logs as $log ) {
175
176 $logs_data[] = array(
177 'ID' => $log->ID,
178 'payment_id' => $log->post_parent,
179 'error' => 'error',
180 'gateway' => edd_get_payment_gateway( $log->post_parent ),
181 'date' => $log->post_date
182 );
183 }
184 }
185
186 return $logs_data;
187 }
188
189 /**
190 * Setup the final data for the table
191 *
192 * @access public
193 * @since 1.4
194 * @global object $edd_logs EDD Logs Object
195 * @uses EDD_Gateway_Error_Log_Table::get_columns()
196 * @uses WP_List_Table::get_sortable_columns()
197 * @uses EDD_Gateway_Error_Log_Table::get_pagenum()
198 * @uses EDD_Gateway_Error_Log_Table::get_logs()
199 * @uses EDD_Gateway_Error_Log_Table::get_log_count()
200 * @return void
201 */
202 public function prepare_items() {
203 global $edd_logs;
204
205 $columns = $this->get_columns();
206 $hidden = array(); // No hidden columns
207 $sortable = $this->get_sortable_columns();
208 $this->_column_headers = array( $columns, $hidden, $sortable );
209 $current_page = $this->get_pagenum();
210 $this->items = $this->get_logs();
211 $total_items = $edd_logs->get_log_count( 0, 'gateway_error' );
212
213 $this->set_pagination_args( array(
214 'total_items' => $total_items,
215 'per_page' => $this->per_page,
216 'total_pages' => ceil( $total_items / $this->per_page )
217 )
218 );
219 }
220 }