-
Notifications
You must be signed in to change notification settings - Fork 0
/
mindshare-events.php
210 lines (168 loc) · 7.01 KB
/
mindshare-events.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Plugin Name: Mindshare Simple Events
* Plugin URI:https://mind.sh/are
* Description: A simple events plugin, for sites that just need the basics.
* Version: 1.1.4
* Author: Mindshare Labs, Inc
* Author URI: https://mind.sh/are
*/
class mindEvents {
private $options = '';
private $token = '';
protected static $instance = NULL;
public function __construct() {
if ( !defined( 'MINDEVENTS_PLUGIN_FILE' ) ) {
define( 'MINDEVENTS_PLUGIN_FILE', __FILE__ );
}
//Define all the constants
$this->define( 'MINDEVENTS_ABSPATH', dirname( MINDEVENTS_PLUGIN_FILE ) . '/' );
$this->define( 'MINDEVENTS_PLUGIN_VERSION', '0.1.4');
$this->define( 'MINDRETURNS_PREPEND', 'mindevents_' );
$this->includes();
$mobile = new Mobile_Detect();
add_action( 'admin_enqueue_scripts', array($this, 'enque_scripts_and_styles'), 100 );
add_action( 'wp_enqueue_scripts', array($this, 'enque_front_scripts_and_styles'), 100 );
add_action ('save_post', array($this, 'add_post_meta'), 100, 2);
add_action ('save_post', array($this, 'sync_sub_event_tax'), 100, 2);
// do_action( 'delete_post', array($this, 'delete_sub_events'), 100, 2);
add_action( 'transition_post_status', array($this, 'transition_sub_events'), 100, 3 );
add_action ('wp_head', array($this, 'generate_schema'));
$this->define( 'MINDRETURNS_IS_MOBILE', $mobile->isMobile() );
$this->options = get_option( 'mindevents_support_settings' );
$this->token = (isset($this->options['mindevents_api_token']) ? $this->options['mindevents_api_token'] : false);
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
private function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
private function includes() {
//General
include_once MINDEVENTS_ABSPATH . 'inc/mobile-detect.php';
include_once MINDEVENTS_ABSPATH . 'inc/events.class.php';
include_once MINDEVENTS_ABSPATH . 'inc/options.php';
include_once MINDEVENTS_ABSPATH . 'inc/admin.class.php';
include_once MINDEVENTS_ABSPATH . 'inc/posttypes.php';
include_once MINDEVENTS_ABSPATH . 'inc/ajax.class.php';
include_once MINDEVENTS_ABSPATH . 'inc/shortcodes.class.php';
include_once MINDEVENTS_ABSPATH . 'inc/front-end.php';
}
public function enque_front_scripts_and_styles() {
wp_register_style('mindevents-css', plugins_url('css/style.css', MINDEVENTS_PLUGIN_FILE), array(), MINDEVENTS_PLUGIN_VERSION, 'all');
wp_enqueue_style('mindevents-css');
wp_register_script('mindevents-js', plugins_url('js/mindevents.js', MINDEVENTS_PLUGIN_FILE), array('jquery'), MINDEVENTS_PLUGIN_VERSION, true);
wp_enqueue_script('mindevents-js');
if(is_post_type_archive('events')) :
$postID = 'archive';
else :
$postID = get_the_ID();
endif;
wp_localize_script( 'mindevents-js', 'mindeventsSettings', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'post_id' => $postID
) );
}
public function enque_scripts_and_styles() {
wp_register_style('mindevents-css', plugins_url('css/style.css', MINDEVENTS_PLUGIN_FILE), array(), MINDEVENTS_PLUGIN_VERSION, 'all');
wp_enqueue_style('mindevents-css');
wp_register_style('timepicker-js', 'https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.css', array(), '1.3.5', 'all');
wp_enqueue_style('timepicker-js');
wp_register_script('timepicker-js', 'https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.js', array('jquery'), '1.3.5', true);
wp_enqueue_script('timepicker-js');
wp_register_script('fontawesome-js', 'https://kit.fontawesome.com/bed26df994.js', array(), MINDEVENTS_PLUGIN_VERSION, true);
wp_enqueue_script('fontawesome-js');
wp_register_script('mindevents-js', plugins_url('js/admin.js', MINDEVENTS_PLUGIN_FILE), array('jquery', 'timepicker-js'), MINDEVENTS_PLUGIN_VERSION, true);
wp_enqueue_script('mindevents-js');
wp_localize_script( 'mindevents-js', 'mindeventsSettings', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'post_id' => get_the_id()
) );
}
static function transition_sub_events($new_status, $old_status, $parentOBJ) {
if($parentOBJ->post_type == 'events') :
$sub_events = get_posts(array(
'post_parent' => $parentOBJ->ID,
'post_type' => 'sub_event',
'posts_per_page' => -1,
'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash')
));
if($sub_events) :
foreach ($sub_events as $key => $post) :
wp_update_post(array(
'ID' => $post->ID,
'post_status' => $new_status
));
endforeach;
endif;
endif;
}
static function sync_sub_event_tax($id, $object) {
if($object->post_type == 'events') :
$terms = wp_get_post_terms( $id, 'event_category', array('fields' => 'ids'));
$sub_events = get_posts(array(
'post_parent' => $object->ID,
'post_type' => 'sub_event',
'posts_per_page' => -1,
'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash')
));
if($sub_events) :
foreach ($sub_events as $key => $post) :
wp_set_post_terms( $post->ID, $terms, 'event_category');
endforeach;
endif;
endif;
}
static function add_post_meta($id, $object) {
if($object->post_type == 'events') :
if(isset($_POST['defaults'])) :
$metas = array_map( 'sanitize_text_field', wp_unslash( $_POST['defaults'] ) );
update_post_meta($id, 'defaults', $metas);
foreach ($metas as $key => $value) {
update_post_meta($id, $key, $value);
}
endif;
$first_event = get_posts(array(
'orderby' => 'meta_value',
'meta_key' => 'event_start_time_stamp',
'meta_type' => 'DATETIME',
'post_parent' => $id,
'order' => 'ASC',
'post_type' => 'sub_event',
'posts_per_page' => 1
));
if($first_event) :
$first_event = $first_event[0];
update_post_meta($id, 'first_event_date', get_post_meta($first_event->ID, 'event_start_time_stamp', true));
endif;
$last_event = get_posts(array(
'orderby' => 'meta_value',
'meta_key' => 'event_start_time_stamp',
'meta_type' => 'DATETIME',
'post_parent' => $id,
'order' => 'DESC',
'post_type' => 'sub_event',
'posts_per_page' => 1
));
if($last_event):
$last_event = $last_event[0];
update_post_meta($id, 'last_event_date', get_post_meta($last_event->ID, 'event_start_time_stamp', true));
endif;
endif;
}
public function generate_schema() {
if(is_singular('events')) :
$event = new mindEventCalendar(get_the_ID());
echo '<script type="application/ld+json">';
echo $event->generate_schema();
echo '</script>';
endif;
}
}//end of class
new mindEvents();