DragDropBuilder
1 month ago
EmailSettings
1 year ago
Membership
1 month ago
AbstractSettingsPage.php
2 years ago
AddNewForm.php
1 year ago
AdminFooter.php
4 years ago
ExtensionsSettingsPage.php
1 year ago
FormList.php
4 months ago
Forms.php
1 year ago
FuseWP.php
3 years ago
GeneralSettings.php
9 months ago
IDUserColumn.php
5 years ago
LicenseUpgrader.php
3 years ago
MailOptin.php
3 years ago
MemberDirectories.php
1 year ago
MembersDirectoryList.php
4 years ago
ToolsSettingsPage.php
4 years ago
index.php
3 years ago
Forms.php
348 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages; |
| 4 | |
| 5 | use ProfilePress\Core\Admin\SettingsPages\DragDropBuilder\DragDropBuilder; |
| 6 | use ProfilePress\Core\Classes\FormRepository as FR; |
| 7 | use ProfilePress\Custom_Settings_Page_Api; |
| 8 | |
| 9 | // Exit if accessed directly |
| 10 | if ( ! defined('ABSPATH')) { |
| 11 | exit; |
| 12 | } |
| 13 | |
| 14 | class Forms extends AbstractSettingsPage |
| 15 | { |
| 16 | /** |
| 17 | * @var FormList |
| 18 | */ |
| 19 | protected $forms_instance; |
| 20 | |
| 21 | protected $DragDropClassInstance; |
| 22 | |
| 23 | public function __construct() |
| 24 | { |
| 25 | add_action('ppress_register_menu_page', [$this, 'register_menu_page']); |
| 26 | |
| 27 | add_action('ppress_admin_settings_page_forms', [$this, 'settings_admin_page_callback']); |
| 28 | add_action('ppress_admin_settings_page_add-new-form', [$this, 'settings_admin_page_callback']); |
| 29 | add_action('ppress_admin_settings_page_edit-shortcode-login', [$this, 'settings_admin_page_callback']); |
| 30 | add_action('ppress_admin_settings_page_edit-shortcode-registration', [$this, 'settings_admin_page_callback']); |
| 31 | add_action('ppress_admin_settings_page_edit-shortcode-password-reset', [$this, 'settings_admin_page_callback']); |
| 32 | add_action('ppress_admin_settings_page_edit-shortcode-edit-profile', [$this, 'settings_admin_page_callback']); |
| 33 | add_action('ppress_admin_settings_page_edit-shortcode-melange', [$this, 'settings_admin_page_callback']); |
| 34 | add_action('ppress_admin_settings_page_edit-shortcode-user-profile', [$this, 'settings_admin_page_callback']); |
| 35 | add_action('ppress_admin_settings_page_drag-drop-builder', [$this, 'settings_admin_page_callback']); |
| 36 | |
| 37 | add_filter('set-screen-option', array($this, 'set_screen'), 10, 3); |
| 38 | add_filter('set_screen_option_forms_per_page', array($this, 'set_screen'), 10, 3); |
| 39 | |
| 40 | $this->DragDropClassInstance = DragDropBuilder::get_instance(); |
| 41 | |
| 42 | do_action('ppress_admin_forms_class_constructor'); |
| 43 | } |
| 44 | |
| 45 | public function admin_page_title() |
| 46 | { |
| 47 | $page_title = esc_html__('Forms & Profiles', 'wp-user-avatar'); |
| 48 | |
| 49 | if (isset($_GET['view'])) { |
| 50 | $page_title = esc_html__('Edit Form', 'wp-user-avatar'); |
| 51 | } |
| 52 | |
| 53 | if (isset($_GET['view']) && $_GET['view'] == 'add-new-form') { |
| 54 | $page_title = esc_html__('Add Form', 'wp-user-avatar'); |
| 55 | } |
| 56 | |
| 57 | if (isset($_GET['view']) && $_GET['view'] == 'edit-shortcode-user-profile') { |
| 58 | $page_title = esc_html__('Edit Frontend Profile', 'wp-user-avatar'); |
| 59 | } |
| 60 | |
| 61 | if (isset($_GET['view'], $_GET['form-type']) && $_GET['form-type'] == FR::USER_PROFILE_TYPE) { |
| 62 | $page_title = esc_html__('Edit Frontend Profile', 'wp-user-avatar'); |
| 63 | } |
| 64 | |
| 65 | return $page_title; |
| 66 | } |
| 67 | |
| 68 | public function register_menu_page() |
| 69 | { |
| 70 | $hook = add_submenu_page( |
| 71 | PPRESS_DASHBOARD_SETTINGS_SLUG, |
| 72 | 'ProfilePress ' . $this->admin_page_title(), |
| 73 | esc_html__('Forms & Profiles', 'wp-user-avatar'), |
| 74 | 'manage_options', |
| 75 | PPRESS_FORMS_SETTINGS_SLUG, |
| 76 | array($this, 'admin_page_callback') |
| 77 | ); |
| 78 | |
| 79 | add_action("load-$hook", array($this, 'screen_option')); |
| 80 | } |
| 81 | |
| 82 | public function default_header_menu() |
| 83 | { |
| 84 | return 'forms'; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sub-menu header for form types. |
| 89 | */ |
| 90 | public function form_sub_header() |
| 91 | { |
| 92 | if ( ! empty($_GET['page']) && $_GET['page'] == PPRESS_FORMS_SETTINGS_SLUG) { |
| 93 | $melange_jbox = esc_html__('Melange combines login, registration & password reset forms in a single form.', 'wp-user-avatar'); |
| 94 | $login_url = add_query_arg('form-type', FR::LOGIN_TYPE, PPRESS_FORMS_SETTINGS_PAGE); |
| 95 | $registration_url = add_query_arg('form-type', FR::REGISTRATION_TYPE, PPRESS_FORMS_SETTINGS_PAGE); |
| 96 | $password_reset_url = add_query_arg('form-type', FR::PASSWORD_RESET_TYPE, PPRESS_FORMS_SETTINGS_PAGE); |
| 97 | $edit_profile_url = add_query_arg('form-type', FR::EDIT_PROFILE_TYPE, PPRESS_FORMS_SETTINGS_PAGE); |
| 98 | $melange_url = add_query_arg('form-type', FR::MELANGE_TYPE, PPRESS_FORMS_SETTINGS_PAGE); |
| 99 | $user_profile_url = add_query_arg('form-type', FR::USER_PROFILE_TYPE, PPRESS_FORMS_SETTINGS_PAGE); |
| 100 | |
| 101 | $login_menu_active = (isset($_GET['page']) && ! isset($_GET['form-type'])) || isset($_GET['form-type']) && $_GET['page'] == PPRESS_FORMS_SETTINGS_SLUG && $_GET['form-type'] == FR::LOGIN_TYPE ? 'pp-type-active' : null; |
| 102 | $registration_menu_active = isset($_GET['form-type']) && $_GET['page'] == PPRESS_FORMS_SETTINGS_SLUG && $_GET['form-type'] == FR::REGISTRATION_TYPE ? 'pp-type-active' : null; |
| 103 | $password_reset_menu_active = isset($_GET['form-type']) && $_GET['page'] == PPRESS_FORMS_SETTINGS_SLUG && $_GET['form-type'] == FR::PASSWORD_RESET_TYPE ? 'pp-type-active' : null; |
| 104 | $edit_profile_menu_active = isset($_GET['form-type']) && $_GET['page'] == PPRESS_FORMS_SETTINGS_SLUG && $_GET['form-type'] == FR::EDIT_PROFILE_TYPE ? 'pp-type-active' : null; |
| 105 | $melange_menu_active = isset($_GET['form-type']) && $_GET['page'] == PPRESS_FORMS_SETTINGS_SLUG && $_GET['form-type'] == FR::MELANGE_TYPE ? 'pp-type-active' : null; |
| 106 | $user_profile_menu_active = isset($_GET['form-type']) && $_GET['page'] == PPRESS_FORMS_SETTINGS_SLUG && $_GET['form-type'] == FR::USER_PROFILE_TYPE ? 'pp-type-active' : null; |
| 107 | ?> |
| 108 | <div id="pp-sub-bar"> |
| 109 | <div class="pp-new-toolbar pp-clear"> |
| 110 | <h4><?php _e('Filter by Type', 'wp-user-avatar'); ?></h4> |
| 111 | <ul class="pp-design-options"> |
| 112 | <li> |
| 113 | <a href="<?php echo $login_url; ?>" class="<?php echo $login_menu_active; ?>"> |
| 114 | <?php _e('Login', 'wp-user-avatar'); ?> |
| 115 | </a> |
| 116 | </li> |
| 117 | <li> |
| 118 | <a href="<?php echo $registration_url; ?>" class="<?php echo $registration_menu_active; ?>"> |
| 119 | <?php _e('Registration', 'wp-user-avatar'); ?> |
| 120 | </a> |
| 121 | </li> |
| 122 | <li> |
| 123 | <li> |
| 124 | <a href="<?php echo $password_reset_url; ?>" class="<?php echo $password_reset_menu_active; ?>"> |
| 125 | <?php _e('Password Reset', 'wp-user-avatar'); ?> |
| 126 | </a> |
| 127 | </li> |
| 128 | <li> |
| 129 | <a href="<?php echo $edit_profile_url; ?>" class="<?php echo $edit_profile_menu_active; ?>"> |
| 130 | <?php _e('Edit Profile', 'wp-user-avatar'); ?> |
| 131 | </a> |
| 132 | </li> |
| 133 | |
| 134 | <?php if (class_exists('ProfilePress\Libsodium\Libsodium')) : ?> |
| 135 | <li> |
| 136 | <a href="<?php echo $melange_url; ?>" class="<?php echo $melange_menu_active; ?>"> |
| 137 | <?php _e('Melange', 'wp-user-avatar'); ?> |
| 138 | </a> |
| 139 | <span class="pp-melange-jbox dashicons dashicons-editor-help" title="<?php echo $melange_jbox; ?>"></span> |
| 140 | </li> |
| 141 | <?php endif; ?> |
| 142 | |
| 143 | <li> |
| 144 | <a href="<?php echo $user_profile_url; ?>" class="<?php echo $user_profile_menu_active; ?>"> |
| 145 | <?php _e('User Profile', 'wp-user-avatar'); ?> |
| 146 | </a> |
| 147 | </li> |
| 148 | </ul> |
| 149 | </div> |
| 150 | </div> |
| 151 | <?php } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | * Save screen option. |
| 157 | * |
| 158 | * @param string $status |
| 159 | * @param string $option |
| 160 | * @param string $value |
| 161 | * |
| 162 | * @return mixed |
| 163 | */ |
| 164 | public function set_screen($status, $option, $value) |
| 165 | { |
| 166 | return $value; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Screen options |
| 171 | */ |
| 172 | public function screen_option() |
| 173 | { |
| 174 | if (isset($_GET['page'], $_GET['view']) && (strpos($_GET['view'], 'edit-shortcode') !== false || strpos($_GET['view'], 'drag-drop') !== false)) return; |
| 175 | |
| 176 | $args = [ |
| 177 | 'label' => esc_html__('Forms', 'wp-user-avatar'), |
| 178 | 'default' => 10, |
| 179 | 'option' => 'forms_per_page', |
| 180 | ]; |
| 181 | |
| 182 | add_screen_option('per_page', $args); |
| 183 | |
| 184 | $this->forms_instance = FormList::get_instance(); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @param $echo |
| 189 | * |
| 190 | * @return string|void |
| 191 | */ |
| 192 | public function live_form_preview_btn($echo = true) |
| 193 | { |
| 194 | if ( ! isset($_GET['view'])) return; |
| 195 | |
| 196 | $form_type = isset($_GET['form-type']) ? sanitize_text_field($_GET['form-type']) : ''; |
| 197 | |
| 198 | switch ($_GET['view']) { |
| 199 | case 'edit-shortcode-login': |
| 200 | $form_type = FR::LOGIN_TYPE; |
| 201 | break; |
| 202 | case 'edit-shortcode-registration': |
| 203 | $form_type = FR::REGISTRATION_TYPE; |
| 204 | break; |
| 205 | case 'edit-shortcode-password-reset': |
| 206 | $form_type = FR::PASSWORD_RESET_TYPE; |
| 207 | break; |
| 208 | case 'edit-shortcode-melange': |
| 209 | $form_type = FR::MELANGE_TYPE; |
| 210 | break; |
| 211 | case 'edit-shortcode-edit-profile': |
| 212 | $form_type = FR::EDIT_PROFILE_TYPE; |
| 213 | break; |
| 214 | case 'edit-shortcode-user-profile': |
| 215 | $form_type = FR::USER_PROFILE_TYPE; |
| 216 | break; |
| 217 | case 'edit-shortcode-member-directory': |
| 218 | $form_type = FR::MEMBERS_DIRECTORY_TYPE; |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | $preview_url = esc_url(add_query_arg( |
| 223 | ['pp_preview_form' => absint($_GET['id']), 'type' => $form_type], |
| 224 | home_url() |
| 225 | )); |
| 226 | |
| 227 | $html = "<a target='_blank' class=\"add-new-h2\" href=\"$preview_url\">" . esc_html__('Live Preview', 'wp-user-avatar') . '</a>'; |
| 228 | |
| 229 | if ($echo === false) { |
| 230 | return $html; |
| 231 | } |
| 232 | |
| 233 | echo $html; |
| 234 | } |
| 235 | |
| 236 | public function no_form_exist_redirect($form_id, $form_type) |
| 237 | { |
| 238 | if ( ! FR::form_id_exist($form_id, $form_type)) { |
| 239 | $url = ($form_type === FR::MEMBERS_DIRECTORY_TYPE) |
| 240 | ? PPRESS_MEMBER_DIRECTORIES_SETTINGS_PAGE |
| 241 | : add_query_arg('form-type', $form_type, PPRESS_FORMS_SETTINGS_PAGE); |
| 242 | |
| 243 | ppress_do_admin_redirect($url); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Build the settings page structure. I.e tab, sidebar. |
| 249 | * |
| 250 | * @return mixed|void |
| 251 | */ |
| 252 | public function settings_admin_page_callback() |
| 253 | { |
| 254 | remove_all_actions('media_buttons'); |
| 255 | remove_all_filters('media_buttons_context'); |
| 256 | remove_all_filters('mce_buttons', 10); |
| 257 | remove_all_filters('mce_external_plugins', 10); |
| 258 | |
| 259 | add_action('media_buttons', 'media_buttons'); |
| 260 | |
| 261 | if ( ! empty($_GET['view']) && $_GET['view'] == 'add-new-form') { |
| 262 | return AddNewForm::get_instance()->settings_admin_page(); |
| 263 | } |
| 264 | |
| 265 | $short_circuit = apply_filters('ppress_forms_settings_admin_page_short_circuit', false); |
| 266 | |
| 267 | if (false !== $short_circuit) return $short_circuit; |
| 268 | |
| 269 | if ( ! empty($_GET['view'])) { |
| 270 | |
| 271 | add_filter('wp_cspa_settings_page_sidebar', [$this->DragDropClassInstance, 'sidebar_section']); |
| 272 | add_action('wp_cspa_before_closing_header', [$this, 'live_form_preview_btn']); |
| 273 | |
| 274 | add_filter('wp_cspa_main_content_area', function ($content, $option_name) { |
| 275 | if ($option_name != 'pp_edit_form') return $content; |
| 276 | |
| 277 | if ($_GET['view'] == 'drag-drop-builder') { |
| 278 | ob_start(); |
| 279 | $this->DragDropClassInstance->admin_page(); |
| 280 | |
| 281 | return ob_get_clean(); |
| 282 | } |
| 283 | |
| 284 | return $content; |
| 285 | |
| 286 | }, 10, 2); |
| 287 | |
| 288 | $instance = Custom_Settings_Page_Api::instance(); |
| 289 | $instance->option_name('pp_edit_form'); |
| 290 | $instance->add_wrap_classes('pp-dnd-form-builder-wrap'); |
| 291 | $instance->page_header($this->admin_page_title()); |
| 292 | $this->register_core_settings($instance); |
| 293 | |
| 294 | return $instance->build(); |
| 295 | } |
| 296 | |
| 297 | add_filter('wp_cspa_main_content_area', array($this, 'wp_list_table'), 10, 2); |
| 298 | add_action('wp_cspa_before_post_body_content', array($this, 'form_sub_header')); |
| 299 | add_action('wp_cspa_before_closing_header', [$this, 'add_new_form_button']); |
| 300 | |
| 301 | $instance = Custom_Settings_Page_Api::instance(); |
| 302 | $instance->option_name(PPRESS_FORMS_DB_OPTION_NAME); |
| 303 | $instance->page_header($this->admin_page_title()); |
| 304 | $this->register_core_settings($instance, true); |
| 305 | echo '<div class="pp-form-listing pp-forms">'; |
| 306 | $instance->build(true); |
| 307 | echo '</div>'; |
| 308 | } |
| 309 | |
| 310 | public function add_new_form_button() |
| 311 | { |
| 312 | $url = add_query_arg('view', 'add-new-form', PPRESS_FORMS_SETTINGS_PAGE); |
| 313 | echo "<a class=\"add-new-h2\" href=\"$url\">" . esc_html__('Add New', 'wp-user-avatar') . '</a>'; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * @param string $content |
| 318 | * @param string $option_name settings Custom_Settings_Page_Api option name. |
| 319 | * |
| 320 | * @return string |
| 321 | */ |
| 322 | public function wp_list_table($content, $option_name) |
| 323 | { |
| 324 | if ($option_name != PPRESS_FORMS_DB_OPTION_NAME) return $content; |
| 325 | |
| 326 | $this->forms_instance->prepare_items(); |
| 327 | |
| 328 | ob_start(); |
| 329 | |
| 330 | $this->forms_instance->display(); |
| 331 | |
| 332 | return ob_get_clean(); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * @return Forms |
| 337 | */ |
| 338 | public static function get_instance() |
| 339 | { |
| 340 | static $instance = null; |
| 341 | |
| 342 | if (is_null($instance)) { |
| 343 | $instance = new self(); |
| 344 | } |
| 345 | |
| 346 | return $instance; |
| 347 | } |
| 348 | } |