developer-branch/admin/partials/list-tables/class-cklph-wdl-list-table.php

462 lines
12 KiB
PHP

<?php
/**
* The list-table class that defines the product details and its add to cart url
*
* Get the product name, sku, thumbnail, and category of a woocommerce product and its checkout
* link.
*
* @link https://chykalophia.com/woocommerce-direct-links
* @since 0.1.0
* @package Woocommerce_Direct_Links
* @subpackage Woocommerce_Direct_Links/admin/partials/list-table
*/
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
if ( ! class_exists( 'Cklph_WP_List_Table' ) ) {
require_once 'class-wp-list-table.php';
}
if ( ! class_exists( 'Cklph_Wdl_List_Table' ) ) {
/**
* Edited list table class
*/
class Cklph_Wdl_List_Table extends Cklph_WP_List_Table {
/**
* Product_List_Table constructor.
*/
public function __construct() {
// Set parent defaults.
parent::__construct(
array(
'singular' => 'product',
'plural' => 'products',
'ajax' => false,
)
);
}
/**
* Defines the columns to use in your listing table
*
* @return array
*/
public function manage_columns() {
// Columns for admin list table.
$columns = array(
'thumb' => '<span class="wc-image tips" data-tip="' . esc_attr__( 'Image', 'woocommerce-direct-links' ) . '">' . __( 'Image', 'woocommerce-direct-links' ) . '</span>',
'post_title' => __( 'Product Name', 'woocommerce-direct-links' ),
'sku' => __( 'SKU', 'woocommerce-direct-links' ),
'attrib' => __( 'Attributes', 'woocommerce-direct-links' ),
'category' => __( 'Category', 'woocomerce-direct-links' ),
'guid' => __( 'Add to Cart Link', 'woocommerce-direct-links' ),
'copy' => __( 'Copy Link', 'woocommerce-direct-links' ),
'refresh' => __( 'Refresh Link', 'woocommerce-direct-links' ),
);
// remove category column for metabox list table.
if ( 'product' === $this->screen->id ) {
array_splice( $columns, 4, 1 );
}
return $columns;
}
/**
* Override the parent columns method for screen options columns.
*
* @return Array
*/
public function get_columns() {
// Columns for screen options.
$columns = array(
'thumb' => __( 'Thumbnail', 'woocommerce-direct-links' ),
'sku' => __( 'SKU', 'woocommerce-direct-links' ),
'attrib' => __( 'Attributes', 'woocommerce-direct-links' ),
'category' => __( 'Category', 'woocomerce-direct-links' ),
);
return $columns;
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns() {
return array();
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data.
* @param String $column_name Current column name.
*
* @return Mixed
*/
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'sku':
case 'attrib':
case 'thumb':
case 'category':
return $item[ $column_name ];
default:
return print_r( $item, true );
}
}
/**
* Prepare the items for the table to process
*
* @return void
*/
public function prepare_items() {
$search = '';
$category = '';
$primary = 'post_title';
$columns = $this->manage_columns();
$sortable = $this->get_sortable_columns();
$get_hidden_columns = $this->get_hidden_columns();
$get_hidden_options = get_user_option( 'manage' . $this->screen->id . 'columnshidden' );
$hidden = is_array( $get_hidden_options ) ? $get_hidden_options : $get_hidden_columns;
if ( isset( $_GET['s'] ) ) {
$search = sanitize_text_field( wp_unslash( $_GET['s'] ) );
}
if ( isset( $_GET['product_cat'] ) ) {
$category = sanitize_text_field( wp_unslash( $_GET['product_cat'] ) );
}
$data = $this->get_data( $search, $category );
usort( $data, array( &$this, 'sort_data' ) );
$per_page = get_user_meta( get_current_user_id(), 'cklph_wdl_per_page', true );
if ( empty( $per_page ) ) {
$per_page = $this->screen->get_option( 'per_page', 'default' );
}
$current_page = $this->get_pagenum();
$total_items = count( $data );
$this->set_pagination_args(
array(
'total_items' => $total_items,
'per_page' => $per_page,
)
);
$data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page );
$this->_column_headers = array( $columns, $hidden, $sortable, $primary );
if ( 'product' === $this->screen->id ) {
$this->_column_headers = array( $columns, $hidden, $sortable, $primary );
}
$this->items = $data;
}
/**
* Retrieves product data. Inlcudes search and filter functions
*
* @param string $search search filter.
* @param string $category category filter.
* @return Array
*/
public function get_data( $search = '', $category = '' ) {
$wc_products = new Cklph_Wdl_Wc_Products();
$productlist = array();
if ( isset( $search ) ) {
$args = array(
'status' => 'publish',
'limit' => -1,
'orderby' => 'title',
'order' => 'ASC',
'type' => array( 'simple', 'variable', 'subscription', 'variable-subscription' ),
's' => $search,
'meta_query' => array(
array(
'key' => '_sku',
'value' => $search,
'compare' => 'LIKE',
),
array(
'key' => '_title',
'value' => $search,
'compare' => 'LIKE',
),
),
);
} else {
$args = array(
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'limit' => -1,
'type' => array( 'simple', 'variable', 'subscription', 'variable-subscription' ),
);
}
if ( isset( $category ) ) {
$args['category'] = $category;
}
if ( 'toplevel_page_cklph-wdl-admin' === $this->screen->id && is_admin() ) {
$productlist = $wc_products->get_all_wc_products_data( $args );
} else {
$productlist = $wc_products->get_metabox_product_data();
}
return $productlist;
}
/**
* Custom row display for Post title
*
* @param int $item item data.
* @return String
*/
protected function column_post_title( $item ) {
$item_id = $item['ID'];
$item_guid = $item['guid'];
$item_title = $item['post_title'];
// Only shows the id / variation id as actions in meta box.
if ( 'product' === $this->screen->id ) {
$actions = array(
'ID' => sprintf(
'<b>%s: %s</b>',
__( 'ID', 'woocommerce-direct-links' ),
esc_attr( $item_id )
),
);
if ( isset( $item['variation_id'] ) ) {
array_splice(
$actions,
1,
0,
array(
'variation_id' => sprintf(
'%s: %s',
__( 'Variation ID', 'woocommerce-direct-links' ),
esc_attr( $item['variation_id'] )
),
)
);
}
} else {
$actions = array(
'ID' => sprintf(
'<b>%s: %s</b>',
__( 'ID', 'woocommerce-direct-links' ),
esc_attr( $item_id )
),
'edit' => sprintf(
'<a href="%s">%s</a>',
esc_attr( get_edit_post_link( $item_id ) ),
__( 'Edit', 'woocommerce-direct-links' )
),
'view' => sprintf(
'<a href="%s">%s</a>',
esc_attr( $item_guid ),
__( 'View', 'woocommerce-direct-links' )
),
);
if ( isset( $item['variation_id'] ) ) {
array_splice(
$actions,
1,
0,
array(
'variation_id' => sprintf(
'%s: %s',
__( 'Variation ID', 'woocommerce-direct-links' ),
esc_attr( $item['variation_id'] )
),
)
);
}
}
return sprintf(
'<a href="%1$s"><strong>%2$s</strong></a>%3$s',
esc_attr( $item_guid ),
esc_attr( $item_title ),
$this->row_actions( $actions )
);
}
/**
* Custom row display for Copy column. Prints the copy button
*
* @param int $item item data.
* @return String
*/
protected function column_copy( $item ) {
$id = array_key_exists( 'variation_id', $item ) ? $item['variation_id'] : $item['ID'];
return sprintf(
'<div class="tooltip">
<button class="button-primary" type="button" onclick="cklph_wdl_copyurl(%1$d)" onmouseout="cklph_wdl_out(%1$d)">
<span class="tooltiptext" id="myTooltip-%1$d">Copy</span>
%2$s
</button>
</div>',
esc_attr( $id ),
__( 'Copy Link', 'woocommerce-direct-links' ),
);
}
/**
* Custom row display for Refresh Link Button.
*
* @param array $item Item data.
* @return String
*/
protected function column_refresh( $item ) {
$id = array_key_exists( 'variation_id', $item ) ? $item['variation_id'] : $item['ID'];
return sprintf(
'<button class="button-primary cklph-wdl-refresh" id="cklph-wdl-btn-%1$d" type="button" onclick=cklph_wdl_rest(%1$d)>
<span class="cklph-wdl-btn-text">%2$s</span>
</button>',
esc_attr( $id ),
__( 'Refresh Link', 'woocommerce-direct-links' ),
);
}
/**
* Custom row display for the add-to-cart link
*
* @param int $item item data.
* @return String
*/
protected function column_guid( $item ) {
$id = array_key_exists( 'variation_id', $item ) ? $item['variation_id'] : $item['ID'];
$shortlink = new Cklph_Wdl_Shortlinks();
$turl = $shortlink->cklph_wdl_get_link( $id );
return sprintf(
'<input id="cklph-wdl-url-%1$s" class="cklph-wdl-input" type="text" size=30 value="%2$s" readonly>',
esc_attr( $id ),
esc_url( $turl )
);
}
/**
* Adds custom filters at the top.
*
* @param string $which identify if content is top or bottom.
*/
public function extra_tablenav( $which ) {
?>
<div class="alignleft actions">
<?php
if ( 'toplevel_page_cklph-wdl-admin' === $this->screen->id && is_admin() ) {
if ( 'top' === $which ) {
_e( '<form method="GET"><input type="hidden" name="page" value="' . esc_attr( $_REQUEST['page'] ) . '">' );
$args = array();
if ( isset( $_GET['product_cat'] ) ) {
$args['selected'] = sanitize_text_field( $_GET['product_cat'] );
}
wc_product_dropdown_categories( $args );
$output = ob_get_clean();
if ( ! empty( $output ) ) {
echo $output;
submit_button( __( 'Filter', 'woocommerce-direct-links' ), '', 'filter_action', false, array( 'id' => 'site-query-submit' ) );
_e( '</form>' );
}
}
}
?>
</div>
<?php
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns() {
return array(
'post_title' => array( 'post_title', false ),
'category' => array( 'category', false ),
);
}
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @return Mixed
*/
private function sort_data( $a, $b ) {
if ( 'toplevel_page_cklph-wdl-admin' === $this->screen->id && is_admin() ) {
// Set defaults.
$orderby = 'post_title';
$order = 'asc';
// If orderby is set, use this as the sort column.
if ( ! empty( $_GET['orderby'] ) ) {
$orderby = $_GET['orderby'];
}
// If order is set use this as the order.
if ( ! empty( $_GET['order'] ) ) {
$order = $_GET['order'];
}
$result = strcmp( $a[ $orderby ], $b[ $orderby ] );
if ( 'asc' === $order ) {
return $result;
}
return -$result;
}
}
/**
* Prints the search box of the List Table
*
* @param string $text search query.
* @param int $input_id input id.
*/
public function search_box( $text, $input_id ) {
if ( 'toplevel_page_cklph-wdl-admin' === $this->screen->id && is_admin() ) {
_e( '<form method="GET"><input type="hidden" name="page" value="'.esc_attr( $_REQUEST['page'] ).'">' );
parent::search_box( $text, $input_id );
_e( '</form>' );
}
}
}
}