From b78011f30d8383a0d04c7735ab8c1dfaac9aba1a Mon Sep 17 00:00:00 2001 From: Vlad Date: Thu, 15 Feb 2024 11:42:28 -0500 Subject: [PATCH] Display notices on All Views/New View pages when GF is not installed This implements #1979 using some "clever hacks" as there's no easy way to override WP's edit.php and post-new.php output. --- future/includes/class-gv-view.php | 68 +++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/future/includes/class-gv-view.php b/future/includes/class-gv-view.php index 2c75ab0fd..38219932a 100644 --- a/future/includes/class-gv-view.php +++ b/future/includes/class-gv-view.php @@ -4,6 +4,8 @@ use GravityKit\GravityView\Foundation\Helpers\Arr; use GF_Query; +use GravityView_Admin_Notices; +use GravityView_Compatibility; /** If this file is called directly, abort. */ if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { @@ -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() ) { + self::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 @@ -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, @@ -1482,7 +1487,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' ); @@ -1776,4 +1780,62 @@ protected function apply_legacy_join_is_approved_query_conditions( \GF_Query $qu $query->where( \GF_Query_Condition::_and( $query_parameters['where'], $condition ) ); } + + /** + * 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 + */ + private static function override_post_pages_when_compatibility_fails() { + global $pagenow; + + if ( ! in_array( $pagenow, array( 'edit.php', 'post-new.php' ) ) && 'gravityview' !== ( $_GET['post_type'] ?? '' ) ) { + return; + } + + $display_notices = function () { + remove_all_actions( 'admin_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. + } + + new GravityView_Admin_Notices(); + + add_filter( 'gravityview/admin/notices', function ( $notices ) use ( $compat_notices ) { + return array_merge( $notices, $compat_notices ); + } ); + + require_once ABSPATH . 'wp-admin/admin-header.php'; + + echo ''; + + require_once ABSPATH . 'wp-admin/admin-footer.php'; + + exit; + }; + + // Override the All Views (i.e., edit post) page. + add_filter( 'bulk_post_updated_messages', function ( $bulk_messages ) use ( $display_notices ) { + if ( get_current_screen()->id != 'edit-gravityview' ) { + return $bulk_messages; + } + + $display_notices(); + } ); + + // Override the New View (i.e., new post) page. + add_filter( 'replace_editor', function ( $replace_editor ) use ( $display_notices ) { + if ( 'gravityview' !== get_current_screen()->id ) { + return $replace_editor; + } + + $display_notices(); + } ); + } }