Test Failed
Push — master ( 315839...9b266f )
by Devin
05:39
created

Give_Stripe_Log_Table   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 254
Duplicated Lines 49.21 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 125
loc 254
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 1
A search_box() 0 2 1
A get_columns() 10 10 1
A column_default() 14 14 3
A column_details() 28 28 2
A display_tablenav() 19 19 2
A get_paged() 0 3 2
A bulk_actions() 0 2 1
A get_logs() 26 26 3
A prepare_items() 16 16 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Give - Stripe Core | Stripe Log View Class
4
 *
5
 * @package    Give
6
 * @subpackage Stripe Core
7
 * @copyright  Copyright (c) 2019, GiveWP
8
 * @license    https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since      2.5.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
// Load WP_List_Table if not loaded.
18
if ( ! class_exists( 'WP_List_Table' ) ) {
19
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
20
}
21
22
/**
23
 * Give_Stripe_Log_Table List Table Class
24
 *
25
 * Renders the stripe log list table
26
 *
27
 * @since 2.5.8
28
 */
29
class Give_Stripe_Log_Table extends WP_List_Table {
30
31
	/**
32
	 * Number of items per page
33
	 *
34
	 * @since  2.5.0
35
	 * @access public
36
	 *
37
	 * @var int
38
	 */
39
	public $per_page = 30;
40
41
	/**
42
	 * Get things started
43
	 *
44
	 * @since  2.5.0
45
	 * @access public
46
	 *
47
	 * @see   WP_List_Table::__construct()
48
	 */
49 View Code Duplication
	public function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
		global $status, $page;
51
52
		// Set parent defaults.
53
		parent::__construct(
54
			array(
55
				'singular' => give_get_forms_label_singular(), // Singular name of the listed records.
56
				'plural'   => give_get_forms_label_plural(), // Plural name of the listed records.
57
				'ajax'     => false, // Does this table support ajax?
58
			)
59
		);
60
	}
61
62
	/**
63
	 * Show the search field
64
	 *
65
	 * @since  2.5.0
66
	 * @access public
67
	 *
68
	 * @param string $text     Label for the search box.
69
	 * @param string $input_id ID of the search box.
70
	 *
71
	 * @return void
72
	 */
73
	public function search_box( $text, $input_id ) {
74
	}
75
76
	/**
77
	 * Retrieve the table columns
78
	 *
79
	 * @access public
80
	 * @since  2.0.8
81
	 *
82
	 * @return array $columns Array of all the list table columns
83
	 */
84 View Code Duplication
	public function get_columns() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
		$columns = array(
86
			'ID'      => __( 'Log ID', 'give' ),
87
			'error'   => __( 'Error', 'give' ),
88
			'date'    => __( 'Date', 'give' ),
89
			'details' => __( 'Process Details', 'give' ),
90
		);
91
92
		return $columns;
93
	}
94
95
	/**
96
	 * This function renders most of the columns in the list table.
97
	 *
98
	 * @access public
99
	 * @since  2.5.0
100
	 *
101
	 * @param array  $item        Contains all the data of the discount code.
102
	 * @param string $column_name The name of the column.
103
	 *
104
	 * @return string Column Name
105
	 */
106 View Code Duplication
	public function column_default( $item, $column_name ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
		switch ( $column_name ) {
108
			case 'ID':
109
				return sprintf(
110
					'<span class="give-item-label give-item-label-gray">%1$s</span>',
111
					esc_attr( $item[ $column_name ] )
112
				);
113
			case 'error':
114
				return esc_attr( $item['title'] );
115
116
			default:
117
				return esc_attr( $item[ $column_name ] );
118
		}
119
	}
120
121
	/**
122
	 * Output Error Message column
123
	 *
124
	 * @access public
125
	 * @since  2.5.0
126
	 *
127
	 * @param array $item Contains all the data of the log.
128
	 *
129
	 * @return void
130
	 */
