Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter to control sorting in View entries query #2126

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 48 additions & 21 deletions future/includes/class-gv-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use GravityKitFoundation;
use GravityView_Compatibility;
use GravityView_Cache;
use GravityView_frontend;
use GVCommon;

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

$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 ) ) );

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

$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 ) ) );

return \GVCommon::generate_notice( '<h3>' . $title . '</h3>' . wpautop( $message . $image ), 'notice' );
return GVCommon::generate_notice( '<h3>' . $title . '</h3>' . wpautop( $message . $image ), 'notice' );
}
break;
case 'in_trash':
Expand All @@ -357,7 +359,7 @@ public static function content( $content ) {
return $content;
}

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

/**
* Editing a single entry.
Expand Down Expand Up @@ -414,7 +416,7 @@ public static function content( $content ) {
}
}

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

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

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

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

if ( ! is_array( $parameters ) ) {
$parameters = array();
Expand Down Expand Up @@ -1102,25 +1104,50 @@ public function get_entries( $request = null ) {
$sort_directions = $view_setting_sort_directions;
}

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

if ( empty( $sort_field_id ) ) {
continue;
foreach ( $sort_field_ids as $key => $id ) {
// The original field ID can be overridden for certain fields, including the Name field
// where a single ID becomes multiple field input IDs joined by a pipe (e.g., "3.3|3.6").
$id_overrides = explode(
'|',
GravityView_frontend::_override_sorting_id_by_field_type( $id, $this->form->ID )
);

foreach ( $id_overrides as $id_override ) {
$sorting_parameters[] = [
'_original_id' => $id,
'id' => $id_override,
'is_numeric' => GVCommon::is_field_numeric( $this->form->ID, $id ),
'direction' => strtoupper( \GV\Utils::get( $sort_directions, $key, 'ASC' ) ),
];
}
}

$sort_field_id = explode( '|', $sort_field_id );
/**
* Modifies the sorting parameters applied during the retrieval of View entries.
*
* @filter `gk/gravityview/view/entries/query/sorting-parameters`
*
* @since TBD
*
* @param array $sorting_parameters The array of sorting parameters, including field ID, field type (direction, and casting types.
* @param View $this The View instance.
*/
$sorting_parameters = apply_filters( 'gk/gravityview/view/entries/query/sorting-parameters', $sorting_parameters, $this );

foreach ( $sort_field_id as $id ) {
$order = new \GF_Query_Column( $id, $this->form->ID );
foreach ( $sorting_parameters as $field ) {
if ( empty( $field['id'] ) ) {
continue;
}

if ( 'id' !== $id && \GVCommon::is_field_numeric( $this->form->ID, $id ) ) {
$order = \GF_Query_Call::CAST( $order, defined( 'GF_Query::TYPE_DECIMAL' ) ? \GF_Query::TYPE_DECIMAL : \GF_Query::TYPE_SIGNED );
}
$order = new \GF_Query_Column( $field['id'], $this->form->ID );

$query->order( $order, $sort_direction );
if ( 'id' !== $field['id'] && (int) $field['is_numeric'] ) {
$order = \GF_Query_Call::CAST( $order, defined( 'GF_Query::TYPE_DECIMAL' ) ? \GF_Query::TYPE_DECIMAL : \GF_Query::TYPE_SIGNED );
}

$query->order( $order, $field['direction'] );
}
}

Expand Down Expand Up @@ -1816,7 +1843,7 @@ protected function apply_legacy_join_is_approved_query_conditions( \GF_Query $qu
return;
}

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

if ( $is_admin_and_can_view ) {
return;
Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h
* Fixed: The maximum number of files allowed in the File Upload field was not respected when editing an entry.
* Fixed: Sorting the View by the Name field would yield incorrect results.

#### 💻 Developer Updates
* Added `gk/gravityview/view/entries/query/sorting-parameters` filter to modify the sorting parameters applied during the retrieval of View entries.

= 2.27.1 on August 14, 2024 =

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