__( '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']; ?>


prepare_items(); self::$materials_obj->search_box( 'search', 'search_id' ); self::$materials_obj->display(); ?>



'Materials', 'default' => 5, 'option' => 'materials_per_page' ]; add_screen_option( $option, $args ); self::$materials_obj = new PQC_Materials_List(); } /** * Add new material * * @param mixed $args */ public static function add_material( $args ) { check_admin_referer( 'nonce_material' ); global $wpdb, $pqc; $table = PQC_MATERIALS_TABLE; $error = false; foreach ( $args as $key => $value ) { if ( empty( $value ) ) { $error = true; break; } } if ( ! $error ) { $author = get_current_user_id(); $material_name = sanitize_text_field( $args['pqc_material_name'] ); $material_desc = sanitize_textarea_field( $args['pqc_material_description'] ); $material_cost = pqc_number_format_raw( $args['pqc_material_cost'] ); $material_dens = sanitize_text_field( $args['pqc_material_density'] ); $date = current_time( 'mysql' ); $exist = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE material_name = %s", $material_name ) ); if ( ! $exist ) { $sql = $wpdb->prepare( " INSERT INTO $table ( author, material_name, material_description, material_cost, material_density, date_created, date_modified ) VALUES ( %d, %s, %s, %s, %s, %s, %s ) ", $author, $material_name, $material_desc, $material_cost, $material_dens, $date, $date ); $add = $wpdb->query( $sql ); } if ( isset( $add ) && $add ) { $pqc->add_notice( __( 'Material added successfully.', 'pqc' ), 'updated', true, true ); } else { $pqc->add_notice( __( 'Failed to add material. Perharps it already exist.', 'pqc' ), 'pqc-update-nag update-nag', true, true ); } } else { $pqc->add_notice( __( 'All fields are required.', 'pqc' ), 'error', true, true ); } } /** * Update Existing Material * * @param mixed $args */ public static function update_material( $args ) { check_admin_referer( 'nonce_material' ); if ( ! isset( $args['pqc_id'] ) || empty( $args['pqc_id'] ) ) return false; global $wpdb, $pqc; $ID = absint( $args['pqc_id'] ); $table = PQC_MATERIALS_TABLE; $error = false; foreach ( $args as $key => $value ) { if ( empty( $value ) ) { $error = true; break; } } if ( ! $error ) { $author = get_current_user_id(); $material_name = sanitize_text_field( $args['pqc_material_name'] ); $material_desc = sanitize_textarea_field( $args['pqc_material_description'] ); $material_cost = pqc_number_format_raw( $args['pqc_material_cost'] ); $material_dens = sanitize_text_field( $args['pqc_material_density'] ); $date = current_time( 'mysql' ); $exist = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $table WHERE ID = %d", $ID ) ); if ( $exist ) { $exist = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $table WHERE material_name = %s", $material_name ) ); if ( $exist == $ID || ! $exist ) { $sql = $wpdb->prepare( " UPDATE $table SET author = %d, material_name = %s, material_description = %s, material_cost = %s, material_density = %s, date_modified = %s WHERE ID = %d ", $author, $material_name, $material_desc, $material_cost, $material_dens, $date, $ID ); $update = $wpdb->query( $sql ); } } if ( isset( $update ) && $update ) { $pqc->add_notice( __( 'Material updated successfully.', 'pqc' ), 'updated', true, true ); } else { $pqc->add_notice( __( 'Failed to update material. Perharps it does not exist. PS: Use unique Material Name.', 'pqc' ), 'pqc-update-nag update-nag', true, true ); } } else { $pqc->add_notice( __( 'All fields are required.', 'pqc' ), 'error', true, true ); } } }