PluginProbe
Themify Event Post / trunk
Themify Event Post vtrunk
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 trunk, at includes/system.php

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