-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet.php
150 lines (122 loc) · 4.77 KB
/
tweet.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* @package Tweet
* @type Plugin (Articleoftheday)
* @version 1.0.0
* @author Simon Champion
* @copyright (C) 2018 Simon Champion
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
**/
use Abraham\TwitterOAuth\TwitterOAuth;
require "twitteroauth/autoload.php";
defined('_JEXEC') or die;
class plgArticleofthedayTweet extends JPlugin
{
const TWEET_LENGTH = 280;
public function onNewArticleOfTheDay($articleID, $fieldName)
{
$connection = $this->connect();
$statues = $connection->post("statuses/update", ["status" => $this->generateTweet($articleID)]);
}
private function connect()
{
$consumer_key = $this->params->get('consumer_key', '');
$consumer_secret = $this->params->get('consumer_secret', '');
$access_token = $this->params->get('access_token', '');
$access_secret = $this->params->get('access_secret', '');
if (!$consumer_key || !$consumer_secret || !$access_token || !$access_secret) {
throw new Exception('Invalid AOTD/TW config.');
}
return new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_secret);
}
private function generateTweet($articleID)
{
$template = $this->params->get('tweet_template', "{content}\n{tags}\n{url}");
$content = $this->cleanContent($this->loadContent($articleID));
$hashtags = $this->hashtags();
$url = $this->getURL($articleID);
$output = str_replace(['{tags}', '{url}'], [$hashtags, $url], $template);
$remainder = self::TWEET_LENGTH - strlen($output);
if (strlen($content) > $remainder) {
$content = substr($content, 0, $remainder - 1) . '…';
}
return str_replace('{content}', $content, $output);
}
private function hashtags()
{
$hashtags = explode(' ',$this->params->get('hashtag_roulette', ''));
$hashtagMax = (int)$this->params->get('hashtag_max', 1);
if (!count($hashtags) || $hashtagMax < 1) {
return '';
}
shuffle($hashtags);
return implode(' ', array_slice($hashtags, 0, mt_rand(1, $hashtagMax)));
}
private function getURL($articleID)
{
$article = $this->loadArticle($articleID);
$root = parse_url(JURI::root());
$domain = $root['scheme'].'://'.$root['host'];
return $domain.JRoute::_(ContentHelperRoute::getArticleRoute($articleID, $article->catid));
}
private function cleanContent($content)
{
$content = preg_replace('~\s+~', " ", $content);
$content = preg_replace('~<br ?/?>+~', "\n", $content);
$output = strip_tags($content);
return $output;
}
private function loadContent($articleID)
{
switch ($this->params->get('content_from', 'none')) {
case 'title' : return $this->contentFromTitle($articleID);
case 'body' : return $this->contentFromBody($articleID);
case 'field' : return $this->contentFromField($articleID);
default : return '';
}
}
private function contentFromTitle($articleID)
{
$article = $this->loadArticle($articleID);
return $article->title;
}
private function contentFromBody($articleID)
{
$article = $this->loadArticle($articleID);
return $article->introtext;
}
private function contentFromField($articleID)
{
$contentField = $this->params->get('content_field', '');
$field = $this->loadFieldForArticle($articleID, $contentField);
return $field->value;
}
private function loadArticle($articleID) {
static $obj = null;
if ($obj && $obj->id == $articleID) {
return $obj;
}
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(['a.id','a.catid','a.title','a.alias','a.introtext','a.fulltext']);
$query->from($db->quoteName('#__content').' as a');
$query->where('a.id = '.$db->quote($articleID));
$db->setQuery($query);
$obj = $db->loadObject();
return $obj;
}
private function loadFieldForArticle($articleID, $fieldName) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(['f.label','f.params','f.fieldparams','v.value']);
$query->from($db->quoteName('#__fields').' as f');
$query->join('inner', $db->quoteName('#__fields_values').' AS v ON f.id = v.field_id');
$query->where('f.context = "com_content.article"');
$query->where('f.state = 1');
$query->where('v.item_id = '.$db->quote($articleID));
$query->where('f.name = '.$db->quote($fieldName));
$query->order('f.ordering');
$db->setQuery($query);
return $db->loadObject();
}
}