Skip to content
Open

v3.0 #98

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
622d155
change PostTypes to abstract class
jjgrainger Dec 4, 2024
cc81d97
update PostTypeRegistrar to accept new PostType
jjgrainger Dec 4, 2024
fd3a798
create Columns and create Column abstract
jjgrainger Dec 4, 2024
5b8c8e6
change Taxonomy to abstract class
jjgrainger Dec 4, 2024
ed869b4
improve test coverage for Columns
jjgrainger Dec 5, 2024
52033e8
improve Column test coverage
jjgrainger Dec 5, 2024
5926229
add PostType tests
jjgrainger Dec 5, 2024
eb2bda8
add Taxonomy tests
jjgrainger Dec 5, 2024
0464c6f
draft new docs for post types
jjgrainger Dec 11, 2024
aa76b94
update summary
jjgrainger Dec 11, 2024
e9b25eb
update post type index
jjgrainger Dec 11, 2024
6b20452
draft new docs for taxonomies
jjgrainger Dec 11, 2024
49b1c7b
update summary
jjgrainger Dec 11, 2024
00d8241
improve Registrar tests
jjgrainger Feb 19, 2025
8d77c38
update README
jjgrainger Jul 16, 2025
3af053f
create columns on init to fix issue with translations
jjgrainger Jul 22, 2025
09778c1
update README and getting started
jjgrainger Jul 24, 2025
f2928a6
remove notes
jjgrainger Jul 24, 2025
d9c2ae4
add documentation for creating columns
jjgrainger Aug 1, 2025
7c56dbe
update examples
jjgrainger Aug 1, 2025
00bc920
update post type index and tweaks
jjgrainger Aug 1, 2025
696edb3
Merge pull request #99 from jjgrainger/v3.0-documentation
jjgrainger Aug 1, 2025
bb08bd1
add PHPStan
jjgrainger Dec 9, 2025
1341686
fix phpstan errors
jjgrainger Dec 9, 2025
002fa42
update workflows
jjgrainger Dec 9, 2025
77fb5c5
update columns methods
jjgrainger Dec 10, 2025
57a178a
update columns integration
jjgrainger Dec 10, 2025
b00aee3
Merge pull request #100 from jjgrainger/feature/add-phpstan
jjgrainger Dec 12, 2025
e9bd01d
update columns method names
jjgrainger Dec 16, 2025
23baeb3
update column method and integrations
jjgrainger Dec 16, 2025
8361735
update examples
jjgrainger Dec 16, 2025
ba2605a
update documentation for columns
jjgrainger Dec 16, 2025
5386025
update columns reference
jjgrainger Dec 17, 2025
ccc5f2f
Merge branch 'v3.0' into feature/columns-enhancements
jjgrainger Dec 17, 2025
e18ceba
Merge pull request #101 from jjgrainger/feature/columns-enhancements
jjgrainger Dec 17, 2025
7a8dbdd
fix documentation
jjgrainger Dec 17, 2025
7560560
add migration guide draft
jjgrainger Dec 17, 2025
0be0c5f
rename files and tweaks
jjgrainger Dec 22, 2025
98864ef
update migration guide
jjgrainger Dec 22, 2025
6e54553
update post type documentation
jjgrainger Dec 22, 2025
380aa96
Merge branch 'v3.0' into feature/documentation-updates
jjgrainger Dec 22, 2025
fd8aee6
update README and file names
jjgrainger Dec 29, 2025
3d67236
add migration guide to README
jjgrainger Dec 29, 2025
cf1d32d
update Taxonomy documentation
jjgrainger Dec 29, 2025
06c4398
update documentation examples
jjgrainger Dec 29, 2025
edd0aae
update getting started code examples
jjgrainger Jan 3, 2026
fa7076f
Merge pull request #102 from jjgrainger/feature/documentation-updates
jjgrainger Jan 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
name: tests
name: Tests

on:
push:
branches:
- master
- main
pull_request:
branches:
- master

jobs:
build:
phpcs:
name: Coding Standards (PHPCS)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -22,20 +21,45 @@ jobs:
- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v4
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: PHPCS
run: composer phpcs

phpstan:
name: Static Analysis (PHPStan)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
php-version: '8.3'

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: PHPStan
run: composer phpstan

phpunit:
name: Unit Tests (PHPUnit)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: composer run-script test
- name: PHPUnit
run: composer phpunit

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
.phpunit.result.cache
coverage.xml
/coverage
155 changes: 109 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# PostTypes v2.2.1
# PostTypes v3.0

