add_action( 'add_meta_boxes', $this, 'add_meta_boxes' );
$loader->add_action( 'save_post_acc_publication', $this, 'save_meta', 10, 2 );
$loader->add_filter( 'manage_acc_publication_posts_columns', $this, 'register_columns' );
$loader->add_action( 'manage_acc_publication_posts_custom_column', $this, 'render_column', 10, 2 );
}
// -------------------------------------------------------------------------
// Meta boxes
// -------------------------------------------------------------------------
public function add_meta_boxes(): void {
add_meta_box(
'acc_publication_details',
__( 'Publication Details', 'academiccrew' ),
[ $this, 'render_details_box' ],
'acc_publication',
'normal',
'high'
);
add_meta_box(
'acc_publication_identifiers',
__( 'Identifiers & Source', 'academiccrew' ),
[ $this, 'render_identifiers_box' ],
'acc_publication',
'normal',
'default'
);
}
// -------------------------------------------------------------------------
// Render: Publication Details
// -------------------------------------------------------------------------
public function render_details_box( \WP_Post $post ): void {
wp_nonce_field( 'acc_publication_meta_save', '_acc_pub_nonce' );
$abstract = get_post_meta( $post->ID, '_acc_abstract', true );
$year = get_post_meta( $post->ID, '_acc_year', true );
$venue = get_post_meta( $post->ID, '_acc_venue', true );
$pub_type = get_post_meta( $post->ID, '_acc_pub_type', true );
$authors_raw = get_post_meta( $post->ID, '_acc_authors_json', true );
$pdf_url = get_post_meta( $post->ID, '_acc_pdf_url', true );
$url = get_post_meta( $post->ID, '_acc_url', true );
$pub_types = [
'journal-article' => __( 'Journal Article', 'academiccrew' ),
'conference-paper' => __( 'Conference Paper', 'academiccrew' ),
'book-chapter' => __( 'Book Chapter', 'academiccrew' ),
'book' => __( 'Book', 'academiccrew' ),
'preprint' => __( 'Preprint', 'academiccrew' ),
'thesis' => __( 'Thesis', 'academiccrew' ),
'patent' => __( 'Patent', 'academiccrew' ),
'report' => __( 'Technical Report', 'academiccrew' ),
'other' => __( 'Other', 'academiccrew' ),
];
?>
ID, '_acc_doi', true );
$orcid_id = get_post_meta( $post->ID, '_acc_orcid_work_id', true );
$scholar_id = get_post_meta( $post->ID, '_acc_scholar_id', true );
$iris_id = get_post_meta( $post->ID, '_acc_iris_id', true );
$source_api = get_post_meta( $post->ID, '_acc_source_api', true );
$citations = get_post_meta( $post->ID, '_acc_citations', true );
?>
post_type ) {
return;
}
$fields_text = [
'acc_doi' => '_acc_doi',
'acc_orcid_work_id' => '_acc_orcid_work_id',
'acc_scholar_id' => '_acc_scholar_id',
'acc_iris_id' => '_acc_iris_id',
'acc_source_api' => '_acc_source_api',
'acc_venue' => '_acc_venue',
'acc_pub_type' => '_acc_pub_type',
];
foreach ( $fields_text as $input => $meta_key ) {
if ( isset( $_POST[ $input ] ) ) {
update_post_meta( $post_id, $meta_key,
sanitize_text_field( wp_unslash( $_POST[ $input ] ) ) );
}
}
if ( isset( $_POST['acc_abstract'] ) ) {
update_post_meta( $post_id, '_acc_abstract',
sanitize_textarea_field( wp_unslash( $_POST['acc_abstract'] ) ) );
}
// Authors JSON — json_decode() only parses the syntax, it does not
// sanitize the decoded string values, so each "name" is run through
// sanitize_text_field() before the array is re-encoded and stored.
if ( isset( $_POST['acc_authors_json'] ) ) {
$raw = wp_unslash( $_POST['acc_authors_json'] );
$decoded = json_decode( $raw, true );
$clean = [];
if ( JSON_ERROR_NONE === json_last_error() && is_array( $decoded ) ) {
foreach ( $decoded as $author ) {
if ( is_array( $author ) && isset( $author['name'] ) ) {
$clean[] = [ 'name' => sanitize_text_field( (string) $author['name'] ) ];
}
}
}
update_post_meta( $post_id, '_acc_authors_json', $clean ? wp_json_encode( $clean ) : '' );
}
if ( isset( $_POST['acc_year'] ) ) {
update_post_meta( $post_id, '_acc_year', (int) $_POST['acc_year'] );
}
if ( isset( $_POST['acc_citations'] ) ) {
update_post_meta( $post_id, '_acc_citations', max( 0, (int) $_POST['acc_citations'] ) );
}
foreach ( [ 'acc_url' => '_acc_url', 'acc_pdf_url' => '_acc_pdf_url' ] as $input => $meta_key ) {
if ( isset( $_POST[ $input ] ) ) {
update_post_meta( $post_id, $meta_key,
esc_url_raw( wp_unslash( $_POST[ $input ] ) ) );
}
}
}
// -------------------------------------------------------------------------
// Admin list columns
// -------------------------------------------------------------------------
public function register_columns( array $columns ): array {
return [
'cb' => $columns['cb'],
'title' => __( 'Title', 'academiccrew' ),
'acc_pub_type' => __( 'Type', 'academiccrew' ),
'acc_year' => __( 'Year', 'academiccrew' ),
'acc_venue' => __( 'Venue', 'academiccrew' ),
'acc_doi' => __( 'DOI', 'academiccrew' ),
'date' => $columns['date'],
];
}
public function render_column( string $column, int $post_id ): void {
switch ( $column ) {
case 'acc_pub_type':
echo esc_html( get_post_meta( $post_id, '_acc_pub_type', true ) ?: '—' );
break;
case 'acc_year':
echo esc_html( get_post_meta( $post_id, '_acc_year', true ) ?: '—' );
break;
case 'acc_venue':
$venue = get_post_meta( $post_id, '_acc_venue', true );
echo $venue ? '' . esc_html( wp_trim_words( $venue, 6 ) ) . '' : '—';
break;
case 'acc_doi':
$doi = get_post_meta( $post_id, '_acc_doi', true );
if ( $doi ) {
printf( '%s', esc_html( $doi ) );
} else {
echo '—';
}
break;
}
}
}