| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) { |
| 4 |
if (!headers_sent()) { |
| 5 |
header('HTTP/1.1 403 Forbidden'); |
| 6 |
} |
| 7 |
die("Protected By WebTotem!"); |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Plugin initializer. |
| 12 |
* |
| 13 |
*/ |
| 14 |
class WebTotemInterface extends WebTotem { |
| 15 |
|
| 16 |
/** |
| 17 |
* Execute pre-checks before every page. |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
public static function startupChecks() { |
| 22 |
$_page = WebTotemRequest::get('page'); |
| 23 |
if(strpos($_page, 'wtotem') === 0){ |
| 24 |
if(!WebTotemOption::isActivated() and $_page !== 'wtotem_activation') { |
| 25 |
// If the plugin is not activated by the API key, then redirect to the activation page. |
| 26 |
wp_safe_redirect( WebTotem::adminURL('admin.php?page=wtotem_activation') ); |
| 27 |
exit; |
| 28 |
} |
| 29 |
elseif (WebTotemOption::isActivated() and ($_page === 'wtotem_activation' or $_page === 'wtotem')){ |
| 30 |
// If the plugin is activated by the API key, then redirect to the main page. |
| 31 |
if(self::isMultiSite() and is_super_admin()){ |
| 32 |
// Main page is all sites page. |
| 33 |
wp_safe_redirect( WebTotem::adminURL('admin.php?page=wtotem_all_sites') ); |
| 34 |
} else { |
| 35 |
// Main page is dashboard page. |
| 36 |
wp_safe_redirect( WebTotem::adminURL('admin.php?page=wtotem_dashboard') ); |
| 37 |
} |
| 38 |
exit; |
| 39 |
} |
| 40 |
elseif(WebTotemOption::isActivated()) { |
| 41 |
// Checking whether agents are installed, if they not installed, then install. |
| 42 |
self::checkAgents(); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
$sapi = @php_sapi_name(); |
| 47 |
if( ! current_user_can( 'publish_posts' ) and $sapi != "cli" ) { |
| 48 |
if ($waf = WebTotemOption::getOption(("waf_installed_file"))) { |
| 49 |
$include_waf_file = ABSPATH . '/_include_' . $waf; |
| 50 |
|
| 51 |
if (is_file($include_waf_file) && is_readable($include_waf_file)) { |
| 52 |
include_once $include_waf_file; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Checking whether agents are installed, if they not installed, then install. |
| 60 |
*/ |
| 61 |
private static function checkAgents(){ |
| 62 |
|
| 63 |
$api_key = WebTotemOption::getOption('api_key'); |
| 64 |
|
| 65 |
if(!$api_key){ |
| 66 |
// Check if the plugin version has changed. |
| 67 |
WebTotemAgentManager::checkVersion(); |
| 68 |
$api_key = WebTotemOption::getOption('api_key'); |
| 69 |
} |
| 70 |
|
| 71 |
$host = WebTotemAPI::siteInfo(); |
| 72 |
|
| 73 |
if ($api_key && array_key_exists('id', $host)) { |
| 74 |
|
| 75 |
// Install Agent Manager if it was not previously installed. |
| 76 |
$am_installed = WebTotemAgentManager::checkInstalledService('am'); |
| 77 |
if (!$am_installed['option_status'] || !$am_installed['file_status']) { |
| 78 |
|
| 79 |
$am_was_installed = WebTotemAgentManager::amInstall(); |
| 80 |
|
| 81 |
if (!$am_was_installed) { |
| 82 |
WebTotemOption::setOptions(['am_installed' => FALSE]); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* When adding a new site, add it to the WebTotem platform. |
| 91 |
*/ |
| 92 |
public static function addNewSite($new_site){ |
| 93 |
if(WebTotem::isMultiSite() and is_super_admin()) { |
| 94 |
$domain = untrailingslashit($new_site->domain . $new_site->path); |
| 95 |
WebTotemOption::setNotification( 'info', _('A new website has been added: ', 'wtotem') . $domain); |
| 96 |
WebTotemAPI::addMultiSiteNewSites([$domain]); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Verify the nonce of the previous page after a form submission. |
| 102 |
* |
| 103 |
* @return bool True if the nonce is valid, false otherwise. |
| 104 |
*/ |
| 105 |
public static function checkNonce() { |
| 106 |
if (!empty($_POST)) { |
| 107 |
$name = 'wtotem_page_nonce'; |
| 108 |
$value = WebTotemRequest::post($name); |
| 109 |
|
| 110 |
if (!$value || !wp_verify_nonce($value, $name)) { |
| 111 |
WebTotemOption::setNotification('error', __('The WordPress CSRF check failed. The submitted form is missing an important unique code. Go back and try again.', 'wtotem')); |
| 112 |
return false; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* A safe way to add JavaScript and css files to a WordPress-managed page |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public static function enqueueScripts() { |
| 125 |
if( str_contains( WebTotemRequest::get( 'page' ), 'wtotem_' ) ){ |
| 126 |
// Adding CSS files. |
| 127 |
wp_register_style( |
| 128 |
'wtotem_flatpickr', |
| 129 |
WEBTOTEM_URL . '/includes/css/flatpickr.min.css', |
| 130 |
array(), |
| 131 |
WebTotem::fileVersion('includes/css/flatpickr.min.css') |
| 132 |
); |
| 133 |
wp_enqueue_style('wtotem_flatpickr'); |
| 134 |
|
| 135 |
wp_register_style( |
| 136 |
'wtotem_main_css', |
| 137 |
WEBTOTEM_URL . '/includes/css/main.css', |
| 138 |
array(), |
| 139 |
WebTotem::fileVersion('includes/css/main.css') |
| 140 |
); |
| 141 |
wp_enqueue_style('wtotem_main_css'); |
| 142 |
|
| 143 |
// Adding JS files. |
| 144 |
wp_register_script( |
| 145 |
'wtotem_amplitude', |
| 146 |
WEBTOTEM_URL . '/includes/js/amplitude.js', |
| 147 |
array( 'jquery' ), |
| 148 |
WebTotem::fileVersion('includes/js/amplitude.js'), |
| 149 |
false |
| 150 |
); |
| 151 |
wp_enqueue_script('wtotem_amplitude'); |
| 152 |
|
| 153 |
wp_register_script( |
| 154 |
'wtotem_d3', |
| 155 |
WEBTOTEM_URL . '/includes/js/d3.v4.js', |
| 156 |
array( 'jquery' ), |
| 157 |
WebTotem::fileVersion('includes/js/d3.v4.js'), |
| 158 |
true |
| 159 |
); |
| 160 |
wp_enqueue_script('wtotem_d3'); |
| 161 |
|
| 162 |
wp_register_script( |
| 163 |
'wtotem_chart', |
| 164 |
WEBTOTEM_URL . '/includes/js/chart.js', |
| 165 |
array( 'jquery', 'wtotem_d3'), |
| 166 |
WebTotem::fileVersion('includes/js/chart.js'), |
| 167 |
true |
| 168 |
); |
| 169 |
wp_enqueue_script('wtotem_chart'); |
| 170 |
|
| 171 |
wp_register_script( |
| 172 |
'wtotem_flatpickr_js', |
| 173 |
WEBTOTEM_URL . '/includes/js/flatpickr.js', |
| 174 |
array( 'jquery', 'wp-i18n' ), |
| 175 |
WebTotem::fileVersion('includes/js/flatpickr.js'), |
| 176 |
true |
| 177 |
); |
| 178 |
wp_enqueue_script('wtotem_flatpickr_js'); |
| 179 |
wp_set_script_translations( 'wtotem_flatpickr_js', 'wtotem' ); |
| 180 |
|
| 181 |
wp_register_script( |
| 182 |
'wtotem_jsdelivr', |
| 183 |
WEBTOTEM_URL . '/includes/js/jsdelivr_chart.js', |
| 184 |
array( 'jquery' ), |
| 185 |
WebTotem::fileVersion('includes/js/jsdelivr_chart.js'), |
| 186 |
true |
| 187 |
); |
| 188 |
wp_enqueue_script('wtotem_jsdelivr'); |
| 189 |
|
| 190 |
wp_register_script( |
| 191 |
'wtotem_progress_bar', |
| 192 |
WEBTOTEM_URL . '/includes/js/progress_bar.js', |
| 193 |
array(), |
| 194 |
WebTotem::fileVersion('includes/js/progress_bar.js'), |
| 195 |
true |
| 196 |
); |
| 197 |
wp_enqueue_script('wtotem_progress_bar'); |
| 198 |
|
| 199 |
wp_register_script( |
| 200 |
'wtotem_main', |
| 201 |
WEBTOTEM_URL . '/includes/js/main.js', |
| 202 |
array( 'jquery' ), |
| 203 |
WebTotem::fileVersion('includes/js/main.js'), |
| 204 |
true |
| 205 |
); |
| 206 |
wp_enqueue_script('wtotem_main'); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|