| 1 |
<?php |
| 2 |
namespace ULTP; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
class Functions{ |
| 7 |
|
| 8 |
public function __construct(){ |
| 9 |
$GLOBALS['ultp_settings'] = get_option('ultp_options'); |
| 10 |
} |
| 11 |
|
| 12 |
public function reusable_id($post_id){ |
| 13 |
$reusable_id = array(); |
| 14 |
if($post_id){ |
| 15 |
$post = get_post($post_id); |
| 16 |
if (has_blocks($post->post_content)) { |
| 17 |
$blocks = parse_blocks($post->post_content); |
| 18 |
foreach ($blocks as $key => $value) { |
| 19 |
if(isset($value['attrs']['ref'])) { |
| 20 |
$reusable_id[] = $value['attrs']['ref']; |
| 21 |
} |
| 22 |
} |
| 23 |
} |
| 24 |
} |
| 25 |
return $reusable_id; |
| 26 |
} |
| 27 |
|
| 28 |
public function set_css_style($post_id){ |
| 29 |
if( $post_id ){ |
| 30 |
$upload_dir_url = wp_get_upload_dir(); |
| 31 |
$upload_css_dir_url = trailingslashit( $upload_dir_url['basedir'] ); |
| 32 |
$css_dir_path = $upload_css_dir_url . "ultimate-post/ultp-css-{$post_id}.css"; |
| 33 |
|
| 34 |
$css_dir_url = trailingslashit( $upload_dir_url['baseurl'] ); |
| 35 |
if (is_ssl()) { |
| 36 |
$css_dir_url = str_replace('http://', 'https://', $css_dir_url); |
| 37 |
} |
| 38 |
|
| 39 |
// Reusable CSS |
| 40 |
$reusable_id = ultimate_post()->reusable_id($post_id); |
| 41 |
foreach ( $reusable_id as $id ) { |
| 42 |
$reusable_dir_path = $upload_css_dir_url."ultimate-post/ultp-css-{$id}.css"; |
| 43 |
if (file_exists( $reusable_dir_path )) { |
| 44 |
$css_url = $css_dir_url . "ultimate-post/ultp-css-{$id}.css"; |
| 45 |
wp_enqueue_style( "ultp-post-{$id}", $css_url, array(), ULTP_VER, 'all' ); |
| 46 |
}else{ |
| 47 |
$css = get_post_meta($id, '_ultp_css', true); |
| 48 |
if( $css ) { |
| 49 |
wp_enqueue_style("ultp-post-{$id}", $css, false, ULTP_VER); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if ( file_exists( $css_dir_path ) ) { |
| 55 |
$css_url = $css_dir_url . "ultimate-post/ultp-css-{$post_id}.css"; |
| 56 |
wp_enqueue_style( "ultp-post-{$post_id}", $css_url, array(), ULTP_VER, 'all' ); |
| 57 |
} else { |
| 58 |
$css = get_post_meta($post_id, '_ultp_css', true); |
| 59 |
if( $css ) { |
| 60 |
wp_enqueue_style("ultp-post-{$post_id}", $css, false, ULTP_VER); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public function get_setting($key = ''){ |
| 67 |
$data = $GLOBALS['ultp_settings']; |
| 68 |
if ($key != '') { |
| 69 |
return isset($data[$key]) ? $data[$key] : ''; |
| 70 |
} else { |
| 71 |
return $data; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public function set_setting($key = '', $val = '') { |
| 76 |
if($key != ''){ |
| 77 |
$data = $GLOBALS['ultp_settings']; |
| 78 |
$data[$key] = $val; |
| 79 |
update_option('ultp_options', $data); |
| 80 |
$GLOBALS['ultp_settings'] = $data; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public function get_image_html($url = '', $size = 'full', $class = '', $alt = ''){ |
| 85 |
$alt = $alt ? ' alt="'.$alt.'" ' : ''; |
| 86 |
if( function_exists('ultimate_post_pro') ){ |
| 87 |
$addon_enable = ultimate_post()->get_setting(); |
| 88 |
if(isset($addon_enable['addons_imageloading']) && $addon_enable['addons_imageloading'] == 'true'){ |
| 89 |
$class = $class ? ' class="ultp-lazy '.$class.'" ' : ' class="ultp-lazy" '; |
| 90 |
return '<img loading="lazy" loading="lazy" '.$class.$alt.' '.ultimate_post_pro()->get_size($size).' src="'.ultimate_post_pro()->img_placeholder($size).'" data-src="'.$url.'"/>'; |
| 91 |
}else{ |
| 92 |
$class = $class ? ' class="'.$class.'" ' : ''; |
| 93 |
return '<img loading="lazy" '.$class.$alt.' src="'.$url.'" />'; |
| 94 |
} |
| 95 |
} else { |
| 96 |
$class = $class ? ' class="'.$class.'" ' : ''; |
| 97 |
return '<img loading="lazy" '.$class.$alt.' src="'.$url.'" />'; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public function get_image($attach_id, $size = 'full', $class = '', $alt = ''){ |
| 102 |
$alt = $alt ? ' alt="'.$alt.'" ' : ''; |
| 103 |
if( function_exists('ultimate_post_pro') ){ |
| 104 |
$addon_enable = ultimate_post()->get_setting(); |
| 105 |
if(isset($addon_enable['addons_imageloading']) && $addon_enable['addons_imageloading'] == 'true'){ |
| 106 |
$class = $class ? ' class="ultp-lazy '.$class.'" ' : ' class="ultp-lazy" '; |
| 107 |
return '<img loading="lazy" '.$class.$alt.' '.ultimate_post_pro()->get_size($size).' src="'.ultimate_post_pro()->img_placeholder($size).'" data-src="'.wp_get_attachment_image_url( $attach_id, $size ).'"/>'; |
| 108 |
}else{ |
| 109 |
$class = $class ? ' class="'.$class.'" ' : ''; |
| 110 |
return '<img loading="lazy" '.$class.$alt.' src="'.wp_get_attachment_image_url( $attach_id, $size ).'" />'; |
| 111 |
} |
| 112 |
} else { |
| 113 |
$class = $class ? ' class="'.$class.'" ' : ''; |
| 114 |
return '<img loading="lazy" '.$class.$alt.' src="'.wp_get_attachment_image_url( $attach_id, $size ).'" />'; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// Init data |
| 119 |
public function init_set_data(){ |
| 120 |
$data = get_option( 'ultp_options', array() ); |
| 121 |
$init_data = array( |
| 122 |
'css_save_as' => 'wp_head', |
| 123 |
'preloader_style' => 'style1', |
| 124 |
'preloader_color' => '#1740f5', |
| 125 |
'container_width' => '1140', |
| 126 |
'editor_container' => 'full_width', |
| 127 |
'hide_import_btn' => '', |
| 128 |
'ultp_templates' => 'true', |
| 129 |
'ultp_elementor' => 'true' |
| 130 |
); |
| 131 |
if (empty($data)) { |
| 132 |
update_option('ultp_options', $init_data); |
| 133 |
$GLOBALS['wopb_settings'] = $init_data; |
| 134 |
} else { |
| 135 |
foreach ($init_data as $key => $single) { |
| 136 |
if (!isset($data[$key])) { |
| 137 |
$data[$key] = $single; |
| 138 |
} |
| 139 |
} |
| 140 |
update_option('ultp_options', $data); |
| 141 |
$GLOBALS['wopb_settings'] = $data; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Excerpt |
| 146 |
public function excerpt( $post_id, $limit = 55 ) { |
| 147 |
return apply_filters( 'the_excerpt', wp_trim_words( get_the_content( $post_id ) , $limit ) ); |
| 148 |
} |
| 149 |
|
| 150 |
// Query Builder |
| 151 |
public function get_query($attr) { |
| 152 |
$query_args = array( |
| 153 |
'posts_per_page' => isset($attr['queryNumber']) ? $attr['queryNumber'] : 3, |
| 154 |
'post_type' => isset($attr['queryType']) ? $attr['queryType'] : 'post', |
| 155 |
'orderby' => isset($attr['queryOrderBy']) ? $attr['queryOrderBy'] : 'date', |
| 156 |
'order' => isset($attr['queryOrder']) ? $attr['queryOrder'] : 'desc', |
| 157 |
'paged' => isset($attr['paged']) ? $attr['paged'] : 1, |
| 158 |
'post_status' => 'publish' |
| 159 |
); |
| 160 |
|
| 161 |
if(isset($attr['queryOrderBy']) && isset($attr['metaKey'])){ |
| 162 |
if($attr['queryOrderBy'] == 'meta_value_num') { |
| 163 |
$query_args['meta_key'] = $attr['metaKey']; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if(isset($attr['queryInclude']) && $attr['queryInclude']){ |
| 168 |
$query_args['post__in'] = explode(',', $attr['queryInclude']); |
| 169 |
$query_args['ignore_sticky_posts'] = 1; |
| 170 |
$query_args['orderby'] = 'post__in'; |
| 171 |
} |
| 172 |
|
| 173 |
if(isset($attr['queryExclude']) && $attr['queryExclude']){ |
| 174 |
$query_args['post__not_in'] = explode(',', $attr['queryExclude']); |
| 175 |
} |
| 176 |
|
| 177 |
if(isset($attr['queryTax'])){ |
| 178 |
if(isset($attr['queryCat'])){ |
| 179 |
if($attr['queryTax'] == 'category' && !empty($attr['queryCat'])){ |
| 180 |
$var = array('relation'=>'OR'); |
| 181 |
foreach (json_decode($attr['queryCat']) as $val) { |
| 182 |
$var[] = array('taxonomy'=>'category', 'field' => 'slug', 'terms' => $val ); |
| 183 |
} |
| 184 |
if(count($var) > 1){ |
| 185 |
$query_args['tax_query'] = $var; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
if(isset($attr['queryTag'])){ |
| 190 |
if($attr['queryTax'] == 'post_tag' && !empty($attr['queryTag'])){ |
| 191 |
$var = array('relation'=>'OR'); |
| 192 |
foreach (json_decode($attr['queryTag']) as $val) { |
| 193 |
$var[] = array('taxonomy'=>'post_tag', 'field' => 'slug', 'terms' => $val ); |
| 194 |
} |
| 195 |
$query_args['tax_query'] = $var; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if(isset($attr['queryQuick'])){ |
| 201 |
if($attr['queryQuick'] != ''){ |
| 202 |
if(function_exists('ultimate_post_pro')){ |
| 203 |
$query_args = ultimate_post_pro()->get_quick_query($attr, $query_args); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
if(isset($attr['queryOffset']) && $attr['queryOffset'] ){ |
| 209 |
if($query_args['paged'] > 1){ |
| 210 |
$offset_post = wp_get_recent_posts($query_args, OBJECT); |
| 211 |
if( count($offset_post) > 0 ){ |
| 212 |
$offset = array(); |
| 213 |
for($x = count($offset_post); $x > count($offset_post) - $attr['queryOffset']; $x--){ |
| 214 |
$offset[] = $offset_post[$x-1]->ID; |
| 215 |
} |
| 216 |
$query_args['post__not_in'] = $offset; |
| 217 |
} |
| 218 |
}else{ |
| 219 |
$query_args['offset'] = isset($attr['queryOffset']) ? $attr['queryOffset'] : 0; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
$query_args['wpnonce'] = wp_create_nonce( 'ultp-nonce' ); |
| 224 |
|
| 225 |
return $query_args; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
public function get_page_number($attr, $post_number) { |
| 230 |
if($post_number > 0){ |
| 231 |
$post_per_page = isset($attr['queryNumber']) ? $attr['queryNumber'] : 3; |
| 232 |
$pages = ceil($post_number/$post_per_page); |
| 233 |
return $pages ? $pages : 1; |
| 234 |
}else{ |
| 235 |
return 1; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
public function get_image_size() { |
| 240 |
$sizes = get_intermediate_image_sizes(); |
| 241 |
$filter = array('full' => 'Full'); |
| 242 |
foreach ($sizes as $value) { |
| 243 |
$filter[$value] = ucwords(str_replace(array('_', '-'), array(' ', ' '), $value)); |
| 244 |
} |
| 245 |
return $filter; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
public function get_post_type() { |
| 250 |
$post_type = get_post_types( '', 'names' ); |
| 251 |
return array_diff($post_type, array( 'attachment', 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset', 'oembed_cache', 'user_request', 'wp_block' )); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
// Pagination |
| 256 |
public function pagination($pages = '', $paginationNav, $range = 1) { |
| 257 |
$html = ''; |
| 258 |
$showitems = 3; |
| 259 |
$paged = is_front_page() ? get_query_var('page') : get_query_var('paged'); |
| 260 |
$paged = $paged ? $paged : 1; |
| 261 |
if($pages == '') { |
| 262 |
global $wp_query; |
| 263 |
$pages = $wp_query->max_num_pages; |
| 264 |
if(!$pages) { |
| 265 |
$pages = 1; |
| 266 |
} |
| 267 |
} |
| 268 |
$data = ($paged>=3?[($paged-1),$paged,$paged+1]:[1,2,3]); |
| 269 |
|
| 270 |
|
| 271 |
if(1 != $pages) { |
| 272 |
$html .= '<ul class="ultp-pagination">'; |
| 273 |
$display_none = 'style="display:none"'; |
| 274 |
if($pages > 4) { |
| 275 |
$html .= '<li class="ultp-prev-page-numbers" '.($paged==1?$display_none:"").'><a href="'.get_pagenum_link($paged-1).'">'.ultimate_post()->svg_icon('leftAngle2').' '.($paginationNav == 'textArrow'?__("Previous", "ultimate-post"):"").'</a></li>'; |
| 276 |
} |
| 277 |
if($pages > 4){ |
| 278 |
$html .= '<li class="ultp-first-pages" '.($paged<2?$display_none:"").' data-current="1"><a href="'.get_pagenum_link(1).'">1</a></li>'; |
| 279 |
} |
| 280 |
if($pages > 4){ |
| 281 |
$html .= '<li class="ultp-first-dot" '.($paged<2? $display_none : "").'><a href="#">...</a></li>'; |
| 282 |
} |
| 283 |
foreach ($data as $i) { |
| 284 |
if($pages >= $i){ |
| 285 |
$html .= ($paged == $i) ? '<li class="ultp-center-item pagination-active" data-current="'.$i.'"><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>':'<li class="ultp-center-item" data-current="'.$i.'"><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>'; |
| 286 |
} |
| 287 |
} |
| 288 |
if($pages > 4){ |
| 289 |
$html .= '<li class="ultp-last-dot" '.($pages<=$paged+1?$display_none:"").'><a href="#">...</a></li>'; |
| 290 |
} |
| 291 |
if($pages > 4){ |
| 292 |
$html .= '<li class="ultp-last-pages" '.($pages<=$paged+1?$display_none:"").' data-current="'.$pages.'"><a href="'.get_pagenum_link($pages).'">'.$pages.'</a></li>'; |
| 293 |
} |
| 294 |
if ($paged != $pages) { |
| 295 |
$html .= '<li class="ultp-next-page-numbers"><a href="'.get_pagenum_link($paged + 1).'">'.($paginationNav == 'textArrow' ? __("Next", "ultimate-post") : "").ultimate_post()->svg_icon('rightAngle2').'</a></li>'; |
| 296 |
} |
| 297 |
$html .= '</ul>'; |
| 298 |
} |
| 299 |
return $html; |
| 300 |
} |
| 301 |
|
| 302 |
public function excerpt_word($charlength = 200) { |
| 303 |
$html = ''; |
| 304 |
$charlength++; |
| 305 |
$excerpt = get_the_excerpt(); |
| 306 |
if ( mb_strlen( $excerpt ) > $charlength ) { |
| 307 |
$subex = mb_substr( $excerpt, 0, $charlength - 5 ); |
| 308 |
$exwords = explode( ' ', $subex ); |
| 309 |
$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) ); |
| 310 |
if ( $excut < 0 ) { |
| 311 |
$html = mb_substr( $subex, 0, $excut ); |
| 312 |
} else { |
| 313 |
$html = $subex; |
| 314 |
} |
| 315 |
$html .= '...'; |
| 316 |
} else { |
| 317 |
$html = $excerpt; |
| 318 |
} |
| 319 |
return $html; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
public function taxonomy( $prams = 'category' ) { |
| 324 |
$data = array(); |
| 325 |
$terms = get_terms( $prams, array( |
| 326 |
'hide_empty' => true, |
| 327 |
)); |
| 328 |
if( !empty($terms) ){ |
| 329 |
foreach ($terms as $val) { |
| 330 |
$data[$val->slug] = $val->name; |
| 331 |
} |
| 332 |
} |
| 333 |
return $data; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
public function next_prev() { |
| 338 |
$html = ''; |
| 339 |
$html .= '<ul>'; |
| 340 |
$html .= '<li>'; |
| 341 |
$html .= '<a class="ultp-prev-action ultp-disable" href="#">'; |
| 342 |
$html .= ultimate_post()->svg_icon('leftAngle2').'<span class="screen-reader-text">'.__("Previous", "ultimate-post").'</span>'; |
| 343 |
$html .= '</a>'; |
| 344 |
$html .= '</li>'; |
| 345 |
$html .= '<li>'; |
| 346 |
$html .= '<a class="ultp-next-action">'; |
| 347 |
$html .= ultimate_post()->svg_icon('rightAngle2').'<span class="screen-reader-text">'.__("Next", "ultimate-post").'</span>'; |
| 348 |
$html .= '</a>'; |
| 349 |
$html .= '</li>'; |
| 350 |
$html .= '</ul>'; |
| 351 |
return $html; |
| 352 |
} |
| 353 |
|
| 354 |
public function loading(){ |
| 355 |
$html = ''; |
| 356 |
$style = 'style1'; |
| 357 |
$option_data = ultimate_post()->get_setting(); |
| 358 |
if( isset($option_data['preloader_style']) ){ |
| 359 |
$style = $option_data['preloader_style']; |
| 360 |
} |
| 361 |
if( $style == 'style2' ){ |
| 362 |
$html .= '<div class="ultp-loading-spinner" style="width:100%;height:100%"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>';//ultp-block-items-wrap |
| 363 |
} else { |
| 364 |
$html .= '<div class="ultp-loading-blocks" style="width:100%;height:100%;"><div style="left: 0;top: 0;animation-delay:0s;"></div><div style="left: 21px;top: 0;animation-delay:0.125s;"></div><div style="left: 42px;top: 0;animation-delay:0.25s;"></div><div style="left: 0;top: 21px;animation-delay:0.875s;"></div><div style="left: 42px;top: 21px;animation-delay:0.375s;"></div><div style="left: 0;top: 42px;animation-delay:0.75s;"></div><div style="left: 42px;top: 42px;animation-delay:0.625s;"></div><div style="left: 21px;top: 42px;animation-delay:0.5s;"></div></div>'; |
| 365 |
} |
| 366 |
return '<div class="ultp-loading">'.$html.'</div>'; |
| 367 |
} |
| 368 |
|
| 369 |
public function filter($filterText = '', $filterType = '', $filterCat = '[]', $filterTag = '[]'){ |
| 370 |
$html = ''; |
| 371 |
$html .= '<ul class="ultp-flex-menu">'; |
| 372 |
if ($filterType == 'category') { |
| 373 |
$cat = $this->taxonomy('category'); |
| 374 |
if($filterText){ |
| 375 |
$html .= '<li class="filter-item"><a data-taxonomy="" href="#">'.$filterText.'</a></li>'; |
| 376 |
} |
| 377 |
foreach (json_decode($filterCat) as $val) { |
| 378 |
$html .= '<li class="filter-item"><a data-taxonomy="'.$val.'" href="#">'.(isset($cat[$val]) ? $cat[$val] : $val).'</a></li>'; |
| 379 |
} |
| 380 |
} else { |
| 381 |
$tag = $this->taxonomy('post_tag'); |
| 382 |
if($filterText){ |
| 383 |
$html .= '<li class="filter-item"><a data-taxonomy="" href="#">'.$filterText.'</a></li>'; |
| 384 |
} |
| 385 |
foreach (json_decode($filterTag) as $val) { |
| 386 |
$html .= '<li class="filter-item"><a data-taxonomy="'.$val.'" href="#">'.(isset($tag[$val]) ? $tag[$val] : $val).'</a></li>'; |
| 387 |
} |
| 388 |
} |
| 389 |
$html .= '</ul>'; |
| 390 |
return $html; |
| 391 |
} |
| 392 |
|
| 393 |
public function svg_icon($icons = ''){ |
| 394 |
|
| 395 |
$icon_lists = array( |
| 396 |
'eye' => file_get_contents(ULTP_PATH.'assets/img/svg/eye.svg'), |
| 397 |
'user' => file_get_contents(ULTP_PATH.'assets/img/svg/user.svg'), |
| 398 |
'calendar' => file_get_contents(ULTP_PATH.'assets/img/svg/calendar.svg'), |
| 399 |
'comment' => file_get_contents(ULTP_PATH.'assets/img/svg/comment.svg'), |
| 400 |
'book' => file_get_contents(ULTP_PATH.'assets/img/svg/book.svg'), |
| 401 |
'tag' => file_get_contents(ULTP_PATH.'assets/img/svg/tag.svg'), |
| 402 |
'clock' => file_get_contents(ULTP_PATH.'assets/img/svg/clock.svg'), |
| 403 |
'leftAngle' => file_get_contents(ULTP_PATH.'assets/img/svg/leftAngle.svg'), |
| 404 |
'rightAngle' => file_get_contents(ULTP_PATH.'assets/img/svg/rightAngle.svg'), |
| 405 |
'leftAngle2' => file_get_contents(ULTP_PATH.'assets/img/svg/leftAngle2.svg'), |
| 406 |
'rightAngle2' => file_get_contents(ULTP_PATH.'assets/img/svg/rightAngle2.svg'), |
| 407 |
'leftArrowLg' => file_get_contents(ULTP_PATH.'assets/img/svg/leftArrowLg.svg'), |
| 408 |
'refresh' => file_get_contents(ULTP_PATH.'assets/img/svg/refresh.svg'), |
| 409 |
'rightArrowLg' => file_get_contents(ULTP_PATH.'assets/img/svg/rightArrowLg.svg'), |
| 410 |
); |
| 411 |
if($icons){ |
| 412 |
return $icon_lists[ $icons ]; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
} |
| 417 |
|