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