1: <?php
2:
3: 4: 5:
6: class Quform_Confirmation
7: {
8: 9: 10:
11: protected $config = array();
12:
13: 14: 15:
16: protected $form;
17:
18: 19: 20: 21:
22: public function __construct(array $config, Quform_Form $form)
23: {
24: $this->setConfig($config);
25: $this->form = $form;
26: }
27:
28: 29: 30: 31: 32:
33: public function getData()
34: {
35: $data = array(
36: 'type' => $this->config('type'),
37: 'message' => $this->getMessage(),
38: 'messageIcon' => $this->config('messageIcon'),
39: 'messagePosition' => $this->config('messagePosition'),
40: 'messageTimeout' => $this->config('messageTimeout'),
41: 'redirectUrl' => esc_url_raw($this->getRedirectUrl()),
42: 'redirectDelay' => $this->config('redirectDelay'),
43: 'hideForm' => $this->config('hideForm'),
44: 'resetForm' => $this->config('resetForm')
45: );
46:
47: $data = apply_filters('quform_confirmation_data', $data, $this, $this->form);
48: $data = apply_filters('quform_confirmation_data_' . $this->getIdentifier(), $data, $this, $this->form);
49:
50: return $data;
51: }
52:
53: 54: 55: 56: 57:
58: public function getMessage()
59: {
60: $message = $this->config('message');
61:
62: if ($this->config('messageAutoFormat')) {
63: $message = nl2br($message);
64: }
65:
66: $message = $this->form->replaceVariables($message, 'html');
67: $message = do_shortcode($message);
68:
69: if (is_admin()) {
70:
71: $message = wp_kses_post($message);
72: }
73:
74: $message = apply_filters('quform_confirmation_message', $message, $this, $this->form);
75: $message = apply_filters('quform_confirmation_message_' . $this->getIdentifier(), $message, $this, $this->form);
76:
77: return $message;
78: }
79:
80: 81: 82: 83: 84:
85: public function getRedirectUrl()
86: {
87: $url = '';
88:
89: switch ($this->config('type')) {
90: case 'redirect-page':
91: case 'message-redirect-page':
92: $post = get_post($this->config('redirectPage'));
93:
94: if ($post instanceof WP_Post && $post->post_status == 'publish') {
95: $url = get_permalink($post);
96: } else {
97: $url = home_url();
98: }
99:
100: break;
101: case 'redirect-url':
102: case 'message-redirect-url':
103: $url = $this->form->replaceVariables($this->config('redirectUrl'), 'rawurl');
104: break;
105: }
106:
107: if (Quform::isNonEmptyString($this->config('redirectQuery'))) {
108: $query = explode('&', $this->config('redirectQuery'));
109:
110: foreach ($query as $part) {
111: $parameter = explode('=', $part);
112:
113: if (count($parameter) == 1) {
114: $url = add_query_arg(
115: $this->form->replaceVariables($parameter[0], 'url'),
116: '',
117: $url
118: );
119: } else if (count($parameter) == 2) {
120: $url = add_query_arg(
121: $this->form->replaceVariables($parameter[0], 'url'),
122: $this->form->replaceVariables($parameter[1], 'url'),
123: $url
124: );
125: }
126: }
127: }
128:
129: $url = apply_filters('quform_confirmation_redirect_url', $url, $this, $this->form);
130: $url = apply_filters('quform_confirmation_redirect_url_' . $this->getIdentifier(), $url, $this, $this->form);
131:
132: return $url;
133: }
134:
135: 136: 137: 138: 139:
140: public function getIdentifier()
141: {
142: return sprintf('%d_%d', $this->form->getId(), $this->config('id'));
143: }
144:
145: 146: 147: 148: 149: 150: 151:
152: public function config($key, $default = null)
153: {
154: $value = Quform::get($this->config, $key, $default);
155:
156: if ($value === null) {
157: $value = Quform::get(call_user_func(array(get_class($this), 'getDefaultConfig')), $key, $default);
158: }
159:
160: return $value;
161: }
162:
163: 164: 165: 166: 167: 168: 169:
170: public function setConfig($key, $value = null)
171: {
172: if (is_array($key)) {
173: foreach ($key as $k => $v) {
174: $this->config[$k] = $v;
175: }
176: } else {
177: $this->config[$key] = $value;
178: }
179:
180: return $this;
181: }
182:
183: 184: 185: 186: 187: 188:
189: public static function getDefaultConfig($key = null)
190: {
191: $config = apply_filters('quform_default_confirmation', array(
192: 'name' => '',
193: 'enabled' => true,
194: 'type' => 'message',
195: 'message' => '',
196: 'messageAutoFormat' => true,
197: 'messageIcon' => '',
198: 'messagePosition' => 'above',
199: 'messageTimeout' => '10',
200: 'redirectPage' => '',
201: 'redirectUrl' => '',
202: 'redirectQuery' => '',
203: 'redirectDelay' => '3',
204: 'logicAction' => true,
205: 'logicMatch' => 'all',
206: 'logicRules' => array(),
207: 'hideForm' => false,
208: 'resetForm' => ''
209: ));
210:
211: if (Quform::isNonEmptyString($key)) {
212: return Quform::get($config, $key);
213: }
214:
215: return $config;
216: }
217: }
218: