Skip to content

Commit

Permalink
Display notices on All Views/New View pages when GF is not installed
Browse files Browse the repository at this point in the history
This implements #1979 using some "clever hacks" as there's no easy
way to override WP's edit.php and post-new.php output.
  • Loading branch information
mrcasual committed Feb 15, 2024
1 parent de5468d commit b78011f
Showing 1 changed file with 65 additions and 3 deletions.
68 changes: 65 additions & 3 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_Admin_Notices;
use GravityView_Compatibility;

/** 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() ) {
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
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 @@ -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' );

Expand Down Expand Up @@ -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 '<style>#screen-meta-links{ display: none !important; }</style>';

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();
} );
}
}

0 comments on commit b78011f

Please sign in to comment.