| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Optml_Settings. |
| 5 |
*/ |
| 6 |
class Optml_Settings { |
| 7 |
|
| 8 |
/** |
| 9 |
* Default settings schema. |
| 10 |
* |
| 11 |
* @var array Settings schema. |
| 12 |
*/ |
| 13 |
private $default_schema = array( |
| 14 |
'api_key' => '', |
| 15 |
'service_data' => '', |
| 16 |
'admin_bar_item' => 'enabled', |
| 17 |
'quality' => 'auto', |
| 18 |
'image_replacer' => 'enabled', |
| 19 |
); |
| 20 |
/** |
| 21 |
* Option key. |
| 22 |
* |
| 23 |
* @var string Option name. |
| 24 |
*/ |
| 25 |
private $namespace; |
| 26 |
/** |
| 27 |
* Holds all options from db. |
| 28 |
* |
| 29 |
* @var array All options. |
| 30 |
*/ |
| 31 |
private $options; |
| 32 |
|
| 33 |
/** |
| 34 |
* Optml_Settings constructor. |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
$this->namespace = OPTML_NAMESPACE . '_settings'; |
| 38 |
$this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema ); |
| 39 |
if ( defined( 'OPTIML_ENABLED_MU' ) && OPTIML_ENABLED_MU && defined( 'OPTIML_MU_SITE_ID' ) && ! empty( OPTIML_MU_SITE_ID ) ) { |
| 40 |
switch_to_blog( OPTIML_MU_SITE_ID ); |
| 41 |
$this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema ); |
| 42 |
restore_current_blog(); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Check if the user is connected to Optimole. |
| 48 |
* |
| 49 |
* @return bool Connection status. |
| 50 |
*/ |
| 51 |
public function is_connected() { |
| 52 |
$service_data = $this->get( 'service_data' ); |
| 53 |
if ( ! isset( $service_data['cdn_key'] ) ) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
if ( empty( $service_data ['cdn_key'] ) || empty( $service_data['cdn_secret'] ) ) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get setting value by key. |
| 65 |
* |
| 66 |
* @param string $key Key to search against. |
| 67 |
* |
| 68 |
* @return mixed|null Setting value. |
| 69 |
*/ |
| 70 |
public function get( $key ) { |
| 71 |
if ( ! $this->is_allowed( $key ) ) { |
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
return isset ( $this->options[ $key ] ) ? $this->options[ $key ] : ''; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Check if key is allowed. |
| 80 |
* |
| 81 |
* @param string $key Is key allowed or not. |
| 82 |
* |
| 83 |
* @return bool Is key allowed or not. |
| 84 |
*/ |
| 85 |
private function is_allowed( $key ) { |
| 86 |
return isset( $this->default_schema[ $key ] ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Check if replacer is enabled. |
| 91 |
* |
| 92 |
* @return bool Replacer enabled |
| 93 |
*/ |
| 94 |
public function is_enabled() { |
| 95 |
$status = $this->get( 'image_replacer' ); |
| 96 |
if ( $status === 'disabled' ) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
if ( empty( $status ) ) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Return cdn url. |
| 108 |
* |
| 109 |
* @return string CDN url. |
| 110 |
*/ |
| 111 |
public function get_cdn_url() { |
| 112 |
$service_data = $this->get( 'service_data' ); |
| 113 |
if ( ! isset( $service_data['cdn_key'] ) ) { |
| 114 |
return ''; |
| 115 |
} |
| 116 |
|
| 117 |
return sprintf( |
| 118 |
'%s.%s', |
| 119 |
strtolower( $service_data['cdn_key'] ), |
| 120 |
'i.optimole.com' |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Reset options to defaults. |
| 126 |
* |
| 127 |
* @return bool Reset action status. |
| 128 |
*/ |
| 129 |
public function reset() { |
| 130 |
|
| 131 |
$update = update_option( $this->namespace, $this->default_schema ); |
| 132 |
if ( $update ) { |
| 133 |
$this->options = $this->default_schema; |
| 134 |
} |
| 135 |
|
| 136 |
return $update; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Update settings. |
| 141 |
* |
| 142 |
* @param string $key Settings key. |
| 143 |
* @param mixed $value Settings value. |
| 144 |
* |
| 145 |
* @return bool Update result. |
| 146 |
*/ |
| 147 |
public function update( $key, $value ) { |
| 148 |
if ( ! $this->is_allowed( $key ) ) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
// If we try to update from a website which is not the main OPTML blog, bail. |
| 152 |
if ( defined( 'OPTIML_ENABLED_MU' ) && OPTIML_ENABLED_MU && defined( 'OPTIML_MU_SITE_ID' ) && ! empty( OPTIML_MU_SITE_ID ) ) { |
| 153 |
if ( intval( OPTIML_MU_SITE_ID ) !== get_current_blog_id() ) { |
| 154 |
return $this->options; |
| 155 |
} |
| 156 |
} |
| 157 |
$options = $this->options; |
| 158 |
$options[ $key ] = $value; |
| 159 |
$update = update_option( $this->namespace, $options ); |
| 160 |
if ( $update ) { |
| 161 |
$this->options = $options; |
| 162 |
} |
| 163 |
|
| 164 |
return $update; |
| 165 |
} |
| 166 |
|
| 167 |
} |
| 168 |
|