Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/php post type and taxonomy scaffold #218

Merged
merged 15 commits into from
Apr 11, 2024
Merged
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@
"type:wordpress-plugin"
]
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/installers": true
}
}
}
}
187 changes: 187 additions & 0 deletions mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php
/**
* AbstractPostType
*
* @package TenUpPlugin
*/

namespace TenUpPlugin\PostTypes;

use TenUpPlugin\Module;

/**
* Abstract class for post types.
darylldoyle marked this conversation as resolved.
Show resolved Hide resolved
*/
abstract class AbstractPostType extends Module {

darylldoyle marked this conversation as resolved.
Show resolved Hide resolved
/**
darylldoyle marked this conversation as resolved.
Show resolved Hide resolved
* Get the post type name.
*
* @return string
*/
abstract public function get_name();
claytoncollie marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get the singular post type label.
*
* @return string
*/
abstract public function get_singular_label();

/**
* Get the plural post type label.
*
* @return string
*/
abstract public function get_plural_label();

/**
* Default post type supported feature names.
*
* @return array
*/
public function get_editor_supports() {
$supports = [
'title',
'editor',
'author',
'thumbnail',
'excerpt',
'revisions',
];

return $supports;
}

/**
* Get the options for the post type.
*
* @return array
*/
public function get_options() {
$options = [
'labels' => $this->get_labels(),
'public' => true,
'has_archive' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'show_in_rest' => true,
'supports' => $this->get_editor_supports(),
];

return $options;
}

/**
* Get the labels for the post type.
*
* @return array
*/
public function get_labels() {
$plural_label = $this->get_plural_label();
$singular_label = $this->get_singular_label();

// phpcs:disable -- ignoring template strings without translators placeholder since this is dynamic
$labels = array(
'name' => $plural_label,
// Already translated via get_plural_label().
'singular_name' => $singular_label,
// Already translated via get_singular_label().
'add_new_item' => sprintf( __( 'Add New %s', 'tenup-plugin' ), $singular_label ),
'edit_item' => sprintf( __( 'Edit %s', 'tenup-plugin' ), $singular_label ),
'new_item' => sprintf( __( 'New %s', 'tenup-plugin' ), $singular_label ),
'view_item' => sprintf( __( 'View %s', 'tenup-plugin' ), $singular_label ),
'view_items' => sprintf( __( 'View %s', 'tenup-plugin' ), $plural_label ),
'search_items' => sprintf( __( 'Search %s', 'tenup-plugin' ), $plural_label ),
'not_found' => sprintf( __( 'No %s found.', 'tenup-plugin' ), strtolower( $plural_label ) ),
'not_found_in_trash' => sprintf( __( 'No %s found in Trash.', 'tenup-plugin' ), strtolower( $plural_label ) ),
'parent_item_colon' => sprintf( __( 'Parent %s:', 'tenup-plugin' ), $plural_label ),
'all_items' => sprintf( __( 'All %s', 'tenup-plugin' ), $plural_label ),
'archives' => sprintf( __( '%s Archives', 'tenup-plugin' ), $singular_label ),
'attributes' => sprintf( __( '%s Attributes', 'tenup-plugin' ), $singular_label ),
'insert_into_item' => sprintf( __( 'Insert into %s', 'tenup-plugin' ), strtolower( $singular_label ) ),
'uploaded_to_this_item' => sprintf( __( 'Uploaded to this %s', 'tenup-plugin' ), strtolower( $singular_label ) ),
'filter_items_list' => sprintf( __( 'Filter %s list', 'tenup-plugin' ), strtolower( $plural_label ) ),
'items_list_navigation' => sprintf( __( '%s list navigation', 'tenup-plugin' ), $plural_label ),
'items_list' => sprintf( __( '%s list', 'tenup-plugin' ), $plural_label ),
'item_published' => sprintf( __( '%s published.', 'tenup-plugin' ), $singular_label ),
'item_published_privately' => sprintf( __( '%s published privately.', 'tenup-plugin' ), $singular_label ),
'item_reverted_to_draft' => sprintf( __( '%s reverted to draft.', 'tenup-plugin' ), $singular_label ),
'item_scheduled' => sprintf( __( '%s scheduled.', 'tenup-plugin' ), $singular_label ),
'item_updated' => sprintf( __( '%s updated.', 'tenup-plugin' ), $singular_label ),
'menu_name' => $plural_label,
'name_admin_bar' => $singular_label,
);
// phpcs:enable

return $labels;
}

/**
* Registers a post type and associates its taxonomies.
*
* @uses $this->get_name() to get the post's type name.
* @return Bool Whether this theme has supports for this post type.
*/
public function register() {
$this->register_post_type();
$this->register_taxonomies();
darylldoyle marked this conversation as resolved.
Show resolved Hide resolved

$this->after_register();

return true;
}

/**
* Registers the current post type with WordPress.
*
* @return void
*/
public function register_post_type() {
register_post_type(
$this->get_name(),
$this->get_options()
);
}

/**
* Registers the taxonomies declared with the current post type.
*
* @return void
*/
public function register_taxonomies() {
$taxonomies = $this->get_supported_taxonomies();

$object_type = $this->get_name();

if ( ! empty( $taxonomies ) ) {
foreach ( $taxonomies as $taxonomy ) {
register_taxonomy_for_object_type(
$taxonomy,
$object_type
);
}
}
}

/**
* Returns the default supported taxonomies. The subclass should declare the
* Taxonomies that it supports here if required.
*
* @return array
*/
public function get_supported_taxonomies() {
return [];
}

/**
* Run any code after the post type has been registered.
*
* @return void
*/
public function after_register() {
// Do nothing.
}

}
71 changes: 71 additions & 0 deletions mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Demo Post Type
*
* @package TenUpPlugin
*/

