Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 8 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Async_Option_Template | |
0.00% |
0 / 8 |
|
0.00% |
0 / 8 |
56 | |
0.00% |
0 / 1 |
| setup_storage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| transform | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| parse | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| sanitize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
0 | |||
| has_errors | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_errors | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| add_error | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack\Packages\Async_Option; |
| 4 | |
| 5 | use Automattic\Jetpack\Packages\Async_Option\Storage\Storage; |
| 6 | use Automattic\Jetpack\Packages\Async_Option\Storage\WP_Option; |
| 7 | |
| 8 | /** |
| 9 | * Any registered async option should use this async option template |
| 10 | * and extend the methods that are necessary. |
| 11 | */ |
| 12 | abstract class Async_Option_Template { |
| 13 | |
| 14 | /** |
| 15 | * The default value if no option is found. |
| 16 | */ |
| 17 | public static $DEFAULT_VALUE = false; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase |
| 18 | |
| 19 | /** |
| 20 | * @var string[] |
| 21 | */ |
| 22 | private $errors = array(); |
| 23 | |
| 24 | /** |
| 25 | * Setup storage mechanism that subscribes to `Storage` contract |
| 26 | * |
| 27 | * @param $storage_namespace string |
| 28 | * |
| 29 | * @return Storage |
| 30 | */ |
| 31 | public function setup_storage( $storage_namespace ) { |
| 32 | return new WP_Option( $storage_namespace ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * On get, |
| 37 | * Transform the value when it's retrieved from storage. |
| 38 | * |
| 39 | * @param $value |
| 40 | * |
| 41 | * @return mixed |
| 42 | */ |
| 43 | public function transform( $value ) { |
| 44 | return $value; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * 1) On submit, |
| 49 | * Parse the received value before it's validated. |
| 50 | * |
| 51 | * This can be used for things like json_decode or casting types. |
| 52 | * |
| 53 | * @param $value |
| 54 | * |
| 55 | * @return mixed |
| 56 | */ |
| 57 | public function parse( $value ) { |
| 58 | return $value; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * 2) On submit, |
| 63 | * Validate a received value before storing it. |
| 64 | * |
| 65 | * Use this method to provide feedback if the value isn't valid. |
| 66 | * Using `$this->add_error()` will prevent the value from being stored. |
| 67 | * |
| 68 | * @param $value |
| 69 | * |
| 70 | * @return bool - Return true on success, false on failure. |
| 71 | */ |
| 72 | public function validate( $value ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Used in subclasses. |
| 73 | return ! $this->has_errors(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * 3) On submit, |
| 78 | * Sanitize the value before inserting it into storage. |
| 79 | * |
| 80 | * This is the only required method of any async option |
| 81 | * because values shouldn't be stored unsanitized. |
| 82 | * Wash your values, friends. |
| 83 | * |
| 84 | * @param $value |
| 85 | * |
| 86 | * @return mixed |
| 87 | */ |
| 88 | abstract public function sanitize( $value ); |
| 89 | |
| 90 | /** |
| 91 | * |
| 92 | * Methods to help handling errors |
| 93 | */ |
| 94 | public function has_errors() { |
| 95 | return ! empty( $this->errors ); |
| 96 | } |
| 97 | |
| 98 | public function get_errors() { |
| 99 | return implode( "\n", $this->errors ); |
| 100 | } |
| 101 | |
| 102 | protected function add_error( $message ) { |
| 103 | $this->errors[] = $message; |
| 104 | } |
| 105 | } |