| @@ -1,9 +1,17 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Core\Util; |
| 4 | 4 | |
| 5 | +if (!defined('ABSPATH')) { | |
| 6 | + exit; | |
| 7 | +} | |
| 8 | + | |
| 9 | +use BitCode\BitForm\Admin\Form\Helpers; | |
| 5 | 10 | use BitCode\BitForm\Core\Database\DB; |
| 11 | +use BitCode\BitForm\Core\Database\FormModel; | |
| 12 | +use WP_Error; | |
| 13 | +use WP_Site; | |
| 6 | 14 | |
| 7 | 15 | /** |
| 8 | 16 | * Class handling plugin activation. |
| 9 | 17 | * |
| @@ -10,38 +18,88 @@ | ||
| 10 | 18 | * @since 1.0.0 |
| 11 | 19 | */ |
| 12 | 20 | final class Activation |
| 13 | 21 | { |
| 14 | - public function activate() | |
| 15 | - { | |
| 16 | - add_action('bitforms_activation', array($this, 'install')); | |
| 22 | + private static $formModel; | |
| 23 | + | |
| 24 | + public function __construct() | |
| 25 | + { | |
| 26 | + static::$formModel = new FormModel(); | |
| 27 | + } | |
| 28 | + | |
| 29 | + public function activate() | |
| 30 | + { | |
| 31 | + add_action('bitforms_activation', [$this, 'install']); | |
| 32 | + add_action('wp_initialize_site', [$this, 'initializeNewSite'], 120, 1); | |
| 33 | + } | |
| 34 | + | |
| 35 | + public function install($networkWide) | |
| 36 | + { | |
| 37 | + if ($networkWide && \function_exists('is_multisite') && is_multisite()) { | |
| 38 | + $sites = get_sites(['fields' => 'ids', 'network_id' => get_current_network_id()]); | |
| 39 | + foreach ($sites as $site) { | |
| 40 | + switch_to_blog($site); | |
| 41 | + $this->installOnCurrentBlog(); | |
| 42 | + restore_current_blog(); | |
| 43 | + } | |
| 44 | + } else { | |
| 45 | + $this->installOnCurrentBlog(); | |
| 17 | 46 | } |
| 47 | + } | |
| 18 | 48 | |
| 19 | - public function install() | |
| 20 | - { | |
| 21 | - $installed = get_option('bitforms_installed'); | |
| 49 | + public function initializeNewSite(WP_Site $site) | |
| 50 | + { | |
| 51 | + switch_to_blog($site->blog_id); | |
| 52 | + $plugin = plugin_basename(BITFORMS_PLUGIN_MAIN_FILE); | |
| 53 | + if (!is_plugin_active_for_network($plugin)) { | |
| 54 | + do_action('bitforms_activation', false); | |
| 55 | + } | |
| 56 | + restore_current_blog(); | |
| 57 | + } | |
| 22 | 58 | |
| 23 | - if (!$installed) { | |
| 24 | - DB::migrate(); | |
| 25 | - $this->createUploadDir(); | |
| 26 | - update_option('bitforms_installed', time()); | |
| 27 | - } | |
| 28 | - update_option('bitforms_version', BITFORMS_VERSION); | |
| 29 | - $this->createUploadDir(); | |
| 30 | - $this->createFrontendPages(); | |
| 59 | + public function installOnCurrentBlog() | |
| 60 | + { | |
| 61 | + $installed = get_option('bitforms_installed'); | |
| 62 | + | |
| 63 | + if (!$installed) { | |
| 64 | + DB::migrate(); | |
| 65 | + $this->createUploadDir(); | |
| 66 | + update_option('bitforms_installed', time()); | |
| 31 | 67 | } |
| 32 | 68 | |
| 33 | - private function createUploadDir() | |
| 34 | - { | |
| 35 | - if (!file_exists(BITFORMS_UPLOAD_DIR)) { | |
| 36 | - wp_mkdir_p(BITFORMS_UPLOAD_DIR); | |
| 37 | - } | |
| 38 | - if (!file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')) { | |
| 39 | - wp_mkdir_p(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles'); | |
| 40 | - } | |
| 41 | - if (file_exists(BITFORMS_UPLOAD_DIR) && !file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . '.htaccess')) { | |
| 42 | - $htaccessFile = fopen(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . '.htaccess', "w"); | |
| 43 | - $rules = " | |
| 69 | + update_option('bitforms_migrated_to_v2', true); | |
| 70 | + $appConfig = get_option('bitform_app_config'); | |
| 71 | + if (!$appConfig) { | |
| 72 | + $config = (object) []; | |
| 73 | + $config->delete_table = true; | |
| 74 | + update_option('bitform_app_config', $config); | |
| 75 | + } | |
| 76 | + $appSettings = get_option('bitform_app_settings'); | |
| 77 | + if (!$appSettings) { | |
| 78 | + $appSettings = (object) []; | |
| 79 | + $appSettings->globalMessages = Helpers::getDefaultGlobalMessages(); | |
| 80 | + update_option('bitform_app_settings', $appSettings); | |
| 81 | + } | |
| 82 | + | |
| 83 | + update_option('bitforms_version', BITFORMS_VERSION); | |
| 84 | + $this->layoutUpdate(); | |
| 85 | + $this->createUploadDir(); | |
| 86 | + | |
| 87 | + if (!defined('WP_TESTS_DOMAIN')) { | |
| 88 | + $this->createFrontendPages(); | |
| 89 | + } | |
| 90 | + } | |
| 91 | + | |
| 92 | + private function createUploadDir() | |
| 93 | + { | |
| 94 | + if (!file_exists(BITFORMS_UPLOAD_DIR)) { | |
| 95 | + wp_mkdir_p(BITFORMS_UPLOAD_DIR); | |
| 96 | + } | |
| 97 | + if (!file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')) { | |
| 98 | + wp_mkdir_p(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles'); | |
| 99 | + } | |
| 100 | + if (file_exists(BITFORMS_UPLOAD_DIR) && !file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . '.htaccess')) { | |
| 101 | + $rules = ' | |
| 44 | 102 | <IfDefine php_flag> |
| 45 | 103 | php_flag engine off |
| 46 | 104 | </IfDefine> |
| 47 | 105 | Options -Indexes |
| @@ -47,74 +105,191 @@ | ||
| 47 | 105 | Options -Indexes |
| 48 | 106 | Order allow,deny |
| 49 | 107 | Deny from all |
| 50 | 108 | Require all denied |
| 51 | - "; | |
| 52 | - fwrite($htaccessFile, $rules); | |
| 53 | - fclose($htaccessFile); | |
| 109 | + '; | |
| 110 | + FileHandler::writeFile(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . '.htaccess', $rules); | |
| 111 | + } | |
| 112 | + if (file_exists(BITFORMS_UPLOAD_DIR) && !file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . 'index.php')) { | |
| 113 | + FileHandler::writeFile(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . 'index.php', "<?php\n"); | |
| 114 | + } | |
| 115 | + if (file_exists(BITFORMS_CONTENT_DIR) && !file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'index.php')) { | |
| 116 | + FileHandler::writeFile(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'index.php', "<?php\n"); | |
| 117 | + } | |
| 118 | + } | |
| 119 | + | |
| 120 | + private function getForms() | |
| 121 | + { | |
| 122 | + $getForms = static::$formModel->get( | |
| 123 | + [ | |
| 124 | + 'id', | |
| 125 | + 'status', | |
| 126 | + 'form_content', | |
| 127 | + ], | |
| 128 | + [ | |
| 129 | + 'status' => [ | |
| 130 | + 'operator' => '!=', | |
| 131 | + 'value' => 2, | |
| 132 | + ], | |
| 133 | + ] | |
| 134 | + ); | |
| 135 | + return $getForms; | |
| 136 | + } | |
| 137 | + | |
| 138 | + private function layoutUpdate() | |
| 139 | + { | |
| 140 | + $getForms = $this->getForms(); | |
| 141 | + | |
| 142 | + if (is_wp_error($getForms)) { | |
| 143 | + return $getForms; | |
| 144 | + } | |
| 145 | + | |
| 146 | + global $wpdb; | |
| 147 | + | |
| 148 | + foreach ($getForms as $index => $form) { | |
| 149 | + $content = json_decode($form->form_content); | |
| 150 | + $layout = $content->layout; | |
| 151 | + $updated = false; | |
| 152 | + | |
| 153 | + if (is_array($layout)) { | |
| 154 | + continue; | |
| 155 | + } | |
| 156 | + foreach ($layout->lg as $lyIndex => $lg) { | |
| 157 | + if ($lg->w < 10) { | |
| 158 | + $updated = true; | |
| 159 | + $layout->lg[$lyIndex]->h = $lg->h * 20; | |
| 160 | + $layout->lg[$lyIndex]->w = $lg->w * 10; | |
| 161 | + $layout->lg[$lyIndex]->x = $lg->x * 10; | |
| 162 | + $layout->lg[$lyIndex]->y = $lg->y * 20; | |
| 163 | + | |
| 164 | + $layout->md[$lyIndex]->h = $layout->md[$lyIndex]->h * 20; | |
| 165 | + $layout->md[$lyIndex]->w = $layout->md[$lyIndex]->w * 10; | |
| 166 | + $layout->md[$lyIndex]->x = $layout->md[$lyIndex]->x * 10; | |
| 167 | + $layout->md[$lyIndex]->y = $layout->md[$lyIndex]->y * 20; | |
| 168 | + | |
| 169 | + $layout->sm[$lyIndex]->h = $layout->sm[$lyIndex]->h * 20; | |
| 170 | + $layout->sm[$lyIndex]->w = $layout->sm[$lyIndex]->w * 10; | |
| 171 | + $layout->sm[$lyIndex]->x = $layout->sm[$lyIndex]->x * 10; | |
| 172 | + $layout->sm[$lyIndex]->y = $layout->sm[$lyIndex]->y * 20; | |
| 173 | + | |
| 174 | + if (isset($layout->lg[$lyIndex]->maxW)) { | |
| 175 | + $layout->lg[$lyIndex]->maxW = $lg->maxW * 10; | |
| 176 | + } | |
| 177 | + if (isset($layout->lg[$lyIndex]->maxH)) { | |
| 178 | + $layout->lg[$lyIndex]->maxH = $lg->maxH * 20; | |
| 179 | + } | |
| 180 | + if (isset($layout->lg[$lyIndex]->minW)) { | |
| 181 | + $layout->lg[$lyIndex]->minW = $lg->minW * 10; | |
| 182 | + } | |
| 183 | + if (isset($layout->lg[$lyIndex]->minH)) { | |
| 184 | + $layout->lg[$lyIndex]->minH = $lg->minH * 20; | |
| 185 | + } | |
| 186 | + | |
| 187 | + if (isset($layout->md[$lyIndex]->maxW)) { | |
| 188 | + $layout->md[$lyIndex]->maxW = $layout->md[$lyIndex]->maxW * 10; | |
| 189 | + } | |
| 190 | + if (isset($layout->md[$lyIndex]->maxH)) { | |
| 191 | + $layout->md[$lyIndex]->maxH = $layout->md[$lyIndex]->maxH * 20; | |
| 192 | + } | |
| 193 | + if (isset($layout->md[$lyIndex]->minW)) { | |
| 194 | + $layout->md[$lyIndex]->minW = $layout->md[$lyIndex]->minW * 10; | |
| 195 | + } | |
| 196 | + if (isset($layout->md[$lyIndex]->minH)) { | |
| 197 | + $layout->md[$lyIndex]->minH = $layout->md[$lyIndex]->minH * 20; | |
| 198 | + } | |
| 199 | + | |
| 200 | + if (isset($layout->sm[$lyIndex]->maxW)) { | |
| 201 | + $layout->sm[$lyIndex]->maxW = $layout->sm[$lyIndex]->maxW * 10; | |
| 202 | + } | |
| 203 | + if (isset($layout->sm[$lyIndex]->maxH)) { | |
| 204 | + $layout->sm[$lyIndex]->maxH = $layout->sm[$lyIndex]->maxH * 20; | |
| 205 | + } | |
| 206 | + if (isset($layout->sm[$lyIndex]->minW)) { | |
| 207 | + $layout->sm[$lyIndex]->minW = $layout->md[$lyIndex]->minW * 10; | |
| 208 | + } | |
| 209 | + if (isset($layout->sm[$lyIndex]->minH)) { | |
| 210 | + $layout->sm[$lyIndex]->minH = $layout->sm[$lyIndex]->minH * 20; | |
| 211 | + } | |
| 54 | 212 | } |
| 55 | - if (file_exists(BITFORMS_UPLOAD_DIR) && !file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . 'index.php')) { | |
| 56 | - $indexFile = fopen(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . 'index.php', "w"); | |
| 57 | - $code = "<?php\n"; | |
| 58 | - fwrite($indexFile, $code); | |
| 59 | - fclose($indexFile); | |
| 213 | + } | |
| 214 | + | |
| 215 | + $getForms[$index]->form_content = wp_json_encode($content); | |
| 216 | + | |
| 217 | + $wpdb->query('START TRANSACTION'); | |
| 218 | + | |
| 219 | + if ($updated) { | |
| 220 | + $status = static::$formModel->update( | |
| 221 | + [ | |
| 222 | + 'form_content' => $getForms[$index]->form_content, | |
| 223 | + ], | |
| 224 | + [ | |
| 225 | + 'id' => $form->id, | |
| 226 | + ] | |
| 227 | + ); | |
| 228 | + | |
| 229 | + if (is_wp_error($status)) { | |
| 230 | + $wpdb->query('ROLLBACK'); | |
| 231 | + return new WP_Error('update_error', __('Sorry, Error occured in updated form', 'bit-form')); | |
| 60 | 232 | } |
| 61 | - if (file_exists(BITFORMS_CONTENT_DIR) && !file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'index.php')) { | |
| 62 | - $indexFile = fopen(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'index.php', "w"); | |
| 63 | - $code = "<?php\n"; | |
| 64 | - fwrite($indexFile, $code); | |
| 65 | - fclose($indexFile); | |
| 66 | - } | |
| 233 | + } | |
| 234 | + | |
| 235 | + $wpdb->query('COMMIT'); | |
| 67 | 236 | } |
| 237 | + } | |
| 68 | 238 | |
| 69 | - private function createFrontendPages() | |
| 70 | - { | |
| 71 | - $args = array( | |
| 72 | - 'label' => __('Bitforms Pages', 'bitform'), | |
| 73 | - 'public' => true, | |
| 74 | - 'show_ui' => true, | |
| 75 | - 'show_in_menu' => false, | |
| 76 | - 'capability_type' => 'page', | |
| 77 | - 'hierarchical' => false, | |
| 78 | - 'query_var' => false, | |
| 79 | - 'supports' => array('title'), | |
| 80 | - 'show_in_rest' => false | |
| 81 | - ); | |
| 82 | - register_post_type('bitforms', $args); | |
| 83 | - $routes = get_option('bitforms_routes'); | |
| 84 | - $route_value = array(); | |
| 85 | - if (!$routes) { | |
| 86 | - $file_route_id = $this->insertPage(); | |
| 87 | - if (!is_wp_error($file_route_id)) { | |
| 88 | - $route_value['file'] = $file_route_id; | |
| 89 | - } | |
| 90 | - } elseif (isset($routes['file'])) { | |
| 91 | - if (empty(get_post($routes['file']))) { | |
| 92 | - $file_route_id = $this->insertPage(); | |
| 93 | - if (!is_wp_error($file_route_id)) { | |
| 94 | - $route_value['file'] = $file_route_id; | |
| 95 | - } | |
| 96 | - } else { | |
| 97 | - $file_page = array('ID' => $routes['file'], 'post_status' => 'publish'); | |
| 98 | - wp_update_post($file_page); | |
| 99 | - } | |
| 239 | + private function createFrontendPages() | |
| 240 | + { | |
| 241 | + $args = [ | |
| 242 | + 'label' => __('Bitforms Pages', 'bit-form'), | |
| 243 | + 'public' => false, | |
| 244 | + 'publicly_queryable' => true, | |
| 245 | + 'exclude_from_search' => true, | |
| 246 | + 'show_in_nav_menus' => false, | |
| 247 | + 'show_ui' => true, | |
| 248 | + 'show_in_menu' => false, | |
| 249 | + 'capability_type' => 'page', | |
| 250 | + 'hierarchical' => false, | |
| 251 | + 'has_archive' => false, | |
| 252 | + 'query_var' => false, | |
| 253 | + 'rewrite' => true, | |
| 254 | + 'supports' => ['title'], | |
| 255 | + 'show_in_rest' => false | |
| 256 | + ]; | |
| 257 | + register_post_type('bitforms', $args); | |
| 258 | + $routes = get_option('bitforms_routes'); | |
| 259 | + $route_value = []; | |
| 260 | + if (!$routes) { | |
| 261 | + $file_route_id = $this->insertPage(); | |
| 262 | + if (!is_wp_error($file_route_id)) { | |
| 263 | + $route_value['file'] = $file_route_id; | |
| 264 | + } | |
| 265 | + } elseif (isset($routes['file'])) { | |
| 266 | + if (empty(get_post($routes['file']))) { | |
| 267 | + $file_route_id = $this->insertPage(); | |
| 268 | + if (!is_wp_error($file_route_id)) { | |
| 269 | + $route_value['file'] = $file_route_id; | |
| 100 | 270 | } |
| 101 | - if (!empty($route_value)) { | |
| 102 | - update_option('bitforms_routes', esc_sql($route_value)); | |
| 103 | - } | |
| 104 | - flush_rewrite_rules(); | |
| 271 | + } else { | |
| 272 | + $file_page = ['ID' => $routes['file'], 'post_status' => 'publish']; | |
| 273 | + wp_update_post($file_page); | |
| 274 | + } | |
| 105 | 275 | } |
| 276 | + if (!empty($route_value)) { | |
| 277 | + update_option('bitforms_routes', esc_sql($route_value)); | |
| 278 | + } | |
| 279 | + flush_rewrite_rules(); | |
| 280 | + } | |
| 106 | 281 | |
| 107 | - private function insertPage() | |
| 108 | - { | |
| 109 | - return wp_insert_post( | |
| 110 | - array( | |
| 111 | - 'post_name' => 'bitforms-file', | |
| 112 | - 'comment_status' => 'closed', | |
| 113 | - 'ping_status' => 'closed', | |
| 114 | - 'post_content' => '<!-- wp:shortcode -->[bitforms-frontend-file /]<!-- /wp:shortcode -->', | |
| 115 | - 'post_status' => 'publish', | |
| 116 | - 'post_type' => 'bitforms' | |
| 117 | - ) | |
| 118 | - ); | |
| 119 | - } | |
| 282 | + private function insertPage() | |
| 283 | + { | |
| 284 | + return wp_insert_post( | |
| 285 | + [ | |
| 286 | + 'post_name' => 'bitforms-file', | |
| 287 | + 'comment_status' => 'closed', | |
| 288 | + 'ping_status' => 'closed', | |
| 289 | + 'post_content' => '<!-- wp:shortcode -->[bitforms-frontend-file /]<!-- /wp:shortcode -->', | |
| 290 | + 'post_status' => 'publish', | |
| 291 | + 'post_type' => 'bitforms' | |
| 292 | + ] | |
| 293 | + ); | |
| 294 | + } | |
| 120 | 295 | } |