1 <?php
2 /**
3 * Admin Page Framework
4 *
5 * http://en.michaeluno.jp/admin-page-framework/
6 * Copyright (c) 2013-2014 Michael Uno; Licensed MIT
7 *
8 */
9 if ( ! class_exists( 'AdminPageFramework_PostType_Model' ) ) :
10 /**
11 * Provides methods of models for the post type factory class.
12 *
13 * Those methods are internal and deal with internal properties.
14 *
15 * @abstract
16 * @since 3.0.4
17 * @package AdminPageFramework
18 * @subpackage PostType
19 */
20 abstract class AdminPageFramework_PostType_Model extends AdminPageFramework_PostType_Router {
21
22 function __construct( $oProp ) {
23
24 parent::__construct( $oProp );
25
26 add_action( 'init', array( $this, '_replyToRegisterPostType' ), 999 ); // this is loaded in the front-end as well so should not be admin_init. Also "if ( is_admin() )" should not be used either.
27
28 // Properties
29 $this->oProp->aColumnHeaders = array(
30 'cb' => '<input type="checkbox" />', // Checkbox for bulk actions.
31 'title' => $this->oMsg->__( 'title' ), // Post title. Includes "edit", "quick edit", "trash" and "view" links. If $mode (set from $_REQUEST['mode']) is 'excerpt', a post excerpt is included between the title and links.
32 'author' => $this->oMsg->__( 'author' ), // Post author.
33 // 'categories' => $this->oMsg->__( 'categories' ), // Categories the post belongs to.
34 // 'tags' => $this->oMsg->__( 'tags' ), // Tags for the post.
35 'comments' => '<div class="comment-grey-bubble"></div>', // Number of pending comments.
36 'date' => $this->oMsg->__( 'date' ), // The date and publish status of the post.
37 );
38
39 if ( $this->_isInThePage() ) :
40
41 // For table columns
42 add_filter( "manage_{$this->oProp->sPostType}_posts_columns", array( $this, '_replyToSetColumnHeader' ) );
43 add_filter( "manage_edit-{$this->oProp->sPostType}_sortable_columns", array( $this, '_replyToSetSortableColumns' ) );
44 add_action( "manage_{$this->oProp->sPostType}_posts_custom_column", array( $this, '_replyToSetColumnCell' ), 10, 2 );
45
46 // Auto-save
47 add_action( 'admin_enqueue_scripts', array( $this, '_replyToDisableAutoSave' ) );
48
49 endif;
50
51 }
52
53 /**
54 * Determines whether the currently loaded page is of the post type page.
55 *
56 * @since 3.0.4
57 */
58 protected function _isInThePage() {
59
60 // If it's not in one of the post type's pages
61 if ( ! $this->oProp->bIsAdmin ) {
62 return false;
63 }
64 if ( ! in_array( $this->oProp->sPageNow, array( 'edit.php', 'edit-tags.php', 'post.php', 'post-new.php' ) ) ) {
65 return false;
66 }
67
68 return ( $this->oUtil->getCurrentPostType() == $this->oProp->sPostType );
69
70 }
71
72
73 /**
74 * Defines the sortable column items in the custom post listing table.
75 *
76 * This method should be overridden by the user in their extended class.
77 *
78 * @since 2.0.0
79 * @remark A callback for the <em>manage_edit-{post type}_sortable_columns</em> hook.
80 * @internal
81 */
82 public function _replyToSetSortableColumns( $aColumns ) {
83 return $this->oUtil->addAndApplyFilter( $this, "sortable_columns_{$this->oProp->sPostType}", $aColumns );
84 }
85
86
87 /**
88 * Defines the column header items in the custom post listing table.
89 *
90 * This method should be overridden by the user in their extended class.
91 *
92 * @since 2.0.0
93 * @remark A callback for the <em>manage_{post type}_post)_columns</em> hook.
94 * @return void
95 * @internal
96 */
97 public function _replyToSetColumnHeader( $aHeaderColumns ) {
98 return $this->oUtil->addAndApplyFilter( $this, "columns_{$this->oProp->sPostType}", $aHeaderColumns );
99 }
100
101 /**
102 *
103 * @internal
104 */
105 public function _replyToSetColumnCell( $sColumnTitle, $iPostID ) {
106
107 // cell_{post type}_{custom column key}
108 echo $this->oUtil->addAndApplyFilter( $this, "cell_{$this->oProp->sPostType}_{$sColumnTitle}", $sCell='', $iPostID );
109
110 }
111
112 /**
113 * Disables the WordPress's built-in auto-save functionality.
114 *
115 * @internal
116 */
117 public function _replyToDisableAutoSave() {
118
119 if ( $this->oProp->bEnableAutoSave ) return;
120 if ( $this->oProp->sPostType != get_post_type() ) return;
121 wp_dequeue_script( 'autosave' );
122
123 }
124
125 /**
126 * Registers the post type passed to the constructor.
127 *
128 * @internal
129 */
130 public function _replyToRegisterPostType() {
131
132 register_post_type( $this->oProp->sPostType, $this->oProp->aPostTypeArgs );
133
134 // if ( ! get_option( "post_type_rules_flased_{$this->oProp->sPostType}" ) ) {
135 // flush_rewrite_rules( false );
136 // update_option( "post_type_rules_flased_{$this->oProp->sPostType}", true );
137 // }
138
139 }
140
141 /**
142 * Registers the set custom taxonomies.
143 *
144 * @internal
145 * @remark 0.01 elapsed for this function call in the demo plugin which has two custom taxonomies.
146 */
147 public function _replyToRegisterTaxonomies() {
148
149 foreach( $this->oProp->aTaxonomies as $_sTaxonomySlug => $_aArgs ) {
150 $_aObjectTypes = is_array( $this->oProp->aTaxonomyObjectTypes[ $_sTaxonomySlug ] ) ? $this->oProp->aTaxonomyObjectTypes[ $_sTaxonomySlug ] : array();
151 $_aObjectTypes[] = $this->oProp->sPostType;
152 register_taxonomy(
153 $_sTaxonomySlug,
154 array_unique( $_aObjectTypes ), // object types
155 $_aArgs // for the argument array keys, refer to: http://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments
156 );
157 }
158
159 }
160
161 /**
162 * Removes taxonomy menu items from the sidebar menu.
163 *
164 * @internal
165 */
166 public function _replyToRemoveTexonomySubmenuPages() {
167
168 foreach( $this->oProp->aTaxonomyRemoveSubmenuPages as $sSubmenuPageSlug => $sTopLevelPageSlug ) {
169 remove_submenu_page( $sTopLevelPageSlug, $sSubmenuPageSlug );
170 }
171
172 }
173
174
175 }
176 endif;