| @@ -12,9 +12,26 @@ | ||
| 12 | 12 | $this->register_field( |
| 13 | 13 | 'doc_category', |
| 14 | 14 | 'doc_category_order', |
| 15 | 15 | [ |
| 16 | - 'get_callback' => [ $this, 'doc_category_order' ] | |
| 16 | + 'get_callback' => [ $this, 'doc_category_order' ], | |
| 17 | + 'update_callback' => [ $this, 'update_doc_category_order' ], | |
| 18 | + // Explicit schema — otherwise BaseAPI's default (a string "thumbnail | |
| 19 | + // URL" schema) applies and the React slide-over's integer order write | |
| 20 | + // fails validation with "not of type string". | |
| 21 | + 'schema' => [ | |
| 22 | + 'description' => 'BetterDocs doc category display order.', | |
| 23 | + 'type' => [ 'integer', 'null' ], | |
| 24 | + 'context' => [ 'view', 'edit' ], | |
| 25 | + // Skip the built-in type check and coerce to int|null ourselves | |
| 26 | + // (a null/empty value is a no-op upstream). | |
| 27 | + 'arg_options' => [ | |
| 28 | + 'validate_callback' => '__return_true', | |
| 29 | + 'sanitize_callback' => function ( $value ) { | |
| 30 | + return ( null === $value || '' === $value ) ? null : (int) $value; | |
| 31 | + }, | |
| 32 | + ], | |
| 33 | + ], | |
| 17 | 34 | ] |
| 18 | 35 | ); |
| 19 | 36 | $this->register_field( |
| 20 | 37 | 'doc_category', |
| @@ -63,17 +80,25 @@ | ||
| 63 | 80 | $meta_key = \WPDeveloper\BetterDocs\Utils\Helper::get_meta_key_with_fallback( 'doc_category_order', $object['id'] ); |
| 64 | 81 | $doc_category_order = get_term_meta( $object['id'], $meta_key, true ); |
| 65 | 82 | |
| 66 | 83 | // If still no order found, try the base key as final fallback |
| 67 | - if ( ! $doc_category_order ) { | |
| 84 | + if ( '' === $doc_category_order || null === $doc_category_order ) { | |
| 68 | 85 | $doc_category_order = get_term_meta( $object['id'], 'doc_category_order', true ); |
| 69 | 86 | } |
| 70 | 87 | |
| 71 | - if ( ! $doc_category_order ) { | |
| 88 | + return ( '' === $doc_category_order || null === $doc_category_order ) ? null : (int) $doc_category_order; | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * Persist the display order written from the React Doc Categories slide-over. | |
| 93 | + * Uses the same language-aware meta key as the reader. $term is a WP_Term. | |
| 94 | + */ | |
| 95 | + public function update_doc_category_order( $value, $term ) { | |
| 96 | + if ( null === $value || '' === $value ) { | |
| 72 | 97 | return; |
| 73 | 98 | } |
| 74 | - | |
| 75 | - return $doc_category_order; | |
| 99 | + $meta_key = \WPDeveloper\BetterDocs\Utils\Helper::get_meta_key_with_fallback( 'doc_category_order', $term->term_id ); | |
| 100 | + update_term_meta( $term->term_id, $meta_key, (int) $value ); | |
| 76 | 101 | } |
| 77 | 102 | |
| 78 | 103 | public function thumbnail_image( $object ) { |
| 79 | 104 | $attachment_id = get_term_meta( $object['id'], 'doc_category_image-id', true ); |