PluginProbe
UiCore Elements – Free widgets and templates for Elementor / 1.0.16
UiCore Elements – Free widgets and templates for Elementor v1.0.16
1.3.17 1.3.16 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 All 41 releases
uicore-elements / includes / widgets / advanced-post-grid.php

advanced-post-grid.php in UiCore Elements – Free widgets and templates for Elementor 1.0.16, at includes/widgets/advanced-post-grid.php

308 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace UiCoreElements;
4
5 use Elementor\Controls_Manager;
6 use UiCoreElements\UiCoreWidget;
7 use UiCoreElements\Utils\Pagination_Trait;
8 use UiCoreElements\Utils\Animation_Trait;
9 use UiCoreElements\Utils\Grid_Trait;
10 use uicoreElements\Utils\Post_Trait;
11 use uiCoreElements\Utils\Post_Filters_Trait;
12
13 defined('ABSPATH') || exit();
14
15 /**
16 * Scripts and Styles Class
17 *
18 */
19 class AdvancedPostGrid extends UiCoreWidget
20 {
21 use Pagination_Trait,
22 Animation_Trait,
23 Grid_Trait,
24 Post_Filters_Trait,
25 Post_Trait;
26
27 private $_query;
28
29 public function get_name()
30 {
31 return 'uicore-advanced-post-grid';
32 }
33 public function get_categories()
34 {
35 return ['uicore'];
36 }
37 public function get_title()
38 {
39 return __('Advanced Post Grid', 'uicore-elements');
40 }
41 public function get_icon()
42 {
43 return 'eicon-gallery-grid ui-e-widget';
44 }
45 public function get_keywords()
46 {
47 return ['post', 'grid', 'blog', 'recent', 'news'];
48 }
49 public function get_styles()
50 {
51 $styles = [
52 'advanced-post-grid',
53 'legacy-grid',
54 'post-meta',
55 'filters' => [
56 'condition' => [
57 'post_filtering' => 'yes',
58 ],
59 ],
60 'animation', // hover animations
61 'entrance', // entrance basic style
62 ];
63 if(!class_exists('\UiCore\Core') && !class_exists('\UiCoreAnimate\Base')){
64 $styles['e-animations'] = [ // entrance animations
65 'external' => true,
66 ];
67 }
68 return $styles;
69 }
70 public function get_scripts()
71 {
72 return [
73 'masonry' => [
74 'condition' => [
75 'masonry' => 'ui-e-maso'
76 ],
77 ],
78 'entrance' => [
79 'condition' => [
80 'animate_items' => 'ui-e-grid-animate'
81 ],
82 ],
83 'ajax-request' => [
84 'condition' => [
85 'pagination_type' => 'load_more'
86 ]
87 ]
88 ];
89 }
90
91 private function filter_missing_taxonomies($settings)
92 {
93 // Check if is an "Advanced Product.." widget, since this widget is extended by other(s)
94 $slug = strpos($this->get_name(), 'product') !== false ?
95 'product-filter_post_type' :
96 'posts-filter_post_type';
97
98 $taxonomy_filter_args = [
99 'show_in_nav_menus' => true,
100 ];
101 if ( !empty($settings[$slug]) ) {
102 $taxonomy_filter_args['object_type'] = [$settings[$slug]];
103 }
104
105 $taxonomies = get_taxonomies($taxonomy_filter_args, 'objects');
106
107 foreach ($taxonomies as $taxonomy => $object) {
108 $controll_id = 'posts-filter_' . $taxonomy . '_ids';
109 \error_log($controll_id);
110 \error_log(print_r($settings[$controll_id], true));
111
112 if (!isset($settings[$controll_id]) || empty($settings[$controll_id])) {
113 continue;
114 }
115
116 //if is set check if this id still exists
117 $settings[$controll_id] = array_filter($settings[$controll_id], function ($term_id) use ($object) {
118 return term_exists($term_id, $object->name);
119 });
120
121 if (empty($settings[$controll_id])) {
122 $settings[$controll_id] = null;
123 }
124 }
125
126 return $settings;
127 }
128 public function on_import($element)
129 {
130 $element['settings'] = $this->filter_missing_taxonomies($element['settings']);
131 return $element;
132 }
133 function get_query()
134 {
135 return $this->_query;
136 }
137
138 protected function register_controls()
139 {
140 // Query(curent/custom/related/manual)
141
142 // /Pagination (filtering?)
143 // /Aditional (no posts)
144
145 // Contents and Settings
146 $this->start_controls_section(
147 'section_layout',
148 [
149 'label' => esc_html__('Grid', 'uicore-elements'),
150 ]
151 );
152 $this->TRAIT_register_grid_layout_controls();
153 $this->end_controls_section();
154
155 $this->TRAIT_register_post_item_controls();
156 $this->TRAIT_register_post_button_controls();
157 $this->TRAIT_register_post_meta_controls();
158 $this->TRAIT_register_post_query_controls(true, $this->get_name());
159 $this->TRAIT_register_filter_controls();
160 $this->TRAIT_register_pagination_controls();
161
162 // Styles
163 $this->TRAIT_register_post_item_style_controls();
164 $this->TRAIT_register_post_content_style_controls();
165 $this->TRAIT_register_post_button_style_controls();
166 $this->TRAIT_register_post_meta_style_controls();
167 $this->TRAIT_register_filter_style_controls();
168 $this->TRAIT_register_pagination_style_controls();
169
170 $this->start_controls_section(
171 'section_style_animations',
172 [
173 'label' => __('Animations', 'uicore-elements'),
174 'tab' => Controls_Manager::TAB_STYLE,
175 ]
176 );
177 $this->TRAIT_register_entrance_animations_controls();
178 $this->TRAIT_register_hover_animation_control(
179 'Item Hover Animation',
180 [],
181 ['underline'],
182 'anim_item'
183 );
184 $this->TRAIT_register_post_animation_controls();
185 $this->end_controls_section();
186
187 // Update masonry from component to a legacy version
188 $this->update_control('masonry', [
189 'label' => __( 'Masonry', 'uicore-elements' ),
190 'type' => Controls_Manager::SWITCHER,
191 'frontend_available' => true,
192 'default' => 'no',
193 'return_value' => 'ui-e-maso',
194 'render_type' => 'template',
195 // reset values from component
196 'prefix_class' => '',
197 'condition' => [],
198 'selectors' => [],
199 ]);
200 }
201
202 function content_template()
203 {
204 }
205
206 /**
207 * Renders the widget content using AJAX.
208 *
209 * This method retrieves the widget settings; set up the query, and renders each post item.
210 * If no posts are found, it returns false. After rendering, it resets the query and returns the output.
211 *
212 * @param array|false $current_query The current query args, or false if the widget isn't set to use the current query.
213 *
214 * @return string|false The rendered widget content or false if no posts are found.
215 */
216 public function render_ajax($current_query)
217 {
218
219 // Get settings and post type
220 $settings = $this->get_settings();
221
222 $this->TRAIT_query_posts( $settings, $current_query );
223 $wp_query = $this->get_query();
224
225 if (!$wp_query->have_posts()) {
226 return false;
227 }
228
229 \ob_start();
230 while ($wp_query->have_posts()) {
231 $wp_query->the_post();
232 $this->TRAIT_render_item(false, true);
233 }
234 $markup = \ob_get_clean();
235
236 return [
237 'markup' => $markup,
238 ];
239 }
240
241 protected function render()
242 {
243 // Get query args, settings and post type slug
244 global $wp_query;
245 $default_query = $wp_query;
246 $settings = $this->get_settings();
247
248 // Build query
249 $this->TRAIT_query_posts( $settings, $wp_query->query );
250 $wp_query = $this->get_query();
251
252 $this->TRAIT_set_meta_font_size_to_svg($settings); // SVG icon experiment font-size adjustment
253
254 // Store widget settings in a transient
255 $ID = strval($this->get_ID());
256 set_transient('ui_elements_widgetdata_' . $ID, $settings, \MONTH_IN_SECONDS);
257
258 // Get the quantity of items and creates a loop control
259 $items = $settings['item_limit']['size'];
260 $loops = 0;
261
262 // Set masonry class
263 $masonry = $settings['masonry'] === 'yes' ? 'ui-e-maso' : '';
264
265 $this->TRAIT_render_filters($settings);
266
267 // No posts found
268 if (!$wp_query->have_posts()) {
269 echo '<p style="text-align:center">' . __('No posts found.', 'uicore-elements') . '</p>';
270 } else {
271
272 ?>
273 <div class="ui-e-adv-grid <?php echo esc_attr($masonry);?>">
274 <?php
275 while ($wp_query->have_posts()) {
276
277 // sticky posts disregards posts per page, so ending the loop if $items == $loop forces the query respects the users item limit
278 if ($settings['sticky'] && $items == $loops) {
279 break;
280 }
281
282 $wp_query->the_post();
283 $this->TRAIT_render_item(false, true);
284
285 $loops++;
286 }
287 ?>
288 </div>
289 <?php
290
291 $this->TRAIT_render_pagination($settings);
292
293 if( $settings['pagination'] == 'yes' && $settings['pagination_type'] == 'load_more' && $settings['posts-filter_post_type'] == 'current' ) {
294
295 echo '<script>';
296 echo 'window.ui_total_pages_' . esc_html($ID) . ' = "none";'; // add none to avoid ajax errors for lack of value, but posts don't need it
297 echo 'window.ui_query_' . esc_html($ID) . ' = ' . \json_encode($wp_query) . ';';
298 echo '</script>';
299 }
300 }
301
302 //reset query
303 wp_reset_query();
304 $wp_query = $default_query;
305 }
306 }
307 \Elementor\Plugin::instance()->widgets_manager->register(new AdvancedPostGrid());
308