PageRenderTime 38ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/grx-kanban/plugins/cmb2/includes/CMB2_Field_Display.php

https://gitlab.com/vitorhugoro1/grx-kanban
PHP | 455 lines | 289 code | 54 blank | 112 comment | 36 complexity | af6ff6915a70b8b5ead2beb9e5bcd6da MD5 | raw file
  1. <?php
  2. /**
  3. * CMB2 field display base.
  4. *
  5. * @since 2.2.2
  6. *
  7. * @category WordPress_Plugin
  8. * @package CMB2
  9. * @author WebDevStudios
  10. * @license GPL-2.0+
  11. * @link http://webdevstudios.com
  12. */
  13. class CMB2_Field_Display {
  14. /**
  15. * A CMB field object
  16. * @var CMB2_Field object
  17. * @since 2.2.2
  18. */
  19. public $field;
  20. /**
  21. * The CMB field object's value.
  22. * @var mixed
  23. * @since 2.2.2
  24. */
  25. public $value;
  26. /**
  27. * Get the corresponding display class for the field type.
  28. * @since 2.2.2
  29. * @param CMB2_Field $field
  30. * @return CMB2_Field_Display
  31. */
  32. public static function get( CMB2_Field $field ) {
  33. switch ( $field->type() ) {
  34. case 'text_url':
  35. $type = new CMB2_Display_Text_Url( $field );
  36. break;
  37. case 'text_money':
  38. $type = new CMB2_Display_Text_Money( $field );
  39. break;
  40. case 'colorpicker':
  41. $type = new CMB2_Display_Colorpicker( $field );
  42. break;
  43. case 'checkbox':
  44. $type = new CMB2_Display_Checkbox( $field );
  45. break;
  46. case 'wysiwyg':
  47. case 'textarea_small':
  48. $type = new CMB2_Display_Textarea( $field );
  49. break;
  50. case 'textarea_code':
  51. $type = new CMB2_Display_Textarea_Code( $field );
  52. break;
  53. case 'text_time':
  54. $type = new CMB2_Display_Text_Time( $field );
  55. break;
  56. case 'text_date':
  57. case 'text_date_timestamp':
  58. case 'text_datetime_timestamp':
  59. $type = new CMB2_Display_Text_Date( $field );
  60. break;
  61. case 'text_datetime_timestamp_timezone':
  62. $type = new CMB2_Display_Text_Date_Timezone( $field );
  63. break;
  64. case 'select':
  65. case 'radio':
  66. case 'radio_inline':
  67. $type = new CMB2_Display_Select( $field );
  68. break;
  69. case 'multicheck':
  70. case 'multicheck_inline':
  71. $type = new CMB2_Display_Multicheck( $field );
  72. break;
  73. case 'taxonomy_radio':
  74. case 'taxonomy_radio_inline':
  75. case 'taxonomy_select':
  76. $type = new CMB2_Display_Taxonomy_Radio( $field );
  77. break;
  78. case 'taxonomy_multicheck':
  79. case 'taxonomy_multicheck_inline':
  80. $type = new CMB2_Display_Taxonomy_Multicheck( $field );
  81. break;
  82. case 'file':
  83. $type = new CMB2_Display_File( $field );
  84. break;
  85. case 'file_list':
  86. $type = new CMB2_Display_File_List( $field );
  87. break;
  88. case 'oembed':
  89. $type = new CMB2_Display_oEmbed( $field );
  90. break;
  91. default:
  92. $type = new self( $field );
  93. break;
  94. }
  95. return $type;
  96. }
  97. /**
  98. * Setup our class vars
  99. * @since 2.2.2
  100. * @param CMB2_Field $field A CMB2 field object
  101. */
  102. public function __construct( CMB2_Field $field ) {
  103. $this->field = $field;
  104. $this->value = $this->field->value;
  105. }
  106. /**
  107. * Catchall method if field's 'display_cb' is NOT defined, or field type does
  108. * not have a corresponding display method
  109. * @since 2.2.2
  110. * @param string $method Non-existent method name
  111. * @param array $arguments All arguments passed to the method
  112. */
  113. public function display() {
  114. // If repeatable
  115. if ( $this->field->args( 'repeatable' ) ) {
  116. // And has a repeatable value
  117. if ( is_array( $this->field->value ) ) {
  118. // Then loop and output.
  119. echo '<ul class="cmb2-'. str_replace( '_', '-', $this->field->type() ) .'">';
  120. foreach ( $this->field->value as $val ) {
  121. $this->value = $val;
  122. echo '<li>', $this->_display(), '</li>';
  123. ;
  124. }
  125. echo '</ul>';
  126. }
  127. } else {
  128. $this->_display();
  129. }
  130. }
  131. /**
  132. * Default fallback display method.
  133. * @since 2.2.2
  134. */
  135. protected function _display() {
  136. print_r( $this->value );
  137. }
  138. }
  139. class CMB2_Display_Text_Url extends CMB2_Field_Display {
  140. /**
  141. * Display url value.
  142. * @since 2.2.2
  143. */
  144. protected function _display() {
  145. echo make_clickable( esc_url( $this->value ) );
  146. }
  147. }
  148. class CMB2_Display_Text_Money extends CMB2_Field_Display {
  149. /**
  150. * Display text_money value.
  151. * @since 2.2.2
  152. */
  153. protected function _display() {
  154. $this->value = $this->value ? $this->value : '0';
  155. echo ( ! $this->field->get_param_callback_result( 'before_field' ) ? '$' : ' ' ), $this->value;
  156. }
  157. }
  158. class CMB2_Display_Colorpicker extends CMB2_Field_Display {
  159. /**
  160. * Display color picker value.
  161. * @since 2.2.2
  162. */
  163. protected function _display() {
  164. echo '<span class="cmb2-colorpicker-swatch"><span style="background-color:', esc_attr( $this->value ), '"></span> ', esc_html( $this->value ), '</span>';
  165. }
  166. }
  167. class CMB2_Display_Checkbox extends CMB2_Field_Display {
  168. /**
  169. * Display multicheck value.
  170. * @since 2.2.2
  171. */
  172. protected function _display() {
  173. echo $this->value === 'on' ? 'on' : 'off';
  174. }
  175. }
  176. class CMB2_Display_Select extends CMB2_Field_Display {
  177. /**
  178. * Display select value.
  179. * @since 2.2.2
  180. */
  181. protected function _display() {
  182. $options = $this->field->options();
  183. $fallback = $this->field->args( 'show_option_none' );
  184. if ( ! $fallback && isset( $options[''] ) ) {
  185. $fallback = $options[''];
  186. }
  187. if ( ! $this->value && $fallback ) {
  188. echo $fallback;
  189. } elseif ( isset( $options[ $this->value ] ) ) {
  190. echo $options[ $this->value ];
  191. } else {
  192. echo esc_attr( $this->value );
  193. }
  194. }
  195. }
  196. class CMB2_Display_Multicheck extends CMB2_Field_Display {
  197. /**
  198. * Display multicheck value.
  199. * @since 2.2.2
  200. */
  201. protected function _display() {
  202. if ( empty( $this->value ) || ! is_array( $this->value ) ) {
  203. return;
  204. }
  205. $options = $this->field->options();
  206. $output = array();
  207. foreach ( $this->value as $val ) {
  208. if ( isset( $options[ $val ] ) ) {
  209. $output[] = $options[ $val ];
  210. } else {
  211. $output[] = esc_attr( $val );
  212. }
  213. }
  214. echo implode( ', ', $output );
  215. }
  216. }
  217. class CMB2_Display_Textarea extends CMB2_Field_Display {
  218. /**
  219. * Display textarea value.
  220. * @since 2.2.2
  221. */
  222. protected function _display() {
  223. echo wpautop( wp_kses_post( $this->value ) );
  224. }
  225. }
  226. class CMB2_Display_Textarea_Code extends CMB2_Field_Display {
  227. /**
  228. * Display textarea_code value.
  229. * @since 2.2.2
  230. */
  231. protected function _display() {
  232. echo '<xmp class="cmb2-code">'. print_r( $this->value, true ) .'</xmp>';
  233. }
  234. }
  235. class CMB2_Display_Text_Time extends CMB2_Field_Display {
  236. /**
  237. * Display text_time value.
  238. * @since 2.2.2
  239. */
  240. protected function _display() {
  241. echo $this->field->get_timestamp_format( 'time_format', $this->value );
  242. }
  243. }
  244. class CMB2_Display_Text_Date extends CMB2_Field_Display {
  245. /**
  246. * Display text_date value.
  247. * @since 2.2.2
  248. */
  249. protected function _display() {
  250. echo $this->field->get_timestamp_format( 'date_format', $this->value );
  251. }
  252. }
  253. class CMB2_Display_Text_Date_Timezone extends CMB2_Field_Display {
  254. /**
  255. * Display text_datetime_timestamp_timezone value.
  256. * @since 2.2.2
  257. */
  258. protected function _display() {
  259. $field = $this->field;
  260. if ( empty( $this->value ) ) {
  261. return;
  262. }
  263. $datetime = maybe_unserialize( $this->value );
  264. $this->value = $tzstring = '';
  265. if ( $datetime && $datetime instanceof DateTime ) {
  266. $tz = $datetime->getTimezone();
  267. $tzstring = $tz->getName();
  268. $this->value = $datetime->getTimestamp();
  269. }
  270. $date = $this->field->get_timestamp_format( 'date_format', $this->value );
  271. $time = $this->field->get_timestamp_format( 'time_format', $this->value );
  272. echo $date, ( $time ? ' ' . $time : '' ), ( $tzstring ? ', ' . $tzstring : '' );
  273. }
  274. }
  275. class CMB2_Display_Taxonomy_Radio extends CMB2_Field_Display {
  276. /**
  277. * Display single taxonomy value.
  278. * @since 2.2.2
  279. */
  280. protected function _display() {
  281. $taxonomy = $this->field->args( 'taxonomy' );
  282. $field_type = new CMB2_Type_Taxonomy_Radio( new CMB2_Types( $this->field ) );
  283. $terms = $field_type->get_object_terms();
  284. $term = false;
  285. if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) {
  286. $term = get_term_by( 'slug', $default, $taxonomy );
  287. } elseif ( ! empty( $terms ) ) {
  288. $term = $terms[key( $terms )];
  289. }
  290. if ( $term ) {
  291. $link = get_edit_term_link( $term->term_id, $taxonomy );
  292. echo '<a href="', esc_url( $link ), '">', esc_html( $term->name ), '</a>';
  293. }
  294. }
  295. }
  296. class CMB2_Display_Taxonomy_Multicheck extends CMB2_Field_Display {
  297. /**
  298. * Display taxonomy values.
  299. * @since 2.2.2
  300. */
  301. protected function _display() {
  302. $taxonomy = $this->field->args( 'taxonomy' );
  303. $field_type = new CMB2_Type_Taxonomy_Multicheck( new CMB2_Types( $this->field ) );
  304. $terms = $field_type->get_object_terms();
  305. if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) {
  306. $terms = array();
  307. if ( is_array( $default ) ) {
  308. foreach ( $default as $slug ) {
  309. $terms[] = get_term_by( 'slug', $slug, $taxonomy );
  310. }
  311. } else {
  312. $terms[] = get_term_by( 'slug', $default, $taxonomy );
  313. }
  314. }
  315. if ( is_array( $terms ) ) {
  316. $links = array();
  317. foreach ( $terms as $term ) {
  318. $link = get_edit_term_link( $term->term_id, $taxonomy );
  319. $links[] = '<a href="'. esc_url( $link ) .'">'. esc_html( $term->name ) .'</a>';
  320. }
  321. // Then loop and output.
  322. echo '<div class="cmb2-taxonomy-terms-', esc_attr( $taxonomy ), '">';
  323. echo implode( ', ', $links );
  324. echo '</div>';
  325. }
  326. }
  327. }
  328. class CMB2_Display_File extends CMB2_Field_Display {
  329. /**
  330. * Display file value.
  331. * @since 2.2.2
  332. */
  333. protected function _display() {
  334. if ( empty( $this->value ) ) {
  335. return;
  336. }
  337. $this->value = esc_url_raw( $this->value );
  338. $field_type = new CMB2_Type_File_Base( new CMB2_Types( $this->field ) );
  339. $id = $this->field->get_field_clone( array(
  340. 'id' => $field_type->_id() . '_id',
  341. ) )->escaped_value( 'absint' );
  342. $this->file_output( $this->value, $id, $field_type );
  343. }
  344. protected function file_output( $url_value, $id, CMB2_Type_File_Base $field_type ) {
  345. // If there is no ID saved yet, try to get it from the url
  346. if ( $url_value && ! $id ) {
  347. $id = cmb2_utils()->image_id_from_url( esc_url_raw( $url_value ) );
  348. }
  349. if ( $field_type->is_valid_img_ext( $url_value ) ) {
  350. $img_size = $this->field->args( 'preview_size' );
  351. if ( $id ) {
  352. $image = wp_get_attachment_image( $id, $img_size, null, array( 'class' => 'cmb-image-display' ) );
  353. } else {
  354. $size = is_array( $img_size ) ? $img_size[0] : 200;
  355. $image = '<img class="cmb-image-display" style="max-width: ' . absint( $size ) . 'px; width: 100%; height: auto;" src="' . $url_value . '" alt="" />';
  356. }
  357. echo $image;
  358. } else {
  359. printf( '<div class="file-status"><span>%1$s <strong><a href="%2$s">%3$s</a></strong></span></div>',
  360. esc_html( $field_type->_text( 'file_text', __( 'File:', 'cmb2' ) ) ),
  361. $url_value,
  362. cmb2_utils()->get_file_name_from_path( $url_value )
  363. );
  364. }
  365. }
  366. }
  367. class CMB2_Display_File_List extends CMB2_Display_File {
  368. /**
  369. * Display file_list value.
  370. * @since 2.2.2
  371. */
  372. protected function _display() {
  373. if ( empty( $this->value ) || ! is_array( $this->value ) ) {
  374. return;
  375. }
  376. $field_type = new CMB2_Type_File_Base( new CMB2_Types( $this->field ) );
  377. echo '<ul class="cmb2-display-file-list">';
  378. foreach ( $this->value as $id => $fullurl ) {
  379. echo '<li>', $this->file_output( esc_url_raw( $fullurl ), $id, $field_type ), '</li>';
  380. }
  381. echo '</ul>';
  382. }
  383. }
  384. class CMB2_Display_oEmbed extends CMB2_Field_Display {
  385. /**
  386. * Display oembed value.
  387. * @since 2.2.2
  388. */
  389. protected function _display() {
  390. if ( ! $this->value ) {
  391. return;
  392. }
  393. cmb2_do_oembed( array(
  394. 'url' => $this->value,
  395. 'object_id' => $this->field->object_id,
  396. 'object_type' => $this->field->object_type,
  397. 'oembed_args' => array( 'width' => '300' ),
  398. 'field_id' => $this->field->id(),
  399. ) );
  400. }
  401. }