| @@ -1,8 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm; |
| 4 | 4 | |
| 5 | +if (!defined('ABSPATH')) { | |
| 6 | + exit; | |
| 7 | +} | |
| 8 | + | |
| 5 | 9 | /** |
| 6 | 10 | * Main class for the plugin. |
| 7 | 11 | * |
| 8 | 12 | * @since 1.0.0-alpha |
| @@ -9,15 +13,17 @@ | ||
| 9 | 13 | */ |
| 10 | 14 | |
| 11 | 15 | use BitCode\BitForm\Core\Database\DB; |
| 12 | 16 | use BitCode\BitForm\Core\Database\FormModel; |
| 17 | +use BitCode\BitForm\Core\Fallback\FormFallback; | |
| 13 | 18 | use BitCode\BitForm\Core\Hooks\Hooks; |
| 14 | 19 | use BitCode\BitForm\Core\Util\Activation; |
| 15 | 20 | use BitCode\BitForm\Core\Util\Deactivation; |
| 16 | -use BitCode\BitForm\Core\Util\FrontendHelpers; | |
| 17 | 21 | use BitCode\BitForm\Core\Util\Uninstallation; |
| 22 | +use BitCode\BitForm\Widgets\RegisterBitFormElementorWidget; | |
| 18 | 23 | |
| 19 | -final class Plugin { | |
| 24 | +final class Plugin | |
| 25 | +{ | |
| 20 | 26 | /** |
| 21 | 27 | * Main instance of the plugin. |
| 22 | 28 | * |
| 23 | 29 | * @since 1.0.0-alpha |
| @@ -29,34 +35,19 @@ | ||
| 29 | 35 | * Registers the plugin with WordPress. |
| 30 | 36 | * |
| 31 | 37 | * @since 1.0.0-alpha |
| 32 | 38 | */ |
| 33 | - public function register() { | |
| 39 | + public function register() | |
| 40 | + { | |
| 34 | 41 | add_action('plugins_loaded', [$this, 'init_plugin']); |
| 35 | - add_action('init', [$this, 'checkIfFirstHttpRequest']); | |
| 42 | + add_action('init', [$this, 'localization_setup']); | |
| 36 | 43 | (new Activation())->activate(); |
| 37 | 44 | (new Deactivation())->register(); |
| 38 | 45 | (new Uninstallation())->register(); |
| 39 | 46 | } |
| 40 | 47 | |
| 41 | - public function checkIfFirstHttpRequest() { | |
| 42 | - $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; | |
| 43 | - $referrerQueryParams = FrontendHelpers::parseQueryParams($referrer); | |
| 44 | - $pageBuilderQueryParams = FrontendHelpers::$pageBuilderList; | |
| 45 | - $referrerMatchesWithPageBuilder = array_intersect_assoc($referrerQueryParams, $pageBuilderQueryParams); | |
| 46 | - if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($referrer) && 0 === count($referrerMatchesWithPageBuilder)) { | |
| 47 | - delete_transient('is_page_builder'); | |
| 48 | - delete_transient('bf_frontend_form_ids'); | |
| 49 | - $currentUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; | |
| 50 | - $currentUrlQueryParams = FrontendHelpers::parseQueryParams($currentUrl); | |
| 51 | - $currentUrlMatchesWithPageBuilder = array_intersect_assoc($currentUrlQueryParams, $pageBuilderQueryParams); | |
| 52 | - if (0 !== count($currentUrlMatchesWithPageBuilder)) { | |
| 53 | - set_transient('is_page_builder', true); | |
| 54 | - } | |
| 55 | - } | |
| 56 | - } | |
| 57 | - | |
| 58 | - private function commaReplace($options, $lbl, $val) { | |
| 48 | + private function commaReplace($options, $lbl, $val) | |
| 49 | + { | |
| 59 | 50 | foreach ($options as $key => $option) { |
| 60 | 51 | if (!empty($option[$lbl]) && !empty($option[$val])) { |
| 61 | 52 | $options[$key][$val] = str_replace(',', '_', $option[$val]); |
| 62 | 53 | } |
| @@ -66,9 +57,10 @@ | ||
| 66 | 57 | } |
| 67 | 58 | return $options; |
| 68 | 59 | } |
| 69 | 60 | |
| 70 | - private function replaceOptionSeparator($formContent) { | |
| 61 | + private function replaceOptionSeparator($formContent) | |
| 62 | + { | |
| 71 | 63 | $updated = false; |
| 72 | 64 | foreach ($formContent['fields'] as $key => $field) { |
| 73 | 65 | if ('check' === $field['typ']) { |
| 74 | 66 | $formContent['fields'][$key]['opt'] = $this->commaReplace($field['opt'], 'lbl', 'val'); |
| @@ -81,9 +73,10 @@ | ||
| 81 | 73 | } |
| 82 | 74 | return [$formContent, $updated]; |
| 83 | 75 | } |
| 84 | 76 | |
| 85 | - public function optionFallBack() { | |
| 77 | + public function optionFallBack() | |
| 78 | + { | |
| 86 | 79 | $formModel = new FormModel(); |
| 87 | 80 | $forms = $formModel->get( |
| 88 | 81 | ['id', 'form_content'] |
| 89 | 82 | ); |
| @@ -108,15 +101,16 @@ | ||
| 108 | 101 | * Updates bit form content tables on wp db |
| 109 | 102 | * |
| 110 | 103 | * @return void |
| 111 | 104 | */ |
| 112 | - public function update_tables() { | |
| 105 | + public function update_tables() | |
| 106 | + { | |
| 113 | 107 | if (!current_user_can('manage_options')) { |
| 114 | 108 | return; |
| 115 | 109 | } |
| 116 | 110 | |
| 117 | 111 | global $bitforms_db_version; |
| 118 | - $installed_db_version = get_site_option('bitforms_db_version'); | |
| 112 | + $installed_db_version = get_option('bitforms_db_version'); | |
| 119 | 113 | if ($installed_db_version !== $bitforms_db_version) { |
| 120 | 114 | DB::migrate(); |
| 121 | 115 | } |
| 122 | 116 | } |
| @@ -127,11 +121,18 @@ | ||
| 127 | 121 | * @param array $links |
| 128 | 122 | * |
| 129 | 123 | * @return array |
| 130 | 124 | */ |
| 131 | - public function init_plugin() { | |
| 125 | + public function init_plugin() | |
| 126 | + { | |
| 127 | + $this->update_tables(); | |
| 128 | + $currentBitFormVersion = BITFORMS_VERSION; | |
| 129 | + $installedBitFormVersion = get_option('bitforms_version'); | |
| 130 | + if ($currentBitFormVersion !== $installedBitFormVersion) { | |
| 131 | + (new FormFallback())->resetJsGeneratedPageIds(); | |
| 132 | + } | |
| 132 | 133 | Hooks::init_hooks(); |
| 133 | - $this->update_tables(); | |
| 134 | + $this->initWidgets(); | |
| 134 | 135 | do_action('bitform_loaded'); |
| 135 | 136 | } |
| 136 | 137 | |
| 137 | 138 | /** |
| @@ -140,14 +141,16 @@ | ||
| 140 | 141 | * @since 1.0.0-alpha |
| 141 | 142 | * |
| 142 | 143 | * @return BITFORM Plugin main instance. |
| 143 | 144 | */ |
| 144 | - public static function instance() { | |
| 145 | + public static function instance() | |
| 146 | + { | |
| 145 | 147 | return static::$instance; |
| 146 | 148 | } |
| 147 | 149 | |
| 148 | - public function plugin_action_links($links) { | |
| 149 | - $links[] = '<a href="https://docs.form.bitapps.pro" target="_blank">' . __('Docs') . '</a>'; | |
| 150 | + public function plugin_action_links($links) | |
| 151 | + { | |
| 152 | + $links[] = '<a href="https://bitapps.pro/docs/bit-form/" target="_blank">' . __('Docs', 'bit-form') . '</a>'; | |
| 150 | 153 | |
| 151 | 154 | return $links; |
| 152 | 155 | } |
| 153 | 156 | |
| @@ -157,9 +160,10 @@ | ||
| 157 | 160 | * @param string $main_file Absolute path to the plugin main file. |
| 158 | 161 | * |
| 159 | 162 | * @return bool True if the plugin main instance could be loaded, false otherwise. |
| 160 | 163 | */ |
| 161 | - public static function load($main_file) { | |
| 164 | + public static function load($main_file) | |
| 165 | + { | |
| 162 | 166 | if (null !== static::$instance) { |
| 163 | 167 | return false; |
| 164 | 168 | } |
| 165 | 169 | |
| @@ -165,6 +169,34 @@ | ||
| 165 | 169 | |
| 166 | 170 | static::$instance = new static($main_file); |
| 167 | 171 | static::$instance->register(); |
| 168 | 172 | return true; |
| 173 | + } | |
| 174 | + | |
| 175 | + private function initWidgets() | |
| 176 | + { | |
| 177 | + if (defined('ELEMENTOR_VERSION')) { | |
| 178 | + new RegisterBitFormElementorWidget(); | |
| 179 | + } | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * Registers the bundled translations directory for the `bit-form` text domain. | |
| 184 | + */ | |
| 185 | + public function localization_setup() | |
| 186 | + { | |
| 187 | + /** | |
| 188 | + * Filters whether Bit Form loads its bundled translations at all. | |
| 189 | + * | |
| 190 | + * Returning false leaves every Free string untranslated — the same filter gates | |
| 191 | + * the React admin bundle in Admin_Bar. Pro has its own | |
| 192 | + * `bitformpro_filter_allow_translation`. | |
| 193 | + * | |
| 194 | + * @param bool $allow Whether to load translations. Default true. | |
| 195 | + */ | |
| 196 | + if (!apply_filters('bitform_filter_allow_translation', true)) { | |
| 197 | + return; | |
| 198 | + } | |
| 199 | + | |
| 200 | + load_plugin_textdomain('bit-form', false, \dirname(BITFORMS_PLUGIN_BASENAME) . '/languages'); | |
| 169 | 201 | } |
| 170 | 202 | } |