/** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget($args, $instance) { global $wpdb; $table = wcs3_get_table_name(); $output = ''; $title = apply_filters('widget_title', $instance['title']); echo $args['before_widget']; if (!empty($title)) { echo $args['before_title'] . $title . $args['after_title']; } // Get today's weekday index $today = date('w', time()); $location_id = $instance['location'] != 'all' ? $instance['location'] : NULL; $max_classes = intval($instance['max_classes']); $limit = is_int($max_classes) ? $max_classes : NULL; $no_entries_msg = strlen($instance['no_entries_text']) > 0 ? $instance['no_entries_text'] : __('No classes today'); $schedule = wcs3_get_day_schedule($today, $location_id, $limit); if ($schedule == FALSE) { $output .= '<div class="wcs3-no-classes">' . $no_entries_msg . '</div>'; echo $output; return; } $output .= '<ul class="wcs3-today-classes-widget-list">'; foreach ($schedule as $key => $entry) { if (isset($entry['visible']) && $entry['visible'] == 'Visible') { $start_hour = $entry['start_hour']; $class_name = $entry['class']; $output .= "<li>{$start_hour} - {$class_name}</li>"; } } $output .= '</ul>'; echo $output; echo $args['after_widget']; }
/** * Gets the wcs2 schedule data and create new wcs3 entries. * * @param array $data: wcs2 data. */ function wcs3_get_wcs2_schedule_data($data) { global $wpdb; $table = $wpdb->prefix . 'wcs2_schedule'; $wcs3_table = wcs3_get_table_name(); $query = "SELECT class_id, instructor_id, classroom_id, weekday, start_hour,\n end_hour, visibility, notes FROM {$table}"; $r = $wpdb->get_results($query); $timezone = wcs3_get_system_timezone(); foreach ($r as $entry) { $new_class_id = $data['classes'][$entry->class_id]['new_id']; $new_inst_id = $data['instructors'][$entry->instructor_id]['new_id']; $new_loc_id = $data['classrooms'][$entry->classroom_id]['new_id']; $wpdb->insert($wcs3_table, array('class_id' => $new_class_id, 'instructor_id' => $new_inst_id, 'location_id' => $new_loc_id, 'weekday' => $entry->weekday, 'start_hour' => $entry->start_hour, 'end_hour' => $entry->end_hour, 'timezone' => $timezone, 'visible' => $entry->visibility, 'notes' => $entry->notes), array('%d', '%d', '%d', '%d', '%s', '%s', '%s', '%d', '%s')); } }
/** * Delete schedule entries when class, instructor, or location gets deleted. */ function wcs3_schedule_sync($post_id) { global $wpdb; $table = wcs3_get_table_name(); // Since all three custom post types are in the same table, we can // assume the the ID will be unique so there's no need to check for // post type. $query = "DELETE FROM {$table} \n WHERE class_id = %d OR instructor_id = %d \n OR location_id = %d"; $wpdb->query($wpdb->prepare($query, array($post_id, $post_id, $post_id))); }
/** * Gets all the visible classes from the database including instructors and locations. * * @param string $layout: 'normal', 'list', etc. * @param string $location * @param string $mode: 12 or 24. */ function wcs3_get_classes($layout, $location, $mode = '12', $instructor = 'all', $class = 'all') { global $wpdb; $format = $mode == '12' ? 'g:i a' : 'G:i'; $schedule_table = wcs3_get_table_name(); $posts_table = $wpdb->prefix . 'posts'; $meta_table = $wpdb->prefix . 'postmeta'; $query = "SELECT \n c.post_title AS class_title, c.post_content AS class_desc,\n i.post_title AS instructor_title, i.post_content AS instructor_desc,\n l.post_title AS location_title, l.post_content AS location_desc,\n s.weekday, s.start_hour, s.end_hour, \n s.notes FROM {$schedule_table} s\n INNER JOIN {$posts_table} c ON s.class_id = c.ID\n INNER JOIN {$posts_table} i ON s.instructor_id = i.ID\n INNER JOIN {$posts_table} l ON s.location_id = l.ID\n WHERE s.visible = 1"; $query = apply_filters('wcs3_filter_get_classes_query', $query, $schedule_table, $posts_table, $meta_table); // Filter by location if ($location != 'all') { $query .= " AND l.post_title = %s"; $query = $wpdb->prepare($query, array($location)); } // Filter by instructor if ($instructor != 'all') { $query .= " AND i.post_title = %s"; $query = $wpdb->prepare($query, array($instructor)); } // Filter by class if ($class != 'all') { $query .= " AND c.post_title = %s"; $query = $wpdb->prepare($query, array($class)); } $query .= " ORDER BY s.start_hour"; $results = $wpdb->get_results($query); $grouped = array(); if ($results) { foreach ($results as $class) { // Prep CSS class name wcs3_format_class_object($class, $format); if ($layout == 'list') { $grouped[$class->weekday][] = $class; } else { $grouped[$class->start_hour_css][] = $class; } } } return $grouped; }
/** * Returns the schedule for a specific day. */ function wcs3_get_day_schedule_callback() { wcs3_verify_nonce(); global $wpdb; $response = __('Day schedule retrieved successfully', 'wcs3'); $result = 'updated'; $table = wcs3_get_table_name(); $required = array('day' => __('Day')); wcs3_verify_required_fields($required); $day = sanitize_text_field($_POST['day']); $day_table = wcs3_render_day_table($day); wcs3_json_response(array('html' => $day_table)); die; }