NF_Display_Render

The Ninja Forms NF Display Render class.

Defined (1)

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

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