Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Username, Password, Database and Unix Socket support. #352

Merged
merged 12 commits into from
Dec 18, 2024
Merged
32 changes: 23 additions & 9 deletions admin/class-nginx-helper-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,15 @@ public function nginx_helper_default_settings() {
'purge_feeds' => 1,
'redis_hostname' => '127.0.0.1',
'redis_port' => '6379',
'redis_prefix' => 'nginx-cache:',
'redis_prefix' => 'nginx-cache:',
'redis_unix_socket' => '',
'redis_database' => 0,
'redis_username' => '',
'redis_password' => '',
'purge_url' => '',
'redis_enabled_by_constant' => 0,
'redis_socket_enabled_by_constant' => 0,
'redis_acl_enabled_by_constant' => 0,
'preload_cache' => 0,
'is_cache_preloaded' => 0
);
Expand Down Expand Up @@ -325,6 +331,7 @@ public function nginx_helper_settings() {
'redis_hostname' => '127.0.0.1',
'redis_port' => '6379',
'redis_prefix' => 'nginx-cache:',
'redis_database' => 0,
)
);

Expand All @@ -339,17 +346,24 @@ public function nginx_helper_settings() {
defined( 'RT_WP_NGINX_HELPER_REDIS_PREFIX' )
);

$data['redis_acl_enabled_by_constant'] = defined('RT_WP_NGINX_HELPER_REDIS_USERNAME') && defined('RT_WP_NGINX_HELPER_REDIS_PASSWORD');
$data['redis_socket_enabled_by_constant'] = defined('RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET');
$data['redis_unix_socket'] = $data['redis_socket_enabled_by_constant'] ? RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET : $data['redis_unix_socket'];
$data['redis_username'] = $data['redis_acl_enabled_by_constant'] ? RT_WP_NGINX_HELPER_REDIS_USERNAME : $data['redis_username'];
$data['redis_password'] = $data['redis_acl_enabled_by_constant'] ? RT_WP_NGINX_HELPER_REDIS_PASSWORD : $data['redis_password'];

if ( ! $is_redis_enabled ) {
return $data;
}

$data['redis_enabled_by_constant'] = $is_redis_enabled;
$data['enable_purge'] = $is_redis_enabled;
$data['cache_method'] = 'enable_redis';
$data['redis_hostname'] = RT_WP_NGINX_HELPER_REDIS_HOSTNAME;
$data['redis_port'] = RT_WP_NGINX_HELPER_REDIS_PORT;
$data['redis_prefix'] = RT_WP_NGINX_HELPER_REDIS_PREFIX;


$data['redis_enabled_by_constant'] = $is_redis_enabled;
$data['enable_purge'] = $is_redis_enabled;
$data['cache_method'] = 'enable_redis';
$data['redis_hostname'] = RT_WP_NGINX_HELPER_REDIS_HOSTNAME;
$data['redis_port'] = RT_WP_NGINX_HELPER_REDIS_PORT;
$data['redis_prefix'] = RT_WP_NGINX_HELPER_REDIS_PREFIX;
$data['redis_database'] = defined('RT_WP_NGINX_HELPER_REDIS_DATABASE') ? RT_WP_NGINX_HELPER_REDIS_DATABASE : 0;

return $data;

}
Expand Down
27 changes: 24 additions & 3 deletions admin/class-phpredis-purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,32 @@ public function __construct() {
try {

$this->redis_object = new Redis();

$redis_acl = array();

$username = $nginx_helper_admin->options['redis_username'];
$password = $nginx_helper_admin->options['redis_password'];

if( $username && $password ) {
$redis_acl['auth'] = array( $username, $password );
}

$hostname = empty( $nginx_helper_admin->options['redis_unix_socket'] ) ? $nginx_helper_admin->options['redis_hostname'] : $nginx_helper_admin->options['redis_unix_socket'];
$port = empty( $nginx_helper_admin->options['redis_unix_socket'] ) ? $nginx_helper_admin->options['redis_port'] : 0;

$this->redis_object->connect(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my local setup, there’s no Nginx server or Redis installed. When I switch branches from master to your branch, the code throws a fatal error. The plugin is trying to connect, but the connection is being refused. If the plugin is attempting to make a connection, there should also be a fatal error in the master branch.

Please take a look into this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SH4LIN I am unable to replicate the issue on my end. Can you please share the error here if possible?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are throwing a fatal error when the Redis connection is refused, which is understandable. However, this makes the entire admin side inaccessible. As a result, there is no way to update the connection configuration except by manually updating the value in the database or using WP-CLI.

One solution is to wrap the $this->redis_object->connect inside try catch and on the catch display WP notice

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SH4LIN I found where the issue was and have fixed it. In predis it does not have checks for connection and instead immeditely executes the statement without checking if a connection is already established or not.

So in class-predis.php where we have addded selecting the databse feature, it was selecting the database without checking for connection establishment causing it to throw error.

$nginx_helper_admin->options['redis_hostname'],
$nginx_helper_admin->options['redis_port'],
5
$hostname,
$port,
5,
null,
0,
0,
$redis_acl
);

if( $nginx_helper_admin->options['redis_database'] !== 0 ) {
$this->redis_object->select($nginx_helper_admin->options['redis_database']);
}

} catch ( Exception $e ) {
$this->log( $e->getMessage(), 'ERROR' );
Expand Down
31 changes: 24 additions & 7 deletions admin/class-predis-purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,36 @@ public function __construct() {
}

Predis\Autoloader::register();


$predis_args = array();

$username = $nginx_helper_admin->options['redis_username'];
$password = $nginx_helper_admin->options['redis_password'];

if( empty( $nginx_helper_admin->options['redis_unix_socket'] ) ) {
$predis_args['path'] = $nginx_helper_admin->options['redis_unix_socket'];
} else {
$predis_args['host'] = $nginx_helper_admin->options['redis_hostname'];;
$predis_args['port'] = $nginx_helper_admin->options['redis_port'];
}

if ( $username && $password ) {
$predis_args['username'] = $username;
$predis_args['password'] = $password;
}

// redis server parameter.
$this->redis_object = new Predis\Client(
array(
'host' => $nginx_helper_admin->options['redis_hostname'],
'port' => $nginx_helper_admin->options['redis_port'],
)
);
$this->redis_object = new Predis\Client( $predis_args );

try {
$this->redis_object->connect();
} catch ( Exception $e ) {
$this->log( $e->getMessage(), 'ERROR' );
return;
}

if( $nginx_helper_admin->options['redis_database'] !== 0 ) {
$this->redis_object->select($nginx_helper_admin->options['redis_database']);
}

}
Expand Down
17 changes: 16 additions & 1 deletion admin/css/nginx-helper-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

.clearfix {
*zoom: 1;
zoom: 1;
}
.clearfix:before,
.clearfix:after {
Expand Down Expand Up @@ -103,3 +103,18 @@ form#purgeall .button-primary:focus {
font-size: 13px;
margin-left: 23px;
}

.password-input {
padding-right: 40px;
}

.password-show-hide-btn {
background-color: transparent;
border: 0;
cursor: pointer;
display: inline-block;
}

.password-wrapper {
display: flex;
}
13 changes: 13 additions & 0 deletions admin/js/nginx-helper-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@
}
);

