Caldera_Forms_Admin

Caldera_Forms Plugin class.

Defined (1)

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

/classes/admin.php  
  1. class Caldera_Forms_Admin { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. const VERSION = CFCORE_VER; 
  7.  
  8. /** 
  9. * @var string 
  10. */ 
  11. protected $plugin_slug = 'caldera-forms'; 
  12.  
  13.  
  14. /** 
  15. * @var string 
  16. */ 
  17. protected $screen_prefix = array(); 
  18.  
  19. /** 
  20. * @var string 
  21. */ 
  22. protected $sub_prefix = null; 
  23.  
  24. /** 
  25. * @var string 
  26. */ 
  27. protected $addons = array(); 
  28.  
  29. /** 
  30. * @var object 
  31. */ 
  32. protected static $instance = null; 
  33.  
  34. /** 
  35. * Holds admin notices 
  36. * @since 1.3.0 
  37. * @var array 
  38. */ 
  39. private static $admin_notices; 
  40.  
  41. /** 
  42. * Initialize the plugin by setting localization, filters, and administration functions. 
  43. */ 
  44. private function __construct() { 
  45.  
  46. add_filter( 'all_plugins', array( $this, 'prepare_filter_addons' ) ); 
  47.  
  48. // Load plugin text domain 
  49. add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); 
  50.  
  51. // Add Admin menu page 
  52. add_action( 'admin_menu', array( $this, 'register_admin_page' ), 9 ); 
  53.  
  54. // Add admin scritps and styles 
  55. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_stylescripts' ), 1 ); 
  56.  
  57. // add element & fields filters 
  58. add_filter('caldera_forms_get_panel_extensions', array( $this, 'get_panel_extensions'), 1); 
  59. add_filter('caldera_forms_entry_viewer_buttons', array( $this, 'set_viewer_buttons'), 10, 4); 
  60. add_filter('caldera_forms_entry_editor_buttons', array( $this, 'set_editor_buttons'), 10, 4); 
  61.  
  62. // action 
  63.  
  64. add_action('caldera_forms_admin_templates', array( $this, 'get_admin_templates'), 1); 
  65. add_action('caldera_forms_entry_meta_templates', array( $this, 'get_admin_meta_templates'), 1); 
  66.  
  67. add_action( 'init', array( $this, 'save_form') ); 
  68. add_action( 'media_buttons', array($this, 'shortcode_insert_button' ), 11 ); 
  69. add_filter( 'wp_fullscreen_buttons', array($this, 'shortcode_insert_button_fs' ), 11 ); 
  70.  
  71. // filter for adding presets 
  72. add_filter( 'caldera_forms_field_option_presets', array($this, 'load_option_presets' ) ); 
  73.  
  74. if( current_user_can( Caldera_Forms::get_manage_cap( 'create' ) ) ) { 
  75. // create forms 
  76. add_action("wp_ajax_create_form", array( $this, 'create_form') ); 
  77.  
  78. if( current_user_can( Caldera_Forms::get_manage_cap( 'admin' ) ) ) { 
  79. add_action( "wp_ajax_toggle_form_state", array( $this, 'toggle_form_state' ) ); 
  80. add_action( "wp_ajax_save_cf_setting", array( $this, 'save_cf_setting' ) ); 
  81. add_action( "wp_ajax_cf_dismiss_pointer", array( $this, 'update_pointer' ) ); 
  82. add_action( "wp_ajax_cf_bulk_action", array( $this, 'bulk_action' ) ); 
  83. add_action("wp_ajax_cf_get_form_preview", array( $this, 'get_form_preview') ); 
  84.  
  85. add_action( 'caldera_forms_admin_footer', array( $this, 'admin_alerts' ) ); 
  86. add_action( 'admin_footer', array( $this, 'add_shortcode_inserter')); 
  87.  
  88. $this->addons = apply_filters( 'caldera_forms_get_active_addons', array() ); 
  89.  
  90. add_action('admin_footer-edit.php', array( $this, 'render_editor_template')); // Fired on the page with the posts table 
  91. add_action('admin_footer-post.php', array( $this, 'render_editor_template')); // Fired on post edit page 
  92. add_action('admin_footer-post-new.php', array( $this, 'render_editor_template')); // Fired on add new post page 
  93.  
  94. add_action( 'caldera_forms_new_form_template_end', array( $this, 'load_new_form_templates') ); 
  95.  
  96. add_action( 'admin_init', array( $this, 'watch_tracking' ) ); 
  97.  
  98. add_action( 'caldera_forms_prerender_edit', array( __CLASS__, 'easy_pods_auto_populate' ) ); 
  99.  
  100. /** 
  101. * Runs after Caldera Forms admin is initialized 
  102. * @since 1.3.5.3 
  103. */ 
  104. do_action( 'caldera_forms_admin_init' ); 
  105.  
  106. /** Adding anything to this constructor after caldera_forms_admin_init action is a violation of intergalactic law */ 
  107.  
  108. public function render_editor_template() { 
  109. ?> 
  110. <script type="text/html" id="tmpl-editor-caldera-forms"> 
  111. <# if ( data.html ) { #> 
  112. {{{ data.html }}} 
  113. <# } else { #> 
  114. <div class="wpview-error"> 
  115. <div class="dashicons dashicons-cf-logo"></div><p style="font-size: 13px;"><?php _e( 'Invalid Form.', 'caldera-forms' ); ?></p> 
  116. </div> 
  117. <# } #> 
  118. </script> 
  119. <?php 
  120.  
  121.  
  122. /** 
  123. * Returns the array of option presets for option based fields. 
  124. * @since 1.4.0 
  125. * @param array $presets current array of presets 
  126. * @return array array of presets 
  127. */ 
  128. public function load_option_presets( $presets ) { 
  129.  
  130. $internal = array_merge( $presets, array( 
  131. 'countries_iso_alpha_2' => array( 
  132. 'name' => __( 'Countries (ISO Alpha-2)', 'caldera-forms'),  
  133. 'data' => file_get_contents( CFCORE_PATH . 'includes/presets/countries_iso_alpha_2.txt' ),  
  134. ),  
  135. 'countries_names' => array( 
  136. 'name' => __( 'Countries (Names Only)', 'caldera-forms'),  
  137. 'data' => file_get_contents( CFCORE_PATH . 'includes/presets/countries_names.txt' ),  
  138. ),  
  139. 'continents' => array( 
  140. 'name' => __( 'Continents', 'caldera-forms'),  
  141. 'data' => array( 
  142. "Africa",  
  143. "Antarctica",  
  144. "Asia",  
  145. "Australia",  
  146. "Europe",  
  147. "North America",  
  148. "South America",  
  149. ),  
  150. ),  
  151. 'canadian_provinces_territories' => array( 
  152. 'name' => __( 'Canadian Provinces & Territories', 'caldera-forms'),  
  153. 'data' => array( 
  154. "Alberta",  
  155. "British Columbia",  
  156. "Manitoba",  
  157. "New Brunswick",  
  158. "Newfoundland and Labrador",  
  159. "Northwest Territories",  
  160. "Nova Scotia",  
  161. "Nunavut",  
  162. "Ontario",  
  163. "Prince Edward Island",  
  164. "Quebec",  
  165. "Saskatchewan",  
  166. "Yukon",  
  167. ),  
  168. ),  
  169. )); 
  170.  
  171. return $internal; 
  172.  
  173. /** 
  174. * Returns the array of form templates. 
  175. * @since 1.2.3 
  176. * @return array The form templates 
  177. */ 
  178. public static function internal_form_templates() { 
  179.  
  180. $internal_templates = array( 
  181. 'starter_contact_form' => array( 
  182. 'name' => esc_html__( 'Contact Form', 'caldera-forms' ),  
  183. 'template' => include CFCORE_PATH . 'includes/templates/starter-contact-form.php' 
  184. ),  
  185. 'variable_price_example' => array( 
  186. 'name' => esc_html__( 'Variable Pricing Form - with add-on products', 'caldera-forms' ),  
  187. 'template' => include CFCORE_PATH . 'includes/templates/variable-price-example.php' 
  188. ),  
  189. 'registration' => array( 
  190. 'name' => esc_html__( 'Registration Form - with optional additional participants', 'caldera-forms' ),  
  191. 'template' => include CFCORE_PATH . 'includes/templates/registration-form-example.php' 
  192. ),  
  193. 'simple_booking_form_example' => array( 
  194. 'name' => esc_html__( 'Simple Booking Form', 'caldera-forms' ),  
  195. 'template' => include CFCORE_PATH . 'includes/templates/simple-booking-form-example.php' 
  196. ),  
  197. 'rate-our-service-example' => array( 
  198. 'name' => esc_html__( 'Rate Our Service Form - with star review', 'caldera-forms' ),  
  199. 'template' => include CFCORE_PATH . 'includes/templates/rate-our-service-example.php' 
  200. ),  
  201. 'job-application-form-example' => array( 
  202. 'name' => esc_html__( 'Job Application Form - with Gravatar preview', 'caldera-forms' ),  
  203. 'template' => include CFCORE_PATH . 'includes/templates/job-application-form-example.php' 
  204. ),  
  205.  
  206.  
  207. ); 
  208.  
  209. /** 
  210. * Filter form templates 
  211. * @since 1.2.3 
  212. * @param array $internal_templates Form templates 
  213. */ 
  214. return apply_filters( 'caldera_forms_get_form_templates', $internal_templates ); 
  215.  
  216.  
  217. public function load_new_form_templates() { 
  218.  
  219. $form_templates = self::internal_form_templates(); 
  220.  
  221. ?> 
  222. <div class="cf-templates-wrapper"> 
  223. <?php 
  224. $selected_field = '';//' checked="checked"'; 
  225. $selected_template = '';//' selected'; 
  226.  
  227. foreach( $form_templates as $template_slug => $template ) { 
  228. if( !empty( $template['template'] ) && !empty( $template['name'] ) ) { 
  229.  
  230. echo '<label class="caldera-grid cf-form-template' . $selected_template . '">'; 
  231. echo '<small>' . $template['name'] . '</small>'; 
  232.  
  233. echo '<input type="radio" name="template" value="' . $template_slug . '" class="cf-template-select"' . $selected_field . '>'; 
  234.  
  235.  
  236. // check a layout exists 
  237. if( !empty( $template['preview'] ) ) { 
  238. echo '<img src="' . esc_url( $template['preview'] ) . '"></label>'; 
  239. continue; 
  240. if( empty( $template['template']['layout_grid'] ) || empty( $template['template']['layout_grid']['structure'] ) || empty( $template['template']['layout_grid']['fields'] ) ) { 
  241. echo '<p class="description" style="padding: 50px 0px; text-align: center;">' . esc_html__( 'Preview not available', 'caldera-forms' ) . '</p></label>'; 
  242. continue;  
  243.  
  244. $struct = explode('|', $template['template']['layout_grid']['structure'] ); 
  245.  
  246. foreach ($struct as $row_num=>$row) { 
  247.  
  248. $columns = explode( ':', $row ); 
  249. echo '<div class="row" style="margin: 6px 0px;">'; 
  250. foreach ($columns as $column_num=>$column) { 
  251. //var_dump( $template['template']['layout_grid']['fields'][ ( $row_num+1) . ':' . ( $column_num+1) ] ); 
  252. $fields = array_keys( $template['template']['layout_grid']['fields'], ( $row_num+1) . ':' . ( $column_num+1) ); 
  253. echo '<div class="col-sm-' . $column . '" style="padding: 0px 3px;">'; 
  254. echo '<div class="cf-template-column">'; 
  255. foreach( $fields as $field ) { 
  256. if( isset( $template['template']['fields'][ $field ] ) ) { 
  257. if( $template['template']['fields'][ $field ]['type'] == 'button') { 
  258. echo '<small class="cf-preview-field cf-preview-button">' . $template['template']['fields'][ $field ]['label'] .'</small>'; 
  259. }elseif( $template['template']['fields'][ $field ]['type'] == 'html') { 
  260. echo '<small class="cf-preview-field cf-preview-field-html"></small>'; 
  261. }elseif( $template['template']['fields'][ $field ]['type'] == 'paragraph') { 
  262. echo '<small class="cf-preview-field" style="height:50px;">' . $template['template']['fields'][ $field ]['label'] .'</small>'; 
  263. }elseif( $template['template']['fields'][ $field ]['type'] == 'hidden') { 
  264. // nope- nothing 
  265. }else{ 
  266. echo '<small class="cf-preview-field">' . $template['template']['fields'][ $field ]['label'] .'</small>'; 
  267. echo '</div>'; 
  268. echo '</div>'; 
  269.  
  270. echo '</div>'; 
  271.  
  272. echo '</label>'; 
  273. // unset selection 
  274. $selected_field = null; 
  275. $selected_template = null; 
  276.  
  277. ?> 
  278. <label class="caldera-grid cf-form-template"> 
  279. <small><?php echo esc_html__( 'Blank Form', 'caldera-forms' ); ?></small> 
  280. <input type="radio" name="template" value="" class="cf-template-select"> 
  281. </label> 
  282. <div class="caldera-grid cf-form-create" style="display:none; visibility: hidden;" aria-hidden="true"> 
  283. <div class="cf-template-title"></div> 
  284.  
  285. <div class="caldera-config-field"> 
  286. <input type="text" class="new-form-name block-input field-config" name="name" value="" required="required" autofocus="true" autocomplete="off" placeholder="<?php echo esc_html__('Form Name', 'caldera-forms' ); ?>"> 
  287. </div> 
  288.  
  289. <button type="button" class="cf-change-template-button"><span class="dashicons dashicons-arrow-left-alt"></span> <?php echo esc_html__( 'Change Template', 'caldera-forms' ); ?></button> 
  290. <button type="button" class="cf-create-form-button ajax-trigger"  
  291. data-action="create_form" 
  292. data-active-class="disabled" 
  293. data-load-class="disabled" 
  294. data-callback="new_form_redirect" 
  295. data-before="serialize_modal_form" 
  296. data-modal-autoclose="new_form" 
  297. data-nonce=<?php echo wp_create_nonce( 'cf_create_form' ); ?> 
  298. ><?php echo esc_html__( 'Create Form', 'caldera-forms' ); ?> <span class="dashicons dashicons-yes"></span><span class="spinner"></span></button> 
  299.  
  300. </div> 
  301.  
  302. </div> 
  303. <?php 
  304. /** 
  305. * Runs at the bottom of the new form modal 
  306. * Use to add extra buttons, etc. 
  307. * @since 1.4.2 
  308. */ 
  309. do_action( 'caldera_forms_new_form_modal_bottom' ); 
  310.  
  311. public function get_form_preview() { 
  312. global $post; 
  313. add_filter('caldera_forms_render_form_element', array( $this, 'set_preview_form_element') ); 
  314. $post = get_post( (int) $_POST['post_id'] ); 
  315. if( isset($_POST['atts']['named']['id']) ) { 
  316. $form = $_POST['atts']['named']['id']; 
  317. }elseif( isset($_POST['atts']['named']['name']) ) { 
  318. $form = $_POST['atts']['named']['name']; 
  319.  
  320. add_filter('caldera_forms_get_form-' . $form, array( $this, 'set_preview_get_form'), 100 ); 
  321.  
  322. $atts = $_POST['atts']['named']; 
  323. $atts['preview'] = true; 
  324.  
  325. if( !empty( $form ) ) { 
  326. ob_start(); 
  327. wp_head(); 
  328. echo Caldera_Forms::render_form( $form ); 
  329. wp_print_footer_scripts(); 
  330. $html = ob_get_clean(); 
  331. $out = array(); 
  332. if( !empty( $html ) ) { 
  333. $out['html'] = $html; 
  334.  
  335. wp_send_json_success( $out ); 
  336. public function set_preview_get_form( $form ) { 
  337. $form['form_ajax'] = false; 
  338. $form['settings']['responsive']['break_point'] = 'xs'; 
  339. return $form; 
  340. public function set_preview_form_element($element) { 
  341. return 'div'; 
  342.  
  343. public function prepare_filter_addons($plugins) { 
  344. global $wp_list_table, $status; 
  345.  
  346. if( !empty( $this->addons ) ) { 
  347. $addons = array(); 
  348. foreach( $this->addons as $addon ) { 
  349. $plugin_slug = basename( dirname( $addon['file'] ) ) .'/'.basename( $addon['file'] ); 
  350. if( isset( $plugins[$plugin_slug] ) ) { 
  351. if( isset( $addon['slug'] ) ) { 
  352. $plugins[$plugin_slug]['slug'] = $addon['slug']; 
  353. if( isset( $_REQUEST['plugin_status'] ) && $_REQUEST['plugin_status'] === 'caldera_forms' ) { 
  354. $status = 'caldera_forms'; 
  355.  
  356. return $plugins; 
  357.  
  358. public function bulk_action() { 
  359.  
  360. // first validate 
  361. self::verify_ajax_action(); 
  362.  
  363.  
  364. if(empty($_POST['do'])) { 
  365. die; 
  366.  
  367. $do_action = strtolower( $_POST['do'] ); 
  368.  
  369. switch ( $do_action ) { 
  370. case 'active': 
  371. case 'trash': 
  372. case 'delete': 
  373. global $wpdb; 
  374.  
  375. $result = false; 
  376. $items = array(); 
  377. $selectors = array(); 
  378. foreach ( (array) $_POST[ 'items' ] as $item_id ) { 
  379. $items[] = (int) $item_id; 
  380. $selectors[] = '#entry_row_' . (int) $item_id; 
  381.  
  382. switch ( $do_action ) { 
  383. case 'delete': 
  384. if( current_user_can( 'delete_others_posts' ) ) { 
  385. $result = Caldera_Forms_Entry_Bulk::delete_entries( $items ); 
  386. $out['status'] = 'reload'; 
  387. wp_send_json( $out ); 
  388. break; 
  389.  
  390. default: 
  391. if( current_user_can( 'edit_others_posts' ) ) { 
  392. $result = Caldera_Forms_Entry_Bulk::change_status( $items, $do_action ); 
  393. break; 
  394.  
  395. if( $result ) { 
  396. $out[ 'status' ] = $do_action; 
  397. $out[ 'undo' ] = ( $do_action === 'trash' ? 'active' : esc_html__( 'Trash', 'caldera-forms' ) ); 
  398. $out[ 'undo_text' ] = ( $do_action === 'trash' ? esc_html__( 'Restore', 'caldera-forms' ) : esc_html__( 'Trash', 'caldera-forms' ) ); 
  399.  
  400. $form = strip_tags( $_POST[ 'form' ] ); 
  401. $out[ 'entries' ] = implode( ', ', $selectors ); 
  402. $out[ 'total' ] = Caldera_Forms_Entry_Bulk::count( $form, false ); 
  403. $out[ 'trash' ] = Caldera_Forms_Entry_Bulk::count( $form, 'trash' ); 
  404. wp_send_json( $out ); 
  405. exit(); 
  406.  
  407. break; 
  408. case 'export': 
  409.  
  410. $transientid = uniqid('cfe'); 
  411. Caldera_Forms_Transient::set_transient( $transientid, $_POST['items'], 180 ); 
  412. $out['url'] = "admin.php?page=caldera-forms&export=" . $_POST['form'] . "&tid=" . $transientid; 
  413. wp_send_json( $out ); 
  414. exit(); 
  415. break; 
  416. default: 
  417. # code... 
  418. break; 
  419. exit(); 
  420.  
  421. /** 
  422. * Dismiss admin pointer 
  423. * @since unknown 
  424. * @uses "wp_ajax_cf_dismiss_pointer" action 
  425. */ 
  426. public static function update_pointer() { 
  427. if( ! isset( $_POST[ 'nonce' ] ) || ! wp_verify_nonce( $_POST[ 'nonce' ], 'cf_dismiss_pointer' ) ) { 
  428. status_header( 500 ); 
  429. exit; 
  430.  
  431. if ( ! empty( $_POST[ 'pointer' ] ) ) { 
  432. add_user_meta( get_current_user_id(), 'cf_pointer_' . $_POST[ 'pointer' ], array( 0 => NULL ) ); 
  433. exit; 
  434.  
  435. /** 
  436. * Load the plugin text domain for translation. 
  437. */ 
  438. public function load_plugin_textdomain() { 
  439. load_plugin_textdomain( $this->plugin_slug, FALSE, basename( CFCORE_PATH ) . '/languages'); 
  440.  
  441.  
  442.  
  443. public static function add_shortcode_inserter() { 
  444.  
  445. $screen = get_current_screen(); 
  446.  
  447. if($screen->base === 'post') { 
  448. include CFCORE_PATH . 'ui/insert_shortcode.php'; 
  449.  
  450. public static function get_admin_templates() { 
  451. include CFCORE_PATH . 'ui/news_templates.php'; 
  452. include CFCORE_PATH . 'ui/admin_templates.php'; 
  453. public static function get_admin_meta_templates() { 
  454.  
  455. $processors = $processors = Caldera_Forms_Processor_Load::get_instance()->get_processors(); 
  456. if(!empty($processors)) { 
  457. foreach($processors as $processor_type=>$processor_config) { 
  458. if( isset( $processor_config['meta_template'] ) && file_exists( $processor_config['meta_template'] ) ) { 
  459. echo "{{#if ".$processor_type."_template}}\r\n"; 
  460. echo "{{#each data}}\r\n"; 
  461. echo "{{#if title}}\r\n"; 
  462. echo "<h4>{{title}}</h4>\r\n"; 
  463. echo "{{/if}}\r\n"; 
  464. echo "{{#each entry}}\r\n"; 
  465. include $processor_config['meta_template']; 
  466. echo "{{/each}}\r\n"; 
  467. echo "{{/each}}\r\n"; 
  468. echo "{{/if}}\r\n"; 
  469.  
  470. /** 
  471. * Get the entry editor's buttons 
  472. * @since unknown 
  473. * @deprecated 
  474. */ 
  475. public static function get_entry_actions() { 
  476. _deprecated_function( __FUNCTION__, 'Caldera_Forms_Entry_UI::get_entry_actions()', '1.5.0' ); 
  477. Caldera_Forms_Entry_UI::get_entry_actions(); 
  478.  
  479. /** 
  480. * Set buttons for entry viewer 
  481. * @since 1.4.0 
  482. * @uses "caldera_forms_entry_viewer_buttons" filter 
  483. * @param array $buttons 
  484. * @return array 
  485. */ 
  486. public static function set_viewer_buttons($buttons) { 
  487.  
  488. $buttons[ 'close_panel' ] = array( 
  489. 'label' => esc_html__( 'Close', 'caldera-forms' ),  
  490. 'config' => 'dismiss',  
  491. 'class' => 'right' 
  492. ); 
  493.  
  494. if ( current_user_can( 'edit_others_posts' ) ) { 
  495. $buttons[ 'edit_entry' ] = array( 
  496. 'label' => esc_html__( 'Edit Entry', 'caldera-forms' ),  
  497. 'config' => array( 
  498. 'data-trigger' => '#edit-entry-{{_entry_id}}' 
  499. ),  
  500. 'class' => 'button-primary' 
  501. ); 
  502.  
  503. return $buttons; 
  504.  
  505.  
  506. public static function set_editor_buttons($buttons) { 
  507.  
  508. $buttons['submit_form'] = array( 
  509. 'label' => esc_html__( 'Save Changes', 'caldera-forms' ),  
  510. 'config' => array( 
  511. "data-for" => "#view_entry_baldrickModalBody .caldera_forms_form" 
  512. ),  
  513. 'class' => 'right button-primary' 
  514. ); 
  515. $buttons['view_entry'] = array( 
  516. 'label' => esc_html__( 'View Entry', 'caldera-forms' ),  
  517. 'config' => array( 
  518. "data-for" => ".view-entry-btn.current-view" 
  519. ),  
  520. 'class' => '' 
  521. ); 
  522.  
  523.  
  524. return $buttons; 
  525.  
  526.  
  527. public static function save_cf_setting() { 
  528. self::verify_ajax_action(); 
  529. if(empty($_POST['set'])) { 
  530. exit; 
  531. $style_includes = get_option( '_caldera_forms_styleincludes' ); 
  532.  
  533. if(empty($style_includes[$_POST['set']])) { 
  534. $style_includes[$_POST['set']] = true; 
  535. }else{ 
  536. $style_includes[$_POST['set']] = false; 
  537. update_option( '_caldera_forms_styleincludes', $style_includes); 
  538. wp_send_json( $style_includes ); 
  539. exit; 
  540.  
  541. /** 
  542. * Insert shortcode media button 
  543. */ 
  544. function shortcode_insert_button() { 
  545. global $post; 
  546. if(!empty($post)) { 
  547. echo "<a id=\"caldera-forms-form-insert\" title=\"". esc_attr__( 'Add Form to Page', 'caldera-forms' ) . "\" class=\"button caldera-forms-insert-button\" href=\"#inst\">\n"; 
  548. echo " <img src=\"". CFCORE_URL . "assets/images/caldera-globe-logo-sm.png\" alt=\"". esc_attr__( 'Insert Form Shortcode' , 'caldera-forms') . "\" style=\"padding: 0px 2px 0px 0px; width: 16px; margin: -2px 0px 0px;\" /> ".__('Caldera Form', 'caldera-forms' )."\n"; 
  549. echo "</a>\n"; 
  550. function shortcode_insert_button_fs($buttons) { 
  551.  
  552. $buttons['caldera-forms'] = array( 
  553. "title" => __( 'Add Form to Page', 'caldera-forms' ),  
  554. "both" => true 
  555. ); 
  556. return $buttons; 
  557.  
  558. /** 
  559. * Change form's state 
  560. * @uses "wp_ajax_toggle_form_state" action 
  561. * @since unknown 
  562. */ 
  563. public static function toggle_form_state() { 
  564. if( ! isset( $_POST[ 'nonce' ] ) || !wp_verify_nonce( $_POST[ 'nonce' ], 'toggle_form_state' ) ) { 
  565. wp_send_json_error( $_POST ); 
  566.  
  567. $forms = Caldera_Forms_Forms::get_forms( true ); 
  568. $form = sanitize_text_field( $_POST['form'] ); 
  569. $form = Caldera_Forms_Forms::get_form( $form ); 
  570. if( empty( $form ) || empty( $form['ID'] ) || empty( $forms[ $form['ID'] ]) ) { 
  571. wp_send_json_error( ); 
  572.  
  573. if( isset( $form['form_draft'] ) ) { 
  574.  
  575. Caldera_Forms_Forms::form_state( $form ); 
  576. $state = 'active-form'; 
  577. $label = esc_html__( 'Deactivate', 'caldera-forms' ); 
  578. }else{ 
  579. Caldera_Forms_Forms::form_state( $form , false ); 
  580. $state = 'draft-form'; 
  581. $label = esc_html__( 'Activate', 'caldera-forms' ); 
  582.  
  583.  
  584. wp_send_json_success( array( 'ID' => $form['ID'], 'state' => $state, 'label' => $label ) ); 
  585.  
  586. /** 
  587. * nonce verifier for ajax actions 
  588. * @since 1.3.2.1 
  589. */ 
  590. private static function verify_ajax_action() { 
  591. if ( ! isset( $_POST['cf_toolbar_actions'] ) || ! wp_verify_nonce( $_POST['cf_toolbar_actions'], 'cf_toolbar' ) || !check_admin_referer( 'cf_toolbar', 'cf_toolbar_actions' ) ) { 
  592. wp_send_json_error( $_POST ); 
  593.  
  594. /** 
  595. * Show entries in admin 
  596. * @deprecated 1.4.0 Use Caldera_Forms_Entry_UI::view_entries() 
  597. * @since unknown 
  598. */ 
  599. public static function browse_entries() { 
  600. _deprecated_function( __FUNCTION__, '1.4.0', 'Caldera_Forms_Entry_UI::view_entries' ); 
  601. self::verify_ajax_action(); 
  602. if ( isset( $_POST[ 'page' ] ) && 0 < $_POST[ 'page' ] ) { 
  603. $page = absint( $_POST[ 'page' ] ); 
  604. }else{ 
  605. $page = 1; 
  606. $entry_perpage = get_option( '_caldera_forms_entry_perpage', 20 ); 
  607. if ( isset( $_POST[ 'perpage' ] ) && 0 < $_POST[ 'perpage' ] ) { 
  608. $perpage = absint( (int) $_POST[ 'perpage' ] ); 
  609. if( $entry_perpage != $perpage ) { 
  610. update_option( '_caldera_forms_entry_perpage', $perpage ); 
  611. }else{ 
  612. $perpage = $entry_perpage; 
  613.  
  614. if ( isset( $_POST[ 'status' ] ) ) { 
  615. $status = strip_tags( $_POST[ 'status' ] ); 
  616. }else{ 
  617. $status = 'active'; 
  618.  
  619. $form = Caldera_Forms_Forms::get_form( $_POST['form'] ); 
  620.  
  621. $data = self::get_entries( $form, $page, $perpage, $status ); 
  622.  
  623. // set status output 
  624. $data['is_' . $status ] = true; 
  625.  
  626. wp_send_json( $data ); 
  627. exit; 
  628.  
  629.  
  630.  
  631. /** 
  632. * Get entries from a form 
  633. * @since 1.2.1 
  634. * @param string|array $form Form ID or form config. 
  635. * @param int $page Optional. Page of entries to get per page. Default is 1. 
  636. * @param int $perpage Optional. Number of entries per page. Default is 20. 
  637. * @param string $status Optional. Form status. Default is active. 
  638. * @return array 
  639. */ 
  640. public static function get_entries( $form, $page = 1, $perpage = 20, $status = 'active' ) { 
  641.  
  642. if ( is_string( $form ) ) { 
  643. $form = Caldera_Forms_Forms::get_form( $form ); 
  644.  
  645. if ( isset( $form[ 'ID' ])) { 
  646. $form_id = $form[ 'ID' ]; 
  647. }else{ 
  648. return; 
  649.  
  650. global $wpdb; 
  651.  
  652. $field_labels = array(); 
  653. $backup_labels = array(); 
  654. $selects = array(); 
  655.  
  656.  
  657. $fields = array(); 
  658. if ( ! empty( $form[ 'fields' ] ) ) { 
  659. foreach ( $form[ 'fields' ] as $fid => $field ) { 
  660. $fields[ $field[ 'slug' ] ] = $field; 
  661.  
  662. if ( ! empty( $field[ 'entry_list' ] ) ) { 
  663. $selects[] = "'" . $field[ 'slug' ] . "'"; 
  664. $field_labels[ $field[ 'slug' ] ] = $field[ 'label' ]; 
  665. $has_vars = array(); 
  666. if ( ! empty( $form[ 'variables' ][ 'types' ] ) ) { 
  667. $has_vars = $form[ 'variables' ][ 'types' ]; 
  668. if ( ( count( $backup_labels ) < 4 && ! in_array( 'entryitem', $has_vars ) ) && in_array( $field[ 'type' ], array( 
  669. 'text',  
  670. 'email',  
  671. 'date',  
  672. 'name' 
  673. ) ) 
  674. ) { 
  675. // backup only first 4 fields 
  676. $backup_labels[ $field[ 'slug' ] ] = $field[ 'label' ]; 
  677.  
  678. if ( empty( $field_labels ) ) { 
  679. $field_labels = $backup_labels; 
  680.  
  681. $entries = new Caldera_Forms_Entry_Entries( $form, $perpage ); 
  682.  
  683. $data = array(); 
  684.  
  685. $filter = null; 
  686.  
  687. $data[ 'trash' ] = $entries->get_total( 'trash' ); 
  688. $data[ 'active' ] = $entries->get_total( 'active' ); 
  689.  
  690. // set current total 
  691. if ( ! empty( $status ) && isset( $data[ $status ] ) ) { 
  692. $data[ 'total' ] = $entries->get_total( $status ); 
  693. } else { 
  694. $data[ 'total' ] = $data[ 'active' ]; 
  695.  
  696.  
  697. $data[ 'pages' ] = ceil( $data[ 'total' ] / $perpage ); 
  698.  
  699. if ( ! empty( $page ) ) { 
  700. $page = abs( $page ); 
  701. if ( $page > $data[ 'pages' ] ) { 
  702. $page = $data[ 'pages' ]; 
  703.  
  704. $data['current_page'] = $page; 
  705.  
  706. if($data['total'] > 0) { 
  707.  
  708. $data[ 'form' ] = $form_id; 
  709.  
  710. $data[ 'fields' ] = $field_labels; 
  711.  
  712.  
  713. $the_entries = $entries->get_page( $page, $status ); 
  714.  
  715. if ( ! empty( $the_entries ) ) { 
  716.  
  717. $ids = array(); 
  718. $data[ 'entries' ] = array(); 
  719.  
  720.  
  721. /** @var Caldera_Forms_Entry $an_entry */ 
  722. foreach ( $the_entries as $an_entry ) { 
  723. $ids[] = $an_entry->get_entry_id(); 
  724. // init field types to initialize view rendering in entry lists 
  725. Caldera_Forms_Fields::get_all(); 
  726.  
  727. foreach ( $ids as $entry_id ) { 
  728. $rows = $entries->get_rows( $page, (int) $entry_id, $status ); 
  729. foreach ( $rows as $row ) { 
  730. $e = 'E' . $row->entry_id; 
  731. if ( ! empty( $row->_user_id ) ) { 
  732. $user = get_userdata( $row->_user_id ); 
  733. if ( ! empty( $user ) ) { 
  734. $data[ 'entries' ][ $e ][ 'user' ][ 'ID' ] = $user->ID; 
  735. $data[ 'entries' ][ $e ][ 'user' ][ 'name' ] = $user->data->display_name; 
  736. $data[ 'entries' ][ $e ][ 'user' ][ 'email' ] = $user->data->user_email; 
  737. $data[ 'entries' ][ $e ][ 'user' ][ 'avatar' ] = get_avatar( $user->ID, 64 ); 
  738.  
  739. $data[ 'entries' ][ $e ][ '_entry_id' ] = $row->entry_id; 
  740.  
  741. $submitted = $row->_datestamp; 
  742.  
  743.  
  744. $data[ 'entries' ][ $e ][ '_date' ] = Caldera_Forms::localize_time( $submitted ); 
  745.  
  746. // setup default data array 
  747. if ( ! isset( $data[ 'entries' ][ $e ][ 'data' ] ) ) { 
  748. if ( isset( $field_labels ) ) { 
  749. foreach ( $field_labels as $slug => $label ) { 
  750. // setup labels ordering 
  751. $data[ 'entries' ][ $e ][ 'data' ][ $slug ] = null; 
  752.  
  753. if ( ! empty( $field_labels[ $row->slug ] ) ) { 
  754.  
  755. $label = $field_labels[ $row->slug ]; 
  756.  
  757. // check view handler 
  758. $field = Caldera_Forms_Field_Util::get_field( $row->slug, $form, true ); 
  759.  
  760. // maybe json? 
  761. $is_json = json_decode( $row->value, ARRAY_A ); 
  762. if ( ! empty( $is_json ) ) { 
  763. $row->value = $is_json; 
  764.  
  765. if( is_string( $row->value ) ) { 
  766. $row->value = esc_html( stripslashes_deep( $row->value ) ); 
  767. }else{ 
  768. $row->value = stripslashes_deep( Caldera_Forms_Sanitize::sanitize( $row->value ) ); 
  769.  
  770. $row->value = apply_filters( 'caldera_forms_view_field_' . $field[ 'type' ], $row->value, $field, $form ); 
  771.  
  772.  
  773. if ( isset( $data[ 'entries' ][ $e ][ 'data' ][ $row->slug ] ) ) { 
  774. // array based - add another entry 
  775. if ( ! is_array( $data[ 'entries' ][ $e ][ 'data' ][ $row->slug ] ) ) { 
  776. $tmp = $data[ 'entries' ][ $e ][ 'data' ][ $row->slug ]; 
  777. $data[ 'entries' ][ $e ][ 'data' ][ $row->slug ] = array( $tmp ); 
  778. $data[ 'entries' ][ $e ][ 'data' ][ $row->slug ][] = $row->value; 
  779. } else { 
  780. $data[ 'entries' ][ $e ][ 'data' ][ $row->slug ] = $row->value; 
  781.  
  782. if ( ! empty( $form[ 'variables' ][ 'types' ] ) ) { 
  783. foreach ( $form[ 'variables' ][ 'types' ] as $var_key => $var_type ) { 
  784. if ( $var_type == 'entryitem' ) { 
  785. $data[ 'fields' ][ $form[ 'variables' ][ 'keys' ][ $var_key ] ] = ucwords( str_replace( '_', ' ', $form[ 'variables' ][ 'keys' ][ $var_key ] ) ); 
  786. $data[ 'entries' ][ $e ][ 'data' ][ $form[ 'variables' ][ 'keys' ][ $var_key ] ] = Caldera_Forms::do_magic_tags( $form[ 'variables' ][ 'values' ][ $var_key ], $row->_entryid ); 
  787.  
  788.  
  789.  
  790.  
  791. return $data; 
  792.  
  793.  
  794.  
  795. /** 
  796. * Return an instance of this class. 
  797. * @return object A single instance of this class. 
  798. */ 
  799. public static function get_instance() { 
  800.  
  801. // If the single instance hasn't been set, set it now. 
  802. if ( null == self::$instance ) { 
  803. self::$instance = new self; 
  804.  
  805. return self::$instance; 
  806.  
  807. /** 
  808. * Registers the admin page 
  809. */ 
  810. public function register_admin_page() { 
  811. global $menu, $submenu; 
  812.  
  813. $forms = Caldera_Forms_Forms::get_forms( true ); 
  814.  
  815. // get current user 
  816. if( current_user_can( Caldera_Forms::get_manage_cap() ) ) { 
  817.  
  818. $this->screen_prefix[] = add_menu_page( __('Caldera Forms', 'caldera-forms' ), __('Caldera Forms', 'caldera-forms' ), Caldera_Forms::get_manage_cap(), $this->plugin_slug, array( $this, 'render_admin' ), 'dashicons-cf-logo', 52.81321 ); 
  819. add_submenu_page( $this->plugin_slug, __('Caldera Forms Admin', 'caldera-forms' ), __('Forms', 'caldera-forms' ), Caldera_Forms::get_manage_cap(), $this->plugin_slug, array( $this, 'render_admin' ) ); 
  820.  
  821. if( ! empty( $forms ) ) { 
  822. foreach($forms as $form_id=>$form) { 
  823. if(!empty($form['pinned'])) { 
  824. $this->screen_prefix[] = add_submenu_page( $this->plugin_slug, __('Caldera Forms', 'caldera-forms' ).' - ' . $form['name'], '- '.$form['name'], Caldera_Forms::get_manage_cap(), $this->plugin_slug . '-pin-' . $form_id, array( $this, 'render_admin' ) ); 
  825.  
  826.  
  827. $this->screen_prefix[] = add_submenu_page( $this->plugin_slug, __('Caldera Forms', 'caldera-forms' ) . ' - ' . __('Extend', 'caldera-forms' ), __('Extend', 'caldera-forms' ), Caldera_Forms::get_manage_cap(), $this->plugin_slug . '-extend', array( $this, 'render_admin' ) ); 
  828. }else{ 
  829. // not an admin - pin for user 
  830. if( ! empty( $forms ) ) { 
  831. $user = wp_get_current_user(); 
  832. if(empty($user->roles)) { 
  833. // no role - bye bye. 
  834. return; 
  835.  
  836. foreach($forms as $form_id=>$form) { 
  837. $capability = null; 
  838. if(!empty($form['pinned']) && !empty( $form['pin_roles'] ) ) { 
  839. if( !empty( $form['pin_roles']['all_roles'] ) ) { 
  840. $user = wp_get_current_user(); 
  841. if( empty( $user ) || empty( $user->roles ) ) { 
  842. continue; 
  843. $capabilities = array_keys( $user->allcaps ); 
  844. if( empty( $capabilities ) ) { 
  845. continue; 
  846. $capability = $capabilities[0]; 
  847. }elseif( !empty( $form['pin_roles']['access_role'] ) ) { 
  848. foreach ($form['pin_roles']['access_role'] as $role => $enabled) { 
  849. if( in_array( $role, $user->roles ) ) { 
  850. $role_details = get_role( $role ); 
  851. if(empty($role_details->capabilities)) { 
  852. continue; 
  853. $capabilities = array_keys( $role_details->capabilities ); 
  854. $capability = $capabilities[0]; 
  855. break; 
  856. if( empty($capability)) { 
  857. // not this one. 
  858. continue; 
  859.  
  860. if( empty( $this->screen_prefix ) ) { 
  861. // make top menu 
  862. $main_slug = $this->plugin_slug . '-pin-' . $form_id; 
  863. $this->screen_prefix[] = add_menu_page( __('Caldera Forms', 'caldera-forms' ), __('Caldera Forms', 'caldera-forms' ), $capability, $main_slug, array( $this, 'render_admin' ), 'dashicons-cf-logo', 52.999 ); 
  864.  
  865.  
  866. $this->screen_prefix[] = add_submenu_page( $main_slug, __('Caldera Forms', 'caldera-forms' ).' - ' . $form['name'], $form['name'], $capability, $this->plugin_slug . '-pin-' . $form_id, array( $this, 'render_admin' ) ); 
  867.  
  868.  
  869.  
  870.  
  871. /** 
  872. * Register and enqueue admin-specific style sheet. 
  873. * @return null 
  874. */ 
  875. public function enqueue_admin_stylescripts() { 
  876.  
  877. $screen = get_current_screen(); 
  878. Caldera_Forms_Render_Assets::register(); 
  879. Caldera_Forms_Admin_Assets::register_scripts(); 
  880. Caldera_Forms_Admin_Assets::register_styles(); 
  881.  
  882. wp_enqueue_style( $this->plugin_slug . '-admin-icon-styles', CFCORE_URL . 'assets/css/dashicon.css', array(), self::VERSION ); 
  883.  
  884. if ( $screen->base === 'post' ) { 
  885. Caldera_Forms_Admin_Assets::post_editor(); 
  886.  
  887.  
  888. if ( ! in_array( $screen->base, $this->screen_prefix ) ) { 
  889. return; 
  890.  
  891. add_action( 'admin_head', array( __CLASS__, 'remove_notice_actions' ) ); 
  892. if( self::is_page( 'caldera-forms-extend' ) ) { 
  893. add_action( 'admin_enqueue_scripts', array( 'Caldera_Forms_Admin_Extend', 'scripts' ), 55 ); 
  894. return; 
  895.  
  896. Caldera_Forms_Admin_Assets::admin_common(); 
  897.  
  898. if ( Caldera_Forms_Admin::is_edit() ) { 
  899. Caldera_Forms_Admin_Assets::form_editor(); 
  900.  
  901. } else { 
  902.  
  903. Caldera_Forms_Render_Assets::enqueue_all_fields(); 
  904.  
  905.  
  906. if ( ! empty( $_GET[ 'edit-entry' ] ) ) { 
  907. Caldera_Forms_Render_Assets::enqueue_style( 'grid' ); 
  908. }else{ 
  909. $clippy = new Caldera_Forms_Admin_Clippy( $this->plugin_slug, site_url() ); 
  910. $clippy->assets(); 
  911.  
  912. Caldera_Forms_Admin_Assets::panels(); 
  913.  
  914.  
  915. /** 
  916. * Renders the admin pages 
  917. */ 
  918. public function render_admin() { 
  919.  
  920. echo " <div class=\"wrap\">\r\n"; 
  921. if(!empty($_GET['edit'])) { 
  922. echo "<form method=\"POST\" action=\"admin.php?page=" . $this->plugin_slug . "\" data-load-element=\"#save_indicator\" data-sender=\"ajax\" class=\"caldera-forms-options-form edit-update-trigger\">\r\n"; 
  923. include CFCORE_PATH . 'ui/edit.php'; 
  924. echo "</form>\r\n"; 
  925. }elseif(!empty($_GET['page']) && $_GET['page'] == 'caldera-forms-extend') { 
  926. include CFCORE_PATH . 'ui/extend.php'; 
  927. }elseif(!empty($_GET['page']) && false !== strpos($_GET['page'], 'caldera-forms-pin-')) { 
  928. $formID = substr($_GET['page'], 18); 
  929. $form = Caldera_Forms_Forms::get_form( $formID ); 
  930. include CFCORE_PATH . 'ui/entries.php'; 
  931.  
  932. }else{ 
  933. add_action( 'caldera_forms_admin_footer', array( 'Caldera_Forms_Entry_Viewer', 'print_scripts' ) ); 
  934. include CFCORE_PATH . 'ui/admin.php'; 
  935.  
  936. echo " </div>\r\n"; 
  937.  
  938.  
  939.  
  940.  
  941. /*** 
  942. * Handles form updating, deleting, exporting and importing 
  943. * @uses "init" action 
  944. */ 
  945. static function save_form() { 
  946. if( ! isset( $_GET[ 'page' ] ) || 'caldera-forms' != $_GET[ 'page' ] ) { 
  947. return; 
  948.  
  949. /// check for form delete 
  950. if(!empty($_GET['delete']) && !empty($_GET['cal_del']) && current_user_can( Caldera_Forms::get_manage_cap( 'save' ), strip_tags( $_GET[ 'delete' ] ) ) ) { 
  951.  
  952. if ( ! wp_verify_nonce( $_GET['cal_del'], 'cf_del_frm' ) ) { 
  953. // This nonce is not valid. 
  954. wp_die( __('Sorry, please try again', 'caldera-forms' ), __('Form Delete Error', 'caldera-forms' ) ); 
  955. }else{ 
  956. $deleted = Caldera_Forms_Forms::delete_form( strip_tags( $_GET['delete'] ) ); 
  957. if ( $deleted ) { 
  958. wp_redirect( 'admin.php?page=caldera-forms' ); 
  959. exit; 
  960. } else { 
  961. wp_die( __('Sorry, please try again', 'caldera-forms' ), __('Form could not be deleted.', 'caldera-forms' ) ); 
  962.  
  963.  
  964.  
  965. /** IMPORT */ 
  966. if( isset($_POST['cfimporter']) && current_user_can( Caldera_Forms::get_manage_cap( 'import' ) ) ) { 
  967.  
  968. if ( check_admin_referer( 'cf-import', 'cfimporter' ) ) { 
  969. if ( isset( $_FILES[ 'import_file' ] ) && ! empty( $_FILES[ 'import_file' ][ 'size' ] ) ) { 
  970. $loc = wp_upload_dir(); 
  971.  
  972. if ( move_uploaded_file( $_FILES[ 'import_file' ][ 'tmp_name' ], $loc[ 'path' ] . '/cf-form-import.json' ) ) { 
  973. $data = json_decode( file_get_contents( $loc[ 'path' ] . '/cf-form-import.json' ), true ); 
  974. if( ! is_array( $data ) ) { 
  975. wp_die( esc_html__( 'File is not a valid Caldera Form Import', 'caldera-forms' ) ); 
  976. if( ! isset( $_POST[ 'name' ] ) ) { 
  977. wp_die( esc_html__( 'Form must have a name.', 'caldera-forms' ) ); 
  978.  
  979.  
  980. $data[ 'name' ] = strip_tags( $_POST[ 'name' ] ); 
  981.  
  982. $new_form_id = Caldera_Forms_Forms::import_form( $data ); 
  983. if( is_string( $new_form_id ) ) { 
  984.  
  985. cf_redirect( add_query_arg(array( 
  986. 'page' => 'caldera-forms',  
  987. 'edit' => $new_form_id 
  988. ), admin_url( 'admin.php' ) ), 302 ); 
  989. exit; 
  990.  
  991. }else{ 
  992. wp_die( esc_html__( 'Form could not be imported.', 'caldera-forms' ) ); 
  993.  
  994.  
  995.  
  996.  
  997. } else { 
  998. wp_die( esc_html__( 'Sorry, File not uploaded.', 'caldera-forms' ), esc_html__( 'Form Import Error', 'caldera-forms' ) ); 
  999.  
  1000. } else { 
  1001.  
  1002. wp_die( esc_html__( 'Sorry, please try again', 'caldera-forms' ), esc_html__( 'Form Import Error', 'caldera-forms' ) ); 
  1003.  
  1004.  
  1005. if(!empty($_GET['export-form']) && current_user_can( Caldera_Forms::get_manage_cap( 'export', strip_tags( $_GET[ 'export-form' ] ) ) )) { 
  1006.  
  1007. $form = Caldera_Forms_Forms::get_form( $_GET['export-form'] ); 
  1008.  
  1009. if(empty($form)) { 
  1010. wp_die( __('Form does not exist.', 'caldera-forms' ) ); 
  1011.  
  1012. header("Pragma: public"); 
  1013. header("Expires: 0"); 
  1014. header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
  1015. header("Cache-Control: private", false); 
  1016. if( empty( $_GET['format'] ) || $_GET['format'] != 'php' ) { 
  1017. header("Content-Type: application/json"); 
  1018. header("Content-Disposition: attachment; filename=\"" . sanitize_file_name( strtolower( $form['name'] ) ) . "-export.json\";" ); 
  1019. echo json_encode($form); 
  1020. }else{ 
  1021.  
  1022. $form_id = sanitize_key( $_GET['form_id'] ); 
  1023. if( !empty( $_GET['pin_menu'] ) ) { 
  1024. $form['pinned'] = 1; 
  1025. header("Content-Type: application/php"); 
  1026. header("Content-Disposition: attachment; filename=\"" . sanitize_file_name( strtolower( $form_id ) ) . "-include.php\";" ); 
  1027. echo '<?php' . "\r\n"; 
  1028. echo "/**\r\n * Caldera Forms - PHP Export \r\n * {$form['name']} \r\n * @version " . CFCORE_VER . "\r\n * @license GPL-2.0+\r\n * \r\n */\r\n\r\n\r\n"; 
  1029.  
  1030. $structure = "/**\r\n * Filter admin forms to include custom form in admin\r\n *\r\n * @since 1.3.1\r\n *\r\n * @param array \$forms All registered forms\r\n */\r\n"; 
  1031. $structure .= 'add_filter( "caldera_forms_get_forms", function( $forms ) {' . "\r\n"; 
  1032. $structure .= "\t" . '$forms["' . $form_id . '"] = apply_filters( "caldera_forms_get_form-' . $form_id . '", array() );' . "\r\n"; 
  1033. $structure .= "\t" . 'return $forms;' . "\r\n"; 
  1034. $structure .= "} );\r\n\r\n"; 
  1035.  
  1036. $structure .= "/**\r\n * Filter form request to include form structure to be rendered\r\n *\r\n * @since 1.3.1\r\n *\r\n * @param \$form array form structure\r\n */\r\n"; 
  1037. $structure .= "add_filter( 'caldera_forms_get_form-{$form_id}', function( \$form ) {\r\n return " . var_export( $form, true ) . ";\r\n" . '} );' . "\r\n"; 
  1038. // cleanups because I'm me 
  1039. $structure = str_replace( 'array (', 'array(', $structure ); 
  1040. $structure = str_replace( $form['ID'], $form_id, $structure ); 
  1041. // switch field IDs 
  1042. if( !empty( $_GET['convert_slugs'] ) ) { 
  1043. if ( !empty( $form['fields'] ) ) { 
  1044. foreach( $form['fields'] as $field_id=>$field ) { 
  1045. $structure = str_replace( $field_id, $field['slug'], $structure ); 
  1046.  
  1047. echo $structure; 
  1048. exit; 
  1049.  
  1050.  
  1051. if(!empty($_GET['export']) && current_user_can( Caldera_Forms::get_manage_cap( 'export', strip_tags( $_GET[ 'export' ] ) ) ) ) { 
  1052.  
  1053. $form = Caldera_Forms_Forms::get_form( $_GET['export'] ); 
  1054.  
  1055. global $wpdb; 
  1056.  
  1057. //build labels 
  1058. $labels = array(); 
  1059. $structure = array(); 
  1060. $field_types = Caldera_Forms_Fields::get_all(); 
  1061. $headers = array(); 
  1062. if(!empty($form['fields'])) { 
  1063. $headers['date_submitted'] = 'Submitted'; 
  1064. foreach( Caldera_Forms_Forms::get_fields( $form, true ) as $field_id => $field ) { 
  1065. if(isset($field_types[$field['type']]['capture']) && false === $field_types[$field['type']]['capture']) { 
  1066. continue; 
  1067. $headers[$field['slug']] = $field['label']; 
  1068. $structure[$field['slug']] = $field_id; 
  1069. $filter = null; 
  1070. // export set - transient 
  1071. if(!empty($_GET['tid'])) { 
  1072. $items = Caldera_Forms_Transient::get_transient( $_GET[ 'tid' ] ); 
  1073.  
  1074. if(!empty($items)) { 
  1075. Caldera_Forms_Transient::delete_transient( $_GET[ 'tid' ] ); 
  1076. $filter = ' AND `entry`.`id` IN (' . implode(', ', $items) . ') '; 
  1077. }else{ 
  1078. wp_die( __('Export selection has expired', 'caldera-forms' ) , __('Export Expired', 'caldera-forms' ) ); 
  1079.  
  1080. $rawdata = $wpdb->get_results($wpdb->prepare(" 
  1081. SELECT 
  1082. `entry`.`id` as `_entryid`,  
  1083. `entry`.`form_id` AS `_form_id`,  
  1084. `entry`.`datestamp` AS `_date_submitted`,  
  1085. `entry`.`user_id` AS `_user_id` 
  1086.  
  1087. FROM `" . $wpdb->prefix ."cf_form_entries` AS `entry` 
  1088.  
  1089.  
  1090. WHERE `entry`.`form_id` = %s 
  1091. " . $filter . " 
  1092. AND `entry`.`status` = 'active' 
  1093. ORDER BY `entry`.`datestamp` DESC;", $_GET['export'])); 
  1094.  
  1095. $data = array(); 
  1096.  
  1097. foreach( $rawdata as $entry) { 
  1098. $submission = Caldera_Forms::get_entry( $entry->_entryid, $form); 
  1099. $data[$entry->_entryid]['date_submitted'] = $entry->_date_submitted; 
  1100.  
  1101. foreach ($structure as $slug => $field_id) { 
  1102. $data[$entry->_entryid][$slug] = ( isset( $submission['data'][$field_id]['value'] ) ? $submission['data'][$field_id]['value'] : null ); 
  1103.  
  1104.  
  1105. if( empty( $headers ) ) { 
  1106. wp_die( esc_html__( 'Could not process export. This is most likely due to a problem with the form configuration.', 'caldera-forms' ) ); 
  1107. $encoding = Caldera_Forms_CSV_Util::character_encoding( $form ); 
  1108.  
  1109. header("Pragma: public"); 
  1110. header("Expires: 0"); 
  1111. header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
  1112. header("Cache-Control: private", false); 
  1113. header("Content-Type: text/csv charset=$encoding;"); 
  1114. header("Content-Disposition: attachment; filename=\"" . sanitize_file_name( $form['name'] ) . ".csv\";" ); 
  1115. header("Content-Transfer-Encoding: binary"); 
  1116. $df = fopen("php://output", 'w'); 
  1117. fputcsv($df, $headers); 
  1118. foreach($data as $row) { 
  1119. $csvrow = array(); 
  1120. foreach($headers as $key=>$label) { 
  1121. if(!isset($row[$key])) { 
  1122. $row[$key] = null; 
  1123. }else{ 
  1124. if( is_array( $row[$key] ) && isset( $row[$key]['label'] ) ) { 
  1125. $row[$key] = $row[$key]['value']; 
  1126. }elseif( is_array( $row[$key] ) ) { 
  1127. $subs = array(); 
  1128. foreach( $row[$key] as $row_part ) { 
  1129. if( is_array( $row_part ) && isset( $row_part['label'] ) ) { 
  1130. $subs[] = $row_part['value']; 
  1131. }else{ 
  1132. $subs[] = $row_part; 
  1133. $row[$key] = implode(', ', $subs ); 
  1134.  
  1135. $csvrow[] = $row[$key]; 
  1136. fputcsv($df, $row); 
  1137. fclose($df); 
  1138. exit; 
  1139.  
  1140. if( isset($_POST['config']) && isset( $_POST['cf_edit_nonce'] ) && current_user_can( Caldera_Forms::get_manage_cap( 'manage' ) ) ) { 
  1141.  
  1142. // if this fails, check_admin_referer() will automatically print a "failed" page and die. 
  1143. if ( check_admin_referer( 'cf_edit_element', 'cf_edit_nonce' ) ) { 
  1144.  
  1145. // strip slashes 
  1146. $data = json_decode( stripslashes_deep($_POST['config']) , ARRAY_A ); 
  1147. self::save_a_form( $data ); 
  1148.  
  1149. if(!empty($_POST['sender'])) { 
  1150. exit; 
  1151.  
  1152. wp_redirect('admin.php?page=caldera-forms'); 
  1153. die; 
  1154.  
  1155. return; 
  1156.  
  1157. /** 
  1158. * Save a form 
  1159. * @since 1.3.4 
  1160. * @param array $data 
  1161. */ 
  1162. public static function save_a_form( $data ) { 
  1163. Caldera_Forms_Forms::save_form( $data ); 
  1164.  
  1165. /** 
  1166. * AJAX callback for new form creation 
  1167. * @since unknown 
  1168. * @uses "wp_ajax_create_form" action 
  1169. */ 
  1170. public static function create_form() { 
  1171. $nonce_validated = false; 
  1172. if( isset( $_POST[ 'nonce' ] ) && wp_verify_nonce( $_POST[ 'nonce'], 'cf_create_form' ) ) { 
  1173. $nonce_validated = true; 
  1174.  
  1175. parse_str( $_POST['data'], $newform ); 
  1176.  
  1177.  
  1178. if( ! $nonce_validated ) { 
  1179. if( isset( $newform, $newform[ 'nonce' ] ) ) { 
  1180. if( wp_verify_nonce( $newform[ 'nonce' ], 'cf_create_form' ) ) { 
  1181. $nonce_validated = true; 
  1182.  
  1183.  
  1184. if( ! $nonce_validated ) { 
  1185. status_header(500); 
  1186. wp_send_json_error( ); 
  1187. $newform = Caldera_Forms_Forms::create_form( $newform ); 
  1188. echo $newform['ID']; 
  1189. exit; 
  1190.  
  1191.  
  1192.  
  1193.  
  1194. // get internal panel extensions 
  1195.  
  1196. public function get_panel_extensions($panels) { 
  1197.  
  1198. $path = CFCORE_PATH . "ui/panels/"; 
  1199.  
  1200. $internal_panels = array( 
  1201. 'form_layout' => array( 
  1202. "name" => __( 'Layout', 'caldera-forms' ),  
  1203. "setup" => array( 
  1204. "scripts" => array( 
  1205. 'jquery-ui-sortable',  
  1206. 'jquery-ui-draggable',  
  1207. 'jquery-ui-droppable',  
  1208. ),  
  1209. "styles" => array( 
  1210. CFCORE_URL . "assets/css/editor-grid.css",  
  1211. CFCORE_URL . "assets/css/processors-edit.css" 
  1212. ),  
  1213. ),  
  1214. "tabs" => array( 
  1215. "layout" => array( 
  1216. "name" => __( 'Layout', 'caldera-forms' ),  
  1217. "location" => "lower",  
  1218. "label" => __( 'Layout Builder', 'caldera-forms' ),  
  1219. "active" => true,  
  1220. "actions" => array( 
  1221. $path . "layout_toolbar.php" 
  1222. ),  
  1223. "repeat" => 0,  
  1224. "canvas" => $path . "layout.php",  
  1225. "side_panel" => $path . "layout_side.php",  
  1226. ),  
  1227. "pages" => array( 
  1228. "name" => __( 'Pages', 'caldera-forms' ),  
  1229. "location" => "lower",  
  1230. "label" => __( 'Form Pages', 'caldera-forms' ),  
  1231. "canvas" => $path . "pages.php",  
  1232. ),  
  1233. "mailer" => array( 
  1234. "name" => __( 'Email', 'caldera-forms' ),  
  1235. "location" => "lower",  
  1236. "label" => __( 'Email Notification Settings', 'caldera-forms' ),  
  1237. "canvas" => $path . "emailer.php",  
  1238. ),  
  1239. "processors" => array( 
  1240. "name" => __( 'Processors', 'caldera-forms' ),  
  1241. "location" => "lower",  
  1242. "label" => __( 'Form Processors', 'caldera-forms' ),  
  1243. "canvas" => $path . "processors.php",  
  1244. ),  
  1245. "conditions" => array( 
  1246. "name" => __( 'Conditions', 'caldera-forms' ),  
  1247. "location" => "lower",  
  1248. "label" => __( 'Conditions', 'caldera-forms' ),  
  1249. "canvas" => $path . "conditions.php",  
  1250. ),  
  1251. "variables" => array( 
  1252. "name" => __( 'Variables', 'caldera-forms' ),  
  1253. "location" => "lower",  
  1254. "label" => __( 'Variables', 'caldera-forms' ),  
  1255. "canvas" => $path . "variables.php",  
  1256. "actions" => array( 
  1257. $path . "variable_add.php" 
  1258. ),  
  1259. ),  
  1260. "responsive" => array( 
  1261. "name" => __( 'Responsive', 'caldera-forms' ),  
  1262. "location" => "lower",  
  1263. "label" => __( 'Responsive Settings', 'caldera-forms' ),  
  1264. "repeat" => 0,  
  1265. "fields" => array( 
  1266. "break_point" => array( 
  1267. "label" => __( 'Grid Collapse', 'caldera-forms' ),  
  1268. "slug" => "break_point",  
  1269. "caption" => __( 'Set the smallest screen size at which to collapse the grid. (based on Bootstrap 3.0)', 'caldera-forms' ),  
  1270. "type" => "radio",  
  1271. "config" => array( 
  1272. "default" => "sm",  
  1273. "option" => array( 
  1274. "xs" => array( 
  1275. 'value' => 'xs',  
  1276. 'label' => __('Maintain grid always', 'caldera-forms' ),  
  1277. ),  
  1278. "sm" => array( 
  1279. 'value' => 'sm',  
  1280. 'label' => '< 767px' 
  1281. ),  
  1282. "md" => array( 
  1283. 'value' => 'md',  
  1284. 'label' => '< 991px' 
  1285. ),  
  1286. "lg" => array( 
  1287. 'value' => 'lg',  
  1288. 'label' => '< 1199px' 
  1289. ),  
  1290. ),  
  1291. ),  
  1292. ),  
  1293. ),  
  1294. ); 
  1295.  
  1296. return array_merge( $panels, $internal_panels ); 
  1297.  
  1298.  
  1299. /** 
  1300. * Add to the admin notices 
  1301. * @since 1.3.0 
  1302. * @param string|array $notice The notice or array of notices to add. 
  1303. */ 
  1304. public static function add_admin_notice( $notice ) { 
  1305. if ( is_string( $notice ) ) { 
  1306. self::$admin_notices[] = $notice; 
  1307.  
  1308. if ( is_array( $notice ) ) { 
  1309. foreach( $notice as $n) { 
  1310. self::add_admin_notice( $n ); 
  1311.  
  1312.  
  1313.  
  1314. /** 
  1315. * Get the admin messages 
  1316. * @since 1.3 
  1317. * @param bool $as_string Optional. To return as string, the default, or as an array 
  1318. * @param string $seperator Optional. What to break notices with, when returning as string. Default is "\n" 
  1319. * @return string|array|void 
  1320. */ 
  1321. public static function get_admin_notices( $as_string = true, $seperator = "\n" ) { 
  1322. if ( ! empty( self::$admin_notices ) ) { 
  1323. if ( $as_string ) { 
  1324. return implode( $seperator, self::$admin_notices ); 
  1325.  
  1326. }else{ 
  1327. return self::$admin_notices; 
  1328.  
  1329.  
  1330.  
  1331.  
  1332. /** 
  1333. * Handle admin alert/notices 
  1334. * @since 1.3.4 
  1335. * @uses "caldera_forms_admin_footer" 
  1336. */ 
  1337. public static function admin_alerts() { 
  1338. $optin_status = Caldera_Forms_Tracking::tracking_optin_status(); 
  1339. if( 'dismiss' !== $optin_status && 0 == $optin_status ) { 
  1340.  
  1341. $allow = Caldera_Forms_Tracking::allow_url(); 
  1342. $dismiss = Caldera_Forms_Tracking::dismiss(); 
  1343. $message[] = __( 'Allow us to track basic usage data and receive a 10% discount at CalderaWP.com.', 'caldera-forms' ); 
  1344. $message[] = __( 'No form entries, or sensitive data will be saved.', 'caldera-forms' ); 
  1345. $message[] = __( 'This data is used to help improve Caldera Forms and it will never be shared with a third-party.', 'caldera-forms' ); 
  1346. $message[] = __( 'If you choose to allow us to track data, a 10% discount code for CalderaWP.com will be sent to the admin email for this site.', 'caldera-forms' ); 
  1347. $message[] = sprintf( '<em><a href="https://calderaforms.com/?post_type=doc&p=17228" target="_blank" title="%s">%s</a></em>',  
  1348. esc_html__( 'Information on CalderaWP site about usage tracking', 'caldera-forms' ),  
  1349. esc_html__( 'Learn more about what is tracked here.', 'caldera-forms' ) 
  1350. ); 
  1351. $message = '<p>' . implode( ' ', $message ) . '</p>'; 
  1352.  
  1353. $message .= sprintf( '<p style="display:inline;float:left;" ><a type="button" class="button button-secondary" href="%s">%s</a></p>', esc_url_raw( $dismiss ), __( 'No Thanks', 'caldera-forms' ) ); 
  1354.  
  1355. $message .= sprintf( '<p style="display:inline; float:right;"><a type="button" class="button button-primary" href="%s">%s</a>', esc_url_raw( $allow ), __( 'Help Us & Save', 'caldera-forms' ) ); 
  1356.  
  1357. self::create_admin_notice( __( 'Help us improve Caldera Forms & Get 10% Off At CalderaWP.com', 'caldera-forms' ), $message, false ); 
  1358. }else{ 
  1359. $notices = self::get_admin_alerts(); 
  1360. if( ! empty( $notices ) ) { 
  1361. shuffle( $notices ); 
  1362. $notice = $notices[0]; 
  1363.  
  1364. if( is_array( $notice ) && isset( $notice[ 'title' ], $notice[ 'content' ] ) ) { 
  1365. unset( $notices[0]); 
  1366. update_option( '_cf_admin_alerts', $notices ); 
  1367. self::create_admin_notice( $notice[ 'title' ], $notice[ 'content' ] ); 
  1368.  
  1369.  
  1370.  
  1371.  
  1372. /** 
  1373. * Create an admin notice 
  1374. * @since 1.3.4 
  1375. * @param $title 
  1376. * @param $content 
  1377. */ 
  1378. public static function create_admin_notice( $title, $content, $sanitize = true ) { 
  1379. if( $sanitize ) { 
  1380. $content = wp_kses( $content, wp_kses_allowed_html( 'post' ) ); 
  1381. ?> 
  1382. <div 
  1383. class="ajax-trigger" 
  1384. data-modal="cf-admin-notice" 
  1385. data-modal-title="<?php echo esc_html( $title ); ?>" 
  1386. data-template="#<?php echo esc_attr( sanitize_key( 'admin-modal' . $title ) ); ?>" 
  1387. data-modal-height="300" 
  1388. data-modal-width="650" 
  1389. data-autoload="true" 
  1390. </div> 
  1391. <script type="text/html" id="<?php echo esc_attr( sanitize_key('admin-modal' . $title ) ); ?>"> 
  1392. <?php echo $content; ?> 
  1393. </script> 
  1394. <?php 
  1395.  
  1396. /** 
  1397. * Get any admin alert/notices from remote API 
  1398. * @since 1.3.4 
  1399. * @return array|mixed|void 
  1400. */ 
  1401. public static function get_admin_alerts() { 
  1402. $notices = get_option( '_cf_admin_alerts', array() ); 
  1403. if( ! is_array( $notices) ) { 
  1404. $notices = array(); 
  1405.  
  1406.  
  1407. $day_ago = time() - DAY_IN_SECONDS; 
  1408. $last_check = get_option( '_cf_last_alert_check', false ); 
  1409.  
  1410. if( false === $last_check || $day_ago > $last_check ) { 
  1411. $url = Caldera_Forms_Tracking::api_url( 'notices' ); 
  1412. $r_notices = Caldera_Forms_Tracking::send_to_api( $url ); 
  1413. if ( ! empty( $r_notices ) ) { 
  1414. if( is_string( $r_notices ) ) { 
  1415. $r_notices = json_decode( $r_notices ); 
  1416.  
  1417. if( is_object( $r_notices ) ) { 
  1418. $r_notices = (array) $r_notices; 
  1419.  
  1420. if ( is_array( $r_notices ) ) { 
  1421. $notices = array_merge($notices, $r_notices); 
  1422. update_option('_cf_admin_alerts', $notices); 
  1423.  
  1424.  
  1425. update_option( '_cf_last_alert_check', time() ); 
  1426.  
  1427.  
  1428.  
  1429. return $notices; 
  1430.  
  1431.  
  1432. /** 
  1433. * Watch for tracking optin change and update if needed 
  1434. * @uses "admin_init" 
  1435. * @since 1.3.5 
  1436. */ 
  1437. public static function watch_tracking() { 
  1438. if( isset( $_GET[ 'cal_tracking' ], $_GET[ 'cal_tracking_nonce' ] ) ) { 
  1439. if( wp_verify_nonce( $_GET[ 'cal_tracking_nonce' ] ) ) { 
  1440. $value = $_GET[ 'cal_tracking' ]; 
  1441. if( is_numeric( $value ) ) { 
  1442.  
  1443. update_option( '_caldera_forms_tracking_allowed', absint( $value ) ); 
  1444. if( 1 == $value ) { 
  1445. $response = wp_remote_get( add_query_arg( 'cf-optin-email', urlencode( get_option( 'admin_email') ), 'http://CalderaWP.com/' ) ); 
  1446. /** 
  1447. * Runs after tracking optin is sent to CalderaWP.com 
  1448. * @since 1.3.5 
  1449. * @param array|WP_Error Response data or WP_Error 
  1450. */ 
  1451. add_action( 'caldera_form_after_tracking_optin', $response ); 
  1452. }elseif( 'dismiss' == trim( $value ) ) { 
  1453. update_option( '_caldera_forms_tracking_allowed', trim( $value ) ); 
  1454.  
  1455. /** 
  1456. * Add Easy Pods as an auto-populate option in admin 
  1457. * @since 1.4.3 
  1458. * @uses "caldera_forms_prerender_edit" action 
  1459. */ 
  1460. public static function easy_pods_auto_populate() { 
  1461. if( version_compare( phpversion(), '5.3.0', '>=' ) ) { 
  1462. if( class_exists( 'Caldera_Easy_Pods' ) ) { 
  1463. new Caldera_Forms_Admin_APEasyPods; 
  1464.  
  1465. if( defined( 'CAEQ_PATH' ) ) { 
  1466. new Caldera_Forms_Admin_APEasyQueries; 
  1467.  
  1468.  
  1469. /** 
  1470. * Remove hooks for admin notices while in Caldera Forms admin 
  1471. * Caldera Forms admin does not play nice with admin notices, so we use a series of steps to remove most of them, sadly can not beat them all. 
  1472. * @since 1.4.7 
  1473. * @uses "admin_head" action 
  1474. */ 
  1475. public static function remove_notice_actions() { 
  1476. remove_all_actions( 'admin_notices' ); 
  1477. remove_all_actions( 'network_admin_notices' ); 
  1478. remove_all_actions( 'user_admin_notices' ); 
  1479. remove_all_actions( 'all_admin_notices' ); 
  1480.  
  1481. /** 
  1482. * Check if is a Caldera Forms page 
  1483. * @since 1.5.0.9 
  1484. * @param null|string $page Optional. Pass page name (get var) for sub page 
  1485. * @return bool 
  1486. */ 
  1487. public static function is_page( $page = null ) { 
  1488. if( is_admin() && isset( $_GET[ 'page' ] ) ) { 
  1489. if( is_null( $page ) ) { 
  1490. return Caldera_Forms::PLUGIN_SLUG == $_GET[ 'page' ]; 
  1491. }elseif ( is_string( $page ) ) { 
  1492. return $page == $_GET[ 'page' ]; 
  1493.  
  1494. return false; 
  1495.  
  1496. /** 
  1497. * Check if is form editor page 
  1498. * @since 1.5.0.9 
  1499. * @return bool 
  1500. */ 
  1501. public static function is_edit() { 
  1502. return Caldera_Forms_Admin::is_page() && isset( $_GET[ 'edit' ] ); 
  1503.  
  1504.  
  1505. /** 
  1506. * Check if is main admin page 
  1507. * @since 1.5.0.9 
  1508. * @return bool 
  1509. */ 
  1510. public static function is_main_page() { 
  1511. return Caldera_Forms_Admin::is_page() && ! isset( $_GET[ 'edit' ] ); 
  1512.  
  1513.