-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_twitter_tweet_renderer.plugin.php
More file actions
223 lines (199 loc) · 6.55 KB
/
_twitter_tweet_renderer.plugin.php
File metadata and controls
223 lines (199 loc) · 6.55 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* This file implements the Twitter Tweet renderer plugin for b2evolution
*
* b2evolution - {@link http://b2evolution.net/}
* Released under GNU GPL License - {@link http://b2evolution.net/about/gnu-gpl-license}
* @copyright (c)2003-2020 by Francois Planque - {@link http://fplanque.com/}
*
* @package plugins
*/
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
/**
* Twitter Tweet Renderer plugin.
*
* @package plugins
*/
class twitter_tweet_renderer_plugin extends Plugin
{
var $code = 'embTweetRdr';
var $name = 'Twitter Tweet renderer';
var $priority = 60;
var $version = '7.1.3';
var $group = 'rendering';
var $short_desc;
var $long_desc;
var $help_topic = 'twitter-tweet-renderer-plugin';
var $number_of_installs = 1;
/**
* Init
*/
function PluginInit( & $params )
{
$this->short_desc = $this->T_('Automatically create embedded Tweets in posts.');
$this->long_desc = $this->T_('This renderer automatically create embedded Tweets from Twitter URLs in your posts. You can customize how the Tweets will appear.');
}
/**
* Define here default custom settings that are to be made available
* in the backoffice for collections, private messages and newsletters.
*
* @param array Associative array of parameters.
* @return array See {@link Plugin::get_custom_setting_definitions()}.
*/
function get_custom_setting_definitions( & $params )
{
return array(
'conversation' => array(
'label' => $this->T_('Conversations'),
'type' => 'select',
'options' => array(
'all' => $this->T_('All'),
'none' => $this->T_('None')
),
'defaultvalue' => 'all',
),
'cards' => array(
'label' => $this->T_('Cards'),
'type' => 'select',
'options' => array(
'hidden' => $this->T_('Hidden'),
'visible' => $this->T_('Visible'),
),
'defaultvalue' => 'visible',
),
'width' => array(
'label' => $this->T_( 'Width' ),
'type' => 'integer',
'note' => $this->T_('Leave empty for default'),
'suffix' => ' px',
'allow_empty' => true,
'defaultvalue' => NULL,
),
'align' => array(
'label' => $this->T_('Align'),
'type' => 'select',
'options' => array(
'' => $this->T_('Auto'),
'left' => $this->T_('Left'),
'right' => $this->T_('Right'),
'center' => $this->T_('Center'),
),
'defaultvalue' => 'auto',
),
'theme' => array(
'label' => $this->T_('Theme'),
'type' => 'select',
'options' => array(
'dark' => $this->T_('Dark'),
'light' => $this->T_('Light'),
),
'defaultvalue' => 'light',
),
'link_color' => array(
'label' => $this->T_('Link color'),
'type' => 'color',
'defaultvalue' => '#2b7bb9',
),
);
}
/**
* Define here default collection/blog settings that are to be made available in the backoffice.
*
* @param array Associative array of parameters.
* @return array See {@link Plugin::get_coll_setting_definitions()}.
*/
function get_coll_setting_definitions( & $params )
{
$default_params = array_merge( $params, array(
'default_comment_rendering' => 'never',
'default_post_rendering' => 'opt-out'
) );
return parent::get_coll_setting_definitions( $default_params );
}
/**
* Perform rendering
*
* @param array Associative array of parameters
* (Output format, see {@link format_to_output()})
* @return boolean true if we can render something for the required output format
*/
function RenderItemAsHtml( & $params )
{
$content = & $params['data'];
$params['check_code_block'] = false;
// Wrap Twitter URLs (not within an HTML tag attribute) with marker element:
$content = replace_content_outcode_shorttags( '#<.+?>(*SKIP)(*F)|https?://twitter\.com/(\w*)/(statuses|status)/(\d*)#is',
'<span class="tweet" data-tweet-id="$3">$0</span>', $content );
return $content;
}
/**
* Event handler: Called when displaying an item/post's content as HTML.
*
* This is different from {@link RenderItemAsHtml()}, because it gets called
* on every display (while rendering gets cached).
*
* @param array Associative array of parameters
* - 'data': the data (by reference). You probably want to modify this.
* - 'format': see {@link format_to_output()}.
* - 'Item': The {@link Item} that gets displayed (by reference).
* - 'Comment': The {@link Comment} that gets displayed (by reference).
* - 'Message': The {@link Message} that gets displayed (by reference).
* - 'EmailCampaign': The {@link EmailCampaign} that gets displayed (by reference).
* - 'Widget': The {@link Widget} that gets displayed (by reference).
* - 'preview': Is this only a preview?
* - 'dispmore': Does this include the "more" text (if available), which means "full post"?
* - 'view_type': What part of a post are we displaying: 'teaser', 'extension' or 'full'
* @return boolean Have we changed something?
*/
function DisplayItemAsHtml( & $params )
{
$marker = '<span class="tweet" data-tweet-id';
if( strpos( $params['data'], $marker ) != false )
{ // A Twitter URL marker was found:
$current_Blog = $this->get_Blog_from_params( $params );
$options = array(
'conversation' => $this->get_coll_setting( 'conversation', $current_Blog ),
'cards' => $this->get_coll_setting( 'cards', $current_Blog ),
'theme' => $this->get_coll_setting( 'theme', $current_Blog ),
'linkColor' => $this->get_coll_setting( 'link_color', $current_Blog ),
);
if( ! empty( $this->get_coll_setting( 'align', $current_Blog ) ) )
{
$options['align'] = $this->get_coll_setting( 'align', $current_Blog );
}
if( ! empty( $this->get_coll_setting( 'width', $current_Blog ) ) )
{
$options['width'] = $this->get_coll_setting( 'width', $current_Blog );
}
$script = '<script>
window.twttr = ( function( d, s, id ) {
var js, fjs = d.getElementsByTagName(s)[0],
t = window.twttr || {};
if ( d.getElementById( id ) ) return t;
js = d.createElement( s );
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore( js, fjs );
t._e = [];
t.ready = function( f ) {
t._e.push(f);
};
return t;
}( document, "script", "twitter-wjs" ) );
twttr.ready( function()
{
var tweets = jQuery( ".tweet" );
var options = '.json_encode( $options ).'
tweets.each( function( index, tweet ) {
var tweet_id = tweet.dataset.tweetId;
tweet.innerHTML = "";
twttr.widgets.createTweet( tweet_id, tweet, options );
} );
} );
</script>';
$params['data'] .= $script;
}
return true;
}
}
?>