@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* A factory class for events.
|
||||
*
|
||||
* @author Time.ly Network, Inc.
|
||||
* @since 2.0
|
||||
* @package Ai1EC
|
||||
* @subpackage Ai1EC.Factory
|
||||
*/
|
||||
class Ai1ec_Factory_Event extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* @var bool whether the theme is legacy
|
||||
*/
|
||||
protected $_legacy;
|
||||
|
||||
/**
|
||||
* Public constructor
|
||||
*
|
||||
* @param Ai1ec_Registry_Object $registry
|
||||
*/
|
||||
public function __construct( Ai1ec_Registry_Object $registry ) {
|
||||
parent::__construct( $registry );
|
||||
$this->_legacy = $registry->get( 'theme.loader' )->is_legacy_theme();
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method for events
|
||||
*
|
||||
* @param string $data
|
||||
* @param string $instance
|
||||
*
|
||||
* @return Ai1ec_Event
|
||||
*/
|
||||
public function create_event_instance(
|
||||
Ai1ec_Registry_Object $registry,
|
||||
$data = null,
|
||||
$instance = false
|
||||
) {
|
||||
$use_backward_compatibility = $registry->get(
|
||||
'compatibility.check'
|
||||
)->use_backward_compatibility();
|
||||
if (
|
||||
$use_backward_compatibility &&
|
||||
true === $this->_legacy
|
||||
) {
|
||||
return new Ai1ec_Event_Legacy(
|
||||
$registry,
|
||||
$data,
|
||||
$instance
|
||||
);
|
||||
}
|
||||
$class_name = 'Ai1ec_Event';
|
||||
if (
|
||||
$use_backward_compatibility &&
|
||||
'Ai1ec_Event' === $class_name
|
||||
) {
|
||||
$class_name = 'Ai1ec_Event_Compatibility';
|
||||
}
|
||||
return new $class_name(
|
||||
$registry,
|
||||
$data,
|
||||
$instance
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* A factory class for html elements
|
||||
*
|
||||
* @author Time.ly Network, Inc.
|
||||
* @since 2.0
|
||||
* @package Ai1EC
|
||||
* @subpackage Ai1EC.Factory
|
||||
*/
|
||||
class Ai1ec_Factory_Html extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $pretty_permalinks_enabled = false;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $page;
|
||||
|
||||
/**
|
||||
* The contructor method.
|
||||
*
|
||||
* @param Ai1ec_Registry_Object $registry
|
||||
*/
|
||||
public function __construct(
|
||||
Ai1ec_Registry_Object $registry
|
||||
) {
|
||||
parent::__construct( $registry );
|
||||
$app = $registry->get( 'bootstrap.registry.application' );
|
||||
$this->page = $app->get( 'calendar_base_page' );
|
||||
$this->pretty_permalinks_enabled = $app->get( 'permalinks_enabled' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of the class which generates href for links.
|
||||
*
|
||||
* @param array $args
|
||||
* @param string $type
|
||||
*
|
||||
* @return Ai1ec_Href_Helper
|
||||
*/
|
||||
public function create_href_helper_instance( array $args, $type = 'normal' ) {
|
||||
$href = new Ai1ec_Html_Element_Href( $args, $this->page );
|
||||
$href->set_pretty_permalinks_enabled( $this->pretty_permalinks_enabled );
|
||||
switch ( $type ) {
|
||||
case 'category':
|
||||
$href->set_is_category( true );
|
||||
break;
|
||||
case 'tag':
|
||||
$href->set_is_tag( true );
|
||||
break;
|
||||
case 'author':
|
||||
$href->set_is_author( true );
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return $href;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the html element used as the UI control for the datepicker button.
|
||||
* The href must keep only active filters.
|
||||
*
|
||||
* @param array $args Populated args for the view
|
||||
* @param int|string|null $initial_date The datepicker's initially set date
|
||||
* @param string $title Title to display in datepicker button
|
||||
* @param string $title_short Short names in title
|
||||
* @return Ai1ec_Generic_Html_Tag
|
||||
*/
|
||||
public function create_datepicker_link(
|
||||
array $args, $initial_date = null, $title = '', $title_short = ''
|
||||
) {
|
||||
$settings = $this->_registry->get( 'model.settings' );
|
||||
$date_system = $this->_registry->get( 'date.system' );
|
||||
|
||||
$date_format_pattern = $date_system->get_date_pattern_by_key(
|
||||
$settings->get( 'input_date_format' )
|
||||
);
|
||||
|
||||
if ( null === $initial_date ) {
|
||||
// If exact_date argument was provided, use its value to initialize
|
||||
// datepicker.
|
||||
if ( isset( $args['exact_date'] ) &&
|
||||
$args['exact_date'] !== false &&
|
||||
$args['exact_date'] !== null ) {
|
||||
$initial_date = $args['exact_date'];
|
||||
}
|
||||
// Else default to today's date.
|
||||
else {
|
||||
$initial_date = $date_system->current_time();
|
||||
}
|
||||
}
|
||||
// Convert initial date to formatted date if required.
|
||||
if ( Ai1ec_Validation_Utility::is_valid_time_stamp( $initial_date ) ) {
|
||||
$initial_date = $date_system->format_date(
|
||||
$initial_date,
|
||||
$settings->get( 'input_date_format' )
|
||||
);
|
||||
}
|
||||
|
||||
$href_args = array(
|
||||
'action' => $args['action'],
|
||||
'cat_ids' => $args['cat_ids'],
|
||||
'tag_ids' => $args['tag_ids'],
|
||||
'exact_date' => "__DATE__",
|
||||
);
|
||||
$href_args = apply_filters(
|
||||
'ai1ec_date_picker_href_args',
|
||||
$href_args,
|
||||
$args
|
||||
);
|
||||
$data_href = $this->create_href_helper_instance( $href_args );
|
||||
|
||||
$attributes = array(
|
||||
'data-date' => $initial_date,
|
||||
'data-date-format' => $date_format_pattern,
|
||||
'data-date-weekstart' => $settings->get( 'week_start_day' ),
|
||||
'href' => '#',
|
||||
'data-href' => $data_href->generate_href(),
|
||||
'data-lang' => str_replace( '_', '-', get_locale() ),
|
||||
);
|
||||
$loader = $this->_registry->get( 'theme.loader' );
|
||||
$file = $loader->get_file( 'date-icon.png' );
|
||||
|
||||
$args = array(
|
||||
'attributes' => $attributes,
|
||||
'data_type' => $args['data_type'],
|
||||
'icon_url' => $file->get_url(),
|
||||
'text_date' => __( 'Choose a date using calendar', AI1EC_PLUGIN_NAME ),
|
||||
'title' => $title,
|
||||
'title_short' => $title_short,
|
||||
);
|
||||
|
||||
return $loader->get_file( 'datepicker_link.twig', $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a select2 Multiselect.
|
||||
*
|
||||
* @param array $args The arguments for the select.
|
||||
* @param array $options The options of the select
|
||||
* @param array $view_args The args used in the front end.
|
||||
*
|
||||
* @return Ai1ec_File_Twig
|
||||
*
|
||||
* @staticvar $cached_flips Maps of taxonomy identifiers.
|
||||
* @staticvar $checkable_types Map of types and taxonomy identifiers.
|
||||
*/
|
||||
public function create_select2_multiselect(
|
||||
array $args,
|
||||
array $options,
|
||||
array $view_args = null
|
||||
) {
|
||||
// if no data is present and we are in the frontend, return a blank
|
||||
// element.
|
||||
if ( empty( $options ) && null !== $view_args ) {
|
||||
return $this->_registry->get( 'html.element.legacy.blank' );
|
||||
}
|
||||
static $cached_flips = array();
|
||||
|
||||
static $checkable_types = array(
|
||||
'category' => 'cat_ids',
|
||||
'tag' => 'tag_ids',
|
||||
'author' => 'auth_ids',
|
||||
);
|
||||
|
||||
$use_id = isset( $args['use_id'] );
|
||||
$options_to_add = array();
|
||||
foreach ( $options as $term ) {
|
||||
$option_arguments = array();
|
||||
$color = false;
|
||||
if ( $args['type'] === 'category' ) {
|
||||
$color = $this->_registry->get( 'model.taxonomy' )
|
||||
->get_category_color( $term->term_id );
|
||||
}
|
||||
if ( $color ) {
|
||||
$option_arguments['data-color'] = $color;
|
||||
}
|
||||
if ( null !== $view_args ) {
|
||||
// create the href for ajax loading
|
||||
$href = $this->create_href_helper_instance(
|
||||
$view_args,
|
||||
$args['type']
|
||||
);
|
||||
$href->set_term_id( $term->term_id );
|
||||
$option_arguments['data-href'] = $href->generate_href();
|
||||
// check if the option is selected
|
||||
$type_to_check = '';
|
||||
// first let's check the correct type
|
||||
if ( isset( $checkable_types[$args['type']] ) ) {
|
||||
$type_to_check = $checkable_types[$args['type']];
|
||||
}
|
||||
// let's flip the array. Just once for performance sake,
|
||||
// the categories doesn't change in the same request
|
||||
if ( ! isset( $cached_flips[$type_to_check] ) ) {
|
||||
$cached_flips[$type_to_check] = array_flip(
|
||||
$view_args[$type_to_check]
|
||||
);
|
||||
}
|
||||
if ( isset( $cached_flips[$type_to_check][$term->term_id] ) ) {
|
||||
$option_arguments['selected'] = 'selected';
|
||||
}
|
||||
}
|
||||
if ( true === $use_id ) {
|
||||
$options_to_add[] = array(
|
||||
'text' => $term->name,
|
||||
'value' => $term->term_id,
|
||||
'args' => $option_arguments,
|
||||
);
|
||||
} else {
|
||||
$options_to_add[] = array(
|
||||
'text' => $term->name,
|
||||
'value' => $term->name,
|
||||
'args' => $option_arguments,
|
||||
);
|
||||
}
|
||||
}
|
||||
$select2_args = array(
|
||||
'multiple' => 'multiple',
|
||||
'data-placeholder' => $args['placeholder'],
|
||||
'class' => 'ai1ec-select2-multiselect-selector span12'
|
||||
);
|
||||
if ( isset( $args['class'] ) ) {
|
||||
$select2_args['class'] .= ' ' . $args['class'];
|
||||
}
|
||||
$container_class = false;
|
||||
if ( isset( $args['type'] ) ) {
|
||||
$container_class = 'ai1ec-' . $args['type'] . '-filter';
|
||||
}
|
||||
$loader = $this->_registry->get( 'theme.loader' );
|
||||
$select2 = $loader->get_file(
|
||||
'select2_multiselect.twig',
|
||||
array(
|
||||
'name' => $args['name'],
|
||||
'id' => $args['id'],
|
||||
'container_class' => $container_class,
|
||||
'select2_args' => $select2_args,
|
||||
'options' => $options_to_add,
|
||||
),
|
||||
true
|
||||
);
|
||||
return $select2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a select2 input.
|
||||
*
|
||||
* @param array $args The arguments of the input.
|
||||
*
|
||||
* @return Ai1ec_File_Twig
|
||||
*/
|
||||
public function create_select2_input( array $args ) {
|
||||
if( ! isset ( $args['name'] ) ) {
|
||||
$args['name'] = $args['id'];
|
||||
}
|
||||
// Get tags.
|
||||
$tags = get_terms(
|
||||
'events_tags',
|
||||
array(
|
||||
'orderby' => 'name',
|
||||
'hide_empty' => 0,
|
||||
)
|
||||
);
|
||||
|
||||
// Build tags array to pass as JSON.
|
||||
$tags_json = array();
|
||||
foreach ( $tags as $term ) {
|
||||
$tags_json[] = $term->name;
|
||||
}
|
||||
$tags_json = json_encode( $tags_json );
|
||||
$tags_json = _wp_specialchars( $tags_json, 'single', 'UTF-8' );
|
||||
$loader =$this->_registry->get( 'theme.loader' );
|
||||
$select2_args = array(
|
||||
'data-placeholder' => __( 'Tags (optional)', AI1EC_PLUGIN_NAME ),
|
||||
'class' => 'ai1ec-tags-selector span12',
|
||||
'data-ai1ec-tags' => $tags_json
|
||||
);
|
||||
$select2 = $loader->get_file(
|
||||
'select2_input.twig',
|
||||
array(
|
||||
'name' => $args['name'],
|
||||
'id' => $args['id'],
|
||||
'select2_args' => $select2_args,
|
||||
|
||||
),
|
||||
true
|
||||
);
|
||||
return $select2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* A factory class for caching strategy.
|
||||
*
|
||||
* @author Time.ly Network, Inc.
|
||||
* @since 2.0
|
||||
* @package Ai1EC
|
||||
* @subpackage Ai1EC.Factory
|
||||
*/
|
||||
class Ai1ec_Factory_Strategy extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* create_cache_strategy_instance method
|
||||
*
|
||||
* Method to instantiate new cache strategy object
|
||||
*
|
||||
* @param string $cache_dirs Cache directory to use
|
||||
* @param array $skip_small_bits Set to true, to ignore small entities
|
||||
* cache engines, as APC [optional=false]
|
||||
*
|
||||
* @return Ai1ec_Cache_Strategy Instantiated writer
|
||||
*/
|
||||
public function create_cache_strategy_instance(
|
||||
$cache_dirs = null,
|
||||
$skip_small_entities_cache = false
|
||||
) {
|
||||
$engine = null;
|
||||
$name = '';
|
||||
if ( false === $skip_small_entities_cache && Ai1ec_Cache_Strategy_Apc::is_available() ) {
|
||||
$engine = $this->_registry->get( 'cache.strategy.apc' );
|
||||
} else if (
|
||||
false === AI1EC_DISABLE_FILE_CACHE &&
|
||||
null !== $cache_dirs &&
|
||||
$cache_dir = $this->_get_writable_cache_dir( $cache_dirs )
|
||||
) {
|
||||
$engine = $this->_registry->get( 'cache.strategy.file', $cache_dir );
|
||||
} else {
|
||||
$engine = $this->_registry->get(
|
||||
'cache.strategy.db',
|
||||
$this->_registry->get( 'model.option' )
|
||||
);
|
||||
}
|
||||
return $engine;
|
||||
}
|
||||
|
||||
/**
|
||||
* create_persistence_context method
|
||||
*
|
||||
* @param string $key_for_persistance
|
||||
* @param string $cache_dirs
|
||||
* @param bool $skip_small_entities_cache
|
||||
*
|
||||
* @return Ai1ec_Persistence_Context Instance of persistance context
|
||||
*/
|
||||
public function create_persistence_context(
|
||||
$key_for_persistance,
|
||||
$cache_dirs = null,
|
||||
$skip_small_entities_cache = false
|
||||
) {
|
||||
return new Ai1ec_Persistence_Context(
|
||||
$key_for_persistance,
|
||||
$this->create_cache_strategy_instance( $cache_dirs, $skip_small_entities_cache )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a writable directory if possible, falling back on wp_contet dir
|
||||
*
|
||||
* @param array $cache_dirs
|
||||
* @return boolean|string
|
||||
*/
|
||||
protected function _get_writable_cache_dir( $cache_dirs ) {
|
||||
$writable_folder = false;
|
||||
foreach ( $cache_dirs as $cache_dir ) {
|
||||
if ( $this->_is_cache_dir_writable( $cache_dir['path'] ) ) {
|
||||
$writable_folder = $cache_dir;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $writable_folder;
|
||||
}
|
||||
|
||||
/**
|
||||
* _is_cache_dir_writable method
|
||||
*
|
||||
* Check if given cache directory is writable.
|
||||
*
|
||||
* @param string $directory A path to check for writability
|
||||
*
|
||||
* @return bool Writability
|
||||
*/
|
||||
protected function _is_cache_dir_writable( $directory ) {
|
||||
static $cache_directories = array();
|
||||
if ( ! isset( $cache_directories[$directory] ) ) {
|
||||
$cache_directories[$directory] = apply_filters(
|
||||
'ai1ec_is_cache_dir_writable',
|
||||
null,
|
||||
$directory
|
||||
);
|
||||
if ( null === $cache_directories[$directory] ) {
|
||||
$filesystem = $this->_registry->get( 'filesystem.checker' );
|
||||
$cache_directories[$directory] = $filesystem->is_writable(
|
||||
$directory
|
||||
);
|
||||
}
|
||||
}
|
||||
return $cache_directories[$directory];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user