| @@ -58,64 +58,42 @@ | ||
| 58 | 58 | true |
| 59 | 59 | ); |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - /** Check is email in wp users and password is correct */ | |
| 63 | - public static function validate_wp_user_if_exist() { | |
| 64 | - check_ajax_referer( 'bookit_validate_wp_user_if_exist', 'nonce' ); | |
| 62 | + /** | |
| 63 | + * Whether the account-lookup endpoint should respond. It supports the | |
| 64 | + * logged-out registered-booking flow, so limit availability to that case. | |
| 65 | + * | |
| 66 | + * @since 2.6.0 | |
| 67 | + * | |
| 68 | + * @return bool | |
| 69 | + */ | |
| 70 | + private static function account_lookup_available() { | |
| 71 | + return is_user_logged_in() | |
| 72 | + || 'registered' === get_option_by_path( 'bookit_settings.booking_type' ); | |
| 73 | + } | |
| 65 | 74 | |
| 66 | - if ( ! isset( $_POST['email'] ) || ( isset( $_POST['email'] ) && empty( $_POST['email'] ) ) | |
| 67 | - || ! isset( $_POST['password'] ) || ( isset( $_POST['password'] ) && empty( $_POST['password'] ) ) ) { | |
| 68 | - wp_send_json_success( | |
| 69 | - array( | |
| 70 | - 'exist' => false, | |
| 71 | - 'valid' => false, | |
| 72 | - ) | |
| 73 | - ); | |
| 74 | - } | |
| 75 | - | |
| 76 | - $exist_customer = get_user_by( 'email', $_POST['email'] ); | |
| 77 | - if ( ! ( $exist_customer instanceof \WP_User ) ) { | |
| 78 | - wp_send_json_success( | |
| 79 | - array( | |
| 80 | - 'exist' => false, | |
| 81 | - 'valid' => false, | |
| 82 | - ) | |
| 83 | - ); | |
| 84 | - } | |
| 85 | - | |
| 86 | - if ( wp_check_password( $_POST['password'], $exist_customer->data->user_pass, $exist_customer->ID ) ) { | |
| 87 | - wp_send_json_success( | |
| 88 | - array( | |
| 89 | - 'exist' => true, | |
| 90 | - 'valid' => true, | |
| 91 | - ) | |
| 92 | - ); | |
| 93 | - } | |
| 94 | - wp_send_json_success( | |
| 95 | - array( | |
| 96 | - 'exist' => true, | |
| 97 | - 'valid' => false, | |
| 98 | - ) | |
| 99 | - ); | |
| 100 | - } | |
| 101 | 75 | /** |
| 102 | - * @param $data | |
| 103 | - * get wp user by email if exist | |
| 76 | + * Tell the registered-booking form whether an account already exists for | |
| 77 | + * an email. Limited to the registered-booking flow. | |
| 78 | + * | |
| 79 | + * @since 2.6.0 | |
| 104 | 80 | */ |
| 105 | 81 | public static function get_wp_user_by_email() { |
| 106 | 82 | check_ajax_referer( 'bookit_get_wp_user_by_email', 'nonce' ); |
| 107 | 83 | |
| 108 | - if ( ! isset( $_POST['email'] ) || ( isset( $_POST['email'] ) && empty( $_POST['email'] ) ) ) { | |
| 109 | - wp_send_json_success( array( 'exist' => false ) ); | |
| 84 | + if ( ! self::account_lookup_available() ) { | |
| 85 | + wp_send_json_error( array( 'message' => __( 'Not available.', 'bookit' ) ) ); | |
| 110 | 86 | } |
| 111 | 87 | |
| 112 | - $exist_customer = get_user_by( 'email', $_POST['email'] ); | |
| 113 | - if ( $exist_customer instanceof \WP_User ) { | |
| 114 | - wp_send_json_success( array( 'exist' => true ) ); | |
| 115 | - } else { | |
| 88 | + $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; | |
| 89 | + if ( empty( $email ) ) { | |
| 116 | 90 | wp_send_json_success( array( 'exist' => false ) ); |
| 117 | 91 | } |
| 92 | + | |
| 93 | + $exist = get_user_by( 'email', $email ) instanceof \WP_User; | |
| 94 | + | |
| 95 | + wp_send_json_success( array( 'exist' => $exist ) ); | |
| 118 | 96 | } |
| 119 | 97 | |
| 120 | 98 | /** |
| 121 | 99 | * Get Customers with Pagination |
| @@ -126,19 +104,28 @@ | ||
| 126 | 104 | if ( ! current_user_can( 'manage_options' ) ) { |
| 127 | 105 | return false; |
| 128 | 106 | } |
| 129 | 107 | |
| 108 | + global $wpdb; | |
| 130 | 109 | $data = CleanHelper::cleanData( $_GET, self::getCleanRules() ); |
| 131 | 110 | |
| 132 | 111 | if ( ! empty( $data['limit'] ) ) { |
| 112 | + $search_where = ''; | |
| 113 | + $search_values = array(); | |
| 114 | + if ( ! empty( $data['search'] ) ) { | |
| 115 | + $like = '%' . $wpdb->esc_like( $data['search'] ) . '%'; | |
| 116 | + $search_where = 'WHERE full_name LIKE %s OR email LIKE %s OR phone LIKE %s'; | |
| 117 | + $search_values = array( $like, $like, $like ); | |
| 118 | + } | |
| 133 | 119 | $response['customers'] = Customers::get_paged( |
| 134 | 120 | $data['limit'], |
| 135 | 121 | $data['offset'], |
| 136 | - ( ! empty( $data['search'] ) ) ? "WHERE full_name LIKE '%{$data['search']}%' OR email LIKE '%{$data['search']}%' OR phone LIKE '%{$data['search']}%'" : '', | |
| 122 | + $search_where, | |
| 137 | 123 | ( isset( $data['sort'] ) && in_array( $data['sort'], self::$sortFields ) ) ? $data['sort'] : '', |
| 138 | - ( isset( $data['order'] ) && in_array( $data['order'], array( 'asc', 'desc' ) ) ) ? $data['order'] : '' | |
| 124 | + ( isset( $data['order'] ) && in_array( $data['order'], array( 'asc', 'desc' ) ) ) ? $data['order'] : '', | |
| 125 | + $search_values | |
| 139 | 126 | ); |
| 140 | - $response['total'] = ( ! empty( $data['search'] ) ) ? count( $response['customers'] ) : Customers::get_count(); | |
| 127 | + $response['total'] = ( ! empty( $search_where ) ) ? count( $response['customers'] ) : Customers::get_count(); | |
| 141 | 128 | |
| 142 | 129 | array_walk( |
| 143 | 130 | $response['customers'], |
| 144 | 131 | function ( &$value, $key ) { |
| @@ -279,12 +266,15 @@ | ||
| 279 | 266 | } |
| 280 | 267 | |
| 281 | 268 | do_action( 'bookit_customer_saved', $data['id'] ); |
| 282 | 269 | |
| 270 | + $customer = Customers::get( 'id', $data['id'] ); | |
| 271 | + | |
| 283 | 272 | wp_send_json_success( |
| 284 | 273 | array( |
| 285 | - 'id' => $data['id'], | |
| 286 | - 'message' => __( 'Customer Saved!', 'bookit' ), | |
| 274 | + 'id' => $data['id'], | |
| 275 | + 'customer' => $customer, | |
| 276 | + 'message' => __( 'Customer Saved!', 'bookit' ), | |
| 287 | 277 | ) |
| 288 | 278 | ); |
| 289 | 279 | } |
| 290 | 280 | |