Skip to content

Commit

Permalink
Merge branch 'feature/post-type-class' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
admturner committed Apr 7, 2022
2 parents 3cfe368 + 0cee3a2 commit e5403e7
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
7 changes: 7 additions & 0 deletions hrswp-employee-recognition.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@

namespace Hrswp\EmployeeRecognition;

use Hrswp\EmployeeRecognition\AwardPostType;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

// Load class.
require_once dirname( __FILE__ ) . '/inc/classes/class-award-post-type.php';

AwardPostType\Award_Post_Type::factory();
135 changes: 135 additions & 0 deletions inc/classes/class-award-post-type.php
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;
}
}

0 comments on commit e5403e7

Please sign in to comment.