# PHP functions

SleekRank defines global PHP functions for its page groups, so a theme or plugin
reads them without knowing how SleekRank stores them. They exist only while
SleekRank is active, so guard calls with `function_exists()` in code that can
run without it. Its hooks, WP-CLI commands and REST operations are listed in the
generated references: [hooks](/docs/rank/reference/hooks.md),
[CLI](/docs/rank/reference/cli.md) and [REST](/docs/rank/reference/rest.md).

Page groups are kept in SleekRank's own table. Each is answered as its `id`,
`name`, `slug` and `status`, followed by its configuration: `urlPattern`,
`basePageId`, `dataSources`, `mappings`, `sitemapEnabled`, `sitemapPriority` and
`sitemapChangefreq`. A data source's token is never answered.

## Functions

- `sleek_rank_get_page_groups( int $limit = -1, int $offset = 0 ): array`: The
  page groups in any status but trash, newest first; `-1` returns all of them.
- `sleek_rank_get_page_group( int $id ): ?array`: The page group with this id,
  or `null`.
- `sleek_rank_get_page_group_by_slug( string $slug ): ?array`: The page group
  with this slug, or `null`.
- `sleek_rank_create_page_group( array $data ): int`: Creates a page group from
  its `name`, `slug`, `status` (`publish` or `draft`) and configuration, and
  returns its id. `basePageId` is required. Throws an `Exception` without one,
  for a URL pattern without exactly one `{slug}`, or when it cannot be stored.
- `sleek_rank_update_page_group( int $id, array $data ): bool`: Changes the
  name, slug and status where `$data` names them and merges the other fields
  into the configuration. Returns `false` when there is no such page group.

## Example

```php
if (function_exists('sleek_rank_get_page_groups')) {
    $id = sleek_rank_create_page_group([
        'name' => 'Cities',
        'basePageId' => 12,
        'urlPattern' => 'cities/{slug}',
    ]);
    sleek_rank_update_page_group($id, ['sitemapPriority' => 0.8]);
}
```
