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

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