1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
<?php
namespace hji\geofarms\common;
class RewriteRules
{
private static $_instance;
private $queryVars;
private $rewriteRules;
private function __construct()
{
add_filter('query_vars', array($this, 'loadQueryVars'), 0);
add_action('init', array($this, 'loadRewriteRules'), 0);
}
static function getInstance()
{
if (!( self::$_instance instanceof self))
{
self::$_instance = new self();
}
return self::$_instance;
}
function addQueryVars(array $queryVars)
{
$this->queryVars = $queryVars;
}
function loadQueryVars($queryVars)
{
if (is_array($this->queryVars) && !empty($this->queryVars))
{
$queryVars = array_merge($queryVars, $this->queryVars);
}
return $queryVars;
}
function addRewriteRule($regex, $redirect, $after = 'top')
{
$this->rewriteRules[] = array(
'regex' => $regex,
'redirect' => $redirect,
'after' => $after
);
}
function loadRewriteRules()
{
if (is_array($this->rewriteRules) && !empty($this->rewriteRules))
{
foreach($this->rewriteRules as $rule)
{
add_rewrite_rule($rule['regex'], $rule['redirect'], $rule['after']);
}
}
}
function refreshRules()
{
$this->loadRewriteRules();
flush_rewrite_rules();
}
}