Skip to content

Commit

Permalink
Display notice on All Views/New View pages when GF is not activated (#…
Browse files Browse the repository at this point in the history
…1989)

Ref: #1979

GV notices are reused but made non-dismissible on those two pages.
  • Loading branch information
zackkatz authored Feb 22, 2024
2 parents 4197079 + 94de19e commit 5ef6db9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
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
9 changes: 6 additions & 3 deletions future/includes/class-gv-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

/** If this file is called directly, abort. */
Expand Down Expand Up @@ -122,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 @@ -193,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 @@ -1512,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
2 changes: 2 additions & 0 deletions includes/class-gravityview-admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ public function admin_notice() {
* Modify the notices displayed in the admin.
*
* @since 1.12
*
* @param array $notices Array of notices to display.
*/
$notices = apply_filters( 'gravityview/admin/notices', self::$admin_notices );

Expand Down
62 changes: 62 additions & 0 deletions includes/class-gravityview-compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,68 @@ public static function get_plugin_status( $location = '' ) {

return 'inactive';
}

/**
* Displays a notice on the All Views or New View page when the compatibility requirements for the plugin are not met.
*
* @since 2.19.7
*
* @return void
*/
public static function override_post_pages_when_compatibility_fails() {
global $pagenow;

if ( ! in_array( $pagenow, array( 'post.php', 'edit.php', 'post-new.php' ) ) ) {
return;
}

$display_notices = function ( $hook_data ) {
global $post;

if ( ! $post instanceof \WP_Post || 'gravityview' !== $post->post_type ) {
return $hook_data;
}

// We only care about GravityView notices :)
remove_all_actions( 'admin_notices' );
remove_all_actions( 'network_admin_notices' );

new GravityView_Admin_Notices();

/**
* Make GravityView notices non-dismissible and display them to all users.
*
* @param array $notices Array of notices to display.
*/
add_filter( 'gravityview/admin/notices', function ( $notices ) {
$compat_notices = GravityView_Compatibility::get_notices();

foreach ( $compat_notices as &$notice ) {
unset( $notice['dismiss'] ); // Make sure the notice is always displayed and is not dismissible.
unset( $notice['cap'] ); // Display the notice to everyone.
}

return array_merge( $notices, $compat_notices );
} );

// Hide the "Screen Options" tab.
add_filter( 'screen_options_show_screen', '__return_false' );

// Render the wrapper for the page, which will include the notices.
require_once ABSPATH . 'wp-admin/admin-header.php';
require_once ABSPATH . 'wp-admin/admin-footer.php';

exit;
};

add_filter( 'bulk_post_updated_messages', $display_notices ); // Fired on All Views page.

/**
* Fired on New View and Edit View pages.
* Without this in place, other notices, the Post Title, and the Publish metabox will continue to be displayed.
*/
add_filter( 'replace_editor', $display_notices );
}
}

GravityView_Compatibility::getInstance();
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h

* Added: Global and View-specific settings to control caching of View entries
* Added: 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 user when creating new or listing existing Views
* Fixed: Deprecation notice in PHP 8.1+ when displaying a View with file upload fields
* Fixed: Fatal error when exporting entries to CSV

Expand Down

0 comments on commit 5ef6db9

Please sign in to comment.