/
Helpers.php
100 lines (83 loc) · 3.25 KB
/
Helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace BarnabyWalters\Posse;
use BarnabyWalters\Helpers\Helpers as H;
use BarnabyWalters\Posse\Truncenator;
use DOMDocument;
use DOMXPath;
/**
* Description of Helpers
*
* @author barnabywalters
*/
class Helpers {
/**
* Prepare For Twitter
*
* Takes some parameters and returns an array suitable for conversion to
* a query string and submitting to the twitter API.
*
* $text does not have to be under 140 chars, THE TRUNCENATOR is used
* internally to truncate the content and append the $url within 140 chars.
*
* If $inReplyTo is set and is a twitter.com status URL, the id will be
* parsed out of it and added to the `in_reply_to_status_id` param of the
* return array.
*
* Note that twitter will not process a tweet as a reply correctly if the @name of the
* target is not included.
*
* @param string $text The body text
* @param string $url = null A URL to append to the content, if any
* @param string $inReplyTo = null A URL the content is in reply to
* @return array An assoc. array ready to pass as POST vars to `statuses/update`
*
* @todo Add some intelligent blockquote/other markdown handling
*/
public static function prepareForTwitter($text, $url = null, $inReplyTo = null) {
// Create the tweet array
$tweet = array();
$tweet['status'] = self::convertHtmlToTwitterSyntax($text);
// Run THE TRUNCENATOR using defaults suitable for twitter
ob_start();
$tweet['status'] = Truncenator::truncate(
strip_tags(H::expandImg($tweet['status'])),
$length = 140,
$uri = $url,
$urilen = 20
);
ob_end_clean();
if ($inReplyTo !== null) {
// Check if there’s a twitter status ID in the URL to use
$tweetPattern = '/https?:\/\/twitter.com\/[a-zA-Z_]{1,20}\/status\/([0-9]*)/';
$matches = array();
preg_match($tweetPattern, $inReplyTo, $matches);
$tweet['in_reply_to_status_id'] = $matches[1];
}
return $tweet;
}
public static function convertHtmlToTwitterSyntax($text) {
libxml_use_internal_errors(true);
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadHTML(mb_convert_encoding($text, 'HTML-ENTITIES', 'UTF-8'));
$xpath = new DOMXPath($dom);
$atNamedElements = $xpath->query('//*[@data-at-name]');
foreach ($atNamedElements as $e) {
$atName = $e->getAttribute('data-at-name');
$e->nodeValue = '@' . ltrim($atName, ' @');
}
$emElements = $xpath->query('//em');
foreach ($emElements as $e) {
$e->nodeValue = '*' . $e->nodeValue . '*';
}
$strongElements = $xpath->query('//strong');
foreach ($strongElements as $e) {
$e->nodeValue = '**' . $e->nodeValue . '**';
}
$blockquoteSmallElements = $xpath->query('//blockquote/small');
foreach ($blockquoteSmallElements as $e) {
$e->nodeValue = '— ' . ltrim($e->nodeValue);
}
return $dom->C14N(true, false, array('query' => '//text()'));
}
}
// EOF