wp/wp-admin/includes/class-wp-application-passwords-list-table.php
changeset 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
       
     1 <?php
       
     2 /**
       
     3  * List Table API: WP_Application_Passwords_List_Table class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Administration
       
     7  * @since 5.6.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Class for displaying the list of application password items.
       
    12  *
       
    13  * @since 5.6.0
       
    14  * @access private
       
    15  *
       
    16  * @see WP_List_Table
       
    17  */
       
    18 class WP_Application_Passwords_List_Table extends WP_List_Table {
       
    19 
       
    20 	/**
       
    21 	 * Gets the list of columns.
       
    22 	 *
       
    23 	 * @since 5.6.0
       
    24 	 *
       
    25 	 * @return array
       
    26 	 */
       
    27 	public function get_columns() {
       
    28 		return array(
       
    29 			'name'      => __( 'Name' ),
       
    30 			'created'   => __( 'Created' ),
       
    31 			'last_used' => __( 'Last Used' ),
       
    32 			'last_ip'   => __( 'Last IP' ),
       
    33 			'revoke'    => __( 'Revoke' ),
       
    34 		);
       
    35 	}
       
    36 
       
    37 	/**
       
    38 	 * Prepares the list of items for displaying.
       
    39 	 *
       
    40 	 * @since 5.6.0
       
    41 	 *
       
    42 	 * @global int $user_id User ID.
       
    43 	 */
       
    44 	public function prepare_items() {
       
    45 		global $user_id;
       
    46 		$this->items = array_reverse( WP_Application_Passwords::get_user_application_passwords( $user_id ) );
       
    47 	}
       
    48 
       
    49 	/**
       
    50 	 * Handles the name column output.
       
    51 	 *
       
    52 	 * @since 5.6.0
       
    53 	 *
       
    54 	 * @param array $item The current application password item.
       
    55 	 */
       
    56 	public function column_name( $item ) {
       
    57 		echo esc_html( $item['name'] );
       
    58 	}
       
    59 
       
    60 	/**
       
    61 	 * Handles the created column output.
       
    62 	 *
       
    63 	 * @since 5.6.0
       
    64 	 *
       
    65 	 * @param array $item The current application password item.
       
    66 	 */
       
    67 	public function column_created( $item ) {
       
    68 		if ( empty( $item['created'] ) ) {
       
    69 			echo '&mdash;';
       
    70 		} else {
       
    71 			echo date_i18n( __( 'F j, Y' ), $item['created'] );
       
    72 		}
       
    73 	}
       
    74 
       
    75 	/**
       
    76 	 * Handles the last used column output.
       
    77 	 *
       
    78 	 * @since 5.6.0
       
    79 	 *
       
    80 	 * @param array $item The current application password item.
       
    81 	 */
       
    82 	public function column_last_used( $item ) {
       
    83 		if ( empty( $item['last_used'] ) ) {
       
    84 			echo '&mdash;';
       
    85 		} else {
       
    86 			echo date_i18n( __( 'F j, Y' ), $item['last_used'] );
       
    87 		}
       
    88 	}
       
    89 
       
    90 	/**
       
    91 	 * Handles the last ip column output.
       
    92 	 *
       
    93 	 * @since 5.6.0
       
    94 	 *
       
    95 	 * @param array $item The current application password item.
       
    96 	 */
       
    97 	public function column_last_ip( $item ) {
       
    98 		if ( empty( $item['last_ip'] ) ) {
       
    99 			echo '&mdash;';
       
   100 		} else {
       
   101 			echo $item['last_ip'];
       
   102 		}
       
   103 	}
       
   104 
       
   105 	/**
       
   106 	 * Handles the revoke column output.
       
   107 	 *
       
   108 	 * @since 5.6.0
       
   109 	 *
       
   110 	 * @param array $item The current application password item.
       
   111 	 */
       
   112 	public function column_revoke( $item ) {
       
   113 		$name = 'revoke-application-password-' . $item['uuid'];
       
   114 		printf(
       
   115 			'<button type="button" name="%1$s" id="%1$s" class="button delete" aria-label="%2$s">%3$s</button>',
       
   116 			esc_attr( $name ),
       
   117 			/* translators: %s: the application password's given name. */
       
   118 			esc_attr( sprintf( __( 'Revoke "%s"' ), $item['name'] ) ),
       
   119 			__( 'Revoke' )
       
   120 		);
       
   121 	}
       
   122 
       
   123 	/**
       
   124 	 * Generates content for a single row of the table
       
   125 	 *
       
   126 	 * @since 5.6.0
       
   127 	 *
       
   128 	 * @param array  $item        The current item.
       
   129 	 * @param string $column_name The current column name.
       
   130 	 */
       
   131 	protected function column_default( $item, $column_name ) {
       
   132 		/**
       
   133 		 * Fires for each custom column in the Application Passwords list table.
       
   134 		 *
       
   135 		 * Custom columns are registered using the {@see 'manage_application-passwords-user_columns'} filter.
       
   136 		 *
       
   137 		 * @since 5.6.0
       
   138 		 *
       
   139 		 * @param string $column_name Name of the custom column.
       
   140 		 * @param array  $item        The application password item.
       
   141 		 */
       
   142 		do_action( "manage_{$this->screen->id}_custom_column", $column_name, $item );
       
   143 	}
       
   144 
       
   145 	/**
       
   146 	 * Generates custom table navigation to prevent conflicting nonces.
       
   147 	 *
       
   148 	 * @since 5.6.0
       
   149 	 *
       
   150 	 * @param string $which The location of the bulk actions: 'top' or 'bottom'.
       
   151 	 */
       
   152 	protected function display_tablenav( $which ) {
       
   153 		?>
       
   154 		<div class="tablenav <?php echo esc_attr( $which ); ?>">
       
   155 			<?php if ( 'bottom' === $which ) : ?>
       
   156 				<div class="alignright">
       
   157 					<button type="button" name="revoke-all-application-passwords" id="revoke-all-application-passwords" class="button delete"><?php _e( 'Revoke all application passwords' ); ?></button>
       
   158 				</div>
       
   159 			<?php endif; ?>
       
   160 			<div class="alignleft actions bulkactions">
       
   161 				<?php $this->bulk_actions( $which ); ?>
       
   162 			</div>
       
   163 			<?php
       
   164 			$this->extra_tablenav( $which );
       
   165 			$this->pagination( $which );
       
   166 			?>
       
   167 			<br class="clear" />
       
   168 		</div>
       
   169 		<?php
       
   170 	}
       
   171 
       
   172 	/**
       
   173 	 * Generates content for a single row of the table.
       
   174 	 *
       
   175 	 * @since 5.6.0
       
   176 	 *
       
   177 	 * @param array $item The current item.
       
   178 	 */
       
   179 	public function single_row( $item ) {
       
   180 		echo '<tr data-uuid="' . esc_attr( $item['uuid'] ) . '">';
       
   181 		$this->single_row_columns( $item );
       
   182 		echo '</tr>';
       
   183 	}
       
   184 
       
   185 	/**
       
   186 	 * Gets the name of the default primary column.
       
   187 	 *
       
   188 	 * @since 5.6.0
       
   189 	 *
       
   190 	 * @return string Name of the default primary column, in this case, 'name'.
       
   191 	 */
       
   192 	protected function get_default_primary_column_name() {
       
   193 		return 'name';
       
   194 	}
       
   195 
       
   196 	/**
       
   197 	 * Prints the JavaScript template for the new row item.
       
   198 	 *
       
   199 	 * @since 5.6.0
       
   200 	 */
       
   201 	public function print_js_template_row() {
       
   202 		list( $columns, $hidden, , $primary ) = $this->get_column_info();
       
   203 
       
   204 		echo '<tr data-uuid="{{ data.uuid }}">';
       
   205 
       
   206 		foreach ( $columns as $column_name => $display_name ) {
       
   207 			$is_primary = $primary === $column_name;
       
   208 			$classes    = "{$column_name} column-{$column_name}";
       
   209 
       
   210 			if ( $is_primary ) {
       
   211 				$classes .= ' has-row-actions column-primary';
       
   212 			}
       
   213 
       
   214 			if ( in_array( $column_name, $hidden, true ) ) {
       
   215 				$classes .= ' hidden';
       
   216 			}
       
   217 
       
   218 			printf( '<td class="%s" data-colname="%s">', esc_attr( $classes ), esc_attr( wp_strip_all_tags( $display_name ) ) );
       
   219 
       
   220 			switch ( $column_name ) {
       
   221 				case 'name':
       
   222 					echo '{{ data.name }}';
       
   223 					break;
       
   224 				case 'created':
       
   225 					// JSON encoding automatically doubles backslashes to ensure they don't get lost when printing the inline JS.
       
   226 					echo '<# print( wp.date.dateI18n( ' . wp_json_encode( __( 'F j, Y' ) ) . ', data.created ) ) #>';
       
   227 					break;
       
   228 				case 'last_used':
       
   229 					echo '<# print( data.last_used !== null ? wp.date.dateI18n( ' . wp_json_encode( __( 'F j, Y' ) ) . ", data.last_used ) : '—' ) #>";
       
   230 					break;
       
   231 				case 'last_ip':
       
   232 					echo "{{ data.last_ip || '—' }}";
       
   233 					break;
       
   234 				case 'revoke':
       
   235 					printf(
       
   236 						'<button type="button" class="button delete" aria-label="%1$s">%2$s</button>',
       
   237 						/* translators: %s: the application password's given name. */
       
   238 						esc_attr( sprintf( __( 'Revoke "%s"' ), '{{ data.name }}' ) ),
       
   239 						esc_html__( 'Revoke' )
       
   240 					);
       
   241 					break;
       
   242 				default:
       
   243 					/**
       
   244 					 * Fires in the JavaScript row template for each custom column in the Application Passwords list table.
       
   245 					 *
       
   246 					 * Custom columns are registered using the {@see 'manage_application-passwords-user_columns'} filter.
       
   247 					 *
       
   248 					 * @since 5.6.0
       
   249 					 *
       
   250 					 * @param string $column_name Name of the custom column.
       
   251 					 */
       
   252 					do_action( "manage_{$this->screen->id}_custom_column_js_template", $column_name );
       
   253 					break;
       
   254 			}
       
   255 
       
   256 			if ( $is_primary ) {
       
   257 				echo '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>';
       
   258 			}
       
   259 
       
   260 			echo '</td>';
       
   261 		}
       
   262 
       
   263 		echo '</tr>';
       
   264 	}
       
   265 }