Skip to content

Commit

Permalink
Merge branch 'trunk'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joost de Valk committed Dec 18, 2014
2 parents d5c11e3 + 7838893 commit beeaf74
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 67 deletions.
4 changes: 3 additions & 1 deletion admin/class-admin-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class Yoast_GA_Admin_Assets {
* Add the scripts to the admin head
*/
public static function enqueue_scripts() {
wp_enqueue_script( 'yoast_ga_admin', self::get_asset_path( 'assets/js/yoast_ga_admin' ) . self::file_ext( '.js' ), array( 'jquery' ), GAWP_VERSION );
wp_enqueue_script( 'yoast_focusable', self::get_asset_path( 'assets/dependencies/focusable/focus-element-overlay.min.js' ), array( 'jquery' ), false );

wp_enqueue_script( 'yoast_ga_admin', self::get_asset_path( 'assets/js/yoast_ga_admin' ) . self::file_ext( '.js' ), array( 'jquery', 'yoast_focusable' ), GAWP_VERSION );

// Enqueue the qtip js file
wp_enqueue_script( 'jquery-qtip', self::get_asset_path( 'assets/dependencies/qtip/jquery.qtip.min.js' ) , array( 'jquery' ), '1.0.0-RC3', true );
Expand Down
47 changes: 19 additions & 28 deletions admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@ public function init_settings() {
// Listener for reconnecting with google analytics
$this->google_analytics_listener();

if ( is_null( $this->get_tracking_code() ) ) {
if ( is_null( $this->get_tracking_code() ) && $this->show_admin_warning() ) {
add_action( 'admin_notices', array( $this, 'config_warning' ) );
}

$last_run = get_transient( 'yst_ga_last_wp_run' );
$last_run = get_option( 'yst_ga_last_wp_run' );
if ( $last_run === false || Yoast_GA_Utils::hours_between( strtotime( $last_run ), time() ) >= 48 ) {
// Show error, something went wrong
if ( ! is_null( $this->get_tracking_code() && current_user_can( 'manage_options' ) ) ) {
if ( ! isset( $_GET['page'] ) || ( isset( $_GET['page'] ) && $_GET['page'] !== 'yst_ga_settings' ) ) {
add_action( 'admin_notices', array( $this, 'warning_fetching_data' ) );
}
if ( ! is_null( $this->get_tracking_code() ) && empty( $this->options['manual_ua_code_field'] ) && $this->show_admin_warning() ) {
add_action( 'admin_notices', array( $this, 'warning_fetching_data' ) );
}
}

Expand Down Expand Up @@ -161,6 +159,21 @@ public static function ga_deactivation_hook() {

}

/**
* Are we allowed to show an warning message? returns true if it's allowed
*
* @return bool
*/
private function show_admin_warning() {
if ( current_user_can( 'manage_options' ) ) {
if ( ! isset( $_GET['page'] ) || ( isset( $_GET['page'] ) && $_GET['page'] !== 'yst_ga_settings' ) ) {
return true;
}
}

return false;
}

/**
* Transform the Profile ID into an helpful UA code
*
Expand Down Expand Up @@ -239,27 +252,6 @@ public function translate_promo() {
return $yoast_ga_i18n;
}

/**
* Checks whether we'll ever be able to reach Google.
*
* @return bool
*/
public function check_google_access() {
$can_access_google = true;
if ( defined( 'WP_HTTP_BLOCK_EXTERNAL' ) && WP_HTTP_BLOCK_EXTERNAL ) {
$can_access_google = false;
if ( defined( 'WP_ACCESSIBLE_HOSTS' ) ) {
// Better to use the internal WP logic from this point forward.
$wp_http = new WP_Http();
if ( $wp_http->block_request( 'https://www.googleapis.com/analytics/v3/management/accountSummaries' ) === false ) {
$can_access_google = true;
}
}
}

return $can_access_google;
}

/**
* Load the page of a menu item in the GA plugin
*/
Expand Down Expand Up @@ -305,7 +297,6 @@ public function get_profiles() {
return $return;
}


/**
* Checks if there is a callback or reauth to get token from Google Analytics api
*/
Expand Down
48 changes: 48 additions & 0 deletions admin/class-google-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,31 @@ public function get_options() {
return get_option( $this->option_name );
}

/**
* Checks whether we'll ever be able to reach Google.
*
* @return bool
*/
public function check_google_access_from_wp() {
$can_access_google = true;
if ( defined( 'WP_HTTP_BLOCK_EXTERNAL' ) && WP_HTTP_BLOCK_EXTERNAL ) {
$can_access_google = false;
if ( defined( 'WP_ACCESSIBLE_HOSTS' ) ) {
// Better to use the internal WP logic from this point forward.
$can_access_google = $this->test_connection_to_google();
}
}

return $can_access_google;
}

/**
* Check if we can access Google Apis from this server by making a dummy connection
*/
public function check_google_access() {
return $this->test_connection_to_google();
}

/**
* Updating the options based on $this->option_name and the internal property $this->options
*/
Expand All @@ -152,6 +177,15 @@ protected function set_client() {
$this->client = new Yoast_Google_Analytics_Client( $config );
}

/**
* Gets an authentication URL
*
* @return mixed
*/
public function create_auth_url() {
return $this->client->createAuthUrl();
}

/**
* Saving profile response in options
*
Expand All @@ -163,6 +197,20 @@ protected function save_profile_response( $accounts ) {
$this->update_options();
}

/**
* Test a connection to Google
*
* @return bool
*/
private function test_connection_to_google(){
$wp_http = new WP_Http();
if ( $wp_http->block_request( 'https://www.googleapis.com/analytics/v3/management/accountSummaries' ) === false ) {
return true;
}

return false;
}

/**
* Format the accounts request
*
Expand Down
4 changes: 2 additions & 2 deletions admin/dashboards/class-admin-dashboards-collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function check_api_call_hook( ) {
* @return datetime
*/
private function get_last_aggregate_run() {
return get_transient( 'yst_ga_last_wp_run' );
return get_option( 'yst_ga_last_wp_run' );
}

/**
Expand Down Expand Up @@ -399,7 +399,7 @@ private function handle_response( $response, $metric, $dimensions, $start_date,
* Success, set a transient which stores the latest runtime
*/
if ( ! empty( $response ) ) {
set_transient( 'yst_ga_last_wp_run', date( 'Y-m-d' ), 48 * HOUR_IN_SECONDS );
update_option( 'yst_ga_last_wp_run', date( 'Y-m-d' ) );
}

return Yoast_GA_Dashboards_Data::set( $name, $response, strtotime( $start_date ), strtotime( $end_date ), $store_as );
Expand Down
17 changes: 5 additions & 12 deletions admin/dashboards/class-admin-dashboards-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@

class Yoast_GA_Dashboards_Data {

/**
* The time to store a transient (in seconds)
*
* @var int
*/
private static $store_transient_time = DAY_IN_SECONDS;

/**
* Get a data object
*
Expand All @@ -27,16 +20,16 @@ class Yoast_GA_Dashboards_Data {
* @return array
*/
public static function get( $type ) {
$transient = get_transient( 'yst_ga_' . $type );
$option = get_option( 'yst_ga_' . $type );

if ( false === $transient ) {
// Transient does not exist, abort
if ( false === $option ) {
// Option does not exist, abort
return array();
}

// @TODO loop through transient to get the correct date range

return $transient;
return $option;
}

/**
Expand All @@ -59,7 +52,7 @@ public static function set( $type, $value, $start_date, $end_date, $store_as ) {
'value' => $value,
);

return set_transient( 'yst_ga_' . $type, $store, self::$store_transient_time );
return update_option( 'yst_ga_' . $type, $store );
}
}

Expand Down
2 changes: 1 addition & 1 deletion admin/license-manager
36 changes: 22 additions & 14 deletions admin/pages/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@
echo '<h2>' . __( 'General settings', 'google-analytics-for-wordpress' ) . '</h2>';

echo '<div id="ga-promote">';
if ( $yoast_ga_admin->check_google_access() ) {

$ga_class = Yoast_Google_Analytics::get_instance();
$wp_block_google = $ga_class->check_google_access_from_wp();
$check_google_access = $ga_class->check_google_access();

if ( $wp_block_google && $check_google_access ) {

$profiles = Yoast_GA_Admin_Form::parse_optgroups( $yoast_ga_admin->get_profiles() );
$ga_url = $_SERVER['PHP_SELF'];
if ( isset( $_GET['page'] ) ) {
$ga_url .= '?page=' . $_GET['page'];
}
$ga_url .= '&reauth=true';

$auth_url = Yoast_Google_Analytics::get_instance()->create_auth_url();
add_thickbox();
echo '<script>yst_thickbox_heading = "' . __( 'Paste your Google authentication code', 'google-analytics-for-wordpress' ) . '";</script>';

echo "<div id='google_ua_code_field'>";
if ( count( $profiles ) == 0 ) {
echo '<div class="ga-form ga-form-input">';
echo '<label class="ga-form ga-form-text-label ga-form-label-left" id="yoast-ga-form-label-text-ga-authwithgoogle" />' . __( 'Google profile', 'google-analytics-for-wordpress' ) . ':</label>';
echo '<a id="yst_ga_authenticate" class="button" href="' . $ga_url . '" target="_blank">' . __( 'Authenticate with your Google account', 'google-analytics-for-wordpress' ) . '</a>';
echo '<a id="yst_ga_authenticate" class="button" onclick="yst_popupwindow(\'' . $auth_url . '\',500,500);">' . __( 'Authenticate with your Google account', 'google-analytics-for-wordpress' ) . '</a>';
echo '</div>';
echo '<div class="ga-form ga-form-input">';
echo '<label class="ga-form ga-form-text-label ga-form-label-left" id="yoast-ga-form-label-text-ga-authwithgoogle" />' . __( 'Current UA-profile', 'google-analytics-for-wordpress' ) . '</label>';
Expand All @@ -52,20 +56,24 @@

echo '<div class="ga-form ga-form-input">';
echo '<label class="ga-form ga-form-text-label ga-form-label-left" id="yoast-ga-form-label-text-ga-authwithgoogle" />&nbsp;</label>';
echo '<a id="yst_ga_authenticate" class="button" href="' . $ga_url . '" target="_blank">' . __( 'Re-authenticate with your Google account', 'google-analytics-for-wordpress' ) . '</a>';
echo '<a id="yst_ga_authenticate" class="button" onclick="yst_popupwindow(\'' . $auth_url . '\',500,500);">' . __( 'Re-authenticate with your Google account', 'google-analytics-for-wordpress' ) . '</a>';
echo '</div>';
}
echo '</div>';

echo '<div id="oauth_code" class="ga-form ga-form-input">';
echo '<label class="ga-form ga-form-text-label ga-form-label-left" id="yoast-ga-form-label-text-ga-authwithgoogle" />' . __( 'Paste your Google code here', 'google-analytics-for-wordpress' ) . ':</label>';
echo Yoast_GA_Admin_Form::input( 'text', null, 'google_auth_code', null, __('Please leave this field empty if everything is all right for you', 'google-analytics-for-wordpress') );
echo '<label class="ga-form ga-form-text-label ga-form-label-left" id="yoast-ga-form-label-text-ga-authwithgoogle" />' . __( 'Paste your Google code and press return', 'google-analytics-for-wordpress' ) . ':</label>';
echo Yoast_GA_Admin_Form::input( 'text', null, 'google_auth_code', null, null );
echo '</label>';

echo '</div>';
} else {
echo '<h3>' . __( 'Cannot connect to Google', 'google-analytics-for-wordpress' ) . '</h3>';
echo '<p>' . __( 'Your server is blocking requests to Google, to fix this, add <code>*.googleapis.com</code> to the <code>WP_ACCESSIBLE_HOSTS</code> constant in your <em>wp-config.php</em> or ask your webhost to do this.', 'google-analytics-for-wordpress' ) . '</p>';
if ( $wp_block_google == false && $check_google_access == false ) {
echo '<p>' . __( 'Your server is blocking requests to Google, to fix this, add <code>*.googleapis.com</code> to the <code>WP_ACCESSIBLE_HOSTS</code> constant in your <em>wp-config.php</em> or ask your webhost to do this.', 'google-analytics-for-wordpress' ) . '</p>';
} else {
echo '<p>' . __( 'Your firewall or webhost is blocking requests to Google, please ask your webhost company to fix this.', 'google-analytics-for-wordpress' ) . '</p>';
}
echo '<p>' . __( 'Until this is fixed, you can only use the manual authentication method and cannot use the dashboards feature.', 'google-analytics-for-wordpress' ) . '</p>';
}

Expand All @@ -74,7 +82,7 @@
echo '</label>';
echo '<div id="enter_ua">';
echo Yoast_GA_Admin_Form::input( 'text', null, 'manual_ua_code_field' );
echo '<p><strong>' . __('Warning: If you use a manual UA code, you won\'t be able to use the dashboards.', 'google-analytics-for-wordpress') . '</strong></p>';
echo '<p><strong>' . __( 'Warning: If you use a manual UA code, you won\'t be able to use the dashboards.', 'google-analytics-for-wordpress' ) . '</strong></p>';
echo '</div>';
echo '<div class="clear"></div></div>';
?>
Expand All @@ -89,7 +97,7 @@
<div id="universal" class="gatab">
<?php
echo '<h2>' . __( 'Universal settings', 'google-analytics-for-wordpress' ) . '</h2>';
echo Yoast_GA_Admin_Form::input( 'checkbox', __( 'Enable Universal tracking', 'google-analytics-for-wordpress' ), 'enable_universal', null, sprintf( __( 'First enable Universal tracking in your Google Analytics account. How to do that, please read %1$sthis guide%2$s to learn how to do that.', 'google-analytics-for-wordpress' ), '<a href="http://kb.yoast.com/article/125-universal-analytics#utm_medium=kb-link&utm_source=gawp-config&utm_campaign=wpgaplugin" target="_blank">', '</a>' ) );
echo Yoast_GA_Admin_Form::input( 'checkbox', __( 'Enable Universal tracking', 'google-analytics-for-wordpress' ), 'enable_universal', null, sprintf( __( 'First enable Universal tracking in your Google Analytics account. How to do that, please read %1$sthis guide%2$s to learn how to do that.', 'google-analytics-for-wordpress' ), '<a href="http://kb.yoast.com/article/125-universal-analytics#utm_medium=kb-link&utm_source=gawp-config&utm_campaign=wpgaplugin" target="_blank">', '</a>' ) );
echo Yoast_GA_Admin_Form::input( 'checkbox', __( 'Enable Demographics and Interest Reports', 'google-analytics-for-wordpress' ), 'demographics', null, sprintf( __( 'You have to enable the Demographics in Google Analytics before you can see the tracking data. We have a doc in our %1$sknowlegde base%2$s about this feature.', 'google-analytics-for-wordpress' ), '<a href="http://kb.yoast.com/article/154-enable-demographics-and-interests-report-in-google-analytics/#utm_medium=kb-link&utm_source=gawp-config&utm_campaign=wpgaplugin" target="_blank">', '</a>' ) );
?>
</div>
Expand Down Expand Up @@ -139,7 +147,7 @@
jQuery(document).ready(
function () {
jQuery('#yoast-ga-form-select-settings-analytics_profile').chosen({
group_search : true
group_search: true
});
jQuery('#yoast-ga-form-select-settings-ignore_users').chosen({placeholder_text_multiple: '<?php echo __( 'Select the users to ignore', 'google-analytics-for-wordpress' ); ?>'});
}
Expand Down
4 changes: 4 additions & 0 deletions assets/css/yoast_ga_styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ p.ga-topdescription {
width: 325px;
}

#yoast-ga-form-settings #oauth_code {
padding: 20px;
}

#yoast-ga-form-settings #oauth_code div {
margin: 0 0 0 5px;
}
Expand Down
2 changes: 1 addition & 1 deletion assets/css/yoast_ga_styles.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit beeaf74

Please sign in to comment.