| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post type Admin API file. |
| 4 |
* |
| 5 |
* @package WordPress Plugin Template/Includes |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Admin API class. |
| 14 |
*/ |
| 15 |
class SQLite_Object_Cache_Admin_API { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor function |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
add_action( 'save_post', array( $this, 'save_meta_boxes' ), 10, 1 ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Add meta box to the dashboard. |
| 26 |
* |
| 27 |
* @param string $id Unique ID for metabox. |
| 28 |
* @param string $title Display title of metabox. |
| 29 |
* @param array $post_types Post types to which this metabox applies. |
| 30 |
* @param string $context Context in which to display this metabox ('advanced' or 'side'). |
| 31 |
* @param string $priority Priority of this metabox ('default', 'low' or 'high'). |
| 32 |
* @param array $callback_args Any axtra arguments that will be passed to the display function for this metabox. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function add_meta_box( $id = '', $title = '', $post_types = array(), $context = 'advanced', $priority = 'default', $callback_args = null ) { |
| 37 |
|
| 38 |
// Get post type(s). |
| 39 |
if ( ! is_array( $post_types ) ) { |
| 40 |
$post_types = array( $post_types ); |
| 41 |
} |
| 42 |
|
| 43 |
// Generate each metabox. |
| 44 |
foreach ( $post_types as $post_type ) { |
| 45 |
add_meta_box( $id, $title, array( $this, 'meta_box_content' ), $post_type, $context, $priority, $callback_args ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Display metabox content |
| 51 |
* |
| 52 |
* @param object $post Post object. |
| 53 |
* @param array $args Arguments unique to this metabox. |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function meta_box_content( $post, $args ) { |
| 58 |
|
| 59 |
$fields = apply_filters( $post->post_type . '_custom_fields', array(), $post->post_type ); |
| 60 |
|
| 61 |
if ( ! is_array( $fields ) || 0 === count( $fields ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
echo '<div class="custom-field-panel">' . PHP_EOL; |
| 66 |
|
| 67 |
foreach ( $fields as $field ) { |
| 68 |
|
| 69 |
if ( ! isset( $field['metabox'] ) ) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! is_array( $field['metabox'] ) ) { |
| 74 |
$field['metabox'] = array( $field['metabox'] ); |
| 75 |
} |
| 76 |
|
| 77 |
if ( in_array( $args['id'], $field['metabox'], true ) ) { |
| 78 |
$this->display_meta_box_field( $field, $post ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
echo '</div>' . PHP_EOL; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Dispay field in metabox |
| 87 |
* |
| 88 |
* @param array $field Field data. |
| 89 |
* @param object $post Post object. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function display_meta_box_field( $field = array(), $post = null ) { |
| 94 |
|
| 95 |
if ( ! is_array( $field ) || 0 === count( $field ) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
echo '<p class="form-field"><label for="' . esc_attr( $field['id'] ) . '">' . esc_html( $field['label'] ) . '</label>'; |
| 99 |
$this->echo_field( $field, $post ); |
| 100 |
echo '</p>' . PHP_EOL; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Generate HTML for displaying fields. |
| 105 |
* |
| 106 |
* @param mixed $data Data array. |
| 107 |
* @param object $post Post object. |
| 108 |
*/ |
| 109 |
public function echo_field( $data = array(), $post = null ) { |
| 110 |
|
| 111 |
// Get field info. |
| 112 |
if ( isset( $data['field'] ) ) { |
| 113 |
$field = $data['field']; |
| 114 |
} else { |
| 115 |
$field = $data; |
| 116 |
} |
| 117 |
|
| 118 |
$option_name = ''; |
| 119 |
if ( isset( $data['option'] ) ) { |
| 120 |
$option_name = $data['option']; |
| 121 |
} |
| 122 |
|
| 123 |
/* Get saved data. */ |
| 124 |
$field_id = $field['id']; |
| 125 |
$data = null; |
| 126 |
|
| 127 |
/* Data to display, if set */ |
| 128 |
$option = get_option( $option_name, array() ); |
| 129 |
if ( is_array( $option ) && array_key_exists( $field_id, $option ) ) { |
| 130 |
$data = $option[ $field_id ]; |
| 131 |
} |
| 132 |
|
| 133 |
/* the reset element sets the value of the field, overriding whatever is in the option */ |
| 134 |
if ( array_key_exists( 'reset', $field ) ) { |
| 135 |
$data = $field['reset']; |
| 136 |
} |
| 137 |
|
| 138 |
/* Show default data if no option saved and default is supplied. */ |
| 139 |
if ( null === $data && isset( $field['default'] ) ) { |
| 140 |
$data = $field['default']; |
| 141 |
} elseif ( null === $data ) { |
| 142 |
$data = ''; |
| 143 |
} |
| 144 |
|
| 145 |
/* CSS class array */ |
| 146 |
$classes = array(); |
| 147 |
if ( array_key_exists( 'cssclass', $field ) ) { |
| 148 |
$classes = is_array( $field['cssclass'] ) ? $field['cssclass'] : array( $field['cssclass'] ); |
| 149 |
} |
| 150 |
|
| 151 |
echo '<input '; |
| 152 |
echo 'id="' . esc_attr( $field['id'] ) . '" '; |
| 153 |
$this->echo_classes( $classes ) . ' '; |
| 154 |
echo 'name="' . esc_attr( $option_name ) . '[' . esc_attr( $field_id ) . ']" '; |
| 155 |
if ( array_key_exists( 'placeholder', $field ) ) { |
| 156 |
echo 'placeholder="' . esc_attr( $field['placeholder'] ) . '" '; |
| 157 |
} |
| 158 |
switch ( $field['type'] ) { |
| 159 |
|
| 160 |
case 'text': |
| 161 |
case 'url': |
| 162 |
case 'email': |
| 163 |
echo 'type="text" '; |
| 164 |
echo 'value="' . esc_attr( $data ) . '" '; |
| 165 |
break; |
| 166 |
|
| 167 |
case 'number': |
| 168 |
echo 'type="number" '; |
| 169 |
echo 'value="' . esc_attr( $data ) . '" '; |
| 170 |
if ( isset( $field['min'] ) ) { |
| 171 |
echo 'min="' . esc_attr( $field['min'] ) . '" '; |
| 172 |
} |
| 173 |
if ( isset( $field['max'] ) ) { |
| 174 |
echo 'max="' . esc_attr( $field['max'] ) . '" '; |
| 175 |
} |
| 176 |
if ( isset( $field['step'] ) ) { |
| 177 |
echo 'step="' . esc_attr( $field['step'] ) . '" '; |
| 178 |
} |
| 179 |
break; |
| 180 |
|
| 181 |
case 'password': |
| 182 |
case 'hidden': |
| 183 |
echo 'type="' . esc_attr( $field['type'] ) . '" '; |
| 184 |
echo 'value="' . esc_attr( $data ) . '" '; |
| 185 |
break; |
| 186 |
|
| 187 |
case 'checkbox': |
| 188 |
echo 'type="checkbox" '; |
| 189 |
if ( 'on' === $data ) { |
| 190 |
echo 'checked="checked" '; |
| 191 |
} |
| 192 |
break; |
| 193 |
} |
| 194 |
echo '>' . PHP_EOL; |
| 195 |
|
| 196 |
if ( ! $post ) { |
| 197 |
echo '<label for="' . esc_attr( $field['id'] ) . '">' . PHP_EOL; |
| 198 |
} |
| 199 |
|
| 200 |
echo '<span class="description">' . esc_html( $field['description'] ) . '</span>' . PHP_EOL; |
| 201 |
|
| 202 |
if ( ! $post ) { |
| 203 |
echo '</label>' . PHP_EOL; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Given an array of CSS class names ['foo', 'bar'] echo class="foo bar". |
| 209 |
* |
| 210 |
* If the array is empty just echo a space. |
| 211 |
* |
| 212 |
* @param array $classes |
| 213 |
* |
| 214 |
* @return void |
| 215 |
*/ |
| 216 |
private function echo_classes( array $classes ) { |
| 217 |
if ( count( $classes ) > 0 ) { |
| 218 |
echo 'class="'; |
| 219 |
echo implode( ' ', array_map( static function ( $class ) { |
| 220 |
return esc_attr( $class ); |
| 221 |
}, $classes ) ); |
| 222 |
echo '"'; |
| 223 |
} |
| 224 |
echo ' '; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Save metabox fields. |
| 229 |
* |
| 230 |
* @param integer $post_id Post ID. |
| 231 |
* |
| 232 |
* @return void |
| 233 |
*/ |
| 234 |
public function save_meta_boxes( $post_id = 0 ) { |
| 235 |
|
| 236 |
if ( ! $post_id ) { |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
$post_type = get_post_type( $post_id ); |
| 241 |
|
| 242 |
$fields = apply_filters( $post_type . '_custom_fields', array(), $post_type ); |
| 243 |
|
| 244 |
if ( ! is_array( $fields ) || 0 === count( $fields ) ) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
foreach ( $fields as $field ) { |
| 249 |
if ( isset( $_REQUEST[ $field['id'] ] ) ) { |
| 250 |
update_post_meta( $post_id, $field['id'], $this->sanitize_field( $_REQUEST[ $field['id'] ], $field['type'] ) ); |
| 251 |
} else { |
| 252 |
update_post_meta( $post_id, $field['id'], '' ); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Sanitize form field |
| 259 |
* |
| 260 |
* @param string $data Submitted value. |
| 261 |
* @param string $type Type of field to sanitize. |
| 262 |
* |
| 263 |
* @return string Sanitized value |
| 264 |
*/ |
| 265 |
public function sanitize_field( $data = '', $type = 'text' ) { |
| 266 |
|
| 267 |
switch ( $type ) { |
| 268 |
case 'text': |
| 269 |
$data = sanitize_text_field( $data ); |
| 270 |
break; |
| 271 |
case 'url': |
| 272 |
$data = sanitize_url( $data ); |
| 273 |
break; |
| 274 |
case 'email': |
| 275 |
$data = sanitize_email( $data ); |
| 276 |
break; |
| 277 |
default: |
| 278 |
$data = sanitize_key ($data); |
| 279 |
break; |
| 280 |
} |
| 281 |
|
| 282 |
return $data; |
| 283 |
} |
| 284 |
|
| 285 |
} |
| 286 |
|