| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Util; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Database\DB; |
| 6 |
use BitCode\BitForm\Core\Form\FormHandler; |
| 7 |
use BitCode\BitForm\Core\Fallback\FormEntryMetaChange; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class handling plugin activation. |
| 11 |
* |
| 12 |
* @since 1.0.0 |
| 13 |
*/ |
| 14 |
final class Activation |
| 15 |
{ |
| 16 |
public function activate() |
| 17 |
{ |
| 18 |
add_action('bitforms_activation', array($this, 'install')); |
| 19 |
} |
| 20 |
|
| 21 |
public function install() |
| 22 |
{ |
| 23 |
$installed = get_option('bitforms_installed'); |
| 24 |
|
| 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(); |
| 33 |
} |
| 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 = " |
| 46 |
<IfDefine php_flag> |
| 47 |
php_flag engine off |
| 48 |
</IfDefine> |
| 49 |
Options -Indexes |
| 50 |
Order allow,deny |
| 51 |
Deny from all |
| 52 |
Require all denied |
| 53 |
"; |
| 54 |
fwrite($htaccessFile, $rules); |
| 55 |
fclose($htaccessFile); |
| 56 |
} |
| 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); |
| 62 |
} |
| 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 |
} |
| 69 |
} |
| 70 |
|
| 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 |
} |
| 102 |
} |
| 103 |
if (!empty($route_value)) { |
| 104 |
update_option('bitforms_routes', esc_sql($route_value)); |
| 105 |
} |
| 106 |
flush_rewrite_rules(); |
| 107 |
} |
| 108 |
|
| 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 |
} |
| 122 |
} |
| 123 |
|