Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 73 additions & 17 deletions hm-top-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,23 @@ private function __construct() {
require_once HMTP_PLUGIN_PATH . 'hmtp.widget.php';
require_once HMTP_PLUGIN_PATH . 'hmtp.template-tags.php';

$options = get_settings_handler()->get_option( 'hmtp_setting' );
$this->token = get_settings_handler()->get_option( 'hmtp_ga_token' );
if ( is_network_activated() ) {
$ga_redirect_url = network_admin_url( 'settings.php?page=hmtp_settings_page' );
} else {
$ga_redirect_url = admin_url( 'options-general.php?page=hmtp_settings_page' );
}

$this->settings = wp_parse_args(
get_option( 'hmtp_setting', array() ),
$options,
array(
'ga_property_id' => null,
'ga_property_account_id' => null,
'ga_property_profile_id' => null,
'ga_client_id' => null,
'ga_client_secret' => null,
'ga_redirect_url' => admin_url( 'options-general.php?page=hmtp_settings_page' ),
'ga_redirect_url' => $ga_redirect_url,
'allow_opt_out' => false,
'allow_opt_out_blogs' => false,
)
Expand All @@ -108,26 +116,28 @@ private function __construct() {
$this->settings['ga_redirect_url'] = HMTP_GA_REDIRECT_URL;
}

$this->token = get_option( 'hmtp_ga_token' );

$this->ga_client = new \Google_Client();

$this->ga_client->setApplicationName( "WP Top Posts by GA" );
$this->ga_client->setApplicationName( 'WP Top Posts by GA' );

// Visit https://code.google.com/apis/console?api=analytics to generate your
// client id, client secret, and to register your redirect uri.
$this->ga_client->setClientId( $this->settings['ga_client_id'] );
$this->ga_client->setClientSecret( $this->settings['ga_client_secret'] );
$this->ga_client->setRedirectUri( $this->settings['ga_redirect_url'] );
$this->ga_client->setScopes( 'https://www.googleapis.com/auth/analytics.readonly' );
$this->ga_client->setScopes( 'https://www.googleapis.com/auth/analytics' );

if ( $this->token ) {
$this->ga_client->setAccessToken( $this->token );

// Refresh token if necessary
if ( $this->ga_client->isAccessTokenExpired() && $this->ga_client->getRefreshToken() ) {
$this->ga_client->refreshToken( $this->ga_client->getRefreshToken() );
update_option( 'hmtp_ga_token', $this->ga_client->getAccessToken() );
try {
$this->ga_client->setAccessToken( $this->token );
// Refresh token if necessary
if ( $this->ga_client->isAccessTokenExpired() && $this->ga_client->getRefreshToken() ) {
$this->ga_client->refreshToken( $this->ga_client->getRefreshToken() );
get_settings_handler()->update_option( 'hmtp_ga_token', $this->ga_client->getAccessToken() );
}
} catch ( \Exception $e ) {
update_option( 'hmtp_top_posts_error_message', $e->getMessage() );
return array();
}
}

Expand Down Expand Up @@ -181,12 +191,14 @@ public function init() {
if ( isset( $_GET['page'] ) && 'hmtp_settings_page' === $_GET['page'] && isset( $_GET['code'] ) ) {

$this->ga_client->authenticate( sanitize_text_field( $_GET['code'] ) );
update_option( 'hmtp_ga_token', $this->ga_client->getAccessToken() );

wp_safe_redirect( admin_url( 'options-general.php?page=hmtp_settings_page' ) );
get_settings_handler()->update_option( 'hmtp_ga_token', $this->ga_client->getAccessToken() );
if ( is_network_activated() ) {
wp_safe_redirect( network_admin_url( 'settings.php?page=hmtp_settings_page' ) );
} else {
wp_safe_redirect( admin_url( 'options-general.php?page=hmtp_settings_page' ) );
}
exit;
}

}

/**
Expand All @@ -201,7 +213,6 @@ public function get_results( array $args = array() ) {
}
return $this->top_posts->get_results( $args );
}

}

/**
Expand Down Expand Up @@ -239,3 +250,48 @@ function top_blogs_fetch_results( array $args = array() ) {
}
return Plugin::get_instance()->top_blogs->fetch_results( $args );
}

/**
* Gets test data.
*
* @param array $args
* @return array
*/
function test_request( array $args = array() ) {
if ( ! Plugin::get_instance()->top_posts ) {
return array();
}
return Plugin::get_instance()->top_posts->test_request( $args );

}

/**
* Detect network activated.
*
* @return bool
*/
function is_network_activated() {
// Makes sure the plugin is defined before trying to use it
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
return is_plugin_active_for_network( 'hm-top-posts/hm-top-posts.php' );
}

function get_settings_handler() {

static $hmtp_settings_handler = null;

if ( ! isset( $hmtp_settings_handler ) ) {
require_once HMTP_PLUGIN_PATH . '/hmtp.settings-handler.php';
require_once HMTP_PLUGIN_PATH . '/hmtp.local-settings-handler.php';
require_once HMTP_PLUGIN_PATH . '/hmtp.network-settings-handler.php';
if ( is_network_activated() ) {
$hmtp_settings_handler = new NetworkSettingsHandler();
} else {
$hmtp_settings_handler = new LocalSettingsHandler();
}
}

return $hmtp_settings_handler;
}
113 changes: 82 additions & 31 deletions hmtp.admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,25 @@ public function __construct( $settings = array(), \Google_Client $ga_client, \Go
add_action( 'init', array( $this, 'init' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'add_options_pages' ) );

add_action( 'network_admin_menu', array( $this, 'add_network_options_pages' ) );
add_action( 'network_admin_edit_forms_processing', array( $this, 'forms_processing' ) );
}

function add_options_pages() {
add_options_page( 'HM Top Posts', 'Top Posts', 'manage_options', 'hmtp_settings_page', array( $this, 'settings_page' ) );
if ( ! is_network_activated() ) {
add_options_page( 'HM Top Posts', 'Top Posts', 'manage_options', 'hmtp_settings_page', array( $this, 'settings_page' ) );
}
}

function add_network_options_pages() {
add_submenu_page(
'settings.php',
__( 'HM Top Posts', 'ub' ),
'Net Top Posts',
'manage_network',
'hmtp_settings_page',
array( $this, 'settings_page' )
);
}

/**
Expand All @@ -73,7 +87,11 @@ function init() {
delete_option( 'hmtp_setting' );
delete_option( 'hmtp_ga_token' );

wp_safe_redirect( admin_url( 'options-general.php?page=hmtp_settings_page' ) );
if ( is_network_activated() ) {
wp_safe_redirect( network_admin_url( 'settings.php?page=hmtp_settings_page' ) );
} else {
wp_safe_redirect( admin_url( 'options-general.php?page=hmtp_settings_page' ) );
}
exit;
}

Expand Down Expand Up @@ -151,42 +169,41 @@ function admin_init() {
* Output the plugin settings page
*/
public function settings_page() {

if ( is_network_activated() ) {
$action = 'edit.php?action=forms_processing';
} else {
$action = 'options.php';
}
?>

<div class="wrap">
<h1>HM Top Posts</h1>
<form action="options.php" method="POST">

<form action="<?php echo esc_url( $action ); ?>" method="POST">
<?php

settings_fields( 'hmtp_settings' );
do_settings_sections( 'hmtp_settings_page' );
submit_button();

?>

</form>
<?php if ( $this->ga_client->getAccessToken() ) : ?>
<form action="<?php echo esc_url( $action ); ?>" method="POST">
<input type="hidden" name="reset_token" value=" "/>

<?php
<?php
settings_fields( 'hmtp_settings' );
echo '<p><input type="submit" name="submit" id="submit" class="button button-secondary" value="Reset settings" /></p>';
?>
</form>

// Demo.
$results = get_top_posts( array( 'post_type' => 'any' ) );
<?php $results = test_request( array( 'post_type' => 'post' ) ); ?>

?>

<h2><?php esc_html_e( 'Top Posts', 'hmtp' ); ?></h2>
<h2><?php esc_html_e( 'Plugin status', 'hmtp' ); ?></h2>
<?php if ( $results['success'] ) : ?>
<?php printf( '<p>%s</p>', esc_html( $results['status_message'] ) ); ?>
<?php else : ?>
<?php printf( '<p>%s</p>', esc_html( $results['status_message'] ) ); ?>
<?php endif; ?>

<?php if ( $results ) : ?>
<ol>
<?php foreach ( $results as $post ) : ?>
<li><?php printf( '<a href="%s">%s</a> (%d)', esc_url( get_permalink( $post['post_id'] ) ), esc_html( get_the_title( $post['post_id'] ) ), absint( $post['views'] ) ); ?></li>
<?php endforeach; ?>
</ol>
<?php else : ?>
<p>No posts found</p>
<?php endif; ?>

</div>

<?php
Expand Down Expand Up @@ -267,8 +284,13 @@ public function settings_field_property_display() {

// If authenticated & api details are provided, show the property select field.
else :
try {
$props = $this->ga_service->management_webproperties->listManagementWebproperties( "~all" );
} catch ( \Exception $e ) {
update_option( 'hmtp_top_posts_error_message', $e->getMessage() );
return array();
}

$props = $this->ga_service->management_webproperties->listManagementWebproperties( "~all" );
$deauth_url = wp_nonce_url( add_query_arg( array() ), 'hmtp_deauth', 'hmtp_deauth' );

?>
Expand Down Expand Up @@ -335,19 +357,48 @@ public function settings_field_opt_out_display() { ?>
* @return bool
*/
public function settings_sanitize( $input ) {

$input['allow_opt_out'] = isset( $input['allow_opt_out'] );
$input['allow_opt_out'] = isset( $input['allow_opt_out'] ) ? (boolean) $input['allow_opt_out'] : false;

// Reset token if client ID / secret change
if ( $input['ga_client_id'] !== $this->settings['ga_client_id'] ) {
delete_option( 'hmtp_ga_token' );
get_settings_handler()->delete_option( 'hmtp_ga_token' );
}
if ( $input['ga_client_secret'] !== $this->settings['ga_client_secret'] ) {
delete_option( 'hmtp_ga_token' );
get_settings_handler()->delete_option( 'hmtp_ga_token' );
}

return $input;
// Reset token if client ID / secret change
if ( isset( $_POST['reset_token'] ) ) {
get_settings_handler()->delete_option( 'hmtp_ga_token' );
exit;
}

return $input;
}

public function forms_processing() {
// Reset token if client ID / secret change
if ( isset( $_POST['reset_token'] ) ) {
get_settings_handler()->delete_option( 'hmtp_ga_token' );
wp_safe_redirect( network_admin_url( 'settings.php?page=hmtp_settings_page' ) );
exit;
}

$options = get_settings_handler()->get_option( 'hmtp_setting' );

$to_save = array();
foreach ( $_POST['hmtp_setting'] as $key => $value ) {
if ( 'ga_access_token' === $key ) { continue; }
$to_save[ $key ] = $value ? $value : ''; // save raw code - doesn't require sanitization.
}
if ( ! array_key_exists( 'allow_opt_out', $to_save ) ) {
$to_save['allow_opt_out'] = false;
}
$options = wp_parse_args( $to_save, $options );
get_settings_handler()->update_option( 'hmtp_setting', $options );
wp_safe_redirect( network_admin_url( 'settings.php?page=hmtp_settings_page' ) );

exit;

}
}
29 changes: 29 additions & 0 deletions hmtp.local-settings-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Contains .
*
* @copyright 2016 Sigma Software
* @package UB
*
* @author Dmitriy Mamlyga(dmitriy.mamlyga@sigma.software).
*/

namespace HMTP;

require_once HMTP_PLUGIN_PATH . '/hmtp.settings-handler.php';


class LocalSettingsHandler extends SettingsHandler {

public function get_option( $option_name ) {
return get_option( $option_name );
}

public function update_option( $option_name, $option_value ) {
update_option( $option_name, $option_value );
}

public function delete_option( $option_name ) {
delete_option( $option_name );
}
}
35 changes: 35 additions & 0 deletions hmtp.network-settings-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Contains .
*
* @copyright 2016 Sigma Software
* @package UB
*
* @author Dmitriy Mamlyga(dmitriy.mamlyga@sigma.software).
*/

namespace HMTP;

require_once HMTP_PLUGIN_PATH . '/hmtp.settings-handler.php';


class NetworkSettingsHandler extends SettingsHandler {

private $network_id = '';

public function __construct() {
$this->network_id = get_current_network_id();
}

public function get_option( $option_name ) {
return get_network_option( $this->network_id, $option_name );
}

public function update_option( $option_name, $option_value ) {
update_network_option( $this->network_id, $option_name, $option_value );
}

public function delete_option( $option_name ) {
delete_network_option( $this->network_id, $option_name );
}
}
Loading