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