| 1 |
<?php |
| 2 |
/** |
| 3 |
* Represents a grid 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 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 { |
| 21 |
|
| 22 |
/** |
| 23 |
* Metadata associated with this item. |
| 24 |
* |
| 25 |
* @since 3.0.0 |
| 26 |
* @access private |
| 27 |
* @var array $meta Item metadata. |
| 28 |
*/ |
| 29 |
private $meta = false; |
| 30 |
|
| 31 |
/** |
| 32 |
* Get new grid item object from meta. |
| 33 |
* |
| 34 |
* @since 3.0.0 |
| 35 |
* @param mixed $meta Meta for this grid item. |
| 36 |
*/ |
| 37 |
public function __construct( $meta ) { |
| 38 |
$this->meta = $meta; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get metadata value. |
| 43 |
* |
| 44 |
* @since 3.0.0 |
| 45 |
* @param mixed $field Metadata field to retrieve. |
| 46 |
* @param mixed $default Default to return if metadata is not set. |
| 47 |
*/ |
| 48 |
public function meta( $field, $default = '' ) { |
| 49 |
if ( isset( $this->meta[ $field ] ) ) { |
| 50 |
return $this->meta[ $field ]; |
| 51 |
} |
| 52 |
|
| 53 |
return $default; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Helper Functions. |
| 58 |
*/ |
| 59 |
public function classes() { |
| 60 |
$classes = array( |
| 61 |
'wpupg-item', |
| 62 |
'wpupg-item-meta', |
| 63 |
); |
| 64 |
|
| 65 |
// Optional classes if set. |
| 66 |
$id = $this->id(); |
| 67 |
if ( $id ) { |
| 68 |
$classes[] = 'wpupg-item-' . $id; |
| 69 |
} |
| 70 |
$post_type = $this->post_type(); |
| 71 |
if ( $post_type ) { |
| 72 |
$classes[] = 'wpupg-type-' . $post_type; |
| 73 |
} |
| 74 |
|
| 75 |
if ( $this->has( 'image' ) ) { |
| 76 |
$classes[] = 'wpupg-item-has-image'; |
| 77 |
} else { |
| 78 |
$classes[] = 'wpupg-item-no-image'; |
| 79 |
} |
| 80 |
|
| 81 |
return $classes; |
| 82 |
} |
| 83 |
public function has( $field ) { |
| 84 |
switch ( $field ) { |
| 85 |
case 'image': |
| 86 |
return '' !== $this->image_url(); // Use URL and not ID because of custom image URL feature. |
| 87 |
default: |
| 88 |
return false !== $this->meta( $field, false ); |
| 89 |
} |
| 90 |
|
| 91 |
return false; |
| 92 |
} |
| 93 |
/** |
| 94 |
* Technical fields. |
| 95 |
*/ |
| 96 |
public function id() { |
| 97 |
return $this->meta( 'id' ); |
| 98 |
} |
| 99 |
/** |
| 100 |
* Item Fields. |
| 101 |
*/ |
| 102 |
public function author_id() { |
| 103 |
return $this->meta( 'author_id' ); |
| 104 |
} |
| 105 |
public function author() { |
| 106 |
$author_id = $this->author_id(); |
| 107 |
|
| 108 |
if ( $author_id ) { |
| 109 |
$author = get_userdata( $author_id ); |
| 110 |
|
| 111 |
if ( $author ) { |
| 112 |
return $author->data->display_name; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return ''; |
| 117 |
} |
| 118 |
public function comment_count() { |
| 119 |
return $this->meta( 'comment_count', 0 ); |
| 120 |
} |
| 121 |
public function content() { |
| 122 |
return $this->meta( 'content' ); |
| 123 |
} |
| 124 |
public function custom_field( $key ) { |
| 125 |
return $this->meta( $key, '' ); |
| 126 |
} |
| 127 |
public function date() { |
| 128 |
return $this->meta( 'date' ); |
| 129 |
} |
| 130 |
public function date_modified() { |
| 131 |
return $this->meta( 'date_modified' ); |
| 132 |
} |
| 133 |
public function excerpt() { |
| 134 |
return $this->meta( 'excerpt' ); |
| 135 |
} |
| 136 |
public function image( $size = 'thumbnail' ) { |
| 137 |
$image_id = $this->image_id(); |
| 138 |
|
| 139 |
if ( $image_id ) { |
| 140 |
return wp_get_attachment_image( $image_id, $size ); |
| 141 |
} else { |
| 142 |
$image_url = $this->image_url(); |
| 143 |
|
| 144 |
if ( $image_url ) { |
| 145 |
return '<img src="' . esc_attr( $image_url ) . '"/>'; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return ''; |
| 150 |
|
| 151 |
} |
| 152 |
public function image_id() { |
| 153 |
$custom_image_id = $this->meta( 'wpupg_custom_image_id', false ); |
| 154 |
|
| 155 |
if ( $custom_image_id ) { |
| 156 |
return $custom_image_id; |
| 157 |
} else { |
| 158 |
return get_post_thumbnail_id( $this->id() ); |
| 159 |
} |
| 160 |
} |
| 161 |
public function image_url( $size = 'thumbnail' ) { |
| 162 |
$custom_image_url = $this->meta( 'wpupg_custom_image', false ); |
| 163 |
|
| 164 |
if ( $custom_image_url ) { |
| 165 |
$custom_image_id = $this->meta( 'wpupg_custom_image_id', false ); |
| 166 |
|
| 167 |
if ( $custom_image_id ) { |
| 168 |
$thumb = wp_get_attachment_image_src( $this->image_id(), $size ); |
| 169 |
} else { |
| 170 |
$thumb = array( $custom_image_url ); |
| 171 |
} |
| 172 |
} else { |
| 173 |
$thumb = wp_get_attachment_image_src( $this->image_id(), $size ); |
| 174 |
} |
| 175 |
|
| 176 |
return $thumb && isset( $thumb[0] ) ? $thumb[0] : ''; |
| 177 |
} |
| 178 |
public function menu_order() { |
| 179 |
return $this->meta( 'menu_order', 0 ); |
| 180 |
} |
| 181 |
public function post_type() { |
| 182 |
return $this->meta( 'post_type' ); |
| 183 |
} |
| 184 |
public function terms( $key ) { |
| 185 |
return $this->meta( $key ); |
| 186 |
} |
| 187 |
public function title() { |
| 188 |
return $this->meta( 'title' ); |
| 189 |
} |
| 190 |
public function url() { |
| 191 |
$custom_url = $this->meta( 'wpupg_custom_link', false ); |
| 192 |
|
| 193 |
if ( $custom_url ) { |
| 194 |
return $custom_url; |
| 195 |
} else { |
| 196 |
return $this->meta( 'url' ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Custom Item Fields. |
| 202 |
*/ |
| 203 |
public function link( $grid ) { |
| 204 |
$custom_link = $this->meta( 'wpupg_custom_link_behaviour', 'default' ); |
| 205 |
|
| 206 |
if ( 'default' === $custom_link ) { |
| 207 |
return $grid->link(); |
| 208 |
} else { |
| 209 |
return 'none' === $custom_link ? false : true; |
| 210 |
} |
| 211 |
} |
| 212 |
public function link_target( $grid ) { |
| 213 |
$custom_link_target = $this->meta( 'wpupg_custom_link_behaviour', 'default' ); |
| 214 |
|
| 215 |
if ( 'default' === $custom_link_target || 'none' === $custom_link_target ) { |
| 216 |
return $grid->link_target(); |
| 217 |
} else { |
| 218 |
return $custom_link_target; |
| 219 |
} |
| 220 |
} |
| 221 |
public function link_nofollow() { |
| 222 |
$custom_url = $this->meta( 'wpupg_custom_link', false ); |
| 223 |
|
| 224 |
if ( $custom_url ) { |
| 225 |
$custom_link_nofollow = $this->meta( 'wpupg_custom_link_nofollow', 'default' ); |
| 226 |
|
| 227 |
if ( 'default' === $custom_link_nofollow ) { |
| 228 |
return WPUPG_Settings::get( 'default_custom_link_nofollow' ); |
| 229 |
} else { |
| 230 |
return $custom_link_nofollow; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// Only nofollow for custom links. |
| 235 |
return 'dofollow'; |
| 236 |
} |
| 237 |
} |
| 238 |
|