Skip to content

Commit

Permalink
feat(subscriptions): sortable columns
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed Mar 8, 2024
1 parent 67569b5 commit b4b4f90
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
65 changes: 62 additions & 3 deletions includes/hub/admin/class-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
* Class to handle the Subscriptions admin page by customizing the Custom Post type screen
*/
class Subscriptions extends Woo {
/**
* Runs the initialization.
*/
public static function init() {
parent::init();
add_filter( 'manage_edit-' . \Newspack_Network\Hub\Database\Subscriptions::POST_TYPE_SLUG . '_sortable_columns', [ __CLASS__, 'subscription_sortable_columns' ] );
add_filter( 'request', [ __CLASS__, 'request_query' ] );
}

/**
* Modify columns on post type table
Expand Down Expand Up @@ -47,7 +55,6 @@ public static function posts_columns( $columns ) {
* @return void
*/
public static function posts_columns_values( $column, $post_id ) {

$store_class_name = str_replace( 'Admin', 'Stores', get_called_class() );
$item = $store_class_name::get_item( $post_id );

Expand Down Expand Up @@ -98,12 +105,12 @@ public static function posts_columns_values( $column, $post_id ) {
$line_item['name'] ?? ''
);
}
echo $output; // phpcs:ignore
echo $output; // phpcs:ignore
break;
case 'orders':
$link = sprintf(
'%s/wp-admin/edit.php?post_status=all&post_type=shop_order&_subscription_related_orders=%d',
$item->get_node_url(),
$item->get_node_url(),
$item->get_remote_id()
);
printf( '<a href="%s" target="blank">%s</a>', $link, $item->get_payment_count() ); // phpcs:ignore
Expand All @@ -113,4 +120,56 @@ public static function posts_columns_values( $column, $post_id ) {
break;
}
}

/**
* Add sortable columns.
*
* @param array $columns Columns.
* @return array
*/
public static function subscription_sortable_columns( $columns ) {
$sortable_columns = [
'start_date' => 'start_date',
'trial_end_date' => 'trial_end_date',
'next_payment_date' => 'next_payment_date',
'last_payment_date' => 'last_payment_date',
'end_date' => 'end_date',
];

return wp_parse_args( $sortable_columns, $columns );
}

/**
* Sorts the request for subscriptions stored in WP Post tables.
*
* @param array $vars Query variables.
*
* @return array
*/
public static function request_query( $vars ) {
global $typenow;

if ( \Newspack_Network\Hub\Database\Subscriptions::POST_TYPE_SLUG === $typenow ) {
if ( isset( $vars['orderby'] ) ) {
switch ( $vars['orderby'] ) {
case 'start_date':
case 'trial_end_date':
case 'next_payment_date':
case 'last_payment_date':
case 'end_date':
$vars = array_merge(
$vars,
[
'meta_key' => $vars['orderby'],
'meta_type' => 'DATETIME',
'orderby' => 'meta_value',
]
);
break;
}
}
}

return $vars;
}
}
5 changes: 2 additions & 3 deletions includes/hub/admin/class-woo.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function init() {
$class_name = get_called_class();
$db_class_name = str_replace( 'Admin', 'Database', $class_name );
self::$post_types[] = $db_class_name::POST_TYPE_SLUG;

// Removes the Bulk actions dropdown.
add_filter( 'bulk_actions-edit-' . $db_class_name::POST_TYPE_SLUG, '__return_empty_array' );

Expand Down Expand Up @@ -158,7 +158,7 @@ public static function pre_get_posts( $query ) {
if ( 'edit.php' !== $pagenow || ! in_array( $post_type, self::$post_types, true ) || ! is_admin() || ! $query->is_main_query() ) {
return null;
}

if ( ! isset( $_GET['node_id'] ) || ! is_numeric( $_GET['node_id'] ) ) { // zero is a valid value.
return null;
}
Expand All @@ -178,7 +178,6 @@ public static function pre_get_posts( $query ) {
*/
public static function parse_query( $query ) {
global $pagenow;

if ( ! is_admin() || 'edit.php' !== $pagenow || empty( $query->query_vars['s'] ) || ! in_array( $query->query_vars['post_type'], self::$post_types, true ) ) {
return;
}
Expand Down

0 comments on commit b4b4f90

Please sign in to comment.