|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Admin Entities Panel |
| 4 | + * |
| 5 | + * Display connected entities in the post edit screen. |
| 6 | + * |
| 7 | + * @package EntityBase |
| 8 | + */ |
| 9 | + |
| 10 | +namespace EntityBase\AdminSingle; |
| 11 | + |
| 12 | +use WP_Post; |
| 13 | +use function EntityBase\Utils\get_entities_for_post; |
| 14 | + |
| 15 | +function setup(): void { |
| 16 | + add_action( 'add_meta_boxes', __NAMESPACE__ . '\\add_entities_meta_box' ); |
| 17 | + add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\admin_css' ); |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Add the entities meta box to the post edit screen. |
| 22 | + */ |
| 23 | +function add_entities_meta_box(): void { |
| 24 | + $allowed_post_types = apply_filters( 'entitybase_allowed_post_types', [ 'post' ] ); |
| 25 | + |
| 26 | + add_meta_box( |
| 27 | + 'entities_meta_box', |
| 28 | + __( 'Entities', 'entity-base' ), |
| 29 | + __NAMESPACE__ . '\\render_entities_meta_box', |
| 30 | + $allowed_post_types, |
| 31 | + 'side', |
| 32 | + 'default' |
| 33 | + ); |
| 34 | + |
| 35 | + // The comments meta box is shown when comment count is greater than 0. |
| 36 | + // As we're using this field to store connected entity count, force hide it. |
| 37 | + remove_meta_box( 'commentsdiv', 'entity', 'normal' ); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Render the entities meta box. |
| 42 | + * |
| 43 | + * @param WP_Post $post The current post object. |
| 44 | + */ |
| 45 | +function render_entities_meta_box( WP_Post $post ): void { |
| 46 | + $entities = get_entities_for_post( $post ); |
| 47 | + |
| 48 | + if ( empty( $entities ) ) { |
| 49 | + echo '<p>' . esc_html__( 'No entities found.', 'entity-base' ) . '</p>'; |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + echo '<div class="entities-table-wrapper">'; |
| 54 | + echo '<table class="widefat">'; |
| 55 | + echo '<thead><tr>'; |
| 56 | + echo '<th>' . esc_html__( 'Entity', 'entity-base' ) . '</th>'; |
| 57 | + echo '<th style="text-align: right;">' . esc_html__( 'Relevance', 'entity-base' ) . '</th>'; |
| 58 | + echo '</tr></thead>'; |
| 59 | + echo '<tbody>'; |
| 60 | + |
| 61 | + foreach ( $entities as $entity ) { |
| 62 | + |
| 63 | + /** |
| 64 | + * Filter the edit link for the entity in the meta box. |
| 65 | + * |
| 66 | + * This filter allows modification of the edit link for the entity displayed in the meta box. |
| 67 | + * |
| 68 | + * @param string $edit_link The edit link for the entity. |
| 69 | + * @param WP_Post $entity The entity object. |
| 70 | + */ |
| 71 | + $edit_link = apply_filters( 'entitybase_entities_meta_box_edit_link', get_edit_post_link( $entity->ID ), $entity ); |
| 72 | + |
| 73 | + echo '<tr>'; |
| 74 | + echo '<td><a href="' . esc_url( $edit_link ) . '">' . esc_html( $entity->post_title ) . '</a></td>'; |
| 75 | + $relevance = (float) get_post_meta( $post->ID, '_entity_rel_' . $entity->post_name, true ) * 100; |
| 76 | + echo '<td style="text-align: right;">' . esc_html( number_format( $relevance, 0 ) ) . '%</td>'; |
| 77 | + echo '</tr>'; |
| 78 | + } |
| 79 | + |
| 80 | + echo '</tbody>'; |
| 81 | + echo '</table>'; |
| 82 | + echo '</div>'; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Add Admin CSS. |
| 87 | + */ |
| 88 | +function admin_css(): void { |
| 89 | + ob_start(); |
| 90 | + ?> |
| 91 | +.entities-table-wrapper { |
| 92 | + overflow-x: auto; |
| 93 | + border: 1px solid #ddd; |
| 94 | +} |
| 95 | + |
| 96 | +#entities_meta_box table { |
| 97 | + width: 100%; |
| 98 | + border-collapse: collapse; |
| 99 | + margin: -1px 0 0 -1px; |
| 100 | + border-right: none; |
| 101 | + border-bottom: none; |
| 102 | +} |
| 103 | + |
| 104 | +#entities_meta_box th { |
| 105 | + font-weight: bold; |
| 106 | +} |
| 107 | + |
| 108 | +#entities_meta_box th, |
| 109 | +#entities_meta_box td { |
| 110 | + padding: 8px; |
| 111 | + border: 1px solid #ddd; |
| 112 | +} |
| 113 | + |
| 114 | +#entities_meta_box th:last-child, |
| 115 | +#entities_meta_box td:last-child { |
| 116 | + border-right: none; |
| 117 | +} |
| 118 | + |
| 119 | +#entities_meta_box tr:last-child td { |
| 120 | + border-bottom: none; |
| 121 | +} |
| 122 | + |
| 123 | +#entities_meta_box th { |
| 124 | + background-color: #f9f9f9; |
| 125 | +} |
| 126 | + <?php |
| 127 | + |
| 128 | + $admin_css = trim( ob_get_clean() ); |
| 129 | + wp_add_inline_style( 'wp-admin', $admin_css ); |
| 130 | +} |
0 commit comments