| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Util; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use BitCode\BitForm\Admin\Form\Helpers; |
| 10 |
use BitCode\BitForm\Core\Database\DB; |
| 11 |
use BitCode\BitForm\Core\Database\FormModel; |
| 12 |
use WP_Error; |
| 13 |
use WP_Site; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class handling plugin activation. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
final class Activation |
| 21 |
{ |
| 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(); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 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 |
} |
| 58 |
|
| 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()); |
| 67 |
} |
| 68 |
|
| 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 = ' |
| 102 |
<IfDefine php_flag> |
| 103 |
php_flag engine off |
| 104 |
</IfDefine> |
| 105 |
Options -Indexes |
| 106 |
Order allow,deny |
| 107 |
Deny from all |
| 108 |
Require all denied |
| 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 |
} |
| 212 |
} |
| 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')); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
$wpdb->query('COMMIT'); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 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; |
| 270 |
} |
| 271 |
} else { |
| 272 |
$file_page = ['ID' => $routes['file'], 'post_status' => 'publish']; |
| 273 |
wp_update_post($file_page); |
| 274 |
} |
| 275 |
} |
| 276 |
if (!empty($route_value)) { |
| 277 |
update_option('bitforms_routes', esc_sql($route_value)); |
| 278 |
} |
| 279 |
flush_rewrite_rules(); |
| 280 |
} |
| 281 |
|
| 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 |
} |
| 295 |
} |
| 296 |
|