API
2 years ago
Admin
1 year ago
Blocks
11 months ago
CLI
10 months ago
Container
10 months ago
Data
11 months ago
Integrations
1 year ago
REST
11 months ago
Theme
3 years ago
Tools
2 years ago
WP
1 year ago
Whatsit
1 year ago
Config_Handler.php
1 year ago
Integration.php
1 year ago
Permissions.php
2 years ago
Pod_Manager.php
2 years ago
Service_Provider.php
1 year ago
Service_Provider_Base.php
3 years ago
Static_Cache.php
4 years ago
Whatsit.php
1 year ago
Wisdom_Tracker.php
1 year ago
Config_Handler.php
857 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Pods; |
| 4 | |
| 5 | use Pods__Prefixed__Spyc as Spyc; |
| 6 | |
| 7 | /** |
| 8 | * Class to handle registration of Pods configs and loading/saving of config files. |
| 9 | * |
| 10 | * @package Pods |
| 11 | */ |
| 12 | class Config_Handler { |
| 13 | |
| 14 | /** |
| 15 | * List of registered config types. |
| 16 | * |
| 17 | * @since 2.9.0 |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $registered_config_types = [ |
| 22 | 'json' => 'json', |
| 23 | 'yml' => 'yml', |
| 24 | ]; |
| 25 | |
| 26 | /** |
| 27 | * List of registered config item types. |
| 28 | * |
| 29 | * @since 2.9.0 |
| 30 | * |
| 31 | * @var array |
| 32 | */ |
| 33 | protected $registered_config_item_types = [ |
| 34 | 'pods' => 'pods', |
| 35 | 'groups' => 'groups', |
| 36 | 'fields' => 'fields', |
| 37 | 'templates' => 'templates', |
| 38 | 'pages' => 'pages', |
| 39 | ]; |
| 40 | |
| 41 | /** |
| 42 | * List of registered paths. |
| 43 | * |
| 44 | * @since 2.9.0 |
| 45 | * |
| 46 | * @var array |
| 47 | */ |
| 48 | protected $registered_paths = []; |
| 49 | |
| 50 | /** |
| 51 | * List of registered files. |
| 52 | * |
| 53 | * @since 2.9.0 |
| 54 | * |
| 55 | * @var array |
| 56 | */ |
| 57 | protected $registered_files = []; |
| 58 | |
| 59 | /** |
| 60 | * List of registered Pods configs. |
| 61 | * |
| 62 | * @since 2.9.0 |
| 63 | * |
| 64 | * @var array |
| 65 | */ |
| 66 | protected $pods = []; |
| 67 | |
| 68 | /** |
| 69 | * List of registered Pods Template configs. |
| 70 | * |
| 71 | * @since 2.9.0 |
| 72 | * |
| 73 | * @var array |
| 74 | */ |
| 75 | protected $templates = []; |
| 76 | |
| 77 | /** |
| 78 | * List of registered Pods Page configs. |
| 79 | * |
| 80 | * @since 2.9.0 |
| 81 | * |
| 82 | * @var array |
| 83 | */ |
| 84 | protected $pages = []; |
| 85 | |
| 86 | /** |
| 87 | * List of registered Pods Helper configs. |
| 88 | * |
| 89 | * @since 2.9.0 |
| 90 | * |
| 91 | * @var array |
| 92 | */ |
| 93 | protected $helpers = []; |
| 94 | |
| 95 | /** |
| 96 | * Associative array list of other registered configs. |
| 97 | * |
| 98 | * @since 2.9.0 |
| 99 | * |
| 100 | * @var array |
| 101 | */ |
| 102 | protected $custom_configs = []; |
| 103 | |
| 104 | /** |
| 105 | * List of config names for each file path. |
| 106 | * |
| 107 | * @since 2.9.0 |
| 108 | * |
| 109 | * @var array |
| 110 | */ |
| 111 | protected $file_path_configs = []; |
| 112 | |
| 113 | /** |
| 114 | * Config constructor. |
| 115 | * |
| 116 | * @since 2.9.0 |
| 117 | */ |
| 118 | public function __construct() { |
| 119 | // Nothing to see here. |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Setup initial registered paths and load configs. |
| 124 | * |
| 125 | * @since 2.9.0 |
| 126 | */ |
| 127 | public function setup() { |
| 128 | // Register theme. |
| 129 | $this->register_path( get_template_directory() ); |
| 130 | |
| 131 | if ( get_template_directory() !== get_stylesheet_directory() ) { |
| 132 | // Register child theme. |
| 133 | $this->register_path( get_stylesheet_directory() ); |
| 134 | } |
| 135 | |
| 136 | $this->load_configs(); |
| 137 | $this->store_configs(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Register a config type. |
| 142 | * |
| 143 | * @since 2.9.0 |
| 144 | * |
| 145 | * @param string $config_type Config type. |
| 146 | */ |
| 147 | public function register_config_type( $config_type ) { |
| 148 | $config_type = sanitize_title( $config_type ); |
| 149 | $config_type = str_replace( [ '/', DIRECTORY_SEPARATOR ], '-', $config_type ); |
| 150 | |
| 151 | $this->registered_config_types[ $config_type ] = $config_type; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Unregister a config type. |
| 156 | * |
| 157 | * @since 2.9.0 |
| 158 | * |
| 159 | * @param string $config_type Config type. |
| 160 | */ |
| 161 | public function unregister_config_type( $config_type ) { |
| 162 | $config_type = sanitize_title( $config_type ); |
| 163 | $config_type = str_replace( [ '/', DIRECTORY_SEPARATOR ], '-', $config_type ); |
| 164 | |
| 165 | if ( isset( $this->registered_config_types[ $config_type ] ) ) { |
| 166 | unset( $this->registered_config_types[ $config_type ] ); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Register a config item type. |
| 172 | * |
| 173 | * @since 2.9.0 |
| 174 | * |
| 175 | * @param string $item_type Config item type. |
| 176 | */ |
| 177 | public function register_config_item_type( $item_type ) { |
| 178 | $item_type = sanitize_title( $item_type ); |
| 179 | $item_type = str_replace( [ '/', DIRECTORY_SEPARATOR ], '-', $item_type ); |
| 180 | |
| 181 | $this->registered_config_item_types[ $item_type ] = $item_type; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Unregister a config item type. |
| 186 | * |
| 187 | * @since 2.9.0 |
| 188 | * |
| 189 | * @param string $item_type Config item type. |
| 190 | */ |
| 191 | public function unregister_config_item_type( $item_type ) { |
| 192 | $item_type = sanitize_title( $item_type ); |
| 193 | $item_type = str_replace( [ '/', DIRECTORY_SEPARATOR ], '-', $item_type ); |
| 194 | |
| 195 | if ( isset( $this->registered_config_item_types[ $item_type ] ) ) { |
| 196 | unset( $this->registered_config_item_types[ $item_type ] ); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Register a config file path. |
| 202 | * |
| 203 | * @since 2.9.0 |
| 204 | * |
| 205 | * @param string $path The config file path to use. |
| 206 | */ |
| 207 | public function register_path( $path ) { |
| 208 | $path = trailingslashit( $path ); |
| 209 | |
| 210 | if ( 0 !== strpos( $path, ABSPATH ) ) { |
| 211 | $path = ABSPATH . $path; |
| 212 | } |
| 213 | |
| 214 | $this->registered_paths[ $path ] = $path; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Unregister a config file path. |
| 219 | * |
| 220 | * @since 2.9.0 |
| 221 | * |
| 222 | * @param string $path The config file path to use. |
| 223 | */ |
| 224 | public function unregister_path( $path ) { |
| 225 | $path = trailingslashit( $path ); |
| 226 | |
| 227 | if ( 0 !== strpos( $path, ABSPATH ) ) { |
| 228 | $path = ABSPATH . $path; |
| 229 | } |
| 230 | |
| 231 | if ( isset( $this->registered_paths[ $path ] ) ) { |
| 232 | unset( $this->registered_paths[ $path ] ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Register a config file. |
| 238 | * |
| 239 | * @since 2.9.0 |
| 240 | * |
| 241 | * @param string $file Config file to use. |
| 242 | * @param string $config_type Config type to use. |
| 243 | */ |
| 244 | public function register_file( $file, $config_type ) { |
| 245 | if ( ! isset( $this->registered_files[ $config_type ] ) ) { |
| 246 | $this->registered_files[ $config_type ] = []; |
| 247 | } |
| 248 | |
| 249 | $this->registered_files[ $config_type ][ $file ] = $file; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Unregister a config file file. |
| 254 | * |
| 255 | * @since 2.9.0 |
| 256 | * |
| 257 | * @param string $file Config file to use. |
| 258 | * @param string $config_type Config type to use. |
| 259 | */ |
| 260 | public function unregister_file( $file, $config_type ) { |
| 261 | if ( isset( $this->registered_files[ $config_type ][ $file ] ) ) { |
| 262 | unset( $this->registered_files[ $config_type ][ $file ] ); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Get file configs based on registered config types and config item types. |
| 268 | * |
| 269 | * @since 2.9.0 |
| 270 | * |
| 271 | * @return array File configs. |
| 272 | */ |
| 273 | protected function get_file_configs() { |
| 274 | $file_configs = []; |
| 275 | |
| 276 | // Flesh out the config types. |
| 277 | foreach ( $this->registered_config_types as $config_type ) { |
| 278 | foreach ( $this->registered_config_item_types as $config_item_type ) { |
| 279 | $theme_support = false; |
| 280 | |
| 281 | // Themes get pods.json / pods.yml support at root. |
| 282 | if ( 'pods' === $config_item_type ) { |
| 283 | $theme_support = true; |
| 284 | } |
| 285 | |
| 286 | $path_type = $config_item_type; |
| 287 | |
| 288 | if ( 'pods' !== $path_type ) { |
| 289 | $path_type = 'pods-' . $path_type; |
| 290 | } |
| 291 | |
| 292 | $path = sprintf( '%s.%s', $path_type, $config_type ); |
| 293 | |
| 294 | $file_configs[] = [ |
| 295 | 'type' => $config_type, |
| 296 | 'file' => $path, |
| 297 | 'item_type' => $config_item_type, |
| 298 | 'theme_support' => $theme_support, |
| 299 | ]; |
| 300 | |
| 301 | // Prepend pods/ to path for theme paths. |
| 302 | $path = sprintf( 'pods%s%s', DIRECTORY_SEPARATOR, $path ); |
| 303 | |
| 304 | $file_configs[] = [ |
| 305 | 'type' => $config_type, |
| 306 | 'file' => $path, |
| 307 | 'item_type' => $config_item_type, |
| 308 | 'theme_support' => true, |
| 309 | ]; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return $file_configs; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Load configs from registered file paths. |
| 318 | * |
| 319 | * @since 2.9.0 |
| 320 | */ |
| 321 | protected function load_configs() { |
| 322 | /** |
| 323 | * Allow plugins/themes to hook into config loading. |
| 324 | * |
| 325 | * @since 2.9.0 |
| 326 | * |
| 327 | * @param Config_Handler $pods_config Pods config object. |
| 328 | * |
| 329 | */ |
| 330 | do_action( 'pods_config_pre_load_configs', $this ); |
| 331 | |
| 332 | $file_configs = $this->get_file_configs(); |
| 333 | |
| 334 | $theme_dirs = [ |
| 335 | trailingslashit( get_template_directory() ) => true, |
| 336 | trailingslashit( get_stylesheet_directory() ) => true, |
| 337 | ]; |
| 338 | |
| 339 | $cached_found_configs = pods_transient_get( 'pods_config_handler_found_configs' ); |
| 340 | |
| 341 | if ( empty( $cached_found_configs ) ) { |
| 342 | $cached_found_configs = false; |
| 343 | } |
| 344 | |
| 345 | $found_configs = []; |
| 346 | |
| 347 | $refresh_cache = false; |
| 348 | |
| 349 | foreach ( $this->registered_paths as $config_path ) { |
| 350 | foreach ( $file_configs as $file_config ) { |
| 351 | if ( empty( $file_config['theme_support'] ) && isset( $theme_dirs[ $config_path ] ) ) { |
| 352 | continue; |
| 353 | } |
| 354 | |
| 355 | $file_path = $config_path . $file_config['file']; |
| 356 | |
| 357 | if ( $cached_found_configs && ! isset( $cached_found_configs[ $file_path ] ) ) { |
| 358 | continue; |
| 359 | } |
| 360 | |
| 361 | $found_config = $this->load_config_file( $file_path, $file_config['type'], $file_config ); |
| 362 | |
| 363 | if ( $found_config ) { |
| 364 | $found_configs[ $file_path ] = true; |
| 365 | } elseif ( $cached_found_configs ) { |
| 366 | $refresh_cache = true; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if ( |
| 372 | $refresh_cache |
| 373 | || ( |
| 374 | ! empty( $found_configs ) |
| 375 | && $found_configs !== $cached_found_configs |
| 376 | ) |
| 377 | ) { |
| 378 | pods_transient_set( 'pods_config_handler_found_configs', $found_configs, WEEK_IN_SECONDS ); |
| 379 | } |
| 380 | |
| 381 | foreach ( $this->registered_files as $config_type => $files ) { |
| 382 | foreach ( $files as $file ) { |
| 383 | $this->load_config_file( $file, $config_type ); |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Load the config file for a config type from a specific file path. |
| 390 | * |
| 391 | * @param string $file_path The config file path to use. |
| 392 | * @param string $config_type The config type to use. |
| 393 | * @param array $file_config File config. |
| 394 | * |
| 395 | * @return bool Whether the config file was found and loaded. |
| 396 | */ |
| 397 | protected function load_config_file( $file_path, $config_type, array $file_config = [] ) { |
| 398 | if ( ! file_exists( $file_path ) || ! is_readable( $file_path ) ) { |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | $raw_config = file_get_contents( $file_path ); |
| 403 | |
| 404 | if ( empty( $raw_config ) ) { |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | $file_config['type'] = $config_type; |
| 409 | |
| 410 | return $this->load_config( $config_type, $raw_config, $file_path, $file_config ); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Load config from registered file path. |
| 415 | * |
| 416 | * @since 2.9.0 |
| 417 | * |
| 418 | * @param string $config_type Config type. |
| 419 | * @param string $raw_config Raw config content. |
| 420 | * @param string $file_path The config file path to use. |
| 421 | * @param array $file_config File config. |
| 422 | * |
| 423 | * @return bool Whether the config was loaded. |
| 424 | */ |
| 425 | protected function load_config( $config_type, $raw_config, $file_path, array $file_config ) { |
| 426 | $config = null; |
| 427 | |
| 428 | if ( 'yml' === $config_type ) { |
| 429 | $config = Spyc::YAMLLoadString( $raw_config ); |
| 430 | } elseif ( 'json' === $config_type ) { |
| 431 | $config = json_decode( $raw_config, true ); |
| 432 | } else { |
| 433 | /** |
| 434 | * Parse Pods config from a custom config type. |
| 435 | * |
| 436 | * @since 2.9.0 |
| 437 | * |
| 438 | * @param array $config Config data. |
| 439 | * @param string $raw_config Raw config content. |
| 440 | */ |
| 441 | $config = apply_filters( "pods_config_parse_{$config_type}", [], $raw_config ); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Allow filtering the config for additional parsing customization. |
| 446 | * |
| 447 | * @since 2.9.0 |
| 448 | * |
| 449 | * @param array $config Config data. |
| 450 | * @param string $config_type Config type. |
| 451 | * @param string $raw_config Raw config content. |
| 452 | */ |
| 453 | $config = apply_filters( 'pods_config_parse', $config, $config_type, $raw_config ); |
| 454 | |
| 455 | if ( $config && is_array( $config ) ) { |
| 456 | $this->register_config( $config, $file_path, $file_config ); |
| 457 | |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Register config for different item types. |
| 466 | * |
| 467 | * @since 2.9.0 |
| 468 | * |
| 469 | * @param array $config Config data. |
| 470 | * @param string $file_path The config file path to use. |
| 471 | * @param array $file_config File config. |
| 472 | */ |
| 473 | protected function register_config( array $config, $file_path, array $file_config = [] ) { |
| 474 | if ( ! isset( $this->file_path_configs[ $file_path ] ) ) { |
| 475 | $this->file_path_configs[ $file_path ] = []; |
| 476 | } |
| 477 | |
| 478 | foreach ( $config as $item_type => $items ) { |
| 479 | if ( empty( $items ) || ! is_array( $items ) ) { |
| 480 | continue; |
| 481 | } |
| 482 | |
| 483 | if ( 0 === strpos( (string) $item_type, '@' ) ) { |
| 484 | continue; |
| 485 | } |
| 486 | |
| 487 | $supported_item_types = [ |
| 488 | $item_type, |
| 489 | // We support all item types for pods configs. |
| 490 | 'pods', |
| 491 | ]; |
| 492 | |
| 493 | // Skip if the item type is not supported for this config file. |
| 494 | if ( ! empty( $file_config['item_type'] ) && ! in_array( $file_config['item_type'], $supported_item_types, true ) ) { |
| 495 | continue; |
| 496 | } |
| 497 | |
| 498 | if ( ! isset( $this->file_path_configs[ $file_path ][ $item_type ] ) ) { |
| 499 | $this->file_path_configs[ $file_path ][ $item_type ] = []; |
| 500 | } |
| 501 | |
| 502 | if ( 'pods' === $item_type ) { |
| 503 | $this->register_config_pods( $items, $file_path ); |
| 504 | } elseif ( 'groups' === $item_type ) { |
| 505 | $this->register_config_groups( $items, $file_path ); |
| 506 | } elseif ( 'fields' === $item_type ) { |
| 507 | $this->register_config_fields( $items, $file_path ); |
| 508 | } elseif ( 'templates' === $item_type ) { |
| 509 | $this->register_config_templates( $items, $file_path ); |
| 510 | } elseif ( 'pages' === $item_type ) { |
| 511 | $this->register_config_pages( $items, $file_path ); |
| 512 | } elseif ( 'helpers' === $item_type ) { |
| 513 | $this->register_config_helpers( $items, $file_path ); |
| 514 | } else { |
| 515 | $this->register_config_custom_item_type( $item_type, $items, $file_path ); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Register pod configs. |
| 523 | * |
| 524 | * @since 2.9.0 |
| 525 | * |
| 526 | * @param array $items Config items. |
| 527 | * @param string $file_path The config file path to use. |
| 528 | */ |
| 529 | protected function register_config_pods( array $items, $file_path = '' ) { |
| 530 | foreach ( $items as $item ) { |
| 531 | // Check if the item type and name exists. |
| 532 | if ( empty( $item['type'] ) || empty( $item['name'] ) ) { |
| 533 | continue; |
| 534 | } |
| 535 | |
| 536 | if ( ! isset( $this->pods[ $item['type'] ] ) ) { |
| 537 | $this->pods[ $item['type'] ] = []; |
| 538 | } |
| 539 | |
| 540 | if ( isset( $item['id'] ) ) { |
| 541 | unset( $item['id'] ); |
| 542 | } |
| 543 | |
| 544 | if ( empty( $item['groups'] ) ) { |
| 545 | $item['groups'] = []; |
| 546 | } |
| 547 | |
| 548 | if ( empty( $item['fields'] ) ) { |
| 549 | $item['fields'] = []; |
| 550 | } |
| 551 | |
| 552 | $item['_pods_file_source'] = $file_path; |
| 553 | |
| 554 | $this->pods[ $item['type'] ][ $item['name'] ] = $item; |
| 555 | |
| 556 | $this->file_path_configs[ $file_path ]['pods'] = $item['type'] . ':' . $item['name']; |
| 557 | } |
| 558 | |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Register pod field configs. |
| 563 | * |
| 564 | * @since 2.9.0 |
| 565 | * |
| 566 | * @param array $items Config items. |
| 567 | * @param string $file_path The config file path to use. |
| 568 | */ |
| 569 | protected function register_config_fields( array $items, $file_path = '' ) { |
| 570 | foreach ( $items as $item ) { |
| 571 | // Check if the pod name, pod type, item type, and item name exists. |
| 572 | if ( empty( $item['type'] ) || empty( $item['name'] ) || empty( $item['pod']['name'] ) || empty( $item['pod']['type'] ) ) { |
| 573 | continue; |
| 574 | } |
| 575 | |
| 576 | if ( ! isset( $this->pods[ $item['pod']['type'] ] ) ) { |
| 577 | $this->pods[ $item['pod']['type'] ] = []; |
| 578 | } |
| 579 | |
| 580 | if ( isset( $item['pod']['id'] ) ) { |
| 581 | unset( $item['pod']['id'] ); |
| 582 | } |
| 583 | |
| 584 | // Check if pod has been registered yet. |
| 585 | if ( ! isset( $this->pods[ $item['pod']['type'][ $item['pod']['name'] ] ] ) ) { |
| 586 | $this->pods[ $item['pod']['type'] ][ $item['pod']['name'] ] = $item['pod']; |
| 587 | } |
| 588 | |
| 589 | // Check if pod has fields that have been registered yet. |
| 590 | if ( ! isset( $this->pods[ $item['pod']['type'][ $item['pod']['name'] ] ]['fields'] ) ) { |
| 591 | $this->pods[ $item['pod']['type'] ][ $item['pod']['name'] ]['fields'] = []; |
| 592 | } |
| 593 | |
| 594 | if ( isset( $item['id'] ) ) { |
| 595 | unset( $item['id'] ); |
| 596 | } |
| 597 | |
| 598 | $item['_pods_file_source'] = $file_path; |
| 599 | |
| 600 | $this->pods[ $item['pod']['type'] ][ $item['pod']['name'] ]['fields'][ $item['name'] ] = $item; |
| 601 | |
| 602 | $this->file_path_configs[ $file_path ]['pods'] = $item['pod']['type'] . ':' . $item['pod']['name'] . ':' . $item['name']; |
| 603 | } |
| 604 | |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Register pod field configs. |
| 609 | * |
| 610 | * @since 2.9.14 |
| 611 | * |
| 612 | * @param array $items Config items. |
| 613 | * @param string $file_path The config file path to use. |
| 614 | */ |
| 615 | protected function register_config_groups( array $items, $file_path = '' ) { |
| 616 | foreach ( $items as $item ) { |
| 617 | // Check if the pod name, pod type, item type, and item name exists. |
| 618 | if ( empty( $item['type'] ) || empty( $item['name'] ) || empty( $item['pod']['name'] ) || empty( $item['pod']['type'] ) ) { |
| 619 | continue; |
| 620 | } |
| 621 | |
| 622 | if ( ! isset( $this->pods[ $item['pod']['type'] ] ) ) { |
| 623 | $this->pods[ $item['pod']['type'] ] = []; |
| 624 | } |
| 625 | |
| 626 | if ( isset( $item['pod']['id'] ) ) { |
| 627 | unset( $item['pod']['id'] ); |
| 628 | } |
| 629 | |
| 630 | // Check if pod has been registered yet. |
| 631 | if ( ! isset( $this->pods[ $item['pod']['type'][ $item['pod']['name'] ] ] ) ) { |
| 632 | $this->pods[ $item['pod']['type'] ][ $item['pod']['name'] ] = $item['pod']; |
| 633 | } |
| 634 | |
| 635 | // Check if pod has groups that have been registered yet. |
| 636 | if ( ! isset( $this->pods[ $item['pod']['type'][ $item['pod']['name'] ] ]['groups'] ) ) { |
| 637 | $this->pods[ $item['pod']['type'] ][ $item['pod']['name'] ]['groups'] = []; |
| 638 | } |
| 639 | |
| 640 | if ( isset( $item['id'] ) ) { |
| 641 | unset( $item['id'] ); |
| 642 | } |
| 643 | |
| 644 | $item['_pods_file_source'] = $file_path; |
| 645 | |
| 646 | $this->pods[ $item['pod']['type'] ][ $item['pod']['name'] ]['groups'][ $item['name'] ] = $item; |
| 647 | |
| 648 | $this->file_path_configs[ $file_path ]['pods'] = $item['pod']['type'] . ':' . $item['pod']['name'] . ':' . $item['name']; |
| 649 | } |
| 650 | |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Register template configs. |
| 655 | * |
| 656 | * @since 2.9.0 |
| 657 | * |
| 658 | * @param array $items Config items. |
| 659 | * @param string $file_path The config file path to use. |
| 660 | */ |
| 661 | protected function register_config_templates( array $items, $file_path = '' ) { |
| 662 | foreach ( $items as $item ) { |
| 663 | // Check if the item name exists. |
| 664 | if ( empty( $item['name'] ) ) { |
| 665 | continue; |
| 666 | } |
| 667 | |
| 668 | if ( isset( $item['id'] ) ) { |
| 669 | unset( $item['id'] ); |
| 670 | } |
| 671 | |
| 672 | // Legacy support for old Template/Page/Helper objects. |
| 673 | $item['label'] = $item['name']; |
| 674 | $item['description'] = $item['code']; |
| 675 | $item['name'] = sanitize_title( $item['label'] ); |
| 676 | |
| 677 | unset( $item['code'] ); |
| 678 | |
| 679 | $item['_pods_file_source'] = $file_path; |
| 680 | |
| 681 | $this->templates[ $item['label'] ] = $item; |
| 682 | |
| 683 | $this->file_path_configs[ $file_path ]['templates'] = $item['label']; |
| 684 | } |
| 685 | |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Register page configs. |
| 690 | * |
| 691 | * @since 2.9.0 |
| 692 | * |
| 693 | * @param array $items Config items. |
| 694 | * @param string $file_path The config file path to use. |
| 695 | */ |
| 696 | protected function register_config_pages( array $items, $file_path = '' ) { |
| 697 | foreach ( $items as $item ) { |
| 698 | // Check if the item name exists. |
| 699 | if ( empty( $item['name'] ) ) { |
| 700 | continue; |
| 701 | } |
| 702 | |
| 703 | if ( isset( $item['id'] ) ) { |
| 704 | unset( $item['id'] ); |
| 705 | } |
| 706 | |
| 707 | // Legacy support for old Template/Page/Helper objects. |
| 708 | $item['label'] = $item['name']; |
| 709 | $item['description'] = $item['code']; |
| 710 | $item['name'] = sanitize_title( $item['label'] ); |
| 711 | |
| 712 | unset( $item['code'] ); |
| 713 | |
| 714 | $item['_pods_file_source'] = $file_path; |
| 715 | |
| 716 | $this->pages[ $item['name'] ] = $item; |
| 717 | |
| 718 | $this->file_path_configs[ $file_path ]['pages'] = $item['name']; |
| 719 | } |
| 720 | |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Register helper configs. |
| 725 | * |
| 726 | * @since 2.9.0 |
| 727 | * |
| 728 | * @param array $items Config items. |
| 729 | * @param string $file_path The config file path to use. |
| 730 | */ |
| 731 | protected function register_config_helpers( array $items, $file_path = '' ) { |
| 732 | foreach ( $items as $item ) { |
| 733 | // Check if the item name exists. |
| 734 | if ( empty( $item['name'] ) ) { |
| 735 | continue; |
| 736 | } |
| 737 | |
| 738 | if ( isset( $item['id'] ) ) { |
| 739 | unset( $item['id'] ); |
| 740 | } |
| 741 | |
| 742 | // Legacy support for old Template/Page/Helper objects. |
| 743 | $item['label'] = $item['name']; |
| 744 | $item['description'] = $item['code']; |
| 745 | $item['name'] = sanitize_title( $item['label'] ); |
| 746 | |
| 747 | unset( $item['code'] ); |
| 748 | |
| 749 | $item['_pods_file_source'] = $file_path; |
| 750 | |
| 751 | $this->helpers[ $item['name'] ] = $item; |
| 752 | |
| 753 | $this->file_path_configs[ $file_path ]['helpers'] = $item['name']; |
| 754 | } |
| 755 | |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * Register config items for custom config item type. |
| 760 | * |
| 761 | * @since 2.9.0 |
| 762 | * |
| 763 | * @param string $item_type Config Item type. |
| 764 | * @param array $items Config items. |
| 765 | * @param string $file_path The config file path to use. |
| 766 | */ |
| 767 | protected function register_config_custom_item_type( $item_type, array $items, $file_path = '' ) { |
| 768 | if ( ! isset( $this->custom_configs[ $item_type ] ) ) { |
| 769 | $this->custom_configs[ $item_type ] = []; |
| 770 | } |
| 771 | |
| 772 | /** @var array $item */ |
| 773 | foreach ( $items as $item ) { |
| 774 | /** |
| 775 | * Pre-process the item to be saved for a custom item type. |
| 776 | * |
| 777 | * @since 2.9.0 |
| 778 | * |
| 779 | * @param array $item Item to pre-process. |
| 780 | * @param string $item_type Item type. |
| 781 | * @param string $file_path The config file path to use. |
| 782 | */ |
| 783 | $item = (array) apply_filters( 'pods_config_register_custom_item', $item, $item_type, $file_path ); |
| 784 | |
| 785 | // Check if the item name exists. |
| 786 | if ( empty( $item['name'] ) ) { |
| 787 | continue; |
| 788 | } |
| 789 | |
| 790 | if ( isset( $item['id'] ) ) { |
| 791 | unset( $item['id'] ); |
| 792 | } |
| 793 | |
| 794 | $item['_pods_file_source'] = $file_path; |
| 795 | |
| 796 | $this->custom_configs[ $item_type ][ $item['name'] ] = $item; |
| 797 | |
| 798 | $this->file_path_configs[ $file_path ][ $item_type ] = $item['name']; |
| 799 | } |
| 800 | |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Store the registered configurations. |
| 805 | */ |
| 806 | protected function store_configs() { |
| 807 | $mapped_object_types = [ |
| 808 | 'pods' => 'pod', |
| 809 | 'templates' => 'template', |
| 810 | 'pages' => 'page', |
| 811 | 'helpers' => 'helper', |
| 812 | ]; |
| 813 | |
| 814 | foreach ( $this->registered_config_item_types as $config_item_type ) { |
| 815 | if ( 'pods' === $config_item_type ) { |
| 816 | $configs = $this->pods; |
| 817 | } elseif ( 'templates' === $config_item_type ) { |
| 818 | $configs = $this->templates; |
| 819 | } elseif ( 'pages' === $config_item_type ) { |
| 820 | $configs = $this->pages; |
| 821 | } elseif ( 'helpers' === $config_item_type ) { |
| 822 | $configs = $this->helpers; |
| 823 | } elseif ( isset( $this->custom_configs[ $config_item_type ] ) ) { |
| 824 | $configs = $this->custom_configs[ $config_item_type ]; |
| 825 | } else { |
| 826 | continue; |
| 827 | } |
| 828 | |
| 829 | $real_type = isset( $mapped_object_types[ $config_item_type ] ) |
| 830 | ? $mapped_object_types[ $config_item_type ] |
| 831 | : $config_item_type; |
| 832 | |
| 833 | foreach ( $configs as $key => $config ) { |
| 834 | if ( 'pod' === $real_type ) { |
| 835 | foreach ( $config as $pod ) { |
| 836 | $pod['object_type'] = $real_type; |
| 837 | $pod['object_storage_type'] = 'file'; |
| 838 | |
| 839 | pods_register_type( $key, $pod['name'], $pod ); |
| 840 | } |
| 841 | } else { |
| 842 | $config['object_storage_type'] = 'file'; |
| 843 | |
| 844 | pods_register_object( $config, $real_type ); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * @todo Get list of configs that do not match DB. |
| 852 | * @todo Handle syncing changed configs to DB. |
| 853 | * @todo Handle syncing configs from DB to file. |
| 854 | */ |
| 855 | |
| 856 | } |
| 857 |