class-acf-field-message.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. if ( ! class_exists( 'acf_field_message' ) ) :
  3. class acf_field_message extends acf_field {
  4. public $show_in_rest = false;
  5. /*
  6. * __construct
  7. *
  8. * This function will setup the field type data
  9. *
  10. * @type function
  11. * @date 5/03/2014
  12. * @since 5.0.0
  13. *
  14. * @param n/a
  15. * @return n/a
  16. */
  17. function initialize() {
  18. // vars
  19. $this->name = 'message';
  20. $this->label = __( 'Message', 'acf' );
  21. $this->category = 'layout';
  22. $this->defaults = array(
  23. 'message' => '',
  24. 'esc_html' => 0,
  25. 'new_lines' => 'wpautop',
  26. );
  27. }
  28. /*
  29. * render_field()
  30. *
  31. * Create the HTML interface for your field
  32. *
  33. * @param $field - an array holding all the field's data
  34. *
  35. * @type action
  36. * @since 3.6
  37. * @date 23/01/13
  38. */
  39. function render_field( $field ) {
  40. // vars
  41. $m = $field['message'];
  42. // wptexturize (improves "quotes")
  43. $m = wptexturize( $m );
  44. // esc_html
  45. if ( $field['esc_html'] ) {
  46. $m = esc_html( $m );
  47. }
  48. // new lines
  49. if ( $field['new_lines'] == 'wpautop' ) {
  50. $m = wpautop( $m );
  51. } elseif ( $field['new_lines'] == 'br' ) {
  52. $m = nl2br( $m );
  53. }
  54. // return
  55. echo acf_esc_html( $m );
  56. }
  57. /*
  58. * render_field_settings()
  59. *
  60. * Create extra options for your field. This is rendered when editing a field.
  61. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  62. *
  63. * @param $field - an array holding all the field's data
  64. *
  65. * @type action
  66. * @since 3.6
  67. * @date 23/01/13
  68. */
  69. function render_field_settings( $field ) {
  70. acf_render_field_setting(
  71. $field,
  72. array(
  73. 'label' => __( 'Message', 'acf' ),
  74. 'instructions' => '',
  75. 'type' => 'textarea',
  76. 'name' => 'message',
  77. )
  78. );
  79. acf_render_field_setting(
  80. $field,
  81. array(
  82. 'label' => __( 'New Lines', 'acf' ),
  83. 'instructions' => __( 'Controls how new lines are rendered', 'acf' ),
  84. 'type' => 'select',
  85. 'name' => 'new_lines',
  86. 'choices' => array(
  87. 'wpautop' => __( 'Automatically add paragraphs', 'acf' ),
  88. 'br' => __( 'Automatically add &lt;br&gt;', 'acf' ),
  89. '' => __( 'No Formatting', 'acf' ),
  90. ),
  91. )
  92. );
  93. acf_render_field_setting(
  94. $field,
  95. array(
  96. 'label' => __( 'Escape HTML', 'acf' ),
  97. 'instructions' => __( 'Allow HTML markup to display as visible text instead of rendering', 'acf' ),
  98. 'name' => 'esc_html',
  99. 'type' => 'true_false',
  100. 'ui' => 1,
  101. )
  102. );
  103. }
  104. /*
  105. * translate_field
  106. *
  107. * This function will translate field settings
  108. *
  109. * @type function
  110. * @date 8/03/2016
  111. * @since 5.3.2
  112. *
  113. * @param $field (array)
  114. * @return $field
  115. */
  116. function translate_field( $field ) {
  117. // translate
  118. $field['message'] = acf_translate( $field['message'] );
  119. // return
  120. return $field;
  121. }
  122. /*
  123. * load_field()
  124. *
  125. * This filter is appied to the $field after it is loaded from the database
  126. *
  127. * @type filter
  128. * @since 3.6
  129. * @date 23/01/13
  130. *
  131. * @param $field - the field array holding all the field options
  132. *
  133. * @return $field - the field array holding all the field options
  134. */
  135. function load_field( $field ) {
  136. // remove name to avoid caching issue
  137. $field['name'] = '';
  138. // remove instructions
  139. $field['instructions'] = '';
  140. // remove required to avoid JS issues
  141. $field['required'] = 0;
  142. // set value other than 'null' to avoid ACF loading / caching issue
  143. $field['value'] = false;
  144. // return
  145. return $field;
  146. }
  147. }
  148. // initialize
  149. acf_register_field_type( 'acf_field_message' );
  150. endif; // class_exists check