Plugin Directory

source: buddyforms/trunk/includes/functions.php

Last change on this file was 3248134, checked in by themekraft, 7 weeks ago

Release 2.8.17

File size: 86.2 KB
Line 
1<?php
2
3if ( !defined( 'ABSPATH' ) ) {
4    exit;
5}
6/**
7 * Add the forms to the admin bar
8 *
9 * @package BuddyForms
10 * @since 0.3 beta
11 */
12add_action(
13    'wp_before_admin_bar_render',
14    'buddyforms_wp_before_admin_bar_render',
15    1,
16    2
17);
18function buddyforms_wp_before_admin_bar_render() {
19    global $wp_admin_bar, $buddyforms;
20    if ( !$buddyforms ) {
21        return;
22    }
23    foreach ( $buddyforms as $key => $buddyform ) {
24        if ( !isset( $buddyform['post_type'] ) || $buddyform['post_type'] == 'none' ) {
25            continue;
26        }
27        if ( isset( $buddyform['admin_bar'][0] ) && $buddyform['post_type'] != 'none' && !empty( $buddyform['attached_page'] ) ) {
28            if ( current_user_can( 'buddyforms_' . $key . '_create' ) ) {
29                $permalink = get_permalink( $buddyform['attached_page'] );
30                $wp_admin_bar->add_menu( array(
31                    'parent' => 'my-account',
32                    'id'     => 'my-account-' . $buddyform['slug'],
33                    'title'  => $buddyform['name'],
34                    'href'   => $permalink,
35                ) );
36                $wp_admin_bar->add_menu( array(
37                    'parent' => 'my-account-' . $buddyform['slug'],
38                    'id'     => 'my-account-' . $buddyform['slug'] . '-view',
39                    'title'  => __( 'View my ', 'buddyforms' ) . $buddyform['name'],
40                    'href'   => $permalink . '/view/' . $buddyform['slug'] . '/',
41                ) );
42                $wp_admin_bar->add_menu( array(
43                    'parent' => 'my-account-' . $buddyform['slug'],
44                    'id'     => 'my-account-' . $buddyform['slug'] . '-new',
45                    'title'  => __( 'New ', 'buddyforms' ) . $buddyform['singular_name'],
46                    'href'   => $permalink . 'create/' . $buddyform['slug'] . '/',
47                ) );
48            }
49        }
50    }
51}
52
53/**
54 * Create the buddyforms post status array.
55 * Other Plugins use the filter buddyforms_get_post_status_array to add there post status to the options array
56 *
57 * @return array
58 */
59function buddyforms_get_post_status_array() {
60    $status_array = array(
61        'publish' => __( 'Publish', 'buddyforms' ),
62        'pending' => __( 'Pending Review', 'buddyforms' ),
63        'draft'   => __( 'Draft', 'buddyforms' ),
64        'future'  => __( 'Schedule', 'buddyforms' ),
65        'private' => __( 'Privately Publish', 'buddyforms' ),
66        'trash'   => __( 'Trash', 'buddyforms' ),
67    );
68    return apply_filters( 'buddyforms_get_post_status_array', $status_array );
69}
70
71/**
72 * Restricting users to view only media library items they upload.
73 *
74 * @package BuddyForms
75 * @since 0.5 beta
76 */
77add_action( 'pre_get_posts', 'buddyforms_restrict_media_library' );
78/**
79 * @param $wp_query_obj
80 */
81function buddyforms_restrict_media_library(  $wp_query_obj  ) {
82    global $current_user, $pagenow;
83    if ( is_super_admin( $current_user->ID ) ) {
84        return;
85    }
86    if ( !is_a( $current_user, 'WP_User' ) ) {
87        return;
88    }
89    if ( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' ) {
90        return;
91    }
92    if ( !current_user_can( 'manage_media_library' ) ) {
93        $wp_query_obj->set( 'author', $current_user->ID );
94    }
95    return;
96}
97
98/**
99 * Check if a subscriber have the needed rights to upload images and add this capabilities if needed.
100 *
101 * @package BuddyForms
102 * @since 0.5 beta
103 */
104add_action( 'init', 'buddyforms_allow_subscriber_uploads' );
105function buddyforms_allow_subscriber_uploads() {
106    if ( current_user_can( 'subscriber' ) && !current_user_can( 'upload_files' ) ) {
107        $role = get_role( 'subscriber' );
108        if ( !empty( $role ) ) {
109            $role->add_cap( 'upload_files' );
110        }
111    }
112}
113
114/**
115 * Get the BuddyForms template directory.
116 *
117 * @return string
118 * @since 0.1 beta
119 *
120 * @uses apply_filters()
121 * @package BuddyForms
122 */
123function buddyforms_get_template_directory() {
124    return apply_filters( 'buddyforms_get_template_directory', constant( 'BUDDYFORMS_TEMPLATE_PATH' ) );
125}
126
127/**
128 * Locate a template
129 *
130 * @param $slug
131 *
132 * @param $form_slug
133 *
134 * @package BuddyForms
135 * @since 0.1 beta
136 *
137 * @since 2.3.1
138 */
139function buddyforms_locate_template(  $slug, $form_slug = ''  ) {
140    global 
141        $buddyforms,
142        $bp,
143        $the_lp_query,
144        $current_user,
145        $post_id
146    ;
147    // Backward compatibility @sinde 2.3.3.
148    if ( empty( $form_slug ) ) {
149        global $form_slug;
150    }
151    // Get the current user so its not needed in the templates
152    $current_user = wp_get_current_user();
153    // create the plugin template path
154    $template_path = BUDDYFORMS_TEMPLATE_PATH . 'buddyforms/' . $slug . '.php';
155    /**
156     * Extend the template from 3rd party plugins
157     *
158     * @since 2.5.9
159     */
160    $template_path = apply_filters(
161        'buddyforms_locate_template',
162        $template_path,
163        $slug,
164        $form_slug
165    );
166    // Check if template exist in the child or parent theme and use this path if available
167    if ( $template_file = locate_template( "buddyforms/{$slug}.php", false, false ) ) {
168        $template_path = $template_file;
169    }
170    $empty_post_message = __( 'There were no posts found. Create your first post now! ', 'buddyforms' );
171    if ( !empty( $form_slug ) ) {
172        if ( !empty( $buddyforms[$form_slug]['empty_submit_list_message_text'] ) ) {
173            $empty_post_message = do_shortcode( $buddyforms[$form_slug]['empty_submit_list_message_text'] );
174        } else {
175            $empty_post_message = do_shortcode( buddyforms_default_message_on_empty_submission_list() );
176        }
177    }
178    // Do the include
179    include $template_path;
180}
181
182/**
183 * Retrieves the post excerpt.
184 *
185 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
186 *
187 * @return string Post excerpt.
188 * @since 2.5.17
189 */
190function buddyforms_get_the_excerpt(  $post = null  ) {
191    $post = get_post( $post );
192    if ( empty( $post ) ) {
193        return '';
194    }
195    if ( post_password_required( $post ) ) {
196        return __( 'There is no excerpt because this is a protected post.' );
197    }
198    return apply_filters( 'buddyforms_get_the_excerpt', $post->post_excerpt, $post );
199}
200
201function buddyforms_granted_list_posts_style() {
202    return apply_filters( 'buddyforms_granted_list_post_style', array('list', 'table') );
203}
204
205// Display the WordPress Login Form
206function buddyforms_wp_login_form(  $hide = false, $form_slug = 'none'  ) {
207    // Get The Login Form
208    $form = buddyforms_get_wp_login_form(
209        $form_slug,
210        '',
211        array(
212            'caller'       => 'template',
213            'redirect_url' => esc_url_raw( $_SERVER['REQUEST_URI'] ),
214        ),
215        $hide
216    );
217    echo wp_kses( $form, buddyforms_wp_kses_allowed_atts() );
218}
219
220/**
221 * Create the BuddyForms Login Form
222 *
223 * @param string $form_slug
224 * @param string $title
225 * @param array  $args
226 *
227 * @param bool   $hide
228 *
229 * @return string|boolean
230 */
231function buddyforms_get_wp_login_form(
232    $form_slug = 'none',
233    $title = '',
234    $args = array(),
235    $hide = false
236) {
237    global $buddyforms, $wp;
238    if ( is_admin() ) {
239        return false;
240    }
241    $caller = $redirect_url = $label_username = $label_password = $label_remember = $label_log_in = '';
242    extract( shortcode_atts( array(
243        'caller'         => 'direct',
244        'redirect_url'   => home_url(),
245        'label_username' => __( 'Username or Email Address', 'buddyforms' ),
246        'label_password' => __( 'Password', 'buddyforms' ),
247        'label_remember' => __( 'Remember Me', 'buddyforms' ),
248        'label_log_in'   => __( 'Log In', 'buddyforms' ),
249    ), $args ) );
250    if ( empty( $title ) ) {
251        $title = __( 'You need to be logged in to view this page', 'buddyforms' );
252    }
253    $hide_style = ( $hide ? 'style="display:none"' : '' );
254    $wp_login_form = '<div class="bf-show-login-form" ' . $hide_style . '>';
255    // include own login basic style
256    ob_start();
257    require BUDDYFORMS_INCLUDES_PATH . '/resources/pfbc/Style/LoginStyle.php';
258    $style = ob_get_clean();
259    if ( !empty( $style ) ) {
260        $style = buddyforms_minify_css( $style );
261        file_put_contents( dirname( BUDDYFORMS_INCLUDES_PATH ) . '/assets/css/bf-wp-login-form.css', $style );
262        $login_form_css_url = BUDDYFORMS_ASSETS . 'css/bf-wp-login-form.css';
263        wp_register_style( 'bf-wp-login-form-css', $login_form_css_url );
264        wp_enqueue_style( 'bf-wp-login-form-css' );
265    }
266    $wp_login_form .= '<h3>' . $title . '</h3>';
267    if ( isset( $_GET['bf_login_error_redirect'] ) ) {
268        // Remove query strings form URL.
269        wp_add_inline_script( 'bf-wp-login-form-css', 'window.history.replaceState(null, null, window.location.pathname);', 'after' );
270        $wp_login_form .= '<div class="bf-login-error">';
271        foreach ( $_GET as $key => $value ) {
272            if ( strpos( $key, 'error_msg_' ) !== false ) {
273                $error = str_replace( 'Error: ', '<strong>Error: </strong>', $value );
274                $wp_login_form .= $error . '<br />';
275            }
276        }
277        $wp_login_form .= '</div>';
278    }
279    if ( empty( $redirect_url ) ) {
280        $redirect_url = home_url();
281    }
282    $login_settings = apply_filters( 'buddyforms_loggin_settings', array(
283        'echo'           => false,
284        'form_id'        => 'bf_loginform',
285        'redirect'       => $redirect_url,
286        'id_username'    => 'bf_user_name',
287        'id_password'    => 'bf_user_pass',
288        'label_username' => $label_username,
289        'label_password' => $label_password,
290        'label_remember' => $label_remember,
291        'label_log_in'   => $label_log_in,
292    ) );
293    $wp_login_form .= wp_login_form( $login_settings );
294    if ( $form_slug !== 'none' ) {
295        $wp_login_form = str_replace( '</form>', '<input type="hidden" name="form_slug" value="' . esc_attr( $form_slug ) . '"></form>', $wp_login_form );
296    }
297    $wp_login_form = str_replace( '</form>', '<input type="hidden" name="caller" value="' . esc_attr( $caller ) . '"></form>', $wp_login_form );
298    if ( isset( $wp->request ) ) {
299        $wp_login_form = str_replace( '</form>', '<input type="hidden" name="login_error_redirect" value="' . $wp->request . '"></form>', $wp_login_form );
300    }
301    if ( $form_slug != 'none' ) {
302        if ( $buddyforms[$form_slug]['public_submit'] == 'registration_form' && $buddyforms[$form_slug]['logged_in_only_reg_form'] != 'none' ) {
303            $reg_form_slug = $buddyforms[$form_slug]['logged_in_only_reg_form'];
304            set_query_var( 'bf_form_slug', $reg_form_slug );
305            $wp_login_form = do_shortcode( '[bf form_slug="' . $reg_form_slug . '"]' );
306        }
307    }
308    $wp_login_form .= '</div>';
309    $wp_login_form = apply_filters( 'buddyforms_wp_login_form', $wp_login_form );
310    return $wp_login_form;
311}
312
313function buddyforms_wp_login_errors_redirect(  $errors  ) {
314    global $pagenow;
315    if ( empty( $_POST['login_error_redirect'] ) ) {
316        return $errors;
317    }
318    if ( isset( $_GET['action'] ) && $_GET['action'] === 'logout' ) {
319        return $errors;
320    }
321    if ( isset( $_GET['action'] ) && $_GET['action'] === 'switch_to_user' ) {
322        return $errors;
323    }
324    if ( isset( $_GET['action'] ) && $_GET['action'] === 'switch_to_olduser' ) {
325        return $errors;
326    }
327    if ( $pagenow !== 'wp-login.php' || $_SERVER['REQUEST_METHOD'] !== 'POST' ) {
328        return $errors;
329    }
330    $login_page = sanitize_text_field( wp_unslash( $_POST['login_error_redirect'] ) );
331    $new_login_page_url = home_url( $login_page ) . '?bf_login_error_redirect=1&';
332    $errors = $errors->get_error_messages();
333    for ($i = 0; $i < count( $errors ); $i++) {
334        $new_login_page_url .= 'error_msg_' . $i . '=' . wp_strip_all_tags( $errors[$i] );
335        // Isn't last iteration?
336        if ( $i !== count( $errors ) - 1 ) {
337            $new_login_page_url .= '&';
338        }
339    }
340    wp_redirect( esc_url_raw( $new_login_page_url ) );
341    exit;
342}
343
344add_filter( 'wp_login_errors', 'buddyforms_wp_login_errors_redirect' );
345/**
346 * since 2.5.13
347 * author @gfirem
348 *
349 * @param $wp_register_url
350 *
351 * @return string
352 */
353function buddyforms_register_url(  $wp_register_url  ) {
354    $buddyforms_registration_page = get_option( 'buddyforms_registration_page' );
355    if ( $buddyforms_registration_page != 'none' ) {
356        $new_url = get_permalink( $buddyforms_registration_page );
357        if ( !empty( $new_url ) ) {
358            return $new_url;
359        }
360    }
361    return $wp_register_url;
362}
363
364add_filter( 'register_url', 'buddyforms_register_url' );
365add_filter(
366    'login_form_bottom',
367    'buddyforms_register_link',
368    10,
369    2
370);
371function buddyforms_register_link(  $wp_login_form, $args  ) {
372    if ( $args['form_id'] !== 'bf_loginform' ) {
373        return $wp_login_form;
374    }
375    $buddyforms_registration_page = get_option( 'buddyforms_registration_page' );
376    if ( $buddyforms_registration_page != 'none' ) {
377        $permalink = get_permalink( $buddyforms_registration_page );
378    } else {
379        $permalink = site_url( '/wp-login.php?action=register&redirect_to=' . get_permalink() );
380    }
381    // new login page
382    $wp_login_form .= '<span class="buddyforms-register"><a href="' . $permalink . '">' . __( 'Register', 'buddyforms' ) . '</a></span> &nbsp;';
383    return $wp_login_form;
384}
385
386add_action(
387    'login_form_bottom',
388    'buddyforms_add_lost_password_link',
389    10,
390    2
391);
392function buddyforms_add_lost_password_link(  $wp_login_form, $args  ) {
393    if ( $args['form_id'] !== 'bf_loginform' ) {
394        return $wp_login_form;
395    }
396    $lost_password_url = apply_filters( 'buddyforms_lost_password_url', wp_lostpassword_url() );
397    $wp_login_form .= '<a href="' . esc_url( $lost_password_url ) . '">' . __( 'Lost Password?', 'buddyforms' ) . '</a> ';
398    return $wp_login_form;
399}
400
401// Helper Function to get the Get the REQUEST_URI Vars
402/**
403 * @param $name
404 *
405 * @return int
406 */
407function buddyforms_get_url_var(  $name  ) {
408    $strURL = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
409    $arrVals = explode( '/', $strURL );
410    $found = 0;
411    foreach ( $arrVals as $index => $value ) {
412        if ( $value == $name ) {
413            $found = $index;
414        }
415    }
416    $place = $found + 1;
417    return ( $found == 0 ? 1 : $arrVals[$place] );
418}
419
420/**
421 * Queue some JavaScript code to be output in the footer.
422 *
423 * @param string $code
424 */
425function buddyforms_enqueue_js(  $code  ) {
426    global $wc_queued_js;
427    if ( empty( $wc_queued_js ) ) {
428        $wc_queued_js = '';
429    }
430    $wc_queued_js .= "\n" . $code . "\n";
431}
432
433/**
434 * Display edit post link for post.
435 *
436 * @param string $text Optional. Anchor text.
437 * @param string $before Optional. Display before edit link.
438 * @param string $after Optional. Display after edit link.
439 * @param int    $id Optional. Post ID.
440 *
441 * @param bool   $echo
442 *
443 * @return string|void
444 * @since 2.3.1
445 *
446 * @since 1.0.0
447 */
448function buddyforms_edit_post_link(
449    $text = null,
450    $before = '',
451    $after = '',
452    $id = 0,
453    $echo = true
454) {
455    if ( !($post = get_post( $id )) ) {
456        return;
457    }
458    if ( !($url = buddyforms_get_edit_post_link( $post->ID )) ) {
459        return;
460    }
461    if ( null === $text ) {
462        $text = __( 'Edit This' );
463    }
464    $link = '<a title="' . __( 'Edit', 'buddyforms' ) . '" class="post-edit-link" href="' . $url . '"><span aria-label="' . __( 'Edit', 'buddyforms' ) . '" class="dashicons dashicons-edit"> </span></a>';
465    /**
466     * Filter the post edit link anchor tag.
467     *
468     * @param string $link Anchor tag for the edit link.
469     * @param int $post_id Post ID.
470     * @param string $text Anchor text.
471     *
472     * @since 2.3.0
473     */
474    $result = $before . apply_filters(
475        'edit_post_link',
476        $link,
477        $post->ID,
478        $text
479    ) . $after;
480    if ( $echo ) {
481        echo wp_kses( $result, buddyforms_wp_kses_allowed_atts() );
482    } else {
483        return $result;
484    }
485}
486
487/**
488 * @param $form_slug
489 */
490function buddyforms_post_entry_actions(  $form_slug  ) {
491    if ( !is_user_logged_in() ) {
492        echo '';
493        return;
494    }
495    if ( empty( $form_slug ) ) {
496        echo '';
497        return;
498    }
499    global $buddyforms, $post;
500    if ( !isset( $buddyforms[$form_slug] ) || empty( $buddyforms[$form_slug]['attached_page'] ) ) {
501        echo '';
502        return;
503    }
504    $attached_page = ( !empty( $buddyforms[$form_slug]['attached_page'] ) ? $buddyforms[$form_slug]['attached_page'] : 'none' );
505    if ( $attached_page == 'none' ) {
506        echo '';
507        return;
508    }
509    ?>
510        <ul class="edit_links">
511                <?php 
512    $is_author = buddyforms_is_author( $post->ID );
513    $user_can_all_submission = current_user_can( 'buddyforms_' . $form_slug . '_all' );
514    if ( $is_author || $user_can_all_submission && isset( $buddyforms[$form_slug]['attached_page'] ) ) {
515        $permalink = '';
516        if ( !empty( $buddyforms[$form_slug]['attached_page'] ) ) {
517            $permalink = get_permalink( $buddyforms[$form_slug]['attached_page'] );
518            $permalink = apply_filters( 'buddyforms_the_loop_edit_permalink', $permalink, $buddyforms[$form_slug]['attached_page'] );
519        }
520        if ( empty( $permalink ) ) {
521            return;
522        }
523        if ( is_multisite() ) {
524            if ( apply_filters( 'buddyforms_enable_multisite', false ) ) {
525                if ( isset( $buddyforms[$form_slug]['blog_id'] ) ) {
526                    $current_site = get_current_site();
527                    $form_blog_id = $buddyforms[$form_slug]['blog_id'];
528                    if ( $current_site->blog_id != $form_blog_id ) {
529                        $form_site = get_blog_details( $form_blog_id, array('blog_id', 'blogname') );
530                        $permalink = str_replace( $form_site->path, $current_site->path, $permalink );
531                    }
532                }
533            }
534        }
535        ob_start();
536        $post_form_slug = get_post_meta( $post->ID, '_bf_form_slug', true );
537        if ( $post_form_slug ) {
538            $form_slug = $post_form_slug;
539        }
540        $current_user_can_edit = apply_filters(
541            'buddyforms_user_can_edit',
542            current_user_can( 'buddyforms_' . $form_slug . '_edit' ),
543            $form_slug,
544            $post->ID
545        );
546        $current_user_can_all = apply_filters(
547            'buddyforms_user_can_all',
548            current_user_can( 'buddyforms_' . $form_slug . '_all' ),
549            $form_slug,
550            $post->ID
551        );
552        $current_user_can_delete = apply_filters(
553            'buddyforms_user_can_delete',
554            current_user_can( 'buddyforms_' . $form_slug . '_delete' ),
555            $form_slug,
556            $post->ID
557        );
558        $current_user_can_create = apply_filters(
559            'buddyforms_user_can_create',
560            current_user_can( 'buddyforms_' . $form_slug . '_create' ),
561            $form_slug,
562            $post->ID
563        );
564        $current_user_can_draft = apply_filters(
565            'buddyforms_user_can_draft',
566            current_user_can( 'buddyforms_' . $form_slug . '_draft' ),
567            $form_slug,
568            $post->ID
569        );
570        $current_post_is_draft = $post->post_status == 'draft';
571        $current_user_edit_draft = $current_user_can_create && !$current_user_can_edit && $current_post_is_draft && $current_user_can_draft;
572        if ( isset( $buddyforms[$form_slug]['form_type'] ) && $buddyforms[$form_slug]['form_type'] != 'contact' ) {
573            if ( $current_user_can_edit || $current_user_can_all || $current_user_edit_draft ) {
574                echo '<li>';
575                if ( isset( $buddyforms[$form_slug]['edit_link'] ) && $buddyforms[$form_slug]['edit_link'] != 'none' ) {
576                    echo wp_kses( apply_filters(
577                        'buddyforms_loop_edit_post_link',
578                        '<a title="' . esc_attr__( 'Edit', 'buddyforms' ) . '" id="' . get_the_ID() . '" class="bf_edit_post" href="' . $permalink . 'edit/' . $form_slug . '/' . get_the_ID() . '"><span aria-label="' . esc_attr__( 'Edit', 'buddyforms' ) . '" class="dashicons dashicons-edit"> </span> ' . esc_attr__( 'Edit', 'buddyforms' ) . '</a>',
579                        get_the_ID(),
580                        $form_slug
581                    ), buddyforms_wp_kses_allowed_atts() );
582                } else {
583                    echo wp_kses( apply_filters(
584                        'buddyforms_loop_edit_post_link',
585                        buddyforms_edit_post_link(
586                            '<span aria-label="' . esc_attr__( 'Edit', 'buddyforms' ) . '" class="dashicons dashicons-edit"> </span> ' . esc_attr__( 'Edit', 'buddyforms' ),
587                            '',
588                            '',
589                            0,
590                            false
591                        ),
592                        get_the_ID(),
593                        $form_slug
594                    ), buddyforms_wp_kses_allowed_atts() );
595                }
596                echo '</li>';
597            }
598        }
599        if ( $current_user_can_delete || $current_user_can_all ) {
600            echo '<li>';
601            echo '<a title="' . esc_attr__( 'Delete', 'buddyforms' ) . '"  id="' . get_the_ID() . '" class="bf_delete_post" href="#"><span aria-label="' . esc_attr__( 'Delete', 'buddyforms' ) . '" title="' . esc_attr__( 'Delete', 'buddyforms' ) . '" class="dashicons dashicons-trash"> </span> ' . esc_attr__( 'Delete', 'buddyforms' ) . '</a></li>';
602            echo '</li>';
603        }
604        // Add custom actions to the entry
605        do_action( 'buddyforms_the_loop_actions', get_the_ID() );
606        $meta_tmp = ob_get_clean();
607        // Display all actions
608        echo wp_kses( apply_filters( 'buddyforms_the_loop_meta_html', $meta_tmp ), buddyforms_wp_kses_allowed_atts() );
609    }
610    do_action( 'buddyforms_the_loop_actions_last', get_the_ID() );
611    ?>
612        </ul>
613        <?php 
614}
615
616/**
617 * Determinate if the current user is the user of the given post
618 *
619 * @param $post_id
620 *
621 * @return bool
622 */
623function buddyforms_is_author(  $post_id  ) {
624    $is_author = false;
625    if ( get_post_field( 'post_author', $post_id ) == get_current_user_id() ) {
626        $is_author = true;
627    }
628    $form_slug = get_post_field( '_bf_form_slug', $post_id );
629    $is_author = apply_filters(
630        'buddyforms_user_can_edit',
631        $is_author,
632        $form_slug,
633        $post_id
634    );
635    return $is_author;
636}
637
638/**
639 * @param $post_status
640 */
641function buddyforms_post_status_readable(  $post_status  ) {
642    echo wp_kses_post( buddyforms_get_post_status_readable( $post_status ) );
643}
644
645/**
646 * @param $post_status
647 *
648 * @return string
649 */
650function buddyforms_get_post_status_readable(  $post_status  ) {
651    if ( $post_status == 'publish' ) {
652        return __( 'Published', 'buddyforms' );
653    }
654    if ( $post_status == 'draft' ) {
655        return __( 'Draft', 'buddyforms' );
656    }
657    if ( $post_status == 'pending' ) {
658        return __( 'Pending Review', 'buddyforms' );
659    }
660    if ( $post_status == 'future' ) {
661        return __( 'Scheduled', 'buddyforms' );
662    }
663    if ( $post_status == 'awaiting-review' ) {
664        return __( 'Awaiting Review', 'buddyforms' );
665    }
666    if ( $post_status == 'edit-draft' ) {
667        return __( 'Edit Draft', 'buddyforms' );
668    }
669    return apply_filters( 'buddyforms_get_post_status_readable', $post_status );
670}
671
672/**
673 * @param $post_status
674 * @param $form_slug
675 */
676function buddyforms_post_status_css_class(  $post_status, $form_slug  ) {
677    echo wp_kses_post( buddyforms_get_post_status_css_class( $post_status, $form_slug ) );
678}
679
680/**
681 * @param $post_status
682 * @param $form_slug
683 *
684 * @return string
685 */
686function buddyforms_get_post_status_css_class(  $post_status, $form_slug  ) {
687    $post_status_css = $post_status;
688    if ( $post_status == 'pending' ) {
689        $post_status_css = 'bf-pending';
690    }
691    return apply_filters( 'buddyforms_post_status_css', $post_status_css, $form_slug );
692}
693
694/**
695 * Allow to remove method for an hook when, it's a class method used and class don't have global for instanciation !
696 *
697 * @param string $hook_name
698 * @param string $method_name
699 * @param int    $priority
700 *
701 * @return bool
702 */
703function buddyforms_remove_filters_with_method_name(  $hook_name = '', $method_name = '', $priority = 0  ) {
704    global $wp_filter;
705    // Take only filters on right hook name and priority
706    if ( !isset( $wp_filter[$hook_name][$priority] ) || !is_array( $wp_filter[$hook_name][$priority] ) ) {
707        return false;
708    }
709    // Loop on filters registered
710    foreach ( (array) $wp_filter[$hook_name][$priority] as $unique_id => $filter_array ) {
711        // Test if filter is an array ! (always for class/method)
712        if ( isset( $filter_array['function'] ) && is_array( $filter_array['function'] ) ) {
713            // Test if object is a class and method is equal to param !
714            if ( is_object( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) && $filter_array['function'][1] == $method_name ) {
715                unset($wp_filter[$hook_name][$priority][$unique_id]);
716            }
717        }
718    }
719    return false;
720}
721
722/**
723 * Allow to remove method for an hook when, it's a class method used and class don't have variable, but you know the class name :)
724 *
725 * @param string $hook_name
726 * @param string $class_name
727 * @param string $method_name
728 * @param int    $priority
729 *
730 * @return bool
731 */
732function buddyforms_remove_filters_for_anonymous_class(
733    $hook_name = '',
734    $class_name = '',
735    $method_name = '',
736    $priority = 0
737) {
738    global $wp_filter;
739    // Take only filters on right hook name and priority
740    if ( !isset( $wp_filter[$hook_name][$priority] ) || !is_array( $wp_filter[$hook_name][$priority] ) ) {
741        return false;
742    }
743    // Loop on filters registered
744    foreach ( (array) $wp_filter[$hook_name][$priority] as $unique_id => $filter_array ) {
745        // Test if filter is an array ! (always for class/method)
746        if ( isset( $filter_array['function'] ) && is_array( $filter_array['function'] ) ) {
747            // Test if object is a class, class and method is equal to param !
748            if ( is_object( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) == $class_name && $filter_array['function'][1] == $method_name ) {
749                unset($wp_filter[$hook_name][$priority][$unique_id]);
750            }
751        }
752    }
753    return false;
754}
755
756/**
757 * Get all taxonomies
758 *
759 * @param $post_type
760 *
761 * @return
762 * @package BuddyForms
763 * @since 0.1-beta
764 */
765function buddyforms_taxonomies(  $post_type  ) {
766    $taxonomies_array = get_object_taxonomies( $post_type, 'objects' );
767    $taxonomies['none'] = 'Select a Taxonomy';
768    foreach ( $taxonomies_array as $tax_slug => $tax ) {
769        $taxonomies[$tax->name] = $tax->label;
770    }
771    return $taxonomies;
772}
773
774function buddyforms_metabox_go_pro() {
775    buddyforms_go_pro(
776        '<span> </span>',
777        '',
778        array(__( 'Priority Support', 'buddyforms' ), __( 'More Form Elements', 'buddyforms' ), __( 'More Options', 'buddyforms' )),
779        false
780    );
781    buddyforms_go_pro(
782        '<span> </span>',
783        __( 'Full Control', 'buddyforms' ),
784        array(
785            __( 'Use your form in the backend admin edit screen like ACF', 'buddyforms' ),
786            __( 'Control who can create, edit and delete content', 'buddyforms' ),
787            __( 'Registration Options', 'buddyforms' ),
788            __( 'Disable ajax form submission', 'buddyforms' ),
789            __( 'Local Storage', 'buddyforms' ),
790            __( 'More Notification Options', 'buddyforms' ),
791            __( 'Import - Export Forms', 'buddyforms' )
792        ),
793        false
794    );
795    buddyforms_go_pro(
796        '<span> </span>',
797        __( 'Permissions Management', 'buddyforms' ),
798        array(__( 'Manage User Roles', 'buddyforms' ), __( 'Manage Capabilities', 'buddyforms' ), __( 'More Validation Options', 'buddyforms' )),
799        false
800    );
801    buddyforms_go_pro(
802        '<span> </span>',
803        __( 'More Post Options', 'buddyforms' ),
804        array(
805            __( 'All Post Types', 'buddyforms' ),
806            __( 'Posts Revision', 'buddyforms' ),
807            __( 'Comment Status', 'buddyforms' ),
808            __( 'Enable Login on the form', 'buddyforms' ),
809            __( 'Create an account during submission?', 'buddyforms' ),
810            __( 'Featured Image Support', 'buddyforms' )
811        ),
812        false
813    );
814    buddyforms_go_pro( '<span> </span>', __( 'Know Your User', 'buddyforms' ) . '<p><small>' . __( 'Get deep Insights about your Submitter', 'buddyforms' ) . '</small></p>', array(
815        __( 'IP Address', 'buddyforms' ),
816        __( 'Referer', 'buddyforms' ),
817        __( 'Browser', 'buddyforms' ),
818        __( 'Platform', 'buddyforms' ),
819        __( 'Reports', 'buddyforms' ),
820        __( 'User Agent', 'buddyforms' )
821    ) );
822}
823
824/**
825 * Get field by slug
826 *
827 * @param $form_slug
828 * @param $field_slug
829 *
830 * @return bool|array
831 * @author Sven edited by gfirem
832 */
833function buddyforms_get_form_field_by_slug(  $form_slug, $field_slug  ) {
834    $result_field = wp_cache_get( 'buddyforms_get_field_' . $field_slug . '_in_form_' . $form_slug, 'buddyforms' );
835    if ( $result_field === false ) {
836        global $buddyforms;
837        if ( isset( $buddyforms[$form_slug]['form_fields'] ) ) {
838            foreach ( $buddyforms[$form_slug]['form_fields'] as $field_key => $field ) {
839                if ( $field['slug'] == $field_slug ) {
840                    $result_field = $field;
841                    wp_cache_set( 'buddyforms_get_field_' . $field_slug . '_in_form_' . $form_slug, $result_field, 'buddyforms' );
842                    return $result_field;
843                }
844            }
845        }
846    }
847    return $result_field;
848}
849
850/**
851 * Get field by slug
852 *
853 * @param $form_slug
854 * @param $field_slug
855 * @param string     $by
856 *
857 * @return bool|array
858 * @since 2.5.11 Added the $by parameter to specify the comparison parameter
859 * @author Sven edited by gfirem
860 */
861function buddyforms_get_form_field_by(  $form_slug, $field_slug, $by = 'slug'  ) {
862    $result_field = wp_cache_get( 'buddyforms_get_field_' . $field_slug . '_in_form_' . $form_slug, 'buddyforms' );
863    if ( $result_field === false ) {
864        global $buddyforms;
865        if ( isset( $buddyforms[$form_slug]['form_fields'] ) ) {
866            foreach ( $buddyforms[$form_slug]['form_fields'] as $field_key => $field ) {
867                if ( $field[$by] == $field_slug ) {
868                    $result_field = $field;
869                    wp_cache_set( 'buddyforms_get_field_' . $field_slug . '_in_form_' . $form_slug, $result_field, 'buddyforms' );
870                    return $result_field;
871                }
872            }
873        }
874    }
875    return $result_field;
876}
877
878/**
879 * Get field by ID
880 *
881 * @param $form_slug
882 * @param $field_id
883 *
884 * @return bool|array
885 * @since 2.4.6
886 */
887function buddyforms_get_form_field_by_id(  $form_slug, $field_id  ) {
888    $result_field = wp_cache_get( 'buddyforms_get_field_' . $field_id . '_in_form_' . $form_slug, 'buddyforms' );
889    if ( $result_field === false ) {
890        global $buddyforms;
891        if ( isset( $buddyforms[$form_slug]['form_fields'] ) ) {
892            if ( isset( $buddyforms[$form_slug]['form_fields'] ) && isset( $buddyforms[$form_slug]['form_fields'][$field_id] ) ) {
893                $result_field = $buddyforms[$form_slug]['form_fields'][$field_id];
894                wp_cache_set( 'buddyforms_get_field_' . $field_id . '_in_form_' . $form_slug, $result_field, 'buddyforms' );
895                return $result_field;
896            }
897        }
898    }
899    return $result_field;
900}
901
902/**
903 * Return teh array of field belong to the form.
904 *
905 * @param $form_slug
906 *
907 * @return bool|array
908 */
909function buddyforms_get_form_fields(  $form_slug  ) {
910    $result_field = wp_cache_get( 'buddyforms_get_form_fields' . $form_slug, 'buddyforms' );
911    if ( $result_field === false ) {
912        global $buddyforms;
913        if ( empty( $form_slug ) ) {
914            return false;
915        }
916        if ( isset( $buddyforms[$form_slug]['form_fields'] ) ) {
917            $result_fields = $buddyforms[$form_slug]['form_fields'];
918            wp_cache_set( 'buddyforms_get_form_fields' . $form_slug, $result_fields, 'buddyforms' );
919            return $result_fields;
920        }
921    }
922    return $result_field;
923}
924
925/**
926 * Check if field type exist in a form
927 *
928 * @param $form_slug
929 * @param $field_type
930 *
931 * @param string     $search_by
932 *
933 * @return bool
934 * @since 2.5.15 added $search_by
935 */
936function buddyforms_exist_field_type_in_form(  $form_slug, $field_type, $search_by = 'type'  ) {
937    $fields = buddyforms_get_form_fields( $form_slug );
938    $exist = false;
939    if ( empty( $fields ) ) {
940        return $exist;
941    }
942    foreach ( $fields as $field ) {
943        if ( $field[$search_by] == $field_type ) {
944            $exist = true;
945            break;
946        }
947    }
948    return $exist;
949}
950
951//
952// Add Placeholder support top the wp editor
953//
954add_filter( 'mce_external_plugins', 'buddyforms_add_mce_placeholder_plugin' );
955function buddyforms_add_mce_placeholder_plugin(  $plugins  ) {
956    if ( is_admin() ) {
957        return $plugins;
958    }
959    $plugins['placeholder'] = BUDDYFORMS_PLUGIN_URL . 'assets/resources/wp-tinymce-placeholder/mce.placeholder.js';
960    return $plugins;
961}
962
963/**
964 * Add garlic support to the wp editor for local save the content of the textarea
965 *
966 * @param $initArray
967 *
968 * @return mixed
969 */
970function buddyforms_tinymce_setup_function(  $initArray  ) {
971    $initArray['setup'] = 'function(editor) {
972                                editor.on("change keyup", function(e){
973                                        editor.save();
974                                        jQuery(editor.getElement()).trigger(\'change\');
975                                });
976                        }';
977    return $initArray;
978}
979
980/**
981 * Get a form by slug
982 *
983 * @param $form_slug
984 *
985 * @return bool|array
986 */
987function buddyforms_get_form_by_slug(  $form_slug  ) {
988    $value = wp_cache_get( 'buddyforms_form_by_slug_' . $form_slug, 'buddyforms' );
989    if ( $value === false ) {
990        global $buddyforms;
991        if ( isset( $buddyforms[$form_slug] ) ) {
992            $value = $buddyforms[$form_slug];
993            wp_cache_set( 'buddyforms_form_by_slug_' . $form_slug, $value, 'buddyforms' );
994        }
995    }
996    return $value;
997}
998
999/**
1000 * Get form option
1001 *
1002 * @param $form_slug
1003 * @param string    $option
1004 *
1005 * @return string|bool
1006 * @since 2.5.19
1007 */
1008function buddyforms_get_form_option(  $form_slug, $option  ) {
1009    $value = false;
1010    if ( !empty( $form_slug ) && !empty( $option ) ) {
1011        $cache_key = 'buddyforms_form_' . $form_slug . '_option_' . $option;
1012        $value = wp_cache_get( $cache_key, 'buddyforms' );
1013        if ( $value === false ) {
1014            $bf_form = buddyforms_get_form_by_slug( $form_slug );
1015            if ( !empty( $bf_form ) ) {
1016                $value = ( isset( $bf_form[$option] ) ? $bf_form[$option] : false );
1017                wp_cache_set( $cache_key, $value, 'buddyforms' );
1018            }
1019        }
1020    }
1021    return $value;
1022}
1023
1024/**
1025 * Will return the form slug from post meta or the default. none if no form is attached
1026 *
1027 * @param $post_id
1028 *
1029 * @return mixed
1030 * @author Sven edited by gfirem
1031 */
1032function buddyforms_get_form_slug_by_post_id(  $post_id  ) {
1033    $value = wp_cache_get( 'buddyform_form_slug_' . $post_id, 'buddyforms' );
1034    if ( $value === false ) {
1035        $value = get_post_meta( $post_id, '_bf_form_slug', true );
1036        if ( empty( $value ) || isset( $value ) && $value == 'none' ) {
1037            $buddyforms_posttypes_default = get_option( 'buddyforms_posttypes_default' );
1038            $post_type = get_post_type( $post_id );
1039            if ( isset( $buddyforms_posttypes_default[$post_type] ) ) {
1040                $value = $buddyforms_posttypes_default[$post_type];
1041            }
1042        }
1043        wp_cache_set( 'buddyform_form_slug_' . $post_id, $value, 'buddyforms' );
1044    }
1045    return $value;
1046}
1047
1048/**
1049 * Get the post types for teh created forms
1050 *
1051 * @return array
1052 */
1053function buddyforms_get_post_types_from_forms() {
1054    global $buddyforms;
1055    $result = array();
1056    if ( !empty( $buddyforms ) ) {
1057        foreach ( $buddyforms as $form ) {
1058            $result[] = $form['post_type'];
1059        }
1060        $result = array_unique( $result );
1061    }
1062    return $result;
1063}
1064
1065function buddyforms_get_post_types() {
1066    $post_types = array();
1067    // Generate the Post Type Array 'none' == Contact Form
1068    $post_types['bf_submissions'] = __( 'none', 'buddyforms' );
1069    $post_types['post'] = __( 'Post', 'buddyforms' );
1070    $post_types['page'] = __( 'Page', 'buddyforms' );
1071    return $post_types;
1072}
1073
1074/**
1075 * This function return the dropdown populated with the pages of the site including the childs
1076 *
1077 * @param $name
1078 * @param $selected
1079 * @param string   $id
1080 * @param string   $default_option_string
1081 * @param string   $default_option_value
1082 * @param string   $view
1083 *
1084 * @return string
1085 * @author gfirem
1086 *
1087 * @since 2.5.10
1088 */
1089function buddyforms_get_all_pages_dropdown(
1090    $name,
1091    $selected,
1092    $id = '',
1093    $default_option_string = 'WordPress Default',
1094    $default_option_value = 'none',
1095    $view = 'form_builder'
1096) {
1097    if ( $default_option_string === 'WordPress Default' ) {
1098        $default_option_string = __( 'WordPress Default', 'buddyforms' );
1099    }
1100    $exclude = array();
1101    $page_on_front = get_option( 'page_on_front' );
1102    if ( !empty( $page_on_front ) && $page_on_front !== 'none' && is_numeric( $page_on_front ) && $page_on_front != $selected ) {
1103        $exclude[] = intval( $page_on_front );
1104    }
1105    if ( $view == 'form_builder' ) {
1106        $buddyforms_registration_page = get_option( 'buddyforms_registration_page' );
1107        if ( !empty( $buddyforms_registration_page ) && $buddyforms_registration_page !== 'none' && is_numeric( $buddyforms_registration_page ) && $buddyforms_registration_page != $selected ) {
1108            $exclude[] = intval( $buddyforms_registration_page );
1109        }
1110    }
1111    $args = array(
1112        'depth'             => 0,
1113        'post_type'         => 'page',
1114        'exclude_tree'      => $exclude,
1115        'selected'          => $selected,
1116        'name'              => $name,
1117        'id'                => ( !empty( $id ) ? $id : $name ),
1118        'show_option_none'  => $default_option_string,
1119        'option_none_value' => $default_option_value,
1120        'sort_column'       => 'post_title',
1121        'echo'              => 0,
1122    );
1123    return wp_kses( wp_dropdown_pages( $args ), buddyforms_wp_kses_allowed_atts() );
1124}
1125
1126function buddyforms_get_all_pages(
1127    $type = 'id',
1128    $view = 'form_builder',
1129    $exclude_global_submission_endpoint = false,
1130    $extra_exclude_ids = array(),
1131    $default_string = ''
1132) {
1133    $exclude = array();
1134    if ( empty( $default_string ) ) {
1135        $default_string = __( 'Select a Page', 'buddyforms' );
1136    }
1137    // get the page_on_front and exclude it from the query. This page should not get used for the endpoints
1138    $page_on_front = get_option( 'page_on_front' );
1139    if ( !empty( $page_on_front ) ) {
1140        $exclude[] = $page_on_front;
1141    }
1142    if ( $view == 'form_builder' ) {
1143        $buddyforms_registration_page = get_option( 'buddyforms_registration_page' );
1144        if ( !empty( $buddyforms_registration_page ) ) {
1145            $exclude[] = $buddyforms_registration_page;
1146        }
1147    }
1148    if ( !empty( $exclude_global_submission_endpoint ) ) {
1149        $buddyforms_submissions_page = get_option( 'buddyforms_submissions_page' );
1150        if ( !empty( $buddyforms_submissions_page ) ) {
1151            $exclude[] = $buddyforms_submissions_page;
1152        }
1153    }
1154    if ( !empty( $extra_exclude_ids ) ) {
1155        $exclude = array_merge( $extra_exclude_ids, $exclude );
1156    }
1157    $args = array(
1158        'sort_order'  => 'asc',
1159        'sort_column' => 'post_title',
1160        'parent'      => -1,
1161        'post_type'   => 'page',
1162        'post_status' => 'publish',
1163    );
1164    if ( !empty( $exclude ) ) {
1165        $args['exclude'] = $exclude;
1166    }
1167    $pages = get_pages( $args );
1168    $all_pages = array();
1169    $all_pages['none'] = $default_string;
1170    if ( $type == 'id' ) {
1171        // Generate the pages array by id
1172        foreach ( $pages as $page ) {
1173            $all_pages[$page->ID] = $page->post_title;
1174        }
1175    }
1176    if ( $type == 'name' ) {
1177        foreach ( $pages as $page ) {
1178            $all_pages[$page->post_name] = $page->post_title;
1179        }
1180    }
1181    return $all_pages;
1182}
1183
1184add_action( 'admin_bar_menu', 'buddyform_admin_bar_shortcut', 60 );
1185/**
1186 * Add a short-code to the admin toolbar to edit the form in the current screen
1187 *
1188 * @param WP_Admin_Bar $wp_admin_bar
1189 */
1190function buddyform_admin_bar_shortcut(  $wp_admin_bar  ) {
1191    if ( is_admin() && is_user_logged_in() ) {
1192        return;
1193    }
1194    global $post, $buddyforms;
1195    if ( empty( $post->ID ) ) {
1196        return;
1197    }
1198    $form_slug = '';
1199    global $wp_query;
1200    if ( !empty( $wp_query->query_vars['bf_form_slug'] ) ) {
1201        $form_slug = sanitize_title( $wp_query->query_vars['bf_form_slug'] );
1202    } elseif ( !empty( $post->post_name ) ) {
1203        $form_slug = $post->post_name;
1204    }
1205    if ( empty( $form_slug ) && is_array( $buddyforms ) && !array_key_exists( $form_slug, $buddyforms ) ) {
1206        return;
1207    }
1208    if ( !current_user_can( 'buddyforms_' . $form_slug . '_create' ) ) {
1209        return;
1210    }
1211    $form = get_page_by_path( $form_slug, 'OBJECT', 'buddyforms' );
1212    if ( empty( $form ) ) {
1213        return;
1214    }
1215    $post_url = sprintf( 'post.php?post=%s&action=edit', $form->ID );
1216    $args = array(
1217        'id'    => 'buddyforms-admin-edit-form',
1218        'title' => __( 'Edit BuddyForm', 'buddyforms' ),
1219        'href'  => admin_url( $post_url ),
1220        'meta'  => array(
1221            'data-post_id' => 33,
1222            'class'        => 'admin-bar dashicons-before dashicons-buddyforms',
1223        ),
1224    );
1225    $wp_admin_bar->add_node( $args );
1226}
1227
1228add_action( 'buddyforms_form_hero_last', 'buddyforms_form_footer_terms' );
1229function buddyforms_form_footer_terms(  $html  ) {
1230    $buddyforms_gdpr = get_option( 'buddyforms_gdpr' );
1231    $html .= ' <div class="terms"><p>';
1232    if ( !empty( $buddyforms_gdpr['terms_label'] ) ) {
1233        $html .= '<span id="" class="buddyforms_terms_label">' . $buddyforms_gdpr['terms_label'] . '</span> ';
1234    }
1235    if ( isset( $buddyforms_gdpr['terms'] ) && $buddyforms_gdpr['terms'] != 'none' ) {
1236        $html .= '<a id="" class="" href="' . get_permalink( $buddyforms_gdpr['terms'] ) . '">' . get_the_title( $buddyforms_gdpr['terms'] ) . '</a>';
1237    }
1238    $html .= '</p></div>';
1239    return $html;
1240}
1241
1242/**
1243 * This function is an internal implementation to generate the nonce base on specific user.
1244 * We create this to generate nonce for user not logged in
1245 *
1246 * @readmore wp-includes/pluggable.php:2147
1247 *
1248 * @param int    $action
1249 * @param int    $user_id
1250 * @param string $token
1251 *
1252 * @return bool|string
1253 * @since 2.5.10 Added the token parameter to emulate loggout user nonce
1254 */
1255function buddyforms_create_nonce(  $action = -1, $user_id = 0, $token = ''  ) {
1256    if ( $user_id === 0 ) {
1257        $user = wp_get_current_user();
1258        $uid = (int) $user->ID;
1259        if ( !$uid ) {
1260            /** This filter is documented in wp-includes/pluggable.php */
1261            $uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
1262        }
1263        $token = wp_get_session_token();
1264    } else {
1265        $uid = $user_id;
1266    }
1267    $i = wp_nonce_tick();
1268    return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
1269}
1270
1271function buddyforms_form_display_message(  $form_slug, $post_id, $source = 'after_submit_message_text'  ) {
1272    global $buddyforms;
1273    $display_message = buddyforms_default_message_on_create();
1274    if ( !empty( $buddyforms[$form_slug][$source] ) ) {
1275        $display_message = $buddyforms[$form_slug][$source];
1276    } else {
1277        if ( $source !== 'after_submit_message_text' ) {
1278            $display_message = buddyforms_default_message_on_update();
1279        }
1280    }
1281    $display_message = apply_filters(
1282        'buddyforms_form_display_message',
1283        $display_message,
1284        $form_slug,
1285        $post_id,
1286        $source
1287    );
1288    if ( !empty( $buddyforms[$form_slug]['attached_page'] ) ) {
1289        $permalink = get_permalink( $buddyforms[$form_slug]['attached_page'] );
1290        $display_message = str_ireplace( '[edit_link]', '<a title="' . __( 'Edit Post', 'buddyforms' ) . '" href="' . $permalink . 'edit/' . $form_slug . '/' . $post_id . '">' . __( 'Continue Editing', 'buddyforms' ) . '</a>', $display_message );
1291    }
1292    $display_message = str_ireplace( '[form_singular_name]', $buddyforms[$form_slug]['singular_name'], $display_message );
1293    $display_message = str_ireplace( '[post_title]', get_the_title( $post_id ), $display_message );
1294    $display_message = str_ireplace( '[post_link]', '<a title="' . __( 'Display Post', 'buddyforms' ) . '" href="' . get_permalink( $post_id ) . '">' . __( 'Display Post', 'buddyforms' ) . '</a>', $display_message );
1295    return do_shortcode( $display_message );
1296}
1297
1298function buddyforms_user_fields_array() {
1299    return array(
1300        'user_login',
1301        'user_email',
1302        'user_first',
1303        'user_last',
1304        'user_pass',
1305        'user_website',
1306        'display_name',
1307        'user_bio',
1308        'country',
1309        'state'
1310    );
1311}
1312
1313function buddyforms_default_message_on_update() {
1314    return __( 'Form Updated Successfully.', 'buddyforms' );
1315}
1316
1317function buddyforms_default_message_on_empty_submission_list() {
1318    return __( 'There were no posts found. Create your first post [bf_new_submission_link name="Now"]!', 'buddyforms' );
1319}
1320
1321function buddyforms_default_message_on_create() {
1322    return __( 'Form Submitted Successfully.', 'buddyforms' );
1323}
1324
1325add_action( 'wp_ajax_nopriv_handle_dropped_media', 'buddyforms_upload_handle_dropped_media' );
1326add_action( 'wp_ajax_handle_dropped_media', 'buddyforms_upload_handle_dropped_media' );
1327function buddyforms_upload_handle_dropped_media() {
1328    check_ajax_referer( 'fac_drop', 'nonce' );
1329    $form_slug = ( isset( $_POST['form_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['form_slug'] ) ) : '' );
1330    $current_user = wp_get_current_user();
1331    $current_user_can_edit = bf_user_can(
1332        $current_user->ID,
1333        'buddyforms_' . $form_slug . '_edit',
1334        array(),
1335        $form_slug
1336    );
1337    $current_user_can_create = bf_user_can(
1338        $current_user->ID,
1339        'buddyforms_' . $form_slug . '_create',
1340        array(),
1341        $form_slug
1342    );
1343    $current_user_can_draft = bf_user_can(
1344        $current_user->ID,
1345        'buddyforms_' . $form_slug . '_draft',
1346        array(),
1347        $form_slug
1348    );
1349    if ( $current_user_can_edit || $current_user_can_create || $current_user_can_draft ) {
1350        status_header( 200 );
1351        $newupload = 0;
1352        if ( !empty( $_FILES ) ) {
1353            $files = $_FILES;
1354            foreach ( $files as $file_id => $file ) {
1355                $newupload = media_handle_upload( $file_id, 0 );
1356            }
1357        }
1358        if ( is_wp_error( $newupload ) ) {
1359            status_header( '500' );
1360            echo wp_kses_post( $newupload->get_error_message() );
1361        } else {
1362            status_header( '200' );
1363            echo wp_kses_post( $newupload );
1364        }
1365        die;
1366    } else {
1367        die;
1368    }
1369}
1370
1371add_action( 'wp_ajax_nopriv_handle_deleted_media', 'buddyforms_upload_handle_delete_media' );
1372add_action( 'wp_ajax_handle_deleted_media', 'buddyforms_upload_handle_delete_media' );
1373function buddyforms_upload_handle_delete_media() {
1374    check_ajax_referer( 'fac_drop', 'nonce' );
1375    if ( isset( $_REQUEST['media_id'] ) ) {
1376        $post_id = absint( $_REQUEST['media_id'] );
1377        $post = get_post( $post_id );
1378        $current_user = wp_get_current_user();
1379        if ( $post->post_author == $current_user->ID ) {
1380            $status = wp_delete_attachment( $post_id, true );
1381        } else {
1382            $status = false;
1383        }
1384        if ( $status ) {
1385            echo wp_json_encode( array(
1386                'status' => 'OK',
1387            ) );
1388        } else {
1389            echo wp_json_encode( array(
1390                'status' => 'FAILED',
1391            ) );
1392        }
1393    }
1394    die;
1395}
1396
1397add_action( 'wp_ajax_nopriv_upload_image_from_url', 'buddyforms_upload_image_from_url' );
1398add_action( 'wp_ajax_upload_image_from_url', 'buddyforms_upload_image_from_url' );
1399function buddyforms_upload_image_from_url() {
1400    $url = ( isset( $_REQUEST['url'] ) ? wp_kses_post( wp_unslash( $_REQUEST['url'] ) ) : '' );
1401    $valid_url = strtolower( $url );
1402    if ( strpos( $valid_url, 'phar://' ) !== false || pathinfo( $valid_url, PATHINFO_EXTENSION ) === 'phar' || strpos( $valid_url, 'php://' ) !== false ) {
1403        return false;
1404    }
1405    $file_id = ( isset( $_REQUEST['id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['id'] ) ) : '' );
1406    $accepted_files = ( isset( $_REQUEST['accepted_files'] ) ? explode( ',', buddyforms_sanitize( '', wp_unslash( $_REQUEST['accepted_files'] ) ) ) : array('jpeg') );
1407    if ( !empty( $url ) && !empty( $file_id ) ) {
1408        $upload_dir = wp_upload_dir();
1409        $image_url = urldecode( $url );
1410        $url_response = wp_safe_remote_get( $image_url );
1411        if ( isset( $url_response['response']['code'] ) && $url_response['response']['code'] != 200 ) {
1412            echo wp_json_encode( array(
1413                'status'   => 'FAILED',
1414                'response' => __( 'Error downloading image.', 'buddyforms' ),
1415            ) );
1416            die;
1417        } else {
1418            $image_data = wp_remote_retrieve_body( $url_response );
1419        }
1420        $image_data_information = getimagesize( $image_url );
1421        $image_mime_information = $image_data_information['mime'];
1422        if ( !in_array( $image_mime_information, $accepted_files ) ) {
1423            echo wp_json_encode( array(
1424                'status'   => 'FAILED',
1425                'response' => __( 'File type ' . $image_mime_information . ' is not allowed.', 'buddyforms' ),
1426            ) );
1427            die;
1428        }
1429        if ( $image_data && $image_data_information ) {
1430            $file_name = $file_id . '.png';
1431            $full_path = wp_normalize_path( $upload_dir['path'] . DIRECTORY_SEPARATOR . $file_name );
1432            $upload_file = wp_upload_bits( $file_name, null, $image_data );
1433            if ( !$upload_file['error'] ) {
1434                $wp_filetype = wp_check_filetype( $file_name, null );
1435                $attachment = array(
1436                    'post_mime_type' => $wp_filetype['type'],
1437                    'post_title'     => preg_replace( '/\\.[^.]+$/', '', $file_name ),
1438                    'post_content'   => '',
1439                    'post_status'    => 'inherit',
1440                );
1441                $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'] );
1442                $url = wp_get_attachment_thumb_url( $attachment_id );
1443                echo wp_json_encode( array(
1444                    'status'        => 'OK',
1445                    'response'      => $url,
1446                    'attachment_id' => $attachment_id,
1447                ) );
1448                die;
1449            } else {
1450                echo wp_json_encode( array(
1451                    'status'   => 'FAILED',
1452                    'response' => 'Error uploading image.',
1453                ) );
1454                die;
1455            }
1456        } else {
1457            echo wp_json_encode( array(
1458                'status'   => 'FAILED',
1459                'response' => 'The Url provided is not an image.',
1460            ) );
1461            die;
1462        }
1463    } else {
1464        echo wp_json_encode( array(
1465            'status'   => 'FAILED',
1466            'response' => 'Wrong Format or Empty Url.',
1467        ) );
1468        die;
1469    }
1470}
1471
1472/**
1473 * Check if a file was include into the global php queue
1474 *
1475 * @param $file_name
1476 *
1477 * @return bool
1478 * @since 2.2.8
1479 *
1480 * @author gfirem
1481 */
1482function buddyforms_check_loaded_file(  $file_name  ) {
1483    $includes_files = get_included_files();
1484    return in_array( $file_name, $includes_files );
1485}
1486
1487function buddyform_get_role_names() {
1488    global $wp_roles;
1489    if ( !isset( $wp_roles ) ) {
1490        $wp_roles = new WP_Roles();
1491    }
1492    return $wp_roles->get_names();
1493}
1494
1495/**
1496 * Get a tag inside a shortcode from a given content.
1497 *
1498 * @param array  $shortcodes
1499 * @param array  $targets_tags
1500 * @param string $content
1501 *
1502 * @return string
1503 * @since 2.3.1
1504 */
1505function buddyforms_get_shortcode_tag(  $shortcodes, $targets_tags, $content  ) {
1506    if ( !is_array( $shortcodes ) || !is_array( $targets_tags ) ) {
1507        return '';
1508    }
1509    foreach ( $shortcodes as $shortcode ) {
1510        $regrex = sprintf( '(\\[%s)(.*?)form_slug=\\"(.*?)\\"', $shortcode );
1511        preg_match_all( "/{$regrex}/m", $content, $match );
1512        if ( !empty( $match ) && !empty( $match[1][0] ) && $match[1][0] === '[' . $shortcode && !empty( $match[3][0] ) ) {
1513            return $match[3][0];
1514        }
1515    }
1516    $pattern = get_shortcode_regex();
1517    $result = '';
1518    preg_replace_callback( "/{$pattern}/m", function ( $tag ) use($shortcodes, $targets_tags, &$result) {
1519        foreach ( $shortcodes as $shortcode_item ) {
1520            if ( $shortcode_item === $tag[2] ) {
1521                $attributes = shortcode_parse_atts( $tag[3] );
1522                if ( !empty( $attributes ) ) {
1523                    foreach ( $targets_tags as $target_item ) {
1524                        if ( array_key_exists( $target_item, $attributes ) ) {
1525                            $result = $attributes[$target_item];
1526                            return $tag[0];
1527                        }
1528                    }
1529                }
1530            }
1531        }
1532        return $tag[0];
1533    }, $content );
1534    return $result;
1535}
1536
1537/**
1538 * Extract the form slug from a html inside the given content reading the inout hidden with the Id `form_slug`
1539 *
1540 * @param $content
1541 *
1542 * @return string
1543 */
1544function buddyforms_get_form_slug_from_html(  $content  ) {
1545    if ( !empty( $content ) ) {
1546        try {
1547            libxml_use_internal_errors( true );
1548            $dom = new DOMDocument();
1549            $dom->validateOnParse = false;
1550            $content = mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' );
1551            $dom->loadHTML( $content );
1552            $form_input_node = $dom->getElementById( 'form_slug' );
1553            libxml_use_internal_errors( false );
1554            if ( !empty( $form_input_node ) && $form_input_node instanceof DOMElement ) {
1555                return $form_input_node->getAttribute( 'value' );
1556            }
1557        } catch ( Exception $e ) {
1558        }
1559    }
1560    return '';
1561}
1562
1563/**
1564 * Extract the form slug from a shortcode inside the given content
1565 *
1566 * @param $content
1567 * @param array   $shortcodes
1568 *
1569 * @return string
1570 */
1571function buddyforms_get_form_slug_from_shortcode(  $content, $shortcodes = array('bf', 'buddyforms_form')  ) {
1572    $form_slug = buddyforms_get_shortcode_tag( $shortcodes, array('form_slug', 'id'), $content );
1573    if ( is_numeric( $form_slug ) ) {
1574        $form_post = get_post( $form_slug );
1575        $form_slug = $form_post->post_name;
1576    }
1577    return $form_slug;
1578}
1579
1580/**
1581 * Extract the form slug from a shortcode inside the given content, if exist the shortcode or reading the hidden input form_slug from the html
1582 *
1583 * @param $content
1584 * @param array   $shortcodes
1585 *
1586 * @return string
1587 */
1588function buddyforms_get_form_slug_from_content(  $content, $shortcodes = array(
1589    'bf-list-submissions',
1590    'buddyforms_form',
1591    'buddyforms_list_all',
1592    'buddyforms_the_loop',
1593    'bf',
1594    'buddyforms_reset_password'
1595)  ) {
1596    // Extract from the a shortcode inside the content
1597    $form_slug = buddyforms_get_shortcode_tag( $shortcodes, array('form_slug', 'id'), $content );
1598    // Extract form the html inside the content, reading the hidden input form_slug
1599    if ( empty( $form_slug ) ) {
1600        // use regex to extract
1601        $regex = array();
1602        preg_match( '/<input type="hidden" name="form_slug" value="(.*?)" id="form_slug"/m', $content, $regex );
1603        if ( !empty( $regex ) && isset( $regex[1] ) ) {
1604            if ( is_array( $regex[1] ) ) {
1605                $form_slug = $regex[1][0];
1606            } else {
1607                $form_slug = $regex[1];
1608            }
1609        }
1610        if ( empty( $form_slug ) ) {
1611            $regex = array();
1612            preg_match( '/"bf_form_slug":"(.+?)"(?=.")/m', $content, $regex );
1613            // gutenberg block
1614            if ( !empty( $regex ) && isset( $regex[1] ) ) {
1615                $form_slug = $regex[1];
1616            }
1617        }
1618        if ( empty( $form_slug ) ) {
1619            $regex = array();
1620            preg_match( '/"bf_form_slug":"(.*)"/m', $content, $regex );
1621            // gutenberg block
1622            if ( !empty( $regex ) && isset( $regex[1] ) ) {
1623                $form_slug = $regex[1];
1624            }
1625        }
1626    }
1627    if ( is_numeric( $form_slug ) ) {
1628        $form_post = get_post( $form_slug );
1629        $form_slug = $form_post->post_name;
1630    }
1631    return $form_slug;
1632}
1633
1634/**
1635 * Detext if is gutenberg
1636 *
1637 * @return boolean
1638 */
1639function buddyforms_is_gutenberg_page() {
1640    if ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) {
1641        // The Gutenberg plugin is on.
1642        return true;
1643    }
1644    require_once ABSPATH . 'wp-admin/includes/screen.php';
1645    require_once ABSPATH . 'wp-admin/includes/admin.php';
1646    $current_screen = get_current_screen();
1647    if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
1648        // Gutenberg page on 5+.
1649        return true;
1650    }
1651    return false;
1652}
1653
1654/**
1655 * This function secure the array of options to use in the buddyformsGlobal
1656 *
1657 * @param $options
1658 * @param $form_slug
1659 * @param $bf_post_id
1660 *
1661 * @return mixed
1662 * @since 2.4.0
1663 */
1664function buddyforms_filter_frontend_js_form_options(  $options, $form_slug, $bf_post_id = 0  ) {
1665    /**
1666     * Let the user change the user granted options to use in the frontend global variable buddyformsGlobal
1667     *
1668     * @param array granted keys from the options
1669     * @param string The form slug from the global wp_query
1670     * @param number The current post id form the wp_query. This can be empty when the form is creating an entry.
1671     *
1672     * @since 2.4.0
1673     */
1674    $granted = apply_filters(
1675        'buddyforms_frontend_granted_forms_option',
1676        array(
1677            'status',
1678            'form_fields',
1679            'draft_action',
1680            'js_validation'
1681        ),
1682        $form_slug,
1683        $bf_post_id
1684    );
1685    foreach ( $granted as $item ) {
1686        if ( isset( $options[$item] ) ) {
1687            $result[$item] = $options[$item];
1688        }
1689    }
1690    // Filter the field options
1691    $remove_field_options = apply_filters(
1692        'buddyforms_remove_frontend_forms_fields_option',
1693        array('captcha_private_key'),
1694        $form_slug,
1695        $bf_post_id
1696    );
1697    if ( !empty( $result['form_fields'] ) ) {
1698        foreach ( $remove_field_options as $remove_field ) {
1699            foreach ( $result['form_fields'] as $field_id => $field ) {
1700                if ( isset( $field[$remove_field] ) ) {
1701                    unset($result['form_fields'][$field_id][$remove_field]);
1702                }
1703            }
1704        }
1705    }
1706    return $result;
1707}
1708
1709/**
1710 * Retrieve the form slug from different sources
1711 *
1712 * @return string
1713 * @since 2.4.0
1714 */
1715function buddyforms_get_form_slug() {
1716    $form_slug = '';
1717    global $wp_query, $post;
1718    if ( !empty( $wp_query->query_vars['bf_form_slug'] ) ) {
1719        $form_slug = sanitize_title( $wp_query->query_vars['bf_form_slug'] );
1720    } elseif ( !empty( $_GET['form_slug'] ) ) {
1721        $form_slug = sanitize_title( wp_unslash( $_GET['form_slug'] ) );
1722    } elseif ( !empty( $wp_query->query_vars['form_slug'] ) ) {
1723        $form_slug = sanitize_title( $wp_query->query_vars['form_slug'] );
1724    } elseif ( !empty( $post ) ) {
1725        $post_content = ( !empty( $content ) ? $content : $post->post_content );
1726        if ( !empty( $post->post_name ) && $post->post_type === 'buddyforms' ) {
1727            $form_slug = $post->post_name;
1728        } elseif ( !empty( $post_content ) ) {
1729            // Extract the shortcode inside the content
1730            $form_slug = buddyforms_get_form_slug_from_content( $post_content );
1731            if ( empty( $form_slug ) ) {
1732                $form_slug = buddyforms_get_form_slug_by_post_id( $post->ID );
1733            }
1734        }
1735    } elseif ( function_exists( 'bp_current_component' ) && function_exists( 'bp_current_action' ) && function_exists( 'buddyforms_members_get_form_by_member_type' ) ) {
1736        global $buddyforms_member_tabs;
1737        $bp_action = bp_current_action();
1738        $bp_component = bp_current_component();
1739        if ( !empty( $buddyforms_member_tabs ) && 'xprofile' !== $bp_component ) {
1740            $form_slug = ( !empty( $buddyforms_member_tabs[bp_current_component()][bp_current_action()] ) ? $buddyforms_member_tabs[bp_current_component()][bp_current_action()] : '' );
1741            if ( $form_slug . '-create' !== $bp_action && $form_slug . '-edit' !== $bp_action && $form_slug . '-revision' !== $bp_action ) {
1742                $member_type = bp_get_member_type( get_current_user_id() );
1743                $form_slug = buddyforms_members_get_form_by_member_type( $member_type );
1744                if ( !$form_slug ) {
1745                    $form_slug = buddyforms_members_get_form_by_member_type( 'none' );
1746                }
1747            }
1748        }
1749    }
1750    return $form_slug;
1751}
1752
1753/**
1754 * Check if the draft is enabled for the given form slug
1755 *
1756 * @param $form_slug
1757 * @param string    $permission
1758 *
1759 * @return bool
1760 * @since 2.5.14
1761 * @author gfirem
1762 */
1763function buddyforms_is_permission_enabled(  $form_slug, $permission = 'draft'  ) {
1764    $result = wp_cache_get( 'buddyforms_user_capability_for_' . $form_slug . '_draft', 'buddyforms' );
1765    if ( !empty( $form_slug ) && $result === false ) {
1766        /** @var WP_User $current_user */
1767        $current_user = wp_get_current_user();
1768        if ( empty( $current_user ) ) {
1769            $result = false;
1770        } else {
1771            $result = bf_user_can(
1772                $current_user->ID,
1773                'buddyforms_' . $form_slug . '_' . $permission,
1774                array(),
1775                $form_slug
1776            );
1777        }
1778        wp_cache_set( 'buddyforms_user_capability_for_' . $form_slug . '_draft', $result, 'buddyforms' );
1779    }
1780    return $result;
1781}
1782
1783/**
1784 * Get the form actions. This function is used to handle the form actions if the form have a form_action element or if not
1785 *
1786 * @param $form Form
1787 * @param $form_slug
1788 * @param $post_id
1789 * @param $field_options
1790 *
1791 * @return Form
1792 * @since 2.4.0
1793 */
1794function buddyforms_form_action_buttons(
1795    $form,
1796    $form_slug,
1797    $post_id,
1798    $field_options
1799) {
1800    global $buddyforms;
1801    $exist_field_status = buddyforms_exist_field_type_in_form( $form_slug, 'status' );
1802    $is_draft_permission_enabled = buddyforms_is_permission_enabled( $form_slug );
1803    $is_form_element_action = !empty( $field_options );
1804    if ( $is_form_element_action ) {
1805        $is_field_publish_enabled = empty( $field_options['disabled_publish'] );
1806        $is_edit_permission_enabled = buddyforms_is_permission_enabled( $form_slug, 'edit' );
1807        $is_create_permission_enabled = buddyforms_is_permission_enabled( $form_slug, 'create' );
1808        $is_draft_enabled = $is_draft_permission_enabled || $is_create_permission_enabled && !$is_edit_permission_enabled && $is_draft_permission_enabled;
1809    } else {
1810        $is_draft_enabled = $is_draft_permission_enabled;
1811        $is_field_publish_enabled = true;
1812    }
1813    $bfdesign = ( isset( $buddyforms[$form_slug]['layout'] ) ? $buddyforms[$form_slug]['layout'] : array() );
1814    $form_type = ( isset( $buddyforms[$form_slug]['form_type'] ) ? $buddyforms[$form_slug]['form_type'] : '' );
1815    $form_status = ( isset( $buddyforms[$form_slug]['status'] ) ? $buddyforms[$form_slug]['status'] : 'publish' );
1816    $button_class = ( !empty( $bfdesign['button_class'] ) ? $bfdesign['button_class'] : '' );
1817    $include_form_draft_button = apply_filters(
1818        'buddyforms_include_form_draft_button',
1819        true,
1820        $form_slug,
1821        $form,
1822        $post_id
1823    );
1824    if ( $is_draft_enabled && $include_form_draft_button ) {
1825        if ( !$exist_field_status && $form_type === 'post' && is_user_logged_in() ) {
1826            $bf_draft_button_text = ( !empty( $bfdesign['draft_text'] ) ? $bfdesign['draft_text'] : apply_filters( 'buddyforms_draft_button_text', __( 'Save as draft', 'buddyforms' ), $form_slug ) );
1827            $bf_draft_button_classes = 'bf-draft ' . $button_class;
1828            $bf_draft_button = new Element_Button($bf_draft_button_text, 'submit', array(
1829                'id'             => $form_slug . '-draft',
1830                'class'          => $bf_draft_button_classes,
1831                'name'           => 'draft',
1832                'formnovalidate' => 'formnovalidate',
1833                'data-target'    => $form_slug,
1834                'data-status'    => 'draft',
1835            ));
1836            if ( $bf_draft_button ) {
1837                $form->addElement( $bf_draft_button );
1838            }
1839        }
1840    }
1841    $include_form_submit_button = apply_filters(
1842        'buddyforms_include_form_submit_button',
1843        true,
1844        $form_slug,
1845        $form,
1846        $post_id
1847    );
1848    if ( $is_field_publish_enabled && $include_form_submit_button ) {
1849        $bf_publish_button_classes = 'bf-submit ' . $button_class;
1850        if ( !empty( $form_type ) && $form_type === 'post' && !$exist_field_status ) {
1851            $bf_button_text = ( !empty( $bfdesign['submit_text'] ) ? $bfdesign['submit_text'] : __( 'Publish', 'buddyforms' ) );
1852        } else {
1853            $bf_button_text = ( !empty( $bfdesign['submit_text'] ) ? $bfdesign['submit_text'] : __( 'Submit', 'buddyforms' ) );
1854        }
1855        $bf_submit_button = new Element_Button($bf_button_text, 'submit', array(
1856            'id'          => $form_slug,
1857            'class'       => $bf_publish_button_classes,
1858            'name'        => 'submitted',
1859            'data-target' => $form_slug,
1860            'data-status' => $form_status,
1861        ));
1862        $form->addElement( $bf_submit_button );
1863    }
1864    $form = apply_filters(
1865        'buddyforms_create_edit_form_button',
1866        $form,
1867        $form_slug,
1868        $post_id
1869    );
1870    return $form;
1871}
1872
1873if ( !function_exists( 'buddyforms_show_error_messages' ) ) {
1874    // displays error messages from form submissions
1875    function buddyforms_show_error_messages() {
1876        $global_error = ErrorHandler::get_instance();
1877        $global_bf_error = $global_error->get_global_error();
1878        if ( !empty( $global_bf_error ) ) {
1879            if ( $global_bf_error->has_errors() ) {
1880                echo '<div class="bf-alert error">';
1881                /**
1882                 * @var string|int $code
1883                 * @var  BuddyForms_Error|WP_Error $error
1884                 */
1885                foreach ( $global_error->get_global_error()->errors as $code => $error ) {
1886                    $message = $global_error->get_global_error()->get_error_message( $code );
1887                    if ( is_array( $message ) ) {
1888                        $message = $message[0];
1889                    }
1890                    echo '<span class="buddyforms_error" data-error-code="' . esc_attr( $code ) . '"><strong>' . esc_html__( 'Error', 'buddyforms' ) . '</strong>: ' . esc_html( $message ) . '</span><br/>';
1891                }
1892                echo '</div>';
1893            }
1894        }
1895    }
1896
1897}
1898if ( !function_exists( 'buddyforms_reset_password_errors' ) ) {
1899    // used for tracking error messages
1900    function buddyforms_reset_password_errors() {
1901        $global_error = ErrorHandler::get_instance();
1902        return $global_error->get_global_error();
1903    }
1904
1905}
1906/**
1907 * Check whether the specified user has a given capability on a given site.
1908 *
1909 * @param int       $user_id
1910 * @param string    $capability Capability or role name.
1911 * @param string    $form_slug
1912 * @param array|int $args {
1913 *     Array of extra arguments applicable to the capability check.
1914 *
1915 * @return bool True if the user has the cap for the given parameters.
1916 * @since 2.5.0
1917 */
1918function bf_user_can(
1919    $user_id,
1920    $capability,
1921    $args = array(),
1922    $form_slug = ''
1923) {
1924    if ( !empty( $form_slug ) ) {
1925        $switched = buddyforms_switch_to_form_blog( $form_slug );
1926    }
1927    $user = get_user_by( 'ID', $user_id );
1928    if ( !$user || !$user->exists() ) {
1929        return false;
1930    }
1931    $result = $user->has_cap( $capability );
1932    if ( !empty( $switched ) ) {
1933        restore_current_blog();
1934    }
1935    return $result;
1936}
1937
1938/**
1939 * Array of fields slug to exclude from the submission columns and email table
1940 *
1941 * since 2.5.0
1942 *
1943 * @return mixed|void
1944 */
1945function buddyforms_get_exclude_field_slugs() {
1946    return apply_filters( 'buddyforms_submission_exclude_columns', array('user_pass', 'captcha', 'html') );
1947}
1948
1949/**
1950 * Sanitizes a slug for an element, replacing whitespace and a few other characters with dashes.
1951 *
1952 * @param string $slug The title to be sanitized.
1953 * @param string $context Optional. The operation for which the string is sanitized.
1954 *
1955 * @return string The sanitized title.
1956 * @since 2.5.6
1957 *
1958 * @see sanitize_title_with_dashes
1959 */
1960function buddyforms_sanitize_slug(  $slug, $context = 'save'  ) {
1961    $slug = strip_tags( $slug );
1962    // Preserve escaped octets.
1963    $slug = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $slug );
1964    // Remove percent signs that are not part of an octet.
1965    $slug = str_replace( '%', '', $slug );
1966    // Restore octets.
1967    $slug = preg_replace( '|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $slug );
1968    if ( seems_utf8( $slug ) ) {
1969        $slug = utf8_uri_encode( $slug, 200 );
1970    }
1971    if ( 'save' == $context ) {
1972        // Convert nbsp, ndash and mdash to hyphens
1973        $slug = str_replace( array('%c2%a0', '%e2%80%93', '%e2%80%94'), '-', $slug );
1974        // Convert nbsp, ndash and mdash HTML entities to hyphens
1975        $slug = str_replace( array(
1976            '&nbsp;',
1977            '&#160;',
1978            '&ndash;',
1979            '&#8211;',
1980            '&mdash;',
1981            '&#8212;'
1982        ), '-', $slug );
1983        // Convert forward slash to hyphen
1984        $slug = str_replace( '/', '-', $slug );
1985        // Strip these characters entirely
1986        $slug = str_replace( array(
1987            // soft hyphens
1988            '%c2%ad',
1989            // iexcl and iquest
1990            '%c2%a1',
1991            '%c2%bf',
1992            // angle quotes
1993            '%c2%ab',
1994            '%c2%bb',
1995            '%e2%80%b9',
1996            '%e2%80%ba',
1997            // curly quotes
1998            '%e2%80%98',
1999            '%e2%80%99',
2000            '%e2%80%9c',
2001            '%e2%80%9d',
2002            '%e2%80%9a',
2003            '%e2%80%9b',
2004            '%e2%80%9e',
2005            '%e2%80%9f',
2006            // copy, reg, deg, hellip and trade
2007            '%c2%a9',
2008            '%c2%ae',
2009            '%c2%b0',
2010            '%e2%80%a6',
2011            '%e2%84%a2',
2012            // acute accents
2013            '%c2%b4',
2014            '%cb%8a',
2015            '%cc%81',
2016            '%cd%81',
2017            // grave accent, macron, caron
2018            '%cc%80',
2019            '%cc%84',
2020            '%cc%8c',
2021        ), '', $slug );
2022        // Convert times to x
2023        $slug = str_replace( '%c3%97', 'x', $slug );
2024    }
2025    $slug = preg_replace( '/&.+?;/', '', $slug );
2026    // kill entities
2027    $slug = str_replace( '.', '-', $slug );
2028    $slug = preg_replace( '/[^%a-zA-Z0-9 _-]/', '', $slug );
2029    $slug = preg_replace( '/\\s+/', '-', $slug );
2030    $slug = preg_replace( '|-+|', '-', $slug );
2031    $slug = trim( $slug, '-' );
2032    return $slug;
2033}
2034
2035/**
2036 * Override the form_slug from the loop to get the correct base on the current post
2037 *
2038 * @param $form_slug
2039 * @param $post_id
2040 *
2041 * @return string
2042 *
2043 * @since 2.5.17
2044 */
2045function buddyforms_contact_author_loop_form_slug(  $form_slug, $post_id  ) {
2046    if ( !empty( $post_id ) && function_exists( 'buddyforms_get_form_slug_by_post_id' ) ) {
2047        $form_slug = buddyforms_get_form_slug_by_post_id( $post_id );
2048    }
2049    return $form_slug;
2050}
2051
2052add_filter(
2053    'buddyforms_loop_form_slug',
2054    'buddyforms_contact_author_loop_form_slug',
2055    10,
2056    2
2057);
2058/**
2059 * Enqueue buddyforms thickbox wrapper
2060 *
2061 * @since 2.5.19
2062 */
2063function buddyforms_add_bf_thickbox() {
2064    wp_enqueue_script( 'buddyforms-thickbox' );
2065    wp_enqueue_style( 'buddyforms-thickbox' );
2066}
2067
2068add_filter(
2069    'buddyforms_mail_to_before_send_notification',
2070    'buddyforms_process_shortcode_notificate_to_attr',
2071    10,
2072    2
2073);
2074function buddyforms_process_shortcode_notificate_to_attr(  $mail_to, $notification  ) {
2075    if ( isset( $_POST['notificate_to'] ) && !empty( $_POST['notificate_to'] ) ) {
2076        $notificate_to = sanitize_text_field( wp_unslash( $_POST['notificate_to'] ) );
2077        $notificate_to = trim( preg_replace( '/\\s+/', '', $notificate_to ) );
2078        $notificate_to = explode( ',', $notificate_to );
2079        foreach ( $notificate_to as $value ) {
2080            $_notificate_to = explode( '-', $value );
2081            $mail_trigger_id = $_notificate_to[0];
2082            $user_email = sanitize_email( $_notificate_to[1] );
2083            // Check if mail_trigger_id match with current notification.
2084            if ( $notification['mail_trigger_id'] === $mail_trigger_id && is_email( $user_email ) ) {
2085                array_push( $mail_to, $user_email );
2086            }
2087        }
2088    }
2089    return $mail_to;
2090}
2091
2092function buddyforms_add_safe_css_attributes(  $atts  ) {
2093    $atts[] = 'display';
2094    $atts[] = 'visibility';
2095    return $atts;
2096}
2097
2098add_filter( 'safe_style_css', 'buddyforms_add_safe_css_attributes' );
2099function buddyforms_wp_kses_allowed_atts() {
2100    $allowed_tags = array(
2101        'div'      => array(
2102            'class'             => array(),
2103            'id'                => array(),
2104            'hidefocus'         => array(),
2105            'tabindex'          => array(),
2106            'role'              => array(),
2107            'style'             => array(),
2108            'action'            => array(),
2109            'page'              => array(),
2110            'aria-labelledby'   => array(),
2111            'aria-haspopup'     => array(),
2112            'aria-pressed'      => array(),
2113            'aria-label'        => array(),
2114            'aria-level'        => array(),
2115            'aria-controls'     => array(),
2116            'aria-selected'     => array(),
2117            'aria-expanded'     => array(),
2118            'data-index'        => array(),
2119            'data-entry'        => array(),
2120            'data-dz-message'   => array(),
2121            'data-type'         => array(),
2122            'data-size'         => array(),
2123            'data-theme'        => array(),
2124            'data-element-slug' => array(),
2125            'data-sitekey'      => array(),
2126        ),
2127        'span'     => array(
2128            'class'                  => array(),
2129            'id'                     => array(),
2130            'dir'                    => array(),
2131            'style'                  => array(),
2132            'role'                   => array(),
2133            'aria-haspopup'          => array(),
2134            'aria-hidden'            => array(),
2135            'aria-expanded'          => array(),
2136            'aria-label'             => array(),
2137            'aria-disable'           => array(),
2138            'aria-labelledby'        => array(),
2139            'tabindex'               => array(),
2140            'data'                   => array(),
2141            'data-tip'               => array(),
2142            'data-dz-size'           => array(),
2143            'data-dz-name'           => array(),
2144            'data-dz-uploadprogress' => array(),
2145            'data-dz-errormessage'   => array(),
2146            'data-select2-id'        => array(),
2147        ),
2148        'strong'   => array(),
2149        'p'        => array(
2150            'class' => array(),
2151            'id'    => array(),
2152            'name'  => array(),
2153            'title' => array(),
2154        ),
2155        'style'    => array(),
2156        'form'     => array(
2157            'class'      => array(),
2158            'name'       => array(),
2159            'id'         => array(),
2160            'action'     => array(),
2161            'method'     => array(),
2162            'novalidate' => array(),
2163            'target'     => array(),
2164        ),
2165        'fieldset' => array(),
2166        'input'    => array(
2167            'class'                           => array(),
2168            'id'                              => array(),
2169            'name'                            => array(),
2170            'type'                            => array(),
2171            'role'                            => array(),
2172            'style'                           => array(),
2173            'step'                            => array(),
2174            'value'                           => array(),
2175            'field-id'                        => array(),
2176            'field_id'                        => array(),
2177            'required'                        => array(),
2178            'checked'                         => array(),
2179            'tabindex'                        => array(),
2180            'pattern'                         => array(),
2181            'placeholder'                     => array(),
2182            'autocomplete'                    => array(),
2183            'autocorrect'                     => array(),
2184            'autocapitalize'                  => array(),
2185            'spellcheck'                      => array(),
2186            'frontend_reset'                  => array(),
2187            'size'                            => array(),
2188            'aria'                            => array(),
2189            'aria-invalid'                    => array(),
2190            'data'                            => array(),
2191            'data-form'                       => array(),
2192            'data-rule-minlength'             => array(),
2193            'data-rule-maxlength'             => array(),
2194            'placeholder'                     => array(),
2195            'aria-autocomplete'               => array(),
2196            'data-rule-upload-required'       => array(),
2197            'data-msg-upload-required'        => array(),
2198            'data-rule-featured-image-error'  => array(),
2199            'upload_error_validation_message' => array(),
2200            'data-type'                       => array(),
2201            'data-size'                       => array(),
2202            'data-theme'                      => array(),
2203            'data-element-slug'               => array(),
2204            'data-sitekey'                    => array(),
2205        ),
2206        'select'   => array(
2207            'class'               => array(),
2208            'id'                  => array(),
2209            'name'                => array(),
2210            'type'                => array(),
2211            'hidden'              => array(),
2212            'multiple'            => array(),
2213            'style'               => array(),
2214            'value'               => array(),
2215            'tabindex'            => array(),
2216            'field-id'            => array(),
2217            'field_id'            => array(),
2218            'required'            => array(),
2219            'aria-hidden'         => array(),
2220            'data-form'           => array(),
2221            'data-action'         => array(),
2222            'data-exclude'        => array(),
2223            'data-sortable'       => array(),
2224            'data-rule-minlength' => array(),
2225            'data-rule-maxlength' => array(),
2226            'data-placeholder'    => array(),
2227            'placeholder'         => array(),
2228            'data-select2-id'     => array(),
2229        ),
2230        'option'   => array(
2231            'class'               => array(),
2232            'id'                  => array(),
2233            'name'                => array(),
2234            'type'                => array(),
2235            'style'               => array(),
2236            'value'               => array(),
2237            'field-id'            => array(),
2238            'field_id'            => array(),
2239            'data-form'           => array(),
2240            'data-country'        => array(),
2241            'data-rule-minlength' => array(),
2242            'data-rule-maxlength' => array(),
2243            'data-unique'         => array(),
2244            'placeholder'         => array(),
2245            'selected'            => array(),
2246        ),
2247        'optgroup' => array(
2248            'class'    => array(),
2249            'id'       => array(),
2250            'name'     => array(),
2251            'label'    => array(),
2252            'style'    => array(),
2253            'value'    => array(),
2254            'field-id' => array(),
2255            'field_id' => array(),
2256            'selected' => array(),
2257        ),
2258        'label'    => array(
2259            'for'      => array(),
2260            'id'       => array(),
2261            'class'    => array(),
2262            'style'    => array(),
2263            'data'     => array(),
2264            'data-tip' => array(),
2265        ),
2266        'link'     => array(
2267            'class' => array(),
2268            'id'    => array(),
2269            'rel'   => array(),
2270            'href'  => array(),
2271            'media' => array(),
2272        ),
2273        'a'        => array(
2274            'class'          => array(),
2275            'id'             => array(),
2276            'style'          => array(),
2277            'target'         => array(),
2278            'href'           => array(),
2279            'name'           => array(),
2280            'title'          => array(),
2281            'type'           => array(),
2282            'tabindex'       => array(),
2283            'data'           => array(),
2284            'data-toggle'    => array(),
2285            'data-type'      => array(),
2286            'data-row'       => array(),
2287            'data-gdpr-type' => array(),
2288            'data-form-slug' => array(),
2289            'onclick'        => array(),
2290        ),
2291        'button'   => array(
2292            'class'             => array(),
2293            'id'                => array(),
2294            'type'              => array(),
2295            'name'              => array(),
2296            'disabled'          => array(),
2297            'data-editor'       => array(),
2298            'data-wp-editor-id' => array(),
2299            'data-target'       => array(),
2300            'data-status'       => array(),
2301            'data-template'     => array(),
2302            'data-type'         => array(),
2303            'onclick'           => array(),
2304            'tabindex'          => array(),
2305            'role'              => array(),
2306            'field-id'          => array(),
2307            'field_id'          => array(),
2308            'accepted_files'    => array(),
2309            'onclick'           => array(),
2310        ),
2311        'i'        => array(
2312            'class' => array(),
2313            'id'    => array(),
2314            'name'  => array(),
2315        ),
2316        'abbr'     => array(
2317            'class' => array(),
2318            'id'    => array(),
2319            'name'  => array(),
2320            'title' => array(),
2321        ),
2322        'iframe'   => array(
2323            'id'                => array(),
2324            'allowtransparency' => array(),
2325            'title'             => array(),
2326            'style'             => array(),
2327            'frameborder'       => array(),
2328        ),
2329        'textarea' => array(
2330            'id'                  => array(),
2331            'class'               => array(),
2332            'name'                => array(),
2333            'style'               => array(),
2334            'placeholder'         => array(),
2335            'data-form'           => array(),
2336            'data-rule-minlength' => array(),
2337            'data-rule-maxlength' => array(),
2338            'rows'                => array(),
2339            'required'            => array(),
2340            'autocomplete'        => array(),
2341            'cols'                => array(),
2342            'aria-hidden'         => array(),
2343        ),
2344        'img'      => array(
2345            'id'                => array(),
2346            'src'               => array(),
2347            'alt'               => array(),
2348            'data-dz-thumbnail' => array(),
2349        ),
2350        'svg'      => array(
2351            'width'   => array(),
2352            'height'  => array(),
2353            'alt'     => array(),
2354            'viewBox' => array(),
2355        ),
2356        'small'    => array(
2357            'class' => array(),
2358            'id'    => array(),
2359            'style' => array(),
2360            'name'  => array(),
2361        ),
2362        'ul'       => array(
2363            'class'           => array(),
2364            'id'              => array(),
2365            'style'           => array(),
2366            'name'            => array(),
2367            'role'            => array(),
2368            'tabindex'        => array(),
2369            'aria-controls'   => array(),
2370            'aria-labelledby' => array(),
2371            'aria-selected'   => array(),
2372            'aria-expanded'   => array(),
2373        ),
2374        'li'       => array(
2375            'class'           => array(),
2376            'id'              => array(),
2377            'style'           => array(),
2378            'name'            => array(),
2379            'role'            => array(),
2380            'tabindex'        => array(),
2381            'aria-controls'   => array(),
2382            'aria-labelledby' => array(),
2383            'aria-selected'   => array(),
2384            'aria-expanded'   => array(),
2385            'data-field-id'   => array(),
2386        ),
2387        'table'    => array(
2388            'class' => array(),
2389            'id'    => array(),
2390            'style' => array(),
2391            'name'  => array(),
2392        ),
2393        'tbody'    => array(
2394            'class' => array(),
2395            'id'    => array(),
2396            'style' => array(),
2397            'name'  => array(),
2398        ),
2399        'tr'       => array(
2400            'class' => array(),
2401            'id'    => array(),
2402            'style' => array(),
2403            'name'  => array(),
2404        ),
2405        'td'       => array(
2406            'class'    => array(),
2407            'id'       => array(),
2408            'style'    => array(),
2409            'name'     => array(),
2410            'role'     => array(),
2411            'tabindex' => array(),
2412            'colspan'  => array(),
2413        ),
2414        'thead'    => array(
2415            'class'    => array(),
2416            'id'       => array(),
2417            'style'    => array(),
2418            'name'     => array(),
2419            'role'     => array(),
2420            'tabindex' => array(),
2421        ),
2422        'th'       => array(
2423            'class'    => array(),
2424            'id'       => array(),
2425            'style'    => array(),
2426            'name'     => array(),
2427            'role'     => array(),
2428            'tabindex' => array(),
2429            'colspan'  => array(),
2430        ),
2431        'tfoot'    => array(
2432            'class'    => array(),
2433            'id'       => array(),
2434            'style'    => array(),
2435            'name'     => array(),
2436            'role'     => array(),
2437            'tabindex' => array(),
2438            'colspan'  => array(),
2439        ),
2440        'style'    => array(
2441            'type' => array(),
2442        ),
2443        'h1'       => array(
2444            'class' => array(),
2445            'id'    => array(),
2446            'style' => array(),
2447            'name'  => array(),
2448        ),
2449        'h2'       => array(
2450            'class' => array(),
2451            'id'    => array(),
2452            'style' => array(),
2453            'name'  => array(),
2454        ),
2455        'h3'       => array(
2456            'class' => array(),
2457            'id'    => array(),
2458            'style' => array(),
2459            'name'  => array(),
2460        ),
2461        'h4'       => array(
2462            'class' => array(),
2463            'id'    => array(),
2464            'style' => array(),
2465            'name'  => array(),
2466        ),
2467        'h5'       => array(
2468            'class' => array(),
2469            'id'    => array(),
2470            'style' => array(),
2471            'name'  => array(),
2472        ),
2473        'strong'   => array(),
2474        'br'       => array(),
2475        'b'        => array(
2476            'class' => array(),
2477            'id'    => array(),
2478            'style' => array(),
2479            'name'  => array(),
2480        ),
2481        'script'   => array(
2482            'src' => array(),
2483        ),
2484    );
2485    return $allowed_tags;
2486}
Note: See TracBrowser for help on using the repository browser.