1: <?php
2:
3: 4: 5:
6: class Quform_Entry_List_Settings
7: {
8: 9: 10:
11: protected $repository;
12:
13: 14: 15:
16: public function __construct(Quform_Repository $repository)
17: {
18: $this->repository = $repository;
19: }
20:
21: 22: 23:
24: public function save()
25: {
26: $this->validateSaveRequest();
27: $this->handleSaveRequest();
28: }
29:
30: 31: 32:
33: protected function validateSaveRequest()
34: {
35: if ( ! Quform::isPostRequest() ||
36: ! isset($_POST['per_page'], $_POST['columns'], $_POST['id']) ||
37: ! is_string($_POST['per_page']) ||
38: ! is_array($_POST['columns']) ||
39: ! is_numeric($_POST['id'])
40: ) {
41: wp_send_json(array(
42: 'type' => 'error',
43: 'message' => __('Bad request', 'quform')
44: ));
45: }
46:
47: if ( ! check_ajax_referer('quform_save_entries_table_settings', false, false)) {
48: wp_send_json(array(
49: 'type' => 'error',
50: 'message' => __('Nonce check failed', 'quform')
51: ));
52: }
53: }
54:
55: 56: 57:
58: protected function handleSaveRequest()
59: {
60: if ( $_POST['per_page'] === '') {
61: wp_send_json(array(
62: 'type' => 'error',
63: 'errors' => array('qfb_entries_per_page' => __('This field is required', 'quform'))
64: ));
65: }
66:
67: if ( ! is_numeric($_POST['per_page'])) {
68: wp_send_json(array(
69: 'type' => 'error',
70: 'errors' => array('qfb_entries_per_page' => __('Value must be numeric', 'quform'))
71: ));
72: }
73:
74: $perPage = (int) $_POST['per_page'];
75:
76: if ($perPage < 1) {
77: wp_send_json(array(
78: 'type' => 'error',
79: 'errors' => array('qfb_entries_per_page' => __('Value must be greater than 1', 'quform'))
80: ));
81: }
82:
83: if ($perPage > 1000000) {
84: wp_send_json(array(
85: 'type' => 'error',
86: 'errors' => array('qfb_entries_per_page' => __('Value must be less than 1000000', 'quform'))
87: ));
88: }
89:
90: update_user_meta(get_current_user_id(), 'quform_entries_per_page', $perPage);
91:
92: $config = $this->repository->getConfig((int) $_POST['id']);
93:
94: if ( ! is_array($config)) {
95: wp_send_json(array(
96: 'type' => 'error',
97: 'message' => __('Could not find the form config', 'quform')
98: ));
99: }
100:
101: $config['entriesTableColumns'] = array_map('sanitize_key', wp_unslash($_POST['columns']));
102:
103: $this->repository->save($config);
104:
105: $labels = isset($_POST['labels']) && is_array($_POST['labels']) ? wp_unslash($_POST['labels']) : array();
106:
107: $this->repository->setFormEntryLabels($config['id'], $labels);
108:
109: wp_send_json(array(
110: 'type' => 'success'
111: ));
112: }
113:
114: 115: 116:
117: public function setEntryLabels()
118: {
119: $this->validateSetEntryLabelsRequest();
120: $this->handleSetEntryLabelsRequest();
121: }
122:
123: 124: 125:
126: protected function validateSetEntryLabelsRequest()
127: {
128: if ( ! Quform::isPostRequest() ||
129: ! isset($_POST['_ajax_nonce'], $_POST['entry_label_id'], $_POST['entry_id'], $_POST['adding']) ||
130: ! is_string($_POST['_ajax_nonce']) ||
131: ! is_numeric($_POST['entry_label_id']) ||
132: ! is_numeric($_POST['entry_id']) ||
133: ! in_array($_POST['adding'], array('true', 'false'), true)
134: ) {
135: wp_send_json(array(
136: 'type' => 'error',
137: 'message' => __('Bad request', 'quform')
138: ));
139: }
140:
141: if ( ! check_ajax_referer('quform_set_entry_labels', false, false)) {
142: wp_send_json(array(
143: 'type' => 'error',
144: 'message' => __('Nonce check failed', 'quform')
145: ));
146: }
147: }
148:
149: 150: 151:
152: protected function handleSetEntryLabelsRequest()
153: {
154: if ($_POST['adding'] == 'false') {
155: $this->repository->deleteEntryEntryLabel((int) $_POST['entry_id'], (int) $_POST['entry_label_id']);
156: } else {
157: $this->repository->addEntryEntryLabel((int) $_POST['entry_id'], (int) $_POST['entry_label_id']);
158: }
159:
160: wp_send_json(array(
161: 'type' => 'success'
162: ));
163: }
164: }
165: