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:         do_action('quform_resender_before_create_form', $config, $entry);
112:         do_action("quform_resender_before_create_form_$formId", $config, $entry);
113: 
114:         $form = $this->factory->create($config);
115: 
116:         $form->setValues(array(), true);
117: 
118:         foreach ($entry as $key => $value) {
119:             if (preg_match('/element_(\d+)/', $key, $matches)) {
120:                 $form->setValueFromStorage($matches[1], $value);
121:                 unset($entry[$key]);
122:             }
123:         }
124: 
125:         do_action('quform_pre_resend_notifications', $form, $entry);
126: 
127:         foreach ($form->getNotifications() as $notification) {
128:             if (in_array($notification->getIdentifier(), $identifiers)) {
129:                 do_action('quform_pre_resend_notification', $notification, $form, $entry);
130:                 do_action('quform_pre_resend_notification_' . $notification->getIdentifier(), $notification, $form, $entry);
131: 
132:                 $notification->send();
133: 
134:                 do_action('quform_post_resend_notification', $notification, $form, $entry);
135:                 do_action('quform_post_resend_notification_' . $notification->getIdentifier(), $notification, $form, $entry);
136:             }
137:         }
138: 
139:         do_action('quform_post_resend_notifications', $form, $entry);
140: 
141:         wp_send_json(array(
142:             'type' => 'success',
143:             'message' => __('The notifications have been sent successfully.', 'quform')
144:         ));
145:     }
146: }
147: