Skip to main content
This guide provides a comprehensive overview of Visual Portfolio’s internal architecture, class organization, and design patterns.

Core Architecture

Main Plugin Class

The plugin uses a Singleton pattern to ensure only one instance exists throughout the WordPress lifecycle. File: class-visual-portfolio.php

Plugin Initialization

The plugin is initialized on the plugins_loaded hook:

Plugin Properties

The main class stores essential plugin information:

Class Loading Order

Classes are loaded in a specific order via include_dependencies() to ensure proper initialization:

1. Deprecations (First)

Handles backward compatibility for deprecated functions and classes.

2. Security & Utilities

Core security measures and utility functions.

3. Template & Asset Systems

Template loading and asset management systems.

4. Core Features

Main plugin functionality and features.

5. Gutenberg Integration

Block editor components and blocks.

6. WordPress Integration

WordPress-specific integrations and admin interfaces.

7. Third-Party Integrations

Compatibility layers for popular plugins and themes.

8. Migration (Last)

Database migrations run after all features are loaded.

Core Classes

Visual_Portfolio_Assets

File: classes/class-assets.php Manages all script and style enqueuing with conditional loading. Key Features:
  • Stores required assets per page load
  • Conditionally enqueues assets only when needed
  • Handles both static and dynamic CSS
  • Manages head and footer asset loading
  • Generates inline styles for customization
Hook Priority:
Asset Storage:

Visual_Portfolio_Custom_Post_Type

File: classes/class-custom-post-type.php Registers the vp_lists custom post type for portfolios. Post Type Configuration:
  • Slug: vp_lists
  • Supports: title, editor, thumbnail, author
  • Capabilities: Custom capability system
  • Taxonomies: Portfolio categories and tags
  • Admin UI: Custom columns and filters

Visual_Portfolio_Templates

File: classes/class-templates.php Handles template file loading with theme override support. Template Hierarchy:
  1. Child theme: wp-content/themes/child-theme/visual-portfolio/
  2. Parent theme: wp-content/themes/theme/visual-portfolio/
  3. Plugin: wp-content/plugins/visual-portfolio/templates/
Methods:

Visual_Portfolio_Gutenberg

File: classes/class-gutenberg.php Integrates the plugin with WordPress block editor. Features:
  • Registers block editor assets
  • Provides editor-only styles
  • Sets up block categories
  • Registers block patterns

Visual_Portfolio_Rest

File: classes/class-rest.php Provides REST API endpoints for AJAX operations. Endpoints:
  • Get portfolio items
  • Lazy load items
  • Filter and sort operations
  • Custom content queries
Security:

Visual_Portfolio_Security

File: classes/class-security.php Implements security measures throughout the plugin. Features:
  • Input sanitization
  • Output escaping
  • Nonce verification
  • Capability checks
  • SQL injection prevention
  • XSS protection

Visual_Portfolio_Get_Portfolio

File: classes/class-get-portfolio.php Core logic for retrieving and rendering portfolios. Responsibilities:
  • Query portfolio items
  • Apply filters and sorting
  • Render layouts
  • Handle pagination
  • Process custom queries

Hook System

Initialization Hooks

The plugin uses multiple init hooks with different priorities:
Priority 5: Sets plugin name for translations
Priority 10: Loads text domain
Priority 20: Runs deferred rewrite rules

Activation/Deactivation

Activation:
  • Sets welcome screen redirect transient
  • Defers rewrite rules flush
Deactivation:
  • Clears capability cache
  • Flushes rewrite rules

Rewrite Rules System

The plugin uses a deferred rewrite flush mechanism to avoid performance issues:
Why Deferred?
  • Post type must be registered before flushing
  • Avoids multiple flushes on same page load
  • Improves performance

Data Storage

Portfolio Settings

Portfolio configurations are stored in post meta:

Transient Caching

The plugin uses transients for caching expensive operations:
Common Transients:
  • vp_flush_rewrite_rules - Deferred rewrite flush
  • _visual_portfolio_welcome_screen_activation_redirect - Welcome screen redirect
  • vp_oembed_data_* - oEmbed data cache

Template System

Template Files

Templates are organized by component:

Theme Override

Themes can override templates by creating:
The template system automatically finds and uses the theme version.

Pro Plugin Integration

The plugin detects and integrates with the Pro version:

Naming Conventions

PHP Classes

Pattern: Visual_Portfolio_ClassName

File Names

Pattern: class-{class-name}.php
  • class-assets.php
  • class-templates.php
  • class-custom-post-type.php

JavaScript

Modules: Modular ES6+ structure
Naming: camelCase for functions, PascalCase for React components

SCSS

BEM Methodology: .vp-block__element--modifier
Prefix: All classes prefixed with vp-

Best Practices

Adding New Features

  1. Create a new class in classes/ directory
  2. Follow naming convention: class-feature-name.php
  3. Use WordPress hooks for initialization
  4. Include in load order via include_dependencies()
  5. Add inline documentation using PHPDoc
  6. Run linters before committing

Extending Functionality

Use WordPress filters and actions:

Performance Optimization

  1. Conditional loading: Only load assets when needed
  2. Transient caching: Cache expensive operations
  3. Lazy loading: Defer image loading
  4. Minification: Use minified assets in production
  5. Database queries: Use WP_Query with proper arguments

Next Steps