| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules; |
| 4 |
|
| 5 |
use FluentForm\App\Databases\Migrations\FormLogs; |
| 6 |
use FluentForm\App\Databases\Migrations\FormSubmissionDetails; |
| 7 |
use FluentForm\App\Modules\Acl\Acl; |
| 8 |
use FluentForm\App\Databases\DatabaseMigrator; |
| 9 |
use FluentForm\App\Modules\Form\Predefined; |
| 10 |
|
| 11 |
class Activator |
| 12 |
{ |
| 13 |
/** |
| 14 |
* This method will be called on plugin activation |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function handleActivation($network_wide) |
| 19 |
{ |
| 20 |
global $wpdb; |
| 21 |
if ($network_wide) { |
| 22 |
// Retrieve all site IDs from this network (WordPress >= 4.6 provides easy to use functions for that). |
| 23 |
if (function_exists('get_sites') && function_exists('get_current_network_id')) { |
| 24 |
$site_ids = get_sites(['fields' => 'ids', 'network_id' => get_current_network_id()]); |
| 25 |
} else { |
| 26 |
$site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE site_id = $wpdb->siteid;"); |
| 27 |
} |
| 28 |
// Install the plugin for all these sites. |
| 29 |
foreach ($site_ids as $site_id) { |
| 30 |
switch_to_blog($site_id); |
| 31 |
$this->migrate(); |
| 32 |
restore_current_blog(); |
| 33 |
} |
| 34 |
} else { |
| 35 |
$this->migrate(); |
| 36 |
$this->maybeMigrateDefaultForms(); |
| 37 |
} |
| 38 |
|
| 39 |
self::setCronSchedule(); |
| 40 |
} |
| 41 |
|
| 42 |
public function migrate() |
| 43 |
{ |
| 44 |
// We are going to migrate all the database tables here. |
| 45 |
DatabaseMigrator::run(); |
| 46 |
// Assign fluentform permission set. |
| 47 |
Acl::setPermissions(); |
| 48 |
$this->setDefaultGlobalSettings(); |
| 49 |
$this->setCurrentVersion(); |
| 50 |
$this->maybeMigrateDB(); |
| 51 |
} |
| 52 |
|
| 53 |
public function maybeMigrateDB() |
| 54 |
{ |
| 55 |
// First we have to check if global modules is set or not |
| 56 |
$this->migrateGlobalAddOns(); |
| 57 |
|
| 58 |
if (!get_option('fluentform_entry_details_migrated')) { |
| 59 |
FormSubmissionDetails::migrate(); |
| 60 |
} |
| 61 |
|
| 62 |
if (!get_option('fluentform_db_fluentform_logs_added')) { |
| 63 |
FormLogs::migrate(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public function migrateGlobalAddOns() |
| 68 |
{ |
| 69 |
$globalModules = get_option('fluentform_global_modules_status'); |
| 70 |
// Modules already set |
| 71 |
if (is_array($globalModules)) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$possibleGloablModules = [ |
| 76 |
'mailchimp' => '_fluentform_mailchimp_details', |
| 77 |
'activecampaign' => '_fluentform_activecampaign_settings', |
| 78 |
'campaign_monitor' => '_fluentform_campaignmonitor_settings', |
| 79 |
'constatantcontact' => '_fluentform_constantcontact_settings', |
| 80 |
'getresponse' => '_fluentform_getresponse_settings', |
| 81 |
'icontact' => '_fluentform_icontact_settings', |
| 82 |
]; |
| 83 |
|
| 84 |
$possibleMetaModules = [ |
| 85 |
'webhook' => 'fluentform_webhook_feed', |
| 86 |
'zapier' => 'fluentform_zapier_feed', |
| 87 |
'slack' => 'slack', |
| 88 |
]; |
| 89 |
|
| 90 |
$moduleStatuses = []; |
| 91 |
foreach ($possibleGloablModules as $moduleName => $settingsKey) { |
| 92 |
if (get_option($settingsKey)) { |
| 93 |
$moduleStatuses[$moduleName] = 'yes'; |
| 94 |
} else { |
| 95 |
$moduleStatuses[$moduleName] = 'no'; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
global $wpdb; |
| 100 |
|
| 101 |
foreach ($possibleMetaModules as $moduleName => $metaName) { |
| 102 |
$row = $wpdb->get_row("SELECT * FROM `{$wpdb->prefix}fluentform_form_meta` WHERE `meta_key` = '{$metaName}' LIMIT 1"); |
| 103 |
|
| 104 |
if ($row) { |
| 105 |
$moduleStatuses[$moduleName] = 'yes'; |
| 106 |
} else { |
| 107 |
$moduleStatuses[$moduleName] = 'no'; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
update_option('fluentform_global_modules_status', $moduleStatuses, 'no'); |
| 112 |
} |
| 113 |
|
| 114 |
private function setDefaultGlobalSettings() |
| 115 |
{ |
| 116 |
if (!get_option('_fluentform_global_form_settings')) { |
| 117 |
update_option('__fluentform_global_form_settings', [ |
| 118 |
'layout' => [ |
| 119 |
'labelPlacement' => 'top', |
| 120 |
'asteriskPlacement' => 'asterisk-right', |
| 121 |
'helpMessagePlacement' => 'with_label', |
| 122 |
'errorMessagePlacement' => 'inline', |
| 123 |
'cssClassName' => '', |
| 124 |
], |
| 125 |
], 'no'); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
private function setCurrentVersion() |
| 130 |
{ |
| 131 |
update_option('_fluentform_installed_version', FLUENTFORM_VERSION, 'no'); |
| 132 |
} |
| 133 |
|
| 134 |
public static function setCronSchedule() |
| 135 |
{ |
| 136 |
add_filter('cron_schedules', function ($schedules) { |
| 137 |
$schedules['ff_every_five_minutes'] = [ |
| 138 |
'interval' => 300, |
| 139 |
'display' => esc_html__('Every 5 Minutes (FluentForm)', 'fluentform'), |
| 140 |
]; |
| 141 |
return $schedules; |
| 142 |
}, 10, 1); |
| 143 |
|
| 144 |
$hookName = 'fluentform_do_scheduled_tasks'; |
| 145 |
if (!wp_next_scheduled($hookName)) { |
| 146 |
wp_schedule_event(time(), 'ff_every_five_minutes', $hookName); |
| 147 |
} |
| 148 |
|
| 149 |
$emailReportHookName = 'fluentform_do_email_report_scheduled_tasks'; |
| 150 |
if (!wp_next_scheduled($emailReportHookName)) { |
| 151 |
wp_schedule_event(time(), 'daily', $emailReportHookName); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
public static function maybeMigrateDefaultForms() |
| 156 |
{ |
| 157 |
global $wpdb; |
| 158 |
$formsTable = $wpdb->prefix . 'fluentform_forms'; |
| 159 |
$firstForm = $wpdb->get_row('SELECT * FROM ' . $formsTable . ' LIMIT 1'); |
| 160 |
|
| 161 |
if (!$firstForm) { |
| 162 |
$forms = [ |
| 163 |
'[{"id":"9","title":"Contact Form Demo","status":"published","appearance_settings":null,"form_fields":{"fields":[{"index":0,"element":"input_name","attributes":{"name":"names","data-type":"name-element"},"settings":{"container_class":"","admin_field_label":"Name","conditional_logics":[]},"fields":{"first_name":{"element":"input_text","attributes":{"type":"text","name":"first_name","value":"","id":"","class":"","placeholder":"First Name"},"settings":{"container_class":"","label":"First Name","help_message":"","visible":true,"validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}},"middle_name":{"element":"input_text","attributes":{"type":"text","name":"middle_name","value":"","id":"","class":"","placeholder":"","required":false},"settings":{"container_class":"","label":"Middle Name","help_message":"","error_message":"","visible":false,"validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}},"last_name":{"element":"input_text","attributes":{"type":"text","name":"last_name","value":"","id":"","class":"","placeholder":"Last Name","required":false},"settings":{"container_class":"","label":"Last Name","help_message":"","error_message":"","visible":true,"validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":[]},"editor_options":{"template":"inputText"}}},"editor_options":{"title":"Name Fields","element":"name-fields","icon_class":"ff-edit-name","template":"nameFields"},"uniqElKey":"el_1570866006692"},{"index":1,"element":"input_email","attributes":{"type":"email","name":"email","value":"","id":"","class":"","placeholder":"Email Address"},"settings":{"container_class":"","label":"Email","label_placement":"","help_message":"","admin_field_label":"","validation_rules":{"required":{"value":true,"message":"This field is required"},"email":{"value":true,"message":"This field must contain a valid email"}},"conditional_logics":[]},"editor_options":{"title":"Email Address","icon_class":"ff-edit-email","template":"inputText"},"uniqElKey":"el_1570866012914"},{"index":2,"element":"input_text","attributes":{"type":"text","name":"subject","value":"","class":"","placeholder":"Subject"},"settings":{"container_class":"","label":"Subject","label_placement":"","admin_field_label":"Subject","help_message":"","validation_rules":{"required":{"value":false,"message":"This field is required"}},"conditional_logics":{"type":"any","status":false,"conditions":[{"field":"","value":"","operator":""}]}},"editor_options":{"title":"Simple Text","icon_class":"ff-edit-text","template":"inputText"},"uniqElKey":"el_1570878958648"},{"index":3,"element":"textarea","attributes":{"name":"message","value":"","id":"","class":"","placeholder":"Your Message","rows":4,"cols":2},"settings":{"container_class":"","label":"Your Message","admin_field_label":"","label_placement":"","help_message":"","validation_rules":{"required":{"value":true,"message":"This field is required"}},"conditional_logics":{"type":"any","status":false,"conditions":[{"field":"","value":"","operator":""}]}},"editor_options":{"title":"Text Area","icon_class":"ff-edit-textarea","template":"inputTextarea"},"uniqElKey":"el_1570879001207"}],"submitButton":{"uniqElKey":"el_1524065200616","element":"button","attributes":{"type":"submit","class":""},"settings":{"align":"left","button_style":"default","container_class":"","help_message":"","background_color":"#409EFF","button_size":"md","color":"#ffffff","button_ui":{"type":"default","text":"Submit Form","img_url":""}},"editor_options":{"title":"Submit Button"}}},"has_payment":"0","type":"","conditions":null,"created_by":"1","created_at":"2021-06-08 04:56:28","updated_at":"2021-06-08 04:56:28","metas":[{"meta_key": "template_name","value": "basic_contact_form"},{"meta_key":"formSettings","value":"{\"confirmation\":{\"redirectTo\":\"samePage\",\"messageToShow\":\"Thank you for your message. We will get in touch with you shortly\",\"customPage\":null,\"samePageFormBehavior\":\"hide_form\",\"customUrl\":null},\"restrictions\":{\"limitNumberOfEntries\":{\"enabled\":false,\"numberOfEntries\":null,\"period\":\"total\",\"limitReachedMsg\":\"Maximum number of entries exceeded.\"},\"scheduleForm\":{\"enabled\":false,\"start\":null,\"end\":null,\"selectedDays\":[\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\",\"Sunday\"],\"pendingMsg\":\"Form submission is not started yet.\",\"expiredMsg\":\"Form submission is now closed.\"},\"requireLogin\":{\"enabled\":false,\"requireLoginMsg\":\"You must be logged in to submit the form.\"},\"denyEmptySubmission\":{\"enabled\":false,\"message\":\"Sorry, you cannot submit an empty form. Let\'s hear what you wanna say.\"}},\"layout\":{\"labelPlacement\":\"top\",\"helpMessagePlacement\":\"with_label\",\"errorMessagePlacement\":\"inline\",\"cssClassName\":\"\",\"asteriskPlacement\":\"asterisk-right\"},\"delete_entry_on_submission\":\"no\",\"appendSurveyResult\":{\"enabled\":false,\"showLabel\":false,\"showCount\":false}}"},{"meta_key":"advancedValidationSettings","value":"{\"status\":false,\"type\":\"all\",\"conditions\":[{\"field\":\"\",\"operator\":\"=\",\"value\":\"\"}],\"error_message\":\"\",\"validation_type\":\"fail_on_condition_met\"}"},{"meta_key":"double_optin_settings","value":"{\"status\":\"no\",\"confirmation_message\":\"Please check your email inbox to confirm this submission\",\"email_body_type\":\"global\",\"email_subject\":\"Please confirm your form submission\",\"email_body\":\"<h2>Please Confirm Your Submission<\/h2><p> <\/p><p style=\"text-align: center;\"><a style=\"color: #ffffff; background-color: #454545; font-size: 16px; border-radius: 5px; text-decoration: none; font-weight: normal; font-style: normal; padding: 0.8rem 1rem; border-color: #0072ff;\" href=\"#confirmation_url#\">Confirm Submission<\/a><\/p><p> <\/p><p>If you received this email by mistake, simply delete it. Your form submission won\'t proceed if you don\'t click the confirmation link above.<\/p>\",\"email_field\":\"\",\"skip_if_logged_in\":\"yes\",\"skip_if_fc_subscribed\":\"no\"}"}]}]', |
| 164 |
'[{"id":"8","title":"Subscription Form","status":"published","appearance_settings":null,"form_fields":{"fields":[{"index":1,"element":"container","attributes":[],"settings":{"container_class":"","conditional_logics":{"type":"any","status":false,"conditions":[{"field":"","value":"","operator":""}]}},"columns":[{"fields":[{"index":1,"element":"input_email","attributes":{"type":"email","name":"email","value":"","id":"","class":"","placeholder":"Your Email Address"},"settings":{"container_class":"","label":"","label_placement":"","help_message":"","admin_field_label":"Email","validation_rules":{"required":{"value":true,"message":"This field is required"},"email":{"value":true,"message":"This field must contain a valid email"}},"conditional_logics":[],"is_unique":"no","unique_validation_message":"Email address need to be unique."},"editor_options":{"title":"Email Address","icon_class":"ff-edit-email","template":"inputText"},"uniqElKey":"el_16231279686950.8779857923682932"}]},{"fields":[{"index":15,"element":"custom_submit_button","attributes":{"class":"","type":"submit"},"settings":{"button_style":"","button_size":"md","align":"left","container_class":"","current_state":"normal_styles","background_color":"","color":"","hover_styles":{"backgroundColor":"#ffffff","borderColor":"#409EFF","color":"#409EFF","borderRadius":"","minWidth":"100%"},"normal_styles":{"backgroundColor":"#409EFF","borderColor":"#409EFF","color":"#ffffff","borderRadius":"","minWidth":"100%"},"button_ui":{"text":"Subscribe","type":"default","img_url":""},"conditional_logics":{"type":"any","status":false,"conditions":[{"field":"","value":"","operator":""}]}},"editor_options":{"title":"Custom Submit Button","icon_class":"dashicons dashicons-arrow-right-alt","template":"customButton"},"uniqElKey":"el_16231279798380.5947400167493171"}]}],"editor_options":{"title":"Two Column Container","icon_class":"ff-edit-column-2"},"uniqElKey":"el_16231279284710.40955091024524304"}],"submitButton":{"uniqElKey":"el_1524065200616","element":"button","attributes":{"type":"submit","class":""},"settings":{"align":"left","button_style":"default","container_class":"","help_message":"","background_color":"#409EFF","button_size":"md","color":"#ffffff","button_ui":{"type":"default","text":"Subscribe","img_url":""}},"editor_options":{"title":"Submit Button"}}},"has_payment":"0","type":"form","conditions":null,"created_by":"1","created_at":"2021-06-08 04:51:36","updated_at":"2021-06-08 04:54:02","metas":[{"meta_key": "template_name","value": "inline_subscription"},{"meta_key":"formSettings","value":"{\"confirmation\":{\"redirectTo\":\"samePage\",\"messageToShow\":\"Thank you for your message. We will get in touch with you shortly\",\"customPage\":null,\"samePageFormBehavior\":\"hide_form\",\"customUrl\":null},\"restrictions\":{\"limitNumberOfEntries\":{\"enabled\":false,\"numberOfEntries\":null,\"period\":\"total\",\"limitReachedMsg\":\"Maximum number of entries exceeded.\"},\"scheduleForm\":{\"enabled\":false,\"start\":null,\"end\":null,\"pendingMsg\":\"Form submission is not started yet.\",\"expiredMsg\":\"Form submission is now closed.\"},\"requireLogin\":{\"enabled\":false,\"requireLoginMsg\":\"You must be logged in to submit the form.\"},\"denyEmptySubmission\":{\"enabled\":false,\"message\":\"Sorry, you cannot submit an empty form. Let\'s hear what you wanna say.\"}},\"layout\":{\"labelPlacement\":\"top\",\"helpMessagePlacement\":\"with_label\",\"errorMessagePlacement\":\"inline\",\"asteriskPlacement\":\"asterisk-right\"}}"},{"meta_key":"notifications","value":"{\"name\":\"Admin Notification Email\",\"sendTo\":{\"type\":\"email\",\"email\":\"{wp.admin_email}\",\"field\":\"email\",\"routing\":[{\"email\":null,\"field\":null,\"operator\":\"=\",\"value\":null}]},\"fromName\":\"\",\"fromEmail\":\"\",\"replyTo\":\"\",\"bcc\":\"\",\"subject\":\"[{inputs.names}] New Form Submission\",\"message\":\"<p>{all_data}<\\\/p>\\n<p>This form submitted at: {embed_post.permalink}<\\\/p>\",\"conditionals\":{\"status\":false,\"type\":\"all\",\"conditions\":[{\"field\":null,\"operator\":\"=\",\"value\":null}]},\"enabled\":false,\"email_template\":\"\"}"},{"meta_key":"step_data_persistency_status","value":"no"},{"meta_key":"_primary_email_field","value":"email"}]}]', |
| 165 |
]; |
| 166 |
|
| 167 |
foreach ($forms as $index => $formJson) { |
| 168 |
$structure = json_decode($formJson, true)[0]; |
| 169 |
|
| 170 |
$insertData = [ |
| 171 |
'title' => $structure['title'], |
| 172 |
'type' => $structure['type'], |
| 173 |
'status' => 'published', |
| 174 |
'created_by' => get_current_user_id(), |
| 175 |
'created_at' => current_time('mysql'), |
| 176 |
'updated_at' => current_time('mysql'), |
| 177 |
'form_fields' => json_encode($structure['form_fields']), |
| 178 |
]; |
| 179 |
|
| 180 |
$wpdb->insert($formsTable, $insertData, [ |
| 181 |
'%s', |
| 182 |
'%s', |
| 183 |
'%s', |
| 184 |
'%d', |
| 185 |
'%s', |
| 186 |
'%s', |
| 187 |
'%s', |
| 188 |
]); |
| 189 |
|
| 190 |
$formId = $wpdb->insert_id; |
| 191 |
|
| 192 |
foreach ($structure['metas'] as $meta) { |
| 193 |
$meta['value'] = trim(preg_replace('/\s+/', ' ', $meta['value'])); |
| 194 |
|
| 195 |
$wpdb->insert($wpdb->prefix . 'fluentform_form_meta', [ |
| 196 |
'form_id' => $formId, |
| 197 |
'meta_key' => $meta['meta_key'], |
| 198 |
'value' => $meta['value'], |
| 199 |
], [ |
| 200 |
'%d', |
| 201 |
'%s', |
| 202 |
'%s', |
| 203 |
]); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|