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