cmb_Meta_Box_types

CMB field types.

Defined (1)

The class is defined in the following location(s).

/admin/includes/CMBF/helpers/cmb_Meta_Box_types.php  
  1. class cmb_Meta_Box_types { 
  2.  
  3. /** 
  4. * An iterator value for repeatable fields 
  5. * @var integer 
  6. * @since 1.0.0 
  7. */ 
  8. public $iterator = 0; 
  9.  
  10. /** 
  11. * Current field 
  12. * @var array 
  13. * @since 1.0.0 
  14. */ 
  15. public $field; 
  16.  
  17. public function __construct( $field ) { 
  18. $this->field = $field; 
  19.  
  20. /** 
  21. * Default fallback. Allows rendering fields via "cmb_render_$name" hook 
  22. * @since 1.0.0 
  23. * @param string $name Non-existent method name 
  24. * @param array $arguments All arguments passed to the method 
  25. */ 
  26. public function __call( $name, $arguments ) { 
  27. // When a non-registered field is called, send it through an action. 
  28. do_action( "cmb_render_$name", $this->field->args(), $this->field->escaped_value(), $this->field->object_id, $this->field->object_type, $this ); 
  29.  
  30. /** 
  31. * Render a field (and handle repeatable) 
  32. * @since 1.1.0 
  33. */ 
  34. public function render() { 
  35. if ( $this->field->args( 'repeatable' ) ) { 
  36. $this->render_repeatable_field(); 
  37. } else { 
  38. $this->_render(); 
  39.  
  40. /** 
  41. * Render a field type 
  42. * @since 1.1.0 
  43. */ 
  44. protected function _render() { 
  45. echo $this->{$this->field->type()}(); 
  46.  
  47. /** 
  48. * Checks if we can get a post object, and if so, uses `get_the_terms` which utilizes caching 
  49. * @since 1.0.2 
  50. * @return mixed Array of terms on success 
  51. */ 
  52. public function get_object_terms() { 
  53. $object_id = $this->field->object_id; 
  54. $taxonomy = $this->field->args( 'taxonomy' ); 
  55.  
  56. if ( ! $post = get_post( $object_id ) ) { 
  57.  
  58. $cache_key = 'cmb-cache-'. $taxonomy .'-'. $object_id; 
  59.  
  60. // Check cache 
  61. $cached = $test = get_transient( $cache_key ); 
  62. if ( $cached ) 
  63. return $cached; 
  64.  
  65. $cached = wp_get_object_terms( $object_id, $taxonomy ); 
  66. // Do our own (minimal) caching. Long enough for a page-load. 
  67. $set = set_transient( $cache_key, $cached, 60 ); 
  68. return $cached; 
  69.  
  70. // WP caches internally so it's better to use 
  71. return get_the_terms( $post, $taxonomy ); 
  72.  
  73.  
  74. /** 
  75. * Determine a file's extension 
  76. * @since 1.0.0 
  77. * @param string $file File url 
  78. * @return string|false File extension or false 
  79. */ 
  80. public function get_file_ext( $file ) { 
  81. $parsed = @parse_url( $file, PHP_URL_PATH ); 
  82. return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false; 
  83.  
  84. /** 
  85. * Determines if a file has a valid image extension 
  86. * @since 1.0.0 
  87. * @param string $file File url 
  88. * @return bool Whether file has a valid image extension 
  89. */ 
  90. public function is_valid_img_ext( $file ) { 
  91. $file_ext = $this->get_file_ext( $file ); 
  92.  
  93. $this->valid = empty( $this->valid ) 
  94. ? (array) apply_filters( 'cmb_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif', 'ico', 'icon' ) ) 
  95. : $this->valid; 
  96.  
  97. return ( $file_ext && in_array( $file_ext, $this->valid ) ); 
  98.  
  99. /** 
  100. * Handles parsing and filtering attributes while preserving any passed in via field config. 
  101. * @since 1.1.0 
  102. * @param array $args Override arguments 
  103. * @param string $element Element for filter 
  104. * @param array $defaults Default arguments 
  105. * @return array Parsed and filtered arguments 
  106. */ 
  107. public function parse_args( $args, $element, $defaults ) { 
  108. return wp_parse_args( apply_filters( "cmb_{$element}_attributes", $this->field->maybe_set_attributes( $args ), $this->field, $this ), $defaults ); 
  109.  
  110. /** 
  111. * Combines attributes into a string for a form element 
  112. * @since 1.1.0 
  113. * @param array $attrs Attributes to concatenate 
  114. * @param array $attr_exclude Attributes that should NOT be concatenated 
  115. * @return string String of attributes for form element 
  116. */ 
  117. public function concat_attrs( $attrs, $attr_exclude = array() ) { 
  118. $attributes = ''; 
  119. foreach ( $attrs as $attr => $val ) { 
  120. if ( ! in_array( $attr, (array) $attr_exclude, true ) ) 
  121. $attributes .= sprintf( ' %s="%s"', $attr, $val ); 
  122. return $attributes; 
  123.  
  124. /** 
  125. * Generates html for an option element 
  126. * @since 1.1.0 
  127. * @param string $opt_label Option label 
  128. * @param string $opt_value Option value 
  129. * @param mixed $selected Selected attribute if option is selected 
  130. * @return string Generated option element html 
  131. */ 
  132. public function option( $opt_label, $opt_value, $selected ) { 
  133. return sprintf( "\t".'<option value="%s" %s>%s</option>', $opt_value, selected( $selected, true, false ), $opt_label )."\n"; 
  134.  
  135. /** 
  136. * Generates options html 
  137. * @since 1.1.0 
  138. * @param array $args Optional arguments 
  139. * @param string $method Method to generate individual option item 
  140. * @return string Concatenated html options 
  141. */ 
  142. public function concat_options( $args = array(), $method = 'list_input' ) { 
  143.  
  144. $options = (array) $this->field->args( 'options' ); 
  145. $saved_value = $this->field->escaped_value(); 
  146. $value = $saved_value ? $saved_value : $this->field->args( 'default' ); 
  147.  
  148. $_options = ''; $i = 1; 
  149. foreach ( $options as $option_key => $option ) { 
  150.  
  151. // Check for the "old" way 
  152. $opt_label = is_array( $option ) && array_key_exists( 'name', $option ) ? $option['name'] : $option; 
  153. $opt_value = is_array( $option ) && array_key_exists( 'value', $option ) ? $option['value'] : $option_key; 
  154. // Check if this option is the value of the input 
  155. $is_current = $value == $opt_value; 
  156.  
  157. if ( ! empty( $args ) ) { 
  158. // Clone args & modify for just this item 
  159. $this_args = $args; 
  160. $this_args['value'] = $opt_value; 
  161. $this_args['label'] = $opt_label; 
  162. if ( $is_current ) 
  163. $this_args['checked'] = 'checked'; 
  164.  
  165. $_options .= $this->$method( $this_args, $i ); 
  166. } else { 
  167. $_options .= $this->option( $opt_label, $opt_value, $is_current ); 
  168. $i++; 
  169. return $_options; 
  170.  
  171. /** 
  172. * Generates html for list item with input 
  173. * @since 1.1.0 
  174. * @param array $args Override arguments 
  175. * @param int $i Iterator value 
  176. * @return string Gnerated list item html 
  177. */ 
  178. public function list_input( $args = array(), $i ) { 
  179. $args = $this->parse_args( $args, 'list_input', array( 
  180. 'type' => 'radio',  
  181. 'class' => 'cmb_option',  
  182. 'name' => $this->_name(),  
  183. 'id' => $this->_id( $i ),  
  184. 'value' => $this->field->escaped_value(),  
  185. 'label' => '',  
  186. ) ); 
  187.  
  188. return sprintf( "\t".'<li><input%s/> <label for="%s">%s</label></li>'."\n", $this->concat_attrs( $args, 'label' ), $args['id'], $args['label'] ); 
  189.  
  190. /** 
  191. * Generates html for list item with checkbox input 
  192. * @since 1.1.0 
  193. * @param array $args Override arguments 
  194. * @param int $i Iterator value 
  195. * @return string Gnerated list item html 
  196. */ 
  197. public function list_input_checkbox( $args, $i ) { 
  198. unset( $args['selected'] ); 
  199. $saved_value = $this->field->escaped_value(); 
  200. if ( is_array( $saved_value ) && in_array( $args['value'], $saved_value ) ) { 
  201. $args['checked'] = 'checked'; 
  202. return $this->list_input( $args, $i ); 
  203.  
  204. /** 
  205. * Generates repeatable field table markup 
  206. * @since 1.0.0 
  207. */ 
  208. public function render_repeatable_field() { 
  209. $table_id = $this->field->id() .'_repeat'; 
  210.  
  211. $this->_desc( true, true ); 
  212. ?> 
  213.  
  214. <table id="<?php echo $table_id; ?>" class="cmb-repeat-table"> 
  215. <tbody> 
  216. <?php $this->repeatable_rows(); ?> 
  217. </tbody> 
  218. </table> 
  219. <p class="add-row"> 
  220. <a data-selector="<?php echo $table_id; ?>" class="add-row-button button" href="#"><?php _e( 'Add Row', 'cmb' ); ?></a> 
  221. </p> 
  222.  
  223. <?php 
  224. // reset iterator 
  225. $this->iterator = 0; 
  226.  
  227. /** 
  228. * Generates repeatable field rows 
  229. * @since 1.1.0 
  230. */ 
  231. public function repeatable_rows() { 
  232. $meta_value = $this->field->escaped_value(); 
  233. // check for default content 
  234. $default = $this->field->args( 'default' ); 
  235.  
  236. // check for saved data 
  237. if ( ! empty( $meta_value ) ) { 
  238. $meta_value = is_array( $meta_value ) ? array_filter( $meta_value ) : $meta_value; 
  239. $meta_value = ! empty( $meta_value ) ? $meta_value : $default; 
  240. } else { 
  241. $meta_value = $default; 
  242.  
  243. // Loop value array and add a row 
  244. if ( ! empty( $meta_value ) ) { 
  245. foreach ( (array) $meta_value as $val ) { 
  246. $this->field->escaped_value = $val; 
  247. $this->repeat_row(); 
  248. $this->iterator++; 
  249. } else { 
  250. // Otherwise add one row 
  251. $this->repeat_row(); 
  252.  
  253. // Then add an empty row 
  254. $this->field->escaped_value = ''; 
  255. $this->iterator = $this->iterator ? $this->iterator : 1; 
  256. $this->repeat_row( 'empty-row' ); 
  257.  
  258. /** 
  259. * Generates a repeatable row's markup 
  260. * @since 1.1.0 
  261. * @param string $class Repeatable table row's class 
  262. */ 
  263. protected function repeat_row( $class = 'repeat-row' ) { 
  264. ?> 
  265.  
  266. <tr class="<?php echo $class; ?>"> 
  267. <td> 
  268. <?php $this->_render(); ?> 
  269. </td> 
  270. <td class="remove-row"> 
  271. <a class="button remove-row-button" href="#"><?php _e( 'Remove', 'cmb' ); ?></a> 
  272. </td> 
  273. </tr> 
  274.  
  275. <?php 
  276.  
  277. /** 
  278. * Generates description markup 
  279. * @since 1.0.0 
  280. * @param boolean $paragraph Paragraph tag or span 
  281. * @param boolean $echo Whether to echo description or only return it 
  282. * @return string Field's description markup 
  283. */ 
  284. public function _desc( $paragraph = false, $echo = false ) { 
  285. // Prevent description from printing multiple times for repeatable fields 
  286. if ( $this->field->args( 'repeatable' ) || $this->iterator > 0 ) { 
  287. return ''; 
  288. $tag = $paragraph ? 'p' : 'span'; 
  289. $desc = "\n<$tag class=\"cmb_metabox_description\">{$this->field->args( 'description' )}</$tag>\n"; 
  290. if ( $echo ) 
  291. echo $desc; 
  292. return $desc; 
  293.  
  294. /** 
  295. * Generate field name attribute 
  296. * @since 1.1.0 
  297. * @param string $suffix For multi-part fields 
  298. * @return string Name attribute 
  299. */ 
  300. public function _name( $suffix = '' ) { 
  301. return $this->field->args( '_name' ) . ( $this->field->args( 'repeatable' ) ? '['. $this->iterator .']' : '' ) . $suffix; 
  302.  
  303. /** 
  304. * Generate field id attribute 
  305. * @since 1.1.0 
  306. * @param string $suffix For multi-part fields 
  307. * @return string Id attribute 
  308. */ 
  309. public function _id( $suffix = '' ) { 
  310. return $this->field->id() . $suffix . ( $this->field->args( 'repeatable' ) ? '_'. $this->iterator .'" data-iterator="'. $this->iterator : '' ); 
  311.  
  312. /** 
  313. * Handles outputting an 'input' element 
  314. * @since 1.1.0 
  315. * @param array $args Override arguments 
  316. * @return string Form input element 
  317. */ 
  318. public function input( $args = array() ) { 
  319. $args = $this->parse_args( $args, 'input', array( 
  320. 'type' => 'text',  
  321. 'class' => 'regular-text',  
  322. 'name' => $this->_name(),  
  323. 'id' => $this->_id(),  
  324. 'value' => $this->field->escaped_value(),  
  325. 'desc' => $this->_desc( true ),  
  326. ) ); 
  327.  
  328. return sprintf( '<input%s/>%s', $this->concat_attrs( $args, 'desc' ), $args['desc'] ); 
  329.  
  330. /** 
  331. * Handles outputting an 'textarea' element 
  332. * @since 1.1.0 
  333. * @param array $args Override arguments 
  334. * @return string Form textarea element 
  335. */ 
  336. public function textarea( $args = array() ) { 
  337. $args = $this->parse_args( $args, 'textarea', array( 
  338. 'class' => 'cmb_textarea',  
  339. 'name' => $this->_name(),  
  340. 'id' => $this->_id(),  
  341. 'cols' => 60,  
  342. 'rows' => 10,  
  343. 'value' => $this->field->escaped_value( 'esc_textarea' ),  
  344. 'desc' => $this->_desc( true ),  
  345. ) ); 
  346. return sprintf( '<textarea%s>%s</textarea>%s', $this->concat_attrs( $args, array( 'desc', 'value' ) ), $args['value'], $args['desc'] ); 
  347.  
  348. /** 
  349. * Begin Field Types 
  350. */ 
  351.  
  352. public function text() { 
  353. return $this->input(); 
  354.  
  355. public function text_small() { 
  356. return $this->input( array( 'class' => 'cmb_text_small', 'desc' => $this->_desc() ) ); 
  357.  
  358. public function text_medium() { 
  359. return $this->input( array( 'class' => 'cmb_text_medium', 'desc' => $this->_desc() ) ); 
  360.  
  361. public function text_email() { 
  362. return $this->input( array( 'class' => 'cmb_text_email cmb_text_medium', 'type' => 'email' ) ); 
  363.  
  364. public function text_url() { 
  365. return $this->input( array( 'class' => 'cmb_text_url cmb_text_medium regular-text', 'value' => $this->field->escaped_value( 'esc_url' ) ) ); 
  366.  
  367. public function text_date() { 
  368. return $this->input( array( 'class' => 'cmb_text_small cmb_datepicker', 'desc' => $this->_desc() ) ); 
  369.  
  370. public function text_time() { 
  371. return $this->input( array( 'class' => 'cmb_timepicker text_time', 'desc' => $this->_desc() ) ); 
  372.  
  373. public function text_money() { 
  374. return ( ! $this->field->args( 'before' ) ? '$ ' : ' ' ) . $this->input( array( 'class' => 'cmb_text_money', 'desc' => $this->_desc() ) ); 
  375.  
  376. public function textarea_small() { 
  377. return $this->textarea( array( 'class' => 'cmb_textarea_small', 'rows' => 4 ) ); 
  378.  
  379. public function textarea_code() { 
  380. return sprintf( '<pre>%s</pre>', $this->textarea( array( 'class' => 'cmb_textarea_code' ) ) ); 
  381.  
  382. public function wysiwyg( $args = array() ) { 
  383. extract( $this->parse_args( $args, 'input', array( 
  384. 'id' => $this->_id(),  
  385. 'value' => $this->field->escaped_value( 'stripslashes' ),  
  386. 'desc' => $this->_desc( true ),  
  387. 'options' => $this->field->args( 'options' ),  
  388. ) ) ); 
  389.  
  390. wp_editor( $value, $id, $options ); 
  391. echo $desc; 
  392.  
  393. public function text_date_timestamp() { 
  394. $meta_value = $this->field->escaped_value(); 
  395. $value = ! empty( $meta_value ) ? date( $this->field->args( 'date_format' ), $meta_value ) : ''; 
  396. return $this->input( array( 'class' => 'cmb_text_small cmb_datepicker', 'value' => $value ) ); 
  397.  
  398. public function text_datetime_timestamp( $meta_value = '' ) { 
  399. $desc = ''; 
  400. if ( ! $meta_value ) { 
  401. $meta_value = $this->field->escaped_value(); 
  402. // This will be used if there is a select_timezone set for this field 
  403. $tz_offset = $this->field->field_timezone_offset(); 
  404. if ( ! empty( $tz_offset ) ) { 
  405. $meta_value -= $tz_offset; 
  406. $desc = $this->_desc(); 
  407.  
  408. $inputs = array( 
  409. $this->input( array( 
  410. 'class' => 'cmb_text_small cmb_datepicker',  
  411. 'name' => $this->_name( '[date]' ),  
  412. 'id' => $this->_id( '_date' ),  
  413. 'value' => ! empty( $meta_value ) ? date( $this->field->args( 'date_format' ), $meta_value ) : '',  
  414. 'desc' => '',  
  415. ) ),  
  416. $this->input( array( 
  417. 'class' => 'cmb_timepicker text_time',  
  418. 'name' => $this->_name( '[time]' ),  
  419. 'id' => $this->_id( '_time' ),  
  420. 'value' => ! empty( $meta_value ) ? date( $this->field->args( 'time_format' ), $meta_value ) : '',  
  421. 'desc' => $desc,  
  422. ) ) 
  423. ); 
  424.  
  425. return implode( "\n", $inputs ); 
  426.  
  427. public function text_datetime_timestamp_timezone() { 
  428. $meta_value = $this->field->escaped_value(); 
  429. $datetime = unserialize( $meta_value ); 
  430. $meta_value = $tzstring = false; 
  431.  
  432. if ( $datetime && $datetime instanceof DateTime ) { 
  433. $tz = $datetime->getTimezone(); 
  434. $tzstring = $tz->getName(); 
  435. $meta_value = $datetime->getTimestamp() + $tz->getOffset( new DateTime( 'NOW' ) ); 
  436.  
  437. $inputs = $this->text_datetime_timestamp( $meta_value ); 
  438. $inputs .= '<select name="'. $this->_name( '[timezone]' ) .'" id="'. $this->_id( '_timezone' ) .'">'; 
  439. $inputs .= wp_timezone_choice( $tzstring ); 
  440. $inputs .= '</select>'. $this->_desc(); 
  441.  
  442. return $inputs; 
  443.  
  444. public function select_timezone() { 
  445. $this->field->args['default'] = $this->field->args( 'default' ) 
  446. ? $this->field->args( 'default' ) 
  447. : cmb_Meta_Box::timezone_string(); 
  448.  
  449. $meta_value = $this->field->escaped_value(); 
  450.  
  451. return '<select name="'. $this->_name() .'" id="'. $this->_id() .'">'. wp_timezone_choice( $meta_value ) .'</select>'; 
  452.  
  453. public function colorpicker() { 
  454. $meta_value = $this->field->escaped_value(); 
  455. $hex_color = '(([a-fA-F0-9]) {3}) {1, 2}$'; 
  456. if ( preg_match( '/^' . $hex_color . '/i', $meta_value ) ) // Value is just 123abc, so prepend #. 
  457. $meta_value = '#' . $meta_value; 
  458. elseif ( ! preg_match( '/^#' . $hex_color . '/i', $meta_value ) ) // Value doesn't match #123abc, so sanitize to just #. 
  459. $meta_value = "#"; 
  460.  
  461. return $this->input( array( 'class' => 'cmb_colorpicker cmb_text_small', 'value' => $meta_value ) ); 
  462.  
  463. public function title() { 
  464. extract( $this->parse_args( array(), 'title', array( 
  465. 'tag' => $this->field->object_type == 'post' ? 'h5' : 'h3',  
  466. 'class' => 'cmb_metabox_title',  
  467. 'name' => $this->field->args( 'name' ),  
  468. 'desc' => $this->_desc( true ),  
  469. ) ) ); 
  470.  
  471. return sprintf( '<%1$s class="%2$s">%3$s</%1$s>%4$s', $tag, $class, $name, $desc ); 
  472.  
  473. public function select( $args = array() ) { 
  474. $args = $this->parse_args( $args, 'select', array( 
  475. 'class' => 'cmb_select',  
  476. 'name' => $this->_name(),  
  477. 'id' => $this->_id(),  
  478. 'desc' => $this->_desc( true ),  
  479. 'options' => $this->concat_options(),  
  480. ) ); 
  481.  
  482. $attrs = $this->concat_attrs( $args, array( 'desc', 'options' ) ); 
  483. return sprintf( '<select%s>%s</select>%s', $attrs, $args['options'], $args['desc'] ); 
  484.  
  485. public function taxonomy_select() { 
  486.  
  487. $names = $this->get_object_terms(); 
  488. $saved_term = is_wp_error( $names ) || empty( $names ) ? $this->field->args( 'default' ) : $names[0]->slug; 
  489. $terms = get_terms( $this->field->args( 'taxonomy' ), 'hide_empty=0' ); 
  490. $options = ''; 
  491.  
  492. foreach ( $terms as $term ) { 
  493. $selected = $saved_term == $term->slug; 
  494. $options .= $this->option( $term->name, $term->slug, $selected ); 
  495.  
  496. return $this->select( array( 'options' => $options ) ); 
  497.  
  498. public function radio( $args = array(), $type = 'radio' ) { 
  499. extract( $this->parse_args( $args, $type, array( 
  500. 'class' => 'cmb_radio_list cmb_list',  
  501. 'options' => $this->concat_options( array( 'label' => 'test' ) ),  
  502. 'desc' => $this->_desc( true ),  
  503. ) ) ); 
  504.  
  505. return sprintf( '<ul class="%s">%s</ul>%s', $class, $options, $desc ); 
  506.  
  507. public function radio_inline() { 
  508. return $this->radio( array(), 'radio_inline' ); 
  509.  
  510. public function multicheck( $type = 'checkbox' ) { 
  511. return $this->radio( array( 'class' => 'cmb_checkbox_list cmb_list', 'options' => $this->concat_options( array( 'type' => 'checkbox', 'name' => $this->_name() .'[]' ), 'list_input_checkbox' ) ), $type ); 
  512.  
  513. public function multicheck_inline() { 
  514. $this->multicheck( 'multicheck_inline' ); 
  515.  
  516. public function checkbox() { 
  517. $meta_value = $this->field->escaped_value(); 
  518. $args = array( 'type' => 'checkbox', 'class' => 'cmb_option cmb_list', 'value' => 'on', 'desc' => '' ); 
  519. if ( ! empty( $meta_value ) ) { 
  520. $args['checked'] = 'checked'; 
  521. return sprintf( '%s <label for="%s">%s</label>', $this->input( $args ), $this->_id(), $this->_desc() ); 
  522.  
  523. public function taxonomy_radio() { 
  524. $names = $this->get_object_terms(); 
  525. $saved_term = is_wp_error( $names ) || empty( $names ) ? $this->field->args( 'default' ) : $names[0]->slug; 
  526. $terms = get_terms( $this->field->args( 'taxonomy' ), 'hide_empty=0' ); 
  527. $options = ''; $i = 1; 
  528.  
  529. if ( ! $terms ) { 
  530. $options .= '<li><label>'. __( 'No terms', 'cmb' ) .'</label></li>'; 
  531. } else { 
  532. foreach ( $terms as $term ) { 
  533. $args = array( 
  534. 'value' => $term->slug,  
  535. 'label' => $term->name,  
  536. ); 
  537.  
  538. if ( $saved_term == $term->slug ) { 
  539. $args['checked'] = 'checked'; 
  540. $options .= $this->list_input( $args, $i ); 
  541. $i++; 
  542.  
  543. return $this->radio( array( 'options' => $options ), 'taxonomy_radio' ); 
  544.  
  545. public function taxonomy_radio_inline() { 
  546. $this->taxonomy_radio(); 
  547.  
  548. public function taxonomy_multicheck() { 
  549.  
  550. $names = $this->get_object_terms(); 
  551. $saved_terms = is_wp_error( $names ) || empty( $names ) 
  552. ? $this->field->args( 'default' ) 
  553. : wp_list_pluck( $names, 'slug' ); 
  554. $terms = get_terms( $this->field->args( 'taxonomy' ), 'hide_empty=0' ); 
  555. $name = $this->_name() .'[]'; 
  556. $options = ''; $i = 1; 
  557.  
  558. if ( ! $terms ) { 
  559. $options .= '<li><label>'. __( 'No terms', 'cmb' ) .'</label></li>'; 
  560. } else { 
  561.  
  562. foreach ( $terms as $term ) { 
  563. $args = array( 
  564. 'value' => $term->slug,  
  565. 'label' => $term->name,  
  566. 'type' => 'checkbox',  
  567. 'name' => $name,  
  568. ); 
  569.  
  570. if ( is_array( $saved_terms ) && in_array( $term->slug, $saved_terms ) ) { 
  571. $args['checked'] = 'checked'; 
  572. $options .= $this->list_input( $args, $i ); 
  573. $i++; 
  574.  
  575. return $this->radio( array( 'class' => 'cmb_checkbox_list cmb_list', 'options' => $options ), 'taxonomy_multicheck' ); 
  576.  
  577. public function taxonomy_multicheck_inline() { 
  578. $this->taxonomy_multicheck(); 
  579.  
  580. public function file_list() { 
  581. $meta_value = $this->field->escaped_value(); 
  582.  
  583. $name = $this->_name(); 
  584.  
  585. echo $this->input( array( 
  586. 'type' => 'hidden',  
  587. 'class' => 'cmb_upload_file cmb_upload_list',  
  588. 'size' => 45, 'desc' => '', 'value' => '',  
  589. ) ),  
  590. $this->input( array( 
  591. 'type' => 'button',  
  592. 'class' => 'cmb_upload_button button cmb_upload_list',  
  593. 'value' => __( 'Add or Upload File', 'cmb' ),  
  594. 'name' => '', 'id' => '',  
  595. ) ); 
  596.  
  597. echo '<ul id="', $this->_id( '_status' ) , '" class="cmb_media_status attach_list">'; 
  598.  
  599. if ( $meta_value && is_array( $meta_value ) ) { 
  600.  
  601. foreach ( $meta_value as $id => $fullurl ) { 
  602. $id_input = $this->input( array( 
  603. 'type' => 'hidden',  
  604. 'value' => $fullurl,  
  605. 'name' => $name .'['. $id .']',  
  606. 'id' => 'filelist-'. $id,  
  607. 'desc' => '', 'class' => '',  
  608. ) ); 
  609.  
  610. if ( $this->is_valid_img_ext( $fullurl ) ) { 
  611. echo 
  612. '<li class="img_status">',  
  613. wp_get_attachment_image( $id, $this->field->args( 'preview_size' ) ),  
  614. '<p class="cmb_remove_wrapper"><a href="#" class="cmb_remove_file_button">'. __( 'Remove Image', 'cmb' ) .'</a></p> 
  615. '. $id_input .' 
  616. </li>'; 
  617.  
  618. } else { 
  619. $parts = explode( '/', $fullurl ); 
  620. for ( $i = 0; $i < count( $parts ); ++$i ) { 
  621. $title = $parts[$i]; 
  622. echo 
  623. '<li>',  
  624. __( 'File:', 'cmb' ), ' <strong>', $title, '</strong>    (<a href="', $fullurl, '" target="_blank" rel="external">'. __( 'Download', 'cmb' ) .'</a> / <a href="#" class="cmb_remove_file_button">'. __( 'Remove', 'cmb' ) .'</a>) 
  625. '. $id_input .' 
  626. </li>'; 
  627.  
  628. echo '</ul>'; 
  629.  
  630. public function file() { 
  631. $meta_value = $this->field->escaped_value(); 
  632. $allow = $this->field->args( 'allow' ); 
  633. $input_type = ( 'url' == $allow || ( is_array( $allow ) && in_array( 'url', $allow ) ) ) 
  634. ? 'text' : 'hidden'; 
  635.  
  636. echo $this->input( array( 
  637. 'type' => $input_type,  
  638. 'class' => 'cmb_upload_file',  
  639. 'size' => 45,  
  640. 'desc' => '',  
  641. ) ),  
  642. '<input class="cmb_upload_button button" type="button" value="'. __( 'Add or Upload File', 'cmb' ) .'" />',  
  643. $this->_desc( true ); 
  644.  
  645. $cached_id = $this->_id(); 
  646. // Reset field args for attachment ID 
  647. $args = $this->field->args(); 
  648. $args['id'] = $args['_id'] . '_id'; 
  649. unset( $args['_id'], $args['_name'] ); 
  650.  
  651. // And get new field object 
  652. $this->field = new cmb_Meta_Box_field( $args, $this->field->group ); 
  653.  
  654. // Get ID value 
  655. $_id_value = $this->field->escaped_value( 'absint' ); 
  656.  
  657. // If there is no ID saved yet, try to get it from the url 
  658. if ( $meta_value && ! $_id_value ) { 
  659. $_id_value = cmb_Meta_Box::image_id_from_url( esc_url_raw( $meta_value ) ); 
  660.  
  661. echo $this->input( array( 
  662. 'type' => 'hidden',  
  663. 'class' => 'cmb_upload_file_id',  
  664. 'value' => $_id_value,  
  665. 'desc' => '',  
  666. ) ),  
  667. '<div id="', $this->_id( '_status' ) , '" class="cmb_media_status">'; 
  668. if ( ! empty( $meta_value ) ) { 
  669.  
  670. if ( $this->is_valid_img_ext( $meta_value ) ) { 
  671. echo '<div class="img_status">'; 
  672. echo '<img style="max-width: 350px; width: 100%; height: auto;" src="', $meta_value, '" alt="" />'; 
  673. echo '<p class="cmb_remove_wrapper"><a href="#" class="cmb_remove_file_button" rel="', $cached_id, '">'. __( 'Remove Image', 'cmb' ) .'</a></p>'; 
  674. echo '</div>'; 
  675. } else { 
  676. // $file_ext = $this->get_file_ext( $meta_value ); 
  677. $parts = explode( '/', $meta_value ); 
  678. for ( $i = 0; $i < count( $parts ); ++$i ) { 
  679. $title = $parts[$i]; 
  680. echo __( 'File:', 'cmb' ), ' <strong>', $title, '</strong>    (<a href="', $meta_value, '" target="_blank" rel="external">'. __( 'Download', 'cmb' ) .'</a> / <a href="#" class="cmb_remove_file_button" rel="', $cached_id, '">'. __( 'Remove', 'cmb' ) .'</a>)'; 
  681. echo '</div>'; 
  682.  
  683. public function oembed() { 
  684. echo $this->input( array( 
  685. 'class' => 'cmb_oembed regular-text',  
  686. 'data-objectid' => $this->field->object_id,  
  687. 'data-objecttype' => $this->field->object_type 
  688. ) ),  
  689. '<p class="cmb-spinner spinner" style="display:none;"><img src="'. admin_url( '/images/wpspin_light.gif' ) .'" alt="spinner"/></p>',  
  690. '<div id="', $this->_id( '_status' ) , '" class="cmb_media_status ui-helper-clearfix embed_wrap">'; 
  691.  
  692. if ( $meta_value = $this->field->escaped_value() ) { 
  693. echo cmb_Meta_Box_ajax::get_oembed( $meta_value, $this->field->object_id, array( 
  694. 'object_type' => $this->field->object_type,  
  695. 'oembed_args' => array( 'width' => '640' ),  
  696. 'field_id' => $this->_id(),  
  697. ) ); 
  698.  
  699. echo '</div>'; 
  700.