Modern Events Calendar single Event create

Frontend form generation of a new MEC Event

Tutorial

Create your own Event Post with “Create Post” Action and then generate with “Custom PHP Script” Action all needed extra data in Custom MEC Tables:

  • wp_mec_events
  • wp_mec_dates

The most of below code is a replica from the original source available in Modern Event Calendar plugin:

Modern Events Calendar Lite

 

				
					// retrieve saved Event ID
global $wpdb;
$post_id = $wpdb->get_var( 'SELECT ID FROM ' . $wpdb->prefix . 'posts' . ' ORDER BY ID DESC LIMIT 1');

// get MEC main class
global $MEC_Main;
if(is_null($MEC_Main)){
        $MEC_Main = new \MEC_main();
}

// adapting submitted data
$event = $fields;
$event['start'] = $event['mec_start_date'];
$event['end'] = $event['finish'] = $event['mec_end_date'];

// /wp-content/plugins/modern-events-calendar-lite/app/libraries/main.php:6013->save_event()

        $start_time_hour = (isset($event['mec_start_time_hour']) ? $event['mec_start_time_hour'] : 8);
        $start_time_minutes = (isset($event['mec_start_time_minutes']) ? $event['mec_start_time_minutes'] : 0);
        $start_time_ampm = (isset($event['mec_start_time_ampm']) ? $event['mec_start_time_ampm'] : 'AM');

        $end_time_hour = (isset($event['mec_end_time_hour']) ? $event['mec_end_time_hour'] : 6);
        $end_time_minutes = (isset($event['mec_end_time_minutes']) ? $event['mec_end_time_minutes'] : 0);
        $end_time_ampm = (isset($event['mec_end_time_ampm']) ? $event['mec_end_time_ampm'] : 'PM');

        $allday = (isset($event['mec_allday']) ? $event['mec_allday'] : 0);
        $time_comment = (isset($event['mec_time_comment']) ? $event['mec_time_comment'] : '');
        $hide_time = ((isset($event['date']) and isset($event['date']['hide_time'])) ? $event['date']['hide_time'] : 0);
        $hide_end_time = ((isset($event['date']) and isset($event['date']['hide_end_time'])) ? $event['date']['hide_end_time'] : 0);

        $day_start_seconds = $MEC_Main->time_to_seconds($MEC_Main->to_24hours($start_time_hour, $start_time_ampm), $start_time_minutes);
        $day_end_seconds = $MEC_Main->time_to_seconds($MEC_Main->to_24hours($end_time_hour, $end_time_ampm), $end_time_minutes);

        $date = array(
            'start'=>array('date'=>$event['start'], 'hour'=>$start_time_hour, 'minutes'=>$start_time_minutes, 'ampm'=>$start_time_ampm),
            'end'=>array('date'=>$event['end'], 'hour'=>$end_time_hour, 'minutes'=>$end_time_minutes, 'ampm'=>$end_time_ampm),
            'repeat'=>((isset($event['date']) and isset($event['date']['repeat']) and is_array($event['date']['repeat'])) ? $event['date']['repeat'] : array()),
            'allday'=>$allday,
            'hide_time'=>((isset($event['date']) and isset($event['date']['hide_time'])) ? $event['date']['hide_time'] : 0),
            'hide_end_time'=>((isset($event['date']) and isset($event['date']['hide_end_time'])) ? $event['date']['hide_end_time'] : 0),
            'comment'=>$time_comment,
        );

        update_post_meta($post_id, 'mec_date', $date);


        // Finish Date
        $finish_date = (isset($event['finish']) ? $event['finish'] : '');
        if($finish_date)
        {
            update_post_meta($post_id, 'mec_repeat_end_at_date', $finish_date);
            update_post_meta($post_id, 'mec_repeat_end', 'date');
        }

        // Not In Days
        $not_in_days = (isset($event['not_in_days']) ? $event['not_in_days'] : '');
        if($not_in_days) update_post_meta($post_id, 'mec_not_in_days', $not_in_days);

// Creating $mec array for inserting in mec_events table
        $mec = array(
            'post_id'=>$post_id, 
            'start'=>$event['start'],
            'repeat'=>isset($event['repeat_status']) ? $event['repeat_status'] : 0,
            'rinterval'=>isset($event['interval']) ? $event['repeat_interval'] : NULL,
            'time_start'=>$day_start_seconds, 
            'time_end'=>$day_end_seconds
        );

        // Add parameters to the $mec
        $mec['end'] = (trim($finish_date) ? $finish_date : '0000-00-00');
        $mec['year'] = isset($event['year']) ? $event['year'] : NULL;
        $mec['month'] = isset($event['month']) ? $event['month'] : NULL;
        $mec['day'] = isset($event['day']) ? $event['day'] : NULL;
        $mec['week'] = isset($event['week']) ? $event['week'] : NULL;
        $mec['weekday'] = isset($event['weekday']) ? $event['weekday'] : NULL;
        $mec['weekdays'] = isset($event['weekdays']) ? $event['weekdays'] : NULL;
        $mec['days'] = isset($event['days']) ? $event['days'] : '';
        $mec['not_in_days'] = $not_in_days;
//var_dump($mec);

// MEC DB Library
        $db = $MEC_Main->getDB();

//$MEC_Main->save_event($fields, $post_id);
    $q1 = "";
    $q2 = "";
    foreach($mec as $key=>$value) {
        $q1 .= "`$key`,";
        if(is_null($value)) $q2 .= "NULL,";
        else $q2 .= "'$value',";
    }
    $db->q("INSERT INTO `#__mec_events` (".trim($q1, ', ').") VALUES (".trim($q2, ', ').")", 'INSERT');

// Update Schedule
        $schedule = $MEC_Main->getSchedule();
        $schedule->reschedule($post_id);