NF_MergeTags_Fields

Class NF_MergeTags_Fields.

Defined (1)

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

/includes/MergeTags/Fields.php  
  1. final class NF_MergeTags_Fields extends NF_Abstracts_MergeTags 
  2. protected $id = 'fields'; 
  3. protected $form_id; 
  4.  
  5. public function __construct() 
  6. parent::__construct(); 
  7. $this->title = __( 'Fields', 'ninja-forms' ); 
  8. $this->merge_tags = Ninja_Forms()->config( 'MergeTagsFields' ); 
  9.  
  10. if( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
  11. $this->merge_tags = array_merge( $this->merge_tags, Ninja_Forms()->config( 'MergeTagsFieldsAJAX' ) ); 
  12.  
  13. add_filter( 'ninja_forms_calc_setting', array( $this, 'pre_parse_calc_settings' ), 9 ); 
  14. //add_filter( 'ninja_forms_calc_setting', array( $this, 'calc_replace' ) ); 
  15.  
  16. public function __call($name, $arguments) 
  17. if(isset($arguments[0]['calc'])) { 
  18. return $this->merge_tags[ $name ][ 'calc_value' ]; 
  19. return $this->merge_tags[ $name ][ 'field_value' ]; 
  20.  
  21. public function all_fields() 
  22. $return = '<table>'; 
  23. $hidden_field_types = array( 'html', 'submit', 'password', 'passwordconfirm' ); 
  24.  
  25. foreach( $this->get_fields_sorted() as $field ) { 
  26.  
  27. if( ! isset( $field[ 'type' ] ) ) continue; 
  28. if( in_array( $field[ 'type' ], array_values( $hidden_field_types ) ) ) continue; 
  29.  
  30. $field[ 'value' ] = apply_filters( 'ninja_forms_merge_tag_value_' . $field[ 'type' ], $field[ 'value' ], $field ); 
  31.  
  32. if( is_array( $field[ 'value' ] ) ) $field[ 'value' ] = implode( ', ', $field[ 'value' ] ); 
  33.  
  34. $return .= '<tr><td>' . $field[ 'label' ] .':</td><td>' . $field[ 'value' ] . '</td></tr>'; 
  35. $return .= '</table>'; 
  36. return $return; 
  37.  
  38. public function all_fields_table() 
  39. $return = '<table>'; 
  40.  
  41. $hidden_field_types = array( 'submit', 'password', 'passwordconfirm' ); 
  42.  
  43. foreach( $this->get_fields_sorted() as $field ) { 
  44. if( ! isset( $field[ 'type' ] ) ) continue; 
  45.  
  46. // Skip specific field types. 
  47. if( in_array( $field[ 'type' ], array_values( $hidden_field_types ) ) ) continue; 
  48.  
  49. $field[ 'value' ] = apply_filters( 'ninja_forms_merge_tag_value_' . $field[ 'type' ], $field[ 'value' ], $field ); 
  50. if( is_array( $field[ 'value' ] ) ) $field[ 'value' ] = implode( ', ', $field[ 'value' ] ); 
  51. $return .= '<tr><td valign="top">' . $field[ 'label' ] .':</td><td>' . $field[ 'value' ] . '</td></tr>'; 
  52. $return .= '</table>'; 
  53. return $return; 
  54.  
  55. public function fields_table() 
  56. $return = '<table>'; 
  57. $hidden_field_types = array( 'html', 'submit', 'password', 'passwordconfirm' ); 
  58.  
  59. foreach( $this->get_fields_sorted() as $field ) { 
  60.  
  61. if( ! isset( $field[ 'type' ] ) ) continue; 
  62.  
  63. // Skip specific field types. 
  64. if( in_array( $field[ 'type' ], array_values( $hidden_field_types ) ) ) continue; 
  65.  
  66. // TODO: Skip hidden fields, ie conditionally hidden. 
  67. if( isset( $field[ 'visible' ] ) && false === $field[ 'visible' ] ) continue; 
  68.  
  69. $field[ 'value' ] = apply_filters( 'ninja_forms_merge_tag_value_' . $field[ 'type' ], $field[ 'value' ], $field ); 
  70.  
  71. // Skip fields without values. 
  72. if( ! $field[ 'value' ] ) continue; 
  73.  
  74. if( is_array( $field[ 'value' ] ) ) $field[ 'value' ] = implode( ', ', $field[ 'value' ] ); 
  75.  
  76. $return .= '<tr><td valign="top">' . $field[ 'label' ] .':</td><td>' . $field[ 'value' ] . '</td></tr>'; 
  77. $return .= '</table>'; 
  78. return $return; 
  79.  
  80. // TODO: Is this being used? 
  81. public function all_field_plain() 
  82. $return = ''; 
  83.  
  84. foreach( $this->get_fields_sorted() as $field ) { 
  85.  
  86. $field[ 'value' ] = apply_filters( 'ninja_forms_merge_tag_value_' . $field[ 'type' ], $field[ 'value' ], $field ); 
  87.  
  88. if( is_array( $field[ 'value' ] ) ) $field[ 'value' ] = implode( ', ', $field[ 'value' ] ); 
  89.  
  90. $return .= $field[ 'label' ] .': ' . $field[ 'value' ] . "\r\n"; 
  91. return $return; 
  92.  
  93. public function add_field( $field ) 
  94. //print_r($field); 
  95. $hidden_field_types = apply_filters( 'nf_sub_hidden_field_types', array() ); 
  96.  
  97. if( 'html' == $field[ 'type' ] ) { // Include HTML fields in the All Fields Table Merge Tag. 
  98. $this->merge_tags[ 'all_fields' ][ 'fields' ][ $field['id'] ] = $field; 
  99.  
  100. if( in_array( $field[ 'type' ], $hidden_field_types ) ) return; 
  101.  
  102. $field_id = $field[ 'id' ]; 
  103. $callback = 'field_' . $field_id; 
  104.  
  105. if( is_array( $field[ 'value' ] ) ) $field[ 'value' ] = implode( ', ', $field[ 'value' ] ); 
  106.  
  107. $field[ 'value' ] = strip_shortcodes( $field[ 'value' ] ); 
  108.  
  109. $this->merge_tags[ 'all_fields' ][ 'fields' ][ $field_id ] = $field; 
  110.  
  111. $value = apply_filters('ninja_forms_merge_tag_value_' . $field['type'], $field['value'], $field); 
  112.  
  113. $this->add( $callback, $field['id'], '{field:' . $field['id'] . '}', $value ); 
  114.  
  115. if( isset( $field[ 'key' ] ) ) { 
  116. $field_key = $field[ 'key' ]; 
  117. $calc_value = apply_filters( 'ninja_forms_merge_tag_calc_value_' . $field[ 'type' ], $field['value'], $field ); 
  118.  
  119. // Add Field Key Callback 
  120. $callback = 'field_' . $field_key; 
  121. $this->add( $callback, $field_key, '{field:' . $field_key . '}', $value, $calc_value ); 
  122.  
  123. // Add Field by Key for All Fields 
  124. $this->merge_tags[ 'all_fields_by_key' ][ 'fields' ][ $field_key ] = $field; 
  125.  
  126. // Add Field Calc Callabck 
  127. if( '' == $calc_value ) $calc_value = '0'; 
  128. //var_dump($calc_value); 
  129. //echo('myspace'); 
  130. $callback = 'field_' . $field_key . '_calc'; 
  131. $this->add( $callback, $field_key, '{field:' . $field_key . ':calc}', $calc_value, $calc_value ); 
  132.  
  133. public function add( $callback, $id, $tag, $value, $calc_value = false ) 
  134. $this->merge_tags[ $callback ] = array( 
  135. 'id' => $id,  
  136. 'tag' => $tag,  
  137. 'callback' => $callback,  
  138. 'field_value' => $value,  
  139. 'calc_value' => ($calc_value === false) ? $value : $calc_value,  
  140. ); 
  141.  
  142. public function set_form_id( $form_id ) 
  143. $this->form_id = $form_id; 
  144.  
  145. private function get_fields_sorted() 
  146. $fields = $this->merge_tags[ 'all_fields' ][ 'fields' ]; 
  147.  
  148. // Filterable Sorting for Add-ons (ie Layout and Multi-Part ). 
  149. if ( has_filter( 'ninja_forms_get_fields_sorted' ) ) { 
  150. $fields_by_key = $this->merge_tags[ 'all_fields_by_key' ][ 'fields' ]; 
  151. $fields = apply_filters( 'ninja_forms_get_fields_sorted', array(), $fields, $fields_by_key, $this->form_id ); 
  152. } else { 
  153. // Default Sorting by Field Order. 
  154. uasort( $fields, array( $this, 'sort_fields' ) ); 
  155.  
  156. return $fields; 
  157.  
  158. public static function sort_fields( $a, $b ) 
  159. if ( $a[ 'order' ] == $b[ 'order' ] ) { 
  160. return 0; 
  161. return ( $a[ 'order' ] < $b[ 'order' ] ) ? -1 : 1; 
  162.  
  163. public function calc_replace( $subject ) { 
  164. if( is_array( $subject ) ) { 
  165. foreach( $subject as $i => $s ) { 
  166. $subject[ $i ] = $this->replace( $s ); 
  167. return $subject; 
  168. //print_r($subject); 
  169.  
  170. preg_match_all("/{(.*?)}/", $subject, $matches ); 
  171.  
  172. if( empty( $matches[0] ) ) return $subject; 
  173.  
  174. foreach( $this->merge_tags as $merge_tag ) { 
  175.  
  176. if( ! in_array( $merge_tag[ 'tag' ], $matches[0] ) ) continue; 
  177.  
  178. if( ! isset($merge_tag[ 'callback' ])) continue; 
  179. //print_r($merge_tag); 
  180. //echo( ' = ' ); 
  181.  
  182. $replace = ( is_callable( array( $this, $merge_tag[ 'callback' ] ) ) ) ? $this->{$merge_tag[ 'callback' ]}(array('calc' => true)) : '0'; 
  183. //print_r($replace); 
  184. //echo(' myspace '); 
  185. if( '' == $replace ) $replace = '0'; 
  186.  
  187. $subject = str_replace( $merge_tag[ 'tag' ], $replace, $subject ); 
  188.  
  189. return $subject; 
  190.  
  191. /** 
  192. |-------------------------------------------------------------------------- 
  193. | Calculations 
  194. |-------------------------------------------------------------------------- 
  195. | Force {field:...:calc} in this context of calculations. 
  196. | Example: {field:list} -> {field:list:calc} 
  197. | When parsing the {field:...:calc} tag, if no calc value is found then the value will be used. 
  198. | TODO: This makes explicit list field "values" inaccessible in calculations. 
  199. */ 
  200.  
  201. public function pre_parse_calc_settings( $eq ) 
  202. return preg_replace_callback( '/{field:([a-z0-9]|_)*}/', array( $this, 'force_field_calc_tags' ), $eq ); 
  203.  
  204. private function force_field_calc_tags( $matches ) 
  205. return str_replace( '}', ':calc}', $matches[0] ); 
  206.  
  207. } // END CLASS NF_MergeTags_Fields