NF_Abstracts_Field

Class NF_Abstracts_Field.

Defined (1)

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

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