Form.php
11 months ago
Init.php
5 years ago
TabbedWidget.php
4 years ago
TabbedWidgetDependency.php
1 year ago
UserPanel.php
2 years ago
index.php
5 years ago
Form.php
156 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Widgets; |
| 4 | |
| 5 | use ProfilePress\Core\Classes\FormRepository as FR; |
| 6 | use WP_Widget; |
| 7 | |
| 8 | class Form extends WP_Widget |
| 9 | { |
| 10 | public function __construct() |
| 11 | { |
| 12 | parent::__construct( |
| 13 | 'pp_form', |
| 14 | esc_html__('ProfilePress Form', 'wp-user-avatar'), |
| 15 | array('description' => esc_html__('Easily add your ProfilePress forms to widget areas.', 'wp-user-avatar')) |
| 16 | ); |
| 17 | } |
| 18 | |
| 19 | public function widget($args, $instance) |
| 20 | { |
| 21 | $chosen_form = sanitize_text_field($instance['chosen_form'] ?? ''); |
| 22 | |
| 23 | $hide_widget = $instance['hide'] ?? ''; |
| 24 | |
| 25 | if ('yes' == $hide_widget && is_user_logged_in()) return; |
| 26 | |
| 27 | echo $args['before_widget']; |
| 28 | |
| 29 | if ( ! empty($instance['title'])) { |
| 30 | echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title']; |
| 31 | } |
| 32 | |
| 33 | echo do_shortcode($chosen_form); |
| 34 | |
| 35 | echo $args['after_widget']; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Back-end widget form. |
| 40 | * |
| 41 | * @param array $instance |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | public function form($instance) |
| 46 | { |
| 47 | $title = ! empty($instance['title']) ? $instance['title'] : ''; |
| 48 | $hide = ! empty($instance['hide']) ? $instance['hide'] : ''; |
| 49 | $chosen_form = ! empty($instance['chosen_form']) ? $instance['chosen_form'] : ''; |
| 50 | |
| 51 | $login_form_ids = FR::get_form_ids(FR::LOGIN_TYPE); |
| 52 | $registration_form_ids = FR::get_form_ids(FR::REGISTRATION_TYPE); |
| 53 | $password_reset_ids = FR::get_form_ids(FR::PASSWORD_RESET_TYPE); |
| 54 | $edit_profile_form_ids = FR::get_form_ids(FR::EDIT_PROFILE_TYPE); |
| 55 | $melange_ids = FR::get_form_ids(FR::MELANGE_TYPE); |
| 56 | |
| 57 | if (empty($login_form_ids) || empty($registration_form_ids) || empty($password_reset_ids) || empty($edit_profile_form_ids) || empty($melange_ids)) { |
| 58 | echo '<p>' . __(apply_filters('ppress_no_form_widget', 'No ProfilePress form is available.'), 'wp-user-avatar') . '</p>'; |
| 59 | |
| 60 | return; |
| 61 | } |
| 62 | ?> |
| 63 | |
| 64 | <p> |
| 65 | <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'wp-user-avatar'); ?></label> |
| 66 | <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>"> |
| 67 | </p> |
| 68 | |
| 69 | <p> |
| 70 | <label for="<?php echo $this->get_field_id('chosen_form'); ?>"><?php _e('Select Form', 'wp-user-avatar'); ?></label><br/> |
| 71 | <select class="widefat" id="<?php echo $this->get_field_id('chosen_form'); ?>" name="<?php echo $this->get_field_name('chosen_form'); ?>"> |
| 72 | <?php |
| 73 | if ( ! empty($login_form_ids)) { |
| 74 | printf('<optgroup label="%s">', esc_html__('Login Form', 'wp-user-avatar')); |
| 75 | foreach ($login_form_ids as $login_form_id) { |
| 76 | $key = sprintf("[profilepress-login id=%s]", $login_form_id); |
| 77 | printf('<option value="%s" %s>%s</option>', |
| 78 | $key, |
| 79 | selected($key, $chosen_form, false), |
| 80 | FR::get_name($login_form_id, FR::LOGIN_TYPE) |
| 81 | ); |
| 82 | } |
| 83 | echo '</optgroup>'; |
| 84 | } |
| 85 | |
| 86 | if ( ! empty($registration_form_ids)) { |
| 87 | printf('<optgroup label="%s">', esc_html__('Registration Form', 'wp-user-avatar')); |
| 88 | foreach ($registration_form_ids as $registration_form_id) { |
| 89 | $key = sprintf("[profilepress-registration id=%s]", $registration_form_id); |
| 90 | printf('<option value="%s" %s>%s</option>', |
| 91 | $key, |
| 92 | selected($key, $chosen_form, false), |
| 93 | FR::get_name($registration_form_id, FR::REGISTRATION_TYPE) |
| 94 | ); |
| 95 | } |
| 96 | echo '</optgroup>'; |
| 97 | } |
| 98 | |
| 99 | if ( ! empty($password_reset_ids)) { |
| 100 | printf('<optgroup label="%s">', esc_html__('Password Reset Form', 'wp-user-avatar')); |
| 101 | foreach ($password_reset_ids as $password_reset_id) { |
| 102 | $key = sprintf("[profilepress-password-reset id=%s]", $password_reset_id); |
| 103 | printf('<option value="%s" %s>%s</option>', |
| 104 | $key, |
| 105 | selected($key, $chosen_form, false), |
| 106 | FR::get_name($password_reset_id, FR::PASSWORD_RESET_TYPE) |
| 107 | ); |
| 108 | } |
| 109 | echo '</optgroup>'; |
| 110 | } |
| 111 | |
| 112 | if ( ! empty($edit_profile_form_ids)) { |
| 113 | printf('<optgroup label="%s">', esc_html__('Edit Profile Form', 'wp-user-avatar')); |
| 114 | foreach ($edit_profile_form_ids as $edit_profile_form_id) { |
| 115 | $key = sprintf("[profilepress-edit-profile id=%s]", $edit_profile_form_id); |
| 116 | printf('<option value="%s" %s>%s</option>', |
| 117 | $key, |
| 118 | selected($key, $chosen_form, false), |
| 119 | FR::get_name($edit_profile_form_id, FR::EDIT_PROFILE_TYPE) |
| 120 | ); |
| 121 | } |
| 122 | echo '</optgroup>'; |
| 123 | } |
| 124 | |
| 125 | if ( ! empty($melange_ids)) { |
| 126 | printf('<optgroup label="%s">', esc_html__('Melange Form', 'wp-user-avatar')); |
| 127 | foreach ($melange_ids as $melange_id) { |
| 128 | $key = sprintf("[profilepress-melange id=%s]", $melange_id); |
| 129 | printf('<option value="%s" %s>%s</option>', |
| 130 | $key, |
| 131 | selected($key, $chosen_form, false), |
| 132 | FR::get_name($melange_id, FR::MELANGE_TYPE) |
| 133 | ); |
| 134 | } |
| 135 | echo '</optgroup>'; |
| 136 | } |
| 137 | ?> |
| 138 | </select> |
| 139 | </p> |
| 140 | <p> |
| 141 | <label for="<?php echo $this->get_field_id('hide'); ?>"><?php _e('Hide when a user is logged in:', 'wp-user-avatar'); ?></label> |
| 142 | <input class="widefat" id="<?php echo $this->get_field_id('hide'); ?>" name="<?php echo $this->get_field_name('hide'); ?>" type="checkbox" value="yes" <?php checked($hide, 'yes'); ?>> |
| 143 | </p> |
| 144 | <?php |
| 145 | } |
| 146 | |
| 147 | public function update($new_instance, $old_instance) |
| 148 | { |
| 149 | $instance = array(); |
| 150 | $instance['title'] = ( ! empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : ''; |
| 151 | $instance['hide'] = ( ! empty($new_instance['hide'])) ? sanitize_text_field($new_instance['hide']) : ''; |
| 152 | $instance['chosen_form'] = ( ! empty($new_instance['chosen_form'])) ? sanitize_text_field($new_instance['chosen_form']) : ''; |
| 153 | |
| 154 | return $instance; |
| 155 | } |
| 156 | } |