jQuery('.password-show-hide-btn').on('click', function() {
var passwordInput = $(this).siblings('.password-input');
var icon = $(this).find('.password-input-icon');

if (passwordInput.attr('type') === 'password') {
passwordInput.attr('type', 'text');
icon.removeClass('dashicons-hidden').addClass('dashicons-visibility');
} else {
passwordInput.attr('type', 'password');
icon.removeClass('dashicons-visibility').addClass('dashicons-hidden');
}
});

/**
* Show OR Hide options on option checkbox
*
Expand Down
90 changes: 88 additions & 2 deletions admin/partials/nginx-helper-general-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
'redis_hostname',
'redis_port',
'redis_prefix',
'redis_database',
'redis_username',
'redis_password',
'redis_unix_socket',
'redis_socket_enabled_by_constant',
'redis_acl_enabled_by_constant',
'purge_homepage_on_edit',
'purge_homepage_on_del',
'purge_url',
Expand Down Expand Up @@ -253,7 +259,7 @@
<tr>
<th><label for="redis_hostname"><?php esc_html_e( 'Hostname', 'nginx-helper' ); ?></label></th>
<td>
<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"' : ''; ?> />
<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"' : ''; ?> />
<?php
if ( $nginx_helper_settings['redis_enabled_by_constant'] ) {

Expand All @@ -263,12 +269,19 @@

}
?>
<?php
if ( $nginx_helper_settings['redis_unix_socket'] ) {
echo '<p class="description">';
esc_html_e( 'Overridden by unix socket path.', 'nginx-helper' );
echo '</p>';
}
?>
</td>
</tr>
<tr>
<th><label for="redis_port"><?php esc_html_e( 'Port', 'nginx-helper' ); ?></label></th>
<td>
<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"' : ''; ?> />
<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"' : ''; ?> />
<?php
if ( $nginx_helper_settings['redis_enabled_by_constant'] ) {

Expand All @@ -278,6 +291,30 @@

}
?>
<?php
if ( $nginx_helper_settings['redis_unix_socket'] ) {

echo '<p class="description">';
esc_html_e( 'Overridden by unix socket path.', 'nginx-helper' );
echo '</p>';

}
?>
</td>
</tr>
<tr>
<th><label for="redis_unix_socket"><?php esc_html_e( 'Socket Path', 'nginx-helper' ); ?></label></th>
<td>
<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"' : ''; ?> />
<?php
if ( $nginx_helper_settings['redis_socket_enabled_by_constant'] ) {

echo '<p class="description">';
esc_html_e( 'Overridden by constant variables.', 'nginx-helper' );
echo '</p>';

}
?>
</td>
</tr>
<tr>
Expand All @@ -295,6 +332,55 @@
?>
</td>
</tr>
<tr>
<th><label for="redis_database"><?php esc_html_e( 'Database', 'nginx-helper' ); ?></label></th>
<td>
<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"' : ''; ?> />
<?php
if ( $nginx_helper_settings['redis_enabled_by_constant'] ) {

echo '<p class="description">';
esc_html_e( 'Overridden by constant variables.', 'nginx-helper' );
echo '</p>';

}
?>
</td>
</tr>

<tr>
<th><label for="redis_username"><?php esc_html_e( 'Username', 'nginx-helper' ); ?></label></th>
<td>
<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"' : ''; ?> />
<?php
if ( $nginx_helper_settings['redis_enabled_by_constant'] ) {

echo '<p class="description">';
esc_html_e( 'Overridden by constant variables.', 'nginx-helper' );
echo '</p>';

}
?>
</td>
</tr>

<tr>
<th><label for="redis_password"><?php esc_html_e( 'Password', 'nginx-helper' ); ?></label></th>
<td>
<div class="password-wrapper">
<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"' : ''; ?> />
<button type="button" class="password-show-hide-btn"><span class="dashicons dashicons-hidden password-input-icon"></span></button>
</div>
<?php
if ( $nginx_helper_settings['redis_enabled_by_constant'] ) {
echo '<p class="description">';
esc_html_e( 'Overridden by constant variables.', 'nginx-helper' );
echo '</p>';

}
?>
</td>
</tr>
</table>
</div> <!-- End of .inside -->
</div>
Expand Down
Loading