| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\WpInit; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('Restricted Access'); |
| 6 |
|
| 7 |
use AcyMailing\Classes\UserClass; |
| 8 |
use AcyMailing\Classes\ListClass; |
| 9 |
|
| 10 |
class ElementorForm extends \ElementorPro\Modules\Forms\Classes\Action_Base |
| 11 |
{ |
| 12 |
public function get_name() |
| 13 |
{ |
| 14 |
return 'acymailing'; |
| 15 |
} |
| 16 |
|
| 17 |
public function get_label() |
| 18 |
{ |
| 19 |
return 'AcyMailing'; |
| 20 |
} |
| 21 |
|
| 22 |
public function register_settings_section($widget) |
| 23 |
{ |
| 24 |
// Check if an id is provided because this function is called many times sometimes there is no id (and not the rest: settings, form_fields,...) |
| 25 |
// So when we try to access fields data that is not currently existing a fatal error appears |
| 26 |
if ($widget->get_id()) { |
| 27 |
$fields = ['' => '']; |
| 28 |
foreach ($widget->get_data('settings')['form_fields'] as $field) { |
| 29 |
if (!isset($field['custom_id']) || !isset($field['field_label'])) { |
| 30 |
continue; |
| 31 |
} |
| 32 |
$fields[$field['custom_id']] = $field['field_label']; |
| 33 |
} |
| 34 |
|
| 35 |
$widget->start_controls_section('section_acymailing', [ |
| 36 |
'label' => 'AcyMailing', |
| 37 |
'condition' => [ |
| 38 |
'submit_actions' => $this->get_name(), |
| 39 |
], |
| 40 |
]); |
| 41 |
|
| 42 |
$listsClass = new ListClass(); |
| 43 |
$lists = $listsClass->getAllWithIdName(); |
| 44 |
|
| 45 |
$widget->add_control('acym_selectLists', [ |
| 46 |
'label' => acym_translation('ACYM_LISTS_SUMMARY'), |
| 47 |
'label_block' => true, |
| 48 |
'description' => acym_translation('ACYM_SELECT_LISTS_USERS_SUBSCRIBE'), |
| 49 |
'type' => \Elementor\Controls_Manager::SELECT2, |
| 50 |
'options' => $lists, |
| 51 |
'multiple' => true, |
| 52 |
'default' => '', |
| 53 |
]); |
| 54 |
|
| 55 |
$widget->add_control('acym_nameField', [ |
| 56 |
'label' => acym_translation('ACYM_NAME_SUMMARY'), |
| 57 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 58 |
'default' => '', |
| 59 |
'options' => $fields, |
| 60 |
]); |
| 61 |
|
| 62 |
$widget->add_control('acym_emailField', [ |
| 63 |
'label' => acym_translation('ACYM_EMAIL'), |
| 64 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 65 |
'default' => '', |
| 66 |
'options' => $fields, |
| 67 |
'description' => acym_translation('ACYM_RELOAD_PAGE_TO_SEE_NEW_FIELDS'), |
| 68 |
]); |
| 69 |
|
| 70 |
$widget->add_control('acym_confirmUsers', [ |
| 71 |
'label' => acym_translation('ACYM_IMPORT_USERS_AS_CONFIRMED'), |
| 72 |
'type' => \Elementor\Controls_Manager::SWITCHER, |
| 73 |
'label_on' => acym_translation('ACYM_YES'), |
| 74 |
'label_off' => acym_translation('ACYM_NO'), |
| 75 |
'default' => true, |
| 76 |
]); |
| 77 |
|
| 78 |
$widget->end_controls_section(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public function on_export($element) |
| 83 |
{ |
| 84 |
unset($element['acym_confirmUsers']); |
| 85 |
unset($element['acym_selectLists']); |
| 86 |
unset($element['acym_nameField']); |
| 87 |
unset($element['acym_emailField']); |
| 88 |
} |
| 89 |
|
| 90 |
public function run($record, $ajax_handler) |
| 91 |
{ |
| 92 |
$settings = $record->get('form_settings'); |
| 93 |
$data = $record->get('sent_data'); |
| 94 |
|
| 95 |
if (empty($data[$settings['acym_emailField']])) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$userClass = new UserClass(); |
| 100 |
$newUser = new \stdClass(); |
| 101 |
|
| 102 |
$newUser->name = $data[$settings['acym_nameField']]; |
| 103 |
$newUser->email = $data[$settings['acym_emailField']]; |
| 104 |
$newUser->creation_date = gmdate('Y-m-d H:i:s'); |
| 105 |
$newUser->confirmed = $settings['acym_confirmUsers'] === 'yes'; |
| 106 |
|
| 107 |
$user = $userClass->getOneByEmail($newUser->email); |
| 108 |
if (!empty($user)) { |
| 109 |
$newUser->id = $user->id; |
| 110 |
} |
| 111 |
|
| 112 |
// We do that because Elementor submit the form via ajax and in ajax mode WordPress always return true to the function is_admin() |
| 113 |
$config = acym_config(); |
| 114 |
if ($config->get('require_confirmation', 1) == 1) { |
| 115 |
$userClass->forceConfAdmin = true; |
| 116 |
} |
| 117 |
|
| 118 |
$userId = $userClass->save($newUser); |
| 119 |
|
| 120 |
if (!empty($settings['acym_selectLists'])) { |
| 121 |
if (!is_array($settings['acym_selectLists'])) { |
| 122 |
$settings['acym_selectLists'] = [$settings['acym_selectLists']]; |
| 123 |
} |
| 124 |
$userClass->subscribe([$userId], $settings['acym_selectLists'], true, true); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|