Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ links: [WPify Scoper](https://packagist.org/packages/wpify/scoper)
composer require wpify/scoper
```

[Next step >>>](https://github.com/wpify/plugin-tutorial/tree/03-custom-fields)
### 03 - WPify Custom Fields

In this step, we are adding the WPify Custom Fields to the plugin, so that we can easily create custom fields for our
plugin. Custom fields are required in composer-deps.json file.

links: [WPify Custom Fields](https://packagist.org/packages/wpify/custom-fields)


[Next step >>>](https://github.com/wpify/plugin-tutorial/tree/04-models)
3 changes: 2 additions & 1 deletion composer-deps.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"require": {
"php": "^8.0",
"php-di/php-di": "^7.0"
"php-di/php-di": "^7.0",
"wpify/custom-fields": "^3.9"
}
}
39 changes: 39 additions & 0 deletions src/Blocks/BookLinkBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace WpifyPluginTutorial\Blocks;

use WpifyPluginTutorial\Managers\BlocksManager;
use WpifyPluginTutorialDeps\Wpify\CustomFields\CustomFields;

class BookLinkBlock {
const KEY = 'wpify/book-link';

public function __construct( CustomFields $custom_fields ) {
$custom_fields->create_gutenberg_block( array(
'name' => self::KEY,
'title' => __( 'Book link', 'wpifypt' ),
'category' => BlocksManager::BOOKS_KEY,
'icon' => 'book',
'supports' => array( 'anchor' => true ),
'example' => array( 'book_id' => 1 ),
'render_callback' => array( $this, 'render' ),
'items' => array(
array(
'type' => 'hidden',
'id' => 'anchor',
),
array(
'type' => 'post',
'id' => 'book_id',
'title' => __( 'WPify Book', 'wpifypt' ),
'post_type' => 'book',
'async' => true,
)
),
) );
}

public function render( $block_attributes, $content ) {
return '<h1>Book: ' . $block_attributes['book_id'] . '</h1>';
}
}
25 changes: 25 additions & 0 deletions src/Managers/BlocksManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace WpifyPluginTutorial\Managers;

use WpifyPluginTutorial\Blocks\BookLinkBlock;

class BlocksManager {
const BOOKS_KEY = 'wpify-books';

public function __construct(
BookLinkBlock $book_link_block,
) {
add_filter( 'block_categories_all', array( $this, 'block_categories_all' ) );
}

public function block_categories_all( $categories ) {
$categories[] = array(
'slug' => self::BOOKS_KEY,
'title' => __( 'WPify Books', 'wpifypt' ),
'icon' => 'books',
);

return $categories;
}
}
2 changes: 2 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WpifyPluginTutorial;

use WpifyPluginTutorial\Managers\BlocksManager;
use WpifyPluginTutorial\Managers\PostTypesManager;
use WpifyPluginTutorial\Managers\TaxonomiesManager;

Expand All @@ -16,6 +17,7 @@ class Plugin {
public function __construct(
PostTypesManager $post_types_manager,
TaxonomiesManager $taxonomies_manager,
BlocksManager $blocks_manager,
) {
// stuff to do on plugin load
}
Expand Down
23 changes: 22 additions & 1 deletion src/PostTypes/BookPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,33 @@
namespace WpifyPluginTutorial\PostTypes;

use WpifyPluginTutorial\Taxonomies\BookAuthorTaxonomy;
use WpifyPluginTutorialDeps\Wpify\CustomFields\CustomFields;

class BookPostType {
const KEY = 'book';

public function __construct() {
public function __construct( CustomFields $custom_fields ) {
add_action( 'init', array( $this, 'register' ), 0 );

$custom_fields->create_metabox( array(
'id' => 'book-info',
'title' => __( 'Book info', 'wpifypt' ),
'context' => 'advanced',
'priority' => 'high',
'items' => array(
array(
'id' => 'isbn',
'type' => 'text',
'label' => __( 'ISBN', 'wpifypt' ),
),
array(
'id' => 'first_published',
'type' => 'date',
'label' => __( 'Published first', 'wpifypt' ),
),
),
'post_types' => array( self::KEY ),
) );
}

public function register() {
Expand Down
23 changes: 20 additions & 3 deletions src/Taxonomies/BookAuthorTaxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,33 @@
namespace WpifyPluginTutorial\Taxonomies;

use WpifyPluginTutorial\PostTypes\BookPostType;
use WpifyPluginTutorialDeps\Wpify\CustomFields\CustomFields;

class BookAuthorTaxonomy {
const KEY = 'book_author';

public function __construct() {
public function __construct( CustomFields $custom_fields ) {
add_action( 'init', array( $this, 'register' ) );

$custom_fields->create_taxonomy_options( array(
'taxonomy' => self::KEY,
'items' => array(
array(
'id' => 'date_of_birth',
'type' => 'date',
'label' => __( 'Date of Birth', 'wpifypt' ),
),
array(
'id' => 'date_of_death',
'type' => 'date',
'label' => __( 'Date of Death', 'wpifypt' ),
),
),
) );
}

public function register() {
$labels = array(
$labels = array(
'name' => _x( 'Book Authors', 'Taxonomy General Name', 'wpifypt' ),
'singular_name' => _x( 'Book Author', 'Taxonomy Singular Name', 'wpifypt' ),
'menu_name' => __( 'Book Author', 'wpifypt' ),
Expand Down Expand Up @@ -41,7 +58,7 @@ public function register() {
'hierarchical' => false,
);

$args = array(
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
Expand Down