balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

functions.php (9611B)


      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_set_meta' ) ) {
     37 	/**
     38 	 * Set meta value.
     39 	 *
     40 	 * @param int    $object_id Object ID. Required.
     41 	 * @param string $key       Meta key. Required.
     42 	 * @param string $value     Meta value. Required.
     43 	 * @param array  $args      Array of arguments. Optional.
     44 	 */
     45 	function rwmb_set_meta( $object_id, $key, $value, $args = array() ) {
     46 		$args = wp_parse_args( $args );
     47 		$field = rwmb_get_field_settings( $key, $args, $object_id );
     48 
     49 		if ( false === $field ) {
     50 			return;
     51 		}
     52 
     53 		$old = RWMB_Field::call( $field, 'raw_meta', $object_id );
     54 		$new = RWMB_Field::process_value( $value, $object_id, $field );
     55 		RWMB_Field::call( $field, 'save', $new, $old, $object_id );
     56 	}
     57 }
     58 
     59 if ( ! function_exists( 'rwmb_get_field_settings' ) ) {
     60 	/**
     61 	 * Get field settings.
     62 	 *
     63 	 * @param string   $key       Meta key. Required.
     64 	 * @param array    $args      Array of arguments. Optional.
     65 	 * @param int|null $object_id Object ID. null for current post. Optional.
     66 	 *
     67 	 * @return array
     68 	 */
     69 	function rwmb_get_field_settings( $key, $args = array(), $object_id = null ) {
     70 		$args = wp_parse_args(
     71 			$args,
     72 			array(
     73 				'object_type' => 'post',
     74 				'type'        => '',
     75 			)
     76 		);
     77 
     78 		/**
     79 		 * Filter meta type from object type and object id.
     80 		 *
     81 		 * @var string     Meta type, default is post type name.
     82 		 * @var string     Object type.
     83 		 * @var string|int Object id.
     84 		 */
     85 		$type = apply_filters( 'rwmb_meta_type', $args['type'], $args['object_type'], $object_id );
     86 		if ( ! $type ) {
     87 			$type = get_post_type( $object_id );
     88 		}
     89 
     90 		return rwmb_get_registry( 'field' )->get( $key, $type, $args['object_type'] );
     91 	}
     92 }
     93 
     94 if ( ! function_exists( 'rwmb_meta_legacy' ) ) {
     95 	/**
     96 	 * Get post meta.
     97 	 *
     98 	 * @param string   $key     Meta key. Required.
     99 	 * @param array    $args    Array of arguments. Optional.
    100 	 * @param int|null $post_id Post ID. null for current post. Optional.
    101 	 *
    102 	 * @return mixed
    103 	 */
    104 	function rwmb_meta_legacy( $key, $args = array(), $post_id = null ) {
    105 		$args  = wp_parse_args(
    106 			$args,
    107 			array(
    108 				'type'     => 'text',
    109 				'multiple' => false,
    110 				'clone'    => false,
    111 			)
    112 		);
    113 		$field = array(
    114 			'id'       => $key,
    115 			'type'     => $args['type'],
    116 			'clone'    => $args['clone'],
    117 			'multiple' => $args['multiple'],
    118 		);
    119 
    120 		$method = 'get_value';
    121 		switch ( $args['type'] ) {
    122 			case 'taxonomy':
    123 			case 'taxonomy_advanced':
    124 				$field['taxonomy'] = $args['taxonomy'];
    125 				break;
    126 			case 'map':
    127 			case 'osm':
    128 			case 'oembed':
    129 				$method = 'the_value';
    130 				break;
    131 		}
    132 		$field = RWMB_Field::call( 'normalize', $field );
    133 
    134 		return RWMB_Field::call( $method, $field, $args, $post_id );
    135 	}
    136 }
    137 
    138 if ( ! function_exists( 'rwmb_get_value' ) ) {
    139 	/**
    140 	 * Get value of custom field.
    141 	 * This is used to replace old version of rwmb_meta key.
    142 	 *
    143 	 * @param  string   $field_id Field ID. Required.
    144 	 * @param  array    $args     Additional arguments. Rarely used. See specific fields for details.
    145 	 * @param  int|null $post_id  Post ID. null for current post. Optional.
    146 	 *
    147 	 * @return mixed false if field doesn't exist. Field value otherwise.
    148 	 */
    149 	function rwmb_get_value( $field_id, $args = array(), $post_id = null ) {
    150 		$args  = wp_parse_args( $args );
    151 		$field = rwmb_get_field_settings( $field_id, $args, $post_id );
    152 
    153 		// Get field value.
    154 		$value = $field ? RWMB_Field::call( 'get_value', $field, $args, $post_id ) : false;
    155 
    156 		/*
    157 		 * Allow developers to change the returned value of field.
    158 		 * For version < 4.8.2, the filter name was 'rwmb_get_field'.
    159 		 *
    160 		 * @param mixed    $value   Field value.
    161 		 * @param array    $field   Field parameters.
    162 		 * @param array    $args    Additional arguments. Rarely used. See specific fields for details.
    163 		 * @param int|null $post_id Post ID. null for current post. Optional.
    164 		 */
    165 		$value = apply_filters( 'rwmb_get_value', $value, $field, $args, $post_id );
    166 
    167 		return $value;
    168 	}
    169 }
    170 
    171 if ( ! function_exists( 'rwmb_the_value' ) ) {
    172 	/**
    173 	 * Display the value of a field
    174 	 *
    175 	 * @param  string   $field_id Field ID. Required.
    176 	 * @param  array    $args     Additional arguments. Rarely used. See specific fields for details.
    177 	 * @param  int|null $post_id  Post ID. null for current post. Optional.
    178 	 * @param  bool     $echo     Display field meta value? Default `true` which works in almost all cases. We use `false` for  the [rwmb_meta] shortcode.
    179 	 *
    180 	 * @return string
    181 	 */
    182 	function rwmb_the_value( $field_id, $args = array(), $post_id = null, $echo = true ) {
    183 		$args  = wp_parse_args( $args );
    184 		$field = rwmb_get_field_settings( $field_id, $args, $post_id );
    185 
    186 		if ( ! $field ) {
    187 			return '';
    188 		}
    189 
    190 		$output = RWMB_Field::call( 'the_value', $field, $args, $post_id );
    191 
    192 		/*
    193 		 * Allow developers to change the returned value of field.
    194 		 * For version < 4.8.2, the filter name was 'rwmb_get_field'.
    195 		 *
    196 		 * @param mixed    $value   Field HTML output.
    197 		 * @param array    $field   Field parameters.
    198 		 * @param array    $args    Additional arguments. Rarely used. See specific fields for details.
    199 		 * @param int|null $post_id Post ID. null for current post. Optional.
    200 		 */
    201 		$output = apply_filters( 'rwmb_the_value', $output, $field, $args, $post_id );
    202 
    203 		if ( $echo ) {
    204 			echo $output; // WPCS: XSS OK.
    205 		}
    206 
    207 		return $output;
    208 	}
    209 }
    210 
    211 if ( ! function_exists( 'rwmb_get_object_fields' ) ) {
    212 	/**
    213 	 * Get defined meta fields for object.
    214 	 *
    215 	 * @param int|string $type_or_id  Object ID or post type / taxonomy (for terms) / user (for users).
    216 	 * @param string     $object_type Object type. Use post, term.
    217 	 *
    218 	 * @return array
    219 	 */
    220 	function rwmb_get_object_fields( $type_or_id, $object_type = 'post' ) {
    221 		$meta_boxes = rwmb_get_registry( 'meta_box' )->get_by( array( 'object_type' => $object_type ) );
    222 		array_walk( $meta_boxes, 'rwmb_check_meta_box_supports', array( $object_type, $type_or_id ) );
    223 		$meta_boxes = array_filter( $meta_boxes );
    224 
    225 		$fields = array();
    226 		foreach ( $meta_boxes as $meta_box ) {
    227 			foreach ( $meta_box->fields as $field ) {
    228 				$fields[ $field['id'] ] = $field;
    229 			}
    230 		}
    231 
    232 		return $fields;
    233 	}
    234 }
    235 
    236 if ( ! function_exists( 'rwmb_check_meta_box_supports' ) ) {
    237 	/**
    238 	 * Check if a meta box supports an object.
    239 	 *
    240 	 * @param  object $meta_box    Meta Box object.
    241 	 * @param  int    $key         Not used.
    242 	 * @param  array  $object_data Object data (type and ID).
    243 	 */
    244 	function rwmb_check_meta_box_supports( &$meta_box, $key, $object_data ) {
    245 		list( $object_type, $type_or_id ) = $object_data;
    246 
    247 		$type = null;
    248 		$prop = null;
    249 		switch ( $object_type ) {
    250 			case 'post':
    251 				$type = is_numeric( $type_or_id ) ? get_post_type( $type_or_id ) : $type_or_id;
    252 				$prop = 'post_types';
    253 				break;
    254 			case 'term':
    255 				$type = $type_or_id;
    256 				if ( is_numeric( $type_or_id ) ) {
    257 					$term = get_term( $type_or_id );
    258 					$type = is_array( $term ) ? $term->taxonomy : null;
    259 				}
    260 				$prop = 'taxonomies';
    261 				break;
    262 			case 'user':
    263 				$type = 'user';
    264 				$prop = 'user';
    265 				break;
    266 			case 'setting':
    267 				$type = $type_or_id;
    268 				$prop = 'settings_pages';
    269 				break;
    270 		}
    271 		if ( ! $type ) {
    272 			$meta_box = false;
    273 			return;
    274 		}
    275 		if ( isset( $meta_box->meta_box[ $prop ] ) && ! in_array( $type, $meta_box->meta_box[ $prop ], true ) ) {
    276 			$meta_box = false;
    277 		}
    278 	}
    279 }
    280 
    281 if ( ! function_exists( 'rwmb_get_registry' ) ) {
    282 	/**
    283 	 * Get the registry by type.
    284 	 * Always return the same instance of the registry.
    285 	 *
    286 	 * @param string $type Registry type.
    287 	 *
    288 	 * @return object
    289 	 */
    290 	function rwmb_get_registry( $type ) {
    291 		static $data = array();
    292 
    293 		$class = 'RWMB_' . RWMB_Helpers_String::title_case( $type ) . '_Registry';
    294 		if ( ! isset( $data[ $type ] ) ) {
    295 			$data[ $type ] = new $class();
    296 		}
    297 
    298 		return $data[ $type ];
    299 	}
    300 }
    301 
    302 if ( ! function_exists( 'rwmb_get_storage' ) ) {
    303 	/**
    304 	 * Get storage instance.
    305 	 *
    306 	 * @param string      $object_type Object type. Use post or term.
    307 	 * @param RW_Meta_Box $meta_box    Meta box object. Optional.
    308 	 * @return RWMB_Storage_Interface
    309 	 */
    310 	function rwmb_get_storage( $object_type, $meta_box = null ) {
    311 		$class   = 'RWMB_' . RWMB_Helpers_String::title_case( $object_type ) . '_Storage';
    312 		$class   = class_exists( $class ) ? $class : 'RWMB_Post_Storage';
    313 		$storage = rwmb_get_registry( 'storage' )->get( $class );
    314 
    315 		return apply_filters( 'rwmb_get_storage', $storage, $object_type, $meta_box );
    316 	}
    317 }
    318 
    319 if ( ! function_exists( 'rwmb_request' ) ) {
    320 	/**
    321 	 * Get request object.
    322 	 *
    323 	 * @return RWMB_Request
    324 	 */
    325 	function rwmb_request() {
    326 		static $request;
    327 		if ( ! $request ) {
    328 			$request = new RWMB_Request();
    329 		}
    330 		return $request;
    331 	}
    332 }