__( 'Material', 'pqc' ), //singular name of the listed records
'plural' => __( 'Materials', 'pqc' ), //plural name of the listed records
'ajax' => false //does this table support ajax?
] );
}
/**
* Retrieve materials data from the database
*
* @param int $per_page
* @param int $page_number
*
* @return mixed
*/
public static function get_materials( $per_page = 5, $page_number = 1 ) {
global $wpdb;
$table = PQC_MATERIALS_TABLE;
if ( isset( $_POST['s'] ) && ! empty( $_POST['s'] ) ) {
$search_term = esc_attr( $_POST['s'] );
$sql = "SELECT * FROM $table WHERE material_name LIKE '%$search_term%'";
} else {
$sql = "SELECT * FROM $table";
}
if ( ! empty( $_REQUEST['orderby'] ) ) {
$sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] );
$sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC';
} else {
$sql .= ' ORDER BY ID DESC';
}
$sql .= " LIMIT $per_page";
$sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
$result = $wpdb->get_results( $sql, 'ARRAY_A' );
return $result;
}
/**
* Delete a material record.
*
* @param int $id material ID
*/
public static function delete_material( $id ) {
global $wpdb;
$table = PQC_MATERIALS_TABLE;
$delete = $wpdb->delete( $table, [ 'ID' => $id ], [ '%d' ] );
return $delete;
}
/**
* Returns the count of records in the database.
*
* @return null|string
*/
public static function record_count() {
global $wpdb;
$table = PQC_MATERIALS_TABLE;
$sql = "SELECT COUNT(*) FROM $table";
return $wpdb->get_var( $sql );
}
/**
* Text displayed when no data is available
*
*/
public function no_items() {
_e( 'No material found.', 'pqc' );
}
/**
* 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 'date':
$text = ( $item['date_created'] === $item['date_modified'] ) ? __( 'Published', 'pqc' ) : __( 'Updated', 'pqc' );
return sprintf(
'%s
%s',
$text, mysql2date( 'Y/m/d h:i:s a', $item['date_modified'] ),
pqc_time_difference( current_time( 'mysql' ), $item['date_modified'] )
);
break;
case 'material_density':
return "{$item[$column_name]}g/cm3";
case 'material_cost':
return pqc_money_format( $item[$column_name], null, true );
case 'material_description':
return $item[$column_name];
case 'material_name':
return $item[$column_name];
break;
default:
return print_r( $item, true ); //Show the whole array for troubleshooting purposes
}
}
/**
* Render the bulk edit checkbox
*
* @param array $item
*
* @return string
*/
public function column_cb( $item ) {
return sprintf(
'', $item['ID']
);
}
/**
* Method for name column
*
* @param array $item an array of DB data
*
* @return string
*/
public function column_material_name( $item ) {
$delete_nonce = wp_create_nonce( 'pqc_delete_material' );
$actions = [
'edit' => sprintf( __( 'Edit' ), absint( $item['ID'] ) ),
'delete' => sprintf( 'Delete', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['ID'] ), $delete_nonce )
];
$content = '';
$content .= '';
$content .= '';
$content .= '';
$material_name = $content . $item['material_name'];
return sprintf( '%1$s %2$s', $material_name, $this->row_actions( $actions ) );
}
/**
* Associative array of columns
*
* @return array
*/
public function get_columns() {
$columns = [
'cb' => '',
'material_name' => __( 'Name', 'pqc' ),
'material_description' => __( 'Description', 'pqc' ),
'material_cost' => __( 'Cost', 'pqc' ),
'material_density' => __( 'Density', 'pqc' ),
'date' => __( 'Date', 'pqc' ),
];
return $columns;
}
/**
* Columns to make sortable.
*
* @return array
*/
public function get_sortable_columns() {
$sortable_columns = array(
'material_name' => array( 'material_name', true ),
'material_density' => array( 'material_density', true ),
'date' => array( 'date_modified', true )
);
return $sortable_columns;
}
/**
* Returns an associative array containing the bulk action
*
* @return array
*/
public function get_bulk_actions() {
$actions = [
'bulk-delete' => 'Delete'
];
return $actions;
}
/**
* Handles data query and filter, sorting, and pagination.
*/
public function prepare_items() {
$this->_column_headers = $this->get_column_info();
// Process bulk action
$this->process_bulk_action();
$per_page = $this->get_items_per_page( 'materials_per_page', 5 );
$current_page = $this->get_pagenum();
$total_items = self::record_count();
$this->set_pagination_args( [
'total_items' => $total_items, // We have to calculate the total number of items
'per_page' => $per_page // We have to determine how many items to show on a page
] );
$this->items = self::get_materials( $per_page, $current_page );
}
public function process_bulk_action() {
global $pqc;
//Detect when a delete action is being triggered...
if ( 'delete' === $this->current_action() ) {
// In our file that handles the request, verify the nonce.
$nonce = sanitize_text_field( $_REQUEST['_wpnonce'] );
wp_verify_nonce( $nonce, 'pqc_delete_material' );
$delete = self::delete_material( absint( $_GET['material'] ) );
if ( $delete ) {
$pqc->add_notice( __( 'Material deleted successfully.', 'pqc' ), 'updated', true, true );
} else {
$pqc->add_notice( __( 'Failed to delete material. Perharps it does not exist anymore.', 'pqc' ), 'pqc-update-nag update-nag', true, true );
}
}
// If the delete bulk action is triggered
elseif ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' )
|| ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' )
) {
$delete_ids = esc_sql( $_POST['bulk-delete'] );
if ( isset( $delete_ids ) && ! empty( $delete_ids ) ) {
// loop over the array of record IDs and delete them
foreach ( $delete_ids as $id ) {
$delete = self::delete_material( (int) $id );
}
if ( $delete ) {
$pqc->add_notice( __( 'Material deleted successfully.', 'pqc' ), 'updated' );
} else {
$pqc->add_notice( __( 'Failed to delete material. Perharps it does not exist anymore.', 'pqc' ), 'pqc-update-nag update-nag' );
}
}
}
}
}
class PQC_Materials {
public static $materials_obj;
public function __construct() {}
/**
* Plugin materials page
*/
public static function materials_page() {
if ( isset( $_POST['pqc_material_add'] ) ) {
unset( $_POST['pqc_id'] );
self::add_material( $_POST );
}
elseif ( isset( $_POST['pqc_material_update'] ) ) {
self::update_material( $_POST );
}
$settings = maybe_unserialize( get_option( PQC_SETTING_OPTIONS ) );
$currency = $settings['pqc_general_settings']['currency'];
?>