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

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

380 lines 11.2 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
62 $categories = get_the_terms( $post->ID, $name );
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_reading_time()
78 {
79 global $post;
80 // get the content
81 $the_content = $post->post_content;
82 // count the number of words
83 $words = str_word_count( wp_strip_all_tags( $the_content ) );
84 // rounding off and deviding per 200 words per minute
85 $minute = floor( $words / 200 );
86
87 // calculate the amount of time needed to read
88 return $minute;
89 }
90
91 static function get_site_domain() {
92 return str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
93 }
94
95 /**
96 * Returns the Uicore Elements settings page URL. You may pass a message so it'll be wrapped under a <a> tag.
97 *
98 * @param string $message Optional. A clickable message to be displayed that'll redirect, in a new tab, to settings page.
99 * @return string Uicore Elements settings page URL or an <a> tag HTML with the url and the passed message.
100 */
101 static function get_admin_settings_url(string $message = '') {
102 $url = admin_url( 'options-general.php?page=uicore-elements' );
103 return !empty($message) ? '<a href="'.esc_url($url).'" target="_blank">' . esc_html($message) . '</a>' : $url;
104 }
105
106 static function register_widget_style($name,$deps=[],$external=false)
107 {
108 $handle = (!$external ? 'ui-e-' : '' ). $name;
109 wp_register_style($handle, UICORE_ELEMENTS_ASSETS . '/css/elements/'.$name.'.css',$deps,UICORE_ELEMENTS_VERSION);
110 return $handle;
111 }
112 static function register_widget_script($name,$deps=[],$external=false)
113 {
114 $handle = (!$external ? 'ui-e-' : '' ). $name;
115 //if name contains / then we need to set a custom path
116 if(strpos($name,'/') !== false){
117 $path ='';
118 }else{
119 $path = 'elements/';
120 }
121 wp_register_script($handle, UICORE_ELEMENTS_ASSETS . '/js/'.$path.$name.'.js',$deps,UICORE_ELEMENTS_VERSION,true);
122 return $handle;
123 }
124
125 public static function get_related($filter, $number)
126 {
127 global $post;
128
129 $args = [];
130
131 if ($filter == 'category') {
132 $categories = get_the_category($post->ID);
133
134 if ($categories) {
135 $category_ids = array_map(fn($cat) => $cat->term_id, $categories);
136
137 // basic args
138 $args = [
139 'post__not_in' => [$post->ID],
140 'posts_per_page' => $number,
141 'category__in' => $category_ids,
142 'ignore_sticky_posts' => 1,
143 ];
144 }
145
146 } elseif ($filter == 'tag') {
147 $tags = wp_get_post_tags($post->ID);
148
149 if ($tags) {
150 $tag_ids = array_map(fn($tag) => $tag->term_id, $tags);
151
152 $args = [
153 'post__not_in' => [$post->ID],
154 'posts_per_page' => $number,
155 'tag__in' => $tag_ids,
156 'ignore_sticky_posts' => 1,
157 ];
158 }
159
160 // default/random filter
161 } else {
162 $args = [
163 'post__not_in' => [$post->ID],
164 'posts_per_page' => $number,
165 'orderby' => 'rand',
166 ];
167 }
168
169 $related_query = new \WP_Query($args);
170 return $related_query->have_posts() ? $related_query : false;
171 }
172
173 public static function get_product_related($limit)
174 {
175 global $post;
176
177 return wc_get_related_products($post->ID, $limit);
178 }
179
180 /*
181 * Get the current post id (used in Theme Builder - UiCore Framework)
182 */
183 static function get_current_meta_id()
184 {
185 if(\class_exists('\UiCore\Blog\Frontend') && \UiCore\Blog\Frontend::is_blog() && !is_singular('post')){
186 $post_id = get_option('page_for_posts', true);
187 }elseif(\class_exists('\UiCore\Portfolio\Frontend') && \UiCore\Portfolio\Frontend::is_portfolio() && !is_singular('portfolio')){
188 $post_id = \UiCore\Portfolio\Frontend::get_portfolio_page_id();
189 }else{
190 $post_id = get_queried_object_id();
191 }
192
193 return $post_id;
194 }
195
196 /**
197 * Return a list of textual html tags for Elementor Controls Options
198 */
199
200 public static function get_title_tags($type = null) {
201
202 $tags = [
203 'h1' => 'H1',
204 'h2' => 'H2',
205 'h3' => 'H3',
206 'h4' => 'H4',
207 'h5' => 'H5',
208 'h6' => 'H6',
209 'div' => 'div',
210 'span' => 'span',
211 'p' => 'p',
212 ];
213
214 return $tags;
215 }
216
217
218 /**
219 * Formats a date meta value according to the specified format.
220 *
221 * @param mixed $date The date value to format.
222 * @param string $format The format to use for formatting the date. Can be 'custom' or a predefined format.
223 * @param string $custom The custom format to use if $format is set to 'custom'.
224 *
225 * @return string The formatted date value.
226 */
227 public static function format_date($date, $format, $custom) {
228
229 if ( 'custom' === $format ) {
230 $date_format = $custom;
231 } else if ('default' === $format ) {
232 $date_format = get_option('date_format');
233 } else {
234 $date_format = $format;
235 }
236
237 $value = date_i18n($date_format, $date);
238
239 return wp_kses_post($value);
240 }
241
242 /**
243 * Sanitizes SVG content, also allowing `post` tags and atts.
244 *
245 * @param string $svg The raw SVG content to be sanitized.
246 * @return string The sanitized SVG content, with only allowed tags and attributes.
247 *
248 * @since 1.0.2
249 */
250 public static function esc_svg($svg) {
251 $default = wp_kses_allowed_html( 'post' );
252
253 $args = array(
254 'svg' => array(
255 'class' => true,
256 'aria-hidden' => true,
257 'aria-labelledby' => true,
258 'role' => true,
259 'xmlns' => true,
260 'width' => true,
261 'height' => true,
262 // has to be lowercase
263 'viewbox' => true,
264 'preserveaspectratio' => true
265 ),
266 'g' => array( 'fill' => true ),
267 'title' => array( 'title' => true ),
268 'path' => array(
269 'd' => true,
270 'fill' => true
271 )
272 );
273 $allowed_tags = array_merge( $default, $args );
274
275 return wp_kses( $svg, $allowed_tags );
276 }
277
278 /**
279 * Sanitizes text strings, but allowing some html tags usefull for styling and manipulating texts.
280 *
281 * @param string $content The content to be sanitized
282 * @return string The sanitized string content.
283 *
284 * @since 1.0.3
285 */
286 public static function esc_string($content) {
287
288 $allowed_tags = [
289 'strong' => array(),
290 'em' => array(),
291 'b' => array(),
292 'i' => array(),
293 'u' => array(),
294 's' => array(),
295 'sub' => array(),
296 'sup' => array(),
297 'span' => array(),
298 'br' => array()
299 ];
300
301 return wp_kses( $content, $allowed_tags );
302 }
303
304 /**
305 * Retrieves the available image sizes.
306 *
307 * @return array An array of image sizes.
308 *
309 * @since 1.0.0
310 */
311 public static function get_images_sizes() {
312 $sizes = [];
313 foreach (get_intermediate_image_sizes() as $size) {
314 $sizes[$size] = $size;
315 }
316 return $sizes;
317 }
318
319 /**
320 * @since 1.0.5
321 */
322 private static function get_element_recursive($elements, $form_id) {
323
324 foreach ($elements as $element) {
325 if ($form_id === $element['id']) {
326 return $element;
327 }
328
329 if (!empty($element['elements'])) {
330 $element = self::get_element_recursive($element['elements'], $form_id);
331
332 if ($element) {
333 return $element;
334 }
335 }
336 }
337
338 return false;
339 }
340
341 /**
342 * Retrieves the settings of a specific widget without relying on transient data.
343 *
344 * @param int $post_id The ID of the post or page.
345 * @param int $widget_id The ID of the widget.
346 * @return array|string The settings of the widget, or an error message if the request is invalid.
347 *
348 * @since 1.0.5
349 * @deprecated Deprecated since version 1.0.11 Use the transient method instead.
350 */
351 public static function get_widget_settings($post_id, $widget_id) {
352
353 if (!$post_id || !$widget_id) {
354 return false;
355 }
356
357 $elementor = Plugin::$instance;
358 $pageMeta = $elementor->documents->get($post_id);
359
360 if (!$pageMeta) {
361 return false;
362 }
363 $metaData = $pageMeta->get_elements_data();
364 if (!$metaData) {
365 return false;
366 }
367
368 $widget_data = self::get_element_recursive($metaData, $widget_id);
369 $settings = [];
370
371 if (is_array($widget_data)) {
372 $widget = $elementor->elements_manager->create_element_instance($widget_data);
373 $settings = $widget->get_settings();
374 }
375
376 return $settings;
377 }
378
379 }
380