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