You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.7 KiB

  1. <?php
  2. namespace p3k\XRay;
  3. class PostType {
  4. // Takes an XRay format post and runs post-type-discovery, returning a single string
  5. // https://www.w3.org/TR/post-type-discovery/
  6. public static function discover($post) {
  7. // A few of the post types are defined as the same as their microformats h-* types
  8. if(in_array($post['type'], ['event','recipe','review']))
  9. return $post['type'];
  10. if(isset($post['rsvp']))
  11. return 'rsvp';
  12. if(isset($post['repost-of']))
  13. return 'repost';
  14. if(isset($post['like-of']))
  15. return 'like';
  16. if(isset($post['in-reply-to']))
  17. return 'reply';
  18. if(isset($post['bookmark-of']))
  19. return 'bookmark';
  20. if(isset($post['follow-of']))
  21. return 'follow';
  22. if(isset($post['checkin']))
  23. return 'checkin';
  24. if(isset($post['video']))
  25. return 'video';
  26. if(isset($post['audio']))
  27. return 'audio';
  28. if(isset($post['photo']))
  29. return 'photo';
  30. $content = '';
  31. if(isset($post['content']))
  32. $content = $post['content']['text'];
  33. elseif(isset($post['summary']))
  34. $content = $post['summary'];
  35. if(!isset($post['name']) || !trim($post['name']))
  36. return 'note';
  37. // Trim all leading/trailing whitespace
  38. $name = trim($post['name']);
  39. // Collapse all sequences of internal whitespace to a single space (0x20) character each
  40. $name = preg_replace('/\s+/', ' ', $name);
  41. $content = preg_replace('/\s+/', ' ', $content);
  42. // If this processed "name" property value is NOT a prefix of the
  43. // processed "content" property, then it is an article post.
  44. if(strpos($content, $name) === false) {
  45. return 'article';
  46. }
  47. return 'note';
  48. }
  49. }