Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
naxvog committed Aug 11, 2019
1 parent 62bf188 commit 0817698
Show file tree
Hide file tree
Showing 404 changed files with 37,454 additions and 7 deletions.
10 changes: 4 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
composer.phar
/vendor/

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
.vscode
.env
*.code-workspace
composer.phar
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# wpr-redis
# WPR-Redis
This plugin enables WP Rocket to store its generated cache files in the Redis database instead of the default filesystem storage.

## Dependencies ##
* WP Rocket WordPress Plugin
* Redis Server

## Constant configuration ##
You may use the following constants to configure the plugin. All constants will take preference over the defined settings if there are any.

* `WPR_REDIS_SCHEME` Needs to be set to `unix` to use a unix socket to connect.
* `WPR_REDIS_HOST` For TCP connections this will be the IP or hostname of the server. If you are using a socket connection this will be the socket path. Default: `localhost`
* `WPR_REDIS_PORT` Port to connect to the server using TCP. Default `6379`
* `WPR_REDIS_DB` The Database to be used. Default `0`
* `WPR_REDIS_PWD` Redis server password if needed.
* `WPR_REDIS_SALT` If set all stored keys will be prefixed by this salt. To further differentiate all keys will pre prefixed by their respective database table prefix as well.

12 changes: 12 additions & 0 deletions assets/css/admin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.pill {
border-radius: 5px;
background-color: #000;
padding: 2px 3px;
color: #fff;
}
.pill.pill-active {
background-color: darkgreen;
}
.pill.pill-inactive {
background-color: darkred;
}
18 changes: 18 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(function($){$(function(){
var evt = 'click.wpr_redis',
elm = '.notice-dismiss';
var evt_trigger = function(){
var type = $( this ).closest( '.notice-wpr-redis' ).data( 'id' );
$.ajax(
ajaxurl,
{
type: 'POST',
data: {
action: 'wpr_redis_notice_handler',
id: type,
}
}
);
};
$( document ).off( evt, elm ).on( evt, elm, evt_trigger );
})})(jQuery);
14 changes: 14 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "naxvog/wpr-redis",
"type": "wordpress-plugin",
"authors": [
{
"name": "Oliver Zieschang",
"email": "[email protected]"
}
],
"require": {
"predis/predis": "^1.1",
"yahnis-elsts/plugin-update-checker": "^4.7"
}
}
113 changes: 113 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

162 changes: 162 additions & 0 deletions includes/class-autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php
/**
* @author naxvog <[email protected]>
* @version 1.0.0
*/

defined( '\\ABSPATH' ) || exit;

return new class {
/**
* An associative array where the key is a namespace prefix and the value
* is an array of base directories for classes in that namespace.
*
* @var array
*/
protected $prefixes = [];

protected $class_file_prefix = 'class-';

/**
* Register loader with SPL autoloader stack.
*
* @return void
*/
public function register() {
spl_autoload_register( [ $this, 'load_class' ] );
}

/**
* Adds a base directory for a namespace prefix.
*
* @param string $prefix The namespace prefix.
* @param string $base_dir A base directory for class files in the
* namespace.
* @param bool $prepend If true, prepend the base directory to the stack
* instead of appending it; this causes it to be searched first rather
* than last.
* @return void
*/
public function add_namespace( $prefix, $base_dir, $prepend = false ) {
// normalize namespace prefix
$prefix = trim( $prefix, '\\' ) . '\\';

// normalize the base directory with a trailing separator
$base_dir = rtrim( $base_dir, DIRECTORY_SEPARATOR ) . '/';

// initialize the namespace prefix array
if ( false === isset( $this->prefixes[ $prefix ] ) ) {
$this->prefixes[ $prefix ] = [];
}

// retain the base directory for the namespace prefix
if ( $prepend ) {
array_unshift( $this->prefixes[ $prefix ], $base_dir );
} else {
array_push( $this->prefixes[ $prefix ], $base_dir );
}
}

/**
* Loads the class file for a given class name.
*
* @param string $class The fully-qualified class name.
* @return mixed The mapped file name on success, or boolean false on
* failure.
*/
public function load_class( $class ) {
// the current namespace prefix
$prefix = $class;

// work backwards through the namespace names of the fully-qualified
// class name to find a mapped file name
// phpcs:ignore
while ( false !== ( $pos = strrpos( $prefix, '\\' ) ) ) {
// retain the trailing namespace separator in the prefix
$prefix = substr( $class, 0, $pos + 1 );

// the rest is the relative class name
$relative_class = substr( $class, $pos + 1 );

// try to load a mapped file for the prefix and relative class
$mapped_file = $this->load_mapped_file( $prefix, $relative_class );
if ( $mapped_file ) {
return $mapped_file;
}

// remove the trailing namespace separator for the next iteration
// of strrpos()
$prefix = rtrim( $prefix, '\\' );
}

// never found a mapped file
return false;
}

/**
* Load the mapped file for a namespace prefix and relative class.
*
* @param string $prefix The namespace prefix.
* @param string $relative_class The relative class name.
* @return mixed Boolean false if no mapped file can be loaded, or the
* name of the mapped file that was loaded.
*/
protected function load_mapped_file( $prefix, $relative_class ) {
// are there any base directories for this namespace prefix?
if ( false === isset( $this->prefixes[ $prefix ] ) ) {
return false;
}

// look through base directories for this namespace prefix
foreach ( $this->prefixes[ $prefix ] as $base_dir ) {
// adhere to WordPress guidlines
$relative_class = strtolower( $relative_class );
$relative_class = strtr( $relative_class, '_', '-' );

// replace the namespace prefix with the base directory,
// replace namespace separators with directory separators
// in the relative class name, append with .php
$file = $base_dir
. str_replace( '\\', '/', $relative_class )
. '.php';

// try to apply the general class file prefix
if ( $this->class_file_prefix ) {
$pos = strrpos( $file, '/' );
$filename = $this->class_file_prefix . substr( $file, $pos + 1 );
$file = substr_replace( $file, $filename, $pos + 1 );
}

// allows users to overwrite loaded classes
$file = apply_filters(
'wpr_redis_autoload_class',
$file,
$relative_class,
$prefix
);

// if the mapped file exists, require it
if ( $this->require_file( $file ) ) {
// yes, we're done
return $file;
}
}

// never found it
return false;
}

/**
* If a file exists, require it from the file system.
*
* @param string $file The file to require.
* @return bool True if the file exists, false if not.
*/
protected function require_file( $file ) {
if ( file_exists( $file ) ) {
require_once $file;
return true;
}
return false;
}
};
Loading

0 comments on commit 0817698

Please sign in to comment.