class-elementor-foogallery-widget.php
188 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Elementor oEmbed Widget. |
| 4 | * |
| 5 | * Elementor widget that inserts an embbedable content into the page, from any given URL. |
| 6 | * |
| 7 | * @since 1.0.0 |
| 8 | */ |
| 9 | class Elementor_FooGallery_Widget extends \Elementor\Widget_Base { |
| 10 | |
| 11 | /** |
| 12 | * Get widget name. |
| 13 | * |
| 14 | * Retrieve widget name. |
| 15 | * |
| 16 | * @since 1.0.0 |
| 17 | * @access public |
| 18 | * |
| 19 | * @return string Widget name. |
| 20 | */ |
| 21 | public function get_name() { |
| 22 | return 'foogallery'; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Get widget title. |
| 27 | * |
| 28 | * Retrieve widget title. |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | * @access public |
| 32 | * |
| 33 | * @return string Widget title. |
| 34 | */ |
| 35 | public function get_title() { |
| 36 | return __( 'FooGallery', 'plugin-name' ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get widget icon. |
| 41 | * |
| 42 | * Retrieve oEmbed widget icon. |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | * @access public |
| 46 | * |
| 47 | * @return string Widget icon. |
| 48 | */ |
| 49 | public function get_icon() { |
| 50 | return 'eicon-gallery-justified'; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get widget categories. |
| 55 | * |
| 56 | * Retrieve the list of categories the oEmbed widget belongs to. |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | * @access public |
| 60 | * |
| 61 | * @return array Widget categories. |
| 62 | */ |
| 63 | public function get_categories() { |
| 64 | return [ 'general' ]; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Build gallery select control options. |
| 69 | * |
| 70 | * Provides a shared list of galleries for both the control definition and |
| 71 | * any runtime refresh requests coming from the editor UI. |
| 72 | * |
| 73 | * @since 1.0.0 |
| 74 | * @access public |
| 75 | * |
| 76 | * @return array |
| 77 | */ |
| 78 | public static function get_gallery_options() { |
| 79 | $options = [ |
| 80 | '' => '', |
| 81 | ]; |
| 82 | |
| 83 | $galleries = foogallery_get_all_galleries(); |
| 84 | |
| 85 | if ( empty( $galleries ) ) { |
| 86 | return $options; |
| 87 | } |
| 88 | |
| 89 | foreach ( $galleries as $gallery ) { |
| 90 | $name = $gallery->name; |
| 91 | if ( empty( $name ) ) { |
| 92 | $name = 'Gallery #' . $gallery->ID; |
| 93 | } |
| 94 | |
| 95 | $options[ $gallery->ID ] = $name; |
| 96 | } |
| 97 | |
| 98 | return $options; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Register oEmbed widget controls. |
| 103 | * |
| 104 | * Adds different input fields to allow the user to change and customize the widget settings. |
| 105 | * |
| 106 | * @since 1.0.0 |
| 107 | * @access protected |
| 108 | */ |
| 109 | protected function _register_controls() { |
| 110 | |
| 111 | $this->start_controls_section( |
| 112 | 'content_section', |
| 113 | [ |
| 114 | 'label' => __( 'Content', 'foogallery' ), |
| 115 | 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, |
| 116 | ] |
| 117 | ); |
| 118 | |
| 119 | $options = self::get_gallery_options(); |
| 120 | |
| 121 | $this->add_control( |
| 122 | 'gallery_id', |
| 123 | [ |
| 124 | 'label' => __( 'Choose the gallery', 'foogallery' ), |
| 125 | 'type' => \Elementor\Controls_Manager::SELECT, |
| 126 | 'options' => $options |
| 127 | ] |
| 128 | ); |
| 129 | |
| 130 | $this->add_control( |
| 131 | 'gallery_refresh', |
| 132 | [ |
| 133 | 'text' => esc_html__( 'Refresh Galleries', 'foogallery' ), |
| 134 | 'type' => \Elementor\Controls_Manager::BUTTON, |
| 135 | 'show_label' => false, |
| 136 | 'event' => 'foogallery:refresh', |
| 137 | ] |
| 138 | ); |
| 139 | |
| 140 | $this->add_control( |
| 141 | 'gallery_edit', |
| 142 | [ |
| 143 | 'text' => esc_html__( 'Edit Gallery', 'foogallery' ), |
| 144 | 'type' => \Elementor\Controls_Manager::BUTTON, |
| 145 | 'show_label' => false, |
| 146 | 'event' => 'foogallery:edit', |
| 147 | 'condition' => [ |
| 148 | 'gallery_id!' => '', |
| 149 | ], |
| 150 | ] |
| 151 | ); |
| 152 | |
| 153 | $this->add_control( |
| 154 | 'gallery_add', |
| 155 | [ |
| 156 | 'text' => esc_html__( 'Add New Gallery', 'foogallery' ), |
| 157 | 'type' => \Elementor\Controls_Manager::BUTTON, |
| 158 | 'show_label' => false, |
| 159 | 'event' => 'foogallery:add', |
| 160 | 'condition' => [ |
| 161 | 'gallery_id!' => '', |
| 162 | ], |
| 163 | ] |
| 164 | ); |
| 165 | |
| 166 | $this->end_controls_section(); |
| 167 | |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Render oEmbed widget output on the frontend. |
| 172 | * |
| 173 | * Written in PHP and used to generate the final HTML. |
| 174 | * |
| 175 | * @since 1.0.0 |
| 176 | * @access protected |
| 177 | */ |
| 178 | protected function render() { |
| 179 | $settings = $this->get_settings_for_display(); |
| 180 | $foogallery_id = intval( $settings['gallery_id'] ); |
| 181 | if ( $foogallery_id > 0 ) { |
| 182 | foogallery_render_gallery( $foogallery_id ); |
| 183 | } else if ( is_admin() ) { |
| 184 | echo '<p>' . esc_html__( 'Please select a gallery to display.', 'foogallery' ) . '</p>'; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 |