diff --git a/wp-content/themes/academyAfrica/includes/widgets/events.php b/wp-content/themes/academyAfrica/includes/widgets/events.php index 03469c12..4c8bafb6 100644 --- a/wp-content/themes/academyAfrica/includes/widgets/events.php +++ b/wp-content/themes/academyAfrica/includes/widgets/events.php @@ -81,6 +81,14 @@ public function get_events($args) 'compare' => 'BETWEEN', ); } + if ($date_filter == "closed") { + $meta_query[] = array( + 'key' => 'date', + 'value' => $current_date, + 'type' => 'DATE', + 'compare' => '<', + ); + } } if (!empty($meta_query)) { $args['meta_query'] = $meta_query; @@ -122,31 +130,135 @@ public function get_events($args) return array($result, $pagination); } - public function get_filter_by() + public function filter_labels() { return [ + 'country' => esc_html__('Country', 'academy-africa'), + 'date' => esc_html__('Date', 'academy-africa'), + 'speaker' => esc_html__('Speaker', 'academy-africa'), + 'status' => esc_html__('Status', 'academy-africa'), + 'organisation' => esc_html__('Organisation', 'academy-africa'), + ]; + } + + protected function register_controls() + { + $this->start_controls_section( + 'labels', [ - "title" => "Country", - "name" => "country", - "options" => [ - "Nigeria" => "Nigeria", - "Kenya" => "Kenya", - "South Africa" => "South Africa", - "Tanzania" => "Tanzania", - ] - ], + 'label' => __('Labels', 'academy-africa'), + ] + ); + $this->add_control( + 'filter_by', [ - "title" => "Date", - "name" => "date", - "options" => [ - "All" => "all", - "This Week" => "week", - "This Month" => "month", - "Closed" => "closed", - - ] + 'label' => __('Filter By', 'academy-africa'), + 'type' => \Elementor\Controls_Manager::TEXTAREA, + 'default' => __('Filter by:', 'academy-africa'), + 'label_block' => true, ] - ]; + ); + $this->add_control( + 'page_title', + [ + 'label' => __('Pages Title', 'academy-africa'), + 'type' => \Elementor\Controls_Manager::TEXTAREA, + 'default' => __('Events', 'academy-africa'), + 'label_block' => true, + ] + ); + $this->add_control( + 'upcoming_title', + [ + 'label' => __('Upcoming Events Title', 'academy-africa'), + 'type' => \Elementor\Controls_Manager::TEXTAREA, + 'default' => __('Upcoming', 'academy-africa'), + 'label_block' => true, + ] + ); + $this->add_control( + 'previous_events_title', + [ + 'label' => __('Previous Events Title', 'academy-africa'), + 'type' => \Elementor\Controls_Manager::TEXTAREA, + 'default' => __('Previous Events', 'academy-africa'), + 'label_block' => true, + ] + ); + $this->end_controls_section(); + $this->start_controls_section( + 'Filter', + [ + 'label' => __('Labels', 'academy-africa'), + ] + ); + $this->add_control( + 'sort_by', + [ + 'label' => __('Sort by label', 'academy-africa'), + 'type' => \Elementor\Controls_Manager::TEXTAREA, + 'default' => __('Sort by:', 'academy-africa'), + 'label_block' => true, + ] + ); + $this->add_control( + 'filter_options', + [ + 'label' => esc_html__('Filter options', 'academy-africa'), + 'type' => \Elementor\Controls_Manager::SELECT2, + 'label_block' => true, + 'multiple' => true, + 'options' => $this->filter_labels(), + 'default' => ['country', 'date'], + ] + ); + $this->end_controls_section(); + } + public function get_filter_by() + { + $settings = $this->get_settings_for_display(); + $filter_options = $settings['filter_options']; + $output = array(); + if (!empty($filter_options)) { + foreach ($filter_options as $option) { + if ($option == "date") { + $output[] = array( + "title" => "Date", + "name" => "date", + "options" => array( + "All" => "all", + "This Week" => "week", + "This Month" => "month", + "Closed" => "closed" + ) + ); + } else { + $args = array( + 'post_type' => 'event', + 'posts_per_page' => -1, + ); + $query = new WP_Query($args); + $options = array(); + if ($query->have_posts()) { + while ($query->have_posts()) { + $query->the_post(); + $post_id = get_the_ID(); + $field_name = get_post_meta($post_id, $option, true); + if ($field_name) { + $options[$field_name] = $field_name; + } + } + wp_reset_postdata(); + } + $output[] = array( + "title" => $this->filter_labels()[$option] ?? $option, + "name" => $option, + "options" => $options + ); + } + } + } + return $output; } public function get_upcoming_events() { @@ -185,23 +297,21 @@ public function get_previous_events() } protected function render() { - $filter_by = "Filter by:"; - $page_title = "Events"; + $settings = $this->get_settings_for_display(); + $filter_by = $settings["filter_by"]; + $page_title = $settings["page_title"]; $upcoming = $this->get_upcoming_events(); $upcoming_events = $upcoming[0]; $upcoming_pagination = $upcoming[1]; $previous = $this->get_previous_events(); $previous_events = $previous[0]; $previous_pagination = $previous[1]; - $upcoming_events_title = "Upcoming"; - $previous_events_title = "Previous Events"; + $upcoming_events_title = $settings["upcoming_title"]; + $previous_events_title = $settings["previous_events_title"]; $filter_options = $this->get_filter_by(); ?>
- +