| 1 |
<?php |
| 2 |
/** |
| 3 |
* Represents a grid post item. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 3.0.0 |
| 7 |
* |
| 8 |
* @package WP_Ultimate_Post_Grid |
| 9 |
* @subpackage WP_Ultimate_Post_Grid/includes/public |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Represents a grid post item. |
| 14 |
* |
| 15 |
* @since 3.0.0 |
| 16 |
* @package WP_Ultimate_Post_Grid |
| 17 |
* @subpackage WP_Ultimate_Post_Grid/includes/public |
| 18 |
* @author Brecht Vandersmissen <brecht@bootstrapped.ventures> |
| 19 |
*/ |
| 20 |
class WPUPG_Item_Post extends WPUPG_Item { |
| 21 |
|
| 22 |
/** |
| 23 |
* WP_Post object associated with this item. |
| 24 |
* |
| 25 |
* @since 3.0.0 |
| 26 |
* @access private |
| 27 |
* @var object $post WP_Post object of this item. |
| 28 |
*/ |
| 29 |
private $post; |
| 30 |
|
| 31 |
/** |
| 32 |
* Metadata associated with this item. |
| 33 |
* |
| 34 |
* @since 3.0.0 |
| 35 |
* @access private |
| 36 |
* @var array $meta Item metadata. |
| 37 |
*/ |
| 38 |
private $meta = false; |
| 39 |
|
| 40 |
/** |
| 41 |
* Get new grid item object from associated post. |
| 42 |
* |
| 43 |
* @since 3.0.0 |
| 44 |
* @param mixed $post_or_post_id WP_Post object or post ID for this grid item. |
| 45 |
*/ |
| 46 |
public function __construct( $post_or_post_id ) { |
| 47 |
$post = is_integer( $post_or_post_id ) ? get_post( $post_or_post_id ) : $post_or_post_id; |
| 48 |
$this->post = $post; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get metadata value. |
| 53 |
* |
| 54 |
* @since 3.0.0 |
| 55 |
* @param mixed $field Metadata field to retrieve. |
| 56 |
* @param mixed $default Default to return if metadata is not set. |
| 57 |
*/ |
| 58 |
public function meta( $field, $default = '' ) { |
| 59 |
if ( ! $this->meta ) { |
| 60 |
$this->meta = get_post_custom( $this->id() ); |
| 61 |
} |
| 62 |
|
| 63 |
if ( isset( $this->meta[ $field ] ) && null !== $this->meta[ $field ][0] ) { |
| 64 |
return $this->meta[ $field ][0]; |
| 65 |
} |
| 66 |
|
| 67 |
return $default; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Helper Functions. |
| 72 |
*/ |
| 73 |
public function classes() { |
| 74 |
$classes = array( |
| 75 |
'wpupg-item', |
| 76 |
'wpupg-item-post', |
| 77 |
'wpupg-item-' . $this->id(), |
| 78 |
'wpupg-type-' . $this->post_type(), |
| 79 |
); |
| 80 |
|
| 81 |
if ( $this->has( 'image' ) ) { |
| 82 |
$classes[] = 'wpupg-item-has-image'; |
| 83 |
} else { |
| 84 |
$classes[] = 'wpupg-item-no-image'; |
| 85 |
} |
| 86 |
|
| 87 |
return $classes; |
| 88 |
} |
| 89 |
public function has( $field ) { |
| 90 |
switch ( $field ) { |
| 91 |
case 'image': |
| 92 |
return '' !== $this->image_url(); // Use URL and not ID because of custom image URL feature. |
| 93 |
default: |
| 94 |
return false !== $this->meta( $field, false ); |
| 95 |
} |
| 96 |
|
| 97 |
return false; |
| 98 |
} |
| 99 |
/** |
| 100 |
* Technical fields. |
| 101 |
*/ |
| 102 |
public function id() { |
| 103 |
return $this->post->ID; |
| 104 |
} |
| 105 |
/** |
| 106 |
* Item Fields. |
| 107 |
*/ |
| 108 |
public function author_id() { |
| 109 |
return $this->post->post_author; |
| 110 |
} |
| 111 |
public function author() { |
| 112 |
$author_id = $this->author_id(); |
| 113 |
|
| 114 |
if ( $author_id ) { |
| 115 |
$author = get_userdata( $author_id ); |
| 116 |
|
| 117 |
if ( $author ) { |
| 118 |
return $author->data->display_name; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return ''; |
| 123 |
} |
| 124 |
public function comment_count() { |
| 125 |
return $this->post->comment_count; |
| 126 |
} |
| 127 |
public function content() { |
| 128 |
return $this->post->post_content; |
| 129 |
} |
| 130 |
public function custom_field( $key ) { |
| 131 |
return $this->meta( $key, '' ); |
| 132 |
} |
| 133 |
public function date() { |
| 134 |
return $this->post->post_date; |
| 135 |
} |
| 136 |
public function date_modified() { |
| 137 |
return get_the_modified_date( 'Y-m-d H:i:s', $this->post ); |
| 138 |
} |
| 139 |
public function excerpt() { |
| 140 |
return $this->post->post_excerpt; |
| 141 |
} |
| 142 |
public function image( $size = 'thumbnail' ) { |
| 143 |
$image_id = $this->image_id(); |
| 144 |
|
| 145 |
if ( $image_id ) { |
| 146 |
return wp_get_attachment_image( $image_id, $size ); |
| 147 |
} else { |
| 148 |
$image_url = $this->image_url(); |
| 149 |
|
| 150 |
if ( $image_url ) { |
| 151 |
return '<img src="' . esc_attr( $image_url ) . '"/>'; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return ''; |
| 156 |
|
| 157 |
} |
| 158 |
public function image_id() { |
| 159 |
$custom_image_id = $this->meta( 'wpupg_custom_image_id', false ); |
| 160 |
|
| 161 |
if ( $custom_image_id ) { |
| 162 |
return $custom_image_id; |
| 163 |
} else { |
| 164 |
if ( 'attachment' === $this->post_type() ) { |
| 165 |
return $this->id(); |
| 166 |
} else { |
| 167 |
return get_post_thumbnail_id( $this->id() ); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
public function image_url( $size = 'thumbnail' ) { |
| 172 |
$custom_image_url = $this->meta( 'wpupg_custom_image', false ); |
| 173 |
|
| 174 |
if ( $custom_image_url ) { |
| 175 |
$custom_image_id = $this->meta( 'wpupg_custom_image_id', false ); |
| 176 |
|
| 177 |
if ( $custom_image_id ) { |
| 178 |
$thumb = wp_get_attachment_image_src( $this->image_id(), $size ); |
| 179 |
} else { |
| 180 |
$thumb = array( $custom_image_url ); |
| 181 |
} |
| 182 |
} else { |
| 183 |
$thumb = wp_get_attachment_image_src( $this->image_id(), $size ); |
| 184 |
} |
| 185 |
|
| 186 |
return $thumb && isset( $thumb[0] ) ? $thumb[0] : ''; |
| 187 |
} |
| 188 |
public function menu_order() { |
| 189 |
return $this->post->menu_order; |
| 190 |
} |
| 191 |
public function post() { |
| 192 |
return $this->post; |
| 193 |
} |
| 194 |
public function post_type() { |
| 195 |
return $this->post->post_type; |
| 196 |
} |
| 197 |
public function terms( $key, $args = array() ) { |
| 198 |
return wp_get_post_terms( $this->post->ID, $key, $args ); |
| 199 |
} |
| 200 |
public function title() { |
| 201 |
return $this->post->post_title; |
| 202 |
} |
| 203 |
public function url() { |
| 204 |
$custom_url = $this->meta( 'wpupg_custom_link', false ); |
| 205 |
|
| 206 |
if ( $custom_url ) { |
| 207 |
return $custom_url; |
| 208 |
} else { |
| 209 |
return get_permalink( $this->id() ); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|