Passed
Push — master ( c65969...fb6e64 )
by Nirjhar
08:27 queued 05:37
created

Plugin/Lib/table.php (1 issue)

Labels
Severity
1
<?php
2
namespace NirjharLo\Cgss\Lib;
3
4
if ( ! defined( 'ABSPATH' ) ) exit;
5
6
7
use \WP_List_Table;
0 ignored issues
show
The type 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...
8
9
/**
10
 * Implimentation of WordPress inbuilt functions for creating an extension of a default table class.
11
 *
12
 * $Table = new CGSS_TABLE();
13
 * $Table->prepare_items();
14
 * $Table->display();
15
 *
16
 */
17
if ( ! class_exists( 'CGSS_TABLE' ) ) {
18
19
	final class Table extends WP_List_Table {
20
21
22
		public function __construct() {
23
24
			parent::__construct( [
25
				'singular' => __( 'Post', 'cgss' ),
26
				'plural'   => __( 'Posts', 'cgss' ),
27
				'ajax'     => false,
28
			] );
29
		}
30
31
32
33
		//fetch the data using custom named method function
34
		public static function get_posts( $per_page = 10, $page_number = 1 ) {
35
36
			global $wpdb;
37
38
			$page = isset($_GET['page']) ? substr($_GET['page'], 9) : 'post';
39
40
			//Build the db query base
41
			$sql = "SELECT * FROM {$wpdb->prefix}posts";
42
			$sql .= " QUERIES WHERE post_status='publish' AND post_type='$page'";
43
44
			//Set filters in the query using $_REQUEST
45
			if ( ! empty( $_REQUEST['orderby'] ) ) {
46
				$sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] );
47
				$sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC';
48
			}
49
			$sql .= " LIMIT $per_page";
50
			$sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
51
52
			//get the data from database
53
			$result = $wpdb->get_results( $sql, 'ARRAY_A' );
54
55
			// FETCH POST META DATA AND MERGE IT WITH RESULTS
56
			$result = SELF::get_post_meta_data($result);
57
58
			return $result;
59
		}
60
61
62
63
		//If there is no data to show
64
		public function no_items() {
65
66
			_e( 'No Published Posts are available.', 'cgss' );
67
		}
68
69
70
71
		//How many rows are present there
72
		public static function record_count() {
73
74
			global $wpdb;
75
76
			//Take pivotal from URL
77
			$page = isset($_GET['page']) ? substr($_GET['page'], 9) : 'post';
78
79
			//Build the db query base
80
			$sql = "SELECT COUNT(*) FROM {$wpdb->prefix}posts";
81
			$sql .= " QUERIES WHERE post_status='publish' AND post_type='$page'";
82
83
			return $wpdb->get_var( $sql );
84
		}
85
86
87
88
		//Display columns content
89
		public function column_name( $item ) {
90
91
			$title = sprintf( '<strong>%s</strong>', $item['post_title'] );
92
93
			//Change the page instruction where you want to show it
94
			$actions = array(
95
					'scan' => sprintf( '<a href="?page='.$_GET['page'].'&scan='.$item['ID'].'" target="_blank">%s</a>', __( 'Scan', 'cgss' ) ),
96
					'view' => sprintf( '<a href="'.get_permalink($item['ID']).'" target="_blank">%s</a>', __( 'View', 'cgss' ) )
97
					);
98
			return $title . $this->row_actions( $actions );
99
		}
100
101
102
103
		//set coulmns name
104
		public function column_default( $item, $column_name ) {
105
106
			switch ( $column_name ) {
107
108
				case 'post_title':
109
					//This is the first column
110
					return $this->column_name( $item );
111
				case 'focus':
112
				case 'word':
113
				case 'link':
114
				case 'image':
115
				case 'share':
116
					return $item[ $column_name ];
117
				case 'time':
118
					return $item[ $column_name ];
119
				default:
120
					//Show the whole array for troubleshooting purposes
121
					return print_r( $item, true );
122
			}
123
		}
124
125
126
127
		//Columns callback
128
		public function get_columns() {
129
130
			$columns = array(
131
							'post_title'	=> __( 'Post', 'cgss' ),
132
							'focus'	=> __( 'Focus', 'cgss' ),
133
							'word'	=> __( 'Words', 'cgss' ),
134
							'link'	=> __( 'Links', 'cgss' ),
135
							'image'	=> __( 'Images', 'cgss' ),
136
							'share'	=> __( 'Shares', 'cgss' ),
137
							'time'	=> __( 'Time(s)', 'cgss' ),
138
139
						);
140
			return $columns;
141
		}
142
143
144
145
		//Decide columns to be sortable by array input
146
		public function get_sortable_columns() {
147
148
			$sortable_columns = array(
149
				'post_title' => array( 'post_title', true ),
150
			);
151
			return $sortable_columns;
152
		}
153
154
155
156
		//Prapare the display variables for screen options
157
		public function prepare_items() {
158
159
			$this->_column_headers = $this->get_column_info();
160
161
			/** Process bulk action */
162
			$per_page     = $this->get_items_per_page( 'post_per_page', 5 );
163
			$current_page = $this->get_pagenum();
164
			$total_items  = self::record_count();
165
			$this->set_pagination_args( array(
166
				'total_items' => $total_items,
167
				'per_page'    => $per_page,
168
			) );
169
170
			$this->items = self::get_posts( $per_page, $current_page );
171
		}
172
173
174
175
		public static function get_post_meta_data($result) {
176
177
			$IDs = array_column($result, 'ID');
178
			$titles = array_column($result, 'post_title');
179
180
			$empty_metas = array(
181
							'text' => array(
182
										'count' => '--',
183
										'top_key' => '--',
184
										'links' => array( 'count' => '--' ),
185
										),
186
							'design' => array(
187
										'image' => array( 'count' => '--' )
188
										),
189
							'social' => '--',
190
							'speed' => array( 'down_time' => '--' ),
191
							);
192
193
			$metas = array();
194
			foreach ($IDs as $post_id) {
195
				$meta = get_post_meta( $post_id, 'cgss_scan_result', true );
196
				$metas[] = is_array($meta) ? $meta : $empty_metas;
197
			}
198
			$text = array_column($metas, 'text');
199
			$words = array_column($text, 'count');
200
			$focus = array_column($text, 'top_key');
201
			$link = array_column($text, 'links');
202
			$link_count = array_column($link, 'count');
203
204
			$design = array_column($metas, 'design');
205
			$image = array_column($design, 'image');
206
			$image_count = array_column($image, 'count');
207
208
			$social = array_column($metas, 'social');
209
			$share = array_sum($social);
210
211
			$speed = array_column($metas, 'speed');
212
			$res_time = array_column($speed, 'down_time');
213
214
			$result = array();
215
			foreach ($IDs as $key => $ID) {
216
				$temp = array();
217
				$temp['ID'] = $ID;
218
				$temp['post_title'] = $titles[$key];
219
				$temp['focus'] = $focus[$key];
220
				$temp['word'] = $words[$key];
221
				$temp['link'] = $link_count[$key];
222
				$temp['image'] = $image_count[$key];
223
				$temp['share'] = $share[$key];
224
				$temp['time'] = $res_time[$key];
225
226
				$result[] = $temp;
227
			}
228
			return $result;
229
		}
230
	}
231
} ?>
232