Measurements WordPress Plugin

mail@pastecode.io avatar
unknown
php
2 years ago
13 kB
4
Indexable
Never
<?php
/*
Plugin Name: Measurments management
Authors: Michał Łabaz, Adam Regulski, Filip Sowa, Toseef Khan
Author: Michał Łabaz, Adam Regulski, Filip Sowa, Toseef Khan
Author URI: https://stronylabaz.pl/
Description: Plugin designed as Team Preengineering Project
*/

require_once( ABSPATH . "wp-includes/pluggable.php" );

// Creating Measurements Custom Post Type
function measurements() {
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Measurements', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Measurement', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Measurements', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Parent measurement', 'twentytwentyone' ),
        'all_items'           => __( 'All measurements', 'twentytwentyone' ),
        'view_item'           => __( 'View measurement', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add measurement', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit measurement', 'twentytwentyone' ),
        'update_item'         => __( 'Update measurement', 'twentytwentyone' ),
        'search_items'        => __( 'Search measurements', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
      
// Set other options for Custom Post Type
      
    $args = array(
        'label'               => __( 'measurements', 'twentytwentyone' ),
        'description'         => __( 'Measurments of window sizes and other products', 'twentytwentyone' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'author', 'revisions', 'custom-fields', ),
		//'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        'taxonomies'          => array( 'genres' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => true,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
  		'query_var'          => true
    );
      
    // Registering your Custom Post Type
    register_post_type( 'measurements', $args );
  
	/* Custom measurement order statuses */
	
	/* Status: To be assigned */
	register_post_status( 'to_be_assigned', array(
		'label'                     => _x( 'To be assigned', 'post status label', 'twentytwentyone' ),
		'public'                    => true,
		'label_count'               => _n_noop( 'To be assigned <span class="count">(%s)</span>', 'To be assigned <span class="count">(%s)</span>', 'twentytwentyone' ),
		'post_type'                 => array( 'measurements' ), // Define one or more post types the status can be applied to.
		'show_in_admin_all_list'    => true,
		'show_in_admin_status_list' => true,
		'show_in_metabox_dropdown'  => true,
		'show_in_inline_dropdown'   => true,
		'show_in_press_this_dropdown' => true,
		'labels'                      => array(
			'metabox_dropdown' => __( 'To be assigned',        'wp-statuses' ),
			'inline_dropdown'  => __( 'To be assigned',        'wp-statuses' ),
		),
		'dashicon'                  => 'dashicons-businessman',
	) );
	
	/* Status: Assigned */
	register_post_status( 'assigned', array(
		'label'                     => _x( 'Assigned', 'post status label', 'twentytwentyone' ),
		'public'                    => true,
		'label_count'               => _n_noop( 'Assigned <span class="count">(%s)</span>', 'Assigned <span class="count">(%s)</span>', 'twentytwentyone' ),
		'post_type'                 => array( 'measurements' ), // Define one or more post types the status can be applied to.
		'show_in_admin_all_list'    => true,
		'show_in_admin_status_list' => true,
		'show_in_metabox_dropdown'  => true,
		'show_in_inline_dropdown'   => true,
		'show_in_press_this_dropdown' => true,
		'labels'                      => array(
			'metabox_dropdown' => __( 'Assigned',        'wp-statuses' ),
			'inline_dropdown'  => __( 'Assigned',        'wp-statuses' ),
		),
		'dashicon'                  => 'dashicons-businessman',
	) );
	
	/* Status: Completed */
	register_post_status( 'completed', array(
		'label'                     => _x( 'Complete', 'post status label', 'twentytwentyone' ),
		'public'                    => true,
		'label_count'               => _n_noop( 'Completed <span class="count">(%s)</span>', 'Completed <span class="count">(%s)</span>', 'twentytwentyone' ),
		'post_type'                 => array( 'measurements' ), // Define one or more post types the status can be applied to.
		'show_in_admin_all_list'    => true,
		'show_in_admin_status_list' => true,
		'show_in_metabox_dropdown'  => true,
		'show_in_inline_dropdown'   => true,
		'show_in_press_this_dropdown' => true,
		'labels'                      => array(
			'metabox_dropdown' => __( 'Completed',        'wp-statuses' ),
			'inline_dropdown'  => __( 'Completed',        'wp-statuses' ),
		),
		'dashicon'                  => 'dashicons-businessman',
	) );
	
	/* Status: Invalid */
	register_post_status( 'invalid', array(
		'label'                     => _x( 'Invalid', 'post status label', 'twentytwentyone' ),
		'public'                    => true,
		'label_count'               => _n_noop( 'Invalid <span class="count">(%s)</span>', 'Invalids <span class="count">(%s)</span>', 'twentytwentyone' ),
		'post_type'                 => array( 'measurements' ), // Define one or more post types the status can be applied to.
		'show_in_admin_all_list'    => true,
		'show_in_admin_status_list' => true,
		'show_in_metabox_dropdown'  => true,
		'show_in_inline_dropdown'   => true,
		'labels'                      => array(
			'metabox_dropdown' => __( 'Invalid',        'wp-statuses' ),
			'inline_dropdown'  => __( 'Invalid',        'wp-statuses' ),
		),
		'dashicon'                  => 'dashicons-dismiss',
	) );
}
add_action( 'init', 'measurements', 0 );

