post_type !== self::POST_TYPE) { return; }
if (!defined('AIEO_FILE')) { return; }
wp_enqueue_media();
wp_enqueue_script(
'aieo-events-admin',
plugins_url('assets/js/aieo-events-admin.js', AIEO_FILE),
array('jquery'),
defined('AIEO_VERSION') ? AIEO_VERSION : '1.0',
true
);
wp_localize_script('aieo-events-admin', 'aieoEvents', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('aieo_events'),
'i18n' => array(
'select' => __('Select image', 'ai-eshop-optimizer'),
'use' => __('Use this image', 'ai-eshop-optimizer'),
'copied' => __('Copied!', 'ai-eshop-optimizer'),
'saving' => __('Saving…', 'ai-eshop-optimizer'),
),
));
self::enqueue_picker_bundle();
if ('post' === $screen->base && function_exists('rooster_admin_picker_assets')) {
rooster_admin_picker_assets();
}
}
public static function enqueue_picker_bundle() {
if (!defined('AIEO_URI') || !defined('AIEO_DIR')) { return; }
foreach (array('select2', 'selectWoo') as $h) { if (wp_script_is($h, 'registered')) { wp_enqueue_script($h); break; } }
foreach (array('select2', 'selectWoo') as $h) { if (wp_style_is($h, 'registered')) { wp_enqueue_style($h); break; } }
wp_enqueue_script('jquery-ui-sortable');
$dmm_js = AIEO_DIR . 'assets/js/dmm.js';
wp_enqueue_script('aieo-dmm', AIEO_URI . 'assets/js/dmm.js', array('jquery', 'jquery-ui-sortable'), file_exists($dmm_js) ? (string) filemtime($dmm_js) : (defined('AIEO_VERSION') ? AIEO_VERSION : '1.0'), true);
$dmm_css = AIEO_DIR . 'assets/css/dmm.css';
if (file_exists($dmm_css)) { wp_enqueue_style('aieo-dmm', AIEO_URI . 'assets/css/dmm.css', array(), (string) filemtime($dmm_css)); }
wp_localize_script('aieo-dmm', 'aieoDmm', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('aieo_dmm_nonce'),
'activeTab' => '',
'i18n' => array(
'pickBrands' => __('Pick brand(s)…', 'ai-eshop-optimizer'),
'pickCategories' => __('Pick categories…', 'ai-eshop-optimizer'),
'pickNeeds' => __('Pick needs…', 'ai-eshop-optimizer'),
'searchByNameSku' => __('Search by name or SKU…', 'ai-eshop-optimizer'),
),
));
}
private static function term_select($name, $css_class, $selected_ids, $describedby = '') {
$out = '';
foreach ((array) $selected_ids as $tid) {
$tid = (int) $tid;
if ($tid <= 0) { continue; }
$term = get_term($tid);
if (!$term || is_wp_error($term)) { continue; }
$out .= '' . esc_html($term->name) . ' ';
}
$out .= ' ';
return $out;
}
public static function register_cpt() {
register_post_type(self::POST_TYPE, array(
'labels' => array(
'name' => __('Events', 'ai-eshop-optimizer'),
'singular_name' => __('Event', 'ai-eshop-optimizer'),
'add_new_item' => __('Add Event', 'ai-eshop-optimizer'),
'edit_item' => __('Edit Event', 'ai-eshop-optimizer'),
'menu_name' => __('Events', 'ai-eshop-optimizer'),
),
'public' => true,
'show_in_rest' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-calendar-alt',
'show_in_menu' => false,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'page-attributes'),
'rewrite' => array('slug' => 'events', 'with_front' => false),
));
register_post_meta(self::POST_TYPE, self::META_GF, array(
'type' => 'integer', 'single' => true, 'show_in_rest' => true,
'auth_callback' => static function () { return current_user_can('edit_posts'); },
));
foreach (array(self::META_SHORTDESC, self::META_LOCATION, self::META_INVITE_SUBJECT, self::META_CURRENCY) as $key) {
register_post_meta(self::POST_TYPE, $key, array(
'type' => 'string', 'single' => true, 'show_in_rest' => true,
'auth_callback' => static function () { return current_user_can('edit_posts'); },
));
}
foreach (array(self::META_IMG_DESK, self::META_IMG_MOB) as $key) {
register_post_meta(self::POST_TYPE, $key, array(
'type' => 'integer', 'single' => true, 'show_in_rest' => true,
'auth_callback' => static function () { return current_user_can('edit_posts'); },
));
}
foreach (array(self::META_START, self::META_END, self::META_RSVP_DEADLINE) as $key) {
register_post_meta(self::POST_TYPE, $key, array(
'type' => 'string', 'single' => true, 'show_in_rest' => true,
'auth_callback' => static function () { return current_user_can('edit_posts'); },
));
}
register_post_meta(self::POST_TYPE, self::META_INVITE_BODY, array(
'type' => 'string', 'single' => true, 'show_in_rest' => true,
'sanitize_callback' => static function ($v) { return wp_kses_post((string) $v); },
'auth_callback' => static function () { return current_user_can('edit_posts'); },
));
foreach (array(self::META_BRANDS, self::META_CATEGORIES, self::META_PRODUCTS) as $key) {
register_post_meta(self::POST_TYPE, $key, array(
'type' => 'array', 'single' => true,
'show_in_rest' => array('schema' => array('type' => 'array', 'items' => array('type' => 'integer'))),
'auth_callback' => static function () { return current_user_can('edit_posts'); },
));
}
register_post_meta(self::POST_TYPE, self::META_FEE, array(
'type' => 'number', 'single' => true, 'show_in_rest' => true,
'auth_callback' => static function () { return current_user_can('edit_posts'); },
));
register_post_meta(self::POST_TYPE, self::META_MAP_URL, array(
'type' => 'string', 'single' => true, 'show_in_rest' => true,
'sanitize_callback' => static function ($v) { return esc_url_raw((string) $v); },
'auth_callback' => static function () { return current_user_can('edit_posts'); },
));
if (!get_option('aieo_event_cpt_flushed')) {
update_option('aieo_event_cpt_flushed', 1, false);
flush_rewrite_rules(false);
}
}
public static function add_metabox() {
add_meta_box(
'aieo-event-details',
__('Event details', 'ai-eshop-optimizer'),
array(__CLASS__, 'render_metabox'),
self::POST_TYPE,
'normal',
'high'
);
add_meta_box(
'aieo-event-invitation',
__('Invitation', 'ai-eshop-optimizer'),
array(__CLASS__, 'render_invitation_metabox'),
self::POST_TYPE,
'normal',
'high'
);
}
public static function render_invitation_metabox($post) {
$subject = (string) get_post_meta($post->ID, self::META_INVITE_SUBJECT, true);
$body_html = (string) get_post_meta($post->ID, self::META_INVITE_BODY, true);
echo '
' . esc_html__('The subject line and body used when you send invitations for this event. Personalise the body with the participant’s name, the schedule, location, and an RSVP link (the send pipeline comes later).', 'ai-eshop-optimizer') . '
';
echo '' . esc_html__('Invitation subject', 'ai-eshop-optimizer') . ' ';
echo '
';
echo '' . esc_html__('Invitation body', 'ai-eshop-optimizer') . '
';
wp_editor($body_html, 'aieoeventinvitebody', array(
'textarea_name' => 'aieo_event_invite_body',
'textarea_rows' => 8,
'media_buttons' => true,
'teeny' => false,
'tinymce' => true,
'quicktags' => true,
));
}
public static function render_metabox($post) {
wp_nonce_field('aieo_event_meta', 'aieo_event_meta_nonce');
$gf_id = (int) get_post_meta($post->ID, self::META_GF, true);
$desc = (string) get_post_meta($post->ID, self::META_SHORTDESC, true);
$loc = (string) get_post_meta($post->ID, self::META_LOCATION, true);
$img_d = (int) get_post_meta($post->ID, self::META_IMG_DESK, true);
$img_m = (int) get_post_meta($post->ID, self::META_IMG_MOB, true);
$start = (string) get_post_meta($post->ID, self::META_START, true);
$end = (string) get_post_meta($post->ID, self::META_END, true);
$rsvp = (string) get_post_meta($post->ID, self::META_RSVP_DEADLINE, true);
echo '' . esc_html__('Location', 'ai-eshop-optimizer') . ' ';
echo '
';
$map = (string) get_post_meta($post->ID, self::META_MAP_URL, true);
echo '' . esc_html__('Map link (Google Maps URL)', 'ai-eshop-optimizer') . ' ';
echo '
';
echo '' . esc_html__('Optional. Paste a Google Maps link so guests can tap for directions. If left empty, a Maps search link is built from the location above.', 'ai-eshop-optimizer') . '
';
echo '' . esc_html__('Short description', 'ai-eshop-optimizer') . ' ';
echo '
';
echo '';
self::render_image_field('desktop', 'aieo_event_img_desktop', __('Desktop image', 'ai-eshop-optimizer'), $img_d);
self::render_image_field('mobile', 'aieo_event_img_mobile', __('Mobile image', 'ai-eshop-optimizer'), $img_m);
echo '
';
echo '';
echo '' . esc_html__('Starts', 'ai-eshop-optimizer') . ' ';
echo '' . esc_html__('Ends', 'ai-eshop-optimizer') . ' ';
echo '' . esc_html__('RSVP deadline', 'ai-eshop-optimizer') . ' ';
echo '
';
echo '' . esc_html__('Optional. Before the start, visitors see a “coming soon” notice; after the end, an “ended” message. Leave start/end empty for an always-open event. The RSVP deadline drives the “please respond by …” message and closes self-registration.', 'ai-eshop-optimizer') . '
';
$brands = (array) get_post_meta($post->ID, self::META_BRANDS, true);
$cats = (array) get_post_meta($post->ID, self::META_CATEGORIES, true);
$prods = array_map('intval', (array) get_post_meta($post->ID, self::META_PRODUCTS, true));
echo ' ';
echo '' . esc_html__('Associated brands / categories / products', 'ai-eshop-optimizer') . ' ' . esc_html__('Optional. Used to monitor which products this event drives (its “pull”). The same pickers as the discount pages.', 'ai-eshop-optimizer') . '
';
echo '' . esc_html__('Brands', 'ai-eshop-optimizer') . ' ';
echo self::term_select('aieo_event_brands', 'aieo-dmm-brand-select', $brands, 'aieo-event-assoc-help');
echo '
';
echo '' . esc_html__('Categories', 'ai-eshop-optimizer') . ' ';
echo self::term_select('aieo_event_categories', 'aieo-dmm-category-select', $cats, 'aieo-event-assoc-help');
echo '
';
if (class_exists('AIEO_DMM_Product_Picker')) {
echo AIEO_DMM_Product_Picker::render_field(array(
'name' => 'aieo_event_products',
'label' => __('Products', 'ai-eshop-optimizer'),
'island' => true,
'mode' => 'include',
'selected' => $prods,
'help' => __('Specific products associated with this event.', 'ai-eshop-optimizer'),
'empty_text' => __('No products associated.', 'ai-eshop-optimizer'),
));
}
$fee = (string) get_post_meta($post->ID, self::META_FEE, true);
$cur = (string) get_post_meta($post->ID, self::META_CURRENCY, true);
echo ' ';
echo '' . esc_html__('Self-registration form', 'ai-eshop-optimizer') . ' ';
echo self::gf_form_select('aieo_event_gf', $gf_id, 'aieo-event-gf-help');
echo '' . esc_html__('Optional. A Gravity Form people can use to self-register for this event (the front-end self-registration flow comes later).', 'ai-eshop-optimizer') . '
';
echo '';
echo '' . esc_html__('Admission fee', 'ai-eshop-optimizer') . ' ';
echo '' . esc_html__('Currency', 'ai-eshop-optimizer') . ' ';
echo '
';
echo '' . esc_html__('Admission fee + currency are reserved for paid events (not charged yet).', 'ai-eshop-optimizer') . '
';
}
private static function render_image_field($which, $name, $label, $attachment_id) {
$url = $attachment_id ? wp_get_attachment_image_url($attachment_id, 'medium') : '';
echo '';
echo '
' . esc_html($label) . ' ';
echo '
';
echo '
';
echo '
' . esc_html__('Select image', 'ai-eshop-optimizer') . ' ';
echo '
' . esc_html__('Remove', 'ai-eshop-optimizer') . ' ';
echo '
';
}
public static function save_metabox($post_id, $post) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; }
if (!isset($_POST['aieo_event_meta_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['aieo_event_meta_nonce'])), 'aieo_event_meta')) { return; }
if (!current_user_can('edit_post', $post_id)) { return; }
$gf = isset($_POST['aieo_event_gf']) ? (int) $_POST['aieo_event_gf'] : 0;
$desc = isset($_POST['aieo_event_desc']) ? sanitize_textarea_field(wp_unslash($_POST['aieo_event_desc'])) : '';
$loc = isset($_POST['aieo_event_location']) ? sanitize_text_field(wp_unslash($_POST['aieo_event_location'])) : '';
$img_d = isset($_POST['aieo_event_img_desktop']) ? (int) $_POST['aieo_event_img_desktop'] : 0;
$img_m = isset($_POST['aieo_event_img_mobile']) ? (int) $_POST['aieo_event_img_mobile'] : 0;
$start = isset($_POST['aieo_event_start']) ? sanitize_text_field(wp_unslash($_POST['aieo_event_start'])) : '';
$end = isset($_POST['aieo_event_end']) ? sanitize_text_field(wp_unslash($_POST['aieo_event_end'])) : '';
$rsvp = isset($_POST['aieo_event_rsvp_deadline']) ? sanitize_text_field(wp_unslash($_POST['aieo_event_rsvp_deadline'])) : '';
$brands = isset($_POST['aieo_event_brands']) ? array_map('intval', (array) wp_unslash($_POST['aieo_event_brands'])) : array();
$categories = isset($_POST['aieo_event_categories']) ? array_map('intval', (array) wp_unslash($_POST['aieo_event_categories'])) : array();
$products = class_exists('AIEO_DMM_Product_Picker')
? AIEO_DMM_Product_Picker::roll_up_to_parents(AIEO_DMM_Product_Picker::collect_ids('aieo_event_products'))
: (isset($_POST['aieo_event_products']) ? array_map('intval', (array) wp_unslash($_POST['aieo_event_products'])) : array());
$fee = isset($_POST['aieo_event_fee']) ? (float) $_POST['aieo_event_fee'] : 0;
$cur = isset($_POST['aieo_event_currency']) ? sanitize_text_field(wp_unslash($_POST['aieo_event_currency'])) : '';
self::persist_fields($post_id, $gf, $desc, $loc, $img_d, $img_m, $start, $end, $rsvp, $brands, $categories, $products, $fee, $cur);
if (isset($_POST['aieo_event_invite_subject'])) {
update_post_meta($post_id, self::META_INVITE_SUBJECT, sanitize_text_field(wp_unslash($_POST['aieo_event_invite_subject'])));
}
if (isset($_POST['aieo_event_invite_body'])) {
update_post_meta($post_id, self::META_INVITE_BODY, wp_kses_post(wp_unslash($_POST['aieo_event_invite_body'])));
}
if (isset($_POST['aieo_event_map_url'])) {
update_post_meta($post_id, self::META_MAP_URL, esc_url_raw(wp_unslash($_POST['aieo_event_map_url'])));
}
}
public static function map_url($post_id) {
$explicit = trim((string) get_post_meta($post_id, self::META_MAP_URL, true));
if ($explicit !== '') { return $explicit; }
$loc = trim((string) get_post_meta($post_id, self::META_LOCATION, true));
if ($loc === '') { return ''; }
return 'https://www.google.com/maps/search/?api=1&query=' . rawurlencode($loc);
}
public static function persist_fields($post_id, $gf_id, $short_desc, $location, $img_desktop, $img_mobile, $start = '', $end = '', $rsvp_deadline = null, $brands = array(), $categories = array(), $products = array(), $fee = 0, $currency = '') {
$gf_id = (int) $gf_id;
if ($gf_id > 0) { update_post_meta($post_id, self::META_GF, $gf_id); } else { delete_post_meta($post_id, self::META_GF); }
update_post_meta($post_id, self::META_SHORTDESC, (string) $short_desc);
update_post_meta($post_id, self::META_LOCATION, (string) $location);
$img_desktop = (int) $img_desktop;
$img_mobile = (int) $img_mobile;
if ($img_desktop > 0) { update_post_meta($post_id, self::META_IMG_DESK, $img_desktop); } else { delete_post_meta($post_id, self::META_IMG_DESK); }
if ($img_mobile > 0) { update_post_meta($post_id, self::META_IMG_MOB, $img_mobile); } else { delete_post_meta($post_id, self::META_IMG_MOB); }
if ($img_desktop > 0) { set_post_thumbnail($post_id, $img_desktop); }
$start = self::normalize_dt($start);
$end = self::normalize_dt($end);
if ($start !== '') { update_post_meta($post_id, self::META_START, $start); } else { delete_post_meta($post_id, self::META_START); }
if ($end !== '') { update_post_meta($post_id, self::META_END, $end); } else { delete_post_meta($post_id, self::META_END); }
if ($rsvp_deadline !== null) {
$rsvp_deadline = self::normalize_dt($rsvp_deadline);
if ($rsvp_deadline !== '') { update_post_meta($post_id, self::META_RSVP_DEADLINE, $rsvp_deadline); } else { delete_post_meta($post_id, self::META_RSVP_DEADLINE); }
}
$clean = static function ($v) {
return array_values(array_unique(array_filter(array_map('intval', (array) $v))));
};
$brands = $clean($brands);
$categories = $clean($categories);
$products = $clean($products);
if ($brands) { update_post_meta($post_id, self::META_BRANDS, $brands); } else { delete_post_meta($post_id, self::META_BRANDS); }
if ($categories) { update_post_meta($post_id, self::META_CATEGORIES, $categories); } else { delete_post_meta($post_id, self::META_CATEGORIES); }
if ($products) { update_post_meta($post_id, self::META_PRODUCTS, $products); } else { delete_post_meta($post_id, self::META_PRODUCTS); }
$fee = max(0, (float) $fee);
$currency = strtoupper(preg_replace('/[^A-Za-z]/', '', (string) $currency));
if ($fee > 0) { update_post_meta($post_id, self::META_FEE, $fee); } else { delete_post_meta($post_id, self::META_FEE); }
if ($currency !== '') { update_post_meta($post_id, self::META_CURRENCY, $currency); } else { delete_post_meta($post_id, self::META_CURRENCY); }
}
public static function normalize_dt($v) {
$v = str_replace('T', ' ', trim((string) $v));
if ($v === '') { return ''; }
if (preg_match('/^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2})(:\d{2})?$/', $v, $m)) {
return $m[1] . ' ' . $m[2] . ((isset($m[3]) && $m[3] !== '') ? $m[3] : ':00');
}
return '';
}
public static function to_input($mysql_dt) {
$mysql_dt = trim((string) $mysql_dt);
if ($mysql_dt === '') { return ''; }
return str_replace(' ', 'T', substr($mysql_dt, 0, 16));
}
public static function fmt_dt($mysql_dt) {
$mysql_dt = trim((string) $mysql_dt);
if ($mysql_dt === '') { return ''; }
$ts = strtotime($mysql_dt);
return $ts ? date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $ts) : $mysql_dt;
}
public static function schedule_state($post_id) {
$start = (string) get_post_meta($post_id, self::META_START, true);
$end = (string) get_post_meta($post_id, self::META_END, true);
$now = current_time('mysql');
if ($start !== '' && $now < $start) { return 'before'; }
if ($end !== '' && $now > $end) { return 'ended'; }
return 'live';
}
private static function local_dt($mysql_dt) {
$mysql_dt = trim((string) $mysql_dt);
if ($mysql_dt === '') { return null; }
try {
return new DateTimeImmutable($mysql_dt, wp_timezone());
} catch (\Exception $e) {
return null;
}
}
private static function local_text(DateTimeImmutable $d, $with_time = true) {
$text = wp_date(get_option('date_format'), $d->getTimestamp(), wp_timezone());
if ($with_time && $d->format('H:i') !== '00:00') {
$text .= ', ' . wp_date(get_option('time_format'), $d->getTimestamp(), wp_timezone());
}
return $text;
}
public static function when_text($post_id) {
$start = self::local_dt(get_post_meta($post_id, self::META_START, true));
$end = self::local_dt(get_post_meta($post_id, self::META_END, true));
if (!$start && !$end) { return ''; }
if ($start && $end && $start->format('Y-m-d') === $end->format('Y-m-d')) {
if ($start->format('H:i') === '00:00') {
return self::local_text($start, false);
}
return self::local_text($start) . ' – ' . wp_date(get_option('time_format'), $end->getTimestamp(), wp_timezone());
}
if ($start && $end) {
return self::local_text($start) . ' – ' . self::local_text($end);
}
if ($start) {
return self::local_text($start);
}
return sprintf(__('Until %s', 'ai-eshop-optimizer'), self::local_text($end));
}
public static function details_html($post_id) {
$post_id = (int) $post_id;
$start = self::local_dt(get_post_meta($post_id, self::META_START, true));
$end = self::local_dt(get_post_meta($post_id, self::META_END, true));
$rsvp = self::local_dt(get_post_meta($post_id, self::META_RSVP_DEADLINE, true));
$where = trim((string) get_post_meta($post_id, self::META_LOCATION, true));
$state = self::schedule_state($post_id);
$when = self::when_text($post_id);
$rows = '';
if ($when !== '') {
$at = $start ? $start : $end;
$rows .= '' . esc_html(_x('When', 'the label of an event\'s date and time', 'ai-eshop-optimizer')) . ' '
. '' . esc_html($when) . ' ';
}
if ($where !== '') {
$map = self::map_url($post_id);
$rows .= '' . esc_html__('Where', 'ai-eshop-optimizer') . ' ' . esc_html($where)
. ($map !== '' ? ' ' . esc_html__('Get directions', 'ai-eshop-optimizer') . ' ' : '')
. ' ';
}
if ($rsvp && $state !== 'ended') {
$label = (time() <= $rsvp->getTimestamp())
? __('Register by', 'ai-eshop-optimizer')
: __('Registration closed', 'ai-eshop-optimizer');
$rows .= '' . esc_html($label) . ' ' . esc_html(self::local_text($rsvp)) . ' ';
}
$badge = ($start || $end) ? self::state_label($state) : '';
if ($rows === '' && $badge === '') { return ''; }
return ($badge !== '' ? '' . esc_html($badge) . '
' : '')
. ($rows !== '' ? '' . $rows . ' ' : '');
}
private static function state_label($state) {
if ($state === 'ended') { return __('Ended', 'ai-eshop-optimizer'); }
if ($state === 'before') { return __('Upcoming', 'ai-eshop-optimizer'); }
return __('On now', 'ai-eshop-optimizer');
}
public static function prepend_details($content) {
if (!is_singular(self::POST_TYPE) || !in_the_loop() || !is_main_query()) { return $content; }
if ((int) get_the_ID() !== (int) get_queried_object_id() || has_block('aieo/event-details', (string) $content)) { return $content; }
return "\n" . $content;
}
public static function event_date_block($content, $block, $instance = null) {
$pid = (is_object($instance) && !empty($instance->context['postId'])) ? (int) $instance->context['postId'] : 0;
if ($pid <= 0 || get_post_type($pid) !== self::POST_TYPE) { return $content; }
$attrs = isset($block['attrs']) && is_array($block['attrs']) ? $block['attrs'] : array();
if (!empty($attrs['displayType']) && $attrs['displayType'] === 'modified') { return $content; }
$start = self::local_dt(get_post_meta($pid, self::META_START, true));
$at = $start ? $start : self::local_dt(get_post_meta($pid, self::META_END, true));
if (!$at) { return $content; }
$format = !empty($attrs['format']) ? (string) $attrs['format'] : (string) get_option('date_format');
$time = '' . esc_html(wp_date($format, $at->getTimestamp(), wp_timezone())) . ' ';
$content = (string) preg_replace_callback('#]*>.*? #s', static function () use ($time) { return $time; }, (string) $content, 1);
$state = self::schedule_state($pid);
$content = (string) preg_replace('/\bclass="wp-block-post-date\b/', 'class="wp-block-post-date aieo-event-date aieo-event-date--' . $state, $content, 1);
$own_page = is_singular(self::POST_TYPE) && (int) get_queried_object_id() === $pid;
if ($state === 'ended' && !$own_page) {
$pos = strrpos($content, '');
if ($pos !== false) {
$content = substr($content, 0, $pos) . ' ' . esc_html__('Ended', 'ai-eshop-optimizer') . ' ' . substr($content, $pos);
}
if (function_exists('generate_block_asset_handle')) {
wp_enqueue_style(generate_block_asset_handle('aieo/event-details', 'style'));
}
}
return $content;
}
public static function state_post_class($classes, $class = '', $post_id = 0) {
$post_id = (int) $post_id;
if ($post_id <= 0 || get_post_type($post_id) !== self::POST_TYPE) { return $classes; }
$start = (string) get_post_meta($post_id, self::META_START, true);
$end = (string) get_post_meta($post_id, self::META_END, true);
if ($start === '' && $end === '') { return $classes; }
$map = array('before' => 'upcoming', 'live' => 'on-now', 'ended' => 'ended');
$classes[] = 'aieo-event--' . $map[self::schedule_state($post_id)];
return $classes;
}
public static function gf_form_options($selected_id) {
$out = array('0' => __('— none —', 'ai-eshop-optimizer'));
if (class_exists('GFAPI')) {
foreach ((array) GFAPI::get_forms(true, false) as $f) {
if (!isset($f['id'])) { continue; }
$id = (int) $f['id'];
$out[(string) $id] = '#' . $id . ' — ' . (string) ($f['title'] ?? ('#' . $id));
}
} else {
$out[(string) (int) $selected_id] = sprintf(__('Gravity Forms inactive (saved form #%d)', 'ai-eshop-optimizer'), (int) $selected_id);
}
return $out;
}
public static function gf_form_select($name, $selected_id, $describedby = '') {
$out = '';
$pick = (string) (int) $selected_id;
foreach (self::gf_form_options($selected_id) as $value => $label) {
$out .= '' . esc_html($label) . ' ';
}
$out .= ' ';
return $out;
}
public static function share_links($post_id) {
$permalink = get_permalink($post_id);
if (!$permalink) { return array('facebook' => '', 'tiktok' => '', 'instagram' => ''); }
$tag = static function ($src) use ($permalink) {
return add_query_arg(array('utm_source' => $src, 'utm_medium' => 'social', 'utm_campaign' => 'event'), $permalink);
};
return array(
'facebook' => 'https://www.facebook.com/sharer/sharer.php?u=' . rawurlencode($tag('facebook')),
'tiktok' => $tag('tiktok'),
'instagram' => $tag('instagram'),
);
}
}