[![tests](https://github.com/jjgrainger/PostTypes/actions/workflows/tests.yml/badge.svg)](https://github.com/jjgrainger/PostTypes/actions/workflows/tests.yml) [![codecov](https://codecov.io/gh/jjgrainger/PostTypes/branch/master/graph/badge.svg?token=SGrK2xDF46)](https://codecov.io/gh/jjgrainger/PostTypes) [![Latest Stable Version](https://flat.badgen.net/github/release/jjgrainger/PostTypes/stable)](https://packagist.org/packages/jjgrainger/posttypes) [![Total Downloads](https://flat.badgen.net/packagist/dt/jjgrainger/PostTypes)](https://packagist.org/packages/jjgrainger/posttypes) [![License](https://flat.badgen.net/github/license/jjgrainger/PostTypes)](https://packagist.org/packages/jjgrainger/posttypes)

> Simple WordPress custom post types.
> Modern PHP abstractions for WordPress post types and taxonomies.

## Migrating from v2 to v3

> **Important**: v3.0 is a breaking release. Existing v2 post type and taxonomy definitions will not work without modification. Please review the migration guide in the [documentation](https://posttypes.jjgrainger.co.uk) on how to upgrade to version 3.

## Requirements

* PHP >=7.2
* PHP >=8.1
* [Composer](https://getcomposer.org/)
* [WordPress](https://wordpress.org) >=5.1
* [WordPress](https://wordpress.org) >=6.3

## Installation

Expand All @@ -20,64 +24,123 @@ Run the following in your terminal to install PostTypes with [Composer](https://
$ composer require jjgrainger/posttypes
```

PostTypes uses [PSR-4](https://www.php-fig.org/psr/psr-4/) autoloading and can be used with the Composer's autoloader. Below is a basic example of getting started, though your setup may be different depending on how you are using Composer.

```php
require __DIR__ . '/vendor/autoload.php';

use PostTypes\PostType;

$books = new PostType( 'book' );

$books->register();
```

See Composer's [basic usage](https://getcomposer.org/doc/01-basic-usage.md#autoloading) guide for details on working with Composer and autoloading.
PostTypes uses [PSR-4](https://www.php-fig.org/psr/psr-4/) autoloading and can be used with the Composer's autoloader. See Composer's [basic usage](https://getcomposer.org/doc/01-basic-usage.md#autoloading) guide for details on working with Composer and autoloading.

## Basic Usage

Below is a basic example of setting up a simple book post type with a genre taxonomy. For more information, check out the [online documentation here](https://posttypes.jjgrainger.co.uk).
#### Create a custom post type

Custom post types are defined as classes that extend the base `PostType` class. At a minimum, the `name` method must be implemented to define the post type slug. All other methods are optional and allow you to configure labels, options, taxonomies, admin columns, filters, and more as needed.

```php
// Require the Composer autoloader.
require __DIR__ . '/vendor/autoload.php';
<?php

// Import PostTypes.
use PostTypes\PostType;
use PostTypes\Taxonomy;

// Create a book post type.
$books = new PostType( 'book' );

// Attach the genre taxonomy (which is created below).
$books->taxonomy( 'genre' );

// Hide the date and author columns.
$books->columns()->hide( [ 'date', 'author' ] );

// Set the Books menu icon.
$books->icon( 'dashicons-book-alt' );
use PostTypes\Columns;

class Book extends PostType {
/**
* Define the Post Type name.
*/
public function name(): string {
return 'book';
}

/**
* Define the Post Type labels.
*/
public function labels(): array {
return [
'name' => __( 'Book', 'text-domain' ),
'singular_name' => __( 'Book', 'text-domain' ),
'menu_name' => __( 'Books', 'text-domain' ),
'all_items' => __( 'Books', 'text-domain' ),
'add_new' => __( 'Add New', 'text-domain' ),
'add_new_item' => __( 'Add New Book', 'text-domain' ),
'edit_item' => __( 'Edit Book', 'text-domain' ),
'new_item' => __( 'New Book', 'text-domain' ),
'view_item' => __( 'View Book', 'text-domain' ),
'search_items' => __( 'Search Books', 'text-domain' ),
'not_found' => __( 'No Books found', 'text-domain' ),
'not_found_in_trash' => __( 'No Books found in Trash', 'text-domain' ),
'parent_item_colon' => __( 'Parent Book', 'text-domain' ),
];
}

/**
* Define Post Type feature supports.
*/
public function supports(): array {
return [
'title',
'editor',
'thumbnail',
'custom-fields',
];
}

/**
* Define Taxonomies associated with the Post Type.
*/
public function taxonomies(): array {
return [
'genre',
'category',
];
}

/**
* Set the menu icon for the Post Type.
*/
public function icon(): string {
return 'dashicons-book';
}

/**
* Set the admin post table filters.
*/
public function filters(): array {
return [
'genre',
'category',
];
}

/**
* Define the columns for the admin post table.
*/
public function columns(Columns $columns): Columns {
// Remove the author and date column.
$columns->remove( [ 'author', 'date' ] );

// Add a Rating column.
$columns->add( 'rating', __( 'Rating', 'post-types' ) );

// Populate the rating column.
$columns->populate( 'rating', function( $post_id ) {
echo get_post_meta( $post_id, 'rating', true );
} );

return $columns;
}
}
```

// Register the post type to WordPress.
$books->register();
### Register a custom post type

// Create a genre taxonomy.
$genres = new Taxonomy( 'genre' );
Once the custom post type class is created it can be registered to WordPress by instantiating and call the register method.

// Set options for the taxonomy.
$genres->options( [
'hierarchical' => false,
] );
```php
// Instantiate the Book PostType class.
$book = new Book;

// Register the taxonomy to WordPress.
$genres->register();
// Register the Book PostType to WordPress.
$book->register();
```

## Notes

* The full documentation can be found online at [posttypes.jjgrainger.co.uk](https://posttypes.jjgrainger.co.uk)
* The class has no methods for making custom fields for post types, use [Advanced Custom Fields](https://advancedcustomfields.com)
* The book's example used in the README.md can be found in [examples/books.php](examples/books.php)
* Licensed under the [MIT License](https://github.com/jjgrainger/PostTypes/blob/master/LICENSE)
* Maintained under the [Semantic Versioning Guide](https://semver.org)

Expand Down
19 changes: 13 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "3.*"
"squizlabs/php_codesniffer": "3.*",
"phpstan/phpstan": "^2.1",
"szepeviktor/phpstan-wordpress": "^2.0",
"phpstan/extension-installer": "^1.4"
},
"autoload": {
"psr-4": {
"PostTypes\\": "src/"
}
},
"scripts": {
"test": [
"./vendor/bin/phpcs --standard=psr2 src",
"./vendor/bin/phpunit --coverage-clover=coverage.xml"
]
"phpcs": "phpcs --standard=psr2 src",
"phpunit": "phpunit --coverage-clover=coverage.xml",
"phpstan": "phpstan analyse --memory-limit=512M"
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}
Loading