131 View Code Duplication
	public function column_details( $item ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
		echo wp_kses_post(
133
			Give()->tooltips->render_link(
134
				array(
135
					'label'       => __( 'View Stripe Log', 'give' ),
136
					'tag_content' => '<span class="dashicons dashicons-visibility"></span>',
137
					'link'        => "#TB_inline?width=640&amp;inlineId=log-details-{$item['ID']}",
138
					'attributes'  => array(
139
						'class' => 'thickbox give-error-log-details-link button button-small',
140
					),
141
				)
142
			)
143
		);
144
		?>
145
		<div id="log-details-<?php echo esc_attr( $item['ID'] ); ?>" style="display:none;">
146
			<?php
147
148
			// Print Log Content, if not empty.
149
			if ( ! empty( $item['log_content'] ) ) {
150
				echo sprintf(
151
					'<p><pre>%1$s</pre></div>',
152
					esc_html( $item['log_content'] )
153
				);
154
			}
155
			?>
156
		</div>
157
		<?php
158
	}
159
160
161
	/**
162
	 * Display Tablenav (extended)
163
	 *
164
	 * Display the table navigation above or below the table even when no items in the logs, so nav doesn't disappear
165
	 *
166
	 * @see    : https://github.com/WordImpress/Give/issues/564
167
	 *
168
	 * @since  2.5.0
169
	 * @access protected
170
	 *
171
	 * @param string $which Top or bottom.
172
	 */
173 View Code Duplication
	protected function display_tablenav( $which ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
		if ( 'top' === $which ) {
175
			wp_nonce_field( 'bulk-' . $this->_args['plural'] );
176
		}
177
		?>
178
		<div class="tablenav <?php echo esc_attr( $which ); ?>">
179
180
			<div class="alignleft actions bulkactions">
181
				<?php $this->bulk_actions( $which ); ?>
182
			</div>
183
			<?php
184
			$this->extra_tablenav( $which );
185
			$this->pagination( $which );
186
			?>
187
188
			<br class="clear"/>
189
		</div>
190
		<?php
191
	}
192
193
	/**
194
	 * Retrieve the current page number
195
	 *
196
	 * @access public
197
	 * @since  2.5.0
198
	 *
199
	 * @return int Current page number
200
	 */
201
	public function get_paged() {
202
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
203
	}
204
205
	/**
206
	 * Outputs the log views
207
	 *
208
	 * @param string $which Top or Bottom.
209
	 *
210
	 * @access public
211
	 * @since  2.0.8
212
	 *
213
	 * @return void
214
	 */
215
	public function bulk_actions( $which = '' ) {
216
	}
217
218
	/**
219
	 * Gets the log entries for the current view
220
	 *
221
	 * @access public
222
	 * @since  2.0.8
223
	 *
224
	 * @return array $logs_data Array of all the Log entires
225
	 */
226 View Code Duplication
	public function get_logs() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
		$logs_data = array();
228
		$paged     = $this->get_paged();
229
		$log_query = array(
230
			'log_type'       => 'stripe',
231
			'paged'          => $paged,
232
			'posts_per_page' => $this->per_page,
233
		);
234
235
		$logs = Give()->logs->get_connected_logs( $log_query );
236
237
		if ( $logs ) {
238
			foreach ( $logs as $log ) {
239
240
				$logs_data[] = array(
241
					'ID'          => $log->ID,
242
					'title'       => $log->log_title,
243
					'date'        => $log->log_date,
244
					'log_content' => $log->log_content,
245
					'log_date'    => $log->log_date,
246
				);
247
			}
248
		}
249
250
		return $logs_data;
251
	}
252
253
	/**
254
	 * Setup the final data for the table
255
	 *
256
	 * @access public
257
	 * @since  2.0.8
258
	 * @uses   Give_Stripe_Log_Table::get_columns()
259
	 * @uses   WP_List_Table::get_sortable_columns()
260
	 * @uses   Give_Stripe_Log_Table::get_pagenum()
261
	 * @uses   Give_Stripe_Log_Table::get_logs()
262
	 * @uses   Give_Stripe_Log_Table::get_log_count()
263
	 *
264
	 * @return void
265
	 */
266 View Code Duplication
	public function prepare_items() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
267
		$columns               = $this->get_columns();
268
		$hidden                = array(); // No hidden columns.
269
		$sortable              = $this->get_sortable_columns();
270
		$this->_column_headers = array( $columns, $hidden, $sortable );
271
		$this->items           = $this->get_logs();
272
		$total_items           = Give()->logs->get_log_count( 0, 'stripe' );
273
274
		$this->set_pagination_args(
275
			array(
276
				'total_items' => $total_items,
277
				'per_page'    => $this->per_page,
278
				'total_pages' => ceil( $total_items / $this->per_page ),
279
			)
280
		);
281
	}
282
}
283