forked from humanmade/S3-Uploads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3-uploads.php
115 lines (95 loc) · 2.89 KB
/
s3-uploads.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/*
Plugin Name: S3 Uploads
Description: Store uploads in S3
Author: Human Made Limited
Version: 2.0.0-beta2
Author URI: http://hmn.md
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once dirname( __FILE__ ) . '/inc/class-s3-uploads-wp-cli-command.php';
}
add_action( 'plugins_loaded', 's3_uploads_init' );
function s3_uploads_init() {
// Ensure the AWS SDK can be loaded.
if ( ! class_exists( '\\Aws\\S3\\S3Client' ) ) {
// Require AWS Autoloader file.
require_once dirname( __FILE__ ) . '/lib/aws-sdk/aws-autoloader.php';
}
if ( ! s3_uploads_check_requirements() ) {
return;
}
if ( ! defined( 'S3_UPLOADS_BUCKET' ) ) {
return;
}
if ( ( ! defined( 'S3_UPLOADS_KEY' ) || ! defined( 'S3_UPLOADS_SECRET' ) ) && ! defined( 'S3_UPLOADS_USE_INSTANCE_PROFILE' ) ) {
return;
}
if ( ! s3_uploads_enabled() ) {
return;
}
if ( ! defined( 'S3_UPLOADS_REGION' ) ) {
wp_die( 'S3_UPLOADS_REGION constant is required. Please define it in your wp-config.php' );
}
$instance = S3_Uploads::get_instance();
$instance->setup();
}
/**
* Check whether the environment meets the plugin's requirements, like the minimum PHP version.
*
* @return bool True if the requirements are met, else false.
*/
function s3_uploads_check_requirements() {
if ( version_compare( '5.5.0', PHP_VERSION, '>' ) ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
add_action( 'admin_notices', 's3_uploads_outdated_php_version_notice' );
}
return false;
}
return true;
}
/**
* Print an admin notice when the PHP version is not high enough.
*
* This has to be a named function for compatibility with PHP 5.2.
*/
function s3_uploads_outdated_php_version_notice() {
printf( '<div class="error"><p>The S3 Uploads plugin requires PHP version 5.5.0 or higher. Your server is running PHP version %s.</p></div>',
PHP_VERSION
);
}
/**
* Check if URL rewriting is enabled.
*
* Define S3_UPLOADS_AUTOENABLE to false in your wp-config to disable, or use the
* s3_uploads_enabled option.
*
* @return bool
*/
function s3_uploads_enabled() {
// Make sure the plugin is enabled when autoenable is on
$constant_autoenable_off = ( defined( 'S3_UPLOADS_AUTOENABLE' ) && false === S3_UPLOADS_AUTOENABLE );
if ( $constant_autoenable_off && 'enabled' !== get_option( 's3_uploads_enabled' ) ) { // If the plugin is not enabled, skip
return false;
}
return true;
}
/**
* Autoload callback.
*
* @param $class_name Name of the class to load.
*/
function s3_uploads_autoload( $class_name ) {
/*
* Load plugin classes:
* - Class name: S3_Uploads_Image_Editor_Imagick.
* - File name: class-s3-uploads-image-editor-imagick.php.
*/
$class_file = 'class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php';
$class_path = dirname( __FILE__ ) . '/inc/' . $class_file;
if ( file_exists( $class_path ) ) {
require $class_path;
return;
}
}
spl_autoload_register( 's3_uploads_autoload' );