# Byline REST API Image URL Implementation

## Current State

### Endpoint
`https://new.igihe.com/english/wp-json/wp/v2/byline/3172`

### What's Currently Exposed

The byline taxonomy is registered with `show_in_rest => true`, which creates a REST endpoint. The byline term meta fields are also registered with `show_in_rest => true`:

- `byline_image` (integer ID of the attachment)
- `byline_user_id` (integer ID of linked user)

These appear in the REST response under the `meta` object:
```json
{
  "id": 3172,
  "name": "Author Name",
  "slug": "author-name",
  "description": "...",
  "meta": {
    "byline_image": 12345,
    "byline_user_id": 789
  }
}
```

### The Problem

The term meta only returns the **attachment ID** (`byline_image: 12345`), not the formatted image data with URL, alt text, sizes, etc.

When posts embed bylines, they use a custom callback that formats the image via `format_attachment_for_rest()` which returns:
```json
{
  "id": 12345,
  "url": "https://...",
  "title": "...",
  "mime_type": "image/jpeg"
}
```

But when accessing the byline term directly via `/wp-json/wp/v2/byline/3172`, this formatting is NOT applied.

## Solution

Add a custom REST field to the byline taxonomy that formats the image data. This is done in:

**File**: `/english/wp-content/plugins/igihe-core/modules/content-model/class-content-model-taxonomies.php`

**Location**: In the `register_rest_fields()` method (after line 104)

### Code to Add

```php
// Register image URL field on byline taxonomy term
register_rest_field(
    'byline',
    'image',
    array(
        'get_callback' => function( $term_arr ) {
            if ( ! isset( $term_arr['id'] ) ) {
                return null;
            }
            $term_id = (int) $term_arr['id'];
            $image_id = (int) get_term_meta( $term_id, self::TERM_META_BYLINE_IMAGE, true );
            
            if ( $image_id > 0 ) {
                return self::format_attachment_for_rest( $image_id );
            }
            
            return self::get_default_byline_image_for_rest();
        },
        'update_callback' => null,
        'schema' => array(
            'type' => array( 'object', 'null' ),
            'description' => 'Byline author image with URL and metadata.',
            'context' => array( 'view', 'edit' ),
            'properties' => array(
                'id' => array( 'type' => 'integer' ),
                'url' => array( 'type' => 'string' ),
                'title' => array( 'type' => 'string' ),
                'mime_type' => array( 'type' => 'string' ),
            ),
        ),
    )
);
```

### Result

After adding this code and flushing rewrite rules, the endpoint will return:

```json
{
  "id": 3172,
  "name": "Author Name",
  "slug": "author-name",
  "description": "...",
  "image": {
    "id": 12345,
    "url": "https://new.igihe.com/path/to/image.jpg",
    "title": "Author Photo",
    "mime_type": "image/jpeg"
  },
  "meta": {
    "byline_image": 12345,
    "byline_user_id": 789
  }
}
```

## Alternative: User ID Field

Similarly, you might want to add a formatted field for `user_id` that returns full user data:

```php
// Register user field on byline taxonomy term
register_rest_field(
    'byline',
    'user',
    array(
        'get_callback' => function( $term_arr ) {
            if ( ! isset( $term_arr['id'] ) ) {
                return null;
            }
            $term_id = (int) $term_arr['id'];
            $user_id = (int) get_term_meta( $term_id, self::TERM_META_BYLINE_USER, true );
            
            if ( $user_id > 0 ) {
                $user = get_userdata( $user_id );
                if ( $user ) {
                    return array(
                        'id' => $user->ID,
                        'name' => $user->display_name,
                        'email' => $user->user_email,
                        'url' => $user->user_url,
                    );
                }
            }
            
            return null;
        },
        'update_callback' => null,
        'schema' => array(
            'type' => array( 'object', 'null' ),
            'context' => array( 'view', 'edit' ),
        ),
    )
);
```

## Implementation Steps

1. Edit the file: [class-content-model-taxonomies.php](class-content-model-taxonomies.php#L200-L250)
2. Add the REST field registration inside `register_rest_fields()` method
3. Flush rewrite rules (go to WordPress Settings → Permalinks and save)
4. Test the endpoint: `curl https://new.igihe.com/english/wp-json/wp/v2/byline/3172`

## Files Involved

- **Main Logic**: `/english/wp-content/plugins/igihe-core/modules/content-model/class-content-model-taxonomies.php`
  - Line 44: Byline taxonomy registered with `'show_in_rest' => true`
  - Line 305-313: Term meta registered with `'show_in_rest' => true`
  - Line 105-200: `register_rest_fields()` method
  - Line 349-363: `format_attachment_for_rest()` helper method
  - Line 320-336: `get_default_byline_image_for_rest()` helper method

- **Byline Admin UI**: `/english/wp-content/plugins/igihe-core/modules/content-model/class-content-model-byline.php`
  - Where the image is stored as term meta
