| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { |
| 2 |
exit; |
| 3 |
} |
| 4 |
|
| 5 |
// WP_List_Table is not loaded automatically so we need to load it in our application |
| 6 |
if ( ! class_exists( 'WP_List_Table' ) ) { |
| 7 |
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Create a new table class that will extend the WP_List_Table |
| 12 |
* |
| 13 |
*/ |
| 14 |
class Wow_List_Table extends WP_List_Table { |
| 15 |
|
| 16 |
/** |
| 17 |
* Number of items per page |
| 18 |
* |
| 19 |
* @var int |
| 20 |
* @since 1.5 |
| 21 |
*/ |
| 22 |
public $per_page = 30; |
| 23 |
|
| 24 |
private $data; |
| 25 |
private $plugin; |
| 26 |
|
| 27 |
/** |
| 28 |
* Wow_List_Table constructor. |
| 29 |
* |
| 30 |
* @param string $data - Name of the datatable |
| 31 |
* @param array $plugin - Information about the plugin |
| 32 |
*/ |
| 33 |
public function __construct( $data, $plugin ) { |
| 34 |
$this->data = $data; |
| 35 |
$this->plugin = $plugin; |
| 36 |
|
| 37 |
// Set parent defaults |
| 38 |
parent::__construct( array( |
| 39 |
'ajax' => false, |
| 40 |
) ); |
| 41 |
$this->process_bulk_action(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Process the bulk actions |
| 46 |
* |
| 47 |
* @access public |
| 48 |
* @return void |
| 49 |
* @since 1.4 |
| 50 |
*/ |
| 51 |
public function process_bulk_action() { |
| 52 |
$ids = isset( $_POST['ID'] ) ? absint( $_POST['ID'] ) : false; |
| 53 |
$action = $this->current_action(); |
| 54 |
if ( ! is_array( $ids ) ) { |
| 55 |
$ids = array( $ids ); |
| 56 |
} |
| 57 |
if ( empty( $action ) ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
global $wpdb; |
| 61 |
$table = $this->data; |
| 62 |
foreach ( $ids as $id ) { |
| 63 |
if ( 'delete-items' === $this->current_action() ) { |
| 64 |
$wpdb->delete( $table, array( 'id' => $id ), array( '%d' ) ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
public function search_box( $text, $input_id ) { |
| 71 |
$input_id = $input_id . '-search-input'; |
| 72 |
if ( ! empty( $_REQUEST['orderby'] ) ) { |
| 73 |
echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />'; |
| 74 |
} |
| 75 |
if ( ! empty( $_REQUEST['order'] ) ) { |
| 76 |
echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />'; |
| 77 |
} |
| 78 |
?> |
| 79 |
<p class="search-box"> |
| 80 |
<label class="screen-reader-text" for="<?php echo esc_attr( $input_id ) ?>"><?php echo esc_attr( $text ); ?> |
| 81 |
:</label> |
| 82 |
<input type="search" id="<?php echo esc_attr( $input_id ) ?>" name="s" |
| 83 |
value="<?php _admin_search_query(); ?>"/> |
| 84 |
<?php submit_button( $text, 'button', false, false, array( 'ID' => 'search-submit' ) ); ?> |
| 85 |
</p> |
| 86 |
<?php |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Define what data to show on each column of the table |
| 91 |
* |
| 92 |
* @param array $item Data |
| 93 |
* @param String $column_name - Current column name |
| 94 |
* |
| 95 |
* @return Mixed |
| 96 |
*/ |
| 97 |
public function column_default( $item, $column_name ) { |
| 98 |
$value = $item[ $column_name ]; |
| 99 |
|
| 100 |
return $value; |
| 101 |
} |
| 102 |
|
| 103 |
public function column_title( $item ) { |
| 104 |
$slug = $this->plugin['slug']; |
| 105 |
$text = $this->plugin['text']; |
| 106 |
$title = ! empty( $item['title'] ) ? $item['title'] : esc_attr__( 'Untitle', $this->plugin['text'] ); |
| 107 |
$edit_url = admin_url( '/admin.php?page=' . esc_attr( $slug ) . '&tab=settings&act=update&id=' |
| 108 |
. urlencode( $item['ID'] ) ); |
| 109 |
$duplicate_url = admin_url( '/admin.php?page=' . esc_attr( $slug ) . '&tab=settings&act=duplicate&id=' |
| 110 |
. urlencode( $item['ID'] ) ); |
| 111 |
$delete_url = admin_url( '/admin.php?page=' . esc_attr( $slug ) . '&info=delete&did=' . urlencode( $item['ID'] ) ); |
| 112 |
$actions = array( |
| 113 |
'edit' => '<a href="' . esc_url( $edit_url ) . '">' . esc_attr__( 'Edit', $this->plugin['text'] ) . '</a>', |
| 114 |
'duplicate' => '<a href="' . esc_url( $duplicate_url ) . '" style="color:green;">' . esc_attr__( 'Duplicate', $this->plugin['text'] ) |
| 115 |
. '</a>', |
| 116 |
'delete' => '<a href="' . esc_url( $delete_url ) . '" style="color:red;">' . esc_attr__( 'Delete', $this->plugin['text'] ) . '</a>', |
| 117 |
); |
| 118 |
|
| 119 |
return '<a href="' . esc_url( $edit_url ) . '">' . $title . '</a>' . $this->row_actions( $actions ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Prepare the items for the table to process |
| 124 |
* |
| 125 |
* @return Void |
| 126 |
*/ |
| 127 |
public function prepare_items() { |
| 128 |
$columns = $this->get_columns(); |
| 129 |
$hidden = $this->get_hidden_columns(); |
| 130 |
$sortable = $this->get_sortable_columns(); |
| 131 |
$data = $this->table_data(); |
| 132 |
$perPage = 30; |
| 133 |
$currentPage = $this->get_pagenum(); |
| 134 |
if ( $data ) { |
| 135 |
usort( $data, array( &$this, 'sort_data' ) ); |
| 136 |
$data = array_slice( $data, ( ( $currentPage - 1 ) * $perPage ), $perPage ); |
| 137 |
} |
| 138 |
$totalItems = $this->list_count(); |
| 139 |
$this->_column_headers = array( $columns, $hidden, $sortable ); |
| 140 |
$this->items = $data; |
| 141 |
$this->set_pagination_args( array( |
| 142 |
'total_items' => $totalItems, |
| 143 |
'per_page' => $perPage, |
| 144 |
'total_pages' => ceil( $totalItems / $perPage ), |
| 145 |
) ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Override the parent columns method. Defines the columns to use in your listing table |
| 150 |
* |
| 151 |
* @return Array |
| 152 |
*/ |
| 153 |
public function get_columns() { |
| 154 |
$columns = array( |
| 155 |
'cb' => '<input type="checkbox" />', |
| 156 |
'title' => 'Title', |
| 157 |
'code' => 'Shortcode', |
| 158 |
'status' => 'Status', |
| 159 |
'testmode' => 'Test Mode', |
| 160 |
); |
| 161 |
|
| 162 |
return $columns; |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Define which columns are hidden |
| 168 |
* |
| 169 |
* @return Array |
| 170 |
*/ |
| 171 |
public function get_hidden_columns() { |
| 172 |
return array(); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Define the sortable columns |
| 177 |
* |
| 178 |
* @return array |
| 179 |
*/ |
| 180 |
public function get_sortable_columns() { |
| 181 |
return array( |
| 182 |
'ID' => array( 'ID', false ), |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Get the table data |
| 188 |
* |
| 189 |
* @return Array |
| 190 |
*/ |
| 191 |
private function table_data() { |
| 192 |
global $wpdb; |
| 193 |
$data = array(); |
| 194 |
$paged = $this->get_paged(); |
| 195 |
$offset = $this->per_page * ( $paged - 1 ); |
| 196 |
$search = $this->get_search(); |
| 197 |
|
| 198 |
$table = $this->data; |
| 199 |
|
| 200 |
|
| 201 |
if ( ! $search || empty( $search ) ) { |
| 202 |
$result = $wpdb->get_results( "SELECT * FROM $table order by id desc" ); |
| 203 |
} elseif ( is_numeric( $search ) ) { |
| 204 |
$query = $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", absint( $search ) ); |
| 205 |
$result = $wpdb->get_results( $query ); |
| 206 |
} else { |
| 207 |
$query = $wpdb->prepare( "SELECT * FROM $table WHERE title=%s order by id desc", sanitize_text_field( $search ) ); |
| 208 |
$result = $wpdb->get_results( $query ); |
| 209 |
} |
| 210 |
|
| 211 |
$slug = $this->plugin['slug']; |
| 212 |
$shortcode = $this->plugin['shortcode']; |
| 213 |
$prefix = $this->plugin['prefix']; |
| 214 |
|
| 215 |
|
| 216 |
if ( $result ) { |
| 217 |
foreach ( $result as $key => $value ) { |
| 218 |
$param = unserialize( $value->param ); |
| 219 |
$option_name_view = '_' . $prefix . '_view_counter_' . $value->id; |
| 220 |
$option_name_action = '_' . $prefix . '_action_counter_' . $value->id; |
| 221 |
$tool_view = get_option( $option_name_view, '0' ); |
| 222 |
$tool_action = get_option( $option_name_action, '0' ); |
| 223 |
if ( ! empty( $tool_view ) ) { |
| 224 |
$conversion = round( $tool_action / $tool_view * 100, 2 ) . '%'; |
| 225 |
} else { |
| 226 |
$conversion = '0%'; |
| 227 |
} |
| 228 |
$title = ! empty( $value->title ) ? $value->title : esc_attr__( 'Untitle', $this->plugin['text'] ) ; |
| 229 |
$check_status = isset( $param['menu_status'] ) ? $param['menu_status'] : 1; |
| 230 |
$status = ! empty( $check_status ) ? '<span style="color:green;">Activated</span>' : '<span style="color:red;">Deactivated</span>'; |
| 231 |
$testmode = empty( $param['test_mode'] ) ? 'OFF' : 'ON'; |
| 232 |
$data[] = array( |
| 233 |
'ID' => $value->id, |
| 234 |
'title' => '<a href="admin.php?page=' . esc_attr($slug) . '&tab=settings&act=update&id=' . absint($value->id) |
| 235 |
. '">' . esc_attr($title) . '</a>', |
| 236 |
'code' => '[' . esc_attr($shortcode) . ' id="' . absint($value->id) . '"]', |
| 237 |
'view' => $tool_view, |
| 238 |
'action' => $tool_action, |
| 239 |
'conversion' => $conversion, |
| 240 |
'edit' => '<a href="admin.php?page=' . esc_attr($slug) . '&tab=settings&act=update&id=' |
| 241 |
. absint($value->id) . '">edit</a>', |
| 242 |
'delete' => '<a href="admin.php?page=' . esc_attr($slug) . '&info=delete&did=' . absint($value->id) |
| 243 |
. '" style="color:red;">delete</a>', |
| 244 |
'duplicate' => '<a href="admin.php?page=' . esc_attr($slug) . '&tab=settings&act=duplicate&id=' |
| 245 |
. absint($value->id) . '" style="color:green;">duplicate</a>', |
| 246 |
'status' => $status, |
| 247 |
'testmode' => $testmode, |
| 248 |
); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return $data; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Retrieve the current page number |
| 257 |
* |
| 258 |
* @access public |
| 259 |
* @return int Current page number |
| 260 |
* @since 1.5 |
| 261 |
*/ |
| 262 |
public function get_paged() { |
| 263 |
return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Retrieves the search query string |
| 268 |
* |
| 269 |
* @access public |
| 270 |
* @return mixed string If search is present, false otherwise |
| 271 |
* @since 1.0 |
| 272 |
*/ |
| 273 |
public function get_search() { |
| 274 |
return ! empty( $_POST['s'] ) ? urldecode( trim( $_POST['s'] ) ) : false; |
| 275 |
} |
| 276 |
|
| 277 |
public function list_count() { |
| 278 |
global $wpdb; |
| 279 |
$data = $this->data; |
| 280 |
$result = $wpdb->get_results( "SELECT * FROM {$data} order by id asc" ); |
| 281 |
|
| 282 |
return count( $result ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Render the checkbox column |
| 287 |
* |
| 288 |
* @access public |
| 289 |
* |
| 290 |
* @param array $item Contains all the data for the checkbox column |
| 291 |
* |
| 292 |
* @return string Displays a checkbox |
| 293 |
* @since 1.0 |
| 294 |
*/ |
| 295 |
public function column_cb( $item ) { |
| 296 |
return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'ID', $item['ID'] ); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Retrieve the bulk actions |
| 301 |
* |
| 302 |
* @access public |
| 303 |
* @return array $actions Array of the bulk actions |
| 304 |
* @since 1.4 |
| 305 |
*/ |
| 306 |
public function get_bulk_actions() { |
| 307 |
$actions = array( |
| 308 |
'delete-items' => esc_attr__('Delate', $this->plugin['text']), |
| 309 |
); |
| 310 |
|
| 311 |
return $actions; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Gets the name of the primary column. |
| 316 |
* |
| 317 |
* @return string Name of the primary column. |
| 318 |
* @since 1.0 |
| 319 |
* @access protected |
| 320 |
* |
| 321 |
*/ |
| 322 |
protected function get_primary_column_name() { |
| 323 |
return 'ID'; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Allows you to sort the data by the variables set in the $_GET |
| 328 |
* |
| 329 |
* @return Mixed |
| 330 |
*/ |
| 331 |
private function sort_data( $a, $b ) { |
| 332 |
// If no sort, default to title |
| 333 |
$orderby = ( ! empty( $_GET['orderby'] ) ) ? sanitize_text_field( $_GET['orderby'] ) : 'ID'; |
| 334 |
// If no order, default to asc |
| 335 |
$order = ( ! empty( $_GET['order'] ) ) ? sanitize_text_field( $_GET['order'] ) : 'desc'; |
| 336 |
// Determine sort order |
| 337 |
$result = strnatcmp( $a[ $orderby ], $b[ $orderby ] ); |
| 338 |
|
| 339 |
// Send final sort direction to usort |
| 340 |
return ( $order === 'asc' ) ? $result : - $result; |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
} |
| 345 |
|