Skip to content

Commit 1e88c9e

Browse files
authored
Merge pull request #7 from humanmade/connected-entities-meta-box
Connected entities meta box
2 parents 2b1feb2 + 713bda8 commit 1e88c9e

File tree

4 files changed

+151
-6
lines changed

4 files changed

+151
-6
lines changed

inc/admin-single.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
}

inc/admin.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ function settings_init() {
5959
register_setting( 'entitybase_settings', OPTION_DBPEDIA_BLOCKLIST );
6060
register_setting( 'entitybase_settings', OPTION_FREEBASE_BLOCKLIST );
6161
register_setting( 'entitybase_settings', OPTION_MIN_CONFIDENCE );
62-
register_setting( 'entitybase_settings', OPTION_MIN_RELEVANCE );
62+
register_setting( 'entitybase_settings', OPTION_MIN_RELEVANCE, [
63+
'sanitize_callback' => __NAMESPACE__ . '\\sanitize_percentage',
64+
] );
6365

6466
add_settings_section(
6567
'entitybase_settings_section',
@@ -220,10 +222,21 @@ function minimum_confidence_callback() {
220222

221223
// Callback function for the minimum relevance field.
222224
function minimum_relevance_callback() {
225+
$relevance = floatval( get_option( OPTION_MIN_RELEVANCE, 0 ) ) * 100;
223226
printf(
224-
'<input type="number" min="0" max="1" step="0.01" name="%s" value="%s" />',
227+
'<input type="number" min="0" max="100" step="0.1" name="%s" value="%s" />&percnt;',
225228
esc_attr( OPTION_MIN_RELEVANCE ),
226-
esc_attr( get_option( OPTION_MIN_RELEVANCE, 0 ) )
229+
esc_attr( $relevance )
227230
);
228-
printf( '<p class="description">%s</p>', esc_html__( 'Between 0 and 1', 'entitybase' ) );
231+
printf( '<p class="description">%s</p>', esc_html__( 'Enter a percentage between 0 and 100.', 'entitybase' ) );
232+
}
233+
234+
/**
235+
* Sanitize a percentage.
236+
*
237+
* @param mixed $value The value to sanitize.
238+
* @return float The sanitized value.
239+
*/
240+
function sanitize_percentage( $value ): float {
241+
return max( 0, min( 1, floatval( $value ) / 100 ) );
229242
}

inc/utils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ function get_entities_for_post( WP_Post $post, array $query_args = [] ): array {
201201
}
202202

203203
// Order entities by relevance
204-
usort( $entity_meta_keys, function( $a, $b ) use ( $meta ){
205-
return $meta[ $a ] <=> $meta[ $b ];
204+
usort( $entity_meta_keys, function( $a, $b ) use ( $meta ) {
205+
return floatval( $meta[ $b ][0] ) <=> floatval( $meta[ $a ][0] );
206206
} );
207207

208208
// Extract slugs from meta keys.

plugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require_once __DIR__ . '/inc/namespace.php';
1818

1919
require_once __DIR__ . '/inc/admin.php';
20+
require_once __DIR__ . '/inc/admin-single.php';
2021
require_once __DIR__ . '/inc/cleanup.php';
2122
require_once __DIR__ . '/inc/cli.php';
2223
require_once __DIR__ . '/inc/export.php';
@@ -25,6 +26,7 @@
2526

2627
setup();
2728
Admin\setup();
29+
AdminSingle\setup();
2830
Cleanup\setup();
2931
CLI\setup();
3032
Export\setup();

0 commit comments

Comments
 (0)