Blog.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace addons\blog;
  3. use addons\blog\library\FulltextSearch;
  4. use app\common\library\Menu;
  5. use think\Addons;
  6. /**
  7. * 博客插件
  8. */
  9. class Blog extends Addons
  10. {
  11. /**
  12. * 插件安装方法
  13. * @return bool
  14. */
  15. public function install()
  16. {
  17. $menu = [
  18. [
  19. 'name' => 'blog',
  20. 'title' => '博客管理',
  21. 'sublist' => [
  22. [
  23. 'name' => 'blog/post',
  24. 'title' => '日志管理',
  25. 'sublist' => [
  26. ['name' => 'blog/post/index', 'title' => '查看'],
  27. ['name' => 'blog/post/add', 'title' => '添加'],
  28. ['name' => 'blog/post/edit', 'title' => '修改'],
  29. ['name' => 'blog/post/del', 'title' => '删除'],
  30. ['name' => 'blog/post/multi', 'title' => '批量更新'],
  31. ]
  32. ],
  33. [
  34. 'name' => 'blog/category',
  35. 'title' => '分类管理',
  36. 'sublist' => [
  37. ['name' => 'blog/category/index', 'title' => '查看'],
  38. ['name' => 'blog/category/add', 'title' => '添加'],
  39. ['name' => 'blog/category/edit', 'title' => '修改'],
  40. ['name' => 'blog/category/del', 'title' => '删除'],
  41. ['name' => 'blog/category/multi', 'title' => '批量更新'],
  42. ]
  43. ],
  44. [
  45. 'name' => 'blog/comment',
  46. 'title' => '评论管理',
  47. 'icon' => 'fa fa-comment',
  48. 'sublist' => [
  49. ['name' => 'blog/comment/index', 'title' => '查看'],
  50. ['name' => 'blog/comment/add', 'title' => '添加'],
  51. ['name' => 'blog/comment/edit', 'title' => '修改'],
  52. ['name' => 'blog/comment/del', 'title' => '删除'],
  53. ['name' => 'blog/comment/multi', 'title' => '批量更新'],
  54. ]
  55. ],
  56. [
  57. 'name' => 'blog/block',
  58. 'title' => '区块管理',
  59. 'icon' => 'fa fa-th-large',
  60. 'sublist' => [
  61. ['name' => 'blog/block/index', 'title' => '查看'],
  62. ['name' => 'blog/block/add', 'title' => '添加'],
  63. ['name' => 'blog/block/edit', 'title' => '修改'],
  64. ['name' => 'blog/block/del', 'title' => '删除'],
  65. ['name' => 'blog/block/multi', 'title' => '批量更新'],
  66. ]
  67. ]
  68. ]
  69. ]
  70. ];
  71. Menu::create($menu);
  72. return true;
  73. }
  74. /**
  75. * 插件卸载方法
  76. * @return bool
  77. */
  78. public function uninstall()
  79. {
  80. Menu::delete('blog');
  81. return true;
  82. }
  83. /**
  84. * 插件启用方法
  85. */
  86. public function enable()
  87. {
  88. Menu::enable('blog');
  89. }
  90. /**
  91. * 插件禁用方法
  92. */
  93. public function disable()
  94. {
  95. Menu::disable('blog');
  96. }
  97. public function xunsearchConfigInit()
  98. {
  99. return FulltextSearch::config();
  100. }
  101. public function xunsearchIndexReset($project)
  102. {
  103. if (!$project['isaddon'] || $project['name'] != 'blog') {
  104. return;
  105. }
  106. return FulltextSearch::reset();
  107. }
  108. /**
  109. * 脚本替换
  110. */
  111. public function viewFilter(& $content)
  112. {
  113. $request = \think\Request::instance();
  114. $dispatch = $request->dispatch();
  115. if ($request->module() || !isset($dispatch['method'][0]) || $dispatch['method'][0] != '\think\addons\Route') {
  116. return;
  117. }
  118. $addon = isset($dispatch['var']['addon']) ? $dispatch['var']['addon'] : $request->param('addon');
  119. if ($addon != 'blog') {
  120. return;
  121. }
  122. $style = '';
  123. $script = '';
  124. $result = preg_replace_callback("/<(script|style)\s(data\-render=\"(script|style)\")([\s\S]*?)>([\s\S]*?)<\/(script|style)>/i", function ($match) use (&$style, &$script) {
  125. if (isset($match[1]) && in_array($match[1], ['style', 'script'])) {
  126. ${$match[1]} .= str_replace($match[2], '', $match[0]);
  127. }
  128. return '';
  129. }, $content);
  130. $content = preg_replace_callback('/^\s+(\{__STYLE__\}|\{__SCRIPT__\})\s+$/m', function ($matches) use ($style, $script) {
  131. return $matches[1] == '{__STYLE__}' ? $style : $script;
  132. }, $result ? $result : $content);
  133. }
  134. }