class-service-category-widget.php
97 lines
| 1 | <?php |
| 2 | use Elementor\Widget_Base; |
| 3 | use Elementor\Controls_Manager; |
| 4 | |
| 5 | if (!defined('ABSPATH')) |
| 6 | exit; |
| 7 | |
| 8 | class Service_Category_Widget extends Widget_Base |
| 9 | { |
| 10 | |
| 11 | public function get_name() |
| 12 | { |
| 13 | return 'service_category_widget'; |
| 14 | } |
| 15 | |
| 16 | public function get_title() |
| 17 | { |
| 18 | return __('Service Categories', 'flex-import'); |
| 19 | } |
| 20 | |
| 21 | public function get_icon() |
| 22 | { |
| 23 | return 'eicon-folder'; |
| 24 | } |
| 25 | |
| 26 | public function get_categories() |
| 27 | { |
| 28 | return ['general']; |
| 29 | } |
| 30 | |
| 31 | protected function _register_controls() |
| 32 | { |
| 33 | $this->start_controls_section( |
| 34 | 'section_content', |
| 35 | [ |
| 36 | 'label' => __('Content', 'flex-import'), |
| 37 | 'tab' => Controls_Manager::TAB_CONTENT, |
| 38 | ] |
| 39 | ); |
| 40 | |
| 41 | $this->add_control( |
| 42 | 'columns', |
| 43 | [ |
| 44 | 'label' => __('Columns', 'flex-import'), |
| 45 | 'type' => Controls_Manager::NUMBER, |
| 46 | 'default' => 4, |
| 47 | 'min' => 1, |
| 48 | 'max' => 6, |
| 49 | ] |
| 50 | ); |
| 51 | |
| 52 | $this->end_controls_section(); |
| 53 | } |
| 54 | |
| 55 | protected function render() |
| 56 | { |
| 57 | $settings = $this->get_settings_for_display(); |
| 58 | $columns = !empty($settings['columns']) ? $settings['columns'] : 4; |
| 59 | |
| 60 | $terms = get_terms([ |
| 61 | 'taxonomy' => 'service_category', |
| 62 | 'hide_empty' => false, |
| 63 | ]); |
| 64 | |
| 65 | if (!empty($terms) && !is_wp_error($terms)) { |
| 66 | |
| 67 | echo '<div class="service-category-grid" style="display: grid; gap: 30px; grid-template-columns: repeat(' . esc_attr($columns) . ', 1fr);">'; |
| 68 | |
| 69 | foreach ($terms as $term) { |
| 70 | $image_id = get_term_meta($term->term_id, 'service_category_image', true); |
| 71 | $image_url = $image_id ? wp_get_attachment_url($image_id) : ''; |
| 72 | |
| 73 | echo '<div class="service-category-item" style="position: relative;">'; |
| 74 | echo '<div class="service-category-box" style="padding: 40px 20px; border-radius: 10px;">'; |
| 75 | echo '<div class="service-category-box-child">'; |
| 76 | |
| 77 | if ($image_url) { |
| 78 | echo '<div class="category-image-wrapper" style="margin-bottom:15px;">'; |
| 79 | echo '<div class="category-image">'; |
| 80 | echo '<img src="' . esc_url($image_url) . '" alt="' . esc_attr($term->name) . '" style="max-width:100%; height:auto;">'; |
| 81 | echo '</div>'; |
| 82 | echo '</div>'; |
| 83 | } |
| 84 | |
| 85 | echo '<h3 class="category-title">' . esc_html($term->name) . '</h3>'; |
| 86 | echo '<p class="category-description">' . esc_html($term->description) . '</p>'; |
| 87 | |
| 88 | echo '</div></div></div>'; |
| 89 | } |
| 90 | |
| 91 | echo '</div>'; |
| 92 | } else { |
| 93 | echo '<p>No service categories found.</p>'; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 |