1: <?php
2:
3: 4: 5:
6: class Quform_Notification_Resender
7: {
8: 9: 10:
11: protected $repository;
12:
13: 14: 15:
16: protected $factory;
17:
18: 19: 20: 21:
22: public function __construct(Quform_Repository $repository, Quform_Form_Factory $factory)
23: {
24: $this->repository = $repository;
25: $this->factory = $factory;
26: }
27:
28: 29: 30:
31: protected function validateResendRequest()
32: {
33: if (
34: ! Quform::isPostRequest() ||
35: ! isset($_POST['data']) ||
36: ! is_string($_POST['data'])
37: ) {
38: wp_send_json(array(
39: 'type' => 'error',
40: 'message' => __('Bad request', 'quform')
41: ));
42: }
43:
44: if ( ! current_user_can('quform_resend_notifications')) {
45: wp_send_json(array(
46: 'type' => 'error',
47: 'message' => __('Insufficient permissions', 'quform')
48: ));
49: }
50:
51: if ( ! check_ajax_referer('quform_resend_notifications', false, false)) {
52: wp_send_json(array(
53: 'type' => 'error',
54: 'message' => __('Nonce check failed', 'quform')
55: ));
56: }
57: }
58:
59: 60: 61:
62: public function resend()
63: {
64: $this->validateResendRequest();
65:
66: $data = json_decode(wp_unslash($_POST['data']), true);
67:
68: if ( ! is_array($data)) {
69: wp_send_json(array(
70: 'type' => 'error',
71: 'message' => __('Bad request', 'quform')
72: ));
73: }
74:
75: $entryId = isset($data['eid']) && is_numeric($data['eid']) ? (int) $data['eid'] : null;
76:
77: $entry = $this->repository->findEntry($entryId);
78:
79: if ( ! is_array($entry)) {
80: wp_send_json(array(
81: 'type' => 'error',
82: 'message' => __('Entry not found', 'quform')
83: ));
84: }
85:
86: $identifiers = isset($data['identifiers']) && is_array($data['identifiers']) ? array_map('sanitize_key', $data['identifiers']) : array();
87:
88: if ( ! count($identifiers)) {
89: wp_send_json(array(
90: 'type' => 'error',
91: 'errors' => array(
92: 'qfb-resend-notifications-identifiers' => __('This field is required', 'quform')
93: )
94: ));
95: }
96:
97: $formId = $this->repository->getFormIdFromEntryId($entryId);
98:
99: $config = $this->repository->getConfig($formId);
100:
101: if ( ! is_array($config)) {
102: wp_send_json(array(
103: 'type' => 'error',
104: 'message' => __('Form not found', 'quform')
105: ));
106: }
107:
108: $config['entryId'] = $entryId;
109: $config['environment'] = 'viewEntry';
110:
111: $form = $this->factory->create($config);
112:
113: $form->setValues(array(), true);
114:
115: foreach ($entry as $key => $value) {
116: if (preg_match('/element_(\d+)/', $key, $matches)) {
117: $form->setValueFromStorage($matches[1], $value);
118: unset($entry[$key]);
119: }
120: }
121:
122: do_action('quform_pre_resend_notifications', $form, $entry);
123:
124: foreach ($form->getNotifications() as $notification) {
125: if (in_array($notification->getIdentifier(), $identifiers)) {
126: do_action('quform_pre_resend_notification', $notification, $form, $entry);
127: do_action('quform_pre_resend_notification_' . $notification->getIdentifier(), $notification, $form, $entry);
128:
129: $notification->send();
130:
131: do_action('quform_post_resend_notification', $notification, $form, $entry);
132: do_action('quform_post_resend_notification_' . $notification->getIdentifier(), $notification, $form, $entry);
133: }
134: }
135:
136: do_action('quform_post_resend_notifications', $form, $entry);
137:
138: wp_send_json(array(
139: 'type' => 'success',
140: 'message' => __('The notifications have been sent successfully.', 'quform')
141: ));
142: }
143: }
144: