Skip to content

Instantly share code, notes, and snippets.

@miklb
Created April 6, 2016 17:14
Show Gist options
  • Save miklb/8d94f64ec7c1168fd48fc90f170b2f57 to your computer and use it in GitHub Desktop.
Save miklb/8d94f64ec7c1168fd48fc90f170b2f57 to your computer and use it in GitHub Desktop.
CPT forms front-end w/CMB2
<?php
add_action( 'init', 'chapel_client_init' );
/**
* Create Client Custom Post Type
*
*/
function chapel_client_init() {
$labels = array(
'name' => 'client',
'singular_name' => 'Client',
'add_new' => 'Add New', 'Client',
'add_new_item' => 'Add New Client',
'menu_name' => 'Clients',
'name_admin_bar' => 'Clients',
'new_item' => 'New Client',
'edit_item' => 'Edit Client',
'view_item' => 'View Client',
'all_items' => 'Show Clients',
'search_items' => 'Search Clients',
'not_found' => 'No Clients found.',
'not_found_in_trash' => 'No Client found in Trash.',
'parent_item_colon' => '',
);
$args = array(
'labels' => $labels,
'description' => __( 'List of all clients.' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-groups',
'query_var' => true,
'rewrite' => array( 'slug' => 'clients' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
//Future proof and expose to REST API
'show_in_rest' => true,
'supports' => array( 'title' ),
);
register_post_type( 'clients', $args );
}
/**
*
* Change text on title input
*
*/
function chapel_change_client_title_text( $title ) {
$screen = get_current_screen();
if ('client' == $screen->post_type) {
$title = 'Enter Client Name';
}
return $title;
}
add_filter( 'enter_title_here', 'chapel_change_client_title_text' );
require_once __DIR__ . '../../CMB2/init.php';
require_once __DIR__ . '../../CMB2-Pipes/cmb2-pipes.php';
/**
* Client Meta Boxes
*/
function chapel_client_metaboxes() {
// underscore hides meta box from custom field list
$prefix = '_chapel_';
$cmb = new_cmb2_box(array(
'id' => 'client_metabox',
'title' => __( 'Client Contact Information', 'chapel' ),
'object_types' => array( 'clients' ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
));
$cmb->add_field(array(
'name' => __( 'Client Name', 'chapel' ),
// no meta called `client` will be written, not really relevant
'id' => cmb2_pipe( 'client_title', '<>', 'post_title' ),
'type' => 'text',
));
$cmb->add_field(array(
'name' => __( 'Street Address', 'chapel' ),
'desc' => __( 'mailing address', 'chapel' ),
'id' => $prefix . 'client_street',
'type' => 'text',
));
$cmb->add_field(array(
'name' => __( 'Street Address', 'chapel' ),
'id' => $prefix . 'client_street2',
'type' => 'text',
));
$cmb->add_field(array(
'name' => __( 'City', 'chapel' ),
'id' => $prefix . 'client_city',
'type' => 'text',
));
$cmb->add_field(array(
'name' => __( 'State', 'chapel' ),
'id' => $prefix . 'client_state',
'type' => 'select',
'options' => array(
'AL' => __( 'Alabama','chapel' ),
'AK' => __( 'Alaska','chapel' ),
'AZ' => __( 'Arizona','chapel' ),
'AR' => __( 'Arkansas','chapel' ),
'CA' => __( 'California','chapel' ),
'CO' => __( 'Colorado','chapel' ),
'CT' => __( 'Connecticut','chapel' ),
'DE' => __( 'Delaware','chapel' ),
'DC' => __( 'District of Columbia','chapel' ),
'FL' => __( 'Florida','chapel' ),
'GA' => __( 'Georgia','chapel' ),
'HI' => __( 'Hawaii','chapel' ),
'ID' => __( 'Idaho','chapel' ),
'IL' => __( 'Illinois','chapel' ),
'IN' => __( 'Indiana','chapel' ),
'IA' => __( 'Iowa','chapel' ),
'KS' => __( 'Kansas','chapel' ),
'KY' => __( 'Kentucky','chapel' ),
'LA' => __( 'Louisiana','chapel' ),
'ME' => __( 'Maine','chapel' ),
'MD' => __( 'Maryland','chapel' ),
'MA' => __( 'Massachusetts','chapel' ),
'MI' => __( 'Michigan','chapel' ),
'MN' => __( 'Minnesota','chapel' ),
'MS' => __( 'Mississippi','chapel' ),
'MO' => __( 'Missouri','chapel' ),
'MT' => __( 'Montana','chapel' ),
'NE' => __( 'Nebraska','chapel' ),
'NV' => __( 'Nevada','chapel' ),
'NH' => __( 'New Hampshire','chapel' ),
'NJ' => __( 'New Jersey','chapel' ),
'NM' => __( 'New Mexico','chapel' ),
'NY' => __( 'New York','chapel' ),
'NC' => __( 'North Carolina','chapel' ),
'ND' => __( 'North Dakota','chapel' ),
'OH' => __( 'Ohio','chapel' ),
'OK' => __( 'Oklahoma','chapel' ),
'OR' => __( 'Oregon','chapel' ),
'PA' => __( 'Pennsylvania','chapel' ),
'RI' => __( 'Rhode Island','chapel' ),
'SC' => __( 'South Carolina','chapel' ),
'SD' => __( 'South Dakota','chapel' ),
'TN' => __( 'Tennessee','chapel' ),
'TX' => __( 'Texas','chapel' ),
'UT' => __( 'Utah','chapel' ),
'VT' => __( 'Vermont','chapel' ),
'VA' => __( 'Virginia','chapel' ),
'WA' => __( 'Washington','chapel' ),
'WV' => __( 'West Virginia','chapel' ),
'WI' => __( 'Wisconsin','chapel' ),
'WY' => __( 'Wyoming','chapel' ),
),
));
$cmb->add_field(array(
'name' => __( 'Zip Code', 'chapel' ),
'id' => $prefix . 'client_zip',
'type' => 'text_small',
));
$cmb->add_field(array(
'name' => __( 'Phone', 'chapel' ),
'id' => $prefix . 'client_phone',
'type' => 'text_medium',
));
$cmb->add_field(array(
'name' => __( 'Sales Tax Exempt', 'chapel' ),
'desc' => __( 'check if client is sales tax exempt.', 'chapel' ),
'id' => $prefix . 'client_salestax',
'type' => 'checkbox',
));
$cmb->add_field(array(
'name' => __( 'State Issued Tax Exempt Number', 'chapel' ),
'desc' => __( 'your state issue tax exempt number if applicaple', 'chapel' ),
'id' => $prefix .'client_exemptnumber',
'type' => 'text_medium',
));
$cmb->add_field(array(
'name' => __( 'Payment Terms', 'chapel' ),
'desc' => __( 'terms of payment', 'chapel' ),
'id' => $prefix . 'client_terms',
'type' => 'radio',
'options' => array(
'cod' => __( 'COD', 'chapel' ),
'net15' => __( 'Net 15', 'chapel' ),
'net30' => __( 'Net 30', 'chapel' ),
),
));
$cmb->add_field(array(
'name' => __( 'Notes', 'chapel' ),
'desc' => __( 'Additonal notes about client.', 'chapel' ),
'id' => $prefix . 'clientnotes',
'type' => 'textarea_small',
));
/**
*
* Add repeatable group for Contacts
*
**/
$group_field_id = $cmb->add_field(array(
'id' => $prefix . 'clients',
'type' => 'group',
'description' => __( 'Client contacts.', 'chapel' ),
'options' => array(
'group_title' => __( 'Contact {#}', 'chapel' ), // {#} gets replaced by row number
'add_button' => __( 'Add Another Contact', 'chapel' ),
'remove_button' => __( 'Remove Contact', 'chapel' ),
'sortable' => true, // beta
),
));
/**
* Group fields works the same, except ids only need
* to be unique to the group. Prefix is not needed.
*
* The parent field's id needs to be passed as the first argument.
*/
$cmb->add_group_field($group_field_id, array(
'name' => __( 'Contact Name', 'chapel' ),
'id' => 'name',
'type' => 'text',
));
$cmb->add_group_field($group_field_id, array(
'name' => __( 'Contact Email', 'chapel' ),
'id' => 'email',
'type' => 'text_email',
));
$cmb->add_group_field($group_field_id, array(
'name' => __( 'Contact Phone #', 'chapel' ),
'id' => 'phone',
'type' => 'text_medium',
));
$cmb->add_group_field($group_field_id, array(
'name' => __( 'Contact Role', 'chapel' ),
'id' => 'role',
'type' => 'text',
));
}
add_filter( 'cmb2_init', 'chapel_client_metaboxes' );
function chapel_new_client_metaboxes() {
// underscore hides meta box from custom field list
$prefix = '_chapel_';
$cmb = new_cmb2_box(array(
'id' => 'new_client_metabox',
'title' => __( 'Client Contact Information', 'chapel' ),
'object_types' => array( 'clients' ),
));
$cmb->add_field(array(
'name' => __( 'Client Name', 'chapel' ),
// no meta called `client` will be written, not really relevant
'id' => 'submitted_client_name',
'type' => 'text',
));
$cmb->add_field(array(
'name' => __( 'Street Address', 'chapel' ),
'desc' => __( 'mailing address', 'chapel' ),
'id' => $prefix . 'client_street',
'type' => 'text',
));
$cmb->add_field(array(
'name' => __( 'Street Address', 'chapel' ),
'id' => $prefix . 'client_street2',
'type' => 'text',
));
$cmb->add_field(array(
'name' => __( 'City', 'chapel' ),
'id' => $prefix . 'client_city',
'type' => 'text',
));
$cmb->add_field(array(
'name' => __( 'State', 'chapel' ),
'id' => $prefix . 'client_state',
'type' => 'select',
'options' => array(
'AL' => __( 'Alabama','chapel' ),
'AK' => __( 'Alaska','chapel' ),
'AZ' => __( 'Arizona','chapel' ),
'AR' => __( 'Arkansas','chapel' ),
'CA' => __( 'California','chapel' ),
'CO' => __( 'Colorado','chapel' ),
'CT' => __( 'Connecticut','chapel' ),
'DE' => __( 'Delaware','chapel' ),
'DC' => __( 'District of Columbia','chapel' ),
'FL' => __( 'Florida','chapel' ),
'GA' => __( 'Georgia','chapel' ),
'HI' => __( 'Hawaii','chapel' ),
'ID' => __( 'Idaho','chapel' ),
'IL' => __( 'Illinois','chapel' ),
'IN' => __( 'Indiana','chapel' ),
'IA' => __( 'Iowa','chapel' ),
'KS' => __( 'Kansas','chapel' ),
'KY' => __( 'Kentucky','chapel' ),
'LA' => __( 'Louisiana','chapel' ),
'ME' => __( 'Maine','chapel' ),
'MD' => __( 'Maryland','chapel' ),
'MA' => __( 'Massachusetts','chapel' ),
'MI' => __( 'Michigan','chapel' ),
'MN' => __( 'Minnesota','chapel' ),
'MS' => __( 'Mississippi','chapel' ),
'MO' => __( 'Missouri','chapel' ),
'MT' => __( 'Montana','chapel' ),
'NE' => __( 'Nebraska','chapel' ),
'NV' => __( 'Nevada','chapel' ),
'NH' => __( 'New Hampshire','chapel' ),
'NJ' => __( 'New Jersey','chapel' ),
'NM' => __( 'New Mexico','chapel' ),
'NY' => __( 'New York','chapel' ),
'NC' => __( 'North Carolina','chapel' ),
'ND' => __( 'North Dakota','chapel' ),
'OH' => __( 'Ohio','chapel' ),
'OK' => __( 'Oklahoma','chapel' ),
'OR' => __( 'Oregon','chapel' ),
'PA' => __( 'Pennsylvania','chapel' ),
'RI' => __( 'Rhode Island','chapel' ),
'SC' => __( 'South Carolina','chapel' ),
'SD' => __( 'South Dakota','chapel' ),
'TN' => __( 'Tennessee','chapel' ),
'TX' => __( 'Texas','chapel' ),
'UT' => __( 'Utah','chapel' ),
'VT' => __( 'Vermont','chapel' ),
'VA' => __( 'Virginia','chapel' ),
'WA' => __( 'Washington','chapel' ),
'WV' => __( 'West Virginia','chapel' ),
'WI' => __( 'Wisconsin','chapel' ),
'WY' => __( 'Wyoming','chapel' ),
),
));
$cmb->add_field(array(
'name' => __( 'Zip Code', 'chapel' ),
'id' => $prefix . 'client_zip',
'type' => 'text_small',
));
$cmb->add_field(array(
'name' => __( 'Phone', 'chapel' ),
'id' => $prefix . 'client_phone',
'type' => 'text_medium',
));
$cmb->add_field(array(
'name' => __( 'Sales Tax Exempt', 'chapel' ),
'desc' => __( 'check if client is sales tax exempt.', 'chapel' ),
'id' => $prefix . 'client_salestax',
'type' => 'checkbox',
));
$cmb->add_field(array(
'name' => __( 'State Issued Tax Exempt Number', 'chapel' ),
'desc' => __( 'your state issue tax exempt number if applicaple', 'chapel' ),
'id' => $prefix .'client_exemptnumber',
'type' => 'text_medium',
));
$cmb->add_field(array(
'name' => __( 'Payment Terms', 'chapel' ),
'desc' => __( 'terms of payment', 'chapel' ),
'id' => $prefix . 'client_terms',
'type' => 'radio',
'options' => array(
'cod' => __( 'COD', 'chapel' ),
'net15' => __( 'Net 15', 'chapel' ),
'net30' => __( 'Net 30', 'chapel' ),
),
));
$cmb->add_field(array(
'name' => __( 'Notes', 'chapel' ),
'desc' => __( 'Additonal notes about client.', 'chapel' ),
'id' => $prefix . 'clientnotes',
'type' => 'textarea_small',
));
/**
*
* Add repeatable group for Contacts
*
**/
$group_field_id = $cmb->add_field(array(
'id' => $prefix . 'clients',
'type' => 'group',
'description' => __( 'Client contacts.', 'chapel' ),
'options' => array(
'group_title' => __( 'Contact {#}', 'chapel' ), // {#} gets replaced by row number
'add_button' => __( 'Add Another Contact', 'chapel' ),
'remove_button' => __( 'Remove Contact', 'chapel' ),
'sortable' => true, // beta
),
));
/**
* Group fields works the same, except ids only need
* to be unique to the group. Prefix is not needed.
*
* The parent field's id needs to be passed as the first argument.
*/
$cmb->add_group_field($group_field_id, array(
'name' => __( 'Contact Name', 'chapel' ),
'id' => 'name',
'type' => 'text',
));
$cmb->add_group_field($group_field_id, array(
'name' => __( 'Contact Email', 'chapel' ),
'id' => 'email',
'type' => 'text_email',
));
$cmb->add_group_field($group_field_id, array(
'name' => __( 'Contact Phone #', 'chapel' ),
'id' => 'phone',
'type' => 'text_medium',
));
$cmb->add_group_field($group_field_id, array(
'name' => __( 'Contact Role', 'chapel' ),
'id' => 'role',
'type' => 'text',
));
}
add_filter( 'cmb2_init', 'chapel_new_client_metaboxes' );
function chapel_client_cmb2_get() {
// Use ID of metabox in chapel_client_metaboxes
$metabox_id = 'new_client_metabox';
// Post/object ID is not applicable since we're using this form for submission
$object_id = 'fake-oject-id2';
// Get CMB2 metabox object
return cmb2_get_metabox( $metabox_id, $object_id );
}
function chapel_do_client_form_submission_shortcode( $atts = array() ) {
// Get CMB2 metabox object
$cmb = chapel_client_cmb2_get();
// Get $cmb object_types
$post_types = $cmb->prop( 'object_types' );
// Current user
$user_id = get_current_user_id();
// Parse attributes
$atts = shortcode_atts( array(
'post_author' => $user_id ? $user_id : 1, // Current user, or admin
'post_status' => 'publish',
'post_type' => reset( $post_types ), // Only use first object_type in array
), $atts, 'new_client_metabox' );
/*
* Let's add these attributes as hidden fields to our cmb form
* so that they will be passed through to our form submission
*/
foreach ( $atts as $key => $value ) {
$cmb->add_hidden_field( array(
'field_args' => array(
'id' => "atts[$key]",
'type' => 'hidden',
'default' => $value,
),
) );
}
// Initiate our output variable
$output = '';
// Get any submission errors
if ( ( $error = $cmb->prop( 'submission_error' ) ) && is_wp_error( $error ) ) {
// If there was an error with the submission, add it to our ouput.
$output .= '<h3>' . sprintf( __( 'There was an error in the submission: %s', 'wds-post-submit' ), '<strong>'. $error->get_error_message() .'</strong>' ) . '</h3>';
}
// If the post was submitted successfully, notify the user.
if ( isset( $_GET['post_submitted'] ) && ( $post = get_post( absint( $_GET['post_submitted'] ) ) ) ) {
// Get submitter's name
$name = get_post_meta( $post->ID, 'submitted_author_name', 1 );
$name = $name ? ' '. $name : '';
// Add notice of submission to our output
$output .= '<h3>' . sprintf( __( 'New client created.', 'wds-post-submit' ), esc_html( $name ) ) . '</h3>';
}
// Get our form
$output .= cmb2_get_metabox_form( $cmb, 'fake-oject-id2', array( 'save_button' => __( 'Create client', 'wds-post-submit' ) ) );
return $output;
}
add_shortcode( 'chapel_new_client', 'chapel_do_client_form_submission_shortcode' );
/**
* Handles form submission on save. Redirects if save is successful, otherwise sets an error message as a cmb property
*
* @return void
*/
function chapel_handle_client_new_post_form_submission() {
// If no form submission, bail
if ( empty( $_POST ) || ! isset( $_POST['submit-cmb'], $_POST['object_id'] ) ) {
return false;
}
// Get CMB2 metabox object
$cmb = chapel_client_cmb2_get();
$post_data = array();
// Get our shortcode attributes and set them as our initial post_data args
if ( isset( $_POST['atts'] ) ) {
foreach ( (array) $_POST['atts'] as $key => $value ) {
$post_data[ $key ] = sanitize_text_field( $value );
}
unset( $_POST['atts'] );
}
// Check security nonce
if ( ! isset( $_POST[ $cmb->nonce() ] ) || ! wp_verify_nonce( $_POST[ $cmb->nonce() ], $cmb->nonce() ) ) {
return $cmb->prop( 'submission_error', new WP_Error( 'security_fail', __( 'Security check failed.' ) ) );
}
// Check title submitted
if ( empty( $_POST['submitted_client_name'] ) ) {
return $cmb->prop( 'submission_error', new WP_Error( 'post_data_missing', __( 'New client requires a name.' ) ) );
}
// And that the title is not the default title
if ( $cmb->get_field( 'submitted_client_name' )->default() == $_POST['submitted_client_name'] ) {
return $cmb->prop( 'submission_error', new WP_Error( 'post_data_missing', __( 'Please enter a new name.' ) ) );
}
/**
* Fetch sanitized values
*/
$sanitized_values = $cmb->get_sanitized_values( $_POST );
// Set our post data arguments
$post_data['post_title'] = $sanitized_values['submitted_client_name'];
unset( $sanitized_values['submitted_client_name'] );
// Create the new post
$new_submission_id = wp_insert_post( $post_data, true );
// If we hit a snag, update the user
if ( is_wp_error( $new_submission_id ) ) {
return $cmb->prop( 'submission_error', $new_submission_id );
}
/**
* Other than post_type and post_status, we want
* our uploaded attachment post to have the same post-data
*/
unset( $post_data['post_type'] );
unset( $post_data['post_status'] );
// Loop through remaining (sanitized) data, and save to post-meta
foreach ( $sanitized_values as $key => $value ) {
if ( is_array( $value ) ) {
$value = array_filter( $value );
if ( ! empty( $value ) ) {
update_post_meta( $new_submission_id, $key, $value );
}
} else {
update_post_meta( $new_submission_id, $key, $value );
}
}
/*
* Redirect back to the form page with a query variable with the new post ID.
* This will help double-submissions with browser refreshes
*/
wp_redirect( esc_url_raw( add_query_arg( 'post_submitted', $new_submission_id ) ) );
exit;
}
add_action( 'cmb2_after_init', 'chapel_handle_client_new_post_form_submission' );
/* Paper */
require 'includes/paper.php';
/* Client */
require_once 'includes/client.php';
<?php
function chapel_paper() {
$labels = array(
'name' => 'Paper',
'singular_name' => 'Paper',
'add_new' => 'Add New', 'paper',
'add_new_item' => 'Add New Paper',
'menu_name' => 'Paper',
'name_admin_bar' => 'Paper',
'new_item' => 'New Paper',
'edit_item' => 'Edit Paper',
'view_item' => 'View Paper',
'all_items' => 'Show Paper',
'search_items' => 'Search Paper',
'not_found' => 'No paper found.',
'not_found_in_trash' => 'No paper found in Trash.',
'parent_item_colon' => '',
);
$args = array(
'labels' => $labels,
'description' => 'Stores the main paper data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title' ),
'has_archive' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
);
register_post_type( 'paper', $args );
}
add_action( 'init', 'chapel_paper' );
// Function to change admin enter title text
function chapel_change_paper_title_text( $title ) {
$screen = get_current_screen();
if ( 'paper' == $screen->post_type ) {
$title = 'Enter name of stock';
}
return $title;
}
add_filter( 'enter_title_here', 'chapel_change_paper_title_text' );
// Meta = = = = = = = = = = = = = = = = = = = = =
require_once __DIR__ . '../../CMB2/init.php';
require_once __DIR__ . '../../CMB2-Pipes/cmb2-pipes.php';
function chapel_paper_metaboxes() {
$prefix = '_chapel_';
$cmb = new_cmb2_box( array(
'id' => 'paper_metabox',
'title' => __( 'Paper Data','chapel' ),
'object_types' => array( 'paper' ),
) );
$cmb->add_field( array(
'name' => __( 'Paper Name', 'CHAPEL' ),
'id' => cmb2_pipe( 'paper_title','<>','post_title' ),
'type' => 'text',
) );
$cmb->add_field(array(
'name' => __( 'Cover or Text', 'chapel' ),
'id' => $prefix . 'covertext',
'type' => 'radio_inline',
'options' => array(
'cover' => __( 'Cover', 'chapel' ),
'text' => __( 'Text', 'chapel' ),
),
));
$cmb->add_field(array(
'name' => __( 'Weight', 'chapel' ),
'id' => $prefix . 'pweight',
'type' => 'select',
'options' => array(
'24' => __( '24#', 'chapel' ),
'28' => __( '28#', 'chapel' ),
'32' => __( '32#', 'chapel' ),
'50' => __( '50#', 'chapel' ),
'60' => __( '60#','chapel' ),
'65' => __( '65#', 'chapel' ),
'70' => __( '70#', 'chapel' ),
'80' => __( '80#', 'chapel' ),
'100' => __( '100#','chapel' ),
'110' => __( '110#', 'chapel' ),
'118' => __( '118#','chapel' ),
'120' => __( '120#', 'chapel' ),
'130' => __( '130#', 'chapel' ),
'140' => __( '140#', 'chapel' ),
'160' => __( '160#', 'chapel' ),
'165' => __( '165#', 'chapel' ),
'180' => __( '180#', 'chapel' ),
'200' => __( '200#', 'chapel' ),
'220' => __( '220#', 'chapel' ),
'236' => __( '236#', 'chapel' ),
),
));
//@todo make this a select text field
$cmb->add_field( array(
'name' => __( 'Finish', 'chapel' ),
'id' => $prefix . 'finish',
'type' => 'select',
'options' => array(
'coated' => __( 'Coated', 'chapel' ),
'uncoated' => __( 'Uncoated', 'chapel' ),
'eggshell' => __( 'Eggshell', 'chapel' ),
),
));
//@todo determine if this is just a name or full contact info
$cmb->add_field( array(
'name' => __( 'Supplier', 'chapel' ),
'id' => $prefix . 'psupplier',
'type' => 'text',
));
$cmb->add_field(array(
'name' => __( 'Sheet or Envelope', 'chapel' ),
'id' => $prefix . 'sheetenv',
'type' => 'radio_inline',
'options' => array(
'sheet' => __( 'Sheet', 'chapel' ),
'envelope' => __( 'Envelope', 'chapel' ),
),
));
$cmb-> add_field( array(
'name' => __( 'Width','chapel' ),
'id' => $prefix . 'paper_parent_width',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Height', 'chapel' ),
'id' => $prefix .'paper_parent_height',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Sheets per Carton', 'chapel' ),
'id' => $prefix . 'paper_sheets_per_carton',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Minimum Order', 'chapel' ),
'id' => $prefix . 'paper_minimum_order',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Stock Number', 'chapel' ),
'id' => $prefix . 'paper_stock_number',
'type' => 'text_medium',
) );
$cmb->add_field( array(
'name' => __( 'Caliper','chapel' ),
'id' => $prefix . 'paper_caliper',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'M Weight','chapel' ),
'id' => $prefix . 'paper_m_weight',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'GSM','chapel' ),
'id' => $prefix . 'paper_gsm',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Cost', 'chapel' ),
'id' => $prefix . 'paper_cost',
'type' => 'text_money',
) );
$cmb->add_field( array(
'name' => __( 'Cost Per',' chapel' ),
'id' => $prefix . 'paper_cost_per',
'default' => '1000',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Markup', 'chapel' ),
'id' => $prefix . 'paper_markup',
'default' => '1.5',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
}
add_filter( 'cmb2_init', 'chapel_paper_metaboxes' );
function chapel_new_paper_metaboxes() {
$prefix = '_chapel_';
$cmb = new_cmb2_box( array(
'id' => 'new_paper_metabox',
'title' => __( 'Paper Data','chapel' ),
'object_types' => array( 'paper' ),
) );
$cmb->add_field( array(
'name' => __( 'Paper Name', 'CHAPEL' ),
'id' => 'submitted_paper_name',
'type' => 'text',
) );
$cmb->add_field(array(
'name' => __( 'Cover or Text', 'chapel' ),
'id' => $prefix . 'covertext',
'type' => 'radio_inline',
'options' => array(
'cover' => __( 'Cover', 'chapel' ),
'text' => __( 'Text', 'chapel' ),
),
));
$cmb->add_field(array(
'name' => __( 'Weight', 'chapel' ),
'id' => $prefix . 'pweight',
'type' => 'select',
'options' => array(
'24' => __( '24#', 'chapel' ),
'28' => __( '28#', 'chapel' ),
'32' => __( '32#', 'chapel' ),
'50' => __( '50#', 'chapel' ),
'60' => __( '60#','chapel' ),
'65' => __( '65#', 'chapel' ),
'70' => __( '70#', 'chapel' ),
'80' => __( '80#', 'chapel' ),
'100' => __( '100#','chapel' ),
'110' => __( '110#', 'chapel' ),
'118' => __( '118#','chapel' ),
'120' => __( '120#', 'chapel' ),
'130' => __( '130#', 'chapel' ),
'140' => __( '140#', 'chapel' ),
'160' => __( '160#', 'chapel' ),
'165' => __( '165#', 'chapel' ),
'180' => __( '180#', 'chapel' ),
'200' => __( '200#', 'chapel' ),
'220' => __( '220#', 'chapel' ),
'236' => __( '236#', 'chapel' ),
),
));
//@todo make this a select text field
$cmb->add_field( array(
'name' => __( 'Finish', 'chapel' ),
'id' => $prefix . 'finish',
'type' => 'select',
'options' => array(
'coated' => __( 'Coated', 'chapel' ),
'uncoated' => __( 'Uncoated', 'chapel' ),
'eggshell' => __( 'Eggshell', 'chapel' ),
),
));
//@todo determine if this is just a name or full contact info
$cmb->add_field( array(
'name' => __( 'Supplier', 'chapel' ),
'id' => $prefix . 'psupplier',
'type' => 'text',
));
$cmb->add_field(array(
'name' => __( 'Sheet or Envelope', 'chapel' ),
'id' => $prefix . 'sheetenv',
'type' => 'radio_inline',
'options' => array(
'sheet' => __( 'Sheet', 'chapel' ),
'envelope' => __( 'Envelope', 'chapel' ),
),
));
$cmb-> add_field( array(
'name' => __( 'Width','chapel' ),
'id' => $prefix . 'paper_parent_width',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Height', 'chapel' ),
'id' => $prefix .'paper_parent_height',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Sheets per Carton', 'chapel' ),
'id' => $prefix . 'paper_sheets_per_carton',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Minimum Order', 'chapel' ),
'id' => $prefix . 'paper_minimum_order',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Stock Number', 'chapel' ),
'id' => $prefix . 'paper_stock_number',
'type' => 'text_medium',
) );
$cmb->add_field( array(
'name' => __( 'Caliper','chapel' ),
'id' => $prefix . 'paper_caliper',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'M Weight','chapel' ),
'id' => $prefix . 'paper_m_weight',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'GSM','chapel' ),
'id' => $prefix . 'paper_gsm',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Cost', 'chapel' ),
'id' => $prefix . 'paper_cost',
'type' => 'text_money',
) );
$cmb->add_field( array(
'name' => __( 'Cost Per',' chapel' ),
'id' => $prefix . 'paper_cost_per',
'default' => '1000',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
$cmb->add_field( array(
'name' => __( 'Markup', 'chapel' ),
'id' => $prefix . 'paper_markup',
'default' => '1.5',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
) );
}
add_filter( 'cmb2_init', 'chapel_new_paper_metaboxes' );
function chapel_paper_cmb2_get() {
// Use ID of metabox in chapel_paper_metaboxes
$metabox_id = 'new_paper_metabox';
// Post/object ID is not applicable since we're using this form for submission
$object_id = 'fake-oject-id';
// Get CMB2 metabox object
return cmb2_get_metabox( $metabox_id, $object_id );
}
function chapel_do_paper_form_submission_shortcode( $atts = array() ) {
// Get CMB2 metabox object
$cmb = chapel_paper_cmb2_get();
// Get $cmb object_types
$post_types = $cmb->prop( 'object_types' );
// Current user
$user_id = get_current_user_id();
// Parse attributes
$atts = shortcode_atts( array(
'post_author' => $user_id ? $user_id : 1, // Current user, or admin
'post_status' => 'publish',
'post_type' => reset( $post_types ), // Only use first object_type in array
), $atts, 'new_paper_metabox' );
/*
* Let's add these attributes as hidden fields to our cmb form
* so that they will be passed through to our form submission
*/
foreach ( $atts as $key => $value ) {
$cmb->add_hidden_field( array(
'field_args' => array(
'id' => "atts[$key]",
'type' => 'hidden',
'default' => $value,
),
) );
}
// Initiate our output variable
$output = '';
// Get any submission errors
if ( ( $error = $cmb->prop( 'submission_error' ) ) && is_wp_error( $error ) ) {
// If there was an error with the submission, add it to our ouput.
$output .= '<h3>' . sprintf( __( 'There was an error in the submission: %s', 'wds-post-submit' ), '<strong>'. $error->get_error_message() .'</strong>' ) . '</h3>';
}
// If the post was submitted successfully, notify the user.
if ( isset( $_GET['post_submitted'] ) && ( $post = get_post( absint( $_GET['post_submitted'] ) ) ) ) {
// Get submitter's name
$name = get_post_meta( $post->ID, 'submitted_author_name', 1 );
$name = $name ? ' '. $name : '';
// Add notice of submission to our output
$output .= '<h3>' . sprintf( __( 'New Paper created.', 'wds-post-submit' ), esc_html( $name ) ) . '</h3>';
}
// Get our form
$output .= cmb2_get_metabox_form( $cmb, 'fake-oject-id', array( 'save_button' => __( 'Create Paper', 'wds-post-submit' ) ) );
return $output;
}
add_shortcode( 'chapel_new_paper', 'chapel_do_paper_form_submission_shortcode' );
/**
* Handles form submission on save. Redirects if save is successful, otherwise sets an error message as a cmb property
*
* @return void
*/
function chapel_handle_paper_new_post_form_submission() {
// If no form submission, bail
if ( empty( $_POST ) || ! isset( $_POST['submit-cmb'], $_POST['object_id'] ) ) {
return false;
}
// Get CMB2 metabox object
$cmb = chapel_paper_cmb2_get();
$post_data = array();
// Get our shortcode attributes and set them as our initial post_data args
if ( isset( $_POST['atts'] ) ) {
foreach ( (array) $_POST['atts'] as $key => $value ) {
$post_data[ $key ] = sanitize_text_field( $value );
}
unset( $_POST['atts'] );
}
// Check security nonce
if ( ! isset( $_POST[ $cmb->nonce() ] ) || ! wp_verify_nonce( $_POST[ $cmb->nonce() ], $cmb->nonce() ) ) {
return $cmb->prop( 'submission_error', new WP_Error( 'security_fail', __( 'Security check failed.' ) ) );
}
// Check title submitted
if ( empty( $_POST['submitted_paper_name'] ) ) {
return $cmb->prop( 'submission_error', new WP_Error( 'post_data_missing', __( 'New paper requires a name.' ) ) );
}
// And that the title is not the default title
if ( $cmb->get_field( 'submitted_paper_name' )->default() == $_POST['submitted_paper_name'] ) {
return $cmb->prop( 'submission_error', new WP_Error( 'post_data_missing', __( 'Please enter a new name.' ) ) );
}
/**
* Fetch sanitized values
*/
$sanitized_values = $cmb->get_sanitized_values( $_POST );
// Set our post data arguments
$post_data['post_title'] = $sanitized_values['submitted_paper_name'];
unset( $sanitized_values['submitted_paper_name'] );
// Create the new post
$new_submission_id = wp_insert_post( $post_data, true );
// If we hit a snag, update the user
if ( is_wp_error( $new_submission_id ) ) {
return $cmb->prop( 'submission_error', $new_submission_id );
}
/**
* Other than post_type and post_status, we want
* our uploaded attachment post to have the same post-data
*/
unset( $post_data['post_type'] );
unset( $post_data['post_status'] );
// Loop through remaining (sanitized) data, and save to post-meta
foreach ( $sanitized_values as $key => $value ) {
if ( is_array( $value ) ) {
$value = array_filter( $value );
if ( ! empty( $value ) ) {
update_post_meta( $new_submission_id, $key, $value );
}
} else {
update_post_meta( $new_submission_id, $key, $value );
}
}
/*
* Redirect back to the form page with a query variable with the new post ID.
* This will help double-submissions with browser refreshes
*/
wp_redirect( esc_url_raw( add_query_arg( 'post_submitted', $new_submission_id ) ) );
exit;
}
add_action( 'cmb2_after_init', 'chapel_handle_paper_new_post_form_submission' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment