-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtec-ical-importer.php
171 lines (144 loc) · 3.58 KB
/
tec-ical-importer.php
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
Plugin Name: The Events Calendar - iCalendar Importer
Description: Imports events from remotely-located iCalendar files into The Events Calendar.
Author: r-a-y
Author URI: http://profiles.wordpress.org/r-a-y
Version: 0.1-alpha
License: GPL v2 or later
*/
/**
* TEC iCal Importer
*
* @package TEC-ICAL
* @subpackage Loader
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
add_action( 'plugins_loaded', array( 'TEC_iCal', 'init' ) );
/**
* TEC iCal Importer Core
*
* @package TEC-ICAL
* @subpackage Classes
*/
class TEC_iCal {
/**
* Settings from database.
* @var array
*/
public static $settings;
/**
* Absolute path to this plugin's directory.
* @var string
*/
public static $plugin_dir;
/**
* URL to this plugin's directory.
* @var string
*/
public static $plugin_url;
/**
* Internal name for this plugin.
* @var string
*/
protected $name = 'tec-ical';
/**
* Cron interval used in {@link TEC_iCal::schedule_cron()}.
* @var string
*/
protected $cron_interval;
/**
* Static initializer.
*/
public static function init() {
return new self();
}
/**
* Constructor.
*/
public function __construct() {
if ( ! class_exists( 'Tribe__Events__Main' ) ) {
return false;
}
// setup properties
$this->setup_properties();
// setup cron
add_action( 'init', array( $this, 'setup_cron' ), 0 );
// Set up admin area if in the WP dashboard
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
require self::$plugin_dir . '/includes/tec-ical-admin.php';
TEC_iCal_Admin::init();
}
}
/**
* Setup pertinent class properties.
*/
protected function setup_properties() {
// load settings
self::$settings = get_option( $this->name );
// plugin properties
self::$plugin_dir = dirname( __FILE__ );
self::$plugin_url = plugin_dir_url( __FILE__ );
}
/**
* Setup cron.
*/
public function setup_cron() {
// setup cron interval
//
// default cron interval
if ( empty( self::$settings['custom-cron-interval'] ) ) {
// default to daily
// can also accept 'twicedaily' or 'hourly'
$this->cron_interval = apply_filters( 'tec_ical_default_cron_interval', 'daily' );
// custom cron interval
} else {
$this->cron_interval = 'tec_ical_custom';
add_filter( 'cron_schedules', array( $this, 'custom_cron_schedule' ) );
}
// schedule cron
$this->schedule_cron();
// run cronjob when WP hits our scheduled task
add_action( 'tec_ical_schedule', array( $this, 'run_cronjob' ) );
}
/**
* Add custom cron interval to WordPress if set.
*
* @param array $schedules The different cron schedules available
* @return array
*/
public function custom_cron_schedule( $schedules ) {
$interval = self::$settings['custom-cron-interval'];
$schedules[$this->cron_interval] = array(
'interval' => (int) $interval * 60, // interval in seconds
'display' => sprintf( __( 'Every %s minutes', 'tec-ical' ), $interval )
);
return $schedules;
}
/**
* Schedule our task.
*/
public function schedule_cron() {
if ( ! wp_next_scheduled( 'tec_ical_schedule' ) ) {
wp_schedule_event( time(), $this->cron_interval, 'tec_ical_schedule' );
}
}
/**
* Sync our iCalendars when WordPress hits our scheduled task.
*/
public function run_cronjob() {
if ( ! class_exists( 'TEC_iCal_Parser' ) ) {
require self::$plugin_dir . '/includes/tec-ical-parser.php';
}
TEC_iCal_Parser::init();
}
/**
* Get iCalendar data saved from settings.
*
* @return array
*/
public static function get_icals() {
return ! empty( self::$settings['icals'] ) ? self::$settings['icals'] : array();
}
}