Skip to content

Instantly share code, notes, and snippets.

@joeydi
Created September 27, 2022 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeydi/7dae09e6d8d0a9c8f5c2cd67718365d3 to your computer and use it in GitHub Desktop.
Save joeydi/7dae09e6d8d0a9c8f5c2cd67718365d3 to your computer and use it in GitHub Desktop.
WF_HolidayOrdering
<?php
new WF_HolidayOrdering();
class WF_HolidayOrdering
{
const MANAGER_ROLES = ['administrator'];
const HOLIDAY_ORDERING_FORM_IDS = [6, 16];
const HIDDEN_FIELD_LABELS = ['In-Store Order'];
const LOCATION_FIELD_ID = 12;
const IN_STORE_FIELD_ID = '26.1';
function __construct()
{
add_filter('gform_pre_render', [$this, 'filter_gform_pre_render']);
add_filter('gform_field_validation', [$this, 'filter_gform_field_validation'], 10, 4);
add_filter('gform_product_info', [$this, 'filter_gform_product_info'], 10, 3);
add_filter('wp_ajax_gf_process_export', [$this, 'filter_wp_ajax_gf_process_export'], 1);
add_filter('login_redirect', [$this, 'filter_login_redirect'], 10, 3);
add_filter('gform_entries_field_header_pre_export', [$this, 'replace_export_coloumn_headers'], 10, 3);
add_filter('gform_export_fields', [$this, 'filter_gform_export_fields']);
}
function filter_gform_pre_render($form)
{
if (!in_array($form['id'], self::HOLIDAY_ORDERING_FORM_IDS)) {
return $form;
}
// Remove hidden fields for non-managers
if (!self::is_location_manager()) {
foreach ($form['fields'] as $key => &$field) {
if (in_array($field['label'], self::HIDDEN_FIELD_LABELS)) {
unset($form['fields'][$key]);
}
}
}
return $form;
}
function filter_gform_field_validation($result, $value, $form, $field)
{
if (!in_array($form['id'], self::HOLIDAY_ORDERING_FORM_IDS)) {
return $result;
}
if ($field->label == 'Pickup Location' && empty($value)) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a valid pickup location.';
}
if ($field->label == 'Pickup Date' && empty($value)) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a valid pickup date.';
}
if ($field->label == 'Pickup Time' && empty($value)) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a valid pickup time.';
}
if ($field->type == 'total') {
$total_quantity = 0;
// Sum Product Quantities from Submission
foreach ($form['fields'] as $field) {
if ($field->type == 'product') {
foreach ($field['inputs'] as $input) {
if ($input['label'] == 'Quantity') {
$input_id = sprintf('input_%s', str_replace('.', '_', $input['id']));
$quantity = rgpost($input_id);
$total_quantity += intval($quantity);
}
}
}
}
// Verify At Least One Item Has Been Ordered
if ($total_quantity === 0) {
$result['is_valid'] = false;
$result['message'] = 'You must order at lease one item.';
}
}
return $result;
}
function filter_gform_product_info($product_info, $form, $entry)
{
$entry_location = $entry[self::LOCATION_FIELD_ID];
$location_parts = explode('-', $entry_location);
$location_name = trim($location_parts[1]);
$location = get_page_by_title($location_name, OBJECT, 'location');
$tax_rate = get_field('tax_rate', $location);
$total = self::get_product_subtotal($product_info);
$tax = ($tax_rate * $total) / 100;
$product_info['products']['tax'] = [
'name' => 'Sales Tax (' . number_format($tax_rate, 2) . '%)',
'price' => $tax,
'quantity' => 1
];
return $product_info;
}
// Modify entry export field order
// There is no filter for export fields, so we directly manipulate the global $_POST
function filter_wp_ajax_gf_process_export()
{
// Remove the In-Store Order field
$_POST['export_field'] = array_diff($_POST['export_field'], [self::IN_STORE_FIELD_ID]);
// Add it backk in at the end of the array
$_POST['export_field'][] = self::IN_STORE_FIELD_ID;
}
function filter_login_redirect($redirect_to, $requested_redirect_to, $user)
{
if (isset($user->ID)) {
// Check if the user has the location_manager field set
$location_manager = get_field('location_manager', sprintf('user_%s', $user->ID));
// Check if the location_manager_redirect options field is set
$location_manager_redirect = get_field('location_manager_redirect', 'options');
if ($location_manager && $location_manager_redirect) {
return $location_manager_redirect;
}
}
return $redirect_to;
}
function replace_export_coloumn_headers($header, $form, $field)
{
$labels = array(
'Hot Cross Buns (Name)' => 'Test Column Header Name',
'Hot Cross Buns (Price)' => 'Test Column Header Price',
'Hot Cross Buns (Quantity)' => 'Test Column Header Qty',
'Pumpkin Pie (Name)' => 'Product 1 Name',
'Pumpkin Pie (Price)' => 'Product 1 Price',
'Pumpkin Pie (Quantity)' => 'Product 1 Qty',
'Crumb Apple Pie (Name)' => 'Product 2 Name',
'Crumb Apple Pie (Price)' => 'Product 2 Price',
'Crumb Apple Pie (Quantity)' => 'Product 2 Qty',
'Roasted Nut Pie (Name)' => 'Product 3 Name',
'Roasted Nut Pie (Price)' => 'Product 3 Price',
'Roasted Nut Pie (Quantity)' => 'Product 3 Qty',
'Cranberry Walnut Batard (Name)' => 'Product 4 Name',
'Cranberry Walnut Batard (Price)' => 'Product 4 Price',
'Cranberry Walnut Batard (Quantity)' => 'Product 4 Qty',
'Cranberry Walnut Batard (Sliced) (Consent)' => 'Product 4 Sliced',
'Pumpkin Cinnamon Bread (Name)' => 'Product 5 Name',
'Pumpkin Cinnamon Bread (Price)' => 'Product 5 Price',
'Pumpkin Cinnamon Bread (Quantity)' => 'Product 5 Qty',
'Pumpkin Cinnamon Bread (Sliced) (Consent)' => 'Product 5 Sliced',
'Stuffing Sandwich Loaf (Name)' => 'Product 6 Name',
'Stuffing Sandwich Loaf (Price)' => 'Product 6 Price',
'Stuffing Sandwich Loaf (Quantity)' => 'Product 6 Qty',
'Stuffing Sandwich Loaf (Sliced) (Consent)' => 'Product 6 Sliced',
'Potato Dill Pull Apart Rolls (Name)' => 'Product 7 Name',
'Potato Dill Pull Apart Rolls (Price)' => 'Product 7 Price',
'Potato Dill Pull Apart Rolls (Quantity)' => 'Product 7 Qty',
'Sourdough Pull Apart Rolls (Name)' => 'Product 8 Name',
'Sourdough Pull Apart Rolls (Price)' => 'Product 8 Price',
'Sourdough Pull Apart Rolls (Quantity)' => 'Product 8 Qty',
'Nine-Grain Pull Apart Rolls (Name)' => 'Product 9 Name',
'Nine-Grain Pull Apart Rolls (Price)' => 'Product 9 Price',
'Nine-Grain Pull Apart Rolls (Quantity)' => 'Product 9 Qty',
'Sourdough Sandwich Loaf (Name)' => 'Product 10 Name',
'Sourdough Sandwich Loaf (Price)' => 'Product 10 Price',
'Sourdough Sandwich Loaf (Quantity)' => 'Product 10 Qty',
'Sourdough Sandwich Loaf (Sliced) (Consent)' => 'Product 10 Sliced',
'Nine-Grain Sandwich Loaf (Name)' => 'Product 11 Name',
'Nine-Grain Sandwich Loaf (Price)' => 'Product 11 Price',
'Nine-Grain Sandwich Loaf (Quantity)' => 'Product 11 Qty',
'Nine-Grain Sandwich Loaf (Sliced) (Consent)' => 'Product 11 Sliced',
);
return isset($labels[$header]) ? $labels[$header] : $header;
}
function filter_gform_export_fields($form)
{
$types = array('consent');
$other = array(
'transaction_id',
'user_agent',
'ip',
'post_id',
'transaction_id',
'source_url',
'date_created'
);
foreach ($form['fields'] as $key => $field) {
$field_id = is_object($field) ? $field->id : $field['id'];
if (in_array($field_id, $other)) {
unset($form['fields'][$key]);
}
if (is_object($field) && in_array($field->get_input_type(), $types)) {
foreach ($field->inputs as $i => $input) {
if (rgar($input, 'isHidden')) {
unset($field->inputs[$i]);
}
}
}
}
return $form;
}
static function get_product_subtotal($products)
{
$total = 0;
foreach ($products["products"] as $product) {
$price = GFCommon::to_number($product["price"]);
if (isset($product["options"]) && is_array($product["options"])) {
foreach ($product["options"] as $option) {
$price += GFCommon::to_number($option["price"]);
}
}
$subtotal = floatval($product["quantity"]) * $price;
$total += $subtotal;
}
$total += floatval($products["shipping"]["price"]);
return $total;
}
static function validate_pickup_dates($dates, $lead_time = 3)
{
if (empty($dates)) {
return [];
}
$dates = wp_list_pluck($dates, 'date');
if (!self::is_location_manager()) {
$now = new DateTime("now");
$now->setTime(0, 0, 0);
$dates = array_filter($dates, function ($date) use ($now, $lead_time) {
$date_object = new DateTime($date);
$date_object->setTime(0, 0, 0);
$date_diff = $now->diff($date_object);
return $date_diff->days >= $lead_time;
});
}
return $dates;
}
static function location_blackout_dates($blackout_dates)
{
if (empty($blackout_dates)) {
return '';
}
$blackout_dates = wp_list_pluck($blackout_dates, 'date');
return implode('|', $blackout_dates);
}
static function location_nicename($location)
{
$address = get_field('address', $location);
$city = $address['city'] ?: null;
$title = get_the_title($location);
$parts = array_filter([$city, $title]);
return implode(' - ', $parts);
}
static function is_location_manager()
{
// Check if we have a logged in user
$current_user = wp_get_current_user();
if (!$current_user) {
return false;
}
// Check if the user has the location_manager field set
$location_manager = get_field('location_manager', sprintf('user_%s', $current_user->ID));
if ($location_manager) {
return self::location_nicename($location_manager);
}
// Check if the user has one of the MANAGER_ROLES
if (array_intersect($current_user->roles, self::MANAGER_ROLES)) {
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment