|
| 1 | +<?php |
| 2 | +namespace CloudVerve\MediaOffloader\Shortcodes; |
| 3 | +use CloudVerve\MediaOffloader\Plugin; |
| 4 | +use CloudVerve\MediaOffloader\Provider\B2; |
| 5 | +use ChrisWhite\B2\Exceptions; |
| 6 | + |
| 7 | +class B2_Object_Shortcode extends Plugin |
| 8 | +{ |
| 9 | + |
| 10 | + public function __construct() { |
| 11 | + |
| 12 | + if( !self::$client ) self::$client = B2::auth(); |
| 13 | + |
| 14 | + // Usage example: [b2_object bucket="my-bucket" object="wp-conteent/uploads/example.pdf"]Example File[/b2_object] |
| 15 | + if ( ! shortcode_exists( 'b2_object' ) ) { |
| 16 | + add_shortcode( 'b2_object', array( $this, 'b2_object_shortcode' ) ); |
| 17 | + } |
| 18 | + |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * A short code that returns "Hello {$name}!", if provided |
| 23 | + * |
| 24 | + * @param $atts array Shortcode Attributes |
| 25 | + * @return string Output of shortcode |
| 26 | + * @since 0.8.0 |
| 27 | + */ |
| 28 | + public function b2_object_shortcode( $atts, $content = null ) { |
| 29 | + $atts = shortcode_atts( [ |
| 30 | + 'bucket' => null, |
| 31 | + 'object' => null, |
| 32 | + 'silent' => false, |
| 33 | + 'output' => null, // image, link, url |
| 34 | + 'title' => null |
| 35 | + ], $atts, 'b2_object' ); |
| 36 | + |
| 37 | + // Validate inputs |
| 38 | + $atts['silent'] = filter_var( $atts['silent'], FILTER_VALIDATE_BOOLEAN ); |
| 39 | + $content = trim( $content ); |
| 40 | + $atts['output'] = strtolower( $atts['output'] ); |
| 41 | + if( $atts['output'] == 'link' && !$content ) $atts['output'] = 'url'; |
| 42 | + if( !$atts['output'] && $content ) $atts['output'] = 'image'; |
| 43 | + $atts['title'] = trim( $atts['title'] ) ? ' title="' . esc_attr( trim( $atts['title'] ) ) . '"' : ''; |
| 44 | + |
| 45 | + // Get bucket object |
| 46 | + $bucket = $atts['bucket'] ? B2::get_bucket_by_name( $atts['bucket'] ) : B2::get_bucket_by_id( $this->get_carbon_plugin_option( 'bucket_id' ) ); |
| 47 | + if( empty( $atts['object'] ) || !$bucket ) return $atts['silent'] ? '' : __( 'Invalid bucket', self::$textdomain ); |
| 48 | + |
| 49 | + // Get file object |
| 50 | + try { |
| 51 | + $file_object = self::$client->getFile( [ 'BucketName' => $bucket['name'], 'FileName' => $atts['object'] ] ); |
| 52 | + } catch ( \ChrisWhite\B2\Exceptions\NotFoundException $e ) { |
| 53 | + return $atts['silent'] ? '' : __( 'Object not found', self::$textdomain ); |
| 54 | + } |
| 55 | + |
| 56 | + // Get object URL |
| 57 | + $custom_url = rtrim( trim( $this->get_carbon_plugin_option( 'custom_url' ) ), '/' ); |
| 58 | + if( $this->get_carbon_plugin_option( 'enable_custom_url' ) && trim( $custom_url ) ) { |
| 59 | + $url = sprintf( '%s/file/%s/%s', $custom_url, $bucket['name'], $atts['object'] ); |
| 60 | + } else { |
| 61 | + $url = self::$client->getDownloadUrl( [ 'BucketName' => $bucket['name'], 'FileName' => $atts['object'] ] ); |
| 62 | + } |
| 63 | + |
| 64 | + // Create hyperlink |
| 65 | + if( $content && $atts['output'] != 'url' ) { |
| 66 | + switch( $atts['output'] ) { |
| 67 | + case 'image': |
| 68 | + return sprintf( '<img src="%s" alt="%s"%s />', esc_attr( $url ), esc_attr( $content ), $atts['title'] ); |
| 69 | + default: |
| 70 | + return sprintf( '<a href="%s">%s</a>', esc_attr( $url ), do_shortcode( $content ) ); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return $url; |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments