説明なし

class-builder.php 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. <?php
  2. /**
  3. * Form builder that contains magic.
  4. *
  5. * @since 1.0.0
  6. * @since 1.6.8 Form Builder Refresh.
  7. * - Added `deregister_common_wp_admin_styles()` method.
  8. * - Changed logic of enqueuing styles.
  9. */
  10. class WPForms_Builder {
  11. /**
  12. * One is the loneliest number that you'll ever do.
  13. *
  14. * @since 1.4.4.1
  15. *
  16. * @var object
  17. */
  18. private static $instance;
  19. /**
  20. * Current view (panel).
  21. *
  22. * @since 1.0.0
  23. *
  24. * @var string
  25. */
  26. public $view;
  27. /**
  28. * Available panels.
  29. *
  30. * @since 1.0.0
  31. *
  32. * @var array
  33. */
  34. public $panels;
  35. /**
  36. * Current form.
  37. *
  38. * @since 1.0.0
  39. *
  40. * @var object
  41. */
  42. public $form;
  43. /**
  44. * Form data and settings.
  45. *
  46. * @since 1.4.4.1
  47. *
  48. * @var array
  49. */
  50. public $form_data;
  51. /**
  52. * Current template information.
  53. *
  54. * @since 1.0.0
  55. *
  56. * @var array
  57. */
  58. public $template;
  59. /**
  60. * Main Instance.
  61. *
  62. * @since 1.4.4.1
  63. *
  64. * @return WPForms_Builder
  65. */
  66. public static function instance() {
  67. if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WPForms_Builder ) ) {
  68. self::$instance = new WPForms_Builder();
  69. add_action( 'admin_init', [ self::$instance, 'init' ], 10 );
  70. add_action( 'admin_init', [ self::$instance, 'deregister_common_wp_admin_styles' ], PHP_INT_MAX );
  71. }
  72. return self::$instance;
  73. }
  74. /**
  75. * Determine if the user is viewing the builder, if so, party on.
  76. *
  77. * @since 1.0.0
  78. */
  79. public function init() {
  80. // Only load if we are actually on the builder.
  81. if ( ! wpforms_is_admin_page( 'builder' ) ) {
  82. return;
  83. }
  84. // Load form if found.
  85. $form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  86. if ( $form_id ) {
  87. // Default view for with an existing form is fields panel.
  88. $this->view = isset( $_GET['view'] ) ? sanitize_key( $_GET['view'] ) : 'fields'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  89. } else {
  90. // Default view for new form is the setup panel.
  91. $this->view = isset( $_GET['view'] ) ? sanitize_key( $_GET['view'] ) : 'setup'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  92. }
  93. if ( $this->view === 'setup' && ! wpforms_current_user_can( 'create_forms' ) ) {
  94. wp_die( esc_html__( 'Sorry, you are not allowed to create new forms.', 'wpforms-lite' ), 403 );
  95. }
  96. if ( $this->view === 'fields' && ! wpforms_current_user_can( 'edit_form_single', $form_id ) ) {
  97. wp_die( esc_html__( 'Sorry, you are not allowed to edit this form.', 'wpforms-lite' ), 403 );
  98. }
  99. // Fetch form.
  100. $this->form = wpforms()->form->get( $form_id );
  101. $this->form_data = $this->form ? wpforms_decode( $this->form->post_content ) : false;
  102. /**
  103. * Active form template data filter.
  104. *
  105. * Allows developers to modify fields structure and form settings in the template of the current form.
  106. *
  107. * @since 1.6.8
  108. *
  109. * @param array $template Template data.
  110. * @param array $form_id Form ID.
  111. */
  112. $this->template = apply_filters( 'wpforms_builder_template_active', [], $this->form );
  113. // Load builder panels.
  114. $this->load_panels();
  115. add_action( 'admin_head', [ $this, 'admin_head' ] );
  116. add_action( 'admin_enqueue_scripts', [ $this, 'enqueues' ], PHP_INT_MAX );
  117. add_action( 'admin_print_footer_scripts', [ $this, 'footer_scripts' ] );
  118. add_action( 'wpforms_admin_page', [ $this, 'output' ] );
  119. // Save the timestamp when the Builder has been opened for the first time.
  120. add_option( 'wpforms_builder_opened_date', time(), '', 'no' );
  121. /**
  122. * Form Builder init action.
  123. *
  124. * Executes after all the form builder UI output.
  125. * Intended to use in addons.
  126. *
  127. * @since 1.6.8
  128. *
  129. * @param string $view Current view.
  130. */
  131. do_action( 'wpforms_builder_init', $this->view );
  132. add_filter( 'teeny_mce_plugins', [ $this, 'tinymce_buttons' ] );
  133. }
  134. /**
  135. * Clear common wp-admin styles, keep only allowed.
  136. *
  137. * @since 1.6.8
  138. */
  139. public function deregister_common_wp_admin_styles() {
  140. if ( ! wpforms_is_admin_page( 'builder' ) ) {
  141. return;
  142. }
  143. /**
  144. * Filters the allowed common wp-admin styles.
  145. *
  146. * @since 1.6.8
  147. *
  148. * @param array $allowed_styles Styles allowed in the Form Builder.
  149. */
  150. $allowed_styles = (array) apply_filters(
  151. 'wpforms_admin_builder_allowed_common_wp_admin_styles',
  152. [
  153. 'wp-editor',
  154. 'wp-editor-font',
  155. 'editor-buttons',
  156. 'dashicons',
  157. 'media-views',
  158. 'imgareaselect',
  159. 'wp-mediaelement',
  160. 'mediaelement',
  161. 'media-views',
  162. 'buttons',
  163. 'admin-bar',
  164. ]
  165. );
  166. wp_styles()->registered = array_intersect_key( wp_styles()->registered, array_flip( $allowed_styles ) );
  167. }
  168. /**
  169. * Define TinyMCE buttons to use with our fancy editor instances.
  170. *
  171. * @since 1.0.3
  172. *
  173. * @param array $buttons List of default buttons.
  174. *
  175. * @return array
  176. */
  177. public function tinymce_buttons( $buttons ) {
  178. return [ 'colorpicker', 'lists', 'wordpress', 'wpeditimage', 'wplink' ];
  179. }
  180. /**
  181. * Load panels.
  182. *
  183. * @since 1.0.0
  184. */
  185. public function load_panels() {
  186. // Base class and functions.
  187. require_once WPFORMS_PLUGIN_DIR . 'includes/admin/builder/panels/class-base.php';
  188. /**
  189. * Form Builder panels slugs array filter.
  190. *
  191. * Allows developers to disable loading of some builder panels.
  192. *
  193. * @since 1.0.0
  194. *
  195. * @param array $panels Panels slugs array.
  196. */
  197. $this->panels = apply_filters(
  198. 'wpforms_builder_panels',
  199. [
  200. 'setup',
  201. 'fields',
  202. 'settings',
  203. 'providers',
  204. 'payments',
  205. ]
  206. );
  207. foreach ( $this->panels as $panel ) {
  208. $panel = sanitize_file_name( $panel );
  209. if ( file_exists( WPFORMS_PLUGIN_DIR . 'includes/admin/builder/panels/class-' . $panel . '.php' ) ) {
  210. require_once WPFORMS_PLUGIN_DIR . 'includes/admin/builder/panels/class-' . $panel . '.php';
  211. } elseif ( file_exists( WPFORMS_PLUGIN_DIR . 'pro/includes/admin/builder/panels/class-' . $panel . '.php' ) ) {
  212. require_once WPFORMS_PLUGIN_DIR . 'pro/includes/admin/builder/panels/class-' . $panel . '.php';
  213. }
  214. }
  215. }
  216. /**
  217. * Admin head area inside the form builder.
  218. *
  219. * @since 1.4.6
  220. */
  221. public function admin_head() {
  222. // Force hide admin side menu.
  223. echo '<style>#adminmenumain { display: none !important }</style>';
  224. do_action( 'wpforms_builder_admin_head', $this->view );
  225. }
  226. /**
  227. * Enqueue assets for the builder.
  228. *
  229. * @since 1.0.0
  230. * @since 1.6.8 All the panel's stylesheets restructured and moved here.
  231. */
  232. public function enqueues() {
  233. // Remove conflicting scripts.
  234. wp_deregister_script( 'serialize-object' );
  235. wp_deregister_script( 'wpclef-ajax-settings' );
  236. do_action( 'wpforms_builder_enqueues_before', $this->view );
  237. $min = wpforms_get_min_suffix();
  238. /*
  239. * Builder CSS.
  240. */
  241. $builder_styles = [
  242. 'overlay',
  243. 'basic',
  244. 'third-party',
  245. 'alerts',
  246. 'ui-general',
  247. 'panels',
  248. 'subsystems',
  249. 'fields',
  250. 'fields-types',
  251. ];
  252. foreach ( $builder_styles as $style ) {
  253. wp_enqueue_style(
  254. $style === 'basic' ? 'wpforms-builder' : 'wpforms-builder-' . $style,
  255. WPFORMS_PLUGIN_URL . "assets/css/builder/builder-{$style}{$min}.css",
  256. [],
  257. WPFORMS_VERSION
  258. );
  259. }
  260. /*
  261. * Third-party CSS.
  262. */
  263. wp_enqueue_style(
  264. 'wpforms-font-awesome',
  265. WPFORMS_PLUGIN_URL . 'assets/css/font-awesome.min.css',
  266. null,
  267. '4.7.0'
  268. );
  269. wp_enqueue_style(
  270. 'tooltipster',
  271. WPFORMS_PLUGIN_URL . 'assets/css/tooltipster.css',
  272. null,
  273. '4.2.6'
  274. );
  275. wp_enqueue_style(
  276. 'jquery-confirm',
  277. WPFORMS_PLUGIN_URL . 'assets/css/jquery-confirm.min.css',
  278. null,
  279. '3.3.2'
  280. );
  281. wp_enqueue_style(
  282. 'minicolors',
  283. WPFORMS_PLUGIN_URL . 'assets/css/jquery.minicolors.css',
  284. null,
  285. '2.2.6'
  286. );
  287. // Remove TinyMCE editor styles from third-party themes and plugins.
  288. remove_editor_styles();
  289. /*
  290. * JavaScript.
  291. */
  292. wp_enqueue_media();
  293. wp_enqueue_script( 'jquery-ui-sortable' );
  294. wp_enqueue_script( 'jquery-ui-draggable' );
  295. wp_enqueue_script( 'wp-util' );
  296. wp_enqueue_script(
  297. 'tooltipster',
  298. WPFORMS_PLUGIN_URL . 'assets/js/jquery.tooltipster.min.js',
  299. [ 'jquery' ],
  300. '4.2.6'
  301. );
  302. wp_enqueue_script(
  303. 'jquery-confirm',
  304. WPFORMS_PLUGIN_URL . 'assets/js/jquery.jquery-confirm.min.js',
  305. [ 'jquery' ],
  306. '3.3.2'
  307. );
  308. wp_enqueue_script(
  309. 'matchheight',
  310. WPFORMS_PLUGIN_URL . 'assets/js/jquery.matchHeight-min.js',
  311. [ 'jquery' ],
  312. '0.7.0'
  313. );
  314. wp_enqueue_script(
  315. 'insert-at-caret',
  316. WPFORMS_PLUGIN_URL . 'assets/js/jquery.insert-at-caret.min.js',
  317. [ 'jquery' ],
  318. '1.1.4'
  319. );
  320. wp_enqueue_script(
  321. 'minicolors',
  322. WPFORMS_PLUGIN_URL . 'assets/js/jquery.minicolors.min.js',
  323. [ 'jquery' ],
  324. '2.2.6'
  325. );
  326. wp_enqueue_script(
  327. 'conditionals',
  328. WPFORMS_PLUGIN_URL . 'assets/js/jquery.conditionals.min.js',
  329. [ 'jquery' ],
  330. '1.0.0'
  331. );
  332. wp_enqueue_script(
  333. 'choicesjs',
  334. WPFORMS_PLUGIN_URL . 'assets/js/choices.min.js',
  335. [],
  336. '9.0.1'
  337. );
  338. wp_enqueue_script(
  339. 'listjs',
  340. WPFORMS_PLUGIN_URL . 'assets/js/list.min.js',
  341. [ 'jquery' ],
  342. '2.3.0'
  343. );
  344. wp_enqueue_script(
  345. 'dom-purify',
  346. WPFORMS_PLUGIN_URL . 'assets/js/purify.min.js',
  347. [],
  348. '2.3.0'
  349. );
  350. if ( wp_is_mobile() ) {
  351. wp_enqueue_script( 'jquery-touch-punch' );
  352. }
  353. wp_enqueue_script(
  354. 'wpforms-utils',
  355. WPFORMS_PLUGIN_URL . "assets/js/admin-utils{$min}.js",
  356. [ 'jquery', 'dom-purify' ],
  357. WPFORMS_VERSION
  358. );
  359. wp_enqueue_script(
  360. 'wpforms-builder',
  361. WPFORMS_PLUGIN_URL . "assets/js/admin-builder{$min}.js",
  362. [ 'wpforms-utils', 'wpforms-admin-builder-templates', 'jquery-ui-sortable', 'jquery-ui-draggable', 'tooltipster', 'jquery-confirm' ],
  363. WPFORMS_VERSION
  364. );
  365. wp_enqueue_script(
  366. 'wpforms-admin-builder-templates',
  367. WPFORMS_PLUGIN_URL . "assets/js/components/admin/builder/templates{$min}.js",
  368. [ 'wp-util' ],
  369. WPFORMS_VERSION,
  370. true
  371. );
  372. wp_localize_script(
  373. 'wpforms-builder',
  374. 'wpforms_builder',
  375. $this->get_localized_strings()
  376. );
  377. wp_localize_script(
  378. 'wpforms-builder',
  379. 'wpforms_addons',
  380. $this->get_localized_addons()
  381. );
  382. /**
  383. * Form Builder enqueues action.
  384. *
  385. * Executes after all the form builder assets were enqueued.
  386. * Intended to use in addons.
  387. *
  388. * @since 1.0.0
  389. *
  390. * @param string $view Current view.
  391. */
  392. do_action( 'wpforms_builder_enqueues', $this->view );
  393. }
  394. /**
  395. * Get localized strings.
  396. *
  397. * @since 1.6.8
  398. *
  399. * @return array
  400. */
  401. private function get_localized_strings() {
  402. $strings = [
  403. 'and' => esc_html__( 'And', 'wpforms-lite' ),
  404. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  405. 'bulk_add_button' => esc_html__( 'Add New Choices', 'wpforms-lite' ),
  406. 'bulk_add_show' => esc_html__( 'Bulk Add', 'wpforms-lite' ),
  407. 'are_you_sure_to_close' => esc_html__( 'Are you sure you want to leave? You have unsaved changes', 'wpforms-lite' ),
  408. 'bulk_add_hide' => esc_html__( 'Hide Bulk Add', 'wpforms-lite' ),
  409. 'bulk_add_heading' => esc_html__( 'Add Choices (one per line)', 'wpforms-lite' ),
  410. 'bulk_add_placeholder' => esc_html__( "Blue\nRed\nGreen", 'wpforms-lite' ),
  411. 'bulk_add_presets_show' => esc_html__( 'Show presets', 'wpforms-lite' ),
  412. 'bulk_add_presets_hide' => esc_html__( 'Hide presets', 'wpforms-lite' ),
  413. 'date_select_day' => 'DD',
  414. 'date_select_month' => 'MM',
  415. 'debug' => wpforms_debug(),
  416. 'dynamic_choice_limit' => sprintf( /* translators: %1$s - data source name (e.g. Categories, Posts), %2$s - data source type (e.g. post type, taxonomy), %3$s - display limit, %4$s - total number of items. */
  417. esc_html__( 'The %1$s %2$s contains over %3$s items (%4$s). This may make the field difficult for your visitors to use and/or cause the form to be slow.', 'wpforms-lite' ),
  418. '{source}',
  419. '{type}',
  420. '{limit}',
  421. '{total}'
  422. ),
  423. 'cancel' => esc_html__( 'Cancel', 'wpforms-lite' ),
  424. 'ok' => esc_html__( 'OK', 'wpforms-lite' ),
  425. 'close' => esc_html__( 'Close', 'wpforms-lite' ),
  426. 'conditionals_change' => esc_html__( 'Due to form changes, conditional logic rules will be removed or updated:', 'wpforms-lite' ),
  427. 'conditionals_disable' => esc_html__( 'Are you sure you want to disable conditional logic? This will remove the rules for this field or setting.', 'wpforms-lite' ),
  428. 'field' => esc_html__( 'Field', 'wpforms-lite' ),
  429. 'field_locked' => esc_html__( 'Field Locked', 'wpforms-lite' ),
  430. 'field_locked_msg' => esc_html__( 'This field cannot be deleted or duplicated.', 'wpforms-lite' ),
  431. 'fields_available' => esc_html__( 'Available Fields', 'wpforms-lite' ),
  432. 'fields_unavailable' => esc_html__( 'No fields available', 'wpforms-lite' ),
  433. 'heads_up' => esc_html__( 'Heads up!', 'wpforms-lite' ),
  434. 'image_placeholder' => WPFORMS_PLUGIN_URL . 'assets/images/builder/placeholder-200x125.svg',
  435. 'nonce' => wp_create_nonce( 'wpforms-builder' ),
  436. 'admin_nonce' => wp_create_nonce( 'wpforms-admin' ),
  437. 'no_email_fields' => esc_html__( 'No email fields', 'wpforms-lite' ),
  438. 'notification_delete' => esc_html__( 'Are you sure you want to delete this notification?', 'wpforms-lite' ),
  439. 'notification_prompt' => esc_html__( 'Enter a notification name', 'wpforms-lite' ),
  440. 'notification_ph' => esc_html__( 'Eg: User Confirmation', 'wpforms-lite' ),
  441. 'notification_error' => esc_html__( 'You must provide a notification name', 'wpforms-lite' ),
  442. 'notification_def_name' => esc_html__( 'Default Notification', 'wpforms-lite' ),
  443. 'confirmation_delete' => esc_html__( 'Are you sure you want to delete this confirmation?', 'wpforms-lite' ),
  444. 'confirmation_prompt' => esc_html__( 'Enter a confirmation name', 'wpforms-lite' ),
  445. 'confirmation_ph' => esc_html__( 'Eg: Alternative Confirmation', 'wpforms-lite' ),
  446. 'confirmation_error' => esc_html__( 'You must provide a confirmation name', 'wpforms-lite' ),
  447. 'confirmation_def_name' => esc_html__( 'Default Confirmation', 'wpforms-lite' ),
  448. 'save' => esc_html__( 'Save', 'wpforms-lite' ),
  449. 'saving' => esc_html__( 'Saving', 'wpforms-lite' ),
  450. 'saved' => esc_html__( 'Saved!', 'wpforms-lite' ),
  451. 'save_exit' => esc_html__( 'Save and Exit', 'wpforms-lite' ),
  452. 'save_embed' => esc_html__( 'Save and Embed', 'wpforms-lite' ),
  453. 'saved_state' => '',
  454. 'layout_selector_show' => esc_html__( 'Show Layouts', 'wpforms-lite' ),
  455. 'layout_selector_hide' => esc_html__( 'Hide Layouts', 'wpforms-lite' ),
  456. 'layout_selector_layout' => esc_html__( 'Select your layout', 'wpforms-lite' ),
  457. 'layout_selector_column' => esc_html__( 'Select your column', 'wpforms-lite' ),
  458. 'loading' => esc_html__( 'Loading', 'wpforms-lite' ),
  459. 'template_name' => ! empty( $this->template['name'] ) ? $this->template['name'] : '',
  460. 'template_slug' => ! empty( $this->template['slug'] ) ? $this->template['slug'] : '',
  461. 'template_modal_title' => ! empty( $this->template['modal']['title'] ) ? $this->template['modal']['title'] : '',
  462. 'template_modal_msg' => ! empty( $this->template['modal']['message'] ) ? $this->template['modal']['message'] : '',
  463. 'template_modal_display' => ! empty( $this->template['modal_display'] ) ? $this->template['modal_display'] : '',
  464. 'template_select' => esc_html__( 'Use Template', 'wpforms-lite' ),
  465. 'template_confirm' => esc_html__( 'Changing templates on an existing form will DELETE existing form fields. Are you sure you want apply the new template?', 'wpforms-lite' ),
  466. 'embed' => esc_html__( 'Embed', 'wpforms-lite' ),
  467. 'exit' => esc_html__( 'Exit', 'wpforms-lite' ),
  468. 'exit_url' => wpforms_current_user_can( 'view_forms' ) ? admin_url( 'admin.php?page=wpforms-overview' ) : admin_url(),
  469. 'exit_confirm' => esc_html__( 'Your form contains unsaved changes. Would you like to save your changes first.', 'wpforms-lite' ),
  470. 'delete_confirm' => esc_html__( 'Are you sure you want to delete this field?', 'wpforms-lite' ),
  471. 'delete_choice_confirm' => esc_html__( 'Are you sure you want to delete this choice?', 'wpforms-lite' ),
  472. 'duplicate_confirm' => esc_html__( 'Are you sure you want to duplicate this field?', 'wpforms-lite' ),
  473. 'duplicate_copy' => esc_html__( '(copy)', 'wpforms-lite' ),
  474. 'error_title' => esc_html__( 'Please enter a form name.', 'wpforms-lite' ),
  475. 'error_choice' => esc_html__( 'This item must contain at least one choice.', 'wpforms-lite' ),
  476. 'off' => esc_html__( 'Off', 'wpforms-lite' ),
  477. 'on' => esc_html__( 'On', 'wpforms-lite' ),
  478. 'or' => esc_html__( 'or', 'wpforms-lite' ),
  479. 'other' => esc_html__( 'Other', 'wpforms-lite' ),
  480. 'operator_is' => esc_html__( 'is', 'wpforms-lite' ),
  481. 'operator_is_not' => esc_html__( 'is not', 'wpforms-lite' ),
  482. 'operator_empty' => esc_html__( 'empty', 'wpforms-lite' ),
  483. 'operator_not_empty' => esc_html__( 'not empty', 'wpforms-lite' ),
  484. 'operator_contains' => esc_html__( 'contains', 'wpforms-lite' ),
  485. 'operator_not_contains' => esc_html__( 'does not contain', 'wpforms-lite' ),
  486. 'operator_starts' => esc_html__( 'starts with', 'wpforms-lite' ),
  487. 'operator_ends' => esc_html__( 'ends with', 'wpforms-lite' ),
  488. 'operator_greater_than' => esc_html__( 'greater than', 'wpforms-lite' ),
  489. 'operator_less_than' => esc_html__( 'less than', 'wpforms-lite' ),
  490. 'payments_entries_off' => esc_html__( 'Entry storage is currently disabled, but is required to accept payments. Please enable in your form settings.', 'wpforms-lite' ),
  491. 'payments_on_entries_off' => esc_html__( 'This form is currently accepting payments. Entry storage is required to accept payments. To disable entry storage, please first disable payments.', 'wpforms-lite' ),
  492. 'previous' => esc_html__( 'Previous', 'wpforms-lite' ),
  493. 'provider_required_flds' => sprintf( /* translators: %s - marketing integration name. */
  494. esc_html__( "In order to complete your form's %s integration, please check that the dropdowns for all required (*) List Fields have been filled out.", 'wpforms-lite' ),
  495. '{provider}'
  496. ),
  497. 'rule_create' => esc_html__( 'Create new rule', 'wpforms-lite' ),
  498. 'rule_create_group' => esc_html__( 'Add New Group', 'wpforms-lite' ),
  499. 'rule_delete' => esc_html__( 'Delete rule', 'wpforms-lite' ),
  500. 'smart_tags' => apply_filters( 'wpforms_builder_enqueues_smart_tags', wpforms()->get( 'smart_tags' )->get_smart_tags() ),
  501. 'smart_tags_disabled_for_fields' => [ 'entry_id' ],
  502. 'smart_tags_show' => esc_html__( 'Show Smart Tags', 'wpforms-lite' ),
  503. 'smart_tags_hide' => esc_html__( 'Hide Smart Tags', 'wpforms-lite' ),
  504. 'select_field' => esc_html__( '--- Select Field ---', 'wpforms-lite' ),
  505. 'select_choice' => esc_html__( '--- Select Choice ---', 'wpforms-lite' ),
  506. 'upload_image_title' => esc_html__( 'Upload or Choose Your Image', 'wpforms-lite' ),
  507. 'upload_image_button' => esc_html__( 'Use Image', 'wpforms-lite' ),
  508. 'upload_image_remove' => esc_html__( 'Remove Image', 'wpforms-lite' ),
  509. 'provider_add_new_acc_btn' => esc_html__( 'Add', 'wpforms-lite' ),
  510. 'pro' => wpforms()->pro,
  511. 'is_gutenberg' => version_compare( get_bloginfo( 'version' ), '5.0', '>=' ) && ! is_plugin_active( 'classic-editor/classic-editor.php' ),
  512. 'cl_fields_supported' => wpforms_get_conditional_logic_form_fields_supported(),
  513. 'redirect_url_field_error' => esc_html__( 'You should enter a valid absolute address to the Confirmation Redirect URL field.', 'wpforms-lite' ),
  514. 'add_custom_value_label' => esc_html__( 'Add Custom Value', 'wpforms-lite' ),
  515. 'choice_empty_label_tpl' => sprintf( /* translators: %s - choice number. */
  516. esc_html__( 'Choice %s', 'wpforms-lite' ),
  517. '{number}'
  518. ),
  519. 'error_save_form' => esc_html__( 'Something went wrong while saving the form. Please reload the page and try again.', 'wpforms-lite' ),
  520. 'error_contact_support' => esc_html__( 'Please contact the plugin support team if this behavior persists.', 'wpforms-lite' ),
  521. 'ms_win_css_url' => WPFORMS_PLUGIN_URL . 'assets/css/builder/builder-ms-win.css',
  522. 'template_addon_prompt' => esc_html__( 'The %template% template requires the %addons%. Would you like to install and activate it?', 'wpforms-lite' ),
  523. 'template_addons_prompt' => esc_html__( 'The %template% template requires the %addons%. Would you like to install and activate all the required addons?', 'wpforms-lite' ),
  524. 'template_addons_error' => esc_html__( 'Could not install OR activate all the required addons. Please download from wpforms.com and install them manually. Would you like to use the template anyway?', 'wpforms-lite' ),
  525. 'use_template' => esc_html__( 'Yes, use template', 'wpforms-lite' ),
  526. 'error_select_template' => esc_html__( 'Something went wrong while applying the template.', 'wpforms-lite' ),
  527. 'blank_form' => esc_html__( 'Blank Form', 'wpforms-lite' ),
  528. 'something_went_wrong' => esc_html__( 'Something went wrong', 'wpforms-lite' ),
  529. 'field_cannot_be_reordered' => esc_html__( 'This field cannot be moved.', 'wpforms-lite' ),
  530. 'empty_label' => esc_html__( 'Empty Label', 'wpforms-lite' ),
  531. ];
  532. $strings['disable_entries'] = sprintf(
  533. wp_kses( /* translators: %s - Link to the WPForms.com doc article. */
  534. __( 'Disabling entry storage for this form will completely prevent any new submissions from getting saved to your site. If you still intend to keep a record of entries through notification emails, then please <a href="%s" target="_blank" rel="noopener noreferrer">test your form</a> to ensure emails send reliably.', 'wpforms-lite' ),
  535. [
  536. 'a' => [
  537. 'href' => [],
  538. 'rel' => [],
  539. 'target' => [],
  540. ],
  541. ]
  542. ),
  543. 'https://wpforms.com/docs/how-to-properly-test-your-wordpress-forms-before-launching-checklist/'
  544. );
  545. $strings = apply_filters( 'wpforms_builder_strings', $strings, $this->form );
  546. // phpcs:disable WordPress.Security.NonceVerification.Recommended
  547. if ( ! empty( $_GET['form_id'] ) ) {
  548. $form_id = (int) $_GET['form_id'];
  549. $strings['preview_url'] = esc_url( add_query_arg( 'new_window', 1, wpforms_get_form_preview_url( $form_id ) ) );
  550. $strings['entries_url'] = esc_url( admin_url( 'admin.php?page=wpforms-entries&view=list&form_id=' . $form_id ) );
  551. }
  552. // phpcs:enable
  553. return $strings;
  554. }
  555. /**
  556. * Get localized addons.
  557. *
  558. * @since 1.6.8
  559. *
  560. * @return array
  561. */
  562. private function get_localized_addons() {
  563. return wpforms_chain( wpforms()->get( 'addons' )->get_available() )
  564. ->map(
  565. function( $addon ) {
  566. return [
  567. 'title' => $addon['title'],
  568. 'action' => $addon['action'],
  569. 'url' => $addon['url'],
  570. ];
  571. }
  572. )
  573. ->value();
  574. }
  575. /**
  576. * Footer JavaScript.
  577. *
  578. * @since 1.3.7
  579. */
  580. public function footer_scripts() {
  581. $countries = wpforms_countries();
  582. $countries_postal = array_keys( $countries );
  583. $countries = array_values( $countries );
  584. sort( $countries_postal );
  585. sort( $countries );
  586. $choices = [
  587. 'countries' => [
  588. 'name' => esc_html__( 'Countries', 'wpforms-lite' ),
  589. 'choices' => $countries,
  590. ],
  591. 'countries_postal' => [
  592. 'name' => esc_html__( 'Countries Postal Code', 'wpforms-lite' ),
  593. 'choices' => $countries_postal,
  594. ],
  595. 'states' => [
  596. 'name' => esc_html__( 'States', 'wpforms-lite' ),
  597. 'choices' => array_values( wpforms_us_states() ),
  598. ],
  599. 'states_postal' => [
  600. 'name' => esc_html__( 'States Postal Code', 'wpforms-lite' ),
  601. 'choices' => array_keys( wpforms_us_states() ),
  602. ],
  603. 'months' => [
  604. 'name' => esc_html__( 'Months', 'wpforms-lite' ),
  605. 'choices' => array_values( wpforms_months() ),
  606. ],
  607. 'days' => [
  608. 'name' => esc_html__( 'Days', 'wpforms-lite' ),
  609. 'choices' => array_values( wpforms_days() ),
  610. ],
  611. ];
  612. /**
  613. * Choices presets array filter.
  614. *
  615. * Allows developers to edit the choices presets used in all choices-based fields.
  616. *
  617. * @since 1.3.7
  618. *
  619. * @param array $choices {
  620. * Choices presets is the [ `slug` => `preset`, ... ] array.
  621. *
  622. * @param array $preset {
  623. * Each preset data is the array with two elements:
  624. *
  625. * @param string $name Name of the preset
  626. * @param array $choices Choices array.
  627. * }
  628. * ...
  629. * }
  630. */
  631. $choices = apply_filters( 'wpforms_builder_preset_choices', $choices );
  632. echo '<script type="text/javascript">wpforms_preset_choices=' . wp_json_encode( $choices ) . '</script>';
  633. do_action( 'wpforms_builder_print_footer_scripts' );
  634. }
  635. /**
  636. * Load the appropriate files to build the page.
  637. *
  638. * @since 1.0.0
  639. */
  640. public function output() {
  641. if ( ! (bool) apply_filters( 'wpforms_builder_output', true ) ) {
  642. return;
  643. }
  644. $form_id = $this->form ? absint( $this->form->ID ) : '';
  645. $field_id = ! empty( $this->form_data['field_id'] ) ? $this->form_data['field_id'] : '';
  646. $preview_url = wpforms_get_form_preview_url( $form_id, true );
  647. $allowed_caps = [ 'edit_posts', 'edit_other_posts', 'edit_private_posts', 'edit_published_posts', 'edit_pages', 'edit_other_pages', 'edit_published_pages', 'edit_private_pages' ];
  648. ?>
  649. <div id="wpforms-builder" class="wpforms-admin-page">
  650. <div id="wpforms-builder-ie-notice" class="wpforms-fullscreen-notice">
  651. <img src="<?php echo esc_url( WPFORMS_PLUGIN_URL . 'assets/images/builder/ie-logo.svg' ); ?>" alt="Internet Explorer &reg;">
  652. <h3><?php esc_html_e( 'You are using an outdated browser!', 'wpforms-lite' ); ?></h3>
  653. <p>
  654. <?php
  655. printf(
  656. wp_kses( /* translators: %1$s - Link to the update Internet Explorer page, %2$s - link to the browse happy page. */
  657. __( 'The Internet Explorer browser no more supported.<br>Our form builder is optimized for modern browsers.<br>Please <a href="%1$s" target="_blank" rel="nofollow noopener">install Microsoft Edge</a> or learn<br>how to <a href="%2$s" target="_blank" rel="nofollow noopener">browse happy</a>.', 'wpforms-lite' ),
  658. [
  659. 'a' => [
  660. 'href' => [],
  661. 'target' => [],
  662. 'rel' => [],
  663. ],
  664. 'br' => [],
  665. ]
  666. ),
  667. 'https://www.microsoft.com/en-us/edge',
  668. 'https://browsehappy.com/'
  669. );
  670. ?>
  671. </p>
  672. <button type="button" class="wpforms-fullscreen-notice-go-back"><?php esc_html_e( 'Go back', 'wpforms-lite' ); ?></button>
  673. </div>
  674. <div id="wpforms-builder-mobile-notice" class="wpforms-fullscreen-notice">
  675. <img src="<?php echo esc_url( WPFORMS_PLUGIN_URL . 'assets/images/sullie-builder-mobile.png' ); ?>" alt="<?php esc_attr_e( 'Sullie the WPForms mascot', 'wpforms-lite' ); ?>">
  676. <h3><?php esc_html_e( 'Oh, hi there!', 'wpforms-lite' ); ?></h3>
  677. <p><?php esc_html_e( 'Our form builder is optimized for desktop computers and tablets. Please manage your forms on a different device, or if you\'re on a tablet, try rotating it.', 'wpforms-lite' ); ?></p>
  678. <button type="button" class="wpforms-fullscreen-notice-go-back"><?php esc_html_e( 'Go back', 'wpforms-lite' ); ?></button>
  679. </div>
  680. <div id="wpforms-builder-overlay">
  681. <div class="wpforms-builder-overlay-content">
  682. <i class="spinner"></i>
  683. <i class="avatar"></i>
  684. </div>
  685. </div>
  686. <form name="wpforms-builder" id="wpforms-builder-form" method="post" data-id="<?php echo esc_attr( $form_id ); ?>">
  687. <input type="hidden" name="id" value="<?php echo esc_attr( $form_id ); ?>">
  688. <input type="hidden" value="<?php echo absint( $field_id ); ?>" name="field_id" id="wpforms-field-id">
  689. <!-- Toolbar -->
  690. <div class="wpforms-toolbar">
  691. <div class="wpforms-left">
  692. <img src="<?php echo esc_url( WPFORMS_PLUGIN_URL . 'assets/images/sullie-alt.png' ); ?>" alt="<?php esc_attr_e( 'Sullie the WPForms mascot', 'wpforms-lite' ); ?>">
  693. </div>
  694. <div class="wpforms-center">
  695. <?php if ( $this->form ) : ?>
  696. <?php esc_html_e( 'Now editing', 'wpforms-lite' ); ?>
  697. <span class="wpforms-center-form-name wpforms-form-name"><?php echo esc_html( $this->form->post_title ); ?></span>
  698. <?php endif; ?>
  699. </div>
  700. <div class="wpforms-right">
  701. <button id="wpforms-help"
  702. class="wpforms-btn wpforms-btn-toolbar wpforms-btn-light-grey"
  703. title="<?php esc_attr_e( 'Help Ctrl+H', 'wpforms-lite' ); ?>">
  704. <i class="fa fa-question-circle-o"></i><span><?php esc_html_e( 'Help', 'wpforms-lite' ); ?></span>
  705. </button>
  706. <?php if ( $this->form ) : ?>
  707. <a href="<?php echo esc_url( $preview_url ); ?>"
  708. id="wpforms-preview-btn"
  709. class="wpforms-btn wpforms-btn-toolbar wpforms-btn-light-grey"
  710. title="<?php esc_attr_e( 'Preview Form Ctrl+P', 'wpforms-lite' ); ?>"
  711. target="_blank"
  712. rel="noopener noreferrer">
  713. <i class="fa fa-eye"></i><span class="text"><?php esc_html_e( 'Preview', 'wpforms-lite' ); ?></span>
  714. </a>
  715. <?php if ( array_filter( (array) $allowed_caps, 'current_user_can' ) ) : ?>
  716. <button id="wpforms-embed"
  717. class="wpforms-btn wpforms-btn-toolbar wpforms-btn-light-grey"
  718. title="<?php esc_attr_e( 'Embed Form Ctrl+B', 'wpforms-lite' ); ?>">
  719. <i class="fa fa-code"></i><span class="text"><?php esc_html_e( 'Embed', 'wpforms-lite' ); ?></span>
  720. </button>
  721. <?php endif; ?>
  722. <button id="wpforms-save"
  723. class="wpforms-btn wpforms-btn-toolbar wpforms-btn-orange"
  724. title="<?php esc_attr_e( 'Save Form Ctrl+S', 'wpforms-lite' ); ?>">
  725. <i class="fa fa-check"></i><i class="wpforms-loading-spinner wpforms-loading-white wpforms-loading-inline wpforms-hidden"></i><span class="text"><?php esc_html_e( 'Save', 'wpforms-lite' ); ?></span>
  726. </button>
  727. <?php endif; ?>
  728. <button id="wpforms-exit" title="<?php esc_attr_e( 'Exit Ctrl+Q', 'wpforms-lite' ); ?>">
  729. <i class="fa fa-times"></i>
  730. </button>
  731. </div>
  732. </div>
  733. <!-- Panel toggle buttons. -->
  734. <div class="wpforms-panels-toggle" id="wpforms-panels-toggle">
  735. <?php do_action( 'wpforms_builder_panel_buttons', $this->form, $this->view ); ?>
  736. </div>
  737. <div class="wpforms-panels">
  738. <?php do_action( 'wpforms_builder_panels', $this->form, $this->view ); ?>
  739. </div>
  740. </form>
  741. </div>
  742. <?php
  743. }
  744. }
  745. WPForms_Builder::instance();