-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/post-type-class' into develop
- Loading branch information
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
/** | ||
* Set up the Award post type | ||
* | ||
* @package EmployeeRecognition | ||
*/ | ||
|
||
namespace Hrswp\EmployeeRecognition\AwardPostType; | ||
|
||
/** | ||
* Post type class. | ||
*/ | ||
class Award_Post_Type { | ||
|
||
/** | ||
* Sets up the award post type. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public function setup(): void { | ||
add_action( 'init', array( $this, 'action_register_post_types' ) ); | ||
add_action( 'init', array( $this, 'action_register_award_meta' ) ); | ||
} | ||
|
||
/** | ||
* Registers the award post type. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @see register_post_type | ||
* @return void | ||
*/ | ||
public function action_register_post_types(): void { | ||
$award_labels = array( | ||
'name' => esc_html_x( 'ER Awards Manager', 'post type general name', 'hrswp-employee-recognition' ), | ||
'singular_name' => esc_html_x( 'Award', 'post type singular name', 'hrswp-employee-recognition' ), | ||
'add_new' => _x( 'Create Award', 'post type', 'hrswp-employee-recognition' ), | ||
'add_new_item' => esc_html__( 'ER Awards Manager', 'hrswp-employee-recognition' ), | ||
'edit_item' => esc_html__( 'Edit Award', 'hrswp-employee-recognition' ), | ||
'new_item' => esc_html__( 'New Award', 'hrswp-employee-recognition' ), | ||
'all_items' => esc_html__( 'ER Awards Manager', 'hrswp-employee-recognition' ), | ||
'view_items' => esc_html__( 'View Award', 'hrswp-employee-recognition' ), | ||
'search_items' => esc_html__( 'Search Awards', 'hrswp-employee-recognition' ), | ||
'not_found' => esc_html__( 'No awards found.', 'hrswp-employee-recognition' ), | ||
'not_found_in_trash' => esc_html__( 'No awards found in trash.', 'hrswp-employee-recognition' ), | ||
'parent_item_colon' => '', | ||
'menu_name' => esc_html__( 'ER Awards', 'hrswp-employee-recognition' ), | ||
'featured_image' => esc_html__( 'Award image', 'hrswp-employee-recognition' ), | ||
'set_featured_image' => esc_html__( 'Set award image', 'hrswp-employee-recognition' ), | ||
'remove_featured_image' => esc_html__( 'Remove award image.', 'hrswp-employee-recognition' ), | ||
'use_featured_image' => esc_html__( 'Use as award image', 'hrswp-employee-recognition' ), | ||
); | ||
|
||
$template = array(); | ||
|
||
$award_args = array( | ||
'labels' => $award_labels, | ||
'public' => false, | ||
'publicly_queryable' => true, | ||
'show_ui' => true, | ||
'show_in_menu' => 'tools.php', | ||
'menu_position' => 80, | ||
'query_var' => false, | ||
'rewrite' => false, | ||
'show_in_rest' => true, | ||
'capability_type' => 'post', | ||
'has_archive' => false, | ||
'hierarchical' => false, | ||
'template' => $template, | ||
'template_lock' => 'all', | ||
'supports' => array( | ||
'title', | ||
'editor', | ||
'author', | ||
'custom-fields', | ||
'thumbnail', | ||
), | ||
); | ||
|
||
register_post_type( 'hrswp_er_awards', $award_args ); | ||
} | ||
|
||
/** | ||
* Registers the ER Awards post meta. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @see register_meta | ||
* @return void | ||
*/ | ||
public function action_register_award_meta() { | ||
register_meta( | ||
'post', | ||
'hrswp_er_awards_year', | ||
array( | ||
'object_subtype' => 'hrswp_er_awards', | ||
'type' => 'integer', | ||
'default' => 1, | ||
'show_in_rest' => true, | ||
'single' => true, | ||
'sanitize_callback' => function( $value ) { | ||
$value = (int) $value; | ||
if ( empty( $value ) ) { | ||
$value = 1; | ||
} | ||
if ( $value < -1 ) { | ||
$value = abs( $value ); | ||
} | ||
return $value; | ||
}, | ||
'auth_callback' => function() { | ||
return current_user_can( 'edit_posts' ); | ||
}, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Returns a singleton instance of the class. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @return Award_Post_Type | ||
*/ | ||
public static function factory(): Award_Post_Type { | ||
static $instance = false; | ||
|
||
if ( ! $instance ) { | ||
$instance = new self(); | ||
$instance->setup(); | ||
} | ||
|
||
return $instance; | ||
} | ||
} |