← All changes
|
jetpack_vendor/automattic/jetpack-jitm/src/class-jitm.php
+60
-1
16.2
→
16.3-beta
View file →
| @@ -24,9 +24,9 @@ | ||
| 24 | 24 | * @since-jetpack 5.6.0 |
| 25 | 25 | */ |
| 26 | 26 | class JITM { |
| 27 | 27 | |
| 28 | - const PACKAGE_VERSION = '5.0.2'; | |
| 28 | + const PACKAGE_VERSION = '5.1.1'; | |
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * List of screen IDs where JITMs are allowed to display. |
| 32 | 32 | * |
| @@ -114,8 +114,67 @@ | ||
| 114 | 114 | if ( null === self::$configured_instance ) { |
| 115 | 115 | self::$configured_instance = self::get_instance(); |
| 116 | 116 | } |
| 117 | 117 | return self::$configured_instance; |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Clears admin notices from the current screen, keeping JITMs. | |
| 122 | + * | |
| 123 | + * A screen that opted out through `jetpack_display_jitms_on_screen` has nothing | |
| 124 | + * to keep, so it stays silent. | |
| 125 | + * | |
| 126 | + * @since 5.1.0 | |
| 127 | + * | |
| 128 | + * @return void | |
| 129 | + */ | |
| 130 | + public static function suppress_foreign_admin_notices() { | |
| 131 | + foreach ( array( 'admin_notices', 'all_admin_notices' ) as $hook ) { | |
| 132 | + $ours = self::collect_own_callbacks( $hook ); | |
| 133 | + | |
| 134 | + remove_all_actions( $hook ); | |
| 135 | + | |
| 136 | + foreach ( $ours as $priority => $callbacks ) { | |
| 137 | + foreach ( $callbacks as $callback ) { | |
| 138 | + add_action( $hook, $callback['function'], $priority, $callback['accepted_args'] ); | |
| 139 | + } | |
| 140 | + } | |
| 141 | + } | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Returns our callbacks on $hook, keyed by priority. | |
| 146 | + * | |
| 147 | + * Matched by class: `prepare_jitms()` hooks the cached instance and | |
| 148 | + * `get_instance()` builds a fresh one, so comparing objects would miss. | |
| 149 | + * | |
| 150 | + * @since 5.1.0 | |
| 151 | + * | |
| 152 | + * @param string $hook Hook name. | |
| 153 | + * @return array<int, array<int, array{function: callable, accepted_args: int}>> | |
| 154 | + */ | |
| 155 | + private static function collect_own_callbacks( $hook ) { | |
| 156 | + global $wp_filter; | |
| 157 | + | |
| 158 | + $kept = array(); | |
| 159 | + | |
| 160 | + if ( ! isset( $wp_filter[ $hook ] ) || ! is_object( $wp_filter[ $hook ] ) ) { | |
| 161 | + return $kept; | |
| 162 | + } | |
| 163 | + | |
| 164 | + foreach ( $wp_filter[ $hook ]->callbacks as $priority => $callbacks ) { | |
| 165 | + foreach ( $callbacks as $callback ) { | |
| 166 | + if ( | |
| 167 | + is_array( $callback['function'] ) | |
| 168 | + && isset( $callback['function'][0] ) | |
| 169 | + && $callback['function'][0] instanceof self | |
| 170 | + ) { | |
| 171 | + $kept[ $priority ][] = $callback; | |
| 172 | + } | |
| 173 | + } | |
| 174 | + } | |
| 175 | + | |
| 176 | + return $kept; | |
| 118 | 177 | } |
| 119 | 178 | |
| 120 | 179 | /** |
| 121 | 180 | * Sets up JITM action callbacks if needed. |