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.

WebmentionsPlugin.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. if (!defined('GNUSOCIAL')) {
  3. exit(1);
  4. }
  5. // composer
  6. require 'vendor/autoload.php';
  7. define('WEBMENTION_SERVICE', 4);
  8. class WebmentionsPlugin extends Plugin {
  9. const VERSION = '0.0.1';
  10. /**
  11. * Send webmentions
  12. */
  13. function onEndNoticeSave($notice) {
  14. $profile_id = $notice->profile_id;
  15. $html = $notice->rendered;
  16. $url = $notice->getUrl();
  17. // Notice wasn't posted by a local user or isn't public
  18. // Either way, don't send webmentions for this notice
  19. if ($notice->is_local !== Notice::LOCAL_PUBLIC) {
  20. return true;
  21. }
  22. $client = new IndieWeb\MentionClient($url, $html);
  23. $sent = $client->sendSupportedMentions();
  24. return true;
  25. }
  26. /**
  27. * Add webmention <link> endpoint after <title>
  28. */
  29. function onEndShowHeadTitle($action) {
  30. $endpoint = common_local_url('webmentions');
  31. $action->element("link", array("rel" => "webmention", "href" => $endpoint));
  32. return true;
  33. }
  34. /**
  35. * Webmention handler
  36. */
  37. function onRouterInitialized($m)
  38. {
  39. $m->connect('api/webmentions', array('action' => 'webmentions'));
  40. return true;
  41. }
  42. /**
  43. * Ensure 'Webmention' foreign service exists
  44. */
  45. function onCheckSchema() {
  46. $foreign_service = Foreign_service::getKV('id', WEBMENTION_SERVICE);
  47. if (!($foreign_service instanceof Foreign_service)) {
  48. $foreign_service = new Foreign_service();
  49. $foreign_service->id = WEBMENTION_SERVICE;
  50. $foreign_service->name = 'Webmention';
  51. $foreign_service->description = 'https://github.com/chimo/gs-webmentions';
  52. $foreign_service->created = common_sql_now();
  53. $foreign_service->insert();
  54. }
  55. return true;
  56. }
  57. // TODO: We'll probably need to handle StartNoticeInScope so we're allowed to
  58. // reply, fav, repeat notices we would otherwise not have access to.
  59. // https://github.com/postblue/social/blob/nightly/classes/Notice.php#L2647
  60. /**
  61. * Plugin version data
  62. *
  63. * @param array &$versions array of version data
  64. *
  65. * @return value
  66. */
  67. function onPluginVersion(&$versions)
  68. {
  69. $versions[] = array('name' => 'Webmentions',
  70. 'version' => self::VERSION,
  71. 'author' => 'chimo',
  72. 'homepage' => 'https://github.com/chimo/gs-webmentions',
  73. 'description' =>
  74. // TRANS: Plugin description.
  75. _m('Send/receive webmentions'));
  76. return true;
  77. }
  78. }