Last active
May 15, 2024 20:12
-
-
Save nathaningram/2c73d310f4bb5f697d176ef91154542f to your computer and use it in GitHub Desktop.
AI Plugin Creation Workshop 2024 (Events Plugin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: BWW - Event Status Updater | |
Description: Automatically updates event status from Upcoming to Past based on the event end datetime. Verifies Action Scheduler functionality is available. | |
Version: 2024.03 | |
Plugin URI: https://brilliantly.net | |
Author: Brilliant Web Works, Inc. | |
Author URI: https://brilliantly.net | |
License: GPL2 | |
*/ | |
/////////////////////////////////////////////////////// | |
// Customize the variables | |
/////////////////////////////////////////////////////// | |
$post_type_slug = 'event'; // Enter the cpt slug | |
$end_time_meta_field = 'event_end'; //PHP datetime | |
$status_meta_field = 'event_status'; // Custom field to change | |
$new_status_value = 'Past'; // New value for the custom field | |
/////////////////////////////////////////////////////// | |
add_action('admin_init', 'bww_check_action_scheduler_functionality'); | |
function bww_check_action_scheduler_functionality() { | |
if (!function_exists('as_schedule_single_action')) { | |
add_action('admin_notices', 'bww_action_scheduler_missing_notice'); | |
} | |
} | |
function bww_action_scheduler_missing_notice() { | |
echo '<div class="notice notice-error"><p>Action Scheduler is not working so Events cannot be automatically updated to Past. Add the <a href="/wp-admin/plugin-install.php?s=action%2520scheduler&tab=search&type=term">Action Scheduler</a> plugin to fix this issue.</p></div>'; | |
} | |
add_action('save_post', 'bww_schedule_event_status_update'); | |
function bww_schedule_event_status_update($post_id) { | |
global $post_type_slug, $end_time_meta_field, $status_meta_field, $new_status_value; | |
if (get_post_type($post_id) !== $post_type_slug) { | |
return; | |
} | |
if (wp_is_post_revision($post_id) || (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) { | |
return; | |
} | |
$current_status = get_post_meta($post_id, $status_meta_field, true); | |
if ($current_status === $new_status_value) { | |
return; // Exit if event status is already 'Past' | |
} | |
$event_end = get_post_meta($post_id, $end_time_meta_field, true); | |
if (empty($event_end)) { | |
return; | |
} | |
// Convert the string datetime to a Unix timestamp and adjust for timezone | |
$timezone_string = get_option('timezone_string'); | |
if (empty($timezone_string)) { | |
$offset = get_option('gmt_offset'); | |
$timezone_string = timezone_name_from_abbr('', $offset * 3600, false); | |
} | |
$event_timezone = new DateTimeZone($timezone_string); | |
$event_end_datetime = new DateTime($event_end, $event_timezone); | |
// Convert to UTC timestamp for scheduling | |
$utc_timezone = new DateTimeZone('UTC'); | |
$event_end_datetime->setTimezone($utc_timezone); | |
$adjusted_event_end = $event_end_datetime->getTimestamp(); | |
// Schedule the event status update | |
as_unschedule_all_actions('bww_update_event_status', array($post_id), 'BWW-Events'); | |
as_schedule_single_action($adjusted_event_end, 'bww_update_event_status', array($post_id), 'BWW-Events'); | |
} | |
add_action('bww_update_event_status', 'bww_update_event_status_callback', 10, 1); | |
function bww_update_event_status_callback($post_id) { | |
global $post_type_slug, $status_meta_field, $new_status_value; | |
// Corrected to check against the global post_type_slug | |
if (get_post_type($post_id) !== $post_type_slug) { | |
return; | |
} | |
update_post_meta($post_id, $status_meta_field, $new_status_value); | |
} | |
// Event Form - Pass Event Cost to Product Price | |
// Using GF ID = 9006 | |
add_filter('gform_pre_render', 'bww_pre_render_modify_product_price'); | |
add_filter('gform_pre_validation', 'bww_pre_render_modify_product_price'); | |
function bww_pre_render_modify_product_price($form) { | |
// Check if this is the right form using the form ID | |
if($form['id'] != 9006) return $form; | |
global $post; | |
// Assuming you are on a single post page, otherwise you'll need to adjust how you get $post | |
$event_cost = get_post_meta($post->ID, 'event_cost', true); | |
if (empty($event_cost)) return $form; // If no event_cost, don't change the form | |
foreach ($form['fields'] as &$field) { | |
// Check if this is the product field by ID | |
if ($field->id == 6) { | |
// Change the product's price to the event_cost | |
$field->basePrice = $event_cost; | |
break; // Exit the loop after modifying the price | |
} | |
} | |
return $form; | |
} | |
// Event Form - Increment Tickets Sold | |
// Using GF ID = 9006 | |
// https://chat.openai.com/c/c8378664-1b0d-4b02-b8f8-5debe414e7b7 | |
add_action('gform_after_submission_9006', 'bww_increment_tickets_sold', 10, 2); | |
function bww_increment_tickets_sold($entry, $form) { | |
$event_title = rgar($entry, '5'); // Assuming Field ID 5 holds the event title | |
// Find the event post by title, with 'event' as the post type slug | |
$event_posts = get_posts([ | |
'post_type' => 'event', | |
'title' => $event_title, | |
'posts_per_page' => 1, | |
]); | |
if (!empty($event_posts)) { | |
$event_post = array_shift($event_posts); | |
// Ensure we are updating the correct event | |
if ($event_post->post_title == $event_title) { | |
$tickets_sold = get_post_meta($event_post->ID, 'event_tickets_sold', true); | |
$tickets_sold = $tickets_sold ? $tickets_sold + 1 : 1; // Increment or initialize | |
update_post_meta($event_post->ID, 'event_tickets_sold', $tickets_sold); | |
} | |
} | |
} | |
// Event Form - Check Tickets Sold vs Limit | |
// Using GF ID = 9006 | |
// https://chat.openai.com/c/c8378664-1b0d-4b02-b8f8-5debe414e7b7 | |
add_filter('gform_get_form_filter_9006', 'bww_check_tickets_and_display_form', 10, 2); | |
function bww_check_tickets_and_display_form($form_string, $form) { | |
global $post; | |
// Check if we're in the correct context (an 'event' post) | |
if (get_post_type($post) != 'event') { | |
return $form_string; // If not, return the form as is | |
} | |
$event_id = $post->ID; | |
$tickets_sold = get_post_meta($event_id, 'event_tickets_sold', true); | |
$ticket_limit = get_post_meta($event_id, 'event_limit', true); | |
// Compare tickets sold against the ticket limit | |
if ($tickets_sold < $ticket_limit) { | |
// If under the limit, allow the form to display normally | |
return $form_string; | |
} else { | |
// If limit is reached or exceeded, replace the form with a message | |
return '<p>There are no more tickets for this event.</p>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment