| 1 |
<?php |
| 2 |
/** |
| 3 |
* Analytify settings file. |
| 4 |
* @package WP_Analytify |
| 5 |
*/ |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Base Class to use for the Add-ons |
| 12 |
* It will be used to extend the functionality of Analytify WordPress Plugin. |
| 13 |
*/ |
| 14 |
|
| 15 |
// Setting Global Values. |
| 16 |
define( 'ANALYTIFY_LIB_PATH', dirname( __FILE__ ) . '/lib/' ); |
| 17 |
define( 'ANALYTIFY_ID', 'wp-analytify-options' ); |
| 18 |
define( 'ANALYTIFY_NICK', 'Analytify' ); |
| 19 |
define( 'ANALYTIFY_ROOT_PATH', dirname( __FILE__ ) ); |
| 20 |
define( 'ANALYTIFY_VERSION', '1.3.1' ); |
| 21 |
define( 'ANALYTIFY_TYPE', 'FREE' ); |
| 22 |
define( 'ANALYTIFY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
| 23 |
define( 'ANALYTIFY_CLIENTID', '958799092305-7p6jlsnmv1dn44a03ma00kmdrau2i31q.apps.googleusercontent.com' ); |
| 24 |
define( 'ANALYTIFY_CLIENTSECRET', 'Mzs1ODgJTpjk8mzQ3mbrypD3' ); |
| 25 |
define( 'ANALYTIFY_REDIRECT', 'https://wp-analytify.com/api/' ); |
| 26 |
define( 'ANALYTIFY_SCOPE', 'https://www.googleapis.com/auth/analytics.readonly' ); // Readonly scope. |
| 27 |
define( 'ANALYTIFY_STORE_URL', 'http://wp-analytify.com' ); |
| 28 |
define( 'ANALYTIFY_PRODUCT_NAME', 'Analytify WordPress Plugin' ); |
| 29 |
|
| 30 |
if ( ! class_exists( 'Analytify_General_FREE' ) ) { |
| 31 |
|
| 32 |
/** |
| 33 |
* Analytify_General_FREE Class for Analytify Free version |
| 34 |
*/ |
| 35 |
class Analytify_General_FREE { |
| 36 |
|
| 37 |
/** |
| 38 |
* __construct |
| 39 |
*/ |
| 40 |
function __construct() { |
| 41 |
|
| 42 |
if ( ! class_exists( 'Analytify_Google_Client' ) ) { |
| 43 |
|
| 44 |
require_once ANALYTIFY_LIB_PATH . 'Google/Client.php'; |
| 45 |
require_once ANALYTIFY_LIB_PATH . 'Google/Service/Analytics.php'; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
$this->client = new Analytify_Google_Client(); |
| 50 |
$this->client->setApprovalPrompt( 'force' ); |
| 51 |
$this->client->setAccessType( 'offline' ); |
| 52 |
|
| 53 |
if ( 'Yes' === get_option( 'ANALYTIFY_USER_KEYS' ) ) { |
| 54 |
|
| 55 |
$this->client->setClientId( get_option( 'ANALYTIFY_CLIENTID' ) ); |
| 56 |
$this->client->setClientSecret( get_option( 'ANALYTIFY_CLIENTSECRET' ) ); |
| 57 |
$this->client->setRedirectUri( get_option( 'ANALYTIFY_REDIRECT_URI' ) ); |
| 58 |
|
| 59 |
} else { |
| 60 |
|
| 61 |
$this->client->setClientId( ANALYTIFY_CLIENTID ); |
| 62 |
$this->client->setClientSecret( ANALYTIFY_CLIENTSECRET ); |
| 63 |
$this->client->setRedirectUri( ANALYTIFY_REDIRECT ); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
$this->client->setScopes( ANALYTIFY_SCOPE ); |
| 68 |
|
| 69 |
try { |
| 70 |
|
| 71 |
$this->service = new Analytify_Google_Service_Analytics( $this->client ); |
| 72 |
$this->pa_connect(); |
| 73 |
|
| 74 |
} catch ( Analytify_Google_Service_Exception $e ) { |
| 75 |
|
| 76 |
// Show error message only for logged in users. |
| 77 |
// Show error message only for logged in users. |
| 78 |
if ( current_user_can( 'manage_options' ) ) { |
| 79 |
|
| 80 |
echo sprintf( esc_html__( '%1$s oOps, Something went wrong. %2$s %5$s %2$s %3$s Don\'t worry, This error message is only visible to Administrators. %4$s %2$s ', 'wp-analytify' ), '<br /><br />', '<br />', '<i>', '</i>', esc_html( $e->getMessage() ) ); |
| 81 |
} |
| 82 |
} catch ( Analytify_Google_Auth_Exception $e ) { |
| 83 |
|
| 84 |
// Show error message only for logged in users. |
| 85 |
if ( current_user_can( 'manage_options' ) ) { |
| 86 |
|
| 87 |
echo sprintf( esc_html__( '%1$s oOps, Try to %2$s Reset %3$s Authentication. %4$s %7$s %4$s %5$s Don\'t worry, This error message is only visible to Administrators. %6$s %4$s', 'wp-analytify' ), '<br /><br />', '<a href="'. esc_url( admin_url( 'admin.php?page=analytify-settings&tab=authentication' ) ) . '" title="Reset">', '</a>', '<br />', '<i>', '</i>', esc_html( $e->getMessage() ) ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Connect with Google Analytics API and get authentication token and save it. |
| 95 |
*/ |
| 96 |
public function pa_connect() { |
| 97 |
|
| 98 |
$ga_google_authtoken = get_option( 'pa_google_token' ); |
| 99 |
|
| 100 |
if ( ! empty( $ga_google_authtoken ) ) { |
| 101 |
|
| 102 |
$this->client->setAccessToken( $ga_google_authtoken ); |
| 103 |
} else { |
| 104 |
|
| 105 |
$auth_code = get_option( 'post_analytics_token' ); |
| 106 |
|
| 107 |
if ( empty( $auth_code ) ) { return false; } |
| 108 |
|
| 109 |
try { |
| 110 |
|
| 111 |
$access_token = $this->client->authenticate( $auth_code ); |
| 112 |
} catch ( Exception $e ) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
if ( $access_token ) { |
| 117 |
|
| 118 |
$this->client->setAccessToken( $access_token ); |
| 119 |
|
| 120 |
update_option( 'pa_google_token', $access_token ); |
| 121 |
|
| 122 |
return true; |
| 123 |
} else { |
| 124 |
|
| 125 |
return false; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
$this->token = json_decode( $this->client->getAccessToken() ); |
| 130 |
|
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* [pa_get_analytics This function is used to fetch analytics from GA.] |
| 136 |
* @param string $metrics pass a list of metrics in string format. |
| 137 |
* @param date $start_date start date from. |
| 138 |
* @param date $end_date end date to. |
| 139 |
* @param boolean $dimensions dimensions. |
| 140 |
* @param boolean $sort sort. |
| 141 |
* @param boolean $filter filter. |
| 142 |
* @param boolean $limit limit pagination. |
| 143 |
* @return string results |
| 144 |
*/ |
| 145 |
public function pa_get_analytics( $metrics, $start_date, $end_date, $dimensions = false, $sort = false, $filter = false, $limit = false ) { |
| 146 |
|
| 147 |
try { |
| 148 |
|
| 149 |
$this->service = new Analytify_Google_Service_Analytics( $this->client ); |
| 150 |
$params = array(); |
| 151 |
|
| 152 |
if ( $dimensions ) { |
| 153 |
$params['dimensions'] = $dimensions; |
| 154 |
} |
| 155 |
|
| 156 |
if ( $sort ) { |
| 157 |
$params['sort'] = $sort; |
| 158 |
} |
| 159 |
|
| 160 |
if ( $filter ) { |
| 161 |
$params['filters'] = $filter; |
| 162 |
} |
| 163 |
|
| 164 |
if ( $limit ) { |
| 165 |
$params['max-results'] = $limit; |
| 166 |
} |
| 167 |
|
| 168 |
$profile_id = get_option( 'pt_webprofile' ); |
| 169 |
|
| 170 |
if ( ! $profile_id ) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
return $this->service->data_ga->get( 'ga:' . $profile_id, $start_date, $end_date, $metrics, $params ); |
| 175 |
} catch ( Analytify_Google_Service_Exception $e ) { |
| 176 |
|
| 177 |
// Show error message only for logged in users. |
| 178 |
if ( is_user_logged_in() ) { echo esc_html( $e->getMessage() ); } |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* [pa_get_analytics_dashboard This function is used to fetch analytics from GA to display in WordPress dashboard.] |
| 184 |
* @param string $metrics pass a list of metrics in string format. |
| 185 |
* @param date $start_date start date from. |
| 186 |
* @param date $end_date end date to. |
| 187 |
* @param boolean $dimensions dimensions. |
| 188 |
* @param boolean $sort sort. |
| 189 |
* @param boolean $filter filter. |
| 190 |
* @param boolean $limit limit pagination. |
| 191 |
* @return string results |
| 192 |
*/ |
| 193 |
public function pa_get_analytics_dashboard( $metrics, $start_date, $end_date, $dimensions = false, $sort = false, $filter = false, $limit = false ) { |
| 194 |
|
| 195 |
try { |
| 196 |
|
| 197 |
$this->service = new Analytify_Google_Service_Analytics( $this->client ); |
| 198 |
$params = array(); |
| 199 |
|
| 200 |
if ( $dimensions ) { |
| 201 |
$params['dimensions'] = $dimensions; |
| 202 |
} |
| 203 |
if ( $sort ) { |
| 204 |
$params['sort'] = $sort; |
| 205 |
} |
| 206 |
if ( $filter ) { |
| 207 |
$params['filters'] = $filter; |
| 208 |
} |
| 209 |
if ( $limit ) { |
| 210 |
$params['max-results'] = $limit; |
| 211 |
} |
| 212 |
|
| 213 |
$profile_id = get_option( 'pt_webprofile_dashboard' ); |
| 214 |
if ( ! $profile_id ) { |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
return $this->service->data_ga->get( 'ga:' . $profile_id, $start_date, $end_date, $metrics, $params ); |
| 219 |
|
| 220 |
} catch ( Analytify_Google_Service_Exception $e ) { |
| 221 |
|
| 222 |
// Show error message only for logged in users. |
| 223 |
if ( is_user_logged_in() ) { echo esc_html( $e->getMessage() ); } |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
?> |
| 229 |
|