| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to act as a wrapper for a single customer |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( !defined( 'ABSPATH' ) ) |
| 7 |
exit; |
| 8 |
|
| 9 |
if ( !class_exists( 'ewdotpCustomer' ) ) { |
| 10 |
class ewdotpCustomer { |
| 11 |
|
| 12 |
// The database ID of the current customer |
| 13 |
public $id = 0; |
| 14 |
|
| 15 |
// Stores all of the custom field values for an customer |
| 16 |
public $custom_fields = array(); |
| 17 |
|
| 18 |
/** |
| 19 |
* Load an customer based on a specific database record |
| 20 |
* @since 3.0.0 |
| 21 |
*/ |
| 22 |
public function load_customer( $db_customer ) { |
| 23 |
global $ewd_otp_controller; |
| 24 |
|
| 25 |
$this->id = is_object( $db_customer ) ? $db_customer->Customer_ID : 0; |
| 26 |
|
| 27 |
$this->number = is_object( $db_customer ) ? $db_customer->Customer_Number : ''; |
| 28 |
|
| 29 |
$this->name = is_object( $db_customer ) ? $db_customer->Customer_Name : ''; |
| 30 |
$this->email = is_object( $db_customer ) ? $db_customer->Customer_Email : ''; |
| 31 |
|
| 32 |
$this->sales_rep = is_object( $db_customer ) ? $db_customer->Sales_Rep_ID : 0; |
| 33 |
$this->wp_id = is_object( $db_customer ) ? $db_customer->Customer_WP_ID : 0; |
| 34 |
$this->feup_id = is_object( $db_customer ) ? $db_customer->Customer_FEUP_ID : 0; |
| 35 |
|
| 36 |
$custom_fields = $ewd_otp_controller->settings->get_customer_custom_fields(); |
| 37 |
|
| 38 |
foreach ( $custom_fields as $custom_field ) { |
| 39 |
|
| 40 |
$this->custom_fields[ $custom_field->id ] = $ewd_otp_controller->customer_manager->get_field_value( $custom_field->id, $this->id ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
public function load_customer_from_id( $customer_id ) { |
| 45 |
global $ewd_otp_controller; |
| 46 |
|
| 47 |
$db_customer = $ewd_otp_controller->customer_manager->get_customer_from_id( $customer_id ); |
| 48 |
|
| 49 |
if( $db_customer ) { |
| 50 |
|
| 51 |
$this->load_customer( $db_customer ); |
| 52 |
|
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public function load_customer_from_number( $customer_number ) { |
| 57 |
global $ewd_otp_controller; |
| 58 |
|
| 59 |
$customer_id = $ewd_otp_controller->customer_manager->get_customer_id_from_number( $customer_number ); |
| 60 |
|
| 61 |
if ( empty( $customer_id ) ) { return false; } |
| 62 |
|
| 63 |
$db_customer = $ewd_otp_controller->customer_manager->get_customer_from_id( $customer_id ); |
| 64 |
|
| 65 |
if( $db_customer ) { |
| 66 |
|
| 67 |
$this->load_customer( $db_customer ); |
| 68 |
|
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Verify that the submitted email matches the one belonging to the customer |
| 74 |
* @since 3.0.0 |
| 75 |
*/ |
| 76 |
public function verify_customer_email( $email_address ) { |
| 77 |
|
| 78 |
if ( $email_address == $this->email ) { return true; } |
| 79 |
|
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get orders belonging to this customer |
| 85 |
* @since 3.0.0 |
| 86 |
*/ |
| 87 |
public function get_customer_orders( $args ) { |
| 88 |
global $ewd_otp_controller; |
| 89 |
|
| 90 |
$args['customer'] = $this->id; |
| 91 |
|
| 92 |
return $ewd_otp_controller->order_manager->get_matching_orders( $args ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Validates a submitted customer, and calls insert_customer if validated |
| 97 |
* @since 3.0.0 |
| 98 |
*/ |
| 99 |
public function process_admin_customer_submission() { |
| 100 |
global $ewd_otp_controller; |
| 101 |
|
| 102 |
$this->validate_admin_submission(); |
| 103 |
if ( $this->is_valid_submission() === false ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
if ( $this->id ) { $this->update_customer(); } |
| 108 |
else { |
| 109 |
|
| 110 |
$this->insert_customer(); |
| 111 |
|
| 112 |
do_action( 'ewd_otp_admin_insert_customer', $this ); |
| 113 |
} |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Validate submission data entered via the admin page |
| 120 |
* @since 3.0.0 |
| 121 |
*/ |
| 122 |
public function validate_admin_submission() { |
| 123 |
global $ewd_otp_controller; |
| 124 |
|
| 125 |
$this->validation_errors = array(); |
| 126 |
|
| 127 |
if ( ! isset( $_POST['ewd-otp-admin-nonce'] ) |
| 128 |
or ! wp_verify_nonce( $_POST['ewd-otp-admin-nonce'], 'ewd-otp-admin-nonce' ) |
| 129 |
) { |
| 130 |
$this->validation_errors[] = __( 'The request has been rejected because it does not appear to have come from this site.', 'order-tracking' ); |
| 131 |
} |
| 132 |
|
| 133 |
// Customer Data |
| 134 |
$this->number = empty( $_POST['ewd_otp_number'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_number'] ); |
| 135 |
|
| 136 |
$this->name = empty( $_POST['ewd_otp_name'] ) ? '' : sanitize_text_field( $_POST['ewd_otp_name'] ); |
| 137 |
$this->email = empty( $_POST['ewd_otp_email'] ) ? '' : sanitize_email( $_POST['ewd_otp_email'] ); |
| 138 |
|
| 139 |
$this->sales_rep = empty( $_POST['ewd_otp_sales_rep'] ) ? 0 : intval( $_POST['ewd_otp_sales_rep'] ); |
| 140 |
|
| 141 |
$this->wp_id = empty( $_POST['ewd_otp_wp_id'] ) ? 0 : intval( $_POST['ewd_otp_wp_id'] ); |
| 142 |
$this->feup_id = empty( $_POST['ewd_otp_feup_id'] ) ? 0 : intval( $_POST['ewd_otp_feup_id'] ); |
| 143 |
|
| 144 |
$custom_fields = $ewd_otp_controller->settings->get_customer_custom_fields(); |
| 145 |
|
| 146 |
foreach ( $custom_fields as $custom_field ) { |
| 147 |
|
| 148 |
$input_name = 'ewd-otp-custom-field-' . $custom_field->id; |
| 149 |
|
| 150 |
if ( $custom_field->type == 'checkbox' ) { $this->custom_fields[ $custom_field->id ] = ( empty( $_POST[ $input_name ] ) or ! is_array( $_POST[ $input_name ] ) ) ? array() : sanitize_text_field( implode( ',', $_POST[ $input_name ] ) ); } |
| 151 |
elseif ( $custom_field->type == 'textarea' ) { $this->custom_fields[ $custom_field->id ] = empty( $_POST[ $input_name ] ) ? false : sanitize_textarea_field( $_POST[ $input_name ] ); } |
| 152 |
elseif ( $custom_field->type == 'file' or $custom_field->type == 'image' ) { $this->custom_fields[ $custom_field->id ] = ! empty( $_FILES[ $input_name ]['name'] ) ? $this->handle_file_upload( $input_name ) : ( ! empty( $_POST[ $input_name ] ) ? sanitize_text_field( $_POST[ $input_name ] ) : '' ); } |
| 153 |
else { $this->custom_fields[ $custom_field->id ] = empty( $_POST[ $input_name ] ) ? false : sanitize_text_field( $_POST[ $input_name ] ); } |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Takes an input name, uploads the file from that input if it exists, returns the file URL |
| 159 |
* @since 3.0.0 |
| 160 |
*/ |
| 161 |
public function handle_file_upload( $input_name ) { |
| 162 |
|
| 163 |
if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 164 |
|
| 165 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 166 |
} |
| 167 |
|
| 168 |
$args = array( |
| 169 |
'test_form' => false |
| 170 |
); |
| 171 |
|
| 172 |
$uploaded_file = wp_handle_upload( $_FILES[ $input_name ], $args ); |
| 173 |
|
| 174 |
if ( $uploaded_file && empty( $uploaded_file['error'] ) ) { |
| 175 |
|
| 176 |
return $uploaded_file['url']; |
| 177 |
} |
| 178 |
else { |
| 179 |
|
| 180 |
return false; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Check if submission is valid |
| 186 |
* @since 3.0.0 |
| 187 |
*/ |
| 188 |
public function is_valid_submission() { |
| 189 |
|
| 190 |
if ( !count( $this->validation_errors ) ) { |
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Insert a new customer into the database |
| 199 |
* @since 3.0.0 |
| 200 |
*/ |
| 201 |
public function insert_customer() { |
| 202 |
global $ewd_otp_controller; |
| 203 |
|
| 204 |
$id = $ewd_otp_controller->customer_manager->insert_customer( $this ); |
| 205 |
|
| 206 |
$this->id = $id; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Update an customer already in the database |
| 211 |
* @since 3.0.0 |
| 212 |
*/ |
| 213 |
public function update_customer() { |
| 214 |
global $ewd_otp_controller; |
| 215 |
|
| 216 |
$ewd_otp_controller->customer_manager->update_customer( $this ); |
| 217 |
|
| 218 |
} |
| 219 |
} |
| 220 |
} |