PluginProbe
Themify Event Post / 1.3.2
Themify Event Post v1.3.2
1.3.7 trunk 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6
themify-event-post / includes / system.php

system.php in Themify Event Post 1.3.2, at includes/system.php

354 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Themify_Event_Post {
4
5 public $dir;
6 public $url;
7 public $version;
8 public $pid = 'themify-event-post';
9 private $options = null;
10 public $date_time_format = 'Y-m-d H:i';
11
12 /* a copy of original $wp_query object before it's modified by the plugin */
13 public $query;
14 public $post;
15
16 /* flag to indicate rendering shortcode or the main loop */
17 private static $is_shortcode = false;
18
19 /**
20 * Creates or returns an instance of this class.
21 *
22 * @return A single instance of this class.
23 */
24 public static function get_instance( $args = array() ) {
25 static $instance = null;
26 if ( $instance === null ) {
27 $instance = new self( $args );
28 }
29 return $instance;
30 }
31
32 function __construct( $args = array() ) {
33 $this->dir = $args['dir'];
34 $this->url = $args['url'];
35 $this->version = $args['version'];
36
37 include( $this->dir . 'includes/post-type.php' );
38 include( $this->dir . 'includes/widgets.php' );
39 include( $this->dir . 'includes/functions.php' );
40 if ( is_admin() ) {
41 add_action( 'admin_enqueue_scripts', array($this, 'enqueue_admin_script') );
42 include( $this->dir . 'includes/admin.php' );
43 new Themify_Event_Post_Admin();
44 } else {
45 add_action( 'pre_get_posts', [ $this, 'pre_get_posts' ] );
46 }
47
48 add_action( 'after_setup_theme', array( $this, 'load_themify_library' ), 15 );
49 add_action( 'init', array( $this, 'i18n' ) );
50 add_shortcode( 'themify_event_post', array( $this, 'shortcode' ) );
51 add_filter( 'themify_metabox/fields/themify-meta-boxes', array( $this, 'themify_do_metaboxes' ), 10, 2 );
52 add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
53 add_filter( 'template_include', array( $this, 'template_include' ) );
54
55 if ( current_user_can( 'publish_posts' ) && get_user_option( 'rich_editing' ) === 'true' ) {
56 include( $this->dir . 'includes/tinymce.php' );
57 new Themify_Events_Posts_TinyMCE();
58 }
59
60 add_action( 'themify_builder_setup_modules', array( $this, 'register_module' ) );
61 if (function_exists('themify_builder_get') && empty(themify_builder_get('setting-search_exclude_event'))) {
62 add_filter( 'themify_search_args', array( $this, 'add_event_to_search_result' ) );
63 }
64 }
65
66 function i18n() {
67 load_plugin_textdomain( 'themify-event-post', false, 'themify-event-post/languages' );
68 }
69
70 function template_include( $template ) {
71 global $wp_query, $themify;
72
73 if ( isset( $wp_query->post ) &&
74 ( is_singular( 'event' ) || is_tax( [ 'event-category', 'event-tag' ] ) )
75 ) {
76
77 if ( class_exists( 'Tbp_Public' ,false) ) {
78 $location = is_singular( 'event' ) ? 'single' : 'archive';
79 /* a Builder Pro template is active, don't change the template */
80 if ( ! empty( Tbp_Public::get_location( $location ) ) ) {
81 return $template;
82 }
83 }
84
85 $template = get_page_template();
86
87 $this->query = clone $wp_query;
88 $this->post = clone $wp_query->post;
89
90 if ( is_singular( 'event' ) ) {
91 $wp_query->post->post_title = '';
92 add_filter( 'the_content', array( $this, 'single_template' ), 999 );
93
94 /* in Themify themes, disable the thumbnail */
95 if ( isset( $themify ) ) {
96 $themify->hide_page_image = 'yes';
97 }
98 } else{
99
100 remove_filter( 'the_content', 'wpautop' );
101
102 $wp_query->posts_per_page = 1;
103 $wp_query->nopaging = true;
104 $wp_query->post_count = 1;
105 $wp_query->post = new WP_Post( new stdClass() );
106 $wp_query->post->ID = 0;
107 $wp_query->post->filter = 'raw';
108 $wp_query->post->post_title = '';
109 $wp_query->post->post_content = $this->get_template( 'archive', array() );
110 $wp_query->posts = array( $wp_query->post );
111 $wp_query->is_page = false;
112 $wp_query->is_archive = true;
113 $wp_query->is_category = $this->query->is_tax( 'event-category' );
114 $wp_query->is_tax = $this->query->is_tax( array( 'event-category', 'event-tag' ) );
115 $wp_query->is_single = false;
116 }
117 }
118 return $template;
119 }
120
121 function single_template( $content ) {
122 if ( get_post_type() === 'event' && self::$is_shortcode === false ) {
123 $content = $this->get_template( 'single', array(
124 'content' => $content
125 ) );
126 }
127
128 return $content;
129 }
130
131 function archive_template( $content ) {
132 if ( get_post_type() === 'event' ) {
133 $content = $this->get_template( 'archive', array() );
134 }
135
136 return $content;
137 }
138
139 public function load_themify_library() {
140 defined( 'THEMIFY_METABOX_DIR' ) || define( 'THEMIFY_METABOX_DIR', $this->dir . 'includes/themify-metabox/' );
141 defined( 'THEMIFY_METABOX_URI' ) || define( 'THEMIFY_METABOX_URI', $this->url . 'includes/themify-metabox/' );
142 include_once( $this->dir . 'includes/themify-metabox/themify-metabox.php' );
143 }
144
145 public function wp_enqueue_scripts() {
146 wp_enqueue_style( $this->pid, $this->url . 'assets/style.css' );
147
148 /* the scripts.js file solely handles the map, on Themify themes this is not needed */
149 if ( ! $this->is_using_themify_theme()) {
150 $mapKey=$this->get_option( 'google_maps_key' );
151 wp_enqueue_script( $this->pid, $this->url . 'assets/scripts.js', array( 'jquery' ), $this->version, true );
152 if ( !empty($mapKey) ) {
153 wp_localize_script( $this->pid, 'themifyEventPosts', array(
154 'map_key' => $mapKey
155 ) );
156 }
157 }
158 }
159
160 public function enqueue_admin_script( $hook ) {
161 $screen = get_current_screen();
162 if ( $screen->post_type !== 'event' ) {
163 return;
164 }
165 wp_enqueue_script( $this->pid . '-admin-script', $this->url . 'assets/admin_script.js', array('jquery'), $this->version );
166 }
167
168 /**
169 * Main shortcode rendering
170 * @param array $atts
171 * @param $post_type
172 * @return string|void
173 */
174 function shortcode( $atts = array(), $content = '', $code = '' ) {
175 self::$is_shortcode = true;
176 $output = include $this->locate_template( 'shortcode' );
177 self::$is_shortcode = false;
178 return $output;
179 }
180
181 /**
182 * Return all options
183 *
184 * @return mixed
185 * @since 1.0
186 */
187 public function get_options() {
188 if( null === $this->options ) {
189 $this->options = get_option( 'themify_event_post', array() );
190 }
191
192 return $this->options;
193 }
194
195 /**
196 * Return an option by its name
197 *
198 * @return mixed
199 * @since 1.0
200 */
201 public function get_option( $name, $default = null ) {
202 $options = $this->get_options();
203 if(isset( $options[$name] )){
204 return !is_array($options[$name])?sanitize_text_field($options[$name]):$options[$name];
205 }
206 return $default;
207 }
208
209 function themify_do_metaboxes( $meta_boxes, $post_type ) {
210 if( 'event' !== $post_type ) {
211 return $meta_boxes;
212 }
213
214 $event_options = array(
215 'name' => __( 'Event Settings', 'themify-event-post' ),
216 'id' => 'event-options',
217 'options' => include( $this->locate_template( 'config-post-meta' ) ),
218 'pages' => 'event',
219 'default_active' => true,
220 );
221
222 return array_merge( array( $event_options ), $meta_boxes );
223 }
224
225 function register_module() {
226 if ( class_exists( 'Themify_Builder_Component_Module' ,false) ) {
227 if(method_exists('Themify_Builder_Model', 'add_module')){
228 Themify_Builder_Model::add_module($this->dir . 'modules/module-event-posts.php' );
229 }
230 else{
231 Themify_Builder_Model::register_directory('templates',$this->dir . 'templates');
232 Themify_Builder_Model::register_directory('modules',$this->dir . 'modules');
233 }
234 }
235 }
236
237 public function get_template( $name, $args = array() ) {
238 extract( $args );
239 if( $path = $this->locate_template( $name ) ) {
240 ob_start();
241 include $path;
242 return ob_get_clean();
243 }
244
245 return false;
246 }
247
248 public function get_shortcode_template( $posts, $slug, $args ) {
249 global $post;
250 if ( is_object( $post ) )
251 $saved_post = clone $post;
252
253 $html = '';
254
255 foreach ( $posts as $post ) {
256 setup_postdata( $post );
257 $html .= $this->get_template( $slug, $args );
258 }
259
260 if ( isset( $saved_post ) && is_object( $saved_post ) ) {
261 $post = $saved_post;
262 setup_postdata( $saved_post );
263 }
264
265 return $html;
266 }
267
268 public function locate_template( $name ) {
269 if( is_child_theme() && is_file( trailingslashit( get_stylesheet_directory() ) . trailingslashit( $this->pid ) . "{$name}.php" ) ) {
270 return trailingslashit( get_stylesheet_directory() ) . trailingslashit( $this->pid ) . "{$name}.php";
271 } else if( is_file( trailingslashit( get_template_directory() ) . trailingslashit( $this->pid ) . "{$name}.php" ) ) {
272 return trailingslashit( get_template_directory() ) . trailingslashit( $this->pid ) . "{$name}.php";
273 } else if( is_file( $this->get_template_dir() . "/{$name}.php" ) ) {
274 return $this->get_template_dir() . "/{$name}.php";
275 } else {
276 return false;
277 }
278 }
279
280 public function get_template_dir() {
281 return $this->dir . 'templates';
282 }
283
284 function add_event_to_search_result( $args ) {
285 $args['post_type'] = array_merge( array( 'event' ), (array) $args['post_type'] );
286 return $args;
287 }
288
289 /**
290 * Returns true if the active theme is using Themify framework
291 *
292 * @return bool
293 */
294 public function is_using_themify_theme() {
295 return is_file( get_template_directory() . '/themify/themify-utils.php' );
296 }
297
298 /**
299 * Modifies the query on archive pages
300 *
301 * @since 1.2.1
302 */
303 function pre_get_posts( $query ) {
304 if ( $query->is_main_query() && (
305 $query->is_tax( [ 'event-category', 'event-tag' ] )
306 || $query->is_post_type_archive( 'event' )
307 ) ) {
308 $order = $this->get_option( 'order', 'desc' );
309 $orderby = $this->get_option( 'orderby', 'date' );
310 $show = $this->get_option( 'show', 'all' );
311
312 $query->set( 'orderby', $orderby );
313 $query->set( 'order', $order );
314 if ( $orderby === 'event_date' ) {
315 $query->set( 'orderby', 'meta_value' );
316 $query->set( 'meta_key', 'start_date' );
317 }
318 if ( $show === 'upcoming' ) {
319 $query->set( 'meta_query', array(
320 'relation' => 'OR',
321 array(
322 'key' => 'end_date',
323 'value' => date_i18n( 'Y-m-d H:i' ),
324 'compare' => '>='
325 ),
326 array(
327 'key' => 'start_date',
328 'value' => date_i18n( 'Y-m-d H:i' ),
329 'compare' => '>='
330 ),
331 array(
332 'key' => 'repeat',
333 'value' => 'none',
334 'compare' => '!='
335 )
336 ) );
337 } elseif ( $show === 'past' ) {
338 $query->set( 'meta_query', array(
339 'relation' => 'AND',
340 array(
341 'key' => 'end_date',
342 'value' => date_i18n( 'Y-m-d H:i' ),
343 'compare' => '<'
344 ),
345 array(
346 'key' => 'end_date',
347 'value' => '',
348 'compare' => '!='
349 ),
350 ) );
351 }
352 }
353 }
354 }