Forms.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu
  3. {
  4. public $page_title = 'Ninja Forms';
  5. public $menu_slug = 'ninja-forms';
  6. public $icon_url = 'dashicons-feedback';
  7. public $position = '35.1337';
  8. // Stores whether or not this form has a password field.
  9. private $legacy_password = false;
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. if( ! defined( 'DOING_AJAX' ) ) {
  14. add_action('current_screen', array($this, 'admin_init'));
  15. add_action( 'current_screen', array( 'NF_Admin_AllFormsTable', 'process_bulk_action' ) );
  16. }
  17. add_action( 'admin_body_class', array( $this, 'body_class' ) );
  18. }
  19. public function body_class( $classes )
  20. {
  21. // Add class for the builder.
  22. if( isset( $_GET['page'] ) && isset( $_GET[ 'form_id' ] ) && $_GET['page'] == $this->menu_slug ) {
  23. $classes = "$classes ninja-forms-app";
  24. }
  25. return $classes;
  26. }
  27. public function get_page_title()
  28. {
  29. return __( 'Ninja Forms', 'ninja-forms' );
  30. }
  31. public function admin_init()
  32. {
  33. /*
  34. * If we aren't on the Ninja Forms menu page, don't admin_init.
  35. */
  36. if ( empty( $_GET[ 'page' ] ) || 'ninja-forms' !== $_GET[ 'page' ] ) {
  37. return false;
  38. }
  39. /*
  40. * Database Table Check
  41. * If the nf3_ database tables do not exist, then re-run activation.
  42. */
  43. if ( ! ninja_forms_three_table_exists() ) {
  44. Ninja_Forms()->activation();
  45. }
  46. if( isset( $_GET[ 'form_id' ] ) && ! is_numeric( $_GET[ 'form_id' ] ) && 'new' != $_GET[ 'form_id' ] ) {
  47. if( current_user_can( apply_filters( 'ninja_forms_admin_import_template_capabilities', 'manage_options' ) ) ) {
  48. $this->import_from_template();
  49. }
  50. }
  51. /* DISABLE OLD FORMS TABLE IN FAVOR OF NEW DASHBOARD */
  52. // $this->table = new NF_Admin_AllFormsTable();
  53. }
  54. public function display()
  55. {
  56. if( isset( $_GET[ 'form_id' ] ) ){
  57. if( 'new' == $_GET[ 'form_id' ] ) {
  58. $form_id = 'tmp-' . time();
  59. } else {
  60. $form_id = (is_numeric($_GET['form_id'])) ? absint($_GET['form_id']) : '';
  61. }
  62. /*
  63. * FORM BUILDER
  64. */
  65. Ninja_Forms::template( 'admin-menu-new-form.html.php' );
  66. $this->_enqueue_the_things( $form_id );
  67. delete_user_option( get_current_user_id(), 'nf_form_preview_' . $form_id );
  68. if( ! isset( $_GET[ 'ajax' ] ) ) {
  69. $this->_localize_form_data( $form_id );
  70. $this->_localize_field_type_data();
  71. $this->_localize_action_type_data();
  72. $this->_localize_form_settings();
  73. $this->_localize_merge_tags();
  74. }
  75. } else {
  76. /*
  77. * ALL FORMS TABLE
  78. * - DISABLE IN FAVOR OF NEW DASHBOARD.
  79. */
  80. // $this->table->prepare_items();
  81. //
  82. // Ninja_Forms::template( 'admin-menu-all-forms.html.php', array(
  83. // 'table' => $this->table,
  84. // 'add_new_url' => admin_url( 'admin.php?page=ninja-forms&form_id=new' ),
  85. // 'add_new_text' => __( 'Add New Form', 'ninja-forms' )
  86. // ) );
  87. $use_services = true; // Feature Flag.
  88. $use_services = apply_filters( 'ninja_forms_use_services', $use_services ); // The WordPress Way.
  89. if ( apply_filters( 'ninja_forms_disable_marketing', false ) ) $use_services = false;
  90. $use_services = $use_services && ( version_compare( PHP_VERSION, '5.6', '>=' ) ); // PHP Version Check.
  91. /*
  92. * DASHBOARD
  93. */
  94. $dash_items = Ninja_Forms()->config('DashboardMenuItems');
  95. ?>
  96. <script>
  97. var nfDashItems = <?php echo( json_encode( array_values( $dash_items ) ) ); ?>;
  98. var useServices = <?php echo ( $use_services ) ? 'true' : 'false'; ?>;
  99. var serviceSuccess = '<?php echo ( isset( $_GET[ 'success' ] ) ) ? $_GET[ 'success' ] : ''; ?>';
  100. </script>
  101. <?php
  102. wp_enqueue_script( 'backbone-radio', Ninja_Forms::$url . 'assets/js/lib/backbone.radio.min.js', array( 'jquery', 'backbone' ) );
  103. wp_enqueue_script( 'backbone-marionette-3', Ninja_Forms::$url . 'assets/js/lib/backbone.marionette3.min.js', array( 'jquery', 'backbone' ) );
  104. wp_enqueue_script( 'nf-jbox', Ninja_Forms::$url . 'assets/js/lib/jBox.min.js', array( 'jquery' ) );
  105. wp_enqueue_script( 'nf-ninjamodal', Ninja_Forms::$url . 'assets/js/lib/ninjaModal.js', array( 'jquery' ) );
  106. wp_enqueue_script( 'nf-moment', Ninja_Forms::$url . 'assets/js/lib/moment-with-locales.min.js', array( 'jquery', 'nf-dashboard' ) );
  107. wp_enqueue_script( 'nf-dashboard', Ninja_Forms::$url . 'assets/js/min/dashboard.min.js', array( 'backbone-radio', 'backbone-marionette-3' ) );
  108. $current_user = wp_get_current_user();
  109. wp_localize_script( 'nf-dashboard', 'nfi18n', Ninja_Forms::config( 'i18nDashboard' ) );
  110. wp_localize_script( 'nf-dashboard', 'nfPromotions', array_values( Ninja_Forms::config( 'DashboardPromotions' ) ) );
  111. wp_localize_script( 'nf-dashboard', 'nfAdmin', array(
  112. 'ajaxNonce' => wp_create_nonce( 'ninja_forms_dashboard_nonce' ),
  113. 'batchNonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
  114. 'formTelemetry' => ( get_option( 'nf_form_tel_sent' ) ) ? 0 : 1,
  115. 'showOptin' => ( get_option( 'ninja_forms_do_not_allow_tracking' ) ||
  116. get_option( 'ninja_forms_allow_tracking' ) ) ? 0 : 1,
  117. 'currentUserEmail' => $current_user->user_email,
  118. 'doingCleanup' => ( ! get_option( 'ninja_forms_data_is_clean' ) &&
  119. isset( $_REQUEST[ 'action' ] ) &&
  120. 'cleanup' == $_REQUEST[ 'action' ] ) ? 1 : 0,
  121. ) );
  122. wp_enqueue_style( 'nf-builder', Ninja_Forms::$url . 'assets/css/builder.css' );
  123. wp_enqueue_style( 'nf-dashboard', Ninja_Forms::$url . 'assets/css/dashboard.min.css' );
  124. wp_enqueue_style( 'nf-jbox', Ninja_Forms::$url . 'assets/css/jBox.css' );
  125. wp_enqueue_style( 'nf-font-awesome', Ninja_Forms::$url . 'assets/css/font-awesome.min.css' );
  126. Ninja_Forms::template( 'admin-menu-dashboard.html.php' );
  127. }
  128. }
  129. public function submenu_separators()
  130. {
  131. add_submenu_page( 'ninja-forms', '', '', 'read', '', '' );
  132. }
  133. private function import_from_template()
  134. {
  135. $template = sanitize_title( $_GET['form_id'] );
  136. $templates = Ninja_Forms::config( 'NewFormTemplates' );
  137. if( isset( $templates[ $template ] ) && ! empty( $templates[ $template ][ 'form' ] ) ) {
  138. $form = $templates[ $template ][ 'form' ];
  139. } else {
  140. $form = Ninja_Forms::template( $template . '.nff', array(), TRUE );
  141. }
  142. if( ! $form ) die( 'Template not found' );
  143. $form = json_decode( html_entity_decode( $form ), true );
  144. $form_id = Ninja_Forms()->form()->import_form( $form );
  145. if( ! $form_id ){
  146. $error_message = ( function_exists( 'json_last_error_msg' ) && json_last_error_msg() ) ? json_last_error_msg() : __( 'Form Template Import Error.', 'ninja-forms' );
  147. wp_die( $error_message );
  148. }
  149. header( "Location: " . admin_url( "admin.php?page=ninja-forms&form_id=$form_id" ) );
  150. exit();
  151. }
  152. private function _enqueue_the_things( $form_id )
  153. {
  154. global $wp_locale;
  155. wp_enqueue_media();
  156. wp_enqueue_style( 'nf-builder', Ninja_Forms::$url . 'assets/css/builder.css' );
  157. wp_enqueue_style( 'nf-font-awesome', Ninja_Forms::$url . 'assets/css/font-awesome.min.css' );
  158. /**
  159. * CSS Libraries
  160. */
  161. wp_enqueue_style( 'wp-color-picker' );
  162. wp_enqueue_style( 'jBox', Ninja_Forms::$url . 'assets/css/jBox.css' );
  163. wp_enqueue_style( 'summernote', Ninja_Forms::$url . 'assets/css/summernote.css' );
  164. wp_enqueue_style( 'codemirror', Ninja_Forms::$url . 'assets/css/codemirror.css' );
  165. wp_enqueue_style( 'codemirror-monokai', Ninja_Forms::$url . 'assets/css/monokai-theme.css' );
  166. wp_enqueue_style( 'pikaday-responsive', Ninja_Forms::$url . 'assets/css/pikaday-package.css' );
  167. /**
  168. * JS Libraries
  169. */
  170. wp_enqueue_script( 'wp-util' );
  171. wp_enqueue_script( 'jquery-autoNumeric', Ninja_Forms::$url . 'assets/js/lib/jquery.autoNumeric.min.js', array( 'jquery', 'backbone' ) );
  172. wp_enqueue_script( 'jquery-maskedinput', Ninja_Forms::$url . 'assets/js/lib/jquery.maskedinput.min.js', array( 'jquery', 'backbone' ) );
  173. wp_enqueue_script( 'backbone-marionette', Ninja_Forms::$url . 'assets/js/lib/backbone.marionette.min.js', array( 'jquery', 'backbone' ) );
  174. wp_enqueue_script( 'backbone-radio', Ninja_Forms::$url . 'assets/js/lib/backbone.radio.min.js', array( 'jquery', 'backbone' ) );
  175. wp_enqueue_script( 'jquery-perfect-scrollbar', Ninja_Forms::$url . 'assets/js/lib/perfect-scrollbar.jquery.min.js', array( 'jquery' ) );
  176. wp_enqueue_script( 'jquery-hotkeys-new', Ninja_Forms::$url . 'assets/js/lib/jquery.hotkeys.min.js' );
  177. wp_enqueue_script( 'jBox', Ninja_Forms::$url . 'assets/js/lib/jBox.min.js' );
  178. wp_enqueue_script( 'nf-ninjamodal', Ninja_Forms::$url . 'assets/js/lib/ninjaModal.js', array( 'jBox' ) );
  179. wp_enqueue_script( 'nf-jquery-caret', Ninja_Forms::$url . 'assets/js/lib/jquery.caret.min.js' );
  180. wp_enqueue_script( 'speakingurl', Ninja_Forms::$url . 'assets/js/lib/speakingurl.js' );
  181. wp_enqueue_script( 'jquery-slugify', Ninja_Forms::$url . 'assets/js/lib/slugify.min.js', array( 'jquery', 'speakingurl' ) );
  182. wp_enqueue_script( 'jquery-mobile-events', Ninja_Forms::$url . 'assets/js/lib/jquery.mobile-events.min.js', array( 'jquery' ) );
  183. wp_enqueue_script( 'jquery-ui-touch-punch', Ninja_Forms::$url . 'assets/js/lib/jquery.ui.touch-punch.min.js', array( 'jquery' ) );
  184. wp_enqueue_script( 'jquery-classy-wiggle', Ninja_Forms::$url . 'assets/js/lib/jquery.classywiggle.min.js', array( 'jquery' ) );
  185. wp_enqueue_script( 'moment-with-locale', Ninja_Forms::$url . 'assets/js/lib/moment-with-locales.min.js', array( 'jquery', 'nf-builder' ) );
  186. wp_enqueue_script( 'pikaday', Ninja_Forms::$url . 'assets/js/lib/pikaday.min.js', array( 'moment-with-locale' ) );
  187. wp_enqueue_script( 'pikaday-responsive', Ninja_Forms::$url . 'assets/js/lib/pikaday-responsive.min.js', array( 'pikaday', 'modernizr' ) );
  188. wp_enqueue_script( 'bootstrap', Ninja_Forms::$url . 'assets/js/lib/bootstrap.min.js', array( 'jquery' ) );
  189. wp_enqueue_script( 'codemirror', Ninja_Forms::$url . 'assets/js/lib/codemirror.min.js', array( 'jquery' ) );
  190. wp_enqueue_script( 'codemirror-xml', Ninja_Forms::$url . 'assets/js/lib/codemirror-xml.min.js', array( 'jquery' ) );
  191. wp_enqueue_script( 'codemirror-formatting', Ninja_Forms::$url . 'assets/js/lib/codemirror-formatting.min.js', array( 'jquery' ) );
  192. wp_enqueue_script( 'summernote', Ninja_Forms::$url . 'assets/js/lib/summernote.min.js', array( 'jquery', 'speakingurl' ) );
  193. 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' ) );
  194. wp_localize_script( 'nf-builder', 'nfi18n', Ninja_Forms::config( 'i18nBuilder' ) );
  195. $home_url = parse_url( home_url() );
  196. wp_localize_script( 'nf-builder', 'nfAdmin', array(
  197. 'ajaxNonce' => wp_create_nonce( 'ninja_forms_builder_nonce' ),
  198. 'batchNonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
  199. 'requireBaseUrl' => Ninja_Forms::$url . 'assets/js/',
  200. 'previewurl' => home_url() . '/?nf_preview_form=',
  201. 'wp_locale' => $wp_locale->number_format,
  202. 'editFormText' => __( 'Edit Form', 'ninja-forms' ),
  203. 'mobile' => ( wp_is_mobile() ) ? 1: 0,
  204. 'currencySymbols' => array_merge( array( '' => Ninja_Forms()->get_setting( 'currency_symbol' ) ), Ninja_Forms::config( 'CurrencySymbol' ) ),
  205. 'dateFormat' => Ninja_Forms()->get_setting( 'date_format' ),
  206. 'formID' => isset( $_GET[ 'form_id' ] ) ? absint( $_GET[ 'form_id' ] ) : 0,
  207. 'home_url_host' => $home_url[ 'host' ]
  208. ));
  209. do_action( 'nf_admin_enqueue_scripts' );
  210. }
  211. private function _localize_form_data( $form_id )
  212. {
  213. $form = Ninja_Forms()->form( $form_id )->get();
  214. if( ! $form->get_tmp_id() ) {
  215. if( $form_cache = WPN_Helper::get_nf_cache( $form_id ) ) {
  216. $fields = $form_cache[ 'fields' ];
  217. } else {
  218. $fields = ($form_id) ? Ninja_Forms()->form($form_id)->get_fields() : array();
  219. }
  220. $actions = ($form_id) ? Ninja_Forms()->form($form_id)->get_actions() : array();
  221. } else {
  222. $fields = array();
  223. $actions = array();
  224. }
  225. $fields_settings = array();
  226. if( ! empty( $fields ) ) {
  227. // TODO: Replace unique field key checks with a refactored model/factory.
  228. // $unique_field_keys = array();
  229. // $form_cache = get_option( 'nf_form_' . $form_id, false );
  230. // $cache_updated = false;
  231. foreach ($fields as $field) {
  232. $field_id = ( is_object( $field ) ) ? $field->get_id() : $field[ 'id' ];
  233. /*
  234. * Duplicate field check.
  235. * TODO: Replace unique field key checks with a refactored model/factory.
  236. */
  237. // $field_key = $field->get_setting( 'key' );
  238. // if( in_array( $field_key, $unique_field_keys ) || '' == $field_key ){
  239. //
  240. // // Delete the field.
  241. // Ninja_Forms()->request( 'delete-field' )->data( array( 'field_id' => $field_id ) )->dispatch();
  242. //
  243. // // Remove the field from cache.
  244. // if( $form_cache ) {
  245. // if( isset( $form_cache[ 'fields' ] ) ){
  246. // foreach( $form_cache[ 'fields' ] as $cached_field_key => $cached_field ){
  247. // if( ! isset( $cached_field[ 'id' ] ) ) continue;
  248. // if( $field_id != $cached_field[ 'id' ] ) continue;
  249. //
  250. // // Flag cache to update.
  251. // $cache_updated = true;
  252. //
  253. // unset( $form_cache[ 'fields' ][ $cached_field_key ] ); // Remove the field.
  254. // }
  255. // }
  256. // }
  257. //
  258. // continue; // Skip the duplicate field.
  259. // }
  260. // array_push( $unique_field_keys, $field_key ); // Log unique key.
  261. /* END Duplicate field check. */
  262. $type = ( is_object( $field ) ) ? $field->get_setting( 'type' ) : $field[ 'settings' ][ 'type' ];
  263. /*
  264. * As of version 3.3.16, we want password fields to only show up if the user is using an add-on that requires them.
  265. * But, because we don't want to break any forms that may already have a password field, we enable them if the current form already has them.
  266. * The $legacy_password class var holds whether or not this form has a pre-existing password or confirm password field.
  267. */
  268. if ( 'password' == $type || 'passwordconfirm' == $type ) {
  269. $this->legacy_password = true;
  270. }
  271. if( ! isset( Ninja_Forms()->fields[ $type ] ) ){
  272. $field = NF_Fields_Unknown::create( $field );
  273. }
  274. $settings = ( is_object( $field ) ) ? $field->get_settings() : $field[ 'settings' ];
  275. $settings[ 'id' ] = $field_id;
  276. $settings = $this->null_data_check( $settings );
  277. $fields_settings[] = $settings;
  278. }
  279. // if( $cache_updated ) {
  280. // update_option('nf_form_' . $form_id, $form_cache); // Update form cache without duplicate fields.
  281. // }
  282. }
  283. $actions_settings = array();
  284. if( ! empty( $actions ) ) {
  285. foreach ($actions as $action) {
  286. $type = $action->get_setting( 'type' );
  287. if( ! isset( Ninja_Forms()->actions[ $type ] ) ) continue;
  288. $settings = $action->get_settings();
  289. $settings['id'] = $action->get_id();
  290. $settings = $this->null_data_check( $settings );
  291. $actions_settings[] = $settings;
  292. }
  293. }
  294. if( $form->get_tmp_id() ){
  295. $actions_settings = Ninja_Forms()->config( 'FormActionDefaults' );
  296. }
  297. $form_data = array();
  298. $form_data['id'] = $form_id;
  299. // Use form cache for form settings.
  300. // TODO: Defer to refactor of factory/model.
  301. if( isset( $form_cache[ 'settings' ] ) ) {
  302. $form_data['settings'] = $form_cache[ 'settings' ];
  303. } else {
  304. $form_data['settings'] = $form->get_settings();
  305. }
  306. $form_data['fields'] = $fields_settings;
  307. $form_data['actions'] = $actions_settings;
  308. ?>
  309. <script>
  310. var preloadedFormData = <?php echo wp_json_encode( $form_data ); ?>;
  311. // console.log( preloadedFormData );
  312. </script>
  313. <?php
  314. }
  315. /**
  316. * Null Data Check
  317. * Accepts array of settings and removes null data the array.
  318. *
  319. * @param $settings - a key/value pair of settings.
  320. * @return array
  321. */
  322. private function null_data_check( $settings )
  323. {
  324. // Loop over the settings we receive.
  325. foreach ($settings as $key => $setting) {
  326. // Check for null values in the settings array.
  327. if ( null === $setting ) {
  328. // Remove null settings from the array.
  329. unset( $settings[ $key ] );
  330. continue;
  331. }
  332. }
  333. return $settings;
  334. }
  335. private function _localize_field_type_data()
  336. {
  337. $field_type_sections = array_values( Ninja_Forms()->config( 'FieldTypeSections' ) );
  338. $field_type_settings = array();
  339. $master_settings = array();
  340. $setting_defaults = array();
  341. foreach( Ninja_Forms()->fields as $field ){
  342. if ( 'password' == $field->get_type() || 'passwordconfirm' == $field->get_type() ) {
  343. if( ! $this->legacy_password && ! apply_filters( 'ninja_forms_enable_password_fields', false ) ){
  344. continue;
  345. }
  346. }
  347. $name = $field->get_name();
  348. $settings = $field->get_settings();
  349. $groups = Ninja_Forms::config( 'SettingsGroups' );
  350. $unique_settings = $this->_unique_settings( $settings );
  351. $master_settings = array_merge( $master_settings, $unique_settings );
  352. $settings_groups = $this->_group_settings( $settings, $groups );
  353. $settings_defaults = $this->_setting_defaults( $unique_settings );
  354. $field_type_settings[ $name ] = array(
  355. 'id' => $name,
  356. 'nicename' => $field->get_nicename(),
  357. 'alias' => $field->get_aliases(),
  358. 'parentType' => $field->get_parent_type(),
  359. 'section' => $field->get_section(),
  360. 'icon' => $field->get_icon(),
  361. 'type' => $field->get_type(),
  362. 'settingGroups' => $settings_groups,
  363. 'settingDefaults' => $settings_defaults
  364. );
  365. }
  366. $saved_fields = Ninja_Forms()->form()->get_fields( array( 'saved' => 1) );
  367. foreach( $saved_fields as $saved_field ){
  368. $settings = $saved_field->get_settings();
  369. $id = $saved_field->get_id();
  370. $type = $settings[ 'type' ];
  371. $label = $settings[ 'label' ];
  372. $field_type_settings[ $id ] = $field_type_settings[ $type ];
  373. $field_type_settings[ $id ][ 'id' ] = $id;
  374. $field_type_settings[ $id ][ 'type' ] = $type;
  375. $field_type_settings[ $id ][ 'nicename' ] = $label;
  376. $field_type_settings[ $id ][ 'section' ] = 'saved';
  377. $defaults = $field_type_settings[ $id ][ 'settingDefaults' ];
  378. $defaults = array_merge( $defaults, $settings );
  379. $defaults[ 'saved' ] = TRUE;
  380. $field_type_settings[ $id ][ 'settingDefaults' ] = $defaults;
  381. }
  382. ?>
  383. <script>
  384. var fieldTypeData = <?php echo wp_json_encode( array_values( $field_type_settings ) ); ?>;
  385. var fieldSettings = <?php echo wp_json_encode( array_values( $master_settings ) ); ?>;
  386. var fieldTypeSections = <?php echo wp_json_encode( $field_type_sections ); ?>;
  387. // console.log( fieldTypeData );
  388. </script>
  389. <?php
  390. }
  391. private function _localize_action_type_data()
  392. {
  393. $action_type_settings = array();
  394. $master_settings_list = array();
  395. foreach( Ninja_Forms()->actions as $action ){
  396. $name = $action->get_name();
  397. $settings = $action->get_settings();
  398. $groups = Ninja_Forms::config( 'SettingsGroups' );
  399. $settings_groups = $this->_group_settings( $settings, $groups );
  400. $master_settings_list = array_merge( $master_settings_list, $settings );
  401. $action_type_settings[ $name ] = array(
  402. 'id' => $name,
  403. 'section' => $action->get_section(),
  404. 'nicename' => $action->get_nicename(),
  405. 'image' => $action->get_image(),
  406. 'settingGroups' => $settings_groups,
  407. 'settingDefaults' => $this->_setting_defaults( $master_settings_list )
  408. );
  409. }
  410. $external_actions = $this->_fetch_action_feed();
  411. $u_id = get_option( 'nf_aff', false );
  412. if ( !$u_id ) $u_id = apply_filters( 'ninja_forms_affiliate_id', false );
  413. foreach( $external_actions as $action){
  414. if( ! isset( $action[ 'name' ] ) || ! $action[ 'name' ] ) continue;
  415. $name = $action[ 'name' ];
  416. $nicename = ( isset( $action[ 'nicename' ] ) ) ? $action[ 'nicename' ] : '';
  417. $image = ( isset( $action[ 'image' ] ) ) ? $action[ 'image' ] : '';
  418. $link = ( isset( $action[ 'link' ] ) ) ? $action[ 'link' ] : '';
  419. $modal_content = ( isset( $action[ 'modal_content' ] ) ) ? $action[ 'modal_content' ] : '';
  420. if ( $u_id ) {
  421. $last_slash = strripos( $link, '/' );
  422. $link = substr( $link, 0, $last_slash );
  423. $link = urlencode( $link );
  424. $link = 'http://www.shareasale.com/r.cfm?u=' . $u_id . '&b=812237&m=63061&afftrack=&urllink=' . $link;
  425. }
  426. if( isset( $action_type_settings[ $name ] ) ) continue;
  427. $action_type_settings[ $name ] = array(
  428. 'id' => $name,
  429. 'section' => 'available',
  430. 'nicename' => $nicename,
  431. 'image' => $image,
  432. 'link' => $link,
  433. 'modal_content' => $modal_content,
  434. 'settingGroups' => array(),
  435. 'settingDefaults' => array()
  436. );
  437. }
  438. $action_type_settings = apply_filters( 'ninja_forms_action_type_settings', $action_type_settings );
  439. ?>
  440. <script>
  441. var actionTypeData = <?php echo wp_json_encode( array_values( $action_type_settings ) ); ?>;
  442. var actionSettings = <?php echo wp_json_encode( array_values( $master_settings_list ) ); ?>;
  443. // console.log( actionTypeData );
  444. </script>
  445. <?php
  446. }
  447. protected function _localize_form_settings()
  448. {
  449. $form_settings_types = Ninja_Forms::config( 'FormSettingsTypes' );
  450. $form_settings[ 'display' ] = Ninja_Forms::config( 'FormDisplaySettings' );
  451. $form_settings[ 'restrictions' ] = Ninja_Forms::config( 'FormRestrictionSettings' );
  452. $form_settings[ 'calculations' ] = Ninja_Forms::config( 'FormCalculationSettings' );
  453. $form_settings = apply_filters( 'ninja_forms_localize_forms_settings', $form_settings );
  454. foreach( $form_settings_types as $group_name => $form_setting_group ){
  455. if( ! isset( $form_settings[ $group_name ] ) ) $form_settings[ $group_name ] = array();
  456. $form_settings[ $group_name ] = apply_filters( 'ninja_forms_localize_form_' . $group_name . '_settings', $form_settings[ $group_name ] );
  457. }
  458. $groups = Ninja_Forms::config( 'SettingsGroups' );
  459. $master_settings = array();
  460. foreach( $form_settings_types as $id => $type ) {
  461. if( ! isset( $form_settings[ $id ] ) ) $form_settings[ $id ] = '';
  462. $unique_settings = $this->_unique_settings( $form_settings[ $id ] );
  463. $master_settings = array_merge( $master_settings, $unique_settings );
  464. $form_settings_types[ $id ]['settingGroups'] = $this->_group_settings($form_settings[ $id ], $groups);
  465. $form_settings_types[ $id ]['settingDefaults'] = $this->_setting_defaults($unique_settings);
  466. }
  467. ?>
  468. <script>
  469. var formSettingTypeData = <?php echo wp_json_encode( array_values( $form_settings_types ) )?>;
  470. var formSettings = <?php echo wp_json_encode( array_values( $master_settings ) )?>;
  471. </script>
  472. <?php
  473. }
  474. protected function _localize_merge_tags()
  475. {
  476. $merge_tags = array(
  477. 'fields' => array(
  478. 'id' => 'fields',
  479. 'label' => __( 'Fields', 'ninja-forms' )
  480. )
  481. );
  482. foreach( Ninja_Forms()->merge_tags as $key => $group ){
  483. /*
  484. * If the merge tag group doesn't have a title, don't localise it.
  485. *
  486. * This convention is used to allow merge tags to continue to function,
  487. * even though they can't be added to new forms.
  488. */
  489. $title = $group->get_title();
  490. if ( empty( $title ) ) continue;
  491. $merge_tags[ $key ] = array(
  492. 'id' => $group->get_id(),
  493. 'label' => $group->get_title(),
  494. 'tags' => array_values( $group->get_merge_tags() ),
  495. 'default_group' => $group->is_default_group()
  496. );
  497. }
  498. ?>
  499. <script>
  500. var mergeTags = <?php echo wp_json_encode( array_values( $merge_tags ) ); ?>;
  501. </script>
  502. <?php
  503. }
  504. protected function _group_settings( $settings, $groups )
  505. {
  506. if( ! is_array( $settings ) ) return $groups;
  507. foreach( $settings as $setting ){
  508. $group = ( isset( $setting[ 'group' ] ) ) ? $setting[ 'group' ] : '';
  509. if( isset( $setting[ 'type'] ) && 'fieldset' == $setting[ 'type' ] ){
  510. $setting[ 'settings' ] = array_values( $setting[ 'settings' ] );
  511. }
  512. $groups[ $group ][ 'settings'][] = $setting;
  513. }
  514. foreach( $groups as $id => $group ) {
  515. if ( empty( $group[ 'settings' ] ) ) {
  516. unset( $groups[ $id ] );
  517. }
  518. }
  519. unset( $groups[ "" ] );
  520. usort($groups, array( $this, 'setting_group_priority' ) );
  521. return $groups;
  522. }
  523. protected function _unique_settings( $settings )
  524. {
  525. $unique_settings = array();
  526. if( ! is_array( $settings ) ) return $unique_settings;
  527. foreach( $settings as $setting ){
  528. if( 'fieldset' == $setting[ 'type' ] ){
  529. $unique_settings = array_merge( $unique_settings, $this->_unique_settings( $setting[ 'settings' ] ) );
  530. } else {
  531. $name = $setting[ 'name' ];
  532. $unique_settings[ $name ] = $setting;
  533. }
  534. }
  535. return $unique_settings;
  536. }
  537. protected function _setting_defaults( $settings )
  538. {
  539. $setting_defaults = array();
  540. foreach( $settings as $setting ){
  541. $name = ( isset( $setting[ 'name' ] ) ) ? $setting[ 'name' ] : '';
  542. $default = ( isset( $setting[ 'value' ] ) ) ? $setting[ 'value' ] : null;
  543. $setting_defaults[ $name ] = $default;
  544. }
  545. return $setting_defaults;
  546. }
  547. protected function _fetch_action_feed()
  548. {
  549. return Ninja_Forms::config( 'AvailableActions' );
  550. }
  551. protected function setting_group_priority( $a, $b )
  552. {
  553. $priority[ 0 ] = ( isset( $a[ 'priority' ] ) ) ? $a[ 'priority' ] : 500;
  554. $priority[ 1 ] = ( isset( $b[ 'priority' ] ) ) ? $b[ 'priority' ] : 500;
  555. return $priority[ 0 ] - $priority[ 1 ];
  556. }
  557. public function get_capability()
  558. {
  559. return apply_filters( 'ninja_forms_admin_parent_menu_capabilities', $this->capability );
  560. }
  561. }