| 1 |
<?php |
| 2 |
|
| 3 |
use Optimole\Sdk\Optimole; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Optml_Config holds configuration for the service. |
| 7 |
* |
| 8 |
* @package \Optml\Inc |
| 9 |
* @author Optimole <friends@optimole.com> |
| 10 |
*/ |
| 11 |
class Optml_Config { |
| 12 |
|
| 13 |
/** |
| 14 |
* A list of allowed extensions. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
public static $image_extensions = [ |
| 19 |
'jpg' => 'image/jpeg', |
| 20 |
'jpeg' => 'image/jpeg', |
| 21 |
'jpe' => 'image/jpeg', |
| 22 |
'png' => 'image/png', |
| 23 |
'heic' => 'image/heif', |
| 24 |
'ico' => 'image/x-icon', |
| 25 |
'avif' => 'image/avif', |
| 26 |
'webp' => 'image/webp', |
| 27 |
'svg' => 'image/svg+xml', |
| 28 |
'gif' => 'image/gif', |
| 29 |
]; |
| 30 |
/** |
| 31 |
* CSS/Js mimetypes. |
| 32 |
* |
| 33 |
* @var string[] |
| 34 |
*/ |
| 35 |
public static $assets_extensions = [ |
| 36 |
'css' => 'text/css', |
| 37 |
'js' => 'text/javascript', |
| 38 |
]; |
| 39 |
/** |
| 40 |
* All extensions that Optimole process. |
| 41 |
* |
| 42 |
* @var string[] |
| 43 |
*/ |
| 44 |
public static $all_extensions = [ |
| 45 |
'jpg|jpeg|jpe' => 'image/jpeg', |
| 46 |
'png' => 'image/png', |
| 47 |
'webp' => 'image/webp', |
| 48 |
'ico' => 'image/x-icon', |
| 49 |
'heic' => 'image/heif', |
| 50 |
'avif' => 'image/avif', |
| 51 |
'svg' => 'image/svg+xml', |
| 52 |
'gif' => 'image/gif', |
| 53 |
'css' => 'text/css', |
| 54 |
'js' => 'text/javascript', |
| 55 |
]; |
| 56 |
/** |
| 57 |
* A string of allowed chars. |
| 58 |
* |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
|
| 62 |
public static $chars = '\/:,~\\\\.\-\–\d_@%^A-Za-z\p{L}\p{M}\p{N}\x{0080}-\x{017F}\x{2200}-\x{22FF}\x{2022}'; |
| 63 |
/** |
| 64 |
* Service api key. |
| 65 |
* |
| 66 |
* @var string Service key. |
| 67 |
*/ |
| 68 |
public static $key = ''; |
| 69 |
/** |
| 70 |
* Service secret key used for signing the requests. |
| 71 |
* |
| 72 |
* @var string Secret key. |
| 73 |
*/ |
| 74 |
public static $secret = ''; |
| 75 |
/** |
| 76 |
* Service url. |
| 77 |
* |
| 78 |
* @var string Service url. |
| 79 |
*/ |
| 80 |
public static $service_url = ''; |
| 81 |
/** |
| 82 |
* Base domain. |
| 83 |
* |
| 84 |
* @var string Base domain. |
| 85 |
*/ |
| 86 |
public static $base_domain = 'i.optimole.com'; |
| 87 |
|
| 88 |
/** |
| 89 |
* Service settings. |
| 90 |
* |
| 91 |
* @param array $service_settings Service settings. |
| 92 |
* |
| 93 |
* @throws \InvalidArgumentException In case that key or secret is not provided. |
| 94 |
*/ |
| 95 |
public static function init( $service_settings = [] ) { |
| 96 |
|
| 97 |
if ( empty( $service_settings['key'] ) && ! defined( 'OPTML_KEY' ) ) { |
| 98 |
throw new \InvalidArgumentException( 'Optimole SDK requires service api key.' ); // @codeCoverageIgnore |
| 99 |
} |
| 100 |
if ( empty( $service_settings['secret'] ) && ! defined( 'OPTML_SECRET' ) ) { |
| 101 |
throw new \InvalidArgumentException( 'Optimole SDK requires service secret key.' ); // @codeCoverageIgnore |
| 102 |
} |
| 103 |
|
| 104 |
if ( defined( 'OPTML_KEY' ) ) { |
| 105 |
self::$key = constant( 'OPTML_KEY' ); |
| 106 |
} |
| 107 |
|
| 108 |
if ( defined( 'OPTML_SECRET' ) ) { |
| 109 |
self::$secret = constant( 'OPTML_SECRET' ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( defined( 'OPTML_BASE_DOMAIN' ) ) { |
| 113 |
self::$base_domain = constant( 'OPTML_BASE_DOMAIN' ); |
| 114 |
} |
| 115 |
|
| 116 |
if ( ! empty( $service_settings['key'] ) ) { |
| 117 |
self::$key = trim( strtolower( $service_settings['key'] ) ); |
| 118 |
} |
| 119 |
|
| 120 |
if ( ! empty( $service_settings['secret'] ) ) { |
| 121 |
self::$secret = trim( $service_settings['secret'] ); |
| 122 |
} |
| 123 |
self::$service_url = sprintf( 'https://%s.%s', self::$key, self::$base_domain ); |
| 124 |
if ( defined( 'OPTML_CUSTOM_DOMAIN' ) ) { |
| 125 |
self::$service_url = constant( 'OPTML_CUSTOM_DOMAIN' ); |
| 126 |
} elseif ( isset( $service_settings['domain'] ) && ! empty( $service_settings['domain'] ) ) { |
| 127 |
self::$service_url = sprintf( 'https://%s', $service_settings['domain'] ); |
| 128 |
} |
| 129 |
|
| 130 |
$options = [ 'domain' => parse_url( self::$service_url, PHP_URL_HOST ) ]; |
| 131 |
|
| 132 |
if ( defined( 'OPTIML_API_ROOT' ) ) { |
| 133 |
$options['dashboard_api_url'] = OPTIML_API_ROOT; |
| 134 |
} |
| 135 |
|
| 136 |
if ( defined( 'OPTIML_UPLOAD_API_ROOT' ) ) { |
| 137 |
$options['upload_api_url'] = OPTIML_UPLOAD_API_ROOT; |
| 138 |
} |
| 139 |
|
| 140 |
if ( isset( $service_settings['key'], $service_settings['secret'], $service_settings['api_key'] ) ) { |
| 141 |
$options['upload_api_credentials'] = [ |
| 142 |
'userKey' => $service_settings['key'], |
| 143 |
'secret' => $service_settings['secret'], |
| 144 |
]; |
| 145 |
$options['dashboard_api_key'] = $service_settings['api_key']; |
| 146 |
} |
| 147 |
|
| 148 |
Optimole::init( self::$key, $options ); |
| 149 |
} |
| 150 |
} |
| 151 |
|