PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/cmb2/includes/CMB2_Field_Display.php

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