← All changes
|
inc/admin/views/meta-boxes/lp-meta-box-functions.php
+41
-11
4.1.7
→
4.4.9
View file →
| @@ -1,6 +1,36 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | + * Resolve post ID for meta box rendering. | |
| 4 | + * | |
| 5 | + * In some admin/AJAX contexts, global $post can be null. | |
| 6 | + * Returning 0 allows callers to safely fall back to default values. | |
| 7 | + * | |
| 8 | + * @return int | |
| 9 | + */ | |
| 10 | +function lp_meta_box_get_post_id() { | |
| 11 | + global $thepostid, $post; | |
| 12 | + | |
| 13 | + if ( ! empty( $thepostid ) ) { | |
| 14 | + return absint( $thepostid ); | |
| 15 | + } | |
| 16 | + | |
| 17 | + if ( is_object( $post ) && ! empty( $post->ID ) ) { | |
| 18 | + $thepostid = absint( $post->ID ); | |
| 19 | + | |
| 20 | + return $thepostid; | |
| 21 | + } | |
| 22 | + | |
| 23 | + $resolved_post_id = absint( get_the_ID() ); | |
| 24 | + | |
| 25 | + if ( $resolved_post_id > 0 ) { | |
| 26 | + $thepostid = $resolved_post_id; | |
| 27 | + } | |
| 28 | + | |
| 29 | + return $resolved_post_id; | |
| 30 | +} | |
| 31 | + | |
| 32 | +/** | |
| 3 | 33 | * Output a text input box. |
| 4 | 34 | * |
| 5 | 35 | * @param array $field |
| 6 | 36 | */ |
| @@ -6,9 +36,9 @@ | ||
| 6 | 36 | */ |
| 7 | 37 | function lp_meta_box_text_input_field( $field ) { |
| 8 | 38 | global $thepostid, $post; |
| 9 | 39 | |
| 10 | - $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; | |
| 40 | + $thepostid = lp_meta_box_get_post_id(); | |
| 11 | 41 | $field['placeholder'] = esc_attr( $field['placeholder'] ?? '' ); |
| 12 | 42 | $field['class'] = esc_attr( $field['class'] ?? '' ); |
| 13 | 43 | $field['style'] = esc_attr( $field['style'] ?? '' ); |
| 14 | 44 | $wrapper_class = esc_attr( $field['wrapper_class'] ?? '' ); |
| @@ -16,10 +46,10 @@ | ||
| 16 | 46 | /** |
| 17 | 47 | * If you want to set default value for input text |
| 18 | 48 | * You must us hook default_{$meta_type}_metadata | Read more get_metadata_default() function |
| 19 | 49 | */ |
| 20 | - $value_exists = LP_Database::getInstance()->check_key_postmeta_exists( $thepostid, $field['id'] ); | |
| 21 | - $value = get_post_meta( $thepostid, $field['id'], true ); | |
| 50 | + $value_exists = $thepostid > 0 ? LP_Database::getInstance()->check_key_postmeta_exists( $thepostid, $field['id'] ) : false; | |
| 51 | + $value = $thepostid > 0 ? get_post_meta( $thepostid, $field['id'], true ) : ''; | |
| 22 | 52 | $field['value'] = $value_exists ? $value : ( $field['default'] ?? '' ); |
| 23 | 53 | $field['id'] = esc_attr( $field['id'] ?? '' ); |
| 24 | 54 | $field['type_input'] = esc_attr( $field['type_input'] ?? 'text' ); |
| 25 | 55 | $field['desc_tip'] = esc_attr( $field['desc_tip'] ?? '' ); |
| @@ -70,15 +100,15 @@ | ||
| 70 | 100 | */ |
| 71 | 101 | function lp_meta_box_textarea_field( $field ) { |
| 72 | 102 | global $thepostid, $post; |
| 73 | 103 | |
| 74 | - $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; | |
| 104 | + $thepostid = lp_meta_box_get_post_id(); | |
| 75 | 105 | $field['id'] = esc_attr( $field['id'] ?? '' ); |
| 76 | 106 | $field['placeholder'] = esc_attr( $field['placeholder'] ?? '' ); |
| 77 | 107 | $field['class'] = esc_attr( $field['class'] ?? 'short' ); |
| 78 | 108 | $field['style'] = esc_attr( $field['style'] ?? '' ); |
| 79 | - $value_exists = LP_Database::getInstance()->check_key_postmeta_exists( $thepostid, $field['id'] ); | |
| 80 | - $value = get_post_meta( $thepostid, $field['id'], true ); | |
| 109 | + $value_exists = $thepostid > 0 ? LP_Database::getInstance()->check_key_postmeta_exists( $thepostid, $field['id'] ) : false; | |
| 110 | + $value = $thepostid > 0 ? get_post_meta( $thepostid, $field['id'], true ) : ''; | |
| 81 | 111 | $field['value'] = esc_textarea( $value_exists ? $value : ( $field['default'] ?? '' ) ); |
| 82 | 112 | $field['desc_tip'] = esc_attr( $field['desc_tip'] ?? '' ); |
| 83 | 113 | $field['name'] = esc_attr( $field['name'] ?? $field['id'] ); |
| 84 | 114 | |
| @@ -123,9 +153,9 @@ | ||
| 123 | 153 | */ |
| 124 | 154 | function lp_meta_box_checkbox_field( $field ) { |
| 125 | 155 | global $thepostid, $post; |
| 126 | 156 | |
| 127 | - $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; | |
| 157 | + $thepostid = lp_meta_box_get_post_id(); | |
| 128 | 158 | $field['id'] = esc_attr( $field['id'] ?? '' ); |
| 129 | 159 | $field['class'] = esc_attr( $field['class'] ?? '' ); |
| 130 | 160 | $field['style'] = esc_attr( $field['style'] ?? '' ); |
| 131 | 161 | $wrapper_class = esc_attr( $field['wrapper_class'] ?? '' ); |
| @@ -182,9 +212,9 @@ | ||
| 182 | 212 | */ |
| 183 | 213 | function lp_meta_box_select_field( $field = array() ) { |
| 184 | 214 | global $thepostid, $post; |
| 185 | 215 | |
| 186 | - $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; | |
| 216 | + $thepostid = lp_meta_box_get_post_id(); | |
| 187 | 217 | $default = ( ! get_post_meta( |
| 188 | 218 | $thepostid, |
| 189 | 219 | $field['id'], |
| 190 | 220 | true |
| @@ -256,9 +286,9 @@ | ||
| 256 | 286 | */ |
| 257 | 287 | function lp_meta_box_radio_field( $field ) { |
| 258 | 288 | global $thepostid, $post; |
| 259 | 289 | |
| 260 | - $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; | |
| 290 | + $thepostid = lp_meta_box_get_post_id(); | |
| 261 | 291 | $field['class'] = $field['class'] ?? 'select'; |
| 262 | 292 | $field['style'] = $field['style'] ?? ''; |
| 263 | 293 | $field['wrapper_class'] = $field['wrapper_class'] ?? ''; |
| 264 | 294 | $field['default'] = ( ! get_post_meta( |
| @@ -304,9 +334,9 @@ | ||
| 304 | 334 | |
| 305 | 335 | function lp_meta_box_file_input_field( $field ) { |
| 306 | 336 | global $thepostid, $post; |
| 307 | 337 | |
| 308 | - $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; | |
| 338 | + $thepostid = lp_meta_box_get_post_id(); | |
| 309 | 339 | $field['class'] = isset( $field['class'] ) ? $field['class'] : 'short'; |
| 310 | 340 | $field['style'] = isset( $field['style'] ) ? $field['style'] : ''; |
| 311 | 341 | $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; |
| 312 | 342 | $field['default'] = ( ! get_post_meta( |
| @@ -391,9 +421,9 @@ | ||
| 391 | 421 | */ |
| 392 | 422 | function lp_meta_box_duration_field( $field ) { |
| 393 | 423 | global $thepostid, $post; |
| 394 | 424 | |
| 395 | - $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; | |
| 425 | + $thepostid = lp_meta_box_get_post_id(); | |
| 396 | 426 | $field['placeholder'] = $field['placeholder'] ?? ''; |
| 397 | 427 | $field['class'] = $field['class'] ?? 'short'; |
| 398 | 428 | $field['style'] = $field['style'] ?? ''; |
| 399 | 429 | $field['wrapper_class'] = $field['wrapper_class'] ?? ''; |