|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Syntatis\FeatureFlipper\Features\Heartbeat; |
| 6 | + |
| 7 | +use SSFV\Codex\Contracts\Hookable; |
| 8 | +use SSFV\Codex\Foundation\Hooks\Hook; |
| 9 | +use Syntatis\FeatureFlipper\Concerns\HasHookName; |
| 10 | +use Syntatis\FeatureFlipper\Helpers\Option; |
| 11 | + |
| 12 | +use function is_numeric; |
| 13 | + |
| 14 | +use const PHP_INT_MAX; |
| 15 | + |
| 16 | +class ManageAdmin implements Hookable |
| 17 | +{ |
| 18 | + use HasHookName; |
| 19 | + |
| 20 | + private bool $heartbeat; |
| 21 | + |
| 22 | + public function __construct() |
| 23 | + { |
| 24 | + $this->heartbeat = (bool) Option::get('heartbeat'); |
| 25 | + } |
| 26 | + |
| 27 | + public function hook(Hook $hook): void |
| 28 | + { |
| 29 | + $hook->addAction('admin_init', [$this, 'deregisterScripts'], PHP_INT_MAX); |
| 30 | + $hook->addFilter('heartbeat_settings', [$this, 'filterSettings'], PHP_INT_MAX); |
| 31 | + $hook->addFilter( |
| 32 | + self::optionName('heartbeat_admin'), |
| 33 | + fn ($value) => $this->heartbeat ? $value : false, |
| 34 | + ); |
| 35 | + $hook->addFilter( |
| 36 | + self::defaultOptionName('heartbeat_admin'), |
| 37 | + fn ($value) => $this->heartbeat ? $value : false, |
| 38 | + ); |
| 39 | + $hook->addFilter( |
| 40 | + self::optionName('heartbeat_admin_interval'), |
| 41 | + fn ($value) => (bool) Option::get('heartbeat_admin') && $this->heartbeat ? $value : null, |
| 42 | + ); |
| 43 | + $hook->addFilter( |
| 44 | + self::defaultOptionName('heartbeat_admin_interval'), |
| 45 | + fn ($value) => (bool) Option::get('heartbeat_admin') && $this->heartbeat ? $value : null, |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function deregisterScripts(): void |
| 50 | + { |
| 51 | + if (! is_admin() || self::isPostEditor() || (bool) Option::get('heartbeat_admin')) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + wp_deregister_script('heartbeat'); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param array<string,mixed> $settings |
| 60 | + * |
| 61 | + * @return array<string,mixed> |
| 62 | + */ |
| 63 | + public function filterSettings(array $settings): array |
| 64 | + { |
| 65 | + /** |
| 66 | + * If it's not admin, return the settings as is. |
| 67 | + * |
| 68 | + * In the post editor, even though that it is on the admin area, settings |
| 69 | + * should also return as is as well, since the settings for post editor |
| 70 | + * would be applied from the `ManagePostEditor` class. |
| 71 | + * |
| 72 | + * @see \Syntatis\FeatureFlipper\Features\Heartbeat\ManagePostEditor::filterSettings() |
| 73 | + */ |
| 74 | + if (! is_admin() || self::isPostEditor()) { |
| 75 | + return $settings; |
| 76 | + } |
| 77 | + |
| 78 | + $interval = Option::get('heartbeat_admin_interval'); |
| 79 | + |
| 80 | + if (is_numeric($interval)) { |
| 81 | + $interval = absint($interval); |
| 82 | + |
| 83 | + $settings['interval'] = $interval; |
| 84 | + $settings['minimalInterval'] = $interval; |
| 85 | + } |
| 86 | + |
| 87 | + return $settings; |
| 88 | + } |
| 89 | + |
| 90 | + private static function isPostEditor(): bool |
| 91 | + { |
| 92 | + $pagenow = $GLOBALS['pagenow'] ?? ''; |
| 93 | + |
| 94 | + return is_admin() && ($pagenow === 'post.php' || $pagenow === 'post-new.php'); |
| 95 | + } |
| 96 | +} |
0 commit comments