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