Skip to content

Commit 2d3f7d7

Browse files
committed
Add filter to control sorting in View entries query (#1759)
This allows for more granular control of sorting parameters for each when fetching View entries.
1 parent 226aebf commit 2d3f7d7

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

future/includes/class-gv-view.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use GravityKitFoundation;
88
use GravityView_Compatibility;
99
use GravityView_Cache;
10+
use GravityView_frontend;
11+
use GVCommon;
1012

1113
/** If this file is called directly, abort. */
1214
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
@@ -327,15 +329,15 @@ public static function content( $content ) {
327329
* This View has no data source. There's nothing to show really.
328330
* ...apart from a nice message if the user can do anything about it.
329331
*/
330-
if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) {
332+
if ( GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) {
331333

332334
$title = sprintf( __( 'This View is not configured properly. Start by <a href="%s">selecting a form</a>.', 'gk-gravityview' ), esc_url( get_edit_post_link( $view->ID, false ) ) );
333335

334336
$message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gk-gravityview' );
335337

336338
$image = sprintf( '<img alt="%s" src="%s" style="margin-top: 10px;" />', esc_attr__( 'Data Source', 'gk-gravityview' ), esc_url( plugins_url( 'assets/images/screenshots/data-source.png', GRAVITYVIEW_FILE ) ) );
337339

338-
return \GVCommon::generate_notice( '<h3>' . $title . '</h3>' . wpautop( $message . $image ), 'notice' );
340+
return GVCommon::generate_notice( '<h3>' . $title . '</h3>' . wpautop( $message . $image ), 'notice' );
339341
}
340342
break;
341343
case 'in_trash':
@@ -357,7 +359,7 @@ public static function content( $content ) {
357359
return $content;
358360
}
359361

360-
$is_admin_and_can_view = $view->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap( 'gravityview_moderate_entries', $view->ID );
362+
$is_admin_and_can_view = $view->settings->get( 'admin_show_all_statuses' ) && GVCommon::has_cap( 'gravityview_moderate_entries', $view->ID );
361363

362364
/**
363365
* Editing a single entry.
@@ -414,7 +416,7 @@ public static function content( $content ) {
414416
}
415417
}
416418

417-
$error = \GVCommon::check_entry_display( $e->as_entry(), $view );
419+
$error = GVCommon::check_entry_display( $e->as_entry(), $view );
418420

419421
if ( is_wp_error( $error ) ) {
420422
gravityview()->log->error(
@@ -535,7 +537,7 @@ public function can_render( $context = null, $request = null ) {
535537
* Is this View an embed-only View? If so, don't allow rendering here,
536538
* as this is a direct request.
537539
*/
538-
if ( $this->settings->get( 'embed_only' ) && ! \GVCommon::has_cap( 'read_private_gravityviews' ) ) {
540+
if ( $this->settings->get( 'embed_only' ) && ! GVCommon::has_cap( 'read_private_gravityviews' ) ) {
539541
return new \WP_Error( 'gravityview/embed_only' );
540542
}
541543
}
@@ -546,7 +548,7 @@ public function can_render( $context = null, $request = null ) {
546548

547549
/** Private, pending, draft, etc. */
548550
$public_states = get_post_stati( array( 'public' => true ) );
549-
if ( ! in_array( $this->post_status, $public_states, true ) && ! \GVCommon::has_cap( 'read_gravityview', $this->ID ) ) {
551+
if ( ! in_array( $this->post_status, $public_states, true ) && ! GVCommon::has_cap( 'read_gravityview', $this->ID ) ) {
550552
gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $this->ID ) );
551553
return new \WP_Error( 'gravityview/not_public' );
552554
}
@@ -1018,10 +1020,10 @@ public function get_entries( $request = null ) {
10181020
/**
10191021
* @todo: Stop using _frontend and use something like $request->get_search_criteria() instead
10201022
*/
1021-
$parameters = \GravityView_frontend::get_view_entries_parameters( $parameters, $this->form->ID );
1023+
$parameters = GravityView_frontend::get_view_entries_parameters( $parameters, $this->form->ID );
10221024

10231025
$parameters['context_view_id'] = $this->ID;
1024-
$parameters = \GVCommon::calculate_get_entries_criteria( $parameters, $this->form->ID );
1026+
$parameters = GVCommon::calculate_get_entries_criteria( $parameters, $this->form->ID );
10251027

10261028
if ( ! is_array( $parameters ) ) {
10271029
$parameters = array();
@@ -1102,24 +1104,43 @@ public function get_entries( $request = null ) {
11021104
$sort_directions = $view_setting_sort_directions;
11031105
}
11041106

1105-
foreach ( (array) $sort_field_ids as $key => $sort_field_id ) {
1106-
$sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type( $sort_field_id, $this->form->ID );
1107-
$sort_direction = strtoupper( \GV\Utils::get( $sort_directions, $key, 'ASC' ) );
1107+
$sorting_parameters = [];
11081108

1109-
if ( empty( $sort_field_id ) ) {
1109+
foreach ( $sort_field_ids as $key => $id ) {
1110+
$sorting_parameters[ $id ] = [
1111+
'id' => GravityView_frontend::_override_sorting_id_by_field_type( $id, $this->form->ID ),
1112+
'type' => GVCommon::is_field_numeric( $this->form->ID, $id ) ? 'numeric' : 'string',
1113+
'direction' => strtoupper( \GV\Utils::get( $sort_directions, $key, 'ASC' ) ),
1114+
];
1115+
}
1116+
1117+
/**
1118+
* Modifies the sorting parameters applied during the retrieval of View entries.
1119+
*
1120+
* @filter `gk/gravityview/view/entries/query/sorting-parameters`
1121+
*
1122+
* @since TBD
1123+
*
1124+
* @param array $sorting_parameters The array of sorting parameters, including field IDs, directions, and casting types.
1125+
* @param View $this The View instance.
1126+
*/
1127+
$sorting_parameters = apply_filters( 'gk/gravityview/view/entries/query/sorting-parameters', $sorting_parameters, $this );
1128+
1129+
foreach ( $sorting_parameters as $field ) {
1130+
if ( empty( $field['id'] ) ) {
11101131
continue;
11111132
}
11121133

1113-
$sort_field_id = explode( '|', $sort_field_id );
1134+
$sort_field_ids = explode( '|', $field['id'] );
11141135

1115-
foreach ( $sort_field_id as $id ) {
1116-
$order = new \GF_Query_Column( $id, $this->form->ID );
1136+
foreach ( $sort_field_ids as $field_id ) {
1137+
$order = new \GF_Query_Column( $field_id, $this->form->ID );
11171138

1118-
if ( 'id' !== $id && \GVCommon::is_field_numeric( $this->form->ID, $id ) ) {
1139+
if ( 'id' !== $field_id && 'numeric' === $field['type'] ) {
11191140
$order = \GF_Query_Call::CAST( $order, defined( 'GF_Query::TYPE_DECIMAL' ) ? \GF_Query::TYPE_DECIMAL : \GF_Query::TYPE_SIGNED );
11201141
}
11211142

1122-
$query->order( $order, $sort_direction );
1143+
$query->order( $order, $field['direction'] );
11231144
}
11241145
}
11251146
}
@@ -1816,7 +1837,7 @@ protected function apply_legacy_join_is_approved_query_conditions( \GF_Query $qu
18161837
return;
18171838
}
18181839

1819-
$is_admin_and_can_view = $this->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap( 'gravityview_moderate_entries', $this->ID );
1840+
$is_admin_and_can_view = $this->settings->get( 'admin_show_all_statuses' ) && GVCommon::has_cap( 'gravityview_moderate_entries', $this->ID );
18201841

18211842
if ( $is_admin_and_can_view ) {
18221843
return;

readme.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h
3232
* Fixed: The maximum number of files allowed in the File Upload field was not respected when editing an entry.
3333
* Fixed: Sorting the View by the Name field would yield incorrect results.
3434

35+
#### 💻 Developer Updates
36+
* Added `gk/gravityview/view/entries/query/sorting-parameters` filter to modify the sorting parameters applied during the retrieval of View entries.
37+
3538
= 2.27.1 on August 14, 2024 =
3639

3740
This release fixes an issue with adding fields in the View editor's Edit Entry layout when the Multiple Forms extension is enabled.

0 commit comments

Comments
 (0)