class-appside-helper-functions.php (15850B)
1 <?php 2 /** 3 * @package Appside 4 * @author Ir-Tech 5 */ 6 if ( ! defined( "ABSPATH" ) ) { 7 exit(); //exit if access directly 8 } 9 10 if ( ! class_exists( 'Appside_Helper_Functions' ) ) { 11 12 class Appside_Helper_Functions { 13 /* 14 * $instance 15 * @since 1.0.0 16 * */ 17 protected static $instance; 18 19 public function __construct() { 20 } 21 22 /** 23 * getInstance() 24 * */ 25 public static function getInstance() { 26 if ( null == self::$instance ) { 27 self::$instance = new self(); 28 } 29 30 return self::$instance; 31 } 32 33 /** 34 * Displays an optional post thumbnail. 35 * 36 * Wraps the post thumbnail in an anchor element on index views, or a div 37 * element when on single views. 38 */ 39 40 function post_thumbnail() { 41 if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) { 42 return; 43 } 44 45 if ( is_singular() ) : 46 ?> 47 48 <div class="post-thumbnail thumb"> 49 <?php the_post_thumbnail( 'appside_classic' ); ?> 50 </div><!-- .post-thumbnail --> 51 52 <?php else : ?> 53 <div class="thumb"> 54 <a class="post-thumbnail " href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1"> 55 <?php 56 the_post_thumbnail( 'appside_classic', array( 57 'alt' => the_title_attribute( array( 58 'echo' => false, 59 ) ), 60 ) ); 61 ?> 62 </a> 63 </div> 64 <?php 65 endif; // End is_singular(). 66 } 67 68 /* 69 * Frontend get post terms 70 * 71 * @since 1.0.0 72 * */ 73 public function get_terms_names( $taxonomy_name = '', $output = '', $hide_empty = false ) { 74 $return_val = []; 75 $terms = get_terms( 76 array( 77 'taxonomy' => $taxonomy_name, 78 'hide_empty' => $hide_empty 79 ) 80 ); 81 foreach ( $terms as $term ) { 82 if ( 'id' == $output ) { 83 $return_val[ $term->term_id ] = $term->name; 84 } else { 85 $return_val[ $term->slug ] = $term->name; 86 } 87 } 88 89 return $return_val; 90 } 91 92 /* 93 * sanitize_px 94 * 95 * @since 1.0.7 96 * */ 97 public function sanitize_px( $value ) { 98 $return_val = $value; 99 if ( filter_var( $value, FILTER_VALIDATE_INT ) ) { 100 $return_val = $value . 'px'; 101 } 102 103 return $return_val; 104 } 105 106 /* 107 * minify_css_lines 108 * 109 * @since 1.0.7 110 * */ 111 public function minify_css_lines( $css ) { 112 // some of the following functions to minimize the css-output are directly taken 113 // from the awesome CSS JS Booster: https://github.com/Schepp/CSS-JS-Booster 114 // all credits to Christian Schaefer: http://twitter.com/derSchepp 115 // remove comments 116 $css = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css ); 117 // backup values within single or double quotes 118 preg_match_all( '/(\'[^\']*?\'|"[^"]*?")/ims', $css, $hit, PREG_PATTERN_ORDER ); 119 for ( $i = 0; $i < count( $hit[1] ); $i ++ ) { 120 $css = str_replace( $hit[1][ $i ], '##########' . $i . '##########', $css ); 121 } 122 // remove traling semicolon of selector's last property 123 $css = preg_replace( '/;[\s\r\n\t]*?}[\s\r\n\t]*/ims', "}\r\n", $css ); 124 // remove any whitespace between semicolon and property-name 125 $css = preg_replace( '/;[\s\r\n\t]*?([\r\n]?[^\s\r\n\t])/ims', ';$1', $css ); 126 // remove any whitespace surrounding property-colon 127 $css = preg_replace( '/[\s\r\n\t]*:[\s\r\n\t]*?([^\s\r\n\t])/ims', ':$1', $css ); 128 // remove any whitespace surrounding selector-comma 129 $css = preg_replace( '/[\s\r\n\t]*,[\s\r\n\t]*?([^\s\r\n\t])/ims', ',$1', $css ); 130 // remove any whitespace surrounding opening parenthesis 131 $css = preg_replace( '/[\s\r\n\t]*{[\s\r\n\t]*?([^\s\r\n\t])/ims', '{$1', $css ); 132 // remove any whitespace between numbers and units 133 $css = preg_replace( '/([\d\.]+)[\s\r\n\t]+(px|em|pt|%)/ims', '$1$2', $css ); 134 // shorten zero-values 135 $css = preg_replace( '/([^\d\.]0)(px|em|pt|%)/ims', '$1', $css ); 136 // constrain multiple whitespaces 137 $css = preg_replace( '/\p{Zs}+/ims', ' ', $css ); 138 // remove newlines 139 $css = str_replace( array( "\r\n", "\r", "\n" ), '', $css ); 140 // Restore backupped values within single or double quotes 141 for ( $i = 0; $i < count( $hit[1] ); $i ++ ) { 142 $css = str_replace( '##########' . $i . '##########', $hit[1][ $i ], $css ); 143 } 144 145 return $css; 146 } 147 148 /* 149 * check is cs framework activated 150 * 151 * @since 1.0.0 152 * */ 153 public function is_cs_framework_active() { 154 return ( defined( 'CS_VERSION' ) ) ? true : false; 155 } 156 157 /* 158 * Pages Links 159 * 160 * @since 1.0.0 161 * */ 162 public function link_pages() { 163 164 $defaults = array( 165 'before' => '<div class="wp-link-pages"><span>' . esc_html__( 'Pages:', 'aapside' ) . '</span>', 166 'after' => '</div>', 167 'link_before' => '', 168 'link_after' => '', 169 'next_or_number' => 'number', 170 'separator' => ' ', 171 'pagelink' => '%', 172 'echo' => 1 173 ); 174 wp_link_pages( $defaults ); 175 } 176 177 /* 178 * Pagination 179 * 180 * @since 1.0.0 181 * */ 182 public function post_pagination( $nav_query = null ) { 183 global $wp_query; 184 $big = 12345678; 185 if ( null == $nav_query ) { 186 $page_format = paginate_links( array( 187 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 188 'format' => '?paged=%#%', 189 'current' => max( 1, get_query_var( 'paged' ) ), 190 'total' => $wp_query->max_num_pages, 191 'type' => 'array', 192 'next_text' => '»', 193 'prev_text' => '«' 194 ) ); 195 if ( is_array( $page_format ) ) { 196 $paged = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' ); 197 echo '<div class="blog-pagination margin-top-60"><ul>'; 198 echo '<li><span>' . esc_html( $paged ) .' '. esc_html__( 'of', 'aapside' ) .' '. esc_html( $wp_query->max_num_pages ) . '</span></li>'; 199 foreach ( $page_format as $page ) { 200 echo "<li>" . wp_kses_post( $page ) . "</li>"; 201 } 202 echo '</ul></div>'; 203 } 204 } else { 205 206 $page_format = paginate_links( array( 207 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 208 'format' => '?paged=%#%', 209 'current' => max( 1, get_query_var( 'paged' ) ), 210 'total' => $nav_query->max_num_pages, 211 'type' => 'array', 212 'next_text' => '»', 213 'prev_text' => '«' 214 ) ); 215 216 if ( is_array( $page_format ) ) { 217 $paged = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' ); 218 echo '<div class="blog-pagination margin-top-60"><ul>'; 219 echo '<li><span>' . esc_html( $paged ) .' '. esc_html__( 'of', 'aapside' ) .' '. esc_html( $nav_query->max_num_pages ) . '</span></li>'; 220 foreach ( $page_format as $page ) { 221 echo "<li>" . wp_kses_post( $page ) . "</li>"; 222 } 223 echo '</ul></div>'; 224 } 225 226 } 227 } 228 229 /** 230 * Prints HTML with meta information for the current post-date/time. 231 */ 232 public function posted_on() { 233 234 $time_string = sprintf( '<time class="entry-date published updated">%1$s</time>', esc_attr( get_the_date() ) ); 235 $time_string = sprintf( $time_string, 236 esc_attr( get_the_date( DATE_W3C ) ) 237 ); 238 239 $posted_on = sprintf( 240 /* translators: %s: post date. */ 241 esc_html_x( ' %s', 'post date', 'aapside' ), 242 '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark"><i class="fa fa-calendar" aria-hidden="true"></i> ' . $time_string . '</a>' 243 ); 244 245 echo '<span class="posted-on">' . $posted_on . '</span>'; // WPCS: XSS OK. 246 247 } 248 249 /** 250 * Prints HTML with meta information of posted by or authors. 251 */ 252 public function posted_by() { 253 $byline = sprintf( 254 /* translators: %s: post author. */ 255 esc_html_x( ' %s', 'post author', 'aapside' ), 256 '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '"><i class="fa fa-user-o" aria-hidden="true"></i> ' . esc_html__( 'By ', 'aapside' ) . esc_html( get_the_author() ) . '</a></span>' 257 ); 258 259 echo '<span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK. 260 261 } 262 263 /* 264 * posted tags 265 * @since 1.0.0 266 * */ 267 public function posted_tag() { 268 /* translators: used between list items, there is a space after the comma */ 269 $tags_list = get_the_tag_list( '', esc_html_x( ' ', 'list item separator', 'aapside' ) ); 270 if ( $tags_list ) { 271 /* translators: 1: list of tags. */ 272 printf( '<ul class="tags"><li class="title">' . esc_html__( 'Tags: ', 'aapside' ) . '</li><li>' . ' %1$s' . '</li></ul>', $tags_list ); // WPCS: XSS OK. 273 } 274 } 275 276 /** 277 * The post navigation 278 * @since 1.0.0 279 * */ 280 public function post_navigation() { 281 the_post_navigation( array( 282 'prev_text' => '<i class="fa fa-angle-double-left" aria-hidden="true"></i> ' . esc_html__( 'Prev Post', 'aapside' ), 283 'next_text' => esc_html__( 'Next Post', 'aapside' ) . ' <i class="fa fa-angle-double-right" aria-hidden="true"></i>', 284 ) ); 285 echo wp_kses_post( '<div class="clearfix"></div>' ); 286 } 287 288 /** 289 * is_home_page() 290 * */ 291 public static function is_home_page() { 292 $check_home_page = true; 293 if ( is_home() && is_front_page() ) { 294 $check_home_page = true; 295 } elseif ( is_front_page() && ! is_home() ) { 296 $check_home_page = true; 297 } elseif ( is_home() && ! is_front_page() ) { 298 $check_home_page = false; 299 } 300 $return_val = $check_home_page; 301 302 return $return_val; 303 } 304 305 /* 306 * get_teams_by_post_id() 307 * @since 1.0.0 308 * */ 309 public function get_terms_by_post_id( $post_id, $taxonomy ) { 310 $all_terms = array(); 311 $all_term_list = get_the_terms( $post_id, $taxonomy ); 312 313 foreach ( $all_term_list as $term_item ) { 314 $term_url = get_term_link( $term_item->term_id, $taxonomy ); 315 $all_terms[ $term_url ] = $term_item->name; 316 } 317 318 return $all_terms; 319 } 320 321 /* 322 * kses allowed html 323 * @since 1.0.0 324 * */ 325 public function kses_allowed_html( $allowed_tags = 'all' ) { 326 $allowed_html = array( 327 'div' => array( 'class' => array(), 'id' => array() ), 328 'header' => array( 'class' => array(), 'id' => array() ), 329 'h1' => array( 'class' => array(), 'id' => array() ), 330 'h2' => array( 'class' => array(), 'id' => array() ), 331 'h3' => array( 'class' => array(), 'id' => array() ), 332 'h4' => array( 'class' => array(), 'id' => array() ), 333 'h5' => array( 'class' => array(), 'id' => array() ), 334 'h6' => array( 'class' => array(), 'id' => array() ), 335 'p' => array( 'class' => array(), 'id' => array() ), 336 'span' => array( 'class' => array(), 'id' => array() ), 337 'i' => array( 'class' => array(), 'id' => array() ), 338 'mark' => array( 'class' => array(), 'id' => array() ), 339 'strong' => array( 'class' => array(), 'id' => array() ), 340 'br' => array( 'class' => array(), 'id' => array() ), 341 'b' => array( 'class' => array(), 'id' => array() ), 342 'em' => array( 'class' => array(), 'id' => array() ), 343 'del' => array( 'class' => array(), 'id' => array() ), 344 'ins' => array( 'class' => array(), 'id' => array() ), 345 'u' => array( 'class' => array(), 'id' => array() ), 346 's' => array( 'class' => array(), 'id' => array() ), 347 'nav' => array( 'class' => array(), 'id' => array() ), 348 'ul' => array( 'class' => array(), 'id' => array() ), 349 'li' => array( 'class' => array(), 'id' => array() ), 350 'form' => array( 'class' => array(), 'id' => array() ), 351 'select' => array( 'class' => array(), 'id' => array() ), 352 'option' => array( 'class' => array(), 'id' => array() ), 353 'img' => array( 'class' => array(), 'id' => array() ), 354 'a' => array( 'class' => array(), 'id' => array(), 'href' => array() ), 355 ); 356 357 if ( 'all' == $allowed_tags ) { 358 return $allowed_html; 359 } else { 360 if ( is_array( $allowed_tags ) && ! empty( $allowed_tags ) ) { 361 $specific_tags = array(); 362 foreach ( $allowed_tags as $allowed_tag ) { 363 if ( array_key_exists( $allowed_tag, $allowed_html ) ) { 364 $specific_tags[ $allowed_tag ] = $allowed_html[ $allowed_tag ]; 365 } 366 } 367 368 return $specific_tags; 369 } 370 } 371 372 } 373 374 /** 375 * get Theme global info 376 * @since 1.0.6 377 * */ 378 public static function get_theme_info( $type = 'name' ) { 379 380 $theme_information = array(); 381 if ( is_child_theme() ) { 382 $theme = wp_get_theme(); 383 $parent = $theme->get( 'Template' ); 384 $theme_info = wp_get_theme( $parent ); 385 } else { 386 $theme_info = wp_get_theme(); 387 } 388 $theme_information['THEME_NAME'] = $theme_info->get( 'Name' ); 389 $theme_information['THEME_VERSION'] = $theme_info->get( 'Version' ); 390 $theme_information['THEME_AUTHOR'] = $theme_info->get( 'Author' ); 391 $theme_information['THEME_AUTHOR_URI'] = $theme_info->get( 'AuthorURI' ); 392 393 switch ( $type ) { 394 case ( "name" ): 395 $return_val = $theme_information['THEME_NAME']; 396 break; 397 case ( "version" ): 398 $return_val = $theme_information['THEME_VERSION']; 399 break; 400 case ( "author" ): 401 $return_val = $theme_information['THEME_AUTHOR']; 402 break; 403 case ( "author_uri" ): 404 $return_val = $theme_information['THEME_AUTHOR_URI']; 405 break; 406 default: 407 $return_val = $theme_information; 408 break; 409 } 410 411 return $return_val; 412 413 } 414 415 /** 416 * get_page_id() 417 * @since 1.0.0 418 * */ 419 public function page_id() { 420 global $post, $wp_query; 421 $page_type_id = ( isset( $post->ID ) && in_array( $post->ID, self::get_pages_id() ) ) ? $post->ID : false; 422 423 if ( false == $page_type_id ) { 424 $page_type_id = isset( $wp_query->post->ID ) ? $wp_query->post->ID : false; 425 } 426 $page_id = ( isset( $page_type_id ) ) ? $page_type_id : false; 427 $page_id = is_home() ? get_option( 'page_for_posts' ) : $page_id; 428 429 return $page_id; 430 } 431 432 /** 433 * get_pages_id() 434 * @since 1.0.0 435 * */ 436 public function get_pages_id() { 437 $pages_id = false; 438 $pages = get_pages( array( 439 'post_type' => 'page', 440 'post_status' => 'publish' 441 ) ); 442 443 if ( ! empty( $pages ) && is_array( $pages ) ) { 444 $pages_id = array(); 445 foreach ( $pages as $page ) { 446 $pages_id[] = $page->ID; 447 } 448 } 449 450 return $pages_id; 451 } 452 453 /** 454 * is appside active 455 * @sicne 1.0.0 456 * */ 457 public function is_appside_active() { 458 $theme_name_array = array( 'Aapside', 'Aapside Child' ); 459 $current_theme = wp_get_theme(); 460 $current_theme_name = $current_theme->get( 'Name' ); 461 462 return in_array( $current_theme_name, $theme_name_array ) ? true : false; 463 } 464 465 /** 466 * is appside master active 467 * @sicne 1.0.0 468 * */ 469 public function is_appside_master_active() { 470 return defined( 'APPSIDE_MASTER_SELF_PATH' ) ? true : false; 471 } 472 473 /** 474 * get post list by post type 475 * @sicne 2.0.0 476 * */ 477 public function get_post_list_by_post_type( $post_type, $output = 'id' ) { 478 $return_val = []; 479 $args = array( 480 'post_type' => $post_type, 481 'post_per_pages' => -1, 482 ); 483 $all_post = new \WP_Query( $args ); 484 485 while ( $all_post->have_posts() ) { 486 $all_post->the_post(); 487 $return_val[ get_the_ID() ] = get_the_title(); 488 } 489 490 return $return_val; 491 } 492 493 /** 494 * render elementor content 495 * @since 2.0.0 496 * */ 497 public function render_elementor_content( $content = null ) { 498 $return_val = ''; 499 if ( defined( 'ELEMENTOR_VERSION' ) ) { 500 $el_plugin_instance = \Elementor\Plugin::instance(); 501 $return_val = $el_plugin_instance->frontend->get_builder_content( $content ); 502 } 503 504 return $return_val; 505 } 506 507 /** 508 * Get list of nav menu 509 * @since 2.0.0 510 * */ 511 public function get_nav_menu_list( $output = 'slug' ) { 512 $return_val = []; 513 $all_menu_list = wp_get_nav_menus(); 514 515 foreach ( $all_menu_list as $menu ) { 516 if ($output == 'slug'){ 517 $return_val[ $menu->slug] = $menu->name; 518 }else{ 519 $return_val[ $menu->term_id] = $menu->name; 520 } 521 } 522 523 return $return_val; 524 } 525 526 }//end class 527 if ( class_exists( 'Appside_Helper_Functions' ) ) { 528 Appside_Helper_Functions::getInstance(); 529 } 530 }