| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm; |
| 4 |
|
| 5 |
/** |
| 6 |
* Main class for the plugin. |
| 7 |
* |
| 8 |
* @since 1.0.0-alpha |
| 9 |
*/ |
| 10 |
|
| 11 |
use BitCode\BitForm\BitApps\WPTelemetry\Telemetry\Telemetry; |
| 12 |
use BitCode\BitForm\BitApps\WPTelemetry\Telemetry\TelemetryConfig; |
| 13 |
use BitCode\BitForm\Core\Database\DB; |
| 14 |
use BitCode\BitForm\Core\Database\FormModel; |
| 15 |
use BitCode\BitForm\Core\Fallback\FormFallback; |
| 16 |
use BitCode\BitForm\Core\Hooks\Hooks; |
| 17 |
use BitCode\BitForm\Core\Util\Activation; |
| 18 |
use BitCode\BitForm\Core\Util\Deactivation; |
| 19 |
use BitCode\BitForm\Core\Util\Uninstallation; |
| 20 |
|
| 21 |
final class Plugin |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Main instance of the plugin. |
| 25 |
* |
| 26 |
* @since 1.0.0-alpha |
| 27 |
* @var Plugin|null |
| 28 |
*/ |
| 29 |
private static $instance = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Registers the plugin with WordPress. |
| 33 |
* |
| 34 |
* @since 1.0.0-alpha |
| 35 |
*/ |
| 36 |
public function register() |
| 37 |
{ |
| 38 |
add_action('plugins_loaded', [$this, 'init_plugin']); |
| 39 |
(new Activation())->activate(); |
| 40 |
(new Deactivation())->register(); |
| 41 |
(new Uninstallation())->register(); |
| 42 |
|
| 43 |
$this->initWpTelemetry(); |
| 44 |
} |
| 45 |
|
| 46 |
private function commaReplace($options, $lbl, $val) |
| 47 |
{ |
| 48 |
foreach ($options as $key => $option) { |
| 49 |
if (!empty($option[$lbl]) && !empty($option[$val])) { |
| 50 |
$options[$key][$val] = str_replace(',', '_', $option[$val]); |
| 51 |
} |
| 52 |
if (!empty($option[$lbl]) && empty($option[$val])) { |
| 53 |
$options[$key][$val] = str_replace(',', '_', $option[$lbl]); |
| 54 |
} |
| 55 |
} |
| 56 |
return $options; |
| 57 |
} |
| 58 |
|
| 59 |
private function replaceOptionSeparator($formContent) |
| 60 |
{ |
| 61 |
$updated = false; |
| 62 |
foreach ($formContent['fields'] as $key => $field) { |
| 63 |
if ('check' === $field['typ']) { |
| 64 |
$formContent['fields'][$key]['opt'] = $this->commaReplace($field['opt'], 'lbl', 'val'); |
| 65 |
$updated = true; |
| 66 |
} |
| 67 |
if ('select' === $field['typ']) { |
| 68 |
$formContent['fields'][$key]['opt'] = $this->commaReplace($field['opt'], 'label', 'value'); |
| 69 |
$updated = true; |
| 70 |
} |
| 71 |
} |
| 72 |
return [$formContent, $updated]; |
| 73 |
} |
| 74 |
|
| 75 |
public function optionFallBack() |
| 76 |
{ |
| 77 |
$formModel = new FormModel(); |
| 78 |
$forms = $formModel->get( |
| 79 |
['id', 'form_content'] |
| 80 |
); |
| 81 |
|
| 82 |
foreach ($forms as $form) { |
| 83 |
$formContent = json_decode($form->form_content, true); |
| 84 |
$optReplaceComma = $this->replaceOptionSeparator($formContent); |
| 85 |
if ($optReplaceComma[1]) { |
| 86 |
$formModel->update( |
| 87 |
[ |
| 88 |
'form_content' => wp_json_encode($optReplaceComma[0]), |
| 89 |
], |
| 90 |
[ |
| 91 |
'id' => $form->id, |
| 92 |
] |
| 93 |
); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Updates bit form content tables on wp db |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function update_tables() |
| 104 |
{ |
| 105 |
if (!current_user_can('manage_options')) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
global $bitforms_db_version; |
| 110 |
$installed_db_version = get_site_option('bitforms_db_version'); |
| 111 |
if ($installed_db_version !== $bitforms_db_version) { |
| 112 |
DB::migrate(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Plugin action links |
| 118 |
* |
| 119 |
* @param array $links |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
public function init_plugin() |
| 124 |
{ |
| 125 |
$currentBitFormVersion = BITFORMS_VERSION; |
| 126 |
$installedBitFormVersion = get_option('bitforms_version'); |
| 127 |
if ($currentBitFormVersion !== $installedBitFormVersion) { |
| 128 |
(new FormFallback())->resetJsGeneratedPageIds(); |
| 129 |
} |
| 130 |
Hooks::init_hooks(); |
| 131 |
$this->update_tables(); |
| 132 |
do_action('bitform_loaded'); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Retrieves the main instance of the plugin. |
| 137 |
* |
| 138 |
* @since 1.0.0-alpha |
| 139 |
* |
| 140 |
* @return BITFORM Plugin main instance. |
| 141 |
*/ |
| 142 |
public static function instance() |
| 143 |
{ |
| 144 |
return static::$instance; |
| 145 |
} |
| 146 |
|
| 147 |
public function plugin_action_links($links) |
| 148 |
{ |
| 149 |
$links[] = '<a href="https://docs.form.bitapps.pro" target="_blank">' . __('Docs') . '</a>'; |
| 150 |
|
| 151 |
return $links; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Loads the plugin main instance and initializes it. |
| 156 |
* |
| 157 |
* @param string $main_file Absolute path to the plugin main file. |
| 158 |
* |
| 159 |
* @return bool True if the plugin main instance could be loaded, false otherwise. |
| 160 |
*/ |
| 161 |
public static function load($main_file) |
| 162 |
{ |
| 163 |
if (null !== static::$instance) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
static::$instance = new static($main_file); |
| 168 |
static::$instance->register(); |
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
private function initWpTelemetry() |
| 173 |
{ |
| 174 |
TelemetryConfig::setSlug(Config::SLUG); |
| 175 |
TelemetryConfig::setTitle(Config::TITLE); |
| 176 |
TelemetryConfig::setVersion(Config::VERSION); |
| 177 |
TelemetryConfig::setPrefix(Config::VAR_PREFIX); |
| 178 |
|
| 179 |
TelemetryConfig::setServerBaseUrl('https://wp-api.bitapps.pro/public/'); |
| 180 |
TelemetryConfig::setTermsUrl('https://bitapps.pro/terms-of-service/'); |
| 181 |
TelemetryConfig::setPolicyUrl('https://bitapps.pro/privacy-policy/'); |
| 182 |
|
| 183 |
Telemetry::report()->addPluginData()->init(); |
| 184 |
Telemetry::feedback()->init(); |
| 185 |
} |
| 186 |
} |
| 187 |
|