-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadd-attribution-feedwordpress.php
More file actions
451 lines (395 loc) · 14.2 KB
/
Copy pathadd-attribution-feedwordpress.php
File metadata and controls
451 lines (395 loc) · 14.2 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
/*
Plugin Name: FWP+: Add Attribution
Plugin URI: http://projects.radgeek.com/add-attribution-feedwordpress/
Description: enable FeedWordPress to add a prefix or a suffix to elements of syndicated posts, containing attribution information
Version: 2016.0506
Author: Charles Johnson
Author URI: http://radgeek.com/
License: GPL
*/
define('ADD_ATTRIBUTION_DEFAULT_HOOK_ORDER', 11000); // at the tail end of the filtering process
$hookOrder = get_option('feedwordpress_add_attribution_hook_order', ADD_ATTRIBUTION_DEFAULT_HOOK_ORDER);
add_action(
/*hook=*/ 'feedwordpress_admin_page_posts_meta_boxes',
/*function=*/ 'add_meta_box_add_source_information',
/*priority=*/ 100,
/*arguments=*/ 1
);
add_action(
/*hook=*/ 'feedwordpress_admin_page_posts_save',
/*function=*/ 'add_source_information_save',
/*priority=*/ 100,
/*arguments=*/ 2
);
add_filter(
/*hook=*/ 'the_title',
/*function=*/ 'add_source_information_title',
/*priority=*/ $hookOrder,
/*arguments=*/ 2
);
add_filter(
/*hook=*/ 'get_the_excerpt',
/*function=*/ 'add_source_information_excerpt',
/*priority=*/ $hookOrder,
/*arguments=*/ 1
);
add_filter(
/*hook=*/ 'the_content',
/*function=*/ 'add_source_information_content',
/*priority=*/ $hookOrder,
/*arguments=*/ 1
);
add_filter(
/*hook=*/ 'the_content_rss',
/*function=*/ 'add_source_information_content',
/*priority=*/ $hookOrder,
/*arguments=*/ 1
);
function add_meta_box_add_source_information ($page) {
add_meta_box(
/*id=*/ 'feedwordpress_add_attribution_box',
/*title=*/ __('Attribution Boilerplate'),
/*callback=*/ 'add_source_information_box',
/*page=*/ $page->meta_box_context(),
/*context=*/ $page->meta_box_context()
);
} /* add_meta_box_add_source_information() */
function add_source_information_box ($page, $box = NULL) {
if ($page->for_feed_settings()) :
$attrib = unserialize($page->link->settings['add attribution rules']);
$syndicatedPosts = 'this feed\'s posts';
else :
$attrib = get_option('feedwordpress_add_attribution');
$syndicatedPosts = 'syndicated posts';
endif;
$hookOrder = intval($page->setting('add attribution hook order', ADD_ATTRIBUTION_DEFAULT_HOOK_ORDER));
?>
<style type="text/css">
.add-attribution-help-box {
float: right;
width: 300px;
border: 1px dotted #777;
margin: 3px;
padding: 5px;
background-color: #f7f7f7;
}
.add-attribution-help-box dt {
font-weight: bold;
font-size: 85%;
}
.add-attribution-help-box dd {
font-size: 80%; line-height: 100%; font-style: italic;
padding-left: 1.5em;
}
.add-attribution-help-box code {
font-size: inherit;
font-style: normal;
background-color: inherit;
}
.add-attribution-li {
padding-bottom: 5px;
margin-bottom: 5px;
border-bottom: 1px dotted black;
}
</style>
<div class="add-attribution-help-box">
<p style="border-bottom: 1px dotted #777;">To remove boilerplate, just blank out the text box and leave it empty.</p>
<p>Use shortcodes to include information about your source:</p>
<dl>
<dt><code>[source]</code></dt>
<dd>A link to the source you syndicated the post from</dd>
<dt><code>[source-name]</code></dt>
<dd>Name of the source you syndicated the post from</dd>
<dt><code>[source-url]</code></dt>
<dd>URL of the source you syndicated the post from</dd>
<dt><code>[original-link]text[/original-link]</code></dt>
<dd>A link to the <em>original post</em> back on the source website,
using <code>text</code> as the link text.</dd>
<dt><code>[original-url]</code></dt>
<dd>URL of the <em>original post</em> back on the source website</dd>
<dt><code>[author]</code></dt>
<dd>A link with the name of the author who wrote the post, linking to other posts by the same author</dd>
<dt><code>[author-name]</code></dt>
<dd>Name of the author who wrote the post</dd>
<dt><code>[feed-setting key="setting-name"]</code></dt>
<dd>Value of a custom feed setting (named <code>setting-name</code>) for the feed</dd>
</dl>
</div>
<?php
print "<ul>\n";
if (!is_array($attrib)) : $attrib = array(); endif;
print '<input type="hidden" id="next-attribution-rule-index" name="next_attribution_rule_index" value="'.count($attrib).'" />';
// In AJAX environment, this is a dummy rule that stays hidden. In a
// non-AJAX environment, this provides a blank rule that we can fill in.
$attrib['new'] = array(
'class' => array('hide-if-js'),
'placement' => 'before',
'element' => 'post',
'template' => '',
);
foreach ($attrib as $index => $line) :
if (isset($line['template'])) :
$selected['before'] = (($line['placement']=='before') ? ' selected="selected"' : '');
$selected['after'] = (($line['placement']=='after') ? ' selected="selected"' : '');
$selected['title'] = (($line['element']=='title') ? ' selected="selected"' : '');
$selected['post'] = (($line['element']=='post') ? ' selected="selected"' : '');
$selected['excerpt'] = (($line['element']=='excerpt') ? ' selected="selected"' : '');
if (!isset($line['class'])) : $line['class'] = array(); endif;
$line['class'][] = 'add-attribution-li';
?>
<li id="add-attribution-<?php print $index; ?>-li" class="<?php print implode(' ', $line['class']); ?>">» <strong>Add</strong> <select id="add-attribution-<?php print $index; ?>-placement" name="add_attribution[<?php print $index; ?>][placement]" style="width: 8.0em">
<option value="before"<?php print $selected['before']; ?>>before</option>
<option value="after"<?php print $selected['after']; ?>>after</option>
</select> the <select style="width: 8.0em" id="add-attribution-<?php print $index; ?>-element" name="add_attribution[<?php print $index; ?>][element]">
<option value="title"<?php print $selected['title']; ?>>title</option>
<option value="post"<?php print $selected['post']; ?>>content</option>
<option value="excerpt"<?php print $selected['excerpt']; ?>>excerpt</option>
</select> of
<?php print $syndicatedPosts; ?>: <textarea style="vertical-align: top; width: 40%;" rows="2" cols="30" class="add-attribution-template" id="add-attribution-<?php print $index; ?>-template" name="add_attribution[<?php print $index; ?>][template]"><?php print htmlspecialchars($line['template']); ?></textarea></li>
<?php
endif;
endforeach;
?>
</ul>
<br style="clear: both" />
<script type="text/javascript">
jQuery(document).ready( function($) {
$('.add-attribution-template').blur( function() {
if (this.value.length == 0) {
var theLi = $('li:has(#'+this.id+")");
theLi.hide('normal')
}
} );
var addRuleLi = document.createElement('li');
addRuleLi.innerHTML = '<strong style="vertical-align: middle; font-size: 110%">[+]</strong> <a style="font-variant: small-caps" id="add-new-attribution-rule" href="#">Add new boilerplate</a> ….';
$('#add-attribution-new-li').after(addRuleLi);
$('#add-new-attribution-rule').click( function() {
// Get index counter
var nextIndex = parseInt($('#next-attribution-rule-index').val());
var newIdPrefix = 'add-attribution-'+nextIndex;
var newNamePrefix = 'add_attribution['+nextIndex+']';
var dummy = {};
dummy['li'] = {'el': $('#add-attribution-new-li') }
dummy['placement'] = {'el': $('#add-attribution-new-placement') };
dummy['element'] = {'el': $('#add-attribution-new-element') };
dummy['template'] = {'el': $('#add-attribution-new-template') };
for (var element in dummy) {
dummy[element]['save'] = {
'id': dummy[element]['el'].attr('id'),
'name': dummy[element]['el'].attr('name')
};
dummy[element]['el'].attr('id', newIdPrefix+'-'+element);
dummy[element]['el'].attr('name', newNamePrefix+'['+element+']');
}
var newLi = $('#'+newIdPrefix+'-li').clone(/*events=*/ true);
//newLi.attr('id', null);
newLi.removeClass('hide-if-js');
newLi.addClass('add-attribution-li');
newLi.css('display', 'none');
// Switch back
for (var element in dummy) {
dummy[element]['el'].attr('id', dummy[element]['save']['id']);
dummy[element]['el'].attr('name', dummy[element]['save']['name']);
}
$('#add-attribution-new-li').before(newLi);
newLi.show('normal');
$('#next-attribution-rule-index').val(nextIndex+1);
return false;
} )
} );
</script>
<?php
if ($page->for_default_settings()) :
?>
<h3>Advanced Settings</h3>
<table class="edit-form narrow">
<tbody>
<tr><th scope="row"><?php _e("Hook Order:") ?></th>
<td><input type="number" name="add_attribution_hook_order" value="<?php print esc_attr($hookOrder); ?>" /></td></tr>
</tbody>
</table>
<?php
endif;
} /* add_source_information_box () */
function add_source_information_save ($params, $page) {
if (isset($params['save']) or isset($params['submit'])) :
if (isset($params['add_attribution'])) :
foreach ($params['add_attribution'] as $index => $line) :
if (0 == strlen(trim($line['template']))) :
unset($params['add_attribution'][$index]);
endif;
endforeach;
// Convert indexes to 0..(N-1) to avoid possible collisions
$params['add_attribution'] = array_values($params['add_attribution']);
if ($page->for_feed_settings()) :
$page->link->settings['add attribution rules'] = serialize($params['add_attribution']);
$page->link->save_settings(/*reload=*/ true);
else :
update_option('feedwordpress_add_attribution', $params['add_attribution']);
endif;
endif;
if (isset($params['add_attribution_hook_order'])) :
$page->update_setting('add attribution hook order', intval($params['add_attribution_hook_order']));
endif;
endif;
}
class AddSourceInformationReformatter {
var $id, $element;
function AddSourceInformationReformatter ($id = NULL, $element = 'post') {
$this->id = $id;
$this->element = $element;
}
function shortcode_methods () {
return array(
'source' => 'source_link',
'source-name' => 'source_name',
'source-url' => 'source_url',
'original-link' => 'original_link',
'original-url' => 'original_url',
'author' => 'source_author_link',
'author-name' => 'source_author',
'feed-setting' => 'source_setting',
);
}
function do_shortcode ($template) {
$codes = $this->shortcode_methods();
// Register shortcodes relative to this object/post ID/element.
foreach ($codes as $code => $method) :
add_shortcode($code, array($this, $method));
endforeach;
$template = do_shortcode($template);
// Unregister shortcodes.
foreach ($codes as $code => $method) :
remove_shortcode($code);
endforeach;
return $template;
}
function source_name ($atts) {
$param = shortcode_atts(array(
'original' => NULL,
), $atts);
return get_syndication_source($param['original'], $this->id);
}
function source_url ($atts) {
$param = shortcode_atts(array(
'original' => NULL,
), $atts);
return get_syndication_source_link($param['original'], $this->id);
}
function source_link ($atts) {
switch (strtolower($atts[0])) :
case '-name' :
$ret = $this->source_name($atts);
break;
case '-url' :
$ret = $this->source_url($atts);
break;
default :
$param = shortcode_atts(array(
'original' => NULL,
), $atts);
if ('title' == $this->element) :
$ret = $this->source_name($atts);
else :
$ret = '<a href="'.htmlspecialchars($this->source_url($atts)).'">'.htmlspecialchars($this->source_name($atts)).'</a>';
endif;
endswitch;
return $ret;
}
function source_setting ($atts) {
$param = shortcode_atts(array(
'key' => NULL,
), $atts);
return get_feed_meta($param['key'], $this->id);
}
function original_link ($atts, $text) {
$url = $this->original_url($atts);
return '<a href="'.esc_url($url).'">'.do_shortcode($text).'</a>';
}
function original_url ($atts) {
return get_syndication_permalink($this->id);
}
function source_author ($atts) {
return get_the_author();
}
function source_author_link ($atts) {
switch (strtolower($atts[0])) :
case '-name' :
$ret = $this->source_author($atts);
break;
default :
global $authordata; // Janky.
if ('title' == $this->element) :
$ret = $this->source_author($atts);
else :
$ret = get_the_author();
$url = get_author_posts_url((int) $authordata->ID, (int) $authordata->user_nicename);
if ($url) :
$ret = '<a href="'.$url.'" '
.'title="Read other posts by '.esc_html($authordata->display_name).'">'
.$ret
.'</a>';
endif;
endif;
endswitch;
return $ret;
}
}
function add_source_information_reformat ($template, $element, $id = NULL) {
if ('post' == $element and !preg_match('/< (p|div) ( \s [^>]+ )? >/xi', $template)) :
$template = '<p class="syndicated-attribution">'.$template.'</p>';
endif;
$ref = new AddSourceInformationReformatter($id, $element);
return $ref->do_shortcode($template);
}
function add_source_information_simple ($element, $title, $id = NULL) {
if (is_syndicated($id)) :
$meta = get_feed_meta('add attribution rules', $id);
if ($meta and !is_array($meta)) : $meta = unserialize($meta); endif;
if (!is_array($meta) or empty($meta)) :
$meta = get_option('feedwordpress_add_attribution');
endif;
if (is_array($meta) and !empty($meta)) :
foreach ($meta as $rule) :
if ($element==$rule['element']) :
$rule['template'] = add_source_information_reformat($rule['template'], $element, $id);
if ('before'==$rule['placement']) :
$title = $rule['template'] . ' ' . $title;
else :
$title = $title . ' ' . $rule['template'];
endif;
endif;
endforeach;
endif;
endif;
return $title;
}
function add_source_information_title ($title, $id = NULL) {
return add_source_information_simple('title', $title, $id);
}
function add_source_information_excerpt ($title, $id = NULL) {
return add_source_information_simple('excerpt', $title, $id);
}
function add_source_information_content ($content) {
if (is_syndicated()) :
$meta = get_feed_meta('add attribution rules');
if ($meta and !is_array($meta)) : $meta = unserialize($meta); endif;
if (!is_array($meta) or empty($meta)) :
$meta = get_option('feedwordpress_add_attribution');
endif;
if (is_array($meta) and !empty($meta)) :
foreach ($meta as $rule) :
if ('post'==$rule['element']) :
$rule['template'] = add_source_information_reformat($rule['template'], 'post');
if ('before'==$rule['placement']) :
$content = $rule['template'] . "\n" . $content;
else :
$content = $content . "\n" . $rule['template'];
endif;
endif;
endforeach;
endif;
endif;
return $content;
}