Skip to content

Commit 402bd65

Browse files
committed
Fixed custom URL support in shortcode
1 parent 4ac5df6 commit 402bd65

4 files changed

Lines changed: 82 additions & 65 deletions

File tree

app/Shortcodes/B2_Link_Shortcode.php

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

app/Shortcodes/Shortcode_Loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Shortcode_Loader extends Plugin {
1313
public function __construct() {
1414

1515
$this->shortcodes = array(
16-
B2_Link_Shortcode::class
16+
B2_Object_Shortcode::class
1717
);
1818

1919
foreach( $this->shortcodes as $shortcodeClass ) {

languages/cloud-media-offloader.pot

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ msgid "Path"
151151
msgstr ""
152152

153153
#: ../app/Settings/Settings_Page.php:99
154-
msgid "Allows you to provide a custom URL/alias to replace the standard endpoint link. Useful when using a CDN in front of B2, such as <a href=\"%s\" target=\"_blank\">CloudFlare</a>."
154+
msgid "Allows you to provide a custom URL/alias to replace the standard endpoint link. Useful when using a CDN in front of B2, such as <a href=\"%s\" target=\"_blank\">Cloudflare</a> (<a href=\"%s\" target=\"_blank\">setup instructions</a>)."
155155
msgstr ""
156156

157157
#: ../app/Settings/Settings_Page.php:98
@@ -218,10 +218,10 @@ msgstr ""
218218
msgid "General"
219219
msgstr ""
220220

221-
#: ../app/Shortcodes/B2_Link_Shortcode.php:40
221+
#: ../app/Shortcodes/B2_Object_Shortcode.php:47
222222
msgid "Invalid bucket"
223223
msgstr ""
224224

225-
#: ../app/Shortcodes/B2_Link_Shortcode.php:46
225+
#: ../app/Shortcodes/B2_Object_Shortcode.php:53
226226
msgid "Object not found"
227227
msgstr ""

0 commit comments

Comments
 (0)