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:  * Add large option value to hidden post type.
  5:  *
  6:  * @param  string $name  Option name.
  7:  * @param  mixed  $value Option value.
  8:  *
  9:  * @return mixed Returns false on failure.
 10:  */
 11: function xu_add_option( $name, $value ) {
 12:     $name = xu_get_option_name( $name );
 13: 
 14:     if ( empty( $name ) ) {
 15:         return false;
 16:     }
 17: 
 18:     if ( xu_get_option( $name ) !== false ) {
 19:         return false;
 20:     }
 21: 
 22:     $post_id = wp_insert_post( [
 23:         'post_type'   => 'xu_large_option',
 24:         'post_name'   => $name,
 25:         'post_title'  => $name
 26:     ] );
 27: 
 28:     if ( is_wp_error( $post_id ) ) {
 29:         return false;
 30:     }
 31: 
 32:     if ( ! update_post_meta( $post_id, 'option_value', $value ) ) {
 33:         return false;
 34:     }
 35: 
 36:     wp_cache_set( $name, $value, 'xu_large_option' );
 37: 
 38:     return true;
 39: }
 40: 
 41: /**
 42:  * Delete option value from to hidden post type.
 43:  *
 44:  * @param  string $name Option name
 45:  *
 46:  * @return bool
 47:  */
 48: function xu_delete_option( $name ) {
 49:     $name = xu_get_option_name( $name );
 50: 
 51:     if ( empty( $name ) ) {
 52:         return false;
 53:     }
 54: 
 55:     if ( ! ( $post_id = xu_get_option_post_id( $name ) ) ) {
 56:         return false;
 57:     }
 58: 
 59:     wp_cache_delete( $name, 'xu_large_option' );
 60: 
 61:     return is_object( wp_delete_post( $post_id, true ) );
 62: }
 63: 
 64: /**
 65:  * Get option value from to hidden post type or default value.
 66:  *
 67:  * @param  string $name    Option name.
 68:  * @param  bool   $default Default value.
 69:  *
 70:  * @return mixed
 71:  */
 72: function xu_get_option( $name, $default = false ) {
 73:     $name = xu_get_option_name( $name );
 74: 
 75:     if ( empty( $name ) ) {
 76:         return $default;
 77:     }
 78: 
 79:     if ( $value = wp_cache_get( $name, __FUNCTION__ ) ) {
 80:         return $value;
 81:     }
 82: 
 83:     if ( ! ( $post_id = xu_get_option_post_id( $name ) ) ) {
 84:         return $default;
 85:     }
 86: 
 87:     if ( $value = get_post_meta( $post_id, 'option_value', true ) ) {
 88:         return $value;
 89:     }
 90: 
 91:     return $default;
 92: }
 93: 
 94: /**
 95:  * Get option name.
 96:  *
 97:  * @param  string $name Option name.
 98:  *
 99:  * @return string
100:  */
101: function xu_get_option_name( $name ) {
102:     if ( ! is_string( $name ) ) {
103:         return;
104:     }
105: 
106:     return sanitize_title( trim( $name ) );
107: }
108: 
109: /**
110:  * Get option post id.
111:  *
112:  * @param  string $name Option name.
113:  *
114:  * @return int
115:  */
116: function xu_get_option_post_id( $name ) {
117:     $name = xu_get_option_name( $name );
118: 
119:     if ( empty( $name ) ) {
120:         return 0;
121:     }
122: 
123:     $query = new WP_Query( [
124:         'fields'         => 'ids',
125:         'name'           => $name,
126:         'post_type'      => 'xu_large_option',
127:         'posts_per_page' => 1
128:     ] );
129: 
130:     $posts = $query->get_posts();
131: 
132:     if ( count( $posts ) ) {
133:         return $posts[0];
134:     }
135: 
136:     return 0;
137: }
138: 
139: /**
140:  * Update or add option value to hidden post type.
141:  *
142:  * @param  string $name  Option name.
143:  * @param  mixed  $value Option value.
144:  *
145:  * @return mixed Returns false on failure.
146:  */
147: function xu_update_option( $name, $value ) {
148:     $name = xu_get_option_name( $name );
149: 
150:     if ( empty( $name ) ) {
151:         return false;
152:     }
153: 
154:     $old = xu_get_option( $name );
155: 
156:     if ( $old === $value ) {
157:         return false;
158:     }
159: 
160:     if ( $old === false ) {
161:         return xu_add_option( $name, $value );
162:     }
163: 
164:     if ( ! ( $post_id = xu_get_option_post_id( $name ) ) ) {
165:         return $default;
166:     }
167: 
168:     if ( ! update_post_meta( $post_id, 'option_value', $value ) ) {
169:         return false;
170:     }
171: 
172:     wp_cache_replace( $name, $value, 'xu_large_option' );
173: 
174:     return true;
175: }
176: 
177: /**
178:  * Register `xu_large_option` hidden post type.
179:  */
180: function xu_register_large_option_post_type() {
181:     register_post_type( 'xu_large_option', [
182:         'can_export'          => true,
183:         'capability_type'     => 'xu_large_option',
184:         'exclude_from_search' => true,
185:         'has_archive'         => false,
186:         'public'              => false,
187:         'publicly_queryable'  => false,
188:         'rewrite'             => false,
189:         'query_var'           => false,
190:         'show_ui'             => false,
191:         'show_in_admin_bar'   => false,
192:         'show_in_menu'        => false,
193:         'show_in_nav_menus'   => false,
194:     ] );
195: }
196: 
API documentation generated by ApiGen