Add upstream plugins

Signed-off-by: Adrian Nöthlich <git@promasu.tech>
This commit is contained in:
2019-10-25 22:42:20 +02:00
parent 5d3c2ec184
commit 290736650a
1186 changed files with 302577 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
<?php
/**
* Feeds (import and export streams) mediator.
*
* @author Time.ly Network Inc.
* @since 2.0
*
* @package AI1EC
* @subpackage AI1EC.Controller
*/
class Ai1ec_Controller_Calendar_Feeds extends Ai1ec_Base {
/**
* @var array Holds the instances of registered plugins.
*/
protected $_plugins = array();
/**
* Add plugin to the internal array.
*
* This assure us that the plugins extends our base abstract class.
*
* @param Ai1ec_Connector_Plugin $plugin Plugin to add.
*
* @return void
*/
public function add_plugin( Ai1ec_Connector_Plugin $plugin ) {
$plugin->initialize_settings_if_not_set();
$this->_plugins[] = $plugin;
}
/**
* Get an instance of a plugin class
*
* @param string $class
* @throws Exception
* @return Ai1ec_Connector_Plugin
*/
public function get_plugin_instance( $class ) {
foreach ( $this->_plugins as $plugin ) {
if( get_class( $plugin ) === $class ) {
return $plugin;
}
}
throw new Exception( "Class not found" );
}
/**
* Give the plugins the possibility to handle data posted in the calendar feeds page
*
* @return void
*/
public function handle_feeds_page_post() {
// Iterate over the plugins and call the methods
foreach ( $this->_plugins as $plugin ) {
$plugin->handle_feeds_page_post();
}
}
/**
* Render the tab header for each plugin
*
* @param $active_feed
* The tab that should be visualized
*/
public function render_tab_headers() {
foreach ( $this->_plugins as $plugin ) {
$plugin->render_tab_header();
}
}
/**
* Render the tab body for each plugin
*
* @param $active_feed
* The tab that should be visualized
*/
public function render_tab_contents() {
foreach ( $this->_plugins as $plugin ) {
$plugin->render_tab_content();
}
}
}

View File

@@ -0,0 +1,62 @@
<?php
/**
* Handles strict_compatibility_content_filtering.
*
* @author Time.ly Network Inc.
* @since 2.1
*
* @package AI1EC
* @subpackage AI1EC.Controller
*/
class Ai1ec_Controller_Content_Filter extends Ai1ec_Base {
/**
* Content filters lib.
* @var Ai1ec_Content_Filters
*/
protected $_content_filter;
/**
* Setting _strict_compatibility_content_filtering.
* @var bool
*/
protected $_strict_compatibility_content_filtering;
/**
* Constructor.
*
* @param Ai1ec_Registry_Object $registry Registry object.
*
* @return void Method does not return.
*/
public function __construct( Ai1ec_Registry_Object $registry ) {
parent::__construct( $registry );
$this->_content_filter = $registry->get( 'content.filter' );
$this->_strict_compatibility_content_filtering =
$registry->get( 'model.settings' )
->get( 'strict_compatibility_content_filtering' );
}
/**
* Clears all the_content filters excluding few defaults.
*
* @return void Method does not return.
*/
public function clear_the_content_filters() {
if ( $this->_strict_compatibility_content_filtering ) {
$this->_content_filter->clear_the_content_filters();
}
}
/**
* Restores the_content filters.
*
* @return void Method does not return.
*/
public function restore_the_content_filters() {
if ( $this->_strict_compatibility_content_filtering ) {
$this->_content_filter->restore_the_content_filters();
}
}
}

View File

@@ -0,0 +1,13 @@
<?php
/**
* The exception thrown when an engine is not set..
*
* @author Time.ly Network Inc.
* @since 2.0
*
* @package AI1EC
* @subpackage AI1EC.Controller.Exception
*/
class Ai1ec_Engine_Not_Set_Exception extends Ai1ec_Exception {
}

View File

@@ -0,0 +1,13 @@
<?php
/**
* The exception thrown when an engine is not set..
*
* @author Time.ly Network Inc.
* @since 2.0
*
* @package AI1EC
* @subpackage AI1EC.Controller.Exception
*/
class Ai1ec_File_Not_Found_Exception extends Ai1ec_Exception {
}

View File

@@ -0,0 +1,119 @@
<?php
/**
* Abstract class for extensions which are sold.
*
* @author Time.ly Network Inc.
* @since 2.0
*
* @package AI1EC
* @subpackage AI1EC.Controller
*/
abstract class Ai1ec_Base_License_Controller extends Ai1ec_Base_Extension_Controller {
/**
* @var string Settings entry name for license key.
*/
protected $_licence;
/**
* @var string Settings entry name for license status output.
*/
protected $_licence_status;
/**
* @var string Licensing API endpoint URI.
*/
protected $_store = 'https://time.ly/';
/**
* Get label to be used for license input field.
*
* @return string Localized label field.
*/
abstract public function get_license_label();
/**
* @param Ai1ec_Registry_Object $registry
*/
public function initialize_licence_actions() {
$this->_register_licence_actions();
$this->_register_licence_fields();
}
/**
* Add the extension tab if not present
*
* @param array $tabs
* @return array
*/
public function add_tabs( array $tabs ) {
if ( ! isset( $tabs['extensions'] ) ) {
$tabs['extensions'] = array(
'name' => Ai1ec_I18n::__( 'Add-ons' ),
'items' => array(),
);
}
return $tabs;
}
/**
* Register action for licences.
*/
protected function _register_licence_actions() {
$dispatcher = $this->_registry->get( 'event.dispatcher' );
// we need the super class so we use get_class()
$class = explode( '_', get_class( $this ) );
$controller = strtolower( end( $class ) );
$dispatcher->register_filter(
'ai1ec_add_setting_tabs',
array( 'controller.' . $controller, 'add_tabs' )
);
}
/**
* Register fields for licence
*/
protected function _register_licence_fields() {
$plugin_id = $this->get_machine_name();
$this->_licence = 'ai1ec_licence_' . $plugin_id;
$this->_licence_status = 'ai1ec_licence_status_' . $plugin_id;
$options = array(
$this->_licence => array(
'type' => 'string',
'version' => $this->get_version(),
'renderer' => array(
'class' => 'input',
'group-class' => 'ai1ec-col-sm-7',
'tab' => 'extensions',
'item' => 'licenses',
'type' => 'normal',
'label' => $this->get_license_label(),
'status' => $this->_licence_status,
),
'default' => '',
),
$this->_licence_status => array(
'type' => 'string',
'version' => $this->get_version(),
'default' => 'invalid',
),
);
$settings = $this->_registry->get( 'model.settings' );
foreach ( $options as $key => $option ) {
$renderer = null;
if ( isset( $option['renderer'] ) ) {
$renderer = $option['renderer'];
}
$settings->register(
$key,
$option['default'],
$option['type'],
$renderer,
$option['version']
);
}
}
}

View File

