-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitstream.php
More file actions
515 lines (455 loc) · 22.5 KB
/
bitstream.php
File metadata and controls
515 lines (455 loc) · 22.5 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?php
/**
* Plugin Name: BitStream
* Description: A lightweight microblogging platform for WordPress with PWA support, masonry layout, and social sharing.
* Version: 3.1.2
* Author: Facundo Pignanelli
* Text Domain: bitstream
* Requires at least: 5.8
* Requires PHP: 7.4
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Define plugin constants
define('BITSTREAM_VERSION', '3.1.2');
define('BITSTREAM_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('BITSTREAM_PLUGIN_URL', plugin_dir_url(__FILE__));
/**
* Main BitStream Plugin Class
*/
class BitStream_Plugin
{
private $components = [];
public function __construct()
{
add_action('plugins_loaded', [$this, 'init']);
}
/**
* Initialize the plugin
*/
public function init()
{
// Load includes
$this->load_includes();
// Initialize components
$this->init_components();
}
/**
* Load required files
*/
private function load_includes()
{
// Core functionality classes
require_once BITSTREAM_PLUGIN_PATH . 'includes/class-post-type.php';
require_once BITSTREAM_PLUGIN_PATH . 'includes/class-ajax-handlers.php';
require_once BITSTREAM_PLUGIN_PATH . 'includes/class-shortcodes.php';
require_once BITSTREAM_PLUGIN_PATH . 'includes/class-og-fetcher.php';
require_once BITSTREAM_PLUGIN_PATH . 'includes/class-error-logger.php';
// New modular classes
require_once BITSTREAM_PLUGIN_PATH . 'includes/class-admin-interface.php';
require_once BITSTREAM_PLUGIN_PATH . 'includes/class-block-editor.php';
require_once BITSTREAM_PLUGIN_PATH . 'includes/class-pwa-manager.php';
require_once BITSTREAM_PLUGIN_PATH . 'includes/class-rss-feeds.php';
require_once BITSTREAM_PLUGIN_PATH . 'includes/class-content-display.php';
require_once BITSTREAM_PLUGIN_PATH . 'includes/class-rebit-mappings.php';
}
/**
* Initialize all plugin components
*/
private function init_components()
{
// Allow audio uploads
add_filter('upload_mimes', function ($mimes) {
$mimes['mp3'] = 'audio/mpeg';
$mimes['m4a'] = 'audio/mp4';
$mimes['ogg'] = 'audio/ogg';
$mimes['wav'] = 'audio/wav';
$mimes['flac'] = 'audio/flac';
return $mimes;
}, 1, 1);
// Bypass file type checks for audio files
add_filter('wp_check_filetype_and_ext', function ($data, $file, $filename, $mimes) {
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (empty($data['type']) && in_array($ext, ['mp3', 'm4a', 'ogg', 'wav', 'flac'], true)) {
$mime_map = [
'mp3' => 'audio/mpeg',
'm4a' => 'audio/mp4',
'ogg' => 'audio/ogg',
'wav' => 'audio/wav',
'flac' => 'audio/flac',
];
$data['ext'] = $ext;
$data['type'] = $mime_map[$ext];
}
return $data;
}, 10, 4);
// Initialize core components
$this->components['post_type'] = new BitStream_Post_Type();
$this->components['ajax_handlers'] = new BitStream_Ajax_Handlers();
$this->components['shortcodes'] = new BitStream_Shortcodes();
$this->components['og_fetcher'] = new BitStream_OG_Fetcher();
$this->components['error_logger'] = new BitStream_Error_Logger();
// Initialize new modular components
$this->components['admin_interface'] = new BitStream_Admin_Interface();
$this->components['block_editor'] = new BitStream_Block_Editor();
$this->components['pwa_manager'] = new BitStream_PWA_Manager();
$this->components['rss_feeds'] = new BitStream_RSS_Feeds();
$this->components['content_display'] = new BitStream_Content_Display();
// Invalidate hashtag count cache when bits are created/updated/deleted
add_action('save_post_bit', ['BitStream_Content_Display', 'flush_hashtag_cache']);
add_action('trashed_post', ['BitStream_Content_Display', 'flush_hashtag_cache']);
add_action('deleted_post', ['BitStream_Content_Display', 'flush_hashtag_cache']);
// ReBit mappings is a utility class, no need to instantiate
}
/**
* Get a component instance
*/
public function get_component($component_name)
{
return isset($this->components[$component_name]) ? $this->components[$component_name] : null;
}
}
/**
* Render ReBit media/preview section for a card
*
* @param int $post_id
* @return string
*/
function bitstream_render_rebit_section($post_id)
{
$rebit_url = get_post_meta($post_id, 'bitstream_rebit_url', true);
if (empty($rebit_url)) {
return '';
}
$parsed = parse_url($rebit_url);
$host = $parsed['host'] ?? '';
ob_start();
$map = BitStream_ReBit_Mappings::get_mapping_for_domain($host);
if ($map) {
echo '<div class="bit-rebit-label" style="margin-bottom:0.5rem;font-size:0.95rem;color:#333;">'
. '<i class="' . esc_attr($map['icon']) . '" aria-hidden="true" style="margin-right:0.5rem;"></i>'
. esc_html($map['label'])
. '</div>';
}
else {
echo '<div class="bit-rebit-label" style="margin-bottom:0.5rem;font-size:0.95rem;color:#333;">'
. '<i class="fas fa-link" aria-hidden="true" style="margin-right:0.5rem;"></i> shared a link</div>';
}
$is_yt = stripos($host, 'youtube.com') !== false || stripos($host, 'youtu.be') !== false;
$is_twitter = stripos($host, 'twitter.com') !== false || stripos($host, 'x.com') !== false;
if ($is_yt) {
$video_id = '';
if (stripos($host, 'youtu.be') !== false) {
$video_id = ltrim($parsed['path'] ?? '', '/');
}
elseif (isset($parsed['query'])) {
parse_str($parsed['query'], $args);
$video_id = $args['v'] ?? '';
}
if ($video_id) {
echo '<div class="bit-rebit-embed" style="position:relative;padding-bottom:56.25%;height:0;overflow:hidden;margin:1rem 0;">'
. '<iframe src="https://www.youtube.com/embed/' . esc_attr($video_id) . '" '
. 'frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" '
. 'allowfullscreen style="position:absolute;top:0;left:0;width:100%;height:100%;"></iframe></div>';
}
else {
echo '<a href="' . esc_url($rebit_url) . '" target="_blank" rel="noopener" style="white-space:normal;overflow-wrap:anywhere;word-break:break-word;">' . esc_html($rebit_url) . '</a>';
}
}
elseif ($is_twitter) {
echo '<div class="bit-rebit-preview bit-rebit-twitter" style="border:1px solid #ddd;border-radius:12px;overflow:hidden;margin-bottom:1rem;display:flex;align-items:center;padding:1rem;gap:1.25rem;background:#fafafa;">';
$og_img = get_post_meta($post_id, '_bitstream_og_image', true);
if ($og_img) {
echo '<div style="flex-shrink:0;width:80px;height:80px;border-radius:8px;overflow:hidden;box-shadow:0 2px 4px rgba(0,0,0,0.1);">';
echo '<img src="' . esc_url($og_img) . '" style="width:100%;height:100%;object-fit:cover;display:block;" alt="">';
echo '</div>';
}
echo '<div style="flex-grow:1;min-width:0;display:flex;flex-direction:column;justify-content:center;">';
$og_title = get_post_meta($post_id, '_bitstream_og_title', true);
$og_desc = get_post_meta($post_id, '_bitstream_og_desc', true);
if ($og_title) {
echo '<h4 style="margin:0 0 0.35rem;font-size:1.05rem;line-height:1.3;">'
. '<a href="' . esc_url($rebit_url) . '" target="_blank" rel="noopener" style="color:var(--wp--preset--color--foreground,#333);text-decoration:none;font-weight:600;">' . wp_kses_post($og_title) . '</a></h4>';
}
if ($og_desc) {
echo '<p style="margin:0;font-size:0.9rem;color:#555;line-height:1.4;display:-webkit-box;-webkit-line-clamp:3;-webkit-box-orient:vertical;overflow:hidden;text-overflow:ellipsis;">' . wp_kses_post($og_desc) . '</p>';
}
if (!$og_desc && !$og_title) {
echo '<a href="' . esc_url($rebit_url) . '" target="_blank" rel="noopener" style="display:block;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-size:0.9rem;">' . esc_html($rebit_url) . '</a>';
}
echo '</div></div>';
}
else {
echo '<div class="bit-rebit-preview" style="border:1px solid #ddd;border-radius:8px;overflow:hidden;margin-bottom:1rem;">';
$og_img = get_post_meta($post_id, '_bitstream_og_image', true);
if ($og_img) {
echo '<img src="' . esc_url($og_img) . '" style="width:100%;display:block;" alt="">';
}
echo '<div style="padding:0.75rem;">';
$og_title = get_post_meta($post_id, '_bitstream_og_title', true);
$og_desc = get_post_meta($post_id, '_bitstream_og_desc', true);
if ($og_title) {
echo '<h4 style="margin:0 0 0.5rem;font-size:1.1rem;">'
. '<a href="' . esc_url($rebit_url) . '" target="_blank" rel="noopener" style="white-space:normal;overflow-wrap:anywhere;word-break:break-word;">' . wp_kses_post($og_title) . '</a></h4>';
}
if ($og_desc) {
echo '<p style="margin:0;font-size:0.95rem;color:#555;">' . wp_kses_post($og_desc) . '</p>';
}
if (!$og_desc) {
echo '<a href="' . esc_url($rebit_url) . '" target="_blank" rel="noopener" style="white-space:normal;overflow-wrap:anywhere;word-break:break-word;">' . esc_html($rebit_url) . '</a>';
}
echo '</div></div>';
}
return ob_get_clean();
}
/**
* Render a nested quoted Bit card without action buttons/comments
*
* @param int $post_id
* @return string
*/
function bitstream_render_nested_quoted_card($post_id)
{
$quoted_post = get_post($post_id);
if (!($quoted_post instanceof WP_Post) || $quoted_post->post_type !== 'bit' || $quoted_post->post_status !== 'publish') {
ob_start();
?>
<article id="bit-quoted-missing-<?php echo esc_attr($post_id); ?>" class="bit-card bit-card-quoted-nested bit-card-quoted-unavailable" style="margin:0;padding:1rem;width:100%;max-width:none;box-sizing:border-box;border:1px solid #ddd;border-radius:15px;background:#fff;">
<div class="bit-card-content" style="font-size:0.95rem;line-height:1.5;margin:0;">
<p style="margin:0;color:var(--wp--preset--color--secondary,#666);">Original Bit unavailable.</p>
</div>
</article>
<?php
return ob_get_clean();
}
$content = wpautop(get_post_field('post_content', $post_id));
if (class_exists('BitStream_Content_Display')) {
global $post;
$original_post = $post;
$post = $quoted_post;
$display = new BitStream_Content_Display();
$content = $display->linkify_hashtags($content);
$post = $original_post;
}
$timestamp = human_time_diff(get_post_time('U', false, $post_id), current_time('timestamp')) . ' ago';
$posted_datetime = get_post_time('d/m/Y H:i', false, $post_id);
$is_edited = get_post_modified_time('U', false, $post_id) > get_post_time('U', false, $post_id);
$timestamp_tooltip = 'Posted: ' . $posted_datetime . ($is_edited ? ' • Edited' : '');
$rebit_markup = bitstream_render_rebit_section($post_id);
ob_start();
?>
<article id="bit-quoted-<?php echo esc_attr($post_id); ?>" class="bit-card bit-card-quoted-nested" style="margin:0;padding:1.5rem;width:100%;max-width:none;box-sizing:border-box;border:1px solid #ddd;border-radius:15px;background:#fff;box-shadow:0 2px 4px rgba(0,0,0,0.05);">
<header class="bit-card-header" style="display:flex;align-items:center;margin-bottom:1rem;">
<div class="bit-meta" style="font-size:0.875rem;color:var(--wp--preset--color--secondary,#666);">
<span class="bit-timestamp" tabindex="0" data-timestamp-tooltip="<?php echo esc_attr($timestamp_tooltip); ?>" title="<?php echo esc_attr($timestamp_tooltip); ?>"><?php echo esc_html($timestamp); ?></span>
</div>
</header>
<div class="bit-card-content" style="font-size:1rem;line-height:1.6;margin-bottom:1rem;">
<?php echo $content; ?>
</div>
<?php echo $rebit_markup; ?>
</article>
<?php
return ob_get_clean();
}
/**
* Render a bit card
*
* @param int $post_id The post ID to render
* @param bool $skip_content_filter Whether to skip the content filter to avoid infinite loops
* @return string The rendered HTML
*/
if (!function_exists('bitstream_comment_callback')) {
function bitstream_comment_callback($comment, $args, $depth)
{
$GLOBALS['comment'] = $comment;
$tag = ('div' === $args['style']) ? 'div' : 'li';
?>
<<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class(empty($args['has_children']) ? '' : 'parent'); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="bit-modern-comment">
<div class="bit-modern-comment-avatar">
<?php if (0 != $args['avatar_size'])
echo get_avatar($comment, $args['avatar_size']); ?>
</div>
<div class="bit-modern-comment-content">
<div class="bit-modern-comment-header">
<cite class="fn bit-modern-comment-author"><?php echo get_comment_author_link(); ?></cite>
<span class="bit-modern-comment-date">
<a href="<?php echo esc_url(get_comment_link($comment, $args)); ?>">
<?php printf('%s', get_comment_date()); ?>
</a>
</span>
</div>
<?php if ('0' == $comment->comment_approved): ?>
<p class="comment-awaiting-moderation"><?php esc_html_e('Your comment is awaiting moderation.', 'bitstream'); ?></p>
<?php
endif; ?>
<div class="bit-modern-comment-text">
<?php comment_text(); ?>
</div>
<div class="bit-modern-comment-actions">
<?php edit_comment_link(esc_html__('Edit', 'bitstream'), '<span class="edit-link">', '</span>'); ?>
<?php
comment_reply_link(array_merge($args, [
'add_below' => 'div-comment',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<span class="reply-link">',
'after' => '</span>'
]));
?>
</div>
</div>
</article>
<?php
}
}
if (!function_exists('bitstream_render_card')) {
function bitstream_render_card($post_id, $skip_content_filter = false)
{
// Avoid infinite loop by skipping content filter when rendering in single bit context
if ($skip_content_filter) {
$content = get_post_field('post_content', $post_id);
$content = wpautop($content); // Basic paragraph formatting
}
else {
$GLOBALS['bitstream_is_rendering_card'] = true;
$content = apply_filters('the_content', get_post_field('post_content', $post_id));
unset($GLOBALS['bitstream_is_rendering_card']);
}
$timestamp = human_time_diff(get_post_time('U', false, $post_id), current_time('timestamp')) . ' ago';
$posted_datetime = get_post_time('d/m/Y H:i', false, $post_id);
$is_edited = get_post_modified_time('U', false, $post_id) > get_post_time('U', false, $post_id);
$timestamp_tooltip = 'Posted: ' . $posted_datetime . ($is_edited ? ' • Edited' : '');
$avatar = get_avatar(get_post_field('post_author', $post_id), 48, '', '', ['class' => 'bit-avatar-img']);
$likes = (int)get_post_meta($post_id, '_bitstream_likes', true);
$comments = get_comments_number($post_id);
$quoted_id = (int)get_post_meta($post_id, '_bitstream_quoted_bit', true);
$is_rebit_card = !empty(get_post_meta($post_id, 'bitstream_rebit_url', true));
$rebit_markup = bitstream_render_rebit_section($post_id);
$quoted_markup = '';
if ($quoted_id > 0) {
$quoted_markup = bitstream_render_nested_quoted_card($quoted_id);
}
ob_start(); ?>
<article id="bit-<?php echo esc_attr($post_id); ?>" class="bit-card" style="margin:0;padding:1.5rem;width:100%;max-width:none;box-sizing:border-box;border:1px solid #eee;border-radius:15px;background:#fff;box-shadow:0 2px 4px rgba(0,0,0,0.05);">
<header class="bit-card-header" style="display:flex;align-items:center;margin-bottom:1rem;">
<div class="bit-avatar" style="width:48px;height:48px;margin-right:0.75rem;border-radius:15px;overflow:hidden;">
<?php echo $avatar; ?>
</div>
<div class="bit-meta" style="font-size:0.875rem;color:var(--wp--preset--color--secondary,#666);">
<span class="bit-timestamp" tabindex="0" data-timestamp-tooltip="<?php echo esc_attr($timestamp_tooltip); ?>" title="<?php echo esc_attr($timestamp_tooltip); ?>"><?php echo esc_html($timestamp); ?></span>
</div>
</header>
<div class="bit-card-content" style="font-size:1rem;line-height:1.6;margin-bottom:1rem;">
<?php echo $content; ?>
</div>
<?php echo $rebit_markup; ?>
<?php if (!empty($quoted_markup)): ?>
<div class="bitstream-quoted-preview">
<?php echo $quoted_markup; ?>
</div>
<?php
endif; ?>
<hr style="margin:1rem 0;border:none;border-top:1px solid #eee;">
<?php
$can_quote = current_user_can('edit_posts');
$can_edit = current_user_can('edit_post', $post_id);
$can_delete = is_user_logged_in() && current_user_can('delete_post', $post_id);
$show_admin_actions = $can_quote || $can_edit || $can_delete;
?>
<footer class="bit-card-footer" style="display:flex;gap:0.75rem;font-size:0.875rem;align-items:center;">
<div class="bit-card-footer-main-actions">
<button class="bit-comment-toggle bit-action" data-target="comments-<?php echo esc_attr($post_id); ?>" style="background:none;border:none;cursor:pointer;">
<i class="fas fa-comment-dots"></i> <?php echo esc_html($comments); ?>
</button>
<button class="bit-like bit-action" data-post-id="<?php echo esc_attr($post_id); ?>" style="background:none;border:none;cursor:pointer;">
<i class="fas fa-heart"></i> <span class="bit-like-count"><?php echo esc_html($likes); ?></span>
</button>
<button class="bit-permalink bit-action" data-url="<?php echo esc_url(get_permalink($post_id)); ?>" style="background:none;border:none;cursor:pointer;" title="Copy link: <?php echo esc_attr(get_permalink($post_id)); ?>">
<i class="fa-solid fa-up-right-from-square"></i>
</button>
</div>
<?php if ($show_admin_actions): ?>
<span class="bit-card-footer-spacer" aria-hidden="true"></span>
<div class="bit-card-footer-admin-actions">
<?php if ($can_quote): ?>
<button class="bit-quote bit-action" data-post-id="<?php echo esc_attr($post_id); ?>" style="background:none;border:none;cursor:pointer;" title="Quote this bit">
<i class="fa-solid fa-retweet"></i>
</button>
<?php
endif; ?>
<?php if ($can_edit): ?>
<button class="bit-edit bit-action" data-post-id="<?php echo esc_attr($post_id); ?>" data-post-type="<?php echo esc_attr($is_rebit_card ? 'rebit' : 'bit'); ?>" style="background:none;border:none;cursor:pointer;" title="Edit this bit">
<i class="fa-solid fa-pencil"></i>
</button>
<?php
endif; ?>
<?php if ($can_delete): ?>
<button class="bit-delete bit-action" data-post-id="<?php echo esc_attr($post_id); ?>" style="background:none;border:none;cursor:pointer;" title="Delete this bit">
<i class="fa-solid fa-trash"></i>
</button>
<?php
endif; ?>
</div>
<?php
endif; ?>
</footer>
<div id="comments-<?php echo $post_id; ?>" class="bit-comments">
<ol class="comment-list">
<?php wp_list_comments(['style' => 'ol', 'callback' => 'bitstream_comment_callback', 'avatar_size' => 48, 'short_ping' => true, 'max_depth' => 3], get_comments(['post_id' => $post_id, 'status' => 'approve'])); ?>
</ol>
<div class="bit-comment-form">
<?php comment_form([
'comment_notes_after' => '',
'title_reply' => 'Leave a Comment',
'logged_in_as' => '',
'comment_field' => '<p><textarea name="comment" required></textarea></p>',
], $post_id); ?>
</div>
</div>
</article>
<?php
return ob_get_clean();
}
}
// Initialize the plugin
new BitStream_Plugin();
// Plugin activation/deactivation hooks
register_activation_hook(__FILE__, 'bitstream_plugin_activate');
register_deactivation_hook(__FILE__, 'bitstream_plugin_deactivate');
/**
* Plugin activation callback
*/
function bitstream_plugin_activate()
{
// Ensure post type is registered before flushing
$plugin = new BitStream_Plugin();
$plugin->init();
// Import default ReBit mappings if none exist (via centralized class)
BitStream_ReBit_Mappings::import_default_mappings();
// Ensure weekly BitStream media cleanup is scheduled
bitstream_schedule_weekly_media_cleanup();
// Flush rewrite rules to ensure permalinks work (including PWA shortcuts)
flush_rewrite_rules();
}
/**
* Schedule weekly media cleanup cron event.
*/
function bitstream_schedule_weekly_media_cleanup()
{
if (!wp_next_scheduled('bitstream_weekly_media_cleanup_event')) {
wp_schedule_event(time() + HOUR_IN_SECONDS, 'bitstream_weekly', 'bitstream_weekly_media_cleanup_event');
}
}
/**
* Plugin deactivation callback
*/
function bitstream_plugin_deactivate()
{
wp_clear_scheduled_hook('bitstream_weekly_media_cleanup_event');
// Flush rewrite rules on deactivation
flush_rewrite_rules();
}