Skip to content

Extending Wikit Core Classes

Overview

The WDG development environment is designed for multi-project support where each project can extend the Wikit framework without modifying core files. This ensures updates to Wikit don't break project-specific customizations.

Project Structure

${WDG_HOME}/
├── wikit-core/           # Core framework (shared)
├── wikit-theme/          # Theme and blocks (shared)
├── wikit-app/            # Application layer (shared)
├── projects/
│   └── {project-name}/
│       └── repositories/
│           └── {repo-name}/
│               ├── plugins/      # Project-specific plugins
│               ├── themes/       # Project-specific themes
│               └── src/          # Project-specific classes

Extending PostType

In Shared wikit-app

For functionality shared across all projects:

php
// /wikit-app/src/PostType/SharedType.php
namespace WDG\App\PostType;

use WDG\Core\PostType;

class SharedType extends PostType {
    protected $post_type = 'shared_type';
    
    // Shared implementation
}

In Project-Specific Code

For project-specific functionality:

php
// /projects/client-site/repositories/client-repo/src/PostType/ClientType.php
namespace ClientSite\PostType;

use WDG\Core\PostType;

class ClientType extends PostType {
    protected $post_type = 'client_type';
    
    // Project-specific implementation
}

Extending Existing Wikit Classes

Method 1: Inheritance

Extend a Wikit class and override specific methods:

php
namespace ClientSite\PostType;

use WDG\App\PostType\People as WikitPeople;

class People extends WikitPeople {
    
    // Add project-specific meta fields
    protected $meta = [
        'department' => [
            'type' => 'string',
            'label' => 'Department',
        ],
        'employee_id' => [
            'type' => 'string',
            'label' => 'Employee ID',
        ],
    ];
    
    // Override init to add project-specific functionality
    public function init() {
        parent::init();
        
        // Add project-specific taxonomy
        new \WDG\Core\Taxonomy('department', [$this->post_type]);
        
        // Add custom hooks
        add_action('save_post_' . $this->post_type, [$this, 'sync_with_crm']);
    }
    
    public function sync_with_crm($post_id) {
        // Project-specific CRM integration
    }
}

Method 2: Composition

Use Wikit classes as components:

php
namespace ClientSite\Features;

use WDG\Core\PostType;
use WDG\Core\Taxonomy;

class ProductCatalog {
    
    private PostType $products;
    private Taxonomy $categories;
    
    public function __construct() {
        // Create post type with project-specific configuration
        $this->products = new PostType('product', [
            'menu_icon' => 'dashicons-cart',
            'supports' => ['title', 'editor', 'thumbnail'],
        ]);
        
        // Add project-specific taxonomy
        $this->categories = new Taxonomy('product_cat', ['product']);
        
        // Add project-specific features
        $this->add_custom_features();
    }
    
    private function add_custom_features() {
        // Project-specific customizations
    }
}

Extending Gutenberg Blocks

Override Block Rendering

php
// In project's functions.php or plugin
add_filter('render_block', function($block_content, $block) {
    if ($block['blockName'] === 'wdg/card-slider') {
        // Modify block output for this project
        $block_content = str_replace(
            'class="wdg-card-slider"',
            'class="wdg-card-slider client-custom-slider"',
            $block_content
        );
    }
    return $block_content;
}, 10, 2);

Add Block Variations

javascript
// In project's JavaScript
wp.blocks.registerBlockVariation('wdg/panel', {
    name: 'client-panel',
    title: 'Client Panel',
    attributes: {
        className: 'client-panel-style',
        backgroundColor: 'brand-primary'
    }
});

Hooks and Filters

Wikit Core Hooks

Wikit provides various hooks for extension:

php
// Modify post type args before registration
add_filter('wdg/post_type/args', function($args, $post_type) {
    if ($post_type === 'product') {
        $args['menu_position'] = 10;
    }
    return $args;
}, 10, 2);

// Modify block editor configuration
add_filter('wdg/block-editor/config', function($config) {
    $config['project_specific'] = true;
    return $config;
});

Best Practices

1. Namespace Appropriately

  • Use WDG\App\ for shared wikit-app code
  • Use ProjectName\ for project-specific code
  • Keep clear separation between shared and project code

2. Prefer Composition Over Inheritance

When possible, use Wikit classes as building blocks rather than extending them:

php
class ProjectFeature {
    public function __construct() {
        // Use Wikit components
        new PostType('feature', $this->get_post_type_args());
        new Taxonomy('feature_type', ['feature']);
    }
}

3. Document Project Extensions

Create a README in your project repository:

markdown
# Project Extensions

## Custom Post Types
- `client_product` - Extended product type with CRM integration

## Modified Behaviors
- People post type includes department field
- Card slider blocks have custom styling

4. Use WordPress Hooks

Leverage WordPress's hook system for modifications:

php
// Better: Use hooks
add_action('init', function() {
    // Project-specific initialization
});

// Avoid: Direct modifications to Wikit files

Common Extension Patterns

Adding Features to Existing CPT

php
// Add to existing post type without extending class
add_action('init', function() {
    // Add support for existing CPT
    add_post_type_support('product', 'comments');
    
    // Register additional taxonomy
    register_taxonomy('product_brand', 'product', [
        'label' => 'Brands',
        'hierarchical' => false,
    ]);
}, 20); // Run after Wikit registration

Project-Specific Meta Fields

php
// Add meta to existing Wikit post type
add_action('init', function() {
    register_post_meta('people', 'project_field', [
        'show_in_rest' => true,
        'single' => true,
        'type' => 'string',
    ]);
});

Custom Admin Columns

php
// Add columns without modifying core
add_filter('manage_product_posts_columns', function($columns) {
    $columns['project_status'] = 'Status';
    return $columns;
});

add_action('manage_product_posts_custom_column', function($column, $post_id) {
    if ($column === 'project_status') {
        echo get_post_meta($post_id, 'status', true);
    }
}, 10, 2);

Troubleshooting

  1. Class Not Found: Ensure proper autoloading in composer.json
  2. Conflicting Post Types: Use unique post type names per project
  3. Hook Priority: Use appropriate priority when modifying Wikit behavior
  4. Namespace Collisions: Keep project namespaces unique

Released under the MIT License.