'error_msg', 'alias'=>'status'))
*/
private static $table_columns;
/**
* Public constructor for class.
*
* @param array $table_columns Table columns.
* @param int $current_page Current page.
* @param int $page_size Page size (optional).
* @param array $row_actions Row actions (optional). Defined as array( 'action' => 'edit', 'label' => 'Edit' ).
*/
public function __construct( $table_columns, $current_page, $page_size = self::DEFAULT_PAGE_SIZE, $row_actions = array() ) {
self::$table_columns = $table_columns;
self::$current_page = max( 1, absint( $current_page ) );
self::$page_size = $page_size;
self::$row_actions = $row_actions;
}
/**
* Build table rows.
*
* @param object $table_data table data.
*
* @return string
*/
public function build_table_rows( $table_data ) {
$row_cells = '';
$index = 0;
foreach ( $table_data as $row_data ) {
$row_cells .= '
';
$row_cells .= self::table_cell( self::eval_row_num( $index + 1 ) );
$row_cells .= self::get_table_cells( $row_data );
if ( ! empty( self::$row_actions ) && isset( $row_data->id ) ) {
$row_cells .= self::table_cell( self::eval_actions( $row_data->id ), 'accredible-cell-actions' );
}
$row_cells .= '
';
++$index;
}
return $row_cells;
}
/**
*
* Returns formatted table cells.
*
* @param object $row_data table row data.
*
* @return string
*/
private static function get_table_cells( $row_data ) {
$table_cells = '';
foreach ( self::$table_columns as $key ) {
$data_key = $key;
if ( is_array( $key ) ) {
$data_key = $key['key'];
$key = $key['alias'];
}
$value = $row_data->$data_key;
switch ( $key ) {
case self::POST_ID:
$name = get_the_title( $value );
$value = ! empty( $name ) ? $name : self::eval_error( 'Not found' );
break;
case self::POST_TYPE:
$value = self::eval_kind_to_type( $value );
break;
case self::GROUP_ID:
$value = self::eval_group_id( $value );
break;
case self::KIND:
$value = self::eval_kind( $value );
break;
case self::DATE_CREATED:
$value = self::eval_date_time( $value );
break;
case self::STATUS:
$value = self::eval_status( $value );
break;
case self::CREDENTIAL_URL:
$value = self::eval_view_url( $value, 'View Credential' );
break;
default:
$value;
}
$table_cells .= '' . $value . ' | ';
}
return $table_cells;
}
/**
* Build a table cell tag .
*
* @param mixed $cell_value value in cell.
* @param string $classes style classes applied to tag.
*
* @return string
*/
private static function table_cell( $cell_value, $classes = '' ) {
$start_cell_tag = empty( $classes ) ? ' | ' : ' | ';
return $start_cell_tag . $cell_value . ' | ';
}
/**
* Evaluates Accredible group ID to string.
*
* @param int $group_id Accredible Group ID.
*
* @return string
*/
private static function eval_group_id( $group_id ) {
if ( null === self::$group_name_cache ) {
self::store_group_name_cache();
}
$key = $group_id;
if ( array_key_exists( $key, self::$group_name_cache ) ) {
$value = self::$group_name_cache[ $key ];
} else {
$value = 'Not found';
}
return $value;
}
/**
* Store group_name_cache.
*/
private static function store_group_name_cache() {
self::$group_name_cache = array();
$page = 1;
$client = new Accredible_Learndash_Api_V1_Client();
while ( ! empty( $page ) ) {
$response = $client->search_groups( null, $page, 50 );
if ( isset( $response['errors'] ) ) {
wp_die( 'Accredible API Search Groups Error ' . esc_attr( $response['errors'] ) );
}
foreach ( $response['groups'] as $group ) {
self::$group_name_cache[ $group['id'] ] = $group['name'];
}
$page = $response['meta']['next_page'];
}
}
/**
* Evaluates kind enum to string.
*
* @param string $kind enum value.
*
* @return string
*/
private static function eval_kind( $kind ) {
switch ( $kind ) {
case 'course_completed':
$kind = 'Course Completed';
break;
case 'lesson_completed':
$kind = 'Lesson Completed';
break;
default:
$kind;
}
return $kind;
}
/**
* Evaluates kind enum to type string.
*
* @param string $kind enum value.
*
* @return string
*/
private static function eval_kind_to_type( $kind ) {
$type = '';
switch ( $kind ) {
case 'course_completed':
$type = 'Course';
break;
case 'lesson_completed':
$type = 'Lesson';
break;
default:
$type;
}
return $type;
}
/**
* Evaluates date values to string.
*
* @param int $timestamp Timestamp value.
*
* @return string
*/
private static function eval_date_time( $timestamp ) {
$date_format = 'd M Y';
$time_format = 'G:i A';
return sprintf(
' %1s %2s ',
wp_date( $date_format, $timestamp ),
wp_date( $time_format, $timestamp )
);
}
/**
* Returns cell error.
*
* @param string $error_message error.
* @param string $tooltip_message tooltip message.
*
* @return string
*/
private static function eval_error( $error_message, $tooltip_message = '' ) {
$error = '';
$error .= '';
$error .= $error_message;
if ( ! empty( $tooltip_message ) ) {
$error .= sprintf(
'
',
ACCREDIBLE_LEARNDASH_PLUGIN_URL . 'assets/images/warning.svg',
$tooltip_message
);
}
$error .= '';
return $error;
}
/**
* Returns cell status.
*
* @param string $value enum value.
*
* @return string
*/
private static function eval_status( $value ) {
$status = 'Success';
if ( ! empty( $value ) ) {
$status = self::eval_error( 'Error', $value );
}
return $status;
}
/**
* Returns row number.
*
* @param int $index item index.
*
* @return int
*/
private static function eval_row_num( $index ) {
return ( self::$current_page - 1 ) * self::$page_size + $index;
}
/**
* Returns a formatted view url.
*
* @param string $url url.
* @param string $label url name.
*
* @return string
*/
private static function eval_view_url( $url, $label ) {
$href = 'javascript:void(0);';
$target = '';
$disabled = 'disabled="disabled"';
if ( ! is_null( $url ) && ! empty( $url ) ) {
$href = $url;
$target = 'target="_blank"';
$disabled = '';
}
return sprintf(
'%4s',
$href,
$disabled,
$target,
$label
);
}
/**
* Returns row actions.
*
* @param int $id id.
*
* @return string
*/
private static function eval_actions( $id ) {
$actions = '';
if ( ! empty( self::$row_actions ) ) {
foreach ( self::$row_actions as $value ) {
if ( ( ! empty( $value['label'] ) ) && ( ! empty( $value['action'] ) ) ) {
$page = '';
$has_confirm_dialog = false;
$need_nonce_url = false;
switch ( $value['action'] ) {
case 'edit_auto_issuance':
$page = 'accredible_learndash_auto_issuance';
break;
case 'delete_auto_issuance':
$page = 'accredible_learndash_admin_action';
$has_confirm_dialog = true;
$need_nonce_url = true;
break;
default:
$page = 'accredible_learndash_issuance_list';
}
$url = admin_url( 'admin.php?page=' . $page . '&call_action=' . $value['action'] . '&page_num=' . self::$current_page . '&id=' . $id );
if ( $need_nonce_url ) {
$url = wp_nonce_url(
$url,
$value['action'] . $id,
'_mynonce'
);
}
$action_params = str_replace( admin_url( 'admin.php?' ), '', $url );
$actions .= sprintf(
'' . $value['label'] . '',
$has_confirm_dialog ? 'data-accredible-dialog="true"' : 'data-accredible-sidenav="true"',
'data-accredible-action-params="' . esc_attr( $action_params ) . '"'
);
}
}
}
return $actions;
}
/**
* Build pagination tile.
*
* @param string $page page.
* @param mixed $page_meta pagination meta.
* @param string $page_name page name used in the tile.
*
* @return void
*/
public static function build_pagination_tile( $page, $page_meta, $page_name ) {
$viewing_from_to = array(
'start' => self::eval_row_num( 1 ),
'end' => intval( $page_meta['current_page'] ) === intval( $page_meta['total_pages'] ) ? $page_meta['total_count'] : $page_meta['current_page'] * $page_meta['page_size'],
);
?>