settings = Plugin::get_settings( 'taxonomies' );
add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] );
if( !current_user_can( 'manage_categories' ) ) {
return;
}
if( empty( $this->settings['enable_taxonomy_term_metadata_box'] ) ) {
return;
}
add_action( 'init', [ $this, 'init' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
add_action( 'admin_footer', [ $this, 'render_modal' ] );
add_action( 'wp_ajax_adminease_get_term_metadata', [ $this, 'ajax_get_term_metadata' ] );
add_action( 'wp_ajax_adminease_update_term_metadata', [ $this, 'ajax_update_term_metadata' ] );
add_action( 'wp_ajax_adminease_delete_term_metadata', [ $this, 'ajax_delete_term_metadata' ] );
}
/**
* Initialize taxonomies on init hook to ensure all custom taxonomies are registered.
*/
public function init() {
$this->taxonomies = apply_filters( 'adminease_term_metadata_box_allowed_taxonomies', Utils::get_taxonomies( [ 'base' ] ) );
foreach( $this->taxonomies as $taxonomy_key => $taxonomy_label ) {
add_action( "{$taxonomy_key}_edit_form", [ $this, 'render_metabox' ], 10, 2 );
}
}
/**
* Modifies and returns the settings fields for taxonomy features.
*
* @param array $fields The existing configuration fields to be updated with additional options.
*
* @return array The updated configuration fields, including the option to add metadata boxes to taxonomy terms.
*/
public function adminease_settings_fields( array $fields ): array {
$fields['taxonomies']['fields'][] = [
'type' => 'switch',
'id' => 'enable-taxonomy-term-metadata-box',
'name' => 'adminease[taxonomies][enable_taxonomy_term_metadata_box]',
'value' => $this->settings['enable_taxonomy_term_metadata_box'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input toggle-field',
'label' => __( 'Enable Taxonomy Term Metadata Box', 'adminease' ),
'description' => __( 'Enable a metadata box on taxonomy term edit screens to view and manage custom fields easily.', 'adminease' ),
];
return $fields;
}
/**
* Renders the meta box content.
*
* @param WP_Term $term Current taxonomy term object.
* @param string $taxonomy Current taxonomy slug.
*
* @return void
*/
public function render_metabox( WP_Term $term, string $taxonomy ): void {
$term_id = $term->term_id;
if( !$term_id ) {
echo '
';
esc_html_e( 'Invalid term.', 'adminease' );
echo '
';
return;
}
?>
` doesn't close the WordPress "Update" form early.
*
* @return void
*/
public function render_modal(): void {
global $pagenow;
if( 'term.php' !== $pagenow ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$taxonomy = isset( $_GET['taxonomy'] ) ? sanitize_key( $_GET['taxonomy'] ) : '';
if( !$taxonomy || !array_key_exists( $taxonomy, $this->taxonomies ) ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$term_id = isset( $_GET['tag_ID'] ) ? absint( $_GET['tag_ID'] ) : 0;
?>
taxonomies ) ) {
return;
}
wp_enqueue_style(
ADMINEASE_NAME . 'TaxonomyTermMetadataBox',
ADMINEASE_PLUGIN_URL . 'assets/css/AdminEaseTaxonomyTermMetadataBox.css',
[ ADMINEASE_NAME . 'NativeBox' ],
filemtime( ADMINEASE_DIR . 'assets/css/AdminEaseTaxonomyTermMetadataBox.css' )
);
wp_enqueue_script(
ADMINEASE_NAME . 'TaxonomyTermMetadataBox',
ADMINEASE_PLUGIN_URL . 'assets/js/AdminEaseTaxonomyTermMetadataBox.js',
[ 'jquery' ],
filemtime( ADMINEASE_DIR . 'assets/js/AdminEaseTaxonomyTermMetadataBox.js' ),
true
);
wp_localize_script(
ADMINEASE_NAME . 'TaxonomyTermMetadataBox',
ADMINEASE_NAME . 'TaxonomyTermMetadataBoxAjaxObj',
[
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'security' => [
'getTermMetadata' => wp_create_nonce( 'adminease_get_term_metadata' ),
'updateTermMetadata' => wp_create_nonce( 'adminease_update_term_metadata' ),
'deleteTermMetadata' => wp_create_nonce( 'adminease_delete_term_metadata' ),
],
'i18n' => [
'metadataModalTitleEdit' => esc_html__( 'Edit Metadata', 'adminease' ),
'metadataModalTitleAdd' => esc_html__( 'Add New Metadata', 'adminease' ),
'metadataConfirmDelete' => esc_html__( 'Are you sure you want to delete this metadata?', 'adminease' ),
'metadataLoadError' => esc_html__( 'Failed to load metadata. Please refresh the page and try again.', 'adminease' ),
'metadataSaveError' => esc_html__( 'Failed to save metadata. Please try again.', 'adminease' ),
'metadataDeleteError' => esc_html__( 'Failed to delete metadata. Please try again.', 'adminease' ),
'metadataSaveSuccess' => esc_html__( 'Metadata saved successfully.', 'adminease' ),
'metadataDeleteSuccess' => esc_html__( 'Metadata deleted successfully.', 'adminease' ),
'metadataKeyRequired' => esc_html__( 'Meta key is required.', 'adminease' ),
'metadataNoMetadata' => esc_html__( 'No metadata found for this term.', 'adminease' ),
'metadataEdit' => esc_html__( 'Edit', 'adminease' ),
'metadataDelete' => esc_html__( 'Delete', 'adminease' ),
'metadataInvalidJSON' => esc_html__( 'Invalid JSON format. Please check your value.', 'adminease' ),
'metadataLoading' => esc_html__( 'Loading metadata...', 'adminease' ),
'metadataTableHeaderKey' => esc_html__( 'Meta Key', 'adminease' ),
'metadataTableHeaderValue' => esc_html__( 'Meta Value', 'adminease' ),
'metadataTableHeaderActions' => esc_html__( 'Actions', 'adminease' ),
'metadataProtectedKey' => esc_html__( 'Protected meta key', 'adminease' ),
'metadataSearch' => esc_html__( 'Search metadata...', 'adminease' ),
'metadataSortAZ' => esc_html__( 'Sort A-Z', 'adminease' ),
'metadataSortZA' => esc_html__( 'Sort Z-A', 'adminease' ),
'metadataNoResults' => esc_html__( 'No metadata found matching your search.', 'adminease' ),
],
]
);
}
/**
* AJAX handler to get term metadata.
*
* @return void
*/
public function ajax_get_term_metadata(): void {
if( !isset( $_POST['security'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['security'] ) ), 'adminease_get_term_metadata' ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Security check failed. Refresh the page and try again.', 'adminease' ) ] );
}
$term_id = isset( $_POST['term_id'] ) ? absint( $_POST['term_id'] ) : 0;
if( !$term_id || !current_user_can( 'manage_categories' ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Invalid term or insufficient permissions.', 'adminease' ) ] );
}
$all_meta = get_term_meta( $term_id );
$metadata = [];
foreach( $all_meta as $key => $values ) {
$value = $values[0] ?? '';
$display_value = $this->format_meta_value( $value );
$metadata[] = [
'key' => $key,
'value' => $value,
'display_value' => $display_value,
'is_serialized' => is_serialized( $value ),
'is_protected' => substr( $key, 0, 1 ) === '_',
];
}
wp_send_json_success( [ 'metadata' => $metadata ] );
}
/**
* AJAX handler to update term metadata.
*
* @return void
*/
public function ajax_update_term_metadata(): void {
if( !isset( $_POST['security'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['security'] ) ), 'adminease_update_term_metadata' ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Security check failed. Refresh the page and try again.', 'adminease' ) ] );
}
$term_id = isset( $_POST['term_id'] ) ? absint( $_POST['term_id'] ) : 0;
$meta_key = isset( $_POST['meta_key'] ) ? sanitize_text_field( wp_unslash( $_POST['meta_key'] ) ) : '';
$meta_value = isset( $_POST['meta_value'] ) ? wp_unslash( $_POST['meta_value'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$original_key = isset( $_POST['original_key'] ) ? sanitize_text_field( wp_unslash( $_POST['original_key'] ) ) : '';
if( !$term_id || !current_user_can( 'manage_categories' ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Invalid term or insufficient permissions.', 'adminease' ) ] );
}
if( empty( $meta_key ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Meta key is required.', 'adminease' ) ] );
}
$processed_value = $this->process_meta_value( $meta_value );
if( !empty( $original_key ) && $original_key !== $meta_key ) {
delete_term_meta( $term_id, $original_key );
}
update_term_meta( $term_id, $meta_key, $processed_value );
wp_send_json_success( [ 'message' => esc_html__( 'Metadata updated successfully.', 'adminease' ) ] );
}
/**
* AJAX handler to delete term metadata.
*
* @return void
*/
public function ajax_delete_term_metadata(): void {
if( !isset( $_POST['security'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['security'] ) ), 'adminease_delete_term_metadata' ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Security check failed. Refresh the page and try again.', 'adminease' ) ] );
}
$term_id = isset( $_POST['term_id'] ) ? absint( $_POST['term_id'] ) : 0;
$meta_key = isset( $_POST['meta_key'] ) ? sanitize_text_field( wp_unslash( $_POST['meta_key'] ) ) : '';
if( !$term_id || !current_user_can( 'manage_categories' ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Invalid term or insufficient permissions.', 'adminease' ) ] );
}
if( empty( $meta_key ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Meta key is required.', 'adminease' ) ] );
}
delete_term_meta( $term_id, $meta_key );
wp_send_json_success( [ 'message' => esc_html__( 'Metadata deleted successfully.', 'adminease' ) ] );
}
/**
* Formats meta value for display.
*
* @param mixed $value The meta value.
*
* @return string Formatted value.
*/
private function format_meta_value( $value ): string {
$unserialized = maybe_unserialize( $value );
if( is_array( $unserialized ) || is_object( $unserialized ) ) {
return wp_json_encode( $unserialized, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
}
return (string) $value;
}
/**
* Processes meta value from form input.
* Detects JSON and converts it to proper PHP structure.
*
* @param mixed $value The input value.
*
* @return mixed Processed value.
*/
private function process_meta_value( $value ) {
if( is_serialized( $value ) ) {
return $value;
}
$decoded = json_decode( $value, true );
if( json_last_error() === JSON_ERROR_NONE && ( is_array( $decoded ) || is_object( $decoded ) ) ) {
return $decoded;
}
return sanitize_textarea_field( $value );
}
}