//UI label: Assigned person
add_filter( 'manage_edit-measurements_columns', 'ui_assigned_person_label' );
function ui_assigned_person_label($columns) {
    $columns['author'] = 'Assigned person';
    return $columns;
}

//UI label: Measurement date
add_filter( 'manage_edit-measurements_columns', 'ui_measurement_date_label' );
function ui_measurement_date_label($columns) {
    $columns['date'] = 'Measurement date';
    return $columns;
}

// All other than registered custom statuses (eg: Publish, Private...) won't be applied to measurements
function remove_default_post_statuses($post_types = array(), $status_name = '') {
return array_diff( $post_types, array('measurements') );
}
add_filter( 'wp_statuses_get_registered_post_types', 'remove_default_post_statuses', 10, 2 );

//Set custom status "To be assigned" to manually added measurement
function post_and_set_custom_status_automatically($data = array(), $postarr = array()) {
	if (empty($postarr['publish'])) {
		return $data;
	}
	
	if ('measurements' !== $data['post_type']) {
		return $data;
	}
	
	if (!empty( $postarr['_wp_statuses_status'] ) && in_array( $postarr['_wp_statuses_status'], array(
		'completed',
		'assigned',
		'to_be_assigned',
		'invalid'
		), true ) ) {
		$data['post_status'] = sanitize_key( $postarr['_wp_statuses_status'] );
	} else {
		$data['post_status'] = 'to_be_assigned';
	}
return $data;
}
add_filter( 'wp_insert_post_data', 'post_and_set_custom_status_automatically', 10, 2 );

//Display form using shortcode

// function that runs when shortcode is called
function get_measurement_shortcode() { ?>
<?php ob_start(); ?>

<!-- HTML form code -->
<form method="post" name="front_end" action="" >
   <!-- Customer's firstname --> 
   <label for="name">Name and surname*
   <input type="text" id="name" name="title"></label>
   <!-- Customer's Phone Number -->
   <label for="tele">Phone Number*
   <input type="number" id="tele" name="phone_number"></label>
   <!-- Customer's Email Address -->
   <label for="email">Email Address
   <input type="email" id="email" name="email"></label>
   <!-- Product -->
   <label for="product">Choose a product:</label>
   <select name="product" id="product">
      <option value="product1">Product 1</option>
      <option value="product2">Product 2</option>
      <option value="product3">Product 3</option>
   </select>
   <!-- Customer's Measurements  -->
   <label>Street
   <input type="text" id="street" name="street"></label>
   <label>House number:
   <input type="text" id="number" name="number"></label>
   <label>Zip code:
   <input type="text" id="zip" name="zip"></label>
   <label>City:
   <input type="text" id="city" name="city"></label>
   <!-- Additional information -->
   <label>Additional Information:
   <input type="text" id="AInfo" name="additional_info"></label>
   <input type="submit" name="action" value="my_post_type">
</form>

<?php
// Sanitaze gathered data from form
 $message = ob_get_clean(); 
 return $message;
}
// Register shortcode
add_shortcode('measurement_form', 'get_measurement_shortcode');

