| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
if (class_exists('Updraft_Abstract_Logger')) return; |
| 6 |
|
| 7 |
require_once 'class-updraft-log-levels.php'; |
| 8 |
require_once 'class-updraft-logger-interface.php'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Updraft_Abstract_Logger |
| 12 |
*/ |
| 13 |
abstract class Updraft_Abstract_Logger implements Updraft_Logger_Interface { |
| 14 |
|
| 15 |
/** |
| 16 |
* True if logger enabled. |
| 17 |
* |
| 18 |
* @var bool |
| 19 |
*/ |
| 20 |
protected $enabled = true; |
| 21 |
|
| 22 |
/** |
| 23 |
* True if possible to add multiple loggers. |
| 24 |
* |
| 25 |
* @var bool |
| 26 |
*/ |
| 27 |
protected $allow_multiple = false; |
| 28 |
|
| 29 |
/** |
| 30 |
* Logger options. |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
protected $options = array(); |
| 35 |
|
| 36 |
/** |
| 37 |
* Updraft_Abstract_Logger constructor |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* True if logger is available for use, i.e. required plugins installed. |
| 44 |
* |
| 45 |
* @return bool |
| 46 |
*/ |
| 47 |
public function is_available() { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns true if allow multiple. |
| 53 |
* |
| 54 |
* @return bool |
| 55 |
*/ |
| 56 |
public function is_allow_multiple() { |
| 57 |
return $this->allow_multiple; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns current options in text. |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
public function get_options_text() { |
| 66 |
$options_values = $this->get_options_values(); |
| 67 |
|
| 68 |
if (array_key_exists('active', $options_values)) { |
| 69 |
unset($options_values['active']); |
| 70 |
} |
| 71 |
|
| 72 |
return join(', ', $options_values); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns list of logger options. |
| 77 |
* |
| 78 |
* @return array |
| 79 |
*/ |
| 80 |
public function get_options_list() { |
| 81 |
return array( |
| 82 |
// 'option_name' => __('Placeholder', 'wp-optimize') |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns array with options values list. |
| 88 |
* |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
public function get_options_values() { |
| 92 |
$options_values = array(); |
| 93 |
$options_list = $this->get_options_list(); |
| 94 |
|
| 95 |
if (empty($options_list)) return $options_values; |
| 96 |
|
| 97 |
foreach (array_keys($options_list) as $option_name) { |
| 98 |
$options_values[$option_name] = $this->get_option($option_name); |
| 99 |
} |
| 100 |
|
| 101 |
return $options_values; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns true if logger is active |
| 106 |
* |
| 107 |
* @return boolean |
| 108 |
*/ |
| 109 |
public function is_enabled() { |
| 110 |
return $this->enabled; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Enable logger |
| 115 |
*/ |
| 116 |
public function enable() { |
| 117 |
$this->enabled = true; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Disable logger |
| 122 |
*/ |
| 123 |
public function disable() { |
| 124 |
$this->enabled = false; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Return option $name value |
| 129 |
* |
| 130 |
* @param string $name Name of the option. |
| 131 |
* @param mixed $default Add default value. |
| 132 |
* @return mixed The value of the option. |
| 133 |
*/ |
| 134 |
public function get_option($name, $default = '') { |
| 135 |
if (!array_key_exists($name, $this->options)) return $default; |
| 136 |
return $this->options[$name]; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Set option $name value. |
| 141 |
* |
| 142 |
* @param mixed $name The name of the option. |
| 143 |
* @param mixed $value The value of the option. |
| 144 |
*/ |
| 145 |
public function set_option($name, $value = '') { |
| 146 |
if (is_array($name)) { |
| 147 |
$this->options = array_merge($this->options, $name); |
| 148 |
} else { |
| 149 |
$this->options[$name] = $value; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Returns logger description |
| 155 |
* |
| 156 |
* @return mixed |
| 157 |
*/ |
| 158 |
public abstract function get_description(); |
| 159 |
|
| 160 |
/** |
| 161 |
* For the Logger: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md |
| 162 |
* |
| 163 |
* @param string $message |
| 164 |
* @param array $context |
| 165 |
* @return string |
| 166 |
*/ |
| 167 |
protected function interpolate($message, $context = array()) { |
| 168 |
$replace = array(); |
| 169 |
foreach ($context as $key => $val) { |
| 170 |
// Check that the value can be cast to string. |
| 171 |
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
| 172 |
$replace['{' . $key . '}'] = $val; |
| 173 |
} |
| 174 |
} |
| 175 |
return strtr($message, $replace); |
| 176 |
} |
| 177 |
} |
| 178 |
|