Skip to content

Commit 2377ee6

Browse files
committed
Added Cloudflare support 🌟
1 parent 9320dfc commit 2377ee6

4 files changed

Lines changed: 76 additions & 24 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ This is currently under development and contains bugs. Test in a development env
2525

2626
This is a WordPress plugin that allows you to serve your WordPress Media Library files via the [Backblaze B2](https://www.backblaze.com/b2/cloud-storage.html#af9kre) cloud storage service.
2727

28+
### Features
29+
30+
- Limit offloading by MIME types
31+
- Option to remove files from origin server
32+
- [Shortcode](https://github.com/cloudverve/wordpress-cloud-media-offloader-plugin/wiki/Shortcodes) to create hyperlinks
33+
- :new: CloudFlare support ([more info](https://www.backblaze.com/blog/backblaze-and-cloudflare-partner-to-provide-free-data-transfer/))
34+
2835
### Contributing
2936

3037
One of the best ways that you can contribute is to help me make it better, either with code or with constructive feedback. Ways to help:

app/Core.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ public function add_attachment_filter( $attachment_id ) {
7171
}
7272

7373
// Get upload filename
74-
$url = self::$client->getDownloadUrl( [ 'BucketName' => $bucket_name, 'FileName' => $file['destfile'] ] );
74+
$custom_url = rtrim( trim( $this->get_carbon_plugin_option( 'custom_url' ) ), '/' );
75+
if( $this->get_carbon_plugin_option( 'enable_custom_url' ) && trim( $custom_url ) ) {
76+
$url = sprintf( '%s/file/%s/%s', $custom_url, $bucket_name, $file['destfile'] );
77+
} else {
78+
$url = self::$client->getDownloadUrl( [ 'BucketName' => $bucket_name, 'FileName' => $file['destfile'] ] );
79+
}
7580

7681
// Set upload name
7782
update_post_meta( $attachment_id, self::prefix( 'external_url' ), $url );

app/Settings/Settings_Page.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace CloudVerve\MediaOffloader\Settings;
33
use CloudVerve\MediaOffloader\Plugin;
44
use CloudVerve\MediaOffloader\Helpers;
5-
use CloudVerve\MediaOffloader\Services\B2;
5+
use CloudVerve\MediaOffloader\Provider\B2;
66
use Carbon_Fields\Datastore\Datastore\Serialized_Theme_Options_Datastore;
77
use Carbon_Fields\Container;
88
use Carbon_Fields\Field;
@@ -71,7 +71,6 @@ public function create_tabbed_options_page() {
7171
Field::make( 'text', $this->prefix( 'account_id' ), __( 'Account ID', self::$textdomain ) )
7272
->set_classes( 'cmo-field-length-small' ),
7373
Field::make( 'text', $this->prefix( 'application_key' ), __( 'Master Application Key', self::$textdomain ) )
74-
->help_text( __( 'These values can be found by clicking the <em>Show Account ID and Application Key</em> link at the top of the bucket list page in the B2 control panel.', self::$textdomain ) )
7574
->set_classes( 'cmo-field-length-small' )
7675
->set_attribute( 'type', 'password' ),
7776
Field::make( 'separator', $this->prefix( 'separator_general_bucket_path' ), __( 'Bucket & Path', self::$textdomain ) )
@@ -95,6 +94,17 @@ public function create_tabbed_options_page() {
9594
->set_default_value( 'wp-content/uploads/' )
9695
]);
9796

97+
$general_fields = array_merge( $general_fields, [
98+
Field::make( 'checkbox', $this->prefix( 'enable_custom_url' ), __( 'Enable Custom URL', self::$textdomain ) )
99+
->help_text( sprintf( __( '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>).', self::$textdomain ), 'https://www.backblaze.com/blog/backblaze-and-cloudflare-partner-to-provide-free-data-transfer/#af9kre', 'https://help.backblaze.com/hc/en-us/articles/217666928-Using-Backblaze-B2-with-the-Cloudflare-CDN#af9kre' ) ),
100+
Field::make( 'text', $this->prefix( 'custom_url' ), __( 'Custom URL/Alias', self::$textdomain ) )
101+
->set_attribute( 'placeholder', __( 'Example:', self::$textdomain ) . ' ' . $this->get_root_domain( 'cdn' ) )
102+
->set_conditional_logic([[
103+
'field' => $this->prefix( 'enable_custom_url' ),
104+
'value' => true
105+
]])
106+
]);
107+
98108
$container = Container::make( 'theme_options', self::$config->get( 'short_name' ) )
99109
->set_page_parent('options-general.php')
100110
->add_tab( __( 'General', self::$textdomain ), $general_fields
@@ -223,4 +233,22 @@ public function inject_custom_css() {
223233

224234
}
225235

236+
/**
237+
* Gets root domain of current site
238+
*
239+
* @param string $prepend Sub-domain to prepend
240+
* @return string
241+
* @since 0.8.1
242+
*/
243+
private function get_root_domain( $prepend = '', $include_scheme = true ) {
244+
245+
$site_url = site_url();
246+
$domain = ltrim( parse_url( $site_url, PHP_URL_HOST ), 'www.' );
247+
$scheme = parse_url( $site_url, PHP_URL_SCHEME );
248+
if( $prepend ) $domain = $prepend . '.' . $domain;
249+
if( $include_scheme ) $domain = $scheme . '://' . $domain . '/';
250+
return $domain;
251+
252+
}
253+
226254
}

languages/cloud-media-offloader.pot

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ msgstr ""
1313
"X-Poedit-SourceCharset: UTF-8\n"
1414
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
1515

16-
#: ../app/Core.php:190
16+
#: ../app/Core.php:195
1717
msgid "Documents/Archives"
1818
msgstr ""
1919

20-
#: ../app/Core.php:190
20+
#: ../app/Core.php:195
2121
msgid "Manage Documents/Archives"
2222
msgstr ""
2323

24-
#: ../app/Core.php:190
24+
#: ../app/Core.php:195
2525
msgid "Document/Archivee"
2626
msgid_plural "Documents/Archives <span class=\"count\">(%s)</span>"
2727
msgstr[0] ""
@@ -123,7 +123,7 @@ msgid "Account ID"
123123
msgstr ""
124124

125125
#: ../app/Settings/Settings_Page.php:73
126-
msgid "Application Key"
126+
msgid "Master Application Key"
127127
msgstr ""
128128

129129
#: ../app/Settings/Settings_Page.php:76
@@ -150,59 +150,71 @@ msgstr ""
150150
msgid "Path"
151151
msgstr ""
152152

153-
#: ../app/Settings/Settings_Page.php:101, ../app/Settings/Settings_Page.php:114
153+
#: ../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>."
155+
msgstr ""
156+
157+
#: ../app/Settings/Settings_Page.php:98
158+
msgid "Enable Custom URL"
159+
msgstr ""
160+
161+
#: ../app/Settings/Settings_Page.php:101, ../app/Settings/Settings_Page.php:119, ../app/Settings/Settings_Page.php:121
162+
msgid "Example:"
163+
msgstr ""
164+
165+
#: ../app/Settings/Settings_Page.php:100
166+
msgid "Custom URL/Alias"
167+
msgstr ""
168+
169+
#: ../app/Settings/Settings_Page.php:112, ../app/Settings/Settings_Page.php:125
154170
msgid "MIME Types"
155171
msgstr ""
156172

157-
#: ../app/Settings/Settings_Page.php:103
173+
#: ../app/Settings/Settings_Page.php:114
158174
msgid "If checked, uploads to Backblaze B2 are limited to specific MIME types."
159175
msgstr ""
160176

161-
#: ../app/Settings/Settings_Page.php:102
177+
#: ../app/Settings/Settings_Page.php:113
162178
msgid "Limit to Specific MIME Types"
163179
msgstr ""
164180

165-
#: ../app/Settings/Settings_Page.php:119
181+
#: ../app/Settings/Settings_Page.php:130
166182
msgid "Add New"
167183
msgstr ""
168184

169-
#: ../app/Settings/Settings_Page.php:114, ../app/Settings/Settings_Page.php:109
185+
#: ../app/Settings/Settings_Page.php:125, ../app/Settings/Settings_Page.php:120
170186
msgid "MIME Type"
171187
msgstr ""
172188

173-
#: ../app/Settings/Settings_Page.php:112
189+
#: ../app/Settings/Settings_Page.php:123
174190
msgid "Add extra MIME types that are not listed below."
175191
msgstr ""
176192

177-
#: ../app/Settings/Settings_Page.php:112
193+
#: ../app/Settings/Settings_Page.php:123
178194
msgid "Examples"
179195
msgstr ""
180196

181-
#: ../app/Settings/Settings_Page.php:108, ../app/Settings/Settings_Page.php:110
182-
msgid "Example:"
183-
msgstr ""
184-
185-
#: ../app/Settings/Settings_Page.php:107
197+
#: ../app/Settings/Settings_Page.php:118
186198
msgid "Extension/Label"
187199
msgstr ""
188200

189-
#: ../app/Settings/Settings_Page.php:104
201+
#: ../app/Settings/Settings_Page.php:115
190202
msgid "Custom MIME Types"
191203
msgstr ""
192204

193-
#: ../app/Settings/Settings_Page.php:121
205+
#: ../app/Settings/Settings_Page.php:132
194206
msgid "Registers custom MIME types (if specified)."
195207
msgstr ""
196208

197-
#: ../app/Settings/Settings_Page.php:120
209+
#: ../app/Settings/Settings_Page.php:131
198210
msgid "Register Custom MIME Types"
199211
msgstr ""
200212

201-
#: ../app/Settings/Settings_Page.php:128
213+
#: ../app/Settings/Settings_Page.php:139
202214
msgid "Built-in MIME Types"
203215
msgstr ""
204216

205-
#: ../app/Settings/Settings_Page.php:99
217+
#: ../app/Settings/Settings_Page.php:110
206218
msgid "General"
207219
msgstr ""
208220

0 commit comments

Comments
 (0)