Field.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Abstracts_Field
  4. */
  5. abstract class NF_Abstracts_Field
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $_name = '';
  11. /**
  12. * @var string
  13. */
  14. protected $_nicename = '';
  15. /**
  16. * @var string
  17. */
  18. protected $_section = '';
  19. /**
  20. * @var string
  21. */
  22. protected $_icon = 'square-o';
  23. /**
  24. * @var array
  25. */
  26. protected $_aliases = array();
  27. /**
  28. * @var array
  29. */
  30. protected $_settings = array();
  31. /**
  32. * @var array
  33. */
  34. protected $_settings_all_fields = array();
  35. /**
  36. * @var array
  37. */
  38. protected $_settings_exclude = array();
  39. /**
  40. * @var array
  41. */
  42. protected $_settings_only = array();
  43. /**
  44. * @var array
  45. */
  46. protected $_use_merge_tags = array( 'user', 'post', 'system', 'fields' );
  47. /**
  48. * @var array
  49. */
  50. protected $_use_merge_tags_include = array();
  51. /**
  52. * @var array
  53. */
  54. protected $_use_merge_tags_exclude = array();
  55. /**
  56. * @var string
  57. */
  58. protected $_test_value = 'test';
  59. /**
  60. * @var string
  61. */
  62. protected $_attr = '';
  63. /**
  64. * @var string
  65. */
  66. protected $_type = '';
  67. /**
  68. * @var string
  69. */
  70. protected $_parent_type = '';
  71. /**
  72. * @var string
  73. */
  74. public static $_base_template = 'input';
  75. /**
  76. * @var array
  77. */
  78. protected $_templates = array();
  79. /**
  80. * @var string
  81. */
  82. protected $_wrap_template = 'wrap';
  83. /**
  84. * @var array
  85. */
  86. protected $_old_classname = '';
  87. //-----------------------------------------------------
  88. // Public Methods
  89. //-----------------------------------------------------
  90. /**
  91. * Constructor
  92. */
  93. public function __construct()
  94. {
  95. if( ! empty( $this->_settings_only ) ){
  96. $this->_settings = array_merge( $this->_settings, $this->_settings_only );
  97. } else {
  98. $this->_settings = array_merge( $this->_settings_all_fields, $this->_settings );
  99. $this->_settings = array_diff( $this->_settings, $this->_settings_exclude );
  100. }
  101. $this->_settings = $this->load_settings( $this->_settings );
  102. $this->_test_value = apply_filters( 'ninja_forms_field_' . $this->_name . '_test_value', $this->_test_value );
  103. add_filter( 'ninja_forms_localize_field_settings_' . $this->_type, array( $this, 'localize_settings' ), 10, 2 );
  104. }
  105. /**
  106. * Validate
  107. *
  108. * @param $field
  109. * @param $data
  110. * @return array $errors
  111. */
  112. public function validate( $field, $data )
  113. {
  114. $errors = array();
  115. // Required check.
  116. if( is_array( $field[ 'value' ] ) ){
  117. $field[ 'value' ] = implode( '', $field[ 'value' ] );
  118. }
  119. if( isset( $field['required'] ) && 1 == $field['required'] && is_null( trim( $field['value'] ) ) ){
  120. $errors[] = 'Field is required.';
  121. }
  122. return $errors;
  123. }
  124. public function process( $field, $data )
  125. {
  126. return $data;
  127. }
  128. /**
  129. * Admin Form Element
  130. *
  131. * Returns the output for editing fields in a submission.
  132. *
  133. * @param $id
  134. * @param $value
  135. * @return string
  136. */
  137. public function admin_form_element( $id, $value )
  138. {
  139. return '<input class="widefat" name="fields[' . intval( $id ) . ']" value="' . esc_attr( $value ) . '" />';
  140. }
  141. public function get_name()
  142. {
  143. return $this->_name;
  144. }
  145. public function get_nicename()
  146. {
  147. return $this->_nicename;
  148. }
  149. public function get_section()
  150. {
  151. return $this->_section;
  152. }
  153. public function get_icon()
  154. {
  155. return $this->_icon;
  156. }
  157. public function get_aliases()
  158. {
  159. return $this->_aliases;
  160. }
  161. public function get_type()
  162. {
  163. return $this->_type;
  164. }
  165. public function get_parent_type()
  166. {
  167. if( $this->_parent_type ){
  168. return $this->_parent_type;
  169. }
  170. // If a type is not set, return 'textbox'
  171. return ( get_parent_class() ) ? parent::$_type : 'textbox';
  172. }
  173. public function get_settings()
  174. {
  175. return $this->_settings;
  176. }
  177. public function use_merge_tags()
  178. {
  179. $use_merge_tags = array_merge( $this->_use_merge_tags, $this->_use_merge_tags_include );
  180. $use_merge_tags = array_diff( $use_merge_tags, $this->_use_merge_tags_exclude );
  181. return $use_merge_tags;
  182. }
  183. public function get_test_value()
  184. {
  185. return $this->_test_value;
  186. }
  187. public function get_templates()
  188. {
  189. $templates = (array) $this->_templates;
  190. // Create a reflection for examining the parent
  191. $reflection = new ReflectionClass( $this );
  192. $parent_class = $reflection->getParentClass();
  193. if ( $parent_class->isAbstract() ) {
  194. $parent_class_name = $parent_class->getName();
  195. $parent_templates = call_user_func( array( $parent_class_name, 'get_base_template' ) ); // Parent Class' Static Property
  196. return array_merge( $templates, (array) $parent_templates );
  197. }
  198. $parent_class_name = strtolower( str_replace('NF_Fields_', '', $parent_class->getName() ) );
  199. if( ! isset( Ninja_Forms()->fields[ $parent_class_name ] ) ) return $templates;
  200. $parent = Ninja_Forms()->fields[ $parent_class_name ];
  201. return array_merge($templates, $parent->get_templates());
  202. }
  203. public function get_wrap_template()
  204. {
  205. return $this->_wrap_template;
  206. }
  207. public function get_old_classname()
  208. {
  209. return $this->_old_classname;
  210. }
  211. protected function load_settings( $only_settings = array() )
  212. {
  213. $settings = array();
  214. // Loads a settings array from the FieldSettings configuration file.
  215. $all_settings = Ninja_Forms::config( 'FieldSettings' );
  216. foreach( $only_settings as $setting ){
  217. if( isset( $all_settings[ $setting ]) ){
  218. $settings[ $setting ] = $all_settings[ $setting ];
  219. }
  220. }
  221. return $settings = apply_filters( 'ninja_forms_field_load_settings', $settings, $this->_name, $this->get_parent_type() );
  222. }
  223. public static function get_base_template()
  224. {
  225. return self::$_base_template;
  226. }
  227. public static function sort_by_order( $a, $b )
  228. {
  229. return $a->get_setting( 'order' ) - $b->get_setting( 'order' );
  230. }
  231. public function localize_settings( $settings, $form_id ) {
  232. return $settings;
  233. }
  234. }