Complex classes like FrmFormsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FrmFormsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmFormsController { |
||
| 4 | |||
| 5 | public static function menu() { |
||
| 6 | $menu_label = __( 'Forms', 'formidable' ); |
||
|
|
|||
| 7 | if ( ! FrmAppHelper::pro_is_installed() ) { |
||
| 8 | $menu_label .= ' (Lite)'; |
||
| 9 | } |
||
| 10 | add_submenu_page('formidable', 'Formidable | ' . $menu_label, $menu_label, 'frm_view_forms', 'formidable', 'FrmFormsController::route' ); |
||
| 11 | |||
| 12 | self::maybe_load_listing_hooks(); |
||
| 13 | } |
||
| 14 | |||
| 15 | public static function maybe_load_listing_hooks() { |
||
| 16 | $action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' ); |
||
| 17 | if ( ! empty( $action ) && ! in_array( $action, array( 'list', 'trash', 'untrash', 'destroy' ) ) ) { |
||
| 18 | return; |
||
| 19 | } |
||
| 20 | |||
| 21 | add_filter( 'get_user_option_managetoplevel_page_formidablecolumnshidden', 'FrmFormsController::hidden_columns' ); |
||
| 22 | |||
| 23 | add_filter( 'manage_toplevel_page_formidable_columns', 'FrmFormsController::get_columns', 0 ); |
||
| 24 | add_filter( 'manage_toplevel_page_formidable_sortable_columns', 'FrmFormsController::get_sortable_columns' ); |
||
| 25 | } |
||
| 26 | |||
| 27 | public static function head() { |
||
| 28 | wp_enqueue_script('formidable-editinplace'); |
||
| 29 | |||
| 30 | if ( wp_is_mobile() ) { |
||
| 31 | wp_enqueue_script( 'jquery-touch-punch' ); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | public static function register_widgets() { |
||
| 36 | require_once(FrmAppHelper::plugin_path() . '/classes/widgets/FrmShowForm.php'); |
||
| 37 | register_widget('FrmShowForm'); |
||
| 38 | } |
||
| 39 | |||
| 40 | public static function list_form() { |
||
| 41 | FrmAppHelper::permission_check('frm_view_forms'); |
||
| 42 | |||
| 43 | $params = FrmForm::list_page_params(); |
||
| 44 | $errors = self::process_bulk_form_actions( array()); |
||
| 45 | $errors = apply_filters('frm_admin_list_form_action', $errors); |
||
| 46 | |||
| 47 | return self::display_forms_list( $params, '', $errors ); |
||
| 48 | } |
||
| 49 | |||
| 50 | public static function new_form( $values = array() ) { |
||
| 51 | FrmAppHelper::permission_check('frm_edit_forms'); |
||
| 52 | |||
| 53 | global $frm_vars; |
||
| 54 | |||
| 55 | $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action'; |
||
| 56 | $action = empty( $values ) ? FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ) : $values[ $action ]; |
||
| 57 | |||
| 58 | if ( $action == 'create' ) { |
||
| 59 | self::create($values); |
||
| 60 | return; |
||
| 61 | } else if ( $action == 'new' ) { |
||
| 62 | $frm_field_selection = FrmField::field_selection(); |
||
| 63 | $values = FrmFormsHelper::setup_new_vars($values); |
||
| 64 | $id = FrmForm::create( $values ); |
||
| 65 | $form = FrmForm::getOne($id); |
||
| 66 | |||
| 67 | self::create_default_email_action( $form ); |
||
| 68 | |||
| 69 | $all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' ); |
||
| 70 | |||
| 71 | $values['id'] = $id; |
||
| 72 | require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' ); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Create the default email action |
||
| 78 | * |
||
| 79 | * @since 2.02.11 |
||
| 80 | * |
||
| 81 | * @param object $form |
||
| 82 | */ |
||
| 83 | private static function create_default_email_action( $form ) { |
||
| 84 | $create_email = apply_filters( 'frm_create_default_email_action', true, $form ); |
||
| 85 | |||
| 86 | if ( $create_email ) { |
||
| 87 | $action_control = FrmFormActionsController::get_form_actions( 'email' ); |
||
| 88 | $action_control->create( $form->id ); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | public static function create( $values = array() ) { |
||
| 93 | FrmAppHelper::permission_check('frm_edit_forms'); |
||
| 94 | |||
| 95 | global $frm_vars; |
||
| 96 | if ( empty( $values ) ) { |
||
| 97 | $values = $_POST; |
||
| 98 | } |
||
| 99 | |||
| 100 | //Set radio button and checkbox meta equal to "other" value |
||
| 101 | if ( FrmAppHelper::pro_is_installed() ) { |
||
| 102 | $values = FrmProEntry::mod_other_vals( $values, 'back' ); |
||
| 103 | } |
||
| 104 | |||
| 105 | $id = isset($values['id']) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' ); |
||
| 106 | |||
| 107 | if ( ! current_user_can( 'frm_edit_forms' ) || ( $_POST && ( ! isset( $values['frm_save_form'] ) || ! wp_verify_nonce( $values['frm_save_form'], 'frm_save_form_nonce' ) ) ) ) { |
||
| 108 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 109 | $errors = array( 'form' => $frm_settings->admin_permission ); |
||
| 110 | } else { |
||
| 111 | $errors = FrmForm::validate($values); |
||
| 112 | } |
||
| 113 | |||
| 114 | if ( count($errors) > 0 ) { |
||
| 115 | $hide_preview = true; |
||
| 116 | $frm_field_selection = FrmField::field_selection(); |
||
| 117 | $form = FrmForm::getOne( $id ); |
||
| 118 | $fields = FrmField::get_all_for_form($id); |
||
| 119 | |||
| 120 | $values = FrmAppHelper::setup_edit_vars($form, 'forms', '', true); |
||
| 121 | $values['fields'] = $fields; |
||
| 122 | $all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' ); |
||
| 123 | |||
| 124 | require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' ); |
||
| 125 | } else { |
||
| 126 | FrmForm::update( $id, $values, true ); |
||
| 127 | $url = admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . $id ); |
||
| 128 | die( FrmAppHelper::js_redirect( $url ) ); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | public static function edit( $values = false ) { |
||
| 133 | FrmAppHelper::permission_check('frm_edit_forms'); |
||
| 134 | |||
| 135 | $id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' ); |
||
| 136 | return self::get_edit_vars($id); |
||
| 137 | } |
||
| 138 | |||
| 139 | public static function settings( $id = false, $message = '' ) { |
||
| 140 | FrmAppHelper::permission_check('frm_edit_forms'); |
||
| 141 | |||
| 142 | if ( ! $id || ! is_numeric($id) ) { |
||
| 143 | $id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' ); |
||
| 144 | } |
||
| 145 | return self::get_settings_vars( $id, array(), $message ); |
||
| 146 | } |
||
| 147 | |||
| 148 | public static function update_settings() { |
||
| 149 | FrmAppHelper::permission_check('frm_edit_forms'); |
||
| 150 | |||
| 151 | $id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' ); |
||
| 152 | |||
| 153 | $errors = FrmForm::validate($_POST); |
||
| 154 | if ( count($errors) > 0 ) { |
||
| 155 | return self::get_settings_vars($id, $errors); |
||
| 156 | } |
||
| 157 | |||
| 158 | do_action('frm_before_update_form_settings', $id); |
||
| 159 | |||
| 160 | FrmForm::update( $id, $_POST ); |
||
| 161 | |||
| 162 | $message = __( 'Settings Successfully Updated', 'formidable' ); |
||
| 163 | return self::get_settings_vars( $id, array(), $message ); |
||
| 164 | } |
||
| 165 | |||
| 166 | public static function edit_key() { |
||
| 167 | _deprecated_function( __METHOD__, '3.0' ); |
||
| 168 | $values = self::edit_in_place_value( 'form_key' ); |
||
| 169 | echo wp_kses( stripslashes( FrmForm::get_key_by_id( $values['form_id'] ) ), array() ); |
||
| 170 | wp_die(); |
||
| 171 | } |
||
| 172 | |||
| 173 | public static function edit_description() { |
||
| 178 | } |
||
| 179 | |||
| 180 | private static function edit_in_place_value( $field ) { |
||
| 181 | _deprecated_function( __METHOD__, '3.0' ); |
||
| 182 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 183 | FrmAppHelper::permission_check('frm_edit_forms', 'hide'); |
||
| 184 | |||
| 185 | $form_id = FrmAppHelper::get_post_param( 'form_id', '', 'absint' ); |
||
| 186 | $value = FrmAppHelper::get_post_param( 'update_value', '', 'wp_filter_post_kses' ); |
||
| 187 | |||
| 188 | $values = array( $field => trim( $value ) ); |
||
| 189 | FrmForm::update( $form_id, $values ); |
||
| 190 | $values['form_id'] = $form_id; |
||
| 191 | |||
| 192 | return $values; |
||
| 193 | } |
||
| 194 | |||
| 195 | public static function update( $values = array() ) { |
||
| 196 | if ( empty( $values ) ) { |
||
| 197 | $values = $_POST; |
||
| 198 | } |
||
| 199 | |||
| 200 | //Set radio button and checkbox meta equal to "other" value |
||
| 201 | if ( FrmAppHelper::pro_is_installed() ) { |
||
| 202 | $values = FrmProEntry::mod_other_vals( $values, 'back' ); |
||
| 203 | } |
||
| 204 | |||
| 205 | $errors = FrmForm::validate( $values ); |
||
| 206 | $permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'frm_save_form', 'frm_save_form_nonce' ); |
||
| 207 | if ( $permission_error !== false ) { |
||
| 208 | $errors['form'] = $permission_error; |
||
| 209 | } |
||
| 210 | |||
| 211 | $id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' ); |
||
| 212 | |||
| 213 | if ( count( $errors ) > 0 ) { |
||
| 214 | return self::get_edit_vars( $id, $errors ); |
||
| 215 | } else { |
||
| 216 | FrmForm::update( $id, $values ); |
||
| 217 | $message = __( 'Form was Successfully Updated', 'formidable' ); |
||
| 218 | if ( defined( 'DOING_AJAX' ) ) { |
||
| 219 | wp_die( $message ); |
||
| 220 | } |
||
| 221 | return self::get_edit_vars( $id, array(), $message ); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Redirect to the url for creating from a template |
||
| 227 | * Also delete the current form |
||
| 228 | * @since 2.0 |
||
| 229 | */ |
||
| 230 | public static function _create_from_template() { |
||
| 231 | FrmAppHelper::permission_check('frm_edit_forms'); |
||
| 232 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 233 | |||
| 234 | $current_form = FrmAppHelper::get_param( 'this_form', '', 'get', 'absint' ); |
||
| 235 | $template_id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' ); |
||
| 236 | |||
| 237 | if ( $current_form ) { |
||
| 238 | FrmForm::destroy( $current_form ); |
||
| 239 | } |
||
| 240 | |||
| 241 | echo esc_url_raw( admin_url( 'admin.php?page=formidable&frm_action=duplicate&id=' . absint( $template_id ) ) ); |
||
| 242 | wp_die(); |
||
| 243 | } |
||
| 244 | |||
| 245 | public static function duplicate() { |
||
| 246 | FrmAppHelper::permission_check('frm_edit_forms'); |
||
| 247 | |||
| 248 | $params = FrmForm::list_page_params(); |
||
| 249 | $form = FrmForm::duplicate( $params['id'], $params['template'], true ); |
||
| 250 | $message = $params['template'] ? __( 'Form template was Successfully Created', 'formidable' ) : __( 'Form was Successfully Copied', 'formidable' ); |
||
| 251 | if ( $form ) { |
||
| 252 | return self::get_edit_vars( $form, array(), $message, true ); |
||
| 253 | } else { |
||
| 254 | return self::display_forms_list($params, __( 'There was a problem creating the new template.', 'formidable' )); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | public static function page_preview() { |
||
| 259 | $params = FrmForm::list_page_params(); |
||
| 260 | if ( ! $params['form'] ) { |
||
| 261 | return; |
||
| 262 | } |
||
| 263 | |||
| 264 | $form = FrmForm::getOne( $params['form'] ); |
||
| 265 | if ( $form ) { |
||
| 266 | return self::show_form( $form->id, '', true, true ); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @since 3.0 |
||
| 272 | */ |
||
| 273 | public static function show_page_preview() { |
||
| 274 | echo self::page_preview(); |
||
| 275 | } |
||
| 276 | |||
| 277 | public static function preview() { |
||
| 278 | do_action( 'frm_wp' ); |
||
| 279 | |||
| 280 | global $frm_vars; |
||
| 281 | $frm_vars['preview'] = true; |
||
| 282 | |||
| 283 | self::load_wp(); |
||
| 284 | |||
| 285 | $include_theme = FrmAppHelper::get_param( 'theme', '', 'get', 'absint' ); |
||
| 286 | if ( $include_theme ) { |
||
| 287 | self::set_preview_query(); |
||
| 288 | self::load_theme_preview(); |
||
| 289 | } else { |
||
| 290 | self::load_direct_preview(); |
||
| 291 | } |
||
| 292 | |||
| 293 | wp_die(); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @since 3.0 |
||
| 298 | */ |
||
| 299 | private static function load_wp() { |
||
| 300 | if ( ! defined( 'ABSPATH' ) && ! defined( 'XMLRPC_REQUEST' ) ) { |
||
| 301 | global $wp; |
||
| 302 | $root = dirname( dirname( dirname( dirname( __FILE__ ) ) ) ); |
||
| 303 | include_once( $root . '/wp-config.php' ); |
||
| 304 | $wp->init(); |
||
| 305 | $wp->register_globals(); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | private static function set_preview_query() { |
||
| 310 | $random_page = get_posts( array( |
||
| 311 | 'numberposts' => 1, |
||
| 312 | 'orderby' => 'date', |
||
| 313 | 'order' => 'ASC', |
||
| 314 | 'post_type' => 'page', |
||
| 315 | ) ); |
||
| 316 | |||
| 317 | if ( ! empty( $random_page ) ) { |
||
| 318 | $random_page = reset( $random_page ); |
||
| 319 | query_posts( array( |
||
| 320 | 'post_type' => 'page', |
||
| 321 | 'page_id' => $random_page->ID, |
||
| 322 | ) ); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @since 3.0 |
||
| 328 | */ |
||
| 329 | private static function load_theme_preview() { |
||
| 330 | add_filter( 'wp_title', 'FrmFormsController::preview_title', 9999 ); |
||
| 331 | add_filter( 'the_title', 'FrmFormsController::preview_page_title', 9999 ); |
||
| 332 | add_filter( 'the_content', 'FrmFormsController::preview_content', 9999 ); |
||
| 333 | add_action( 'loop_no_results', 'FrmFormsController::show_page_preview' ); |
||
| 334 | add_filter( 'is_active_sidebar', '__return_false' ); |
||
| 335 | get_template_part( 'page' ); |
||
| 336 | } |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * Set the page title for the theme preview page |
||
| 341 | * |
||
| 342 | * @since 3.0 |
||
| 343 | */ |
||
| 344 | public static function preview_page_title( $title ) { |
||
| 345 | if ( in_the_loop() ) { |
||
| 346 | $title = self::preview_title( $title ); |
||
| 347 | } |
||
| 348 | return $title; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Set the page title for the theme preview page |
||
| 353 | * |
||
| 354 | * @since 3.0 |
||
| 355 | */ |
||
| 356 | public static function preview_title( $title ) { |
||
| 357 | return __( 'Form Preview', 'formidable' ); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Set the page content for the theme preview page |
||
| 362 | * |
||
| 363 | * @since 3.0 |
||
| 364 | */ |
||
| 365 | public static function preview_content( $content ) { |
||
| 366 | if ( in_the_loop() ) { |
||
| 367 | $content = FrmFormsController::show_page_preview(); |
||
| 368 | } |
||
| 369 | return $content; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @since 3.0 |
||
| 374 | */ |
||
| 375 | private static function load_direct_preview() { |
||
| 376 | header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
||
| 377 | |||
| 378 | $key = FrmAppHelper::simple_get( 'form', 'sanitize_title' ); |
||
| 379 | if ( $key == '' ) { |
||
| 380 | $key = FrmAppHelper::get_post_param( 'form', '', 'sanitize_title' ); |
||
| 381 | } |
||
| 382 | |||
| 383 | $form = FrmForm::getAll( array( 'form_key' => $key ), '', 1 ); |
||
| 384 | if ( empty( $form ) ) { |
||
| 385 | $form = FrmForm::getAll( array(), '', 1 ); |
||
| 386 | } |
||
| 387 | |||
| 388 | require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/direct.php' ); |
||
| 389 | } |
||
| 390 | |||
| 391 | public static function register_pro_scripts() { |
||
| 392 | _deprecated_function( __FUNCTION__, '2.03', 'FrmProEntriesController::register_scripts' ); |
||
| 393 | if ( FrmAppHelper::pro_is_installed() ) { |
||
| 394 | FrmProEntriesController::register_scripts(); |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | public static function untrash() { |
||
| 399 | self::change_form_status( 'untrash' ); |
||
| 400 | } |
||
| 401 | |||
| 402 | public static function bulk_untrash( $ids ) { |
||
| 403 | FrmAppHelper::permission_check('frm_edit_forms'); |
||
| 404 | |||
| 405 | $count = FrmForm::set_status( $ids, 'published' ); |
||
| 406 | |||
| 407 | $message = sprintf(_n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), 1 ); |
||
| 408 | return $message; |
||
| 409 | } |
||
| 410 | |||
| 411 | public static function trash() { |
||
| 412 | self::change_form_status( 'trash' ); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param string $status |
||
| 417 | * |
||
| 418 | * @return int The number of forms changed |
||
| 419 | */ |
||
| 420 | public static function change_form_status( $status ) { |
||
| 421 | $available_status = array( |
||
| 422 | 'untrash' => array( |
||
| 423 | 'permission' => 'frm_edit_forms', |
||
| 424 | 'new_status' => 'published', |
||
| 425 | ), |
||
| 426 | 'trash' => array( |
||
| 427 | 'permission' => 'frm_delete_forms', |
||
| 428 | 'new_status' => 'trash', |
||
| 429 | ), |
||
| 430 | ); |
||
| 431 | |||
| 432 | if ( ! isset( $available_status[ $status ] ) ) { |
||
| 433 | return; |
||
| 434 | } |
||
| 435 | |||
| 436 | FrmAppHelper::permission_check( $available_status[ $status ]['permission'] ); |
||
| 437 | |||
| 438 | $params = FrmForm::list_page_params(); |
||
| 439 | |||
| 440 | //check nonce url |
||
| 441 | check_admin_referer( $status . '_form_' . $params['id'] ); |
||
| 442 | |||
| 443 | $count = 0; |
||
| 444 | if ( FrmForm::set_status( $params['id'], $available_status[ $status ]['new_status'] ) ) { |
||
| 445 | $count++; |
||
| 446 | } |
||
| 447 | |||
| 448 | $form_type = FrmAppHelper::get_simple_request( array( |
||
| 449 | 'param' => 'form_type', |
||
| 450 | 'type' => 'request', |
||
| 451 | ) ); |
||
| 452 | |||
| 453 | $available_status['untrash']['message'] = sprintf(_n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), $count ); |
||
| 454 | $available_status['trash']['message'] = sprintf( _n( '%1$s form moved to the Trash. %2$sUndo%3$s', '%1$s forms moved to the Trash. %2$sUndo%3$s', $count, 'formidable' ), $count, '<a href="' . esc_url( wp_nonce_url( '?page=formidable&frm_action=untrash&form_type=' . $form_type . '&id=' . $params['id'], 'untrash_form_' . $params['id'] ) ) . '">', '</a>' ); |
||
| 455 | |||
| 456 | $message = $available_status[ $status ]['message']; |
||
| 457 | |||
| 458 | self::display_forms_list( $params, $message ); |
||
| 459 | } |
||
| 460 | |||
| 461 | public static function bulk_trash( $ids ) { |
||
| 462 | FrmAppHelper::permission_check('frm_delete_forms'); |
||
| 463 | |||
| 464 | $count = 0; |
||
| 465 | foreach ( $ids as $id ) { |
||
| 466 | if ( FrmForm::trash( $id ) ) { |
||
| 467 | $count++; |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | $current_page = FrmAppHelper::get_simple_request( array( |
||
| 472 | 'param' => 'form_type', |
||
| 473 | 'type' => 'request', |
||
| 474 | ) ); |
||
| 475 | $message = sprintf( _n( '%1$s form moved to the Trash. %2$sUndo%3$s', '%1$s forms moved to the Trash. %2$sUndo%3$s', $count, 'formidable' ), $count, '<a href="' . esc_url( wp_nonce_url( '?page=formidable&frm_action=list&action=bulk_untrash&form_type=' . $current_page . '&item-action=' . implode( ',', $ids ), 'bulk-toplevel_page_formidable' ) ) . '">', '</a>' ); |
||
| 476 | |||
| 477 | return $message; |
||
| 478 | } |
||
| 479 | |||
| 480 | public static function destroy() { |
||
| 481 | FrmAppHelper::permission_check('frm_delete_forms'); |
||
| 482 | |||
| 483 | $params = FrmForm::list_page_params(); |
||
| 484 | |||
| 485 | //check nonce url |
||
| 486 | check_admin_referer('destroy_form_' . $params['id']); |
||
| 487 | |||
| 488 | $count = 0; |
||
| 489 | if ( FrmForm::destroy( $params['id'] ) ) { |
||
| 490 | $count++; |
||
| 491 | } |
||
| 492 | |||
| 493 | $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count); |
||
| 494 | |||
| 495 | self::display_forms_list( $params, $message ); |
||
| 496 | } |
||
| 497 | |||
| 498 | public static function bulk_destroy( $ids ) { |
||
| 499 | FrmAppHelper::permission_check('frm_delete_forms'); |
||
| 500 | |||
| 501 | $count = 0; |
||
| 502 | foreach ( $ids as $id ) { |
||
| 503 | $d = FrmForm::destroy( $id ); |
||
| 504 | if ( $d ) { |
||
| 505 | $count++; |
||
| 506 | } |
||
| 507 | } |
||
| 508 | |||
| 509 | $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count); |
||
| 510 | |||
| 511 | return $message; |
||
| 512 | } |
||
| 513 | |||
| 514 | private static function delete_all() { |
||
| 515 | //check nonce url |
||
| 516 | $permission_error = FrmAppHelper::permission_nonce_error('frm_delete_forms', '_wpnonce', 'bulk-toplevel_page_formidable'); |
||
| 517 | if ( $permission_error !== false ) { |
||
| 518 | self::display_forms_list( array(), '', array( $permission_error ) ); |
||
| 519 | return; |
||
| 520 | } |
||
| 521 | |||
| 522 | $count = FrmForm::scheduled_delete( time() ); |
||
| 523 | $message = sprintf(_n( '%1$s form permanently deleted.', '%1$s forms permanently deleted.', $count, 'formidable' ), $count); |
||
| 524 | |||
| 525 | self::display_forms_list( array(), $message ); |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Inserts Formidable button |
||
| 530 | * Hook exists since 2.5.0 |
||
| 531 | * |
||
| 532 | * @since 2.0.15 |
||
| 533 | */ |
||
| 534 | public static function insert_form_button() { |
||
| 535 | if ( current_user_can('frm_view_forms') ) { |
||
| 536 | $menu_name = FrmAppHelper::get_menu_name(); |
||
| 537 | $content = '<a href="#TB_inline?width=50&height=50&inlineId=frm_insert_form" class="thickbox button add_media frm_insert_form" title="' . esc_attr__( 'Add forms and content', 'formidable' ) . '"> |
||
| 538 | <span class="frm-buttons-icon wp-media-buttons-icon"></span> ' . |
||
| 539 | $menu_name . '</a>'; |
||
| 540 | echo wp_kses_post( $content ); |
||
| 541 | } |
||
| 542 | } |
||
| 543 | |||
| 544 | public static function insert_form_popup() { |
||
| 545 | $page = basename( FrmAppHelper::get_server_value( 'PHP_SELF' ) ); |
||
| 546 | if ( ! in_array( $page, array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) ) ) { |
||
| 547 | return; |
||
| 548 | } |
||
| 549 | |||
| 550 | FrmAppHelper::load_admin_wide_js(); |
||
| 551 | |||
| 552 | $shortcodes = array( |
||
| 553 | 'formidable' => array( |
||
| 554 | 'name' => __( 'Form', 'formidable' ), |
||
| 555 | 'label' => __( 'Insert a Form', 'formidable' ), |
||
| 556 | ), |
||
| 557 | ); |
||
| 558 | |||
| 559 | $shortcodes = apply_filters('frm_popup_shortcodes', $shortcodes); |
||
| 560 | |||
| 561 | include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/insert_form_popup.php' ); |
||
| 562 | } |
||
| 563 | |||
| 564 | public static function get_shortcode_opts() { |
||
| 565 | FrmAppHelper::permission_check('frm_view_forms'); |
||
| 566 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 567 | |||
| 568 | $shortcode = FrmAppHelper::get_post_param( 'shortcode', '', 'sanitize_text_field' ); |
||
| 569 | if ( empty($shortcode) ) { |
||
| 570 | wp_die(); |
||
| 571 | } |
||
| 572 | |||
| 573 | echo '<div id="sc-opts-' . esc_attr( $shortcode ) . '" class="frm_shortcode_option">'; |
||
| 574 | echo '<input type="radio" name="frmsc" value="' . esc_attr( $shortcode ) . '" id="sc-' . esc_attr( $shortcode ) . '" class="frm_hidden" />'; |
||
| 575 | |||
| 576 | $form_id = ''; |
||
| 577 | $opts = array(); |
||
| 578 | switch ( $shortcode ) { |
||
| 579 | case 'formidable': |
||
| 580 | $opts = array( |
||
| 581 | 'form_id' => 'id', |
||
| 582 | //'key' => ', |
||
| 583 | 'title' => array( |
||
| 584 | 'val' => 1, |
||
| 585 | 'label' => __( 'Display form title', 'formidable' ), |
||
| 586 | ), |
||
| 587 | 'description' => array( |
||
| 588 | 'val' => 1, |
||
| 589 | 'label' => __( 'Display form description', 'formidable' ), |
||
| 590 | ), |
||
| 591 | 'minimize' => array( |
||
| 592 | 'val' => 1, |
||
| 593 | 'label' => __( 'Minimize form HTML', 'formidable' ), |
||
| 594 | ), |
||
| 595 | ); |
||
| 596 | } |
||
| 597 | $opts = apply_filters( 'frm_sc_popup_opts', $opts, $shortcode ); |
||
| 598 | |||
| 599 | if ( isset( $opts['form_id'] ) && is_string( $opts['form_id'] ) ) { |
||
| 600 | // allow other shortcodes to use the required form id option |
||
| 601 | $form_id = $opts['form_id']; |
||
| 602 | unset( $opts['form_id'] ); |
||
| 603 | } |
||
| 604 | |||
| 605 | include( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/shortcode_opts.php' ); |
||
| 606 | |||
| 607 | echo '</div>'; |
||
| 608 | |||
| 609 | wp_die(); |
||
| 610 | } |
||
| 611 | |||
| 612 | public static function display_forms_list( $params = array(), $message = '', $errors = array() ) { |
||
| 613 | FrmAppHelper::permission_check( 'frm_view_forms' ); |
||
| 614 | |||
| 615 | global $wpdb, $frm_vars; |
||
| 616 | |||
| 617 | if ( empty( $params ) ) { |
||
| 618 | $params = FrmForm::list_page_params(); |
||
| 619 | } |
||
| 620 | |||
| 621 | $wp_list_table = new FrmFormsListHelper( compact( 'params' ) ); |
||
| 622 | |||
| 623 | $pagenum = $wp_list_table->get_pagenum(); |
||
| 624 | |||
| 625 | $wp_list_table->prepare_items(); |
||
| 626 | |||
| 627 | $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' ); |
||
| 628 | if ( $pagenum > $total_pages && $total_pages > 0 ) { |
||
| 629 | wp_redirect( esc_url_raw( add_query_arg( 'paged', $total_pages ) ) ); |
||
| 630 | die(); |
||
| 631 | } |
||
| 632 | |||
| 633 | require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/list.php' ); |
||
| 634 | } |
||
| 635 | |||
| 636 | public static function get_columns( $columns ) { |
||
| 637 | $columns['cb'] = '<input type="checkbox" />'; |
||
| 638 | $columns['id'] = 'ID'; |
||
| 639 | |||
| 640 | $type = FrmAppHelper::get_simple_request( array( |
||
| 641 | 'param' => 'form_type', |
||
| 642 | 'type' => 'request', |
||
| 643 | 'default' => 'published', |
||
| 644 | ) ); |
||
| 645 | |||
| 646 | if ( 'template' == $type ) { |
||
| 647 | $columns['name'] = __( 'Template Name', 'formidable' ); |
||
| 648 | $columns['type'] = __( 'Type', 'formidable' ); |
||
| 649 | $columns['form_key'] = __( 'Key', 'formidable' ); |
||
| 650 | } else { |
||
| 651 | $columns['name'] = __( 'Form Title', 'formidable' ); |
||
| 652 | $columns['entries'] = __( 'Entries', 'formidable' ); |
||
| 653 | $columns['form_key'] = __( 'Key', 'formidable' ); |
||
| 654 | $columns['shortcode'] = __( 'Shortcodes', 'formidable' ); |
||
| 655 | } |
||
| 656 | |||
| 657 | $columns['created_at'] = __( 'Date', 'formidable' ); |
||
| 658 | |||
| 659 | add_screen_option( 'per_page', array( |
||
| 660 | 'label' => __( 'Forms', 'formidable' ), |
||
| 661 | 'default' => 20, |
||
| 662 | 'option' => 'formidable_page_formidable_per_page', |
||
| 663 | ) ); |
||
| 664 | |||
| 665 | return $columns; |
||
| 666 | } |
||
| 667 | |||
| 668 | public static function get_sortable_columns() { |
||
| 669 | return array( |
||
| 670 | 'id' => 'id', |
||
| 671 | 'name' => 'name', |
||
| 672 | 'description' => 'description', |
||
| 673 | 'form_key' => 'form_key', |
||
| 674 | 'created_at' => 'created_at', |
||
| 675 | ); |
||
| 676 | } |
||
| 677 | |||
| 678 | public static function hidden_columns( $hidden_columns ) { |
||
| 679 | $type = FrmAppHelper::get_simple_request( array( |
||
| 680 | 'param' => 'form_type', |
||
| 681 | 'type' => 'request', |
||
| 682 | ) ); |
||
| 683 | |||
| 684 | if ( $type === 'template' ) { |
||
| 685 | $hidden_columns[] = 'id'; |
||
| 686 | $hidden_columns[] = 'form_key'; |
||
| 687 | } |
||
| 688 | |||
| 689 | return $hidden_columns; |
||
| 690 | } |
||
| 691 | |||
| 692 | public static function save_per_page( $save, $option, $value ) { |
||
| 693 | if ( $option == 'formidable_page_formidable_per_page' ) { |
||
| 694 | $save = (int) $value; |
||
| 695 | } |
||
| 696 | return $save; |
||
| 697 | } |
||
| 698 | |||
| 699 | private static function get_edit_vars( $id, $errors = array(), $message = '', $create_link = false ) { |
||
| 700 | global $frm_vars; |
||
| 701 | |||
| 702 | $form = FrmForm::getOne( $id ); |
||
| 703 | if ( ! $form ) { |
||
| 704 | wp_die( __( 'You are trying to edit a form that does not exist.', 'formidable' ) ); |
||
| 705 | } |
||
| 706 | |||
| 707 | if ( $form->parent_form_id ) { |
||
| 708 | wp_die( sprintf( __( 'You are trying to edit a child form. Please edit from %1$shere%2$s', 'formidable' ), '<a href="' . esc_url( admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form->parent_form_id ) ) . '">', '</a>' )); |
||
| 709 | } |
||
| 710 | |||
| 711 | $frm_field_selection = FrmField::field_selection(); |
||
| 712 | $fields = FrmField::get_all_for_form($form->id); |
||
| 713 | |||
| 714 | // Automatically add end section fields if they don't exist (2.0 migration) |
||
| 715 | $reset_fields = false; |
||
| 716 | FrmFormsHelper::auto_add_end_section_fields( $form, $fields, $reset_fields ); |
||
| 717 | |||
| 718 | if ( $reset_fields ) { |
||
| 719 | $fields = FrmField::get_all_for_form( $form->id, '', 'exclude' ); |
||
| 720 | } |
||
| 721 | |||
| 722 | unset($end_section_values, $last_order, $open, $reset_fields); |
||
| 723 | |||
| 724 | $args = array( 'parent_form_id' => $form->id ); |
||
| 725 | $values = FrmAppHelper::setup_edit_vars( $form, 'forms', '', true, array(), $args ); |
||
| 726 | $values['fields'] = $fields; |
||
| 727 | |||
| 728 | $edit_message = __( 'Form was Successfully Updated', 'formidable' ); |
||
| 729 | if ( $form->is_template && $message == $edit_message ) { |
||
| 730 | $message = __( 'Template was Successfully Updated', 'formidable' ); |
||
| 731 | } |
||
| 732 | |||
| 733 | $all_templates = FrmForm::getAll( array( 'is_template' => 1 ), 'name' ); |
||
| 734 | |||
| 735 | if ( $form->default_template ) { |
||
| 736 | wp_die(__( 'That template cannot be edited', 'formidable' )); |
||
| 737 | } else if ( defined('DOING_AJAX') ) { |
||
| 738 | wp_die(); |
||
| 739 | } else if ( $create_link ) { |
||
| 740 | require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/new.php' ); |
||
| 741 | } else { |
||
| 742 | require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/edit.php' ); |
||
| 743 | } |
||
| 744 | } |
||
| 745 | |||
| 746 | public static function get_settings_vars( $id, $errors = array(), $message = '' ) { |
||
| 747 | FrmAppHelper::permission_check( 'frm_edit_forms' ); |
||
| 748 | |||
| 749 | global $frm_vars; |
||
| 750 | |||
| 751 | $form = FrmForm::getOne( $id ); |
||
| 752 | |||
| 753 | $fields = FrmField::get_all_for_form($id); |
||
| 754 | $values = FrmAppHelper::setup_edit_vars($form, 'forms', $fields, true); |
||
| 755 | |||
| 756 | if ( isset($values['default_template']) && $values['default_template'] ) { |
||
| 757 | wp_die(__( 'That template cannot be edited', 'formidable' )); |
||
| 758 | } |
||
| 759 | |||
| 760 | self::clean_submit_html( $values ); |
||
| 761 | |||
| 762 | $action_controls = FrmFormActionsController::get_form_actions(); |
||
| 763 | |||
| 764 | $sections = apply_filters('frm_add_form_settings_section', array(), $values); |
||
| 765 | $pro_feature = FrmAppHelper::pro_is_installed() ? '' : ' class="pro_feature"'; |
||
| 766 | |||
| 767 | $styles = apply_filters('frm_get_style_opts', array()); |
||
| 768 | |||
| 769 | $first_h3 = 'frm_first_h3'; |
||
| 770 | |||
| 771 | require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/settings.php' ); |
||
| 772 | } |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Replace old Submit Button href with new href to avoid errors in Chrome |
||
| 776 | * |
||
| 777 | * @since 2.03.08 |
||
| 778 | * |
||
| 779 | * @param array|boolean $values |
||
| 780 | */ |
||
| 781 | private static function clean_submit_html( &$values ) { |
||
| 782 | if ( is_array( $values ) && isset( $values['submit_html'] ) ) { |
||
| 783 | $values['submit_html'] = str_replace( 'javascript:void(0)', '#', $values['submit_html'] ); |
||
| 784 | } |
||
| 785 | } |
||
| 786 | |||
| 787 | public static function mb_tags_box( $form_id, $class = '' ) { |
||
| 788 | $fields = FrmField::get_all_for_form($form_id, '', 'include'); |
||
| 789 | $linked_forms = array(); |
||
| 790 | $col = 'one'; |
||
| 791 | $settings_tab = FrmAppHelper::is_admin_page('formidable' ) ? true : false; |
||
| 792 | |||
| 793 | $cond_shortcodes = apply_filters( 'frm_conditional_shortcodes', array() ); |
||
| 794 | $adv_shortcodes = self::get_advanced_shortcodes(); |
||
| 795 | $user_fields = apply_filters( 'frm_user_shortcodes', array() ); |
||
| 796 | $entry_shortcodes = self::get_shortcode_helpers( $settings_tab ); |
||
| 797 | |||
| 798 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/mb_adv_info.php' ); |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Get an array of the options to display in the advanced tab |
||
| 803 | * of the customization panel |
||
| 804 | * @since 2.0.6 |
||
| 805 | */ |
||
| 806 | private static function get_advanced_shortcodes() { |
||
| 807 | $adv_shortcodes = array( |
||
| 808 | 'sep=", "' => array( |
||
| 809 | 'label' => __( 'Separator', 'formidable' ), |
||
| 810 | 'title' => __( 'Use a different separator for checkbox fields', 'formidable' ), |
||
| 811 | ), |
||
| 812 | 'format="d-m-Y"' => __( 'Date Format', 'formidable' ), |
||
| 813 | 'show="field_label"' => __( 'Field Label', 'formidable' ), |
||
| 814 | 'wpautop=0' => array( |
||
| 815 | 'label' => __( 'No Auto P', 'formidable' ), |
||
| 816 | 'title' => __( 'Do not automatically add any paragraphs or line breaks', 'formidable' ), |
||
| 817 | ), |
||
| 818 | ); |
||
| 819 | $adv_shortcodes = apply_filters( 'frm_advanced_shortcodes', $adv_shortcodes ); |
||
| 820 | // __( 'Leave blank instead of defaulting to User Login', 'formidable' ) : blank=1 |
||
| 821 | |||
| 822 | return $adv_shortcodes; |
||
| 823 | } |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Get an array of the helper shortcodes to display in the customization panel |
||
| 827 | * @since 2.0.6 |
||
| 828 | */ |
||
| 829 | private static function get_shortcode_helpers( $settings_tab ) { |
||
| 830 | $entry_shortcodes = array( |
||
| 831 | 'id' => __( 'Entry ID', 'formidable' ), |
||
| 832 | 'key' => __( 'Entry Key', 'formidable' ), |
||
| 833 | 'post_id' => __( 'Post ID', 'formidable' ), |
||
| 834 | 'ip' => __( 'User IP', 'formidable' ), |
||
| 835 | 'created-at' => __( 'Entry created', 'formidable' ), |
||
| 836 | 'updated-at' => __( 'Entry updated', 'formidable' ), |
||
| 837 | '' => '', |
||
| 838 | 'siteurl' => __( 'Site URL', 'formidable' ), |
||
| 839 | 'sitename' => __( 'Site Name', 'formidable' ), |
||
| 840 | ); |
||
| 841 | |||
| 842 | if ( ! FrmAppHelper::pro_is_installed() ) { |
||
| 843 | unset( $entry_shortcodes['post_id'] ); |
||
| 844 | } |
||
| 845 | |||
| 846 | if ( $settings_tab ) { |
||
| 847 | $entry_shortcodes['default-message'] = __( 'Default Msg', 'formidable' ); |
||
| 848 | $entry_shortcodes['default-html'] = __( 'Default HTML', 'formidable' ); |
||
| 849 | $entry_shortcodes['default-plain'] = __( 'Default Plain', 'formidable' ); |
||
| 850 | } |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Use this hook to add or remove buttons in the helpers section |
||
| 854 | * in the customization panel |
||
| 855 | * @since 2.0.6 |
||
| 856 | */ |
||
| 857 | $entry_shortcodes = apply_filters( 'frm_helper_shortcodes', $entry_shortcodes, $settings_tab ); |
||
| 858 | |||
| 859 | return $entry_shortcodes; |
||
| 860 | } |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Insert the form class setting into the form |
||
| 864 | */ |
||
| 865 | public static function form_classes( $form ) { |
||
| 866 | if ( isset($form->options['form_class']) ) { |
||
| 867 | echo esc_attr( sanitize_text_field( $form->options['form_class'] ) ); |
||
| 868 | } |
||
| 869 | |||
| 870 | if ( isset( $form->options['js_validate'] ) && $form->options['js_validate'] ) { |
||
| 871 | echo ' frm_js_validate '; |
||
| 872 | } |
||
| 873 | } |
||
| 874 | |||
| 875 | public static function get_email_html() { |
||
| 876 | FrmAppHelper::permission_check( 'frm_view_forms' ); |
||
| 877 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 878 | |||
| 879 | echo FrmEntriesController::show_entry_shortcode( array( |
||
| 880 | 'form_id' => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ), |
||
| 881 | 'default_email' => true, |
||
| 882 | 'plain_text' => FrmAppHelper::get_post_param( 'plain_text', '', 'absint' ), |
||
| 883 | ) ); |
||
| 884 | wp_die(); |
||
| 885 | } |
||
| 886 | |||
| 887 | public static function filter_content( $content, $form, $entry = false ) { |
||
| 888 | self::get_entry_by_param( $entry ); |
||
| 889 | if ( ! $entry ) { |
||
| 890 | return $content; |
||
| 891 | } |
||
| 892 | |||
| 893 | if ( is_object( $form ) ) { |
||
| 894 | $form = $form->id; |
||
| 895 | } |
||
| 896 | |||
| 897 | $shortcodes = FrmFieldsHelper::get_shortcodes( $content, $form ); |
||
| 898 | $content = apply_filters( 'frm_replace_content_shortcodes', $content, $entry, $shortcodes ); |
||
| 899 | |||
| 900 | return $content; |
||
| 901 | } |
||
| 902 | |||
| 903 | private static function get_entry_by_param( &$entry ) { |
||
| 904 | if ( ! $entry || ! is_object( $entry ) ) { |
||
| 905 | if ( ! $entry || ! is_numeric( $entry ) ) { |
||
| 906 | $entry = FrmAppHelper::get_post_param( 'id', false, 'sanitize_title' ); |
||
| 907 | } |
||
| 908 | |||
| 909 | FrmEntry::maybe_get_entry( $entry ); |
||
| 910 | } |
||
| 911 | } |
||
| 912 | |||
| 913 | public static function replace_content_shortcodes( $content, $entry, $shortcodes ) { |
||
| 914 | return FrmFieldsHelper::replace_content_shortcodes( $content, $entry, $shortcodes ); |
||
| 915 | } |
||
| 916 | |||
| 917 | public static function process_bulk_form_actions( $errors ) { |
||
| 918 | if ( ! $_REQUEST ) { |
||
| 919 | return $errors; |
||
| 920 | } |
||
| 921 | |||
| 922 | $bulkaction = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' ); |
||
| 923 | if ( $bulkaction == -1 ) { |
||
| 924 | $bulkaction = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' ); |
||
| 925 | } |
||
| 926 | |||
| 927 | if ( ! empty( $bulkaction ) && strpos( $bulkaction, 'bulk_' ) === 0 ) { |
||
| 928 | FrmAppHelper::remove_get_action(); |
||
| 929 | |||
| 930 | $bulkaction = str_replace( 'bulk_', '', $bulkaction ); |
||
| 931 | } |
||
| 932 | |||
| 933 | $ids = FrmAppHelper::get_param( 'item-action', '', 'get', 'sanitize_text_field' ); |
||
| 934 | if ( empty( $ids ) ) { |
||
| 935 | $errors[] = __( 'No forms were specified', 'formidable' ); |
||
| 936 | return $errors; |
||
| 937 | } |
||
| 938 | |||
| 939 | $permission_error = FrmAppHelper::permission_nonce_error( '', '_wpnonce', 'bulk-toplevel_page_formidable' ); |
||
| 940 | if ( $permission_error !== false ) { |
||
| 941 | $errors[] = $permission_error; |
||
| 942 | return $errors; |
||
| 943 | } |
||
| 944 | |||
| 945 | if ( ! is_array( $ids ) ) { |
||
| 946 | $ids = explode( ',', $ids ); |
||
| 947 | } |
||
| 948 | |||
| 949 | switch ( $bulkaction ) { |
||
| 950 | case 'delete': |
||
| 951 | $message = self::bulk_destroy( $ids ); |
||
| 952 | break; |
||
| 953 | case 'trash': |
||
| 954 | $message = self::bulk_trash( $ids ); |
||
| 955 | break; |
||
| 956 | case 'untrash': |
||
| 957 | $message = self::bulk_untrash( $ids ); |
||
| 958 | } |
||
| 959 | |||
| 960 | if ( isset( $message ) && ! empty( $message ) ) { |
||
| 961 | echo '<div id="message" class="updated frm_updated_message">' . FrmAppHelper::kses( $message, array( 'a' ) ) . '</div>'; |
||
| 962 | } |
||
| 963 | |||
| 964 | return $errors; |
||
| 965 | } |
||
| 966 | |||
| 967 | public static function add_default_templates( $path, $default = true, $template = true ) { |
||
| 968 | _deprecated_function( __FUNCTION__, '1.07.05', 'FrmXMLController::add_default_templates()' ); |
||
| 969 | |||
| 970 | $path = untrailingslashit(trim($path)); |
||
| 971 | $templates = glob( $path . '/*.php' ); |
||
| 972 | |||
| 973 | for ( $i = count( $templates ) - 1; $i >= 0; $i-- ) { |
||
| 974 | $filename = str_replace( '.php', '', str_replace( $path . '/', '', $templates[ $i ] ) ); |
||
| 975 | $template_query = array( 'form_key' => $filename ); |
||
| 976 | if ( $template ) { |
||
| 977 | $template_query['is_template'] = 1; |
||
| 978 | } |
||
| 979 | if ( $default ) { |
||
| 980 | $template_query['default_template'] = 1; |
||
| 981 | } |
||
| 982 | $form = FrmForm::getAll( $template_query, '', 1 ); |
||
| 983 | |||
| 984 | $values = FrmFormsHelper::setup_new_vars(); |
||
| 985 | $values['form_key'] = $filename; |
||
| 986 | $values['is_template'] = $template; |
||
| 987 | $values['status'] = 'published'; |
||
| 988 | if ( $default ) { |
||
| 989 | $values['default_template'] = 1; |
||
| 990 | } |
||
| 991 | |||
| 992 | include( $templates[ $i ] ); |
||
| 993 | |||
| 994 | //get updated form |
||
| 995 | if ( isset($form) && ! empty($form) ) { |
||
| 996 | $old_id = $form->id; |
||
| 997 | $form = FrmForm::getOne($form->id); |
||
| 998 | } else { |
||
| 999 | $old_id = false; |
||
| 1000 | $form = FrmForm::getAll( $template_query, '', 1 ); |
||
| 1001 | } |
||
| 1002 | |||
| 1003 | if ( $form ) { |
||
| 1004 | do_action( 'frm_after_duplicate_form', $form->id, (array) $form, array( 'old_id' => $old_id ) ); |
||
| 1005 | } |
||
| 1006 | } |
||
| 1007 | } |
||
| 1008 | |||
| 1009 | public static function route() { |
||
| 1010 | $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action'; |
||
| 1011 | $vars = array(); |
||
| 1012 | if ( isset( $_POST['frm_compact_fields'] ) ) { |
||
| 1013 | FrmAppHelper::permission_check( 'frm_edit_forms' ); |
||
| 1014 | |||
| 1015 | $json_vars = htmlspecialchars_decode(nl2br(stripslashes(str_replace('"', '\\\"', $_POST['frm_compact_fields'] )))); |
||
| 1016 | $json_vars = json_decode($json_vars, true); |
||
| 1017 | if ( empty($json_vars) ) { |
||
| 1018 | // json decoding failed so we should return an error message |
||
| 1019 | $action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); |
||
| 1020 | if ( 'edit' == $action ) { |
||
| 1021 | $action = 'update'; |
||
| 1022 | } |
||
| 1023 | |||
| 1024 | add_filter('frm_validate_form', 'FrmFormsController::json_error'); |
||
| 1025 | } else { |
||
| 1026 | $vars = FrmAppHelper::json_to_array($json_vars); |
||
| 1027 | $action = $vars[ $action ]; |
||
| 1028 | unset( $_REQUEST['frm_compact_fields'], $_POST['frm_compact_fields'] ); |
||
| 1029 | $_REQUEST = array_merge( $_REQUEST, $vars ); |
||
| 1030 | $_POST = array_merge( $_POST, $_REQUEST ); |
||
| 1031 | } |
||
| 1032 | } else { |
||
| 1033 | $action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); |
||
| 1034 | if ( isset( $_REQUEST['delete_all'] ) ) { |
||
| 1035 | // override the action for this page |
||
| 1036 | $action = 'delete_all'; |
||
| 1037 | } |
||
| 1038 | } |
||
| 1039 | |||
| 1040 | add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' ); |
||
| 1041 | FrmAppHelper::trigger_hook_load( 'form' ); |
||
| 1042 | |||
| 1043 | switch ( $action ) { |
||
| 1044 | case 'new': |
||
| 1045 | return self::new_form($vars); |
||
| 1046 | case 'create': |
||
| 1047 | case 'edit': |
||
| 1048 | case 'update': |
||
| 1049 | case 'duplicate': |
||
| 1050 | case 'trash': |
||
| 1051 | case 'untrash': |
||
| 1052 | case 'destroy': |
||
| 1053 | case 'delete_all': |
||
| 1054 | case 'settings': |
||
| 1055 | case 'update_settings': |
||
| 1056 | return self::$action( $vars ); |
||
| 1057 | default: |
||
| 1058 | do_action( 'frm_form_action_' . $action ); |
||
| 1059 | if ( apply_filters( 'frm_form_stop_action_' . $action, false ) ) { |
||
| 1060 | return; |
||
| 1061 | } |
||
| 1062 | |||
| 1063 | $action = FrmAppHelper::get_param( 'action', '', 'get', 'sanitize_text_field' ); |
||
| 1064 | if ( $action == -1 ) { |
||
| 1065 | $action = FrmAppHelper::get_param( 'action2', '', 'get', 'sanitize_title' ); |
||
| 1066 | } |
||
| 1067 | |||
| 1068 | if ( strpos($action, 'bulk_') === 0 ) { |
||
| 1069 | FrmAppHelper::remove_get_action(); |
||
| 1070 | return self::list_form(); |
||
| 1071 | } |
||
| 1072 | |||
| 1073 | return self::display_forms_list(); |
||
| 1074 | } |
||
| 1075 | } |
||
| 1076 | |||
| 1077 | public static function json_error( $errors ) { |
||
| 1078 | $errors['json'] = __( 'Abnormal HTML characters prevented your form from saving correctly', 'formidable' ); |
||
| 1079 | return $errors; |
||
| 1080 | } |
||
| 1081 | |||
| 1082 | |||
| 1083 | /* FRONT-END FORMS */ |
||
| 1084 | public static function admin_bar_css() { |
||
| 1085 | if ( is_admin() || ! current_user_can( 'frm_edit_forms' ) ) { |
||
| 1086 | return; |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | add_action( 'wp_before_admin_bar_render', 'FrmFormsController::admin_bar_configure' ); |
||
| 1090 | FrmAppHelper::load_font_style(); |
||
| 1091 | } |
||
| 1092 | |||
| 1093 | public static function admin_bar_configure() { |
||
| 1094 | global $frm_vars; |
||
| 1095 | if ( empty($frm_vars['forms_loaded']) ) { |
||
| 1096 | return; |
||
| 1097 | } |
||
| 1098 | |||
| 1099 | $actions = array(); |
||
| 1100 | foreach ( $frm_vars['forms_loaded'] as $form ) { |
||
| 1101 | if ( is_object($form) ) { |
||
| 1102 | $actions[ $form->id ] = $form->name; |
||
| 1103 | } |
||
| 1104 | unset($form); |
||
| 1105 | } |
||
| 1106 | |||
| 1107 | if ( empty($actions) ) { |
||
| 1108 | return; |
||
| 1109 | } |
||
| 1110 | |||
| 1111 | self::add_menu_to_admin_bar(); |
||
| 1112 | self::add_forms_to_admin_bar( $actions ); |
||
| 1113 | } |
||
| 1114 | |||
| 1115 | /** |
||
| 1116 | * @since 2.05.07 |
||
| 1117 | */ |
||
| 1118 | public static function add_menu_to_admin_bar() { |
||
| 1119 | global $wp_admin_bar; |
||
| 1120 | |||
| 1121 | $wp_admin_bar->add_node( array( |
||
| 1122 | 'id' => 'frm-forms', |
||
| 1123 | 'title' => '<span class="ab-icon"></span><span class="ab-label">' . FrmAppHelper::get_menu_name() . '</span>', |
||
| 1124 | 'href' => admin_url( 'admin.php?page=formidable' ), |
||
| 1125 | 'meta' => array( |
||
| 1126 | 'title' => FrmAppHelper::get_menu_name(), |
||
| 1127 | ), |
||
| 1128 | ) ); |
||
| 1129 | } |
||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * @since 2.05.07 |
||
| 1133 | */ |
||
| 1134 | private static function add_forms_to_admin_bar( $actions ) { |
||
| 1135 | global $wp_admin_bar; |
||
| 1136 | |||
| 1137 | asort( $actions ); |
||
| 1138 | |||
| 1139 | foreach ( $actions as $form_id => $name ) { |
||
| 1140 | |||
| 1141 | $wp_admin_bar->add_node( array( |
||
| 1142 | 'parent' => 'frm-forms', |
||
| 1143 | 'id' => 'edit_form_' . $form_id, |
||
| 1144 | 'title' => empty( $name ) ? __( '(no title)' ) : $name, |
||
| 1145 | 'href' => admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id ), |
||
| 1146 | ) ); |
||
| 1147 | } |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | //formidable shortcode |
||
| 1151 | public static function get_form_shortcode( $atts ) { |
||
| 1152 | global $frm_vars; |
||
| 1153 | if ( isset($frm_vars['skip_shortcode']) && $frm_vars['skip_shortcode'] ) { |
||
| 1154 | $sc = '[formidable'; |
||
| 1155 | if ( ! empty( $atts ) ) { |
||
| 1156 | foreach ( $atts as $k => $v ) { |
||
| 1157 | $sc .= ' ' . $k . '="' . esc_attr( $v ) . '"'; |
||
| 1158 | } |
||
| 1159 | } |
||
| 1160 | return $sc . ']'; |
||
| 1161 | } |
||
| 1162 | |||
| 1163 | $shortcode_atts = shortcode_atts( array( |
||
| 1164 | 'id' => '', |
||
| 1165 | 'key' => '', |
||
| 1166 | 'title' => false, |
||
| 1167 | 'description' => false, |
||
| 1168 | 'readonly' => false, |
||
| 1169 | 'entry_id' => false, |
||
| 1170 | 'fields' => array(), |
||
| 1171 | 'exclude_fields' => array(), |
||
| 1172 | 'minimize' => false, |
||
| 1173 | ), $atts ); |
||
| 1174 | do_action( 'formidable_shortcode_atts', $shortcode_atts, $atts ); |
||
| 1175 | |||
| 1176 | return self::show_form( |
||
| 1177 | $shortcode_atts['id'], $shortcode_atts['key'], $shortcode_atts['title'], |
||
| 1178 | $shortcode_atts['description'], $atts |
||
| 1179 | ); |
||
| 1180 | } |
||
| 1181 | |||
| 1182 | public static function show_form( $id = '', $key = '', $title = false, $description = false, $atts = array() ) { |
||
| 1183 | if ( empty( $id ) ) { |
||
| 1184 | $id = $key; |
||
| 1185 | } |
||
| 1186 | |||
| 1187 | $form = self::maybe_get_form_to_show( $id ); |
||
| 1188 | if ( ! $form ) { |
||
| 1189 | return __( 'Please select a valid form', 'formidable' ); |
||
| 1190 | } |
||
| 1191 | |||
| 1192 | FrmAppController::maybe_update_styles(); |
||
| 1193 | |||
| 1194 | add_action( 'frm_load_form_hooks', 'FrmHooksController::trigger_load_form_hooks' ); |
||
| 1195 | FrmAppHelper::trigger_hook_load( 'form', $form ); |
||
| 1196 | |||
| 1197 | $form = apply_filters( 'frm_pre_display_form', $form ); |
||
| 1198 | |||
| 1199 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 1200 | |||
| 1201 | if ( self::is_viewable_draft_form( $form ) ) { |
||
| 1202 | // don't show a draft form on a page |
||
| 1203 | $form = __( 'Please select a valid form', 'formidable' ); |
||
| 1204 | } else if ( self::user_should_login( $form ) ) { |
||
| 1205 | $form = do_shortcode( $frm_settings->login_msg ); |
||
| 1206 | } else if ( self::user_has_permission_to_view( $form ) ) { |
||
| 1207 | $form = do_shortcode( $frm_settings->login_msg ); |
||
| 1208 | } else { |
||
| 1209 | $form = self::get_form( $form, $title, $description, $atts ); |
||
| 1210 | |||
| 1211 | /** |
||
| 1212 | * Use this shortcode to check for external shortcodes that may span |
||
| 1213 | * across multiple fields in the customizable HTML |
||
| 1214 | * @since 2.0.8 |
||
| 1215 | */ |
||
| 1216 | $form = apply_filters( 'frm_filter_final_form', $form ); |
||
| 1217 | } |
||
| 1218 | |||
| 1219 | return $form; |
||
| 1220 | } |
||
| 1221 | |||
| 1222 | private static function maybe_get_form_to_show( $id ) { |
||
| 1223 | $form = false; |
||
| 1224 | |||
| 1225 | if ( ! empty( $id ) ) { // no form id or key set |
||
| 1226 | $form = FrmForm::getOne( $id ); |
||
| 1227 | if ( ! $form || $form->parent_form_id || $form->status == 'trash' ) { |
||
| 1228 | $form = false; |
||
| 1229 | } |
||
| 1230 | } |
||
| 1231 | |||
| 1232 | return $form; |
||
| 1233 | } |
||
| 1234 | |||
| 1235 | private static function is_viewable_draft_form( $form ) { |
||
| 1236 | global $post; |
||
| 1237 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 1238 | return $form->status == 'draft' && current_user_can( 'frm_edit_forms' ) && ! FrmAppHelper::is_preview_page(); |
||
| 1239 | } |
||
| 1240 | |||
| 1241 | private static function user_should_login( $form ) { |
||
| 1242 | return $form->logged_in && ! is_user_logged_in(); |
||
| 1243 | } |
||
| 1244 | |||
| 1245 | private static function user_has_permission_to_view( $form ) { |
||
| 1246 | return $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'] ); |
||
| 1247 | } |
||
| 1248 | |||
| 1249 | public static function get_form( $form, $title, $description, $atts = array() ) { |
||
| 1250 | ob_start(); |
||
| 1251 | |||
| 1252 | do_action( 'frm_before_get_form', $atts ); |
||
| 1253 | |||
| 1254 | self::get_form_contents( $form, $title, $description, $atts ); |
||
| 1255 | self::enqueue_scripts( FrmForm::get_params( $form ) ); |
||
| 1256 | |||
| 1257 | $contents = ob_get_contents(); |
||
| 1258 | ob_end_clean(); |
||
| 1259 | |||
| 1260 | self::maybe_minimize_form( $atts, $contents ); |
||
| 1261 | |||
| 1262 | return $contents; |
||
| 1263 | } |
||
| 1264 | |||
| 1265 | public static function enqueue_scripts( $params ) { |
||
| 1266 | do_action( 'frm_enqueue_form_scripts', $params ); |
||
| 1267 | } |
||
| 1268 | |||
| 1269 | public static function get_form_contents( $form, $title, $description, $atts ) { |
||
| 1270 | $params = FrmForm::get_params( $form ); |
||
| 1271 | $errors = self::get_saved_errors( $form, $params ); |
||
| 1272 | $fields = FrmFieldsHelper::get_form_fields( $form->id, $errors ); |
||
| 1273 | $reset = false; |
||
| 1274 | $pass_args = compact( 'form', 'fields', 'errors', 'title', 'description', 'reset' ); |
||
| 1275 | |||
| 1276 | $handle_process_here = $params['action'] == 'create' && $params['posted_form_id'] == $form->id && $_POST; |
||
| 1277 | |||
| 1278 | if ( ! $handle_process_here ) { |
||
| 1279 | do_action( 'frm_display_form_action', $params, $fields, $form, $title, $description ); |
||
| 1280 | if ( apply_filters( 'frm_continue_to_new', true, $form->id, $params['action'] ) ) { |
||
| 1281 | self::show_form_after_submit( $pass_args ); |
||
| 1282 | } |
||
| 1283 | } elseif ( ! empty( $errors ) ) { |
||
| 1284 | self::show_form_after_submit( $pass_args ); |
||
| 1285 | |||
| 1286 | } else { |
||
| 1287 | |||
| 1288 | do_action( 'frm_validate_form_creation', $params, $fields, $form, $title, $description ); |
||
| 1289 | |||
| 1290 | if ( apply_filters( 'frm_continue_to_create', true, $form->id ) ) { |
||
| 1291 | $entry_id = self::just_created_entry( $form->id ); |
||
| 1292 | $pass_args['entry_id'] = $entry_id; |
||
| 1293 | $pass_args['reset'] = true; |
||
| 1294 | $pass_args['conf_method'] = self::get_confirmation_method( compact( 'form', 'entry_id' ) ); |
||
| 1295 | |||
| 1296 | self::run_success_action( $pass_args ); |
||
| 1297 | |||
| 1298 | do_action( 'frm_after_entry_processed', array( |
||
| 1299 | 'entry_id' => $entry_id, |
||
| 1300 | 'form' => $form, |
||
| 1301 | ) ); |
||
| 1302 | } |
||
| 1303 | } |
||
| 1304 | } |
||
| 1305 | |||
| 1306 | /** |
||
| 1307 | * If the form was processed earlier (init), get the generated errors |
||
| 1308 | * @since 2.05 |
||
| 1309 | */ |
||
| 1310 | private static function get_saved_errors( $form, $params ) { |
||
| 1311 | global $frm_vars; |
||
| 1312 | |||
| 1313 | if ( $params['posted_form_id'] == $form->id && $_POST && isset( $frm_vars['created_entries'][ $form->id ] ) ) { |
||
| 1314 | $errors = $frm_vars['created_entries'][ $form->id ]['errors']; |
||
| 1315 | } else { |
||
| 1316 | $errors = array(); |
||
| 1317 | } |
||
| 1318 | return $errors; |
||
| 1319 | } |
||
| 1320 | |||
| 1321 | /** |
||
| 1322 | * @since 2.2.7 |
||
| 1323 | */ |
||
| 1324 | public static function just_created_entry( $form_id ) { |
||
| 1325 | global $frm_vars; |
||
| 1326 | return ( isset( $frm_vars['created_entries'] ) && isset( $frm_vars['created_entries'][ $form_id ] ) && isset( $frm_vars['created_entries'][ $form_id ]['entry_id'] ) ) ? $frm_vars['created_entries'][ $form_id ]['entry_id'] : 0; |
||
| 1327 | } |
||
| 1328 | |||
| 1329 | /** |
||
| 1330 | * @since 3.0 |
||
| 1331 | */ |
||
| 1332 | private static function get_confirmation_method( $atts ) { |
||
| 1333 | $opt = 'success_action'; |
||
| 1334 | $method = ( isset( $atts['form']->options[ $opt ] ) && ! empty( $atts['form']->options[ $opt ] ) ) ? $atts['form']->options[ $opt ] : 'message'; |
||
| 1335 | $method = apply_filters( 'frm_success_filter', $method, $atts['form'], 'create' ); |
||
| 1336 | |||
| 1337 | if ( $method != 'message' && ( ! $atts['entry_id'] || ! is_numeric( $atts['entry_id'] ) ) ) { |
||
| 1338 | $method = 'message'; |
||
| 1339 | } |
||
| 1340 | |||
| 1341 | return $method; |
||
| 1342 | } |
||
| 1343 | |||
| 1344 | public static function maybe_trigger_redirect( $form, $params, $args ) { |
||
| 1345 | if ( ! isset( $params['id'] ) ) { |
||
| 1346 | global $frm_vars; |
||
| 1347 | $params['id'] = $frm_vars['created_entries'][ $form->id ]['entry_id']; |
||
| 1348 | } |
||
| 1349 | |||
| 1350 | $conf_method = self::get_confirmation_method( array( |
||
| 1351 | 'form' => $form, |
||
| 1352 | 'entry_id' => $params['id'], |
||
| 1353 | ) ); |
||
| 1354 | |||
| 1355 | if ( 'redirect' === $conf_method ) { |
||
| 1356 | self::trigger_redirect( $form, $params, $args ); |
||
| 1357 | } |
||
| 1358 | } |
||
| 1359 | |||
| 1360 | public static function trigger_redirect( $form, $params, $args ) { |
||
| 1361 | $success_args = array( |
||
| 1362 | 'action' => $params['action'], |
||
| 1363 | 'conf_method' => 'redirect', |
||
| 1364 | 'form' => $form, |
||
| 1365 | 'entry_id' => $params['id'], |
||
| 1366 | ); |
||
| 1367 | |||
| 1368 | if ( isset( $args['ajax'] ) ) { |
||
| 1369 | $success_args['ajax'] = $args['ajax']; |
||
| 1370 | } |
||
| 1371 | |||
| 1372 | self::run_success_action( $success_args ); |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | /** |
||
| 1376 | * Used when the success action is not 'message' |
||
| 1377 | * @since 2.05 |
||
| 1378 | */ |
||
| 1379 | public static function run_success_action( $args ) { |
||
| 1380 | $extra_args = $args; |
||
| 1381 | unset( $extra_args['form'] ); |
||
| 1382 | |||
| 1383 | do_action( 'frm_success_action', $args['conf_method'], $args['form'], $args['form']->options, $args['entry_id'], $extra_args ); |
||
| 1384 | |||
| 1385 | $opt = ( ! isset( $args['action'] ) || $args['action'] == 'create' ) ? 'success' : 'edit'; |
||
| 1386 | $args['success_opt'] = $opt; |
||
| 1387 | if ( $args['conf_method'] == 'page' && is_numeric( $args['form']->options[ $opt . '_page_id' ] ) ) { |
||
| 1388 | self::load_page_after_submit( $args ); |
||
| 1389 | } elseif ( $args['conf_method'] == 'redirect' ) { |
||
| 1390 | self::redirect_after_submit( $args ); |
||
| 1391 | } else { |
||
| 1392 | self::show_message_after_save( $args ); |
||
| 1393 | } |
||
| 1394 | } |
||
| 1395 | |||
| 1396 | /** |
||
| 1397 | * @since 3.0 |
||
| 1398 | */ |
||
| 1399 | private static function load_page_after_submit( $args ) { |
||
| 1400 | global $post; |
||
| 1401 | $opt = $args['success_opt']; |
||
| 1402 | if ( ! $post || $args['form']->options[ $opt . '_page_id' ] != $post->ID ) { |
||
| 1403 | $page = get_post( $args['form']->options[ $opt . '_page_id' ] ); |
||
| 1404 | $old_post = $post; |
||
| 1405 | $post = $page; |
||
| 1406 | $content = apply_filters( 'frm_content', $page->post_content, $args['form'], $args['entry_id'] ); |
||
| 1407 | echo apply_filters( 'the_content', $content ); |
||
| 1408 | $post = $old_post; |
||
| 1409 | } |
||
| 1410 | } |
||
| 1411 | |||
| 1412 | /** |
||
| 1413 | * @since 3.0 |
||
| 1414 | */ |
||
| 1415 | private static function redirect_after_submit( $args ) { |
||
| 1416 | global $frm_vars; |
||
| 1417 | |||
| 1418 | add_filter( 'frm_use_wpautop', '__return_false' ); |
||
| 1419 | |||
| 1420 | $opt = $args['success_opt']; |
||
| 1421 | $success_url = trim( $args['form']->options[ $opt . '_url' ] ); |
||
| 1422 | $success_url = apply_filters( 'frm_content', $success_url, $args['form'], $args['entry_id'] ); |
||
| 1423 | |||
| 1424 | $success_msg = isset( $args['form']->options[ $opt . '_msg' ] ) ? $args['form']->options[ $opt . '_msg' ] : __( 'Please wait while you are redirected.', 'formidable' ); |
||
| 1425 | |||
| 1426 | $redirect_msg = self::get_redirect_message( $success_url, $success_msg, $args ); |
||
| 1427 | |||
| 1428 | $args['id'] = $args['entry_id']; |
||
| 1429 | FrmEntriesController::delete_entry_before_redirect( $success_url, $args['form'], $args ); |
||
| 1430 | |||
| 1431 | add_filter( 'frm_redirect_url', 'FrmEntriesController::prepare_redirect_url' ); |
||
| 1432 | $success_url = apply_filters( 'frm_redirect_url', $success_url, $args['form'], $args); |
||
| 1433 | |||
| 1434 | $doing_ajax = FrmAppHelper::doing_ajax(); |
||
| 1435 | |||
| 1436 | if ( isset( $args['ajax'] ) && $args['ajax'] && $doing_ajax ) { |
||
| 1437 | echo json_encode( array( 'redirect' => $success_url ) ); |
||
| 1438 | wp_die(); |
||
| 1439 | } elseif ( ! headers_sent() ) { |
||
| 1440 | wp_redirect( esc_url_raw( $success_url ) ); |
||
| 1441 | die(); // do not use wp_die or redirect fails |
||
| 1442 | } else { |
||
| 1443 | add_filter( 'frm_use_wpautop', '__return_true' ); |
||
| 1444 | |||
| 1445 | echo $redirect_msg; |
||
| 1446 | echo "<script type='text/javascript'>window.onload = function(){setTimeout(window.location='" . esc_url_raw( $success_url ) . "', 8000);}</script>"; |
||
| 1447 | } |
||
| 1448 | } |
||
| 1449 | |||
| 1450 | /** |
||
| 1451 | * @since 3.0 |
||
| 1452 | * @param string $success_url |
||
| 1453 | * @param string $success_msg |
||
| 1454 | * @param array $args |
||
| 1455 | */ |
||
| 1456 | private static function get_redirect_message( $success_url, $success_msg, $args ) { |
||
| 1457 | $redirect_msg = '<div class="' . esc_attr( FrmFormsHelper::get_form_style_class( $args['form'] ) ) . '"><div class="frm-redirect-msg frm_message">' . $success_msg . '<br/>' . |
||
| 1458 | sprintf( __( '%1$sClick here%2$s if you are not automatically redirected.', 'formidable' ), '<a href="' . esc_url( $success_url ) . '">', '</a>') . |
||
| 1459 | '</div></div>'; |
||
| 1460 | |||
| 1461 | return apply_filters( 'frm_redirect_msg', $redirect_msg, array( |
||
| 1462 | 'entry_id' => $args['entry_id'], |
||
| 1463 | 'form_id' => $args['form']->id, |
||
| 1464 | 'form' => $args['form'], |
||
| 1465 | ) ); |
||
| 1466 | } |
||
| 1467 | |||
| 1468 | /** |
||
| 1469 | * Prepare to show the success message and empty form after submit |
||
| 1470 | * @since 2.05 |
||
| 1471 | */ |
||
| 1472 | public static function show_message_after_save( $atts ) { |
||
| 1473 | $atts['message'] = self::prepare_submit_message( $atts['form'], $atts['entry_id'] ); |
||
| 1474 | |||
| 1475 | if ( ! isset( $atts['form']->options['show_form'] ) || $atts['form']->options['show_form'] ) { |
||
| 1476 | self::show_form_after_submit( $atts ); |
||
| 1477 | } else { |
||
| 1478 | self::show_lone_success_messsage( $atts ); |
||
| 1479 | } |
||
| 1480 | } |
||
| 1481 | |||
| 1482 | /** |
||
| 1483 | * Show an empty form |
||
| 1484 | * @since 2.05 |
||
| 1485 | */ |
||
| 1486 | private static function show_form_after_submit( $args ) { |
||
| 1487 | self::fill_atts_for_form_display( $args ); |
||
| 1488 | |||
| 1489 | $errors = $args['errors']; |
||
| 1490 | $message = $args['message']; |
||
| 1491 | $form = $args['form']; |
||
| 1492 | $title = $args['title']; |
||
| 1493 | $description = $args['description']; |
||
| 1494 | |||
| 1495 | if ( empty( $args['fields'] ) ) { |
||
| 1496 | $values = array(); |
||
| 1497 | } else { |
||
| 1498 | $values = FrmEntriesHelper::setup_new_vars( $args['fields'], $form, $args['reset'] ); |
||
| 1499 | } |
||
| 1500 | unset( $args ); |
||
| 1501 | |||
| 1502 | $include_form_tag = apply_filters( 'frm_include_form_tag', true, $form ); |
||
| 1503 | |||
| 1504 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 1505 | $submit = isset( $form->options['submit_value'] ) ? $form->options['submit_value'] : $frm_settings->submit_value; |
||
| 1506 | |||
| 1507 | include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/new.php' ); |
||
| 1508 | } |
||
| 1509 | |||
| 1510 | /** |
||
| 1511 | * Get all the values needed on the new.php entry page |
||
| 1512 | * @since 2.05 |
||
| 1513 | */ |
||
| 1514 | private static function fill_atts_for_form_display( &$args ) { |
||
| 1515 | $defaults = array( |
||
| 1516 | 'errors' => array(), |
||
| 1517 | 'message' => '', |
||
| 1518 | 'fields' => array(), |
||
| 1519 | 'form' => array(), |
||
| 1520 | 'title' => true, |
||
| 1521 | 'description' => false, |
||
| 1522 | 'reset' => false, |
||
| 1523 | ); |
||
| 1524 | $args = wp_parse_args( $args, $defaults ); |
||
| 1525 | } |
||
| 1526 | |||
| 1527 | /** |
||
| 1528 | * Show the success message without the form |
||
| 1529 | * @since 2.05 |
||
| 1530 | */ |
||
| 1531 | private static function show_lone_success_messsage( $atts ) { |
||
| 1532 | global $frm_vars; |
||
| 1533 | $values = FrmEntriesHelper::setup_new_vars( $atts['fields'], $atts['form'], true ); |
||
| 1534 | self::maybe_load_css( $atts['form'], $values['custom_style'], $frm_vars['load_css'] ); |
||
| 1535 | |||
| 1536 | $include_extra_container = 'frm_forms' . FrmFormsHelper::get_form_style_class( $values ); |
||
| 1537 | $errors = array(); |
||
| 1538 | $form = $atts['form']; |
||
| 1539 | $message = $atts['message']; |
||
| 1540 | |||
| 1541 | include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/errors.php' ); |
||
| 1542 | } |
||
| 1543 | |||
| 1544 | /** |
||
| 1545 | * Prepare the success message before it's shown |
||
| 1546 | * @since 2.05 |
||
| 1547 | */ |
||
| 1548 | private static function prepare_submit_message( $form, $entry_id ) { |
||
| 1549 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 1550 | |||
| 1551 | if ( $entry_id && is_numeric( $entry_id ) ) { |
||
| 1552 | $message = isset( $form->options['success_msg'] ) ? $form->options['success_msg'] : $frm_settings->success_msg; |
||
| 1553 | $class = 'frm_message'; |
||
| 1554 | } else { |
||
| 1555 | $message = $frm_settings->failed_msg; |
||
| 1556 | $class = FrmFormsHelper::form_error_class(); |
||
| 1557 | } |
||
| 1558 | |||
| 1559 | $message = FrmFormsHelper::get_success_message( compact( 'message', 'form', 'entry_id', 'class' ) ); |
||
| 1560 | return apply_filters( 'frm_main_feedback', $message, $form, $entry_id ); |
||
| 1561 | } |
||
| 1562 | |||
| 1563 | public static function front_head() { |
||
| 1564 | $version = FrmAppHelper::plugin_version(); |
||
| 1565 | $suffix = FrmAppHelper::js_suffix(); |
||
| 1566 | |||
| 1567 | if ( ! empty( $suffix ) && self::has_combo_js_file() ) { |
||
| 1568 | wp_register_script( 'formidable', FrmAppHelper::plugin_url() . '/js/frm.min.js', array( 'jquery' ), $version, true ); |
||
| 1569 | } else { |
||
| 1570 | wp_register_script( 'formidable', FrmAppHelper::plugin_url() . "/js/formidable{$suffix}.js", array( 'jquery' ), $version, true ); |
||
| 1571 | } |
||
| 1572 | |||
| 1573 | add_filter( 'script_loader_tag', 'FrmFormsController::defer_script_loading', 10, 2 ); |
||
| 1574 | |||
| 1575 | if ( FrmAppHelper::is_admin() ) { |
||
| 1576 | // don't load this in back-end |
||
| 1577 | return; |
||
| 1578 | } |
||
| 1579 | |||
| 1580 | FrmAppHelper::localize_script( 'front' ); |
||
| 1581 | FrmStylesController::enqueue_css( 'register' ); |
||
| 1582 | } |
||
| 1583 | |||
| 1584 | /** |
||
| 1585 | * @since 3.0 |
||
| 1586 | */ |
||
| 1587 | public static function has_combo_js_file() { |
||
| 1589 | } |
||
| 1590 | |||
| 1591 | public static function maybe_load_css( $form, $this_load, $global_load ) { |
||
| 1598 | } |
||
| 1599 | } |
||
| 1600 | |||
| 1601 | public static function defer_script_loading( $tag, $handle ) { |
||
| 1602 | if ( 'recaptcha-api' == $handle && ! strpos( $tag, 'defer' ) ) { |
||
| 1603 | $tag = str_replace( ' src', ' defer="defer" async="async" src', $tag ); |
||
| 1604 | } |
||
| 1605 | return $tag; |
||
| 1606 | } |
||
| 1607 | |||
| 1608 | public static function footer_js( $location = 'footer' ) { |
||
| 1609 | global $frm_vars; |
||
| 1610 | |||
| 1611 | FrmStylesController::enqueue_css(); |
||
| 1612 | |||
| 1613 | if ( ! FrmAppHelper::is_admin() && $location != 'header' && ! empty( $frm_vars['forms_loaded'] ) ) { |
||
| 1614 | //load formidable js |
||
| 1615 | wp_enqueue_script( 'formidable' ); |
||
| 1616 | } |
||
| 1617 | } |
||
| 1618 | |||
| 1619 | /** |
||
| 1620 | * @since 2.0.8 |
||
| 1621 | */ |
||
| 1622 | private static function maybe_minimize_form( $atts, &$content ) { |
||
| 1623 | // check if minimizing is turned on |
||
| 1624 | if ( self::is_minification_on( $atts ) ) { |
||
| 1625 | $content = str_replace( array( "\r\n", "\r", "\n", "\t", ' ' ), '', $content ); |
||
| 1626 | } |
||
| 1627 | } |
||
| 1628 | |||
| 1629 | /** |
||
| 1630 | * @since 2.0.8 |
||
| 1631 | * @return boolean |
||
| 1632 | */ |
||
| 1633 | private static function is_minification_on( $atts ) { |
||
| 1634 | return isset( $atts['minimize'] ) && ! empty( $atts['minimize'] ); |
||
| 1635 | } |
||
| 1636 | |||
| 1637 | public static function bulk_create_template( $ids ) { |
||
| 1638 | _deprecated_function( __METHOD__, '3.0', 'FrmForm::duplicate( $id, true, true )' ); |
||
| 1639 | FrmAppHelper::permission_check( 'frm_edit_forms' ); |
||
| 1640 | |||
| 1641 | foreach ( $ids as $id ) { |
||
| 1642 | FrmForm::duplicate( $id, true, true ); |
||
| 1643 | } |
||
| 1644 | |||
| 1645 | return __( 'Form template was Successfully Created', 'formidable' ); |
||
| 1646 | } |
||
| 1647 | } |
||
| 1648 |