| 1 |
<?php |
| 2 |
|
| 3 |
class LSD_Meta extends LSD_Base |
| 4 |
{ |
| 5 |
const URL = 'url'; |
| 6 |
const TEXT = 'text'; |
| 7 |
const NUMBER = 'number'; |
| 8 |
const IMAGE = 'image'; |
| 9 |
const GALLERY = 'gallery'; |
| 10 |
const DATETIME = 'datetime'; |
| 11 |
const EMAIL = 'email'; |
| 12 |
const TEL = 'tel'; |
| 13 |
|
| 14 |
public static function all(): array |
| 15 |
{ |
| 16 |
$callback = function ($key, $id) |
| 17 |
{ |
| 18 |
return get_post_meta($id, $key, true); |
| 19 |
}; |
| 20 |
|
| 21 |
$date_format = get_option('date_format'); |
| 22 |
|
| 23 |
$metas = [ |
| 24 |
'post_url' => [ |
| 25 |
'key' => 'post_url', |
| 26 |
'type' => LSD_Meta::URL, |
| 27 |
'name' => esc_html__('Listing URL', 'listdom'), |
| 28 |
'get' => function ($key, $id) |
| 29 |
{ |
| 30 |
return get_post_permalink($id); |
| 31 |
}, |
| 32 |
], |
| 33 |
'author_url' => [ |
| 34 |
'key' => 'author_url', |
| 35 |
'type' => LSD_Meta::URL, |
| 36 |
'name' => esc_html__('Author URL', 'listdom'), |
| 37 |
'get' => function ($key, $id) |
| 38 |
{ |
| 39 |
// Post |
| 40 |
$post = get_post($id); |
| 41 |
|
| 42 |
return $post && isset($post->post_author) |
| 43 |
? LSD_User::profile_link($post->post_author) |
| 44 |
: ''; |
| 45 |
}, |
| 46 |
], |
| 47 |
'post_title' => [ |
| 48 |
'key' => 'post_title', |
| 49 |
'type' => LSD_Meta::TEXT, |
| 50 |
'name' => esc_html__('Listing Title', 'listdom'), |
| 51 |
'get' => function ($key, $id) |
| 52 |
{ |
| 53 |
return get_the_title($id); |
| 54 |
}, |
| 55 |
], |
| 56 |
'post_featured_image' => [ |
| 57 |
'key' => 'post_featured_image', |
| 58 |
'type' => LSD_Meta::IMAGE, |
| 59 |
'name' => esc_html__('Listing Image', 'listdom'), |
| 60 |
'get' => function ($key, $id) |
| 61 |
{ |
| 62 |
return get_post_thumbnail_id($id); |
| 63 |
}, |
| 64 |
], |
| 65 |
'author_avatar' => [ |
| 66 |
'key' => 'author_avatar', |
| 67 |
'type' => LSD_Meta::URL, |
| 68 |
'name' => esc_html__('Author Avatar', 'listdom'), |
| 69 |
'get' => function ($key, $id) |
| 70 |
{ |
| 71 |
// Post |
| 72 |
$post = get_post($id); |
| 73 |
|
| 74 |
return $post && isset($post->post_author) |
| 75 |
? get_avatar($post->post_author, 48) |
| 76 |
: ''; |
| 77 |
}, |
| 78 |
], |
| 79 |
'post_excerpt' => [ |
| 80 |
'key' => 'post_excerpt', |
| 81 |
'type' => LSD_Meta::TEXT, |
| 82 |
'name' => esc_html__('Listing Excerpt', 'listdom'), |
| 83 |
'get' => function ($key, $id) |
| 84 |
{ |
| 85 |
return get_the_excerpt($id); |
| 86 |
}, |
| 87 |
], |
| 88 |
'post_date' => [ |
| 89 |
'key' => 'post_date', |
| 90 |
'type' => LSD_Meta::DATETIME, |
| 91 |
'name' => esc_html__('Listing Date', 'listdom'), |
| 92 |
'get' => function ($key, $id) use ($date_format) |
| 93 |
{ |
| 94 |
return get_the_date($date_format, $id); |
| 95 |
}, |
| 96 |
], |
| 97 |
'post_modified' => [ |
| 98 |
'key' => 'post_modified', |
| 99 |
'type' => LSD_Meta::DATETIME, |
| 100 |
'name' => esc_html__('Listing Modification Date', 'listdom'), |
| 101 |
'get' => function ($key, $id) use ($date_format) |
| 102 |
{ |
| 103 |
return get_the_modified_date($date_format, $id); |
| 104 |
}, |
| 105 |
], |
| 106 |
'post_comments' => [ |
| 107 |
'key' => 'post_comments', |
| 108 |
'type' => LSD_Meta::NUMBER, |
| 109 |
'name' => esc_html__('Listing Comments Count', 'listdom'), |
| 110 |
'get' => function ($key, $id) |
| 111 |
{ |
| 112 |
// Post |
| 113 |
$post = get_post($id); |
| 114 |
|
| 115 |
return $post && isset($post->comment_count) ? $post->comment_count : 0; |
| 116 |
}, |
| 117 |
], |
| 118 |
'author_display_name' => [ |
| 119 |
'key' => 'author_display_name', |
| 120 |
'type' => LSD_Meta::TEXT, |
| 121 |
'name' => esc_html__('Author Display Name', 'listdom'), |
| 122 |
'get' => function ($key, $id) |
| 123 |
{ |
| 124 |
// Post |
| 125 |
$post = get_post($id); |
| 126 |
|
| 127 |
return $post && isset($post->post_author) |
| 128 |
? get_the_author_meta('display_name', $post->post_author) |
| 129 |
: ''; |
| 130 |
}, |
| 131 |
], |
| 132 |
'author_first_name' => [ |
| 133 |
'key' => 'author_first_name', |
| 134 |
'type' => LSD_Meta::TEXT, |
| 135 |
'name' => esc_html__('Author First Name', 'listdom'), |
| 136 |
'get' => function ($key, $id) |
| 137 |
{ |
| 138 |
// Post |
| 139 |
$post = get_post($id); |
| 140 |
|
| 141 |
return $post && isset($post->post_author) |
| 142 |
? get_the_author_meta('first_name', $post->post_author) |
| 143 |
: ''; |
| 144 |
}, |
| 145 |
], |
| 146 |
'author_last_name' => [ |
| 147 |
'key' => 'author_last_name', |
| 148 |
'type' => LSD_Meta::TEXT, |
| 149 |
'name' => esc_html__('Author Last Name', 'listdom'), |
| 150 |
'get' => function ($key, $id) |
| 151 |
{ |
| 152 |
// Post |
| 153 |
$post = get_post($id); |
| 154 |
|
| 155 |
return $post && isset($post->post_author) |
| 156 |
? get_the_author_meta('last_name', $post->post_author) |
| 157 |
: ''; |
| 158 |
}, |
| 159 |
], |
| 160 |
'author_email' => [ |
| 161 |
'key' => 'author_email', |
| 162 |
'type' => LSD_Meta::EMAIL, |
| 163 |
'name' => esc_html__('Author Email', 'listdom'), |
| 164 |
'get' => function ($key, $id) |
| 165 |
{ |
| 166 |
// Post |
| 167 |
$post = get_post($id); |
| 168 |
|
| 169 |
return $post && isset($post->post_author) |
| 170 |
? get_the_author_meta('user_email', $post->post_author) |
| 171 |
: ''; |
| 172 |
}, |
| 173 |
], |
| 174 |
'author_job_title' => [ |
| 175 |
'key' => 'author_job_title', |
| 176 |
'type' => LSD_Meta::TEXT, |
| 177 |
'name' => esc_html__('Author Job Title', 'listdom'), |
| 178 |
'get' => function ($key, $id) |
| 179 |
{ |
| 180 |
// Post |
| 181 |
$post = get_post($id); |
| 182 |
|
| 183 |
return $post && isset($post->post_author) |
| 184 |
? get_user_meta($post->post_author, 'lsd_job_title', true) |
| 185 |
: ''; |
| 186 |
}, |
| 187 |
], |
| 188 |
'author_bio' => [ |
| 189 |
'key' => 'author_bio', |
| 190 |
'type' => LSD_Meta::TEXT, |
| 191 |
'name' => esc_html__('Author Bio', 'listdom'), |
| 192 |
'get' => function ($key, $id) |
| 193 |
{ |
| 194 |
// Post |
| 195 |
$post = get_post($id); |
| 196 |
|
| 197 |
return $post && isset($post->post_author) |
| 198 |
? get_user_meta($post->post_author, 'description', true) |
| 199 |
: ''; |
| 200 |
}, |
| 201 |
], |
| 202 |
'author_phone' => [ |
| 203 |
'key' => 'author_phone', |
| 204 |
'type' => LSD_Meta::TEL, |
| 205 |
'name' => esc_html__('Author Phone', 'listdom'), |
| 206 |
'get' => function ($key, $id) |
| 207 |
{ |
| 208 |
// Post |
| 209 |
$post = get_post($id); |
| 210 |
|
| 211 |
return $post && isset($post->post_author) |
| 212 |
? get_user_meta($post->post_author, 'lsd_phone', true) |
| 213 |
: ''; |
| 214 |
}, |
| 215 |
], |
| 216 |
'author_mobile' => [ |
| 217 |
'key' => 'author_mobile', |
| 218 |
'type' => LSD_Meta::TEL, |
| 219 |
'name' => esc_html__('Author Mobile', 'listdom'), |
| 220 |
'get' => function ($key, $id) |
| 221 |
{ |
| 222 |
// Post |
| 223 |
$post = get_post($id); |
| 224 |
|
| 225 |
return $post && isset($post->post_author) |
| 226 |
? get_user_meta($post->post_author, 'lsd_mobile', true) |
| 227 |
: ''; |
| 228 |
}, |
| 229 |
], |
| 230 |
'author_website' => [ |
| 231 |
'key' => 'author_website', |
| 232 |
'type' => LSD_Meta::URL, |
| 233 |
'name' => esc_html__('Author Website', 'listdom'), |
| 234 |
'get' => function ($key, $id) |
| 235 |
{ |
| 236 |
// Post |
| 237 |
$post = get_post($id); |
| 238 |
|
| 239 |
return $post && isset($post->post_author) |
| 240 |
? get_user_meta($post->post_author, 'lsd_website', true) |
| 241 |
: ''; |
| 242 |
}, |
| 243 |
], |
| 244 |
'author_fax' => [ |
| 245 |
'key' => 'author_fax', |
| 246 |
'type' => LSD_Meta::TEL, |
| 247 |
'name' => esc_html__('Author Fax', 'listdom'), |
| 248 |
'get' => function ($key, $id) |
| 249 |
{ |
| 250 |
// Post |
| 251 |
$post = get_post($id); |
| 252 |
|
| 253 |
return $post && isset($post->post_author) |
| 254 |
? get_user_meta($post->post_author, 'lsd_fax', true) |
| 255 |
: ''; |
| 256 |
}, |
| 257 |
], |
| 258 |
'lsd_address' => [ |
| 259 |
'key' => 'lsd_address', |
| 260 |
'type' => LSD_Meta::TEXT, |
| 261 |
'name' => esc_html__('Address', 'listdom'), |
| 262 |
'get' => $callback, |
| 263 |
], |
| 264 |
'lsd_latitude' => [ |
| 265 |
'key' => 'lsd_latitude', |
| 266 |
'type' => LSD_Meta::NUMBER, |
| 267 |
'name' => esc_html__('Latitude', 'listdom'), |
| 268 |
'get' => $callback, |
| 269 |
], |
| 270 |
'lsd_longitude' => [ |
| 271 |
'key' => 'lsd_longitude', |
| 272 |
'type' => LSD_Meta::NUMBER, |
| 273 |
'name' => esc_html__('Longitude', 'listdom'), |
| 274 |
'get' => $callback, |
| 275 |
], |
| 276 |
'lsd_price' => [ |
| 277 |
'key' => 'lsd_price', |
| 278 |
'type' => LSD_Meta::NUMBER, |
| 279 |
'name' => esc_html__('Price', 'listdom'), |
| 280 |
'get' => $callback, |
| 281 |
], |
| 282 |
'lsd_price_max' => [ |
| 283 |
'key' => 'lsd_price_max', |
| 284 |
'type' => LSD_Meta::NUMBER, |
| 285 |
'name' => esc_html__('Price (Max)', 'listdom'), |
| 286 |
'get' => $callback, |
| 287 |
], |
| 288 |
'lsd_price_after' => [ |
| 289 |
'key' => 'lsd_price_after', |
| 290 |
'type' => LSD_Meta::TEXT, |
| 291 |
'name' => esc_html__('Price Description', 'listdom'), |
| 292 |
'get' => $callback, |
| 293 |
], |
| 294 |
'lsd_price_class' => [ |
| 295 |
'key' => 'lsd_price_class', |
| 296 |
'type' => LSD_Meta::TEXT, |
| 297 |
'name' => esc_html__('Price Class', 'listdom'), |
| 298 |
'get' => function ($key, $id) |
| 299 |
{ |
| 300 |
$class = (int) get_post_meta($id, $key, true); |
| 301 |
if (!trim($class)) $class = 2; |
| 302 |
|
| 303 |
return str_repeat('$', $class); |
| 304 |
}, |
| 305 |
], |
| 306 |
'lsd_currency' => [ |
| 307 |
'key' => 'lsd_currency', |
| 308 |
'type' => LSD_Meta::TEXT, |
| 309 |
'name' => esc_html__('Currency', 'listdom'), |
| 310 |
'get' => $callback, |
| 311 |
], |
| 312 |
'lsd_primary_category' => [ |
| 313 |
'key' => 'lsd_primary_category', |
| 314 |
'type' => LSD_Meta::TEXT, |
| 315 |
'name' => esc_html__('Primary Category', 'listdom'), |
| 316 |
'get' => function ($key, $id) |
| 317 |
{ |
| 318 |
$category = LSD_Entity_Listing::get_primary_category($id); |
| 319 |
|
| 320 |
return $category && isset($category->name) ? $category->name : ''; |
| 321 |
}, |
| 322 |
], |
| 323 |
'lsd_email' => [ |
| 324 |
'key' => 'lsd_email', |
| 325 |
'type' => LSD_Meta::EMAIL, |
| 326 |
'name' => esc_html__('Email', 'listdom'), |
| 327 |
'get' => $callback, |
| 328 |
], |
| 329 |
'lsd_phone' => [ |
| 330 |
'key' => 'lsd_phone', |
| 331 |
'type' => LSD_Meta::TEL, |
| 332 |
'name' => esc_html__('Phone', 'listdom'), |
| 333 |
'get' => $callback, |
| 334 |
], |
| 335 |
'lsd_website' => [ |
| 336 |
'key' => 'lsd_website', |
| 337 |
'type' => LSD_Meta::URL, |
| 338 |
'name' => esc_html__('Website', 'listdom'), |
| 339 |
'get' => $callback, |
| 340 |
], |
| 341 |
'lsd_contact_address' => [ |
| 342 |
'key' => 'lsd_contact_address', |
| 343 |
'type' => LSD_Meta::TEXT, |
| 344 |
'name' => esc_html__('Contact Address', 'listdom'), |
| 345 |
'get' => $callback, |
| 346 |
], |
| 347 |
'lsd_remark' => [ |
| 348 |
'key' => 'lsd_remark', |
| 349 |
'type' => LSD_Meta::TEXT, |
| 350 |
'name' => esc_html__('Remark (Owner Message)', 'listdom'), |
| 351 |
'get' => $callback, |
| 352 |
], |
| 353 |
'lsd_link' => [ |
| 354 |
'key' => 'lsd_link', |
| 355 |
'type' => LSD_Meta::URL, |
| 356 |
'name' => esc_html__('Listing Custom Link', 'listdom'), |
| 357 |
'get' => $callback, |
| 358 |
], |
| 359 |
'lsd_gallery' => [ |
| 360 |
'key' => 'lsd_gallery', |
| 361 |
'type' => LSD_Meta::GALLERY, |
| 362 |
'name' => esc_html__('Gallery', 'listdom'), |
| 363 |
'get' => function ($key, $id) |
| 364 |
{ |
| 365 |
$gallery = get_post_meta($id, $key, true); |
| 366 |
if (!is_array($gallery)) $gallery = []; |
| 367 |
|
| 368 |
return $gallery; |
| 369 |
}, |
| 370 |
], |
| 371 |
'lsd_visible_until' => [ |
| 372 |
'key' => 'lsd_visible_until', |
| 373 |
'type' => LSD_Meta::DATETIME, |
| 374 |
'name' => esc_html__('Visible Until', 'listdom'), |
| 375 |
'get' => function ($key, $id) use ($date_format) |
| 376 |
{ |
| 377 |
$timestamp = (int) get_post_meta($id, $key, true); |
| 378 |
return $timestamp ? wp_date($date_format, $timestamp) : ''; |
| 379 |
}, |
| 380 |
], |
| 381 |
]; |
| 382 |
|
| 383 |
// Socials |
| 384 |
$sc = new LSD_Socials(); |
| 385 |
|
| 386 |
$networks = LSD_Options::socials(); |
| 387 |
foreach ($networks as $network => $values) |
| 388 |
{ |
| 389 |
$obj = $sc->get($network, $values); |
| 390 |
|
| 391 |
// Social Network for Listing |
| 392 |
if ($obj && $obj->option('listing')) |
| 393 |
{ |
| 394 |
$key = 'lsd_' . $obj->key(); |
| 395 |
$metas[$key] = [ |
| 396 |
'key' => $key, |
| 397 |
'type' => LSD_Meta::URL, |
| 398 |
'name' => $obj->label(), |
| 399 |
'get' => $callback, |
| 400 |
]; |
| 401 |
} |
| 402 |
|
| 403 |
// Social Network for Profile |
| 404 |
if ($obj && $obj->option('profile')) |
| 405 |
{ |
| 406 |
$key = 'author_' . $obj->key(); |
| 407 |
$metas[$key] = [ |
| 408 |
'key' => $key, |
| 409 |
'type' => LSD_Meta::URL, |
| 410 |
'name' => sprintf( |
| 411 |
/* translators: %s: Social network label. */ |
| 412 |
esc_html__('Author %s', 'listdom'), |
| 413 |
$obj->label() |
| 414 |
), |
| 415 |
'get' => function ($key, $id) use ($obj) |
| 416 |
{ |
| 417 |
// Post |
| 418 |
$post = get_post($id); |
| 419 |
|
| 420 |
return $post && isset($post->post_author) |
| 421 |
? get_user_meta($post->post_author, 'lsd_' . $obj->key(), true) |
| 422 |
: ''; |
| 423 |
}, |
| 424 |
]; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
// Attributes |
| 429 |
$attributes = LSD_Main::get_attributes(); |
| 430 |
|
| 431 |
foreach ($attributes as $attribute) |
| 432 |
{ |
| 433 |
$type = get_term_meta($attribute->term_id, 'lsd_field_type', true); |
| 434 |
if ($type === 'separator') continue; |
| 435 |
|
| 436 |
$attribute_id = (int) $attribute->term_id; |
| 437 |
$attribute_slug = isset($attribute->slug) ? sanitize_title($attribute->slug) : ''; |
| 438 |
if ($attribute_id <= 0 || $attribute_slug === '') continue; |
| 439 |
|
| 440 |
$key = 'lsd_attribute_' . $attribute_slug; |
| 441 |
$metas[$key] = [ |
| 442 |
'key' => $key, |
| 443 |
'type' => in_array($type, [LSD_Meta::NUMBER, LSD_Meta::EMAIL, LSD_Meta::URL, LSD_Meta::TEL, LSD_Meta::IMAGE]) |
| 444 |
? $type |
| 445 |
: LSD_Meta::TEXT, |
| 446 |
'name' => $attribute->name, |
| 447 |
'attribute_id' => $attribute_id, |
| 448 |
'attribute_slug' => $attribute_slug, |
| 449 |
'attribute_type' => $type, |
| 450 |
'get' => function ($key, $id) use ($attribute_id) |
| 451 |
{ |
| 452 |
$attribute = new LSD_Entity_Attribute($attribute_id); |
| 453 |
return $attribute->value($id); |
| 454 |
}, |
| 455 |
]; |
| 456 |
} |
| 457 |
|
| 458 |
return apply_filters('lsd_listing_meta', $metas, $callback); |
| 459 |
} |
| 460 |
|
| 461 |
public static function get($key): array |
| 462 |
{ |
| 463 |
// All Meta Fields |
| 464 |
$all = LSD_Meta::all(); |
| 465 |
|
| 466 |
// Normalize Legacy Attribute Keys |
| 467 |
$key = self::normalize_attribute_key((string) $key); |
| 468 |
|
| 469 |
// Return Meta |
| 470 |
return isset($all[$key]) && is_array($all[$key]) ? $all[$key] : []; |
| 471 |
} |
| 472 |
|
| 473 |
public static function type(string $type): array |
| 474 |
{ |
| 475 |
$metas = []; |
| 476 |
foreach (LSD_Meta::all() as $key => $meta) |
| 477 |
{ |
| 478 |
if ($meta['type'] !== $type) continue; |
| 479 |
|
| 480 |
$metas[$key] = $meta; |
| 481 |
} |
| 482 |
|
| 483 |
return $metas; |
| 484 |
} |
| 485 |
|
| 486 |
public static function value(string $key, int $post_id) |
| 487 |
{ |
| 488 |
$key = self::normalize_attribute_key($key); |
| 489 |
|
| 490 |
// Meta Field |
| 491 |
$meta = LSD_Meta::get($key); |
| 492 |
|
| 493 |
// No Callback! |
| 494 |
if (!isset($meta['get']) || !is_callable($meta['get'])) return ''; |
| 495 |
|
| 496 |
// Get the content |
| 497 |
return call_user_func($meta['get'], $key, $post_id); |
| 498 |
} |
| 499 |
|
| 500 |
public static function pairs(array $types = []): array |
| 501 |
{ |
| 502 |
$metas = []; |
| 503 |
foreach (LSD_Meta::all() as $key => $meta) |
| 504 |
{ |
| 505 |
if ($types && !in_array($meta['type'], $types)) continue; |
| 506 |
$metas[$key] = $meta['name']; |
| 507 |
} |
| 508 |
|
| 509 |
return $metas; |
| 510 |
} |
| 511 |
|
| 512 |
private static function normalize_attribute_key(string $key): string |
| 513 |
{ |
| 514 |
if (!preg_match('/^lsd_attribute_(\d+)$/', $key, $matches)) return $key; |
| 515 |
|
| 516 |
$slug = LSD_Main::get_attr_slug((int) $matches[1]); |
| 517 |
return $slug !== '' ? 'lsd_attribute_' . $slug : $key; |
| 518 |
} |
| 519 |
} |
| 520 |
|