scripts.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. function ninja_forms_display_js( $form_id, $local_vars = '' ) {
  3. global $post, $ninja_forms_display_localize_js, $wp_locale, $ninja_forms_loading, $ninja_forms_processing;
  4. if ( defined( 'NINJA_FORMS_JS_DEBUG' ) && NINJA_FORMS_JS_DEBUG ) {
  5. $suffix = '';
  6. $src = 'dev';
  7. } else {
  8. $suffix = '.min';
  9. $src = 'min';
  10. }
  11. // Get all of our form fields to see if we need to include the datepicker and/or jqueryUI
  12. $datepicker = 0;
  13. $qtip = 0;
  14. $mask = 0;
  15. $currency = 0;
  16. $input_limit = 0;
  17. $rating = 0;
  18. $calc_value = array();
  19. $calc_fields = array();
  20. $calc_eq = false;
  21. $sub_total = false;
  22. $tax = false;
  23. $fields = ninja_forms_get_fields_by_form_id( $form_id );
  24. if( is_array( $fields ) AND !empty( $fields ) ){
  25. foreach( $fields as $field ){
  26. if ( isset ( $field['id'] ) ) {
  27. $field_id = $field['id'];
  28. } else {
  29. $field_id = '';
  30. }
  31. if ( isset ( $field['type'] ) ) {
  32. $field_type = $field['type'];
  33. } else {
  34. $field_type = '';
  35. }
  36. $field['data'] = apply_filters( 'ninja_forms_display_script_field_data', $field['data'], $field_id );
  37. if( isset( $field['data']['datepicker'] ) AND $field['data']['datepicker'] == 1 ){
  38. $datepicker = 1;
  39. }
  40. if( isset( $field['data']['show_help'] ) AND $field['data']['show_help'] == 1 ){
  41. $qtip = 1;
  42. }
  43. if( isset( $field['data']['mask'] ) AND $field['data']['mask'] != '' ){
  44. $mask = 1;
  45. }
  46. if( isset( $field['data']['mask'] ) AND $field['data']['mask'] == 'currency' ){
  47. $currency = 1;
  48. }
  49. if( isset( $field['data']['input_limit'] ) AND $field['data']['input_limit'] != '' ){
  50. $input_limit = $field['data']['input_limit'];
  51. $input_limit_type = $field['data']['input_limit_type'];
  52. }
  53. if( $field_type == '_rating' ){
  54. $rating = 1;
  55. }
  56. // Populate an array of calculation values for the form fields.
  57. // Check to see if this field has a calc_value. If it does, add this to our calc_value array so that we can tell what it is in our JS.
  58. if ( isset( $field['data']['calc_value'] ) ) {
  59. $calc_value[$field_id] = $field['data']['calc_value'];
  60. } else if ( $field_type == '_list' ) {
  61. // Get a list of options and their 'calc' setting.
  62. if ( isset ( $field['data']['list']['options'] ) AND is_array ( $field['data']['list']['options'] ) ) {
  63. $list_options = $field['data']['list']['options'];
  64. foreach ( $list_options as $option ) {
  65. if ( isset ( $field['data']['list_show_value'] ) AND $field['data']['list_show_value'] == 1 ) {
  66. $key = $option['value'];
  67. } else {
  68. $key = $option['label'];
  69. }
  70. if ( !isset ( $option['calc'] ) OR ( isset ( $option['calc'] ) AND empty ( $option['calc'] ) ) ) {
  71. $option['calc'] = 0;
  72. }
  73. $calc_value[$field_id][$key] = $option['calc'];
  74. }
  75. }
  76. }
  77. // Check to see if this is a tax field;
  78. if ( $field_type == '_tax' ) {
  79. $tax = $field_id;
  80. }
  81. // Check to see if this is a calculation field. If it is, store it in our calc_fields array along with its method.
  82. if ( $field_type == '_calc' ) {
  83. if ( isset ( $field['data']['calc_method'] ) ) {
  84. $calc_method = $field['data']['calc_method'];
  85. } else {
  86. $calc_method = 'auto';
  87. }
  88. // Check to see if this is a sub_total calculation
  89. if ( isset ( $field['data']['payment_sub_total'] ) AND $field['data']['payment_sub_total'] == 1 ) {
  90. $sub_total = $field_id;
  91. }
  92. switch ( $calc_method ) {
  93. case 'auto':
  94. $calc_fields[$field_id] = array( 'method' => 'auto' );
  95. break;
  96. case 'fields':
  97. $field_ops = $field['data']['calc'];
  98. $calc_fields[$field_id] = array( 'method' => 'fields', 'fields' => $field_ops );
  99. break;
  100. case 'eq':
  101. $calc_fields[$field_id] = array( 'method' => 'eq', 'eq' => $field['data']['calc_eq'] );
  102. $calc_eq = true;
  103. break;
  104. }
  105. $calc_fields[$field_id]['places'] = $field['data']['calc_places'];
  106. }
  107. }
  108. // Loop through our fields again looking for calc fields that are totals.
  109. foreach( $fields as $field ){
  110. if ( isset ( $field['id'] ) ) {
  111. $field_id = $field['id'];
  112. } else {
  113. $field_id = '';
  114. }
  115. if ( isset ( $field['type'] ) ) {
  116. $field_type = $field['type'];
  117. } else {
  118. $field_type = '';
  119. }
  120. if ( $field_type == '_calc' ) {
  121. if ( isset ( $field['data']['payment_total'] ) AND $field['data']['payment_total'] == 1 ) {
  122. if ( $sub_total AND $tax AND $field['data']['calc_method'] == 'auto' ) {
  123. $calc_fields[$field_id]['method'] = 'eq';
  124. $calc_fields[$field_id]['eq'] = 'field_'.$sub_total.' + ( field_'.$sub_total.' * field_'.$tax.' )';
  125. $calc_eq = true;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. // Loop through our fields once more to add them to our calculation field with the method of 'eq'.
  132. if ( $calc_eq ) {
  133. foreach ( $calc_fields as $calc_id => $calc ) {
  134. if( $calc['method'] == 'eq' ) {
  135. foreach( $fields as $field ){
  136. $field_id = $field['id'];
  137. if (preg_match("/\bfield_".$field_id."\b/i", $calc['eq'] ) ) {
  138. $calc_fields[$calc_id]['fields'][] = $field_id;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. if ( $datepicker == 1 ) {
  145. wp_enqueue_script( 'jquery-ui-datepicker' );
  146. }
  147. if ( $qtip == 1 ) {
  148. wp_enqueue_script( 'jquery-qtip',
  149. NINJA_FORMS_URL .'js/min/jquery.qtip.min.js',
  150. array( 'jquery', 'jquery-ui-position' ) );
  151. }
  152. if ( $mask == 1 ) {
  153. wp_enqueue_script( 'jquery-maskedinput',
  154. NINJA_FORMS_URL .'js/min/jquery.maskedinput.min.js',
  155. array( 'jquery' ) );
  156. }
  157. if ( $currency == 1 ) {
  158. wp_enqueue_script('jquery-autonumeric',
  159. NINJA_FORMS_URL .'js/min/autoNumeric.min.js',
  160. array( 'jquery' ) );
  161. }
  162. if ( $input_limit != 0 ) {
  163. wp_enqueue_script('jquery-char-input-limit',
  164. NINJA_FORMS_URL .'js/min/word-and-character-counter.min.js',
  165. array( 'jquery' ) );
  166. }
  167. if ( $rating == 1 ) {
  168. wp_enqueue_script('jquery-rating',
  169. NINJA_FORMS_URL .'js/min/jquery.rating.min.js',
  170. array( 'jquery' ) );
  171. }
  172. $form_row = ninja_forms_get_form_by_id($form_id);
  173. $form_row = apply_filters( 'ninja_forms_display_form_form_data', $form_row );
  174. if( isset( $form_row['data']['ajax'] ) ){
  175. $ajax = $form_row['data']['ajax'];
  176. }else{
  177. $ajax = 0;
  178. }
  179. if( isset( $form_row['data']['hide_complete'] ) ){
  180. $hide_complete = $form_row['data']['hide_complete'];
  181. }else{
  182. $hide_complete = 0;
  183. }
  184. if( isset( $form_row['data']['clear_complete'] ) ){
  185. $clear_complete = $form_row['data']['clear_complete'];
  186. }else{
  187. $clear_complete = 0;
  188. }
  189. $ninja_forms_js_form_settings['ajax'] = $ajax;
  190. $ninja_forms_js_form_settings['hide_complete'] = $hide_complete;
  191. $ninja_forms_js_form_settings['clear_complete'] = $clear_complete;
  192. $calc_settings['calc_value'] = '';
  193. $calc_settings['calc_fields'] = '';
  194. if ( !empty ( $calc_value ) ) {
  195. $calc_settings['calc_value'] = $calc_value;
  196. }
  197. $calc_settings['calc_fields'] = $calc_fields;
  198. $plugin_settings = nf_get_settings();
  199. if(isset($plugin_settings['date_format'])){
  200. $date_format = $plugin_settings['date_format'];
  201. }else{
  202. $date_format = 'm/d/Y';
  203. }
  204. $date_format = ninja_forms_date_to_datepicker($date_format);
  205. $datepicker_args = array();
  206. if ( !empty( $date_format ) ) {
  207. $datepicker_args['dateFormat'] = $date_format;
  208. }
  209. $currency_symbol = $plugin_settings['currency_symbol'];
  210. $password_mismatch = esc_html(stripslashes($plugin_settings['password_mismatch']));
  211. $msg_format = 'inline';
  212. wp_enqueue_script( 'ninja-forms-display',
  213. NINJA_FORMS_URL . 'js/' . $src .'/ninja-forms-display' . $suffix . '.js?nf_ver=' . NF_PLUGIN_VERSION,
  214. array( 'jquery', 'jquery-form', 'backbone', 'underscore' ) );
  215. if( !isset( $ninja_forms_display_localize_js ) OR !$ninja_forms_display_localize_js ){
  216. wp_localize_script( 'ninja-forms-display', 'ninja_forms_settings', array('ajax_msg_format' => $msg_format, 'password_mismatch' => $password_mismatch, 'plugin_url' => NINJA_FORMS_URL, 'datepicker_args' => apply_filters( 'ninja_forms_forms_display_datepicker_args', $datepicker_args ), 'currency_symbol' => $currency_symbol, 'date_format' => $date_format ) );
  217. $ninja_forms_display_localize_js = true;
  218. }
  219. wp_localize_script( 'ninja-forms-display','thousandsSeparator', addslashes( $wp_locale->number_format['thousands_sep'] ) );
  220. wp_localize_script( 'ninja-forms-display','decimalPoint', addslashes( $wp_locale->number_format['decimal_point'] ) );
  221. wp_localize_script( 'ninja-forms-display', 'ninja_forms_form_'.$form_id.'_settings', apply_filters( 'nf_form_js_settings', $ninja_forms_js_form_settings, $form_id ) );
  222. wp_localize_script( 'ninja-forms-display', 'ninja_forms_form_'.$form_id.'_calc_settings', $calc_settings );
  223. wp_localize_script( 'ninja-forms-display', 'ninja_forms_password_strength', array(
  224. 'empty' => __( 'Strength indicator', 'ninja-forms' ),
  225. 'short' => __( 'Very weak', 'ninja-forms' ),
  226. 'bad' => __( 'Weak', 'ninja-forms' ),
  227. /* translators: password strength */
  228. 'good' => _x( 'Medium', 'password strength', 'ninja-forms' ),
  229. 'strong' => __( 'Strong', 'ninja-forms' ),
  230. 'mismatch' => __( 'Mismatch', 'ninja-forms' )
  231. ) );
  232. }
  233. add_action( 'ninja_forms_display_js', 'ninja_forms_display_js', 10, 2 );
  234. function ninja_forms_display_css(){
  235. wp_enqueue_style( 'ninja-forms-display', NINJA_FORMS_URL .'css/ninja-forms-display.css?nf_ver=' . NF_PLUGIN_VERSION );
  236. wp_enqueue_style( 'jquery-qtip', NINJA_FORMS_URL .'css/qtip.css' );
  237. wp_enqueue_style( 'jquery-rating', NINJA_FORMS_URL .'css/jquery.rating.css' );
  238. }
  239. add_action( 'ninja_forms_display_css', 'ninja_forms_display_css', 10, 2 );