Skip to content

Instantly share code, notes, and snippets.

@mehrshaddarzi
Last active November 15, 2021 16:54
Show Gist options
  • Save mehrshaddarzi/f12c8ae255b295b39c03bbe0fc3a5474 to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/f12c8ae255b295b39c03bbe0fc3a5474 to your computer and use it in GitHub Desktop.
get_views in wp_list_table
<?php
namespace WP_Statistics\Actions;
/**
* Class WP_Statistics_Actions
*/
class WP_Statistics_Actions {
/**
* WP_List_Table object
*/
public $actions_obj;
/**
* The single instance of the class.
*/
protected static $_instance = null;
/**
* mysql Database tbl name
* @type string
*/
const table = 'statistics_actions';
/**
* Admin Page Slug
* @type string
*/
const admin_slug = 'wp-statistics-actions';
/**
* Plugin Version
* @type string
*/
public static $plugin_version;
/**
* Plugin textDomain
* @type string
*/
public static $textdomain;
/**
* Plugin Url
* @type string
*/
public static $plugin_url;
/**
* Plugin base Path
* @type string
*/
public static $plugin_path;
/**
* Current time System
* @type string
*/
public static $current_time;
/**
* Set Actions Class NameSpace
* @type string
*/
public static $action_namespace;
/**
* Set Loop Time Expiration data
* @type string
*/
public static $expiration_time;
/**
* Set Loop Time Run Action
* @type string
*/
public static $run_time;
/**
* Plugin Option
* @type string
*/
public static $plugin_option;
/**
* WP_Statistics_Actions constructor.
*
* @param $plugin_data
*/
public function __construct( $plugin_data, $plugin_url, $plugin_path ) {
//Get Plugin Option
self::$plugin_option = get_option( 'wpstatistics_actions_settings' );
//Set Plugin Data
self::$textdomain = $plugin_data['TextDomain'];
self::$plugin_version = $plugin_data['Version'];
self::$plugin_url = $plugin_url;
self::$plugin_path = $plugin_path;
//Set Current Time
self::$current_time = current_time( "Y-m-d H:i:s" );
//Set Loop Time for Run Action
self::$expiration_time = HOUR_IN_SECONDS * 3;
self::$run_time = MINUTE_IN_SECONDS * 15;
$interval = WP_Statistics_Actions_Helper::interval_time();
if ( isset( self::$plugin_option['interval_time'] ) and isset( $interval[ self::$plugin_option['interval_time'] ] ) ) {
self::$run_time = $interval[ self::$plugin_option['interval_time'] ]['val'];
}
//Set Action NameSpace
self::$action_namespace = __NAMESPACE__ . '\\Actions\\';
//Load function in Admin Area
if ( is_admin() ) {
//Load Default Setting Option
new WP_Statistics_Actions_Settings();
//Setup Ajax Action
new WP_Statistics_Actions_Ajax();
//Actions Admin Pages
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_assets' ) );
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
add_action( 'admin_head', array( $this, 'wp_list_table_css' ) );
add_action( 'admin_init', array( $this, 'set_list_table_redirect' ) );
add_action( 'admin_print_scripts', array( $this, 'disable_all_admin_notices' ) );
add_action( 'admin_init', array( $this, 'save_actions_form' ) );
add_action( 'admin_init', array( $this, 'clone_actions' ) );
//Add wp-pointer For First time
$wps_action_opt = get_option( "wps_actions_pointer" );
if ( empty( $wps_action_opt ) and self::in_wp_statistics_actions_admin_page() ) {
add_action( 'admin_enqueue_scripts', array( WP_Statistics_Actions_UI::instance(), 'add_wp_pointer' ) );
add_action( 'admin_footer', array( WP_Statistics_Actions_UI::instance(), 'wp_pointer' ), 999 );
}
//set list table Options
if ( self::in_wp_statistics_actions_admin_page() and ! isset( $_GET['method'] ) ) {
add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
}
} else {
//Check Action Expiration Time
add_action( 'init', array( $this, 'expiration_actions' ) );
//Run Actions
add_action( 'wp', array( $this, 'run_actions' ), 999 );
}
}
/**
* Screen Option
*/
public static function set_screen( $status, $option, $value ) {
return $value;
}
/**
* Screen options
*/
public function screen_option() {
if ( self::in_wp_statistics_actions_admin_page() and ! isset( $_GET['method'] ) ) {
//Set Screen Option
$option = 'per_page';
$args = array( 'label' => __( "Item Per Page", 'wp-statistics-actions' ), 'default' => 10, 'option' => 'actions_per_page' ); //options is user Meta
add_screen_option( $option, $args );
//Load WP_List_Table
$this->actions_obj = new WP_Statistics_Actions_Admin_List();
$this->actions_obj->prepare_items();
}
//Set Help Tab
$screen = get_current_screen();
$screen->add_help_tab( array(
'id' => 'actions_th1',
'title' => __( 'Actions', 'wp-statistics-actions' ),
'content' => '<p>' . __( 'A job or task that will be done, Task Actions can start or stop Tasks. Tasks run User Programs. For each trigger, you can define which Tasks to start, stop, or do nothing to. The Program Triggers will allow a Task to be started only at a label in a User Program. To find out how to set up the Task Action, for example, updating the title of a post.', 'wp-statistics-actions' ) . '</p>',
) );
$screen->add_help_tab( array(
'id' => 'actions_th2',
'title' => __( 'Trigger', 'wp-statistics-actions' ),
'content' => '<p>' . __( 'The Program Triggers start user programs when user-specified events occur.', 'wp-statistics-actions' ) . '</p>',
) );
$screen->add_help_tab( array(
'id' => 'actions_th3',
'title' => __( 'Conditions', 'wp-statistics-actions' ),
'content' => '<p>' . __( 'The Program Triggers can handle simple or complex conditions. For example, a condition can check for certain inputs to be on and the Actual position to be greater than a certain value. Conditions are created using expressions and are therefore very flexible.', 'wp-statistics-actions' ) . '</p>',
) );
$screen->set_help_sidebar( '<br><a href="https://veronalabs.com"><img src="' . self::$plugin_url . '/assets/images/veronalabs.svg" width="130" title="VeronaLabs"/></a><br><p>' . __( 'Power by VeronaLabs', 'wp-statistics-actions' ) . '</p><br>' );
}
/**
* Admin Menu
*/
public function admin_menu() {
$hook = add_menu_page( __( 'WP-Statistics Actions', 'wp-statistics-actions' ), __( 'Actions', 'wp-statistics-actions' ), 'manage_options', self::admin_slug, array( $this, 'admin_page' ), 'dashicons-randomize', 65 );
add_action( "load-$hook", array( $this, 'screen_option' ) );
}
/**
* Wp List Table Column Css
*/
public function wp_list_table_css() {
if ( self::in_wp_statistics_actions_admin_page() ) {
echo '<style>';
if ( ! isset( $_GET['method'] ) ) {
echo '
table.widefat th.column-action_name {width: 240px;}
form#adv-settings fieldset.columns-prefs { display: none; }
';
} else {
echo '#screen-options-link-wrap { display:none; }';
}
echo '</style>';
}
}
/**
* Admin Notice
*/
public static function admin_notice() {
if ( self::in_wp_statistics_actions_admin_page() and isset( $_GET['alert'] ) ) {
switch ( $_GET['alert'] ) {
//Delete Alert
case "delete":
WP_Statistics_Actions_Helper::wp_admin_notice( __( "Selected item has been Deleted.", 'wp-statistics-actions' ), "success" );
break;
//Deactivate Alert
case "deactivate":
WP_Statistics_Actions_Helper::wp_admin_notice( __( "Selected item has been Deactivated.", 'wp-statistics-actions' ), "success" );
break;
//Saved Alert
case "saved":
WP_Statistics_Actions_Helper::wp_admin_notice( __( "the Action has been Saved.", 'wp-statistics-actions' ), "success" );
break;
//clone Alert
case "clone":
if ( isset( $_GET['ID'] ) and is_numeric( $_GET['ID'] ) ) {
WP_Statistics_Actions_Helper::wp_admin_notice( __( "The Clone Action was done.", 'wp-statistics-actions' ) . ' <a href="' . add_query_arg( array( 'page' => WP_Statistics_Actions::admin_slug, 'method' => 'edit', 'ID' => $_GET['ID'] ), admin_url( "admin.php" ) ) . '">( ' . __( "Edit", 'wp-statistics-actions' ) . ' )</a>', "success" );
}
break;
}
}
}
/**
* Admin Page
*/
public function admin_page() {
if ( ! isset( $_GET['method'] ) ) {
//Show Wp List Table
WP_Statistics_Actions_UI::wp_list_table( $this->actions_obj );
} else {
//Edit Action Page
if ( $_GET['method'] == "edit" and isset( $_GET['ID'] ) ) {
if ( is_numeric( $_GET['ID'] ) and WP_Statistics_Actions_Helper::check_exist_actions_in_tbl( $_GET['ID'] ) ) {
WP_Statistics_Actions_UI::edit_page( $_GET['ID'] );
} else {
WP_Statistics_Actions_UI::js_redirect( add_query_arg( array( 'page' => WP_Statistics_Actions::admin_slug ), admin_url( "admin.php" ) ) );
}
} else {
WP_Statistics_Actions_UI::js_redirect( add_query_arg( array( 'page' => WP_Statistics_Actions::admin_slug ), admin_url( "admin.php" ) ) );
}
}
}
/**
* Load assets file in admin
*/
public function admin_assets() {
if ( self::in_wp_statistics_actions_admin_page() ) {
//Style
wp_enqueue_style( 'font-awesome', plugin_dir_url( __DIR__ ) . 'assets/css/font-awesome.min.css', true, '4.0.7' );
wp_enqueue_style( 'jQuery-confirm', plugin_dir_url( __DIR__ ) . 'assets/css/jquery-confirm.min.css', true, '3.3.0' );
wp_enqueue_style( 'select-2', plugin_dir_url( __DIR__ ) . 'assets/css/select2.min.css', true, '4.0.6' );
wp_enqueue_style( 'flatpickr', plugin_dir_url( __DIR__ ) . 'assets/css/flatpickr.min.css', true, '4.5.2' );
wp_enqueue_style( 'wp-statistics-actions', plugin_dir_url( __DIR__ ) . 'assets/css/style.css', true, self::$plugin_version );
//Script
wp_enqueue_script( 'jQuery-confirm', plugin_dir_url( __DIR__ ) . 'assets/js/jquery-confirm.min.js', array( 'jquery' ), '3.3.0', true );
wp_enqueue_script( 'select-2', plugin_dir_url( __DIR__ ) . 'assets/js/select2.min.js', array( 'jquery' ), '4.0.6', true );
wp_enqueue_script( 'flatpickr', plugin_dir_url( __DIR__ ) . 'assets/js/flatpickr.min.js', array( 'jquery' ), '4.5.2', true );
wp_enqueue_script( 'wp-statistics-actions', plugin_dir_url( __DIR__ ) . 'assets/js/script.js', array( 'jquery' ), self::$plugin_version, true );
wp_localize_script( 'wp-statistics-actions', 'wps_actions_js', array(
'ajax' => admin_url( "admin-ajax.php" ),
'is_rtl' => ( is_rtl() ? 1 : 0 ),
'loading_img' => admin_url( "/images/spinner.gif" ),
'loading_text' => __( "Loading ...", 'wp-statistics-actions' ),
'remove_text' => __( "Remove", 'wp-statistics-actions' ),
'pls_enter_name' => __( "Please Enter action name :", 'wp-statistics-actions' ),
'sure_remove' => __( "Are You Sure for Delete this action completely ?", 'wp-statistics-actions' ),
'btn_confirm' => __( "Confrim", 'wp-statistics-actions' ),
'btn_cancel' => __( "Cancel", 'wp-statistics-actions' ),
'add_new_action' => __( "Add New Action", 'wp-statistics-actions' ),
'lets_go' => __( "Let's go", 'wp-statistics-actions' ),
'error_ajax_statistics' => __( "error ajax load, please try again.", 'wp-statistics-actions' ),
'act_name' => __( "e.g. Feature the products when get 1000 view", 'wp-statistics-actions' ),
'change_user_creator' => __( "Change Action Creator User", 'wp-statistics-actions' ),
'pls_select_user' => __( "Select a user :", 'wp-statistics-actions' ),
) );
}
}
/*
* Check in Statistics Page
*/
public static function in_wp_statistics_actions_admin_page() {
global $pagenow;
if ( $pagenow == "admin.php" and isset( $_GET['page'] ) and $_GET['page'] == self::admin_slug ) {
return true;
} else {
return false;
}
}
/*
* Set init redirect in wp_list_table
*/
public function set_list_table_redirect() {
if ( self::in_wp_statistics_actions_admin_page() and ! isset( $_GET['method'] ) ) {
//Redirect For $_POST Form Performance
foreach ( array( "s", "user" ) as $post ) {
if ( isset( $_POST[ $post ] ) and ! empty( $_POST[ $post ] ) ) {
$args = array( 'page' => self::admin_slug, $post => urlencode( $_POST[ $post ] ) );
if ( isset( $_GET['filter'] ) ) {
$args['filter'] = urlencode( $_GET['filter'] );
}
wp_redirect( add_query_arg( $args, admin_url( "admin.php" ) ) );
exit;
}
}
//Remove Admin Notice From Pagination
if ( isset( $_GET['alert'] ) and isset( $_GET['paged'] ) ) {
wp_redirect( remove_query_arg( array( 'alert' ) ) );
exit;
}
}
}
/**
* Disable All Admin Notice in list table page
*/
public function disable_all_admin_notices() {
global $wp_filter;
if ( self::in_wp_statistics_actions_admin_page() and ! isset( $_GET['alert'] ) ) {
if ( isset( $wp_filter['user_admin_notices'] ) ) {
unset( $wp_filter['user_admin_notices'] );
}
if ( isset( $wp_filter['admin_notices'] ) ) {
unset( $wp_filter['admin_notices'] );
}
if ( isset( $wp_filter['all_admin_notices'] ) ) {
unset( $wp_filter['all_admin_notices'] );
}
}
}
/**
* Save Actions Form
*/
public function save_actions_form() {
global $wpdb;
if ( self::in_wp_statistics_actions_admin_page() and isset( $_POST['validate_request_update_wps_actions'] ) and isset( $_POST['ID'] ) ) {
//Check Validate Nonce Form
if ( wp_verify_nonce( $_POST['validate_request_update_wps_actions'], 'save_wps_action_form' ) ) {
//Create Array For Push Data To DB
$args = array();
//Save Basic information
$ID = sanitize_text_field( $_POST['ID'] );
$args['action_name'] = sanitize_text_field( $_POST['action_name'] );
$args['user_create'] = sanitize_text_field( $_POST['user_create'] );
if ( isset( $_POST['action_status'] ) ) {
$args['action_status'] = sanitize_text_field( $_POST['action_status'] );
}
$args['expiration_date'] = null;
if ( isset( $_POST['expiration_date'] ) and ! empty( $_POST['expiration_date'] ) ) {
$regEx = '/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/';
if ( preg_match( $regEx, $_POST['expiration_date'] ) ) {
$args['expiration_date'] = $_POST['expiration_date'] . ':00';
}
}
//Get Now Level Of Action
$now_level = WP_Statistics_Actions_Helper::action_edit_step( $ID );
//Save Trigger
if ( isset( $_POST['trigger_type'] ) ) {
$args['action_trigger'] = serialize( array( 'type' => sanitize_text_field( $_POST['trigger_type'] ), 'value' => sanitize_text_field( $_POST['trigger_value'] ) ) );
}
//Save Conditions
if ( isset( $_POST['action_conditions'] ) ) {
$args['action_conditions'] = serialize( $_POST['action_conditions'] );
}
//Save Operations
if ( isset( $_POST['action_approach'] ) ) {
if ( $now_level < 4 ) {
$args['action_status'] = 1; //Active
}
$args['action_approach'] = serialize( $_POST['action_approach'] );
}
//Save To DB
$wpdb->update(
WP_Statistics_Actions_Helper::tbl(),
$args,
array( 'ID' => $ID )
);
//Redirect After Change
if ( isset( $_POST['level'] ) and is_numeric( $_POST['level'] ) ) {
if ( ( $_POST['level'] + 1 ) > 3 ) {
//Edit Action
$url = add_query_arg( array( 'page' => WP_Statistics_Actions::admin_slug, 'alert' => 'saved' ), admin_url( "admin.php" ) );
} else {
//Create Action Step
$url = add_query_arg( array( 'page' => WP_Statistics_Actions::admin_slug, 'method' => 'edit', 'ID' => $ID ), admin_url( "admin.php" ) );
}
wp_redirect( $url );
exit;
}
}
}
}
/**
* Clone Action
*/
public function clone_actions() {
global $wpdb;
if ( self::in_wp_statistics_actions_admin_page() and isset( $_GET['clone'] ) ) {
//Check Security
if ( wp_verify_nonce( esc_attr( $_REQUEST['_wpnonce'] ), 'clone_action_nonce' ) ) {
//Check Action ID exist
if ( is_numeric( $_GET['clone'] ) and WP_Statistics_Actions_Helper::check_exist_actions_in_tbl( $_GET['clone'] ) === true ) {
//Get Action Data
$action = WP_Statistics_Actions_Helper::get_action_data( $_GET['clone'] );
//insert New
$wpdb->insert(
WP_Statistics_Actions_Helper::tbl(),
array(
'user_create' => $action['user_create'],
'action_name' => $action['action_name'] . '-' . __( "copy", 'wp-statistics-actions' ),
'action_status' => 2, //Save Draft
'action_trigger' => $action['action_trigger'],
'action_conditions' => $action['action_conditions'],
'action_approach' => $action['action_approach'],
'expiration_date' => $action['expiration_date'],
)
);
//Redirect To Success
$url = add_query_arg( array( 'page' => WP_Statistics_Actions::admin_slug, 'alert' => 'clone', 'ID' => $wpdb->insert_id ), admin_url( "admin.php" ) );
wp_redirect( $url );
exit;
}
}
}
}
/**
* Check Expiration Date of Action
*/
public static function expiration_actions() {
global $wpdb;
//This Action run Every Loop
$wps_expiration = get_option( "wps_actions_check_expiration_date" );
if ( isset( $wps_expiration ) and is_numeric( $wps_expiration ) ) {
if ( ( $wps_expiration + self::$expiration_time ) <= strtotime( self::$current_time ) ) {
return false;
}
}
//Change Action Status if expiration time > Current time
$list = $wpdb->get_results( "SELECT * FROM `" . WP_Statistics_Actions_Helper::tbl() . "` WHERE `expiration_date` >= '" . self::$current_time . "'", ARRAY_A );
if ( count( $list ) > 0 ) {
foreach ( $list as $action ) {
WP_Statistics_Actions_Helper::set_action_status( $action['ID'], 0 );
}
}
//Update Last run this Action
update_option( "wps_actions_check_expiration_date", strtotime( self::$current_time ) + self::$expiration_time );
}
/**
* Run Actions
*/
public static function run_actions() {
global $wpdb;
//This Action run Every Loop
$wps_run = get_option( "wps_actions_run_date" );
if ( isset( $wps_run ) and is_numeric( $wps_run ) ) {
if ( ( $wps_run + self::$run_time ) <= strtotime( self::$current_time ) ) {
return false;
}
}
//Run Action if Status is Activate
$list = $wpdb->get_results( "SELECT * FROM `" . WP_Statistics_Actions_Helper::tbl() . "` WHERE `action_status` = 1", ARRAY_A );
if ( count( $list ) > 0 ) {
foreach ( $list as $action ) {
$action_id = $action['ID'];
$obj = new WP_Statistics_Actions_Run( array(
'ID' => $action_id,
'trigger' => WP_Statistics_Actions_Helper::get_data( $action['action_trigger'] ),
'conditions' => WP_Statistics_Actions_Helper::get_data( $action['action_conditions'] ),
'approach' => WP_Statistics_Actions_Helper::get_data( $action['action_approach'] ),
) );
if ( $obj->trigger() === false ) {
continue;
} elseif ( $obj->where() === false ) {
continue;
} elseif ( $obj->conditions() === false ) {
continue;
} else {
$obj->run();
}
}
}
//Update Last run this Action
update_option( "wps_actions_run_date", strtotime( self::$current_time ) + self::$run_time );
}
}
<?php
<?php
namespace WP_Statistics\Actions;
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class WP_Statistics_Actions_Admin_List extends \WP_List_Table {
/** Class constructor */
public function __construct() {
parent::__construct( array(
'singular' => 'action',
'plural' => 'actions',
'ajax' => false
) );
}
/**
* Handles data query and filter, sorting, and pagination.
*/
public function prepare_items() {
//Column Option
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$this->_column_headers = $this->get_column_info();
//Process Bulk and Row Action
$this->process_bulk_action();
//Prepare Data
$per_page = $this->get_items_per_page( 'actions_per_page', 10 );
$current_page = $this->get_pagenum();
$total_items = self::record_count();
//Create Pagination
$this->set_pagination_args( array(
'total_items' => $total_items,
'per_page' => $per_page
) );
//return items
$this->items = self::get_actions( $per_page, $current_page );
}
/**
* Retrieve Items data from the database
*
* @param int $per_page
* @param int $page_number
*
* @return mixed
*/
public static function get_actions( $per_page = 10, $page_number = 1 ) {
global $wpdb;
$tbl = $wpdb->prefix . WP_Statistics_Actions::table;
$sql = "SELECT * FROM `$tbl`";
//Where conditional
$conditional = self::conditional_sql();
if ( ! empty( $conditional ) ) {
$sql .= ' WHERE ' . implode( ' AND ', $conditional );
}
//Check Order By
if ( ! empty( $_REQUEST['orderby'] ) ) {
$sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] );
} else {
$sql .= ' ORDER BY `ID`';
}
//Check Order Fields
$sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' DESC';
$sql .= " LIMIT $per_page";
$sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
$result = $wpdb->get_results( $sql, 'ARRAY_A' );
return $result;
}
/**
* Conditional sql
*/
public static function conditional_sql() {
//Where conditional
$where = false;
//Check Search
if ( isset( $_GET['s'] ) and ! empty( $_GET['s'] ) ) {
$search = sanitize_text_field( $_GET['s'] );
$where[] = "`action_name` LIKE '%{$search}%'";
}
//Check Filter Method
if ( isset( $_GET['filter'] ) and ! empty( $_GET['filter'] ) ) {
$status_id = array( "active" => 1, "inactive" => 0, "draft" => 2 );
$where[] = '`action_status` =' . $status_id[ sanitize_text_field( $_GET["filter"] ) ];
}
//Check filter Creator User
if ( isset( $_GET['user'] ) and ! empty( $_GET['user'] ) ) {
$where[] = '`user_create` =' . $_GET['user'];
}
return $where;
}
/**
* Delete a action record.
*
* @param int $id action ID
*/
public static function delete_action( $id ) {
global $wpdb;
$tbl = $wpdb->prefix . WP_Statistics_Actions::table;
$wpdb->delete( $tbl, array( 'ID' => $id ), array( '%d' ) );
}
/**
* Returns the count of records in the database.
* @return null|string
*/
public static function record_count() {
global $wpdb;
$tbl = $wpdb->prefix . WP_Statistics_Actions::table;
$sql = "SELECT COUNT(*) FROM `$tbl`";
//Where conditional
$conditional = self::conditional_sql();
if ( ! empty( $conditional ) ) {
$sql .= ' WHERE ' . implode( ' AND ', $conditional );
}
return $wpdb->get_var( $sql );
}
/**
* Not Found Item Text
*/
public function no_items() {
_e( 'No actions avaliable.', WP_Statistics_Actions::$textdomain );
}
/**
* Associative array of columns
* @return array
*/
function get_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'action_name' => __( 'Name', WP_Statistics_Actions::$textdomain ),
'date_create' => __( 'Date Create', WP_Statistics_Actions::$textdomain ),
'user_create' => __( 'Created By', WP_Statistics_Actions::$textdomain ),
'trigger' => __( 'Trigger', WP_Statistics_Actions::$textdomain ),
'action' => __( 'Action', WP_Statistics_Actions::$textdomain ),
'expiration_date' => __( 'Expiration Date', WP_Statistics_Actions::$textdomain ),
'run_date' => __( 'Run Time', WP_Statistics_Actions::$textdomain ),
'status' => __( 'Status', WP_Statistics_Actions::$textdomain ),
);
return $columns;
}
/**
* Define which columns are hidden
* @return array
*/
public function get_hidden_columns() {
return array();
}
/**
* Render the bulk edit checkbox
*
* @param array $item
*
* @return string
*/
function column_cb( $item ) {
return sprintf(
'<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['ID']
);
}
/**
* Render a column when no column specific method exist.
*
* @param array $item
* @param string $column_name
*
* @return mixed
*/
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'action_name' :
// row actions to edit
$actions['edit'] = '<a href="' . add_query_arg( array( 'page' => WP_Statistics_Actions::admin_slug, 'method' => 'edit', 'ID' => $item['ID'] ), admin_url( "admin.php" ) ) . '">' . __( 'Edit', WP_Statistics_Actions::$textdomain ) . '</a>';
// row actions to Delete
$actions['trash'] = '<a data-trash="yes" href="' . add_query_arg( array( 'page' => WP_Statistics_Actions::admin_slug, 'action' => 'delete', '_wpnonce' => wp_create_nonce( 'delete_action_nonce' ), 'del' => $item['ID'] ), admin_url( "admin.php" ) ) . '">' . __( 'Delete', WP_Statistics_Actions::$textdomain ) . '</a>';
return $item['action_name'] . $this->row_actions( $actions );
break;
case 'date_create' :
$date = date_i18n( "j F Y", strtotime( $item['date_create'] ) );
$actions['create_time'] = date_i18n( "H:i:s", strtotime( $item['date_create'] ) );
return $date . $this->row_actions( $actions );
break;
case 'user_create' :
return WP_Statistics_Actions_Helper::get_user_fullname( $item['user_create'] );
break;
case 'trigger' :
$trigger = WP_Statistics_Actions_Trigger::get_trigger_data( $item['action_trigger'] );
if ( $trigger === false ) {
return '<span aria-hidden="true">—</span><span class="screen-reader-text">' . __( "Unknown", WP_Statistics_Actions::$textdomain ) . '</span>';
} else {
return '<span class="text-primary">' . $trigger['name'] . '</span> <br/>' . ( $trigger['admin_link'] != "" ? '<a href="' . $trigger['admin_link'] . '" class="text-danger" target="_blank">' : '' ) . $trigger['id'] . ' : ' . number_format( $trigger['value'] ) . ( $trigger['admin_link'] != "" ? '</a>' : '' );
}
break;
case 'action' :
$action = WP_Statistics_Actions_Approach::get_action_data( $item['action_approach'] );
if ( ! isset( $action['method'] ) ) {
return '<span aria-hidden="true">—</span><span class="screen-reader-text">' . __( "Unknown", WP_Statistics_Actions::$textdomain ) . '</span>';
} else {
return '<span class="text-success">' . WP_Statistics_Actions_Approach::get_group_name( $action['type'] ) . '</span> <br> ' . WP_Statistics_Actions_Approach::get_method_name( $action['type'], $action['method'] );
}
break;
case 'expiration_date' :
if ( is_null( $item['expiration_date'] ) || empty( $item['expiration_date'] ) ) {
return '<span aria-hidden="true">—</span><span class="screen-reader-text">' . __( "Unknown", WP_Statistics_Actions::$textdomain ) . '</span>';
} else {
$date = date_i18n( "j F Y", strtotime( $item['expiration_date'] ) );
$time = date_i18n( "H:i:s", strtotime( $item['expiration_date'] ) );
return $date . '<br>' . $time;
}
break;
case 'run_date' :
if ( is_null( $item['run_date'] ) || empty( $item['run_date'] ) ) {
return '<span aria-hidden="true">—</span><span class="screen-reader-text">' . __( "Unknown", WP_Statistics_Actions::$textdomain ) . '</span>';
} else {
$date = date_i18n( "j F Y", strtotime( $item['run_date'] ) );
$time = date_i18n( "H:i:s", strtotime( $item['run_date'] ) );
return $date . '<br>' . $time;
}
break;
case 'status' :
$status = '<div class="tooltip">';
if ( $item['action_status'] == 0 ) {
//InActivate
$status .= '<i class="fa fa-circle text-danger circle"></i><span class="tooltiptext">' . __( "inActive", WP_Statistics_Actions::$textdomain );
} elseif ( $item['action_status'] == 1 ) {
//Active
$status .= '<i class="fa fa-circle active_status circle"></i><span class="tooltiptext">' . __( "Active", WP_Statistics_Actions::$textdomain );
} else {
//Draft
$status .= '<i class="fa fa-circle circle"></i><span class="tooltiptext">' . __( "Draft", WP_Statistics_Actions::$textdomain );
}
$status .='</span></div>';
return $status;
break;
}
}
/**
* Columns to make sortable.
* @return array
*/
public function get_sortable_columns() {
$sortable_columns = array(
'action_name' => array( 'action_name', true ),
'date_create' => array( 'date_create', false ),
'user_create' => array( 'user_create', false ),
);
return $sortable_columns;
}
/**
* Show SubSub Filter
*/
protected function get_views() {
$views = array();
$current = ( ! empty( $_REQUEST['filter'] ) ? $_REQUEST['filter'] : 'all' );
//All Actions
$class = ( $current == 'all' ? ' class="current"' : '' );
$all_url = remove_query_arg( array( 'filter', 's', 'paged', 'alert', 'user' ) );
$views['all'] = "<a href='{$all_url }' {$class} >" . __( "All", WP_Statistics_Actions::$textdomain ) . " <span class=\"count\">(" . number_format( WP_Statistics_Actions_Helper::get_number_actions_tbl() ) . ")</span></a>";
$views_item = array(
'active' => array( "name" => __( "Active", WP_Statistics_Actions::$textdomain ), "status_id" => 1 ),
'inactive' => array( "name" => __( "Inactive", WP_Statistics_Actions::$textdomain ), "status_id" => 0 ),
'draft' => array( "name" => __( "Draft", WP_Statistics_Actions::$textdomain ), "status_id" => 2 )
);
foreach ( $views_item as $k => $v ) {
$custom_url = add_query_arg( 'filter', $k, remove_query_arg( array( 's', 'paged', 'alert' ) ) );
$class = ( $current == $k ? ' class="current"' : '' );
$views[ $k ] = "<a href='{$custom_url}' {$class} >" . $v['name'] . " <span class=\"count\">(" . number_format( WP_Statistics_Actions_Helper::get_number_actions_tbl( $v['status_id'] ) ) . ")</span></a>";
}
return $views;
}
/**
* Advance Custom Filter
*/
function extra_tablenav( $which ) {
if ( $which == "top" ) {
?>
<div class="alignleft actions bulkactions">
<label for="bulk-action-selector-top" class="screen-reader-text"><?php _e( "Creator User", WP_Statistics_Actions::$textdomain ); ?></label>
<select name="user" id="bulk-action-selector-top">
<option value=""><?php _e( "Creator User", WP_Statistics_Actions::$textdomain ); ?></option>
<?php
foreach ( WP_Statistics_Actions_Helper::get_list_user_created_actions() as $user_id => $user_name ) {
$selected = '';
if ( isset( $_GET['user'] ) and $_GET['user'] == $user_id ) {
$selected = "selected";
}
echo '<option value="' . $user_id . '" ' . $selected . '>' . $user_name . '</option>';
}
?>
</select>
<input type="submit" id="doaction" class="button action" value="<?php _e( "Filter", WP_Statistics_Actions::$textdomain ); ?>">
</div>
<?php
}
}
/**
* Returns an associative array containing the bulk action
* @return array
*/
public function get_bulk_actions() {
$actions = array(
'bulk-deactivate' => __( 'Deactivate', WP_Statistics_Actions::$textdomain ),
'bulk-delete' => __( 'Delete', WP_Statistics_Actions::$textdomain ),
);
return $actions;
}
/**
* Search Box
*
* @param $text
* @param $input_id
*/
public function search_box( $text, $input_id ) {
if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) {
return;
}
$input_id = $input_id . '-search-input';
if ( ! empty( $_REQUEST['orderby'] ) ) {
echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
}
if ( ! empty( $_REQUEST['order'] ) ) {
echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
}
if ( ! empty( $_REQUEST['post_mime_type'] ) ) {
echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
}
if ( ! empty( $_REQUEST['detached'] ) ) {
echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
}
?>
<p class="search-box">
<label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
<input type="search" placeholder="<?php echo __( "Action Name", WP_Statistics_Actions::$textdomain ); ?>" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" autocomplete="off"/>
<?php submit_button( $text, 'button', false, false, array( 'id' => 'search-submit' ) ); ?>
</p>
<?php
}
/**
* Bulk and Row Actions
*/
public function process_bulk_action() {
// Row Action Delete
if ( 'delete' === $this->current_action() ) {
$nonce = esc_attr( $_REQUEST['_wpnonce'] );
if ( ! wp_verify_nonce( $nonce, 'delete_action_nonce' ) ) {
die( __( "You are not Permission for this action.", WP_Statistics_Actions::$textdomain ) );
} else {
self::delete_action( absint( $_GET['del'] ) );
wp_redirect( esc_url_raw( add_query_arg( array( 'page' => WP_Statistics_Actions::admin_slug, 'alert' => 'delete' ), admin_url( "admin.php" ) ) ) );
exit;
}
}
//Bulk Action Delete
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) ) {
$delete_ids = esc_sql( $_POST['bulk-delete'] );
if ( is_array( $delete_ids ) and count( $delete_ids ) > 0 ) {
foreach ( $delete_ids as $id ) {
self::delete_action( $id );
}
wp_redirect( esc_url_raw( add_query_arg( array( 'page' => WP_Statistics_Actions::admin_slug, 'alert' => 'delete' ), admin_url( "admin.php" ) ) ) );
exit;
}
}
//Bulk Action Deactivated
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-deactivate' ) ) {
$deactivate_ids = esc_sql( $_POST['bulk-delete'] );
if ( is_array( $deactivate_ids ) and count( $deactivate_ids ) > 0 ) {
foreach ( $deactivate_ids as $id ) {
WP_Statistics_Actions_Helper::set_action_status( $id, 0 );
}
wp_redirect( esc_url_raw( add_query_arg( array( 'page' => WP_Statistics_Actions::admin_slug, 'alert' => 'deactivate' ), admin_url( "admin.php" ) ) ) );
exit;
}
}
}
}
$status_links = array(
"all" => __( "<a href=\"edit.php?post_type=post\" class=\"current\" aria-current=\"page\">All <span class=\"count\">(14)</span></a>", 'my-plugin-slug' ),
"active" => __( "<a href='#'>Published</a>", 'my-plugin-slug' ),
"inactive" => __( "<a href='#'>Trashed</a>", 'my-plugin-slug' ),
"draft" => __( "<a href='#'>Trashed</a>", 'my-plugin-slug' ),
);
return $status_links;
function get_views(){
$views = array();
$current = ( !empty($_REQUEST['customvar']) ? $_REQUEST['customvar'] : 'all');
//All link
$class = ($current == 'all' ? ' class="current"' :'');
$all_url = remove_query_arg('customvar');
$views['all'] = "<a href='{$all_url }' {$class} >All</a>";
//Foo link
$foo_url = add_query_arg('customvar','foo');
$class = ($current == 'foo' ? ' class="current"' :'');
$views['foo'] = "<a href='{$foo_url}' {$class} >Foo</a>";
//Bar link
$bar_url = add_query_arg('customvar','bar');
$class = ($current == 'bar' ? ' class="current"' :'');
$views['bar'] = "<a href='{$bar_url}' {$class} >Bar</a>";
return $views;
}
https://stackoverflow.com/questions/23859559/how-to-add-custom-drop-down-filter-in-wordpress-wp-list-table
//Set Screen Tab
public function screen_option() {
//Set Screen Option
$option = 'per_page';
$args = array( 'label' => __( "Item Per Page", self::$textdomain ), 'default' => 10, 'option' => 'actions_per_page' ); //options is user Meta
add_screen_option( $option, $args );
https://www.sitepoint.com/contextual-help-tab-custom-post-types/
$screen = get_current_screen();
$screen->add_help_tab( array(
'id' => 'my_help_tab',
'title' => __('My Help Tab'),
'content' => '<p>' . __( 'Descriptive content that will show in My Help Tab-body goes here.' ) . '</p>',
) );
// Add a sidebar to contextual help.
$screen->set_help_sidebar( 'This is the content you will be adding to the sidebar.' );
//Load WP_List_Table
$this->actions_obj = new WP_Statistics_Actions_Admin_List();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment