Skip to content

Commit

Permalink
Merge pull request #18 from jjgrainger/rc-v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jjgrainger authored Oct 24, 2017
2 parents 736c43e + fc86817 commit 9fa23e7
Show file tree
Hide file tree
Showing 12 changed files with 1,480 additions and 662 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
language: php
notifications:
email: false
php:
- 5.6
- 7.0
- 7.1
- hhvm
script:
- ./vendor/bin/phpcs --standard=PSR2 src
- ./vendor/bin/phpcs --standard=psr2 src
- ./vendor/bin/phpunit
before_script:
- composer self-update
- composer install --dev --no-interaction --prefer-source
- composer install --no-interaction --prefer-source
8 changes: 8 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

##### v2.0
* fix [issue #9](https://github.com/jjgrainger/PostTypes/issues/9): add unit tests
* fix [issue #12](https://github.com/jjgrainger/PostTypes/issues/12) and [issue #13](https://github.com/jjgrainger/PostTypes/issues/13): generating duplicate columns
* fix [issue #2](https://github.com/jjgrainger/PostTypes/issues/2) and [issue #16](https://github.com/jjgrainger/PostTypes/issues/16): translations not working
* create Taxonomy class
* fix [issue #11](https://github.com/jjgrainger/PostTypes/issues/11): add `columns()` to Taxonomy class
* update [`examples/books.php`](https://github.com/jjgrainger/PostTypes/blob/master/examples/books.php)

##### v1.1.2
* fix PHPCS as dev requirement
* add version to composer json
Expand Down
166 changes: 142 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PostTypes v1.1.2
# PostTypes v2.0

[![Build Status](https://travis-ci.org/jjgrainger/PostTypes.svg?branch=master)](https://travis-ci.org/jjgrainger/PostTypes) [![Total Downloads](https://poser.pugx.org/jjgrainger/posttypes/downloads)](https://packagist.org/packages/jjgrainger/posttypes) [![Latest Stable Version](https://poser.pugx.org/jjgrainger/posttypes/v/stable)](https://packagist.org/packages/jjgrainger/posttypes) [![License](https://poser.pugx.org/jjgrainger/posttypes/license)](https://packagist.org/packages/jjgrainger/posttypes)

Expand All @@ -22,6 +22,8 @@ require __DIR__ . '/vendor/autoload.php';
use PostTypes\PostType;

$books = new PostType('book');

$books->register();
```

See Composers [basic usage](https://getcomposer.org/doc/01-basic-usage.md#autoloading) guide for details on working Composer and autoloading.
Expand All @@ -30,10 +32,14 @@ See Composers [basic usage](https://getcomposer.org/doc/01-basic-usage.md#autolo

#### Create a new Post Type

A new post type can be created by simply passing the post types name to the class constructor.
A new post type can be created by simply passing the post types name to the class constructor. To register the post type to WordPress you must call the `register()` method.

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

// Register the post type to WordPress
$books->register();
```

#### Defining names
Expand All @@ -49,6 +55,8 @@ $names = [
];

$books = new PostType($names);

$books->register();
```

It can accept the following names:
Expand All @@ -70,6 +78,8 @@ $options = [
];

$books = new PostType('book', $options);

$books->register();
```

All available options are on the [WordPress Codex](https://codex.wordpress.org/Function_Reference/register_post_type)
Expand All @@ -84,6 +94,20 @@ $labels = [
];

$books = new PostType('book', $options, $labels);

$books->register();
```

Alternatively, you can use the `labels()` method to set the labels for the post type.

```php
$books = new PostType('books');

$books->labels([
'add_new_item' => __('Add new Book')
]);

$books->register();
```

All available labels are on the [WordPress Codex](https://codex.wordpress.org/Function_Reference/register_post_type)
Expand All @@ -93,19 +117,29 @@ All available labels are on the [WordPress Codex](https://codex.wordpress.org/Fu
To work with exisiting post types simple pass the post type name into the object. Be careful using global variables (i.e `$post`) which can lead to unwanted results.

```php
// Create a PostType object for an existing post type in WordPress
$blog = new PostType('post');

// Make changes to the post type...

// You still need to register the changes to WordPress
$blog->register();
```

## Add Taxonomies
## Taxonomies

Adding taxonomies to a post type is easily achieved by using the `taxonomy()` method.
Taxonomies are created using the `Taxonomy` class. This works indetically to the `PostType` class and holds similar methods.

#### Create new taxonomy

To create a new taxonomy simply pass the taxonomy name to the `taxonomy()` method. Labels and the taxonomy slug are generated from the taxonomy name.
To create a new taxonomy simply pass the taxonomy name to the `Taxonomy` class constructor. Labels and the taxonomy slug are generated from the taxonomy name.

```php
$books->taxonomy('genre');
// Create a new taxonomy
$genres = new Taxonomy('genre');

// Register the taxonomy to WordPress
$genres->register();
```

#### Defining names
Expand All @@ -125,7 +159,9 @@ $names = [
'slug' => 'genres'
];

$books->taxonomy($names);
$genres = new Taxonomy($names);

$genres->register();
```

#### Adding options
Expand All @@ -134,27 +170,91 @@ You can further customise taxonomies by passing an array of options as the secon

```php
$options = [
'hierarchical' => false,
'hierarchical' => false,
];

$books->taxonomy('genre', $options);
$genres = new Taxonomy('genre', $options);

$genres->register();
```

All available options are on the [WordPress Codex](https://codex.wordpress.org/Function_Reference/register_taxonomy)

#### Adding Exisiting Taxonomies
#### Adding labels

You can define the labels for a Taxonomy by passing an array as the third argument in the class constructor.

```php
$labels = [
'add_new_item' => __('Add new Genre'),
];

$genres = new Taxonomy('genres', $options, $labels);

You can add existing taxonomies by passing the taxonomy name to the `taxonomy()` method. This works with custom taxonomies too. You only need to pass the options/names for the taxonomy **once**, afterwards you only need to pass the taxonomy name.
$genres->register();
```

Alternatively, you can use the `labels()` method to set the labels for the post type.

```php
$books->taxonomy('post_tag');
$genres = new Taxonomy('genre');

$genres->labels([
'add_new_item' => __('Add new Genre')
]);

$genres->register();
```

All available labels are on the [WordPress Codex](https://codex.wordpress.org/Function_Reference/register_taxonomy)

#### Exisiting Taxonomies

You can work with existing taxonomies by passing the taxonomy name to the Taxonoy constructor. Once you have made your changes you need to register them to WordPress using the `register()` method.

```php
// Create a new Taxonomy object for an existing taxonomy
$tags = new Taxonomy('post_tags');

// Modify the taxonomy...

// Regsiter changes to WordPress
$tags->register();
```

## Link Taxonomies and PostTypes

Depending on the object type (Taxonomy/PostType) you can link the two together with the respective methods.

For registering a Taxonomy to a PostType use the `taxonomy()` method.

For regsitering a PostType to a Taxonomy use the `posttype()` method.

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

// Add the genre taxonomy to the book post type
$books->taxonomy('genre');

// Register the post type to WordPress
$books->register();

// Create the genre taxonomy
$genres = new Taxonomy('genre');

// Use this method instead of the PostTypes taxonomy() method
$genres->posttype('book');

// register the genre taxonomy to WordPress
$genres->register();
```

## Admin Edit Screen

#### Filters

Set the taxonomy filters on the admin edit screen by passing an array to the `filters()` method
Set the taxonomy filters on the post type admin edit screen by passing an array to the `filters()` method

```php
$books->filters(['genres', 'category']);
Expand All @@ -164,6 +264,21 @@ The order of the filters are set by the order of the items in the array. An empt

#### Columns

You can now modify a `Taxonomy` columns using exactly the same methods listed below. For example:

```php
// Create a taxonomy
$genres = new Taxonomy('genre');

// Add a column to the taxonomy admin table
$genres->columns()->add([
'popularity' => __('Popularity')
]);

// Register the taxonomy to WordPress
$genres->register();
```

###### Adding Columns

You can add columns to the admin edit screen by passing an array of slugs and labels to the `add()` method.
Expand Down Expand Up @@ -213,15 +328,7 @@ $books->columns()->order([

###### Populating Columns

Columns that are automatically populated with correct slug

* `post_id` - the post id
* `title` - the posts title with edit links
* `author` - the posts author
* `date` - the posts dates
* `{taxonomy_name}` - a list of the taxonomy terms attached to the post
* `thumbnail` - the post featured image
* `meta_{meta_key}` - the post meta for that key
You can populate any column using the `populate()` method and passing the column slug and function.

```php
$books->columns()->populate('rating', function($column, $post_id) {
Expand Down Expand Up @@ -264,10 +371,21 @@ $books->flush();

### Translation

The class is setup for translation, but if you need to set your own textdomain to work with your theme or plugin use the `translation()` method:
Since 2.0 the `translation()` method has been removed. You can translate any labels and names when you assign them to the PostType or Taxonomy. It was removed to provide more control to the developer while encouraging best practices around internationalizing plugins and themes set out by [WordPress](https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/).

```php
$books->translation('your-textdomain');
// Translating the PostType plural and singular names
$books = new PostType([
'name' => 'book',
'singular' => __('Book', 'YOUR_TEXTDOMAIN'),
'plural' => __('Books', 'YOUR_TEXTDOMAIN'),
'slug' => 'books'
]);

// Translating Labels
$books->labels([
'add_new_item' => __('Add new Book', 'YOUR_TEXTDOMAIN')
]);
```

## Notes
Expand Down
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jjgrainger/posttypes",
"version": "1.1.2",
"version": "2.0",
"description": "Simple WordPress custom post types.",
"homepage": "https://github.com/jjgrainger/posttype",
"license": "MIT",
Expand All @@ -16,9 +16,16 @@
"php": ">=5.3.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^2.8"
"squizlabs/php_codesniffer": "^2.8",
"phpunit/phpunit": "5.7"
},
"autoload": {
"psr-4": { "PostTypes\\": "src/" }
},
"scripts": {
"test": [
"./vendor/bin/phpcs --standard=psr2 src",
"./vendor/bin/phpunit"
]
}
}
22 changes: 21 additions & 1 deletion examples/books.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
require __DIR__ . '/vendor/autoload.php';

use PostTypes\PostType;
use PostTypes\Taxonomy;

// Create a books Post Type
$books = new PostType('book');

// Add a Genre Taxonomy
// Add the Genre Taxonomy
$books->taxonomy('genre');

// Hide the date and author columns
Expand Down Expand Up @@ -37,3 +38,22 @@

// Set the Books menu icon
$books->icon('dashicons-book-alt');

// Register the PostType to WordPress
$books->register();

// Create the genre Taxonomy
$genres = new Taxonomy('genre');

// Add a popularity column to the genre taxonomy
$genres->columns()->add([
'popularity' => 'Popularity'
]);

// Populate the new column
$genres->columns()->populate('popularity', function($content, $column, $term_id) {
return get_term_meta($term_id, 'popularity', true);
});

// Register the taxonomy to WordPress
$genres->register();
11 changes: 11 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Test suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
Loading

0 comments on commit 9fa23e7

Please sign in to comment.