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