/includes/Display/Render.php

  1. <?php if ( ! defined( 'ABSPATH' ) ) exit; 
  2.  
  3. final class NF_Display_Render 
  4. protected static $loaded_templates = array( 
  5. 'app-layout',  
  6. 'app-before-form',  
  7. 'app-after-form',  
  8. 'app-before-fields',  
  9. 'app-after-fields',  
  10. 'app-before-field',  
  11. 'app-after-field',  
  12. 'form-layout',  
  13. 'form-hp',  
  14. 'field-layout',  
  15. 'field-before',  
  16. 'field-after',  
  17. 'fields-wrap',  
  18. 'fields-wrap-no-label',  
  19. 'fields-wrap-no-container',  
  20. 'fields-label',  
  21. 'fields-error',  
  22. 'form-error',  
  23. 'field-input-limit',  
  24. 'field-null' 
  25. ); 
  26.  
  27. protected static $use_test_values = FALSE; 
  28.  
  29. protected static $form_uses_recaptcha = array(); 
  30. protected static $form_uses_datepicker = array(); 
  31. protected static $form_uses_inputmask = array(); 
  32. protected static $form_uses_rte = array(); 
  33. protected static $form_uses_textarea_media = array(); 
  34. protected static $form_uses_helptext = array(); 
  35. protected static $form_uses_starrating = array(); 
  36.  
  37. public static function localize( $form_id ) 
  38. global $wp_locale; 
  39.  
  40. $capability = apply_filters( 'ninja_forms_display_test_values_capabilities', 'read' ); 
  41. if( isset( $_GET[ 'ninja_forms_test_values' ] ) && current_user_can( $capability ) ) { 
  42. self::$use_test_values = TRUE; 
  43.  
  44. if( ! has_action( 'wp_footer', 'NF_Display_Render::output_templates', 9999 ) ) { 
  45. add_action( 'wp_footer', 'NF_Display_Render::output_templates', 9999 ); 
  46. $form = Ninja_Forms()->form( $form_id )->get(); 
  47.  
  48. $settings = $form->get_settings(); 
  49.  
  50. foreach( $settings as $name => $value ) { 
  51. if( ! in_array( 
  52. $name,  
  53. array( 
  54. 'changeEmailErrorMsg',  
  55. 'confirmFieldErrorMsg',  
  56. 'fieldNumberNumMinError',  
  57. 'fieldNumberNumMaxError',  
  58. 'fieldNumberIncrementBy',  
  59. 'formErrorsCorrectErrors',  
  60. 'validateRequiredField',  
  61. 'honeypotHoneypotError',  
  62. 'fieldsMarkedRequired',  
  63. ) ) continue; 
  64.  
  65. if( $value ) continue; 
  66.  
  67. unset( $settings[ $name ] ); 
  68.  
  69. $settings = array_merge( Ninja_Forms::config( 'i18nFrontEnd' ), $settings );  
  70. $settings = apply_filters( 'ninja_forms_display_form_settings', $settings, $form_id ); 
  71.  
  72. $form->update_settings( $settings ); 
  73.  
  74. if( $form->get_setting( 'logged_in' ) && ! is_user_logged_in() ) { 
  75. echo do_shortcode( $form->get_setting( 'not_logged_in_msg' )); 
  76. return; 
  77.  
  78. if( $form->get_setting( 'sub_limit_number' ) ) { 
  79. $subs = Ninja_Forms()->form( $form_id )->get_subs(); 
  80.  
  81. // TODO: Optimize Query 
  82. global $wpdb; 
  83. $count = 0; 
  84. $subs = $wpdb->get_results( "SELECT post_id FROM " . $wpdb->postmeta . " WHERE `meta_key` = '_form_id' AND `meta_value` = $form_id" ); 
  85. foreach( $subs as $sub ) { 
  86. if( 'publish' == get_post_status( $sub->post_id ) ) $count++; 
  87.  
  88. if( $count >= $form->get_setting( 'sub_limit_number' ) ) { 
  89. echo do_shortcode( apply_filters( 'nf_sub_limit_reached_msg', $form->get_setting( 'sub_limit_msg' ), $form_id )); 
  90. return; 
  91.  
  92. if( ! apply_filters( 'ninja_forms_display_show_form', true, $form_id, $form ) ) return; 
  93.  
  94. $currency = $form->get_setting( 'currency', Ninja_Forms()->get_setting( 'currency' ) ); 
  95. $currency_symbol = Ninja_Forms::config( 'CurrencySymbol' ); 
  96. $form->update_setting( 'currency_symbol', ( isset( $currency_symbol[ $currency ] ) ) ? $currency_symbol[ $currency ] : '' ); 
  97.  
  98. $title = apply_filters( 'ninja_forms_form_title', $form->get_setting( 'title' ), $form_id ); 
  99. $form->update_setting( 'title', $title ); 
  100.  
  101. $before_form = apply_filters( 'ninja_forms_display_before_form', '', $form_id ); 
  102. $form->update_setting( 'beforeForm', $before_form ); 
  103.  
  104. $before_fields = apply_filters( 'ninja_forms_display_before_fields', '', $form_id ); 
  105. $form->update_setting( 'beforeFields', $before_fields ); 
  106.  
  107. $after_fields = apply_filters( 'ninja_forms_display_after_fields', '', $form_id ); 
  108. $form->update_setting( 'afterFields', $after_fields ); 
  109.  
  110. $after_form = apply_filters( 'ninja_forms_display_after_form', '', $form_id ); 
  111. $form->update_setting( 'afterForm', $after_form ); 
  112.  
  113. $form_fields = Ninja_Forms()->form( $form_id )->get_fields(); 
  114. $fields = array(); 
  115.  
  116. if( empty( $form_fields ) ) { 
  117. echo __( 'No Fields Found.', 'ninja-forms' ); 
  118. } else { 
  119.  
  120. // TODO: Replace unique field key checks with a refactored model/factory. 
  121. $unique_field_keys = array(); 
  122. $cache_updated = false; 
  123.  
  124. foreach ($form_fields as $field) { 
  125.  
  126. if( is_object( $field ) ) { 
  127. $field = array( 
  128. 'id' => $field->get_id(),  
  129. 'settings' => $field->get_settings() 
  130. ); 
  131.  
  132. $field_id = $field[ 'id' ]; 
  133.  
  134.  
  135. /** 
  136. * Duplicate field check. 
  137. * TODO: Replace unique field key checks with a refactored model/factory. 
  138. */ 
  139. $field_key = $field[ 'settings' ][ 'key' ]; 
  140.  
  141. if( in_array( $field_key, $unique_field_keys ) || '' == $field_key ) { 
  142.  
  143. // Delete the field. 
  144. Ninja_Forms()->request( 'delete-field' )->data( array( 'field_id' => $field_id ) )->dispatch(); 
  145.  
  146. // Remove the field from cache. 
  147. if( $form_cache ) { 
  148. if( isset( $form_cache[ 'fields' ] ) ) { 
  149. foreach( $form_cache[ 'fields' ] as $cached_field_key => $cached_field ) { 
  150. if( ! isset( $cached_field[ 'id' ] ) ) continue; 
  151. if( $field_id != $cached_field[ 'id' ] ) continue; 
  152.  
  153. // Flag cache to update. 
  154. $cache_updated = true; 
  155.  
  156. unset( $form_cache[ 'fields' ][ $cached_field_key ] ); // Remove the field. 
  157.  
  158. continue; // Skip the duplicate field. 
  159. array_push( $unique_field_keys, $field_key ); // Log unique key. 
  160. /** END Duplicate field check. */ 
  161.  
  162. $field_type = $field[ 'settings' ][ 'type' ]; 
  163.  
  164. if( ! is_string( $field_type ) ) continue; 
  165.  
  166. if( ! isset( Ninja_Forms()->fields[ $field_type ] ) ) { 
  167. $unknown_field = NF_Fields_Unknown::create( $field ); 
  168. $field = array( 
  169. 'settings' => $unknown_field->get_settings(),  
  170. 'id' => $unknown_field->get_id() 
  171. ); 
  172. $field_type = $field[ 'settings' ][ 'type' ]; 
  173.  
  174. $field = apply_filters('ninja_forms_localize_fields', $field); 
  175. $field = apply_filters('ninja_forms_localize_field_' . $field_type, $field); 
  176.  
  177. $field_class = Ninja_Forms()->fields[$field_type]; 
  178.  
  179. if (self::$use_test_values) { 
  180. $field[ 'value' ] = $field_class->get_test_value(); 
  181.  
  182. // Copy field ID into the field settings array for use in localized data. 
  183. $field[ 'settings' ][ 'id' ] = $field[ 'id' ]; 
  184.  
  185.  
  186. /** 
  187. * TODO: For backwards compatibility, run the original action, get contents from the output buffer, and return the contents through the filter. Also display a PHP Notice for a deprecate filter. 
  188. */ 
  189.  
  190. $display_before = apply_filters( 'ninja_forms_display_before_field_type_' . $field[ 'settings' ][ 'type' ], '' ); 
  191. $display_before = apply_filters( 'ninja_forms_display_before_field_key_' . $field[ 'settings' ][ 'key' ], $display_before ); 
  192. $field[ 'settings' ][ 'beforeField' ] = $display_before; 
  193.  
  194. $display_after = apply_filters( 'ninja_forms_display_after_field_type_' . $field[ 'settings' ][ 'type' ], '' ); 
  195. $display_after = apply_filters( 'ninja_forms_display_after_field_key_' . $field[ 'settings' ][ 'key' ], $display_after ); 
  196. $field[ 'settings' ][ 'afterField' ] = $display_after; 
  197.  
  198. $templates = $field_class->get_templates(); 
  199.  
  200. if (!array($templates)) { 
  201. $templates = array($templates); 
  202.  
  203. foreach ($templates as $template) { 
  204. self::load_template('fields-' . $template); 
  205.  
  206. $settings = $field[ 'settings' ]; 
  207. foreach ($settings as $key => $setting) { 
  208. if (is_numeric($setting)) $settings[$key] = floatval($setting); 
  209.  
  210. if( ! isset( $settings[ 'label_pos' ] ) || 'default' == $settings[ 'label_pos' ] ) { 
  211. $settings[ 'label_pos' ] = $form->get_setting( 'default_label_pos' ); 
  212.  
  213. $settings[ 'parentType' ] = $field_class->get_parent_type(); 
  214.  
  215. if( 'list' == $settings[ 'parentType' ] && isset( $settings[ 'options' ] ) && is_array( $settings[ 'options' ] ) ) { 
  216. $settings[ 'options' ] = apply_filters( 'ninja_forms_render_options', $settings[ 'options' ], $settings ); 
  217. $settings[ 'options' ] = apply_filters( 'ninja_forms_render_options_' . $field_type, $settings[ 'options' ], $settings ); 
  218.  
  219. $default_value = ( isset( $settings[ 'default' ] ) ) ? $settings[ 'default' ] : null; 
  220. $default_value = apply_filters('ninja_forms_render_default_value', $default_value, $field_type, $settings); 
  221. if ( $default_value ) { 
  222.  
  223. $default_value = preg_replace( '/{.*}/', '', $default_value ); 
  224.  
  225. if ($default_value) { 
  226. $settings['value'] = $default_value; 
  227.  
  228. ob_start(); 
  229. do_shortcode( $settings['value'] ); 
  230. $ob = ob_get_clean(); 
  231.  
  232. if( ! $ob ) { 
  233. $settings['value'] = do_shortcode( $settings['value'] ); 
  234.  
  235. $thousands_sep = $wp_locale->number_format[ 'thousands_sep']; 
  236. $decimal_point = $wp_locale->number_format[ 'decimal_point' ]; 
  237.  
  238. // TODO: Find a better way to do this. 
  239. if ('shipping' == $settings['type']) { 
  240. $settings[ 'shipping_cost' ] = preg_replace ('/[^\d, \.]/', '', $settings[ 'shipping_cost' ] ); 
  241. $settings[ 'shipping_cost' ] = str_replace( Ninja_Forms()->get_setting( 'currency_symbol' ), '', $settings[ 'shipping_cost' ] ); 
  242.  
  243. $settings[ 'shipping_cost' ] = str_replace( $decimal_point, '||', $settings[ 'shipping_cost' ] ); 
  244. $settings[ 'shipping_cost' ] = str_replace( $thousands_sep, '', $settings[ 'shipping_cost' ] ); 
  245. $settings[ 'shipping_cost' ] = str_replace( '||', '.', $settings[ 'shipping_cost' ] ); 
  246. } elseif ('product' == $settings['type']) { 
  247. $settings['product_price'] = preg_replace ('/[^\d, \.]/', '', $settings[ 'product_price' ] ); 
  248. $settings['product_price'] = str_replace( Ninja_Forms()->get_setting( 'currency_symbol' ), '', $settings['product_price']); 
  249.  
  250. $settings[ 'product_price' ] = str_replace( $decimal_point, '||', $settings[ 'product_price' ] ); 
  251. $settings[ 'product_price' ] = str_replace( $thousands_sep, '', $settings[ 'product_price' ] ); 
  252. $settings[ 'product_price' ] = str_replace( '||', '.', $settings[ 'product_price' ] ); 
  253.  
  254. } elseif ('total' == $settings['type'] && isset($settings['value'])) { 
  255. $settings['value'] = number_format($settings['value'], 2); 
  256.  
  257. $settings['element_templates'] = $templates; 
  258. $settings['old_classname'] = $field_class->get_old_classname(); 
  259. $settings['wrap_template'] = $field_class->get_wrap_template(); 
  260.  
  261. $fields[] = apply_filters( 'ninja_forms_localize_field_settings_' . $field_type, $settings, $form ); 
  262.  
  263. if( 'recaptcha' == $field[ 'settings' ][ 'type' ] ) { 
  264. array_push( self::$form_uses_recaptcha, $form_id ); 
  265. if( 'date' == $field[ 'settings' ][ 'type' ] ) { 
  266. array_push( self::$form_uses_datepicker, $form_id ); 
  267. if( 'starrating' == $field[ 'settings' ][ 'type' ] ) { 
  268. array_push( self::$form_uses_starrating, $form_id ); 
  269. if( isset( $field[ 'settings' ][ 'mask' ] ) && $field[ 'settings' ][ 'mask' ] ) { 
  270. array_push( self::$form_uses_inputmask, $form_id ); 
  271. if( isset( $field[ 'settings' ][ 'textarea_rte' ] ) && $field[ 'settings' ][ 'textarea_rte' ] ) { 
  272. array_push( self::$form_uses_rte, $form_id ); 
  273. if( isset( $field[ 'settings' ][ 'textarea_media' ] ) && $field[ 'settings' ][ 'textarea_media' ] ) { 
  274. array_push( self::$form_uses_textarea_media, $form_id ); 
  275. if( isset( $field[ 'settings' ][ 'help_text' ] ) && strip_tags( $field[ 'settings' ][ 'help_text' ] ) ) { 
  276. array_push( self::$form_uses_helptext, $form_id ); 
  277.  
  278. if( $cache_updated ) { 
  279. update_option('nf_form_' . $form_id, $form_cache); // Update form cache without duplicate fields. 
  280.  
  281. $fields = apply_filters( 'ninja_forms_display_fields', $fields ); 
  282.  
  283. // Output Form Container 
  284. do_action( 'ninja_forms_before_container', $form_id, $form->get_settings(), $form_fields ); 
  285. Ninja_Forms::template( 'display-form-container.html.php', compact( 'form_id' ) ); 
  286.  
  287. ?> 
  288. <!-- TODO: Move to Template File. --> 
  289. <script> 
  290. var formDisplay = 1; 
  291.  
  292. /* Maybe initialize nfForms object */ 
  293. var nfForms = nfForms || []; 
  294.  
  295. /* Build Form Data */ 
  296. var form = []; 
  297. form.id = '<?php echo $form_id; ?>'; 
  298. form.settings = <?php echo wp_json_encode( $form->get_settings() ); ?>; 
  299. form.fields = <?php echo wp_json_encode( $fields ); ?>; 
  300.  
  301. /* Add Form Data to nfForms object */ 
  302. nfForms.push( form ); 
  303. </script> 
  304.  
  305. <?php 
  306. self::enqueue_scripts( $form_id ); 
  307.  
  308. public static function localize_preview( $form_id ) 
  309. $capability = apply_filters( 'ninja_forms_display_test_values_capabilities', 'read' ); 
  310. if( isset( $_GET[ 'ninja_forms_test_values' ] ) && current_user_can( $capability ) ) { 
  311. self::$use_test_values = TRUE; 
  312.  
  313. add_action( 'wp_footer', 'NF_Display_Render::output_templates', 9999 ); 
  314.  
  315. $form = get_user_option( 'nf_form_preview_' . $form_id ); 
  316.  
  317. if( ! $form ) { 
  318. self::localize( $form_id ); 
  319. return; 
  320.  
  321. if( isset( $form[ 'settings' ][ 'logged_in' ] ) && $form[ 'settings' ][ 'logged_in' ] && ! is_user_logged_in() ) { 
  322. echo do_shortcode( $form[ 'settings' ][ 'not_logged_in_msg' ]); 
  323. return; 
  324.  
  325. $form[ 'settings' ] = array_merge( Ninja_Forms::config( 'i18nFrontEnd' ), $form[ 'settings' ] ); 
  326. $form[ 'settings' ] = apply_filters( 'ninja_forms_display_form_settings', $form[ 'settings' ], $form_id ); 
  327.  
  328.  
  329. $form[ 'settings' ][ 'is_preview' ] = TRUE; 
  330.  
  331. $currency = ( isset( $form[ 'settings' ][ 'currency' ] ) && $form[ 'settings' ][ 'currency' ] ) ? $form[ 'settings' ][ 'currency' ] : Ninja_Forms()->get_setting( 'currency' ) ; 
  332. $currency_symbol = Ninja_Forms::config( 'CurrencySymbol' ); 
  333. $form[ 'settings' ][ 'currency_symbol' ] = ( isset( $currency_symbol[ $currency ] ) ) ? $currency_symbol[ $currency ] : ''; 
  334.  
  335. $before_form = apply_filters( 'ninja_forms_display_before_form', '', $form_id, TRUE ); 
  336. $form[ 'settings' ][ 'beforeForm'] = $before_form; 
  337.  
  338. $before_fields = apply_filters( 'ninja_forms_display_before_fields', '', $form_id, TRUE ); 
  339. $form[ 'settings' ][ 'beforeFields'] = $before_fields; 
  340.  
  341. $after_fields = apply_filters( 'ninja_forms_display_after_fields', '', $form_id, TRUE ); 
  342. $form[ 'settings' ][ 'afterFields'] = $after_fields; 
  343.  
  344. $after_form = apply_filters( 'ninja_forms_display_after_form', '', $form_id, TRUE ); 
  345. $form[ 'settings' ][ 'afterForm'] = $after_form; 
  346.  
  347. $fields = array(); 
  348.  
  349. if( empty( $form['fields'] ) ) { 
  350. echo __( 'No Fields Found.', 'ninja-forms' ); 
  351. } else { 
  352. foreach ($form['fields'] as $field_id => $field) { 
  353.  
  354. $field_type = $field['settings']['type']; 
  355.  
  356. if( ! isset( Ninja_Forms()->fields[ $field_type ] ) ) continue; 
  357. if( ! apply_filters( 'ninja_forms_preview_display_type_' . $field_type, TRUE ) ) continue; 
  358. if( ! apply_filters( 'ninja_forms_preview_display_field', $field ) ) continue; 
  359.  
  360. $field['settings']['id'] = $field_id; 
  361.  
  362. $field = apply_filters('ninja_forms_localize_fields_preview', $field); 
  363. $field = apply_filters('ninja_forms_localize_field_' . $field_type . '_preview', $field); 
  364.  
  365. $display_before = apply_filters( 'ninja_forms_display_before_field_type_' . $field['settings'][ 'type' ], '' ); 
  366. $display_before = apply_filters( 'ninja_forms_display_before_field_key_' . $field['settings'][ 'key' ], $display_before ); 
  367. $field['settings'][ 'beforeField' ] = $display_before; 
  368.  
  369. $display_after = apply_filters( 'ninja_forms_display_after_field_type_' . $field['settings'][ 'type' ], '' ); 
  370. $display_after = apply_filters( 'ninja_forms_display_after_field_key_' . $field['settings'][ 'key' ], $display_after ); 
  371. $field['settings'][ 'afterField' ] = $display_after; 
  372.  
  373. foreach ($field['settings'] as $key => $setting) { 
  374. if (is_numeric($setting)) $field['settings'][$key] = floatval($setting); 
  375.  
  376. if( ! isset( $field['settings'][ 'label_pos' ] ) || 'default' == $field['settings'][ 'label_pos' ] ) { 
  377. if( isset( $form[ 'settings' ][ 'default_label_pos' ] ) ) { 
  378. $field['settings'][ 'label_pos' ] = $form[ 'settings' ][ 'default_label_pos' ]; 
  379.  
  380. $field_class = Ninja_Forms()->fields[$field_type]; 
  381.  
  382. $templates = $field_class->get_templates(); 
  383.  
  384. if (!array($templates)) { 
  385. $templates = array($templates); 
  386.  
  387. foreach ($templates as $template) { 
  388. self::load_template('fields-' . $template); 
  389.  
  390. if (self::$use_test_values) { 
  391. $field['settings']['value'] = $field_class->get_test_value(); 
  392.  
  393. $field[ 'settings' ][ 'parentType' ] = $field_class->get_parent_type(); 
  394.  
  395. if( 'list' == $field[ 'settings' ][ 'parentType' ] && isset( $field['settings'][ 'options' ] ) && is_array( $field['settings'][ 'options' ] ) ) { 
  396. $field['settings'][ 'options' ] = apply_filters( 'ninja_forms_render_options', $field['settings'][ 'options' ], $field['settings'] ); 
  397. $field['settings'][ 'options' ] = apply_filters( 'ninja_forms_render_options_' . $field['settings'][ 'type' ], $field['settings'][ 'options' ], $field['settings'] ); 
  398.  
  399. $default_value = ( isset( $field[ 'settings' ][ 'default' ] ) ) ? $field[ 'settings' ][ 'default' ] : null; 
  400. $default_value = apply_filters( 'ninja_forms_render_default_value', $default_value, $field_type, $field[ 'settings' ]); 
  401. if( $default_value ) { 
  402.  
  403. $default_value = preg_replace( '/{.*}/', '', $default_value ); 
  404.  
  405. if ($default_value) { 
  406. $field['settings']['value'] = $default_value; 
  407.  
  408. ob_start(); 
  409. do_shortcode( $field['settings']['value'] ); 
  410. $ob = ob_get_clean(); 
  411.  
  412. if( ! $ob ) { 
  413. $field['settings']['value'] = do_shortcode( $field['settings']['value'] ); 
  414.  
  415. // TODO: Find a better way to do this. 
  416. if ('shipping' == $field['settings']['type']) { 
  417. $field['settings']['shipping_cost'] = preg_replace ('/[^\d, \.]/', '', $field['settings']['shipping_cost'] ); 
  418. $field['settings']['shipping_cost'] = str_replace( Ninja_Forms()->get_setting( 'currency_symbol' ), '', $field['settings']['shipping_cost'] ); 
  419. $field['settings']['shipping_cost'] = number_format($field['settings']['shipping_cost'], 2); 
  420. } elseif ('product' == $field['settings']['type']) { 
  421. // TODO: Does the currency marker need to stripped here? 
  422. $field['settings']['product_price'] = preg_replace ('/[^\d, \.]/', '', $field['settings']['product_price'] ); 
  423. $field['settings']['product_price'] = str_replace( Ninja_Forms()->get_setting( 'currency_symbol' ), '', $field['settings']['product_price'] ); 
  424. $field['settings']['product_price'] = number_format($field['settings']['product_price'], 2); 
  425. } elseif ('total' == $field['settings']['type']) { 
  426. if( ! isset( $field[ 'settings' ][ 'value' ] ) ) $field[ 'settings' ][ 'value' ] = 0; 
  427. $field['settings']['value'] = number_format($field['settings']['value'], 2); 
  428.  
  429. $field['settings']['element_templates'] = $templates; 
  430. $field['settings']['old_classname'] = $field_class->get_old_classname(); 
  431. $field['settings']['wrap_template'] = $field_class->get_wrap_template(); 
  432.  
  433. $fields[] = apply_filters( 'ninja_forms_localize_field_settings_' . $field_type, $field['settings'], $form ); 
  434.  
  435. // Output Form Container 
  436. do_action( 'ninja_forms_before_container_preview', $form_id, $form[ 'settings' ], $fields ); 
  437. Ninja_Forms::template( 'display-form-container.html.php', compact( 'form_id' ) ); 
  438.  
  439. ?> 
  440. <!-- TODO: Move to Template File. --> 
  441. <script> 
  442. // Maybe initialize nfForms object 
  443. var nfForms = nfForms || []; 
  444.  
  445. // Build Form Data 
  446. var form = []; 
  447. form.id = '<?php echo $form['id']; ?>'; 
  448. form.settings = JSON.parse( '<?php echo WPN_Helper::addslashes( wp_json_encode( $form['settings'] ) ); ?>' ); 
  449.  
  450. form.fields = JSON.parse( '<?php echo WPN_Helper::addslashes( wp_json_encode( $fields ) ); ?>' ); 
  451.  
  452. // Add Form Data to nfForms object 
  453. nfForms.push( form ); 
  454. </script> 
  455.  
  456. <?php 
  457. self::enqueue_scripts( $form_id, true ); 
  458.  
  459. public static function enqueue_scripts( $form_id, $is_preview = false ) 
  460. global $wp_locale; 
  461. $form = Ninja_Forms()->form( $form_id )->get(); 
  462.  
  463. $ver = Ninja_Forms::VERSION; 
  464. $js_dir = Ninja_Forms::$url . 'assets/js/min/'; 
  465. $css_dir = Ninja_Forms::$url . 'assets/css/'; 
  466.  
  467. self::enqueue_styles_display( $css_dir ); 
  468.  
  469. if( $is_preview || in_array( $form_id, self::$form_uses_recaptcha ) ) { 
  470. $recaptcha_lang = Ninja_Forms()->get_setting('recaptcha_lang'); 
  471. wp_enqueue_script('nf-google-recaptcha', 'https://www.google.com/recaptcha/api.js?hl=' . $recaptcha_lang . '&onload=nfRenderRecaptcha&render=explicit', array( 'jquery', 'nf-front-end-deps' ), $ver, TRUE ); 
  472.  
  473. if( $is_preview || in_array( $form_id, self::$form_uses_datepicker ) ) { 
  474. wp_enqueue_style( 'pikaday-responsive', $css_dir . 'pikaday-package.css', $ver ); 
  475. wp_enqueue_script('nf-front-end--datepicker', $js_dir . 'front-end--datepicker.min.js', array( 'jquery' ), $ver ); 
  476.  
  477. if( $is_preview || in_array( $form_id, self::$form_uses_inputmask ) ) { 
  478. wp_enqueue_script('nf-front-end--inputmask', $js_dir . 'front-end--inputmask.min.js', array( 'jquery' ), $ver ); 
  479.  
  480. if( $is_preview || in_array( $form_id, self::$form_uses_rte ) ) { 
  481. if( $is_preview || in_array( $form_id, self::$form_uses_textarea_media ) ) { 
  482. wp_enqueue_media(); 
  483.  
  484. wp_enqueue_style( 'summernote', $css_dir . 'summernote.css' , $ver ); 
  485. wp_enqueue_style( 'codemirror', $css_dir . 'codemirror.css' , $ver ); 
  486. wp_enqueue_style( 'codemirror-monokai', $css_dir . 'monokai-theme.css', $ver ); 
  487. wp_enqueue_script('nf-front-end--rte', $js_dir . 'front-end--rte.min.js', array( 'jquery' ), $ver ); 
  488.  
  489. if( $is_preview || in_array( $form_id, self::$form_uses_helptext ) ) { 
  490. wp_enqueue_style( 'jBox', $css_dir . 'jBox.css', $ver ); 
  491. wp_enqueue_script('nf-front-end--helptext', $js_dir . 'front-end--helptext.min.js', array( 'jquery' ), $ver ); 
  492.  
  493. if( $is_preview || in_array( $form_id, self::$form_uses_starrating ) ) { 
  494. wp_enqueue_style( 'rating', $css_dir . 'rating.css', Ninja_Forms::VERSION ); 
  495. wp_enqueue_script('nf-front-end--starrating', $js_dir . 'front-end--starrating.min.js', array( 'jquery' ), $ver ); 
  496.  
  497. wp_enqueue_script( 'nf-front-end-deps', $js_dir . 'front-end-deps.js', array( 'jquery', 'backbone' ), $ver ); 
  498. wp_enqueue_script( 'nf-front-end', $js_dir . 'front-end.js', array( 'nf-front-end-deps' ), $ver ); 
  499.  
  500. wp_localize_script( 'nf-front-end', 'nfi18n', Ninja_Forms::config( 'i18nFrontEnd' ) ); 
  501.  
  502. $data = apply_filters( 'ninja_forms_render_localize_script_data', array( 
  503. 'ajaxNonce' => wp_create_nonce( 'ninja_forms_display_nonce' ),  
  504. 'adminAjax' => admin_url( 'admin-ajax.php' ),  
  505. 'requireBaseUrl' => Ninja_Forms::$url . 'assets/js/',  
  506. 'use_merge_tags' => array(),  
  507. 'opinionated_styles' => Ninja_Forms()->get_setting( 'opinionated_styles' ) 
  508. )); 
  509.  
  510. foreach( Ninja_Forms()->fields as $field ) { 
  511. foreach( $field->use_merge_tags() as $merge_tag ) { 
  512. $data[ 'use_merge_tags' ][ $merge_tag ][ $field->get_type() ] = $field->get_type(); 
  513.  
  514. wp_localize_script( 'nf-front-end', 'nfFrontEnd', $data ); 
  515.  
  516. do_action( 'ninja_forms_enqueue_scripts', array( 'form_id' => $form_id ) ); 
  517.  
  518. do_action( 'nf_display_enqueue_scripts' ); 
  519.  
  520. /** 
  521. * Enqueue NF frontend basic display styles. 
  522. * 
  523. * @param string $css_dir 
  524. */ 
  525. public static function enqueue_styles_display( $css_dir ) { 
  526. switch( Ninja_Forms()->get_setting( 'opinionated_styles' ) ) { 
  527. case 'light': 
  528. wp_enqueue_style( 'nf-display', $css_dir . 'display-opinions-light.css', array( 'dashicons' ) ); 
  529. wp_enqueue_style( 'nf-font-awesome', $css_dir . 'font-awesome.min.css' ); 
  530. break; 
  531. case 'dark': 
  532. wp_enqueue_style( 'nf-display', $css_dir . 'display-opinions-dark.css', array( 'dashicons' ) ); 
  533. wp_enqueue_style( 'nf-font-awesome', $css_dir . 'font-awesome.min.css' ); 
  534. break; 
  535. default: 
  536. wp_enqueue_style( 'nf-display', $css_dir . 'display-structure.css', array( 'dashicons' ) ); 
  537.  
  538. protected static function load_template( $file_name = '' ) 
  539. if( ! $file_name ) return; 
  540.  
  541. if( self::is_template_loaded( $file_name ) ) return; 
  542.  
  543. self::$loaded_templates[] = $file_name; 
  544.  
  545. public static function output_templates() 
  546. // Build File Path Hierarchy 
  547. $file_paths = apply_filters( 'ninja_forms_field_template_file_paths', array( 
  548. get_template_directory() . '/ninja-forms/templates/',  
  549. )); 
  550.  
  551. $file_paths[] = Ninja_Forms::$dir . 'includes/Templates/'; 
  552.  
  553. // Search for and Output File Templates 
  554. foreach( self::$loaded_templates as $file_name ) { 
  555.  
  556. foreach( $file_paths as $path ) { 
  557.  
  558. if( file_exists( $path . "$file_name.html" ) ) { 
  559. echo file_get_contents( $path . "$file_name.html" ); 
  560. break; 
  561.  
  562. ?> 
  563. <script> 
  564. var post_max_size = '<?php echo WPN_Helper::string_to_bytes( ini_get('post_max_size') ); ?>'; 
  565. var upload_max_filesize = '<?php echo WPN_Helper::string_to_bytes( ini_get( 'upload_max_filesize' ) ); ?>'; 
  566. var wp_memory_limit = '<?php echo WPN_Helper::string_to_bytes( WP_MEMORY_LIMIT ); ?>'; 
  567. </script> 
  568. <?php 
  569.  
  570. // Action to Output Custom Templates 
  571. do_action( 'ninja_forms_output_templates' ); 
  572.  
  573. /** 
  574. * UTILITY 
  575. */ 
  576.  
  577. protected static function is_template_loaded( $template_name ) 
  578. return ( in_array( $template_name, self::$loaded_templates ) ) ? TRUE : FALSE ; 
  579.  
  580. } // End Class NF_Display_Render 
.