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

382 lines 11.3 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 // TODO: problems related to the lack of ID on posts object should be treated here.
178
179 return wc_get_related_products($post->ID, $limit);
180 }
181
182 /*
183 * Get the current post id (used in Theme Builder - UiCore Framework)
184 */
185 static function get_current_meta_id()
186 {
187 if(\class_exists('\UiCore\Blog\Frontend') && \UiCore\Blog\Frontend::is_blog() && !is_singular('post')){
188 $post_id = get_option('page_for_posts', true);
189 }elseif(\class_exists('\UiCore\Portfolio\Frontend') && \UiCore\Portfolio\Frontend::is_portfolio() && !is_singular('portfolio')){
190 $post_id = \UiCore\Portfolio\Frontend::get_portfolio_page_id();
191 }else{
192 $post_id = get_queried_object_id();
193 }
194
195 return $post_id;
196 }
197
198 /**
199 * Return a list of textual html tags for Elementor Controls Options
200 */
201
202 public static function get_title_tags($type = null) {
203
204 $tags = [
205 'h1' => 'H1',
206 'h2' => 'H2',
207 'h3' => 'H3',
208 'h4' => 'H4',
209 'h5' => 'H5',
210 'h6' => 'H6',
211 'div' => 'div',
212 'span' => 'span',
213 'p' => 'p',
214 ];
215
216 return $tags;
217 }
218
219
220 /**
221 * Formats a date meta value according to the specified format.
222 *
223 * @param mixed $date The date value to format.
224 * @param string $format The format to use for formatting the date. Can be 'custom' or a predefined format.
225 * @param string $custom The custom format to use if $format is set to 'custom'.
226 *
227 * @return string The formatted date value.
228 */
229 public static function format_date($date, $format, $custom) {
230
231 if ( 'custom' === $format ) {
232 $date_format = $custom;
233 } else if ('default' === $format ) {
234 $date_format = get_option('date_format');
235 } else {
236 $date_format = $format;
237 }
238
239 $value = date_i18n($date_format, $date);
240
241 return wp_kses_post($value);
242 }
243
244 /**
245 * Sanitizes SVG content, also allowing `post` tags and atts.
246 *
247 * @param string $svg The raw SVG content to be sanitized.
248 * @return string The sanitized SVG content, with only allowed tags and attributes.
249 *
250 * @since 1.0.2
251 */
252 public static function esc_svg($svg) {
253 $default = wp_kses_allowed_html( 'post' );
254
255 $args = array(
256 'svg' => array(
257 'class' => true,
258 'aria-hidden' => true,
259 'aria-labelledby' => true,
260 'role' => true,
261 'xmlns' => true,
262 'width' => true,
263 'height' => true,
264 // has to be lowercase
265 'viewbox' => true,
266 'preserveaspectratio' => true
267 ),
268 'g' => array( 'fill' => true ),
269 'title' => array( 'title' => true ),
270 'path' => array(
271 'd' => true,
272 'fill' => true
273 )
274 );
275 $allowed_tags = array_merge( $default, $args );
276
277 return wp_kses( $svg, $allowed_tags );
278 }
279
280 /**
281 * Sanitizes text strings, but allowing some html tags usefull for styling and manipulating texts.
282 *
283 * @param string $content The content to be sanitized
284 * @return string The sanitized string content.
285 *
286 * @since 1.0.3
287 */
288 public static function esc_string($content) {
289
290 $allowed_tags = [
291 'strong' => array(),
292 'em' => array(),
293 'b' => array(),
294 'i' => array(),
295 'u' => array(),
296 's' => array(),
297 'sub' => array(),
298 'sup' => array(),
299 'span' => array(),
300 'br' => array()
301 ];
302
303 return wp_kses( $content, $allowed_tags );
304 }
305
306 /**
307 * Retrieves the available image sizes.
308 *
309 * @return array An array of image sizes.
310 *
311 * @since 1.0.0
312 */
313 public static function get_images_sizes() {
314 $sizes = [];
315 foreach (get_intermediate_image_sizes() as $size) {
316 $sizes[$size] = $size;
317 }
318 return $sizes;
319 }
320
321 /**
322 * @since 1.0.5
323 */
324 private static function get_element_recursive($elements, $form_id) {
325
326 foreach ($elements as $element) {
327 if ($form_id === $element['id']) {
328 return $element;
329 }
330
331 if (!empty($element['elements'])) {
332 $element = self::get_element_recursive($element['elements'], $form_id);
333
334 if ($element) {
335 return $element;
336 }
337 }
338 }
339
340 return false;
341 }
342
343 /**
344 * Retrieves the settings of a specific widget without relying on transient data.
345 *
346 * @param int $post_id The ID of the post or page.
347 * @param int $widget_id The ID of the widget.
348 * @return array|string The settings of the widget, or an error message if the request is invalid.
349 *
350 * @since 1.0.5
351 * @deprecated Deprecated since version 1.0.11 Use the transient method instead.
352 */
353 public static function get_widget_settings($post_id, $widget_id) {
354
355 if (!$post_id || !$widget_id) {
356 return false;
357 }
358
359 $elementor = Plugin::$instance;
360 $pageMeta = $elementor->documents->get($post_id);
361
362 if (!$pageMeta) {
363 return false;
364 }
365 $metaData = $pageMeta->get_elements_data();
366 if (!$metaData) {
367 return false;
368 }
369
370 $widget_data = self::get_element_recursive($metaData, $widget_id);
371 $settings = [];
372
373 if (is_array($widget_data)) {
374 $widget = $elementor->elements_manager->create_element_instance($widget_data);
375 $settings = $widget->get_settings();
376 }
377
378 return $settings;
379 }
380
381 }
382