PluginProbe
WooCommerce / 11.1.0-beta.2
WooCommerce v11.1.0-beta.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Internal / ProductDownloads / ApprovedDirectories / Admin / Table.php
Table.php
331 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Admin;
4
5 use Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Register;
6 use Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\StoredUrl;
7 use WP_List_Table;
8 use WP_Screen;
9
10 /**
11 * Admin list table used to render our current list of approved directories.
12 */
13 class Table extends WP_List_Table {
14 /**
15 * Initialize the webhook table list.
16 */
17 public function __construct() {
18 parent::__construct(
19 array(
20 'singular' => 'url',
21 'plural' => 'urls',
22 'ajax' => false,
23 )
24 );
25
26 add_filter( 'manage_woocommerce_page_wc-settings_columns', array( $this, 'get_columns' ) );
27 $this->items_per_page();
28 set_screen_options();
29 }
30
31 /**
32 * Sets up an items-per-page control.
33 */
34 private function items_per_page() {
35 add_screen_option(
36 'per_page',
37 array(
38 'default' => 20,
39 'option' => 'edit_approved_directories_per_page',
40 )
41 );
42
43 add_filter( 'set_screen_option_edit_approved_directories_per_page', array( $this, 'set_items_per_page' ), 10, 3 );
44 }
45
46 /**
47 * Saves the items-per-page setting.
48 *
49 * @param mixed $default The default value.
50 * @param string $option The option being configured.
51 * @param int $value The submitted option value.
52 *
53 * @return mixed
54 */
55 public function set_items_per_page( $default, string $option, int $value ) {
56 return 'edit_approved_directories_per_page' === $option ? absint( $value ) : $default;
57 }
58
59 /**
60 * No items found text.
61 */
62 public function no_items() {
63 esc_html_e( 'No approved directory URLs found.', 'woocommerce' );
64 }
65
66 /**
67 * Displays the list of views available on this table.
68 */
69 public function render_views() {
70 $register = wc_get_container()->get( Register::class );
71
72 $enabled_count = $register->count( true );
73 $disabled_count = $register->count( false );
74 $all_count = $enabled_count + $disabled_count;
75 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
76 $selected_view = isset( $_REQUEST['view'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['view'] ) ) : 'all';
77
78 $all_url = esc_url( add_query_arg( 'view', 'all', $this->get_base_url() ) );
79 $all_class = 'all' === $selected_view ? 'class="current"' : '';
80 $all_text = sprintf(
81 /* translators: %s is the count of approved directory list entries. */
82 _nx(
83 'All <span class="count">(%s)</span>',
84 'All <span class="count">(%s)</span>',
85 $all_count,
86 'Approved product download directory views',
87 'woocommerce'
88 ),
89 $all_count
90 );
91
92 $enabled_url = esc_url( add_query_arg( 'view', 'enabled', $this->get_base_url() ) );
93 $enabled_class = 'enabled' === $selected_view ? 'class="current"' : '';
94 $enabled_text = sprintf(
95 /* translators: %s is the count of enabled approved directory list entries. */
96 _nx(
97 'Enabled <span class="count">(%s)</span>',
98 'Enabled <span class="count">(%s)</span>',
99 $enabled_count,
100 'Approved product download directory views',
101 'woocommerce'
102 ),
103 $enabled_count
104 );
105
106 $disabled_url = esc_url( add_query_arg( 'view', 'disabled', $this->get_base_url() ) );
107 $disabled_class = 'disabled' === $selected_view ? 'class="current"' : '';
108 $disabled_text = sprintf(
109 /* translators: %s is the count of disabled directory list entries. */
110 _nx(
111 'Disabled <span class="count">(%s)</span>',
112 'Disabled <span class="count">(%s)</span>',
113 $disabled_count,
114 'Approved product download directory views',
115 'woocommerce'
116 ),
117 $disabled_count
118 );
119
120 $views = array(
121 'all' => "<a href='{$all_url}' {$all_class}>{$all_text}</a>",
122 'enabled' => "<a href='{$enabled_url}' {$enabled_class}>{$enabled_text}</a>",
123 'disabled' => "<a href='{$disabled_url}' {$disabled_class}>{$disabled_text}</a>",
124 );
125
126 $this->screen->render_screen_reader_content( 'heading_views' );
127
128 echo '<ul class="subsubsub list-table-filters">';
129 foreach ( $views as $slug => $view ) {
130 $views[ $slug ] = "<li class='{$slug}'>{$view}";
131 }
132 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
133 echo implode( ' | </li>', $views ) . "</li>\n";
134 echo '</ul>';
135 }
136
137 /**
138 * Get list columns.
139 *
140 * @return array
141 */
142 public function get_columns() {
143 return array(
144 'cb' => '<input type="checkbox" />',
145 'title' => _x( 'URL', 'Approved product download directories', 'woocommerce' ),
146 'enabled' => _x( 'Enabled', 'Approved product download directories', 'woocommerce' ),
147 );
148 }
149
150 /**
151 * Checklist column, used for selecting items for processing by a bulk action.
152 *
153 * @param StoredUrl $item The approved directory information for the current row.
154 *
155 * @return string
156 */
157 public function column_cb( $item ) {
158 return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', esc_attr( $this->_args['singular'] ), esc_attr( $item->get_id() ) );
159 }
160
161 /**
162 * URL column.
163 *
164 * @param StoredUrl $item The approved directory information for the current row.
165 *
166 * @return string
167 */
168 public function column_title( $item ) {
169 $id = (int) $item->get_id();
170 $url = esc_html( $item->get_url() );
171 $enabled = $item->is_enabled();
172
173 $edit_url = esc_url( $this->get_action_url( 'edit', $id ) );
174 $enable_disable_url = esc_url( $enabled ? $this->get_action_url( 'disable', $id ) : $this->get_action_url( 'enable', $id ) );
175 $enable_disable_text = esc_html( $enabled ? __( 'Disable', 'woocommerce' ) : __( 'Enable', 'woocommerce' ) );
176 $delete_url = esc_url( $this->get_action_url( 'delete', $id ) );
177 $edit_link = "<a href='{$edit_url}'>" . esc_html_x( 'Edit', 'Product downloads list', 'woocommerce' ) . '</a>';
178 $enable_disable_link = "<a href='{$enable_disable_url}'>{$enable_disable_text}</a>";
179 $delete_link = "<a href='{$delete_url}' class='submitdelete wc-confirm-delete'>" . esc_html_x( 'Delete permanently', 'Product downloads list', 'woocommerce' ) . '</a>';
180 $url_link = "<a href='{$edit_url}'>{$url}</a>";
181
182 return "
183 <strong>{$url_link}</strong>
184 <div class='row-actions'>
185 <span class='id'>ID: {$id}</span> |
186 <span class='edit'>{$edit_link}</span> |
187 <span class='enable-disable'>{$enable_disable_link}</span> |
188 <span class='delete'><a class='submitdelete'>{$delete_link}</a></span>
189 </div>
190 ";
191 }
192
193 /**
194 * Rule-is-enabled column.
195 *
196 * @param StoredUrl $item The approved directory information for the current row.
197 *
198 * @return string
199 */
200 public function column_enabled( StoredUrl $item ): string {
201 return $item->is_enabled()
202 ? '<mark class="yes" title="' . esc_html__( 'Enabled', 'woocommerce' ) . '"><span class="dashicons dashicons-yes"></span></mark>'
203 : '<mark class="no" title="' . esc_html__( 'Disabled', 'woocommerce' ) . '">&ndash;</mark>';
204 }
205
206 /**
207 * Get bulk actions.
208 *
209 * @return array
210 */
211 protected function get_bulk_actions() {
212 return array(
213 'enable' => __( 'Enable rule', 'woocommerce' ),
214 'disable' => __( 'Disable rule', 'woocommerce' ),
215 'delete' => __( 'Delete permanently', 'woocommerce' ),
216 );
217 }
218
219 /**
220 * Builds an action URL (ie, to edit or delete a row).
221 *
222 * @param string $action The action to be created.
223 * @param int $id The ID that is the subject of the action.
224 * @param string $nonce_action Action used to add a nonce to the URL.
225 *
226 * @return string
227 */
228 public function get_action_url( string $action, int $id, string $nonce_action = 'modify_approved_directories' ): string {
229 return add_query_arg(
230 array(
231 'check' => wp_create_nonce( $nonce_action ),
232 'action' => $action,
233 'url' => $id,
234 ),
235 $this->get_base_url()
236 );
237 }
238
239 /**
240 * Supplies the 'base' admin URL for this admin table.
241 *
242 * @return string
243 */
244 public function get_base_url(): string {
245 return add_query_arg(
246 array(
247 'page' => 'wc-settings',
248 'tab' => 'products',
249 'section' => 'download_urls',
250 ),
251 admin_url( 'admin.php' )
252 );
253 }
254
255 /**
256 * Generate the table navigation above or below the table.
257 * Included to remove extra nonce input.
258 *
259 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
260 */
261 protected function display_tablenav( $which ) {
262 $directories = wc_get_container()->get( Register::class );
263 echo '<div class="tablenav ' . esc_attr( $which ) . '">';
264
265 if ( $this->has_items() ) {
266 echo '<div class="alignleft actions bulkactions">';
267 $this->bulk_actions( $which );
268
269 if ( $directories->count( false ) > 0 ) {
270 echo '<a href="' . esc_url( $this->get_action_url( 'enable-all', 0 ) ) . '" class="wp-core-ui button">' . esc_html_x( 'Enable All', 'Approved product download directories', 'woocommerce' ) . '</a> ';
271 }
272
273 if ( $directories->count( true ) > 0 ) {
274 echo '<a href="' . esc_url( $this->get_action_url( 'disable-all', 0 ) ) . '" class="wp-core-ui button">' . esc_html_x( 'Disable All', 'Approved product download directories', 'woocommerce' ) . '</a>';
275 }
276
277 echo '</div>';
278 }
279
280 $this->pagination( $which );
281 echo '<br class="clear" />';
282 echo '</div>';
283 }
284
285 /**
286 * Prepare table list items.
287 */
288 public function prepare_items() {
289 // phpcs:disable WordPress.Security.NonceVerification.Recommended
290 // phpcs:disable WordPress.Security.NonceVerification.Missing
291 $current_page = $this->get_pagenum();
292 $per_page = $this->get_items_per_page( 'edit_approved_directories_per_page' );
293 $search = sanitize_text_field( wp_unslash( $_REQUEST['s'] ?? '' ) );
294
295 switch ( $_REQUEST['view'] ?? '' ) {
296 case 'enabled':
297 $enabled = true;
298 break;
299
300 case 'disabled':
301 $enabled = false;
302 break;
303
304 default:
305 $enabled = null;
306 break;
307 }
308 // phpcs:enable
309
310 $approved_directories = wc_get_container()->get( Register::class )->list(
311 array(
312 'page' => $current_page,
313 'per_page' => $per_page,
314 'search' => $search,
315 'enabled' => $enabled,
316 )
317 );
318
319 $this->items = $approved_directories['approved_directories'];
320
321 // Set the pagination.
322 $this->set_pagination_args(
323 array(
324 'total_items' => $approved_directories['total_urls'],
325 'total_pages' => $approved_directories['total_pages'],
326 'per_page' => $per_page,
327 )
328 );
329 }
330 }
331