Issues (196)

plugin/Lib/Table.php (21 issues)

1
<?php
2
namespace NirjharLo\WP_Plugin_Framework\Lib;
3
4
/**
5
 * Implimentation of WordPress inbuilt functions for creating an extension of a default table class.
6
 *
7
 * $my_plugin_name_table = new PLUGIN_TABLE();
8
 * $my_plugin_name_table->prepare_items();
9
 * $my_plugin_name_table->display();
10
 *
11
 * @author     Nirjhar Lo
12
 * @package    wp-plugin-framework
13
 */
14
if ( ! class_exists( 'Table' ) ) {
15
16
	if ( ! class_exists( 'WP_List_Table' ) ) {
17
    	require_once( ABSPATH . 'wp-admin/includes/screen.php' );
0 ignored issues
show
The constant NirjharLo\WP_Plugin_Framework\Lib\ABSPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
18
    	require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
19
	}
20
21
	final class Table extends WP_List_Table {
0 ignored issues
show
The type NirjharLo\WP_Plugin_Framework\Lib\WP_List_Table was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
24
		/**
25
		 * Instantiate the table
26
		 *
27
		 * @return Void
28
		 */
29
		public function __construct() {
30
31
			parent::__construct( [
32
				'singular' => __( 'Name', 'textdomain' ),
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
				'singular' => /** @scrutinizer ignore-call */ __( 'Name', 'textdomain' ),
Loading history...
33
				'plural'   => __( 'Names', 'textdomain' ),
34
				'ajax'     => false,
35
			] );
36
		}
37
38
39
		/**
40
		 * Fetch the data using custom named method function
41
		 *
42
		 * @return Array
43
		 */
44
		public static function get_Table( $per_page = 5, $page_number = 1 ) {
45
46
			global $wpdb;
47
48
			//Take pivotal from URL
49
			$link = ( isset( $_GET['link'] ) ? $_GET['link'] : 'link' );
50
51
			//Build the db query base
52
			$sql = "SELECT * FROM {$wpdb->prefix}wordpress_table";
53
			$sql .= " QUERIES with $link'";
54
55
			//Set filters in the query using $_REQUEST
56
			if ( ! empty( $_REQUEST['orderby'] ) ) {
57
				$sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] );
0 ignored issues
show
The function esc_sql was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
				$sql .= ' ORDER BY ' . /** @scrutinizer ignore-call */ esc_sql( $_REQUEST['orderby'] );
Loading history...
58
				$sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC';
59
			}
60
			$sql .= " LIMIT $per_page";
61
			$sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
62
63
			//get the data from database
64
			$result = $wpdb->get_results( $sql, 'ARRAY_A' );
65
66
			return $result;
67
		}
68
69
70
		/**
71
		 * Delete individual data
72
		 *
73
		 * @return Void
74
		 */
75
		public static function delete_url( $id ) {
76
77
			global $wpdb;
78
79
			$wpdb->delete( "{$wpdb->prefix}wordpress_table", array( 'ID' => $id ), array( '%s' ) );
80
		}
81
82
83
		/**
84
		 * If there is no data to show
85
		 *
86
		 * @return String
87
		 */
88
		public function no_items() {
89
90
			_e( 'No Items Added yet.', 'textDomain' );
0 ignored issues
show
The function _e was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
			/** @scrutinizer ignore-call */ 
91
   _e( 'No Items Added yet.', 'textDomain' );
Loading history...
91
		}
92
93
94
		/**
95
		 * How many rows are present there
96
		 *
97
		 * @return Int
98
		 */
99
		public static function record_count() {
100
101
			global $wpdb;
102
103
			//Take pivotal from URL
104
			$link = ( isset( $_GET['link'] ) ? $_GET['link'] : 'link' );
105
106
			//Build the db query base
107
			$sql = "SELECT COUNT(*) FROM {$wpdb->prefix}wordpress_table";
108
			$sql .= " QUERIES with $link'";
109
110
			return $wpdb->get_var( $sql );
111
		}
