@@ -0,0 +1,243 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Routing (management) base class
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Routing
|
||||
*/
|
||||
|
||||
class Ai1ec_Router extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
private $at_least_one_filter_set_in_request;
|
||||
|
||||
/**
|
||||
* @var string Calendar base url
|
||||
*/
|
||||
protected $_calendar_base = null;
|
||||
|
||||
/**
|
||||
* @var string Base URL of WP installation
|
||||
*/
|
||||
protected $_site_url = NULL;
|
||||
|
||||
/**
|
||||
* @var Ai1ec_Adapter_Query_Interface Query manager object
|
||||
*/
|
||||
protected $_query_manager = null;
|
||||
|
||||
/**
|
||||
* @var Ai1ec_Cookie_Present_Dto
|
||||
*/
|
||||
protected $cookie_set_dto;
|
||||
|
||||
/**
|
||||
* @var array Rewrite structure.
|
||||
*/
|
||||
protected $_rewrite = null;
|
||||
|
||||
/**
|
||||
* Check if at least one filter is set in the request
|
||||
*
|
||||
* @param array $view_args
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_at_least_one_filter_set_in_request( array $view_args ) {
|
||||
if ( null === $this->at_least_one_filter_set_in_request ) {
|
||||
$filter_set = false;
|
||||
$ai1ec_settings = $this->_registry->get( 'model.settings' );
|
||||
// check if something in the filters is set
|
||||
$types = apply_filters(
|
||||
'ai1ec_filter_types',
|
||||
Ai1ec_Cookie_Utility::$types
|
||||
);
|
||||
foreach ( $types as $type ) {
|
||||
if (
|
||||
! is_array( $type ) &&
|
||||
isset( $view_args[$type] ) &&
|
||||
! empty( $view_args[$type] )
|
||||
) {
|
||||
$filter_set = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// check if the default view is set
|
||||
$mode = wp_is_mobile() ? '_mobile' : '';
|
||||
$setting = 'default_calendar_view' . $mode;
|
||||
if ( $ai1ec_settings->get( $setting ) !== $view_args['action'] ) {
|
||||
$filter_set = true;
|
||||
}
|
||||
$this->at_least_one_filter_set_in_request = $filter_set;
|
||||
}
|
||||
return $this->at_least_one_filter_set_in_request;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the $cookie_set_dto
|
||||
*/
|
||||
public function get_cookie_set_dto() {
|
||||
$utility = $this->_registry->get( 'cookie.utility' );
|
||||
if( null === $this->cookie_set_dto ) {
|
||||
$this->cookie_set_dto = $utility->is_cookie_set_for_current_page();
|
||||
}
|
||||
return $this->cookie_set_dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Ai1ec_Cookie_Present_Dto $cookie_set_dto
|
||||
*/
|
||||
public function set_cookie_set_dto( Ai1ec_Cookie_Present_Dto $cookie_set_dto = null ) {
|
||||
$this->cookie_set_dto = $cookie_set_dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set base (AI1EC) URI
|
||||
*
|
||||
* @param string $url Base URI (i.e. http://www.example.com/calendar)
|
||||
*
|
||||
* @return Ai1ec_Router Object itself
|
||||
*/
|
||||
public function asset_base( $url ) {
|
||||
$this->_calendar_base = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get base URL of WP installation
|
||||
*
|
||||
* @return string URL where WP is installed
|
||||
*/
|
||||
public function get_site_url() {
|
||||
if ( null === $this->_site_url ) {
|
||||
$this->_site_url = ai1ec_site_url();
|
||||
}
|
||||
return $this->_site_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate (update) URI
|
||||
*
|
||||
* @param array $arguments List of arguments to inject into AI1EC group
|
||||
* @param string $page Page URI to modify
|
||||
*
|
||||
* @return string Generated URI
|
||||
*/
|
||||
public function uri( array $arguments, $page = NULL ) {
|
||||
if ( NULL === $page ) {
|
||||
$page = $this->_calendar_base;
|
||||
}
|
||||
$uri_parser = new Ai1ec_Uri();
|
||||
$parsed_url = $uri_parser->parse( $page );
|
||||
$parsed_url['ai1ec'] = array_merge(
|
||||
$parsed_url['ai1ec'],
|
||||
$arguments
|
||||
);
|
||||
$result_uri = $uri_parser->write( $parsed_url );
|
||||
|
||||
return $result_uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Properly capitalize encoded URL sequence.
|
||||
*
|
||||
* @param string $url Original URL to use.
|
||||
*
|
||||
* @return string Modified URL.
|
||||
*/
|
||||
protected function _fix_encoded_uri( $url ) {
|
||||
$particles = preg_split(
|
||||
'|(%[a-f0-9]{2})|',
|
||||
$url,
|
||||
-1,
|
||||
PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY
|
||||
);
|
||||
$state = false;
|
||||
$output = '';
|
||||
foreach ( $particles as $particle ) {
|
||||
if ( '%' === $particle ) {
|
||||
$state = true;
|
||||
} else {
|
||||
if ( ! $state && '%' === $particle{0} ) {
|
||||
$particle = strtoupper( $particle );
|
||||
}
|
||||
$state = false;
|
||||
}
|
||||
$output .= $particle;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register rewrite rule to enable work with pretty URIs
|
||||
*/
|
||||
public function register_rewrite( $rewrite_to ) {
|
||||
if (
|
||||
! $this->_calendar_base &&
|
||||
! $this->_query_manager->rewrite_enabled()
|
||||
) {
|
||||
return $this;
|
||||
}
|
||||
$base = basename( $this->_calendar_base );
|
||||
if ( false !== strpos( $base, '?' ) ) {
|
||||
return $this;
|
||||
}
|
||||
$base = $this->_fix_encoded_uri( $base );
|
||||
$base = '(?:.+/)?' . $base;
|
||||
$named_args = str_replace(
|
||||
'[:DS:]',
|
||||
preg_quote( Ai1ec_Uri::DIRECTION_SEPARATOR ),
|
||||
'[a-z][a-z0-9\-_[:DS:]\/]*[:DS:][a-z0-9\-_[:DS:]\/]'
|
||||
);
|
||||
|
||||
$regexp = $base . '(\/' . $named_args . ')';
|
||||
$clean_base = trim( $this->_calendar_base, '/' );
|
||||
$clean_site = trim( $this->get_site_url(), '/' );
|
||||
if ( 0 === strcmp( $clean_base, $clean_site ) ) {
|
||||
$regexp = '(' . $named_args . ')';
|
||||
$rewrite_to = remove_query_arg( 'pagename', $rewrite_to );
|
||||
}
|
||||
$this->_query_manager->register_rule(
|
||||
$regexp,
|
||||
$rewrite_to
|
||||
);
|
||||
$this->_rewrite = array(
|
||||
'mask' => $regexp,
|
||||
'target' => $rewrite_to,
|
||||
);
|
||||
add_filter(
|
||||
'rewrite_rules_array',
|
||||
array( $this, 'rewrite_rules_array' )
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate internal variables
|
||||
*/
|
||||
public function __construct( Ai1ec_Registry_Object $registry ) {
|
||||
parent::__construct( $registry );
|
||||
$this->_query_manager = $registry->get( 'query.helper' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if calendar rewrite rule is registered.
|
||||
*
|
||||
* @param array $rules Rewrite rules.
|
||||
*
|
||||
* @return array Rewrite rules.
|
||||
*/
|
||||
public function rewrite_rules_array( $rules ) {
|
||||
if ( null !== $this->_rewrite ) {
|
||||
$newrules[$this->_rewrite['mask']] = $this->_rewrite['target'];
|
||||
$rules = array_merge( $newrules, $rules );
|
||||
}
|
||||
return $rules;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class to aid WP URI management
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Routing
|
||||
*/
|
||||
class Ai1ec_Wp_Uri_Helper {
|
||||
|
||||
/**
|
||||
* Redirect to local site page
|
||||
*
|
||||
* Local redirect restricts redirection to parts of the same site. Primary
|
||||
* use for this is in post-submit actions, when form is submitted to point
|
||||
* user back to submission page and clear status from browser, which might
|
||||
* cause issues, such as double submission, with users clicking refresh on
|
||||
* target page.
|
||||
*
|
||||
* @param string $target_uri URI to redirect to (must be local site)
|
||||
* @param array $extra Extra arguments to add to query [optional]
|
||||
* @param int $status HTTP redirect status [optional=302]
|
||||
*
|
||||
* @return void Method does not return. It perform implicit `exit` to
|
||||
* protect against further processing
|
||||
*/
|
||||
static public function local_redirect(
|
||||
$target_uri,
|
||||
array $extra = array(),
|
||||
$status = 302
|
||||
) {
|
||||
$target_uri = esc_url_raw( add_query_arg( $extra, $target_uri ) );
|
||||
wp_safe_redirect( $target_uri, $status );
|
||||
exit( 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a URI, extracts pagebase value, as used in `index.php?pagebase={arg}`
|
||||
* when matching rewrites.
|
||||
* It may, optionally, provide arguments from URI to append to query string.
|
||||
* This is indicated via setting {@see $qsa} to non-`false` value.
|
||||
*
|
||||
* @param string $uri URI to parse pagebase value from
|
||||
* @param string $qsa Separator to use to append query arguments [optional]
|
||||
*
|
||||
* @return string Parsed URL
|
||||
*/
|
||||
static public function get_pagebase( $uri, $qsa = false ) {
|
||||
$parsed = parse_url( $uri );
|
||||
if ( empty( $parsed ) ) {
|
||||
return '';
|
||||
}
|
||||
$output = '';
|
||||
if ( isset( $parsed['path'] ) ) {
|
||||
$output = basename( $parsed['path'] );
|
||||
}
|
||||
if ( isset( $parsed['query'] ) && false !== $qsa ) {
|
||||
$output .= $qsa . $parsed['query'];
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the calendar pagebase with the full url but without the language.
|
||||
*
|
||||
* @param string $uri
|
||||
* @param string $lang
|
||||
* @return string
|
||||
*/
|
||||
static public function get_pagebase_for_links( $uri, $lang ) {
|
||||
if( empty( $lang ) ) {
|
||||
return $uri;
|
||||
}
|
||||
if( false !== strpos( $uri, '&lang=' ) ) {
|
||||
return str_replace( '&lang=' . $lang, '' , $uri );
|
||||
}
|
||||
if( false !== strpos( $uri, '?lang=' ) ) {
|
||||
return str_replace( '?lang=' . $lang, '' , $uri );
|
||||
}
|
||||
return $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the currently requested URL.
|
||||
*
|
||||
* @return string Canonical URL, that is currently requested
|
||||
*/
|
||||
static public function get_current_url( $skip_port = false ) {
|
||||
$page_url = 'http';
|
||||
if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) {
|
||||
$page_url .= 's';
|
||||
}
|
||||
$page_url .= '://';
|
||||
if ( isset( $_SERVER['SERVER_NAME'] ) && isset( $_SERVER['SERVER_PORT'] ) && isset( $_SERVER['REQUEST_URI'] ) ) {
|
||||
if ( $_SERVER['SERVER_PORT'] !== '80' && true !== $skip_port ) {
|
||||
$page_url .= $_SERVER['SERVER_NAME'] . ':' .
|
||||
$_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
|
||||
} else {
|
||||
$page_url .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
|
||||
}
|
||||
}
|
||||
return $page_url;
|
||||
}
|
||||
|
||||
}
|
||||
228
wp-content/plugins/all-in-one-event-calendar/lib/routing/uri.php
Normal file
228
wp-content/plugins/all-in-one-event-calendar/lib/routing/uri.php
Normal file
@@ -0,0 +1,228 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* URI management class
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Routing
|
||||
*/
|
||||
class Ai1ec_Uri
|
||||
{
|
||||
|
||||
/**
|
||||
* Set direction separator
|
||||
*/
|
||||
const DIRECTION_SEPARATOR = '~';
|
||||
|
||||
/**
|
||||
* @var string Arguments (list) separator
|
||||
*/
|
||||
protected $_arguments_separator = '/';
|
||||
|
||||
/**
|
||||
* Parse page to internal URI representation
|
||||
*
|
||||
* @param string $page Page URI to parse
|
||||
*
|
||||
* @return array|bool Parsed URI or false on failure
|
||||
*/
|
||||
public function parse( $page ) {
|
||||
$result = array(
|
||||
'base' => '',
|
||||
'ai1ec' => array(),
|
||||
'args' => null,
|
||||
'hash' => null,
|
||||
'_type' => 'args', // enum: args,hash,pretty
|
||||
);
|
||||
if ( false === ( $parsed = parse_url( $page ) ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( isset( $parsed['scheme'] ) ) {
|
||||
if ( !isset( $parsed['host'] ) ) {
|
||||
return false;
|
||||
}
|
||||
$result['base'] = $parsed['scheme'] . '://' . $parsed['host'];
|
||||
}
|
||||
|
||||
if ( ! empty( $parsed['path'] ) ) {
|
||||
if ( false !== strpos( $parsed['path'], self::DIRECTION_SEPARATOR ) ) {
|
||||
$result['_type'] = 'pretty';
|
||||
$elements = explode(
|
||||
$this->_arguments_separator,
|
||||
$parsed['path']
|
||||
);
|
||||
foreach ( $elements as $particle ) {
|
||||
$sep = strpos( $particle, self::DIRECTION_SEPARATOR );
|
||||
if ( false !== $sep ) {
|
||||
$key = substr( $particle, 0, $sep );
|
||||
$result['ai1ec'][$key] = substr( $particle, $sep + 1 );
|
||||
} else {
|
||||
$result['base'] .= $this->_arguments_separator .
|
||||
$particle;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$query_pos = array(
|
||||
'query' => 'args',
|
||||
'fragment' => 'hash',
|
||||
);
|
||||
foreach ( $query_pos as $source => $destination ) {
|
||||
if ( 'hash' === $destination ) {
|
||||
$result['_type'] = $destination;
|
||||
}
|
||||
if ( ! empty( $parsed[$source] ) ) {
|
||||
$result = Ai1ec_Utility_Array::deep_merge(
|
||||
$result,
|
||||
$this->parse_query_str( $parsed[$source], $destination )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce query to use in URI
|
||||
*
|
||||
* @param array $parsed Internal query representation
|
||||
*
|
||||
* @return string Normalized URI
|
||||
*/
|
||||
public function write( array $parsed ) {
|
||||
$uri = $parsed['base'];
|
||||
|
||||
if ( 'pretty' == $parsed['_type'] ) {
|
||||
$uri = rtrim( $uri, $this->_arguments_separator );
|
||||
foreach ( $parsed['ai1ec'] as $key => $value ) {
|
||||
$uri .= $this->_arguments_separator .
|
||||
$this->_clean_key( $key ) .
|
||||
self::DIRECTION_SEPARATOR .
|
||||
$this->_safe_uri_value( $value );
|
||||
}
|
||||
} else {
|
||||
$place = $parsed['_type'];
|
||||
$action = isset( $parsed['ai1ec']['action'] )
|
||||
? $parsed['ai1ec']['action']
|
||||
: NULL;
|
||||
if ( empty( $action ) ) {
|
||||
foreach ( array( 'args', 'hash' ) as $place ) {
|
||||
if ( ! empty( $parsed[$place]['action'] ) ) {
|
||||
$action = $parsed[$place]['action'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( empty( $action ) ) {
|
||||
$action = 'uri_err';
|
||||
}
|
||||
if ( 0 !== strncmp( $action, 'ai1ec_', 6 ) ) {
|
||||
$action = 'ai1ec_' . $action;
|
||||
}
|
||||
$combined_ai1ec = array();
|
||||
foreach ( $parsed['ai1ec'] as $key => $value ) {
|
||||
$combined_ai1ec[] = $this->_clean_key( $key ) .
|
||||
self::DIRECTION_SEPARATOR . $this->_safe_uri_value( $value );
|
||||
}
|
||||
$combined_ai1ec = implode( '|', $combined_ai1ec );
|
||||
$parsed[$place]['ai1ec'] = $combined_ai1ec;
|
||||
if ( 'hash' === $place ) {
|
||||
$parsed[$place]['action'] = $action;
|
||||
}
|
||||
unset( $combined_ai1ec, $place );
|
||||
}
|
||||
|
||||
$arg_list = array(
|
||||
'args' => '?',
|
||||
'hash' => '#',
|
||||
);
|
||||
|
||||
foreach ( $arg_list as $name => $separator ) {
|
||||
if ( ! empty( $parsed[$name] ) ) {
|
||||
$uri .= $separator . build_query( $parsed[$name] );
|
||||
}
|
||||
}
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert query string to internal representation
|
||||
*
|
||||
* @param string $query Query to parse
|
||||
* @param string $name Positional name (i.e. 'hash')
|
||||
*
|
||||
* @return array Parsed query map with 'ai1ec' and {$name} keys
|
||||
*/
|
||||
public function parse_query_str( $query, $name ) {
|
||||
$result = array(
|
||||
'ai1ec' => array(),
|
||||
$name => array(),
|
||||
);
|
||||
$parsed = null;
|
||||
parse_str( $query, $parsed );
|
||||
foreach ( $parsed as $key => $value ) {
|
||||
if ( 0 === strncmp( $key, 'ai1ec', 5 ) ) {
|
||||
$result['_type'] = $name;
|
||||
if ( ! is_array( $value ) ) {
|
||||
if ( 'ai1ec' === $key ) {
|
||||
$value_list = explode( '|', $value );
|
||||
$value = array();
|
||||
foreach ( $value_list as $entry ) {
|
||||
list( $sub_key, $sub_value ) = explode(
|
||||
self::DIRECTION_SEPARATOR,
|
||||
$entry
|
||||
);
|
||||
$value[$sub_key] = $sub_value;
|
||||
}
|
||||
unset( $value_list );
|
||||
} else {
|
||||
$value = array( substr($key, 6) => $value );
|
||||
}
|
||||
}
|
||||
$result['ai1ec'] = array_merge(
|
||||
$result['ai1ec'],
|
||||
$value
|
||||
);
|
||||
} else {
|
||||
$result[$name][$key] = $value;
|
||||
}
|
||||
}
|
||||
unset( $parsed );
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create URI-safe value (scalar)
|
||||
*
|
||||
* @param mixed $value Value provided for URL
|
||||
*
|
||||
* @return string Value to use in URI
|
||||
*/
|
||||
protected function _safe_uri_value( $value ) {
|
||||
if ( is_array( $value ) ) {
|
||||
$value = implode(',', $value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean AI1EC sub-element key
|
||||
*
|
||||
* @param string $key Key to clean
|
||||
*
|
||||
* @return string Cleaned key to output
|
||||
*/
|
||||
protected function _clean_key( $key ) {
|
||||
if ( 0 === strncmp( $key, 'ai1ec_', 6 ) ) {
|
||||
$key = substr( $key, 6 );
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user