| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post type declaration file. |
| 4 |
* |
| 5 |
* @package Bricksable/Includes |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Post type declaration class. |
| 14 |
*/ |
| 15 |
class Bricksable_Post_Type { |
| 16 |
|
| 17 |
/** |
| 18 |
* The name for the custom post type. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
* @access public |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
public $post_type; |
| 25 |
|
| 26 |
/** |
| 27 |
* The plural name for the custom post type posts. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
* @access public |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public $plural; |
| 34 |
|
| 35 |
/** |
| 36 |
* The singular name for the custom post type posts. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
* @access public |
| 40 |
* @since 1.0.0 |
| 41 |
*/ |
| 42 |
public $single; |
| 43 |
|
| 44 |
/** |
| 45 |
* The description of the custom post type. |
| 46 |
* |
| 47 |
* @var string |
| 48 |
* @access public |
| 49 |
* @since 1.0.0 |
| 50 |
*/ |
| 51 |
public $description; |
| 52 |
|
| 53 |
/** |
| 54 |
* The options of the custom post type. |
| 55 |
* |
| 56 |
* @var array |
| 57 |
* @access public |
| 58 |
* @since 1.0.0 |
| 59 |
*/ |
| 60 |
public $options; |
| 61 |
|
| 62 |
/** |
| 63 |
* Constructor |
| 64 |
* |
| 65 |
* @param string $post_type Post type. |
| 66 |
* @param string $plural Post type plural name. |
| 67 |
* @param string $single Post type singular name. |
| 68 |
* @param string $description Post type description. |
| 69 |
* @param array $options Post type options. |
| 70 |
*/ |
| 71 |
public function __construct( $post_type = '', $plural = '', $single = '', $description = '', $options = array() ) { |
| 72 |
|
| 73 |
if ( ! $post_type || ! $plural || ! $single ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
// Post type name and labels. |
| 78 |
$this->post_type = $post_type; |
| 79 |
$this->plural = $plural; |
| 80 |
$this->single = $single; |
| 81 |
$this->description = $description; |
| 82 |
$this->options = $options; |
| 83 |
|
| 84 |
// Regsiter post type. |
| 85 |
add_action( 'init', array( $this, 'register_post_type' ) ); |
| 86 |
|
| 87 |
// Display custom update messages for posts edits. |
| 88 |
add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) ); |
| 89 |
add_filter( 'bulk_post_updated_messages', array( $this, 'bulk_updated_messages' ), 10, 2 ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Register new post type |
| 94 |
* |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function register_post_type() { |
| 98 |
//phpcs:disable |
| 99 |
$labels = array( |
| 100 |
'name' => $this->plural, |
| 101 |
'singular_name' => $this->single, |
| 102 |
'name_admin_bar' => $this->single, |
| 103 |
'add_new' => _x( 'Add New', $this->post_type, 'bricksable' ), |
| 104 |
'add_new_item' => sprintf( __( 'Add New %s', 'bricksable' ), $this->single ), |
| 105 |
'edit_item' => sprintf( __( 'Edit %s', 'bricksable' ), $this->single ), |
| 106 |
'new_item' => sprintf( __( 'New %s', 'bricksable' ), $this->single ), |
| 107 |
'all_items' => sprintf( __( 'All %s', 'bricksable' ), $this->plural ), |
| 108 |
'view_item' => sprintf( __( 'View %s', 'bricksable' ), $this->single ), |
| 109 |
'search_items' => sprintf( __( 'Search %s', 'bricksable' ), $this->plural ), |
| 110 |
'not_found' => sprintf( __( 'No %s Found', 'bricksable' ), $this->plural ), |
| 111 |
'not_found_in_trash' => sprintf( __( 'No %s Found In Trash', 'bricksable' ), $this->plural ), |
| 112 |
'parent_item_colon' => sprintf( __( 'Parent %s' ), $this->single ), |
| 113 |
'menu_name' => $this->plural, |
| 114 |
); |
| 115 |
//phpcs:enable |
| 116 |
|
| 117 |
$args = array( |
| 118 |
'labels' => apply_filters( $this->post_type . '_labels', $labels ), |
| 119 |
'description' => $this->description, |
| 120 |
'public' => true, |
| 121 |
'publicly_queryable' => true, |
| 122 |
'exclude_from_search' => false, |
| 123 |
'show_ui' => true, |
| 124 |
'show_in_menu' => true, |
| 125 |
'show_in_nav_menus' => true, |
| 126 |
'query_var' => true, |
| 127 |
'can_export' => true, |
| 128 |
'rewrite' => true, |
| 129 |
'capability_type' => 'post', |
| 130 |
'has_archive' => true, |
| 131 |
'hierarchical' => true, |
| 132 |
'show_in_rest' => true, |
| 133 |
'rest_base' => $this->post_type, |
| 134 |
'rest_controller_class' => 'WP_REST_Posts_Controller', |
| 135 |
'supports' => array( 'title', 'editor', 'excerpt', 'comments', 'thumbnail' ), |
| 136 |
'menu_position' => 5, |
| 137 |
'menu_icon' => 'dashicons-admin-post', |
| 138 |
); |
| 139 |
|
| 140 |
$args = array_merge( $args, $this->options ); |
| 141 |
|
| 142 |
register_post_type( $this->post_type, apply_filters( $this->post_type . '_register_args', $args, $this->post_type ) ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Set up admin messages for post type |
| 147 |
* |
| 148 |
* @param array $messages Default message. |
| 149 |
* @return array Modified messages. |
| 150 |
*/ |
| 151 |
public function updated_messages( $messages = array() ) { |
| 152 |
global $post, $post_ID; |
| 153 |
//phpcs:disable |
| 154 |
$messages[ $this->post_type ] = array( |
| 155 |
0 => '', |
| 156 |
1 => sprintf( __( '%1$s updated. %2$sView %3$s%4$s.', 'bricksable' ), $this->single, '<a href="' . esc_url( get_permalink( $post_ID ) ) . '">', $this->single, '</a>' ), |
| 157 |
2 => __( 'Custom field updated.', 'bricksable' ), |
| 158 |
3 => __( 'Custom field deleted.', 'bricksable' ), |
| 159 |
4 => sprintf( __( '%1$s updated.', 'bricksable' ), $this->single ), |
| 160 |
5 => isset( $_GET['revision'] ) ? sprintf( __( '%1$s restored to revision from %2$s.', 'bricksable' ), $this->single, wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, |
| 161 |
6 => sprintf( __( '%1$s published. %2$sView %3$s%4$s.', 'bricksable' ), $this->single, '<a href="' . esc_url( get_permalink( $post_ID ) ) . '">', $this->single, '</a>' ), |
| 162 |
7 => sprintf( __( '%1$s saved.', 'bricksable' ), $this->single ), |
| 163 |
8 => sprintf( __( '%1$s submitted. %2$sPreview post%3$s%4$s.', 'bricksable' ), $this->single, '<a target="_blank" href="' . esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ) . '">', $this->single, '</a>' ), |
| 164 |
9 => sprintf( __( '%1$s scheduled for: %2$s. %3$sPreview %4$s%5$s.', 'bricksable' ), $this->single, '<strong>' . date_i18n( __( 'M j, Y @ G:i', 'bricksable' ), strtotime( $post->post_date ) ) . '</strong>', '<a target="_blank" href="' . esc_url( get_permalink( $post_ID ) ) . '">', $this->single, '</a>' ), |
| 165 |
10 => sprintf( __( '%1$s draft updated. %2$sPreview %3$s%4$s.', 'bricksable' ), $this->single, '<a target="_blank" href="' . esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ) . '">', $this->single, '</a>' ), |
| 166 |
); |
| 167 |
//phpcs:enable |
| 168 |
|
| 169 |
return $messages; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Set up bulk admin messages for post type |
| 174 |
* |
| 175 |
* @param array $bulk_messages Default bulk messages. |
| 176 |
* @param array $bulk_counts Counts of selected posts in each status. |
| 177 |
* @return array Modified messages. |
| 178 |
*/ |
| 179 |
public function bulk_updated_messages( $bulk_messages = array(), $bulk_counts = array() ) { |
| 180 |
|
| 181 |
//phpcs:disable |
| 182 |
$bulk_messages[ $this->post_type ] = array( |
| 183 |
'updated' => sprintf( _n( '%1$s %2$s updated.', '%1$s %3$s updated.', $bulk_counts['updated'], 'bricksable' ), $bulk_counts['updated'], $this->single, $this->plural ), |
| 184 |
'locked' => sprintf( _n( '%1$s %2$s not updated, somebody is editing it.', '%1$s %3$s not updated, somebody is editing them.', $bulk_counts['locked'], 'bricksable' ), $bulk_counts['locked'], $this->single, $this->plural ), |
| 185 |
'deleted' => sprintf( _n( '%1$s %2$s permanently deleted.', '%1$s %3$s permanently deleted.', $bulk_counts['deleted'], 'bricksable' ), $bulk_counts['deleted'], $this->single, $this->plural ), |
| 186 |
'trashed' => sprintf( _n( '%1$s %2$s moved to the Trash.', '%1$s %3$s moved to the Trash.', $bulk_counts['trashed'], 'bricksable' ), $bulk_counts['trashed'], $this->single, $this->plural ), |
| 187 |
'untrashed' => sprintf( _n( '%1$s %2$s restored from the Trash.', '%1$s %3$s restored from the Trash.', $bulk_counts['untrashed'], 'bricksable' ), $bulk_counts['untrashed'], $this->single, $this->plural ), |
| 188 |
); |
| 189 |
//phpcs:enable |
| 190 |
|
| 191 |
return $bulk_messages; |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|