| @@ -7,246 +7,180 @@ | ||
| 7 | 7 | * |
| 8 | 8 | * @since 1.0.0-alpha |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | -use BitCode\BitForm\Admin\Admin_Bar; | |
| 11 | +use BitCode\BitForm\BitApps\WPTelemetry\Telemetry\Telemetry; | |
| 12 | +use BitCode\BitForm\BitApps\WPTelemetry\Telemetry\TelemetryConfig; | |
| 12 | 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; | |
| 13 | 17 | use BitCode\BitForm\Core\Util\Activation; |
| 14 | -use BitCode\BitForm\Core\Form\FormHandler; | |
| 15 | -use BitCode\BitForm\Core\Ajax\AjaxService; | |
| 16 | 18 | use BitCode\BitForm\Core\Util\Deactivation; |
| 17 | 19 | use BitCode\BitForm\Core\Util\Uninstallation; |
| 18 | -use BitCode\BitForm\Core\Capability\Request; | |
| 19 | -use BitCode\BitForm\Core\Util\GutenBlockProvider; | |
| 20 | -use BitCode\BitForm\Core\Integration\Integrations; | |
| 21 | -use BitCode\BitForm\Core\Util\FileDownloadProvider; | |
| 22 | -use BitCode\BitForm\Core\Fallback\FormEntryMetaChange; | |
| 23 | -use BitCode\BitForm\API\Route\Routes; | |
| 24 | 20 | |
| 25 | 21 | final class Plugin |
| 26 | 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; | |
| 27 | 30 | |
| 28 | - /** | |
| 29 | - * Main instance of the plugin. | |
| 30 | - * | |
| 31 | - * @since 1.0.0-alpha | |
| 32 | - * @var Plugin|null | |
| 33 | - */ | |
| 34 | - private static $instance = null; | |
| 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(); | |
| 35 | 42 | |
| 43 | + $this->initWpTelemetry(); | |
| 44 | + } | |
| 36 | 45 | |
| 37 | - /** | |
| 38 | - * Registers the plugin with WordPress. | |
| 39 | - * | |
| 40 | - * @since 1.0.0-alpha | |
| 41 | - */ | |
| 42 | - public function register() | |
| 43 | - { | |
| 44 | - add_action('plugins_loaded', array($this, 'init_plugin')); | |
| 45 | - (new Activation())->activate(); | |
| 46 | - (new Deactivation())->register(); | |
| 47 | - (new Uninstallation())->register(); | |
| 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 | + } | |
| 48 | 55 | } |
| 56 | + return $options; | |
| 57 | + } | |
| 49 | 58 | |
| 50 | - | |
| 51 | - /** | |
| 52 | - * Initialize the hooks | |
| 53 | - * | |
| 54 | - * @return void | |
| 55 | - */ | |
| 56 | - public function init_hooks() | |
| 57 | - { | |
| 58 | - add_action('init', array($this, 'registerBitformsPostType')); | |
| 59 | - add_action('init', array($this, 'registerCustomPostType')); | |
| 60 | - add_action("bitforms_exec_integrations", array(Integrations::class, 'integrationExecutionHelper'), 1, 5); | |
| 61 | - // Localize our plugin | |
| 62 | - add_action('init', array($this, 'localization_setup')); | |
| 63 | - | |
| 64 | - // initialize the classes | |
| 65 | - add_action('init', array($this, 'init_classes')); | |
| 66 | - | |
| 67 | - add_action('init', array($this, 'entryMetaKeyUpdate')); | |
| 68 | - | |
| 69 | - add_filter('plugin_action_links_' . plugin_basename(BITFORMS_PLUGIN_MAIN_FILE), array($this, 'plugin_action_links')); | |
| 70 | - add_action('rest_api_init', [$this, 'register_api']); | |
| 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 | 71 | } |
| 72 | + return [$formContent, $updated]; | |
| 73 | + } | |
| 72 | 74 | |
| 73 | - public function register_api() | |
| 74 | - { | |
| 75 | - $routes = new Routes(); | |
| 76 | - $routes->register_routes(); | |
| 77 | - } | |
| 75 | + public function optionFallBack() | |
| 76 | + { | |
| 77 | + $formModel = new FormModel(); | |
| 78 | + $forms = $formModel->get( | |
| 79 | + ['id', 'form_content'] | |
| 80 | + ); | |
| 78 | 81 | |
| 79 | - public function entryMetaKeyUpdate() | |
| 80 | - { | |
| 81 | - $installed = get_option('bitforms_installed'); | |
| 82 | - $oldversion = null; | |
| 83 | - if ($installed) { | |
| 84 | - $oldversion = get_option('bitforms_version'); | |
| 85 | - } | |
| 86 | - if ($oldversion && version_compare($oldversion, BITFORMS_VERSION, '!=')) { | |
| 87 | - update_option('bitforms_version', BITFORMS_VERSION); | |
| 88 | - if (version_compare('1.0.4', $oldversion, '>=')) { | |
| 89 | - // if version is lower or equal than 1.0.4 | |
| 90 | - $formEntryMetaChange = new FormEntryMetaChange(); | |
| 91 | - $formEntryMetaChange->changeMeta(); | |
| 92 | - } | |
| 93 | - } | |
| 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 | + } | |
| 94 | 95 | } |
| 96 | + } | |
| 95 | 97 | |
| 96 | - /** | |
| 97 | - * Updates bit form content tables on wp db | |
| 98 | - * | |
| 99 | - * @return void | |
| 100 | - */ | |
| 101 | - public function update_tables() | |
| 102 | - { | |
| 103 | - if (!current_user_can('manage_options')) { | |
| 104 | - return; | |
| 105 | - } | |
| 106 | - | |
| 107 | - global $bitforms_db_version; | |
| 108 | - $installed_db_version = get_site_option("bitforms_db_version"); | |
| 109 | - if ($installed_db_version != $bitforms_db_version) { | |
| 110 | - DB::migrate(); | |
| 111 | - } | |
| 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; | |
| 112 | 107 | } |
| 113 | 108 | |
| 114 | - /** | |
| 115 | - * Register post type for bitforms | |
| 116 | - * | |
| 117 | - * @return void | |
| 118 | - */ | |
| 119 | - public function registerBitformsPostType() | |
| 120 | - { | |
| 121 | - $args = array( | |
| 122 | - 'label' => __('Bitform Pages', 'bitform'), | |
| 123 | - 'public' => true, | |
| 124 | - 'show_ui' => true, | |
| 125 | - 'show_in_menu' => false, | |
| 126 | - 'capability_type' => 'page', | |
| 127 | - 'hierarchical' => false, | |
| 128 | - 'query_var' => false, | |
| 129 | - 'supports' => array('title'), | |
| 130 | - 'show_in_rest' => false | |
| 131 | - ); | |
| 132 | - register_post_type('bitforms', $args); | |
| 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(); | |
| 133 | 113 | } |
| 114 | + } | |
| 134 | 115 | |
| 135 | - public function registerCustomPostType() | |
| 136 | - { | |
| 137 | - flush_rewrite_rules( false ); | |
| 138 | - $cpts = get_option('bitform_custom_post_types'); | |
| 139 | - if (!empty($cpts)) { | |
| 140 | - foreach ($cpts as $cpt ) { | |
| 141 | - $labels = array( | |
| 142 | - 'name' => _x($cpt->name, 'Post type general name', 'textdomain'), | |
| 143 | - 'singular_name' => _x($cpt->singular_label, 'Post type singular name', 'textdomain'), | |
| 144 | - 'menu_name' => _x($cpt->menu_name, 'Admin Menu text', 'textdomain'), | |
| 145 | - ); | |
| 146 | - $args = array( | |
| 147 | - 'labels' => $labels, | |
| 148 | - 'public' => $cpt->public == 1 ? true : false, | |
| 149 | - 'publicly_queryable' => $cpt->public_queryable == 1 ? true : false, | |
| 150 | - 'show_ui' => $cpt->show_ui == 1 ? true : false, | |
| 151 | - 'show_in_menu' => $cpt->show_in_menu == 1 ? true : false, | |
| 152 | - 'query_var' => true, | |
| 153 | - 'rewrite' => array('slug' => $cpt->name), | |
| 154 | - 'capability_type' => 'post', | |
| 155 | - 'has_archive' => true, | |
| 156 | - 'hierarchical' => false, | |
| 157 | - 'show_in_rest' => isset($cpt->show_in_rest) == 1 ? true : false, | |
| 158 | - 'menu_icon' => $cpt->menu_icon, | |
| 159 | - 'supports' => array('title','editor', 'author', 'excerpt', 'comments'), | |
| 160 | - ); | |
| 161 | - register_post_type($cpt->name, $args); | |
| 162 | - } | |
| 163 | - } | |
| 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(); | |
| 164 | 129 | } |
| 130 | + Hooks::init_hooks(); | |
| 131 | + $this->update_tables(); | |
| 132 | + do_action('bitform_loaded'); | |
| 133 | + } | |
| 165 | 134 | |
| 166 | - /** | |
| 167 | - * Initialize plugin for localization | |
| 168 | - * | |
| 169 | - * @uses load_plugin_textdomain() | |
| 170 | - */ | |
| 171 | - public function localization_setup() | |
| 172 | - { | |
| 173 | - load_plugin_textdomain('bitform', false, dirname(BITFORMS_PLUGIN_BASENAME) . '/languages'); | |
| 174 | - } | |
| 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 | + } | |
| 175 | 146 | |
| 176 | - /** | |
| 177 | - * Instantiate the required classes | |
| 178 | - * | |
| 179 | - * @return void | |
| 180 | - */ | |
| 181 | - public function init_classes() | |
| 182 | - { | |
| 183 | - if (Request::Check('admin')) { | |
| 184 | - (new Admin_Bar())->register(); | |
| 185 | - } | |
| 186 | - if (Request::Check('ajax')) { | |
| 187 | - new AjaxService(); | |
| 188 | - } | |
| 189 | - if (Request::Check('frontend')) { | |
| 190 | - $formHandler = new FormHandler(); | |
| 191 | - $formHandler->frontend; | |
| 192 | - } | |
| 193 | - if (Request::isPluginPage()) { | |
| 194 | - (new FileDownloadProvider())->register(); | |
| 195 | - } | |
| 196 | - if (current_user_can('edit_posts')) { | |
| 197 | - (new GutenBlockProvider())->register(); | |
| 198 | - } | |
| 199 | - } | |
| 147 | + public function plugin_action_links($links) | |
| 148 | + { | |
| 149 | + $links[] = '<a href="https://bitapps.pro/docs/bit-form/" target="_blank">' . __('Docs') . '</a>'; | |
| 200 | 150 | |
| 201 | - /** | |
| 202 | - * Plugin action links | |
| 203 | - * | |
| 204 | - * @param array $links | |
| 205 | - * | |
| 206 | - * @return array | |
| 207 | - */ | |
| 208 | - public function plugin_action_links($links) | |
| 209 | - { | |
| 210 | - $links[] = '<a href="https://www.bitpress.pro" target="_blank">' . __('Docs', 'bitform') . '</a>'; | |
| 151 | + return $links; | |
| 152 | + } | |
| 211 | 153 | |
| 212 | - return $links; | |
| 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; | |
| 213 | 165 | } |
| 214 | 166 | |
| 167 | + static::$instance = new static($main_file); | |
| 168 | + static::$instance->register(); | |
| 169 | + return true; | |
| 170 | + } | |
| 215 | 171 | |
| 216 | - public function init_plugin() | |
| 217 | - { | |
| 218 | - $this->init_hooks(); | |
| 219 | - $this->update_tables(); | |
| 220 | - do_action('bitform_loaded'); | |
| 221 | - } | |
| 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); | |
| 222 | 178 | |
| 223 | - /** | |
| 224 | - * Retrieves the main instance of the plugin. | |
| 225 | - * | |
| 226 | - * @since 1.0.0-alpha | |
| 227 | - * | |
| 228 | - * @return BITFORM Plugin main instance. | |
| 229 | - */ | |
| 230 | - public static function instance() | |
| 231 | - { | |
| 232 | - return static::$instance; | |
| 233 | - } | |
| 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/'); | |
| 234 | 182 | |
| 235 | - /** | |
| 236 | - * Loads the plugin main instance and initializes it. | |
| 237 | - * | |
| 238 | - * @param string $main_file Absolute path to the plugin main file. | |
| 239 | - * | |
| 240 | - * @return bool True if the plugin main instance could be loaded, false otherwise. | |
| 241 | - */ | |
| 242 | - public static function load($main_file) | |
| 243 | - { | |
| 244 | - if (null !== static::$instance) { | |
| 245 | - return false; | |
| 246 | - } | |
| 247 | - | |
| 248 | - static::$instance = new static($main_file); | |
| 249 | - static::$instance->register(); | |
| 250 | - return true; | |
| 251 | - } | |
| 183 | + Telemetry::report()->addPluginData()->init(); | |
| 184 | + Telemetry::feedback()->init(); | |
| 185 | + } | |
| 252 | 186 | } |