PluginProbe
WPCasa – Real Estate for WordPress / trunk
WPCasa – Real Estate for WordPress vtrunk
1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.1.1 trunk 1.2.0 1.2.1 1.2.10 1.2.10.1 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.2.9.1 1.2.9.2 1.3.0 All 31 releases
wpcasa / includes / class-wpsight-template.php

class-wpsight-template.php in WPCasa – Real Estate for WordPress trunk, at includes/class-wpsight-template.php

560 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * WPSight_Template class
8 */
9 class WPSight_Template {
10
11 /**
12 * locate_template()
13 *
14 * Locate a template and return the path
15 * for inclusion or load if desired.
16 *
17 * This is the load order:
18 *
19 * /wp-content/themes/ theme (child) / $template_name
20 *
21 * /wp-content/themes/ theme (child) / WPSIGHT_DOMAIN (e.g. wpcasa) / $template_name
22 *
23 * /wp-content/themes/ theme (parent) / $template_name
24 *
25 * /wp-content/themes/ theme (parent) / WPSIGHT_DOMAIN (e.g. wpcasa) / $template_name
26 *
27 * $template_path (custom path from addon for example) / $template_name
28 *
29 * /wp-content/plugins/ WPSIGHT_DOMAIN (e.g. wpcasa) / templates / $template_name
30 *
31 * @param string|array $template_names Template name (incl. file extension like .php)
32 * @param string $template_path Custom template path for plugins and addons (default: '')
33 * @param bool $load Call load_template() if true or return template path if false
34 * @uses trailingslashit()
35 * @uses get_stylesheet_directory()
36 * @uses get_template_directory()
37 * @uses wpsight_get_templates_dir()
38 * @return string $located Absolute path to template file (if $load is false)
39 *
40 * @since 1.0.0
41 */
42 public static function locate_template( $template_names, $args = array(), $template_path = '', $load = false, $require_once = false ) {
43 global $post, $wp_query, $wpdb;
44
45 if ( $args && is_array( $args ) )
46 extract( $args );
47
48 // No file found yet
49 $located = false;
50
51 // Try to find a template file
52 foreach ( (array) $template_names as $template_name ) {
53
54 // Continue if template is empty
55 if ( empty( $template_name ) )
56 continue;
57
58 // Trim off any slashes from the template name
59 $template_name = ltrim( $template_name, '/' );
60
61 // Check child theme
62 if ( file_exists( trailingslashit( get_stylesheet_directory() ) . $template_name ) ) {
63 $located = trailingslashit( get_stylesheet_directory() ) . $template_name;
64 break;
65
66 // Check extra folder in child theme
67 } elseif ( file_exists( trailingslashit( get_stylesheet_directory() . '/' . WPSIGHT_DOMAIN ) . $template_name ) ) {
68 $located = trailingslashit( get_stylesheet_directory() . '/' . WPSIGHT_DOMAIN ) . $template_name;
69 break;
70
71 // Check parent theme
72 } elseif ( file_exists( trailingslashit( get_template_directory() ) . $template_name ) ) {
73 $located = trailingslashit( get_template_directory() ) . $template_name;
74 break;
75
76 // Check extra folder parent theme
77 } elseif ( file_exists( trailingslashit( get_template_directory() . '/' . WPSIGHT_DOMAIN ) . $template_name ) ) {
78 $located = trailingslashit( get_template_directory() . '/' . WPSIGHT_DOMAIN ) . $template_name;
79 break;
80
81 // Check custom path templates (e.g. from addons)
82 } elseif ( ! empty( $template_path ) && file_exists( trailingslashit( $template_path ) . $template_name ) ) {
83 $located = trailingslashit( $template_path ) . $template_name;
84 break;
85
86 // Check plugin templates
87 } elseif ( file_exists( trailingslashit( wpsight_get_templates_dir() ) . $template_name ) ) {
88 $located = trailingslashit( wpsight_get_templates_dir() ) . $template_name;
89 break;
90 }
91
92 }
93
94 $located = apply_filters( 'wpsight_locate_template', $located, $template_names, $template_path, $load, $require_once );
95
96 // Load found template if required
97
98 if ( ( true == $load ) && ! empty( $located ) ) {
99
100 if ( $require_once )
101 require_once $located;
102 else
103 require $located;
104
105 }
106
107 // Or return template file path
108 return $located;
109 }
110
111 /**
112 * get_template_part()
113 *
114 * Load specific template part.
115 *
116 * @param string $slug The slug name for the generic template
117 * @param string $name The name of the specialized template
118 * @param string $template_path Custom template path for plugins and addons (default: '')
119 * @param bool $load Call load_template() if true or return template path if false
120 * @uses self::locate_template()
121 * @return string $located Absolute path to template file (if $load is false)
122 *
123 * @since 1.0.0
124 */
125 public static function get_template_part( $slug, $name = null, $args = array(), $template_path = '', $load = true, $require_once = false ) {
126
127 // Execute code for this part
128 do_action( 'wpsight_get_template_part_' . $slug, $slug, $name, $args, $template_path, $load, $require_once );
129
130 // Setup possible parts
131 $templates = array();
132 if ( isset( $name ) )
133 $templates[] = $slug . '-' . $name . '.php';
134 $templates[] = $slug . '.php';
135
136 // Allow template parts to be filtered
137 $templates = apply_filters( 'wpsight_get_template_part', $templates, $slug, $name, $args, $template_path, $load, $require_once );
138
139 // Return the part that is found
140 return self::locate_template( $templates, $args, $template_path, $load, $require_once );
141 }
142
143 /**
144 * get_orderby()
145 *
146 * Return orderby options for listing pages
147 *
148 * @param array $args Array of arguments
149 * @uses wpsight_get_template()
150 * @return string|bool $orderby HTML output or false if empty
151 *
152 * @since 1.0.0
153 */
154 public static function get_orderby( $args = array() ) {
155
156 $defaults = array(
157
158 'type' => 'select', // can be 'links'
159 'orderby' => true,
160 'order' => true,
161 'labels' => array(
162 'orderby' => _x( 'Order by', 'listings panel actions', 'wpcasa' ),
163 'date' => _x( 'Date', 'listings panel actions', 'wpcasa' ),
164 'orderby_sep' => _x( 'or', 'listings panel actions', 'wpcasa' ),
165 'price' => _x( 'Price', 'listings panel actions', 'wpcasa' ),
166 'order' => _x( 'Order', 'listings panel actions', 'wpcasa' ),
167 'desc' => _x( 'DESC', 'listings panel actions', 'wpcasa' ),
168 'order_sep' => _x( 'or', 'listings panel actions', 'wpcasa' ),
169 'asc' => _x( 'ASC', 'listings panel actions', 'wpcasa' ),
170 'title' => _x( 'Title', 'listings panel actions', 'wpcasa' )
171 )
172
173 );
174
175 $args = wp_parse_args( $args, $defaults );
176
177 $args = apply_filters( 'wpsight_get_orderby_args', $args );
178
179 // Setup $orderby markup
180 $orderby = '';
181
182 if ( ( isset( $args['orderby'] ) || isset( $args['order'] ) ) && isset( $args['type'] ) ) {
183
184 ob_start();
185
186 // Get orderby template
187 wpsight_get_template( 'listings-panel-' . sanitize_file_name( $args['type'] ) . '.php', array( 'args' => $args ) );
188
189 $orderby = ob_get_clean();
190
191 }
192
193 if ( ! empty( $orderby ) )
194 return $orderby;
195
196 return false;
197
198 }
199
200 /**
201 * get_panel()
202 *
203 * Return formatted listings control panel (with title and order options)
204 *
205 * @uses wpsight_get_template()
206 * @return string|bool HTML markup of listings panel or false if empty
207 *
208 * @since 1.0.0
209 */
210 public static function get_panel( $args = array() ) {
211
212 $output = false;
213
214 // If only one listing is called, don't show panel
215
216 if ( ! isset( $args->query_vars['p'] ) || empty( $args->query_vars['p'] ) ) {
217
218 ob_start();
219
220 // Get panel template
221 wpsight_get_template( 'listings-panel.php' );
222
223 $output = ob_get_clean();
224
225 }
226
227 return apply_filters( 'wpsight_get_panel', $output, $args );
228
229 }
230
231 /**
232 * get_listing_title()
233 *
234 * Return formatted single listing
235 * title with title actions.
236 *
237 * @uses get_the_ID()
238 * @uses get_the_title()
239 * @uses wpsight_listing_actions()
240 * @return string HTML markup of listing title
241 *
242 * @since 1.0.0
243 */
244 public static function get_listing_title( $post_id = '', $actions = array() ) {
245
246 // Use global post ID if not defined
247
248 if ( ! $post_id )
249 $post_id = get_the_ID();
250
251 ob_start(); ?>
252
253 <div class="wpsight-listing-title clearfix">
254
255 <h1 class="entry-title">
256 <?php echo wp_kses_post( get_the_title( $post_id ) ); ?>
257 </h1>
258
259 <?php wpsight_listing_actions( $post_id, $actions ); ?>
260
261 </div><?php
262
263 return apply_filters( 'wpsight_get_listing_title', ob_get_clean(), $post_id, $actions );
264
265 }
266
267 /**
268 * get_archive_title()
269 *
270 * Return formatted listings archive title
271 *
272 * @return string Listings archive title for the given query
273 *
274 * @since 1.0.0
275 */
276 public static function get_archive_title() {
277 global $wp_query, $wpsight_query;
278
279 if ( isset( $wpsight_query->found_posts ) ) {
280 $title = '<span class="listings-panel-found">' . $wpsight_query->found_posts . '</span>' . ' ' . _nx( 'Listing', 'Listings', intval( $wpsight_query->found_posts ), 'archive title', 'wpcasa' );
281 } elseif ( isset( $wp_query->found_posts ) ) {
282 $title = '<span class="listings-panel-found">' . $wp_query->found_posts . '</span>' . ' ' . _nx( 'Listing', 'Listings', intval( $wp_query->found_posts ), 'archive title', 'wpcasa' );
283 } else {
284 $title = __( 'Listings', 'wpcasa' );
285 }
286
287 return apply_filters( 'wpsight_get_archive_title', $title );
288
289 }
290
291 /**
292 * get_pagination()
293 *
294 * Return formatted listings pagination
295 *
296 * @param int $max_num_pages max_num_pages parameter of corresponding query
297 * @param array $args paginate_links() arguments
298 * @uses get_query_var()
299 * @uses is_rtl()
300 * @uses get_pagenum_link()
301 * @uses wp_parse_args()
302 * @return string|bool HTML markup of listings pagination or false if empty
303 *
304 * @since 1.0.0
305 */
306 public static function get_pagination( $max_num_pages = '', $args = array() ) {
307
308 // Check for max_num_pages
309
310 if ( empty( $max_num_pages ) ) {
311 global $wp_query;
312 $total = $wp_query->max_num_pages;
313 } else {
314 $total = $max_num_pages;
315 }
316
317 // need an unlikely integer
318 $big = 999999999;
319
320 // Make sure paging works
321
322 if ( get_query_var( 'paged' ) ) {
323 $paged = get_query_var( 'paged' );
324 } elseif ( get_query_var( 'page' ) ) {
325 $paged = get_query_var( 'page' );
326 } else {
327 $paged = 1;
328 }
329
330 // Set prev/next arrows
331
332 $arr_prev = ! is_rtl() ? '&larr; ' : '&rarr; ';
333 $arr_next = ! is_rtl() ? ' &rarr;' : ' &larr;';
334
335 // Set paginate_links() defaults
336
337 $defaults = array(
338 'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
339 'format' => '?paged=%#%',
340 'current' => max( 1, $paged ),
341 'total' => $total,
342 'show_all' => false,
343 'end_size' => 1,
344 'mid_size' => 4,
345 'prev_next' => true,
346 'prev_text' => $arr_prev . __( 'Previous', 'wpcasa' ),
347 'next_text' => __( 'Next', 'wpcasa' ) . $arr_next,
348 'type' => 'list',
349 'add_args' => false,
350 'add_fragment' => '',
351 'before_page_number' => '',
352 'after_page_number' => ''
353 );
354
355 // Parse default with args
356 $args = wp_parse_args( $args, $defaults );
357
358 // Apply filter
359 $args = apply_filters( 'wpsight_get_pagination_args', $args );
360
361 // Execute paginate_links()
362 $pagination = apply_filters( 'wpsight_get_pagination', paginate_links( $args ) );
363
364 if ( ! empty( $pagination ) )
365 return '<div class="wpsight-pagination">' . $pagination . '</div><!-- .wpsight-pagination -->';
366
367 return false;
368
369 }
370
371 /**
372 * get_listing_class()
373 *
374 * Get listing/post classes.
375 *
376 * @param string $class
377 * @param mixed $post_id
378 * @uses get_the_ID()
379 * @uses get_post()
380 * @uses wpsight_post_type()
381 * @uses wpsight_is_listing_sticky()
382 * @uses wpsight_is_listing_featured()
383 * @uses wpsight_is_listing_expired()
384 * @uses wpsight_is_listing_not_available()
385 * @return bool|array
386 *
387 * @since 1.0.0
388 */
389 public static function get_listing_class( $class = '', $post_id = false ) {
390
391 if ( $post_id === false )
392 $post_id = get_the_ID();
393
394 $post = get_post( $post_id );
395
396 $classes = array();
397
398 if ( empty( $post ) || $post->post_type != wpsight_post_type() )
399 return $classes;
400
401 $classes[] = 'listing';
402
403 if ( wpsight_is_listing_sticky( $post_id ) )
404 $classes[] = 'listing-sticky';
405
406 if ( wpsight_is_listing_featured( $post_id ) )
407 $classes[] = 'listing-featured';
408
409 if ( wpsight_is_listing_expired( $post_id ) )
410 $classes[] = 'listing-expired';
411
412 if ( wpsight_is_listing_not_available( $post_id ) )
413 $classes[] = 'listing-not-available';
414
415 if ( ! empty( $class ) ) {
416 if ( ! is_array( $class ) ) {
417 $class = preg_split( '#\s+#', $class );
418 }
419 $classes = array_merge( $classes, $class );
420 }
421
422 return apply_filters( 'wpsight_get_listing_class', get_post_class( $classes, $post->ID ), $post_id, $class, $classes );
423
424 }
425
426 /**
427 * get_listing_actions()
428 *
429 * Get listing actions (save, print etc.) array.
430 *
431 * @uses get_permalink()
432 * @uses get_the_ID()
433 * @uses add_query_arg()
434 * @uses wpsight_sort_array_by_position()
435 * @return array Array of listing actions
436 *
437 * @since 1.0.0
438 */
439 public static function get_listing_actions( $post_id = '' ) {
440
441 // Use global post ID if not defined
442
443 if ( ! $post_id )
444 $post_id = get_the_ID();
445
446 $actions = array(
447 'print' => array(
448 'label' => __( 'Print', 'wpcasa' ),
449 'name' => false,
450 'id' => false,
451 'class' => 'actions-print action-link',
452 'href' => esc_url( add_query_arg( array( 'print' => absint( $post_id ) ), get_permalink( $post_id ) ) ),
453 'target' => false,
454 'icon' => false,
455 'atts' => false,
456 'priority' => 20,
457 'callback' => false
458 )
459 );
460
461 // Apply filter, sort and return array
462 return wpsight_sort_array_by_priority( apply_filters( 'wpsight_get_listing_actions', $actions, $post_id ) );
463
464 }
465 /**
466 * listing_actions()
467 *
468 * Return listing actions
469 *
470 * @param integer $post_id
471 * @param array $actions
472 * @uses get_the_ID()
473 * @uses post_password_required()
474 * @uses get_post_status()
475 * @uses wpsight_get_listing_actions()
476 * @uses wp_parse_args()
477 * @uses call_user_func()
478 * @return string
479 *
480 * @since 1.0.0
481 */
482 public static function listing_actions( $post_id = '', $actions = array() ) {
483
484 $output = '';
485
486 // Use global post ID if not defined
487
488 if ( ! $post_id )
489 $post_id = get_the_ID();
490
491 if ( ! post_password_required( $post_id ) && 'publish' == get_post_status( $post_id ) ) {
492
493 // Get listing actions
494 $listing_actions = wpsight_get_listing_actions( $post_id );
495
496 $listing_actions = wp_parse_args( $actions, $listing_actions );
497
498 // Generate output
499 $output .= '<div class="wpsight-listing-actions">';
500
501 foreach ( $listing_actions as $action => $v ) {
502
503 if ( ! is_array( $listing_actions[ $action ] ) )
504 continue;
505
506 $output .= '<div class="wpsight-listing-action ' . esc_attr( sanitize_html_class( 'wpsight-listing-action-' . $action ) ) . '">';
507
508 // First check if we have a callback
509
510 if ( isset( $v['callback'] ) && is_callable( sanitize_key( $v['callback'] ) ) ) {
511
512 $output .= call_user_func( sanitize_key( $v['callback'] ), $v );
513
514 } else {
515
516 // Manage css class
517
518 $css_class = '';
519
520 if ( $v['class'] ) {
521
522 $css_class = ! is_array( $v['class'] ) ? explode( ' ', $v['class'] ) : $v['class'];
523
524 $css_class = array_map( 'sanitize_html_class', $css_class );
525 $css_class = ' class="' . implode( ' ', $css_class ) . '"';
526
527 }
528
529 // Manage link target
530
531 $target = '';
532
533 if ( $v['target'] ) {
534
535 $allowed_targets = array( '_blank', '_parent', '_top' );
536 $target = in_array( $v['target'], $allowed_targets ) ? $v['target'] : '';
537
538 if ( ! empty( $target ) )
539 $target = ' target="' . $target . '"';
540
541 }
542
543 $output .= sprintf( '<a href="%1$s"%2$s%3$s%4$s>%5$s%6$s</a>', esc_url( $v['href'] ), esc_attr( $v['id'] ), $css_class, $target, wp_kses_post( $v['icon'] ), esc_html( $v['label'] ) );
544
545 }
546
547 $output .= '</div>';
548
549 }
550
551 $output .= '</div>';
552
553 }
554
555 return apply_filters( 'wpsight_listing_actions', $output );
556
557 }
558
559 }
560