| 1 |
<?php |
| 2 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
if ( !class_exists( 'ewdotpUltimateWPMail' ) ) { |
| 5 |
/** |
| 6 |
* Class to handle Ultimate WP Mail integration for Order Tracking |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
class ewdotpUltimateWPMail { |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
|
| 14 |
add_filter( 'uwpm_register_custom_element_section', array( $this, 'add_element_section' ) ); |
| 15 |
add_action( 'uwpm_register_custom_element', array( $this, 'add_elements' ) ); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Adds in a section for OTP tags in Ultimate WP Mail |
| 20 |
* @since 3.0.0 |
| 21 |
*/ |
| 22 |
public function add_element_section() { |
| 23 |
|
| 24 |
if ( ! function_exists( 'uwpm_register_custom_element_section' ) ) { return; } |
| 25 |
|
| 26 |
$args = array( |
| 27 |
'label' => 'Order Tracking Tags' |
| 28 |
); |
| 29 |
|
| 30 |
uwpm_register_custom_element_section( 'ewd_otp_uwpm_elements', $args ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Adds in tags for order information and a tracking link |
| 35 |
* @since 3.0.0 |
| 36 |
*/ |
| 37 |
public function add_elements() { |
| 38 |
global $ewd_otp_controller; |
| 39 |
|
| 40 |
if ( ! function_exists( 'uwpm_register_custom_element' ) ) { return; } |
| 41 |
|
| 42 |
$args = array( |
| 43 |
'label' => 'Order Name', |
| 44 |
'callback_function' => 'ewd_otp_get_order_name', |
| 45 |
'section' => 'ewd_otp_uwpm_elements' |
| 46 |
); |
| 47 |
|
| 48 |
uwpm_register_custom_element( 'ewd_otp_order_name', $args ); |
| 49 |
|
| 50 |
$args = array( |
| 51 |
'label' => 'Order Number', |
| 52 |
'callback_function' => 'ewd_otp_get_order_number', |
| 53 |
'section' => 'ewd_otp_uwpm_elements' |
| 54 |
); |
| 55 |
|
| 56 |
uwpm_register_custom_element( 'ewd_otp_order_number', $args ); |
| 57 |
|
| 58 |
$args = array( |
| 59 |
'label' => 'Order Status', |
| 60 |
'callback_function' => 'ewd_otp_get_order_status', |
| 61 |
'section' => 'ewd_otp_uwpm_elements' |
| 62 |
); |
| 63 |
|
| 64 |
uwpm_register_custom_element( 'ewd_otp_order_status', $args ); |
| 65 |
|
| 66 |
$args = array( |
| 67 |
'label' => 'Order Notes', |
| 68 |
'callback_function' => 'ewd_otp_get_order_notes', |
| 69 |
'section' => 'ewd_otp_uwpm_elements' |
| 70 |
); |
| 71 |
|
| 72 |
uwpm_register_custom_element( 'ewd_otp_order_notes', $args ); |
| 73 |
|
| 74 |
$args = array( |
| 75 |
'label' => 'Order Customer Notes', |
| 76 |
'callback_function' => 'ewd_otp_get_order_customer_notes', |
| 77 |
'section' => 'ewd_otp_uwpm_elements' |
| 78 |
); |
| 79 |
|
| 80 |
uwpm_register_custom_element( 'ewd_otp_order_customer_notes', $args ); |
| 81 |
|
| 82 |
$args = array( |
| 83 |
'label' => 'Order Updated Time', |
| 84 |
'callback_function' => 'ewd_otp_get_order_updated_time', |
| 85 |
'section' => 'ewd_otp_uwpm_elements' |
| 86 |
); |
| 87 |
|
| 88 |
uwpm_register_custom_element( 'ewd_otp_order_updated_time', $args ); |
| 89 |
|
| 90 |
$args = array( |
| 91 |
'label' => 'Tracking Link', |
| 92 |
'callback_function' => 'ewd_otp_get_order_tracking_link', |
| 93 |
'section' => 'ewd_otp_uwpm_elements', |
| 94 |
'attributes' => array( |
| 95 |
array( |
| 96 |
'attribute_name' => 'ewd_otp_tracking_page_url', |
| 97 |
'attribute_label' => 'Tracking Page URL', |
| 98 |
'attribute_type' => 'TextBox' |
| 99 |
), |
| 100 |
array( |
| 101 |
'attribute_name' => 'ewd_otp_tracking_link_text_label', |
| 102 |
'attribute_label' => '"Track Your Order!" label', |
| 103 |
'attribute_type' => 'TextBox' |
| 104 |
) |
| 105 |
) |
| 106 |
); |
| 107 |
|
| 108 |
uwpm_register_custom_element( 'ewd_otp_order_tracking_link', $args ); |
| 109 |
|
| 110 |
$args = array( |
| 111 |
'label' => 'Customer Name', |
| 112 |
'callback_function' => 'ewd_otp_get_order_customer_name', |
| 113 |
'section' => 'ewd_otp_uwpm_elements' |
| 114 |
); |
| 115 |
|
| 116 |
uwpm_register_custom_element( 'ewd_otp_order_customer_name', $args ); |
| 117 |
|
| 118 |
$args = array( |
| 119 |
'label' => 'Customer ID', |
| 120 |
'callback_function' => 'ewd_otp_get_order_customer_id', |
| 121 |
'section' => 'ewd_otp_uwpm_elements' |
| 122 |
); |
| 123 |
|
| 124 |
uwpm_register_custom_element( 'ewd_otp_order_customer_id', $args ); |
| 125 |
|
| 126 |
$args = array( |
| 127 |
'label' => 'Sales Rep Name', |
| 128 |
'callback_function' => 'ewd_otp_get_order_sales_rep_name', |
| 129 |
'section' => 'ewd_otp_uwpm_elements' |
| 130 |
); |
| 131 |
|
| 132 |
uwpm_register_custom_element( 'ewd_otp_order_sales_rep_name', $args ); |
| 133 |
|
| 134 |
foreach( $ewd_otp_controller->settings->get_order_custom_fields() as $custom_field ) { |
| 135 |
|
| 136 |
$args = array( |
| 137 |
'label' => $custom_field->name, |
| 138 |
'callback_function' => 'ewd_otp_get_custom_field', |
| 139 |
'section' => 'ewd_otp_uwpm_elements' |
| 140 |
); |
| 141 |
|
| 142 |
uwpm_register_custom_element( 'ewd_otp_' . $custom_field->slug, $args ); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns the name of the specified order |
| 150 |
* @since 3.0.0 |
| 151 |
*/ |
| 152 |
function ewd_otp_get_order_name( $params, $user ) { |
| 153 |
global $ewd_otp_controller; |
| 154 |
|
| 155 |
return ! empty( $params['order_id'] ) ? $ewd_otp_controller->order_manager->get_order_field( 'name', $params['order_id'] ) : ''; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns the number of the specified order |
| 160 |
* @since 3.0.0 |
| 161 |
*/ |
| 162 |
function ewd_otp_get_order_number( $params, $user ) { |
| 163 |
global $ewd_otp_controller; |
| 164 |
|
| 165 |
return ! empty( $params['order_id'] ) ? $ewd_otp_controller->order_manager->get_order_field( 'number', $params['order_id'] ) : ''; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Returns the external status of the specified order |
| 170 |
* @since 3.0.0 |
| 171 |
*/ |
| 172 |
function ewd_otp_get_order_status( $params, $user ) { |
| 173 |
global $ewd_otp_controller; |
| 174 |
|
| 175 |
return ! empty( $params['order_id'] ) ? $ewd_otp_controller->order_manager->get_order_field( 'external_status', $params['order_id'] ) : ''; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns the public notes for the specified order |
| 180 |
* @since 3.0.0 |
| 181 |
*/ |
| 182 |
function ewd_otp_get_order_notes( $params, $user ) { |
| 183 |
global $ewd_otp_controller; |
| 184 |
|
| 185 |
return ! empty( $params['order_id'] ) ? $ewd_otp_controller->order_manager->get_order_field( 'notes_public', $params['order_id'] ) : ''; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Returns the customer notes for the specified order |
| 190 |
* @since 3.0.0 |
| 191 |
*/ |
| 192 |
function ewd_otp_get_order_customer_notes( $params, $user ) { |
| 193 |
global $ewd_otp_controller; |
| 194 |
|
| 195 |
return ! empty( $params['order_id'] ) ? $ewd_otp_controller->order_manager->get_order_field( 'customer_notes', $params['order_id'] ) : ''; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Returns the time the specified order was last updated |
| 200 |
* @since 3.0.0 |
| 201 |
*/ |
| 202 |
function ewd_otp_get_order_updated_time( $params, $user ) { |
| 203 |
global $ewd_otp_controller; |
| 204 |
|
| 205 |
return ! empty( $params['order_id'] ) ? $ewd_otp_controller->order_manager->get_order_field( 'status_updated', $params['order_id'] ) : ''; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Returns the customer name for the specified order |
| 210 |
* @since 3.0.0 |
| 211 |
*/ |
| 212 |
function ewd_otp_get_order_customer_name( $params, $user ) { |
| 213 |
global $ewd_otp_controller; |
| 214 |
|
| 215 |
$customer_id = ! empty( $params['order_id'] ) ? $ewd_otp_controller->order_manager->get_order_field( 'customer', $params['order_id'] ) : ''; |
| 216 |
|
| 217 |
return ! empty( $customer_id ) ? $ewd_otp_controller->customer_manager->get_customer_field( 'name', $customer_id ) : ''; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Returns the customer ID for the specified order |
| 222 |
* @since 3.0.0 |
| 223 |
*/ |
| 224 |
function ewd_otp_get_order_customer_id( $params, $user ) { |
| 225 |
global $ewd_otp_controller; |
| 226 |
|
| 227 |
return ! empty( $params['order_id'] ) ? $ewd_otp_controller->order_manager->get_order_field( 'customer', $params['order_id'] ) : ''; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Returns the sales rep ID for the specified order |
| 232 |
* @since 3.0.0 |
| 233 |
*/ |
| 234 |
function ewd_otp_get_order_sales_rep_name( $params, $user ) { |
| 235 |
global $ewd_otp_controller; |
| 236 |
|
| 237 |
$sales_rep_id = ! empty( $params['order_id'] ) ? $ewd_otp_controller->order_manager->get_order_field( 'sales_rep', $params['order_id'] ) : ''; |
| 238 |
|
| 239 |
return ! empty( $sales_rep_id ) ? ( $ewd_otp_controller->sales_rep_manager->get_sales_rep_field( 'first_name', $sales_rep_id ) . ' ' . $ewd_otp_controller->sales_rep_manager->get_sales_rep_field( 'last_name', $sales_rep_id ) ) : ''; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Returns a tracking link for the specified order |
| 244 |
* @since 3.0.0 |
| 245 |
*/ |
| 246 |
function ewd_otp_get_order_tracking_link( $params, $user ) { |
| 247 |
global $ewd_otp_controller; |
| 248 |
|
| 249 |
if ( empty( $params['order_id'] ) ) { return ''; } |
| 250 |
|
| 251 |
$attributes = (array) $params['attributes']; |
| 252 |
|
| 253 |
foreach ( $attributes as $attribute_name => $attribute_value ) { |
| 254 |
|
| 255 |
if ( $attribute_name == 'ewd_otp_tracking_link_text_label' ) { $link_text = $attribute_value; } |
| 256 |
|
| 257 |
if ( $attribute_name == 'ewd_otp_tracking_page_url' ) { |
| 258 |
|
| 259 |
$order = new ewdotpOrder(); |
| 260 |
$order->load_order_from_id( $params['order_id'] ); |
| 261 |
|
| 262 |
$args = array( |
| 263 |
'tracking_number' => $order->number, |
| 264 |
'order_email' => $order->email, |
| 265 |
'tl_code' => ewd_random_string() |
| 266 |
); |
| 267 |
|
| 268 |
$tracking_url = add_query_arg( $args, $attribute_value ); |
| 269 |
|
| 270 |
$order->tracking_link_code = $args['tl_code']; |
| 271 |
|
| 272 |
$order->update_order(); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
$link_text = ! empty( $link_text ) ? $link_text : __( 'Track your order!', 'order-tracking' ); |
| 277 |
$tracking_url = ! empty( $tracking_url ) ? $tracking_url : $ewd_otp_controller->settings->get_setting( 'tracking-page-url' ); |
| 278 |
|
| 279 |
return '<a href="' . $tracking_url . '">' . $link_text . '</a>'; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Returns the value for the specified custom field for the specified order |
| 284 |
* @since 3.0.0 |
| 285 |
*/ |
| 286 |
function ewd_otp_get_custom_field( $params, $user ) { |
| 287 |
global $ewd_otp_controller; |
| 288 |
|
| 289 |
if ( empty( $params['order_id'] ) or empty( $params['slug'] ) ) { return; } |
| 290 |
|
| 291 |
$custom_fields = $ewd_otp_controller->settings->get_order_custom_fields(); |
| 292 |
|
| 293 |
$field_id = 0; |
| 294 |
|
| 295 |
foreach ( $custom_fields as $custom_field ) { |
| 296 |
|
| 297 |
if ( 'ewd_otp_' . $custom_field->slug == $params['slug'] ) { |
| 298 |
|
| 299 |
return $ewd_otp_controller->order_manager->get_field_value( $custom_field->id, $params['order_id'] ); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return false; |
| 304 |
} |