Overview

Classes

  • xu
  • Xu\Foundation\Foundation
  • Xu\Foundation\Jsonable
  • Xu\Model\Collection
  • Xu\Model\Model

Functions

  • xu
  • xu_add_option
  • xu_attachment_url_to_postid
  • xu_cache_delete
  • xu_cache_delete_group
  • xu_cache_get
  • xu_cache_key
  • xu_camel_case
  • xu_clean_attachment_cache
  • xu_clean_nav_menu_cache
  • xu_clean_post_cache
  • xu_contains
  • xu_convert_to_string
  • xu_current_attachment_url_to_postid
  • xu_current_url
  • xu_current_url_to_postid
  • xu_dashify
  • xu_delete_option
  • xu_delete_site_transient
  • xu_delete_transient
  • xu_doing_ajax
  • xu_ends_with
  • xu_get_class_name
  • xu_get_model
  • xu_get_option
  • xu_get_option_name
  • xu_get_option_post_id
  • xu_get_posts
  • xu_get_site_transient
  • xu_get_top_parent_post
  • xu_get_top_parent_post_type
  • xu_get_top_parent_post_type_object
  • xu_get_transient
  • xu_is_empty
  • xu_is_http_method
  • xu_is_json
  • xu_is_post_type
  • xu_is_wp
  • xu_is_xml
  • xu_post_exists
  • xu_register_large_option_post_type
  • xu_snake_case
  • xu_starts_with
  • xu_strip_spaces
  • xu_studly_case
  • xu_update_option
  • xu_url_to_postid
  • xu_valid_personnummer
  • xu_wp_get_attachment_image
  • xu_wp_get_attachment_image_sizes
  • xu_wp_get_attachment_image_src
  • xu_wp_get_attachment_image_srcset
  • xu_wp_get_attachment_url
  • xu_wp_nav_menu
  • Overview
  • Class
  1: <?php
  2: 
  3: /**
  4:  * Is the current page the given `$post_type`?
  5:  *
  6:  * @param  int $id
  7:  * @param  string $post_type
  8:  *
  9:  * @return bool
 10:  */
 11: function xu_is_post_type( $id, $post_type = '' ) {
 12:     if ( ! is_numeric( $id ) ) {
 13:         $post_type = is_string( $id ) ? $id : '';
 14:         $id        = get_the_id();
 15:     }
 16: 
 17:     return $post_type === get_post_type( $id );
 18: }
 19: 
 20: /**
 21:  * Cached version of `get_posts`.
 22:  *
 23:  * @see https://developer.wordpress.org/reference/functions/get_posts/
 24:  *
 25:  * Think before using `get_posts` since it can be bad.
 26:  *
 27:  * @see https://10up.github.io/Engineering-Best-Practices/php/#performance
 28:  * @see https://vip.wordpress.com/documentation/caching/uncached-functions/
 29:  *
 30:  * @param  array|null $args
 31:  *
 32:  * @return array
 33:  */
 34: function xu_get_posts( array $args = null ) {
 35:     $args = is_array( $args ) ? $args : [];
 36: 
 37:     // Set suppress filters to false always.
 38:     $args['suppress_filters'] = false;
 39: 
 40:     return xu_cache_get( 'get_posts', [$args], __FUNCTION__ );
 41: }
 42: 
 43: /**
 44:  * Get top-most parent post.
 45:  *
 46:  * @param  WP_Post|int $post
 47:  *
 48:  * @throws Exception if post is not a instance of WP_Post
 49:  *
 50:  * @return WP_Post|array|null
 51:  */
 52: function xu_get_top_parent_post( $post = null ) {
 53:     if ( is_null( $post ) ) {
 54:         $post = get_post();
 55:     }
 56: 
 57:     if ( is_numeric( $post ) ) {
 58:         $post = get_post( (int) $post );
 59:     }
 60: 
 61:     if ( $post instanceof WP_Post === false ) {
 62:         throw new Exception( sprintf( '%s is not a instance of WP_Post', strtolower( gettype( $post ) ) ) );
 63:     }
 64: 
 65:     if ( $post->post_parent ) {
 66:         $ancestors = get_post_ancestors( $post->ID );
 67:         $root      = count( $ancestors ) - 1;
 68: 
 69:         return get_post( $ancestors[$root] );
 70:     } else {
 71:         $url     = $_SERVER['REQUEST_URI'];
 72:         $url     = str_replace( $post->post_name, '', $url );
 73:         $url     = rtrim( $url, '/' );
 74:         $post_id = url_to_postid( $url );
 75: 
 76:         return $post_id > 0 ? get_post( $post_id ) : $post;
 77:     }
 78: }
 79: 
 80: /**
 81:  * Get top-most parent post type.
 82:  *
 83:  * @param  WP_Post|int $post
 84:  *
 85:  * @throws Exception if post is not a instance of WP_Post
 86:  *
 87:  * @return WP_Post|array|null
 88:  */
 89: function xu_get_top_parent_post_type( $post = null ) {
 90:     return get_post_type( xu_get_top_parent_post( $post ) );
 91: }
 92: 
 93: /**
 94:  * Get top-most parent post type object.
 95:  *
 96:  * @param  WP_Post|int $post
 97:  *
 98:  * @throws Exception if post is not a instance of WP_Post
 99:  *
100:  * @return WP_Post|array|null
101:  */
102: function xu_get_top_parent_post_type_object( $post = null ) {
103:     return get_post_type_object( xu_get_top_parent_post_type( $post ) );
104: }
105: 
106: /**
107:  * Determines if a post exist or not.
108:  *
109:  * @param  int $post_id
110:  *
111:  * @return bool
112:  */
113: function xu_post_exists( $post_id = 0 ) {
114:     return is_string( get_post_status( $post_id ) );
115: }
116: 
API documentation generated by ApiGen