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