WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-custom-post-type.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * [DRAFT]
6  *
7  * THE FOLLOWING CODE IS A DRAFT. FEEL FREE TO USE IT TO MAKE SOME EXPERIMENTS, BUT DO NOT USE IT IN ANY CASE IN
8  * PRODUCTION ENVIRONMENT. ALL CLASSES AND RELATIVE METHODS BELOW CAN CHNAGE IN THE FUTURE RELEASES.
9  *
10  */
11 
23 class WPDKCustomPostType extends WPDKObject {
24 
32  public $__version = '1.0.2';
33 
41  public $id = '';
42 
51  public $url_images = '';
52 
60  public function __construct( $id, $args )
61  {
62  /* Save useful properties */
63  $this->id = $id;
64 
65  /* Do a several control check in the input $args array */
66 
67  /* Register MetaBox */
68  if ( !isset( $args['register_meta_box_cb'] ) ) {
69  $args['register_meta_box_cb'] = array( $this, 'register_meta_box' );
70  }
71 
72  /* Get icons path */
73  if ( isset( $args['menu_icon'] ) ) {
74  $this->url_images = trailingslashit( dirname( $args['menu_icon'] ) );
75  }
76 
77  /* Register custom post type. */
78  register_post_type( $id, $args );
79 
80  /* Init admin hook */
81  $this->initAdminHook();
82  }
83 
89  private function initAdminHook()
90  {
91  if ( is_admin() ) {
92 
93  /* Body header class */
94  add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
95 
96  /* Feedback */
97  add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
98 
99  /* Default Enter title */
100  add_filter( 'enter_title_here', array( $this, '_enter_title_here' ) );
101 
102  /* Hook save post */
103  add_action( 'save_post_' . $this->id, array( $this, 'save_post' ), 10, 2 );
104 
105  /* Manage column */
106  add_action( 'manage_' . $this->id . '_posts_custom_column', array( $this, 'manage_posts_custom_column' ) );
107  add_filter( 'manage_edit-' . $this->id . '_columns', array( $this, 'manage_edit_columns') );
108  add_filter( 'manage_edit-' . $this->id . '_sortable_columns', array( $this, 'manage_edit_sortable_columns' ) );
109 
110  /* Will loaded... */
111  /* Action when cpt list o cpt edit are invokes */
112  add_action( 'admin_head-post.php', array( $this, '_will_load_post_list_edit' ) );
113  add_action( 'admin_head-edit.php', array( $this, '_will_load_post_list_edit' ) );
114  add_action( 'admin_head-post-new.php', array( $this, '_will_load_post_new' ) );
115  add_action( 'current_screen', array( $this, '_current_screen' ) );
116 
117  /* Footer page. */
118  add_action( 'admin_footer-post.php', array( $this, 'admin_footer') );
119 
120  /* Display right icon on the title */
121  if( !empty( $this->url_images ) ) {
122  add_action( 'admin_head', array( $this, 'admin_head' ) );
123  }
124  }
125  }
126 
132  public function admin_head()
133  {
134  global $post_type;
135 
136  if ( $post_type != $this->id ) {
137  return;
138  }
139 
141  ?>
142  <style type="text/css">
143  body.post-type-<?php echo $this->id ?> .wrap > h2
144  {
145  background-image : url(<?php echo $this->url_images ?>logo-64x64.png);
146  background-repeat : no-repeat;
147  height : 64px;
148  line-height : 64px;
149  padding : 0 0 0 80px;
150  }
151  </style>
152  <?php
154  }
155 
165  public function admin_body_class( $classes )
166  {
167  //$classes .= ' wpdk-header-view ' . self::ID;
168  return $classes;
169  }
170 
183  public function post_updated_messages()
184  {
185  /* You can override this hook method */
186  }
187 
197  public function _enter_title_here( $title )
198  {
199  global $post_type;
200 
201  if ( $post_type == $this->id ) {
202  $title = $this->shouldEnterTitleHere( $title );
203  }
204  return $title;
205  }
206 
216  public function shouldEnterTitleHere( $title )
217  {
218  /* You can override this delegate method */
219  return $title;
220  }
221 
233  public function save_post( $post_id, $post = '' )
234  {
235 
236  // Do not save...
237  if ( ( defined( 'DOING_AUTOSAVE' ) && true === DOING_AUTOSAVE ) ||
238  ( defined( 'DOING_AJAX' ) && true === DOING_AJAX ) ||
239  ( defined( 'DOING_CRON' ) && true === DOING_CRON )
240  ) {
241  return;
242  }
243 
244  // Get post type information
245  $post_type = get_post_type();
246  $post_type_object = get_post_type_object( $post_type );
247 
248  // Exit
249  if ( false == $post_type || is_null( $post_type_object ) ) {
250  return;
251  }
252 
253  // This function only applies to the following post_types
254  if ( !in_array( $post_type, array( $this->id ) ) ) {
255  return;
256  }
257 
258  // Find correct capability from post_type arguments
259  if ( isset( $post_type_object->cap->edit_posts ) ) {
260  $capability = $post_type_object->cap->edit_posts;
261 
262  // Return if current user cannot edit this post
263  if ( !current_user_can( $capability ) ) {
264  return;
265  }
266  }
267 
268  // If all ok and post request then update()
269  if ( wpdk_is_request_post() ) {
270  $this->update( $post_id, $post );
271  }
272 
273  }
274 
285  public function update( $post_id, $post )
286  {
287  /* You can override this method to save your own data */
288  }
289 
299  public function manage_posts_custom_column( $column )
300  {
301  /* You can override this hook method */
302  }
303 
313  public function manage_edit_columns( $columns )
314  {
315  /* You can override this hook method */
316  return $columns;
317  }
318 
328  public function manage_edit_sortable_columns( $columns )
329  {
330  /* You can override this hook method */
331  return $columns;
332  }
333 
339  public function _will_load_post_list_edit()
340  {
341  global $post_type;
342 
343  if ( $post_type == $this->id ) {
344  $this->willLoadAdminPost();
345  if ( isset( $_REQUEST['action'] ) && 'edit' == $_REQUEST['action'] ) {
346  $this->willLoadEditPost();
347  } else {
348  $this->willLoadListPost();
349  }
350  }
351  }
352 
358  public function willLoadEditPost()
359  {
360  /* You can override this delegate method */
361  }
362 
368  public function willLoadListPost()
369  {
370  /* You can override this delegate method */
371  }
372 
378  public function _will_load_post_new()
379  {
380  global $post_type;
381 
382  if ( $this->id == $post_type ) {
383  $this->willLoadAdminPost();
384  $this->willLoadPostNew();
385  }
386  }
387 
393  public function willLoadPostNew()
394  {
395  /* You can override this delegate method */
396  }
397 
403  public function willLoadAdminPost()
404  {
405  /* You can override this delegate method */
406  }
407 
415  public function _current_screen( $screen )
416  {
417  if ( !empty( $screen->post_type ) && $screen->post_type == $this->id ) {
418  $this->willLoadAdminPost();
419  }
420  }
421 
422 
429  public function admin_footer()
430  {
431 
432  }
433 
439  public function register_meta_box()
440  {
441  /* You can override this hook method */
442  }
443 
444 }
445