PluginProbe
UiCore Elements – Free widgets and templates for Elementor / 1.2.1
UiCore Elements – Free widgets and templates for Elementor v1.2.1
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.2.1, at includes/widgets/advanced-post-grid.php

319 lines 10.0 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 // TODO: remove or set as false, after 3.30, when the full deprecation of widget innet wrapper is ready
91 public function has_widget_inner_wrapper(): bool {
92 return true;
93 }
94
95 private function filter_missing_taxonomies($settings)
96 {
97 // Check if is an "Advanced Product.." widget, since this widget is extended by other(s)
98 $slug = strpos($this->get_name(), 'product') !== false ?
99 'product-filter_post_type' :
100 'posts-filter_post_type';
101
102 $taxonomy_filter_args = [
103 'show_in_nav_menus' => true,
104 ];
105 if ( !empty($settings[$slug]) ) {
106 $taxonomy_filter_args['object_type'] = [$settings[$slug]];
107 }
108
109 $taxonomies = get_taxonomies($taxonomy_filter_args, 'objects');
110
111 foreach ($taxonomies as $taxonomy => $object) {
112 $controll_id = 'posts-filter_' . $taxonomy . '_ids';
113 //error_log($controll_id);
114 //error_log(print_r($settings[$controll_id], true));
115
116 if (!isset($settings[$controll_id]) || empty($settings[$controll_id])) {
117 continue;
118 }
119
120 //if is set check if this id still exists
121 $settings[$controll_id] = array_filter($settings[$controll_id], function ($term_id) use ($object) {
122 return term_exists($term_id, $object->name);
123 });
124
125 if (empty($settings[$controll_id])) {
126 $settings[$controll_id] = null;
127 }
128 }
129
130 return $settings;
131 }
132 public function on_import($element)
133 {
134 $element['settings'] = $this->filter_missing_taxonomies($element['settings']);
135 return $element;
136 }
137 function get_query()
138 {
139 return $this->_query;
140 }
141
142 protected function register_controls()
143 {
144 // Query(curent/custom/related/manual)
145
146 // /Pagination (filtering?)
147 // /Aditional (no posts)
148
149 // Contents and Settings
150 $this->start_controls_section(
151 'section_layout',
152 [
153 'label' => esc_html__('Grid', 'uicore-elements'),
154 ]
155 );
156 $this->TRAIT_register_grid_layout_controls();
157 $this->end_controls_section();
158
159 $this->TRAIT_register_post_item_controls();
160 $this->TRAIT_register_post_button_controls();
161 $this->TRAIT_register_post_meta_controls();
162 $this->TRAIT_register_post_query_controls(true, $this->get_name());
163 $this->TRAIT_register_filter_controls();
164 $this->TRAIT_register_pagination_controls();
165
166 // Styles
167 $this->TRAIT_register_post_item_style_controls();
168 $this->TRAIT_register_post_content_style_controls();
169 $this->TRAIT_register_post_button_style_controls();
170 $this->TRAIT_register_post_meta_style_controls();
171 $this->TRAIT_register_filter_style_controls();
172 $this->TRAIT_register_pagination_style_controls();
173
174 $this->start_controls_section(
175 'section_style_animations',
176 [
177 'label' => __('Animations', 'uicore-elements'),
178 'tab' => Controls_Manager::TAB_STYLE,
179 ]
180 );
181 $this->TRAIT_register_entrance_animations_controls();
182 $this->TRAIT_register_hover_animation_control(
183 'Item Hover Animation',
184 [],
185 ['underline'],
186 'anim_item'
187 );
188 $this->TRAIT_register_post_animation_controls();
189 $this->end_controls_section();
190
191 // Update masonry from component to a legacy version
192 $this->update_control('masonry', [
193 'label' => __( 'Masonry', 'uicore-elements' ),
194 'type' => Controls_Manager::SWITCHER,
195 'frontend_available' => true,
196 'default' => 'no',
197 'return_value' => 'ui-e-maso',
198 'render_type' => 'template',
199 // reset values from component
200 'prefix_class' => '',
201 'condition' => [],
202 'selectors' => [],
203 ]);
204 }
205
206 function content_template()
207 {
208 }
209
210 /**
211 * Renders the widget content using AJAX.
212 *
213 * This method retrieves the widget settings; set up the query, and renders each post item.
214 * If no posts are found, it returns false. After rendering, it resets the query and returns the output.
215 *
216 * @param array|false $current_query The current query args, or false if the widget isn't set to use the current query.
217 *
218 * @return string|false The rendered widget content or false if no posts are found.
219 */
220 public function render_ajax($current_query)
221 {
222
223 // Get settings and post type
224 $settings = $this->get_settings();
225 $loop = 0;
226
227 $this->TRAIT_query_posts( $settings, $current_query );
228 $wp_query = $this->get_query();
229
230 if (!$wp_query->have_posts()) {
231 return false;
232 }
233
234 \ob_start();
235 while ($wp_query->have_posts()) {
236 $wp_query->the_post();
237 $this->TRAIT_render_item($loop, false, true);
238 $loop++;
239 }
240 $markup = \ob_get_clean();
241
242 return [
243 'markup' => $markup,
244 ];
245 }
246
247 protected function render()
248 {
249 // Get query args, settings and post type slug
250 global $wp_query;
251 $default_query = $wp_query;
252 $settings = $this->get_settings();
253
254 // Build query
255 $this->TRAIT_query_posts( $settings, $wp_query->query );
256 $wp_query = $this->get_query();
257
258 $this->TRAIT_set_meta_font_size_to_svg($settings); // SVG icon experiment font-size adjustment
259
260 // Store widget settings in a transient
261 $ID = strval($this->get_ID());
262 set_transient('ui_elements_widgetdata_' . $ID, $settings, \MONTH_IN_SECONDS);
263
264 // Get the quantity of items and creates a loop control
265 $items = $settings['item_limit']['size'];
266 $loops = 0;
267
268 // Set masonry class
269 $masonry = $settings['masonry'] === 'yes' ? 'ui-e-maso' : '';
270
271 $this->TRAIT_render_filters($settings);
272
273 // No posts found
274 if (!$wp_query->have_posts()) {
275 echo '<p style="text-align:center">' . esc_html__('No posts found.', 'uicore-elements') . '</p>';
276 } else {
277
278 ?>
279 <div class="ui-e-adv-grid <?php echo esc_attr($masonry);?>">
280 <?php
281 while ($wp_query->have_posts()) {
282
283 // sticky posts disregards posts per page, so ending the loop if $items == $loop forces the query respects the users item limit
284 if ($settings['sticky'] && $items == $loops) {
285 break;
286 }
287
288 $wp_query->the_post();
289 $this->TRAIT_render_item($loops, false, true);
290
291 $loops++;
292 }
293 ?>
294 </div>
295 <?php
296
297 $this->TRAIT_render_pagination($settings);
298
299 if( $settings['pagination'] == 'yes' && $settings['pagination_type'] == 'load_more' && $settings['posts-filter_post_type'] == 'current' ) {
300
301 // Build the public query args the ajax request script passes to rest api
302 $public_query = array();
303 $public_query['query']['post_type'] = get_post_type();
304 $public_query['query_vars'] = $wp_query->query_vars;
305
306 echo '<script>';
307 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
308 echo 'window.ui_query_' . esc_html($ID) . ' = ' . \json_encode($public_query) . ';';
309 echo '</script>';
310 }
311 }
312
313 //reset query
314 wp_reset_query();
315 $wp_query = $default_query;
316 }
317 }
318 \Elementor\Plugin::instance()->widgets_manager->register(new AdvancedPostGrid());
319