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