true), 'objects');
$filtered_post_types = array_filter($all_post_types, function($post_type) {
return !in_array($post_type->name, array('page', 'attachment'));
});
?>
' . __('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 ($action === 'add' || $action === 'edit') {
$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'])) : [];
if (empty($key) || empty($label) || empty($type)) {
echo '' . __('All fields are required.', 'ait-easy-post-customization') . '
';
return;
}
if ($action === 'add' && array_key_exists($key, $custom_fields)) {
echo '' . __('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);
echo '' . __('Custom field ', 'ait-easy-post-customization') . ($action === 'add' ? __('added', 'ait-easy-post-customization') : __('updated', 'ait-easy-post-customization')) . __(' successfully!', 'ait-easy-post-customization') . '
';
} elseif ($action === 'delete') {
$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 '' . __('Custom field deleted successfully!', 'ait-easy-post-customization') . '
';
}
}
}
// Function to display custom fields management
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']) : '';
$edit_field = $edit_key && isset($custom_fields[$edit_key]) ? $custom_fields[$edit_key] : null;
?>