| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPCoder\Dashboard; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use WP_List_Table; |
| 8 |
use WPCoder\WPCoder; |
| 9 |
|
| 10 |
class ListTable extends WP_List_Table { |
| 11 |
|
| 12 |
public $per_page = 30; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
// Set parent defaults |
| 16 |
parent::__construct( array( |
| 17 |
'ajax' => false, |
| 18 |
) ); |
| 19 |
$this->process_bulk_action(); |
| 20 |
} |
| 21 |
|
| 22 |
public function column_default( $item, $column_name ) { |
| 23 |
return $item[ $column_name ]; |
| 24 |
} |
| 25 |
|
| 26 |
public function search_box( $text, $input_id ) { |
| 27 |
$input_id = $input_id . '-search-input'; |
| 28 |
if ( ! empty( $_REQUEST['orderby'] ) ) { |
| 29 |
echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />'; |
| 30 |
} |
| 31 |
if ( ! empty( $_REQUEST['order'] ) ) { |
| 32 |
echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />'; |
| 33 |
} |
| 34 |
?> |
| 35 |
<p class="search-box"> |
| 36 |
<label class="screen-reader-text" for="<?php |
| 37 |
echo esc_attr( $input_id ) ?>"><?php |
| 38 |
echo esc_attr( $text ); ?> |
| 39 |
:</label> |
| 40 |
<input type="search" id="<?php |
| 41 |
echo esc_attr( $input_id ) ?>" name="s" value="<?php |
| 42 |
_admin_search_query(); ?>"/> |
| 43 |
<?php |
| 44 |
submit_button( $text, 'button', false, false, array( 'ID' => 'search-submit' ) ); ?> |
| 45 |
</p> |
| 46 |
<?php |
| 47 |
} |
| 48 |
|
| 49 |
public function column_title( $item ): string { |
| 50 |
$title = ! empty( $item['title'] ) ? $item['title'] : __( 'Untitled', 'wp-coder' ); |
| 51 |
|
| 52 |
$actions = [ |
| 53 |
'id' => 'ID: ' . $item['ID'], |
| 54 |
'edit' => '<a href="' . esc_url( Link::edit( $item['ID'] ) ) . '">' . esc_attr__( 'Edit', |
| 55 |
'wp-coder' ) . '</a>', |
| 56 |
'duplicate' => '<a href="' . esc_url( Link::duplicate( $item['ID'] ) ) . '">' . esc_attr__( 'Duplicate', |
| 57 |
'wp-coder' ) . '</a>', |
| 58 |
'delete' => '<a href="' . esc_url( Link::remove( $item['ID'] ) ) . '" >' . esc_attr__( 'Delete', |
| 59 |
'wp-coder' ) . '</a>', |
| 60 |
'export' => '<a href="' . esc_url( Link::export( $item['ID'] ) ) . '" >' . esc_attr__( 'Export', |
| 61 |
'wp-coder' ) . '</a>', |
| 62 |
]; |
| 63 |
|
| 64 |
|
| 65 |
return $title . $this->row_actions( $actions ); |
| 66 |
} |
| 67 |
|
| 68 |
public function prepare_items(): void { |
| 69 |
$columns = $this->get_columns(); |
| 70 |
$hidden = $this->get_hidden_columns(); |
| 71 |
$sortable = $this->get_sortable_columns(); |
| 72 |
$data = $this->table_data(); |
| 73 |
$perPage = 30; |
| 74 |
$currentPage = $this->get_pagenum(); |
| 75 |
if ( $data ) { |
| 76 |
usort( $data, [ &$this, 'sort_data' ] ); |
| 77 |
$data = array_slice( $data, ( ( $currentPage - 1 ) * $perPage ), $perPage ); |
| 78 |
} |
| 79 |
$totalItems = $this->list_count(); |
| 80 |
$this->_column_headers = [ $columns, $hidden, $sortable ]; |
| 81 |
$this->items = $data; |
| 82 |
$this->set_pagination_args( [ |
| 83 |
'total_items' => $totalItems, |
| 84 |
'per_page' => $perPage, |
| 85 |
'total_pages' => ceil( $totalItems / $perPage ), |
| 86 |
] ); |
| 87 |
} |
| 88 |
|
| 89 |
public function get_columns(): array { |
| 90 |
return [ |
| 91 |
'cb' => '<input type="checkbox" />', |
| 92 |
'title' => __( 'Title', 'wp-coder' ), |
| 93 |
'code' => __( 'Shortcode', 'wp-coder' ), |
| 94 |
'code-alt' => __( 'Alternative Shortcode', 'wp-coder' ), |
| 95 |
'tag' => __( 'Tag', 'wp-coder' ), |
| 96 |
'mode' => __( 'Test mode', |
| 97 |
'wp-coder' ) . '<sup class="wowp-tooltip" data-tooltip="' . __( 'The item will only be displayed for administrators.', |
| 98 |
'wp-coder' ) . '">ℹ</sup>', |
| 99 |
'status' => __( 'Status', |
| 100 |
'wp-coder' ) . '<sup class="wowp-tooltip" data-tooltip="' . __( 'The item will only be displayed for administrators.', |
| 101 |
'wp-coder' ) . '">ℹ</sup>', |
| 102 |
]; |
| 103 |
} |
| 104 |
|
| 105 |
public function get_hidden_columns(): array { |
| 106 |
return []; |
| 107 |
} |
| 108 |
|
| 109 |
public function get_sortable_columns(): array { |
| 110 |
return [ |
| 111 |
'ID' => [ 'ID', false ], |
| 112 |
]; |
| 113 |
} |
| 114 |
|
| 115 |
private function table_data(): array { |
| 116 |
$data = []; |
| 117 |
$paged = $this->get_paged(); |
| 118 |
$offset = $this->per_page * ( $paged - 1 ); |
| 119 |
|
| 120 |
$result = $this->get_results(); |
| 121 |
|
| 122 |
$slug = WPCoder::SLUG; |
| 123 |
$shortcode = WPCoder::SHORTCODE; |
| 124 |
|
| 125 |
if ( empty( $result ) ) { |
| 126 |
return $data; |
| 127 |
} |
| 128 |
$paged = $_GET['paged'] ?? ''; |
| 129 |
|
| 130 |
$main_link_arg = [ |
| 131 |
'page' => $slug . '-settings', |
| 132 |
'action' => 'update' |
| 133 |
]; |
| 134 |
|
| 135 |
if ( ! empty( $paged ) ) { |
| 136 |
$main_link_arg['wpcoder-page'] = absint( $paged ); |
| 137 |
} |
| 138 |
|
| 139 |
$main_link = add_query_arg( $main_link_arg, admin_url( 'admin.php' ) ); |
| 140 |
|
| 141 |
|
| 142 |
foreach ( $result as $key => $value ) { |
| 143 |
$title = ! empty( $value->title ) ? $value->title : __( 'Untitled', 'wp-coder' ); |
| 144 |
$tooltip_off = esc_attr__( 'Click for Deactivate.', 'wp-coder' ); |
| 145 |
$tooltip_on = esc_attr__( 'Click for Activate.', 'wp-coder' ); |
| 146 |
$status_off = '<a href="' . esc_url( Link::activate_url( $value->id ) ) . '" class="wowp-toogle is-off" data-tooltip="' . esc_attr( $tooltip_on ) . '"><span>' . esc_attr__( 'OFF', |
| 147 |
'wp-coder' ) . '</span></a>'; |
| 148 |
$status_on = '<a href="' . esc_url( Link::deactivate_url( $value->id ) ) . '" class="wowp-toogle is-on" data-tooltip="' . esc_attr( $tooltip_off ) . '"><span>' . esc_attr__( 'ON', |
| 149 |
'wp-coder' ) . '</span></a>'; |
| 150 |
$status = ! empty( $value->status ) ? $status_off : $status_on; |
| 151 |
|
| 152 |
$mode_tooltip_off = esc_attr__( 'Click for OFF.', 'wp-coder' ); |
| 153 |
$mode_tooltip_on = esc_attr__( 'Click for ON.', 'wp-coder' ); |
| 154 |
|
| 155 |
$mode_off = '<a href="' . esc_url( Link::activate_mode( $value->id ) ) . '" class="wowp-toogle is-off" data-tooltip="' . esc_attr( $mode_tooltip_on ) . '"><span>' . esc_attr__( 'OFF', |
| 156 |
'wp-coder' ) . '</span></a>'; |
| 157 |
$mode_on = '<a href="' . esc_url( Link::deactivate_mode( $value->id ) ) . '" class="wowp-toogle is-on" data-tooltip="' . esc_attr( $mode_tooltip_off ) . '"><span>' . esc_attr__( 'ON', |
| 158 |
'wp-coder' ) . '</span></a>'; |
| 159 |
|
| 160 |
$mode = empty( $value->mode ) ? $mode_off : $mode_on; |
| 161 |
|
| 162 |
$tag = ''; |
| 163 |
if ( isset( $value->tag ) ) { |
| 164 |
$tag_url = add_query_arg( [ |
| 165 |
'page' => WPCoder::SLUG, |
| 166 |
'tag' => $value->tag |
| 167 |
], admin_url( 'admin.php' ) ); |
| 168 |
$tag = '<a href="' . esc_url( $tag_url ) . '">' . esc_attr( $value->tag ) . '</a>'; |
| 169 |
} |
| 170 |
|
| 171 |
$link = add_query_arg( [ 'id' => $value->id ], $main_link ); |
| 172 |
$data[] = array( |
| 173 |
'ID' => $value->id, |
| 174 |
'title' => '<a href="' . esc_url( $link ) . '">' . esc_attr( $title ) . '</a>', |
| 175 |
'code' => '<div class="wowp-field has-addon"><label for="shortcode" class="label is-addon"><span class="wowp-tooltip on-right is-pointer can-copy" data-tooltip="Copy"><span class="wowp-copy icon icon-copy"></span></span></label><input type="text" value="[' . esc_attr( $shortcode ) . ' id="' . absint( $value->id ) . '"]" readonly="readonly"></div>', |
| 176 |
'code-alt' => '<div class="wowp-field has-addon"><label for="shortcode" class="label is-addon"><span class="wowp-tooltip on-right is-pointer can-copy" data-tooltip="Copy"><span class="wowp-copy icon icon-copy"></span></span></label><input type="text" value="[' . esc_attr( $shortcode ) . ' title="' . esc_attr( $title ) . '"]" readonly="readonly"></div>', |
| 177 |
'tag' => $tag, |
| 178 |
'mode' => $mode, |
| 179 |
'status' => $status, |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
return $data; |
| 184 |
} |
| 185 |
|
| 186 |
public function column_cb( $item ) { |
| 187 |
return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'ID', $item['ID'] ); |
| 188 |
} |
| 189 |
|
| 190 |
public function get_paged(): int { |
| 191 |
return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
| 192 |
} |
| 193 |
|
| 194 |
public function get_search() { |
| 195 |
return ! empty( $_POST['s'] ) ? urldecode( trim( $_POST['s'] ) ) : false; |
| 196 |
} |
| 197 |
|
| 198 |
public function list_count(): int { |
| 199 |
$result = $this->get_results(); |
| 200 |
|
| 201 |
if ( empty( $result ) ) { |
| 202 |
return 0; |
| 203 |
} |
| 204 |
$count = count( $result ); |
| 205 |
|
| 206 |
|
| 207 |
return (int) $count; |
| 208 |
} |
| 209 |
|
| 210 |
public function get_results() { |
| 211 |
global $wpdb; |
| 212 |
|
| 213 |
$search = $this->get_search(); |
| 214 |
|
| 215 |
$tag_search = ( ! empty( $_REQUEST['tag'] ) ) ? sanitize_text_field( $_REQUEST ['tag'] ) : ''; |
| 216 |
$tag_search = ( $tag_search === 'all' ) ? '' : $tag_search; |
| 217 |
|
| 218 |
|
| 219 |
$result = ''; |
| 220 |
|
| 221 |
$table = $wpdb->prefix . WPCoder::PREFIX; |
| 222 |
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 223 |
if ( empty( $search ) ) { |
| 224 |
$result = $wpdb->get_results( "SELECT * FROM $table order by id desc" ); |
| 225 |
if ( ! empty( $tag_search ) ) { |
| 226 |
$result = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table WHERE tag=%s order by id desc", $tag_search )); |
| 227 |
} |
| 228 |
} elseif ( trim( $search ) === 'Untitled' ) { |
| 229 |
$result = $wpdb->get_results( "SELECT * FROM $table WHERE title='' order by id desc" ); |
| 230 |
if ( ! empty( $tag_search ) ) { |
| 231 |
$result = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table WHERE title='' AND tag=%s order by id desc", $tag_search)); |
| 232 |
} |
| 233 |
} elseif ( is_numeric( $search ) ) { |
| 234 |
$result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", absint( $search ) ) ); |
| 235 |
if ( ! empty( $tag_search ) ) { |
| 236 |
$result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table WHERE id=%d AND tag=%s", absint( $search ), |
| 237 |
$tag_search ) ); |
| 238 |
} |
| 239 |
} else { |
| 240 |
$wild = '%'; |
| 241 |
$find = sanitize_text_field( $search ); |
| 242 |
$like = $wild . $wpdb->esc_like( $find ) . $wild; |
| 243 |
$result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table WHERE title LIKE %s order by id desc", $like ) ); |
| 244 |
if ( ! empty( $tag_search ) ) { |
| 245 |
$result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table WHERE title LIKE %s AND tag=%s order by id desc", $like, $tag_search ) ); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// phpcs:enable |
| 250 |
return $result; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
public function get_bulk_actions() { |
| 255 |
$actions = [ |
| 256 |
'delete' => __( 'Delate', 'wp-coder' ), |
| 257 |
'activate' => __( 'Activate', 'wp-coder' ), |
| 258 |
'deactivate' => __( 'Deactivate', 'wp-coder' ), |
| 259 |
'test_on' => __( 'Test mode ON', 'wp-coder' ), |
| 260 |
'test_off' => __( 'Test mode OFF', 'wp-coder' ), |
| 261 |
'export' => __( 'Export', 'wp-coder' ), |
| 262 |
]; |
| 263 |
|
| 264 |
return $actions; |
| 265 |
} |
| 266 |
|
| 267 |
public function process_bulk_action() { |
| 268 |
$ids = isset( $_POST['ID'] ) ? ( map_deep( $_POST['ID'], 'absint' ) ) : false; |
| 269 |
$action = $this->current_action(); |
| 270 |
if ( ! is_array( $ids ) ) { |
| 271 |
$ids = [ $ids ]; |
| 272 |
} |
| 273 |
if ( empty( $action ) ) { |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
$verify = $this->verify(); |
| 278 |
|
| 279 |
if ( ! $verify ) { |
| 280 |
return false; |
| 281 |
} |
| 282 |
|
| 283 |
foreach ( $ids as $id ) { |
| 284 |
if ( 'delete' === $this->current_action() ) { |
| 285 |
DBManager::delete( $id ); |
| 286 |
} |
| 287 |
if ( 'activate' === $this->current_action() ) { |
| 288 |
Settings::activate_item( $id ); |
| 289 |
} |
| 290 |
if ( 'deactivate' === $this->current_action() ) { |
| 291 |
Settings::deactivate_item( $id ); |
| 292 |
} |
| 293 |
if ( 'test_on' === $this->current_action() ) { |
| 294 |
Settings::activate_mode( $id ); |
| 295 |
} |
| 296 |
if ( 'test_off' === $this->current_action() ) { |
| 297 |
Settings::deactivate_mode( $id ); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
protected function extra_tablenav( $which ) { |
| 303 |
if ( 'top' === $which ) { |
| 304 |
$tags = DBManager::get_tags_from_table(); |
| 305 |
|
| 306 |
$tag_search = ( ! empty( $_REQUEST['tag'] ) ) ? sanitize_text_field( $_REQUEST ['tag'] ) : ''; |
| 307 |
$tag_search = ( $tag_search === 'all' ) ? '' : $tag_search; |
| 308 |
|
| 309 |
echo '<div class="alignleft actions"><label for="filter-by-tag" class="screen-reader-text">' . esc_attr__( 'Filter by tag', |
| 310 |
'wp-coder' ) . '</label>'; |
| 311 |
echo '<select name="tag" id="filter-by-tag">'; |
| 312 |
echo '<option value="all"' . selected( 'all', $tag_search, false ) . '>' . esc_attr__( 'All', |
| 313 |
'wp-coder' ) . '</option>'; |
| 314 |
|
| 315 |
if ( ! empty( $tags ) ) { |
| 316 |
foreach ( $tags as $tag ) { |
| 317 |
if ( empty( $tag['tag'] ) ) { |
| 318 |
continue; |
| 319 |
} |
| 320 |
echo '<option value="' . esc_attr( trim( $tag['tag'] ) ) . '"' . selected( $tag['tag'], $tag_search, |
| 321 |
false ) . '>' . esc_html( $tag['tag'] ) . '</option>'; |
| 322 |
} |
| 323 |
} |
| 324 |
echo '</select>'; |
| 325 |
submit_button( __( 'Filter', 'wp-coder' ), 'secondary', 'action', false ); |
| 326 |
echo '</div>'; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
private function sort_data( $a, $b ) { |
| 331 |
// If no sort, default to title |
| 332 |
$orderby = ( ! empty( $_GET['orderby'] ) ) ? sanitize_text_field( $_GET['orderby'] ) : 'ID'; |
| 333 |
// If no order, default to asc |
| 334 |
$order = ( ! empty( $_GET['order'] ) ) ? sanitize_text_field( $_GET['order'] ) : 'desc'; |
| 335 |
// Determine sort order |
| 336 |
$result = strnatcmp( $a[ $orderby ], $b[ $orderby ] ); |
| 337 |
|
| 338 |
// Send final sort direction to usort |
| 339 |
return ( $order === 'asc' ) ? $result : - $result; |
| 340 |
} |
| 341 |
|
| 342 |
private function verify() { |
| 343 |
$name = WPCoder::PREFIX . '_list_action'; |
| 344 |
$nonce_action = WPCoder::PREFIX . '_nonce'; |
| 345 |
|
| 346 |
return ! ( ! isset( $_POST[ $name ] ) || ! wp_verify_nonce( $_POST[ $name ], |
| 347 |
$nonce_action ) || ! current_user_can( 'unfiltered_html' ) ); |
| 348 |
} |
| 349 |
|
| 350 |
} |