| 1 |
<?php |
| 2 |
/** |
| 3 |
* Texty admin-notice bootstrap. |
| 4 |
* |
| 5 |
* Owns the wp-kit `NoticeManager` and registers the REST controller that |
| 6 |
* powers plugin-ui's `<AdminNotice />` (`/texty/v1/notices/{admin,dismiss}`). |
| 7 |
* |
| 8 |
* Other modules surface notices by implementing `NoticeProviderInterface` |
| 9 |
* and registering with `texty()->notices()->register_provider(...)`. |
| 10 |
* |
| 11 |
* @package Texty |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Texty; |
| 15 |
|
| 16 |
use Texty\Dependencies\WeDevs\WPKit\AdminNotification\Contracts\NoticeProviderInterface; |
| 17 |
use Texty\Dependencies\WeDevs\WPKit\AdminNotification\NoticeManager; |
| 18 |
use Texty\Dependencies\WeDevs\WPKit\AdminNotification\NoticeRESTController; |
| 19 |
|
| 20 |
defined( 'ABSPATH' ) || exit; |
| 21 |
|
| 22 |
/** |
| 23 |
* Texty admin-notice bootstrap. |
| 24 |
*/ |
| 25 |
class Notices { |
| 26 |
|
| 27 |
/** |
| 28 |
* Plugin prefix used for the notice option keys (dismissed list, etc.). |
| 29 |
*/ |
| 30 |
public const PREFIX = 'texty'; |
| 31 |
|
| 32 |
/** |
| 33 |
* REST API namespace shared with the rest of the plugin. |
| 34 |
*/ |
| 35 |
public const REST_NAMESPACE = 'texty/v1'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Notice manager. |
| 39 |
* |
| 40 |
* @var NoticeManager|null |
| 41 |
*/ |
| 42 |
protected ?NoticeManager $manager = null; |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor — registers the REST routes lazily via rest_api_init. |
| 46 |
*/ |
| 47 |
public function __construct() { |
| 48 |
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Lazily build the notice manager. |
| 53 |
* |
| 54 |
* @return NoticeManager |
| 55 |
*/ |
| 56 |
public function get_manager(): NoticeManager { |
| 57 |
if ( null === $this->manager ) { |
| 58 |
$this->manager = new NoticeManager( self::PREFIX ); |
| 59 |
} |
| 60 |
|
| 61 |
return $this->manager; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Register a notice provider. |
| 66 |
* |
| 67 |
* @param NoticeProviderInterface $provider Provider implementation. |
| 68 |
* |
| 69 |
* @return self |
| 70 |
*/ |
| 71 |
public function register_provider( NoticeProviderInterface $provider ): self { |
| 72 |
$this->get_manager()->register_provider( $provider ); |
| 73 |
|
| 74 |
return $this; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Register the wp-kit notice REST controller. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function register_rest_routes(): void { |
| 83 |
$notices = new NoticeRESTController( $this->get_manager(), self::REST_NAMESPACE ); |
| 84 |
$notices->register_routes(); |
| 85 |
} |
| 86 |
} |
| 87 |
|