namespace TenUpPlugin\PostTypes;

/**
* Demo post type.
*/
class Demo extends AbstractPostType {

/**
* Get the post type name.
*
* @return string
*/
public function get_name() {
return 'tenup-demo';
}

/**
* Get the singular post type label.
*
* @return string
*/
public function get_singular_label() {
return esc_html__( 'Demo', 'tenup-plugin' );
}

/**
* Get the plural post type label.
*
* @return string
*/
public function get_plural_label() {
return esc_html__( 'Demos', 'tenup-plugin' );
}

/**
* Can the class be registered?
*
* @return bool
*/
public function can_register() {
return false;
}

/**
* Returns the default supported taxonomies. The subclass should declare the
* Taxonomies that it supports here if required.
*
* @return array
*/
public function get_supported_taxonomies() {
return [
'tenup-tax-demo',
];
}

/**
* Run any code after the post type has been registered.
*
* @return void
*/
public function after_register() {
// Register any hooks/filters you need.
}
}
32 changes: 32 additions & 0 deletions mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Page Post Type
*
* @package TenUpPlugin
*/

namespace TenUpPlugin\PostTypes;

/**
* Page Post Type
*/
class Page extends \TenUpPlugin\Module {

/**
* Can the class be registered?
*
* @return bool
*/
public function can_register() {
return true;
}

/**
* Register hooks and filters.
*
* @return void
*/
public function register() {
// Register any hooks/filters you need.
}
}
32 changes: 32 additions & 0 deletions mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Post Post type.
*
* @package TenUpPlugin
*/

namespace TenUpPlugin\PostTypes;

/**
* Post Post type.
*/
class Post extends \TenUpPlugin\Module {
darylldoyle marked this conversation as resolved.
Show resolved Hide resolved

/**
* Can the class be registered?
*
* @return bool
*/
public function can_register() {
return true;
}

/**
* Register hooks and filters.
*
* @return void
*/
public function register() {
// Register any hooks/filters you need.
}
}
Loading
Loading