api-auth.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. <?php
  2. /**
  3. * Google Client admin class.
  4. *
  5. * Handles retrieving whether a particular notice has been dismissed or not,
  6. * as well as marking a notice as dismissed.
  7. *
  8. * @since 7.0.0
  9. *
  10. * @package MonsterInsights
  11. * @subpackage GA Client
  12. * @author Chris Christoff
  13. */
  14. // Exit if accessed directly
  15. if ( ! defined( 'ABSPATH' ) ) {
  16. exit;
  17. }
  18. final class MonsterInsights_API_Auth {
  19. /**
  20. * Primary class constructor.
  21. *
  22. * @access public
  23. * @since 7.0.0
  24. */
  25. public function __construct() {
  26. // Authentication Actions
  27. add_action( 'wp_ajax_monsterinsights_maybe_authenticate', array( $this, 'maybe_authenticate' ) );
  28. add_action( 'wp_ajax_monsterinsights_maybe_reauthenticate', array( $this, 'maybe_reauthenticate' ) );
  29. add_action( 'wp_ajax_monsterinsights_maybe_verify', array( $this, 'maybe_verify' ) );
  30. add_action( 'wp_ajax_monsterinsights_maybe_delete', array( $this, 'maybe_delete' ) );
  31. add_action( 'admin_init', array( $this, 'authenticate_listener' ) );
  32. add_action( 'admin_init', array( $this, 'reauthenticate_listener' ) );
  33. add_action( 'wp_ajax_nopriv_monsterinsights_is_installed', array( $this, 'is_installed' ) );
  34. add_action( 'wp_ajax_nopriv_monsterinsights_rauthenticate', array( $this, 'authenticate' ) );
  35. }
  36. public function get_tt(){
  37. $tt = is_network_admin() ? get_site_option( 'monsterinsights_network_tt', '' ) : get_option( 'monsterinsights_site_tt', '' );
  38. if ( empty( $tt ) ) {
  39. // if TT is empty, generate a new one, save it and then return it
  40. $tt = $this->generate_tt();
  41. $this->is_network_admin() ? update_site_option( 'monsterinsights_network_tt', $tt ) : update_option( 'monsterinsights_site_tt', $tt );
  42. }
  43. return $tt;
  44. }
  45. public function rotate_tt(){
  46. $tt = $this->generate_tt();
  47. is_network_admin() ? update_site_option( 'monsterinsights_network_tt', $tt ) : update_option( 'monsterinsights_site_tt', $tt );
  48. }
  49. public function generate_tt(){
  50. return hash( 'sha512', wp_generate_password( 128, true, true ) . AUTH_SALT . uniqid( "", true ) );
  51. }
  52. public function validate_tt( $passed_tt = '' ) {
  53. $tt = $this->get_tt();
  54. return hash_equals( $tt, $passed_tt );
  55. }
  56. public function is_installed() {
  57. wp_send_json_success(
  58. array(
  59. 'version' => MONSTERINSIGHTS_VERSION,
  60. 'pro' => monsterinsights_is_pro_version(),
  61. )
  62. );
  63. }
  64. public function maybe_authenticate(){
  65. // Check nonce
  66. check_ajax_referer( 'mi-admin-nonce', 'nonce' );
  67. // current user can authenticate
  68. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  69. wp_send_json_error( array( 'message' => __( "You don't have permission to authenticate MonsterInsights.", 'google-analytics-for-wordpress' ) ) );
  70. }
  71. if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) {
  72. define( 'WP_NETWORK_ADMIN', true );
  73. }
  74. // Only for Pro users, require a license key to be entered first so we can link to things.
  75. $valid = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed();
  76. if ( monsterinsights_is_pro_version() && ! $valid ) {
  77. wp_send_json_error( array( 'message' => __( "Cannot authenticate. Please enter a valid, active license key for MonsterInsights Pro into the settings.", 'google-analytics-for-wordpress' ) ) );
  78. }
  79. // we do not have a current auth
  80. if ( ! $this->is_network_admin() && MonsterInsights()->auth->is_authed() ) {
  81. wp_send_json_error( array( 'message' => __( "Cannot authenticate. Please re-authenticate.", 'google-analytics-for-wordpress' ) ) );
  82. } else if ( $this->is_network_admin() && MonsterInsights()->auth->is_network_authed() ) {
  83. wp_send_json_error( array( 'message' => __( "Cannot network authenticate. Please re-authenticate on the network settings panel.", 'google-analytics-for-wordpress' ) ) );
  84. }
  85. $sitei = $this->get_sitei();
  86. //update_network_option( get_current_network_id(), 'monsterinsights_network_sitei', $sitei );
  87. $siteurl = add_query_arg( array(
  88. 'tt' => $this->get_tt(),
  89. 'sitei' => $sitei,
  90. 'miversion' => MONSTERINSIGHTS_VERSION,
  91. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  92. 'network' => is_network_admin() ? 'network' : 'site',
  93. 'siteurl' => is_network_admin() ? network_admin_url() : site_url(),
  94. 'return' => is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_network' ) : admin_url( 'admin.php?page=monsterinsights_settings' ),
  95. ), $this->get_route( 'https://' . monsterinsights_get_api_url() . 'auth/new/{type}' ) );
  96. if ( monsterinsights_is_pro_version() ) {
  97. $key = is_network_admin() ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key();
  98. $siteurl = add_query_arg( 'license', $key, $siteurl );
  99. }
  100. $siteurl = apply_filters( 'monsterinsights_maybe_authenticate_siteurl', $siteurl );
  101. wp_send_json_success( array( 'redirect' => $siteurl ) );
  102. }
  103. public function authenticate() {
  104. // Check for missing params
  105. $reqd_args = array( 'key', 'token', 'ua', 'miview', 'a', 'w', 'p', 'tt', 'network' );
  106. foreach ( $reqd_args as $arg ) {
  107. if ( empty( $_REQUEST[$arg] ) ) {
  108. wp_send_json_error(
  109. array(
  110. 'error' => 'authenticate_missing_arg',
  111. 'message' => 'Authenticate missing parameter: ' . $arg,
  112. 'version' => MONSTERINSIGHTS_VERSION,
  113. 'pro' => monsterinsights_is_pro_version(),
  114. )
  115. );
  116. }
  117. }
  118. if ( ! $this->validate_tt( $_REQUEST['tt'] ) ) {
  119. wp_send_json_error(
  120. array(
  121. 'error' => 'authenticate_invalid_tt',
  122. 'message' => 'Invalid TT sent',
  123. 'version' => MONSTERINSIGHTS_VERSION,
  124. 'pro' => monsterinsights_is_pro_version(),
  125. )
  126. );
  127. }
  128. // Invalid UA code
  129. $ua = monsterinsights_is_valid_ua( $_REQUEST['ua'] );
  130. if ( empty( $ua ) ) {
  131. wp_send_json_error(
  132. array(
  133. 'error' => 'authenticate_invalid_ua',
  134. 'message' => 'Invalid UA code sent',
  135. 'version' => MONSTERINSIGHTS_VERSION,
  136. 'pro' => monsterinsights_is_pro_version(),
  137. )
  138. );
  139. }
  140. $profile = array(
  141. 'key' => sanitize_text_field( $_REQUEST['key'] ),
  142. 'token' => sanitize_text_field( $_REQUEST['token'] ),
  143. 'ua' => monsterinsights_is_valid_ua( $_REQUEST['ua'] ),
  144. 'viewname' => sanitize_text_field( $_REQUEST['miview'] ),
  145. 'a' => sanitize_text_field( $_REQUEST['a'] ),
  146. 'w' => sanitize_text_field( $_REQUEST['w'] ),
  147. 'p' => sanitize_text_field( $_REQUEST['p'] ),
  148. 'siteurl' => site_url(),
  149. 'neturl' => network_admin_url(),
  150. );
  151. $worked = $this->verify_auth( $profile );
  152. if ( ! $worked ) {
  153. wp_send_json_error(
  154. array(
  155. 'error' => 'authenticate_auth_verification_failed',
  156. 'message' => 'Authenticate verification failed',
  157. 'version' => MONSTERINSIGHTS_VERSION,
  158. 'pro' => monsterinsights_is_pro_version(),
  159. )
  160. );
  161. }
  162. // Rotate tt
  163. $this->rotate_tt();
  164. // Save Profile
  165. $is_network = $_REQUEST['network'] === 'network';
  166. if ( $is_network ) {
  167. MonsterInsights()->auth->set_network_analytics_profile( $profile );
  168. } else {
  169. MonsterInsights()->auth->set_analytics_profile( $profile );
  170. }
  171. // Clear cache
  172. $where = $is_network ? 'network' : 'site';
  173. MonsterInsights()->reporting->delete_aggregate_data( $where );
  174. wp_send_json_success();
  175. }
  176. public function authenticate_listener(){
  177. // Make sure it's for us
  178. if ( empty( $_REQUEST['mi-oauth-action'] ) || $_REQUEST['mi-oauth-action'] !== 'auth' ) {
  179. return;
  180. }
  181. // User can authenticate
  182. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  183. return;
  184. }
  185. // Invalid request
  186. if ( empty( $_REQUEST['tt'] ) || ! $this->validate_tt( $_REQUEST['tt'] ) ) {
  187. return;
  188. }
  189. // Make sure has required params
  190. if ( empty( $_REQUEST['key'] ) ||
  191. empty( $_REQUEST['token'] ) ||
  192. empty( $_REQUEST['ua'] ) ||
  193. empty( $_REQUEST['miview'] ) ||
  194. empty( $_REQUEST['a'] ) ||
  195. empty( $_REQUEST['w'] ) ||
  196. empty( $_REQUEST['p'] )
  197. ) {
  198. return;
  199. }
  200. // Invalid UA code
  201. $ua = monsterinsights_is_valid_ua( $_REQUEST['ua'] );
  202. if ( empty( $ua ) ) {
  203. return;
  204. }
  205. $profile = array(
  206. 'key' => sanitize_text_field( $_REQUEST['key'] ),
  207. 'token' => sanitize_text_field( $_REQUEST['token'] ),
  208. 'ua' => monsterinsights_is_valid_ua( $_REQUEST['ua'] ),
  209. 'viewname' => sanitize_text_field( $_REQUEST['miview'] ),
  210. 'a' => sanitize_text_field( $_REQUEST['a'] ), // AccountID
  211. 'w' => sanitize_text_field( $_REQUEST['w'] ), // PropertyID
  212. 'p' => sanitize_text_field( $_REQUEST['p'] ), // View ID
  213. 'siteurl' => site_url(),
  214. 'neturl' => network_admin_url(),
  215. );
  216. $worked = $this->verify_auth( $profile );
  217. if ( ! $worked ) {
  218. return;
  219. }
  220. // Rotate tt
  221. $this->rotate_tt();
  222. // Save Profile
  223. $this->is_network_admin() ? MonsterInsights()->auth->set_network_analytics_profile( $profile ) : MonsterInsights()->auth->set_analytics_profile( $profile );
  224. // Clear cache
  225. $where = $this->is_network_admin() ? 'network' : 'site';
  226. MonsterInsights()->reporting->delete_aggregate_data( $where );
  227. $url = $this->is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_network' ) : admin_url( 'admin.php?page=monsterinsights_settings' ) ;
  228. $url = add_query_arg( array(
  229. 'mi_action' => 'auth',
  230. 'success' => 'true',
  231. ), $url );
  232. wp_safe_redirect( $url );
  233. exit;
  234. }
  235. public function maybe_reauthenticate(){
  236. // Check nonce
  237. check_ajax_referer( 'mi-admin-nonce', 'nonce' );
  238. // current user can authenticate
  239. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  240. wp_send_json_error( array( 'message' => __( "You don't have permission to re-authenticate MonsterInsights.", 'google-analytics-for-wordpress' ) ) );
  241. }
  242. if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) {
  243. define( 'WP_NETWORK_ADMIN', true );
  244. }
  245. // Only for Pro users, require a license key to be entered first so we can link to things.
  246. $valid = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed();
  247. if ( monsterinsights_is_pro_version() && ! $valid ) {
  248. wp_send_json_error( array( 'message' => __( "Cannot re-authenticate. Please enter a valid, active license key for MonsterInsights Pro into the settings.", 'google-analytics-for-wordpress' ) ) );
  249. }
  250. // we do have a current auth
  251. if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) {
  252. wp_send_json_error( array( 'message' => __( "Cannot re-authenticate. Please authenticate.", 'google-analytics-for-wordpress' ) ) );
  253. } else if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) {
  254. wp_send_json_error( array( 'message' => __( "Cannot re-authenticate the network. Please authenticate on the network settings panel.", 'google-analytics-for-wordpress' ) ) );
  255. }
  256. $siteurl = add_query_arg( array(
  257. 'tt' => $this->get_tt(),
  258. 'sitei' => $this->get_sitei(),
  259. 'miversion' => MONSTERINSIGHTS_VERSION,
  260. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  261. 'network' => is_network_admin() ? 'network' : 'site',
  262. 'siteurl' => is_network_admin() ? network_admin_url() : site_url(),
  263. 'key' => MonsterInsights()->auth->get_key(),
  264. 'token' => MonsterInsights()->auth->get_token(),
  265. 'return' => is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_network' ) : admin_url( 'admin.php?page=monsterinsights_settings' ),
  266. ), $this->get_route( 'https://' . monsterinsights_get_api_url() . 'auth/reauth/{type}' ) );
  267. if ( monsterinsights_is_pro_version() ) {
  268. $key = is_network_admin() ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key();
  269. $siteurl = add_query_arg( 'license', $key, $siteurl );
  270. }
  271. $siteurl = apply_filters( 'monsterinsights_maybe_authenticate_siteurl', $siteurl );
  272. wp_send_json_success( array( 'redirect' => $siteurl ) );
  273. }
  274. public function reauthenticate_listener(){
  275. // Make sure it's for us
  276. if ( empty( $_REQUEST['mi-oauth-action'] ) || $_REQUEST['mi-oauth-action'] !== 'reauth' ) {
  277. return;
  278. }
  279. // User can authenticate
  280. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  281. return;
  282. }
  283. // Invalid request
  284. if ( empty( $_REQUEST['tt'] ) || ! $this->validate_tt( $_REQUEST['tt'] ) ) {
  285. return;
  286. }
  287. // Make sure has required params
  288. if (
  289. empty( $_REQUEST['ua'] ) ||
  290. empty( $_REQUEST['miview'] ) ||
  291. empty( $_REQUEST['a'] ) ||
  292. empty( $_REQUEST['w'] ) ||
  293. empty( $_REQUEST['p'] )
  294. ) {
  295. return;
  296. }
  297. // Invalid UA code
  298. $ua = monsterinsights_is_valid_ua( $_REQUEST['ua'] );
  299. if ( empty( $ua ) ) {
  300. return;
  301. }
  302. // we do have a current auth
  303. $existing = $this->is_network_admin() ? MonsterInsights()->auth->get_network_analytics_profile() : MonsterInsights()->auth->get_analytics_profile();
  304. if ( empty( $existing['key'] ) || empty( $existing['token'] ) ) {
  305. return;
  306. }
  307. $profile = array(
  308. 'key' => $existing['key'],
  309. 'token' => $existing['token'],
  310. 'ua' => monsterinsights_is_valid_ua( $_REQUEST['ua'] ),
  311. 'viewname' => sanitize_text_field( $_REQUEST['miview'] ),
  312. 'a' => sanitize_text_field( $_REQUEST['a'] ),
  313. 'w' => sanitize_text_field( $_REQUEST['w'] ),
  314. 'p' => sanitize_text_field( $_REQUEST['p'] ),
  315. 'siteurl' => site_url(),
  316. 'neturl' => network_admin_url(),
  317. );
  318. // Rotate tt
  319. $this->rotate_tt();
  320. // Save Profile
  321. $this->is_network_admin() ? MonsterInsights()->auth->set_network_analytics_profile( $profile ) : MonsterInsights()->auth->set_analytics_profile( $profile );
  322. // Clear cache
  323. $where = $this->is_network_admin() ? 'network' : 'site';
  324. MonsterInsights()->reporting->delete_aggregate_data( $where );
  325. $url = $this->is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_network' ) : admin_url( 'admin.php?page=monsterinsights_settings' ) ;
  326. $url = add_query_arg( array(
  327. 'mi_action' => 'reauth',
  328. 'success' => 'true',
  329. ), $url );
  330. wp_safe_redirect( $url );
  331. exit;
  332. }
  333. public function maybe_verify(){
  334. // Check nonce
  335. check_ajax_referer( 'mi-admin-nonce', 'nonce' );
  336. // current user can verify
  337. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  338. wp_send_json_error( array( 'message' => __( "You don't have permission to verify MonsterInsights.", 'google-analytics-for-wordpress' ) ) );
  339. }
  340. if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) {
  341. define( 'WP_NETWORK_ADMIN', true );
  342. }
  343. // we have an auth to verify
  344. if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) {
  345. wp_send_json_error( array( 'message' => __( "Cannot verify. Please authenticate.", 'google-analytics-for-wordpress' ) ) );
  346. } else if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) {
  347. wp_send_json_error( array( 'message' => __( "Cannot verify. Please authenticate.", 'google-analytics-for-wordpress' ) ) );
  348. }
  349. $valid = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed();
  350. if ( monsterinsights_is_pro_version() && ! $valid ) {
  351. wp_send_json_error( array( 'message' => __( "Cannot verify. Please enter a valid, active license key for MonsterInsights Pro into the settings.", 'google-analytics-for-wordpress' ) ) );
  352. }
  353. $worked = $this->verify_auth();
  354. if ( $worked && ! is_wp_error( $worked ) ) {
  355. wp_send_json_success( array( 'message' => __( "Successfully verified.", 'google-analytics-for-wordpress' ) ) );
  356. } else {
  357. wp_send_json_error( array( 'message' => __( "Could not verify.", 'google-analytics-for-wordpress' ) ) );
  358. }
  359. }
  360. public function verify_auth( $credentials = array() ){
  361. $creds = ! empty( $credentials ) ? $credentials : ( $this->is_network_admin() ? MonsterInsights()->auth->get_network_analytics_profile( true ) : MonsterInsights()->auth->get_analytics_profile( true ) );
  362. if ( empty( $creds['key'] ) ) {
  363. return false;
  364. }
  365. $api = new MonsterInsights_API_Request( $this->get_route( 'auth/verify/{type}/' ), array( 'network' => $this->is_network_admin(), 'tt' => $this->get_tt(), 'key' => $creds['key'], 'token' => $creds['token'] ) );
  366. $ret = $api->request();
  367. if ( is_wp_error( $ret ) ) {
  368. return false;
  369. } else {
  370. return true;
  371. }
  372. }
  373. public function maybe_delete(){
  374. // Check nonce
  375. check_ajax_referer( 'mi-admin-nonce', 'nonce' );
  376. // current user can delete
  377. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  378. wp_send_json_error( array( 'message' => __( "You don't have permission to deauthenticate MonsterInsights.", 'google-analytics-for-wordpress' ) ) );
  379. }
  380. if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) {
  381. define( 'WP_NETWORK_ADMIN', true );
  382. }
  383. // we have an auth to delete
  384. if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) {
  385. wp_send_json_error( array( 'message' => __( "Cannot deauthenticate. You are not currently authed.", 'google-analytics-for-wordpress' ) ) );
  386. } else if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) {
  387. wp_send_json_error( array( 'message' => __( "Cannot deauthenticate. You are not currently authed.", 'google-analytics-for-wordpress' ) ) );
  388. }
  389. $valid = is_network_admin() ? MonsterInsights()->license->is_network_licensed() : MonsterInsights()->license->is_site_licensed();
  390. if ( monsterinsights_is_pro_version() && ! $valid ) {
  391. wp_send_json_error( array( 'message' => __( "Cannot deauthenticate. Please enter a valid, active license key for MonsterInsights Pro into the settings.", 'google-analytics-for-wordpress' ) ) );
  392. }
  393. $force = ! empty( $_REQUEST['forcedelete'] ) && $_REQUEST['forcedelete'] === 'true';
  394. $worked = $this->delete_auth( $force );
  395. if ( $worked && ! is_wp_error( $worked ) ) {
  396. wp_send_json_success( array( 'message' => __( "Successfully deauthenticated.", 'google-analytics-for-wordpress' ) ) );
  397. } else {
  398. if ( $force ) {
  399. wp_send_json_success( array( 'message' => __( "Successfully force deauthenticated.", 'google-analytics-for-wordpress' ) ) );
  400. } else {
  401. wp_send_json_error( array( 'message' => __( "Could not deauthenticate.", 'google-analytics-for-wordpress' ) ) );
  402. }
  403. }
  404. }
  405. public function delete_auth( $force = false ){
  406. if ( $this->is_network_admin() && ! MonsterInsights()->auth->is_network_authed() ) {
  407. return false;
  408. } else if ( ! $this->is_network_admin() && ! MonsterInsights()->auth->is_authed() ) {
  409. return false;
  410. }
  411. $creds = $this->is_network_admin() ? MonsterInsights()->auth->get_network_analytics_profile( true ) : MonsterInsights()->auth->get_analytics_profile( true );
  412. if ( empty( $creds['key'] ) ) {
  413. return false;
  414. }
  415. // If we have a new siteurl enabled option and the profile site doesn't match the current site, deactivate anyways
  416. if ( is_network_admin() ) {
  417. $siteurl = network_admin_url();
  418. if ( ! empty( $creds['neturl' ] ) && $creds['neturl'] !== $siteurl ) {
  419. MonsterInsights()->auth->delete_network_analytics_profile( true );
  420. return true;
  421. }
  422. } else {
  423. $siteurl = site_url();
  424. if ( ! empty( $creds['siteurl' ] ) && $creds['siteurl'] !== $siteurl ) {
  425. MonsterInsights()->auth->delete_analytics_profile( true );
  426. return true;
  427. }
  428. }
  429. $api = new MonsterInsights_API_Request( $this->get_route( 'auth/delete/{type}/' ), array( 'network' => $this->is_network_admin(), 'tt' => $this->get_tt(), 'key' => $creds['key'], 'token' => $creds['token'] ) );
  430. $ret = $api->request();
  431. if ( is_wp_error( $ret ) && ! $force ) {
  432. return false;
  433. } else {
  434. if ( $this->is_network_admin() ) {
  435. MonsterInsights()->auth->delete_network_analytics_profile( true );
  436. } else {
  437. MonsterInsights()->auth->delete_analytics_profile( true );
  438. }
  439. return true;
  440. }
  441. }
  442. public function get_type() {
  443. return monsterinsights_is_pro_version() ? 'pro' : 'lite';
  444. }
  445. public function get_route( $route = '' ) {
  446. $route = str_replace( '{type}', $this->get_type(), $route );
  447. $route = trailingslashit( $route );
  448. return $route;
  449. }
  450. public function is_network_admin() {
  451. return is_multisite() && is_network_admin();
  452. }
  453. public function get_sitei() {
  454. // $sitei = get_network_option( get_current_network_id(), 'monsterinsights_network_sitei', false );
  455. // if ( ! empty( $sitei ) && strlen( $sitei ) >= 1 ) {
  456. // return $sitei;
  457. // }
  458. $auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
  459. $secure_auth_key = defined( 'SECURE_AUTH_KEY' ) ? SECURE_AUTH_KEY : '';
  460. $logged_in_key = defined( 'LOGGED_IN_KEY' ) ? LOGGED_IN_KEY : '';
  461. $sitei = $auth_key . $secure_auth_key . $logged_in_key;
  462. $sitei = preg_replace('/[^a-zA-Z0-9]/', '', $sitei );
  463. $sitei = sanitize_text_field( $sitei );
  464. $sitei = trim( $sitei );
  465. $sitei = ( strlen($sitei) > 30 ) ? substr($sitei, 0, 30 ) : $sitei;
  466. return $sitei;
  467. }
  468. }