PluginProbe
UiCore Elements – Free widgets and templates for Elementor / 1.2.0
UiCore Elements – Free widgets and templates for Elementor v1.2.0
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.0, at includes/class-helper.php

394 lines 11.5 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 // get the content
95 $the_content = $post->post_content;
96 // count the number of words
97 $words = str_word_count( wp_strip_all_tags( $the_content ) );
98 // rounding off and deviding per 200 words per minute
99 $minute = floor( $words / 200 );
100
101 // calculate the amount of time needed to read
102 return $minute;
103 }
104
105 static function get_site_domain() {
106 return str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
107 }
108
109 /**
110 * Returns the Uicore Elements settings page URL. You may pass a message so it'll be wrapped under a <a> tag.
111 *
112 * @param string $message Optional. A clickable message to be displayed that'll redirect, in a new tab, to settings page.
113 * @return string Uicore Elements settings page URL or an <a> tag HTML with the url and the passed message.
114 */
115 static function get_admin_settings_url(string $message = '') {
116 $url = admin_url( 'options-general.php?page=uicore-elements' );
117 return !empty($message) ? '<a href="'.esc_url($url).'" target="_blank">' . esc_html($message) . '</a>' : $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
214 public static function get_title_tags($type = null) {
215
216 $tags = [
217 'h1' => 'H1',
218 'h2' => 'H2',
219 'h3' => 'H3',
220 'h4' => 'H4',
221 'h5' => 'H5',
222 'h6' => 'H6',
223 'div' => 'div',
224 'span' => 'span',
225 'p' => 'p',
226 ];
227
228 return $tags;
229 }
230
231
232 /**
233 * Formats a date meta value according to the specified format.
234 *
235 * @param mixed $date The date value to format.
236 * @param string $format The format to use for formatting the date. Can be 'custom' or a predefined format.
237 * @param string $custom The custom format to use if $format is set to 'custom'.
238 *
239 * @return string The formatted date value.
240 */
241 public static function format_date($date, $format, $custom) {
242
243 if ( 'custom' === $format ) {
244 $date_format = $custom;
245 } else if ('default' === $format ) {
246 $date_format = get_option('date_format');
247 } else {
248 $date_format = $format;
249 }
250
251 $value = date_i18n($date_format, $date);
252
253 return wp_kses_post($value);
254 }
255
256 /**
257 * Sanitizes SVG content, also allowing `post` tags and atts.
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.
294 *
295 * @param string $content The content to be sanitized
296 * @return string The sanitized string content.
297 *
298 * @since 1.0.3
299 */
300 public static function esc_string($content) {
301
302 $allowed_tags = [
303 'strong' => array(),
304 'em' => array(),
305 'b' => array(),
306 'i' => array(),
307 'u' => array(),
308 's' => array(),
309 'sub' => array(),
310 'sup' => array(),
311 'span' => array(),
312 'br' => array()
313 ];
314
315 return wp_kses( $content, $allowed_tags );
316 }
317
318 /**
319 * Retrieves the available image sizes.
320 *
321 * @return array An array of image sizes.
322 *
323 * @since 1.0.0
324 */
325 public static function get_images_sizes() {
326 $sizes = [];
327 foreach (get_intermediate_image_sizes() as $size) {
328 $sizes[$size] = $size;
329 }
330 return $sizes;
331 }
332
333 /**
334 * @since 1.0.5
335 */
336 private static function get_element_recursive($elements, $form_id) {
337
338 foreach ($elements as $element) {
339 if ($form_id === $element['id']) {
340 return $element;
341 }
342
343 if (!empty($element['elements'])) {
344 $element = self::get_element_recursive($element['elements'], $form_id);
345
346 if ($element) {
347 return $element;
348 }
349 }
350 }
351
352 return false;
353 }
354
355 /**
356 * Retrieves the settings of a specific widget without relying on transient data.
357 *
358 * @param int $post_id The ID of the post or page.
359 * @param int $widget_id The ID of the widget.
360 * @return array|string The settings of the widget, or an error message if the request is invalid.
361 *
362 * @since 1.0.5
363 * @deprecated Deprecated since version 1.0.11 Use the transient method instead.
364 */
365 public static function get_widget_settings($post_id, $widget_id) {
366
367 if (!$post_id || !$widget_id) {
368 return false;
369 }
370
371 $elementor = Plugin::$instance;
372 $pageMeta = $elementor->documents->get($post_id);
373
374 if (!$pageMeta) {
375 return false;
376 }
377 $metaData = $pageMeta->get_elements_data();
378 if (!$metaData) {
379 return false;
380 }
381
382 $widget_data = self::get_element_recursive($metaData, $widget_id);
383 $settings = [];
384
385 if (is_array($widget_data)) {
386 $widget = $elementor->elements_manager->create_element_instance($widget_data);
387 $settings = $widget->get_settings();
388 }
389
390 return $settings;
391 }
392
393 }
394