PluginProbe
Code Engine – PHP Snippets, AI Functions & Automation for WordPress / 0.0.2
Code Engine – PHP Snippets, AI Functions & Automation for WordPress v0.0.2
0.5.6 0.5.5 0.5.4 0.5.3 0.5.2 0.5.1 0.5.0 0.4.9 0.4.8 0.4.7 0.4.6 trunk 0.0.1 0.0.2 0.2.8 0.2.9 0.3.0 0.3.1 0.3.2 0.3.3 0.3.4 0.3.5 0.3.6 0.3.7 0.3.8 All 32 releases
code-engine / classes / list_table.php

list_table.php in Code Engine – PHP Snippets, AI Functions & Automation for WordPress 0.0.2, at classes/list_table.php

276 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! class_exists( 'WP_List_Table' ) ) {
3 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
4 }
5
6 class Meow_CDEGN_List_Table extends WP_List_Table {
7
8 /** @var array<string> a source list of snippets. */
9 protected $sources = [
10 [ 'source' => 'code_snippets', 'label' => 'Code Snippets' ],
11 [ 'source' => 'wpcode', 'label' => 'WPCode' ],
12 [ 'source' => 'others', 'label' => 'Others' ],
13 ];
14
15 /** @var string default source */
16 protected $default_source = 'code_snippets';
17
18 /** @var Meow_CDEGN_Snippets_Code_Snippets */
19 protected $source_code_snippets = null;
20
21 /** @var Meow_CDEGN_Snippets_Wpcode */
22 protected $source_wpcode = null;
23
24 /** @var array<array> */
25 protected $cdegn_functions = [];
26
27 public function __construct()
28 {
29 // Class Settings
30 $this->source_code_snippets = new Meow_CDEGN_Snippets_Code_Snippets();
31 $this->source_wpcode = new Meow_CDEGN_Snippets_Wpcode();
32
33 // Set the vars from the request
34 global $cdegn_source, $cdegn_page, $cdegn_order, $cdegn_orderBy;
35
36 $cdegn_source = $this->default_source;
37 $sources = array_column( $this->sources, 'source' );
38 if ( isset( $_REQUEST['source'] ) && in_array( sanitize_key( $_REQUEST['source'] ), $sources, true ) ) {
39 $cdegn_source = sanitize_key( $_REQUEST['source'] );
40 }
41 $cdegn_order = 'asc';
42 $cdegn_orderBy = 'id';
43 if ( ! empty( $_REQUEST['order'] ) ) {
44 $cdegn_order = sanitize_key( wp_unslash( $_REQUEST['order'] ) );
45 }
46 if ( ! empty( $_REQUEST['orderby'] ) ) {
47 $cdegn_orderBy = sanitize_key( wp_unslash( $_REQUEST['orderby'] ) );
48 }
49
50 // Set the page
51 $cdegn_page = $this->get_pagenum();
52
53 // Set the filters
54 $filters = [ 'wptexturize', 'convert_smilies', 'convert_chars', 'wpautop', 'shortcode_unautop', 'capital_P_dangit', 'wp_kses_post' ];
55 foreach ( $filters as $filter ) {
56 add_filter( 'code_engine/list_table/column_description', $filter );
57 }
58
59 // Set the hidden columns
60 add_filter( 'default_hidden_columns', array( $this, 'default_hidden_columns' ) );
61
62 // Set the cdegn_functions option
63 $this->cdegn_functions = array_map( function( $cdegn_function ) {
64 return new Meow_CDEGN_Models_Cdegn_Function( $cdegn_function );
65 }, get_option( 'cdegn_functions', [] ) );
66
67 // Set the args
68 // @see: https://developer.wordpress.org/reference/classes/wp_list_table/__construct/
69 parent::__construct(
70 [
71 'plural' => 'snippets',
72 'singular' => 'snippet',
73 ]
74 );
75 }
76
77 /**
78 * Set the 'id' column as hidden by default.
79 *
80 * @param array<string> $hidden
81 * @return array<string>
82 */
83 public function default_hidden_columns( array $hidden ): array {
84 $hidden[] = 'id';
85 return $hidden;
86 }
87
88 /**
89 * Set the 'name' column as the primary column.
90 *
91 * @return string
92 */
93 protected function get_default_primary_column_name(): string {
94 return 'name';
95 }
96
97 /**
98 * Define the output of all columns that have no callback function
99 *
100 * @param Meow_CDEGN_Models_Snippet $item
101 * @param string $column_name
102 *
103 * @return string The content of the column to output.
104 */
105 protected function column_default( $item, $column_name ): string {
106 $cdegn_function = $this->get_cdegn_function( $item->id(), $item->src() );
107 $exists_cdegn_function = $cdegn_function !== null;
108
109 switch ( $column_name ) {
110 case 'id':
111 return $item->id();
112
113 case 'name':
114 $url = $item->edit_url();
115 $link = sprintf( '<a href="%s">%s</a>', esc_url( $url ), $item->name() );
116 $function = sprintf( '<p class="function-overview">%s</p>', $cdegn_function?->overview() ?? '' );
117 return $link . $function;
118
119 case 'description':
120 return apply_filters( 'code_engine/list_table/column_description', $item->description() );
121
122 case 'actions':
123 $display_none_style = 'style="display:none;"';
124 $html = '<div style="display: flex; gap: 4px; align-items: center;">';
125 $html .= '<input type="button" value="' . esc_html__( 'Register', 'code-engine' ) . '" class="button button-primary cdegn-register" data-id="'. $item->id() .'" data-src="' . $item->src() . '" ' . ($exists_cdegn_function ? $display_none_style : '') . '>';
126 $html .= '<input type="button" value="' . esc_html__( 'Unregister', 'code-engine' ) . '" class="button button-primary cdegn-unregister" data-id="'. $item->id() .'" data-src="' . $item->src() . '" ' . (!$exists_cdegn_function ? $display_none_style : '') . '>';
127 $html .= '<input type="button" value="' . esc_html__( 'Refresh', 'code-engine' ) . '" class="button button-secondary cdegn-refresh" data-id="'. $item->id() .'" data-src="' . $item->src() . '" ' . (!$exists_cdegn_function ? $display_none_style : '') . '>';
128 $html .= '<input type="button" value="' . esc_html__( 'Check Quality', 'code-engine' ) . '" class="button button-secondary cdegn-check-quality" data-id="'. $item->id() .'" data-src="' . $item->src() . '" ' . (!$exists_cdegn_function ? $display_none_style : '') . '>';
129 $html .= '<span class="spinner" data-id="'. $item->id() .'" data-src="' . $item->src() . '"></span>';
130 $html .= '</div>';
131 return $html;
132 }
133 }
134
135 /**
136 * Handles the checkbox column output.
137 *
138 * @param Meow_CDEGN_Models_Snippet $item
139 * @return string
140 */
141 protected function column_cb( $item ): string {
142 return sprintf( '<input type="checkbox" name="ids[]" value="%s">', $item->id() );
143 }
144
145 /**
146 * Prepares the list of items for displaying.
147 *
148 * @return void
149 */
150 public function prepare_items(): void {
151 global $cdegn_totals, $cdegn_source, $cdegn_order, $cdegn_orderBy, $cdegn_page;
152
153 // Set options related to pagination
154 $per_page = 20;
155 $limit = $per_page;
156 $offset = ( $cdegn_page - 1 ) * $limit;
157
158 // Get the items
159 $code_snippets = $this->source_code_snippets->get( $cdegn_order, $cdegn_orderBy, $limit, $offset );
160 $wpcode = $this->source_wpcode->get( $cdegn_order, $cdegn_orderBy, $limit, $offset );
161 $others = [];
162 switch ( $cdegn_source ) {
163 case 'code_snippets':
164 $this->items = $code_snippets;
165 break;
166 case 'wpcode':
167 $this->items = $wpcode;
168 break;
169 case 'others':
170 $this->items = $others;
171 break;
172 }
173
174 // Set the totals
175 $cdegn_totals = [
176 'code_snippets' => $this->source_code_snippets->get_total(),
177 'wpcode' => $this->source_wpcode->get_total(),
178 'others' => count( $others ),
179 ];
180
181 // Set the pagination
182 $total_items = $cdegn_totals[ $cdegn_source ];
183 $this->set_pagination_args(
184 [
185 'total_items' => $total_items,
186 'per_page' => $per_page,
187 'total_pages' => ceil( $total_items / $per_page ),
188 ]
189 );
190 }
191
192 /**
193 * Gets a list of columns.
194 *
195 * @return array
196 */
197 public function get_columns(): array {
198 $columns = [
199 'cb' => '<input type="checkbox">',
200 'name' => __( 'Name', 'code-engine' ),
201 'description' => __( 'Description', 'code-engine' ),
202 'id' => __( 'ID', 'code-engine' ),
203 'actions' => __( 'Actions', 'code-engine' ),
204 ];
205
206 return $columns;
207 }
208
209 /**
210 * Gets a list of sortable columns.
211 *
212 * @return array
213 */
214 public function get_sortable_columns(): array {
215 $sortable_columns = [
216 'id' => [ 'id', true ],
217 'name' => 'name',
218 ];
219
220 return $sortable_columns;
221 }
222
223 /**
224 * Gets the list of views available on this table.
225 *
226 * @return array
227 */
228 public function get_views(): array {
229 global $cdegn_totals, $cdegn_source;
230 $source_links = parent::get_views();
231
232 foreach ( $this->sources as $source_info ) {
233 $type = $source_info['source'];
234 $label = $source_info['label'];
235
236 if ( ! isset( $cdegn_totals[ $type ] ) ) {
237 continue;
238 }
239
240 $count = $cdegn_totals[ $type ];
241
242 $format_single = sprintf( esc_html__( '%1$s', 'code-engine' ) . ' <span class="count">(%2$s)</span>', $label, number_format_i18n( $count ) );
243 $format_plural = sprintf( esc_html__( '%1$s', 'code-engine' ) . ' <span class="count">(%2$s)</span>', $label, number_format_i18n( $count ) );
244
245 $format = _n( $format_single, $format_plural, $count, 'code-engine' );
246
247 $url = esc_url( add_query_arg( 'source', $type ) );
248
249 $class = $type === $cdegn_source ? ' class="current"' : '';
250
251 $text = sprintf( $format, esc_html( $label ), number_format_i18n( $count ) );
252
253 $source_links[ $type ] = sprintf( '<a href="%s"%s>%s</a>', $url, $class, $text );
254
255
256 }
257
258 return $source_links;
259 }
260
261 /**
262 * Gets the specific cdegn_function by the snippet ID and source.
263 * return null if not found.
264 *
265 * @param int $id
266 * @param string $src
267 * @return Meow_CDEGN_Models_Cdegn_Function|null
268 */
269 protected function get_cdegn_function( int $id, string $src ): ?Meow_CDEGN_Models_Cdegn_Function {
270 $cdegn_function = array_filter( $this->cdegn_functions, function( $cdegn_function ) use ( $id, $src ) {
271 return $cdegn_function->snippet_id() === $id && $cdegn_function->snippet_src() === $src;
272 } );
273 return count( $cdegn_function ) > 0 ? array_values( $cdegn_function )[0] : null;
274 }
275 }
276