@@ -10,16 +10,16 @@ |
||
| 10 | 10 | |
| 11 | 11 | class Provider implements ProviderContract |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * @return void |
|
| 15 | - */ |
|
| 16 | - public function register(Application $app) |
|
| 17 | - { |
|
| 18 | - $app->bind(Application::class, $app); |
|
| 19 | - $app->singleton(Hooks::class); |
|
| 20 | - $app->singleton(OptionManager::class); |
|
| 21 | - $app->singleton(Translator::class); |
|
| 22 | - $app->singleton(Translation::class); |
|
| 23 | - $app->singleton(MainController::class); // this goes last |
|
| 24 | - } |
|
| 13 | + /** |
|
| 14 | + * @return void |
|
| 15 | + */ |
|
| 16 | + public function register(Application $app) |
|
| 17 | + { |
|
| 18 | + $app->bind(Application::class, $app); |
|
| 19 | + $app->singleton(Hooks::class); |
|
| 20 | + $app->singleton(OptionManager::class); |
|
| 21 | + $app->singleton(Translator::class); |
|
| 22 | + $app->singleton(Translation::class); |
|
| 23 | + $app->singleton(MainController::class); // this goes last |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -11,292 +11,292 @@ |
||
| 11 | 11 | |
| 12 | 12 | abstract class Container |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var static |
|
| 16 | - */ |
|
| 17 | - protected static $instance; |
|
| 14 | + /** |
|
| 15 | + * @var static |
|
| 16 | + */ |
|
| 17 | + protected static $instance; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var array[] |
|
| 21 | - */ |
|
| 22 | - protected $bindings = []; |
|
| 19 | + /** |
|
| 20 | + * @var array[] |
|
| 21 | + */ |
|
| 22 | + protected $bindings = []; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var array[] |
|
| 26 | - */ |
|
| 27 | - protected $buildStack = []; |
|
| 24 | + /** |
|
| 25 | + * @var array[] |
|
| 26 | + */ |
|
| 27 | + protected $buildStack = []; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @var array[] |
|
| 31 | - */ |
|
| 32 | - protected $instances = []; |
|
| 29 | + /** |
|
| 30 | + * @var array[] |
|
| 31 | + */ |
|
| 32 | + protected $instances = []; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @var array[] |
|
| 36 | - */ |
|
| 37 | - protected $with = []; |
|
| 34 | + /** |
|
| 35 | + * @var array[] |
|
| 36 | + */ |
|
| 37 | + protected $with = []; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @return static |
|
| 41 | - */ |
|
| 42 | - public static function load() |
|
| 43 | - { |
|
| 44 | - if (empty(static::$instance)) { |
|
| 45 | - static::$instance = new static(); |
|
| 46 | - } |
|
| 47 | - return static::$instance; |
|
| 48 | - } |
|
| 39 | + /** |
|
| 40 | + * @return static |
|
| 41 | + */ |
|
| 42 | + public static function load() |
|
| 43 | + { |
|
| 44 | + if (empty(static::$instance)) { |
|
| 45 | + static::$instance = new static(); |
|
| 46 | + } |
|
| 47 | + return static::$instance; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @param string $abstract |
|
| 52 | - * @param mixed $concrete |
|
| 53 | - * @param bool $shared |
|
| 54 | - * @return void |
|
| 55 | - */ |
|
| 56 | - public function bind($abstract, $concrete = null, $shared = false) |
|
| 57 | - { |
|
| 58 | - $this->dropStaleInstances($abstract); |
|
| 59 | - if (is_null($concrete)) { |
|
| 60 | - $concrete = $abstract; |
|
| 61 | - } |
|
| 62 | - if (!$concrete instanceof Closure) { |
|
| 63 | - $concrete = $this->getClosure($abstract, $concrete); |
|
| 64 | - } |
|
| 65 | - $this->bindings[$abstract] = compact('concrete', 'shared'); |
|
| 66 | - } |
|
| 50 | + /** |
|
| 51 | + * @param string $abstract |
|
| 52 | + * @param mixed $concrete |
|
| 53 | + * @param bool $shared |
|
| 54 | + * @return void |
|
| 55 | + */ |
|
| 56 | + public function bind($abstract, $concrete = null, $shared = false) |
|
| 57 | + { |
|
| 58 | + $this->dropStaleInstances($abstract); |
|
| 59 | + if (is_null($concrete)) { |
|
| 60 | + $concrete = $abstract; |
|
| 61 | + } |
|
| 62 | + if (!$concrete instanceof Closure) { |
|
| 63 | + $concrete = $this->getClosure($abstract, $concrete); |
|
| 64 | + } |
|
| 65 | + $this->bindings[$abstract] = compact('concrete', 'shared'); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @param mixed $abstract |
|
| 70 | - * @return mixed |
|
| 71 | - */ |
|
| 72 | - public function make($abstract, array $parameters = []) |
|
| 73 | - { |
|
| 74 | - if (is_string($abstract) && !class_exists($abstract)) { |
|
| 75 | - $alias = __NAMESPACE__.'\\'.Str::removePrefix(__NAMESPACE__, $abstract); |
|
| 76 | - if (class_exists($alias)) { |
|
| 77 | - $abstract = $alias; |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - return $this->resolve($abstract, $parameters); |
|
| 81 | - } |
|
| 68 | + /** |
|
| 69 | + * @param mixed $abstract |
|
| 70 | + * @return mixed |
|
| 71 | + */ |
|
| 72 | + public function make($abstract, array $parameters = []) |
|
| 73 | + { |
|
| 74 | + if (is_string($abstract) && !class_exists($abstract)) { |
|
| 75 | + $alias = __NAMESPACE__.'\\'.Str::removePrefix(__NAMESPACE__, $abstract); |
|
| 76 | + if (class_exists($alias)) { |
|
| 77 | + $abstract = $alias; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + return $this->resolve($abstract, $parameters); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * @param string $abstract |
|
| 85 | - * @param mixed $concrete |
|
| 86 | - * @return void |
|
| 87 | - */ |
|
| 88 | - public function singleton($abstract, $concrete = null) |
|
| 89 | - { |
|
| 90 | - $this->bind($abstract, $concrete, true); |
|
| 91 | - } |
|
| 83 | + /** |
|
| 84 | + * @param string $abstract |
|
| 85 | + * @param mixed $concrete |
|
| 86 | + * @return void |
|
| 87 | + */ |
|
| 88 | + public function singleton($abstract, $concrete = null) |
|
| 89 | + { |
|
| 90 | + $this->bind($abstract, $concrete, true); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * @param Closure|string $concrete |
|
| 95 | - * @return mixed |
|
| 96 | - * @throws BindingResolutionException |
|
| 97 | - */ |
|
| 98 | - protected function construct($concrete) |
|
| 99 | - { |
|
| 100 | - if ($concrete instanceof Closure) { |
|
| 101 | - return $concrete($this, $this->getLastParameterOverride()); // probably a bound closure |
|
| 102 | - } |
|
| 103 | - try { |
|
| 104 | - $reflector = new ReflectionClass($concrete); // class or classname provided |
|
| 105 | - } catch (ReflectionException $e) { |
|
| 106 | - throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e); |
|
| 107 | - } |
|
| 108 | - if (!$reflector->isInstantiable()) { |
|
| 109 | - $this->throwNotInstantiable($concrete); // not an instantiable class |
|
| 110 | - } |
|
| 111 | - $this->buildStack[] = $concrete; |
|
| 112 | - if (is_null($constructor = $reflector->getConstructor())) { |
|
| 113 | - array_pop($this->buildStack); |
|
| 114 | - return new $concrete(); // class has no __construct |
|
| 115 | - } |
|
| 116 | - try { |
|
| 117 | - $instances = $this->resolveDependencies($constructor->getParameters()); // resolve class dependencies |
|
| 118 | - } catch (BindingResolutionException $e) { |
|
| 119 | - array_pop($this->buildStack); |
|
| 120 | - throw $e; |
|
| 121 | - } |
|
| 122 | - array_pop($this->buildStack); |
|
| 123 | - return $reflector->newInstanceArgs($instances); // return a new class |
|
| 124 | - } |
|
| 93 | + /** |
|
| 94 | + * @param Closure|string $concrete |
|
| 95 | + * @return mixed |
|
| 96 | + * @throws BindingResolutionException |
|
| 97 | + */ |
|
| 98 | + protected function construct($concrete) |
|
| 99 | + { |
|
| 100 | + if ($concrete instanceof Closure) { |
|
| 101 | + return $concrete($this, $this->getLastParameterOverride()); // probably a bound closure |
|
| 102 | + } |
|
| 103 | + try { |
|
| 104 | + $reflector = new ReflectionClass($concrete); // class or classname provided |
|
| 105 | + } catch (ReflectionException $e) { |
|
| 106 | + throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e); |
|
| 107 | + } |
|
| 108 | + if (!$reflector->isInstantiable()) { |
|
| 109 | + $this->throwNotInstantiable($concrete); // not an instantiable class |
|
| 110 | + } |
|
| 111 | + $this->buildStack[] = $concrete; |
|
| 112 | + if (is_null($constructor = $reflector->getConstructor())) { |
|
| 113 | + array_pop($this->buildStack); |
|
| 114 | + return new $concrete(); // class has no __construct |
|
| 115 | + } |
|
| 116 | + try { |
|
| 117 | + $instances = $this->resolveDependencies($constructor->getParameters()); // resolve class dependencies |
|
| 118 | + } catch (BindingResolutionException $e) { |
|
| 119 | + array_pop($this->buildStack); |
|
| 120 | + throw $e; |
|
| 121 | + } |
|
| 122 | + array_pop($this->buildStack); |
|
| 123 | + return $reflector->newInstanceArgs($instances); // return a new class |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * @param string $abstract |
|
| 128 | - * @return void |
|
| 129 | - */ |
|
| 130 | - protected function dropStaleInstances($abstract) |
|
| 131 | - { |
|
| 132 | - unset($this->instances[$abstract]); |
|
| 133 | - } |
|
| 126 | + /** |
|
| 127 | + * @param string $abstract |
|
| 128 | + * @return void |
|
| 129 | + */ |
|
| 130 | + protected function dropStaleInstances($abstract) |
|
| 131 | + { |
|
| 132 | + unset($this->instances[$abstract]); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * @param string $abstract |
|
| 137 | - * @param string $concrete |
|
| 138 | - * @return Closure |
|
| 139 | - */ |
|
| 140 | - protected function getClosure($abstract, $concrete) |
|
| 141 | - { |
|
| 142 | - return function ($container, $parameters = []) use ($abstract, $concrete) { |
|
| 143 | - return $abstract == $concrete |
|
| 144 | - ? $container->construct($concrete) |
|
| 145 | - : $container->resolve($concrete, $parameters); |
|
| 146 | - }; |
|
| 147 | - } |
|
| 135 | + /** |
|
| 136 | + * @param string $abstract |
|
| 137 | + * @param string $concrete |
|
| 138 | + * @return Closure |
|
| 139 | + */ |
|
| 140 | + protected function getClosure($abstract, $concrete) |
|
| 141 | + { |
|
| 142 | + return function ($container, $parameters = []) use ($abstract, $concrete) { |
|
| 143 | + return $abstract == $concrete |
|
| 144 | + ? $container->construct($concrete) |
|
| 145 | + : $container->resolve($concrete, $parameters); |
|
| 146 | + }; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * @param string $abstract |
|
| 151 | - * @return mixed |
|
| 152 | - */ |
|
| 153 | - protected function getConcrete($abstract) |
|
| 154 | - { |
|
| 155 | - if (isset($this->bindings[$abstract])) { |
|
| 156 | - return $this->bindings[$abstract]['concrete']; |
|
| 157 | - } |
|
| 158 | - return $abstract; |
|
| 159 | - } |
|
| 149 | + /** |
|
| 150 | + * @param string $abstract |
|
| 151 | + * @return mixed |
|
| 152 | + */ |
|
| 153 | + protected function getConcrete($abstract) |
|
| 154 | + { |
|
| 155 | + if (isset($this->bindings[$abstract])) { |
|
| 156 | + return $this->bindings[$abstract]['concrete']; |
|
| 157 | + } |
|
| 158 | + return $abstract; |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * @return array |
|
| 163 | - */ |
|
| 164 | - protected function getLastParameterOverride() |
|
| 165 | - { |
|
| 166 | - return count($this->with) ? end($this->with) : []; |
|
| 167 | - } |
|
| 161 | + /** |
|
| 162 | + * @return array |
|
| 163 | + */ |
|
| 164 | + protected function getLastParameterOverride() |
|
| 165 | + { |
|
| 166 | + return count($this->with) ? end($this->with) : []; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * @param ReflectionParameter $dependency |
|
| 171 | - * @return mixed |
|
| 172 | - */ |
|
| 173 | - protected function getParameterOverride($dependency) |
|
| 174 | - { |
|
| 175 | - return $this->getLastParameterOverride()[$dependency->name]; |
|
| 176 | - } |
|
| 169 | + /** |
|
| 170 | + * @param ReflectionParameter $dependency |
|
| 171 | + * @return mixed |
|
| 172 | + */ |
|
| 173 | + protected function getParameterOverride($dependency) |
|
| 174 | + { |
|
| 175 | + return $this->getLastParameterOverride()[$dependency->name]; |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * @param ReflectionParameter $dependency |
|
| 180 | - * @return bool |
|
| 181 | - */ |
|
| 182 | - protected function hasParameterOverride($dependency) |
|
| 183 | - { |
|
| 184 | - return array_key_exists($dependency->name, $this->getLastParameterOverride()); |
|
| 185 | - } |
|
| 178 | + /** |
|
| 179 | + * @param ReflectionParameter $dependency |
|
| 180 | + * @return bool |
|
| 181 | + */ |
|
| 182 | + protected function hasParameterOverride($dependency) |
|
| 183 | + { |
|
| 184 | + return array_key_exists($dependency->name, $this->getLastParameterOverride()); |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * @param mixed $concrete |
|
| 189 | - * @param string $abstract |
|
| 190 | - * @return bool |
|
| 191 | - */ |
|
| 192 | - protected function isBuildable($concrete, $abstract) |
|
| 193 | - { |
|
| 194 | - return $concrete === $abstract || $concrete instanceof Closure; |
|
| 195 | - } |
|
| 187 | + /** |
|
| 188 | + * @param mixed $concrete |
|
| 189 | + * @param string $abstract |
|
| 190 | + * @return bool |
|
| 191 | + */ |
|
| 192 | + protected function isBuildable($concrete, $abstract) |
|
| 193 | + { |
|
| 194 | + return $concrete === $abstract || $concrete instanceof Closure; |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - /** |
|
| 198 | - * @param string $abstract |
|
| 199 | - * @return bool |
|
| 200 | - */ |
|
| 201 | - protected function isShared($abstract) |
|
| 202 | - { |
|
| 203 | - return isset($this->instances[$abstract]) || !empty($this->bindings[$abstract]['shared']); |
|
| 204 | - } |
|
| 197 | + /** |
|
| 198 | + * @param string $abstract |
|
| 199 | + * @return bool |
|
| 200 | + */ |
|
| 201 | + protected function isShared($abstract) |
|
| 202 | + { |
|
| 203 | + return isset($this->instances[$abstract]) || !empty($this->bindings[$abstract]['shared']); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * @param mixed $abstract |
|
| 208 | - * @param array $parameters |
|
| 209 | - * @return mixed |
|
| 210 | - * @throws BindingResolutionException |
|
| 211 | - */ |
|
| 212 | - protected function resolve($abstract, $parameters = []) |
|
| 213 | - { |
|
| 214 | - if (isset($this->instances[$abstract]) && empty($parameters)) { |
|
| 215 | - return $this->instances[$abstract]; // return an existing singleton |
|
| 216 | - } |
|
| 217 | - $this->with[] = $parameters; |
|
| 218 | - $concrete = $this->getConcrete($abstract); |
|
| 219 | - $object = $this->isBuildable($concrete, $abstract) |
|
| 220 | - ? $this->construct($concrete) |
|
| 221 | - : $this->make($concrete); |
|
| 222 | - if ($this->isShared($abstract) && empty($parameters)) { |
|
| 223 | - $this->instances[$abstract] = $object; // store as a singleton |
|
| 224 | - } |
|
| 225 | - array_pop($this->with); |
|
| 226 | - return $object; |
|
| 227 | - } |
|
| 206 | + /** |
|
| 207 | + * @param mixed $abstract |
|
| 208 | + * @param array $parameters |
|
| 209 | + * @return mixed |
|
| 210 | + * @throws BindingResolutionException |
|
| 211 | + */ |
|
| 212 | + protected function resolve($abstract, $parameters = []) |
|
| 213 | + { |
|
| 214 | + if (isset($this->instances[$abstract]) && empty($parameters)) { |
|
| 215 | + return $this->instances[$abstract]; // return an existing singleton |
|
| 216 | + } |
|
| 217 | + $this->with[] = $parameters; |
|
| 218 | + $concrete = $this->getConcrete($abstract); |
|
| 219 | + $object = $this->isBuildable($concrete, $abstract) |
|
| 220 | + ? $this->construct($concrete) |
|
| 221 | + : $this->make($concrete); |
|
| 222 | + if ($this->isShared($abstract) && empty($parameters)) { |
|
| 223 | + $this->instances[$abstract] = $object; // store as a singleton |
|
| 224 | + } |
|
| 225 | + array_pop($this->with); |
|
| 226 | + return $object; |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - /** |
|
| 230 | - * Resolve a class based dependency from the container. |
|
| 231 | - * @return mixed |
|
| 232 | - * @throws Exception |
|
| 233 | - */ |
|
| 234 | - protected function resolveClass(ReflectionParameter $parameter) |
|
| 235 | - { |
|
| 236 | - try { |
|
| 237 | - return $this->make($parameter->getClass()->name); |
|
| 238 | - } catch (Exception $error) { |
|
| 239 | - if ($parameter->isOptional()) { |
|
| 240 | - return $parameter->getDefaultValue(); |
|
| 241 | - } |
|
| 242 | - throw $error; |
|
| 243 | - } |
|
| 244 | - } |
|
| 229 | + /** |
|
| 230 | + * Resolve a class based dependency from the container. |
|
| 231 | + * @return mixed |
|
| 232 | + * @throws Exception |
|
| 233 | + */ |
|
| 234 | + protected function resolveClass(ReflectionParameter $parameter) |
|
| 235 | + { |
|
| 236 | + try { |
|
| 237 | + return $this->make($parameter->getClass()->name); |
|
| 238 | + } catch (Exception $error) { |
|
| 239 | + if ($parameter->isOptional()) { |
|
| 240 | + return $parameter->getDefaultValue(); |
|
| 241 | + } |
|
| 242 | + throw $error; |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * @return array |
|
| 248 | - */ |
|
| 249 | - protected function resolveDependencies(array $dependencies) |
|
| 250 | - { |
|
| 251 | - $results = []; |
|
| 252 | - foreach ($dependencies as $dependency) { |
|
| 253 | - if ($this->hasParameterOverride($dependency)) { |
|
| 254 | - $results[] = $this->getParameterOverride($dependency); |
|
| 255 | - continue; |
|
| 256 | - } |
|
| 257 | - $results[] = is_null($dependency->getClass()) |
|
| 258 | - ? $this->resolvePrimitive($dependency) |
|
| 259 | - : $this->resolveClass($dependency); |
|
| 260 | - } |
|
| 261 | - return $results; |
|
| 262 | - } |
|
| 246 | + /** |
|
| 247 | + * @return array |
|
| 248 | + */ |
|
| 249 | + protected function resolveDependencies(array $dependencies) |
|
| 250 | + { |
|
| 251 | + $results = []; |
|
| 252 | + foreach ($dependencies as $dependency) { |
|
| 253 | + if ($this->hasParameterOverride($dependency)) { |
|
| 254 | + $results[] = $this->getParameterOverride($dependency); |
|
| 255 | + continue; |
|
| 256 | + } |
|
| 257 | + $results[] = is_null($dependency->getClass()) |
|
| 258 | + ? $this->resolvePrimitive($dependency) |
|
| 259 | + : $this->resolveClass($dependency); |
|
| 260 | + } |
|
| 261 | + return $results; |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - /** |
|
| 265 | - * @param ReflectionParameter $parameter |
|
| 266 | - * @return mixed |
|
| 267 | - * @throws BindingResolutionException |
|
| 268 | - */ |
|
| 269 | - protected function resolvePrimitive(ReflectionParameter $parameter) |
|
| 270 | - { |
|
| 271 | - if ($parameter->isDefaultValueAvailable()) { |
|
| 272 | - return $parameter->getDefaultValue(); |
|
| 273 | - } |
|
| 274 | - $this->throwUnresolvablePrimitive($parameter); |
|
| 275 | - } |
|
| 264 | + /** |
|
| 265 | + * @param ReflectionParameter $parameter |
|
| 266 | + * @return mixed |
|
| 267 | + * @throws BindingResolutionException |
|
| 268 | + */ |
|
| 269 | + protected function resolvePrimitive(ReflectionParameter $parameter) |
|
| 270 | + { |
|
| 271 | + if ($parameter->isDefaultValueAvailable()) { |
|
| 272 | + return $parameter->getDefaultValue(); |
|
| 273 | + } |
|
| 274 | + $this->throwUnresolvablePrimitive($parameter); |
|
| 275 | + } |
|
| 276 | 276 | |
| 277 | - /** |
|
| 278 | - * @param string $concrete |
|
| 279 | - * @return void |
|
| 280 | - * @throws BindingResolutionException |
|
| 281 | - */ |
|
| 282 | - protected function throwNotInstantiable($concrete) |
|
| 283 | - { |
|
| 284 | - if (empty($this->buildStack)) { |
|
| 285 | - $message = "Target [$concrete] is not instantiable."; |
|
| 286 | - } else { |
|
| 287 | - $previous = implode(', ', $this->buildStack); |
|
| 288 | - $message = "Target [$concrete] is not instantiable while building [$previous]."; |
|
| 289 | - } |
|
| 290 | - throw new BindingResolutionException($message); |
|
| 291 | - } |
|
| 277 | + /** |
|
| 278 | + * @param string $concrete |
|
| 279 | + * @return void |
|
| 280 | + * @throws BindingResolutionException |
|
| 281 | + */ |
|
| 282 | + protected function throwNotInstantiable($concrete) |
|
| 283 | + { |
|
| 284 | + if (empty($this->buildStack)) { |
|
| 285 | + $message = "Target [$concrete] is not instantiable."; |
|
| 286 | + } else { |
|
| 287 | + $previous = implode(', ', $this->buildStack); |
|
| 288 | + $message = "Target [$concrete] is not instantiable while building [$previous]."; |
|
| 289 | + } |
|
| 290 | + throw new BindingResolutionException($message); |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | - /** |
|
| 294 | - * @param ReflectionParameter $parameter |
|
| 295 | - * @return void |
|
| 296 | - * @throws BindingResolutionException |
|
| 297 | - */ |
|
| 298 | - protected function throwUnresolvablePrimitive(ReflectionParameter $parameter) |
|
| 299 | - { |
|
| 300 | - throw new BindingResolutionException("Unresolvable dependency resolving [$parameter] in class {$parameter->getDeclaringClass()->getName()}"); |
|
| 301 | - } |
|
| 293 | + /** |
|
| 294 | + * @param ReflectionParameter $parameter |
|
| 295 | + * @return void |
|
| 296 | + * @throws BindingResolutionException |
|
| 297 | + */ |
|
| 298 | + protected function throwUnresolvablePrimitive(ReflectionParameter $parameter) |
|
| 299 | + { |
|
| 300 | + throw new BindingResolutionException("Unresolvable dependency resolving [$parameter] in class {$parameter->getDeclaringClass()->getName()}"); |
|
| 301 | + } |
|
| 302 | 302 | } |
@@ -7,29 +7,29 @@ |
||
| 7 | 7 | |
| 8 | 8 | class RegisterTinymcePopups implements Contract |
| 9 | 9 | { |
| 10 | - public $popups; |
|
| 10 | + public $popups; |
|
| 11 | 11 | |
| 12 | - public function __construct($input) |
|
| 13 | - { |
|
| 14 | - $this->popups = $input; |
|
| 15 | - } |
|
| 12 | + public function __construct($input) |
|
| 13 | + { |
|
| 14 | + $this->popups = $input; |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @return void |
|
| 19 | - */ |
|
| 20 | - public function handle() |
|
| 21 | - { |
|
| 22 | - foreach ($this->popups as $slug => $label) { |
|
| 23 | - $buttonClass = Helper::buildClassName($slug.'Tinymce', 'Tinymce'); |
|
| 24 | - if (!class_exists($buttonClass)) { |
|
| 25 | - glsr_log()->error(sprintf('Tinymce Popup class missing (%s)', $buttonClass)); |
|
| 26 | - continue; |
|
| 27 | - } |
|
| 28 | - $shortcode = glsr($buttonClass)->register($slug, [ |
|
| 29 | - 'label' => $label, |
|
| 30 | - 'title' => $label, |
|
| 31 | - ]); |
|
| 32 | - glsr()->append('mce', $shortcode->properties, $slug); |
|
| 33 | - } |
|
| 34 | - } |
|
| 17 | + /** |
|
| 18 | + * @return void |
|
| 19 | + */ |
|
| 20 | + public function handle() |
|
| 21 | + { |
|
| 22 | + foreach ($this->popups as $slug => $label) { |
|
| 23 | + $buttonClass = Helper::buildClassName($slug.'Tinymce', 'Tinymce'); |
|
| 24 | + if (!class_exists($buttonClass)) { |
|
| 25 | + glsr_log()->error(sprintf('Tinymce Popup class missing (%s)', $buttonClass)); |
|
| 26 | + continue; |
|
| 27 | + } |
|
| 28 | + $shortcode = glsr($buttonClass)->register($slug, [ |
|
| 29 | + 'label' => $label, |
|
| 30 | + 'title' => $label, |
|
| 31 | + ]); |
|
| 32 | + glsr()->append('mce', $shortcode->properties, $slug); |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -6,28 +6,28 @@ |
||
| 6 | 6 | |
| 7 | 7 | class PermissionDefaults extends Defaults |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @return array |
|
| 11 | - */ |
|
| 12 | - protected function defaults() |
|
| 13 | - { |
|
| 14 | - return [ |
|
| 15 | - 'addons' => 'install_plugins', |
|
| 16 | - 'documentation' => [ |
|
| 17 | - 'faq' => 'edit_others_posts', |
|
| 18 | - 'functions' => 'manage_options', |
|
| 19 | - 'hooks' => 'edit_others_posts', |
|
| 20 | - 'index' => 'edit_posts', |
|
| 21 | - 'support' => 'edit_others_posts', |
|
| 22 | - ], |
|
| 23 | - 'settings' => 'manage_options', |
|
| 24 | - 'tools' => [ |
|
| 25 | - 'console' => 'edit_others_posts', |
|
| 26 | - 'general' => 'edit_others_posts', |
|
| 27 | - 'index' => 'edit_others_posts', |
|
| 28 | - 'sync' => 'manage_options', |
|
| 29 | - 'system-info' => 'edit_others_posts', |
|
| 30 | - ], |
|
| 31 | - ]; |
|
| 32 | - } |
|
| 9 | + /** |
|
| 10 | + * @return array |
|
| 11 | + */ |
|
| 12 | + protected function defaults() |
|
| 13 | + { |
|
| 14 | + return [ |
|
| 15 | + 'addons' => 'install_plugins', |
|
| 16 | + 'documentation' => [ |
|
| 17 | + 'faq' => 'edit_others_posts', |
|
| 18 | + 'functions' => 'manage_options', |
|
| 19 | + 'hooks' => 'edit_others_posts', |
|
| 20 | + 'index' => 'edit_posts', |
|
| 21 | + 'support' => 'edit_others_posts', |
|
| 22 | + ], |
|
| 23 | + 'settings' => 'manage_options', |
|
| 24 | + 'tools' => [ |
|
| 25 | + 'console' => 'edit_others_posts', |
|
| 26 | + 'general' => 'edit_others_posts', |
|
| 27 | + 'index' => 'edit_others_posts', |
|
| 28 | + 'sync' => 'manage_options', |
|
| 29 | + 'system-info' => 'edit_others_posts', |
|
| 30 | + ], |
|
| 31 | + ]; |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -19,46 +19,46 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | abstract class Addon |
| 21 | 21 | { |
| 22 | - use Plugin; |
|
| 22 | + use Plugin; |
|
| 23 | 23 | |
| 24 | - const ID = ''; |
|
| 25 | - const NAME = ''; |
|
| 26 | - const SLUG = ''; |
|
| 27 | - const UPDATE_URL = ''; |
|
| 24 | + const ID = ''; |
|
| 25 | + const NAME = ''; |
|
| 26 | + const SLUG = ''; |
|
| 27 | + const UPDATE_URL = ''; |
|
| 28 | 28 | |
| 29 | - protected $updater; |
|
| 29 | + protected $updater; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @return void |
|
| 33 | - */ |
|
| 34 | - public function init() |
|
| 35 | - { |
|
| 36 | - $reflection = new ReflectionClass($this); |
|
| 37 | - $className = Str::replaceLast($reflection->getShortname(), 'Hooks', $reflection->getName()); |
|
| 38 | - if (class_exists($className)) { |
|
| 39 | - (new $className())->run(); |
|
| 40 | - } else { |
|
| 41 | - glsr_log()->error('The '.static::NAME.' add-on is missing a Hooks class'); |
|
| 42 | - } |
|
| 43 | - } |
|
| 31 | + /** |
|
| 32 | + * @return void |
|
| 33 | + */ |
|
| 34 | + public function init() |
|
| 35 | + { |
|
| 36 | + $reflection = new ReflectionClass($this); |
|
| 37 | + $className = Str::replaceLast($reflection->getShortname(), 'Hooks', $reflection->getName()); |
|
| 38 | + if (class_exists($className)) { |
|
| 39 | + (new $className())->run(); |
|
| 40 | + } else { |
|
| 41 | + glsr_log()->error('The '.static::NAME.' add-on is missing a Hooks class'); |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - public function make($class, array $parameters = []) |
|
| 46 | - { |
|
| 47 | - $class = Str::camelCase($class); |
|
| 48 | - $class = ltrim(str_replace([__NAMESPACE__, 'GeminiLabs\SiteReviews'], '', $class), '\\'); |
|
| 49 | - $class = __NAMESPACE__.'\\'.$class; |
|
| 50 | - return glsr($class, $parameters); |
|
| 51 | - } |
|
| 45 | + public function make($class, array $parameters = []) |
|
| 46 | + { |
|
| 47 | + $class = Str::camelCase($class); |
|
| 48 | + $class = ltrim(str_replace([__NAMESPACE__, 'GeminiLabs\SiteReviews'], '', $class), '\\'); |
|
| 49 | + $class = __NAMESPACE__.'\\'.$class; |
|
| 50 | + return glsr($class, $parameters); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return void |
|
| 55 | - */ |
|
| 56 | - public function update() |
|
| 57 | - { |
|
| 58 | - $this->updater = new Updater(static::UPDATE_URL, $this->file, [ |
|
| 59 | - 'license' => glsr_get_option('settings.licenses.'.static::ID), |
|
| 60 | - 'testedTo' => $this->testedTo, |
|
| 61 | - ]); |
|
| 62 | - $this->updater->init(); |
|
| 63 | - } |
|
| 53 | + /** |
|
| 54 | + * @return void |
|
| 55 | + */ |
|
| 56 | + public function update() |
|
| 57 | + { |
|
| 58 | + $this->updater = new Updater(static::UPDATE_URL, $this->file, [ |
|
| 59 | + 'license' => glsr_get_option('settings.licenses.'.static::ID), |
|
| 60 | + 'testedTo' => $this->testedTo, |
|
| 61 | + ]); |
|
| 62 | + $this->updater->init(); |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -12,163 +12,163 @@ |
||
| 12 | 12 | |
| 13 | 13 | class SettingsController extends Controller |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @param mixed $input |
|
| 17 | - * @return array |
|
| 18 | - * @callback register_setting |
|
| 19 | - */ |
|
| 20 | - public function callbackRegisterSettings($input) |
|
| 21 | - { |
|
| 22 | - $settings = Arr::consolidate($input); |
|
| 23 | - if (1 === count($settings) && array_key_exists('settings', $settings)) { |
|
| 24 | - $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input); |
|
| 25 | - $options = $this->sanitizeGeneral($input, $options); |
|
| 26 | - $options = $this->sanitizeLicenses($input, $options); |
|
| 27 | - $options = $this->sanitizeSubmissions($input, $options); |
|
| 28 | - $options = $this->sanitizeTranslations($input, $options); |
|
| 29 | - $options = glsr()->filterArray('settings/callback', $options, $settings); |
|
| 30 | - if (filter_input(INPUT_POST, 'option_page') == Application::ID.'-settings') { |
|
| 31 | - glsr(Notice::class)->addSuccess(_x('Settings updated.', 'admin-text', 'site-reviews')); |
|
| 32 | - } |
|
| 33 | - return $options; |
|
| 34 | - } |
|
| 35 | - return $input; |
|
| 36 | - } |
|
| 15 | + /** |
|
| 16 | + * @param mixed $input |
|
| 17 | + * @return array |
|
| 18 | + * @callback register_setting |
|
| 19 | + */ |
|
| 20 | + public function callbackRegisterSettings($input) |
|
| 21 | + { |
|
| 22 | + $settings = Arr::consolidate($input); |
|
| 23 | + if (1 === count($settings) && array_key_exists('settings', $settings)) { |
|
| 24 | + $options = array_replace_recursive(glsr(OptionManager::class)->all(), $input); |
|
| 25 | + $options = $this->sanitizeGeneral($input, $options); |
|
| 26 | + $options = $this->sanitizeLicenses($input, $options); |
|
| 27 | + $options = $this->sanitizeSubmissions($input, $options); |
|
| 28 | + $options = $this->sanitizeTranslations($input, $options); |
|
| 29 | + $options = glsr()->filterArray('settings/callback', $options, $settings); |
|
| 30 | + if (filter_input(INPUT_POST, 'option_page') == Application::ID.'-settings') { |
|
| 31 | + glsr(Notice::class)->addSuccess(_x('Settings updated.', 'admin-text', 'site-reviews')); |
|
| 32 | + } |
|
| 33 | + return $options; |
|
| 34 | + } |
|
| 35 | + return $input; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @return void |
|
| 40 | - * @action admin_init |
|
| 41 | - */ |
|
| 42 | - public function registerSettings() |
|
| 43 | - { |
|
| 44 | - register_setting(Application::ID.'-settings', OptionManager::databaseKey(), [ |
|
| 45 | - 'sanitize_callback' => [$this, 'callbackRegisterSettings'], |
|
| 46 | - ]); |
|
| 47 | - } |
|
| 38 | + /** |
|
| 39 | + * @return void |
|
| 40 | + * @action admin_init |
|
| 41 | + */ |
|
| 42 | + public function registerSettings() |
|
| 43 | + { |
|
| 44 | + register_setting(Application::ID.'-settings', OptionManager::databaseKey(), [ |
|
| 45 | + 'sanitize_callback' => [$this, 'callbackRegisterSettings'], |
|
| 46 | + ]); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @return array |
|
| 51 | - */ |
|
| 52 | - protected function sanitizeGeneral(array $input, array $options) |
|
| 53 | - { |
|
| 54 | - $key = 'settings.general'; |
|
| 55 | - $inputForm = Arr::get($input, $key); |
|
| 56 | - if (!$this->hasMultilingualIntegration(Arr::get($inputForm, 'multilingual'))) { |
|
| 57 | - $options = Arr::set($options, $key.'.multilingual', ''); |
|
| 58 | - } |
|
| 59 | - if ('' == trim(Arr::get($inputForm, 'notification_message'))) { |
|
| 60 | - $defaultValue = Arr::get(glsr()->defaults, $key.'.notification_message'); |
|
| 61 | - $options = Arr::set($options, $key.'.notification_message', $defaultValue); |
|
| 62 | - } |
|
| 63 | - $defaultValue = Arr::get($inputForm, 'notifications', []); |
|
| 64 | - $options = Arr::set($options, $key.'.notifications', $defaultValue); |
|
| 65 | - return $options; |
|
| 66 | - } |
|
| 49 | + /** |
|
| 50 | + * @return array |
|
| 51 | + */ |
|
| 52 | + protected function sanitizeGeneral(array $input, array $options) |
|
| 53 | + { |
|
| 54 | + $key = 'settings.general'; |
|
| 55 | + $inputForm = Arr::get($input, $key); |
|
| 56 | + if (!$this->hasMultilingualIntegration(Arr::get($inputForm, 'multilingual'))) { |
|
| 57 | + $options = Arr::set($options, $key.'.multilingual', ''); |
|
| 58 | + } |
|
| 59 | + if ('' == trim(Arr::get($inputForm, 'notification_message'))) { |
|
| 60 | + $defaultValue = Arr::get(glsr()->defaults, $key.'.notification_message'); |
|
| 61 | + $options = Arr::set($options, $key.'.notification_message', $defaultValue); |
|
| 62 | + } |
|
| 63 | + $defaultValue = Arr::get($inputForm, 'notifications', []); |
|
| 64 | + $options = Arr::set($options, $key.'.notifications', $defaultValue); |
|
| 65 | + return $options; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @return array |
|
| 70 | - */ |
|
| 71 | - protected function sanitizeLicenses(array $input, array $options) |
|
| 72 | - { |
|
| 73 | - $key = 'settings.licenses'; |
|
| 74 | - $licenses = Arr::consolidate(Arr::get($input, $key)); |
|
| 75 | - foreach ($licenses as $slug => &$license) { |
|
| 76 | - if (empty($license)) { |
|
| 77 | - continue; |
|
| 78 | - } |
|
| 79 | - $license = $this->verifyLicense($license, $slug); |
|
| 80 | - } |
|
| 81 | - $options = Arr::set($options, $key, $licenses); |
|
| 82 | - return $options; |
|
| 83 | - } |
|
| 68 | + /** |
|
| 69 | + * @return array |
|
| 70 | + */ |
|
| 71 | + protected function sanitizeLicenses(array $input, array $options) |
|
| 72 | + { |
|
| 73 | + $key = 'settings.licenses'; |
|
| 74 | + $licenses = Arr::consolidate(Arr::get($input, $key)); |
|
| 75 | + foreach ($licenses as $slug => &$license) { |
|
| 76 | + if (empty($license)) { |
|
| 77 | + continue; |
|
| 78 | + } |
|
| 79 | + $license = $this->verifyLicense($license, $slug); |
|
| 80 | + } |
|
| 81 | + $options = Arr::set($options, $key, $licenses); |
|
| 82 | + return $options; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * @return array |
|
| 87 | - */ |
|
| 88 | - protected function sanitizeSubmissions(array $input, array $options) |
|
| 89 | - { |
|
| 90 | - $key = 'settings.submissions'; |
|
| 91 | - $inputForm = Arr::get($input, $key); |
|
| 92 | - $defaultValue = isset($inputForm['required']) |
|
| 93 | - ? $inputForm['required'] |
|
| 94 | - : []; |
|
| 95 | - $options = Arr::set($options, $key.'.required', $defaultValue); |
|
| 96 | - return $options; |
|
| 97 | - } |
|
| 85 | + /** |
|
| 86 | + * @return array |
|
| 87 | + */ |
|
| 88 | + protected function sanitizeSubmissions(array $input, array $options) |
|
| 89 | + { |
|
| 90 | + $key = 'settings.submissions'; |
|
| 91 | + $inputForm = Arr::get($input, $key); |
|
| 92 | + $defaultValue = isset($inputForm['required']) |
|
| 93 | + ? $inputForm['required'] |
|
| 94 | + : []; |
|
| 95 | + $options = Arr::set($options, $key.'.required', $defaultValue); |
|
| 96 | + return $options; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * @return array |
|
| 101 | - */ |
|
| 102 | - protected function sanitizeTranslations(array $input, array $options) |
|
| 103 | - { |
|
| 104 | - $key = 'settings.strings'; |
|
| 105 | - $inputForm = Arr::consolidate(Arr::get($input, $key)); |
|
| 106 | - if (!empty($inputForm)) { |
|
| 107 | - $options = Arr::set($options, $key, array_values(array_filter($inputForm))); |
|
| 108 | - $allowedTags = [ |
|
| 109 | - 'a' => ['class' => [], 'href' => [], 'target' => []], |
|
| 110 | - 'span' => ['class' => []], |
|
| 111 | - ]; |
|
| 112 | - array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) { |
|
| 113 | - if (isset($string['s2'])) { |
|
| 114 | - $string['s2'] = wp_kses($string['s2'], $allowedTags); |
|
| 115 | - } |
|
| 116 | - if (isset($string['p2'])) { |
|
| 117 | - $string['p2'] = wp_kses($string['p2'], $allowedTags); |
|
| 118 | - } |
|
| 119 | - }); |
|
| 120 | - } |
|
| 121 | - return $options; |
|
| 122 | - } |
|
| 99 | + /** |
|
| 100 | + * @return array |
|
| 101 | + */ |
|
| 102 | + protected function sanitizeTranslations(array $input, array $options) |
|
| 103 | + { |
|
| 104 | + $key = 'settings.strings'; |
|
| 105 | + $inputForm = Arr::consolidate(Arr::get($input, $key)); |
|
| 106 | + if (!empty($inputForm)) { |
|
| 107 | + $options = Arr::set($options, $key, array_values(array_filter($inputForm))); |
|
| 108 | + $allowedTags = [ |
|
| 109 | + 'a' => ['class' => [], 'href' => [], 'target' => []], |
|
| 110 | + 'span' => ['class' => []], |
|
| 111 | + ]; |
|
| 112 | + array_walk($options['settings']['strings'], function (&$string) use ($allowedTags) { |
|
| 113 | + if (isset($string['s2'])) { |
|
| 114 | + $string['s2'] = wp_kses($string['s2'], $allowedTags); |
|
| 115 | + } |
|
| 116 | + if (isset($string['p2'])) { |
|
| 117 | + $string['p2'] = wp_kses($string['p2'], $allowedTags); |
|
| 118 | + } |
|
| 119 | + }); |
|
| 120 | + } |
|
| 121 | + return $options; |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * @param string $integrationSlug |
|
| 126 | - * @return bool |
|
| 127 | - */ |
|
| 128 | - protected function hasMultilingualIntegration($integrationSlug) |
|
| 129 | - { |
|
| 130 | - $integration = glsr(Multilingual::class)->getIntegration($integrationSlug); |
|
| 131 | - if (!$integration) { |
|
| 132 | - return false; |
|
| 133 | - } |
|
| 134 | - if (!$integration->isActive()) { |
|
| 135 | - glsr(Notice::class)->addError(sprintf( |
|
| 136 | - _x('Please install/activate the %s plugin to enable integration.', 'admin-text', 'site-reviews'), |
|
| 137 | - $integration->pluginName |
|
| 138 | - )); |
|
| 139 | - return false; |
|
| 140 | - } elseif (!$integration->isSupported()) { |
|
| 141 | - glsr(Notice::class)->addError(sprintf( |
|
| 142 | - _x('Please update the %s plugin to v%s or greater to enable integration.', 'admin-text', 'site-reviews'), |
|
| 143 | - $integration->pluginName, |
|
| 144 | - $integration->supportedVersion |
|
| 145 | - )); |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - return true; |
|
| 149 | - } |
|
| 124 | + /** |
|
| 125 | + * @param string $integrationSlug |
|
| 126 | + * @return bool |
|
| 127 | + */ |
|
| 128 | + protected function hasMultilingualIntegration($integrationSlug) |
|
| 129 | + { |
|
| 130 | + $integration = glsr(Multilingual::class)->getIntegration($integrationSlug); |
|
| 131 | + if (!$integration) { |
|
| 132 | + return false; |
|
| 133 | + } |
|
| 134 | + if (!$integration->isActive()) { |
|
| 135 | + glsr(Notice::class)->addError(sprintf( |
|
| 136 | + _x('Please install/activate the %s plugin to enable integration.', 'admin-text', 'site-reviews'), |
|
| 137 | + $integration->pluginName |
|
| 138 | + )); |
|
| 139 | + return false; |
|
| 140 | + } elseif (!$integration->isSupported()) { |
|
| 141 | + glsr(Notice::class)->addError(sprintf( |
|
| 142 | + _x('Please update the %s plugin to v%s or greater to enable integration.', 'admin-text', 'site-reviews'), |
|
| 143 | + $integration->pluginName, |
|
| 144 | + $integration->supportedVersion |
|
| 145 | + )); |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + return true; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * @param string $license |
|
| 153 | - * @param string $slug |
|
| 154 | - * @return string |
|
| 155 | - */ |
|
| 156 | - protected function verifyLicense($license, $slug) |
|
| 157 | - { |
|
| 158 | - try { |
|
| 159 | - $addon = glsr($slug); |
|
| 160 | - $updater = new Updater($addon->update_url, $addon->file, [ |
|
| 161 | - 'license' => $license, |
|
| 162 | - 'testedTo' => $addon->testedTo, |
|
| 163 | - ]); |
|
| 164 | - if (!$updater->isLicenseValid()) { |
|
| 165 | - throw new Exception('Invalid license: '.$license.' ('.$addon->id.')'); |
|
| 166 | - } |
|
| 167 | - } catch (Exception $e) { |
|
| 168 | - $license = ''; |
|
| 169 | - glsr_log()->debug($e->getMessage()); |
|
| 170 | - glsr(Notice::class)->addError(_x('A license you entered was invalid.', 'admin-text', 'site-reviews')); |
|
| 171 | - } |
|
| 172 | - return $license; |
|
| 173 | - } |
|
| 151 | + /** |
|
| 152 | + * @param string $license |
|
| 153 | + * @param string $slug |
|
| 154 | + * @return string |
|
| 155 | + */ |
|
| 156 | + protected function verifyLicense($license, $slug) |
|
| 157 | + { |
|
| 158 | + try { |
|
| 159 | + $addon = glsr($slug); |
|
| 160 | + $updater = new Updater($addon->update_url, $addon->file, [ |
|
| 161 | + 'license' => $license, |
|
| 162 | + 'testedTo' => $addon->testedTo, |
|
| 163 | + ]); |
|
| 164 | + if (!$updater->isLicenseValid()) { |
|
| 165 | + throw new Exception('Invalid license: '.$license.' ('.$addon->id.')'); |
|
| 166 | + } |
|
| 167 | + } catch (Exception $e) { |
|
| 168 | + $license = ''; |
|
| 169 | + glsr_log()->debug($e->getMessage()); |
|
| 170 | + glsr(Notice::class)->addError(_x('A license you entered was invalid.', 'admin-text', 'site-reviews')); |
|
| 171 | + } |
|
| 172 | + return $license; |
|
| 173 | + } |
|
| 174 | 174 | } |
@@ -8,97 +8,97 @@ |
||
| 8 | 8 | |
| 9 | 9 | class WelcomeController extends Controller |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @return array |
|
| 13 | - * @filter plugin_action_links_site-reviews/site-reviews.php |
|
| 14 | - */ |
|
| 15 | - public function filterActionLinks(array $links) |
|
| 16 | - { |
|
| 17 | - $links['welcome'] = glsr(Builder::class)->a(_x('About', 'admin-text', 'site-reviews'), [ |
|
| 18 | - 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=welcome'), |
|
| 19 | - ]); |
|
| 20 | - return $links; |
|
| 21 | - } |
|
| 11 | + /** |
|
| 12 | + * @return array |
|
| 13 | + * @filter plugin_action_links_site-reviews/site-reviews.php |
|
| 14 | + */ |
|
| 15 | + public function filterActionLinks(array $links) |
|
| 16 | + { |
|
| 17 | + $links['welcome'] = glsr(Builder::class)->a(_x('About', 'admin-text', 'site-reviews'), [ |
|
| 18 | + 'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=welcome'), |
|
| 19 | + ]); |
|
| 20 | + return $links; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @return string |
|
| 25 | - * @filter admin_title |
|
| 26 | - */ |
|
| 27 | - public function filterAdminTitle($title) |
|
| 28 | - { |
|
| 29 | - return Application::POST_TYPE.'_page_welcome' == glsr_current_screen()->id |
|
| 30 | - ? sprintf(_x('Welcome to %s — WordPress', 'admin-text', 'site-reviews'), glsr()->name) |
|
| 31 | - : $title; |
|
| 32 | - } |
|
| 23 | + /** |
|
| 24 | + * @return string |
|
| 25 | + * @filter admin_title |
|
| 26 | + */ |
|
| 27 | + public function filterAdminTitle($title) |
|
| 28 | + { |
|
| 29 | + return Application::POST_TYPE.'_page_welcome' == glsr_current_screen()->id |
|
| 30 | + ? sprintf(_x('Welcome to %s — WordPress', 'admin-text', 'site-reviews'), glsr()->name) |
|
| 31 | + : $title; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @param string $text |
|
| 36 | - * @return string |
|
| 37 | - * @filter admin_footer_text |
|
| 38 | - */ |
|
| 39 | - public function filterFooterText($text) |
|
| 40 | - { |
|
| 41 | - if (Application::POST_TYPE.'_page_welcome' != glsr_current_screen()->id) { |
|
| 42 | - return $text; |
|
| 43 | - } |
|
| 44 | - $url = 'https://wordpress.org/support/view/plugin-reviews/site-reviews?filter=5#new-post'; |
|
| 45 | - return wp_kses_post(sprintf( |
|
| 46 | - _x('Please rate %s on %s and help us spread the word. Thank you so much!', 'admin-text', 'site-reviews'), |
|
| 47 | - '<strong>'.glsr()->name.'</strong> <a href="'.$url.'" target="_blank">★★★★★</a>', |
|
| 48 | - '<a href="'.$url.'" target="_blank">wordpress.org</a>' |
|
| 49 | - )); |
|
| 50 | - } |
|
| 34 | + /** |
|
| 35 | + * @param string $text |
|
| 36 | + * @return string |
|
| 37 | + * @filter admin_footer_text |
|
| 38 | + */ |
|
| 39 | + public function filterFooterText($text) |
|
| 40 | + { |
|
| 41 | + if (Application::POST_TYPE.'_page_welcome' != glsr_current_screen()->id) { |
|
| 42 | + return $text; |
|
| 43 | + } |
|
| 44 | + $url = 'https://wordpress.org/support/view/plugin-reviews/site-reviews?filter=5#new-post'; |
|
| 45 | + return wp_kses_post(sprintf( |
|
| 46 | + _x('Please rate %s on %s and help us spread the word. Thank you so much!', 'admin-text', 'site-reviews'), |
|
| 47 | + '<strong>'.glsr()->name.'</strong> <a href="'.$url.'" target="_blank">★★★★★</a>', |
|
| 48 | + '<a href="'.$url.'" target="_blank">wordpress.org</a>' |
|
| 49 | + )); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @param string $plugin |
|
| 54 | - * @param bool $isNetworkActivation |
|
| 55 | - * @return void |
|
| 56 | - * @action activated_plugin |
|
| 57 | - */ |
|
| 58 | - public function redirectOnActivation($plugin, $isNetworkActivation) |
|
| 59 | - { |
|
| 60 | - if (!$isNetworkActivation |
|
| 61 | - && 'cli' !== php_sapi_name() |
|
| 62 | - && $plugin === plugin_basename(glsr()->file)) { |
|
| 63 | - wp_safe_redirect(admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=welcome')); |
|
| 64 | - exit; |
|
| 65 | - } |
|
| 66 | - } |
|
| 52 | + /** |
|
| 53 | + * @param string $plugin |
|
| 54 | + * @param bool $isNetworkActivation |
|
| 55 | + * @return void |
|
| 56 | + * @action activated_plugin |
|
| 57 | + */ |
|
| 58 | + public function redirectOnActivation($plugin, $isNetworkActivation) |
|
| 59 | + { |
|
| 60 | + if (!$isNetworkActivation |
|
| 61 | + && 'cli' !== php_sapi_name() |
|
| 62 | + && $plugin === plugin_basename(glsr()->file)) { |
|
| 63 | + wp_safe_redirect(admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=welcome')); |
|
| 64 | + exit; |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @return void |
|
| 70 | - * @action admin_menu |
|
| 71 | - */ |
|
| 72 | - public function registerPage() |
|
| 73 | - { |
|
| 74 | - add_submenu_page('edit.php?post_type='.Application::POST_TYPE, |
|
| 75 | - sprintf(_x('Welcome to %s', 'admin-text', 'site-reviews'), glsr()->name), |
|
| 76 | - glsr()->name, |
|
| 77 | - glsr()->getPermission('welcome'), |
|
| 78 | - 'welcome', |
|
| 79 | - [$this, 'renderPage'] |
|
| 80 | - ); |
|
| 81 | - remove_submenu_page('edit.php?post_type='.Application::POST_TYPE, 'welcome'); |
|
| 82 | - } |
|
| 68 | + /** |
|
| 69 | + * @return void |
|
| 70 | + * @action admin_menu |
|
| 71 | + */ |
|
| 72 | + public function registerPage() |
|
| 73 | + { |
|
| 74 | + add_submenu_page('edit.php?post_type='.Application::POST_TYPE, |
|
| 75 | + sprintf(_x('Welcome to %s', 'admin-text', 'site-reviews'), glsr()->name), |
|
| 76 | + glsr()->name, |
|
| 77 | + glsr()->getPermission('welcome'), |
|
| 78 | + 'welcome', |
|
| 79 | + [$this, 'renderPage'] |
|
| 80 | + ); |
|
| 81 | + remove_submenu_page('edit.php?post_type='.Application::POST_TYPE, 'welcome'); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * @return void |
|
| 86 | - * @see $this->registerPage() |
|
| 87 | - * @callback add_submenu_page |
|
| 88 | - */ |
|
| 89 | - public function renderPage() |
|
| 90 | - { |
|
| 91 | - $tabs = glsr()->filterArray('addon/welcome/tabs', [ |
|
| 92 | - 'getting-started' => _x('Getting Started', 'admin-text', 'site-reviews'), |
|
| 93 | - 'whatsnew' => _x('What\'s New', 'admin-text', 'site-reviews'), |
|
| 94 | - 'upgrade-guide' => _x('Upgrade Guide', 'admin-text', 'site-reviews'), |
|
| 95 | - 'support' => _x('Support', 'admin-text', 'site-reviews'), |
|
| 96 | - ]); |
|
| 97 | - glsr()->render('pages/welcome/index', [ |
|
| 98 | - 'data' => ['context' => []], |
|
| 99 | - 'http_referer' => (string) wp_get_referer(), |
|
| 100 | - 'tabs' => $tabs, |
|
| 101 | - 'template' => glsr(Template::class), |
|
| 102 | - ]); |
|
| 103 | - } |
|
| 84 | + /** |
|
| 85 | + * @return void |
|
| 86 | + * @see $this->registerPage() |
|
| 87 | + * @callback add_submenu_page |
|
| 88 | + */ |
|
| 89 | + public function renderPage() |
|
| 90 | + { |
|
| 91 | + $tabs = glsr()->filterArray('addon/welcome/tabs', [ |
|
| 92 | + 'getting-started' => _x('Getting Started', 'admin-text', 'site-reviews'), |
|
| 93 | + 'whatsnew' => _x('What\'s New', 'admin-text', 'site-reviews'), |
|
| 94 | + 'upgrade-guide' => _x('Upgrade Guide', 'admin-text', 'site-reviews'), |
|
| 95 | + 'support' => _x('Support', 'admin-text', 'site-reviews'), |
|
| 96 | + ]); |
|
| 97 | + glsr()->render('pages/welcome/index', [ |
|
| 98 | + 'data' => ['context' => []], |
|
| 99 | + 'http_referer' => (string) wp_get_referer(), |
|
| 100 | + 'tabs' => $tabs, |
|
| 101 | + 'template' => glsr(Template::class), |
|
| 102 | + ]); |
|
| 103 | + } |
|
| 104 | 104 | } |
@@ -9,158 +9,158 @@ |
||
| 9 | 9 | |
| 10 | 10 | class OptionManager |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var array |
|
| 14 | - */ |
|
| 15 | - protected $options; |
|
| 12 | + /** |
|
| 13 | + * @var array |
|
| 14 | + */ |
|
| 15 | + protected $options; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @return string |
|
| 19 | - */ |
|
| 20 | - public static function databaseKey($version = null) |
|
| 21 | - { |
|
| 22 | - if (1 == $version) { |
|
| 23 | - return 'geminilabs_site_reviews_settings'; |
|
| 24 | - } |
|
| 25 | - if (2 == $version) { |
|
| 26 | - return 'geminilabs_site_reviews-v2'; |
|
| 27 | - } |
|
| 28 | - if (null === $version) { |
|
| 29 | - $version = explode('.', glsr()->version); |
|
| 30 | - $version = array_shift($version); |
|
| 31 | - } |
|
| 32 | - return Str::snakeCase(Application::ID.'-v'.intval($version)); |
|
| 33 | - } |
|
| 17 | + /** |
|
| 18 | + * @return string |
|
| 19 | + */ |
|
| 20 | + public static function databaseKey($version = null) |
|
| 21 | + { |
|
| 22 | + if (1 == $version) { |
|
| 23 | + return 'geminilabs_site_reviews_settings'; |
|
| 24 | + } |
|
| 25 | + if (2 == $version) { |
|
| 26 | + return 'geminilabs_site_reviews-v2'; |
|
| 27 | + } |
|
| 28 | + if (null === $version) { |
|
| 29 | + $version = explode('.', glsr()->version); |
|
| 30 | + $version = array_shift($version); |
|
| 31 | + } |
|
| 32 | + return Str::snakeCase(Application::ID.'-v'.intval($version)); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @return array |
|
| 37 | - */ |
|
| 38 | - public function all() |
|
| 39 | - { |
|
| 40 | - if (empty($this->options)) { |
|
| 41 | - $this->reset(); |
|
| 42 | - } |
|
| 43 | - return $this->options; |
|
| 44 | - } |
|
| 35 | + /** |
|
| 36 | + * @return array |
|
| 37 | + */ |
|
| 38 | + public function all() |
|
| 39 | + { |
|
| 40 | + if (empty($this->options)) { |
|
| 41 | + $this->reset(); |
|
| 42 | + } |
|
| 43 | + return $this->options; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @param string $path |
|
| 48 | - * @return bool |
|
| 49 | - */ |
|
| 50 | - public function delete($path) |
|
| 51 | - { |
|
| 52 | - $keys = explode('.', $path); |
|
| 53 | - $last = array_pop($keys); |
|
| 54 | - $options = $this->all(); |
|
| 55 | - $pointer = &$options; |
|
| 56 | - foreach ($keys as $key) { |
|
| 57 | - if (!isset($pointer[$key]) || !is_array($pointer[$key])) { |
|
| 58 | - continue; |
|
| 59 | - } |
|
| 60 | - $pointer = &$pointer[$key]; |
|
| 61 | - } |
|
| 62 | - unset($pointer[$last]); |
|
| 63 | - return $this->set($options); |
|
| 64 | - } |
|
| 46 | + /** |
|
| 47 | + * @param string $path |
|
| 48 | + * @return bool |
|
| 49 | + */ |
|
| 50 | + public function delete($path) |
|
| 51 | + { |
|
| 52 | + $keys = explode('.', $path); |
|
| 53 | + $last = array_pop($keys); |
|
| 54 | + $options = $this->all(); |
|
| 55 | + $pointer = &$options; |
|
| 56 | + foreach ($keys as $key) { |
|
| 57 | + if (!isset($pointer[$key]) || !is_array($pointer[$key])) { |
|
| 58 | + continue; |
|
| 59 | + } |
|
| 60 | + $pointer = &$pointer[$key]; |
|
| 61 | + } |
|
| 62 | + unset($pointer[$last]); |
|
| 63 | + return $this->set($options); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @param string $path |
|
| 68 | - * @param mixed $fallback |
|
| 69 | - * @param string $cast |
|
| 70 | - * @return mixed |
|
| 71 | - */ |
|
| 72 | - public function get($path = '', $fallback = '', $cast = '') |
|
| 73 | - { |
|
| 74 | - $result = Arr::get($this->all(), $path, $fallback); |
|
| 75 | - return Helper::castTo($cast, $result); |
|
| 76 | - } |
|
| 66 | + /** |
|
| 67 | + * @param string $path |
|
| 68 | + * @param mixed $fallback |
|
| 69 | + * @param string $cast |
|
| 70 | + * @return mixed |
|
| 71 | + */ |
|
| 72 | + public function get($path = '', $fallback = '', $cast = '') |
|
| 73 | + { |
|
| 74 | + $result = Arr::get($this->all(), $path, $fallback); |
|
| 75 | + return Helper::castTo($cast, $result); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * @param string $path |
|
| 80 | - * @param mixed $fallback |
|
| 81 | - * @return bool |
|
| 82 | - */ |
|
| 83 | - public function getBool($path, $fallback = false) |
|
| 84 | - { |
|
| 85 | - return $this->get($path, $fallback, 'bool'); |
|
| 86 | - } |
|
| 78 | + /** |
|
| 79 | + * @param string $path |
|
| 80 | + * @param mixed $fallback |
|
| 81 | + * @return bool |
|
| 82 | + */ |
|
| 83 | + public function getBool($path, $fallback = false) |
|
| 84 | + { |
|
| 85 | + return $this->get($path, $fallback, 'bool'); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * @param string $path |
|
| 90 | - * @param mixed $fallback |
|
| 91 | - * @param string $cast |
|
| 92 | - * @return mixed |
|
| 93 | - */ |
|
| 94 | - public function getWP($path, $fallback = '', $cast = '') |
|
| 95 | - { |
|
| 96 | - $option = get_option($path, $fallback); |
|
| 97 | - if (empty($option)) { |
|
| 98 | - $option = $fallback; |
|
| 99 | - } |
|
| 100 | - return Helper::castTo($cast, $option); |
|
| 101 | - } |
|
| 88 | + /** |
|
| 89 | + * @param string $path |
|
| 90 | + * @param mixed $fallback |
|
| 91 | + * @param string $cast |
|
| 92 | + * @return mixed |
|
| 93 | + */ |
|
| 94 | + public function getWP($path, $fallback = '', $cast = '') |
|
| 95 | + { |
|
| 96 | + $option = get_option($path, $fallback); |
|
| 97 | + if (empty($option)) { |
|
| 98 | + $option = $fallback; |
|
| 99 | + } |
|
| 100 | + return Helper::castTo($cast, $option); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - public function json() |
|
| 107 | - { |
|
| 108 | - return json_encode($this->all()); |
|
| 109 | - } |
|
| 103 | + /** |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + public function json() |
|
| 107 | + { |
|
| 108 | + return json_encode($this->all()); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * @return array |
|
| 113 | - */ |
|
| 114 | - public function normalize(array $options = []) |
|
| 115 | - { |
|
| 116 | - $options = wp_parse_args( |
|
| 117 | - Arr::flatten($options), |
|
| 118 | - glsr(DefaultsManager::class)->defaults() |
|
| 119 | - ); |
|
| 120 | - array_walk($options, function (&$value) { |
|
| 121 | - if (!is_string($value)) { |
|
| 122 | - return; |
|
| 123 | - } |
|
| 124 | - $value = wp_kses($value, wp_kses_allowed_html('post')); |
|
| 125 | - }); |
|
| 126 | - return Arr::convertFromDotNotation($options); |
|
| 127 | - } |
|
| 111 | + /** |
|
| 112 | + * @return array |
|
| 113 | + */ |
|
| 114 | + public function normalize(array $options = []) |
|
| 115 | + { |
|
| 116 | + $options = wp_parse_args( |
|
| 117 | + Arr::flatten($options), |
|
| 118 | + glsr(DefaultsManager::class)->defaults() |
|
| 119 | + ); |
|
| 120 | + array_walk($options, function (&$value) { |
|
| 121 | + if (!is_string($value)) { |
|
| 122 | + return; |
|
| 123 | + } |
|
| 124 | + $value = wp_kses($value, wp_kses_allowed_html('post')); |
|
| 125 | + }); |
|
| 126 | + return Arr::convertFromDotNotation($options); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * @return bool |
|
| 131 | - */ |
|
| 132 | - public function isRecaptchaEnabled() |
|
| 133 | - { |
|
| 134 | - $integration = $this->get('settings.submissions.recaptcha.integration'); |
|
| 135 | - return 'all' == $integration || ('guest' == $integration && !is_user_logged_in()); |
|
| 136 | - } |
|
| 129 | + /** |
|
| 130 | + * @return bool |
|
| 131 | + */ |
|
| 132 | + public function isRecaptchaEnabled() |
|
| 133 | + { |
|
| 134 | + $integration = $this->get('settings.submissions.recaptcha.integration'); |
|
| 135 | + return 'all' == $integration || ('guest' == $integration && !is_user_logged_in()); |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * @return array |
|
| 140 | - */ |
|
| 141 | - public function reset() |
|
| 142 | - { |
|
| 143 | - $options = $this->getWP(static::databaseKey(), []); |
|
| 144 | - if (!is_array($options) || empty($options)) { |
|
| 145 | - delete_option(static::databaseKey()); |
|
| 146 | - $options = Arr::consolidate(glsr()->defaults ?: []); |
|
| 147 | - } |
|
| 148 | - $this->options = $options; |
|
| 149 | - } |
|
| 138 | + /** |
|
| 139 | + * @return array |
|
| 140 | + */ |
|
| 141 | + public function reset() |
|
| 142 | + { |
|
| 143 | + $options = $this->getWP(static::databaseKey(), []); |
|
| 144 | + if (!is_array($options) || empty($options)) { |
|
| 145 | + delete_option(static::databaseKey()); |
|
| 146 | + $options = Arr::consolidate(glsr()->defaults ?: []); |
|
| 147 | + } |
|
| 148 | + $this->options = $options; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * @param string|array $pathOrOptions |
|
| 153 | - * @param mixed $value |
|
| 154 | - * @return bool |
|
| 155 | - */ |
|
| 156 | - public function set($pathOrOptions, $value = '') |
|
| 157 | - { |
|
| 158 | - if (is_string($pathOrOptions)) { |
|
| 159 | - $pathOrOptions = Arr::set($this->all(), $pathOrOptions, $value); |
|
| 160 | - } |
|
| 161 | - if ($result = update_option(static::databaseKey(), (array) $pathOrOptions)) { |
|
| 162 | - $this->reset(); |
|
| 163 | - } |
|
| 164 | - return $result; |
|
| 165 | - } |
|
| 151 | + /** |
|
| 152 | + * @param string|array $pathOrOptions |
|
| 153 | + * @param mixed $value |
|
| 154 | + * @return bool |
|
| 155 | + */ |
|
| 156 | + public function set($pathOrOptions, $value = '') |
|
| 157 | + { |
|
| 158 | + if (is_string($pathOrOptions)) { |
|
| 159 | + $pathOrOptions = Arr::set($this->all(), $pathOrOptions, $value); |
|
| 160 | + } |
|
| 161 | + if ($result = update_option(static::databaseKey(), (array) $pathOrOptions)) { |
|
| 162 | + $this->reset(); |
|
| 163 | + } |
|
| 164 | + return $result; |
|
| 165 | + } |
|
| 166 | 166 | } |
@@ -8,266 +8,266 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Updater |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $apiUrl; |
|
| 15 | - /** |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - protected $data; |
|
| 19 | - /** |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $plugin; |
|
| 23 | - /** |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - protected $transientName; |
|
| 11 | + /** |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $apiUrl; |
|
| 15 | + /** |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + protected $data; |
|
| 19 | + /** |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $plugin; |
|
| 23 | + /** |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + protected $transientName; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @param string $apiUrl |
|
| 30 | - * @param string $file |
|
| 31 | - */ |
|
| 32 | - public function __construct($apiUrl, $file, array $data = []) |
|
| 33 | - { |
|
| 34 | - if (!function_exists('get_plugin_data')) { |
|
| 35 | - require_once ABSPATH.WPINC.'/plugin.php'; |
|
| 36 | - } |
|
| 37 | - $this->apiUrl = trailingslashit(glsr()->filterString('addon/api-url', $apiUrl)); |
|
| 38 | - $this->data = wp_parse_args($data, get_plugin_data($file)); |
|
| 39 | - $this->plugin = plugin_basename($file); |
|
| 40 | - $this->transientName = Application::PREFIX.md5(Arr::get($data, 'TextDomain')); |
|
| 41 | - } |
|
| 28 | + /** |
|
| 29 | + * @param string $apiUrl |
|
| 30 | + * @param string $file |
|
| 31 | + */ |
|
| 32 | + public function __construct($apiUrl, $file, array $data = []) |
|
| 33 | + { |
|
| 34 | + if (!function_exists('get_plugin_data')) { |
|
| 35 | + require_once ABSPATH.WPINC.'/plugin.php'; |
|
| 36 | + } |
|
| 37 | + $this->apiUrl = trailingslashit(glsr()->filterString('addon/api-url', $apiUrl)); |
|
| 38 | + $this->data = wp_parse_args($data, get_plugin_data($file)); |
|
| 39 | + $this->plugin = plugin_basename($file); |
|
| 40 | + $this->transientName = Application::PREFIX.md5(Arr::get($data, 'TextDomain')); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @return object |
|
| 45 | - */ |
|
| 46 | - public function activateLicense(array $data = []) |
|
| 47 | - { |
|
| 48 | - return $this->request('activate_license', $data); |
|
| 49 | - } |
|
| 43 | + /** |
|
| 44 | + * @return object |
|
| 45 | + */ |
|
| 46 | + public function activateLicense(array $data = []) |
|
| 47 | + { |
|
| 48 | + return $this->request('activate_license', $data); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @return object |
|
| 53 | - */ |
|
| 54 | - public function checkLicense(array $data = []) |
|
| 55 | - { |
|
| 56 | - $response = $this->request('check_license', $data); |
|
| 57 | - if ('valid' === Arr::get($response, 'license')) { |
|
| 58 | - $this->getPluginUpdate(true); |
|
| 59 | - } |
|
| 60 | - return $response; |
|
| 61 | - } |
|
| 51 | + /** |
|
| 52 | + * @return object |
|
| 53 | + */ |
|
| 54 | + public function checkLicense(array $data = []) |
|
| 55 | + { |
|
| 56 | + $response = $this->request('check_license', $data); |
|
| 57 | + if ('valid' === Arr::get($response, 'license')) { |
|
| 58 | + $this->getPluginUpdate(true); |
|
| 59 | + } |
|
| 60 | + return $response; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @return object |
|
| 65 | - */ |
|
| 66 | - public function deactivateLicense(array $data = []) |
|
| 67 | - { |
|
| 68 | - return $this->request('deactivate_license', $data); |
|
| 69 | - } |
|
| 63 | + /** |
|
| 64 | + * @return object |
|
| 65 | + */ |
|
| 66 | + public function deactivateLicense(array $data = []) |
|
| 67 | + { |
|
| 68 | + return $this->request('deactivate_license', $data); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * @param false|object|array $result |
|
| 73 | - * @param string $action |
|
| 74 | - * @param object $args |
|
| 75 | - * @return mixed |
|
| 76 | - */ |
|
| 77 | - public function filterPluginUpdateDetails($result, $action, $args) |
|
| 78 | - { |
|
| 79 | - if ('plugin_information' != $action |
|
| 80 | - || Arr::get($this->data, 'TextDomain') != Arr::get($args, 'slug')) { |
|
| 81 | - return $result; |
|
| 82 | - } |
|
| 83 | - if ($updateInfo = $this->getPluginUpdate()) { |
|
| 84 | - return $this->modifyUpdateDetails($updateInfo); |
|
| 85 | - } |
|
| 86 | - return $result; |
|
| 87 | - } |
|
| 71 | + /** |
|
| 72 | + * @param false|object|array $result |
|
| 73 | + * @param string $action |
|
| 74 | + * @param object $args |
|
| 75 | + * @return mixed |
|
| 76 | + */ |
|
| 77 | + public function filterPluginUpdateDetails($result, $action, $args) |
|
| 78 | + { |
|
| 79 | + if ('plugin_information' != $action |
|
| 80 | + || Arr::get($this->data, 'TextDomain') != Arr::get($args, 'slug')) { |
|
| 81 | + return $result; |
|
| 82 | + } |
|
| 83 | + if ($updateInfo = $this->getPluginUpdate()) { |
|
| 84 | + return $this->modifyUpdateDetails($updateInfo); |
|
| 85 | + } |
|
| 86 | + return $result; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @param object $transient |
|
| 91 | - * @return object |
|
| 92 | - */ |
|
| 93 | - public function filterPluginUpdates($transient) |
|
| 94 | - { |
|
| 95 | - if ($updateInfo = $this->getPluginUpdate()) { |
|
| 96 | - return $this->modifyPluginUpdates($transient, $updateInfo); |
|
| 97 | - } |
|
| 98 | - return $transient; |
|
| 99 | - } |
|
| 89 | + /** |
|
| 90 | + * @param object $transient |
|
| 91 | + * @return object |
|
| 92 | + */ |
|
| 93 | + public function filterPluginUpdates($transient) |
|
| 94 | + { |
|
| 95 | + if ($updateInfo = $this->getPluginUpdate()) { |
|
| 96 | + return $this->modifyPluginUpdates($transient, $updateInfo); |
|
| 97 | + } |
|
| 98 | + return $transient; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * @return object |
|
| 103 | - */ |
|
| 104 | - public function getVersion(array $data = []) |
|
| 105 | - { |
|
| 106 | - return $this->request('get_version', $data); |
|
| 107 | - } |
|
| 101 | + /** |
|
| 102 | + * @return object |
|
| 103 | + */ |
|
| 104 | + public function getVersion(array $data = []) |
|
| 105 | + { |
|
| 106 | + return $this->request('get_version', $data); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * @return void |
|
| 111 | - */ |
|
| 112 | - public function init() |
|
| 113 | - { |
|
| 114 | - if ($this->apiUrl === trailingslashit(home_url())) { |
|
| 115 | - return; |
|
| 116 | - } |
|
| 117 | - add_filter('plugins_api', [$this, 'filterPluginUpdateDetails'], 10, 3); |
|
| 118 | - add_filter('pre_set_site_transient_update_plugins', [$this, 'filterPluginUpdates'], 999); |
|
| 119 | - add_action('load-update-core.php', [$this, 'onForceUpdateCheck'], 9); |
|
| 120 | - add_action('in_plugin_update_message-'.$this->plugin, [$this, 'renderLicenseMissingLink']); |
|
| 121 | - } |
|
| 109 | + /** |
|
| 110 | + * @return void |
|
| 111 | + */ |
|
| 112 | + public function init() |
|
| 113 | + { |
|
| 114 | + if ($this->apiUrl === trailingslashit(home_url())) { |
|
| 115 | + return; |
|
| 116 | + } |
|
| 117 | + add_filter('plugins_api', [$this, 'filterPluginUpdateDetails'], 10, 3); |
|
| 118 | + add_filter('pre_set_site_transient_update_plugins', [$this, 'filterPluginUpdates'], 999); |
|
| 119 | + add_action('load-update-core.php', [$this, 'onForceUpdateCheck'], 9); |
|
| 120 | + add_action('in_plugin_update_message-'.$this->plugin, [$this, 'renderLicenseMissingLink']); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * @return bool |
|
| 125 | - */ |
|
| 126 | - public function isLicenseValid() |
|
| 127 | - { |
|
| 128 | - $result = $this->checkLicense(); |
|
| 129 | - return 'valid' === Arr::get($result, 'license'); |
|
| 130 | - } |
|
| 123 | + /** |
|
| 124 | + * @return bool |
|
| 125 | + */ |
|
| 126 | + public function isLicenseValid() |
|
| 127 | + { |
|
| 128 | + $result = $this->checkLicense(); |
|
| 129 | + return 'valid' === Arr::get($result, 'license'); |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * @return void |
|
| 134 | - */ |
|
| 135 | - public function onForceUpdateCheck() |
|
| 136 | - { |
|
| 137 | - if (!filter_input(INPUT_GET, 'force-check')) { |
|
| 138 | - return; |
|
| 139 | - } |
|
| 140 | - foreach (glsr()->addons as $addon) { |
|
| 141 | - try { |
|
| 142 | - glsr($addon)->updater->getPluginUpdate(true); |
|
| 143 | - } catch (\Exception $e) { |
|
| 144 | - glsr_log()->error($e->getMessage()); |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - } |
|
| 132 | + /** |
|
| 133 | + * @return void |
|
| 134 | + */ |
|
| 135 | + public function onForceUpdateCheck() |
|
| 136 | + { |
|
| 137 | + if (!filter_input(INPUT_GET, 'force-check')) { |
|
| 138 | + return; |
|
| 139 | + } |
|
| 140 | + foreach (glsr()->addons as $addon) { |
|
| 141 | + try { |
|
| 142 | + glsr($addon)->updater->getPluginUpdate(true); |
|
| 143 | + } catch (\Exception $e) { |
|
| 144 | + glsr_log()->error($e->getMessage()); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * @return void |
|
| 151 | - */ |
|
| 152 | - public function renderLicenseMissingLink() |
|
| 153 | - { |
|
| 154 | - if (!$this->isLicenseValid()) { |
|
| 155 | - glsr()->render('partials/addons/license-missing'); |
|
| 156 | - } |
|
| 157 | - } |
|
| 149 | + /** |
|
| 150 | + * @return void |
|
| 151 | + */ |
|
| 152 | + public function renderLicenseMissingLink() |
|
| 153 | + { |
|
| 154 | + if (!$this->isLicenseValid()) { |
|
| 155 | + glsr()->render('partials/addons/license-missing'); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * @return false|object |
|
| 161 | - */ |
|
| 162 | - protected function getCachedVersion() |
|
| 163 | - { |
|
| 164 | - return get_transient($this->transientName); |
|
| 165 | - } |
|
| 159 | + /** |
|
| 160 | + * @return false|object |
|
| 161 | + */ |
|
| 162 | + protected function getCachedVersion() |
|
| 163 | + { |
|
| 164 | + return get_transient($this->transientName); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * @param bool $force |
|
| 169 | - * @return false|object |
|
| 170 | - */ |
|
| 171 | - protected function getPluginUpdate($force = false) |
|
| 172 | - { |
|
| 173 | - $version = $this->getCachedVersion(); |
|
| 174 | - if (false === $version || $force) { |
|
| 175 | - $version = $this->getVersion(); |
|
| 176 | - $this->setCachedVersion($version); |
|
| 177 | - } |
|
| 178 | - if (isset($version->error)) { |
|
| 179 | - glsr_log()->error($version->error); |
|
| 180 | - return false; |
|
| 181 | - } |
|
| 182 | - return $version; |
|
| 183 | - } |
|
| 167 | + /** |
|
| 168 | + * @param bool $force |
|
| 169 | + * @return false|object |
|
| 170 | + */ |
|
| 171 | + protected function getPluginUpdate($force = false) |
|
| 172 | + { |
|
| 173 | + $version = $this->getCachedVersion(); |
|
| 174 | + if (false === $version || $force) { |
|
| 175 | + $version = $this->getVersion(); |
|
| 176 | + $this->setCachedVersion($version); |
|
| 177 | + } |
|
| 178 | + if (isset($version->error)) { |
|
| 179 | + glsr_log()->error($version->error); |
|
| 180 | + return false; |
|
| 181 | + } |
|
| 182 | + return $version; |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * @param object $transient |
|
| 187 | - * @param object $updateInfo |
|
| 188 | - * @return object |
|
| 189 | - */ |
|
| 190 | - protected function modifyPluginUpdates($transient, $updateInfo) |
|
| 191 | - { |
|
| 192 | - $updateInfo->id = Application::ID.'/'.Arr::get($this->data, 'TextDomain'); |
|
| 193 | - $updateInfo->plugin = $this->plugin; |
|
| 194 | - $updateInfo->requires_php = Arr::get($this->data, 'RequiresPHP'); |
|
| 195 | - $updateInfo->tested = Arr::get($this->data, 'testedTo'); |
|
| 196 | - $transient->checked[$this->plugin] = Arr::get($this->data, 'Version'); |
|
| 197 | - $transient->last_checked = time(); |
|
| 198 | - if (Helper::isGreaterThan($updateInfo->new_version, Arr::get($this->data, 'Version'))) { |
|
| 199 | - unset($transient->no_update[$this->plugin]); |
|
| 200 | - $updateInfo->update = true; |
|
| 201 | - $transient->response[$this->plugin] = $updateInfo; |
|
| 202 | - } else { |
|
| 203 | - unset($transient->response[$this->plugin]); |
|
| 204 | - $transient->no_update[$this->plugin] = $updateInfo; |
|
| 205 | - } |
|
| 206 | - return $transient; |
|
| 207 | - } |
|
| 185 | + /** |
|
| 186 | + * @param object $transient |
|
| 187 | + * @param object $updateInfo |
|
| 188 | + * @return object |
|
| 189 | + */ |
|
| 190 | + protected function modifyPluginUpdates($transient, $updateInfo) |
|
| 191 | + { |
|
| 192 | + $updateInfo->id = Application::ID.'/'.Arr::get($this->data, 'TextDomain'); |
|
| 193 | + $updateInfo->plugin = $this->plugin; |
|
| 194 | + $updateInfo->requires_php = Arr::get($this->data, 'RequiresPHP'); |
|
| 195 | + $updateInfo->tested = Arr::get($this->data, 'testedTo'); |
|
| 196 | + $transient->checked[$this->plugin] = Arr::get($this->data, 'Version'); |
|
| 197 | + $transient->last_checked = time(); |
|
| 198 | + if (Helper::isGreaterThan($updateInfo->new_version, Arr::get($this->data, 'Version'))) { |
|
| 199 | + unset($transient->no_update[$this->plugin]); |
|
| 200 | + $updateInfo->update = true; |
|
| 201 | + $transient->response[$this->plugin] = $updateInfo; |
|
| 202 | + } else { |
|
| 203 | + unset($transient->response[$this->plugin]); |
|
| 204 | + $transient->no_update[$this->plugin] = $updateInfo; |
|
| 205 | + } |
|
| 206 | + return $transient; |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * @param object $updateInfo |
|
| 211 | - * @return object |
|
| 212 | - */ |
|
| 213 | - protected function modifyUpdateDetails($updateInfo) |
|
| 214 | - { |
|
| 215 | - $updateInfo->author = Arr::get($this->data, 'Author'); |
|
| 216 | - $updateInfo->author_profile = Arr::get($this->data, 'AuthorURI'); |
|
| 217 | - $updateInfo->requires = Arr::get($this->data, 'RequiresWP'); |
|
| 218 | - $updateInfo->requires_php = Arr::get($this->data, 'RequiresPHP'); |
|
| 219 | - $updateInfo->tested = Arr::get($this->data, 'testedTo'); |
|
| 220 | - $updateInfo->version = $updateInfo->new_version; |
|
| 221 | - return $updateInfo; |
|
| 222 | - } |
|
| 209 | + /** |
|
| 210 | + * @param object $updateInfo |
|
| 211 | + * @return object |
|
| 212 | + */ |
|
| 213 | + protected function modifyUpdateDetails($updateInfo) |
|
| 214 | + { |
|
| 215 | + $updateInfo->author = Arr::get($this->data, 'Author'); |
|
| 216 | + $updateInfo->author_profile = Arr::get($this->data, 'AuthorURI'); |
|
| 217 | + $updateInfo->requires = Arr::get($this->data, 'RequiresWP'); |
|
| 218 | + $updateInfo->requires_php = Arr::get($this->data, 'RequiresPHP'); |
|
| 219 | + $updateInfo->tested = Arr::get($this->data, 'testedTo'); |
|
| 220 | + $updateInfo->version = $updateInfo->new_version; |
|
| 221 | + return $updateInfo; |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | - /** |
|
| 225 | - * @param \WP_Error|array $response |
|
| 226 | - * @return object |
|
| 227 | - */ |
|
| 228 | - protected function normalizeResponse($response) |
|
| 229 | - { |
|
| 230 | - $body = wp_remote_retrieve_body($response); |
|
| 231 | - if ($data = json_decode($body)) { |
|
| 232 | - $data = array_map('maybe_unserialize', (array) $data); |
|
| 233 | - return (object) $data; |
|
| 234 | - } |
|
| 235 | - $error = is_wp_error($response) |
|
| 236 | - ? $response->get_error_message() |
|
| 237 | - : 'Update server not responding ('.Arr::get($this->data, 'TextDomain').')'; |
|
| 238 | - return (object) ['error' => $error]; |
|
| 239 | - } |
|
| 224 | + /** |
|
| 225 | + * @param \WP_Error|array $response |
|
| 226 | + * @return object |
|
| 227 | + */ |
|
| 228 | + protected function normalizeResponse($response) |
|
| 229 | + { |
|
| 230 | + $body = wp_remote_retrieve_body($response); |
|
| 231 | + if ($data = json_decode($body)) { |
|
| 232 | + $data = array_map('maybe_unserialize', (array) $data); |
|
| 233 | + return (object) $data; |
|
| 234 | + } |
|
| 235 | + $error = is_wp_error($response) |
|
| 236 | + ? $response->get_error_message() |
|
| 237 | + : 'Update server not responding ('.Arr::get($this->data, 'TextDomain').')'; |
|
| 238 | + return (object) ['error' => $error]; |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - /** |
|
| 242 | - * @param string $action activate_license|check_license|deactivate_license|get_version |
|
| 243 | - * @return object |
|
| 244 | - */ |
|
| 245 | - protected function request($action, array $data = []) |
|
| 246 | - { |
|
| 247 | - $data = wp_parse_args($data, $this->data); |
|
| 248 | - $response = wp_remote_post($this->apiUrl, [ |
|
| 249 | - 'body' => [ |
|
| 250 | - 'edd_action' => $action, |
|
| 251 | - 'item_id' => Arr::get($data, 'item_id'), |
|
| 252 | - 'item_name' => Arr::get($data, 'Name'), |
|
| 253 | - 'license' => Arr::get($data, 'license'), |
|
| 254 | - 'slug' => Arr::get($data, 'TextDomain'), |
|
| 255 | - 'url' => home_url(), |
|
| 256 | - ], |
|
| 257 | - 'sslverify' => glsr()->filterBool('sslverify/post', false), |
|
| 258 | - 'timeout' => 15, |
|
| 259 | - ]); |
|
| 260 | - return $this->normalizeResponse($response); |
|
| 261 | - } |
|
| 241 | + /** |
|
| 242 | + * @param string $action activate_license|check_license|deactivate_license|get_version |
|
| 243 | + * @return object |
|
| 244 | + */ |
|
| 245 | + protected function request($action, array $data = []) |
|
| 246 | + { |
|
| 247 | + $data = wp_parse_args($data, $this->data); |
|
| 248 | + $response = wp_remote_post($this->apiUrl, [ |
|
| 249 | + 'body' => [ |
|
| 250 | + 'edd_action' => $action, |
|
| 251 | + 'item_id' => Arr::get($data, 'item_id'), |
|
| 252 | + 'item_name' => Arr::get($data, 'Name'), |
|
| 253 | + 'license' => Arr::get($data, 'license'), |
|
| 254 | + 'slug' => Arr::get($data, 'TextDomain'), |
|
| 255 | + 'url' => home_url(), |
|
| 256 | + ], |
|
| 257 | + 'sslverify' => glsr()->filterBool('sslverify/post', false), |
|
| 258 | + 'timeout' => 15, |
|
| 259 | + ]); |
|
| 260 | + return $this->normalizeResponse($response); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * @param object $version |
|
| 265 | - * @return void |
|
| 266 | - */ |
|
| 267 | - protected function setCachedVersion($version) |
|
| 268 | - { |
|
| 269 | - if (!isset($version->error)) { |
|
| 270 | - set_transient($this->transientName, $version, 3 * HOUR_IN_SECONDS); |
|
| 271 | - } |
|
| 272 | - } |
|
| 263 | + /** |
|
| 264 | + * @param object $version |
|
| 265 | + * @return void |
|
| 266 | + */ |
|
| 267 | + protected function setCachedVersion($version) |
|
| 268 | + { |
|
| 269 | + if (!isset($version->error)) { |
|
| 270 | + set_transient($this->transientName, $version, 3 * HOUR_IN_SECONDS); |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | 273 | } |