Notice.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Widget;
  3. use Typecho\Cookie;
  4. use Typecho\Widget;
  5. if (!defined('__TYPECHO_ROOT_DIR__')) {
  6. exit;
  7. }
  8. /**
  9. * 提示框组件
  10. *
  11. * @package Widget
  12. */
  13. class Notice extends Widget
  14. {
  15. /**
  16. * 提示高亮
  17. *
  18. * @var string
  19. */
  20. public $highlight;
  21. /**
  22. * 高亮相关元素
  23. *
  24. * @param string $theId 需要高亮元素的id
  25. */
  26. public function highlight(string $theId)
  27. {
  28. $this->highlight = $theId;
  29. Cookie::set(
  30. '__typecho_notice_highlight',
  31. $theId
  32. );
  33. }
  34. /**
  35. * 获取高亮的id
  36. *
  37. * @return integer
  38. */
  39. public function getHighlightId(): int
  40. {
  41. return preg_match("/[0-9]+/", $this->highlight, $matches) ? $matches[0] : 0;
  42. }
  43. /**
  44. * 设定堆栈每一行的值
  45. *
  46. * @param string|array $value 值对应的键值
  47. * @param string|null $type 提示类型
  48. * @param string $typeFix 兼容老插件
  49. */
  50. public function set($value, ?string $type = 'notice', string $typeFix = 'notice')
  51. {
  52. $notice = is_array($value) ? array_values($value) : [$value];
  53. if (empty($type) && $typeFix) {
  54. $type = $typeFix;
  55. }
  56. Cookie::set(
  57. '__typecho_notice',
  58. json_encode($notice)
  59. );
  60. Cookie::set(
  61. '__typecho_notice_type',
  62. $type
  63. );
  64. }
  65. }