# Product Gallery Filter WordPress Plugin

A WordPress plugin that allows admin users to create gallery items with images, headings, descriptions, and links, assign them to categories, and order them within each category.

## Features

- **Gallery Items Custom Post Type**: Create and manage gallery items with custom fields
- **Categories Taxonomy**: Flat (non-hierarchical) categories for organizing items
- **Custom Fields**:
  - Image (using WordPress featured image)
  - Heading (optional, falls back to post title)
  - Description (optional, falls back to post content)
  - Link URL with custom link text
- **Many-to-Many Relationships**: Items can be assigned to multiple categories
- **Per-Category Ordering**: Drag & drop interface to order items within each category
- **Admin Interface**: Clean, intuitive admin interface with visual feedback

## Installation

1. Upload the `product-gallery-filter` folder to `/wp-content/plugins/`
2. Activate the plugin through the 'Plugins' menu in WordPress
3. The plugin will automatically create the necessary database table

## Usage

### Creating Gallery Items

1. Go to **Gallery Items → Add New**
2. Add a title and content (these serve as fallbacks for heading and description)
3. Set a featured image for the gallery item
4. Fill in the custom fields in the "Gallery Item Details" meta box:
   - Heading (optional)
   - Description (optional)
   - Link URL (optional)
   - Link Text (optional, defaults to "Learn More")
5. Assign categories in the "Categories" meta box
6. Publish the gallery item

### Managing Categories

1. Go to **Gallery Items → Categories**
2. Add new categories or manage existing ones
3. Each category shows:
   - Number of assigned items
   - Link to the ordering interface

### Ordering Items Within Categories

1. Go to **Gallery Items → Order Items**
2. Select a category from the dropdown
3. Drag and drop items to reorder them
4. The order is saved automatically when you stop dragging

### Database Structure

The plugin creates a custom table for many-to-many relationships with per-category ordering:

```sql
CREATE TABLE wp_product_gallery_item_categories (
    id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    gallery_item_id BIGINT(20) UNSIGNED NOT NULL,
    category_id BIGINT(20) UNSIGNED NOT NULL,
    sort_order INT(11) NOT NULL DEFAULT 0,
    PRIMARY KEY (id),
    UNIQUE KEY unique_item_category (gallery_item_id, category_id),
    KEY idx_category_order (category_id, sort_order)
);
```

## Security

- All forms use WordPress nonces for security
- Capability checks ensure only authorized users can perform actions
- Input sanitization and output escaping throughout
- Prepared SQL statements for database operations

## Development

### File Structure

```
product-gallery-filter/
├── product-gallery-filter.php          # Main plugin file
├── includes/
│   ├── class-plugin.php                # Main plugin class
│   ├── class-cpt-gallery-item.php      # Gallery Items CPT
│   ├── class-taxonomy-category.php     # Categories taxonomy
│   ├── class-meta-boxes.php            # Custom fields
│   ├── class-admin-ordering.php        # Drag & drop ordering UI
│   └── class-database.php              # Custom table for ordering
├── admin/
│   ├── css/admin.css                   # General admin styles
│   ├── css/ordering.css                # Ordering page styles
│   ├── js/admin.js                     # General admin JavaScript
│   └── js/ordering.js                  # Ordering page JavaScript
└── assets/                             # Plugin assets
```

### Constants

- `PGF_PLUGIN_VERSION`: Plugin version
- `PGF_PLUGIN_PATH`: Absolute path to plugin directory
- `PGF_PLUGIN_URL`: URL to plugin directory
- `PGF_PLUGIN_BASENAME`: Plugin basename

### Hooks

**Actions:**
- `plugins_loaded`: Initializes the plugin
- `admin_enqueue_scripts`: Enqueues admin assets
- `save_post_gallery_item`: Saves meta data and category assignments
- `before_delete_post`: Cleans up relationships when items are deleted
- `pre_delete_term`: Cleans up relationships when categories are deleted

**Filters:**
- `manage_gallery_item_posts_columns`: Adds custom columns to admin list
- `manage_edit-gallery_category_columns`: Adds custom columns to taxonomy list

## Future Enhancements

Planned features for future versions:

1. **Frontend Widget**: Shortcode and widget for displaying gallery items
2. **REST API Endpoints**: For headless WordPress usage
3. **Bulk Operations**: Bulk assign/remove categories
4. **Import/Export**: CSV import/export functionality
5. **Advanced Filtering**: AJAX filtering on frontend

## Requirements

- WordPress 5.0 or higher
- PHP 7.2 or higher
- MySQL 5.6 or higher / MariaDB 10.1 or higher

## License

GPL v2 or later

## Support

For support, feature requests, or bug reports, please create an issue on the plugin's GitHub repository (if available) or contact the developer directly.