Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FrmFormsHelper 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 FrmFormsHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmFormsHelper { |
||
| 7 | |||
| 8 | public static function maybe_get_form( &$form ) { |
||
| 9 | _deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::maybe_get_form' ); |
||
| 10 | FrmForm::maybe_get_form( $form ); |
||
| 11 | } |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @since 2.2.10 |
||
| 15 | */ |
||
| 16 | public static function form_error_class() { |
||
| 17 | return apply_filters( 'frm_form_error_class', 'frm_error_style' ); |
||
| 18 | } |
||
| 19 | |||
| 20 | public static function get_direct_link( $key, $form = false ) { |
||
| 21 | $target_url = esc_url( admin_url( 'admin-ajax.php?action=frm_forms_preview&form=' . $key ) ); |
||
| 22 | $target_url = apply_filters('frm_direct_link', $target_url, $key, $form); |
||
| 23 | |||
| 24 | return $target_url; |
||
| 25 | } |
||
| 26 | |||
| 27 | public static function forms_dropdown( $field_name, $field_value = '', $args = array() ) { |
||
| 28 | $defaults = array( |
||
| 29 | 'blank' => true, |
||
| 30 | 'field_id' => false, |
||
| 31 | 'onchange' => false, |
||
| 32 | 'exclude' => false, |
||
| 33 | 'class' => '', |
||
| 34 | 'inc_children' => 'exclude', |
||
| 35 | ); |
||
| 36 | $args = wp_parse_args( $args, $defaults ); |
||
| 37 | |||
| 38 | if ( ! $args['field_id'] ) { |
||
| 39 | $args['field_id'] = $field_name; |
||
| 40 | } |
||
| 41 | |||
| 42 | $query = array(); |
||
| 43 | if ( $args['exclude'] ) { |
||
| 44 | $query['id !'] = $args['exclude']; |
||
| 45 | } |
||
| 46 | |||
| 47 | $where = apply_filters('frm_forms_dropdown', $query, $field_name); |
||
| 48 | $forms = FrmForm::get_published_forms( $where, 999, $args['inc_children'] ); |
||
| 49 | $add_html = array(); |
||
| 50 | self::add_html_attr( $args['onchange'], 'onchange', $add_html ); |
||
| 51 | self::add_html_attr( $args['class'], 'class', $add_html ); |
||
| 52 | |||
| 53 | ?> |
||
| 54 | <select name="<?php echo esc_attr( $field_name ); ?>" id="<?php echo esc_attr( $args['field_id'] ) ?>" <?php echo implode( ' ', $add_html ); ?>> |
||
|
|
|||
| 55 | <?php if ( $args['blank'] ) { ?> |
||
| 56 | <option value=""><?php echo ( $args['blank'] == 1 ) ? ' ' : '- ' . esc_attr( $args['blank'] ) . ' -'; ?></option> |
||
| 57 | <?php } ?> |
||
| 58 | <?php foreach ( $forms as $form ) { ?> |
||
| 59 | <option value="<?php echo esc_attr( $form->id ); ?>" <?php selected( $field_value, $form->id ); ?>><?php |
||
| 60 | echo ( '' == $form->name ) ? esc_html__( '(no title)', 'formidable' ) : esc_html( FrmAppHelper::truncate( $form->name, 50 ) ) . ( $form->parent_form_id ? esc_html__( ' (child)', 'formidable' ) : '' ) ; |
||
| 61 | ?></option> |
||
| 62 | <?php } ?> |
||
| 63 | </select> |
||
| 64 | <?php |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $class |
||
| 69 | * @param string $param |
||
| 70 | * @param array $add_html |
||
| 71 | * |
||
| 72 | * @since 2.0.6 |
||
| 73 | */ |
||
| 74 | public static function add_html_attr( $class, $param, &$add_html ) { |
||
| 75 | if ( ! empty( $class ) ) { |
||
| 76 | $add_html[ $param ] = sanitize_title( $param ) . '="' . esc_attr( trim( sanitize_text_field( $class ) ) ) . '"'; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | public static function form_switcher() { |
||
| 81 | $where = apply_filters( 'frm_forms_dropdown', array(), '' ); |
||
| 82 | $forms = FrmForm::get_published_forms( $where ); |
||
| 83 | |||
| 84 | $args = array( 'id' => 0, 'form' => 0 ); |
||
| 85 | if ( isset( $_GET['id'] ) && ! isset( $_GET['form'] ) ) { |
||
| 86 | unset( $args['form'] ); |
||
| 87 | } else if ( isset( $_GET['form']) && ! isset( $_GET['id'] ) ) { |
||
| 88 | unset( $args['id'] ); |
||
| 89 | } |
||
| 90 | |||
| 91 | $frm_action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' ); |
||
| 92 | if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) && in_array( $frm_action, array( 'edit', 'show', 'destroy_all' ) ) ) { |
||
| 93 | $args['frm_action'] = 'list'; |
||
| 94 | $args['form'] = 0; |
||
| 95 | } else if ( FrmAppHelper::is_admin_page('formidable' ) && in_array( $frm_action, array( 'new', 'duplicate' ) ) ) { |
||
| 96 | $args['frm_action'] = 'edit'; |
||
| 97 | } else if ( isset( $_GET['post'] ) ) { |
||
| 98 | $args['form'] = 0; |
||
| 99 | $base = admin_url('edit.php?post_type=frm_display'); |
||
| 100 | } |
||
| 101 | |||
| 102 | ?> |
||
| 103 | <li class="dropdown last" id="frm_bs_dropdown"> |
||
| 104 | <a href="#" id="frm-navbarDrop" class="frm-dropdown-toggle" data-toggle="dropdown"><?php _e( 'Switch Form', 'formidable' ) ?> <b class="caret"></b></a> |
||
| 105 | <ul class="frm-dropdown-menu frm-on-top" role="menu" aria-labelledby="frm-navbarDrop"> |
||
| 106 | <?php |
||
| 107 | foreach ( $forms as $form ) { |
||
| 108 | if ( isset( $args['id'] ) ) { |
||
| 109 | $args['id'] = $form->id; |
||
| 110 | } |
||
| 111 | if ( isset( $args['form'] ) ) { |
||
| 112 | $args['form'] = $form->id; |
||
| 113 | } |
||
| 114 | ?> |
||
| 115 | <li><a href="<?php echo esc_url( isset( $base ) ? add_query_arg( $args, $base ) : add_query_arg( $args ) ); ?>" tabindex="-1"><?php echo esc_html( empty( $form->name ) ? __( '(no title)') : FrmAppHelper::truncate( $form->name, 60 ) ); ?></a></li> |
||
| 116 | <?php |
||
| 117 | unset( $form ); |
||
| 118 | } ?> |
||
| 119 | </ul> |
||
| 120 | </li> |
||
| 121 | <?php |
||
| 122 | } |
||
| 123 | |||
| 124 | public static function get_sortable_classes( $col, $sort_col, $sort_dir ) { |
||
| 125 | echo ($sort_col == $col) ? 'sorted' : 'sortable'; |
||
| 126 | echo ($sort_col == $col && $sort_dir == 'desc') ? ' asc' : ' desc'; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the invalid form error message |
||
| 131 | * |
||
| 132 | * @since 2.02.07 |
||
| 133 | * @param array $args |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public static function get_invalid_error_message( $args ) { |
||
| 137 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 138 | |||
| 139 | $invalid_msg = apply_filters( 'frm_invalid_error_message', $frm_settings->invalid_msg, $args ); |
||
| 140 | |||
| 141 | return $invalid_msg; |
||
| 142 | } |
||
| 143 | |||
| 144 | public static function get_success_message( $atts ) { |
||
| 145 | $message = apply_filters( 'frm_content', $atts['message'], $atts['form'], $atts['entry_id'] ); |
||
| 146 | $message = FrmAppHelper::use_wpautop( do_shortcode( $message ) ); |
||
| 147 | $message = '<div class="' . esc_attr( $atts['class'] ) . '">' . $message . '</div>'; |
||
| 148 | return $message; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Used when a form is created |
||
| 153 | */ |
||
| 154 | public static function setup_new_vars( $values = array() ) { |
||
| 155 | global $wpdb; |
||
| 156 | |||
| 157 | if ( ! empty( $values ) ) { |
||
| 158 | $post_values = $values; |
||
| 159 | } else { |
||
| 160 | $values = array(); |
||
| 161 | $post_values = isset($_POST) ? $_POST : array(); |
||
| 162 | } |
||
| 163 | |||
| 164 | foreach ( array( 'name' => '', 'description' => '' ) as $var => $default ) { |
||
| 165 | if ( ! isset( $values[ $var ] ) ) { |
||
| 166 | $values[ $var ] = FrmAppHelper::get_param( $var, $default ); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | $values['description'] = FrmAppHelper::use_wpautop($values['description']); |
||
| 171 | |||
| 172 | foreach ( array( 'form_id' => '', 'logged_in' => '', 'editable' => '', 'default_template' => 0, 'is_template' => 0, 'status' => 'draft', 'parent_form_id' => 0 ) as $var => $default ) { |
||
| 173 | if ( ! isset( $values[ $var ] ) ) { |
||
| 174 | $values[ $var ] = FrmAppHelper::get_param( $var, $default ); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | if ( ! isset( $values['form_key'] ) ) { |
||
| 179 | $values['form_key'] = ( $post_values && isset( $post_values['form_key'] ) ) ? $post_values['form_key'] : FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_forms', 'form_key' ); |
||
| 180 | } |
||
| 181 | |||
| 182 | $values = self::fill_default_opts( $values, false, $post_values ); |
||
| 183 | $values['custom_style'] = FrmAppHelper::custom_style_value( $post_values ); |
||
| 184 | |||
| 185 | return apply_filters('frm_setup_new_form_vars', $values); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Used when editing a form |
||
| 190 | */ |
||
| 191 | public static function setup_edit_vars( $values, $record, $post_values = array() ) { |
||
| 192 | if ( empty( $post_values ) ) { |
||
| 193 | $post_values = stripslashes_deep( $_POST ); |
||
| 194 | } |
||
| 195 | |||
| 196 | $values['form_key'] = isset($post_values['form_key']) ? $post_values['form_key'] : $record->form_key; |
||
| 197 | $values['default_template'] = isset($post_values['default_template']) ? $post_values['default_template'] : $record->default_template; |
||
| 198 | $values['is_template'] = isset($post_values['is_template']) ? $post_values['is_template'] : $record->is_template; |
||
| 199 | $values['status'] = $record->status; |
||
| 200 | |||
| 201 | $values = self::fill_default_opts($values, $record, $post_values); |
||
| 202 | |||
| 203 | return apply_filters('frm_setup_edit_form_vars', $values); |
||
| 204 | } |
||
| 205 | |||
| 206 | public static function fill_default_opts( $values, $record, $post_values ) { |
||
| 207 | |||
| 208 | $defaults = self::get_default_opts(); |
||
| 209 | foreach ( $defaults as $var => $default ) { |
||
| 210 | if ( is_array($default) ) { |
||
| 211 | if ( ! isset( $values[ $var ] ) ) { |
||
| 212 | $values[ $var ] = ( $record && isset( $record->options[ $var ] ) ) ? $record->options[ $var ] : array(); |
||
| 213 | } |
||
| 214 | |||
| 215 | foreach ( $default as $k => $v ) { |
||
| 216 | $values[ $var ][ $k ] = ( $post_values && isset( $post_values[ $var ][ $k ] ) ) ? $post_values[ $var ][ $k ] : ( ( $record && isset( $record->options[ $var ] ) && isset( $record->options[ $var ][ $k ] ) ) ? $record->options[ $var ][ $k ] : $v); |
||
| 217 | |||
| 218 | if ( is_array( $v ) ) { |
||
| 219 | foreach ( $v as $k1 => $v1 ) { |
||
| 220 | $values[ $var ][ $k ][ $k1 ] = ( $post_values && isset( $post_values[ $var ][ $k ][ $k1 ] ) ) ? $post_values[ $var ][ $k ][ $k1 ] : ( ( $record && isset( $record->options[ $var ] ) && isset( $record->options[ $var ][ $k ] ) && isset( $record->options[ $var ][ $k ][ $k1 ] ) ) ? $record->options[ $var ][ $k ][ $k1 ] : $v1 ); |
||
| 221 | unset( $k1, $v1 ); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | unset($k, $v); |
||
| 226 | } |
||
| 227 | } else { |
||
| 228 | $values[ $var ] = ( $post_values && isset( $post_values['options'][ $var ] ) ) ? $post_values['options'][ $var ] : ( ( $record && isset( $record->options[ $var ] ) ) ? $record->options[ $var ] : $default ); |
||
| 229 | } |
||
| 230 | |||
| 231 | unset($var, $default); |
||
| 232 | } |
||
| 233 | |||
| 234 | return $values; |
||
| 235 | } |
||
| 236 | |||
| 237 | public static function get_default_opts() { |
||
| 238 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 239 | |||
| 240 | return array( |
||
| 241 | 'submit_value' => $frm_settings->submit_value, 'success_action' => 'message', |
||
| 242 | 'success_msg' => $frm_settings->success_msg, 'show_form' => 0, 'akismet' => '', |
||
| 243 | 'no_save' => 0, 'ajax_load' => 0, 'form_class' => '', 'custom_style' => 1, |
||
| 244 | 'before_html' => self::get_default_html('before'), |
||
| 245 | 'after_html' => '', |
||
| 246 | 'submit_html' => self::get_default_html('submit'), |
||
| 247 | ); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param array $options |
||
| 252 | * @param array $values |
||
| 253 | * @since 2.0.6 |
||
| 254 | */ |
||
| 255 | public static function fill_form_options( &$options, $values ) { |
||
| 256 | $defaults = self::get_default_opts(); |
||
| 257 | foreach ( $defaults as $var => $default ) { |
||
| 258 | $options[ $var ] = isset( $values['options'][ $var ] ) ? $values['options'][ $var ] : $default; |
||
| 259 | unset( $var, $default ); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $loc |
||
| 265 | */ |
||
| 266 | public static function get_default_html( $loc ) { |
||
| 267 | if ( $loc == 'submit' ) { |
||
| 268 | $sending = __( 'Sending', 'formidable' ); |
||
| 269 | $draft_link = self::get_draft_link(); |
||
| 270 | $img = '[frmurl]/images/ajax_loader.gif'; |
||
| 271 | $default_html = <<<SUBMIT_HTML |
||
| 272 | <div class="frm_submit"> |
||
| 273 | [if back_button]<input type="button" value="[back_label]" name="frm_prev_page" formnovalidate="formnovalidate" class="frm_prev_page" [back_hook] />[/if back_button] |
||
| 274 | <input type="submit" value="[button_label]" [button_action] /> |
||
| 275 | <img class="frm_ajax_loading" src="$img" alt="$sending"/> |
||
| 276 | $draft_link |
||
| 277 | </div> |
||
| 278 | SUBMIT_HTML; |
||
| 279 | } else if ( $loc == 'before' ) { |
||
| 280 | $default_html = <<<BEFORE_HTML |
||
| 281 | <legend class="frm_hidden">[form_name]</legend> |
||
| 282 | [if form_name]<h3 class="frm_form_title">[form_name]</h3>[/if form_name] |
||
| 283 | [if form_description]<div class="frm_description">[form_description]</div>[/if form_description] |
||
| 284 | BEFORE_HTML; |
||
| 285 | } else { |
||
| 286 | $default_html = ''; |
||
| 287 | } |
||
| 288 | |||
| 289 | return $default_html; |
||
| 290 | } |
||
| 291 | |||
| 292 | public static function get_draft_link() { |
||
| 293 | $link = '[if save_draft]<a href="#" class="frm_save_draft" [draft_hook]>[draft_label]</a>[/if save_draft]'; |
||
| 294 | return $link; |
||
| 295 | } |
||
| 296 | |||
| 297 | public static function get_custom_submit( $html, $form, $submit, $form_action, $values ) { |
||
| 298 | $button = self::replace_shortcodes($html, $form, $submit, $form_action, $values); |
||
| 299 | if ( ! strpos($button, '[button_action]') ) { |
||
| 300 | return; |
||
| 301 | } |
||
| 302 | |||
| 303 | $button_parts = explode('[button_action]', $button); |
||
| 304 | echo $button_parts[0]; |
||
| 305 | //echo ' id="frm_submit_"'; |
||
| 306 | |||
| 307 | $classes = apply_filters('frm_submit_button_class', array(), $form); |
||
| 308 | if ( ! empty($classes) ) { |
||
| 309 | echo ' class="' . esc_attr( implode( ' ', $classes ) ) . '"'; |
||
| 310 | } |
||
| 311 | |||
| 312 | do_action('frm_submit_button_action', $form, $form_action); |
||
| 313 | echo $button_parts[1]; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Automatically add end section fields if they don't exist (2.0 migration) |
||
| 318 | * @since 2.0 |
||
| 319 | * |
||
| 320 | * @param boolean $reset_fields |
||
| 321 | */ |
||
| 322 | public static function auto_add_end_section_fields( $form, $fields, &$reset_fields ) { |
||
| 323 | if ( empty( $fields ) ) { |
||
| 324 | return; |
||
| 325 | } |
||
| 326 | |||
| 327 | $end_section_values = apply_filters( 'frm_before_field_created', FrmFieldsHelper::setup_new_vars( 'end_divider', $form->id ) ); |
||
| 328 | $open = $prev_order = false; |
||
| 329 | $add_order = 0; |
||
| 330 | $last_field = false; |
||
| 331 | foreach ( $fields as $field ) { |
||
| 332 | if ( $prev_order === $field->field_order ) { |
||
| 333 | $add_order++; |
||
| 334 | } |
||
| 335 | |||
| 336 | if ( $add_order ) { |
||
| 337 | $reset_fields = true; |
||
| 338 | $field->field_order = $field->field_order + $add_order; |
||
| 339 | FrmField::update( $field->id, array( 'field_order' => $field->field_order ) ); |
||
| 340 | } |
||
| 341 | |||
| 342 | switch ( $field->type ) { |
||
| 343 | case 'divider': |
||
| 344 | // create an end section if open |
||
| 345 | self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $field, 'move' ); |
||
| 346 | |||
| 347 | // mark it open for the next end section |
||
| 348 | $open = true; |
||
| 349 | break; |
||
| 350 | case 'break'; |
||
| 351 | self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $field, 'move' ); |
||
| 352 | break; |
||
| 353 | case 'end_divider': |
||
| 354 | if ( ! $open ) { |
||
| 355 | // the section isn't open, so this is an extra field that needs to be removed |
||
| 356 | FrmField::destroy( $field->id ); |
||
| 357 | $reset_fields = true; |
||
| 358 | } |
||
| 359 | |||
| 360 | // There is already an end section here, so there is no need to create one |
||
| 361 | $open = false; |
||
| 362 | } |
||
| 363 | $prev_order = $field->field_order; |
||
| 364 | |||
| 365 | $last_field = $field; |
||
| 366 | unset( $field ); |
||
| 367 | } |
||
| 368 | |||
| 369 | self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $last_field ); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Create end section field if it doesn't exist. This is for migration from < 2.0 |
||
| 374 | * Fix any ordering that may be messed up |
||
| 375 | */ |
||
| 376 | public static function maybe_create_end_section( &$open, &$reset_fields, &$add_order, $end_section_values, $field, $move = 'no' ) { |
||
| 377 | if ( ! $open ) { |
||
| 378 | return; |
||
| 379 | } |
||
| 380 | |||
| 381 | $end_section_values['field_order'] = $field->field_order + 1; |
||
| 382 | |||
| 383 | FrmField::create( $end_section_values ); |
||
| 384 | |||
| 385 | if ( $move == 'move' ) { |
||
| 386 | // bump the order of current field unless we're at the end of the form |
||
| 387 | FrmField::update( $field->id, array( 'field_order' => $field->field_order + 2 ) ); |
||
| 388 | } |
||
| 389 | |||
| 390 | $add_order += 2; |
||
| 391 | $open = false; |
||
| 392 | $reset_fields = true; |
||
| 393 | } |
||
| 394 | |||
| 395 | public static function replace_shortcodes( $html, $form, $title = false, $description = false, $values = array() ) { |
||
| 396 | foreach ( array( 'form_name' => $title, 'form_description' => $description, 'entry_key' => true ) as $code => $show ) { |
||
| 397 | if ( $code == 'form_name' ) { |
||
| 398 | $replace_with = $form->name; |
||
| 399 | } else if ( $code == 'form_description' ) { |
||
| 400 | $replace_with = FrmAppHelper::use_wpautop($form->description); |
||
| 401 | } else if ( $code == 'entry_key' && isset($_GET) && isset($_GET['entry']) ) { |
||
| 402 | $replace_with = FrmAppHelper::simple_get( 'entry' ); |
||
| 403 | } else { |
||
| 404 | $replace_with = ''; |
||
| 405 | } |
||
| 406 | |||
| 407 | FrmFieldsHelper::remove_inline_conditions( ( FrmAppHelper::is_true($show) && $replace_with != '' ), $code, $replace_with, $html ); |
||
| 408 | } |
||
| 409 | |||
| 410 | //replace [form_key] |
||
| 411 | $html = str_replace('[form_key]', $form->form_key, $html); |
||
| 412 | |||
| 413 | //replace [frmurl] |
||
| 414 | $html = str_replace('[frmurl]', FrmFieldsHelper::dynamic_default_values( 'frmurl' ), $html); |
||
| 415 | |||
| 416 | if ( strpos( $html, '[button_label]' ) ) { |
||
| 417 | add_filter( 'frm_submit_button', 'FrmFormsHelper::submit_button_label', 1 ); |
||
| 418 | $submit_label = apply_filters( 'frm_submit_button', $title, $form ); |
||
| 419 | $submit_label = esc_attr( do_shortcode( $submit_label ) ); |
||
| 420 | $html = str_replace( '[button_label]', $submit_label, $html ); |
||
| 421 | } |
||
| 422 | |||
| 423 | $html = apply_filters('frm_form_replace_shortcodes', $html, $form, $values); |
||
| 424 | |||
| 425 | if ( strpos( $html, '[if back_button]' ) ) { |
||
| 426 | $html = preg_replace( '/(\[if\s+back_button\])(.*?)(\[\/if\s+back_button\])/mis', '', $html ); |
||
| 427 | } |
||
| 428 | |||
| 429 | if ( strpos( $html, '[if save_draft]' ) ) { |
||
| 430 | $html = preg_replace( '/(\[if\s+save_draft\])(.*?)(\[\/if\s+save_draft\])/mis', '', $html ); |
||
| 431 | } |
||
| 432 | |||
| 433 | if ( apply_filters( 'frm_do_html_shortcodes', true ) ) { |
||
| 434 | $html = do_shortcode( $html ); |
||
| 435 | } |
||
| 436 | |||
| 437 | return $html; |
||
| 438 | } |
||
| 439 | |||
| 440 | public static function submit_button_label( $submit ) { |
||
| 441 | if ( ! $submit || empty($submit) ) { |
||
| 442 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 443 | $submit = $frm_settings->submit_value; |
||
| 444 | } |
||
| 445 | |||
| 446 | return $submit; |
||
| 447 | } |
||
| 448 | |||
| 449 | public static function get_form_style_class( $form = false ) { |
||
| 450 | $style = self::get_form_style($form); |
||
| 451 | $class = ' with_frm_style'; |
||
| 452 | |||
| 453 | if ( empty($style) ) { |
||
| 454 | if ( FrmAppHelper::is_admin_page('formidable-entries') ) { |
||
| 455 | return $class; |
||
| 456 | } else { |
||
| 457 | return; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | |||
| 461 | //If submit button needs to be inline or centered |
||
| 462 | if ( is_object($form) ) { |
||
| 463 | $form = $form->options; |
||
| 464 | } |
||
| 465 | |||
| 466 | $submit_align = isset( $form['submit_align'] ) ? $form['submit_align'] : ''; |
||
| 467 | |||
| 468 | if ( $submit_align == 'inline' ) { |
||
| 469 | $class .= ' frm_inline_form'; |
||
| 470 | } else if ( $submit_align == 'center' ) { |
||
| 471 | $class .= ' frm_center_submit'; |
||
| 472 | } |
||
| 473 | |||
| 474 | $class = apply_filters('frm_add_form_style_class', $class, $style); |
||
| 475 | |||
| 476 | return $class; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param string|boolean $form |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | public static function get_form_style( $form ) { |
||
| 485 | $style = 1; |
||
| 486 | if ( empty( $form ) || 'default' == 'form' ) { |
||
| 487 | return $style; |
||
| 488 | } else if ( is_object( $form ) && $form->parent_form_id ) { |
||
| 489 | // get the parent form if this is a child |
||
| 490 | $form = $form->parent_form_id; |
||
| 491 | } else if ( is_array( $form ) && isset( $form['parent_form_id'] ) && $form['parent_form_id'] ) { |
||
| 492 | $form = $form['parent_form_id']; |
||
| 493 | } else if ( is_array( $form ) && isset( $form['custom_style'] ) ) { |
||
| 494 | $style = $form['custom_style']; |
||
| 495 | } |
||
| 496 | |||
| 497 | if ( $form && is_string( $form ) ) { |
||
| 498 | $form = FrmForm::getOne( $form ); |
||
| 499 | } |
||
| 500 | |||
| 501 | $style = ( $form && is_object( $form ) && isset( $form->options['custom_style'] ) ) ? $form->options['custom_style'] : $style; |
||
| 502 | |||
| 503 | return $style; |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Display the validation error messages when an entry is submitted |
||
| 508 | * |
||
| 509 | * @param array $args - includes img, errors |
||
| 510 | * @since 2.0.6 |
||
| 511 | */ |
||
| 512 | public static function show_errors( $args ) { |
||
| 513 | $invalid_msg = self::get_invalid_error_message( $args ); |
||
| 514 | |||
| 515 | if ( empty( $invalid_msg ) ) { |
||
| 516 | $show_img = false; |
||
| 517 | } else { |
||
| 518 | echo wp_kses_post( $invalid_msg ); |
||
| 519 | $show_img = true; |
||
| 520 | } |
||
| 521 | |||
| 522 | self::show_error( array( 'img' => $args['img'], 'errors' => $args['errors'], 'show_img' => $show_img ) ); |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Display the error message in the front-end along with the image if set |
||
| 527 | * The image was removed from the styling settings, but it may still be set with a hook |
||
| 528 | * If the message in the global settings is empty, show every validation message in the error box |
||
| 529 | * |
||
| 530 | * @param array $args - includes img, errors, and show_img |
||
| 531 | * @since 2.0.6 |
||
| 532 | */ |
||
| 533 | public static function show_error( $args ) { |
||
| 534 | $line_break_first = $args['show_img']; |
||
| 535 | foreach ( $args['errors'] as $error_key => $error ) { |
||
| 536 | if ( $line_break_first && ! is_numeric( $error_key ) && ( $error_key == 'cptch_number' || strpos( $error_key, 'field' ) === 0 ) ) { |
||
| 537 | continue; |
||
| 538 | } |
||
| 539 | |||
| 540 | if ( $line_break_first ) { |
||
| 541 | echo '<br/>'; |
||
| 542 | } |
||
| 543 | |||
| 544 | if ( $args['show_img'] && ! empty( $args['img'] ) ) { |
||
| 545 | ?><img src="<?php echo esc_attr( $args['img'] ) ?>" alt="" /><?php |
||
| 546 | } else { |
||
| 547 | $args['show_img'] = true; |
||
| 548 | } |
||
| 549 | |||
| 550 | echo wp_kses_post( $error ); |
||
| 551 | |||
| 552 | if ( ! $line_break_first ) { |
||
| 553 | echo '<br/>'; |
||
| 554 | } |
||
| 555 | } |
||
| 556 | } |
||
| 557 | |||
| 558 | public static function maybe_get_scroll_js( $id ) { |
||
| 559 | $offset = apply_filters( 'frm_scroll_offset', 4 ); |
||
| 560 | if ( $offset != -1 ) { |
||
| 561 | self::get_scroll_js( $id ) |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | public static function get_scroll_js( $form_id ) { |
||
| 566 | ?><script type="text/javascript">document.addEventListener('DOMContentLoaded',function(){frmFrontForm.scrollMsg(<?php echo (int) $form_id ?>);})</script><?php |
||
| 567 | } |
||
| 568 | |||
| 569 | public static function edit_form_link( $form_id ) { |
||
| 570 | if ( is_object($form_id) ) { |
||
| 571 | $form = $form_id; |
||
| 572 | $name = $form->name; |
||
| 573 | $form_id = $form->id; |
||
| 574 | } else { |
||
| 575 | $name = FrmForm::getName($form_id); |
||
| 576 | } |
||
| 577 | |||
| 578 | if ( $form_id ) { |
||
| 579 | $val = '<a href="' . esc_url( admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id ) ) . '">' . ( '' == $name ? __( '(no title)' ) : FrmAppHelper::truncate( $name, 40 ) ) . '</a>'; |
||
| 580 | } else { |
||
| 581 | $val = ''; |
||
| 582 | } |
||
| 583 | |||
| 584 | return $val; |
||
| 585 | } |
||
| 586 | |||
| 587 | public static function delete_trash_link( $id, $status, $length = 'long' ) { |
||
| 588 | $link = ''; |
||
| 589 | $labels = array( |
||
| 590 | 'restore' => array( |
||
| 591 | 'long' => __( 'Restore from Trash', 'formidable' ), |
||
| 592 | 'short' => __( 'Restore', 'formidable' ), |
||
| 593 | ), |
||
| 594 | 'trash' => array( |
||
| 595 | 'long' => __( 'Move to Trash', 'formidable' ), |
||
| 596 | 'short' => __( 'Trash', 'formidable' ), |
||
| 597 | ), |
||
| 598 | 'delete' => array( |
||
| 599 | 'long' => __( 'Delete Permanently', 'formidable' ), |
||
| 600 | 'short' => __( 'Delete', 'formidable' ), |
||
| 601 | ), |
||
| 602 | ); |
||
| 603 | |||
| 604 | $current_page = isset( $_REQUEST['form_type'] ) ? $_REQUEST['form_type'] : ''; |
||
| 605 | $base_url = '?page=formidable&form_type=' . $current_page . '&id=' . $id; |
||
| 606 | if ( 'trash' == $status ) { |
||
| 607 | $link = '<a href="' . esc_url( wp_nonce_url( $base_url . '&frm_action=untrash', 'untrash_form_' . $id ) ) . '" class="submitdelete deletion">' . $labels['restore'][ $length ] . '</a>'; |
||
| 608 | } else if ( current_user_can('frm_delete_forms') ) { |
||
| 609 | if ( EMPTY_TRASH_DAYS ) { |
||
| 610 | $link = '<a href="' . esc_url( wp_nonce_url( $base_url . '&frm_action=trash', 'trash_form_' . $id ) ) . '" class="submitdelete deletion">' . $labels['trash'][ $length ] . '</a>'; |
||
| 611 | } else { |
||
| 612 | $link = '<a href="' . esc_url( wp_nonce_url( $base_url . '&frm_action=destroy', 'destroy_form_' . $id ) ) . '" class="submitdelete deletion" onclick="return confirm(\'' . esc_attr( __( 'Are you sure you want to delete this form and all its entries?', 'formidable' ) ) . '\')">' . $labels['delete'][ $length ] . '</a>'; |
||
| 613 | } |
||
| 614 | } |
||
| 615 | |||
| 616 | return $link; |
||
| 617 | } |
||
| 618 | |||
| 619 | public static function status_nice_name( $status ) { |
||
| 620 | $nice_names = array( |
||
| 621 | 'draft' => __( 'Draft', 'formidable' ), |
||
| 622 | 'trash' => __( 'Trash', 'formidable' ), |
||
| 623 | 'publish' => __( 'Published', 'formidable' ), |
||
| 624 | ); |
||
| 625 | |||
| 626 | if ( ! in_array($status, array_keys($nice_names)) ) { |
||
| 627 | $status = 'publish'; |
||
| 628 | } |
||
| 629 | |||
| 630 | $name = $nice_names[ $status ]; |
||
| 631 | |||
| 632 | return $name; |
||
| 633 | } |
||
| 634 | |||
| 635 | public static function get_params() { |
||
| 636 | _deprecated_function( __FUNCTION__, '2.0.9', 'FrmForm::list_page_params' ); |
||
| 637 | return FrmForm::list_page_params(); |
||
| 638 | } |
||
| 645 |