Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
4.17% covered (danger)
4.17%
2 / 48
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AdminNotices
4.17% covered (danger)
4.17%
2 / 48
50.00% covered (danger)
50.00%
2 / 4
161.74
0.00% covered (danger)
0.00%
0 / 1
 maybeRenderAdminNotices
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
56
 openContainer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 closeContainer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 adminScripts
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace NewfoldLabs\WP\Module\Notifications;
4
5use wpscholar\Url;
6use function NewfoldLabs\WP\ModuleLoader\container;
7
8/**
9 * Class AdminNotices
10 */
11class AdminNotices {
12
13    /**
14     * Render admin notices where appropriate.
15     */
16    public static function maybeRenderAdminNotices() {
17
18        $screen = get_current_screen();
19
20        // Bail if we're in the plugin app, since we already handle notifications in our React app.
21        if ( false !== strpos( $screen->id, container()->plugin()->id ) ) {
22            return;
23        }
24
25        // Bail if we're on a post or page list view in the admin
26        if ( $screen->base === 'edit' ) {
27            return;
28        }
29
30        // Handle realtime notifications
31        if ( 'plugin-install' === $screen->id ) {
32            ?>
33            <style>
34                .newfold-realtime-notice {
35                    margin: 5px 0 15px 0;
36                }
37            </style>
38            <?php
39        }
40
41        $page          = str_replace( admin_url(), '', Url::getCurrentUrl() );
42        $notifications = new NotificationsRepository( false );
43        $collection    = $notifications->collection();
44
45        // Constant container for admin notices
46        self::openContainer();
47
48        if ( $collection->count() ) {
49            // Only show the latest notification that should be shown on this page
50            foreach ( $collection as $notification ) {
51                if ( $notification->shouldShow( 'wp-admin-notice', array( 'page' => $page ) ) ) {
52                    ?>
53                    <div class="newfold-notice" data-id="<?php echo esc_attr( $notification->id ); ?>">
54                        <?php echo $notification->content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
55                    </div>
56                    <?php
57                    break;
58                }
59            }
60        }
61
62        self::closeContainer();
63
64        self::adminScripts( $collection->count() );
65
66    }
67
68
69    /**
70     * Open the notifactions container
71     */
72    public static function openContainer() {
73        echo wp_kses_post( '<div id="newfold-notifications" class="newfold-notifications-wrapper">' );
74    }
75
76    /**
77     * Close the notifications container
78     */
79    public static function closeContainer() {
80        echo wp_kses_post( '</div>' );
81    }
82
83    /**
84     * Handle scripts
85     *
86     *     @param int $notifications_count The number of notifications
87     */
88    public static function adminScripts( $notifications_count ) {
89
90        // Handle realtime notifications
91        $screen = get_current_screen();
92        if ( 'plugin-install' === $screen->id || 'theme-install' === $screen->id ) {
93            // Enqueue and set local values for realtime script on plugin install page only
94            wp_enqueue_script(
95                'newfold-plugin-realtime-notices',
96                plugins_url( 'vendor/newfold-labs/wp-module-notifications/assets/js/realtime-notices.js', container()->plugin()->file ),
97                array( 'lodash', 'nfd-runtime' ),
98                container()->plugin()->version,
99                true
100            );
101
102            // Localize the script with screen ID
103            wp_localize_script( 'newfold-plugin-realtime-notices', 'newfoldRealtimeData', array(
104                'screenID' => $screen->id,
105            ) );
106        }
107
108        if ( $notifications_count ) {
109            // Enqueue and set local values for dismiss script
110            wp_enqueue_script(
111                'newfold-dismiss-notices',
112                plugins_url( 'vendor/newfold-labs/wp-module-notifications/assets/js/dismiss-notices.js', container()->plugin()->file ),
113                array( 'nfd-runtime' ),
114                container()->plugin()->version,
115                true
116            );
117        }
118
119    }
120
121}