PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 1.07.11
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v1.07.11
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / controllers / FrmFormsController.php

FrmFormsController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 1.07.11, at classes/controllers/FrmFormsController.php

816 lines 32.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Formidable
4 */
5
6 if(!defined('ABSPATH')) die('You are not allowed to call this page directly.');
7
8 if(class_exists('FrmFormsController'))
9 return;
10
11 class FrmFormsController{
12 public static function load_hooks(){
13 add_action('admin_menu', 'FrmFormsController::menu', 10);
14 add_action('admin_menu', 'FrmFormsController::mid_menu', 40);
15 add_action('admin_head-toplevel_page_formidable', 'FrmFormsController::head');
16 add_action('widgets_init', 'FrmFormsController::register_widgets');
17 add_action('wp_ajax_frm_form_key_in_place_edit', 'FrmFormsController::edit_key');
18 add_action('wp_ajax_frm_form_desc_in_place_edit', 'FrmFormsController::edit_description');
19 add_action('wp_ajax_frm_delete_form_wo_fields', 'FrmFormsController::destroy_wo_fields');
20 add_action('frm_after_duplicate_form', 'FrmFormsController::after_duplicate', 10, 2);
21 add_action('wp_ajax_frm_save_form', 'FrmFormsController::route');
22 add_filter('frm_submit_button', 'FrmFormsController::submit_button_label');
23 add_filter('media_buttons_context', 'FrmFormsController::insert_form_button');
24 //add_action('media_buttons', 'FrmFormsController::show_form_button', 20);
25 add_action('admin_footer', 'FrmFormsController::insert_form_popup');
26
27 add_filter('set-screen-option', 'FrmFormsController::save_per_page', 10, 3);
28
29 add_action('wp_ajax_frm_forms_preview', 'FrmFormsController::preview');
30 add_action('wp_ajax_nopriv_frm_forms_preview', 'FrmFormsController::preview');
31
32 add_filter('frm_admin_list_form_action', 'FrmFormsController::process_bulk_form_actions');
33
34 add_action( 'admin_bar_init', 'FrmFormsController::admin_bar_css' );
35 add_action( 'wp_before_admin_bar_render', 'FrmFormsController::admin_bar_configure' );
36
37 //Shortcodes
38 add_shortcode('formidable', 'FrmFormsController::get_form_shortcode');
39 add_filter( 'widget_text', 'FrmFormsController::widget_text_filter', 9 );
40 }
41
42 public static function menu(){
43 global $frm_settings;
44 add_submenu_page('formidable', $frm_settings->menu .' | '. __('Forms', 'formidable'), __('Forms', 'formidable'), 'frm_view_forms', 'formidable', 'FrmFormsController::route');
45
46 add_action('admin_head-'. sanitize_title($frm_settings->menu) .'_page_formidable-templates', 'FrmFormsController::head');
47
48 add_filter('manage_toplevel_page_formidable_columns', 'FrmFormsController::get_columns', 0 );
49 add_filter('manage_'. sanitize_title($frm_settings->menu) .'_page_formidable-templates_columns', 'FrmFormsController::get_columns', 0 );
50 add_filter('manage_toplevel_page_formidable_sortable_columns', 'FrmFormsController::get_sortable_columns');
51 add_filter('manage_'. sanitize_title($frm_settings->menu) .'_page_formidable-templates_sortable_columns', 'FrmFormsController::get_sortable_columns');
52 add_filter('get_user_option_managetoplevel_page_formidablecolumnshidden', 'FrmFormsController::hidden_columns');
53 add_filter('get_user_option_manage'. sanitize_title($frm_settings->menu) .'_page_formidable-templatescolumnshidden', 'FrmFormsController::hidden_columns');
54 }
55
56 public static function mid_menu(){
57 global $frm_settings;
58 add_submenu_page('formidable', $frm_settings->menu .' | '. __('Templates', 'formidable'), __('Templates', 'formidable'), 'frm_view_forms', 'formidable-templates', 'FrmFormsController::template_list');
59 }
60
61 public static function head(){
62 global $frm_settings;
63
64 wp_enqueue_script('formidable-editinplace');
65 wp_enqueue_script('jquery-frm-themepicker');
66 }
67
68 public static function register_widgets(){
69 require_once(FrmAppHelper::plugin_path() . '/classes/widgets/FrmShowForm.php');
70 register_widget('FrmShowForm');
71 }
72
73 public static function list_form(){
74 $params = self::get_params();
75 $errors = apply_filters('frm_admin_list_form_action', array());
76 return self::display_forms_list($params, '', false, false, $errors);
77 }
78
79 public static function template_list(){
80 $_POST['template'] = 1;
81 $errors = apply_filters('frm_admin_list_form_action', array());
82 return self::display_forms_list();
83 }
84
85 public static function new_form($values=false){
86 global $frm_vars;
87
88 $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action';
89 $action = ($values) ? $values[$action] : FrmAppHelper::get_param($action);
90 $frm_form = new FrmForm();
91
92 if ($action == 'create'){
93 return self::create($values);
94 }else if ($action == 'new'){
95 $frm_field_selection = FrmFieldsHelper::field_selection();
96 $values = FrmFormsHelper::setup_new_vars($values);
97 $id = $frm_form->create( $values );
98 $form = $frm_form->getOne($id);
99 $values['id'] = $id;
100 require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/new.php');
101 }else{
102 $all_templates = $frm_form->getAll(array('is_template' => 1), 'name');
103 require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/new-selection.php');
104 }
105 }
106
107 public static function create($values=false){
108 global $frm_entry, $frm_field, $frm_vars;
109 if(!$values)
110 $values = $_POST;
111
112 if ( ! current_user_can('frm_edit_forms') || ( $_POST && (!isset($values['frm_save_form']) || !wp_verify_nonce($values['frm_save_form'], 'frm_save_form_nonce'))) ) {
113 global $frm_settings;
114 $errors['form'] = $frm_settings->admin_permission;
115 }
116
117 $id = isset($values['id']) ? (int)$values['id'] : (int)FrmAppHelper::get_param('id');
118
119 $frm_form = new FrmForm();
120 $errors = $frm_form->validate($values);
121
122 if( count($errors) > 0 ){
123 $hide_preview = true;
124 $frm_field_selection = FrmFieldsHelper::field_selection();
125 $form = $frm_form->getOne( $id );
126 $fields = $frm_field->getAll(array('fi.form_id' => $id), 'field_order');
127 $values = FrmAppHelper::setup_edit_vars($form, 'forms', $fields, true);
128 require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/new.php');
129 }else{
130 $form = $frm_form->update( $id, $values, true );
131 die(FrmAppHelper::js_redirect(admin_url('admin.php?page=formidable&frm_action=settings&id='. $id)));
132 //$message = __('Form was Successfully Created', 'formidable');
133 //return self::settings($record, $message);
134 }
135 }
136
137 public static function edit($values=false){
138 if ( ! current_user_can('frm_edit_forms') ) {
139 global $frm_settings;
140 wp_die($frm_settings->admin_permission);
141 }
142
143 $id = isset($values['id']) ? (int)$values['id'] : (int)FrmAppHelper::get_param('id');
144 return self::get_edit_vars($id);
145 }
146
147 public static function settings($id=false, $message=''){
148 if(!$id or !is_numeric($id))
149 $id = isset($values['id']) ? (int)$values['id'] : (int)FrmAppHelper::get_param('id');
150 return self::get_settings_vars($id, '', $message);
151 }
152
153 public static function update_settings(){
154 $id = FrmAppHelper::get_param('id');
155
156 $frm_form = new FrmForm();
157 $errors = $frm_form->validate($_POST);
158
159 if( count($errors) > 0 ){
160 return self::get_settings_vars($id, $errors);
161 }else{
162 $record = $frm_form->update( $_POST['id'], $_POST );
163 $message = __('Settings Successfully Updated', 'formidable');
164 return self::get_settings_vars($id, '', $message);
165 }
166 }
167
168 public static function edit_key(){
169 if ( ! current_user_can('frm_edit_forms') ) {
170 die();
171 }
172
173 global $wpdb;
174 $values = array('form_key' => trim($_POST['update_value']));
175 $frm_form = new FrmForm();
176 $form = $frm_form->update($_POST['form_id'], $values);
177 $key = $wpdb->get_var($wpdb->prepare("SELECT form_key FROM {$wpdb->prefix}frm_forms WHERE id=%d", $_POST['form_id']));
178 echo stripslashes($key);
179 die();
180 }
181
182 public static function edit_description(){
183 if ( ! current_user_can('frm_edit_forms') ) {
184 die();
185 }
186
187 $frm_form = new FrmForm();
188 $form = $frm_form->update($_POST['form_id'], array('description' => $_POST['update_value']));
189 $description = stripslashes($_POST['update_value']);
190 if(apply_filters('frm_use_wpautop', true))
191 $description = wpautop(str_replace( '<br>', '<br />', $description));
192 echo $description;
193 die();
194 }
195
196 public static function update($values=false){
197 $frm_form = new FrmForm();
198
199 if(!$values)
200 $values = $_POST;
201
202 $errors = $frm_form->validate($values);
203
204 if ( ! current_user_can('frm_edit_forms') || ( $_POST && (!isset($values['frm_save_form']) || !wp_verify_nonce($values['frm_save_form'], 'frm_save_form_nonce')) ) ) {
205 global $frm_settings;
206 $errors['form'] = $frm_settings->admin_permission;
207 }
208
209 $id = isset($values['id']) ? (int)$values['id'] : (int)FrmAppHelper::get_param('id');
210
211 if( count($errors) > 0 ){
212 return self::get_edit_vars($id, $errors);
213 }else{
214 $record = $frm_form->update( $id, $values );
215 $message = __('Form was Successfully Updated', 'formidable');
216 if ( defined('DOING_AJAX') ) {
217 die($message);
218 }
219 return self::get_edit_vars($id, '', $message);
220 }
221 }
222
223 public static function duplicate(){
224 if ( ! current_user_can('frm_edit_forms') ) {
225 global $frm_settings;
226 wp_die($frm_settings->admin_permission);
227 }
228
229 $frm_form = new FrmForm();
230
231 $params = self::get_params();
232 $form = $frm_form->duplicate( $params['id'], $params['template'], true );
233 $message = ($params['template']) ? __('Form template was Successfully Created', 'formidable') : __('Form was Successfully Copied', 'formidable');
234 if ( $form ) {
235 return self::get_edit_vars($form, '', $message, true);
236 } else {
237 return self::display_forms_list($params, __('There was a problem creating new template.', 'formidable'));
238 }
239 }
240
241
242 public static function after_duplicate($form_id, $values) {
243 $frm_form = new FrmForm();
244 $frm_form->after_duplicate($form_id, $values);
245 }
246
247 public static function page_preview(){
248 $params = self::get_params();
249 if (!$params['form']) return;
250
251 $frm_form = new FrmForm();
252 $form = $frm_form->getOne($params['form']);
253 if(!$form) return;
254 return self::show_form($form->id, '', true, true);
255 }
256
257 public static function preview(){
258 do_action('frm_wp');
259
260 global $frm_settings, $frm_vars;
261 $frm_vars['preview'] = true;
262
263 $frm_form = new FrmForm();
264 if ( !defined( 'ABSPATH' ) && !defined( 'XMLRPC_REQUEST' )) {
265 global $wp;
266 $root = dirname(dirname(dirname(dirname(__FILE__))));
267 include_once( $root.'/wp-config.php' );
268 $wp->init();
269 $wp->register_globals();
270 }
271
272 if($frm_vars['pro_is_installed'])
273 FrmProEntriesController::register_scripts();
274
275 header("Content-Type: text/html; charset=". get_option( 'blog_charset' ));
276
277 $plugin = FrmAppHelper::get_param('plugin');
278 $controller = FrmAppHelper::get_param('controller');
279 $key = (isset($_GET['form']) ? $_GET['form'] : (isset($_POST['form']) ? $_POST['form'] : ''));
280 $form = $frm_form->getAll(array('form_key' => $key), '', 1);
281 if (!$form) $form = $frm_form->getAll('', '', 1);
282
283 require(FrmAppHelper::plugin_path() .'/classes/views/frm-entries/direct.php');
284 die();
285 }
286
287 public static function destroy(){
288 if(!current_user_can('frm_delete_forms')){
289 global $frm_settings;
290 wp_die($frm_settings->admin_permission);
291 }
292
293 $frm_form = new FrmForm();
294 $params = self::get_params();
295 $message = '';
296 if ($frm_form->destroy( $params['id'] ))
297 $message = __('Form was Successfully Deleted', 'formidable');
298 self::display_forms_list($params, $message, '', 1);
299 }
300
301 public static function destroy_wo_fields(){
302 global $frm_field, $frmdb, $wpdb;
303 $id = $_POST['form_id'];
304 if ($frmdb->get_count($wpdb->prefix . 'frm_fields', array('form_id' => $id)) <= 0){
305 $frm_form = new FrmForm();
306 $frm_form->destroy($id);
307 }
308 die();
309 }
310
311 public static function submit_button_label($submit){
312 if (!$submit or empty($submit)){
313 global $frm_settings;
314 $submit = $frm_settings->submit_value;
315 }
316 return $submit;
317 }
318
319 public static function insert_form_button($content){
320 if(current_user_can('frm_view_forms'))
321 $content .= '<a href="#TB_inline?width=450&height=550&inlineId=frm_insert_form" class="thickbox button add_media frm_insert_form" title="' . __("Add Formidable Form", 'formidable') . '"><span class="frm-buttons-icon wp-media-buttons-icon"></span> '. __('Add Form', 'formidable') . '</a>';
322 return $content;
323 }
324
325 public static function show_form_button($id){
326 if($id != 'content')
327 return;
328 echo '<a href="#TB_inline?width=450&height=550&inlineId=frm_insert_form" class="thickbox" title="' . __("Add Formidable Form", 'formidable') . '"><img src="'. esc_url(FrmAppHelper::plugin_url() .'/images/form_16.png') .'" alt="' . __("Add Formidable Form", 'formidable') . '" /></a>';
329 }
330
331 public static function insert_form_popup(){
332 $page = basename($_SERVER['PHP_SELF']);
333 if(in_array($page, array('post.php', 'page.php', 'page-new.php', 'post-new.php')) or (isset($_GET) and isset($_GET['page']) and $_GET['page'] == 'formidable-entry-templates')){
334 if(class_exists('FrmProDisplay')){
335 global $frmpro_display;
336 $displays = $frmpro_display->getAll('', 'post_title');
337 }
338 require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/insert_form_popup.php');
339 }
340 }
341
342 public static function display_forms_list($params=false, $message='', $page_params_ov = false, $current_page_ov = false, $errors = array()){
343 global $wpdb, $frmdb, $frm_entry, $frm_vars;
344
345 if(!$params)
346 $params = self::get_params();
347
348 $page_params = '&action=0&&frm_action=0&page=formidable';
349
350 $frm_form = new FrmForm();
351 if ($params['template']){
352 $default_templates = $frm_form->getAll(array('default_template' => 1));
353 $all_templates = $frm_form->getAll(array('is_template' => 1), 'name');
354 }
355
356 require( FrmAppHelper::plugin_path() .'/classes/helpers/FrmListHelper.php' );
357
358 $args = array('table_name' => $wpdb->prefix .'frm_forms', 'params' => $params);
359 $args['page_name'] = $params['template'] ? '-template' : '';
360 $wp_list_table = new FrmListHelper($args);
361 unset($args);
362
363 $pagenum = $wp_list_table->get_pagenum();
364
365 $wp_list_table->prepare_items();
366
367 $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' );
368 if ( $pagenum > $total_pages && $total_pages > 0 ) {
369 wp_redirect( add_query_arg( 'paged', $total_pages ) );
370 die();
371 }
372
373 if ( ! empty( $_REQUEST['s'] ) )
374 $page_params .= '&s='. urlencode($_REQUEST['s']);
375
376 require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/list.php');
377 }
378
379 public static function get_columns($columns){
380 $columns['cb'] = '<input type="checkbox" />';
381 $columns['id'] = 'ID';
382 $columns['name'] = __('Name');
383 $columns['description'] = __('Description');
384 $columns['form_key'] = __('Key', 'formidable');
385
386 if($_GET['page'] == 'formidable-templates'){
387 add_screen_option( 'per_page', array('label' => __('Templates', 'formidable'), 'default' => 10, 'option' => 'formidable_page_formidable_templates_per_page') );
388 }else{
389 $columns['entries'] = __('Entries', 'formidable');
390 $columns['link'] = __('Actions', 'formidable');
391 $columns['shortcode'] = __('Shortcodes', 'formidable');
392 add_screen_option( 'per_page', array('label' => __('Forms', 'formidable'), 'default' => 20, 'option' => 'formidable_page_formidable_per_page') );
393 }
394
395 $columns['created_at'] = __('Date', 'formidable');
396
397 return $columns;
398 }
399
400 public static function get_sortable_columns() {
401 return array(
402 'id' => 'id',
403 'name' => 'name',
404 'description' => 'description',
405 'form_key' => 'form_key',
406 'created_at' => 'created_at'
407 );
408 }
409
410 public static function hidden_columns($result){
411 $return = false;
412 foreach((array)$result as $r){
413 if(!empty($r)){
414 $return = true;
415 break;
416 }
417 }
418
419 if($return)
420 return $result;
421
422 $result[] = 'created_at';
423 if($_GET['page'] == 'formidable-templates'){
424 $result[] = 'id';
425 $result[] = 'form_key';
426 }
427
428 return $result;
429 }
430
431 public static function save_per_page($save, $option, $value){
432 if($option == 'formidable_page_formidable_per_page' or $option == 'formidable_page_formidable_templates_per_page')
433 $save = (int)$value;
434 return $save;
435 }
436
437 private static function get_edit_vars($id, $errors = '', $message='', $create_link=false){
438 global $frm_entry, $frm_field, $frm_vars;
439 $frm_form = new FrmForm();
440 $form = $frm_form->getOne( $id );
441 if ( !$form ) {
442 wp_die( __('You are trying to edit a form that does not exist.', 'formidable') );
443 }
444
445 $frm_field_selection = FrmFieldsHelper::field_selection();
446 $fields = $frm_field->getAll(array('fi.form_id' => $form->id), 'field_order');
447 $values = FrmAppHelper::setup_edit_vars($form, 'forms', $fields, true);
448
449 $edit_message = __('Form was Successfully Updated', 'formidable');
450 if ( $form->is_template && $message == $edit_message ) {
451 $message = __('Template was Successfully Updated', 'formidable');
452 }
453
454 if ( $form->default_template ) {
455 wp_die(__('That template cannot be edited', 'formidable'));
456 } else if ( defined('DOING_AJAX') ) {
457 die();
458 } else if ( $create_link ) {
459 require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/new.php');
460 } else {
461 require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/edit.php');
462 }
463 }
464
465 public static function get_settings_vars($id, $errors = '', $message=''){
466 global $frm_entry, $frm_field, $frm_vars;
467 $frm_form = new FrmForm();
468 $form = $frm_form->getOne( $id );
469 $fields = $frm_field->getAll(array('fi.form_id' => $id), 'field_order');
470 $values = FrmAppHelper::setup_edit_vars($form, 'forms', $fields, true);
471 $sections = apply_filters('frm_add_form_settings_section', array(), $values);
472 $pro_feature = $frm_vars['pro_is_installed'] ? '' : ' class="pro_feature"';
473 if (isset($values['default_template']) && $values['default_template'])
474 wp_die(__('That template cannot be edited', 'formidable'));
475 else
476 require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/settings.php');
477 }
478
479 public static function get_params(){
480 $values = array();
481 foreach (array('template' => 0, 'id' => '', 'paged' => 1, 'form' => '', 'search' => '', 'sort' => '', 'sdir' => '') as $var => $default)
482 $values[$var] = FrmAppHelper::get_param($var, $default);
483
484 return $values;
485 }
486
487 public static function process_bulk_form_actions($errors) {
488 if ( !isset($_POST) ) return;
489
490 $bulkaction = FrmAppHelper::get_param('action');
491 if ( $bulkaction == -1 ) {
492 $bulkaction = FrmAppHelper::get_param('action2');
493 }
494
495 if ( !empty($bulkaction) && strpos($bulkaction, 'bulk_') === 0 ) {
496 if ( isset($_GET) && isset($_GET['action']) ) {
497 $_SERVER['REQUEST_URI'] = str_replace('&action=' .$_GET['action'], '', $_SERVER['REQUEST_URI']);
498 }
499 if ( isset($_GET) && isset($_GET['action2']) ) {
500 $_SERVER['REQUEST_URI'] = str_replace('&action=' .$_GET['action2'], '', $_SERVER['REQUEST_URI']);
501 }
502
503 $bulkaction = str_replace('bulk_', '', $bulkaction);
504 } else {
505 $bulkaction = '-1';
506 if(isset($_POST['bulkaction']) && $_POST['bulkaction'] != '-1') {
507 $bulkaction = $_POST['bulkaction'];
508 } else if(isset($_POST['bulkaction2']) && $_POST['bulkaction2'] != '-1') {
509 $bulkaction = $_POST['bulkaction2'];
510 }
511 }
512
513 $ids = FrmAppHelper::get_param('item-action', '');
514 if ( empty($ids) ) {
515 $errors[] = __('No forms were specified', 'formidable');
516 } else {
517 if ( $bulkaction == 'delete' ) {
518 if ( !current_user_can('frm_delete_forms') ) {
519 global $frm_settings;
520 $errors[] = $frm_settings->admin_permission;
521 } else {
522 if ( !is_array($ids) ) {
523 $ids = explode(',', $ids);
524 }
525
526 if ( is_array($ids) ) {
527 if ( $bulkaction == 'delete' ) {
528 $frm_form = new FrmForm();
529 foreach ( $ids as $form_id ) {
530 $frm_form->destroy($form_id);
531 }
532 }
533 }
534 }
535 }
536 }
537 return $errors;
538 }
539
540 public static function add_default_templates($path, $default=true, $template=true){
541 _deprecated_function( __FUNCTION__, '1.07.05', 'FrmXMLController::add_default_templates()' );
542
543 global $frm_field;
544 $path = untrailingslashit(trim($path));
545 $templates = glob($path."/*.php");
546
547 $frm_form = new FrmForm();
548 for($i = count($templates) - 1; $i >= 0; $i--){
549 $filename = str_replace('.php', '', str_replace($path.'/', '', $templates[$i]));
550 $template_query = array('form_key' => $filename);
551 if($template) $template_query['is_template'] = 1;
552 if($default) $template_query['default_template'] = 1;
553 $form = $frm_form->getAll($template_query, '', 1);
554
555 $values = FrmFormsHelper::setup_new_vars();
556 $values['form_key'] = $filename;
557 $values['is_template'] = $template;
558 $values['status'] = 'published';
559 if($default) $values['default_template'] = 1;
560
561 include($templates[$i]);
562
563 //get updated form
564 if ( isset($form) && $form ) {
565 $form = $frm_form->getOne($form->id);
566 } else {
567 $form = $frm_form->getAll($template_query, '', 1);
568 }
569
570 if($form)
571 do_action('frm_after_duplicate_form', $form->id, (array)$form);
572 }
573 }
574
575 public static function route(){
576 $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action';
577 $vars = false;
578 if(isset($_POST['frm_compact_fields'])){
579 if ( !current_user_can('frm_edit_forms') && !current_user_can('administrator') ) {
580 global $frm_settings;
581 wp_die($frm_settings->admin_permission);
582 }
583 $json_vars = htmlspecialchars_decode(nl2br(stripslashes(str_replace('&quot;', '\\\"', $_POST['frm_compact_fields'] ))));
584 $json_vars = json_decode($json_vars, true);
585 if ( empty($json_vars) ) {
586 // json decoding failed so we should return an error message
587 $action = FrmAppHelper::get_param($action);
588 if ( 'edit' == $action ) {
589 $action = 'update';
590 }
591
592 add_filter('frm_validate_form', array(__CLASS__, 'json_error'));
593 } else {
594 $vars = FrmAppHelper::json_to_array($json_vars);
595 $action = $vars[$action];
596 }
597 }else{
598 $action = FrmAppHelper::get_param($action);
599 }
600
601 if($action == 'new' or $action == 'new-selection')
602 return self::new_form($vars);
603 else if($action == 'create')
604 return self::create($vars);
605 else if($action == 'edit')
606 return self::edit($vars);
607 else if($action == 'update')
608 return self::update($vars);
609 else if($action == 'duplicate')
610 return self::duplicate();
611 else if($action == 'destroy')
612 return self::destroy();
613 else if($action == 'list-form')
614 return self::list_form();
615 else if($action == 'settings')
616 return self::settings();
617 else if($action == 'update_settings')
618 return self::update_settings();
619 else{
620 do_action('frm_form_action_'. $action);
621 if(apply_filters('frm_form_stop_action_'. $action, false))
622 return;
623
624 $action = FrmAppHelper::get_param('action');
625 if($action == -1)
626 $action = FrmAppHelper::get_param('action2');
627
628 if(strpos($action, 'bulk_') === 0){
629 if(isset($_GET) and isset($_GET['action']))
630 $_SERVER['REQUEST_URI'] = str_replace('&action='.$_GET['action'], '', $_SERVER['REQUEST_URI']);
631 if(isset($_GET) and isset($_GET['action2']))
632 $_SERVER['REQUEST_URI'] = str_replace('&action='.$_GET['action2'], '', $_SERVER['REQUEST_URI']);
633
634 return self::list_form();
635 }else{
636 return self::display_forms_list();
637 }
638 }
639 }
640
641 public static function json_error($errors) {
642 $errors['json'] = __('Abnormal HTML characters prevented your form from saving correctly', 'formidable');
643 return $errors;
644 }
645
646
647 /* FRONT-END FORMS */
648 public static function admin_bar_css() {
649 FrmAppController::load_wp_admin_style();
650 }
651
652 public static function admin_bar_configure() {
653 if ( is_admin() || !current_user_can('frm_edit_forms') ) {
654 return;
655 }
656
657 global $frm_vars;
658 if ( empty($frm_vars['forms_loaded']) ) {
659 return;
660 }
661
662 $actions = array();
663 foreach ($frm_vars['forms_loaded'] as $form ) {
664 if ( is_object($form) ) {
665 $actions[$form->id] = $form->name;
666 }
667 unset($form);
668 }
669
670 if ( empty($actions) ) {
671 return;
672 }
673
674 asort($actions);
675
676 global $wp_admin_bar;
677
678 if ( count($actions) == 1 ) {
679 $wp_admin_bar->add_menu( array(
680 'title' => 'Edit Form',
681 'href' => admin_url('admin.php?page=formidable&frm_action=edit&id='. current( array_keys( $actions ) )),
682 'id' => 'frm-forms'
683 ) );
684 } else {
685 $wp_admin_bar->add_menu( array(
686 'id' => 'frm-forms',
687 'title' => '<span class="ab-icon"></span><span class="ab-label">' . __( 'Edit Forms', 'formidable' ) . '</span>',
688 'href' => admin_url( 'admin.php?page=formidable&frm_action=edit&id='. current( array_keys( $actions ) ) ),
689 'meta' => array(
690 'title' => __( 'Edit Forms', 'formidable' ),
691 ),
692 ) );
693
694 foreach ( $actions as $form_id => $name ) {
695
696 $wp_admin_bar->add_menu( array(
697 'parent' => 'frm-forms',
698 'id' => 'edit_form_'. $form_id,
699 'title' => empty($name) ? __('(no title)') : $name,
700 'href' => admin_url( 'admin.php?page=formidable&frm_action=edit&id='. $form_id )
701 ) );
702 }
703 }
704 }
705
706
707
708 //formidable shortcode
709 public static function get_form_shortcode($atts) {
710 global $frm_vars;
711 if ( isset($frm_vars['skip_shortcode']) && $frm_vars['skip_shortcode'] ) {
712 $sc = '[formidable';
713 foreach ( $atts as $k => $v ) {
714 $sc .= ' '. $k .'="'. $v .'"';
715 }
716 return $sc .']';
717 }
718
719 $shortcode_atts = shortcode_atts(array(
720 'id' => '', 'key' => '', 'title' => false, 'description' => false,
721 'readonly' => false, 'entry_id' => false, 'fields' => array(),
722 'exclude_fields' => array(), 'minimize' => false,
723 ), $atts);
724 do_action('formidable_shortcode_atts', $shortcode_atts, $atts);
725 extract($shortcode_atts);
726 return self::show_form($id, $key, $title, $description, $atts);
727 }
728
729 //filter form shortcode in text widgets
730 public static function widget_text_filter( $content ) {
731 $regex = '/\[\s*formidable\s+.*\]/';
732 return preg_replace_callback( $regex, 'FrmAppController::widget_text_filter_callback', $content );
733 }
734
735 public static function show_form($id = '', $key = '', $title = false, $description = false, $atts = array()) {
736 global $frm_settings, $post;
737
738 $frm_form = new FrmForm();
739 if ( empty($id) ) {
740 $id = $key;
741 }
742
743 // no form id or key set
744 if ( empty($id) ) {
745 return __('Please select a valid form', 'formidable');
746 }
747
748 $form = $frm_form->getOne($id);
749 if ( !$form ) {
750 return __('Please select a valid form', 'formidable');
751 }
752 $form = apply_filters('frm_pre_display_form', $form);
753
754 // don't show a draft form on a page
755 if ( $form->status == 'draft' && (!$post || $post->ID != $frm_settings->preview_page_id) ) {
756 return __('Please select a valid form', 'formidable');
757 }
758
759 // don't show the form if user should be logged in
760 if ( $form->logged_in && !is_user_logged_in() ) {
761 return do_shortcode($frm_settings->login_msg);
762 }
763
764 // don't show the form if user doesn't have permission
765 if ( $form->logged_in && get_current_user_id() && isset($form->options['logged_in_role']) && $form->options['logged_in_role'] != '' && !FrmAppHelper::user_has_permission($form->options['logged_in_role']) ) {
766 return do_shortcode($frm_settings->login_msg);
767 }
768
769 $form = self::get_form($form, $title, $description, $atts);
770
771 // check for external shortcodes
772 $form = do_shortcode($form);
773
774 return $form;
775 }
776
777 public static function get_form($form, $title, $description, $atts = array()) {
778 global $frm_field, $frm_entry, $frm_entry_meta, $frm_settings, $frm_vars;
779 $form_name = $form->name;
780
781 $frm_form = new FrmForm();
782 $submit = isset($form->options['submit_value']) ? $form->options['submit_value'] : $frm_settings->submit_value;
783 $saved_message = isset($form->options['success_msg']) ? $form->options['success_msg'] : $frm_settings->success_msg;
784
785 $user_ID = get_current_user_id();
786
787 $params = FrmEntriesController::get_params($form);
788
789 $message = $errors = '';
790
791 FrmEntriesHelper::enqueue_scripts($params);
792
793 if ( $params['posted_form_id'] == $form->id && $_POST ) {
794 $errors = isset($frm_vars['created_entries'][$form->id]) ? $frm_vars['created_entries'][$form->id]['errors'] : array();
795 }
796
797 $fields = FrmFieldsHelper::get_form_fields($form->id, (isset($errors) && !empty($errors)));
798
799 $filename = FrmAppHelper::plugin_path() .'/classes/views/frm-entries/frm-entry.php';
800
801 if ( is_file($filename) ) {
802 ob_start();
803 include $filename;
804 $contents = ob_get_contents();
805 ob_end_clean();
806 // check if minimizing is turned on
807 if ( isset($atts['minimize']) && !empty($atts['minimize']) ) {
808 $contents = str_replace(array("\r\n", "\r", "\n", "\t", " "), '', $contents);
809 }
810 return $contents;
811 }
812 return false;
813 }
814
815 }
816