<?php

// Get List of Sub Categories for Slug
function getSubpagesForSlug($arrayOfWPPages, $slug)
{
  foreach ($arrayOfWPPages as $page) {
    if ($page['page']->post_name === $slug) {
      return $page['subpages'];
    }
  }
  // Return an empty array if the slug is not found
  return [];
}

// Return WordPress Page For A Slug
function getPageForSlug($arrayOfWPPages, $slug)
{
  foreach ($arrayOfWPPages as $page) {
    if ($page['page']->post_name === $slug) {
      return $page['page'];
    }

    foreach ($page['subpages'] as $index => $subpage) {
      if ($subpage->post_name === $slug) {

        return $page['subpages'][$index];
      }
    }
  }

  // Return an empty array if the slug is not found
  return [];
}

function ddd($variable)
{
  echo '<pre>';
  var_dump($variable);
  echo '<pre>';
}

// Get An Array of Top Level Pages and their Subpages
function get_pages_as_array()
{
  $pages = get_pages();
  $page_hierarchy = array();

  foreach ($pages as $page) {
    $parent_id = $page->post_parent;

    // If it's a top-level page
    if ($parent_id == 0) {
      $page_hierarchy[$page->ID] = array(
        'page' => $page,
        'subpages' => array()
      );
    } else {
      // If it's a child page, add it under its parent
      $page_hierarchy[$parent_id]['subpages'][] = $page;
    }
  }

  return $page_hierarchy;
}
