prepare_items();
$message = '';
if ('delete' === $table->current_action()) {
$message = '
' . sprintf(__('Forms deleted: %d', "ajaxy-forms"), count((array)$_REQUEST['id'])) . '
';
}
?>
['id' => [], 'class' => []], 'p' => [], 'a' => []]); ?>
prefix . 'form_entries'; // do not forget about tables prefix
$message = '';
$notice = '';
// this is default $item which will be used for new records
$default = array(
'id' => 0,
'name' => '',
'email' => '',
'age' => null,
);
// here we are verifying does this request is post back and have correct nonce
if (isset($_REQUEST['nonce']) && wp_verify_nonce($_REQUEST['nonce'], basename(__FILE__))) {
// combine our default item with request params
$item = shortcode_atts($default, $_REQUEST);
// validate data, and if all ok save item to database
// if id is zero insert otherwise update
$item_valid = $this->validate($item);
if ($item_valid === true) {
if ($item['id'] == 0) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$result = $wpdb->insert($table_name, $item);
$item['id'] = $wpdb->insert_id;
if ($result) {
$message = __('Item was successfully saved', "ajaxy-forms");
} else {
$notice = __('There was an error while saving item', "ajaxy-forms");
}
} else {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$result = $wpdb->update($table_name, $item, array('id' => $item['id']));
if ($result) {
$message = __('Item was successfully updated', "ajaxy-forms");
} else {
$notice = __('There was an error while updating item', "ajaxy-forms");
}
}
} else {
// if $item_valid not true it contains error message(s)
$notice = $item_valid;
}
} else {
// if this is not post back we load item to edit or give new one to create
$item = $default;
if (isset($_REQUEST['id'])) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$item = $wpdb->get_row($wpdb->prepare("SELECT * FROM %i WHERE id = %s", $table_name, $_REQUEST['id']), ARRAY_A);
if (!$item) {
$item = $default;
$notice = __('Item not found', "ajaxy-forms");
}
}
}
// here we adding our custom meta box
add_meta_box('ajaxy_forms_entries_meta_box', 'Data', [$this, 'form_meta_box_handler'], 'ajaxy_form_entry', 'normal', 'default');
?>
', $messages);
}
function form_meta_box_handler($item)
{
?>