Skip to content

Commit

Permalink
Release 2.20 (#1995)
Browse files Browse the repository at this point in the history
This release introduces new settings for better control over View
caching, adds support for the Advanced Post Creation Add-On when editing
entries, fixes a fatal error when exporting entries to CSV, and updates
internal components for better performance and compatibility.

#### 🚀 Added
- Global and View-specific settings to control caching of View entries.
- Support for the [Advanced Post Creation
Add-On](https://www.gravityforms.com/add-ons/advanced-post-creation/)
when editing entries in GravityView's Edit Entry mode.

#### ✨ Improved
- If Gravity Forms is not installed and/or activated, a notice is
displayed to alert users when creating new or listing existing Views.

#### 🐛 Fixed
- Deprecation notice in PHP 8.1+ when displaying a View with file upload
fields.
- Fatal error when exporting entries to CSV.

#### 🔧 Updated
* [Foundation](https://www.gravitykit.com/foundation/) to versions
1.2.9.
- GravityKit products that are already installed can now be activated
without a valid license.
- Fixed PHP warning messages that appeared when deactivating the last
active product with Foundation installed.
  • Loading branch information
zackkatz authored Feb 23, 2024
2 parents 81f4ef6 + 64ec085 commit 2548055
Show file tree
Hide file tree
Showing 29 changed files with 11,889 additions and 7,586 deletions.
2 changes: 1 addition & 1 deletion assets/js/admin-views.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@

var show_warning = ! dismissed_warning && value.configured === 0;

$( '#' + index + '-fields' ).find( '.notice-warning' ).toggle( show_warning );
$( '#' + index + '-fields' ).find( '.notice-no-link' ).toggle( show_warning );
$( 'li[aria-controls="' + index + '-view"]' )
.toggleClass( 'tab-not-configured', show_warning )
.find( '.tab-icon' )
Expand Down
2 changes: 1 addition & 1 deletion assets/js/admin-views.min.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions future/includes/class-gv-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ private function init() {
/** More legacy core. @todo Deprecate */
$this->plugin->include_legacy_core();

/** Register the gravityview post type upon WordPress core init. */
require_once $this->plugin->dir( 'future/includes/class-gv-view.php' );
add_action( 'init', array( '\GV\View', 'register_post_type' ) );
add_action( 'init', array( '\GV\View', 'add_rewrite_endpoint' ) );
add_filter( 'map_meta_cap', array( '\GV\View', 'restrict' ), 11, 4 );
add_action( 'template_redirect', array( '\GV\View', 'template_redirect' ) );
add_action( 'the_content', array( '\GV\View', 'content' ) );
/**
* Stop all further functionality from loading if the WordPress
* plugin is incompatible with the current environment.
Expand All @@ -141,16 +148,10 @@ private function init() {
*/
if ( ! $this->plugin->is_compatible() ) {
$this->log->error( 'GravityView 2.0 is not compatible with this environment. Stopped loading.' );

return;
}

/** Register the gravityview post type upon WordPress core init. */
require_once $this->plugin->dir( 'future/includes/class-gv-view.php' );
add_action( 'init', array( '\GV\View', 'register_post_type' ) );
add_action( 'init', array( '\GV\View', 'add_rewrite_endpoint' ) );
add_filter( 'map_meta_cap', array( '\GV\View', 'restrict' ), 11, 4 );
add_action( 'template_redirect', array( '\GV\View', 'template_redirect' ) );
add_action( 'the_content', array( '\GV\View', 'content' ) );

/** Add rewrite endpoint for single-entry URLs. */
require_once $this->plugin->dir( 'future/includes/class-gv-entry.php' );
Expand Down
95 changes: 93 additions & 2 deletions future/includes/class-gv-settings-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ public function get_gravitykit_setting( $setting, $default = null ) {
*/
public function defaults() {
$defaults = array(
'rest_api' => 0,
'public_entry_moderation' => 0,
'rest_api' => 0,
'public_entry_moderation' => 0,
'caching' => 1,
'caching_entries' => DAY_IN_SECONDS,
);

/**
Expand Down Expand Up @@ -228,6 +230,91 @@ public function add_settings( $plugins_data ) {

$default_settings = $this->defaults();

$cache_filters_in_use = [];

if ( has_filter( 'gravityview_use_cache' ) ) {
$cache_filters_in_use[] = 'gravityview_use_cache';
}

if ( has_filter( 'gravityview_cache_time_entries' ) ) {
$cache_filters_in_use[] = 'gravityview_cache_time_entries';
}

$cache_settings = [];

if ( ! empty( $cache_filters_in_use ) ) {
$notice = 1 === count( $cache_filters_in_use )
? esc_html_x( 'The [filter] active filter could be overriding cache settings.', 'Placeholders inside [] are not to be translated.', 'gk-gravityview' )
: esc_html_x( 'The following active filters could be overriding cache settings: [filters].', 'Placeholders inside [] are not to be translated.', 'gk-gravityview' );

$notice = strtr(
$notice,
[
'[filter]' => '<code>' . implode( '</code>, <code>', $cache_filters_in_use ) . '</code>',
'[filters]' => '<code>' . implode( '</code>, <code>', $cache_filters_in_use ) . '</code>',
]
);

$notice = <<<HTML
<div class="bg-yellow-50 p-4 rounded-md">
<div class="flex">
<div class="flex-shrink-0">
<svg class="h-5 w-5 text-yellow-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd" />
</svg>
</div>
<div class="ml-3">
<p class="text-sm">
{$notice}
</p>
</div>
</div>
</div>
HTML;

$cache_settings[] = [
'id' => 'caching_filters_notice',
'html' => $notice,
'markdown' => false,
'excludeFromSave' => true,
];
}

$cache_settings = array_merge( $cache_settings, [
array(
'id' => 'caching',
'type' => 'checkbox',
'title' => esc_html__( 'Enable Caching', 'gk-gravityview' ),
'description' => strtr(
esc_html_x( '[url]Enabling caching[/url] improves performance by reducing the number of queries during page loads. When enabled, you can also specify cache duration for entries.', 'Placeholders inside [] are not to be translated.', 'gk-gravityview' ),
[
'[url]' => '<a class="underline" href="https://docs.gravitykit.com/article/58-about-gravityview-caching" rel="noopener noreferrer" target="_blank">',
'[/url]' => '</a>',
]
),
'value' => $this->get( 'caching', $default_settings['caching'] ),
),
array(
'id' => 'caching_entries',
'type' => 'number',
'requires' => array(
'id' => 'caching',
'operator' => '==',
'value' => 1,
),
'validation' => array(
array(
'rule' => 'min:1',
'message' => esc_html__( 'The cache duration must be at least 1 second.', 'gk-gravityview' ),
),
),
'title' => esc_html__( 'Entry Cache Duration', 'gk-gravityview' ),
'description' => esc_html__( 'Specify the duration in seconds that entry data should remain cached before being refreshed. A shorter duration ensures more up-to-date data, while a longer duration improves performance.', 'gk-gravityview' ),
'value' => $this->get( 'caching_entries', $default_settings['caching_entries'] ),
),
]
);

$settings = array(
'id' => self::SETTINGS_PLUGIN_ID,
'title' => 'GravityView',
Expand All @@ -246,6 +333,10 @@ public function add_settings( $plugins_data ) {
),
),
),
array(
'title' => esc_html__( 'Caching', 'gk-gravityview' ),
'settings' => $cache_settings,
),
array(
'title' => esc_html__( 'Permissions', 'gk-gravityview' ),
'settings' => array(
Expand Down
31 changes: 30 additions & 1 deletion future/includes/class-gv-settings-view.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace GV;

use GravityKit\GravityView\Foundation\Settings\Framework as SettingsFramework;

/** If this file is called directly, abort. */
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
die();
Expand Down Expand Up @@ -46,7 +48,6 @@ public static function with_defaults( $detailed = false ) {
* @param boolean $full_width True: Display the input and label together when rendering. False: Display label and input in separate columns when rendering.
*/
public static function defaults( $detailed = false, $group = null ) {

$default_settings = array_merge(
array(
'id' => array(
Expand Down Expand Up @@ -100,6 +101,34 @@ public static function defaults( $detailed = false, $group = null ) {
'url' => 'https://docs.gravitykit.com/article/490-entry-approval-gravity-forms',
),
),
'caching' => array(
'label' => __( 'Enable Caching', 'gk-gravityview' ),
'type' => 'checkbox',
'group' => 'default',
'value' => gravityview()->plugin->settings->get( 'caching' ),
'desc' => strtr(
esc_html_x( 'Turn caching on or off to improve performance. Default settings are configured in [url]GravityView Caching Settings[/url].', 'Placeholders inside [] are not to be translated.', 'gk-gravityview' ),
[
'[url]' => '<a href="' . esc_url( SettingsFramework::get_instance()->get_plugin_settings_url( Plugin_Settings::SETTINGS_PLUGIN_ID ) . '&s=1' ) . '">',
'[/url]' => '</a>',
]
),
'show_in_shortcode' => false,
'article' => array(
'id' => '54c67bb6e4b051242988550a',
'url' => 'https://docs.gravitykit.com/article/58-about-gravityview-caching',
),
),
'caching_entries' => array(
'label' => __( 'Entry Cache Duration', 'gk-gravityview' ),
'tooltip' => esc_html__( 'Specify the duration, in seconds, that entry data should remain cached before being refreshed. A shorter duration ensures more up-to-date data, while a longer duration improves performance.', 'gk-gravityview' ),
'type' => 'number',
'group' => 'default',
'value' => gravityview()->plugin->settings->get( 'caching_entries' ),
'show_in_shortcode' => false,
'requires' => 'caching=1',
'min' => 1,
),
'no_entries_options' => array(
'label' => __( 'No Entries Behavior', 'gk-gravityview' ),
'type' => 'select',
Expand Down
51 changes: 42 additions & 9 deletions future/includes/class-gv-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use GravityKit\GravityView\Foundation\Helpers\Arr;
use GF_Query;
use GravityView_Compatibility;
use GravityView_Cache;

/** If this file is called directly, abort. */
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
Expand Down Expand Up @@ -121,12 +123,15 @@ public function __construct() {
* @return void
*/
public static function register_post_type() {

/** Register only once */
if ( post_type_exists( 'gravityview' ) ) {
return;
}

if ( ! gravityview()->plugin->is_compatible() ) {
GravityView_Compatibility::override_post_pages_when_compatibility_fails();
}

/**
* Make GravityView Views hierarchical by returning TRUE.
* This will allow for Views to be nested with Parents and also allows for menu order to be set in the Page Attributes metabox
Expand Down Expand Up @@ -192,7 +197,7 @@ public static function register_post_type() {
* @param int $view_id The ID of the View currently being requested. `0` for general setting
*/
'public' => apply_filters( 'gravityview_direct_access', gravityview()->plugin->is_compatible(), 0 ),
'show_ui' => gravityview()->plugin->is_compatible(),
'show_ui' => true,
'show_in_menu' => false, // Menu items are added in \GV\Plugin::add_to_gravitykit_admin_menu()
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
Expand Down Expand Up @@ -1434,6 +1439,7 @@ function () use ( $query ) {

/**
* Queries database and conditionally caches results.
* First, checks if the long-lived cache is enabled and if the query is cached. If not, it checks if the short-lived cache is enabled and if the query is cached.
*
* @since 2.18.2
*
Expand All @@ -1442,8 +1448,38 @@ function () use ( $query ) {
* @return array{0: array, 1: GF_Query} Array of entries and the query object. The latter may be needed as it is modified during the query.
*/
private function run_db_query( GF_Query $query ) {
$db_entries = null;

$query_hash = md5( serialize( $query->_introspect() ) );

$atts = $this->settings->all();

$atts['query_hash'] = $query_hash;

$long_lived_cache = new GravityView_Cache( $this->form->ID, $atts );

if ( $long_lived_cache->use_cache() ) {
$cached_entries = $long_lived_cache->get();

if ( $cached_entries ) {
return [
$cached_entries,
$query,
];
}

$db_entries = $query->get();

if ( $long_lived_cache->set( $db_entries, 'entries' ) ) {
return [
$db_entries,
$query,
];
}
}

/**
* Controls whether the query is cached.
* Controls whether the query is cached per request. This is a short-lived cache.
*
* @filter gk/gravityview/view/entries/cache
*
Expand All @@ -1454,16 +1490,14 @@ private function run_db_query( GF_Query $query ) {
if ( ! apply_filters( 'gk/gravityview/view/entries/cache', true ) ) {
$db_entries = $query->get();

return array(
return [
$db_entries,
$query,
);
];
}

$query_hash = md5( serialize( $query->_introspect() ) );

if ( ! Arr::get( self::$cache, $query_hash ) ) {
$db_entries = $query->get();
$db_entries = $db_entries ?? $query->get();

self::$cache[ $query_hash ] = array(
$db_entries,
Expand All @@ -1482,7 +1516,6 @@ private function run_db_query( GF_Query $query ) {
* @return void
*/
public static function template_redirect() {

$is_csv = get_query_var( 'csv' );
$is_tsv = get_query_var( 'tsv' );

Expand Down
Loading

0 comments on commit 2548055

Please sign in to comment.