• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / #887

16 Jan 2025 10:08PM UTC coverage: 83.189% (-0.8%) from 83.968%
#887

push

php-coveralls

web-flow
Merge pull request #3272 from wp-graphql/release/v1.30.0

release: v1.30.0

473 of 718 new or added lines in 23 files covered. (65.88%)

2 existing lines in 2 files now uncovered.

12995 of 15621 relevant lines covered (83.19%)

298.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.63
/src/Admin/Updates/Updates.php
1
<?php
2
/**
3
 * Initializes Plugin Update functionality.
4
 *
5
 * @package WPGraphQL\Admin\Updates
6
 * @since 1.30.0
7
 */
8

9
namespace WPGraphQL\Admin\Updates;
10

11
/**
12
 * Class Updates
13
 */
14
final class Updates {
15
        /**
16
         * Initialize the Updates functionality.
17
         */
18
        public function init(): void {
19
                // Expose the plugin headers.
20
                add_filter( 'extra_plugin_headers', [ $this, 'enable_plugin_headers' ] );
8✔
21
                add_filter( 'extra_theme_headers', [ $this, 'enable_plugin_headers' ] );
8✔
22

23
                // Prevent autoupdates for untested WPGraphQL Extensions.
24
                add_filter( 'auto_update_plugin', [ $this, 'maybe_allow_autoupdates' ], 10, 2 );
8✔
25

26
                // Load the Update Checker for the current screen.
27
                add_action( 'current_screen', [ $this, 'load_screen_checker' ] );
8✔
28

29
                // Disable incompatible plugins.
30
                add_action( 'admin_init', [ $this, 'disable_incompatible_plugins' ] );
8✔
31
                add_action( 'graphql_activate', [ $this, 'disable_incompatible_plugins' ] );
8✔
32
                add_action( 'admin_notices', [ $this, 'disable_incompatible_plugins_notice' ] );
8✔
33

34
                // Register admin assets.
35
                add_action( 'admin_enqueue_scripts', [ $this, 'register_assets' ] );
8✔
36
        }
37

38
        /**
39
         * Enable the plugin headers.
40
         *
41
         * @param string[] $headers The headers.
42
         * @return string[]
43
         */
44
        public function enable_plugin_headers( $headers ) {
45
                $headers[] = UpdateChecker::VERSION_HEADER;
607✔
46
                $headers[] = UpdateChecker::TESTED_UP_TO_HEADER;
607✔
47

48
                return $headers;
607✔
49
        }
50

51
        /**
52
         * Prevent autoupdates when there is an untested WPGraphQL Extension.
53
         *
54
         * @param bool   $should_update Whether the plugin should autoupdate.
55
         * @param object $plugin The plugin data object.
56
         *
57
         * @return bool Whether the plugin should autoupdate.
58
         */
59
        public function maybe_allow_autoupdates( $should_update, $plugin ) {
60
                // Bail if it's not our plugin.
61
                if ( ! isset( $plugin->plugin ) || ! isset( $plugin->new_version ) || 'wp-graphql/wp-graphql.php' !== $plugin->plugin ) {
5✔
62
                        return $should_update;
1✔
63
                }
64

65
                // If autoupdates are already disabled we don't need to check further.
66
                if ( false === $should_update ) {
5✔
67
                        return $should_update;
1✔
68
                }
69

70
                $new_version = sanitize_text_field( $plugin->new_version );
5✔
71

72
                if ( '' === $new_version ) {
5✔
73
                        return $should_update;
1✔
74
                }
75

76
                // Store the sanitized version in the plugin object.
77
                $plugin->new_version = $new_version;
5✔
78

79
                $plugin_updates = new UpdateChecker( $plugin );
5✔
80

81
                return $plugin_updates->should_autoupdate( (bool) $should_update );
5✔
82
        }
83

84
        /**
85
         * Maybe loads the Update Checker for the current admin screen.
86
         */
87
        public function load_screen_checker(): void {
88
                $screen = get_current_screen();
2✔
89

90
                // Bail if we're not on a screen.
91
                if ( ! $screen ) {
2✔
NEW
92
                        return;
×
93
                }
94

95
                // Loaders for the different WPAdmin Screens.
96
                $loaders = [
2✔
97
                        'plugins'     => PluginsScreenLoader::class,
2✔
98
                        'update-core' => UpdatesScreenLoader::class,
2✔
99
                ];
2✔
100

101
                // Bail if the current screen doesn't need an update checker.
102
                if ( ! in_array( $screen->id, array_keys( $loaders ), true ) ) {
2✔
103
                        return;
1✔
104
                }
105

106
                // Load the update checker for the current screen.
107
                new $loaders[ $screen->id ]();
2✔
108
        }
109

110
        /**
111
         * Registers the admin assets.
112
         */
113
        public function register_assets(): void {
114
                $screen          = get_current_screen();
1✔
115
                $allowed_screens = [
1✔
116
                        'plugins',
1✔
117
                        'update-core',
1✔
118
                ];
1✔
119

120
                // Bail if we're not on a screen.
121
                if ( ! $screen || ! in_array( $screen->id, $allowed_screens, true ) ) {
1✔
122
                        return;
1✔
123
                }
124

125
                $asset_file = include WPGRAPHQL_PLUGIN_DIR . 'build/updates.asset.php';
1✔
126

127
                wp_enqueue_style(
1✔
128
                        'wp-graphql-admin-updates',
1✔
129
                        WPGRAPHQL_PLUGIN_URL . 'build/updates.css',
1✔
130
                        $asset_file['dependencies'],
1✔
131
                        $asset_file['version']
1✔
132
                );
1✔
133
        }
134

135
        /**
136
         * Disables plugins that don't meet the minimum `Requires WPGraphQL` version.
137
         */
138
        public function disable_incompatible_plugins(): void {
139
                // Initialize the Update Checker.
140
                $update_checker = new UpdateChecker( (object) get_plugins()['wp-graphql/wp-graphql.php'] );
1✔
141

142
                // Get the incompatible plugins.
143
                $incompatible_plugins = $update_checker->get_incompatible_plugins( WPGRAPHQL_VERSION, true );
1✔
144

145
                // Deactivate the incompatible plugins.
146
                $notice_data = [];
1✔
147
                foreach ( $incompatible_plugins as $file => $plugin ) {
1✔
148
                        $notice_data[] = [
1✔
149
                                'name'    => $plugin['Name'],
1✔
150
                                'version' => $plugin[ UpdateChecker::VERSION_HEADER ],
1✔
151
                        ];
1✔
152
                        deactivate_plugins( $file );
1✔
153
                }
154

155
                // Display a notice to the user.
156
                if ( ! empty( $notice_data ) ) {
1✔
157
                        set_transient( 'wpgraphql_incompatible_plugins', $notice_data );
1✔
158
                }
159
        }
160

161
        /**
162
         * Displays a one-time notice to the user if incompatible plugins were deactivated.
163
         */
164
        public function disable_incompatible_plugins_notice(): void {
165
                $incompatible_plugins = get_transient( 'wpgraphql_incompatible_plugins' );
1✔
166

167
                if ( empty( $incompatible_plugins ) ) {
1✔
168
                        return;
1✔
169
                }
170

171
                $notice = sprintf(
1✔
172
                        '<p>%s</p>',
1✔
173
                        __( 'The following plugins were deactivated because they require a newer version of WPGraphQL. Please update WPGraphQL to a newer version to reactivate these plugins.', 'wp-graphql' )
1✔
174
                );
1✔
175

176
                $notice .= '<ul class="ul-disc">';
1✔
177
                foreach ( $incompatible_plugins as $plugin ) {
1✔
178
                        $notice .= sprintf(
1✔
179
                                '<li><strong>%s</strong> (requires at least WPGraphQL: v%s)</li>',
1✔
180
                                esc_html( $plugin['name'] ),
1✔
181
                                esc_html( $plugin['version'] )
1✔
182
                        );
1✔
183
                }
184
                $notice .= '</ul>';
1✔
185

186
                echo wp_kses_post( sprintf( '<div class="notice notice-error is-dismissable">%s</div>', $notice ) );
1✔
187

188
                // Delete once the notice is displayed.
189
                delete_transient( 'wpgraphql_incompatible_plugins' );
1✔
190
        }
191
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc