true ), 'objects' );
$filtered_post_types = array_filter(
$all_post_types,
static function ( $post_type ) {
return ! in_array( $post_type->name, array( 'page', 'attachment' ), true );
}
);
// Determine active tab — default to post-types.
$active_tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'post-types'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$allowed_tabs = array( 'post-types', 'custom-fields', 'email-notifications' );
if ( ! in_array( $active_tab, $allowed_tabs, true ) ) {
$active_tab = 'post-types';
}
?>
'
. esc_html__( 'Security check failed. Please try again.', 'ait-easy-post-customization' )
. '';
return;
}
$custom_fields = get_option( 'aitepc_custom_fields', array() );
$action = sanitize_text_field( wp_unslash( $_POST['aitepc_custom_fields_action'] ) );
if ( 'add' === $action || 'edit' === $action ) {
$key = sanitize_key( wp_unslash( $_POST['field_key'] ?? '' ) );
$label = sanitize_text_field( wp_unslash( $_POST['field_label'] ?? '' ) );
$type = sanitize_text_field( wp_unslash( $_POST['field_type'] ?? '' ) );
$post_types = isset( $_POST['field_post_types'] )
? array_map( 'sanitize_text_field', wp_unslash( $_POST['field_post_types'] ) )
: array();
if ( empty( $key ) || empty( $label ) || empty( $type ) ) {
echo ''
. esc_html__( 'All fields are required.', 'ait-easy-post-customization' )
. '
';
return;
}
if ( 'add' === $action && array_key_exists( $key, $custom_fields ) ) {
echo ''
. esc_html__( 'Field key already exists.', 'ait-easy-post-customization' )
. '
';
return;
}
$custom_fields[ $key ] = array(
'label' => $label,
'type' => $type,
'post_types' => $post_types,
);
update_option( 'aitepc_custom_fields', $custom_fields );
$verb = ( 'add' === $action )
? __( 'added', 'ait-easy-post-customization' )
: __( 'updated', 'ait-easy-post-customization' );
echo ''
. sprintf(
/* translators: %s: added or updated */
esc_html__( 'Custom field %s successfully!', 'ait-easy-post-customization' ),
esc_html( $verb )
)
. '
';
} elseif ( 'delete' === $action ) {
$key = sanitize_key( wp_unslash( $_POST['field_key'] ?? '' ) );
if ( array_key_exists( $key, $custom_fields ) ) {
unset( $custom_fields[ $key ] );
update_option( 'aitepc_custom_fields', $custom_fields );
echo ''
. esc_html__( 'Custom field deleted successfully!', 'ait-easy-post-customization' )
. '
';
}
}
}
// ---------------------------------------------------------------------------
// Custom Fields display
// ---------------------------------------------------------------------------
/**
* Render the custom fields add/edit form and the existing fields table.
*
* @param array $filtered_post_types Post type objects (excluding page/attachment).
*/
function aitepc_display_custom_fields_form( $filtered_post_types ) {
$custom_fields = get_option( 'aitepc_custom_fields', array() );
$edit_key = isset( $_GET['edit'] ) ? sanitize_key( $_GET['edit'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$edit_field = ( $edit_key && isset( $custom_fields[ $edit_key ] ) ) ? $custom_fields[ $edit_key ] : null;
?>