274 if ( !is_null( $record ) && is_numeric( $record ) ) {
275 $this->initPostByID( absint( $record ) );
279 elseif ( !is_null( $record ) && is_object( $record ) && isset( $record->ID ) ) {
280 $this->initPostByPost( $record );
284 elseif ( !is_null( $record ) && is_string( $record ) ) {
286 $object = get_page_by_path( $record, OBJECT,
$post_type );
287 $this->initPostByPost( $object );
291 elseif ( is_null( $record ) ) {
293 $defaults = $this->postEmpty();
294 $this->initPostByArgs( $defaults );
309 private function initPostByID( $id_post )
311 if ( isset( $GLOBALS[__CLASS__][$id_post] ) ) {
312 $post = $GLOBALS[__CLASS__][$id_post];
315 $GLOBALS[__CLASS__][$id_post] = $post = get_post( $id_post );
317 $this->initPostByPost( $post );
327 private function initPostByPost( $post )
329 if ( is_object( $post ) ) {
331 foreach ( $post as $property => $value ) {
332 $this->$property = $value;
345 private function postEmpty()
350 'post_date' =>
'0000-00-00 00:00:00',
351 'post_date_gmt' =>
'0000-00-00 00:00:00',
352 'post_content' =>
'',
354 'post_excerpt' =>
'',
356 'comment_status' =>
'open',
357 'ping_status' =>
'open',
358 'post_password' =>
'',
362 'post_modified' =>
'0000-00-00 00:00:00',
363 'post_modified_gmt' =>
'0000-00-00 00:00:00',
364 'post_content_filtered' =>
'',
369 'post_mime_type' =>
'',
384 private function initPostByArgs( $args )
386 foreach ( $args as $property => $value ) {
387 $this->$property = $value;
405 public function delete()
407 return wp_delete_post( $this->ID,
true );
423 return wp_trash_post( $this->ID );
438 return wp_untrash_post( $this->ID );
458 if (
'post.php' != $pagenow ) {
459 return wp_update_post( $this,
true );
471 self::updateMetaWithID( $this->ID, $args );
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 );
505 if ( empty( $this->ID ) ) {
508 if ( func_num_args() > 1 ) {
509 $value = func_get_arg( 1 );
510 return update_post_meta( $this->ID, $meta_key, $value );
512 return get_post_meta( $this->ID, $meta_key,
true );
528 if ( empty( $this->ID ) ) {
531 if ( func_num_args() > 1 ) {
532 $value = func_get_arg( 1 );
533 return update_post_meta( $this->ID, $meta_key, $value );
535 return get_post_meta( $this->ID, $meta_key );
551 return self::thumbnailWithID( $this->ID, $size );
568 if ( empty( $post_id ) || $post_id != absint( $post_id ) ) {
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 );
581 $alt = trim( strip_tags( get_post_meta( $thumbnail_id,
'_wp_attachment_image_alt',
true ) ) );
584 $caption = get_post_field(
'post_excerpt', $thumbnail_id );
587 if ( !empty( $caption ) ) {
588 $img->addData(
'caption', $caption );
590 $img->addData(
'thumbnail_id', $thumbnail_id );
591 $img->addData(
'post_id', $post_id );
592 $img->addData(
'size', $size );
615 return self::imageAttachmentsWithID( $this->ID, $size, $index );
635 if ( function_exists(
'wp_get_attachment_image' ) ) {
637 'post_parent' => $post_id,
641 'post_mime_type' =>
'image',
643 'orderby' =>
'menu_order ASC'
645 $children = get_children( $args );
647 if ( empty( $children ) || !is_array( $children ) ) {
652 $item = current( $children );
656 $item = current( array_slice( $children, $index - 1, 1 ) );
659 if ( is_object( $item ) && isset( $item->ID ) ) {
660 $thumbnail_id = $item->ID;
662 $image = wp_get_attachment_image_src( $thumbnail_id, $size );
666 $alt = trim( strip_tags( get_post_meta( $thumbnail_id,
'_wp_attachment_image_alt',
true ) ) );
669 $caption = get_post_field(
'post_excerpt', $thumbnail_id );
672 if ( !empty( $caption ) ) {
673 $img->addData(
'caption', $caption );
675 $img->addData(
'thumbnail_id', $thumbnail_id );
676 $img->addData(
'post_id', $post_id );
677 $img->addData(
'size', $size );
695 return self::imageContentWithID( $this->ID );
711 preg_match_all(
'|<img.*?src=[\'"](.*?)[\'"].*?>|i', get_post_field(
'post_content', $post_id ), $matches );
714 if ( isset( $matches ) && is_array( $matches ) && !empty( $matches[1][0] ) ) {
715 $src = $matches[1][0];
718 $img->addData(
'post_id', $post_id );
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 ),
771 return apply_filters(
'wpdk-posts-statuses', $statuses );
861 $this->_post =
new WPDKPost( $post );
877 $id_post = absint( $id_post );
879 if ( !empty( $id_post ) ) {
881 if ( is_null( $meta_value ) ) {
882 delete_post_meta( $id_post, $meta_key );
886 if ( substr( $meta_key, -2 ) ==
'[]' ) {
887 $meta_key = substr( $meta_key, 0, strlen( $meta_key ) - 2 );
889 update_post_meta( $id_post, $meta_key, $meta_value );
903 if ( !empty( $key ) && !empty( $this->_post ) ) {
904 return get_post_meta( $this->_post->ID, $key,
true );
922 if ( !empty( $key ) && !empty( $this->_post ) ) {
923 return get_post_meta( $this->_post->ID, $key,
false );