WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-post.php
Go to the documentation of this file.
1 <?php
2 
25 class WPDKPost extends WPDKObject {
26 
27  const COLUMN_COMMENT_COUNT = 'comment_count';
28  const COLUMN_COMMENT_STATUS = 'comment_status';
29  const COLUMN_GUID = 'guid';
30  const COLUMN_ID = 'ID';
31  const COLUMN_MENU_ORDER = 'menu_order';
32  const COLUMN_PINGED = 'pinged';
33  const COLUMN_PING_STATUS = 'ping_status';
34  const COLUMN_POST_AUTHOR = 'post_author';
35  const COLUMN_POST_CONTENT = 'post_content';
36  const COLUMN_POST_CONTENT_FILTERED = 'post_content_filtered';
37  const COLUMN_POST_DATE = 'post_date';
38  const COLUMN_POST_DATE_GMT = 'post_date_gmt';
39  const COLUMN_POST_EXCERPT = 'post_excerpt';
40  const COLUMN_POST_MIME_TYPE = 'post_mime_type';
41  const COLUMN_POST_MODIFIED = 'post_modified';
42  const COLUMN_POST_MODIFIED_GMT = 'post_modified_gmt';
43  const COLUMN_POST_NAME = 'post_name';
44  const COLUMN_POST_PARENT = 'post_parent';
45  const COLUMN_POST_PASSWORD = 'post_password';
46  const COLUMN_POST_STATUS = 'post_status';
47  const COLUMN_POST_TITLE = 'post_title';
48  const COLUMN_POST_TYPE = 'post_type';
49  const COLUMN_TO_PING = 'to_ping';
50 
58  public $__version = '1.0.2';
59 
67  public $ID;
92  public $guid;
100  public $menu_order;
108  public $ping_status;
116  public $pinged;
124  public $post_author;
158  public $post_date;
208  public $post_name;
216  public $post_parent;
232  public $post_status;
240  public $post_title;
249  public $post_type;
257  public $to_ping;
258 
270  public function __construct( $record = null, $post_type = 'page' )
271  {
272 
273  /* Get post by id. */
274  if ( !is_null( $record ) && is_numeric( $record ) ) {
275  $this->initPostByID( absint( $record ) );
276  }
277 
278  /* Get post from database record. */
279  elseif ( !is_null( $record ) && is_object( $record ) && isset( $record->ID ) ) {
280  $this->initPostByPost( $record );
281  }
282 
283  /* Get post by name. */
284  elseif ( !is_null( $record ) && is_string( $record ) ) {
285  /* @todo Use get by name */
286  $object = get_page_by_path( $record, OBJECT, $post_type );
287  $this->initPostByPost( $object );
288  }
289 
290  /* Create an empty post. */
291  elseif ( is_null( $record ) ) {
292  /* Create a new onfly post */
293  $defaults = $this->postEmpty();
294  $this->initPostByArgs( $defaults );
295  }
296  }
297 
298  // -----------------------------------------------------------------------------------------------------------------
299  // Create/Get Post
300  // -----------------------------------------------------------------------------------------------------------------
301 
309  private function initPostByID( $id_post )
310  {
311  if ( isset( $GLOBALS[__CLASS__][$id_post] ) ) {
312  $post = $GLOBALS[__CLASS__][$id_post];
313  }
314  else {
315  $GLOBALS[__CLASS__][$id_post] = $post = get_post( $id_post );
316  }
317  $this->initPostByPost( $post );
318  }
319 
327  private function initPostByPost( $post )
328  {
329  if ( is_object( $post ) ) {
330  /* Get properties. */
331  foreach ( $post as $property => $value ) {
332  $this->$property = $value;
333  }
334  }
335  }
336 
345  private function postEmpty()
346  {
347  $args = array(
348  'ID' => 0,
349  'post_author' => 0,
350  'post_date' => '0000-00-00 00:00:00',
351  'post_date_gmt' => '0000-00-00 00:00:00',
352  'post_content' => '',
353  'post_title' => '',
354  'post_excerpt' => '',
355  'post_status' => WPDKPostStatus::PUBLISH,
356  'comment_status' => 'open',
357  'ping_status' => 'open',
358  'post_password' => '',
359  'post_name' => '',
360  'to_ping' => '',
361  'pinged' => '',
362  'post_modified' => '0000-00-00 00:00:00',
363  'post_modified_gmt' => '0000-00-00 00:00:00',
364  'post_content_filtered' => '',
365  'post_parent' => 0,
366  'guid' => '',
367  'menu_order' => 0,
368  'post_type' => WPDKPostType::POST,
369  'post_mime_type' => '',
370  'comment_count' => 0
371  );
372  return $args;
373  }
374 
375  // -----------------------------------------------------------------------------------------------------------------
376  // Empty Post
377  // -----------------------------------------------------------------------------------------------------------------
378 
384  private function initPostByArgs( $args )
385  {
386  foreach ( $args as $property => $value ) {
387  $this->$property = $value;
388  }
389  }
390 
391  // -----------------------------------------------------------------------------------------------------------------
392  // CRUD
393  // -----------------------------------------------------------------------------------------------------------------
394 
405  public function delete()
406  {
407  return wp_delete_post( $this->ID, true );
408  }
409 
421  public function trash()
422  {
423  return wp_trash_post( $this->ID );
424  }
425 
436  public function untrash()
437  {
438  return wp_untrash_post( $this->ID );
439  }
440 
453  public function update()
454  {
455  /* Avoid update when we are in admin backend area. */
456  global $pagenow;
457 
458  if ( 'post.php' != $pagenow ) {
459  return wp_update_post( $this, true );
460  }
461  }
462 
469  public function updateMeta( $args = array() )
470  {
471  self::updateMetaWithID( $this->ID, $args );
472  }
473 
483  public static function updateMetaWithID( $post_id, $args = array() )
484  {
485  if ( !empty( $post_id ) && !empty( $args ) ) {
486  foreach ( $args as $meta_key => $meta_value ) {
487  update_post_meta( $post_id, $meta_key, $meta_value );
488  }
489  }
490  }
491 
503  public function metaValue( $meta_key )
504  {
505  if ( empty( $this->ID ) ) {
506  return false;
507  }
508  if ( func_num_args() > 1 ) {
509  $value = func_get_arg( 1 );
510  return update_post_meta( $this->ID, $meta_key, $value );
511  }
512  return get_post_meta( $this->ID, $meta_key, true );
513  }
514 
526  public function metaValues( $meta_key )
527  {
528  if ( empty( $this->ID ) ) {
529  return false;
530  }
531  if ( func_num_args() > 1 ) {
532  $value = func_get_arg( 1 );
533  return update_post_meta( $this->ID, $meta_key, $value );
534  }
535  return get_post_meta( $this->ID, $meta_key );
536  }
537 
549  public function thumbnail( $size = 'full' )
550  {
551  return self::thumbnailWithID( $this->ID, $size );
552  }
553 
566  public static function thumbnailWithID( $post_id, $size = 'full' )
567  {
568  if ( empty( $post_id ) || $post_id != absint( $post_id ) ) {
569  return false;
570  }
571 
572  if ( function_exists( 'has_post_thumbnail' ) ) {
573  if ( has_post_thumbnail( $post_id ) ) {
574  $thumbnail_id = get_post_thumbnail_id( $post_id );
575  $image = wp_get_attachment_image_src( $thumbnail_id, $size );
576 
577  /* Get src attribute */
578  $src = $image[0];
579 
580  /* Get the attachment alt text. */
581  $alt = trim( strip_tags( get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true ) ) );
582 
583  /* Get the attachment caption. */
584  $caption = get_post_field( 'post_excerpt', $thumbnail_id );
585 
586  $img = new WPDKHTMLTagImg( $src, $alt );
587  if ( !empty( $caption ) ) {
588  $img->addData( 'caption', $caption );
589  }
590  $img->addData( 'thumbnail_id', $thumbnail_id );
591  $img->addData( 'post_id', $post_id );
592  $img->addData( 'size', $size );
593 
594  return $img;
595  }
596  }
597  return false;
598  }
599 
613  public function imageAttachments( $size = 'full', $index = 1 )
614  {
615  return self::imageAttachmentsWithID( $this->ID, $size, $index );
616  }
617 
632  public static function imageAttachmentsWithID( $post_id, $size = 'full', $index = 1 )
633  {
634  /* Check for support */
635  if ( function_exists( 'wp_get_attachment_image' ) ) {
636  $args = array(
637  'post_parent' => $post_id,
638  'post_type' => WPDKPostType::ATTACHMENT,
639  'numberposts' => -1,
640  'post_status' => WPDKPostStatus::INHERIT,
641  'post_mime_type' => 'image',
642  'order' => 'ASC',
643  'orderby' => 'menu_order ASC'
644  );
645  $children = get_children( $args );
646 
647  if ( empty( $children ) || !is_array( $children ) ) {
648  return false;
649  }
650 
651  /* Get the first */
652  $item = current( $children );
653 
654  /* Try to get the $index element */
655  if ( $index > 1 ) {
656  $item = current( array_slice( $children, $index - 1, 1 ) );
657  }
658 
659  if ( is_object( $item ) && isset( $item->ID ) ) {
660  $thumbnail_id = $item->ID;
661 
662  $image = wp_get_attachment_image_src( $thumbnail_id, $size );
663  $src = $image[0];
664 
665  /* Get the attachment alt text. */
666  $alt = trim( strip_tags( get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true ) ) );
667 
668  /* Get the attachment caption. */
669  $caption = get_post_field( 'post_excerpt', $thumbnail_id );
670 
671  $img = new WPDKHTMLTagImg( $src, $alt );
672  if ( !empty( $caption ) ) {
673  $img->addData( 'caption', $caption );
674  }
675  $img->addData( 'thumbnail_id', $thumbnail_id );
676  $img->addData( 'post_id', $post_id );
677  $img->addData( 'size', $size );
678 
679  return $img;
680  }
681  }
682  return false;
683  }
684 
693  public function imageContent()
694  {
695  return self::imageContentWithID( $this->ID );
696  }
697 
708  public static function imageContentWithID( $post_id )
709  {
710  /* Search the post's content for the <img /> tag and get its URL. */
711  preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', get_post_field( 'post_content', $post_id ), $matches );
712 
713  /* If there is a match for the image, return its URL. */
714  if ( isset( $matches ) && is_array( $matches ) && !empty( $matches[1][0] ) ) {
715  $src = $matches[1][0];
716 
717  $img = new WPDKHTMLTagImg( $src, '' );
718  $img->addData( 'post_id', $post_id );
719 
720  return $img;
721  }
722  return false;
723  }
724 
725 }
726 
738  const AUTO_DRAFT = 'auto-draft';
739  const DRAFT = 'draft';
740  const FUTURE = 'future';
741  const INHERIT = 'inherit';
742  const PENDING = 'pending';
746  const PRIVATE_ = 'private';
747  const PUBLISH = 'publish';
748  const TRASH = 'trash';
749 
758  public static function statuses()
759  {
760  $statuses = array(
761  self::AUTO_DRAFT => __( 'A newly created post, with no content', WPDK_TEXTDOMAIN ),
762  self::DRAFT => __( 'The post is draft', WPDK_TEXTDOMAIN ),
763  self::FUTURE => __( 'The post to publish in the future', WPDK_TEXTDOMAIN ),
764  self::INHERIT => __( 'The post is a revision', WPDK_TEXTDOMAIN ),
765  self::PENDING => __( 'The post is pending review', WPDK_TEXTDOMAIN ),
766  self::PRIVATE_ => __( 'Not visible to users who are not logged in', WPDK_TEXTDOMAIN ),
767  self::PUBLISH => __( 'A published post or page', WPDK_TEXTDOMAIN ),
768  self::TRASH => __( 'The post is in trashbin', WPDK_TEXTDOMAIN ),
769  );
770 
771  return apply_filters( 'wpdk-posts-statuses', $statuses );
772  }
773 }
774 
776 /* Backward copatibility */
777 class _WPDKPost extends WPDKPost {}
779 
791  const ATTACHMENT = 'attachment';
792  const NAV_MENU_ITEM = 'nav_menu_item';
793  const PAGE = 'page';
794  const POST = 'post';
795  const REVISION = 'revision';
796 }
797 
798 
812 class WPDKPosts {
813 
821  public function __construct()
822  {
823  }
824 
825 }
826 
827 
840 
848  private $_post;
849 
859  public function __construct( $post )
860  {
861  $this->_post = new WPDKPost( $post );
862  }
863 
873  public static function updatePostMetaWithDeleteIfNotSet( $id_post, $meta_key, $meta_value = null )
874  {
875 
876  /* Sanitize post id. */
877  $id_post = absint( $id_post );
878 
879  if ( !empty( $id_post ) ) {
880  /* Se il parametro meta_value รจ null elimino il post meta. */
881  if ( is_null( $meta_value ) ) {
882  delete_post_meta( $id_post, $meta_key );
883  }
884  else {
885  /* Sanitizo il nome della meta key che potrebbe arrivara come name di un campo input array. */
886  if ( substr( $meta_key, -2 ) == '[]' ) {
887  $meta_key = substr( $meta_key, 0, strlen( $meta_key ) - 2 );
888  }
889  update_post_meta( $id_post, $meta_key, $meta_value );
890  }
891  }
892  }
893 
901  public function value( $key )
902  {
903  if ( !empty( $key ) && !empty( $this->_post ) ) {
904  return get_post_meta( $this->_post->ID, $key, true );
905  }
906  return null;
907  }
908 
909  // -----------------------------------------------------------------------------------------------------------------
910  // Utility
911  // -----------------------------------------------------------------------------------------------------------------
912 
920  public function values( $key )
921  {
922  if ( !empty( $key ) && !empty( $this->_post ) ) {
923  return get_post_meta( $this->_post->ID, $key, false );
924  }
925  return null;
926  }
927 }