| 1 |
<?php |
| 2 |
|
| 3 |
namespace SiteLeads\Core; |
| 4 |
|
| 5 |
use SiteLeads\Constants; |
| 6 |
use SiteLeads\Utils; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Activation { |
| 13 |
use Singleton; |
| 14 |
|
| 15 |
protected function __construct() { |
| 16 |
register_activation_hook( SITELEADS_ENTRY_FILE, array( $this, 'onActivate' ) ); |
| 17 |
$this->maybeRedirectAfterActivation(); |
| 18 |
} |
| 19 |
|
| 20 |
protected function maybeRedirectAfterActivation() { |
| 21 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
add_action( |
| 26 |
'admin_init', |
| 27 |
function () { |
| 28 |
if ( get_option( 'siteleads_activation_redirect', false ) ) { |
| 29 |
delete_option( 'siteleads_activation_redirect' ); |
| 30 |
wp_safe_redirect( admin_url( 'admin.php?page=siteleads' ) ); |
| 31 |
exit; |
| 32 |
} |
| 33 |
} |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
public function onActivate() { |
| 39 |
$this->setFlags(); |
| 40 |
$this->loadDefaultData(); |
| 41 |
$this->createAnalyticsTables(); |
| 42 |
$this->registerActivationRedirect(); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
public function setFlags() { |
| 47 |
|
| 48 |
$activation_time_key = 'activation_time'; |
| 49 |
|
| 50 |
if ( defined( 'SITELEADS_IS_PRO' ) && SITELEADS_IS_PRO ) { |
| 51 |
$activation_time_key = 'pro_' . $activation_time_key; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! Flags::get( $activation_time_key ) ) { |
| 55 |
Flags::set( $activation_time_key, time() ); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function registerActivationRedirect() { |
| 60 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
add_option( 'siteleads_activation_redirect', true ); |
| 73 |
} |
| 74 |
|
| 75 |
public function loadDefaultData() { |
| 76 |
$query = new \WP_Query( |
| 77 |
array( |
| 78 |
'post_type' => Constants::$siteLeadsDataPostType, |
| 79 |
'post_status' => array( 'draft', 'publish' ), |
| 80 |
'no_found_rows' => true, |
| 81 |
'post_per_page' => 1, |
| 82 |
|
| 83 |
) |
| 84 |
); |
| 85 |
|
| 86 |
if ( ! $query->have_posts() ) { |
| 87 |
$this->insertDefaultData(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public function insertDefaultData() { |
| 92 |
$content = file_get_contents( Utils::getFilePath( 'defaults/default-data.json' ) ); |
| 93 |
wp_insert_post( |
| 94 |
array( |
| 95 |
'post_content' => $content, |
| 96 |
'post_status' => 'publish', |
| 97 |
'post_type' => Constants::$siteLeadsDataPostType, |
| 98 |
'post_name' => Constants::$siteLeadsDataPostType, |
| 99 |
'post_title' => __( 'SiteLeads data', 'siteleads' ), |
| 100 |
), |
| 101 |
true |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
public function createAnalyticsTables() { |
| 106 |
global $wpdb; |
| 107 |
$table_name = $wpdb->prefix . 'siteleads_events'; |
| 108 |
$charset_collate = $wpdb->get_charset_collate(); |
| 109 |
|
| 110 |
$sql = "CREATE TABLE $table_name ( |
| 111 |
event_id CHAR(36) NOT NULL, |
| 112 |
event_type VARCHAR(50) NOT NULL, |
| 113 |
channel VARCHAR(50) NULL, |
| 114 |
channels TEXT NOT NULL, |
| 115 |
visitor_id VARCHAR(64) NULL, |
| 116 |
user_id BIGINT(20) NULL, |
| 117 |
session_id CHAR(36) NULL, |
| 118 |
widget_id VARCHAR(50) NULL, |
| 119 |
agent_id VARCHAR(50) NULL, |
| 120 |
page_url TEXT, |
| 121 |
device_type VARCHAR(20) DEFAULT 'unknown', |
| 122 |
has_parent_widget BOOL DEFAULT TRUE, |
| 123 |
created_at DATE NOT NULL, |
| 124 |
PRIMARY KEY (event_id) |
| 125 |
) $charset_collate;"; |
| 126 |
|
| 127 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 128 |
dbDelta( $sql ); |
| 129 |
} |
| 130 |
} |
| 131 |
|