config
4 years ago
notices
1 month ago
Config.php
1 month ago
Dismissed.php
1 month ago
Display.php
1 month ago
Loader.php
1 month ago
MessageTexts.php
1 month ago
PageConfig.php
1 month ago
ScreenConfig.php
1 month ago
Store.php
1 month ago
Loader.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | namespace OTGS\Installer\AdminNotices; |
| 4 | |
| 5 | use function OTGS\Installer\FP\partial; |
| 6 | |
| 7 | class Loader { |
| 8 | |
| 9 | public static function addHooks( $isAjax ) { |
| 10 | add_action( 'current_screen', self::class . '::initDisplay' ); |
| 11 | if ( $isAjax ) { |
| 12 | add_action( 'wp_ajax_installer_dismiss_nag', Dismissed::class . '::dismissNotice' ); |
| 13 | } |
| 14 | add_action( 'activate_plugin', Dismissed::class . '::dismissNoticeOnPluginActivation', 10, 2 ); |
| 15 | } |
| 16 | |
| 17 | public static function initDisplay() { |
| 18 | |
| 19 | remove_action( 'current_screen', self::class . '::initDisplay' ); |
| 20 | |
| 21 | $messages = apply_filters( 'otgs_installer_admin_notices', [] ); |
| 22 | |
| 23 | if ( ! empty( $messages ) ) { |
| 24 | |
| 25 | $config = apply_filters( 'otgs_installer_admin_notices_config', [] ); |
| 26 | |
| 27 | $texts = apply_filters( 'otgs_installer_admin_notices_texts', [] ); |
| 28 | |
| 29 | $dismissedNotices = self::refreshDismissed(); |
| 30 | |
| 31 | ( new Display( |
| 32 | $messages, |
| 33 | $config, |
| 34 | new MessageTexts( $texts ), |
| 35 | partial( Dismissed::class . '::isDismissed', $dismissedNotices ) |
| 36 | ) )->addHooks(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | public static function isDismissed( $repository_id, $notice_id ) { |
| 41 | $remainigNotices = self::refreshDismissed(); |
| 42 | return Dismissed::isDismissed( $remainigNotices, $repository_id, $notice_id ); |
| 43 | } |
| 44 | |
| 45 | private static function refreshDismissed() { |
| 46 | $store = new Store(); |
| 47 | |
| 48 | $dismissedMessages = $store->get( Dismissed::STORE_KEY, [] ); |
| 49 | $remainingMessages = Dismissed::clearExpired( |
| 50 | $dismissedMessages, |
| 51 | [ self::class, 'timeOut' ] |
| 52 | ); |
| 53 | |
| 54 | if ( $dismissedMessages !== $remainingMessages ) { |
| 55 | $store->save( Dismissed::STORE_KEY, $remainingMessages ); |
| 56 | } |
| 57 | |
| 58 | return $remainingMessages; |
| 59 | } |
| 60 | |
| 61 | public static function timeOut( $start, $repo, $id ) { |
| 62 | $timeout = apply_filters( |
| 63 | 'otgs_installer_admin_notices_dismissed_time', |
| 64 | 2 * MONTH_IN_SECONDS, |
| 65 | $repo, |
| 66 | $id |
| 67 | ); |
| 68 | |
| 69 | return time() - $start > $timeout; |
| 70 | } |
| 71 | |
| 72 | } |
| 73 |