112
113
114
		/**
115
		 * Display columns content
116
		 *
117
		 * @return Html
0 ignored issues
show
The type NirjharLo\WP_Plugin_Framework\Lib\Html was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
118
		 */
119
		public function column_name( $item ) {
120
121
			$delete_nonce = wp_create_nonce( 'delete_url' );
0 ignored issues
show
The function wp_create_nonce was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
			$delete_nonce = /** @scrutinizer ignore-call */ wp_create_nonce( 'delete_url' );
Loading history...
122
			$title = sprintf( '<strong>%s</strong>', $item['item_name'] );
123
124
			//Change the page instruction where you want to show it
125
			$actions = array(
126
					'delete' => sprintf( '<a href="?page=%s&action=%s&instruction=%s&_wpnonce=%s">%s</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['ID'] ), $delete_nonce, __( 'Delete', 'textdomain' ) )
0 ignored issues
show
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
					'delete' => sprintf( '<a href="?page=%s&action=%s&instruction=%s&_wpnonce=%s">%s</a>', esc_attr( $_REQUEST['page'] ), 'delete', /** @scrutinizer ignore-call */ absint( $item['ID'] ), $delete_nonce, __( 'Delete', 'textdomain' ) )
Loading history...
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
					'delete' => sprintf( '<a href="?page=%s&action=%s&instruction=%s&_wpnonce=%s">%s</a>', /** @scrutinizer ignore-call */ esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['ID'] ), $delete_nonce, __( 'Delete', 'textdomain' ) )
Loading history...
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
					'delete' => sprintf( '<a href="?page=%s&action=%s&instruction=%s&_wpnonce=%s">%s</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['ID'] ), $delete_nonce, /** @scrutinizer ignore-call */ __( 'Delete', 'textdomain' ) )
Loading history...
127
					);
128
129
			return $title . $this->row_actions( $actions );
0 ignored issues
show
Bug Best Practice introduced by
The expression return $title . $this->row_actions($actions) returns the type string which is incompatible with the documented return type NirjharLo\WP_Plugin_Framework\Lib\Html.
Loading history...
130
		}
131
132
133
		/**
134
		 * set coulmns name
135
		 *
136
		 * @return Html
137
		 */
138
		public function column_default( $item, $column_name ) {
139
140
			switch ( $column_name ) {
141
142
				case 'name':
143
					//This is the first column
144
					return $this->column_name( $item );
145
				case 'caseOne':
146
				case 'caseTwo':
147
				case 'caseThree':
148
					return $item[ $column_name ];
149
150
				default:
151
152
					//Show the whole array for troubleshooting purposes
153
					return print_r( $item, true );
0 ignored issues
show
Bug Best Practice introduced by
The expression return print_r($item, true) returns the type string|true which is incompatible with the documented return type NirjharLo\WP_Plugin_Framework\Lib\Html.
Loading history...
154
			}
155
		}
156
157
158
		/**
159
		 * Set checkboxes to delete
160
		 *
161
		 * @return Html
162
		 */
163
		public function column_cb( $item ) {
164
165
			return sprintf( '<input type="checkbox" name="bulk-select[]" value="%s" />', $item['ID'] );
0 ignored issues
show
Bug Best Practice introduced by
The expression return sprintf('<input t...="%s" />', $item['ID']) returns the type string which is incompatible with the documented return type NirjharLo\WP_Plugin_Framework\Lib\Html.
Loading history...
166
		}
167
168
169
		/**
170
		 * Columns callback
171
		 *
172
		 * @return Array
173
		 */
174
		public function get_columns() {
175
176
			$columns = array(
177
							'cb'		=> '<input type="checkbox" />',
178
							'name'	=> __( 'Name', 'textdomain' ),
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
							'name'	=> /** @scrutinizer ignore-call */ __( 'Name', 'textdomain' ),
Loading history...
179
							'caseOne'	=> __( 'Case One', 'textdomain' ),
180
							'caseTwo'	=> __( 'Case Two', 'textdomain' ),
181
							'caseThree'	=> __( 'Case Three', 'textdomain' ),
182
						);
183
184
			return $columns;
185
		}
186
187
188
		/**
189
		 * Decide columns to be sortable by array input
190
		 *
191
		 * @return Array
192
		 */
193
		public function get_sortable_columns() {
194
195
			$sortable_columns = array(
196
				'name' => array( 'name', true ),
197
				'caseOne' => array( 'caseOne', false ),
198
				'caseTwo' => array( 'caseTwo', false ),
199
			);
200
201
			return $sortable_columns;
202
		}
203
204
205
		/**
206
		 * Determine bulk actions in the table dropdown
207
		 *
208
		 * @return Array
209
		 */
210
		public function get_bulk_actions() {
211
212
			$actions = array( 'bulk-delete' => 'Delete'	);
213
214
			return $actions;
215
		}
216
217
218
		/**
219
		 * Prapare the display variables for screen options
220
		 *
221
		 * @return Void
222
		 */
223
		public function prepare_items() {
224
225
			$this->_column_headers = $this->get_column_info();
0 ignored issues
show
Bug Best Practice introduced by
The property _column_headers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
226
227
			/** Process bulk action */
228
			$this->process_bulk_action();
229
			$per_page     = $this->get_items_per_page( 'option_name_per_page', 5 );
230
			$current_page = $this->get_pagenum();
231
			$total_items  = self::record_count();
232
			$this->_column_headers = [
233
    			$this->get_columns(),
234
    			[], // hidden columns
235
    			$this->get_sortable_columns(),
236
    			$this->get_primary_column_name(),
237
			];
238
			$this->set_pagination_args( array(
239
				'total_items' => $total_items,
240
				'per_page'    => $per_page,
241
			) );
242
243
			$this->items = self::get_Table( $per_page, $current_page );
244
		}
245
246
247
		/**
248
		 * Process bulk action
249
		 *
250
		 * @return Void
251
		 */
252
		public function process_bulk_action() {
253
254
			//Detect when a bulk action is being triggered...
255
			if ( 'delete' === $this->current_action() ) {
256
257
				//In our file that handles the request, verify the nonce.
258
				$nonce = esc_attr( $_REQUEST['_wpnonce'] );
0 ignored issues
show
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

258
				$nonce = /** @scrutinizer ignore-call */ esc_attr( $_REQUEST['_wpnonce'] );
Loading history...
259
260
				if ( ! wp_verify_nonce( $nonce, 'delete_url' ) ) {
0 ignored issues
show
The function wp_verify_nonce was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

260
				if ( ! /** @scrutinizer ignore-call */ wp_verify_nonce( $nonce, 'delete_url' ) ) {
Loading history...
261
					die( 'Go get a live script kiddies' );
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
262
				} else {
263
					self::delete_url( absint( $_GET['instruction'] ) ); //Remember the instruction param from column_name method
0 ignored issues
show
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

263
					self::delete_url( /** @scrutinizer ignore-call */ absint( $_GET['instruction'] ) ); //Remember the instruction param from column_name method
Loading history...
264
				}
265
			}
266
267
			//If the delete bulk action is triggered
268
			if ( isset( $_POST['action'] ) ) {
269
				if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) ) {
270
					$delete_ids = esc_sql( $_POST['bulk-select'] );
0 ignored issues
show
The function esc_sql was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

270
					$delete_ids = /** @scrutinizer ignore-call */ esc_sql( $_POST['bulk-select'] );
Loading history...
271
					foreach ( $delete_ids as $id ) {
272
						self::delete_url( $id );
273
					}
274
				}
275
			}
276
		}
277
	}
278
} ?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
279