|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace ICXCNIKA\DelayedUpdates; |
| 5 | + |
| 6 | +final class Delay { |
| 7 | + |
| 8 | + public function __construct( private array $settings ) {} |
| 9 | + |
| 10 | + public function register(): void { |
| 11 | + if ( ! $this->any_delay() ) { |
| 12 | + return; |
| 13 | + } |
| 14 | + add_filter( 'auto_update_plugin', array( $this, 'filter_plugin' ), 100, 2 ); |
| 15 | + add_filter( 'auto_update_theme', array( $this, 'filter_theme' ), 100, 2 ); |
| 16 | + add_filter( 'auto_update_core', array( $this, 'filter_core' ), 100, 2 ); |
| 17 | + } |
| 18 | + |
| 19 | + public function filter_plugin( $update, $item ) { |
| 20 | + return $this->decide( $update, 'plugin', self::describe_plugin( $item ) ); |
| 21 | + } |
| 22 | + |
| 23 | + public function filter_theme( $update, $item ) { |
| 24 | + return $this->decide( $update, 'theme', self::describe_theme( $item ) ); |
| 25 | + } |
| 26 | + |
| 27 | + public function filter_core( $update, $item ) { |
| 28 | + return $this->decide( $update, 'core', self::describe_core( $item ) ); |
| 29 | + } |
| 30 | + |
| 31 | + private function decide( $update, string $type, ?array $subject ) { |
| 32 | + if ( true !== $update || null === $subject ) { |
| 33 | + return $update; |
| 34 | + } |
| 35 | + |
| 36 | + $days = $this->days_for( $type, $subject['version'] ); |
| 37 | + if ( $days <= 0 ) { |
| 38 | + return $update; |
| 39 | + } |
| 40 | + |
| 41 | + if ( 'plugin' === $type && in_array( $subject['id'], $this->settings['immediate'], true ) ) { |
| 42 | + return $update; |
| 43 | + } |
| 44 | + |
| 45 | + if ( $this->settings['security_bypass'] |
| 46 | + && Release::security_notice( $type, $subject['slug'], $subject['version'], $subject['package'], $subject['notice'] ) |
| 47 | + ) { |
| 48 | + return $update; |
| 49 | + } |
| 50 | + |
| 51 | + $held = self::held_until( $type, $subject, $days ); |
| 52 | + if ( null === $held || $held <= time() ) { |
| 53 | + return $update; |
| 54 | + } |
| 55 | + |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + public static function held_until( string $type, array $subject, int $days ): ?int { |
| 60 | + if ( $days <= 0 ) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + $released = Release::date( $type, $subject['slug'], $subject['version'], $subject['package'] ); |
| 64 | + if ( null === $released ) { |
| 65 | + $released = Store::first_seen( $type, $subject['id'], $subject['version'] ); |
| 66 | + } |
| 67 | + return $released + ( $days * DAY_IN_SECONDS ); |
| 68 | + } |
| 69 | + |
| 70 | + public function days_for( string $type, string $version ): int { |
| 71 | + if ( 'plugin' === $type ) { |
| 72 | + return (int) $this->settings['delay_plugin']; |
| 73 | + } |
| 74 | + if ( 'theme' === $type ) { |
| 75 | + return (int) $this->settings['delay_theme']; |
| 76 | + } |
| 77 | + return self::is_major_core( $version ) |
| 78 | + ? (int) $this->settings['delay_core_major'] |
| 79 | + : (int) $this->settings['delay_core_minor']; |
| 80 | + } |
| 81 | + |
| 82 | + public static function is_major_core( string $version ): bool { |
| 83 | + $current = function_exists( 'wp_get_wp_version' ) ? wp_get_wp_version() : (string) get_bloginfo( 'version' ); |
| 84 | + return self::branch( $current ) !== self::branch( $version ); |
| 85 | + } |
| 86 | + |
| 87 | + private static function branch( string $version ): string { |
| 88 | + $parts = explode( '.', preg_replace( '/[^0-9.].*$/', '', $version ) ?? $version ); |
| 89 | + return ( $parts[0] ?? '0' ) . '.' . ( $parts[1] ?? '0' ); |
| 90 | + } |
| 91 | + |
| 92 | + public static function describe_plugin( $item ): ?array { |
| 93 | + $item = is_array( $item ) ? (object) $item : $item; |
| 94 | + if ( ! is_object( $item ) || empty( $item->plugin ) || empty( $item->new_version ) ) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + return array( |
| 98 | + 'id' => (string) $item->plugin, |
| 99 | + 'slug' => (string) ( $item->slug ?? dirname( (string) $item->plugin ) ), |
| 100 | + 'version' => (string) $item->new_version, |
| 101 | + 'package' => (string) ( $item->package ?? '' ), |
| 102 | + 'notice' => (string) ( $item->upgrade_notice ?? '' ), |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + public static function describe_theme( $item ): ?array { |
| 107 | + $item = is_array( $item ) ? (object) $item : $item; |
| 108 | + if ( ! is_object( $item ) || empty( $item->theme ) || empty( $item->new_version ) ) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + return array( |
| 112 | + 'id' => (string) $item->theme, |
| 113 | + 'slug' => (string) $item->theme, |
| 114 | + 'version' => (string) $item->new_version, |
| 115 | + 'package' => (string) ( $item->package ?? '' ), |
| 116 | + 'notice' => '', |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + public static function describe_core( $item ): ?array { |
| 121 | + if ( ! is_object( $item ) || empty( $item->current ) ) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + return array( |
| 125 | + 'id' => 'core', |
| 126 | + 'slug' => '', |
| 127 | + 'version' => (string) $item->current, |
| 128 | + 'package' => '', |
| 129 | + 'notice' => '', |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + private function any_delay(): bool { |
| 134 | + return $this->settings['delay_plugin'] > 0 |
| 135 | + || $this->settings['delay_theme'] > 0 |
| 136 | + || $this->settings['delay_core_minor'] > 0 |
| 137 | + || $this->settings['delay_core_major'] > 0; |
| 138 | + } |
| 139 | +} |
0 commit comments