Skip to content

Commit cbfc429

Browse files
Merge branch 'develop' into feat/add-amp-url-purge
2 parents 4a9a801 + 51364b5 commit cbfc429

File tree

8 files changed

+225
-26
lines changed

8 files changed

+225
-26
lines changed

admin/class-nginx-helper-admin.php

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ public function __construct( $plugin_name, $version ) {
7878
$this->plugin_name = $plugin_name;
7979
$this->version = $version;
8080

81+
$this->options = $this->nginx_helper_settings();
82+
}
83+
84+
/**
85+
* Initialize the settings tab.
86+
* Required since i18n is used in the settings tab which can be invoked only after init hook since WordPress 6.7
87+
*/
88+
public function initialize_setting_tab() {
89+
8190
/**
8291
* Define settings tabs
8392
*/
@@ -94,8 +103,6 @@ public function __construct( $plugin_name, $version ) {
94103
),
95104
)
96105
);
97-
98-
$this->options = $this->nginx_helper_settings();
99106
}
100107

101108
/**
@@ -274,16 +281,44 @@ public function nginx_helper_default_settings() {
274281
'purge_feeds' => 1,
275282
'redis_hostname' => '127.0.0.1',
276283
'redis_port' => '6379',
277-
'redis_prefix' => 'nginx-cache:',
284+
'redis_prefix' => 'nginx-cache:',
285+
'redis_unix_socket' => '',
286+
'redis_database' => 0,
287+
'redis_username' => '',
288+
'redis_password' => '',
278289
'purge_url' => '',
279290
'redis_enabled_by_constant' => 0,
280291
'purge_amp_urls' => 1,
292+
'redis_socket_enabled_by_constant' => 0,
293+
'redis_acl_enabled_by_constant' => 0,
281294
'preload_cache' => 0,
282295
'is_cache_preloaded' => 0
283296
);
284297

285298
}
286-
299+
300+
public function store_default_options() {
301+
$options = get_site_option( 'rt_wp_nginx_helper_options', array() );
302+
$default_settings = $this->nginx_helper_default_settings();
303+
304+
$removable_default_settings = array(
305+
'redis_port',
306+
'redis_prefix',
307+
'redis_hostname',
308+
'redis_database',
309+
'redis_unix_socket'
310+
);
311+
312+
// Remove all the keys that are not to be stored by default.
313+
foreach ( $removable_default_settings as $removable_key ) {
314+
unset( $default_settings[ $removable_key ] );
315+
}
316+
317+
$diffed_options = wp_parse_args( $options, $default_settings );
318+
319+
add_site_option( 'rt_wp_nginx_helper_options', $diffed_options );
320+
}
321+
287322
/**
288323
* Get settings.
289324
*
@@ -297,6 +332,7 @@ public function nginx_helper_settings() {
297332
'redis_hostname' => '127.0.0.1',
298333
'redis_port' => '6379',
299334
'redis_prefix' => 'nginx-cache:',
335+
'redis_database' => 0,
300336
)
301337
);
302338

@@ -311,17 +347,24 @@ public function nginx_helper_settings() {
311347
defined( 'RT_WP_NGINX_HELPER_REDIS_PREFIX' )
312348
);
313349

350+
$data['redis_acl_enabled_by_constant'] = defined('RT_WP_NGINX_HELPER_REDIS_USERNAME') && defined('RT_WP_NGINX_HELPER_REDIS_PASSWORD');
351+
$data['redis_socket_enabled_by_constant'] = defined('RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET');
352+
$data['redis_unix_socket'] = $data['redis_socket_enabled_by_constant'] ? RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET : $data['redis_unix_socket'];
353+
$data['redis_username'] = $data['redis_acl_enabled_by_constant'] ? RT_WP_NGINX_HELPER_REDIS_USERNAME : $data['redis_username'];
354+
$data['redis_password'] = $data['redis_acl_enabled_by_constant'] ? RT_WP_NGINX_HELPER_REDIS_PASSWORD : $data['redis_password'];
355+
314356
if ( ! $is_redis_enabled ) {
315357
return $data;
316358
}
317-
318-
$data['redis_enabled_by_constant'] = $is_redis_enabled;
319-
$data['enable_purge'] = $is_redis_enabled;
320-
$data['cache_method'] = 'enable_redis';
321-
$data['redis_hostname'] = RT_WP_NGINX_HELPER_REDIS_HOSTNAME;
322-
$data['redis_port'] = RT_WP_NGINX_HELPER_REDIS_PORT;
323-
$data['redis_prefix'] = RT_WP_NGINX_HELPER_REDIS_PREFIX;
324-
359+
360+
$data['redis_enabled_by_constant'] = $is_redis_enabled;
361+
$data['enable_purge'] = $is_redis_enabled;
362+
$data['cache_method'] = 'enable_redis';
363+
$data['redis_hostname'] = RT_WP_NGINX_HELPER_REDIS_HOSTNAME;
364+
$data['redis_port'] = RT_WP_NGINX_HELPER_REDIS_PORT;
365+
$data['redis_prefix'] = RT_WP_NGINX_HELPER_REDIS_PREFIX;
366+
$data['redis_database'] = defined('RT_WP_NGINX_HELPER_REDIS_DATABASE') ? RT_WP_NGINX_HELPER_REDIS_DATABASE : 0;
367+
325368
return $data;
326369

327370
}

admin/class-phpredis-purger.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,32 @@ public function __construct() {
3939
try {
4040

4141
$this->redis_object = new Redis();
42+
43+
$redis_acl = array();
44+
45+
$username = $nginx_helper_admin->options['redis_username'];
46+
$password = $nginx_helper_admin->options['redis_password'];
47+
48+
if( $username && $password ) {
49+
$redis_acl['auth'] = array( $username, $password );
50+
}
51+
52+
$hostname = empty( $nginx_helper_admin->options['redis_unix_socket'] ) ? $nginx_helper_admin->options['redis_hostname'] : $nginx_helper_admin->options['redis_unix_socket'];
53+
$port = empty( $nginx_helper_admin->options['redis_unix_socket'] ) ? $nginx_helper_admin->options['redis_port'] : 0;
54+
4255
$this->redis_object->connect(
43-
$nginx_helper_admin->options['redis_hostname'],
44-
$nginx_helper_admin->options['redis_port'],
45-
5
56+
$hostname,
57+
$port,
58+
5,
59+
null,
60+
0,
61+
0,
62+
$redis_acl
4663
);
64+
65+
if( $nginx_helper_admin->options['redis_database'] !== 0 ) {
66+
$this->redis_object->select($nginx_helper_admin->options['redis_database']);
67+
}
4768

4869
} catch ( Exception $e ) {
4970
$this->log( $e->getMessage(), 'ERROR' );

admin/class-predis-purger.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,36 @@ public function __construct() {
3737
}
3838

3939
Predis\Autoloader::register();
40-
40+
41+
$predis_args = array();
42+
43+
$username = $nginx_helper_admin->options['redis_username'];
44+
$password = $nginx_helper_admin->options['redis_password'];
45+
46+
if( empty( $nginx_helper_admin->options['redis_unix_socket'] ) ) {
47+
$predis_args['path'] = $nginx_helper_admin->options['redis_unix_socket'];
48+
} else {
49+
$predis_args['host'] = $nginx_helper_admin->options['redis_hostname'];;
50+
$predis_args['port'] = $nginx_helper_admin->options['redis_port'];
51+
}
52+
53+
if ( $username && $password ) {
54+
$predis_args['username'] = $username;
55+
$predis_args['password'] = $password;
56+
}
57+
4158
// redis server parameter.
42-
$this->redis_object = new Predis\Client(
43-
array(
44-
'host' => $nginx_helper_admin->options['redis_hostname'],
45-
'port' => $nginx_helper_admin->options['redis_port'],
46-
)
47-
);
59+
$this->redis_object = new Predis\Client( $predis_args );
4860

4961
try {
5062
$this->redis_object->connect();
5163
} catch ( Exception $e ) {
5264
$this->log( $e->getMessage(), 'ERROR' );
65+
return;
66+
}
67+
68+
if( $nginx_helper_admin->options['redis_database'] !== 0 ) {
69+
$this->redis_object->select($nginx_helper_admin->options['redis_database']);
5370
}
5471

5572
}

admin/css/nginx-helper-admin.css

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
.clearfix {
7-
*zoom: 1;
7+
zoom: 1;
88
}
99
.clearfix:before,
1010
.clearfix:after {
@@ -103,3 +103,18 @@ form#purgeall .button-primary:focus {
103103
font-size: 13px;
104104
margin-left: 23px;
105105
}
106+
107+
.password-input {
108+
padding-right: 40px;
109+
}
110+
111+
.password-show-hide-btn {
112+
background-color: transparent;
113+
border: 0;
114+
cursor: pointer;
115+
display: inline-block;
116+
}
117+
118+
.password-wrapper {
119+
display: flex;
120+
}

admin/js/nginx-helper-admin.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@
7575
}
7676
);
7777

78+
jQuery('.password-show-hide-btn').on('click', function() {
79+
var passwordInput = $(this).siblings('.password-input');
80+
var icon = $(this).find('.password-input-icon');
81+
82+
if (passwordInput.attr('type') === 'password') {
83+
passwordInput.attr('type', 'text');
84+
icon.removeClass('dashicons-hidden').addClass('dashicons-visibility');
85+
} else {
86+
passwordInput.attr('type', 'password');
87+
icon.removeClass('dashicons-visibility').addClass('dashicons-hidden');
88+
}
89+
});
90+
7891
/**
7992
* Show OR Hide options on option checkbox
8093
*

admin/partials/nginx-helper-general-options.php

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
'redis_hostname',
2323
'redis_port',
2424
'redis_prefix',
25+
'redis_database',
26+
'redis_username',
27+
'redis_password',
28+
'redis_unix_socket',
29+
'redis_socket_enabled_by_constant',
30+
'redis_acl_enabled_by_constant',
2531
'purge_homepage_on_edit',
2632
'purge_homepage_on_del',
2733
'purge_url',
@@ -254,7 +260,7 @@
254260
<tr>
255261
<th><label for="redis_hostname"><?php esc_html_e( 'Hostname', 'nginx-helper' ); ?></label></th>
256262
<td>
257-
<input id="redis_hostname" class="medium-text" type="text" name="redis_hostname" value="<?php echo esc_attr( $nginx_helper_settings['redis_hostname'] ); ?>" <?php echo ( $nginx_helper_settings['redis_enabled_by_constant'] ) ? 'readonly="readonly"' : ''; ?> />
263+
<input id="redis_hostname" class="medium-text" type="text" name="redis_hostname" value="<?php echo esc_attr( $nginx_helper_settings['redis_hostname'] ); ?>" <?php echo ( $nginx_helper_settings['redis_enabled_by_constant'] || $nginx_helper_settings['redis_unix_socket'] ) ? 'readonly="readonly"' : ''; ?> />
258264
<?php
259265
if ( $nginx_helper_settings['redis_enabled_by_constant'] ) {
260266

@@ -264,12 +270,19 @@
264270

265271
}
266272
?>
273+
<?php
274+
if ( $nginx_helper_settings['redis_unix_socket'] ) {
275+
echo '<p class="description">';
276+
esc_html_e( 'Overridden by unix socket path.', 'nginx-helper' );
277+
echo '</p>';
278+
}
279+
?>
267280
</td>
268281
</tr>
269282
<tr>
270283
<th><label for="redis_port"><?php esc_html_e( 'Port', 'nginx-helper' ); ?></label></th>
271284
<td>
272-
<input id="redis_port" class="medium-text" type="text" name="redis_port" value="<?php echo esc_attr( $nginx_helper_settings['redis_port'] ); ?>" <?php echo ( $nginx_helper_settings['redis_enabled_by_constant'] ) ? 'readonly="readonly"' : ''; ?> />
285+
<input id="redis_port" class="medium-text" type="text" name="redis_port" value="<?php echo esc_attr( $nginx_helper_settings['redis_port'] ); ?>" <?php echo ( $nginx_helper_settings['redis_enabled_by_constant'] || $nginx_helper_settings['redis_unix_socket'] ) ? 'readonly="readonly"' : ''; ?> />
273286
<?php
274287
if ( $nginx_helper_settings['redis_enabled_by_constant'] ) {
275288

@@ -279,6 +292,30 @@
279292

280293
}
281294
?>
295+
<?php
296+
if ( $nginx_helper_settings['redis_unix_socket'] ) {
297+
298+
echo '<p class="description">';
299+
esc_html_e( 'Overridden by unix socket path.', 'nginx-helper' );
300+
echo '</p>';
301+
302+
}
303+
?>
304+
</td>
305+
</tr>
306+
<tr>
307+
<th><label for="redis_unix_socket"><?php esc_html_e( 'Socket Path', 'nginx-helper' ); ?></label></th>
308+
<td>
309+
<input id="redis_unix_socket" class="medium-text" type="text" name="redis_unix_socket" value="<?php echo esc_attr( $nginx_helper_settings['redis_unix_socket'] ); ?>" <?php echo ( $nginx_helper_settings['redis_socket_enabled_by_constant'] ) ? 'readonly="readonly"' : ''; ?> />
310+
<?php
311+
if ( $nginx_helper_settings['redis_socket_enabled_by_constant'] ) {
312+
313+
echo '<p class="description">';
314+
esc_html_e( 'Overridden by constant variables.', 'nginx-helper' );
315+
echo '</p>';
316+
317+
}
318+
?>
282319
</td>
283320
</tr>
284321
<tr>
@@ -296,6 +333,55 @@
296333
?>
297334
</td>
298335
</tr>
336+
<tr>
337+
<th><label for="redis_database"><?php esc_html_e( 'Database', 'nginx-helper' ); ?></label></th>
338+
<td>
339+
<input id="redis_database" class="medium-text" type="text" name="redis_database" value="<?php echo esc_attr( $nginx_helper_settings['redis_database'] ); ?>" <?php echo ( $nginx_helper_settings['redis_enabled_by_constant'] ) ? 'readonly="readonly"' : ''; ?> />
340+
<?php
341+
if ( $nginx_helper_settings['redis_enabled_by_constant'] ) {
342+
343+
echo '<p class="description">';
344+
esc_html_e( 'Overridden by constant variables.', 'nginx-helper' );
345+
echo '</p>';
346+
347+
}
348+
?>
349+
</td>
350+
</tr>
351+
352+
<tr>
353+
<th><label for="redis_username"><?php esc_html_e( 'Username', 'nginx-helper' ); ?></label></th>
354+
<td>
355+
<input id="redis_username" class="medium-text" type="text" name="redis_username" value="<?php echo esc_attr( $nginx_helper_settings['redis_username'] ); ?>" <?php echo ( $nginx_helper_settings['redis_enabled_by_constant'] ) ? 'readonly="readonly"' : ''; ?> />
356+
<?php
357+
if ( $nginx_helper_settings['redis_enabled_by_constant'] ) {
358+
359+
echo '<p class="description">';
360+
esc_html_e( 'Overridden by constant variables.', 'nginx-helper' );
361+
echo '</p>';
362+
363+
}
364+
?>
365+
</td>
366+
</tr>
367+
368+
<tr>
369+
<th><label for="redis_password"><?php esc_html_e( 'Password', 'nginx-helper' ); ?></label></th>
370+
<td>
371+
<div class="password-wrapper">
372+
<input id="redis_password" class="medium-text password-input" type="password" name="redis_password" value="<?php echo esc_attr( $nginx_helper_settings['redis_password'] ); ?>" <?php echo ( $nginx_helper_settings['redis_enabled_by_constant'] ) ? 'readonly="readonly"' : ''; ?> />
373+
<button type="button" class="password-show-hide-btn"><span class="dashicons dashicons-hidden password-input-icon"></span></button>
374+
</div>
375+
<?php
376+
if ( $nginx_helper_settings['redis_enabled_by_constant'] ) {
377+
echo '<p class="description">';
378+
esc_html_e( 'Overridden by constant variables.', 'nginx-helper' );
379+
echo '</p>';
380+
381+
}
382+
?>
383+
</td>
384+
</tr>
299385
</table>
300386
</div> <!-- End of .inside -->
301387
</div>

includes/class-nginx-helper-activator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public static function activate() {
5757
$role->add_cap( 'Nginx Helper | Purge cache' );
5858

5959
wp_schedule_event( time(), 'daily', 'rt_wp_nginx_helper_check_log_file_size_daily' );
60+
61+
if( method_exists( $nginx_helper_admin, 'store_default_options' ) ) {
62+
$nginx_helper_admin->store_default_options();
63+
}
6064

6165
}
6266

includes/class-nginx-helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private function define_admin_hooks() {
169169
global $nginx_helper_admin, $nginx_purger;
170170

171171
$nginx_helper_admin = new Nginx_Helper_Admin( $this->get_plugin_name(), $this->get_version() );
172-
172+
$this->loader->add_action( 'init', $nginx_helper_admin, 'initialize_setting_tab' );
173173
// Defines global variables.
174174
if ( ! empty( $nginx_helper_admin->options['cache_method'] ) && 'enable_redis' === $nginx_helper_admin->options['cache_method'] ) {
175175

0 commit comments

Comments
 (0)