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 / class-helper.php

class-helper.php in UiCore Elements – Free widgets and templates for Elementor 1.2.1, at includes/class-helper.php

434 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace UiCoreElements;
3
4 use Elementor\Plugin;
5
6 defined('ABSPATH') || exit();
7 /**
8 * UiCore Utils Functions
9 */
10 class Helper
11 {
12
13 public static function get_separator()
14 {
15 return '<span class="uicore-meta-separator"></span>';
16 }
17
18 public static function get_taxonomies($custom = true, $only_products = false)
19 {
20
21 // Woo only taxonomies
22 if ($only_products) {
23 $taxonomies = [
24 'product_cat' => 'Product Categories',
25 'product_tag' => 'Product Tags'
26 ];
27
28 $attributes = wc_get_attribute_taxonomies();
29 foreach ($attributes as $att) {
30 $taxonomies['pa_' . $att->attribute_name] = $att->attribute_label;
31 }
32
33 // All taxonomies
34 } else {
35
36 $taxonomies = get_taxonomies(['public' => true], 'objects');
37 $exclusions = ['nav_menu', 'link_category', 'post_format']; // Exclude these taxonomies from the list
38
39 $taxonomies = array_filter($taxonomies, function ($taxonomy) use ($exclusions) {
40 return !in_array($taxonomy->name, $exclusions);
41 });
42
43 $taxonomies = array_map(function ($taxonomy) {
44 if('portfolio_category' === $taxonomy->name){
45 return 'Portfolio Category';
46 }
47 return $taxonomy->label;
48 }, $taxonomies);
49 }
50
51 if ($custom) {
52 $taxonomies = array_merge(['custom' => __('Custom Meta', 'uicore-elements')], $taxonomies);
53 }
54
55 return $taxonomies;
56 }
57
58 static function get_taxonomy($name)
59 {
60 global $post;
61 $categories = get_the_terms( $post->ID, $name );
62
63 if ( ! $categories || is_wp_error( $categories ) ) {
64 return false;
65 }
66
67 $categories = array_values( $categories );
68 foreach ($categories as $t) {
69 $term_name[] =
70 '<a href="' . get_term_link($t) . '" title="View ' . \esc_attr($t->name) . ' posts">' . esc_html($t->name) . '</a>';
71 }
72 $category = implode(', ', $term_name);
73
74 return $category;
75 }
76
77 static function get_custom_meta($name)
78 {
79 global $post;
80
81 $meta = get_post_meta( $post->ID, $name, true );
82
83 // If is an array means meta is not valid since we are expecting a single value string
84 if ( ! $meta || is_array($meta) ) {
85 return false;
86 }
87
88 return $meta;
89 }
90
91 static function get_reading_time()
92 {
93 global $post;
94
95 $the_content = $post->post_content;
96 $words = str_word_count( wp_strip_all_tags( $the_content ) ); // count the number of words
97 $minute = floor( $words / 200 ); // rounding off and deviding per 200 words per minute
98
99 return $minute;
100 }
101
102 static function get_site_domain() {
103 return str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
104 }
105
106 /**
107 * Returns the Uicore Elements settings page URL. You may pass a message so it'll be wrapped under a <a> tag.
108 *
109 * @param string $message Optional. A clickable message to be displayed that'll redirect, in a new tab, to settings page.
110 *
111 * @return string Uicore Elements settings page URL or an <a> tag HTML with the url and the passed message.
112 */
113 static function get_admin_settings_url(string $message = '') {
114 $url = admin_url( 'options-general.php?page=uicore-elements' );
115 return !empty($message)
116 ? '<a href="'.esc_url($url).'" target="_blank">' . esc_html($message) . '</a>'
117 : $url;
118 }
119
120 static function register_widget_style($name,$deps=[],$external=false)
121 {
122 $handle = (!$external ? 'ui-e-' : '' ). $name;
123 wp_register_style($handle, UICORE_ELEMENTS_ASSETS . '/css/elements/'.$name.'.css',$deps,UICORE_ELEMENTS_VERSION);
124 return $handle;
125 }
126 static function register_widget_script($name,$deps=[],$external=false)
127 {
128 $handle = (!$external ? 'ui-e-' : '' ). $name;
129 //if name contains / then we need to set a custom path
130 if(strpos($name,'/') !== false){
131 $path ='';
132 }else{
133 $path = 'elements/';
134 }
135 wp_register_script($handle, UICORE_ELEMENTS_ASSETS . '/js/'.$path.$name.'.js',$deps,UICORE_ELEMENTS_VERSION,true);
136 return $handle;
137 }
138
139 public static function get_related($filter, $number)
140 {
141 global $post;
142
143 $args = [];
144
145 if ($filter == 'category') {
146 $categories = get_the_category($post->ID);
147
148 if ($categories) {
149 $category_ids = array_map(fn($cat) => $cat->term_id, $categories);
150
151 // basic args
152 $args = [
153 'post__not_in' => [$post->ID],
154 'posts_per_page' => $number,
155 'category__in' => $category_ids,
156 'ignore_sticky_posts' => 1,
157 ];
158 }
159
160 } elseif ($filter == 'tag') {
161 $tags = wp_get_post_tags($post->ID);
162
163 if ($tags) {
164 $tag_ids = array_map(fn($tag) => $tag->term_id, $tags);
165
166 $args = [
167 'post__not_in' => [$post->ID],
168 'posts_per_page' => $number,
169 'tag__in' => $tag_ids,
170 'ignore_sticky_posts' => 1,
171 ];
172 }
173
174 // default/random filter
175 } else {
176 $args = [
177 'post__not_in' => [$post->ID],
178 'posts_per_page' => $number,
179 'orderby' => 'rand',
180 ];
181 }
182
183 $related_query = new \WP_Query($args);
184 return $related_query->have_posts() ? $related_query : false;
185 }
186
187 public static function get_product_related($limit)
188 {
189 global $post;
190
191 return wc_get_related_products($post->ID, $limit);
192 }
193
194 /*
195 * Get the current post id (used in Theme Builder - UiCore Framework)
196 */
197 static function get_current_meta_id()
198 {
199 if(\class_exists('\UiCore\Blog\Frontend') && \UiCore\Blog\Frontend::is_blog() && !is_singular('post')){
200 $post_id = get_option('page_for_posts', true);
201 }elseif(\class_exists('\UiCore\Portfolio\Frontend') && \UiCore\Portfolio\Frontend::is_portfolio() && !is_singular('portfolio')){
202 $post_id = \UiCore\Portfolio\Frontend::get_portfolio_page_id();
203 }else{
204 $post_id = get_queried_object_id();
205 }
206
207 return $post_id;
208 }
209
210 /**
211 * Return a list of textual html tags for Elementor Controls Options
212 */
213 public static function get_title_tags() {
214
215 $tags = [
216 'h1' => 'H1',
217 'h2' => 'H2',
218 'h3' => 'H3',
219 'h4' => 'H4',
220 'h5' => 'H5',
221 'h6' => 'H6',
222 'div' => 'div',
223 'span' => 'span',
224 'p' => 'p',
225 ];
226
227 return $tags;
228 }
229
230
231 /**
232 * Formats a date meta value according to the specified format.
233 *
234 * @param mixed $date The date value to format.
235 * @param string $format The format to use for formatting the date. Can be 'custom' or a predefined format.
236 * @param string $custom The custom format to use if $format is set to 'custom'.
237 *
238 * @return string The formatted date value.
239 */
240 public static function format_date($date, $format, $custom) {
241
242 if ( 'custom' === $format ) {
243 $date_format = $custom;
244 } else if ('default' === $format ) {
245 $date_format = get_option('date_format');
246 } else {
247 $date_format = $format;
248 }
249
250 $value = date_i18n($date_format, $date);
251
252 return wp_kses_post($value);
253 }
254
255 /**
256 * Sanitizes SVG content, also allowing `post` tags and atts. Since is a custom sanitizer, require us,
257 * by wp plugin repo security standards, to add `//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped` comment.
258 *
259 * @param string $svg The raw SVG content to be sanitized.
260 * @return string The sanitized SVG content, with only allowed tags and attributes.
261 *
262 * @since 1.0.2
263 */
264 public static function esc_svg($svg) {
265 $default = wp_kses_allowed_html( 'post' );
266
267 $args = array(
268 'svg' => array(
269 'class' => true,
270 'aria-hidden' => true,
271 'aria-labelledby' => true,
272 'role' => true,
273 'xmlns' => true,
274 'width' => true,
275 'height' => true,
276 // has to be lowercase
277 'viewbox' => true,
278 'preserveaspectratio' => true
279 ),
280 'g' => array( 'fill' => true ),
281 'title' => array( 'title' => true ),
282 'path' => array(
283 'd' => true,
284 'fill' => true
285 )
286 );
287 $allowed_tags = array_merge( $default, $args );
288
289 return wp_kses( $svg, $allowed_tags );
290 }
291
292 /**
293 * Sanitizes text strings, but allowing some html tags usefull for styling and manipulating texts. Since is a custom sanitizer,
294 * require us, by wp plugin repo security standards, to add `//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped` comment.
295 *
296 * @param string $content The content to be sanitized
297 * @return string The sanitized string content.
298 *
299 * @since 1.0.3
300 */
301 public static function esc_string($content) {
302
303 $allowed_tags = [
304 'strong' => array(),
305 'em' => array(),
306 'b' => array(),
307 'i' => array(),
308 'u' => array(),
309 's' => array(),
310 'sub' => array(),
311 'sup' => array(),
312 'span' => array(),
313 'br' => array(),
314 'a' => [
315 'href' => true,
316 'title' => true,
317 'target' => true,
318 'rel' => true
319 ]
320 ];
321
322 return wp_kses( $content, $allowed_tags );
323 }
324
325 /**
326 * Retrieves the available image sizes.
327 *
328 * @return array An array of image sizes.
329 *
330 * @since 1.0.0
331 */
332 public static function get_images_sizes() {
333 $sizes = [];
334 foreach (get_intermediate_image_sizes() as $size) {
335 $sizes[$size] = $size;
336 }
337 return $sizes;
338 }
339
340 /**
341 * @since 1.0.5
342 */
343 private static function get_element_recursive($elements, $form_id) {
344
345 foreach ($elements as $element) {
346 if ($form_id === $element['id']) {
347 return $element;
348 }
349
350 if (!empty($element['elements'])) {
351 $element = self::get_element_recursive($element['elements'], $form_id);
352
353 if ($element) {
354 return $element;
355 }
356 }
357 }
358
359 return false;
360 }
361
362 /**
363 * Retrieves the settings of a specific widget without relying on transient data.
364 *
365 * @param int $post_id The ID of the post or page.
366 * @param int $widget_id The ID of the widget.
367 * @return array|string The settings of the widget, or an error message if the request is invalid.
368 *
369 * @since 1.0.5
370 * @deprecated Deprecated since version 1.0.11 Use the transient method instead.
371 */
372 public static function get_widget_settings($post_id, $widget_id) {
373
374 if (!$post_id || !$widget_id) {
375 return false;
376 }
377
378 $elementor = Plugin::$instance;
379 $pageMeta = $elementor->documents->get($post_id);
380
381 if (!$pageMeta) {
382 return false;
383 }
384 $metaData = $pageMeta->get_elements_data();
385 if (!$metaData) {
386 return false;
387 }
388
389 $widget_data = self::get_element_recursive($metaData, $widget_id);
390 $settings = [];
391
392 if (is_array($widget_data)) {
393 $widget = $elementor->elements_manager->create_element_instance($widget_data);
394 $settings = $widget->get_settings();
395 }
396
397 return $settings;
398 }
399
400 /**
401 * Get the `posts per page` value from Framework or WordPress settings last case scenario.
402 * Usefull if we're using `current` post type and don't have posts per page option.
403 *
404 * @param string $post_type The post type slug.
405 *
406 * @since 1.2.1
407 * @return int The number of posts per page.
408 */
409 public static function get_framework_visible_posts( string $post_type )
410 {
411 // Get from Uicore Framework, if available
412 if(defined('UICORE_ASSETS')){
413
414 switch($post_type){
415 case 'product':
416 return \Uicore\Helper::get_option('woocommerce_posts_number');
417
418 case 'post':
419 return \Uicore\Helper::get_option('blog_posts_number');
420
421 case 'portfolio':
422 return \Uicore\Helper::get_option('portfolio_posts_number');
423
424 default:
425 return get_option('posts_per_page');
426
427 }
428 }
429
430 return get_option('posts_per_page');
431 }
432
433 }
434