Changeset 43008 for trunk/src/wp-admin/includes/user.php
- Timestamp:
- 04/27/2018 10:12:01 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/user.php
r43000 r43008 582 582 583 583 /** 584 * Get action description from the name.584 * Resend an existing request and return the result. 585 585 * 586 586 * @since 4.9.6 587 587 * @access private 588 588 * 589 * @return string 590 */ 591 function _wp_privacy_action_description( $request_type ) { 592 switch ( $request_type ) { 593 case 'user_export_request': 594 return __( 'Export Personal Data' ); 595 case 'user_remove_request': 596 return __( 'Remove Personal Data' ); 597 } 598 } 599 600 /** 601 * Log a request and send to the user. 602 * 603 * @since 4.9.6 604 * @access private 605 * 606 * @param string $email_address Email address sending the request to. 607 * @param string $action Action being requested. 608 * @param string $description Description of request. 609 * @return bool|WP_Error depending on success. 610 */ 611 function _wp_privacy_create_request( $email_address, $action, $description ) { 612 $user_id = 0; 613 $user = get_user_by( 'email', $email_address ); 614 615 if ( $user ) { 616 $user_id = $user->ID; 617 } 618 619 $privacy_request_id = wp_insert_post( array( 620 'post_author' => $user_id, 621 'post_status' => 'request-pending', 622 'post_type' => $action, 623 'post_date' => current_time( 'mysql', false ), 624 'post_date_gmt' => current_time( 'mysql', true ), 625 ), true ); 626 627 if ( is_wp_error( $privacy_request_id ) ) { 628 return $privacy_request_id; 629 } 630 631 update_post_meta( $privacy_request_id, '_user_email', $email_address ); 632 update_post_meta( $privacy_request_id, '_action_name', $action ); 633 update_post_meta( $privacy_request_id, '_confirmed_timestamp', false ); 634 635 return wp_send_account_verification_key( $email_address, $action, $description, array( 636 'privacy_request_id' => $privacy_request_id, 637 ) ); 638 } 639 640 /** 641 * Resend an existing request and return the result. 642 * 643 * @since 4.9.6 644 * @access private 645 * 646 * @param int $privacy_request_id Request ID. 589 * @param int $request_id Request ID. 647 590 * @return bool|WP_Error 648 591 */ 649 function _wp_privacy_resend_request( $ privacy_request_id ) {650 $ privacy_request_id = absint( $privacy_request_id );651 $ privacy_request = get_post( $privacy_request_id );652 653 if ( ! $ privacy_request || ! in_array( $privacy_request->post_type, _wp_privacy_action_request_types(), true )) {592 function _wp_privacy_resend_request( $request_id ) { 593 $request_id = absint( $request_id ); 594 $request = get_post( $request_id ); 595 596 if ( ! $request || 'user_request' !== $request->post_type ) { 654 597 return new WP_Error( 'privacy_request_error', __( 'Invalid request.' ) ); 655 598 } 656 599 657 $email_address = get_post_meta( $privacy_request_id, '_user_email', true ); 658 $action = get_post_meta( $privacy_request_id, '_action_name', true ); 659 $description = _wp_privacy_action_description( $action ); 660 $result = wp_send_account_verification_key( $email_address, $action, $description, array( 661 'privacy_request_id' => $privacy_request_id, 662 ) ); 600 $result = wp_send_user_request( $request_id ); 663 601 664 602 if ( is_wp_error( $result ) ) { … … 668 606 } 669 607 670 wp_update_post( array(671 'ID' => $privacy_request_id,672 'post_status' => 'request-pending',673 'post_date' => current_time( 'mysql', false ),674 'post_date_gmt' => current_time( 'mysql', true ),675 ) );676 677 608 return true; 678 609 } … … 684 615 * @access private 685 616 * 686 * @param int $ privacy_request_id Request ID.687 * @return bool|WP_Error688 */ 689 function _wp_privacy_completed_request( $ privacy_request_id ) {690 $ privacy_request_id = absint( $privacy_request_id );691 $ privacy_request = get_post( $privacy_request_id );692 693 if ( ! $ privacy_request || ! in_array( $privacy_request->post_type, _wp_privacy_action_request_types(), true )) {617 * @param int $request_id Request ID. 618 * @return int|WP_Error Request ID on succes or WP_Error. 619 */ 620 function _wp_privacy_completed_request( $request_id ) { 621 $request_id = absint( $request_id ); 622 $request_data = wp_get_user_request_data( $request_id ); 623 624 if ( ! $request_data ) { 694 625 return new WP_Error( 'privacy_request_error', __( 'Invalid request.' ) ); 695 626 } 696 627 697 wp_update_post( array( 698 'ID' => $privacy_request_id, 699 'post_status' => 'request-completed', 628 update_post_meta( $request_id, '_wp_user_request_confirmed_timestamp', time() ); 629 $request = wp_update_post( array( 630 'ID' => $request_data['request_id'], 631 'post_status' => 'request-confirmed', 700 632 ) ); 701 702 update_post_meta( $privacy_request_id, '_completed_timestamp', time() ); 633 return $request; 703 634 } 704 635 … … 804 735 } 805 736 806 if ( ! empty( $email_address ) ) { 807 $result = _wp_privacy_create_request( $email_address, $action_type, _wp_privacy_action_description( $action_type ) ); 808 809 if ( is_wp_error( $result ) ) { 810 add_settings_error( 811 'username_or_email_to_export', 812 'username_or_email_to_export', 813 $result->get_error_message(), 814 'error' 815 ); 816 } elseif ( ! $result ) { 817 add_settings_error( 818 'username_or_email_to_export', 819 'username_or_email_to_export', 820 __( 'Unable to initiate confirmation request.' ), 821 'error' 822 ); 823 } else { 824 add_settings_error( 825 'username_or_email_to_export', 826 'username_or_email_to_export', 827 __( 'Confirmation request initiated successfully.' ), 828 'updated' 829 ); 830 } 737 if ( empty( $email_address ) ) { 738 break; 831 739 } 740 741 $request_id = wp_create_user_request( $email_address, $action_type ); 742 743 if ( is_wp_error( $request_id ) ) { 744 add_settings_error( 745 'username_or_email_to_export', 746 'username_or_email_to_export', 747 $request_id->get_error_message(), 748 'error' 749 ); 750 break; 751 } elseif ( ! $request_id ) { 752 add_settings_error( 753 'username_or_email_to_export', 754 'username_or_email_to_export', 755 __( 'Unable to initiate confirmation request.' ), 756 'error' 757 ); 758 break; 759 } 760 761 wp_send_user_request( $request_id ); 762 763 add_settings_error( 764 'username_or_email_to_export', 765 'username_or_email_to_export', 766 __( 'Confirmation request initiated successfully.' ), 767 'updated' 768 ); 832 769 break; 833 770 } … … 872 809 <?php wp_nonce_field( 'personal-data-request' ); ?> 873 810 <input type="hidden" name="action" value="add_export_personal_data_request" /> 874 <input type="hidden" name="type_of_action" value=" user_export_request" />811 <input type="hidden" name="type_of_action" value="export_personal_data" /> 875 812 </form> 876 813 <hr /> … … 938 875 <?php wp_nonce_field( 'personal-data-request' ); ?> 939 876 <input type="hidden" name="action" value="add_remove_personal_data_request" /> 940 <input type="hidden" name="type_of_action" value=" user_remove_request" />877 <input type="hidden" name="type_of_action" value="remove_personal_data" /> 941 878 </form> 942 879 <hr /> … … 1012 949 public function get_columns() { 1013 950 $columns = array( 1014 'cb' => '<input type="checkbox" />',1015 'email' => __( 'Requester' ),1016 'status' => __( 'Status' ),1017 'requested '=> __( 'Requested' ),1018 'next_steps' => __( 'Next Steps' ),951 'cb' => '<input type="checkbox" />', 952 'email' => __( 'Requester' ), 953 'status' => __( 'Status' ), 954 'requested_timestamp' => __( 'Requested' ), 955 'next_steps' => __( 'Next Steps' ), 1019 956 ); 1020 957 return $columns; … … 1041 978 protected function get_default_primary_column_name() { 1042 979 return 'email'; 980 } 981 982 /** 983 * Count number of requests for each status. 984 * 985 * @since 4.9.6 986 * 987 * @return object Number of posts for each status. 988 */ 989 protected function get_request_counts() { 990 global $wpdb; 991 992 $cache_key = $this->post_type . '-' . $this->request_type; 993 $counts = wp_cache_get( $cache_key, 'counts' ); 994 995 if ( false !== $counts ) { 996 return $counts; 997 } 998 999 $query = " 1000 SELECT post_status, COUNT( * ) AS num_posts 1001 FROM {$wpdb->posts} 1002 WHERE post_type = %s 1003 AND post_title = %s 1004 GROUP BY post_status"; 1005 1006 $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $this->post_type, $this->request_type ), ARRAY_A ); 1007 $counts = array_fill_keys( get_post_stati(), 0 ); 1008 1009 foreach ( $results as $row ) { 1010 $counts[ $row['post_status'] ] = $row['num_posts']; 1011 } 1012 1013 $counts = (object) $counts; 1014 wp_cache_set( $cache_key, $counts, 'counts' ); 1015 1016 return $counts; 1043 1017 } 1044 1018 … … 1056 1030 $views = array(); 1057 1031 $admin_url = admin_url( 'tools.php?page=' . $this->request_type ); 1058 $counts = wp_count_posts( $this->post_type);1032 $counts = $this->get_request_counts(); 1059 1033 1060 1034 $current_link_attributes = empty( $current_status ) ? ' class="current" aria-current="page"' : ''; … … 1091 1065 $action = $this->current_action(); 1092 1066 $request_ids = isset( $_REQUEST['request_id'] ) ? wp_parse_id_list( wp_unslash( $_REQUEST['request_id'] ) ) : array(); // WPCS: input var ok, CSRF ok. 1067 $count = 0; 1093 1068 1094 1069 if ( $request_ids ) { … … 1098 1073 switch ( $action ) { 1099 1074 case 'delete': 1100 $count = 0;1101 1102 1075 foreach ( $request_ids as $request_id ) { 1103 1076 if ( wp_delete_post( $request_id, true ) ) { … … 1114 1087 break; 1115 1088 case 'resend': 1116 $count = 0;1117 1118 1089 foreach ( $request_ids as $request_id ) { 1119 if ( _wp_privacy_resend_request( $request_id ) ) { 1120 $count ++; 1090 $resend = _wp_privacy_resend_request( $request_id ); 1091 1092 if ( $resend && ! is_wp_error( $resend ) ) { 1093 $count++; 1121 1094 } 1122 1095 } … … 1152 1125 $args = array( 1153 1126 'post_type' => $this->post_type, 1127 'title' => $this->request_type, 1154 1128 'posts_per_page' => $posts_per_page, 1155 1129 'offset' => isset( $_REQUEST['paged'] ) ? max( 0, absint( $_REQUEST['paged'] ) - 1 ) * $posts_per_page: 0, … … 1167 1141 'relation' => 'AND', 1168 1142 array( 1169 'key' => '_ user_email',1143 'key' => '_wp_user_request_user_email', 1170 1144 'value' => isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ): '', 1171 'compare' => 'LIKE' 1145 'compare' => 'LIKE', 1172 1146 ), 1173 1147 ); 1174 1148 } 1175 1149 1176 $privacy_requests_query = new WP_Query( $args ); 1177 $privacy_requests = $privacy_requests_query->posts; 1178 1179 foreach ( $privacy_requests as $privacy_request ) { 1180 $this->items[] = array( 1181 'request_id' => $privacy_request->ID, 1182 'user_id' => $privacy_request->post_author, 1183 'email' => get_post_meta( $privacy_request->ID, '_user_email', true ), 1184 'action' => get_post_meta( $privacy_request->ID, '_action_name', true ), 1185 'requested' => strtotime( $privacy_request->post_date_gmt ), 1186 'confirmed' => get_post_meta( $privacy_request->ID, '_confirmed_timestamp', true ), 1187 'completed' => get_post_meta( $privacy_request->ID, '_completed_timestamp', true ), 1188 ); 1150 $requests_query = new WP_Query( $args ); 1151 $requests = $requests_query->posts; 1152 1153 foreach ( $requests as $request ) { 1154 $this->items[] = wp_get_user_request_data( $request->ID ); 1189 1155 } 1190 1156 1191 1157 $this->set_pagination_args( 1192 1158 array( 1193 'total_items' => $ privacy_requests_query->found_posts,1159 'total_items' => $requests_query->found_posts, 1194 1160 'per_page' => $posts_per_page, 1195 1161 ) … … 1229 1195 switch ( $status ) { 1230 1196 case 'request-confirmed': 1231 $timestamp = $item['confirmed '];1197 $timestamp = $item['confirmed_timestamp']; 1232 1198 break; 1233 1199 case 'request-completed': 1234 $timestamp = $item['completed '];1200 $timestamp = $item['completed_timestamp']; 1235 1201 break; 1236 1202 } … … 1280 1246 $cell_value = $item[ $column_name ]; 1281 1247 1282 if ( in_array( $column_name, array( 'requested ' ), true ) ) {1248 if ( in_array( $column_name, array( 'requested_timestamp' ), true ) ) { 1283 1249 return $this->get_timestamp_as_date( $cell_value ); 1284 1250 } … … 1353 1319 * @var string $post_type The post type. 1354 1320 */ 1355 protected $post_type = 'user_ export_request';1321 protected $post_type = 'user_request'; 1356 1322 1357 1323 /** … … 1438 1404 * @var string $post_type The post type. 1439 1405 */ 1440 protected $post_type = 'user_re move_request';1406 protected $post_type = 'user_request'; 1441 1407 1442 1408 /**
Note: See TracChangeset
for help on using the changeset viewer.