# Services & Modules Reference

## `services/wordpress.js`

Wrapper around `wpapi` for standard and custom WordPress endpoints.

### Route Registration
Registers custom CPT route:
- Endpoint: `/wp/v2/cms-themes/(?P<id>)`
- Handler accessor: `this.wp.cmsThemes`

### Methods

| Method | Signature | Returns | Description |
|---|---|---|---|
| `constructor` | `(credentials: {endpoint, username, password})` | `WordPress` | Instantiates `WPAPI` client & registers custom `cms-themes` route |
| `getCMSThemes` | `()` | `Promise<Array>` | Retrieves all CMS themes |
| `getCMSTheme` | `(slug: string)` | `Promise<Array>` | Retrieves CMS theme post by slug |
| `updateCMSTheme` | `(id: number, data: object)` | `Promise<object>` | Updates CMS theme post content/metadata |
| `getPages` | `()` | `Promise<Array>` | Retrieves standard WP pages |
| `updatePost` | `(postID: number, data: object)` | `Promise<object>` | Updates standard page content |
| `getTypes` | `()` | `Promise<object>` | Lists registered post types |
| `getFromType` | `(type: string)` | `Promise<object>` | Gets schema for a specific post type |
| `getPostMedia` | `(postID: number)` | `Promise<Array>` | Gets media items attached to post |
| `getPostMediaContent` | `(postID: number)` | `Promise<Array>` | Filters attached media where `caption` contains `demo-content-theme-image` |
| `uploadMedia` | `(file: string, data: object, postID: number)` | `Promise<object>` | Uploads image file and associates it with `postID` |
| `removeMedia` | `(mediaID: number)` | `Promise<object>` | Deletes media item (`force: true`) |

---

## `controllers/image-crawler.js`

Automates Chromium browser to generate full-page screenshots.

### `generateImages(settings, themes, publicPages, privatePages)`
- **Input**:
  - `settings`: `{ URL: string }`
  - `themes`: `string[]` (list of theme slugs)
  - `publicPages`: `Record<string, string>` (key: pageName, value: relative URL)
  - `privatePages`: `Record<string, string>`
- **Output**: `Promise<string[]>` (successfully captured theme slugs)
- **Internal functions**:
  - `navigateTo(theme, pageURL)`: Formats URL with placeholders & `isScreenshot` param
  - `takeScreenshot(theme, pageName)`: Saves to `screenshots/${theme}/${theme}-${pageName}.png` (`fullPage: true`)
  - `doLogin(theme)`: Submits credentials `demo`/`demo` at `en/login`
  - `loopPages(pages, theme)`: Loops over page map and captures each

---

## `controllers/update-wordpress.js`

Uploads generated screenshots and updates the CMS theme Gutenberg layout.

### `updateWordpress(wordpress, themes, publicPages, privatePages)`
- **Input**:
  - `wordpress`: `WordPress` service instance
  - `themes`: `string[]` (theme slugs to update)
  - `publicPages`, `privatePages`: page route dictionaries
- **Gutenberg Layout Generator**:
  - Arranges uploaded images in a 3-column grid (`<!-- wp:columns -->` / `<!-- wp:column -->`)
  - Empty column placeholders added if image count % 3 != 0
  - Updates post via `wordpress.updateCMSTheme(id, { content })`