@@ -0,0 +1,291 @@
<?php
/**
* Basic extension controller.
*
* @author Time.ly Network Inc.
* @since 2.0
*
* @package AI1EC
* @subpackage AI1EC.Controller
*/
abstract class Ai1ec_Base_Extension_Controller {
/**
* @var Ai1ec_Registry_Object
*/
protected $_registry;
/**
* @var array
*/
protected $_settings;
/**
* @var Ai1ec_Registry_Object
*/
protected static $_registry_static;
/**
* @var array
*/
protected static $_settings_static;
/**
* @var array
*/
protected static $_schema;
/**
* Get the long name of the extension
*/
abstract public function get_name();
/**
* Get the machine name of the extension
*/
abstract public function get_machine_name();
/**
* Get the version of the extension
*/
abstract public function get_version();
/**
* Get the name of the main plugin file
*/
abstract public function get_file();
/**
* Add extension specific settings
*/
abstract protected function _get_settings();
/**
* Register action/filters/shortcodes for the extension
*
* @param Ai1ec_Event_Dispatcher $dispatcher
*/
abstract protected function _register_actions(
Ai1ec_Event_Dispatcher $dispatcher
);
/**
* Perform the basic compatibility check.
*
* @param string $ai1ec_version
*
* @return boolean
*/
public function check_compatibility( $ai1ec_version ) {
return version_compare(
$ai1ec_version,
$this->minimum_core_required(),
'>='
);
}
/**
* @return string
*/
public function minimum_core_required() {
return '2.0.7';
}
/**
* Removes options when uninstalling the plugin.
*/
public static function on_uninstall() {
global $wpdb;
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$settings = self::$_registry_static->get( 'model.settings' );
foreach ( self::$_settings_static as $name => $params ) {
$settings->remove_option( $name );
}
$schema = self::$_schema;
foreach ( $schema['tables'] as $table_name ) {
// Delete table events
$wpdb->query( 'DROP TABLE IF EXISTS ' . $table_name );
}
}
/**
* Public constructor
*/
public function __construct() {
global $wpdb;
self::$_schema = $this->_get_schema( $wpdb->prefix );
$settings = $this->_get_settings();
$this->_settings = $settings;
self::$_settings_static = $settings;
}
/**
* initialize the extension.
*/
public function init( Ai1ec_Registry_Object $registry ) {
$this->_registry = $registry;
// static properties are needed as uninstall hook must be static
// http://wpseek.com/register_uninstall_hook/
self::$_registry_static = $registry;
register_deactivation_hook(
$this->get_file(),
array( $this, 'on_deactivation' )
);
$this->_install_schema( $registry );
$this->_register_actions( $registry->get( 'event.dispatcher' ) );
$this->_add_settings( $registry->get( 'model.settings' ) );
$this->_perform_upgrade( $registry );
if ( method_exists( $this, 'initialize_licence_actions' ) ) {
$this->initialize_licence_actions();
}
}
/**
* Hides settings on deactivation.
*/
public function on_deactivation() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
$referer = 'deactivate-plugin_' . $plugin;
// if we are disabling the plugin in the exception handler, this can't be done.
// but i want to disable options
if ( function_exists( '_get_list_table' ) ) {
$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
$action = $wp_list_table->current_action();
if ( 'deactivate-selected' === $action ) {
$referer = 'bulk-plugins';
}
check_admin_referer( $referer );
}
$settings = $this->_registry->get( 'model.settings' );
foreach ( $this->_settings as $name => $params ) {
$settings->hide_option( $name );
}
}
/**
* Show the settings
*/
public function show_settings( Ai1ec_Registry_Object $registry ) {
$settings = $registry->get( 'model.settings' );
foreach ( $this->_settings as $name => $params ) {
if ( isset( $params['renderer'] ) ) {
$settings->show_option( $name, $params['renderer'] );
}
}
$settings->set( 'allow_statistics', true );
}
/**
* If extensions need to add tables, they will need to override the function to add a schema.
*
* @param string $prefix Database prefix to use for table names.
*
* @return array An array with two keys, schema and tables which are used
* for installing and dropping the table.
*/
protected static function _get_schema( $prefix ) {
return array(
'tables' => array(),
'schema' => '',
);
}
/**
* Performe upgarde actions based on extension version
*
* @param Ai1ec_Registry_Object $registry
*/
protected function _perform_upgrade( Ai1ec_Registry_Object $registry ) {
$version_variable = 'ai1ec_' . $this->get_machine_name() .
'_version';
$option = $registry->get( 'model.option' );
$version = $option->get( $version_variable );
if ( $version !== $this->get_version() ) {
$registry->get( 'model.settings' )->perform_upgrade_actions();
$this->_perform_upgrade_actions();
$option->set( $version_variable, $this->get_version(), true );
}
}
/**
* Function called on add on upgrade.
* Can be overridden by add ons for extra behaviour
*/
protected function _perform_upgrade_actions() {
}
/**
* Since the call the to the uninstall hook it's static, if a different behaviour
* is needed also this call must be overridden.
*/
protected function _register_uninstall_hook() {
register_uninstall_hook(
$this->get_file(),
array( get_class( $this ), 'on_uninstall' )
);
}
/**
* Adds extension settings
*
* @param Ai1ec_Settings $settings
*/
protected function _add_settings( Ai1ec_Settings $settings ) {
foreach ( $this->_settings as $name => $params ) {
$renderer = null;
if ( isset( $params['renderer'] ) ) {
$renderer = $params['renderer'];
}
$settings->register(
$name,
$params['value'],
$params['type'],
$renderer,
$this->get_version()
);
}
}
/**
* Check if the schema needs to be updated
*
* @param Ai1ec_Registry_Object $registry
* @throws Ai1ec_Database_Update_Exception
*/
protected function _install_schema( Ai1ec_Registry_Object $registry ) {
$option = $registry->get( 'model.option' );
$schema = self::$_schema;
if (
is_admin() &&
! empty( $schema['schema'] )
) {
$db_version_variable = 'ai1ec_' . $this->get_machine_name() .
'_db_version';
$version = sha1( $schema['schema'] );
if (
$option->get( $db_version_variable ) !== $version
) {
if (
$registry->get( 'database.helper' )->apply_delta(
$schema['schema']
)
) {
$option->set( $db_version_variable, $version );
} else {
throw new Ai1ec_Database_Update_Exception(
'Database upgrade for ' . $this->get_name() .
' failed'
);
}
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,112 @@
<?php
/**
* The controller which handles import/export.
*
* @author Time.ly Network Inc.
* @since 2.0
*
* @package AI1EC
* @subpackage AI1EC.Controller
*/
class Ai1ec_Import_Export_Controller {
/**
* @var array The registered engines.
*/
protected $_engines = array();
/**
* @var Ai1ec_Registry_Object
*/
protected $_registry;
/**
* @var array Import / export params.
*/
protected $_params;
/**
* This controller is instanciated only if we need to import/export something.
*
* When it is instanciated it allows other engines to be injected through a
* filter. If we do not plan to ship core engines, let's skip the
* $core_engines param.
*
* @param Ai1ec_Registry_Object $registry
* @param array $core_engines
* @param array $params
*/
public function __construct(
Ai1ec_Registry_Object $registry,
array $core_engines = array( 'ics', 'api-ics' ),
array $params = array()
) {
$this->_registry = $registry;
$known_engines = apply_filters(
'ai1ec_register_import_export_engines',
$core_engines
);
$this->_params = $params;
foreach ( $known_engines as $engine ) {
$this->register( $engine );
}
}
/**
* Register an import-export engine.
*
* @param string $engine
*/
public function register( $engine ) {
$this->_engines[$engine] = true;
}
/**
* Import events into the calendar.
*
* @param string $engine
* @param array $args
*
* @throws Ai1ec_Engine_Not_Set_Exception If the engine is not set.
* @throws Ai1ec_Parse_Exception If an error happens during parse.
*
* @return int The number of imported events
*/
public function import_events( $engine, array $args ) {
if ( ! isset( $this->_engines[$engine] ) ) {
throw new Ai1ec_Engine_Not_Set_Exception(
'The engine ' . $engine . 'is not registered.'
);
}
// external engines must register themselves into the registry.
$engine = $this->_registry->get( 'import-export.' . $engine );
$exception = null;
try {
return $engine->import( $args );
} catch ( Ai1ec_Parse_Exception $parse_exception ) {
$exception = $parse_exception;
}
throw $exception;
}
/**
* Export the events using the specified engine.
*
* @param string $engine
* @param array $args
*
* @throws Ai1ec_Engine_Not_Set_Exception
*/
public function export_events( $engine, array $args ) {
if ( ! isset( $this->_engines[$engine] ) ) {
throw new Ai1ec_Engine_Not_Set_Exception(
'The engine ' . $engine . 'is not registered.'
);
}
// external engines must register themselves into the registry.
$engine = $this->_registry->get( 'import-export.' . $engine );
return $engine->export( $args, $this->_params );
}
}

View File

@@ -0,0 +1,248 @@
<?php
/**
* Handles Super Widget.
*
* @author Time.ly Network Inc.
* @since 2.0
*
* @package AI1EC
* @subpackage AI1EC.Javascript
*/
class Ai1ec_Controller_Javascript_Widget extends Ai1ec_Base {
const WIDGET_PARAMETER = 'ai1ec_js_widget';
const LEGACY_WIDGET_PARAMETER = 'ai1ec_super_widget';
const WIDGET_JS_CACHE_FILE = '/public/js_cache/ai1ec_js_widget.js';
protected $_widgets = array();
public function add_widget( $widget_id, $widget_class ) {
$this->_widgets[$widget_id] = $widget_class;
}
public function get_widgets() {
return $this->_widgets;
}
/**
* Adds Super Widget JS to admin screen.
*
* @param array $files
* @param string $page_to_load
*
* @return array
*/
public function add_js( array $files, $page_to_load ) {
if ( 'admin_settings.js' === $page_to_load ) {
$files[] = AI1ECSW_PATH . '/public/js/pages/admin_settings.js';
}
return $files;
}
/**
* @param array $data
* @return array
*/
public function add_js_translation( array $data ) {
$data['set_calendar_page'] = __(
'You must choose the Calendar page before using the Super Widget',
AI1EC_PLUGIN_NAME
);
return $data;
}
/**
* Sets the flag to revalidate cached js files on next render.
*/
public function revalidate_cache() {
$this->_registry->get( 'model.option' )->set( 'jswidgetupdated', '0' );
}
/**
* Renders everything that's needed for the embedded widget.
*/
public function render_js_widget() {
if ( isset( $_GET['render'] ) && 'true' === $_GET['render'] ) {
if ( isset( $_GET[self::WIDGET_PARAMETER] ) ){
$widget = $_GET[self::WIDGET_PARAMETER];
} else if ( isset( $_GET[self::LEGACY_WIDGET_PARAMETER] ) ) {
$widget = $_GET[self::LEGACY_WIDGET_PARAMETER];
}
$widget_class = null;
if ( isset( $this->_widgets[$widget] ) ) {
$widget_class = $this->_widgets[$widget];
}
if ( null === $widget_class ) {
return;
}
$widget_instance = $this->_registry->get( $widget_class );
$this->render_content( $widget_instance );
} else {
if (
! $this->_registry->get( 'model.settings' )->get( 'cache_dynamic_js' ) ||
'1' != $this->_registry->get( 'model.option' )->get( 'jswidgetupdated' ) ||
! $this->_registry->get( 'filesystem.checker' )->check_file_exists(
AI1EC_PATH . self::WIDGET_JS_CACHE_FILE,
true
)
) {
$this->render_javascript();
} else {
header(
'Location: '
. plugin_dir_url( 'all-in-one-event-calendar/public/js_cache/.' )
. 'ai1ec_js_widget.js'
);
exit( 0 );
}
}
}
public function render_javascript() {
header( 'Content-Type: application/javascript' );
header(
'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 31536000 ) . ' GMT'
);
header( 'Cache-Control: public, max-age=31536000' );
$jscontroller = $this->_registry->get( 'controller.javascript' );
$css_controller = $this->_registry->get( 'css.frontend' );
$require_main = AI1EC_ADMIN_THEME_JS_PATH . DIRECTORY_SEPARATOR . 'require.js';
$widget_file = AI1EC_PATH . '/public/js/widget/common_widget.js';
$translation = $jscontroller->get_frontend_translation_data();
$page_id = $this->_registry->get(
'model.settings'
)->get( 'calendar_page_id' );
$permalink = get_permalink(
$page_id
);
$full_permalink = $this->_registry->get(
'template.link.helper'
)->get_full_permalink( $page_id );
// load the css to hardcode, saving a call
$css_rules = $css_controller->get_compiled_css();
$css_rules = addslashes( $css_rules );
$translation['permalinks_structure'] = $this->
_registry->get( 'model.option' )->get( 'permalink_structure' );
$translation['calendar_url'] = preg_replace( '/^https?:/', '', $permalink );
$translation['full_calendar_url'] = preg_replace( '/^https?:/', '', $full_permalink );
// Let extensions add their scripts.
// look at Extended Views or Super Widget for examples
$extension_urls = array();
$extension_urls = apply_filters(
'ai1ec_render_js',
$extension_urls,
'ai1ec_widget.js'
);
// Removing http:// or https:// from extension URLs
foreach ( $extension_urls as &$extension_url ) {
$extension_url = preg_replace( '/https?:/', '', $extension_url );
}
$translation['extension_urls'] = $extension_urls;
// the single event page js is loaded dinamically.
$translation['event_page'] = array(
'id' => 'ai1ec_event',
'url' => preg_replace( '/^https?:/', '', AI1EC_URL ) . '/public/js/pages/event.js',
);
$translation_module = $jscontroller->create_require_js_module(
Ai1ec_Javascript_Controller::FRONTEND_CONFIG_MODULE,
$translation
);
// get requirejs
$require = file_get_contents( $require_main );
$main_widget = file_get_contents( $widget_file );
$require_config = $jscontroller->create_require_js_config_object();
$config = $jscontroller->create_require_js_module(
'ai1ec_config',
$jscontroller->get_translation_data()
);
// get jquery
$jquery = $jscontroller->get_jquery_version_based_on_browser(
isset( $_SERVER['HTTP_USER_AGENT'] )
? $_SERVER['HTTP_USER_AGENT']
: ''
);
$domready = $jscontroller->get_module(
'domReady.js'
);
$frontend = $jscontroller->get_module(
'scripts/common_scripts/frontend/common_frontend.js'
);
// compress data if possible
$compatibility_ob = $this->_registry->get( 'compatibility.ob' );
$js = <<<JS
/******** Called once Require.js has loaded ******/
(function() {
var timely_css = document.createElement( 'style' );
timely_css.innerHTML = '$css_rules';
( document.getElementsByTagName( "head" )[0] || document.documentElement ).appendChild( timely_css );
// bring in requires
$require
// make timely global
window.timely = timely;
$require_config
// Load other modules
$translation_module
$config
$jquery
$frontend
// start up the widget
$main_widget
})(); // We call our anonymous function immediately
JS;
$compatibility_ob->gzip_if_possible( $js );
if (
$this->_registry->get( 'model.settings' )->get( 'cache_dynamic_js' ) &&
(
'0' === $this->_registry->get( 'model.option' )->get( 'jswidgetupdated' ) ||
! $this->_registry->get( 'filesystem.checker' )->check_file_exists(
AI1EC_PATH . self::WIDGET_JS_CACHE_FILE,
true
)
)
) {
try {
$js_path = AI1EC_ADMIN_THEME_JS_PATH . DIRECTORY_SEPARATOR;
$js_saved = file_put_contents(
$js_path . '../js_cache/ai1ec_js_widget.js',
$js
);
if ( $js_saved ) {
$this->_registry->get( 'model.option' )->set( 'jswidgetupdated', '1' );
}
} catch ( Exception $e ) {
$this->_registry->get( 'model.settings' )->set( 'cache_dynamic_js', false );
}
}
exit( 0 );
}
public function render_content( Ai1ec_Embeddable $widget_instance ) {
$args = array();
$defaults = $widget_instance->get_js_widget_configurable_defaults();
foreach ( $defaults as $id => $value ) {
if ( isset( $_GET[$id] ) ) {
$args[$id] = $_GET[$id];
}
}
$html = $widget_instance->javascript_widget( $args );
$jsonp = $this->_registry->get( 'http.response.render.strategy.jsonp' );
$jsonp->render(
array(
'data' => array( 'html' => $html )
)
);
}
}

View File

@@ -0,0 +1,917 @@
<?php
/**
* Controller that handles javascript related functions.
*
* @author Time.ly Network Inc.
* @since 2.0
*
* @package AI1EC
* @subpackage AI1EC.Controller
*/
class Ai1ec_Javascript_Controller {
// The js handle used when enqueueing
const JS_HANDLE = 'ai1ec_requirejs';
// The namespace for require.js functions
const REQUIRE_NAMESPACE = 'timely';
// the name of the configuration module for the frontend
const FRONTEND_CONFIG_MODULE = 'ai1ec_calendar';
//the name of the get parameter we use for loading js
const LOAD_JS_PARAMETER = 'ai1ec_render_js';
// just load backend scripts
const LOAD_ONLY_BACKEND_SCRIPTS = 'common_backend';
// just load backend scripts
const LOAD_ONLY_FRONTEND_SCRIPTS = 'common_frontend';
// Are we in the backend
const IS_BACKEND_PARAMETER = 'is_backend';
// Are we on the calendar page
const IS_CALENDAR_PAGE = 'is_calendar_page';
// this is the value of IS_BACKEND_PARAMETER which triggers loading of backend script
const TRUE_PARAM = 'true';
// the javascript file for event page
const EVENT_PAGE_JS = 'event.js';
// the javascript file for calendar page
const CALENDAR_PAGE_JS = 'calendar.js';
// the file for the calendar feedsa page
const CALENDAR_FEEDS_PAGE = 'calendar_feeds.js';
// add new event page js
const ADD_NEW_EVENT_PAGE = 'add_new_event.js';
// event category page js
const EVENT_CATEGORY_PAGE = 'event_category.js';
// less variable editing page
const LESS_VARIBALES_PAGE = 'less_variables_editing.js';
// settings page
const SETTINGS_PAGE = 'admin_settings.js';
//widget creator page
const WIDGET_CREATOR = 'widget-creator.js';
//ticketing page
const TICKETING = 'ticketing.js';
//cache file
const CALENDAR_JS_CACHE_FILE = '/public/js_cache/calendar.js';
/**
* @var Ai1ec_Registry_Object
*/
private $_registry;
/**
* The core js pages to load.
* Used to avoid errors when extensions add pages.
*
* @var array
*/
private $_core_pages = array(
self::CALENDAR_FEEDS_PAGE => true,
self::ADD_NEW_EVENT_PAGE => true,
self::EVENT_CATEGORY_PAGE => true,
self::LESS_VARIBALES_PAGE => true,
self::SETTINGS_PAGE => true,
self::EVENT_PAGE_JS => true,
self::CALENDAR_PAGE_JS => true,
self::WIDGET_CREATOR => true,
self::TICKETING => true,
);
/**
* Holds an instance of the settings object
*
* @var Ai1ec_Settings
*/
private $_settings;
/**
* @var Ai1ec_Locale
*/
private $_locale;
/**
* @var Ai1ec_Scripts
*/
private $_scripts_helper;
/**
* @var Ai1ec_Acl_Aco
*/
private $_aco;
/**
* @var Ai1ec_Template_Link_Helper
*/
private $_template_link_helper;
/**
* @var bool
*/
protected $_frontend_scripts_loaded = false;
/**
* Public constructor.
*
* @param Ai1ec_Registry_Object $registry
*
* @return void
*/
public function __construct( Ai1ec_Registry_Object $registry ) {
$this->_registry = $registry;
$this->_settings = $registry->get( 'model.settings' );
$this->_locale = $registry->get( 'p28n.wpml' );
$this->_aco = $registry->get( 'acl.aco' );
$this->_template_link_helper = $registry->get( 'template.link.helper' );
// this will need to be modified
$this->_scripts_helper = $registry->get( 'script.helper' );
}
/**
* Load javascript files for frontend pages.
*
* @wp-hook ai1ec_load_frontend_js
*
* @param $is_calendar_page boolean Whether we are displaying the main
* calendar page or not
*
* @return void
*/
public function load_frontend_js( $is_calendar_page, $is_shortcode = false ) {
$page = null;
// ======
// = JS =
// ======
if( $this->_are_we_accessing_the_single_event_page() === true ) {
$page = self::EVENT_PAGE_JS;
}
if( $is_calendar_page === true ) {
$page = self::CALENDAR_PAGE_JS;
}
if( null !== $page ) {
$this->add_link_to_render_js( $page, false );
}
}
/**
* Render the javascript for the appropriate page.
*
* @return void
*/
public function render_js() {
$js_path = AI1EC_ADMIN_THEME_JS_PATH . DIRECTORY_SEPARATOR;
$common_js = '';
$js_cache = $this->_settings->get( 'cache_dynamic_js' );
if ( ! isset( $_GET[self::LOAD_JS_PARAMETER] ) ) {
return null;
}
$page_to_load = $_GET[self::LOAD_JS_PARAMETER];
$scripts_updated = $this->_registry->get( 'model.option' )->get( 'calendarjsupdated' );
if (
$js_cache &&
$page_to_load === self::CALENDAR_PAGE_JS &&
'1' === $scripts_updated &&
$this->_registry->get( 'filesystem.checker' )->check_file_exists(
AI1EC_PATH . self::CALENDAR_JS_CACHE_FILE,
true
)
) {
Ai1ec_Http_Response_Helper::stop( 0 );
return;
}
if (
isset( $_GET[self::IS_BACKEND_PARAMETER] ) &&
$_GET[self::IS_BACKEND_PARAMETER] === self::TRUE_PARAM
) {
$common_js = file_get_contents( $js_path . 'pages/common_backend.js' );
} else if (
$page_to_load === self::EVENT_PAGE_JS ||
$page_to_load === self::CALENDAR_PAGE_JS ||
$page_to_load === self::LOAD_ONLY_FRONTEND_SCRIPTS
) {
if (
$page_to_load === self::LOAD_ONLY_FRONTEND_SCRIPTS &&
true === $this->_frontend_scripts_loaded
) {
return;
}
if ( false === $this->_frontend_scripts_loaded ) {
$common_js = file_get_contents(
$js_path . 'pages/common_frontend.js'
);
$this->_frontend_scripts_loaded = true;
}
}
// Create the config object for Require.js.
$require_config = $this->create_require_js_config_object();
// Load Require.js script.
$require = file_get_contents( $js_path . 'require.js' );
// Load appropriate jQuery script based on browser.
$jquery = $this->get_jquery_version_based_on_browser(
isset( $_SERVER['HTTP_USER_AGENT'] )
? $_SERVER['HTTP_USER_AGENT']
: ''
);
// Load the main script for the page.
$page_js = '';
if ( isset( $this->_core_pages[$page_to_load] ) ) {
$page_js = file_get_contents( $js_path . 'pages/' . $page_to_load );
}
// Load translation module.
$translation = $this->get_frontend_translation_data();
$permalink = $this->_template_link_helper
->get_permalink( $this->_settings->get( 'calendar_page_id' ) );
$full_permalink = $this->_template_link_helper
->get_full_permalink( $this->_settings->get( 'calendar_page_id' ) );
$translation['calendar_url'] = $permalink;
$translation['full_calendar_url'] = $full_permalink;
$translation_module = $this->create_require_js_module(
self::FRONTEND_CONFIG_MODULE,
$translation
);
// Load Ai1ec config script.
$config = $this->create_require_js_module(
'ai1ec_config',
$this->get_translation_data()
);
// Let extensions add their scripts.
$extension_files = array();
$extension_files = apply_filters(
'ai1ec_render_js',
$extension_files,
$page_to_load
);
$ext_js = '';
foreach ( $extension_files as $file ) {
$ext_js .= file_get_contents( $file );
}
// Finally, load the page_ready script to execute code that must run after
// all scripts have been loaded.
$page_ready = file_get_contents(
$js_path . 'scripts/common_scripts/page_ready.js'
);
$javascript = $require . $require_config . $translation_module .
$config . $jquery . $common_js . $ext_js . $page_js . $page_ready;
// add to blank spaces to fix issues with js
// being truncated onn some installs
$javascript .= ' ';
if (
$js_cache &&
$page_to_load === self::CALENDAR_PAGE_JS &&
(
'0' === $scripts_updated ||
! $this->_registry->get( 'filesystem.checker' )->check_file_exists(
AI1EC_PATH . self::CALENDAR_JS_CACHE_FILE,
true
)
)
) {
$js_saved = false;
try {
$js_saved = file_put_contents(
$js_path . '../js_cache/' . self::CALENDAR_PAGE_JS,
$javascript
);
if ( $js_saved ) {
$this->_registry->get( 'model.option' )->set( 'calendarjsupdated', '1' );
}
} catch ( Exception $e ) {
$this->_settings->set( 'cache_dynamic_js', false );
}
}
$this->_echo_javascript( $javascript );
}
/**
* Sets the flag to revalidate cached js files on next render.
*/
public function revalidate_cache() {
$this->_registry->get( 'model.option' )->set( 'calendarjsupdated', '0' );
}
/**
* Get a compiled javascript file ( used by extensions )
*
* @param string $name
*
* @return string
*/
public function get_module( $name ) {
$js_path = AI1EC_ADMIN_THEME_JS_PATH . DIRECTORY_SEPARATOR;
return file_get_contents( $js_path . $name );
}
/**
* Check what file needs to be loaded and add the correct link.
*
* @wp-hook init
*
* @return void
*/
public function load_admin_js() {
// Initialize dashboard view
$script_to_load = FALSE;
if ( $this->are_we_on_calendar_feeds_page() === TRUE ) {
// Load script for the importer plugins
$script_to_load[] = self::CALENDAR_FEEDS_PAGE;
}
// Start the scripts for the event category page
if ( $this->_are_we_editing_event_categories() === TRUE ) {
// Load script required when editing categories
$script_to_load[] = self::EVENT_CATEGORY_PAGE;
}
if ( $this->_are_we_editing_less_variables() === TRUE ) {
// Load script required when editing categories
$script_to_load[] = self::LESS_VARIBALES_PAGE;
}
// Load the js needed when you edit an event / add a new event
if (
true === $this->_are_we_creating_a_new_event() ||
true === $this->_are_we_editing_an_event()
) {
// Load script for adding / modifying events
$script_to_load[] = self::ADD_NEW_EVENT_PAGE;
}
if ( true === $this->_are_we_accessing_the_calendar_settings_page() ) {
$script_to_load[] = self::SETTINGS_PAGE;
}
if ( true === $this->_are_we_creating_widgets() ) {
$script_to_load[] = self::WIDGET_CREATOR;
}
if (
true === $this->_are_we_managing_tickets() ||
true === $this->_are_we_managing_events_list()
) {
$script_to_load[] = self::TICKETING;
}
if ( false === $script_to_load ) {
$script_to_load[] = apply_filters( 'ai1ec_backend_js', self::LOAD_ONLY_BACKEND_SCRIPTS );
}
foreach ($script_to_load as $value) {
$this->add_link_to_render_js( $value, true );
}
}
/**
* Loads version 1.9 or 2.0 of jQuery based on user agent.
* If $user_agent is null (due to lack of HTTP header) we always serve
* jQuery 2.0.
*
* @param string $user_agent
*
* @return string
*/
public function get_jquery_version_based_on_browser( $user_agent ) {
$js_path = AI1EC_ADMIN_THEME_JS_PATH . DIRECTORY_SEPARATOR;
$jquery = 'jquery_timely20.js';
preg_match( '/MSIE (.*?);/', $user_agent, $matches );
if ( count( $matches ) > 1 ) {
//Then we're using IE
$version = (int) $matches[1];
if ( $version <= 8 ) {
//IE 8 or under!
$jquery = 'jquery_timely19.js';
}
}
return file_get_contents( $js_path . $jquery );
}
/**
* Creates a requirejs module that can be used for translations
*
* @param string $object_name
* @param array $data
*
* @return string
*/
public function create_require_js_module( $object_name, array $data ) {
foreach ( (array) $data as $key => $value ) {
if ( ! is_scalar( $value ) )
continue;
$data[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
}
$json_data = json_encode( $data );
$prefix = self::REQUIRE_NAMESPACE;
$script = "$prefix.define( '$object_name', $json_data );";
return $script;
}
/**
* Create the array needed for translation and passing other settings to JS.
*
* @return $data array the dynamic data array
*/
public function get_translation_data() {
$force_ssl_admin = force_ssl_admin();
if ( $force_ssl_admin && ! is_ssl() ) {
force_ssl_admin( false );
}
$ajax_url = ai1ec_admin_url( 'admin-ajax.php' );
force_ssl_admin( $force_ssl_admin );
$settings = $this->_registry->get( 'model.settings' );
$locale = $this->_registry->get( 'p28n.wpml' );
$blog_timezone = $this->_registry->get( 'model.option' )
->get( 'gmt_offset' );
$application = $this->_registry->get( 'bootstrap.registry.application' );
$data = array(
'calendar_feeds_nonce' => wp_create_nonce( 'ai1ec_ics_feed_nonce'),
// ICS feed error messages
'duplicate_feed_message' => esc_html(
Ai1ec_I18n::__( 'This feed is already being imported.' )
),
'invalid_url_message' => esc_html(
Ai1ec_I18n::__( 'Please enter a valid iCalendar URL.' )
),
'invalid_website_message' => esc_html(
Ai1ec_I18n::__( 'Please enter a valid Website URL.' )
),
'invalid_registration_message' => esc_html(
Ai1ec_I18n::__( 'Please enter a valid Registration URL, starting with https:// or http://.' )
),
'invalid_email_message' => esc_html(
Ai1ec_I18n::__( 'Please enter a valid email address.' )
),
'choose_image_message' => Ai1ec_I18n::__( 'Choose Image' ),
'now' => $this->_registry->get( 'date.system' )
->current_time(),
'size_less_variable_not_ok' => Ai1ec_I18n::__(
'The value you have entered is not a valid CSS length.'
),
'confirm_reset_theme' => Ai1ec_I18n::__(
'Are you sure you want to reset your theme options to their default values?'
),
'error_message_not_valid_lat' => Ai1ec_I18n::__(
'Please enter a valid latitude. A valid latitude is comprised between +90 and -90.'
),
'error_message_not_valid_long' => Ai1ec_I18n::__(
'Please enter a valid longitude. A valid longitude is comprised between +180 and -180.'
),
'error_message_not_entered_lat' => Ai1ec_I18n::__(
'When the "Input coordinates" checkbox is checked, "Latitude" is a required field.'
),
'error_message_not_entered_long' => Ai1ec_I18n::__(
'When the "Input coordinates" checkbox is checked, "Longitude" is a required field.'
),
'ai1ec_contact_url_not_valid' => Ai1ec_I18n::__(
'The URL you have entered in the <b>Organizer Contact Info</b> &gt; <b>Website URL</b> seems to be invalid.'
),
'ai1ec_ticket_ext_url_not_valid' => Ai1ec_I18n::__(
'The URL you have entered in the <b>Event Cost and Tickets</b> &gt; <b>Tickets or Registration URL</b> seems to be invalid.'
),
'ai1ec_contact_email_not_valid' => Ai1ec_I18n::__(
'The Email you have entered in the <b>Organizer Contact Info</b> &gt; <b>E-mail</b> seems to be invalid.'
),
'general_url_not_valid' => Ai1ec_I18n::__(
'Please remember that URLs must start with either "http://" or "https://".'
),
'calendar_loading' => Ai1ec_I18n::__(
'Loading&hellip;'
),
'ticketing_required_fields' => Ai1ec_I18n::__(
'<b>Required or incorrect fields for Ticketing are outlined red.</b>'
),
'ticketing_repeat_not_supported' => Ai1ec_I18n::__( '<b>The Repeat option was selected but recurrence is not supported by Event with Tickets.</b>'
),
'ticketing_no_tickets_included' => Ai1ec_I18n::__( '<b>
The Event has the cost option Tickets selected but no ticket was included.</b>'
),
'discovery_event_success' => Ai1ec_I18n::__(
'Event was imported successfully.'
),
'discovery_event_error' => Ai1ec_I18n::__(
'An error occurred when importing event. Please, try later.'
),
'language' => $this->_registry->get( 'p28n.wpml' )->get_lang(),
'ajax_url' => $ajax_url,
// 24h time format for time pickers
'twentyfour_hour' => $settings->get( 'input_24h_time' ),
// Date format for date pickers
'date_format' => $settings->get( 'input_date_format' ),
// Names for months in date picker header (escaping is done in wp_localize_script)
'month_names' => $locale->get_localized_month_names(),
// Names for days in date picker header (escaping is done in wp_localize_script)
'day_names' => $locale->get_localized_week_names(),
// Start the week on this day in the date picker
'week_start_day' => $settings->get( 'week_start_day' ),
'week_view_starts_at' => $settings->get( 'week_view_starts_at' ),
'week_view_ends_at' => $settings->get( 'week_view_ends_at' ),
'google_maps_api_key' => $settings->get( 'google_maps_api_key' ),
'blog_timezone' => $blog_timezone,
'affix_filter_menu' => $settings->get( 'affix_filter_menu' ),
'affix_vertical_offset_md' => $settings->get( 'affix_vertical_offset_md' ),
'affix_vertical_offset_lg' => $settings->get( 'affix_vertical_offset_lg' ),
'affix_vertical_offset_sm' => $settings->get( 'affix_vertical_offset_sm' ),
'affix_vertical_offset_xs' => $settings->get( 'affix_vertical_offset_xs' ),
'calendar_page_id' => $settings->get( 'calendar_page_id' ),
'region' => ( $settings->get( 'geo_region_biasing' ) ) ? $locale->get_region() : '',
'site_url' => trailingslashit(
ai1ec_get_site_url()
),
'javascript_widgets' => array(),
'widget_creator' => array(
'preview' => Ai1ec_I18n::__( 'Preview:' ),
'preview_loading' => Ai1ec_I18n::__(
'Loading preview&nbsp;<i class="ai1ec-fa ai1ec-fa-spin ai1ec-fa-spinner"></i>'
)
),
'ticketing' => array(
'details' => Ai1ec_I18n::__( 'Ticketing Details' ),
'hide_details' => Ai1ec_I18n::__( 'Hide Ticketing Details' ),
'loading_details' => Ai1ec_I18n::__( 'Loading tickets details...' ),
'type_and_price' => Ai1ec_I18n::__( 'Type and price' ),
'info' => Ai1ec_I18n::__( 'Info' ),
'information' => Ai1ec_I18n::__( 'Information' ),
'report' => Ai1ec_I18n::__( 'Report' ),
'sale_dates' => Ai1ec_I18n::__( 'Sale dates' ),
'limits' => Ai1ec_I18n::__( 'Limits' ),
'actions' => Ai1ec_I18n::__( 'Actions' ),
'sold' => Ai1ec_I18n::__( 'Sold:' ),
'left' => Ai1ec_I18n::__( 'Left:' ),
'start' => Ai1ec_I18n::__( 'Start:' ),
'end' => Ai1ec_I18n::__( 'End:' ),
'min' => Ai1ec_I18n::__( 'Min:' ),
'max' => Ai1ec_I18n::__( 'Max:' ),
'attendees' => Ai1ec_I18n::__( 'Attendees' ),
'hide_attendees' => Ai1ec_I18n::__( 'Hide Attendees' ),
'attendees_list' => Ai1ec_I18n::__( 'Attendees List' ),
'guest_name' => Ai1ec_I18n::__( 'Guest Name' ),
'status' => Ai1ec_I18n::__( 'Status' ),
'email' => Ai1ec_I18n::__( 'Email' ),
'no_attendees' => Ai1ec_I18n::__( 'No attendees for this ticket type.' ),
'edit' => Ai1ec_I18n::__( 'Edit' ),
'code' => Ai1ec_I18n::__( 'Code' ),
'unlimited' => Ai1ec_I18n::__( 'Unlimited' ),
'open_for_sale' => Ai1ec_I18n::__( 'Open for sale' ),
'no_delete_text' => Ai1ec_I18n::__( 'You have sold tickets for this ticket type. Please change it\'s status to "Canceled" and make refunds to all users that purchased tickets.' ),
'cancel_message' => Ai1ec_I18n::__( 'You have sold tickets for this ticket type. Please make refunds to all users that purchased tickets' )
),
'review' => array(
'message_sent' => Ai1ec_I18n::__( 'Your message has been sent. Thank you for your feedback.' ),
'message_error' => Ai1ec_I18n::__( 'Your message has not been sent. Please try again or contact us.' )
),
'load_views_error' => Ai1ec_I18n::__(
'Something went wrong while fetching events.<br>The request status is: %STATUS% <br>The error thrown was: %ERROR%'
),
'load_views_error_popup_title' => Ai1ec_I18n::__( 'Response text received from server' ),
'load_views_error_link_popup' => Ai1ec_I18n::__( 'Click here for technical details' ),
'cookie_path' => $this->_registry->get(
'cookie.utility'
)->get_path_for_cookie(),
'disable_autocompletion' => $settings->get( 'disable_autocompletion' ),
'end_must_be_after_start' => __( 'The end date can\'t be earlier than the start date.', AI1EC_PLUGIN_NAME ),
'show_at_least_six_hours' => __( 'For week and day view, you must select an interval of at least 6 hours.', AI1EC_PLUGIN_NAME ),
'ai1ec_permalinks_enabled' => $application->get( 'permalinks_enabled' ),
);
return apply_filters( 'ai1ec_js_translations', $data );
}
/**
* Get the array with translated data for the frontend
*
* @return array
*/
public function get_frontend_translation_data() {
$data = array(
'export_url' => AI1EC_EXPORT_URL,
);
// Replace desired CSS selector with calendar, if selector has been set
$calendar_selector = $this->_settings->get( 'calendar_css_selector' );
if( $calendar_selector ) {
$page = get_post(
$this->_settings->get( 'calendar_page_id' )
);
$data['selector'] = $calendar_selector;
$data['title'] = $page->post_title;
}
// DEPRECATED: Only still here for backwards compatibility with Ai1ec 1.x.
$data['fonts'] = array();
$fonts_dir = AI1EC_DEFAULT_THEME_URL . 'font_css/';
$data['fonts'][] = array(
'name' => 'League Gothic',
'url' => $fonts_dir . 'font-league-gothic.css',
);
$data['fonts'][] = array(
'name' => 'fontawesome',
'url' => $fonts_dir . 'font-awesome.css',
);
return $data;
}
/**
* Echoes the Javascript if not cached.
*
* Echoes the javascript with the correct content.
* Since the content is dinamic, i use the hash function.
*
* @param string $javascript
*
* @return void
*/
private function _echo_javascript( $javascript ) {
$conditional_get = new HTTP_ConditionalGet( array(
'contentHash' => md5( $javascript )
)
);
$conditional_get->sendHeaders();
if ( ! $conditional_get->cacheIsValid ) {
$http_encoder = $this->_registry->get(
'http.encoder',
array(
'content' => $javascript,
'type' => 'text/javascript'
)
);
$compression_level = null;
if ( $this->_registry->get( 'model.settings' )->get( 'disable_gzip_compression' ) ) {
// set the compression level to 0 to disable it.
$compression_level = 0;
}
$http_encoder->encode( $compression_level );
$http_encoder->sendAll();
}
Ai1ec_Http_Response_Helper::stop( 0 );
}
/**
* Create the config object for requirejs.
*
* @return string
*/
public function create_require_js_config_object() {
$js_url = AI1EC_ADMIN_THEME_JS_URL;
$version = AI1EC_VERSION;
$namespace = self::REQUIRE_NAMESPACE;
$config = <<<JSC
$namespace.require.config( {
waitSeconds : 15,
urlArgs : 'ver=$version',
baseUrl : '$js_url'
} );
JSC;
return $config;
}
/**
* Check if we are in the calendar feeds page
*
* @return boolean TRUE if we are in the calendar feeds page FALSE otherwise
*/
public function are_we_on_calendar_feeds_page() {
if ( !isset( $_SERVER['SCRIPT_NAME'] ) ) {
return FALSE;
}
$path_details = pathinfo( $_SERVER['SCRIPT_NAME'] );
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : FALSE;
$page = isset( $_GET['page'] ) ? $_GET['page'] : FALSE;
if( $post_type === FALSE || $page === FALSE ) {
return FALSE;
}
$is_calendar_feed_page = $path_details['basename'] === 'edit.php' &&
$post_type === 'ai1ec_event' &&
$page === 'all-in-one-event-calendar-feeds';
return $is_calendar_feed_page;
}
/**
* Add the link to render the javascript
*
* @param string $page
* @param boolean $backend
*
* @return void
*/
public function add_link_to_render_js( $page, $backend ) {
$load_backend_script = 'false';
if ( true === $backend ) {
$load_backend_script = self::TRUE_PARAM;
}
$is_calendar_page = false;
if(
true === is_page( $this->_settings->get( 'calendar_page_id' ) ) ||
self::CALENDAR_PAGE_JS === $page
) {
$is_calendar_page = self::TRUE_PARAM;
}
$url = add_query_arg(
array(
// Add the page to load
self::LOAD_JS_PARAMETER => $page,
// If we are in the backend, we must load the common scripts
self::IS_BACKEND_PARAMETER => $load_backend_script,
// If we are on the calendar page we must load the correct option
self::IS_CALENDAR_PAGE => $is_calendar_page,
),
trailingslashit( ai1ec_get_site_url() )
);
if (
$this->_settings->get( 'cache_dynamic_js' ) &&
$is_calendar_page &&
'1' === $this->_registry->get( 'model.option' )->get( 'calendarjsupdated' ) &&
$this->_registry->get( 'filesystem.checker' )->check_file_exists(
AI1EC_PATH . self::CALENDAR_JS_CACHE_FILE,
true
)
) {
$url = plugin_dir_url( 'all-in-one-event-calendar/public/js_cache/.' ) . $page;
}
if ( true === $backend ) {
$this->_scripts_helper->enqueue_script(
self::JS_HANDLE,
$url,
array( 'postbox' ),
true
);
} else {
$this->_scripts_helper->enqueue_script(
self::JS_HANDLE,
$url,
array(),
false
);
}
}
/**
* check if we are editing an event
*
* @return boolean TRUE if we are editing an event FALSE otherwise
*/
private function _are_we_editing_an_event() {
if ( !isset( $_SERVER['SCRIPT_NAME'] ) ) {
return FALSE;
}
$path_details = pathinfo( $_SERVER['SCRIPT_NAME'] );
$post_id = isset( $_GET['post'] ) ? $_GET['post'] : FALSE;
$action = isset( $_GET['action'] ) ? $_GET['action'] : FALSE;
if( $post_id === FALSE || $action === FALSE ) {
return FALSE;
}
$editing = (
'post.php' === $path_details['basename'] &&
'edit' === $action &&
$this->_aco->is_our_post_type( $post_id )
);
return $editing;
}
/**
* check if we are viewing events list
*
* @return boolean TRUE if we are on the events list FALSE otherwise
*/
private function _are_we_managing_events_list() {
if ( !isset( $_SERVER['SCRIPT_NAME'] ) ) {
return FALSE;
}
$path_details = pathinfo( $_SERVER['SCRIPT_NAME'] );
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : FALSE;
if ( FALSE === $post_type ) {
return FALSE;
}
$page = isset( $_GET['page'] ) ? $_GET['page'] : '';
$events_list = (
'edit.php' === $path_details['basename'] &&
'ai1ec_event' === $post_type &&
ai1ec_is_blank( $page )
);
return $events_list;
}
/**
* check if we are creating a new event
*
* @return boolean TRUE if we are creating a new event FALSE otherwise
*/
private function _are_we_creating_a_new_event() {
if ( !isset( $_SERVER['SCRIPT_NAME'] ) ) {
return FALSE;
}
$path_details = pathinfo( $_SERVER['SCRIPT_NAME'] );
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
return $path_details['basename'] === 'post-new.php' &&
$post_type === AI1EC_POST_TYPE;
}
/**
* Check if we are accessing the settings page
*
* @return boolean TRUE if we are accessing the settings page FALSE otherwise
*/
private function _are_we_accessing_the_calendar_settings_page() {
if ( !isset( $_SERVER['SCRIPT_NAME'] ) ) {
return FALSE;
}
$path_details = pathinfo( $_SERVER['SCRIPT_NAME'] );
$page = isset( $_GET['page'] ) ? $_GET['page'] : '';
return $path_details['basename'] === 'edit.php' &&
$page === AI1EC_PLUGIN_NAME . '-settings';
}
protected function _are_we_creating_widgets() {
if ( !isset( $_SERVER['SCRIPT_NAME'] ) ) {
return FALSE;
}
$path_details = pathinfo( $_SERVER['SCRIPT_NAME'] );
$page = isset( $_GET['page'] ) ? $_GET['page'] : '';
return $path_details['basename'] === 'edit.php' &&
$page === AI1EC_PLUGIN_NAME . '-widget-creator';
}
protected function _are_we_managing_tickets() {
if ( !isset( $_SERVER['SCRIPT_NAME'] ) ) {
return FALSE;
}
$path_details = pathinfo( $_SERVER['SCRIPT_NAME'] );
$page = isset( $_GET['page'] ) ? $_GET['page'] : '';
return $path_details['basename'] === 'edit.php' &&
$page === AI1EC_PLUGIN_NAME . '-tickets';
}
/**
* Check if we are editing less variables
*
* @return boolean TRUE if we are accessing a single event page FALSE otherwise
*/
private function _are_we_editing_less_variables() {
if ( !isset( $_SERVER['SCRIPT_NAME'] ) ) {
return FALSE;
}
$path_details = pathinfo( $_SERVER['SCRIPT_NAME'] );
$page = isset( $_GET['page'] ) ? $_GET['page'] : '';
return $path_details['basename'] === 'edit.php' && $page === AI1EC_PLUGIN_NAME . '-edit-css';
}
/**
* Check if we are accessing the events category page
*
* @return boolean TRUE if we are accessing the events category page FALSE otherwise
*/
private function _are_we_editing_event_categories() {
if ( !isset( $_SERVER['SCRIPT_NAME'] ) ) {
return FALSE;
}
$path_details = pathinfo( $_SERVER['SCRIPT_NAME'] );
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
return (
$path_details['basename'] === 'edit-tags.php' ||
$path_details['basename'] === 'term.php'
) && $post_type === AI1EC_POST_TYPE;
}
/**
* Check if we are accessing a single event page
*
* @return boolean TRUE if we are accessing a single event page FALSE otherwise
*/
private function _are_we_accessing_the_single_event_page() {
return $this->_aco->is_our_post_type();
}
}

View File

@@ -0,0 +1,105 @@
<?php
/**
* Controller that handles shutdown functions.
*
* @author Time.ly Network Inc.
* @since 2.0
*
* @package AI1EC
* @subpackage AI1EC.Controller
*/
class Ai1ec_Shutdown_Controller {
/**
* @var array Map of object names and class names they represent to preserve
*/
protected $_preserve = array(
'wpdb' => 'wpdb',
'wp_object_cache' => 'WP_Object_Cache',
);
/**
* @var array Map of object names and their representation from global scope
*/
protected $_restorables = array();
/**
* @var array List of callbacks to be executed during shutdown sequence
*/
protected $_callbacks = array();
/**
* Destructor
*
* Here processing of globals is made - values are replaced, callbacks
* are executed and globals are restored to the previous state.
*
* @return void Destructor does not return
*/
public function __destruct() {
// replace globals from our internal store
$restore = array();
foreach ( $this->_preserve as $name => $class ) {
if (
! isset( $GLOBALS[$name] ) ||
! ( $GLOBALS[$name] instanceof $class )
) {
$restore[$name] = NULL;
if ( isset( $GLOBALS[$name] ) ) {
$restore[$name] = $GLOBALS[$name];
}
$GLOBALS[$name] = $this->_restorables[$name];
}
}
// execute callbacks
foreach ( $this->_callbacks as $callback ) {
call_user_func( $callback );
}
// restore globals to previous state
foreach ( $restore as $name => $object ) {
if ( NULL === $object ) {
unset( $GLOBALS[$name] );
} else {
$GLOBALS[$name] = $object;
}
}
// destroy local references
foreach ( $this->_restorables as $name => $object ) {
unset( $object, $this->_restorables[$name] );
}
if ( AI1EC_DEBUG ) {
// __destruct is called twice if facebook extension is installed
// still can't find the reason, this fixes it but prevent other plugins
// __destruct() so let's just use it in dev until we fix this.
exit();
}
}
/**
* Register a callback to be executed during shutdown sequence
*
* @param callback $callback Valid PHP callback
*
* @return Ai1ec_Shutdown_Utility Self instance for chaining
*/
public function register( $callback ) {
$this->_callbacks[] = $callback;
return $this;
}
/**
* Constructor
*
* Here global variables are referenced locally to ensure their preservation
*
* @return void Constructor does not return
*/
public function __construct() {
foreach ( $this->_preserve as $name => $class ) {
$this->_restorables[$name] = $GLOBALS[$name];
}
}
}