about
7 years ago
fields
7 years ago
helpers
7 years ago
interfaces
9 years ago
storages
7 years ago
templates
8 years ago
walkers
7 years ago
autoloader.php
7 years ago
clone.php
7 years ago
core.php
8 years ago
field-registry.php
8 years ago
field.php
7 years ago
functions.php
7 years ago
loader.php
7 years ago
media-modal.php
8 years ago
meta-box-registry.php
8 years ago
meta-box.php
7 years ago
sanitizer.php
7 years ago
storage-registry.php
7 years ago
validation.php
7 years ago
wpml.php
8 years ago
functions.php
355 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin public functions. |
| 4 | * |
| 5 | * @package Meta Box |
| 6 | */ |
| 7 | |
| 8 | if ( ! function_exists( 'rwmb_meta' ) ) { |
| 9 | /** |
| 10 | * Get post meta. |
| 11 | * |
| 12 | * @param string $key Meta key. Required. |
| 13 | * @param array $args Array of arguments. Optional. |
| 14 | * @param int|null $post_id Post ID. null for current post. Optional. |
| 15 | * |
| 16 | * @return mixed |
| 17 | */ |
| 18 | function rwmb_meta( $key, $args = array(), $post_id = null ) { |
| 19 | $args = wp_parse_args( $args ); |
| 20 | $field = rwmb_get_field_settings( $key, $args, $post_id ); |
| 21 | |
| 22 | /* |
| 23 | * If field is not found, which can caused by registering meta boxes for the backend only or conditional registration. |
| 24 | * Then fallback to the old method to retrieve meta (which uses get_post_meta() as the latest fallback). |
| 25 | */ |
| 26 | if ( false === $field ) { |
| 27 | return apply_filters( 'rwmb_meta', rwmb_meta_legacy( $key, $args, $post_id ) ); |
| 28 | } |
| 29 | $meta = in_array( $field['type'], array( 'oembed', 'map', 'osm' ), true ) ? |
| 30 | rwmb_the_value( $key, $args, $post_id, false ) : |
| 31 | rwmb_get_value( $key, $args, $post_id ); |
| 32 | return apply_filters( 'rwmb_meta', $meta, $key, $args, $post_id ); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | if ( ! function_exists( 'rwmb_get_field_settings' ) ) { |
| 37 | /** |
| 38 | * Get field settings. |
| 39 | * |
| 40 | * @param string $key Meta key. Required. |
| 41 | * @param array $args Array of arguments. Optional. |
| 42 | * @param int|null $object_id Object ID. null for current post. Optional. |
| 43 | * |
| 44 | * @return array |
| 45 | */ |
| 46 | function rwmb_get_field_settings( $key, $args = array(), $object_id = null ) { |
| 47 | $args = wp_parse_args( |
| 48 | $args, |
| 49 | array( |
| 50 | 'object_type' => 'post', |
| 51 | ) |
| 52 | ); |
| 53 | |
| 54 | /** |
| 55 | * Filter meta type from object type and object id. |
| 56 | * |
| 57 | * @var string Meta type, default is post type name. |
| 58 | * @var string Object type. |
| 59 | * @var string|int Object id. |
| 60 | */ |
| 61 | $type = apply_filters( 'rwmb_meta_type', '', $args['object_type'], $object_id ); |
| 62 | if ( ! $type ) { |
| 63 | $type = get_post_type( $object_id ); |
| 64 | } |
| 65 | |
| 66 | return rwmb_get_registry( 'field' )->get( $key, $type, $args['object_type'] ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if ( ! function_exists( 'rwmb_meta_legacy' ) ) { |
| 71 | /** |
| 72 | * Get post meta. |
| 73 | * |
| 74 | * @param string $key Meta key. Required. |
| 75 | * @param array $args Array of arguments. Optional. |
| 76 | * @param int|null $post_id Post ID. null for current post. Optional. |
| 77 | * |
| 78 | * @return mixed |
| 79 | */ |
| 80 | function rwmb_meta_legacy( $key, $args = array(), $post_id = null ) { |
| 81 | $args = wp_parse_args( |
| 82 | $args, |
| 83 | array( |
| 84 | 'type' => 'text', |
| 85 | 'multiple' => false, |
| 86 | 'clone' => false, |
| 87 | ) |
| 88 | ); |
| 89 | $field = array( |
| 90 | 'id' => $key, |
| 91 | 'type' => $args['type'], |
| 92 | 'clone' => $args['clone'], |
| 93 | 'multiple' => $args['multiple'], |
| 94 | ); |
| 95 | |
| 96 | $method = 'get_value'; |
| 97 | switch ( $args['type'] ) { |
| 98 | case 'taxonomy': |
| 99 | case 'taxonomy_advanced': |
| 100 | $field['taxonomy'] = $args['taxonomy']; |
| 101 | break; |
| 102 | case 'map': |
| 103 | case 'osm': |
| 104 | case 'oembed': |
| 105 | $method = 'the_value'; |
| 106 | break; |
| 107 | } |
| 108 | $field = RWMB_Field::call( 'normalize', $field ); |
| 109 | |
| 110 | return RWMB_Field::call( $method, $field, $args, $post_id ); |
| 111 | } |
| 112 | } // End if(). |
| 113 | |
| 114 | if ( ! function_exists( 'rwmb_get_value' ) ) { |
| 115 | /** |
| 116 | * Get value of custom field. |
| 117 | * This is used to replace old version of rwmb_meta key. |
| 118 | * |
| 119 | * @param string $field_id Field ID. Required. |
| 120 | * @param array $args Additional arguments. Rarely used. See specific fields for details. |
| 121 | * @param int|null $post_id Post ID. null for current post. Optional. |
| 122 | * |
| 123 | * @return mixed false if field doesn't exist. Field value otherwise. |
| 124 | */ |
| 125 | function rwmb_get_value( $field_id, $args = array(), $post_id = null ) { |
| 126 | $args = wp_parse_args( $args ); |
| 127 | $field = rwmb_get_field_settings( $field_id, $args, $post_id ); |
| 128 | |
| 129 | // Get field value. |
| 130 | $value = $field ? RWMB_Field::call( 'get_value', $field, $args, $post_id ) : false; |
| 131 | |
| 132 | /* |
| 133 | * Allow developers to change the returned value of field. |
| 134 | * For version < 4.8.2, the filter name was 'rwmb_get_field'. |
| 135 | * |
| 136 | * @param mixed $value Field value. |
| 137 | * @param array $field Field parameters. |
| 138 | * @param array $args Additional arguments. Rarely used. See specific fields for details. |
| 139 | * @param int|null $post_id Post ID. null for current post. Optional. |
| 140 | */ |
| 141 | $value = apply_filters( 'rwmb_get_value', $value, $field, $args, $post_id ); |
| 142 | |
| 143 | return $value; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if ( ! function_exists( 'rwmb_the_value' ) ) { |
| 148 | /** |
| 149 | * Display the value of a field |
| 150 | * |
| 151 | * @param string $field_id Field ID. Required. |
| 152 | * @param array $args Additional arguments. Rarely used. See specific fields for details. |
| 153 | * @param int|null $post_id Post ID. null for current post. Optional. |
| 154 | * @param bool $echo Display field meta value? Default `true` which works in almost all cases. We use `false` for the [rwmb_meta] shortcode. |
| 155 | * |
| 156 | * @return string |
| 157 | */ |
| 158 | function rwmb_the_value( $field_id, $args = array(), $post_id = null, $echo = true ) { |
| 159 | $args = wp_parse_args( $args ); |
| 160 | $field = rwmb_get_field_settings( $field_id, $args, $post_id ); |
| 161 | |
| 162 | if ( ! $field ) { |
| 163 | return ''; |
| 164 | } |
| 165 | |
| 166 | $output = RWMB_Field::call( 'the_value', $field, $args, $post_id ); |
| 167 | |
| 168 | /* |
| 169 | * Allow developers to change the returned value of field. |
| 170 | * For version < 4.8.2, the filter name was 'rwmb_get_field'. |
| 171 | * |
| 172 | * @param mixed $value Field HTML output. |
| 173 | * @param array $field Field parameters. |
| 174 | * @param array $args Additional arguments. Rarely used. See specific fields for details. |
| 175 | * @param int|null $post_id Post ID. null for current post. Optional. |
| 176 | */ |
| 177 | $output = apply_filters( 'rwmb_the_value', $output, $field, $args, $post_id ); |
| 178 | |
| 179 | if ( $echo ) { |
| 180 | echo $output; // WPCS: XSS OK. |
| 181 | } |
| 182 | |
| 183 | return $output; |
| 184 | } |
| 185 | } // End if(). |
| 186 | |
| 187 | if ( ! function_exists( 'rwmb_get_object_fields' ) ) { |
| 188 | /** |
| 189 | * Get defined meta fields for object. |
| 190 | * |
| 191 | * @param int|string $type_or_id Object ID or post type / taxonomy (for terms) / user (for users). |
| 192 | * @param string $object_type Object type. Use post, term. |
| 193 | * |
| 194 | * @return array |
| 195 | */ |
| 196 | function rwmb_get_object_fields( $type_or_id, $object_type = 'post' ) { |
| 197 | $meta_boxes = rwmb_get_registry( 'meta_box' )->get_by( array( 'object_type' => $object_type ) ); |
| 198 | array_walk( $meta_boxes, 'rwmb_check_meta_box_supports', array( $object_type, $type_or_id ) ); |
| 199 | $meta_boxes = array_filter( $meta_boxes ); |
| 200 | |
| 201 | $fields = array(); |
| 202 | foreach ( $meta_boxes as $meta_box ) { |
| 203 | foreach ( $meta_box->fields as $field ) { |
| 204 | $fields[ $field['id'] ] = $field; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return $fields; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if ( ! function_exists( 'rwmb_check_meta_box_supports' ) ) { |
| 213 | /** |
| 214 | * Check if a meta box supports an object. |
| 215 | * |
| 216 | * @param object $meta_box Meta Box object. |
| 217 | * @param int $key Not used. |
| 218 | * @param array $object_data Object data (type and ID). |
| 219 | */ |
| 220 | function rwmb_check_meta_box_supports( &$meta_box, $key, $object_data ) { |
| 221 | list( $object_type, $type_or_id ) = $object_data; |
| 222 | |
| 223 | $type = null; |
| 224 | $prop = null; |
| 225 | switch ( $object_type ) { |
| 226 | case 'post': |
| 227 | $type = is_numeric( $type_or_id ) ? get_post_type( $type_or_id ) : $type_or_id; |
| 228 | $prop = 'post_types'; |
| 229 | break; |
| 230 | case 'term': |
| 231 | $type = $type_or_id; |
| 232 | if ( is_numeric( $type_or_id ) ) { |
| 233 | $term = get_term( $type_or_id ); |
| 234 | $type = is_array( $term ) ? $term->taxonomy : null; |
| 235 | } |
| 236 | $prop = 'taxonomies'; |
| 237 | break; |
| 238 | } |
| 239 | if ( ! $type || ! in_array( $type, $meta_box->meta_box[ $prop ], true ) ) { |
| 240 | $meta_box = false; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if ( ! function_exists( 'rwmb_meta_shortcode' ) ) { |
| 246 | /** |
| 247 | * Shortcode to display meta value. |
| 248 | * |
| 249 | * @param array $atts Shortcode attributes, same as rwmb_meta() function, but has more "meta_key" parameter. |
| 250 | * |
| 251 | * @return string |
| 252 | */ |
| 253 | function rwmb_meta_shortcode( $atts ) { |
| 254 | $atts = wp_parse_args( |
| 255 | $atts, |
| 256 | array( |
| 257 | 'id' => '', |
| 258 | 'object_id' => get_queried_object_id(), |
| 259 | 'attribute' => '', |
| 260 | ) |
| 261 | ); |
| 262 | RWMB_Helpers_Array::change_key( $atts, 'post_id', 'object_id' ); |
| 263 | RWMB_Helpers_Array::change_key( $atts, 'meta_key', 'id' ); |
| 264 | |
| 265 | if ( empty( $atts['id'] ) ) { |
| 266 | return ''; |
| 267 | } |
| 268 | |
| 269 | $field_id = $atts['id']; |
| 270 | $object_id = $atts['object_id']; |
| 271 | unset( $atts['id'], $atts['object_id'] ); |
| 272 | |
| 273 | $attribute = $atts['attribute']; |
| 274 | if ( ! $attribute ) { |
| 275 | return rwmb_the_value( $field_id, $atts, $object_id, false ); |
| 276 | } |
| 277 | |
| 278 | $value = rwmb_get_value( $field_id, $atts, $object_id ); |
| 279 | |
| 280 | if ( ! is_array( $value ) && ! is_object( $value ) ) { |
| 281 | return $value; |
| 282 | } |
| 283 | |
| 284 | if ( is_object( $value ) ) { |
| 285 | return $value->$attribute; |
| 286 | } |
| 287 | |
| 288 | $value = wp_list_pluck( $value, $attribute ); |
| 289 | $value = implode( ',', $value ); |
| 290 | |
| 291 | return $value; |
| 292 | } |
| 293 | |
| 294 | add_shortcode( 'rwmb_meta', 'rwmb_meta_shortcode' ); |
| 295 | } |
| 296 | |
| 297 | if ( ! function_exists( 'rwmb_get_registry' ) ) { |
| 298 | /** |
| 299 | * Get the registry by type. |
| 300 | * Always return the same instance of the registry. |
| 301 | * |
| 302 | * @param string $type Registry type. |
| 303 | * |
| 304 | * @return object |
| 305 | */ |
| 306 | function rwmb_get_registry( $type ) { |
| 307 | static $data = array(); |
| 308 | |
| 309 | $class = 'RWMB_' . RWMB_Helpers_String::title_case( $type ) . '_Registry'; |
| 310 | if ( ! isset( $data[ $type ] ) ) { |
| 311 | $data[ $type ] = new $class(); |
| 312 | } |
| 313 | |
| 314 | return $data[ $type ]; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if ( ! function_exists( 'rwmb_get_storage' ) ) { |
| 319 | /** |
| 320 | * Get storage instance. |
| 321 | * |
| 322 | * @param string $object_type Object type. Use post or term. |
| 323 | * @param RW_Meta_Box $meta_box Meta box object. Optional. |
| 324 | * @return RWMB_Storage_Interface |
| 325 | */ |
| 326 | function rwmb_get_storage( $object_type, $meta_box = null ) { |
| 327 | $class = 'RWMB_' . RWMB_Helpers_String::title_case( $object_type ) . '_Storage'; |
| 328 | $class = class_exists( $class ) ? $class : 'RWMB_Post_Storage'; |
| 329 | $storage = rwmb_get_registry( 'storage' )->get( $class ); |
| 330 | |
| 331 | return apply_filters( 'rwmb_get_storage', $storage, $object_type, $meta_box ); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if ( ! function_exists( 'rwmb_get_meta_box' ) ) { |
| 336 | /** |
| 337 | * Get meta box object from meta box data. |
| 338 | * |
| 339 | * @param array $meta_box Array of meta box data. |
| 340 | * @return RW_Meta_Box |
| 341 | */ |
| 342 | function rwmb_get_meta_box( $meta_box ) { |
| 343 | /** |
| 344 | * Allow filter meta box class name. |
| 345 | * |
| 346 | * @var string Meta box class name. |
| 347 | * @var array Meta box data. |
| 348 | */ |
| 349 | $class_name = apply_filters( 'rwmb_meta_box_class_name', 'RW_Meta_Box', $meta_box ); |
| 350 | |
| 351 | return new $class_name( $meta_box ); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 |