PluginProbe
Property Hive / 2.3.0
Property Hive v2.3.0
2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 All 260 releases
propertyhive / includes / class-ph-template-loader.php

class-ph-template-loader.php in Property Hive 2.3.0, at includes/class-ph-template-loader.php

325 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- Preserve the existing public PH_Template_Loader class name for plugin and extension compatibility.
15 class PH_Template_Loader {
16
17 /**
18 * Constructor
19 */
20 public function __construct() {
21
22 add_action( 'init', array( $this, 'init' ) );
23 }
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
42 /**
43 * Load a template.
44 *
45 * Handles template usage so that we can use our own templates instead of the themes.
46 *
47 * Templates are in the 'templates' folder. propertyhive looks for theme
48 * overrides in /theme/propertyhive/ by default
49 *
50 * For beginners, it also looks for a propertyhive.php template first. If the user adds
51 * this to the theme (containing a propertyhive() inside) this will be used for all
52 * propertyhive templates.
53 *
54 * @param mixed $template
55 * @return string
56 */
57 public function template_loader( $template ) {
58
59 $find = array( 'propertyhive.php' );
60 $file = '';
61
62 if ( is_single() && get_post_type() == 'property' )
63 {
64 $use_property_hive_template = true;
65
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 );
102
103 if ( $template_query->have_posts() )
104 {
105 $use_property_hive_template = false;
106 }
107 wp_reset_postdata();
108 }
109
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 );
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
306 }
307
308 if ( $file )
309 {
310 $template = locate_template( array_unique($find) );
311 if ( ! $template )
312 {
313 $template = PH()->plugin_path() . '/templates/' . $file;
314 }
315 }
316
317 $template = apply_filters( 'propertyhive_loaded_template', $template );
318
319 return $template;
320 }
321
322 }
323
324 new PH_Template_Loader();
325