// Tích hợp API DooStream function integrate_doostream_api($video_url) { $api_key = '204936hck7bjv63ovp1n22'; $api_url = "https://doodstream.com/api/upload/url?key=" . $api_key . "&url=" . urlencode($video_url); $response = wp_remote_get($api_url, array( 'timeout' => 30, 'sslverify' => false, )); if (is_wp_error($response)) { return $video_url; // Trả về URL gốc nếu lỗi } $data = json_decode(wp_remote_retrieve_body($response), true); if (isset($data['result']['filecode'])) { return "https://doodstream.com/e/" . $data['result']['filecode']; } return $video_url; } // Tích hợp API Voe.sx function integrate_voesx_api($video_url) { $api_key = 'q6dR5VgUDjnbtwq3F4XnZqkfXGrAc2snTfab6AtU5NNmKpguWP79Nl2RgNghEA58'; $api_url = "https://voe.sx/api/upload/url?key=" . $api_key . "&url=" . urlencode($video_url); $response = wp_remote_get($api_url, array( 'timeout' => 30, 'sslverify' => false, )); if (is_wp_error($response)) { return $video_url; // Trả về URL gốc nếu lỗi } $data = json_decode(wp_remote_retrieve_body($response), true); if (isset($data['result']['embed_url'])) { return $data['result']['embed_url']; } return $video_url; } // Shortcode cho DooStream add_shortcode('doostream', function($atts) { $atts = shortcode_atts(array('url' => ''), $atts); $embed_url = integrate_doostream_api($atts['url']); return '
'; }); // Shortcode cho Voe.sx add_shortcode('voesx', function($atts) { $atts = shortcode_atts(array('url' => ''), $atts); $embed_url = integrate_voesx_api($atts['url']); return '
'; }); // Bảo vệ và tối ưu iframe cũ add_filter('the_content', function($content) { global $post; $video_code = get_post_meta($post->ID, 'video_code', true); if (!empty($video_code)) { // Bọc iframe cũ trong div responsive $content = preg_replace('//i', '
', $content); // Thêm video_code nếu chưa có if (strpos($content, $video_code) === false) { $content = '
' . $video_code . '
' . $content; } } return $content; }); // Tự động tích hợp API khi lưu bài viết add_action('save_post', function($post_id) { if (isset($_POST['video_code']) && !wp_is_post_revision($post_id)) { $video_code = $_POST['video_code']; // Nếu video_code là URL, thử tích hợp với API if (filter_var($video_code, FILTER_VALIDATE_URL)) { $doostream_url = integrate_doostream_api($video_code); $voesx_url = integrate_voesx_api($video_code); if ($doostream_url !== $video_code) { update_post_meta($post_id, 'video_code', ''); } elseif ($voesx_url !== $video_code) { update_post_meta($post_id, 'video_code', ''); } } } }, 20); // Tối ưu hiệu suất add_action('wp_enqueue_scripts', function() { // Thêm lazy load wp_enqueue_script('lazysizes', 'https://cdnjs.cloudflare.com/ajax/libs/lazysizes/5.3.2/lazysizes.min.js', array(), '5.3.2', true); }, 99); // Lấy thumbnail từ API nếu cần add_filter('tube_getcustomfield', function($value, $field, $post_id) { if ($field === 'thumb' && empty($value)) { $video_code = get_post_meta($post_id, 'video_code', true); if (preg_match('/doodstream\.com\/e\/([a-zA-Z0-9]+)/', $video_code, $matches)) { $filecode = $matches[1]; $api_key = '204936hck7bjv63ovp1n22'; $api_url = "https://doodstream.com/api/file/info?key=" . $api_key . "&file_code=" . $filecode; $response = wp_remote_get($api_url, array('timeout' => 30, 'sslverify' => false)); if (!is_wp_error($response)) { $data = json_decode(wp_remote_retrieve_body($response), true); if (isset($data['result'][0]['thumbnail'])) { return $data['result'][0]['thumbnail']; } } } } return $value; }, 10, 3);