| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
/** |
| 6 |
* Template Loader |
| 7 |
* |
| 8 |
* @class PH_Template_Loader |
| 9 |
* @version 1.0.0 |
| 10 |
* @package PropertyHive/Classes |
| 11 |
* @category Class |
| 12 |
* @author PropertyHive |
| 13 |
*/ |
| 14 |
class PH_Template_Loader { |
| 15 |
|
| 16 |
/** |
| 17 |
* Constructor |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
|
| 21 |
add_action( 'init', array( $this, 'init' ) ); |
| 22 |
} |
| 23 |
|
| 24 |
public function init() |
| 25 |
{ |
| 26 |
$use_propertyhive_templates = apply_filters( 'propertyhive_use_propertyhive_templates', true ); |
| 27 |
if ( $use_propertyhive_templates === false ) |
| 28 |
{ |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$priority = 10; |
| 33 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 34 |
if ( is_plugin_active( 'oxygen/functions.php' ) ) |
| 35 |
{ |
| 36 |
$priority = 99; |
| 37 |
} |
| 38 |
add_filter( 'template_include', array( $this, 'template_loader' ), $priority ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Load a template. |
| 43 |
* |
| 44 |
* Handles template usage so that we can use our own templates instead of the themes. |
| 45 |
* |
| 46 |
* Templates are in the 'templates' folder. propertyhive looks for theme |
| 47 |
* overrides in /theme/propertyhive/ by default |
| 48 |
* |
| 49 |
* For beginners, it also looks for a propertyhive.php template first. If the user adds |
| 50 |
* this to the theme (containing a propertyhive() inside) this will be used for all |
| 51 |
* propertyhive templates. |
| 52 |
* |
| 53 |
* @param mixed $template |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
public function template_loader( $template ) { |
| 57 |
|
| 58 |
$find = array( 'propertyhive.php' ); |
| 59 |
$file = ''; |
| 60 |
|
| 61 |
if ( is_single() && get_post_type() == 'property' ) |
| 62 |
{ |
| 63 |
$use_property_hive_template = true; |
| 64 |
|
| 65 |
// Check for single Divi property page template |
| 66 |
if ( class_exists( 'ET_Theme_Builder_Request' ) ) |
| 67 |
{ |
| 68 |
$template_query = new WP_Query( |
| 69 |
array( |
| 70 |
'post_type' => ET_THEME_BUILDER_TEMPLATE_POST_TYPE, |
| 71 |
'post_status' => 'publish', |
| 72 |
'posts_per_page' => 1, |
| 73 |
'fields' => 'ids', |
| 74 |
'no_found_rows' => true, |
| 75 |
'update_post_meta_cache' => false, |
| 76 |
'update_post_term_cache' => false, |
| 77 |
'meta_query' => array( |
| 78 |
array( |
| 79 |
'key' => '_et_enabled', |
| 80 |
'value' => '1', |
| 81 |
'compare' => '=', |
| 82 |
), |
| 83 |
array( |
| 84 |
'key' => '_et_body_layout_id', |
| 85 |
'value' => '0', |
| 86 |
'compare' => '!=', |
| 87 |
), |
| 88 |
array( |
| 89 |
'key' => "_et_use_on", |
| 90 |
'value' => 'singular:post_type:property:all', |
| 91 |
'compare' => '=', |
| 92 |
), |
| 93 |
array( |
| 94 |
'key' => '_et_theme_builder_marked_as_unused', |
| 95 |
'compare' => 'NOT EXISTS', |
| 96 |
), |
| 97 |
), |
| 98 |
) |
| 99 |
); |
| 100 |
|
| 101 |
if ( $template_query->have_posts() ) |
| 102 |
{ |
| 103 |
$use_property_hive_template = false; |
| 104 |
} |
| 105 |
wp_reset_postdata(); |
| 106 |
} |
| 107 |
|
| 108 |
// Check for single Bricks Builder property page template |
| 109 |
if ( defined('BRICKS_DB_TEMPLATE_SLUG') ) |
| 110 |
{ |
| 111 |
$template_query = new WP_Query( |
| 112 |
array( |
| 113 |
'post_type' => BRICKS_DB_TEMPLATE_SLUG, |
| 114 |
'post_status' => 'publish', |
| 115 |
'fields' => 'ids', |
| 116 |
'no_found_rows' => true, |
| 117 |
'update_post_meta_cache' => false, |
| 118 |
'update_post_term_cache' => false, |
| 119 |
'meta_query' => array( |
| 120 |
array( |
| 121 |
'key' => '_bricks_template_type', |
| 122 |
'compare' => 'content', |
| 123 |
), |
| 124 |
), |
| 125 |
) |
| 126 |
); |
| 127 |
|
| 128 |
if ( $template_query->have_posts() ) |
| 129 |
{ |
| 130 |
while ( $template_query->have_posts() ) |
| 131 |
{ |
| 132 |
$template_query->the_post(); |
| 133 |
|
| 134 |
$template_settings = get_post_meta( get_the_ID(), '_bricks_template_settings', TRUE ); |
| 135 |
|
| 136 |
if ( |
| 137 |
isset($template_settings['templateConditions']) && |
| 138 |
is_array($template_settings['templateConditions']) && |
| 139 |
!empty($template_settings['templateConditions']) |
| 140 |
) |
| 141 |
{ |
| 142 |
foreach ( $template_settings['templateConditions'] as $templateCondition ) |
| 143 |
{ |
| 144 |
if ( |
| 145 |
isset($templateCondition['main']) && |
| 146 |
$templateCondition['main'] == 'postType' && |
| 147 |
isset($templateCondition['postType']) && |
| 148 |
is_array($templateCondition['postType']) && |
| 149 |
in_array('property', $templateCondition['postType']) |
| 150 |
) |
| 151 |
{ |
| 152 |
$use_property_hive_template = false; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
wp_reset_postdata(); |
| 159 |
} |
| 160 |
|
| 161 |
// Check for single Salient property page template |
| 162 |
if ( defined('NECTAR_THEME_NAME') && NECTAR_THEME_NAME == 'salient' && class_exists( 'Salient_Core' ) ) |
| 163 |
{ |
| 164 |
$template_query = new WP_Query( |
| 165 |
array( |
| 166 |
'post_type' => 'salient_g_sections', |
| 167 |
'post_status' => 'publish', |
| 168 |
'fields' => 'ids', |
| 169 |
'no_found_rows' => true, |
| 170 |
'update_post_meta_cache' => false, |
| 171 |
'update_post_term_cache' => false, |
| 172 |
) |
| 173 |
); |
| 174 |
|
| 175 |
if ( $template_query->have_posts() ) |
| 176 |
{ |
| 177 |
while ( $template_query->have_posts() ) |
| 178 |
{ |
| 179 |
$template_query->the_post(); |
| 180 |
|
| 181 |
$conditions = get_post_meta( get_the_ID(), 'nectar_g_section_conditions', TRUE ); |
| 182 |
|
| 183 |
if ( is_array($conditions) ) |
| 184 |
{ |
| 185 |
foreach ( $conditions as $condition ) |
| 186 |
{ |
| 187 |
$condition = (array)$condition; |
| 188 |
if ( !isset($condition['options']) || !is_array($condition['options']) ) |
| 189 |
{ |
| 190 |
continue; |
| 191 |
} |
| 192 |
|
| 193 |
$include_met = false; |
| 194 |
$condition_met = false; |
| 195 |
foreach ( $condition['options'] as $option ) |
| 196 |
{ |
| 197 |
$option = (array)$option; |
| 198 |
|
| 199 |
if ( |
| 200 |
isset($option['type']) && $option['type'] === 'include' && |
| 201 |
isset($option['value']) && $option['value'] === 'include' |
| 202 |
) |
| 203 |
{ |
| 204 |
$include_met = true; |
| 205 |
} |
| 206 |
if ( |
| 207 |
isset($option['type']) && $option['type'] === 'condition' && |
| 208 |
isset($option['value']) && $option['value'] === 'post_type__property' |
| 209 |
) |
| 210 |
{ |
| 211 |
$condition_met = true; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if ( $include_met && $condition_met ) |
| 216 |
{ |
| 217 |
$use_property_hive_template = false; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
if ( $use_property_hive_template ) |
| 226 |
{ |
| 227 |
$file = 'single-property.php'; |
| 228 |
$find[] = $file; |
| 229 |
$find[] = PH_TEMPLATE_PATH . $file; |
| 230 |
} |
| 231 |
} |
| 232 |
elseif ( is_post_type_archive( 'property' ) || is_page( ph_get_page_id( 'search_results' ) ) ) |
| 233 |
{ |
| 234 |
$use_property_hive_template = true; |
| 235 |
|
| 236 |
// Check for single Bricks Builder property page template |
| 237 |
if ( defined('BRICKS_DB_TEMPLATE_SLUG') ) |
| 238 |
{ |
| 239 |
$template_query = new WP_Query( |
| 240 |
array( |
| 241 |
'post_type' => BRICKS_DB_TEMPLATE_SLUG, |
| 242 |
'post_status' => 'publish', |
| 243 |
'fields' => 'ids', |
| 244 |
'no_found_rows' => true, |
| 245 |
'update_post_meta_cache' => false, |
| 246 |
'update_post_term_cache' => false, |
| 247 |
'meta_query' => array( |
| 248 |
array( |
| 249 |
'key' => '_bricks_template_type', |
| 250 |
'compare' => 'archive', |
| 251 |
), |
| 252 |
), |
| 253 |
) |
| 254 |
); |
| 255 |
|
| 256 |
if ( $template_query->have_posts() ) |
| 257 |
{ |
| 258 |
while ( $template_query->have_posts() ) |
| 259 |
{ |
| 260 |
$template_query->the_post(); |
| 261 |
|
| 262 |
$template_settings = get_post_meta( get_the_ID(), '_bricks_template_settings', TRUE ); |
| 263 |
|
| 264 |
if ( |
| 265 |
isset($template_settings['templateConditions']) && |
| 266 |
is_array($template_settings['templateConditions']) && |
| 267 |
!empty($template_settings['templateConditions']) |
| 268 |
) |
| 269 |
{ |
| 270 |
foreach ( $template_settings['templateConditions'] as $templateCondition ) |
| 271 |
{ |
| 272 |
if ( |
| 273 |
isset($templateCondition['main']) && |
| 274 |
$templateCondition['main'] == 'archiveType' && |
| 275 |
is_array($templateCondition['archiveType']) && |
| 276 |
in_array('postType', $templateCondition['archiveType']) && |
| 277 |
is_array($templateCondition['archivePostTypes']) && |
| 278 |
in_array('property', $templateCondition['archivePostTypes']) |
| 279 |
) |
| 280 |
{ |
| 281 |
$use_property_hive_template = false; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
if ( $use_property_hive_template ) |
| 290 |
{ |
| 291 |
$file = 'archive-property.php'; |
| 292 |
$find[] = $file; |
| 293 |
$find[] = PH_TEMPLATE_PATH . $file; |
| 294 |
} |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
if ( $file ) |
| 299 |
{ |
| 300 |
$template = locate_template( array_unique($find) ); |
| 301 |
if ( ! $template ) |
| 302 |
{ |
| 303 |
$template = PH()->plugin_path() . '/templates/' . $file; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
$template = apply_filters( 'propertyhive_loaded_template', $template ); |
| 308 |
|
| 309 |
return $template; |
| 310 |
} |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
new PH_Template_Loader(); |