| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\DisplayConditions\Managers; |
| 4 |
|
| 5 |
use Templately\Modules\ThemeBuilder\ThemeBuilder; |
| 6 |
use Templately\Modules\ThemeBuilder\Types\ThemeTemplate; |
| 7 |
use Templately\Modules\DisplayConditions\Conditions\Condition; |
| 8 |
|
| 9 |
class ConditionManager { |
| 10 |
const META_KEY = '_templately_conditions'; |
| 11 |
|
| 12 |
private $cache; |
| 13 |
|
| 14 |
private $locations = []; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var Condition[] |
| 18 |
*/ |
| 19 |
private $conditions = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var ThemeBuilder |
| 23 |
*/ |
| 24 |
private $builder; |
| 25 |
|
| 26 |
private $location_cache = []; |
| 27 |
|
| 28 |
public function __construct( $builder ) { |
| 29 |
$this->cache = new Cache(); |
| 30 |
$this->builder = $builder; |
| 31 |
|
| 32 |
add_action( 'wp_loaded', [ $this, 'register' ] ); |
| 33 |
|
| 34 |
// The three `wp_ajax_templately_*conditions*` actions that used to be |
| 35 |
// registered here were already commented out. Their handlers |
| 36 |
// (`get_conditions()`, `check_conditions()`) and the `verify_nonce()` helper |
| 37 |
// were removed with them (spec 043 / PRD PHP-5): `verify_nonce()` was |
| 38 |
// hardcoded to `return true` with the real check commented out, so |
| 39 |
// re-enabling any of those lines would have exposed an unauthenticated |
| 40 |
// endpoint. Conditions are read and written through |
| 41 |
// `modules/display-conditions/REST/Conditions.php`, which is nonce- and |
| 42 |
// capability-checked by the REST framework. |
| 43 |
} |
| 44 |
|
| 45 |
public function get_conditions_for_display( $template_id = null ): array { |
| 46 |
if ( ! is_numeric( $template_id ) ) { |
| 47 |
$template_id = null; |
| 48 |
} |
| 49 |
|
| 50 |
$config = []; |
| 51 |
foreach ( $this->conditions as $condition ) { |
| 52 |
$config[ $condition->get_name() ] = $condition->get_config(); |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! $template_id ) { |
| 56 |
return [ |
| 57 |
'config' => $config |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
$template = $this->builder::$templates_manager->get( $template_id ); |
| 62 |
$conditions_meta = $template->get_meta( self::META_KEY ); |
| 63 |
|
| 64 |
$conditions_for_display = []; |
| 65 |
|
| 66 |
if ( is_array( $conditions_meta ) && ! empty( $conditions_meta ) ) { |
| 67 |
foreach ( $conditions_meta as $c ) { |
| 68 |
$conditions_for_display[] = $this->parse_condition( $c ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
return [ |
| 74 |
'config' => $config, |
| 75 |
'template_conditions' => $conditions_for_display |
| 76 |
]; |
| 77 |
} |
| 78 |
|
| 79 |
public function save_conditions( $post_id, $conditions = [] ): bool { |
| 80 |
$conditions_to_save = []; |
| 81 |
foreach ( $conditions as $condition ) { |
| 82 |
if ( is_string( $condition ) ) { |
| 83 |
$conditions_to_save[] = rtrim( $condition, '/' ); |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
unset( $condition['id'] ); |
| 88 |
$conditions_to_save[] = rtrim( implode( '/', $condition ), '/' ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @var ThemeTemplate $template |
| 93 |
*/ |
| 94 |
$template = $this->builder->get_template( $post_id ); |
| 95 |
|
| 96 |
if ( empty( $conditions_to_save ) ) { |
| 97 |
$template->delete_meta( self::META_KEY ); |
| 98 |
$this->cache->remove( $post_id )->save(); |
| 99 |
} else { |
| 100 |
$template->update_meta( self::META_KEY, $conditions ); |
| 101 |
$this->cache->add( $template, $conditions )->save(); |
| 102 |
} |
| 103 |
|
| 104 |
$this->cache->regenerate(); |
| 105 |
|
| 106 |
// Conditions just changed → the in-memory per-location winners are stale. Drop them |
| 107 |
// so a subsequent get_templates_by_location() in the same request re-resolves against |
| 108 |
// the new conditions (otherwise the first-call cache wins and ignores the edit). |
| 109 |
$this->location_cache = []; |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
public function register() { |
| 115 |
$this->register_condition( 'general' ); |
| 116 |
} |
| 117 |
|
| 118 |
public function register_condition( string $id, $args = [] ) { |
| 119 |
if ( isset( $this->conditions[ $id ] ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
// if ( strpos( $id, '_' ) !== false ) { |
| 124 |
// $id = implode( '', array_map( 'ucfirst', explode( '_', $id ) ) ); |
| 125 |
// } |
| 126 |
|
| 127 |
$class_name = '\\Templately\\Modules\\DisplayConditions\\Conditions\\' . ucfirst( $id ); |
| 128 |
/** @var Condition $condition */ |
| 129 |
$condition = new $class_name( $args ); |
| 130 |
$this->register_condition_instance( $condition ); |
| 131 |
|
| 132 |
foreach ( $condition->get_sub_conditions() as $key => $_sub_id ) { |
| 133 |
if ( is_numeric( $key ) ) { |
| 134 |
$id = $_sub_id; |
| 135 |
$args = []; |
| 136 |
} else { |
| 137 |
$id = $key; |
| 138 |
$args = $_sub_id; |
| 139 |
} |
| 140 |
$this->register_condition( $_sub_id, $args ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @param Condition $object |
| 146 |
*/ |
| 147 |
public function register_condition_instance( Condition $object ) { |
| 148 |
$this->conditions[ $object->get_name() ] = $object; |
| 149 |
} |
| 150 |
|
| 151 |
public function get_templates_by_location( $location ): array { |
| 152 |
if ( isset( $this->location_cache[ $location ] ) ) { |
| 153 |
return $this->location_cache[ $location ]; |
| 154 |
} |
| 155 |
|
| 156 |
$template_ids = $this->get_template_ids( $location ); |
| 157 |
|
| 158 |
// dump( $location, $template_ids ); |
| 159 |
|
| 160 |
$templates = []; |
| 161 |
|
| 162 |
$location_settings = templately()->theme_builder::$location_manager->get_location( $location ); |
| 163 |
|
| 164 |
if ( ! empty( $template_ids ) ) { |
| 165 |
foreach ( $template_ids as $template_id => $priority ) { |
| 166 |
$template = $this->builder::$templates_manager->get( $template_id ); |
| 167 |
|
| 168 |
if ( $template ) { |
| 169 |
$templates[ $template_id ] = $template; |
| 170 |
} |
| 171 |
|
| 172 |
if ( empty( $location_settings['multiple'] ) ) { |
| 173 |
break; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
$this->location_cache[ $location ] = $templates; |
| 179 |
|
| 180 |
return $templates; |
| 181 |
} |
| 182 |
|
| 183 |
private function get_template_ids( $location ): array { |
| 184 |
$current_post_id = get_the_ID(); |
| 185 |
$template = $this->builder->get_template( $current_post_id ); |
| 186 |
if ( $template && $location === $template->get_location() ) { |
| 187 |
return [ $current_post_id => 1 ]; |
| 188 |
} |
| 189 |
|
| 190 |
return $this->get_location_templates( $location ); |
| 191 |
} |
| 192 |
|
| 193 |
public function get_location_templates( string $location ): array { |
| 194 |
$conditions_priority = []; |
| 195 |
$conditions_groups = $this->cache->get_by_location( $location ); |
| 196 |
|
| 197 |
if ( empty( $conditions_groups ) ) { |
| 198 |
return $conditions_priority; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
$excludes = []; |
| 203 |
|
| 204 |
// dump( $conditions_groups ); |
| 205 |
|
| 206 |
foreach ( $conditions_groups as $template_id => $conditions ) { |
| 207 |
foreach ( $conditions as $condition ) { |
| 208 |
$origin_c = $condition; |
| 209 |
|
| 210 |
extract( $this->parse_condition( $condition ) ); |
| 211 |
|
| 212 |
$is_include = $type === 'include'; |
| 213 |
$condition = $this->get_condition( $name ); |
| 214 |
|
| 215 |
if ( ! $condition ) { |
| 216 |
continue; |
| 217 |
} |
| 218 |
|
| 219 |
$is_passed = $condition->check(); |
| 220 |
$sub_condition = null; |
| 221 |
|
| 222 |
|
| 223 |
if ( $is_passed && $sub_name ) { |
| 224 |
$sub_condition = $this->get_condition( $sub_name ); |
| 225 |
|
| 226 |
|
| 227 |
if ( ! $sub_condition ) { |
| 228 |
continue; |
| 229 |
} |
| 230 |
|
| 231 |
$is_passed = $sub_condition->check( [ 'id' => $sub_id ] ); |
| 232 |
} |
| 233 |
|
| 234 |
if ( $is_passed ) { |
| 235 |
// Get the post ID in the current language |
| 236 |
if($template_id_locale = apply_filters( 'wpml_object_id', $template_id )){ |
| 237 |
$template_id = $template_id_locale; |
| 238 |
} |
| 239 |
|
| 240 |
$post_status = get_post_status( $template_id ); |
| 241 |
|
| 242 |
if ( $post_status !== 'publish' ) { |
| 243 |
continue; |
| 244 |
} |
| 245 |
|
| 246 |
if ( $is_include ) { |
| 247 |
$conditions_priority[ $template_id ] = $this->get_priority( $condition, $sub_condition, $sub_id ); |
| 248 |
} else { |
| 249 |
$excludes[] = $template_id; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
foreach ( $excludes as $exclude_id ) { |
| 256 |
unset( $conditions_priority[ $exclude_id ] ); |
| 257 |
} |
| 258 |
|
| 259 |
$this->sort_by_priority( $conditions_priority ); |
| 260 |
|
| 261 |
return $conditions_priority; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Sort `[template_id => priority]` by priority ascending (lower priority |
| 266 |
* value = more specific condition = wins — unchanged from the previous |
| 267 |
* `asort()` behavior), with a deterministic tie-break on EQUAL priority: |
| 268 |
* the lowest post/template ID wins, regardless of insertion order. |
| 269 |
* |
| 270 |
* `asort()` is stable-by-insertion-order for equal values, so which |
| 271 |
* template won a tie used to depend on iteration order over |
| 272 |
* `$conditions_groups` (effectively arbitrary — see spec 034 SC-003). |
| 273 |
* |
| 274 |
* The tie itself is now settled by active flag, then recency, then ID. Lowest-ID |
| 275 |
* alone meant the OLDEST template won, so a newly imported header could never |
| 276 |
* displace the one a previous import left behind. Still deterministic — the |
| 277 |
* property SC-003 actually cares about — just no longer ordered by age ascending. |
| 278 |
* |
| 279 |
* @param array $conditions_priority [template_id => priority], sorted in place. |
| 280 |
*/ |
| 281 |
/** Memoized per request — `usort` asks the same question many times. */ |
| 282 |
private static $active_cache = []; |
| 283 |
private static $modified_cache = []; |
| 284 |
|
| 285 |
/** Whether a template carries the active flag for its type. */ |
| 286 |
private static function is_active_template( $template_id ): bool { |
| 287 |
if ( ! isset( self::$active_cache[ $template_id ] ) ) { |
| 288 |
self::$active_cache[ $template_id ] = |
| 289 |
'1' === (string) get_post_meta( (int) $template_id, '_templately_is_active_template', true ); |
| 290 |
} |
| 291 |
|
| 292 |
return self::$active_cache[ $template_id ]; |
| 293 |
} |
| 294 |
|
| 295 |
/** A template's last-modified time, as a comparable integer. */ |
| 296 |
private static function template_modified( $template_id ): int { |
| 297 |
if ( ! isset( self::$modified_cache[ $template_id ] ) ) { |
| 298 |
$post = get_post( (int) $template_id ); |
| 299 |
self::$modified_cache[ $template_id ] = |
| 300 |
( $post && ! empty( $post->post_modified_gmt ) ) ? (int) strtotime( $post->post_modified_gmt ) : 0; |
| 301 |
} |
| 302 |
|
| 303 |
return self::$modified_cache[ $template_id ]; |
| 304 |
} |
| 305 |
|
| 306 |
private function sort_by_priority( array &$conditions_priority ) { |
| 307 |
$entries = []; |
| 308 |
foreach ( $conditions_priority as $template_id => $priority ) { |
| 309 |
$entries[] = [ $template_id, $priority ]; |
| 310 |
} |
| 311 |
|
| 312 |
usort( $entries, function ( $a, $b ) { |
| 313 |
if ( $a[1] === $b[1] ) { |
| 314 |
// Tie: same location, same specificity. Conditions have said all they can, |
| 315 |
// so fall through three rules, each fully deterministic. |
| 316 |
|
| 317 |
// 1. The template marked active for its type. This is the whole reason the |
| 318 |
// flag exists: an imported pack's header must take over from the one a |
| 319 |
// previous import left behind. |
| 320 |
$a_active = self::is_active_template( $a[0] ); |
| 321 |
$b_active = self::is_active_template( $b[0] ); |
| 322 |
|
| 323 |
if ( $a_active !== $b_active ) { |
| 324 |
return $a_active ? -1 : 1; |
| 325 |
} |
| 326 |
|
| 327 |
// 2. The more recently modified one. Covers every site that predates the |
| 328 |
// flag, where nothing is marked at all — and "the one I touched last" |
| 329 |
// is a far better guess than "the one I imported first". |
| 330 |
$a_time = self::template_modified( $a[0] ); |
| 331 |
$b_time = self::template_modified( $b[0] ); |
| 332 |
|
| 333 |
if ( $a_time !== $b_time ) { |
| 334 |
return $b_time <=> $a_time; |
| 335 |
} |
| 336 |
|
| 337 |
// 3. Lowest post ID, so two templates saved in the same second still |
| 338 |
// resolve the same way on every request. |
| 339 |
return $a[0] <=> $b[0]; |
| 340 |
} |
| 341 |
|
| 342 |
// Lower priority value = more specific condition = wins. |
| 343 |
return $a[1] <=> $b[1]; |
| 344 |
} ); |
| 345 |
|
| 346 |
$conditions_priority = []; |
| 347 |
foreach ( $entries as $entry ) { |
| 348 |
$conditions_priority[ $entry[0] ] = $entry[1]; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
private function get_condition( $name ) { |
| 353 |
return $this->conditions[ $name ] ?? null; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* @param $condition |
| 358 |
* @param $sub_condition |
| 359 |
* @param $sub_id |
| 360 |
* |
| 361 |
* @return int|mixed |
| 362 |
*/ |
| 363 |
private function get_priority( $condition, $sub_condition, $sub_id ) { |
| 364 |
$priority = $condition->get_priority(); |
| 365 |
|
| 366 |
if ( $sub_condition ) { |
| 367 |
if ( $sub_condition->get_priority() < $priority ) { |
| 368 |
$priority = $sub_condition->get_priority(); |
| 369 |
} |
| 370 |
|
| 371 |
$priority -= 10; |
| 372 |
|
| 373 |
if ( $sub_id ) { |
| 374 |
$priority -= 10; |
| 375 |
} elseif ( 0 === count( $sub_condition->get_sub_conditions() ) ) { |
| 376 |
$priority -= 5; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
return $priority; |
| 381 |
} |
| 382 |
|
| 383 |
protected function parse_condition( $condition ): array { |
| 384 |
if ( is_string( $condition ) ) { |
| 385 |
list( $type, $name, $sub_name, $sub_id ) = array_pad( explode( '/', $condition ), 4, '' ); |
| 386 |
$condition = compact( 'type', 'name', 'sub_name', 'sub_id' ); |
| 387 |
} |
| 388 |
|
| 389 |
return $condition; |
| 390 |
} |
| 391 |
|
| 392 |
} |