summary_callback; } /** * Get all fields for the entity type that are relevant for this metabox. * * @param Entity $entity * * @return array */ public function get_fields( Entity $entity ): array { $registry = Util::get_field_registry( $entity ); $type_id = $this->get_entity_type( $entity ); $fields = $registry->get_fields_for_type_filtered( $type_id, true ); $relevant = []; foreach ( $fields as $field ) { if ( isset( $field['box'] ) && $field['box'] === static::class ) { $relevant[] = FieldNormalizer::normalize( $field ); } } return $relevant; } /** * If the section controls dependencies. * * @return bool */ public function controls_dependencies(): bool { return false; } /** * If the section receives dependencies. * * @return bool */ public function receives_dependencies(): bool { return false; } /** * Displays the metabox content. */ public function display( WP_Post $post, array $box ): void { $entity = Util::get_entity_from_post( $post ); if ( ! $entity ) { return; } foreach ( $this->get_fields( $entity ) as $field ) { if ( in_array( $field['id'] ?? '', $this->hidden_fields, true ) ) { continue; } if ( $field['type'] === 'html-editor' ) { /* @var Ad $entity */ $field['value'] = $entity->get_content(); } else { $field['value'] = $entity->get_meta( $field['id'] ?? '', $field['default'] ?? '' ); } FieldRenderer::render( $field ); } } /** * Saves the metabox data when the post is saved. * * @param int $post_id * @param ParamsHandler $params_handler * * @return void * * @internal This method should not be called directly. Security checks (nonce verification, * capability checks) are handled by the calling orchestrator class (e.g., Metabox_Ad). */ public function save( int $post_id, ParamsHandler $params_handler ): void { $post = get_post( $post_id ); if ( ! $post instanceof WP_Post ) { return; } $entity = Util::get_entity_from_post( $post ); if ( ! $entity ) { return; } $fields = $this->get_fields( $entity ); $queue = $fields; $processed_ids = []; while ( ! empty( $queue ) ) { $field = array_shift( $queue ); $id = $field['id'] ?? null; if ( ! $id || in_array( $id, $processed_ids, true ) ) { continue; } $processed_ids[] = $id; $sanitized_value = ParamsHelper::sanitize_type_field( $field, $params_handler ); if ( ( $field['controls_dependencies'] ?? false ) && $sanitized_value !== null && ! is_array( $sanitized_value ) && isset( $field['options'][ $sanitized_value ]['dependent_fields'] ) ) { $dependent_fields = $field['options'][ $sanitized_value ]['dependent_fields']; // One field. if ( isset( $dependent_fields['id'] ) ) { $queue[] = $dependent_fields; } // Multiple fields. else { foreach ( $dependent_fields as $dependent_field ) { $queue[] = $dependent_field; } } } if ( $sanitized_value !== null ) { // update_post_meta() unslashes; backslashes in code fields must survive. update_post_meta( $post_id, Constants::META_PREFIX . $id, wp_slash( $sanitized_value ) ); } } } }