|
4 | 4 | use WpAddon\Services\CacheService; |
5 | 5 | use WpAddon\Services\OptionService; |
6 | 6 |
|
7 | | -class PageCache implements ModuleInterface |
8 | | -{ |
9 | | - private CacheService $cache; |
10 | | - private OptionService $optionService; |
11 | | - private array $config; |
12 | | - |
13 | | - public function __construct(OptionService $optionService) |
14 | | - { |
15 | | - $this->optionService = $optionService; |
16 | | - $this->loadConfig(); |
17 | | - $this->cache = new CacheService($this->config['cache_dir'], $this->config['ttl']); |
18 | | - } |
19 | | - |
20 | | - private function loadConfig(): void |
21 | | - { |
22 | | - $defaultConfig = require RW_PLUGIN_DIR . 'src/Config/cache.php'; |
23 | | - |
24 | | - $preloadPagesSetting = $this->optionService->getSetting('cache_preload_pages', ''); |
25 | | - $preloadPages = []; |
26 | | - if (!empty($preloadPagesSetting)) { |
27 | | - $preloadPages = array_filter(explode("\n", $preloadPagesSetting)); |
28 | | - } |
29 | | - // Если preload пустой - будет заполнен автоматически в preloadPages() |
30 | | - |
31 | | - $this->config = [ |
32 | | - 'enabled' => $this->optionService->getSetting('cache_enabled', $defaultConfig['enabled']), |
33 | | - 'ttl' => $this->optionService->getSetting('cache_ttl', $defaultConfig['ttl']), |
34 | | - 'exclude_logged_in' => $this->optionService->getSetting('cache_exclude_logged_in', $defaultConfig['exclude_logged_in']), |
35 | | - 'exclude_urls' => array_filter(explode("\n", $this->optionService->getSetting('cache_exclude_urls', implode("\n", $defaultConfig['exclude_urls'])))), |
36 | | - 'preload_pages' => $preloadPages, |
37 | | - 'auto_preload' => empty($preloadPagesSetting), |
38 | | - 'clear_on_post_save' => $this->optionService->getSetting('cache_clear_on_post_save', true), |
39 | | - 'cache_dir' => $defaultConfig['cache_dir'], |
40 | | - ]; |
41 | | - } |
42 | | - |
43 | | - private function getAutoPreloadPages(): array |
44 | | - { |
45 | | - $pages = []; |
46 | | - |
47 | | - // Получаем главную страницу |
48 | | - $frontPageId = get_option('page_on_front'); |
49 | | - if ($frontPageId) { |
50 | | - $frontPageUrl = get_permalink($frontPageId); |
51 | | - if ($frontPageUrl) { |
52 | | - $pages[] = parse_url($frontPageUrl, PHP_URL_PATH) ?: '/'; |
53 | | - } |
54 | | - } else { |
55 | | - $pages[] = '/'; |
56 | | - } |
57 | | - |
58 | | - // Получаем страницы из главного меню |
59 | | - $locations = get_nav_menu_locations(); |
60 | | - if (isset($locations['primary']) || isset($locations['main'])) { |
61 | | - $menuId = $locations['primary'] ?? $locations['main']; |
62 | | - $menuItems = wp_get_nav_menu_items($menuId); |
63 | | - |
64 | | - if ($menuItems) { |
65 | | - foreach ($menuItems as $item) { |
66 | | - if ($item->type === 'post_type' && $item->object === 'page') { |
67 | | - $pageUrl = parse_url($item->url, PHP_URL_PATH); |
68 | | - if ($pageUrl && $pageUrl !== '/' && !in_array($pageUrl, $pages)) { |
69 | | - $pages[] = $pageUrl; |
70 | | - } |
71 | | - } |
72 | | - } |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - // Добавляем страницу блога, если она есть |
77 | | - $blogPageId = get_option('page_for_posts'); |
78 | | - if ($blogPageId && $blogPageId !== $frontPageId) { |
79 | | - $blogUrl = get_permalink($blogPageId); |
80 | | - if ($blogUrl) { |
81 | | - $blogPath = parse_url($blogUrl, PHP_URL_PATH); |
82 | | - if ($blogPath && !in_array($blogPath, $pages)) { |
83 | | - $pages[] = $blogPath; |
84 | | - } |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - return array_slice($pages, 0, 10); // Ограничиваем до 10 страниц |
89 | | - } |
90 | | - |
91 | | - public function init(): void |
92 | | - { |
93 | | - if (!$this->config['enabled']) { |
94 | | - return; |
95 | | - } |
96 | | - |
97 | | - add_action('init', [$this, 'startCache']); |
98 | | - if ($this->config['clear_on_post_save']) { |
99 | | - add_action('save_post', [$this, 'clearCache']); |
100 | | - } |
101 | | - add_action('wp_loaded', [$this, 'preloadPages']); |
102 | | - |
103 | | - // Preload hook |
104 | | - add_action('page_cache_preload', [$this, 'doPreload']); |
105 | | - } |
106 | | - |
107 | | - public function doPreload(): void |
108 | | - { |
109 | | - $preloadPagesSetting = $this->optionService->getSetting('cache_preload_pages', ''); |
110 | | - $pages = []; |
111 | | - |
112 | | - if (!empty($preloadPagesSetting)) { |
113 | | - $pages = array_filter(explode("\n", $preloadPagesSetting)); |
114 | | - } else { |
115 | | - // Auto preload mode |
116 | | - $pages = $this->getAutoPreloadPages(); |
117 | | - } |
118 | | - |
119 | | - if (!empty($pages)) { |
120 | | - foreach ($pages as $url) { |
121 | | - $response = wp_remote_get(home_url(trim($url))); |
122 | | - if (!is_wp_error($response)) { |
123 | | - $key = $this->cache->generateCacheKey($url); |
124 | | - $this->cache->saveCachedContent($key, wp_remote_retrieve_body($response)); |
125 | | - } |
126 | | - } |
127 | | - } |
128 | | - } |
129 | | - |
130 | | - public function preloadPages(): void |
131 | | - { |
132 | | - if (!wp_next_scheduled('page_cache_preload')) { |
133 | | - wp_schedule_event(time(), 'hourly', 'page_cache_preload'); |
134 | | - } |
135 | | - } |
136 | | - |
137 | | - public function startCache(): void |
138 | | - { |
139 | | - if (!$this->shouldCache()) { |
140 | | - return; |
141 | | - } |
142 | | - |
143 | | - $key = $this->cache->generateCacheKey($_SERVER['REQUEST_URI']); |
144 | | - $cached = $this->cache->getCachedContent($key); |
145 | | - |
146 | | - if ($cached) { |
147 | | - echo $cached; |
148 | | - exit; |
149 | | - } |
150 | | - |
151 | | - ob_start([$this, 'cacheOutput']); |
152 | | - } |
153 | | - |
154 | | - public function cacheOutput(string $content): string |
155 | | - { |
156 | | - if ($this->shouldCache()) { |
157 | | - $key = $this->cache->generateCacheKey($_SERVER['REQUEST_URI']); |
158 | | - $this->cache->saveCachedContent($key, $content); |
159 | | - } |
160 | | - return $content; |
161 | | - } |
162 | | - |
163 | | - public function shouldCache(): bool |
164 | | - { |
165 | | - if (is_admin() || defined('DOING_AJAX') && DOING_AJAX) { |
166 | | - return false; |
167 | | - } |
168 | | - |
169 | | - if ($this->config['exclude_logged_in'] && is_user_logged_in()) { |
170 | | - return false; |
171 | | - } |
172 | | - |
173 | | - $url = $_SERVER['REQUEST_URI']; |
174 | | - foreach ($this->config['exclude_urls'] as $exclude) { |
175 | | - if (strpos($url, trim($exclude)) === 0) { |
176 | | - return false; |
177 | | - } |
178 | | - } |
179 | | - |
180 | | - return true; |
181 | | - } |
182 | | - |
183 | | - public function clearCache(): void |
184 | | - { |
185 | | - $this->cache->clearCache(); |
186 | | - } |
187 | | - |
188 | | - public function getExcludeRules(): array |
189 | | - { |
190 | | - return $this->config['exclude_urls']; |
191 | | - } |
| 7 | +class PageCache implements ModuleInterface { |
| 8 | + private CacheService $cache; |
| 9 | + private OptionService $optionService; |
| 10 | + private array $config; |
| 11 | + |
| 12 | + public function __construct( OptionService $optionService ) { |
| 13 | + $this->optionService = $optionService; |
| 14 | + $this->loadConfig(); |
| 15 | + $this->cache = new CacheService( $this->config['cache_dir'], $this->config['ttl'] ); |
| 16 | + } |
| 17 | + |
| 18 | + private function loadConfig(): void { |
| 19 | + $defaultConfig = require RW_PLUGIN_DIR . 'src/Config/cache.php'; |
| 20 | + |
| 21 | + $preloadPagesSetting = $this->optionService->getSetting( 'cache_preload_pages', '' ); |
| 22 | + $preloadPages = []; |
| 23 | + if ( ! empty( $preloadPagesSetting ) ) { |
| 24 | + $preloadPages = array_filter( explode( "\n", $preloadPagesSetting ) ); |
| 25 | + } |
| 26 | + // Если preload пустой - будет заполнен автоматически в preloadPages() |
| 27 | + |
| 28 | + $this->config = [ |
| 29 | + 'enabled' => $this->optionService->getSetting( 'cache_enabled', $defaultConfig['enabled'] ), |
| 30 | + 'ttl' => (int) $this->optionService->getSetting( 'cache_ttl', $defaultConfig['ttl'] ), |
| 31 | + 'exclude_logged_in' => $this->optionService->getSetting( 'cache_exclude_logged_in', $defaultConfig['exclude_logged_in'] ), |
| 32 | + 'exclude_urls' => array_filter( explode( "\n", $this->optionService->getSetting( 'cache_exclude_urls', implode( "\n", $defaultConfig['exclude_urls'] ) ) ) ), |
| 33 | + 'preload_pages' => $preloadPages, |
| 34 | + 'auto_preload' => empty( $preloadPagesSetting ), |
| 35 | + 'clear_on_post_save' => $this->optionService->getSetting( 'cache_clear_on_post_save', true ), |
| 36 | + 'cache_dir' => $defaultConfig['cache_dir'], |
| 37 | + ]; |
| 38 | + } |
| 39 | + |
| 40 | + public function init(): void { |
| 41 | + if ( ! $this->config['enabled'] ) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + add_action( 'init', [ $this, 'startCache' ] ); |
| 46 | + if ( $this->config['clear_on_post_save'] ) { |
| 47 | + add_action( 'save_post', [ $this, 'clearCache' ] ); |
| 48 | + } |
| 49 | + add_action( 'wp_loaded', [ $this, 'preloadPages' ] ); |
| 50 | + |
| 51 | + // Preload hook |
| 52 | + add_action( 'page_cache_preload', [ $this, 'doPreload' ] ); |
| 53 | + } |
| 54 | + |
| 55 | + public function doPreload(): void { |
| 56 | + $preloadPagesSetting = $this->optionService->getSetting( 'cache_preload_pages', '' ); |
| 57 | + $pages = []; |
| 58 | + |
| 59 | + if ( ! empty( $preloadPagesSetting ) ) { |
| 60 | + $pages = array_filter( explode( "\n", $preloadPagesSetting ) ); |
| 61 | + } else { |
| 62 | + // Auto preload mode |
| 63 | + $pages = $this->getAutoPreloadPages(); |
| 64 | + } |
| 65 | + |
| 66 | + if ( ! empty( $pages ) ) { |
| 67 | + foreach ( $pages as $url ) { |
| 68 | + $response = wp_remote_get( home_url( trim( $url ) ) ); |
| 69 | + if ( ! is_wp_error( $response ) ) { |
| 70 | + $key = $this->cache->generateCacheKey( $url ); |
| 71 | + $this->cache->saveCachedContent( $key, wp_remote_retrieve_body( $response ) ); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private function getAutoPreloadPages(): array { |
| 78 | + $pages = []; |
| 79 | + |
| 80 | + // Получаем главную страницу |
| 81 | + $frontPageId = get_option( 'page_on_front' ); |
| 82 | + if ( $frontPageId ) { |
| 83 | + $frontPageUrl = get_permalink( $frontPageId ); |
| 84 | + if ( $frontPageUrl ) { |
| 85 | + $pages[] = parse_url( $frontPageUrl, PHP_URL_PATH ) ?: '/'; |
| 86 | + } |
| 87 | + } else { |
| 88 | + $pages[] = '/'; |
| 89 | + } |
| 90 | + |
| 91 | + // Получаем страницы из главного меню |
| 92 | + $locations = get_nav_menu_locations(); |
| 93 | + if ( isset( $locations['primary'] ) || isset( $locations['main'] ) ) { |
| 94 | + $menuId = $locations['primary'] ?? $locations['main']; |
| 95 | + $menuItems = wp_get_nav_menu_items( $menuId ); |
| 96 | + |
| 97 | + if ( $menuItems ) { |
| 98 | + foreach ( $menuItems as $item ) { |
| 99 | + if ( $item->type === 'post_type' && $item->object === 'page' ) { |
| 100 | + $pageUrl = parse_url( $item->url, PHP_URL_PATH ); |
| 101 | + if ( $pageUrl && $pageUrl !== '/' && ! in_array( $pageUrl, $pages ) ) { |
| 102 | + $pages[] = $pageUrl; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Добавляем страницу блога, если она есть |
| 110 | + $blogPageId = get_option( 'page_for_posts' ); |
| 111 | + if ( $blogPageId && $blogPageId !== $frontPageId ) { |
| 112 | + $blogUrl = get_permalink( $blogPageId ); |
| 113 | + if ( $blogUrl ) { |
| 114 | + $blogPath = parse_url( $blogUrl, PHP_URL_PATH ); |
| 115 | + if ( $blogPath && ! in_array( $blogPath, $pages ) ) { |
| 116 | + $pages[] = $blogPath; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return array_slice( $pages, 0, 10 ); // Ограничиваем до 10 страниц |
| 122 | + } |
| 123 | + |
| 124 | + public function preloadPages(): void { |
| 125 | + if ( ! wp_next_scheduled( 'page_cache_preload' ) ) { |
| 126 | + wp_schedule_event( time(), 'hourly', 'page_cache_preload' ); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public function startCache(): void { |
| 131 | + if ( ! $this->shouldCache() ) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + $key = $this->cache->generateCacheKey( $_SERVER['REQUEST_URI'] ); |
| 136 | + $cached = $this->cache->getCachedContent( $key ); |
| 137 | + |
| 138 | + if ( $cached ) { |
| 139 | + echo $cached; |
| 140 | + exit; |
| 141 | + } |
| 142 | + |
| 143 | + ob_start( [ $this, 'cacheOutput' ] ); |
| 144 | + } |
| 145 | + |
| 146 | + public function shouldCache(): bool { |
| 147 | + if ( is_admin() || defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + if ( $this->config['exclude_logged_in'] && is_user_logged_in() ) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + $url = $_SERVER['REQUEST_URI']; |
| 156 | + foreach ( $this->config['exclude_urls'] as $exclude ) { |
| 157 | + if ( strpos( $url, trim( $exclude ) ) === 0 ) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return true; |
| 163 | + } |
| 164 | + |
| 165 | + public function cacheOutput( string $content ): string { |
| 166 | + if ( $this->shouldCache() ) { |
| 167 | + $key = $this->cache->generateCacheKey( $_SERVER['REQUEST_URI'] ); |
| 168 | + $this->cache->saveCachedContent( $key, $content ); |
| 169 | + } |
| 170 | + |
| 171 | + return $content; |
| 172 | + } |
| 173 | + |
| 174 | + public function clearCache(): void { |
| 175 | + $this->cache->clearCache(); |
| 176 | + } |
| 177 | + |
| 178 | + public function getExcludeRules(): array { |
| 179 | + return $this->config['exclude_urls']; |
| 180 | + } |
192 | 181 | } |
0 commit comments