Pink-Crab /
Perique-Registerables
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Meta Box model |
||
| 7 | * |
||
| 8 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
| 9 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
| 10 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
| 11 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||
| 12 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
| 13 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
| 14 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 15 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 16 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
| 17 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
| 18 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 19 | * |
||
| 20 | * @author Glynn Quelch <[email protected]> |
||
| 21 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
| 22 | * @package PinkCrab\Registerables |
||
| 23 | */ |
||
| 24 | |||
| 25 | namespace PinkCrab\Registerables; |
||
| 26 | |||
| 27 | class Meta_Box { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The meta box key |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | * @required |
||
| 34 | */ |
||
| 35 | public string $key = ''; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The meta box label/title |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | * @required |
||
| 42 | */ |
||
| 43 | public string $label = ''; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The view callback |
||
| 47 | * |
||
| 48 | * @var callable|null |
||
| 49 | */ |
||
| 50 | public $view; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The view args passed to view. |
||
| 54 | * |
||
| 55 | * @var array<string, mixed> |
||
| 56 | */ |
||
| 57 | public array $view_vars = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The path relative to the defined base path |
||
| 61 | * in config |
||
| 62 | * |
||
| 63 | * @var string|null |
||
| 64 | */ |
||
| 65 | public ?string $view_template = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Screens to display meta box. |
||
| 69 | * |
||
| 70 | * @var array<int, string> |
||
| 71 | * @required |
||
| 72 | */ |
||
| 73 | public array $screen = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Meta box context/position |
||
| 77 | * |
||
| 78 | * @var 'advanced'|'normal'|'side' |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 79 | * @required |
||
| 80 | */ |
||
| 81 | public string $context = 'normal'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * What is the loading priority |
||
| 85 | * |
||
| 86 | * @var 'core'|'default'|'high'|'low' |
||
|
0 ignored issues
–
show
|
|||
| 87 | */ |
||
| 88 | public string $priority = 'default'; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Define any hooks that should fire with the meta box. |
||
| 92 | * |
||
| 93 | * @var array<string, array{callback:callable,priority:int,params:int}> |
||
| 94 | */ |
||
| 95 | public array $actions = array(); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Filter for pushing post specific data into the views |
||
| 99 | * global variable scope |
||
| 100 | * |
||
| 101 | * @var null|callable(\WP_Post $post,array<string, mixed> $args):array<string, mixed> |
||
| 102 | */ |
||
| 103 | public $view_data_filter; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Creates a Meta Box with a defined key. |
||
| 107 | * |
||
| 108 | * @param string $key |
||
| 109 | */ |
||
| 110 | final public function __construct( string $key ) { |
||
| 111 | $this->key = $key; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Creates a full width meta box with a defined key. |
||
| 116 | * |
||
| 117 | * @param string $key |
||
| 118 | * @return self |
||
| 119 | */ |
||
| 120 | public static function normal( string $key ): self { |
||
| 121 | $meta_box = new static( $key ); |
||
| 122 | $meta_box->context = 'normal'; |
||
| 123 | return $meta_box; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Creates a full width meta box with a defined key. |
||
| 128 | * |
||
| 129 | * @param string $key |
||
| 130 | * @return self |
||
| 131 | */ |
||
| 132 | public static function side( string $key ): self { |
||
| 133 | $meta_box = new static( $key ); |
||
| 134 | $meta_box->context = 'side'; |
||
| 135 | return $meta_box; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Sets the label |
||
| 140 | * |
||
| 141 | * @param string $label |
||
| 142 | * @return self |
||
| 143 | */ |
||
| 144 | public function label( string $label ): self { |
||
| 145 | $this->label = $label; |
||
| 146 | return $this; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Sets the screens this meta box will be loaded. |
||
| 151 | * |
||
| 152 | * @param string|array<mixed>|\WP_Screen $screen |
||
| 153 | * @return self |
||
| 154 | */ |
||
| 155 | public function screen( $screen ): self { |
||
| 156 | array_push( $this->screen, $screen ); |
||
| 157 | return $this; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Sets the view args. |
||
| 162 | * |
||
| 163 | * @param array<string, mixed> $view_vars |
||
| 164 | * @return self |
||
| 165 | */ |
||
| 166 | public function view_vars( array $view_vars ): self { |
||
| 167 | $this->view_vars = $view_vars; |
||
| 168 | return $this; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Sets the views template path. |
||
| 173 | * Should be relative to the base path defined in config. |
||
| 174 | * |
||
| 175 | * @param string $view_template |
||
| 176 | * @return self |
||
| 177 | */ |
||
| 178 | public function view_template( string $view_template ): self { |
||
| 179 | $this->view_template = $view_template; |
||
| 180 | return $this; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Sets the view method. |
||
| 185 | * Can be a callable or a function or class|method array. |
||
| 186 | * |
||
| 187 | * @param callable $view_callback |
||
| 188 | * @return self |
||
| 189 | */ |
||
| 190 | public function view( $view_callback ): self { |
||
| 191 | $this->view = $view_callback; |
||
| 192 | return $this; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Adds a acton to be defined as a hook key and callable|function |
||
| 197 | * |
||
| 198 | * @param string $hook |
||
| 199 | * @param callable $callback |
||
| 200 | * @param int $priority |
||
| 201 | * @param int $params |
||
| 202 | * @return self |
||
| 203 | */ |
||
| 204 | public function add_action( string $hook, callable $callback, int $priority = 10, int $params = 1 ): self { |
||
| 205 | $this->actions[ $hook ] = |
||
| 206 | array( |
||
| 207 | 'callback' => $callback, |
||
| 208 | 'priority' => $priority, |
||
| 209 | 'params' => $params, |
||
| 210 | ); |
||
| 211 | return $this; |
||
| 212 | } |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Set $args):args |
||
| 217 | * |
||
| 218 | * @param callable(\WP_Post $post,array<string, mixed> $args):array<string, mixed> $view_data_filter |
||
| 219 | * @return self |
||
| 220 | */ |
||
| 221 | public function view_data_filter( callable $view_data_filter ): self { |
||
| 222 | $this->view_data_filter = $view_data_filter; |
||
| 223 | return $this; |
||
| 224 | } |
||
| 225 | } |
||
| 226 |