//Post form into measurements custom post type
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "my_post_type") {

//Store post vars into variables for later use
	$title     = $_POST['title'];
  //$content   = $_POST['content'];
	$post_type = 'Measurements';
	$custom_field_1 = $_POST['phone_number'];
	$custom_field_2 = $_POST['email'];   
	$custom_field_3 = $_POST['street']; 
	$custom_field_4 = $_POST['number']; 
	$custom_field_5 = $_POST['zip']; 
	$custom_field_6 = $_POST['additional_info'];
	$custom_field_7 = $_POST['product']; 
	$custom_field_8 = $_POST['city']; 
	
//The array of arguements to be inserted with wp_insert_post
	$new_post = array(
	'post_title'    => $title,
	//'post_content'  => $content,
	'post_status'   => 'to_be_assigned',          
	'post_type'     => $post_type 
	);

//Inserting the the post into database by passing $new_post to wp_insert_post

//Measurement ID stored in a variable $pid
$pid = wp_insert_post($new_post);

//$pid post to meta data
	add_post_meta($pid, 'Phone Number', $custom_field_1, true);
	add_post_meta($pid, 'Email Address', $custom_field_2, true);
	add_post_meta($pid, 'Product', $custom_field_7, true);
	add_post_meta($pid, 'Street', $custom_field_3, true);
	add_post_meta($pid, 'House number', $custom_field_4, true);
	add_post_meta($pid, 'Zip code', $custom_field_5, true);
	add_post_meta($pid, 'City', $custom_field_8, true);
	add_post_meta($pid, 'Additional Information', $custom_field_6, true);
}

//Add technician role
function add_technician_role() {
    if ( get_option( 'custom_roles_version' ) < 1 ) {
        add_role( 'custom_role', 'technician', array( 'read' => true, 'level_0' => true ) );
        update_option( 'custom_roles_version', 1 );
    }
}
add_action( 'init', 'add_technician_role' );

// function add_measurement_caps() {
//     // gets the administrator role
//     $admins = get_role( 'technician' );
	
//     $admins->add_cap( 'Edit measurement'); 
//     $admins->add_cap( 'edit_galleries' ); 
//     $admins->add_cap( 'edit_other_galleries' ); 
//     $admins->add_cap( 'publish_galleries' ); 
//     $admins->add_cap( 'read_gallery' ); 
//     $admins->add_cap( 'read_private_galleries' ); 
//     $admins->add_cap( 'delete_gallery' ); 
// }
// add_action( 'admin_init', 'add_measurement_caps');

//         'name'                => _x( 'Measurements', 'Post Type General Name', 'twentytwentyone' ),
//         'singular_name'       => _x( 'Measurement', 'Post Type Singular Name', 'twentytwentyone' ),
//         'menu_name'           => __( 'Measurements', 'twentytwentyone' ),
//         'parent_item_colon'   => __( 'Parent measurement', 'twentytwentyone' ),
//         'all_items'           => __( 'All measurements', 'twentytwentyone' ),
//         'view_item'           => __( 'View measurement', 'twentytwentyone' ),
//         'add_new_item'        => __( 'Add measurement', 'twentytwentyone' ),
//         'add_new'             => __( 'Add New', 'twentytwentyone' ),
//         'edit_item'           => __( 'Edit measurement', 'twentytwentyone' ),
//         'update_item'         => __( 'Update measurement', 'twentytwentyone' ),
//         'search_items'        => __( 'Search measurements', 'twentytwentyone' ),
//         'not_found'           => __( 'Not Found', 'twentytwentyone' ),
//         'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),

?>