array( 'new', 'cancelled', 'processing', 'completed', 'refunded' ),
'status' => array( 'pending', 'processing', 'on-hold', 'completed', 'cancelled', 'refunded', 'failed' ),
'items' => array(),
'payment_method' => 'PayPal',
'firstname' => '',
'lastname' => '',
'email' => '',
'payer_email' => '',
'address' => '',
'city' => '',
'zipcode' => '',
'state' => '',
'note' => '',
'order_action' => '',
'order_status' => '',
'coupon' => '',
'shipping_option' => '',
'shipping_cost' => 0.00,
'allow_payment' => 0,
'cart_total' => '',
'subtotal' => '',
'total' => '',
'currency' => '',
'currency_pos' => null,
'user_ip' => '',
'user_id' => '',
'date' => '',
);
/**
* The Constructor
*
*/
public function __construct()
{
// Action Before initialization
do_action( 'pqc_order_before_init' );
$this->init();
// Action After initialization
do_action( 'pqc_order_after_init' );
}
/**
* Initialize Order
*
*/
private function init()
{
// Register Order Post Type
$this->register_order();
add_action( 'admin_head', array( $this, 'admin_head' ) );
add_action( 'save_post_' . $this->key, array( $this, 'metabox_save' ), 10, 3 );
add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) );
add_filter( 'bulk_post_updated_messages', array( $this, 'bulk_updated_messages' ), 10, 2 );
add_filter( 'bulk_actions-edit-' . $this->key, array( $this, 'bulk_actions' ) );
add_filter( 'manage_edit-' . $this->key . '_columns', array( $this, 'edit_columns' ) );
add_filter( 'manage_edit-' . $this->key . '_sortable_columns', array( $this, 'sortable_columns' ) );
add_action( 'manage_' . $this->key . '_posts_custom_column', array( $this, 'custom_columns' ) );
add_action( 'add_meta_boxes_' . $this->key, array( $this, 'metabox' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ), 999 );
}
/**
* Register Order Post type
*
*/
private function register_order()
{
$labels = array(
'name' => _x( 'Orders', 'Post Type General Name', 'pqc' ),
'singular_name' => _x( 'Order', 'Post Type Singular Name', 'pqc' ),
'menu_name' => __( 'Orders', 'pqc' ),
'name_admin_bar' => __( 'Orders', 'pqc' ),
'parent_item_colon' => __( 'Parent Orders:', 'pqc' ),
'all_items' => __( 'Orders', 'pqc' ),
'add_new_item' => __( 'Add New Order', 'pqc' ),
'add_new' => __( 'Add Order', 'pqc' ),
'new_item' => __( 'New Order', 'pqc' ),
'edit_item' => __( 'Edit Order', 'pqc' ),
'update_item' => __( 'Update Order', 'pqc' ),
'view_item' => __( 'View Order', 'pqc' ),
'view_items' => __( 'View Orders', 'pqc' ),
'search_items' => __( 'Search Order', 'pqc' ),
'not_found' => __( 'No Order found', 'pqc' ),
'not_found_in_trash' => __( 'No Order found in Trash', 'pqc' ),
);
$args = array(
'label' => __( 'Orders', 'pqc' ),
'description' => __( 'Order used in 3DPC Quote Calculator', 'pqc' ),
'labels' => $labels,
'supports' => array( 'title' ),
'hierarchical' => false,
'public' => false,
'show_ui' => true,
'show_in_menu' => 'pqc-settings-page',
'show_in_admin_bar' => false,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'rewrite' => false,
'map_meta_cap' => true,
'capabilities' => array(
'create_posts' => 'do_not_allow',
)
);
register_post_type( $this->key, $args );
remove_post_type_support( $this->key, 'title' );
// Action after Registering the Order Post Type
do_action( 'pqc_after_register_order' );
}
public function admin_head()
{
if ( get_post_type() != $this->key ) return;
?>
post_type );
$can_edit_post = current_user_can( 'edit_post', $post->ID );
$actions = array();
$title = _draft_or_post_title();
if ( $can_edit_post && 'trash' != $post->post_status ) {
$actions['edit'] = sprintf(
'%s',
get_edit_post_link( $post->ID ),
/* translators: %s: post title */
esc_attr( sprintf( __( 'Edit “%s”', 'pqc' ), $title ) ),
__( 'Edit', 'pqc' )
);
}
if ( current_user_can( 'delete_post', $post->ID ) ) {
if ( 'trash' === $post->post_status ) {
$actions['untrash'] = sprintf(
'%s',
wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&action=untrash', $post->ID ) ), 'untrash-post_' . $post->ID ),
/* translators: %s: post title */
esc_attr( sprintf( __( 'Restore “%s” from the Trash', 'pqc' ), $title ) ),
__( 'Restore', 'pqc' )
);
} elseif ( EMPTY_TRASH_DAYS ) {
$actions['trash'] = sprintf(
'%s',
get_delete_post_link( $post->ID ),
/* translators: %s: post title */
esc_attr( sprintf( __( 'Move “%s” to the Trash', 'pqc' ), $title ) ),
_x( 'Trash', 'verb', 'pqc' )
);
}
if ( 'trash' === $post->post_status || ! EMPTY_TRASH_DAYS ) {
$actions['delete'] = sprintf(
'%s',
get_delete_post_link( $post->ID, '', true ),
/* translators: %s: post title */
esc_attr( sprintf( __( 'Delete “%s” permanently', 'pqc' ), $title ) ),
__( 'Delete Permanently', 'pqc' )
);
}
}
if ( is_post_type_viewable( $post_type_object ) ) {
if ( in_array( $post->post_status, array( 'pending', 'draft', 'future', ) ) ) {
if ( $can_edit_post ) {
$preview_link = get_preview_post_link( $post );
$actions['view'] = sprintf(
'%s',
esc_url( $preview_link ),
/* translators: %s: post title */
esc_attr( sprintf( __( 'Preview “%s”', 'pqc' ), $title ) ),
__( 'Preview', 'pqc' )
);
}
} elseif ( 'trash' != $post->post_status ) {
$actions['view'] = sprintf(
'%s',
get_permalink( $post->ID ),
/* translators: %s: post title */
esc_attr( sprintf( __( 'View “%s”', 'pqc' ), $title ) ),
__( 'View', 'pqc' )
);
}
}
return $this->row_actions( $actions );
}
/**
* Prepare the row actions
*
* @param array $actions
*/
private function row_actions( $actions )
{
if ( ! is_array( $actions ) ) return;
$total = count( $actions );
$string = '
';
$i = 1;
foreach( $actions as $key => $value ) {
$string .= '' . $value . '';
if ( $i != $total ) $string .= ' | ';
$i++;
}
$string .= '
';
return $string;
}
/**
* Sets Messages for update
*
* @param mixed $messages
*/
public function updated_messages( $messages )
{
if ( $this->key != get_current_screen()->post_type ) return $messages;
$data = get_post_custom();
$data = isset( $data[$this->key . '_data'] ) ? maybe_unserialize( maybe_unserialize( $data[$this->key . '_data'][0] ) ) : $this->default_data;
$status = isset( $data['order_status'] ) ? $data['order_status'] : $this->default_data['order_status'];
$currency = isset( $data['currency'] ) ? $data['currency'] : $this->default_data['currency'];
$c_currency = pqc_money_format_control()['currency'];
$c_c_name = pqc_money_format_control()['currencies'][$c_currency]['name'];
if ( $status == 'pending' && $currency != $c_currency ) {
global $pqc;
$pqc->add_notice(
sprintf(
__( 'Sorry you can edit cost/price for this order. This order currency does not match your preferred currency %s (%s).', 'pqc' ),
$c_c_name, $c_currency
), 'pqc-update-nag'
);
}
$post = get_post();
pqc_session_start();
$astroprint_msg = @$_SESSION['astroprint_msg'];
if ( !empty( $astroprint_msg ) ) {
$messages[$this->key] = array(
'',
$astroprint_msg,
false,
false,
$astroprint_msg,
false,
__( 'Order published.', 'pqc' ),
__( 'Order saved.', 'pqc' ),
__( 'Order submitted.', 'pqc' ),
sprintf(
__( 'Order scheduled for: %1$s.', 'pqc' ),
date_i18n( 'M j, Y @ G:i', strtotime( $post->post_date ) )
),
__( 'Order draft updated.', 'pqc' ),
);
return $messages;
}
$messages[$this->key] = array(
'',
__( 'Order updated.', 'pqc' ),
false,
false,
__( 'Order updated.', 'pqc' ),
false,
__( 'Order published.', 'pqc' ),
__( 'Order saved.', 'pqc' ),
__( 'Order submitted.', 'pqc' ),
sprintf(
__( 'Order scheduled for: %1$s.', 'pqc' ),
date_i18n( 'M j, Y @ G:i', strtotime( $post->post_date ) )
),
__( 'Order draft updated.', 'pqc' ),
);
return $messages;
}
/**
* Sets Messages for Bulk Update
*
* @param mixed $bulk_messages
* @param mixed $bulk_counts
*/
public function bulk_updated_messages( $bulk_messages, $bulk_counts )
{
if ( $this->key != get_current_screen()->post_type ) return $bulk_messages;
$bulk_messages[$this->key] = array(
'updated' => _n( '%s order updated.', '%s orders updated.', $bulk_counts['updated'] ),
'locked' => _n( '%s order not updated, somebody is editing it.', '%s orders not updated, somebody is editing them.', $bulk_counts['locked'] ),
'deleted' => _n( '%s order permanently deleted.', '%s orders permanently deleted.', $bulk_counts['deleted'] ),
'trashed' => _n( '%s order moved to the Trash.', '%s orders moved to the Trash.', $bulk_counts['trashed'] ),
'untrashed' => _n( '%s order restored from the Trash.', '%s orders restored from the Trash.', $bulk_counts['untrashed'] ),
);
return $bulk_messages;
}
/**
* Modify colums
*
* @param mixed $columns
*/
public function edit_columns( $columns )
{
$columns = array(
'cb' => '',
'order_title' => __( 'Order', 'pqc' ),
// 'order_items' => __( 'Purchased', 'pqc' ),
'shipping_address' => __( 'Ship to', 'pqc' ),
'order_status' => __( 'Payment Status', 'pqc' ),
'order_total' => __( 'Total', 'pqc' ),
'order_date' => __( 'Date', 'pqc' ),
);
return $columns;
}
/**
* Make columns sortable.
*
* @param array $columns
* @return array
*/
public function sortable_columns( $columns )
{
$custom = array(
'order_title' => 'ID',
'order_total' => 'total',
'order_date' => 'date',
);
unset( $columns['comments'] );
return wp_parse_args( $custom, $columns );
}
/**
* Sets custom columns for order
*
* @param mixed $column
*/
public function custom_columns( $column )
{
global $post;
$data = get_post_custom();
$data = isset( $data[$this->key . '_data'] ) ? maybe_unserialize( maybe_unserialize( $data[$this->key . '_data'][0] ) ) : $this->default_data;
$items = isset( $data['items'] ) ? $data['items'] : $this->default_data['items'];
$payment_method = isset( $data['payment_method'] ) ? $data['payment_method'] : $this->default_data['payment_method'];
$firstname = isset( $data['firstname'] ) ? $data['firstname'] : $this->default_data['firstname'];
$lastname = isset( $data['lastname'] ) ? $data['lastname'] : $this->default_data['lastname'];
$email = isset( $data['email'] ) ? $data['email'] : $this->default_data['email'];
$payer_email = isset( $data['payer_email'] ) ? $data['payer_email'] : $this->default_data['payer_email'];
$address = isset( $data['address'] ) ? $data['address'] : $this->default_data['address'];
$note = isset( $data['note'] ) ? $data['note'] : $this->default_data['note'];
$status = isset( $data['order_status'] ) ? $data['order_status'] : $this->default_data['order_status'];
$coupon = isset( $data['coupon'] ) ? $data['coupon'] : $this->default_data['coupon'];
$shipping_option = isset( $data['shipping_option'] ) ? $data['shipping_option'] : $this->default_data['shipping_option'];
$cart_total = isset( $data['cart_total'] ) ? $data['cart_total'] : $this->default_data['cart_total'];
$subtotal = isset( $data['subtotal'] ) ? $data['subtotal'] : $this->default_data['subtotal'];
$total = isset( $data['total'] ) ? $data['total'] : $this->default_data['total'];
$currency = isset( $data['currency'] ) ? $data['currency'] : $this->default_data['currency'];
$currency_pos = isset( $data['currency_pos'] ) ? $data['currency_pos'] : $this->default_data['currency_pos'];
$user_id = isset( $data['user_id'] ) ? $data['user_id'] : $this->default_data['user_id'];
$date = isset( $data['date'] ) ? $data['date'] : $this->default_data['date'];
switch ( $column ) {
case 'order_title':
if ( ! empty( $user_id ) ) {
$user = get_user_by( 'id', $user_id );
$username = '';
$username .= esc_html( ucwords( $user->display_name ) );
$username .= '';
}
else {
$username = trim( sprintf( _x( '%1$s %2$s', 'full name', 'pqc' ), $firstname, $lastname ) );
}
printf(
__( '%1$s by %2$s', 'pqc' ),
'#' . esc_attr( $post->ID ) . '',
$username
);
if ( $email ) {
echo '' . esc_html( $email ) . '';
}
echo count( $items ) . ' items';
echo $this->handle_row_actions( $post );
echo '';
break;
case 'shipping_address':
echo $address . 'Via ' . $shipping_option . '';
break;
case 'order_total':
echo pqc_money_format( $total, $currency, true, $currency_pos ) . 'Via ' . $payment_method . '';
break;
case 'order_status':
echo $status;
break;
case 'order_date':
$date = empty( $date ) ? $post->post_modified : $date;
return printf( '
%s', mysql2date( 'c', $date ), mysql2date( 'Y-m-d', $date ), mysql2date( 'h:i:s a', $date ) );
break;
}
}
/**
* Order Meta Box to the Edit Screen
*
*/
public function metabox()
{
if ( $this->key != get_current_screen()->post_type ) return;
add_meta_box( 'order_data', 'Order Data', array( $this, 'metabox_html' ), $this->key, 'normal', 'high' );
add_meta_box( 'order_actions', 'Order Actions', array( $this, 'metabox_order_actions_html' ), $this->key, 'side', 'high' );
add_meta_box( 'order_meta', 'Order Meta', array( $this, 'metabox_order_meta_html' ), $this->key, 'side', 'high' );
// Remove the Slug Meta Box
remove_meta_box( 'slugdiv', $this->key, 'normal' );
}
/**
* HTML content to add to order metabox
*
*/
public function metabox_html()
{
if ( $this->key != get_current_screen()->post_type ) return;
$post = get_post();
$data = get_post_custom();
$data = isset( $data[$this->key . '_data'] ) ? maybe_unserialize( maybe_unserialize( $data[$this->key . '_data'][0] ) ) : $this->default_data;
$items = isset( $data['items'] ) ? $data['items'] : $this->default_data['items'];
$payment_method = isset( $data['payment_method'] ) ? $data['payment_method'] : $this->default_data['payment_method'];
$firstname = isset( $data['firstname'] ) ? $data['firstname'] : $this->default_data['firstname'];
$lastname = isset( $data['lastname'] ) ? $data['lastname'] : $this->default_data['lastname'];
$email = isset( $data['email'] ) ? $data['email'] : $this->default_data['email'];
$address = isset( $data['address'] ) ? $data['address'] : $this->default_data['address'];
$city = isset( $data['city'] ) ? $data['city'] : $this->default_data['city'];
$zipcode = isset( $data['zipcode'] ) ? $data['zipcode'] : $this->default_data['zipcode'];
$state = isset( $data['state'] ) ? $data['state'] : $this->default_data['state'];
$note = isset( $data['note'] ) ? $data['note'] : $this->default_data['note'];
$status = isset( $data['order_status'] ) ? $data['order_status'] : $this->default_data['order_status'];
$coupon = isset( $data['coupon'] ) ? $data['coupon'] : $this->default_data['coupon'];
$shipping_option = isset( $data['shipping_option'] ) ? $data['shipping_option'] : $this->default_data['shipping_option'];
$cart_total = isset( $data['cart_total'] ) ? $data['cart_total'] : $this->default_data['cart_total'];
$subtotal = isset( $data['subtotal'] ) ? $data['subtotal'] : $this->default_data['subtotal'];
$total = isset( $data['total'] ) ? $data['total'] : $this->default_data['total'];
$currency = isset( $data['currency'] ) ? $data['currency'] : $this->default_data['currency'];
$currency_pos = isset( $data['currency_pos'] ) ? $data['currency_pos'] : $this->default_data['currency_pos'];
$pay_for_order_id = isset( $data['pay_for_order_id'] ) ? $data['pay_for_order_id'] : '';
?>
key . '_nonce', $this->key . '_data' ); ?>
post_title ); ?>
ID
);
printf(
'Customer payment page',
$href, $title
);
?>
key != get_current_screen()->post_type ) return;
$post = get_post();
$data = get_post_custom();
$data = isset( $data[$this->key . '_data'] ) ? maybe_unserialize( maybe_unserialize( $data[$this->key . '_data'][0] ) ) : $this->default_data;
$action = isset( $data['order_action'] ) ? $data['order_action'] : $this->default_data['order_action'];
$a_payment = isset( $data['allow_payment'] ) ? $data['allow_payment'] : $this->default_data['allow_payment'];
$status = isset( $data['order_status'] ) ? $data['order_status'] : $this->default_data['order_status'];
$currency = isset( $data['currency'] ) ? $data['currency'] : $this->default_data['currency'];
?>
key != get_current_screen()->post_type ) return;
$post = get_post();
$data = get_post_custom();
$data = isset( $data[$this->key . '_data'] ) ? maybe_unserialize( maybe_unserialize( $data[$this->key . '_data'][0] ) ) : $this->default_data;
$coupon = isset( $data['coupon'] ) ? $data['coupon'] : $this->default_data['coupon'];
$shipping_option = isset( $data['shipping_option'] ) ? $data['shipping_option'] : $this->default_data['shipping_option'];
$shipping_cost = isset( $data['shipping_cost'] ) ? $data['shipping_cost'] : $this->default_data['shipping_cost'];
$cart_total = isset( $data['cart_total'] ) ? $data['cart_total'] : $this->default_data['cart_total'];
$subtotal = isset( $data['subtotal'] ) ? $data['subtotal'] : $this->default_data['subtotal'];
$total = isset( $data['total'] ) ? $data['total'] : $this->default_data['total'];
$currency = isset( $data['currency'] ) ? $data['currency'] : $this->default_data['currency'];
$currency_pos = isset( $data['currency_pos'] ) ? $data['currency_pos'] : $this->default_data['currency_pos'];
?>
-
%s',
pqc_money_format( $subtotal, $currency, true, $currency_pos )
);
?>
-
%s',
pqc_money_format( $shipping_cost, $currency, true, $currency_pos )
);
?>
-
Total: %s',
pqc_money_format( $total, $currency, true, $currency_pos )
);
?>
post_type != $this->key ) return;
if ( ! current_user_can( 'manage_options' ) ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE || $post->post_status == 'auto-draft' ) return;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) return;
if ( ! isset( $_POST[$this->key . '_data'] ) ) return;
if ( wp_verify_nonce( $_POST[$this->key . '_data'], $this->key . '_nonce' ) == false ) return;
$order_action = sanitize_text_field( $_POST['order_action'] );
// Validate
if ( ! in_array( $order_action, $this->default_data['actions'] ) ) return;
$data = get_post_custom();
$data = isset( $data[$this->key . '_data'] ) ?
maybe_unserialize( maybe_unserialize( $data[$this->key . '_data'][0] ) ) : $this->default_data;
$data = wp_parse_args( array( 'order_action' => $order_action ), $data );
if (
isset($_POST['order_items_bulk_actions']) &&
$_POST['order_items_bulk_actions'] == 'astroprint' &&
isset($_POST['items_cb']) &&
!empty($_POST['items_cb'])
) {
for( $i = 0; $i < count( $data['items'] ); $i++ ) {
$current = $data['items'][$i];
if ( ! array_key_exists( $current['unique_id'], $_POST['items_cb'] ) ) continue;
$files[$current['name']] = $current['url'];
}
if ( empty( $files ) ) return;
pqc_session_start();
require_once PQC_PATH . 'admin/templates/orders/astroprint.php';
$astroprint = new PQC_Orders_AstroPrint($files);
$request = $astroprint->request();
return;
} elseif (
$data['order_status'] == 'pending' &&
$data['currency'] == pqc_money_format_control()['currency']
) {
// Update item price, subtotal, cart total and total if payment status is pending
for( $i = 0; $i < count( $data['items'] ); $i++ ) {
$current = $data['items'][$i];
if ( ! array_key_exists( $current['unique_id'], $_POST['item_price'] ) ) continue;
$price = pqc_number_format_raw( $_POST['item_price'][$current['unique_id']] );
if ( ! isset( $data['items'][$i]['infill'] ) || empty( $data['items'][$i]['infill'] ) )
$data['items'][$i]['infill'] = 100;
$data['items'][$i]['price'] = $price;
$data['items'][$i]['amount'] = floatval( $price * $data['items'][$i]['quantity'] );
$subtotal[] = $data['items'][$i]['amount'];
}
$subtotal = array_sum( $subtotal );
$cart_total = $subtotal;
if ( isset( $data['coupon'] ) && ! empty( $data['coupon'] ) ) {
$coupon_id = (int) pqc_get_coupon_id( esc_attr( $data['coupon'] ) );
$old_subtotal = $subtotal;
$subtotal = pqc_apply_coupon( $coupon_id, $subtotal );
if ( $subtotal === false ) $subtotal = $old_subtotal;
}
$total = $subtotal;
if ( isset( $data['shipping_cost'] ) && ! empty( $data['shipping_cost'] ) ) {
$shipping_option_cost = floatval( $data['shipping_cost'] );
$total = $subtotal + $shipping_option_cost;
}
$data['pay_for_order_id'] = uniqid( pqc_get_random_string() );
$data['allow_payment'] = isset( $_POST['allow_payment'] ) ? 1 : 0;
$data['cart_total'] = $cart_total;
$data['subtotal'] = $subtotal;
$data['total'] = $total;
$send_email = $data['allow_payment'] == 1 ? true : false;
}
update_post_meta( $post_id, $this->key . '_data', $data );
if ( isset( $send_email ) && $send_email ) pqc_send_order_email( $post_id );
}
/**
* Enqueue/Dequeue Admin Scripts
*
*/
public function admin_scripts()
{
if ( $this->key != get_current_screen()->post_type ) return;
wp_dequeue_script( PQC_NAME . ' URL MOD' );
}
}
new PQC_Orders();