balmet.com

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

object-choice.php (4174B)


      1 <?php
      2 /**
      3  * The object choice class which allows users to select specific objects in WordPress.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * Abstract field to select an object: post, user, taxonomy, etc.
     10  */
     11 abstract class RWMB_Object_Choice_Field extends RWMB_Choice_Field {
     12 	/**
     13 	 * Show field HTML.
     14 	 * Populate field options before showing to make sure query is made only once.
     15 	 *
     16 	 * @param array $field   Field parameters.
     17 	 * @param bool  $saved   Whether the meta box is saved at least once.
     18 	 * @param int   $post_id Post ID.
     19 	 */
     20 	public static function show( $field, $saved, $post_id = 0 ) {
     21 		// Get unique saved IDs for ajax fields.
     22 		$meta = self::call( $field, 'meta', $post_id, $saved );
     23 		$meta = self::filter( 'field_meta', $meta, $field, $saved );
     24 		$meta = RWMB_Helpers_Array::flatten( (array) $meta );
     25 		$meta = array_unique( array_filter( array_map( 'absint', $meta ) ) );
     26 		sort( $meta );
     27 
     28 		$field['options'] = self::call( $field, 'query', $meta );
     29 
     30 		parent::show( $field, $saved, $post_id );
     31 	}
     32 
     33 	/**
     34 	 * Get field HTML.
     35 	 *
     36 	 * @param mixed $meta  Meta value.
     37 	 * @param array $field Field parameters.
     38 	 * @return string
     39 	 */
     40 	public static function html( $meta, $field ) {
     41 		$html = call_user_func( array( self::get_type_class( $field ), 'html' ), $meta, $field );
     42 
     43 		if ( $field['add_new'] ) {
     44 			$html .= self::call( 'add_new_form', $field );
     45 		}
     46 
     47 		return $html;
     48 	}
     49 
     50 	/**
     51 	 * Render "Add New" form
     52 	 *
     53 	 * @param array $field Field settings.
     54 	 * @return string
     55 	 */
     56 	public static function add_new_form( $field ) {
     57 		return '';
     58 	}
     59 
     60 	/**
     61 	 * Normalize parameters for field.
     62 	 *
     63 	 * @param array $field Field parameters.
     64 	 *
     65 	 * @return array
     66 	 */
     67 	public static function normalize( $field ) {
     68 		$field = parent::normalize( $field );
     69 		$field = wp_parse_args(
     70 			$field,
     71 			array(
     72 				'flatten'    => true,
     73 				'query_args' => array(),
     74 				'field_type' => 'select_advanced',
     75 				'add_new'    => false,
     76 				'ajax'       => true,
     77 			)
     78 		);
     79 		if ( 'select_advanced' !== $field['field_type'] ) {
     80 			$field['ajax'] = false;
     81 		}
     82 		if ( 'checkbox_tree' === $field['field_type'] ) {
     83 			$field['field_type'] = 'checkbox_list';
     84 			$field['flatten']    = false;
     85 		}
     86 		if ( 'radio_list' === $field['field_type'] ) {
     87 			$field['field_type'] = 'radio';
     88 		}
     89 		$field = call_user_func( array( self::get_type_class( $field ), 'normalize' ), $field );
     90 
     91 		return $field;
     92 	}
     93 
     94 	/**
     95 	 * Set ajax parameters.
     96 	 *
     97 	 * @param array $field Field settings.
     98 	 */
     99 	protected static function set_ajax_params( &$field ) {
    100 		if ( ! $field['ajax'] ) {
    101 			return;
    102 		}
    103 
    104 		if ( empty( $field['js_options']['ajax'] ) ) {
    105 			$field['js_options']['ajax'] = array();
    106 		}
    107 		$field['js_options']['ajax']      = wp_parse_args(
    108 			array(
    109 				'url' => admin_url( 'admin-ajax.php' ),
    110 			),
    111 			$field['js_options']['ajax']
    112 		);
    113 		$field['js_options']['ajax_data'] = array(
    114 			'field'    => array(
    115 				'id'         => $field['id'],
    116 				'type'       => $field['type'],
    117 				'query_args' => $field['query_args'],
    118 			),
    119 			'_wpnonce' => wp_create_nonce( 'query' ),
    120 		);
    121 	}
    122 
    123 	/**
    124 	 * Get the attributes for a field.
    125 	 *
    126 	 * @param array $field Field parameters.
    127 	 * @param mixed $value Meta value.
    128 	 *
    129 	 * @return array
    130 	 */
    131 	public static function get_attributes( $field, $value = null ) {
    132 		$attributes = call_user_func( array( self::get_type_class( $field ), 'get_attributes' ), $field, $value );
    133 		if ( 'select_advanced' === $field['field_type'] ) {
    134 			$attributes['class'] .= ' rwmb-select_advanced';
    135 		} elseif ( 'select' === $field['field_type'] ) {
    136 			$attributes['class'] .= ' rwmb-select';
    137 		}
    138 		return $attributes;
    139 	}
    140 
    141 	/**
    142 	 * Enqueue scripts and styles.
    143 	 */
    144 	public static function admin_enqueue_scripts() {
    145 		RWMB_Input_List_Field::admin_enqueue_scripts();
    146 		RWMB_Select_Field::admin_enqueue_scripts();
    147 		RWMB_Select_Tree_Field::admin_enqueue_scripts();
    148 		RWMB_Select_Advanced_Field::admin_enqueue_scripts();
    149 	}
    150 
    151 	/**
    152 	 * Get correct rendering class for the field.
    153 	 *
    154 	 * @param array $field Field parameters.
    155 	 * @return string
    156 	 */
    157 	protected static function get_type_class( $field ) {
    158 		return RWMB_Helpers_Field::get_class(
    159 			array(
    160 				'type' => $field['field_type'],
    161 			)
    162 		);
    163 	}
    164 }