| 1 |
<?php |
| 2 |
namespace UiCoreElements; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit(); |
| 5 |
/** |
| 6 |
* UiCore Utils Functions |
| 7 |
*/ |
| 8 |
class Helper |
| 9 |
{ |
| 10 |
|
| 11 |
public static function get_separator() |
| 12 |
{ |
| 13 |
return '<span class="uicore-meta-separator"></span>'; |
| 14 |
} |
| 15 |
|
| 16 |
public static function get_taxonomies($custom = true) |
| 17 |
{ |
| 18 |
$taxonomies = get_taxonomies(['public' => true], 'objects'); |
| 19 |
$exclusions = ['nav_menu', 'link_category', 'post_format']; // Exclude these taxonomies from the list |
| 20 |
|
| 21 |
$taxonomies = array_filter($taxonomies, function ($taxonomy) use ($exclusions) { |
| 22 |
return !in_array($taxonomy->name, $exclusions); |
| 23 |
}); |
| 24 |
|
| 25 |
$taxonomies = array_map(function ($taxonomy) { |
| 26 |
if('portfolio_category' === $taxonomy->name){ |
| 27 |
return 'Portfolio Category'; |
| 28 |
} |
| 29 |
return $taxonomy->label; |
| 30 |
}, $taxonomies); |
| 31 |
|
| 32 |
if ($custom) { |
| 33 |
$taxonomies = array_merge(['custom' => __('Custom Meta', 'uicore-elements')], $taxonomies); |
| 34 |
} |
| 35 |
|
| 36 |
return $taxonomies; |
| 37 |
} |
| 38 |
|
| 39 |
static function get_taxonomy($name) |
| 40 |
{ |
| 41 |
global $post; |
| 42 |
|
| 43 |
$categories = get_the_terms( $post->ID, $name ); |
| 44 |
if ( ! $categories || is_wp_error( $categories ) ) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
$categories = array_values( $categories ); |
| 49 |
foreach ($categories as $t) { |
| 50 |
$term_name[] = |
| 51 |
'<a href="' . get_term_link($t) . '" title="View ' . \esc_attr($t->name) . ' posts">' . esc_html($t->name) . '</a>'; |
| 52 |
} |
| 53 |
$category = implode(', ', $term_name); |
| 54 |
|
| 55 |
return $category; |
| 56 |
} |
| 57 |
|
| 58 |
static function get_reading_time() |
| 59 |
{ |
| 60 |
global $post; |
| 61 |
// get the content |
| 62 |
$the_content = $post->post_content; |
| 63 |
// count the number of words |
| 64 |
$words = str_word_count( wp_strip_all_tags( $the_content ) ); |
| 65 |
// rounding off and deviding per 200 words per minute |
| 66 |
$minute = floor( $words / 200 ); |
| 67 |
|
| 68 |
// calculate the amount of time needed to read |
| 69 |
return $minute; |
| 70 |
} |
| 71 |
|
| 72 |
static function register_widget_style($name,$deps=[],$external=false) |
| 73 |
{ |
| 74 |
$handle = (!$external ? 'ui-e-' : '' ). $name; |
| 75 |
wp_register_style($handle, UICORE_ELEMENTS_ASSETS . '/css/elements/'.$name.'.css',$deps,UICORE_ELEMENTS_VERSION); |
| 76 |
return $handle; |
| 77 |
} |
| 78 |
static function register_widget_script($name,$deps=[],$external=false) |
| 79 |
{ |
| 80 |
$handle = (!$external ? 'ui-e-' : '' ). $name; |
| 81 |
wp_register_script($handle, UICORE_ELEMENTS_ASSETS . '/js/elements/'.$name.'.js',$deps,UICORE_ELEMENTS_VERSION,true); |
| 82 |
return $handle; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
public static function get_related($filter, $number) |
| 87 |
{ |
| 88 |
global $post; |
| 89 |
|
| 90 |
$args = []; |
| 91 |
|
| 92 |
if ($filter == 'category') { |
| 93 |
$categories = get_the_category($post->ID); |
| 94 |
|
| 95 |
if ($categories) { |
| 96 |
$category_ids = []; |
| 97 |
foreach ($categories as $individual_category) { |
| 98 |
$category_ids[] = $individual_category->term_id; |
| 99 |
} |
| 100 |
|
| 101 |
$args = [ |
| 102 |
'category__in' => $category_ids, |
| 103 |
'post__not_in' => [$post->ID], |
| 104 |
'posts_per_page' => $number, |
| 105 |
'ignore_sticky_posts' => 1, |
| 106 |
]; |
| 107 |
} |
| 108 |
} elseif ($filter == 'tag') { |
| 109 |
$tags = wp_get_post_tags($post->ID); |
| 110 |
|
| 111 |
if ($tags) { |
| 112 |
$tag_ids = []; |
| 113 |
foreach ($tags as $individual_tag) { |
| 114 |
$tag_ids[] = $individual_tag->term_id; |
| 115 |
} |
| 116 |
$args = [ |
| 117 |
'tag__in' => $tag_ids, |
| 118 |
'post__not_in' => [$post->ID], |
| 119 |
'posts_per_page' => $number, |
| 120 |
'ignore_sticky_posts' => 1, |
| 121 |
]; |
| 122 |
} |
| 123 |
} else { |
| 124 |
$args = [ |
| 125 |
'post__not_in' => [$post->ID], |
| 126 |
'posts_per_page' => $number, |
| 127 |
'orderby' => 'rand', |
| 128 |
]; |
| 129 |
} |
| 130 |
|
| 131 |
$related_query = new \wp_query($args); |
| 132 |
|
| 133 |
if ($related_query->have_posts()) { |
| 134 |
return $related_query; |
| 135 |
} else { |
| 136 |
return false; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/* |
| 142 |
* Get the current post id (used in Theme Builder - UiCore Framework) |
| 143 |
*/ |
| 144 |
static function get_current_meta_id() |
| 145 |
{ |
| 146 |
if(\class_exists('\UiCore\Blog\Frontend') && \UiCore\Blog\Frontend::is_blog() && !is_singular('post')){ |
| 147 |
$post_id = get_option('page_for_posts', true); |
| 148 |
}elseif(\class_exists('\UiCore\Portfolio\Frontend') && \UiCore\Portfolio\Frontend::is_portfolio() && !is_singular('portfolio')){ |
| 149 |
$post_id = \UiCore\Portfolio\Frontend::get_portfolio_page_id(); |
| 150 |
}else{ |
| 151 |
$post_id = get_queried_object_id(); |
| 152 |
} |
| 153 |
|
| 154 |
return $post_id; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Return a list of textual html tags for Elementor Controls Options |
| 159 |
*/ |
| 160 |
|
| 161 |
public static function get_title_tags($type = null) { |
| 162 |
|
| 163 |
$tags = [ |
| 164 |
'h1' => 'H1', |
| 165 |
'h2' => 'H2', |
| 166 |
'h3' => 'H3', |
| 167 |
'h4' => 'H4', |
| 168 |
'h5' => 'H5', |
| 169 |
'h6' => 'H6', |
| 170 |
'div' => 'div', |
| 171 |
'span' => 'span', |
| 172 |
'p' => 'p', |
| 173 |
]; |
| 174 |
|
| 175 |
return $tags; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* Formats a date meta value according to the specified format. |
| 181 |
* |
| 182 |
* @param mixed $date The date value to format. |
| 183 |
* @param string $format The format to use for formatting the date. Can be 'custom' or a predefined format. |
| 184 |
* @param string $custom The custom format to use if $format is set to 'custom'. |
| 185 |
* |
| 186 |
* @return string The formatted date value. |
| 187 |
*/ |
| 188 |
public static function format_date($date, $format, $custom) { |
| 189 |
|
| 190 |
if ( 'custom' === $format ) { |
| 191 |
$date_format = $custom; |
| 192 |
} else { |
| 193 |
$date_format = $format; |
| 194 |
|
| 195 |
if ( 'default' === $date_format ) { |
| 196 |
$date_format = get_option('date_format'); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
$value = date_i18n($date_format, $date); |
| 201 |
|
| 202 |
return wp_kses_post($value); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Sanitizes SVG content |
| 207 |
* |
| 208 |
* @param string $svg The raw SVG content to be sanitized. |
| 209 |
* @return string The sanitized SVG content, with only allowed tags and attributes. |
| 210 |
* |
| 211 |
* @since 1.0.2 |
| 212 |
*/ |
| 213 |
public static function esc_svg($svg) { |
| 214 |
$default = wp_kses_allowed_html( 'post' ); |
| 215 |
|
| 216 |
$args = array( |
| 217 |
'svg' => array( |
| 218 |
'class' => true, |
| 219 |
'aria-hidden' => true, |
| 220 |
'aria-labelledby' => true, |
| 221 |
'role' => true, |
| 222 |
'xmlns' => true, |
| 223 |
'width' => true, |
| 224 |
'height' => true, |
| 225 |
// has to be lowercase |
| 226 |
'viewbox' => true, |
| 227 |
'preserveaspectratio' => true |
| 228 |
), |
| 229 |
'g' => array( 'fill' => true ), |
| 230 |
'title' => array( 'title' => true ), |
| 231 |
'path' => array( |
| 232 |
'd' => true, |
| 233 |
'fill' => true |
| 234 |
) |
| 235 |
); |
| 236 |
$allowed_tags = array_merge( $default, $args ); |
| 237 |
|
| 238 |
return wp_kses( $svg, $allowed_tags ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Sanitizes text strings, but allowing some html tags usefull for styling and manipulating texts. |
| 243 |
* |
| 244 |
* @param string $content The content to be sanitized |
| 245 |
* @return string The sanitized string content. |
| 246 |
* |
| 247 |
* @since 1.0.3 |
| 248 |
*/ |
| 249 |
public static function esc_string($content) { |
| 250 |
|
| 251 |
$allowed_tags = [ |
| 252 |
'strong' => array(), |
| 253 |
'em' => array(), |
| 254 |
'b' => array(), |
| 255 |
'i' => array(), |
| 256 |
'u' => array(), |
| 257 |
's' => array(), |
| 258 |
'sub' => array(), |
| 259 |
'sup' => array(), |
| 260 |
'span' => array(), |
| 261 |
'br' => array() |
| 262 |
]; |
| 263 |
|
| 264 |
return wp_kses( $content, $allowed_tags ); |
| 265 |
} |
| 266 |
|
| 267 |
} |
| 268 |
|