auth.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Auth class.
  4. *
  5. * Helper for auth.
  6. *
  7. * @since 7.0.0
  8. *
  9. * @package MonsterInsights
  10. * @subpackage Auth
  11. * @author Chris Christoff
  12. */
  13. // Exit if accessed directly
  14. if ( ! defined( 'ABSPATH' ) ) {
  15. exit;
  16. }
  17. final class MonsterInsights_Auth {
  18. private $profile = array();
  19. private $network = array();
  20. /**
  21. * Primary class constructor.
  22. *
  23. * @access public
  24. * @since 7.0.0
  25. */
  26. public function __construct() {
  27. $this->profile = $this->get_analytics_profile();
  28. $this->network = $this->get_network_analytics_profile();
  29. }
  30. public function is_manual() {
  31. return ! empty( $this->profile['manual'] );
  32. }
  33. public function is_network_manual() {
  34. return ! empty( $this->network['manual'] );
  35. }
  36. public function is_authed() {
  37. return ! empty( $this->profile['key'] );
  38. }
  39. public function is_network_authed() {
  40. return ! empty( $this->network['key'] );
  41. }
  42. public function get_analytics_profile( $force = false ) {
  43. if ( ! empty( $this->profile ) && ! $force ) {
  44. return $this->profile;
  45. } else {
  46. $profile = get_option( 'monsterinsights_site_profile', array() );
  47. $this->profile = $profile;
  48. return $profile;
  49. }
  50. }
  51. public function get_network_analytics_profile( $force = false ) {
  52. if ( ! empty( $this->network ) && ! $force ) {
  53. return $this->network;
  54. } else {
  55. $profile = get_site_option( 'monsterinsights_network_profile', array() );
  56. $this->network = $profile;
  57. return $profile;
  58. }
  59. }
  60. public function set_analytics_profile( $data = array() ){
  61. update_option( 'monsterinsights_site_profile', $data );
  62. $this->profile = $data;
  63. }
  64. public function set_network_analytics_profile( $data = array() ){
  65. update_site_option( 'monsterinsights_network_profile', $data );
  66. $this->network = $data;
  67. }
  68. public function delete_analytics_profile( $migrate = true ){
  69. if ( $migrate ) {
  70. $newdata = array();
  71. if ( isset( $this->profile['ua'] ) ) {
  72. $newdata['manual'] = $this->profile['ua'];
  73. }
  74. $this->profile = $newdata;
  75. $this->set_analytics_profile( $newdata );
  76. } else {
  77. $this->profile = array();
  78. delete_option( 'monsterinsights_site_profile' );
  79. }
  80. }
  81. public function delete_network_analytics_profile( $migrate = true ){
  82. if ( $migrate ) {
  83. $newdata = array();
  84. if ( isset( $this->network['ua'] ) ) {
  85. $newdata['manual'] = $this->network['ua'];
  86. }
  87. $this->network = $newdata;
  88. $this->set_network_analytics_profile( $newdata );
  89. } else {
  90. $this->network = array();
  91. delete_site_option( 'monsterinsights_network_profile' );
  92. }
  93. }
  94. public function set_manual_ua( $ua = '' ) {
  95. if ( empty( $ua ) ) {
  96. return;
  97. }
  98. if ( $this->is_authed() ) {
  99. MonsterInsights()->api_auth->delete_auth();
  100. }
  101. $data = array();
  102. if ( empty( $this->profile ) ) {
  103. $data['manual'] = $ua;
  104. } else {
  105. $data = $this->profile;
  106. $data['manual'] = $ua;
  107. }
  108. do_action( 'monsterinsights_reports_delete_aggregate_data' );
  109. $this->profile = $data;
  110. $this->set_analytics_profile( $data );
  111. }
  112. public function set_network_manual_ua( $ua = '' ) {
  113. if ( empty( $ua ) ) {
  114. return;
  115. }
  116. if ( $this->is_network_authed() ) {
  117. MonsterInsights()->api_auth->delete_auth();
  118. }
  119. $data = array();
  120. if ( empty( $this->network ) ) {
  121. $data['manual'] = $ua;
  122. } else {
  123. $data = $this->network;
  124. $data['manual'] = $ua;
  125. }
  126. do_action( 'monsterinsights_reports_delete_network_aggregate_data' );
  127. $this->network = $data;
  128. $this->set_network_analytics_profile( $data );
  129. }
  130. public function delete_manual_ua() {
  131. if ( ! empty( $this->profile ) && ! empty( $this->profile['manual'] ) ) {
  132. unset( $this->profile['manual'] );
  133. $this->set_analytics_profile( $this->profile );
  134. }
  135. }
  136. public function delete_network_manual_ua() {
  137. if ( ! empty( $this->network ) && ! empty( $this->network['manual'] ) ) {
  138. unset( $this->network['manual'] );
  139. $this->set_network_analytics_profile( $this->network );
  140. }
  141. }
  142. public function get_manual_ua() {
  143. return ! empty( $this->profile['manual'] ) ? monsterinsights_is_valid_ua( $this->profile['manual'] ) : '';
  144. }
  145. public function get_network_manual_ua() {
  146. return ! empty( $this->network['manual'] ) ? monsterinsights_is_valid_ua( $this->network['manual'] ) : '';
  147. }
  148. public function get_ua() {
  149. return ! empty( $this->profile['ua'] ) ? monsterinsights_is_valid_ua( $this->profile['ua'] ) : '';
  150. }
  151. public function get_network_ua() {
  152. return ! empty( $this->network['ua'] ) ? monsterinsights_is_valid_ua( $this->network['ua'] ) : '';
  153. }
  154. public function get_viewname(){
  155. return ! empty( $this->profile['viewname'] ) ? $this->profile['viewname'] : '';
  156. }
  157. public function get_network_viewname(){
  158. return ! empty( $this->network['viewname'] ) ? $this->network['viewname'] : '';
  159. }
  160. public function get_accountid(){
  161. return ! empty( $this->profile['a'] ) ? $this->profile['a'] : '';
  162. }
  163. public function get_network_accountid(){
  164. return ! empty( $this->network['a'] ) ? $this->network['a'] : '';
  165. }
  166. public function get_propertyid(){
  167. return ! empty( $this->profile['w'] ) ? $this->profile['w'] : '';
  168. }
  169. public function get_network_propertyid(){
  170. return ! empty( $this->network['w'] ) ? $this->network['w'] : '';
  171. }
  172. public function get_viewid(){ // also known as profileID
  173. return ! empty( $this->profile['p'] ) ? $this->profile['p'] : '';
  174. }
  175. public function get_network_viewid(){ // also known as profileID
  176. return ! empty( $this->network['p'] ) ? $this->network['p'] : '';
  177. }
  178. public function get_key(){
  179. return ! empty( $this->profile['key'] ) ? $this->profile['key'] : '';
  180. }
  181. public function get_network_key(){
  182. return ! empty( $this->network['key'] ) ? $this->network['key'] : '';
  183. }
  184. public function get_token(){
  185. return ! empty( $this->profile['token'] ) ? $this->profile['token'] : '';
  186. }
  187. public function get_network_token(){
  188. return ! empty( $this->network['token'] ) ? $this->network['token'] : '';
  189. }
  190. public function get_referral_url(){
  191. $url = '';
  192. if ( $this->is_authed() ) {
  193. $url .= 'a' . MonsterInsights()->auth->get_accountid() . 'w' . MonsterInsights()->auth->get_propertyid() . 'p' . MonsterInsights()->auth->get_viewid() . '/';
  194. } else if ( $this->is_network_authed() ) {
  195. $url .= 'a' . MonsterInsights()->auth->get_network_accountid() . 'w' . MonsterInsights()->auth->get_network_propertyid() . 'p' . MonsterInsights()->auth->get_network_viewid() . '/';
  196. }
  197. return $url;
  198. }
  199. }