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

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions admin/class-nginx-helper-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,14 @@ public function nginx_helper_default_settings() {
'redis_hostname' => '127.0.0.1',
'redis_port' => '6379',
'redis_prefix' => 'nginx-cache:',
'redis_unix_socket' => '',
'redis_database' => 0,
'redis_username' => '',
'redis_password' => '',
Comment on lines +279 to +282
Copy link

Choose a reason for hiding this comment

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

Fix the indentation

'purge_url' => '',
'redis_enabled_by_constant' => 0,
'redis_socket_enabled_by_constant' => 0,
'redis_acl_enabled_by_constant' => 0,
);

}
Expand All @@ -295,6 +301,7 @@ public function nginx_helper_settings() {
'redis_hostname' => '127.0.0.1',
'redis_port' => '6379',
'redis_prefix' => 'nginx-cache:',
'redis_database' => 0,
)
);

Expand All @@ -308,17 +315,24 @@ public function nginx_helper_settings() {
defined( 'RT_WP_NGINX_HELPER_REDIS_PORT' ) &&
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;
Copy link

Choose a reason for hiding this comment

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

Fix indentation


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.

$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
30 changes: 23 additions & 7 deletions admin/class-predis-purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,30 @@ 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 );

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

try {
$this->redis_object->connect();
Expand Down
88 changes: 86 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',
Copy link

Choose a reason for hiding this comment

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

Fix the indentation

'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 @@ -246,7 +252,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 @@ -256,12 +262,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 @@ -271,8 +284,32 @@

}
?>
<?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>
Copy link

Choose a reason for hiding this comment

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

Fix the indentation and use tabs instead of the space

<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>
<th><label for="redis_prefix"><?php esc_html_e( 'Prefix', 'nginx-helper' ); ?></label></th>
<td>
Expand All @@ -288,6 +325,53 @@
?>
</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>
<input id="redis_password" class="medium-text" type="text" name="redis_password" value="<?php echo esc_attr( $nginx_helper_settings['redis_password'] ); ?>" <?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>
</table>
</div> <!-- End of .inside -->
</div>
Expand Down
Loading