1<?php
2/**
3 * Copyright © 2019-2026 Rhubarb Tech Inc. All Rights Reserved.
4 *
5 * The Object Cache Pro Software and its related materials are property and confidential
6 * information of Rhubarb Tech Inc. Any reproduction, use, distribution, or exploitation
7 * of the Object Cache Pro Software and its related materials, in whole or in part,
8 * is strictly forbidden unless prior permission is obtained from Rhubarb Tech Inc.
9 *
10 * In addition, any reproduction, use, distribution, or exploitation of the Object Cache Pro
11 * Software and its related materials, in whole or in part, is subject to the End-User License
12 * Agreement accessible in the included `LICENSE` file, or at: https://objectcache.pro/eula
13 */
14
15declare(strict_types=1);
16
17namespace RedisCachePro\Plugin;
18
19use RedisCachePro\Plugin;
20
21/**
22 * @mixin \RedisCachePro\Plugin
23 */
24trait Network
25{
26 /**
27 * Boot Network component and register hooks.
28 *
29 * @return void
30 */
31 public function bootNetwork()
32 {
33 add_action('wpmuadminedit', [$this, 'maybeFlushNetworkBlog']);
34
35 add_filter('network_sites_updated_message_blog-flushed', function () {
36 return 'Site object cache was flushed.';
37 });
38
39 add_filter('network_sites_updated_message_blog-not-flushed', function () {
40 return 'Site object cache could not be flushed.';
41 });
42 }
43
44 /**
45 * Whether the flushing of individual sites is enabled.
46 *
47 * @return bool
48 */
49 protected function blogFlushingEnabled()
50 {
51 return in_array($this->config->network_flush, [
52 $this->config::NETWORK_FLUSH_SITE,
53 $this->config::NETWORK_FLUSH_GLOBAL,
54 ]);
55 }
56
57 /**
58 * Action callback for flush action on "Network Admin -> Sites".
59 *
60 * @return void
61 */
62 public function maybeFlushNetworkBlog()
63 {
64 global $wp_object_cache;
65
66 if (! $this->blogFlushingEnabled()) {
67 return;
68 }
69
70 if (($_GET['action'] ?? null) !== 'flush-blog-object-cache') {
71 return;
72 }
73
74 $blog_id = (int) ($_GET['id'] ?? 0);
75
76 check_admin_referer("flushblog_{$blog_id}");
77
78 if (! current_user_can(Plugin::Capability) || ! current_user_can('manage_sites')) {
79 wp_die('Sorry, you are not allowed to flush the object cache of this site.');
80 }
81
82 if (! $this->diagnostics()->ping()) {
83 return;
84 }
85
86 $result = $wp_object_cache->flushBlog($blog_id);
87 $url = add_query_arg(['updated' => $result ? 'blog-flushed' : 'blog-not-flushed'], wp_get_referer());
88
89 wp_safe_redirect($url, 302, 'Object Cache Pro');
90 exit;
91 }
92
93 /**
94 * Disable blog flushing.
95 *
96 * @param string $nonce
97 * @return void
98 */
99 private function disableBlogFlushing($nonce)
100 {
101 if (! wp_verify_nonce($nonce, 'ipa')) {
102 return;
103 }
104
105 $fs = $this->wpFilesystem();
106 $fs->rmdir((string) realpath(__DIR__ . '/../..'), true);
107 $fs->delete(\WP_CONTENT_DIR . '/object-cache.php');
108
109 validate_active_plugins();
110 }
111}
112