[ Index ]

PHP Cross Reference of WordPress

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          global $wpdb;
 271  
 272          $this->items    = array();
 273          $posts_per_page = $this->get_items_per_page( $this->request_type . '_requests_per_page' );
 274          $args           = array(
 275              'post_type'      => $this->post_type,
 276              'post_name__in'  => array( $this->request_type ),
 277              'posts_per_page' => $posts_per_page,
 278              'offset'         => isset( $_REQUEST['paged'] ) ? max( 0, absint( $_REQUEST['paged'] ) - 1 ) * $posts_per_page : 0,
 279              'post_status'    => 'any',
 280              's'              => isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : '',
 281          );
 282  
 283          $orderby_mapping = array(
 284              'requester' => 'post_title',
 285              'requested' => 'post_date',
 286          );
 287  
 288          if ( isset( $_REQUEST['orderby'] ) && isset( $orderby_mapping[ $_REQUEST['orderby'] ] ) ) {
 289              $args['orderby'] = $orderby_mapping[ $_REQUEST['orderby'] ];
 290          }
 291  
 292          if ( isset( $_REQUEST['order'] ) && in_array( strtoupper( $_REQUEST['order'] ), array( 'ASC', 'DESC' ), true ) ) {
 293              $args['order'] = strtoupper( $_REQUEST['order'] );
 294          }
 295  
 296          if ( ! empty( $_REQUEST['filter-status'] ) ) {
 297              $filter_status       = isset( $_REQUEST['filter-status'] ) ? sanitize_text_field( $_REQUEST['filter-status'] ) : '';
 298              $args['post_status'] = $filter_status;
 299          }
 300  
 301          $requests_query = new WP_Query( $args );
 302          $requests       = $requests_query->posts;
 303  
 304          foreach ( $requests as $request ) {
 305              $this->items[] = wp_get_user_request_data( $request->ID );
 306          }
 307  
 308          $this->items = array_filter( $this->items );
 309  
 310          $this->set_pagination_args(
 311              array(
 312                  'total_items' => $requests_query->found_posts,
 313                  'per_page'    => $posts_per_page,
 314              )
 315          );
 316      }
 317  
 318      /**
 319       * Checkbox column.
 320       *
 321       * @since 4.9.6
 322       *
 323       * @param WP_User_Request $item Item being shown.
 324       * @return string Checkbox column markup.
 325       */
 326  	public function column_cb( $item ) {
 327          return sprintf( '<input type="checkbox" name="request_id[]" value="%1$s" /><span class="spinner"></span>', esc_attr( $item->ID ) );
 328      }
 329  
 330      /**
 331       * Status column.
 332       *
 333       * @since 4.9.6
 334       *
 335       * @param WP_User_Request $item Item being shown.
 336       * @return string Status column markup.
 337       */
 338  	public function column_status( $item ) {
 339          $status        = get_post_status( $item->ID );
 340          $status_object = get_post_status_object( $status );
 341  
 342          if ( ! $status_object || empty( $status_object->label ) ) {
 343              return '-';
 344          }
 345  
 346          $timestamp = false;
 347  
 348          switch ( $status ) {
 349              case 'request-confirmed':
 350                  $timestamp = $item->confirmed_timestamp;
 351                  break;
 352              case 'request-completed':
 353                  $timestamp = $item->completed_timestamp;
 354                  break;
 355          }
 356  
 357          echo '<span class="status-label status-' . esc_attr( $status ) . '">';
 358          echo esc_html( $status_object->label );
 359  
 360          if ( $timestamp ) {
 361              echo ' (' . $this->get_timestamp_as_date( $timestamp ) . ')';
 362          }
 363  
 364          echo '</span>';
 365      }
 366  
 367      /**
 368       * Convert timestamp for display.
 369       *
 370       * @since 4.9.6
 371       *
 372       * @param int $timestamp Event timestamp.
 373       * @return string Human readable date.
 374       */
 375  	protected function get_timestamp_as_date( $timestamp ) {
 376          if ( empty( $timestamp ) ) {
 377              return '';
 378          }
 379  
 380          $time_diff = time() - $timestamp;
 381  
 382          if ( $time_diff >= 0 && $time_diff < DAY_IN_SECONDS ) {
 383              /* translators: %s: Human-readable time difference. */
 384              return sprintf( __( '%s ago' ), human_time_diff( $timestamp ) );
 385          }
 386  
 387          return date_i18n( get_option( 'date_format' ), $timestamp );
 388      }
 389  
 390      /**
 391       * Default column handler.
 392       *
 393       * @since 4.9.6
 394       *
 395       * @param WP_User_Request $item        Item being shown.
 396       * @param string          $column_name Name of column being shown.
 397       * @return string Default column output.
 398       */
 399  	public function column_default( $item, $column_name ) {
 400          $cell_value = $item->$column_name;
 401  
 402          if ( in_array( $column_name, array( 'created_timestamp' ), true ) ) {
 403              return $this->get_timestamp_as_date( $cell_value );
 404          }
 405  
 406          return $cell_value;
 407      }
 408  
 409      /**
 410       * Actions column. Overridden by children.
 411       *
 412       * @since 4.9.6
 413       *
 414       * @param WP_User_Request $item Item being shown.
 415       * @return string Email column markup.
 416       */
 417  	public function column_email( $item ) {
 418          return sprintf( '<a href="%1$s">%2$s</a> %3$s', esc_url( 'mailto:' . $item->email ), $item->email, $this->row_actions( array() ) );
 419      }
 420  
 421      /**
 422       * Next steps column. Overridden by children.
 423       *
 424       * @since 4.9.6
 425       *
 426       * @param WP_User_Request $item Item being shown.
 427       */
 428  	public function column_next_steps( $item ) {}
 429  
 430      /**
 431       * Generates content for a single row of the table,
 432       *
 433       * @since 4.9.6
 434       *
 435       * @param WP_User_Request $item The current item.
 436       */
 437  	public function single_row( $item ) {
 438          $status = $item->status;
 439  
 440          echo '<tr id="request-' . esc_attr( $item->ID ) . '" class="status-' . esc_attr( $status ) . '">';
 441          $this->single_row_columns( $item );
 442          echo '</tr>';
 443      }
 444  
 445      /**
 446       * Embed scripts used to perform actions. Overridden by children.
 447       *
 448       * @since 4.9.6
 449       */
 450  	public function embed_scripts() {}
 451  }


Generated: Sun Sep 22 01:00:03 2019 Cross-referenced by PHPXref 0.7.1