Bez popisu

class-wp-application-passwords-list-table.php 6.7KB

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