1: <?php
2:
3: 4: 5:
6: class Quform_Captcha
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: 32:
33: public function regenerate()
34: {
35: $formId = (int) Quform::get($_GET, 'quform_form_id');
36: $elementId = (int) Quform::get($_GET, 'quform_element_id');
37: $uniqueId = Quform_Form::isValidUniqueId(Quform::get($_GET, 'quform_unique_id')) ? Quform::get($_GET, 'quform_unique_id') : null;
38:
39: if ( ! $formId || ! $elementId || ! $uniqueId) {
40: wp_send_json(array('type' => 'error'));
41: }
42:
43: $config = $this->repository->getConfig($formId);
44:
45: if ( ! is_array($config)) {
46: wp_send_json(array('type' => 'error'));
47: }
48:
49: $config['uniqueId'] = $uniqueId;
50:
51: $form = $this->factory->create($config);
52:
53: if ( ! ($form instanceof Quform_Form)) {
54: wp_send_json(array('type' => 'error'));
55: }
56:
57: $element = $form->getElementById($elementId);
58:
59: if ( ! $element instanceof Quform_Element_Captcha) {
60: wp_send_json(array('type' => 'error'));
61: }
62:
63: wp_send_json(array(
64: 'type' => 'success',
65: 'image' => $element->generateImageData()
66: ));
67: }
68: }
69: