[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-admin/includes/ -> class-wp-privacy-requests-table.php (source)

   1  <?php
   2  /**
   3   * List Table API: WP_Privacy_Requests_Table class
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   * @since 4.9.6
   8   */
   9  
  10  abstract class WP_Privacy_Requests_Table extends WP_List_Table {
  11  
  12      /**
  13       * Action name for the requests this table will work with. Classes
  14       * which inherit from WP_Privacy_Requests_Table should define this.
  15       *
  16       * Example: 'export_personal_data'.
  17       *
  18       * @since 4.9.6
  19       *
  20       * @var string $request_type Name of action.
  21       */
  22      protected $request_type = 'INVALID';
  23  
  24      /**
  25       * Post type to be used.
  26       *
  27       * @since 4.9.6
  28       *
  29       * @var string $post_type The post type.
  30       */
  31      protected $post_type = 'INVALID';
  32  
  33      /**
  34       * Get columns to show in the list table.
  35       *
  36       * @since 4.9.6
  37       *
  38       * @return array Array of columns.
  39       */
  40  	public function get_columns() {
  41          $columns = array(
  42              'cb'                => '<input type="checkbox" />',
  43              'email'             => __( 'Requester' ),
  44              'status'            => __( 'Status' ),
  45              'created_timestamp' => __( 'Requested' ),
  46              'next_steps'        => __( 'Next Steps' ),
  47          );
  48          return $columns;
  49      }
  50  
  51      /**
  52       * Normalize the admin URL to the current page (by request_type).
  53       *
  54       * @since 5.3.0
  55       *
  56       * @return string URL to the current admin page.
  57       */
  58  	protected function get_admin_url() {
  59          $pagenow = str_replace( '_', '-', $this->request_type );
  60  
  61          if ( 'remove-personal-data' === $pagenow ) {
  62              $pagenow = 'erase-personal-data';
  63          }
  64  
  65          return admin_url( $pagenow . '.php' );
  66      }
  67  
  68      /**
  69       * Get a list of sortable columns.
  70       *
  71       * @since 4.9.6
  72       *
  73       * @return array Default sortable columns.
  74       */
  75  	protected function get_sortable_columns() {
  76          // The initial sorting is by 'Requested' (post_date) and descending.
  77          // With initial sorting, the first click on 'Requested' should be ascending.
  78          // With 'Requester' sorting active, the next click on 'Requested' should be descending.
  79          $desc_first = isset( $_GET['orderby'] );
  80  
  81          return array(
  82              'email'             => 'requester',
  83              'created_timestamp' => array( 'requested', $desc_first ),
  84          );
  85      }
  86  
  87      /**
  88       * Default primary column.
  89       *
  90       * @since 4.9.6
  91       *
  92       * @return string Default primary column name.
  93       */
  94  	protected function get_default_primary_column_name() {
  95          return 'email';
  96      }
  97  
  98      /**
  99       * Count number of requests for each status.
 100       *
 101       * @since 4.9.6
 102       *
 103       * @return object Number of posts for each status.
 104       */
 105  	protected function get_request_counts() {
 106          global $wpdb;
 107  
 108          $cache_key = $this->post_type . '-' . $this->request_type;
 109          $counts    = wp_cache_get( $cache_key, 'counts' );
 110  
 111          if ( false !== $counts ) {
 112              return $counts;
 113          }
 114  
 115          $query = "
 116              SELECT post_status, COUNT( * ) AS num_posts
 117              FROM {$wpdb->posts}
 118              WHERE post_type = %s
 119              AND post_name = %s
 120              GROUP BY post_status";
 121  
 122          $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $this->post_type, $this->request_type ), ARRAY_A );
 123          $counts  = array_fill_keys( get_post_stati(), 0 );
 124  
 125          foreach ( $results as $row ) {
 126              $counts[ $row['post_status'] ] = $row['num_posts'];
 127          }
 128  
 129          $counts = (object) $counts;
 130          wp_cache_set( $cache_key, $counts, 'counts' );
 131  
 132          return $counts;
 133      }
 134  
 135      /**
 136       * Get an associative array ( id => link ) with the list of views available on this table.
 137       *
 138       * @since 4.9.6
 139       *
 140       * @return array Associative array of views in the format of $view_name => $view_markup.
 141       */
 142  	protected function get_views() {
 143          $current_status = isset( $_REQUEST['filter-status'] ) ? sanitize_text_field( $_REQUEST['filter-status'] ) : '';
 144          $statuses       = _wp_privacy_statuses();
 145          $views          = array();
 146          $counts         = $this->get_request_counts();
 147          $total_requests = absint( array_sum( (array) $counts ) );
 148  
 149          // Normalized admin URL
 150          $admin_url = $this->get_admin_url();
 151  
 152          $current_link_attributes = empty( $current_status ) ? ' class="current" aria-current="page"' : '';
 153          $status_label            = sprintf(
 154              /* translators: %s: Number of requests. */
 155              _nx(
 156                  'All <span class="count">(%s)</span>',
 157                  'All <span class="count">(%s)</span>',
 158                  $total_requests,
 159                  'requests'
 160              ),
 161              number_format_i18n( $total_requests )
 162          );
 163  
 164          $views['all'] = sprintf(
 165              '<a href="%s"%s>%s</a>',
 166              esc_url( $admin_url ),
 167              $current_link_attributes,
 168              $status_label
 169          );
 170  
 171          foreach ( $statuses as $status => $label ) {
 172              $post_status = get_post_status_object( $status );
 173              if ( ! $post_status ) {
 174                  continue;
 175              }
 176  
 177              $current_link_attributes = $status === $current_status ? ' class="current" aria-current="page"' : '';
 178              $total_status_requests   = absint( $counts->{$status} );
 179  
 180              $status_label = sprintf(
 181                  translate_nooped_plural( $post_status->label_count, $total_status_requests ),
 182                  number_format_i18n( $total_status_requests )
 183              );
 184  
 185              $status_link = add_query_arg( 'filter-status', $status, $admin_url );
 186  
 187              $views[ $status ] = sprintf(
 188                  '<a href="%s"%s>%s</a>',
 189                  esc_url( $status_link ),
 190                  $current_link_attributes,
 191                  $status_label
 192              );
 193          }
 194  
 195          return $views;
 196      }
 197  
 198      /**
 199       * Get bulk actions.
 200       *
 201       * @since 4.9.6
 202       *
 203       * @return array List of bulk actions.
 204       */
 205  	protected function get_bulk_actions() {
 206          return array(
 207              'delete' => __( 'Remove' ),
 208              'resend' => __( 'Resend email' ),
 209          );
 210      }
 211  
 212      /**
 213       * Process bulk actions.
 214       *
 215       * @since 4.9.6
 216       */
 217  	public function process_bulk_action() {
 218          $action      = $this->current_action();
 219          $request_ids = isset( $_REQUEST['request_id'] ) ? wp_parse_id_list( wp_unslash( $_REQUEST['request_id'] ) ) : array();
 220  
 221          $count = 0;
 222  
 223          if ( $request_ids ) {
 224              check_admin_referer( 'bulk-privacy_requests' );
 225          }
 226  
 227          switch ( $action ) {
 228              case 'delete':
 229                  foreach ( $request_ids as $request_id ) {
 230                      if ( wp_delete_post( $request_id, true ) ) {
 231                          $count ++;
 232                      }
 233                  }
 234  
 235                  add_settings_error(
 236                      'bulk_action',
 237                      'bulk_action',
 238                      /* translators: %d: Number of requests. */
 239                      sprintf( _n( 'Deleted %d request', 'Deleted %d requests', $count ), $count ),
 240                      'success'
 241                  );
 242                  break;
 243              case 'resend':
 244                  foreach ( $request_ids as $request_id ) {
 245                      $resend = _wp_privacy_resend_request( $request_id );
 246  
 247                      if ( $resend && ! is_wp_error( $resend ) ) {
 248                          $count++;
 249                      }
 250                  }
 251  
 252                  add_settings_error(
 253                      'bulk_action',
 254                      'bulk_action',
 255                      /* translators: %d: Number of requests. */
 256                      sprintf( _n( 'Re-sent %d request', 'Re-sent %d requests', $count ), $count ),
 257                      'success'
 258                  );
 259                  break;
 260          }
 261      }
 262  
 263      /**
 264       * Prepare items to output.
 265       *
 266       * @since 4.9.6
 267       * @since 5.1.0 Added support for column sorting.
 268       */
 269  	public function prepare_items() {
 270          $this->items    = array();
 271          $posts_per_page = $this->get_items_per_page( $this->request_type . '_requests_per_page' );
 272          $args           = array(
 273              'post_type'      => $this->post_type,
 274              'post_name__in'  => array( $this->request_type ),
 275              'posts_per_page' => $posts_per_page,
 276              'offset'         => isset( $_REQUEST['paged'] ) ? max( 0, absint( $_REQUEST['paged'] ) - 1 ) * $posts_per_page : 0,
 277              'post_status'    => 'any',
 278              's'              => isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : '',
 279          );
 280  
 281          $orderby_mapping = array(
 282              'requester' => 'post_title',
 283              'requested' => 'post_date',
 284          );
 285  
 286          if ( isset( $_REQUEST['orderby'] ) && isset( $orderby_mapping[ $_REQUEST['orderby'] ] ) ) {
 287              $args['orderby'] = $orderby_mapping[ $_REQUEST['orderby'] ];
 288          }
 289  
 290          if ( isset( $_REQUEST['order'] ) && in_array( strtoupper( $_REQUEST['order'] ), array( 'ASC', 'DESC' ), true ) ) {
 291              $args['order'] = strtoupper( $_REQUEST['order'] );
 292          }
 293  
 294          if ( ! empty( $_REQUEST['filter-status'] ) ) {
 295              $filter_status       = isset( $_REQUEST['filter-status'] ) ? sanitize_text_field( $_REQUEST['filter-status'] ) : '';
 296              $args['post_status'] = $filter_status;
 297          }
 298  
 299          $requests_query = new WP_Query( $args );
 300          $requests       = $requests_query->posts;
 301  
 302          foreach ( $requests as $request ) {
 303              $this->items[] = wp_get_user_request_data( $request->ID );
 304          }
 305  
 306          $this->items = array_filter( $this->items );
 307  
 308          $this->set_pagination_args(
 309              array(
 310                  'total_items' => $requests_query->found_posts,
 311                  'per_page'    => $posts_per_page,
 312              )
 313          );
 314      }
 315  
 316      /**
 317       * Checkbox column.
 318       *
 319       * @since 4.9.6
 320       *
 321       * @param WP_User_Request $item Item being shown.
 322       * @return string Checkbox column markup.
 323       */
 324  	public function column_cb( $item ) {
 325          return sprintf( '<input type="checkbox" name="request_id[]" value="%1$s" /><span class="spinner"></span>', esc_attr( $item->ID ) );
 326      }
 327  
 328      /**
 329       * Status column.
 330       *
 331       * @since 4.9.6
 332       *
 333       * @param WP_User_Request $item Item being shown.
 334       * @return string Status column markup.
 335       */
 336  	public function column_status( $item ) {
 337          $status        = get_post_status( $item->ID );
 338          $status_object = get_post_status_object( $status );
 339  
 340          if ( ! $status_object || empty( $status_object->label ) ) {
 341              return '-';
 342          }
 343  
 344          $timestamp = false;
 345  
 346          switch ( $status ) {
 347              case 'request-confirmed':
 348                  $timestamp = $item->confirmed_timestamp;
 349                  break;
 350              case 'request-completed':
 351                  $timestamp = $item->completed_timestamp;
 352                  break;
 353          }
 354  
 355          echo '<span class="status-label status-' . esc_attr( $status ) . '">';
 356          echo esc_html( $status_object->label );
 357  
 358          if ( $timestamp ) {
 359              echo ' (' . $this->get_timestamp_as_date( $timestamp ) . ')';
 360          }
 361  
 362          echo '</span>';
 363      }
 364  
 365      /**
 366       * Convert timestamp for display.
 367       *
 368       * @since 4.9.6
 369       *
 370       * @param int $timestamp Event timestamp.
 371       * @return string Human readable date.
 372       */
 373  	protected function get_timestamp_as_date( $timestamp ) {
 374          if ( empty( $timestamp ) ) {
 375              return '';
 376          }
 377  
 378          $time_diff = time() - $timestamp;
 379  
 380          if ( $time_diff >= 0 && $time_diff < DAY_IN_SECONDS ) {
 381              /* translators: %s: Human-readable time difference. */
 382              return sprintf( __( '%s ago' ), human_time_diff( $timestamp ) );
 383          }
 384  
 385          return date_i18n( get_option( 'date_format' ), $timestamp );
 386      }
 387  
 388      /**
 389       * Default column handler.
 390       *
 391       * @since 4.9.6
 392       *
 393       * @param WP_User_Request $item        Item being shown.
 394       * @param string          $column_name Name of column being shown.
 395       * @return string Default column output.
 396       */
 397  	public function column_default( $item, $column_name ) {
 398          $cell_value = $item->$column_name;
 399  
 400          if ( in_array( $column_name, array( 'created_timestamp' ), true ) ) {
 401              return $this->get_timestamp_as_date( $cell_value );
 402          }
 403  
 404          return $cell_value;
 405      }
 406  
 407      /**
 408       * Actions column. Overridden by children.
 409       *
 410       * @since 4.9.6
 411       *
 412       * @param WP_User_Request $item Item being shown.
 413       * @return string Email column markup.
 414       */
 415  	public function column_email( $item ) {
 416          return sprintf( '<a href="%1$s">%2$s</a> %3$s', esc_url( 'mailto:' . $item->email ), $item->email, $this->row_actions( array() ) );
 417      }
 418  
 419      /**
 420       * Next steps column. Overridden by children.
 421       *
 422       * @since 4.9.6
 423       *
 424       * @param WP_User_Request $item Item being shown.
 425       */
 426  	public function column_next_steps( $item ) {}
 427  
 428      /**
 429       * Generates content for a single row of the table,
 430       *
 431       * @since 4.9.6
 432       *
 433       * @param WP_User_Request $item The current item.
 434       */
 435  	public function single_row( $item ) {
 436          $status = $item->status;
 437  
 438          echo '<tr id="request-' . esc_attr( $item->ID ) . '" class="status-' . esc_attr( $status ) . '">';
 439          $this->single_row_columns( $item );
 440          echo '</tr>';
 441      }
 442  
 443      /**
 444       * Embed scripts used to perform actions. Overridden by children.
 445       *
 446       * @since 4.9.6
 447       */
 448  	public function embed_scripts() {}
 449  }


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7