Skip to content

Commit 0d4014a

Browse files
committed
Show hardcoded servers list on settings page
1 parent 3d17cfb commit 0d4014a

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/Admin/SettingsMain.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SnapCache\Admin;
44

5+
use SnapCache\Memcached;
56
use SnapCache\Options;
67

78
class SettingsMain {
@@ -94,14 +95,52 @@ public static function field_object_cache(): void {
9495
}
9596

9697
public static function field_memcached_servers(): void {
98+
$configurable = ! Options::isMemcachedServersInWpConfig();
99+
97100
$val = esc_textarea( get_option( 'snapcache_memcached_servers', 'localhost:11211' ) );
98101
?>
99102
<textarea name="snapcache_memcached_servers" rows="5" cols="50" class="large-text"
103+
<?php
104+
if ( ! $configurable ) {
105+
echo 'style="display: none"'; } ?>
100106
><?php echo $val; ?></textarea>
101107
<p class="description">
102-
One host per line. Format: <code>host:port</code>.
108+
<?php if ( $configurable ) : ?>
109+
One host per line. Format: <code>host:port</code> or <code>host:port weight</code>.
103110
Default is <code>localhost:11211</code>.
104-
</p>
105-
<?php
111+
</p>
112+
<?php
113+
else :
114+
$mc = Memcached::getSnapCacheMemcached();
115+
$servers = $mc::getServersFromConfig();
116+
?>
117+
<table class="widefat striped" style="width: auto; margin-top: 10px;">
118+
<thead>
119+
<tr>
120+
<th scope="col" style="padding-left: 10pt; width: auto">Host</th>
121+
<th scope="col" style="text-align: center; padding-left: 20pt; width: auto">
122+
Port</th>
123+
<th scope="col" style="text-align: center; padding-left: 20pt; width: auto">
124+
Weight</th>
125+
</tr>
126+
</thead>
127+
<tbody>
128+
<?php foreach ( $servers as $server ) : ?>
129+
<tr>
130+
<td style="padding-left: 10pt"><?php echo esc_html( $server[0] ); ?></td>
131+
<td style="text-align: center; padding-left: 20pt">
132+
<?php echo esc_html( $server[1] ?? 11211 ); ?></td>
133+
<td style="text-align: center; padding-left: 20pt">
134+
<?php echo esc_html( $server[2] ?? 0 ); ?></td>
135+
</tr>
136+
<?php endforeach; ?>
137+
</tbody>
138+
</table>
139+
<p>
140+
To change the server list, either edit <code>SNAPCACHE_MEMCACHED_SERVERS</code>
141+
in <code>wp-config.php</code> or remove it and refresh this page.
142+
</p>
143+
<?php
144+
endif;
106145
}
107146
}

src/Options.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ public static function getObjectCacheType(): string {
3030

3131
return $v;
3232
}
33+
34+
/**
35+
* Returns true if memcached servers are set by wp-config.php.
36+
* If false, servers are set by the `snapcache_object_cache` option.
37+
*/
38+
public static function isMemcachedServersInWpConfig(): bool {
39+
return defined( 'SNAPCACHE_MEMCACHED_SERVERS' );
40+
}
3341
}

0 commit comments

Comments
 (0)