/** * Get sermon data * * @since 0.9 * @param int $post_id Post ID to get data for; null for current post * @return array Sermon data */ function ctfw_sermon_data($post_id = null) { // Get URL to upload directory $upload_dir = wp_upload_dir(); $upload_dir_url = $upload_dir['baseurl']; // Get meta values $data = ctfw_get_meta_data(array('video', 'audio', 'pdf', 'has_full_text'), $post_id); // Get media player code // Embed code generated from uploaded file, URL for file on other site, page on oEmbed-supported site, or manual embed code (HTML or shortcode) $data['video_player'] = ctfw_embed_code($data['video']); $data['audio_player'] = ctfw_embed_code($data['audio']); // Get file data for media // Path and size will be populated for local files only $media_types = array('audio', 'video', 'pdf'); foreach ($media_types as $media_type) { $data[$media_type . '_extension'] = ''; $data[$media_type . '_path'] = ''; $data[$media_type . '_size_bytes'] = ''; $data[$media_type . '_size'] = ''; // Get extension // This can be determined for local and external files // Empty for YouTube, SoundCloud, etc. $filetype = wp_check_filetype($data[$media_type]); $data[$media_type . '_extension'] = $filetype['ext']; // File is local, so can get path and size if ($data[$media_type] && ctfw_is_local_url($data[$media_type])) { // Local path $data[$media_type . '_path'] = $upload_dir['basedir'] . str_replace($upload_dir_url, '', $data[$media_type]); // Exists? if (!file_exists($data[$media_type . '_path'])) { $data[$media_type . '_path'] = ''; // clear it } else { // File type $filetype = wp_check_filetype($data[$media_type]); $data[$media_type . '_extension'] = $filetype['ext']; // File size $data[$media_type . '_size_bytes'] = filesize($data[$media_type . '_path']); $data[$media_type . '_size'] = size_format($data[$media_type . '_size_bytes']); // 30 MB, 2 GB, 220 kB, etc. } } } // Get download URL's // URL is returned if is local or external and has an extension. // Those without an extension (YouTube, SoundCloud, etc. page URL) return empty (nothing to download). // If locally hosted, URL is changed to force "Save As" via headers. // Use <a href="" download="download"> to attempt Save As via limited browser support for externally hosted files. $data['video_download_url'] = ctfw_download_url($data['video']); $data['audio_download_url'] = ctfw_download_url($data['audio']); $data['pdf_download_url'] = ctfw_download_url($data['pdf']); // Has at least one downloadable file URL? $data['has_download'] = false; if ($data['video_download_url'] || $data['audio_download_url'] || $data['pdf_download_url']) { // path empty if doesn't exist $data['has_download'] = true; } // Return filtered return apply_filters('ctfw_sermon_data', $data); }
/** * Convert download URL to one that forces "Save As" via headers * * This keeps the browser from doing what it wants with the file (such as play MP3 or show PDF). * Note that file must be in uploads folder and extension must be allowed by WordPress. * * See ctfw_download_url() which uses this. Use it with download="download" attribute as fallback. * This function will be deprecated when near 100% browser support exists for the attribute. * * Makes this: http://yourname.com/?download=%2F2009%2F10%2Ffile.pdf * Out of: http://yourname.com/wp-content/uploads/2013/05/file.pdf * http://yourname.com/wp-content/uploads/sites/6/2013/05/file.pdf (multisite) * * @since 0.9 * @param string $url URL for file * @return string URL forcing "Save As" on file if local */ function ctfw_force_download_url($url) { // In case URL is not local or feature not supported by theme $download_url = $url; // Theme supports this? if (current_theme_supports('ctfw-force-downloads')) { // Is URL local? if (ctfw_is_local_url($url)) { // Get URL to upload directory $upload_dir = wp_upload_dir(); $upload_dir_url = $upload_dir['baseurl']; // Get relative URL for file $relative_url = str_replace($upload_dir_url, '', $url); // remove base URL $relative_url = ltrim($relative_url); // remove preceding slash // Is it actually relative? // If file is outside of upload directory, it won't be // And in that case it cannot be piped through ?download if (!preg_match('/\\:\\/\\//', $relative_url)) { // Add ?download=file to site URL $download_url = home_url('/') . '?download=' . urlencode($relative_url) . '&nocache'; } } } return apply_filters('ctfw_force_download_url', $download_url, $url); }