y_reset_password_warning cookie enabled then show warning message on reset password page. if ( isset( $_COOKIE['display_reset_password_warning'] ) ) { $message = empty( $this->model->message ) ? $this->default_msg : $this->model->message; $errors->add( 'defender_password_reset', $message ); // Remove the one time cookie notice once it's displayed. $this->service->remove_cookie_notice( 'display_reset_password_warning' ); return $errors; } $login_password = $this->service->get_submitted_password(); if ( ! empty( $user->ID ) && ! empty( $login_password ) && wp_check_password( $login_password, get_userdata( $user->ID )->user_pass, $user->ID ) ) { $message = wp_kses( esc_html__( 'This password has been used already. Please choose a different one.', 'wpdef' ), array( 'strong' => array() ) ); $errors->add( 'defender_password_reset', $message ); return $errors; } return $errors; } /** * Update the time when a user resets their password. * * @param WP_User $user User object. * * @return void */ public function handle_password_reset( WP_User $user ): void { $this->service->handle_password_updated( $user ); } /** * Update password data when a user object is set or updated. * * @param int $user_id User ID. * @param WP_User $old_user_data Old user data. * * @return void */ public function handle_update_user( int $user_id, WP_User $old_user_data ) { $user = get_userdata( $user_id ); if ( $user->user_pass === $old_user_data->user_pass ) { return; } $this->service->handle_password_updated( $user ); } /** * Provide data to the frontend via localized script. * * @param array $data Data collection is ready to passed. * * @return array Modified data array with added this controller data. */ public function script_data( array $data ): array { $data['password_reset'] = $this->data_frontend(); return $data; } /** * Save settings. * * @param Request $request The request object containing new settings data. * * @return Response * @defender_route */ public function save_settings( Request $request ): Response { $data = $request->get_data_by_model( $this->model ); $this->model->import( $data ); if ( $this->model->validate() ) { $this->model->save(); Config_Hub_Helper::set_clear_active_flag(); $response = array( 'message' => esc_html__( 'Your settings have been updated.', 'wpdef' ), 'auto_close' => true, ); return new Response( true, array_merge( $response, $this->data_frontend() ) ); } return new Response( false, array( 'message' => $this->model->get_formatted_errors(), ) ); } /** * Save settings. * * @param Request $request The request object containing new settings data. * * @return Response * @defender_route */ public function toggle_reset( Request $request ) { $response = array(); $data = $request->get_data_by_model( $this->model ); if ( isset( $data['expire_force'] ) && true === $data['expire_force'] ) { $data['force_time'] = time(); $response = array( 'message' => esc_html__( 'Selected user roles are required to reset their password upon next login.', 'wpdef' ), ); } else { $response['message'] = esc_html__( 'Force Reset Password has been disabled.', 'wpdef' ); } $this->model->import( $data ); if ( $this->model->validate() ) { $this->model->save(); Config_Hub_Helper::set_clear_active_flag(); return new Response( true, array_merge( $response, $this->data_frontend() ) ); } return new Response( false, array( 'message' => $this->model->get_formatted_errors(), ) ); } /** * Removes settings for all submodules. */ public function remove_settings() { } /** * Delete all the data & the cache. */ public function remove_data(): void { delete_metadata( 'user', null, 'wd_last_password_change', null, true ); } /** * Provides data for the frontend. * * @return array An array of data for the frontend. */ public function data_frontend(): array { $model = $this->get_model(); return array_merge( array( 'model' => $model->export(), 'all_roles' => wp_list_pluck( get_editable_roles(), 'name' ), 'reset_last' => empty( $model->force_time ) ? '' : $this->format_date_time( $model->force_time ), 'default_message' => $this->default_msg, ), $this->dump_routes_and_nonces() ); } /** * Imports data into the model. * * @param array $data Data to be imported into the model. * * @throws Exception If table is not defined. */ public function import_data( array $data ) { $model = $this->get_model(); $model->import( $data ); if ( $model->validate() ) { $model->save(); } } /** * Converts the current object state to an array. * * @return array The array representation of the object. */ public function to_array(): array { return array(); } /** * Exports strings. * * @return array An array of strings. */ public function export_strings() { return array(); } }y_reset_password_warning cookie enabled then show warning message on reset password page. if ( isset( $_COOKIE['display_reset_password_warning'] ) ) { $message = empty( $this->model->message ) ? $this->default_msg : $this->model->message; $errors->add( 'defender_password_reset', $message ); // Remove the one time cookie notice once it's displayed. $this->service->remove_cookie_notice( 'display_reset_password_warning' ); return $errors; } $login_password = $this->service->get_submitted_password(); if ( ! empty( $user->ID ) && ! empty( $login_password ) && wp_check_password( $login_password, get_userdata( $user->ID )->user_pass, $user->ID ) ) { $message = wp_kses( esc_html__( 'This password has been used already. Please choose a different one.', 'wpdef' ), array( 'strong' => array() ) ); $errors->add( 'defender_password_reset', $message ); return $errors; } return $errors; } /** * Update the time when a user resets their password. * * @param WP_User $user User object. * * @return void */ public function handle_password_reset( WP_User $user ): void { $this->service->handle_password_updated( $user ); } /** * Update password data when a user object is set or updated. * * @param int $user_id User ID. * @param WP_User $old_user_data Old user data. * * @return void */ public function handle_update_user( int $user_id, WP_User $old_user_data ) { $user = get_userdata( $user_id ); if ( $user->user_pass === $old_user_data->user_pass ) { return; } $this->service->handle_password_updated( $user ); } /** * Provide data to the frontend via localized script. * * @param array $data Data collection is ready to passed. * * @return array Modified data array with added this controller data. */ public function script_data( array $data ): array { $data['password_reset'] = $this->data_frontend(); return $data; } /** * Save settings. * * @param Request $request The request object containing new settings data. * * @return Response * @defender_route */ public function save_settings( Request $request ): Response { $data = $request->get_data_by_model( $this->model ); $this->model->import( $data ); if ( $this->model->validate() ) { $this->model->save(); Config_Hub_Helper::set_clear_active_flag(); $response = array( 'message' => esc_html__( 'Your settings have been updated.', 'wpdef' ), 'auto_close' => true, ); return new Response( true, array_merge( $response, $this->data_frontend() ) ); } return new Response( false, array( 'message' => $this->model->get_formatted_errors(), ) ); } /** * Save settings. * * @param Request $request The request object containing new settings data. * * @return Response * @defender_route */ public function toggle_reset( Request $request ) { $response = array(); $data = $request->get_data_by_model( $this->model ); if ( isset( $data['expire_force'] ) && true === $data['expire_force'] ) { $data['force_time'] = time(); $response = array( 'message' => esc_html__( 'Selected user roles are required to reset their password upon next login.', 'wpdef' ), ); } else { $response['message'] = esc_html__( 'Force Reset Password has been disabled.', 'wpdef' ); } $this->model->import( $data ); if ( $this->model->validate() ) { $this->model->save(); Config_Hub_Helper::set_clear_active_flag(); return new Response( true, array_merge( $response, $this->data_frontend() ) ); } return new Response( false, array( 'message' => $this->model->get_formatted_errors(), ) ); } /** * Removes settings for all submodules. */ public function remove_settings() { } /** * Delete all the data & the cache. */ public function remove_data(): void { delete_metadata( 'user', null, 'wd_last_password_change', null, true ); } /** * Provides data for the frontend. * * @return array An array of data for the frontend. */ public function data_frontend(): array { $model = $this->get_model(); return array_merge( array( 'model' => $model->export(), 'all_roles' => wp_list_pluck( get_editable_roles(), 'name' ), 'reset_last' => empty( $model->force_time ) ? '' : $this->format_date_time( $model->force_time ), 'default_message' => $this->default_msg, ), $this->dump_routes_and_nonces() ); } /** * Imports data into the model. * * @param array $data Data to be imported into the model. * * @throws Exception If table is not defined. */ public function import_data( array $data ) { $model = $this->get_model(); $model->import( $data ); if ( $model->validate() ) { $model->save(); } } /** * Converts the current object state to an array. * * @return array The array representation of the object. */ public function to_array(): array { return array(); } /** * Exports strings. * * @return array An array of strings. */ public function export_strings() { return array(); } }