| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Builder\Managers; |
| 4 |
|
| 5 |
use Templately\Builder\Conditions\Condition; |
| 6 |
use Templately\Builder\ThemeBuilder; |
| 7 |
use Templately\Builder\Types\ThemeTemplate; |
| 8 |
|
| 9 |
class ConditionManager { |
| 10 |
const NONCE_KEY = 'templately_ajax_nonce'; |
| 11 |
const META_KEY = '_templately_conditions'; |
| 12 |
|
| 13 |
private $cache; |
| 14 |
|
| 15 |
private $locations = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var Condition[] |
| 19 |
*/ |
| 20 |
private $conditions = []; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var ThemeBuilder |
| 24 |
*/ |
| 25 |
private $builder; |
| 26 |
|
| 27 |
private $location_cache = []; |
| 28 |
|
| 29 |
public function __construct( $builder ) { |
| 30 |
$this->cache = new Cache(); |
| 31 |
$this->builder = $builder; |
| 32 |
|
| 33 |
add_action( 'wp_loaded', [ $this, 'register' ] ); |
| 34 |
// add_action( 'wp_ajax_templately_conditions', [ $this, 'get_conditions' ] ); |
| 35 |
// add_action( 'wp_ajax_templately_save_conditions', [ $this, 'save_conditions' ] ); |
| 36 |
// add_action( 'wp_ajax_templately_check_conditions', [ $this, 'check_conditions' ] ); |
| 37 |
} |
| 38 |
|
| 39 |
public function get_conditions() { |
| 40 |
$config = []; |
| 41 |
|
| 42 |
foreach ( $this->conditions as $condition ) { |
| 43 |
$config[ $condition->get_name() ] = $condition->get_config(); |
| 44 |
} |
| 45 |
|
| 46 |
// throw new \Exception('Something went wrong'); |
| 47 |
|
| 48 |
wp_send_json( $config ); |
| 49 |
} |
| 50 |
|
| 51 |
public function get_conditions_for_display( $template_id = null ): array { |
| 52 |
if ( ! is_numeric( $template_id ) ) { |
| 53 |
$template_id = null; |
| 54 |
} |
| 55 |
|
| 56 |
$config = []; |
| 57 |
foreach ( $this->conditions as $condition ) { |
| 58 |
$config[ $condition->get_name() ] = $condition->get_config(); |
| 59 |
} |
| 60 |
|
| 61 |
if ( ! $template_id ) { |
| 62 |
return [ |
| 63 |
'config' => $config |
| 64 |
]; |
| 65 |
} |
| 66 |
|
| 67 |
$template = $this->builder::$templates_manager->get( $template_id ); |
| 68 |
$conditions_meta = $template->get_meta( self::META_KEY ); |
| 69 |
|
| 70 |
$conditions_for_display = []; |
| 71 |
|
| 72 |
if ( is_array( $conditions_meta ) && ! empty( $conditions_meta ) ) { |
| 73 |
foreach ( $conditions_meta as $c ) { |
| 74 |
$conditions_for_display[] = $this->parse_condition( $c ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
return [ |
| 80 |
'config' => $config, |
| 81 |
'template_conditions' => $conditions_for_display |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
private function verify_nonce(): bool { |
| 86 |
return true; |
| 87 |
// return isset( $_REQUEST['_nonce'] ) && wp_verify_nonce( $_REQUEST['_nonce'], self::NONCE_KEY ); |
| 88 |
} |
| 89 |
|
| 90 |
public function save_conditions( $post_id, $conditions = [] ): bool { |
| 91 |
$conditions_to_save = []; |
| 92 |
foreach ( $conditions as $condition ) { |
| 93 |
if ( is_string( $condition ) ) { |
| 94 |
$conditions_to_save[] = rtrim( $condition, '/' ); |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
unset( $condition['id'] ); |
| 99 |
$conditions_to_save[] = rtrim( implode( '/', $condition ), '/' ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @var ThemeTemplate $template |
| 104 |
*/ |
| 105 |
$template = $this->builder->get_template( $post_id ); |
| 106 |
|
| 107 |
if ( empty( $conditions_to_save ) ) { |
| 108 |
$template->delete_meta( self::META_KEY ); |
| 109 |
$this->cache->remove( $post_id )->save(); |
| 110 |
} else { |
| 111 |
$template->update_meta( self::META_KEY, $conditions ); |
| 112 |
$this->cache->add( $template, $conditions )->save(); |
| 113 |
} |
| 114 |
|
| 115 |
$this->cache->regenerate(); |
| 116 |
|
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
public function check_conditions() { |
| 121 |
if ( ! $this->verify_nonce() ) { |
| 122 |
wp_send_json_error( [ 'Unauthorized to take action.' ], 401 ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
public function register() { |
| 127 |
$this->register_condition( 'general' ); |
| 128 |
} |
| 129 |
|
| 130 |
public function register_condition( string $id, $args = [] ) { |
| 131 |
if ( isset( $this->conditions[ $id ] ) ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
// if ( strpos( $id, '_' ) !== false ) { |
| 136 |
// $id = implode( '', array_map( 'ucfirst', explode( '_', $id ) ) ); |
| 137 |
// } |
| 138 |
|
| 139 |
$class_name = '\\Templately\\Builder\\Conditions\\' . ucfirst( $id ); |
| 140 |
/** @var Condition $condition */ |
| 141 |
$condition = new $class_name( $args ); |
| 142 |
$this->register_condition_instance( $condition ); |
| 143 |
|
| 144 |
foreach ( $condition->get_sub_conditions() as $key => $_sub_id ) { |
| 145 |
if ( is_numeric( $key ) ) { |
| 146 |
$id = $_sub_id; |
| 147 |
$args = []; |
| 148 |
} else { |
| 149 |
$id = $key; |
| 150 |
$args = $_sub_id; |
| 151 |
} |
| 152 |
$this->register_condition( $_sub_id, $args ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* @param Condition $object |
| 158 |
*/ |
| 159 |
public function register_condition_instance( Condition $object ) { |
| 160 |
$this->conditions[ $object->get_name() ] = $object; |
| 161 |
} |
| 162 |
|
| 163 |
public function get_templates_by_location( $location ): array { |
| 164 |
if ( isset( $this->location_cache[ $location ] ) ) { |
| 165 |
return $this->location_cache[ $location ]; |
| 166 |
} |
| 167 |
|
| 168 |
$template_ids = $this->get_template_ids( $location ); |
| 169 |
|
| 170 |
// dump( $location, $template_ids ); |
| 171 |
|
| 172 |
$templates = []; |
| 173 |
|
| 174 |
$location_settings = templately()->theme_builder::$location_manager->get_location( $location ); |
| 175 |
|
| 176 |
if ( ! empty( $template_ids ) ) { |
| 177 |
foreach ( $template_ids as $template_id => $priority ) { |
| 178 |
$template = $this->builder::$templates_manager->get( $template_id ); |
| 179 |
|
| 180 |
if ( $template ) { |
| 181 |
$templates[ $template_id ] = $template; |
| 182 |
} |
| 183 |
|
| 184 |
if ( empty( $location_settings['multiple'] ) ) { |
| 185 |
break; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
$this->location_cache[ $location ] = $templates; |
| 191 |
|
| 192 |
return $templates; |
| 193 |
} |
| 194 |
|
| 195 |
private function get_template_ids( $location ): array { |
| 196 |
$current_post_id = get_the_ID(); |
| 197 |
$template = $this->builder->get_template( $current_post_id ); |
| 198 |
if ( $template && $location === $template->get_location() ) { |
| 199 |
return [ $current_post_id => 1 ]; |
| 200 |
} |
| 201 |
|
| 202 |
return $this->get_location_templates( $location ); |
| 203 |
} |
| 204 |
|
| 205 |
public function get_location_templates( string $location ): array { |
| 206 |
$conditions_priority = []; |
| 207 |
$conditions_groups = $this->cache->get_by_location( $location ); |
| 208 |
|
| 209 |
if ( empty( $conditions_groups ) ) { |
| 210 |
return $conditions_priority; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
$excludes = []; |
| 215 |
|
| 216 |
// dump( $conditions_groups ); |
| 217 |
|
| 218 |
foreach ( $conditions_groups as $template_id => $conditions ) { |
| 219 |
foreach ( $conditions as $condition ) { |
| 220 |
$origin_c = $condition; |
| 221 |
|
| 222 |
extract( $this->parse_condition( $condition ) ); |
| 223 |
|
| 224 |
$is_include = $type === 'include'; |
| 225 |
$condition = $this->get_condition( $name ); |
| 226 |
|
| 227 |
if ( ! $condition ) { |
| 228 |
continue; |
| 229 |
} |
| 230 |
|
| 231 |
$is_passed = $condition->check(); |
| 232 |
$sub_condition = null; |
| 233 |
|
| 234 |
|
| 235 |
if ( $is_passed && $sub_name ) { |
| 236 |
$sub_condition = $this->get_condition( $sub_name ); |
| 237 |
|
| 238 |
|
| 239 |
if ( ! $sub_condition ) { |
| 240 |
continue; |
| 241 |
} |
| 242 |
|
| 243 |
$is_passed = $sub_condition->check( [ 'id' => $sub_id ] ); |
| 244 |
} |
| 245 |
|
| 246 |
if ( $is_passed ) { |
| 247 |
// Get the post ID in the current language |
| 248 |
if($template_id_locale = apply_filters( 'wpml_object_id', $template_id )){ |
| 249 |
$template_id = $template_id_locale; |
| 250 |
} |
| 251 |
|
| 252 |
$post_status = get_post_status( $template_id ); |
| 253 |
|
| 254 |
if ( $post_status !== 'publish' ) { |
| 255 |
continue; |
| 256 |
} |
| 257 |
|
| 258 |
if ( $is_include ) { |
| 259 |
$conditions_priority[ $template_id ] = $this->get_priority( $condition, $sub_condition, $sub_id ); |
| 260 |
} else { |
| 261 |
$excludes[] = $template_id; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
foreach ( $excludes as $exclude_id ) { |
| 268 |
unset( $conditions_priority[ $exclude_id ] ); |
| 269 |
} |
| 270 |
|
| 271 |
asort( $conditions_priority ); |
| 272 |
|
| 273 |
return $conditions_priority; |
| 274 |
} |
| 275 |
|
| 276 |
private function get_condition( $name ) { |
| 277 |
return $this->conditions[ $name ] ?? null; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* @param $condition |
| 282 |
* @param $sub_condition |
| 283 |
* @param $sub_id |
| 284 |
* |
| 285 |
* @return int|mixed |
| 286 |
*/ |
| 287 |
private function get_priority( $condition, $sub_condition, $sub_id ) { |
| 288 |
$priority = $condition->get_priority(); |
| 289 |
|
| 290 |
if ( $sub_condition ) { |
| 291 |
if ( $sub_condition->get_priority() < $priority ) { |
| 292 |
$priority = $sub_condition->get_priority(); |
| 293 |
} |
| 294 |
|
| 295 |
$priority -= 10; |
| 296 |
|
| 297 |
if ( $sub_id ) { |
| 298 |
$priority -= 10; |
| 299 |
} elseif ( 0 === count( $sub_condition->get_sub_conditions() ) ) { |
| 300 |
$priority -= 5; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return $priority; |
| 305 |
} |
| 306 |
|
| 307 |
protected function parse_condition( $condition ): array { |
| 308 |
if ( is_string( $condition ) ) { |
| 309 |
list( $type, $name, $sub_name, $sub_id ) = array_pad( explode( '/', $condition ), 4, '' ); |
| 310 |
$condition = compact( 'type', 'name', 'sub_name', 'sub_id' ); |
| 311 |
} |
| 312 |
|
| 313 |
return $condition; |
| 314 |
} |
| 315 |
|
| 316 |
} |