‚SnowTok Videos‘,
‚public‘ => true,
’show_in_rest‘ => true,
’supports‘ => [‚title‘,’editor‘,’thumbnail‘,’author‘],
‚menu_icon‘ => ‚dashicons-video-alt3‘,
‚rewrite‘ => [’slug‘ => ’snowtok-video‘]
]);
});
/* ———- 2) Kleinste Security-Helfer ———- */
function snowtok_nonce_field($action){
$nonce = wp_create_nonce($action);
return ‚‚;
}
function snowtok_verify_nonce($action, $field_val){
return (bool) wp_verify_nonce($field_val, $action);
}
/* ———- 3) Shortcode: FEED im TikTok-Look ———- */
add_shortcode(’snowtok‘, function(){
// Daten
$videos = get_posts([
‚post_type‘ => ’snowtok_video‘,
‚posts_per_page‘ => 50,
‚orderby‘ => ‚date‘,
‚order‘ => ‚DESC‘,
‚post_status‘ => ‚publish‘,
]);
// Inline CSS (kompakt)
$css = ‚
‚;
// HTML
$html = ‚
if(empty($videos)){
$html .= ‚
‚;
} else {
foreach($videos as $v){
$vid_url = “;
// Bevorzugt: URL in Post-Content (einfach: nur URL rein)
$content = trim( wp_strip_all_tags( $v->post_content ) );
if(filter_var($content, FILTER_VALIDATE_URL)) $vid_url = esc_url($content);
// Fallback: Aus Metadaten (optional)
if(!$vid_url){
$meta = get_post_meta($v->ID, ‚_snowtok_video_url‘, true);
if($meta && filter_var($meta, FILTER_VALIDATE_URL)) $vid_url = esc_url($meta);
}
if(!$vid_url) continue;
$title = esc_html(get_the_title($v));
$desc = esc_html(wp_trim_words(get_post_field(‚post_content‘, $v->ID), 20));
$author = get_the_author_meta(‚display_name‘, $v->post_author) ?: ‚User‘;
$pid = (int)$v->ID;
$html .= ‚
‚;
}
}
$html .= ‚
‚;
// Inline JS – AutoPlay + Likes + Sound + Snap
$js = ‚‚;
return $css . $html . $js;
});
/* ———- 4) Shortcode: Frontend-Upload (ohne Login, mit PIN) ———- */
add_shortcode(’snowtok_upload‘, function($atts){
$a = shortcode_atts([‚pin‘ => ‚1234‘, ‚maxmb‘ => ‚200‘], $atts, ’snowtok_upload‘);
$maxBytes = max(5, intval($a[‚maxmb‘])) * 1024 * 1024; // Default 200MB
// Form + Minimal-CSS + JS (AJAX an admin-ajax)
ob_start(); ?>
SnowTok Upload
false,’message’=>’Ungültige Anfrage (Nonce).‘], 403);
}
// PIN prüfen (einfacher Umweg für Upload ohne Login)
$pin = isset($_POST[’snowtok_pin‘]) ? trim($_POST[’snowtok_pin‘]) : “;
// Standard-PIN 1234, kann pro Shortcode geändert werden; hier nur Basiskontrolle
if ($pin === “) {
wp_send_json([’success’=>false,’message’=>’PIN fehlt.‘], 400);
}
// Datei prüfen
if (empty($_FILES[’snowtok_file‘]) || $_FILES[’snowtok_file‘][‚error‘] !== UPLOAD_ERR_OK) {
wp_send_json([’success’=>false,’message’=>’Keine Datei erhalten.‘], 400);
}
// Upload vorbereiten (ohne is_user_logged_in – bewusster Umweg)
require_once ABSPATH . ‚wp-admin/includes/file.php‘;
require_once ABSPATH . ‚wp-admin/includes/media.php‘;
require_once ABSPATH . ‚wp-admin/includes/image.php‘;
// Begrenze auf Video-MIME
$filetype = wp_check_filetype_and_ext($_FILES[’snowtok_file‘][‚tmp_name‘], $_FILES[’snowtok_file‘][’name‘]);
$allowed = [‚mp4’=>’video/mp4‘, ‚webm’=>’video/webm‘, ‚ogg’=>’video/ogg‘];
if (!in_array($filetype[‚type‘], $allowed, true)) {
wp_send_json([’success’=>false,’message’=>’Nur MP4/WEBM/OGG erlaubt.‘], 400);
}
// Hochladen
$overrides = [‚test_form’=>false, ‚mimes’=>$allowed];
$movefile = wp_handle_upload($_FILES[’snowtok_file‘], $overrides);
if (isset($movefile[‚error‘])) {
wp_send_json([’success’=>false,’message’=>’Upload fehlgeschlagen: ‚.$movefile[‚error‘]], 500);
}
$title = sanitize_text_field($_POST[’snowtok_title‘] ?? “);
$desc = sanitize_textarea_field($_POST[’snowtok_desc‘] ?? “);
$url = esc_url_raw($movefile[‚url‘]);
$file = $movefile[‚file‘];
$type = $movefile[‚type‘];
// Attachment anlegen
$attachment = [
‚post_mime_type‘ => $type,
‚post_title‘ => $title ?: basename($file),
‚post_content‘ => “,
‚post_status‘ => ‚inherit‘
];
$attach_id = wp_insert_attachment($attachment, $file);
if (is_wp_error($attach_id)) {
wp_send_json([’success’=>false,’message’=>’Attachment-Fehler.‘], 500);
}
// Metadaten generieren (Video braucht keine Thumbs, aber harmless)
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
// SnowTok-Post anlegen (öffentlich)
$post_id = wp_insert_post([
‚post_type‘ => ’snowtok_video‘,
‚post_title‘ => $title ?: ‚Clip‘,
‚post_status‘ => ‚publish‘,
‚post_content’=> $url, // einfacher „Umweg“: URL im Content
‚post_excerpt’=> $desc
]);
if (is_wp_error($post_id)) {
wp_send_json([’success’=>false,’message’=>’Post konnte nicht erstellt werden.‘], 500);
}
// Optional: URL zusätzlich als Meta
update_post_meta($post_id, ‚_snowtok_video_url‘, $url);
wp_send_json([’success’=>true,’message’=>’Upload OK‘,’post_id’=>$post_id]);
}
