| @@ -7,13 +7,11 @@ | ||
| 7 | 7 | * @since 1.0.0 |
| 8 | 8 | * @author GeoDirectory Team <info@wpgeodirectory.com> |
| 9 | 9 | */ |
| 10 | 10 | class UsersWP_Ajax { |
| 11 | - | |
| 12 | - private $form_builder; | |
| 13 | 11 | |
| 14 | - public function __construct($form_builder) { | |
| 15 | - $this->form_builder = $form_builder; | |
| 12 | + public function __construct() { | |
| 13 | + add_action( 'init', array( $this, 'add_ajax_events' ), 0 ); | |
| 16 | 14 | } |
| 17 | 15 | |
| 18 | 16 | /** |
| 19 | 17 | * Handles the create custom field and sort custom field ajax requests. |
| @@ -21,29 +19,40 @@ | ||
| 21 | 19 | * @since 1.0.0 |
| 22 | 20 | * @package userswp |
| 23 | 21 | * @return void |
| 24 | 22 | */ |
| 25 | - public function handler() | |
| 23 | + public function add_ajax_events() | |
| 26 | 24 | { |
| 27 | - if ((isset($_REQUEST['uwp_ajax']) && $_REQUEST['uwp_ajax'] == 'admin_ajax') || isset($_REQUEST['create_field']) || isset($_REQUEST['sort_create_field'])) { | |
| 28 | - if (current_user_can('manage_options')) { | |
| 29 | - if (isset($_REQUEST['create_field'])) { | |
| 30 | - $this->create_field($_REQUEST); | |
| 31 | - $this->uwp_die(); | |
| 32 | - } | |
| 33 | - } else { | |
| 34 | - $login_page = uwp_get_page_id('login_page', false); | |
| 35 | - if ($login_page) { | |
| 36 | - wp_redirect(get_permalink($login_page)); | |
| 37 | - } else { | |
| 38 | - wp_redirect(home_url('/')); | |
| 39 | - } | |
| 40 | - $this->uwp_die(); | |
| 25 | + | |
| 26 | + $ajax_events = $this->get_ajax_events(); | |
| 27 | + | |
| 28 | + foreach ( $ajax_events as $ajax_event => $nopriv ) { | |
| 29 | + add_action( 'wp_ajax_uwp_' . $ajax_event, array( $this, $ajax_event ) ); | |
| 30 | + | |
| 31 | + if ( $nopriv ) { | |
| 32 | + add_action( 'wp_ajax_nopriv_uwp_' . $ajax_event, array( $this, $ajax_event ) ); | |
| 41 | 33 | } |
| 42 | 34 | } |
| 43 | 35 | } |
| 44 | 36 | |
| 37 | + public function get_ajax_events(){ | |
| 38 | + // uwp_event_name => nopriv | |
| 39 | + return array( | |
| 40 | + 'notice_clear_try_bootstrap' => false, | |
| 41 | + 'wizard_setup_menu' => false, | |
| 42 | + ); | |
| 43 | + } | |
| 44 | + | |
| 45 | 45 | /** |
| 46 | + * A quick delete of the try bootstrap notice option. | |
| 47 | + */ | |
| 48 | + public function notice_clear_try_bootstrap(){ | |
| 49 | + if (current_user_can('manage_options')) { | |
| 50 | + delete_option("uwp_notice_try_bootstrap"); | |
| 51 | + } | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 46 | 55 | * Kill WordPress execution and display HTML message with error message. |
| 47 | 56 | * |
| 48 | 57 | * @since 1.0.0 |
| 49 | 58 | * @package userswp |
| @@ -54,82 +63,34 @@ | ||
| 54 | 63 | */ |
| 55 | 64 | public function uwp_die( $message = '', $title = '', $status = 400 ) { |
| 56 | 65 | add_filter( 'wp_die_ajax_handler', '_uwp_die_handler', 10, 3 ); |
| 57 | 66 | add_filter( 'wp_die_handler', '_uwp_die_handler', 10, 3 ); |
| 58 | - wp_die( $message, $title, array( 'response' => $status )); | |
| 67 | + wp_die( $message, $title, array( 'response' => $status )); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 59 | 68 | } |
| 60 | 69 | |
| 61 | - /** | |
| 62 | - * Handles the create custom field ajax request. | |
| 63 | - * | |
| 64 | - * @since 1.0.0 | |
| 65 | - * @package userswp | |
| 66 | - * @param array $data Submitted $_REQUEST data. | |
| 67 | - * @return void | |
| 68 | - */ | |
| 69 | - public function create_field($data) { | |
| 70 | + /** | |
| 71 | + * Function to setup menu in the setup wizard. | |
| 72 | + * | |
| 73 | + * @return void | |
| 74 | + */ | |
| 75 | + public function wizard_setup_menu() { | |
| 70 | 76 | |
| 71 | - $form_type = isset($data['form_type']) ? sanitize_text_field($data['form_type']) : ''; | |
| 72 | - $field_type = isset($data['field_type']) ? sanitize_text_field($data['field_type']) : ''; | |
| 73 | - $field_type_key = isset($data['field_type_key']) ? sanitize_text_field($data['field_type_key']) : ''; | |
| 74 | - $field_action = isset($data['field_ins_upd']) ? sanitize_text_field($data['field_ins_upd']) : ''; | |
| 75 | - $field_id = isset($data['field_id']) ? sanitize_text_field($data['field_id']) : ''; | |
| 77 | + check_ajax_referer( 'uwp-wizard-setup-menu', 'security' ); | |
| 76 | 78 | |
| 77 | - $field_id = $field_id != '' ? trim($field_id, '_') : $field_id; | |
| 79 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 80 | + wp_die( -1 ); | |
| 81 | + } | |
| 78 | 82 | |
| 79 | - $field_ids = array(); | |
| 80 | - if (!empty($data['licontainer']) && is_array($data['licontainer'])) { | |
| 81 | - foreach ($data['licontainer'] as $lic_id) { | |
| 82 | - $field_ids[] = sanitize_text_field($lic_id); | |
| 83 | - } | |
| 84 | - } | |
| 83 | + $menu_id = isset($_REQUEST['menu_id']) ? sanitize_title_with_dashes($_REQUEST['menu_id']) : ''; | |
| 84 | + $menu_location = isset($_REQUEST['menu_location']) ? sanitize_title_with_dashes($_REQUEST['menu_location']) : ''; | |
| 85 | + $result = UsersWP_Tools::setup_menu( $menu_id, $menu_location); | |
| 85 | 86 | |
| 86 | - /* ------- check nonce field ------- */ | |
| 87 | - if (isset($data['update']) && $data['update'] == "update" && isset($data['create_field']) && isset($data['manage_field_type']) && $data['manage_field_type'] == 'custom_fields') { | |
| 88 | - echo $this->form_builder->uwp_set_field_order($field_ids); | |
| 89 | - } | |
| 87 | + if(is_wp_error( $result ) ){ | |
| 88 | + wp_send_json_error( $result->get_error_message() ); | |
| 89 | + }else{ | |
| 90 | + wp_send_json_success($result); | |
| 91 | + } | |
| 90 | 92 | |
| 91 | - /* ---- Show field form in admin ---- */ | |
| 92 | - if ($field_type != '' && $field_id != '' && $field_action == 'new' && isset($data['create_field']) && isset($data['manage_field_type']) && $data['manage_field_type'] == 'custom_fields') { | |
| 93 | - $this->form_builder->uwp_form_field_adminhtml($field_type, $field_id, $field_action,$field_type_key, $form_type); | |
| 94 | - } | |
| 93 | + wp_die(); | |
| 95 | 94 | |
| 96 | - | |
| 97 | - /* ---- Delete field ---- */ | |
| 98 | - if ($field_id != '' && $field_action == 'delete' && isset($data['_wpnonce']) && isset($data['create_field']) && isset($data['manage_field_type']) && $data['manage_field_type'] == 'custom_fields') { | |
| 99 | - if (!wp_verify_nonce($data['_wpnonce'], 'custom_fields_' . $field_id)) | |
| 100 | - return; | |
| 101 | - | |
| 102 | - echo $this->form_builder->uwp_admin_form_field_delete($field_id); | |
| 103 | - } | |
| 104 | - | |
| 105 | - /* ---- Save field ---- */ | |
| 106 | - if ($field_id != '' && $field_action == 'submit' && isset($data['_wpnonce']) && isset($data['create_field']) && isset($data['manage_field_type']) && $data['manage_field_type'] == 'custom_fields') { | |
| 107 | - if (!wp_verify_nonce($data['_wpnonce'], 'custom_fields_' . $field_id)) | |
| 108 | - return; | |
| 109 | - | |
| 110 | - foreach ($data as $pkey => $pval) { | |
| 111 | - if (is_array($data[$pkey])) { | |
| 112 | - $tags = 'skip_field'; | |
| 113 | - } else { | |
| 114 | - $tags = ''; | |
| 115 | - } | |
| 116 | - | |
| 117 | - if ($tags != 'skip_field') { | |
| 118 | - $data[$pkey] = strip_tags($data[$pkey], $tags); | |
| 119 | - } | |
| 120 | - } | |
| 121 | - | |
| 122 | - $return = $this->form_builder->uwp_admin_form_field_save($data); | |
| 123 | - | |
| 124 | - if (is_int($return)) { | |
| 125 | - $lastid = $return; | |
| 126 | - $this->form_builder->uwp_form_field_adminhtml($field_type, $lastid, 'submit',$field_type_key, $form_type); | |
| 127 | - } else { | |
| 128 | - echo $return; | |
| 129 | - } | |
| 130 | - } | |
| 131 | - | |
| 132 | - wp_die(); | |
| 133 | - | |
| 134 | - } | |
| 95 | + } | |
| 135 | 96 | } |