NF_Admin_Menus_Forms

The Ninja Forms NF Admin Menus Forms class.

Defined (1)

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

/includes/Admin/Menus/Forms.php  
  1. final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu 
  2. public $page_title = 'Forms'; 
  3.  
  4. public $menu_slug = 'ninja-forms'; 
  5.  
  6. public $icon_url = 'dashicons-feedback'; 
  7.  
  8. public $position = '35.1337'; 
  9.  
  10. public function __construct() 
  11. parent::__construct(); 
  12.  
  13. if( ! defined( 'DOING_AJAX' ) ) { 
  14. add_action('admin_init', array($this, 'admin_init')); 
  15. add_action( 'admin_init', array( 'NF_Admin_AllFormsTable', 'process_bulk_action' ) ); 
  16.  
  17. public function get_page_title() 
  18. return __( 'Forms', 'ninja-forms' ); 
  19.  
  20. public function admin_init() 
  21. if( isset( $_GET[ 'form_id' ] ) && ! is_numeric( $_GET[ 'form_id' ] ) && 'new' != $_GET[ 'form_id' ] ) { 
  22. if( current_user_can( apply_filters( 'ninja_forms_admin_import_template_capabilities', 'manage_options' ) ) ) { 
  23. $this->import_from_template(); 
  24.  
  25. $this->table = new NF_Admin_AllFormsTable(); 
  26.  
  27. public function display() 
  28. if( isset( $_GET[ 'form_id' ] ) ) { 
  29.  
  30.  
  31. if( 'new' == $_GET[ 'form_id' ] ) { 
  32. $form_id = 'tmp-' . time(); 
  33. } else { 
  34. $form_id = (is_numeric($_GET['form_id'])) ? absint($_GET['form_id']) : ''; 
  35.  
  36. /** 
  37. * FORM BUILDER 
  38. */ 
  39.  
  40. Ninja_Forms::template( 'admin-menu-new-form.html.php' ); 
  41.  
  42. $this->_enqueue_the_things( $form_id ); 
  43.  
  44. delete_user_option( get_current_user_id(), 'nf_form_preview_' . $form_id ); 
  45.  
  46. if( ! isset( $_GET[ 'ajax' ] ) ) { 
  47. $this->_localize_form_data( $form_id ); 
  48.  
  49. $this->_localize_field_type_data(); 
  50.  
  51. $this->_localize_action_type_data(); 
  52.  
  53. $this->_localize_form_settings(); 
  54.  
  55. $this->_localize_merge_tags(); 
  56. } else { 
  57.  
  58. /** 
  59. * ALL FORMS TABLE 
  60. */ 
  61.  
  62. $this->table->prepare_items(); 
  63.  
  64. Ninja_Forms::template( 'admin-menu-all-forms.html.php', array( 
  65. 'table' => $this->table,  
  66. 'add_new_url' => admin_url( 'admin.php?page=ninja-forms&form_id=new' ),  
  67. 'add_new_text' => __( 'Add New Form', 'ninja-forms' ) 
  68. ) ); 
  69.  
  70. public function submenu_separators() 
  71. add_submenu_page( 'ninja-forms', '', '', 'read', '', '' ); 
  72.  
  73. private function import_from_template() 
  74. $template = sanitize_title( $_GET['form_id'] ); 
  75.  
  76. $form = Ninja_Forms::template( $template . '.nff', array(), TRUE ); 
  77.  
  78. if( ! $form ) die( 'Template not found' ); 
  79.  
  80. $form = json_decode( html_entity_decode( $form ), true ); 
  81.  
  82. $form_id = Ninja_Forms()->form()->import_form( $form ); 
  83.  
  84. if( ! $form_id ) { 
  85. $error_message = ( function_exists( 'json_last_error_msg' ) && json_last_error_msg() ) ? json_last_error_msg() : __( 'Form Template Import Error.', 'ninja-forms' ); 
  86. wp_die( $error_message ); 
  87.  
  88. header( "Location: " . admin_url( "admin.php?page=ninja-forms&form_id=$form_id" ) ); 
  89. exit(); 
  90.  
  91. private function _enqueue_the_things( $form_id ) 
  92. global $wp_locale; 
  93.  
  94. wp_enqueue_media(); 
  95.  
  96. wp_enqueue_style( 'nf-builder', Ninja_Forms::$url . 'assets/css/builder.css' ); 
  97. wp_enqueue_style( 'nf-font-awesome', Ninja_Forms::$url . 'assets/css/font-awesome.min.css' ); 
  98. /** 
  99. * CSS Libraries 
  100. */ 
  101. wp_enqueue_style( 'wp-color-picker' ); 
  102. wp_enqueue_style( 'jBox', Ninja_Forms::$url . 'assets/css/jBox.css' ); 
  103. wp_enqueue_style( 'summernote', Ninja_Forms::$url . 'assets/css/summernote.css' ); 
  104. wp_enqueue_style( 'codemirror', Ninja_Forms::$url . 'assets/css/codemirror.css' ); 
  105. wp_enqueue_style( 'codemirror-monokai', Ninja_Forms::$url . 'assets/css/monokai-theme.css' ); 
  106. wp_enqueue_style( 'pikaday-responsive', Ninja_Forms::$url . 'assets/css/pikaday-package.css' ); 
  107.  
  108. /** 
  109. * JS Libraries 
  110. */ 
  111. wp_enqueue_script( 'wp-util' ); 
  112. wp_enqueue_script( 'jquery-autoNumeric', Ninja_Forms::$url . 'assets/js/lib/jquery.autoNumeric.min.js', array( 'jquery', 'backbone' ) ); 
  113. wp_enqueue_script( 'jquery-maskedinput', Ninja_Forms::$url . 'assets/js/lib/jquery.maskedinput.min.js', array( 'jquery', 'backbone' ) ); 
  114. wp_enqueue_script( 'backbone-marionette', Ninja_Forms::$url . 'assets/js/lib/backbone.marionette.min.js', array( 'jquery', 'backbone' ) ); 
  115. wp_enqueue_script( 'backbone-radio', Ninja_Forms::$url . 'assets/js/lib/backbone.radio.min.js', array( 'jquery', 'backbone' ) ); 
  116. wp_enqueue_script( 'jquery-perfect-scrollbar', Ninja_Forms::$url . 'assets/js/lib/perfect-scrollbar.jquery.min.js', array( 'jquery' ) ); 
  117. wp_enqueue_script( 'jquery-hotkeys-new', Ninja_Forms::$url . 'assets/js/lib/jquery.hotkeys.min.js' ); 
  118. wp_enqueue_script( 'jBox', Ninja_Forms::$url . 'assets/js/lib/jBox.min.js' ); 
  119. wp_enqueue_script( 'jquery-caret', Ninja_Forms::$url . 'assets/js/lib/jquery.caret.min.js' ); 
  120. wp_enqueue_script( 'speakingurl', Ninja_Forms::$url . 'assets/js/lib/speakingurl.js' ); 
  121. wp_enqueue_script( 'jquery-slugify', Ninja_Forms::$url . 'assets/js/lib/slugify.min.js', array( 'jquery', 'speakingurl' ) ); 
  122. wp_enqueue_script( 'jquery-mobile-events', Ninja_Forms::$url . 'assets/js/lib/jquery.mobile-events.min.js', array( 'jquery' ) ); 
  123. wp_enqueue_script( 'jquery-ui-touch-punch', Ninja_Forms::$url . 'assets/js/lib/jquery.ui.touch-punch.min.js', array( 'jquery' ) ); 
  124. wp_enqueue_script( 'jquery-classy-wiggle', Ninja_Forms::$url . 'assets/js/lib/jquery.classywiggle.min.js', array( 'jquery' ) ); 
  125. wp_enqueue_script( 'moment-with-locale', Ninja_Forms::$url . 'assets/js/lib/moment-with-locales.min.js', array( 'jquery' ) ); 
  126. wp_enqueue_script( 'modernizr', Ninja_Forms::$url . 'assets/js/lib/modernizr.min.js', array( 'jquery' ) ); 
  127. wp_enqueue_script( 'pikaday', Ninja_Forms::$url . 'assets/js/lib/pikaday.min.js', array( 'moment-with-locale' ) ); 
  128. wp_enqueue_script( 'pikaday-responsive', Ninja_Forms::$url . 'assets/js/lib/pikaday-responsive.min.js', array( 'pikaday', 'modernizr' ) ); 
  129.  
  130. wp_enqueue_script( 'bootstrap', Ninja_Forms::$url . 'assets/js/lib/bootstrap.min.js', array( 'jquery' ) ); 
  131. wp_enqueue_script( 'codemirror', Ninja_Forms::$url . 'assets/js/lib/codemirror.min.js', array( 'jquery' ) ); 
  132. wp_enqueue_script( 'codemirror-xml', Ninja_Forms::$url . 'assets/js/lib/codemirror-xml.min.js', array( 'jquery' ) ); 
  133. wp_enqueue_script( 'codemirror-formatting', Ninja_Forms::$url . 'assets/js/lib/codemirror-formatting.min.js', array( 'jquery' ) ); 
  134. wp_enqueue_script( 'summernote', Ninja_Forms::$url . 'assets/js/lib/summernote.min.js', array( 'jquery', 'speakingurl' ) ); 
  135.  
  136.  
  137. wp_enqueue_script( 'nf-builder', Ninja_Forms::$url . 'assets/js/min/builder.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'jquery-effects-bounce', 'wp-color-picker' ) ); 
  138. wp_localize_script( 'nf-builder', 'nfi18n', Ninja_Forms::config( 'i18nBuilder' ) ); 
  139.  
  140. wp_localize_script( 'nf-builder', 'nfAdmin', array( 
  141. 'ajaxNonce' => wp_create_nonce( 'ninja_forms_builder_nonce' ),  
  142. 'requireBaseUrl' => Ninja_Forms::$url . 'assets/js/',  
  143. 'previewurl' => home_url() . '/?nf_preview_form=',  
  144. 'wp_locale' => $wp_locale->number_format,  
  145. 'editFormText' => __( 'Edit Form', 'ninja-forms' ),  
  146. 'mobile' => ( wp_is_mobile() ) ? 1: 0,  
  147. 'currencySymbols' => array_merge( array( '' => Ninja_Forms()->get_setting( 'currency_symbol' ) ), Ninja_Forms::config( 'CurrencySymbol' ) ),  
  148. 'dateFormat' => Ninja_Forms()->get_setting( 'date_format' ) 
  149. )); 
  150.  
  151. do_action( 'nf_admin_enqueue_scripts' ); 
  152.  
  153. private function _localize_form_data( $form_id ) 
  154. $form = Ninja_Forms()->form( $form_id )->get(); 
  155.  
  156. if( ! $form->get_tmp_id() ) { 
  157. $fields = ($form_id) ? Ninja_Forms()->form($form_id)->get_fields() : array(); 
  158. $actions = ($form_id) ? Ninja_Forms()->form($form_id)->get_actions() : array(); 
  159. } else { 
  160. $fields = array(); 
  161. $actions = array(); 
  162.  
  163. $fields_settings = array(); 
  164.  
  165. // echo "<pre>"; 
  166. // print_r( $fields ); 
  167. // echo "</pre>"; 
  168. // die(); 
  169.  
  170. if( ! empty( $fields ) ) { 
  171. foreach ($fields as $field) { 
  172.  
  173. $type = $field->get_setting( 'type' ); 
  174.  
  175. $field_id = $field->get_id(); 
  176.  
  177. if( ! isset( Ninja_Forms()->fields[ $type ] ) ) { 
  178. $field = NF_Fields_Unknown::create( $field ); 
  179.  
  180. $settings = $field->get_settings(); 
  181. $settings[ 'id' ] = $field_id; 
  182.  
  183.  
  184. foreach ($settings as $key => $setting) { 
  185. if (is_numeric($setting)) $settings[$key] = floatval($setting); 
  186.  
  187. $fields_settings[] = $settings; 
  188.  
  189. $actions_settings = array(); 
  190.  
  191. if( ! empty( $actions ) ) { 
  192. foreach ($actions as $action) { 
  193.  
  194. $type = $action->get_setting( 'type' ); 
  195.  
  196. if( ! isset( Ninja_Forms()->actions[ $type ] ) ) continue; 
  197.  
  198. $settings = $action->get_settings(); 
  199. $settings['id'] = $action->get_id(); 
  200.  
  201. $actions_settings[] = $settings; 
  202.  
  203. if( $form->get_tmp_id() ) { 
  204.  
  205. $actions_settings = Ninja_Forms()->config( 'FormActionDefaults' ); 
  206.  
  207. $form_data = array(); 
  208. $form_data['id'] = $form_id; 
  209. $form_data['settings'] = $form->get_settings(); 
  210. $form_data['fields'] = $fields_settings; 
  211. $form_data['actions'] = $actions_settings; 
  212.  
  213. ?> 
  214. <script> 
  215. var preloadedFormData = <?php echo wp_json_encode( $form_data ); ?>; 
  216. // console.log( preloadedFormData ); 
  217. </script> 
  218. <?php 
  219.  
  220. private function _localize_field_type_data() 
  221. $field_type_sections = array_values( Ninja_Forms()->config( 'FieldTypeSections' ) ); 
  222. $field_type_settings = array(); 
  223.  
  224. $master_settings = array(); 
  225.  
  226. $setting_defaults = array(); 
  227.  
  228. foreach( Ninja_Forms()->fields as $field ) { 
  229.  
  230. $name = $field->get_name(); 
  231. $settings = $field->get_settings(); 
  232. $groups = Ninja_Forms::config( 'SettingsGroups' ); 
  233.  
  234. $unique_settings = $this->_unique_settings( $settings ); 
  235.  
  236. $master_settings = array_merge( $master_settings, $unique_settings ); 
  237.  
  238. $settings_groups = $this->_group_settings( $settings, $groups ); 
  239.  
  240. $settings_defaults = $this->_setting_defaults( $unique_settings ); 
  241.  
  242. $field_type_settings[ $name ] = array( 
  243. 'id' => $name,  
  244. 'nicename' => $field->get_nicename(),  
  245. 'alias' => $field->get_aliases(),  
  246. 'parentType' => $field->get_parent_type(),  
  247. 'section' => $field->get_section(),  
  248. 'icon' => $field->get_icon(),  
  249. 'type' => $field->get_type(),  
  250. 'settingGroups' => $settings_groups,  
  251. 'settingDefaults' => $settings_defaults 
  252. ); 
  253.  
  254. $saved_fields = Ninja_Forms()->form()->get_fields( array( 'saved' => 1) ); 
  255.  
  256. foreach( $saved_fields as $saved_field ) { 
  257.  
  258. $settings = $saved_field->get_settings(); 
  259.  
  260. $id = $saved_field->get_id(); 
  261. $type = $settings[ 'type' ]; 
  262. $label = $settings[ 'label' ]; 
  263.  
  264. $field_type_settings[ $id ] = $field_type_settings[ $type ]; 
  265. $field_type_settings[ $id ][ 'id' ] = $id; 
  266. $field_type_settings[ $id ][ 'type' ] = $type; 
  267. $field_type_settings[ $id ][ 'nicename' ] = $label; 
  268. $field_type_settings[ $id ][ 'section' ] = 'saved'; 
  269.  
  270. $defaults = $field_type_settings[ $id ][ 'settingDefaults' ]; 
  271. $defaults = array_merge( $defaults, $settings ); 
  272. $defaults[ 'saved' ] = TRUE; 
  273.  
  274. $field_type_settings[ $id ][ 'settingDefaults' ] = $defaults; 
  275.  
  276. ?> 
  277. <script> 
  278. var fieldTypeData = <?php echo wp_json_encode( array_values( $field_type_settings ) ); ?>; 
  279. var fieldSettings = <?php echo wp_json_encode( array_values( $master_settings ) ); ?>; 
  280. var fieldTypeSections = <?php echo wp_json_encode( $field_type_sections ); ?>; 
  281. // console.log( fieldTypeData ); 
  282. </script> 
  283. <?php 
  284.  
  285. private function _localize_action_type_data() 
  286. $action_type_settings = array(); 
  287.  
  288. $master_settings_list = array(); 
  289.  
  290. foreach( Ninja_Forms()->actions as $action ) { 
  291.  
  292. $name = $action->get_name(); 
  293. $settings = $action->get_settings(); 
  294. $groups = Ninja_Forms::config( 'SettingsGroups' ); 
  295.  
  296. $settings_groups = $this->_group_settings( $settings, $groups ); 
  297.  
  298. $master_settings_list = array_merge( $master_settings_list, $settings ); 
  299.  
  300. $action_type_settings[ $name ] = array( 
  301. 'id' => $name,  
  302. 'section' => $action->get_section(),  
  303. 'nicename' => $action->get_nicename(),  
  304. 'image' => $action->get_image(),  
  305. 'settingGroups' => $settings_groups,  
  306. 'settingDefaults' => $this->_setting_defaults( $master_settings_list ) 
  307. ); 
  308.  
  309. $external_actions = $this->_fetch_action_feed(); 
  310. $u_id = get_option( 'nf_aff', false ); 
  311. if ( !$u_id ) $u_id = apply_filters( 'ninja_forms_affiliate_id', false ); 
  312. foreach( $external_actions as $action) { 
  313.  
  314. if( ! isset( $action[ 'name' ] ) || ! $action[ 'name' ] ) continue; 
  315.  
  316. $name = $action[ 'name' ]; 
  317. $nicename = ( isset( $action[ 'nicename' ] ) ) ? $action[ 'nicename' ] : ''; 
  318. $image = ( isset( $action[ 'image' ] ) ) ? $action[ 'image' ] : ''; 
  319. $link = ( isset( $action[ 'link' ] ) ) ? $action[ 'link' ] : ''; 
  320.  
  321. if ( $u_id ) { 
  322. $last_slash = strripos( $link, '/' ); 
  323. $link = substr( $link, 0, $last_slash ); 
  324. $link = urlencode( $link ); 
  325. $link = 'http://www.shareasale.com/r.cfm?u=' . $u_id . '&b=812237&m=63061&afftrack=&urllink=' . $link; 
  326.  
  327. if( isset( $action_type_settings[ $name ] ) ) continue; 
  328.  
  329. $action_type_settings[ $name ] = array( 
  330. 'id' => $name,  
  331. 'section' => 'available',  
  332. 'nicename' => $nicename,  
  333. 'image' => $image,  
  334. 'link' => $link,  
  335. 'settingGroups' => array(),  
  336. 'settingDefaults' => array() 
  337. ); 
  338.  
  339. $action_type_settings = apply_filters( 'ninja_forms_action_type_settings', $action_type_settings ); 
  340.  
  341. ?> 
  342. <script> 
  343. var actionTypeData = <?php echo wp_json_encode( array_values( $action_type_settings ) ); ?>; 
  344. var actionSettings = <?php echo wp_json_encode( array_values( $master_settings_list ) ); ?>; 
  345. // console.log( actionTypeData ); 
  346. </script> 
  347. <?php 
  348.  
  349. protected function _localize_form_settings() 
  350. $form_settings_types = Ninja_Forms::config( 'FormSettingsTypes' ); 
  351.  
  352. $form_settings[ 'display' ] = Ninja_Forms::config( 'FormDisplaySettings' ); 
  353. $form_settings[ 'restrictions' ] = Ninja_Forms::config( 'FormRestrictionSettings' ); 
  354. $form_settings[ 'calculations' ] = Ninja_Forms::config( 'FormCalculationSettings' ); 
  355. $form_settings = apply_filters( 'ninja_forms_localize_forms_settings', $form_settings ); 
  356.  
  357. foreach( $form_settings_types as $group_name => $form_setting_group ) { 
  358. if( ! isset( $form_settings[ $group_name ] ) ) $form_settings[ $group_name ] = array(); 
  359. $form_settings[ $group_name ] = apply_filters( 'ninja_forms_localize_form_' . $group_name . '_settings', $form_settings[ $group_name ] ); 
  360.  
  361. $groups = Ninja_Forms::config( 'SettingsGroups' ); 
  362.  
  363. $master_settings = array(); 
  364.  
  365. foreach( $form_settings_types as $id => $type ) { 
  366.  
  367. if( ! isset( $form_settings[ $id ] ) ) $form_settings[ $id ] = ''; 
  368.  
  369. $unique_settings = $this->_unique_settings( $form_settings[ $id ] ); 
  370. $master_settings = array_merge( $master_settings, $unique_settings ); 
  371.  
  372. $form_settings_types[ $id ]['settingGroups'] = $this->_group_settings($form_settings[ $id ], $groups); 
  373. $form_settings_types[ $id ]['settingDefaults'] = $this->_setting_defaults($unique_settings); 
  374. ?> 
  375. <script> 
  376. var formSettingTypeData = <?php echo wp_json_encode( array_values( $form_settings_types ) )?>; 
  377. var formSettings = <?php echo wp_json_encode( array_values( $master_settings ) )?>; 
  378. </script> 
  379. <?php 
  380.  
  381. protected function _localize_merge_tags() 
  382. $merge_tags = array( 
  383. 'fields' => array( 
  384. 'id' => 'fields',  
  385. 'label' => __( 'Fields', 'ninja-forms' ) 
  386. ); 
  387.  
  388. foreach( Ninja_Forms()->merge_tags as $key => $group ) { 
  389.  
  390. $merge_tags[ $key ] = array( 
  391. 'id' => $group->get_id(),  
  392. 'label' => $group->get_title(),  
  393. 'tags' => array_values( $group->get_merge_tags() ),  
  394. 'default_group' => $group->is_default_group() 
  395. ); 
  396. ?> 
  397. <script> 
  398. var mergeTags = <?php echo wp_json_encode( array_values( $merge_tags ) ); ?>; 
  399. </script> 
  400. <?php 
  401.  
  402.  
  403. protected function _group_settings( $settings, $groups ) 
  404. if( ! is_array( $settings ) ) return $groups; 
  405.  
  406. foreach( $settings as $setting ) { 
  407.  
  408. $group = ( isset( $setting[ 'group' ] ) ) ? $setting[ 'group' ] : ''; 
  409.  
  410. if( isset( $setting[ 'type'] ) && 'fieldset' == $setting[ 'type' ] ) { 
  411. $setting[ 'settings' ] = array_values( $setting[ 'settings' ] ); 
  412.  
  413. $groups[ $group ][ 'settings'][] = $setting; 
  414.  
  415. foreach( $groups as $id => $group ) { 
  416. if ( empty( $group[ 'settings' ] ) ) { 
  417. unset( $groups[ $id ] ); 
  418.  
  419. unset( $groups[ "" ] ); 
  420.  
  421. usort($groups, array( $this, 'setting_group_priority' ) ); 
  422.  
  423. return $groups; 
  424.  
  425. protected function _unique_settings( $settings ) 
  426. $unique_settings = array(); 
  427.  
  428. if( ! is_array( $settings ) ) return $unique_settings; 
  429.  
  430. foreach( $settings as $setting ) { 
  431.  
  432. if( 'fieldset' == $setting[ 'type' ] ) { 
  433.  
  434. $unique_settings = array_merge( $unique_settings, $this->_unique_settings( $setting[ 'settings' ] ) ); 
  435. } else { 
  436.  
  437. $name = $setting[ 'name' ]; 
  438. $unique_settings[ $name ] = $setting; 
  439.  
  440.  
  441. return $unique_settings; 
  442.  
  443. protected function _setting_defaults( $settings ) 
  444. $setting_defaults = array(); 
  445.  
  446. foreach( $settings as $setting ) { 
  447.  
  448. $name = ( isset( $setting[ 'name' ] ) ) ? $setting[ 'name' ] : ''; 
  449. $default = ( isset( $setting[ 'value' ] ) ) ? $setting[ 'value' ] : ''; 
  450. $setting_defaults[ $name ] = $default; 
  451.  
  452. return $setting_defaults; 
  453.  
  454. protected function _fetch_action_feed() 
  455. return Ninja_Forms::config( 'AvailableActions' ); 
  456.  
  457. protected function setting_group_priority( $a, $b ) 
  458. $priority[ 0 ] = ( isset( $a[ 'priority' ] ) ) ? $a[ 'priority' ] : 500; 
  459. $priority[ 1 ] = ( isset( $b[ 'priority' ] ) ) ? $b[ 'priority' ] : 500; 
  460.  
  461. return $priority[ 0 ] - $priority[ 1 ]; 
  462.  
  463. public function get_capability() 
  464. return apply_filters( 'ninja_forms_admin_parent_menu_capabilities', $this->capability ); 
  465.