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