Plugins
This commit is contained in:
@@ -1,123 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Imagify\CDN;
|
||||
|
||||
use Imagify\EventManagement\SubscriberInterface;
|
||||
|
||||
/**
|
||||
* CDN subscriber
|
||||
*/
|
||||
class CDN implements SubscriberInterface {
|
||||
/**
|
||||
* Array of events this subscriber listens to
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'imagify_cdn_source_url' => 'get_cdn_source',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the CDN "source".
|
||||
*
|
||||
* @since 1.9.3
|
||||
*
|
||||
* @param string $option_url An URL to use instead of the one stored in the option. It is used only if no constant/filter.
|
||||
*
|
||||
* @return array {
|
||||
* @type string $source Where does it come from? Possible values are 'constant', 'filter', or 'option'.
|
||||
* @type string $name Who? Can be a constant name, a plugin name, or an empty string.
|
||||
* @type string $url The CDN URL, with a trailing slash. An empty string if no URL is set.
|
||||
* }
|
||||
*/
|
||||
public function get_cdn_source( $option_url = '' ) {
|
||||
if ( defined( 'IMAGIFY_CDN_URL' ) && IMAGIFY_CDN_URL && is_string( IMAGIFY_CDN_URL ) ) {
|
||||
// Use a constant.
|
||||
$source = [
|
||||
'source' => 'constant',
|
||||
'name' => 'IMAGIFY_CDN_URL',
|
||||
'url' => IMAGIFY_CDN_URL,
|
||||
];
|
||||
} else {
|
||||
// Maybe use a filter.
|
||||
$filter_source = [
|
||||
'name' => null,
|
||||
'url' => null,
|
||||
];
|
||||
|
||||
/**
|
||||
* Provide a custom CDN source.
|
||||
*
|
||||
* @since 1.9.3
|
||||
*
|
||||
* @param array $filter_source {
|
||||
* @type $name string The name of which provides the URL (plugin name, etc).
|
||||
* @type $url string The CDN URL.
|
||||
* }
|
||||
*/
|
||||
$filter_source = apply_filters( 'imagify_cdn_source', $filter_source );
|
||||
|
||||
if ( ! empty( $filter_source['url'] ) ) {
|
||||
$source = [
|
||||
'source' => 'filter',
|
||||
'name' => ! empty( $filter_source['name'] ) ? $filter_source['name'] : '',
|
||||
'url' => $filter_source['url'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $source['url'] ) ) {
|
||||
// No constant, no filter: use the option.
|
||||
$source = [
|
||||
'source' => 'option',
|
||||
'name' => '',
|
||||
'url' => $option_url && is_string( $option_url ) ? $option_url : get_imagify_option( 'cdn_url' ),
|
||||
];
|
||||
}
|
||||
|
||||
if ( empty( $source['url'] ) ) {
|
||||
// Nothing set.
|
||||
return [
|
||||
'source' => 'option',
|
||||
'name' => '',
|
||||
'url' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$source['url'] = $this->sanitize_cdn_url( $source['url'] );
|
||||
|
||||
if ( empty( $source['url'] ) ) {
|
||||
// Not an URL.
|
||||
return [
|
||||
'source' => 'option',
|
||||
'name' => '',
|
||||
'url' => '',
|
||||
];
|
||||
}
|
||||
|
||||
return $source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize the CDN URL value.
|
||||
*
|
||||
* @since 1.9.3
|
||||
*
|
||||
* @param string $url The URL to sanitize.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function sanitize_cdn_url( $url ) {
|
||||
$url = sanitize_text_field( $url );
|
||||
|
||||
if ( ! $url || ! preg_match( '@^https?://.+\.[^.]+@i', $url ) ) {
|
||||
// Not an URL.
|
||||
return '';
|
||||
}
|
||||
|
||||
return trailingslashit( $url );
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
<?php
|
||||
namespace Imagify\CDN;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Interface to use for Push CDNs.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
interface PushCDNInterface {
|
||||
|
||||
/**
|
||||
* Tell if the CDN is ready (not necessarily reachable).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_ready();
|
||||
|
||||
/**
|
||||
* Tell if the media is on the CDN.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function media_is_on_cdn();
|
||||
|
||||
/**
|
||||
* Get files from the CDN.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $file_paths A list of file paths.
|
||||
* @return bool|\WP_Error True on success. A \WP_error object on failure.
|
||||
*/
|
||||
public function get_files_from_cdn( $file_paths );
|
||||
|
||||
/**
|
||||
* Remove files from the CDN.
|
||||
* Don't use this to empty a folder.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $file_paths A list of file paths. Those paths are not necessary absolute, and can be also file names.
|
||||
* @return bool|\WP_Error True on success. A \WP_error object on failure.
|
||||
*/
|
||||
public function remove_files_from_cdn( $file_paths );
|
||||
|
||||
/**
|
||||
* Send all files from a media to the CDN.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $is_new_upload Tell if the current media is a new upload. If not, it means it's a media being regenerated, restored, etc.
|
||||
* @return bool|\WP_Error True/False if sent or not. A \WP_error object on failure.
|
||||
*/
|
||||
public function send_to_cdn( $is_new_upload );
|
||||
|
||||
/**
|
||||
* Get a file URL.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $file_name Name of the file. Leave empty for the full size file.
|
||||
* @return string URL to the file.
|
||||
*/
|
||||
public function get_file_url( $file_name = false );
|
||||
|
||||
/**
|
||||
* Get a file path.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $file_name Name of the file. Leave empty for the full size file. Use 'original' to get the path to the original file.
|
||||
* @return string Path to the file.
|
||||
*/
|
||||
public function get_file_path( $file_name = false );
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Imagify\CDN;
|
||||
|
||||
use Imagify\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
|
||||
|
||||
/**
|
||||
* Service provider for CDN compatibility
|
||||
*/
|
||||
class ServiceProvider extends AbstractServiceProvider {
|
||||
/**
|
||||
* Services provided by this provider
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $provides = [
|
||||
'cdn',
|
||||
];
|
||||
|
||||
/**
|
||||
* Subscribers provided by this provider
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $subscribers = [
|
||||
'cdn',
|
||||
];
|
||||
|
||||
/**
|
||||
* Registers the provided classes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register() {
|
||||
$this->getContainer()->share( 'cdn', CDN::class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the subscribers array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_subscribers() {
|
||||
return $this->subscribers;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user