cache.inc.php
1 month ago
landing.inc.php
1 month ago
skingenerator.inc.php
1 month ago
tools.inc.php
1 month ago
tools.inc.php
60 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! class_exists( 'WooCommerceMusicPlayerTools' ) ) { |
| 4 | class WooCommerceMusicPlayerTools { |
| 5 | |
| 6 | public static function get_google_drive_download_url( $url ) { |
| 7 | // Match different possible Google Drive URL patterns |
| 8 | $patterns = [ |
| 9 | '/drive\.google\.com\/file\/d\/([a-zA-Z0-9_-]+)/i', // format: /file/d/FILE_ID/ |
| 10 | '/drive\.google\.com\/open\?id=([a-zA-Z0-9_-]+)/i', // format: /open?id=FILE_ID |
| 11 | '/drive\.google\.com\/uc\?id=([a-zA-Z0-9_-]+)/i' // format: /uc?id=FILE_ID |
| 12 | ]; |
| 13 | |
| 14 | foreach ($patterns as $pattern) { |
| 15 | if (preg_match($pattern, $url, $matches)) { |
| 16 | $fileId = $matches[1]; |
| 17 | return "https://drive.google.com/uc?export=download&id={$fileId}"; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | // Return original URL if it's not a recognized format |
| 22 | return $url; |
| 23 | } // End preprocess_google_drive_url |
| 24 | |
| 25 | public static function get_google_drive_file_name( $url ) { |
| 26 | $download_url = self::get_google_drive_download_url( $url ); |
| 27 | |
| 28 | $pattern = '/drive\.google\.com\/uc\?export\=download&id\=[a-zA-Z0-9_-]+/i'; |
| 29 | |
| 30 | try { |
| 31 | if ( |
| 32 | preg_match( $pattern, $download_url, $matches ) |
| 33 | ) { |
| 34 | |
| 35 | // Trying to obtain the file information directly from Google Drive. |
| 36 | $response = wp_remote_head( $download_url, [ |
| 37 | 'redirection' => 5, |
| 38 | 'timeout' => 15, |
| 39 | ]); |
| 40 | |
| 41 | if ( ! is_wp_error( $response ) ) { |
| 42 | $headers = wp_remote_retrieve_headers($response); |
| 43 | |
| 44 | // Check for Content-Disposition header |
| 45 | if ( ! empty( $headers['content-disposition'] ) ) { |
| 46 | if ( preg_match( '/filename="([^"]+)"/', $headers['content-disposition'], $matches ) ) { |
| 47 | return $matches[1]; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } catch ( Exception $err ) { |
| 53 | error_log( $err ); |
| 54 | } |
| 55 | |
| 56 | return $url; |
| 57 | } // En get_google_drive_file_name |
| 58 | |
| 59 | } // End WooCommerceMusicPlayerTools |
| 60 | } |