first commit
This commit is contained in:
+412
@@ -0,0 +1,412 @@
|
||||
<?php
|
||||
/**
|
||||
* The admin-specific functionality of the module.
|
||||
*
|
||||
* @link https://codesupply.co
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package Powerkit
|
||||
* @subpackage Modules/Admin
|
||||
*/
|
||||
|
||||
/**
|
||||
* The admin-specific functionality of the module.
|
||||
*/
|
||||
class Powerkit_Post_Views_Admin extends Powerkit_Module_Admin {
|
||||
|
||||
/**
|
||||
* Initialize
|
||||
*/
|
||||
public function initialize() {
|
||||
|
||||
add_filter( 'init', function() {
|
||||
$post_types = get_post_types( array(
|
||||
'publicly_queryable' => 1,
|
||||
'_builtin' => false,
|
||||
) );
|
||||
|
||||
// Merge post types.
|
||||
$post_types = array_merge( array(
|
||||
'post' => 'post',
|
||||
), $post_types );
|
||||
|
||||
foreach ( $post_types as $post_type ) {
|
||||
add_filter( "manage_{$post_type}_posts_columns", array( $this, 'column_views' ) );
|
||||
add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'custom_column_views' ), 6, 2 );
|
||||
}
|
||||
} );
|
||||
|
||||
add_action( 'admin_menu', array( $this, 'register_options_page' ) );
|
||||
add_action( 'admin_head', array( $this, 'column_style' ) );
|
||||
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register admin page
|
||||
*/
|
||||
public function register_options_page() {
|
||||
add_options_page( esc_html__( 'Post Views', 'powerkit' ), esc_html__( 'Post Views', 'powerkit' ), 'manage_options', powerkit_get_page_slug( $this->slug ), array( $this, 'build_options_page' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build admin page
|
||||
*/
|
||||
public function build_options_page() {
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( esc_html__( 'You do not have sufficient rights to view this page.', 'powerkit' ) );
|
||||
}
|
||||
|
||||
$this->process_options_page();
|
||||
|
||||
$options = powerkit_post_views_options();
|
||||
?>
|
||||
<div class="wrap pk-wrap">
|
||||
<h1><?php esc_html_e( 'Post Views Settings', 'powerkit' ); ?></h1>
|
||||
|
||||
<div class="pk-settings">
|
||||
<form method="post" action="<?php echo esc_url( powerkit_get_page_url( $this->slug ) ); ?>">
|
||||
<table class="form-table">
|
||||
<tbody>
|
||||
<?php
|
||||
if ( empty( $options['token'] ) ) {
|
||||
|
||||
if ( empty( $options['clientid'] ) || empty( $options['psecret'] ) ) {
|
||||
?>
|
||||
|
||||
<p><?php echo wp_kses( __( 'In order to connect to your Google Analytics Account, you need to create a new project in the <a href="https://console.developers.google.com/project" target="_blank">Google API Console</a> and activate the Analytics API in "APIs & Services"', 'powerkit' ), 'post' ); ?></p>
|
||||
|
||||
<ol>
|
||||
<li><?php echo wp_kses( __( 'Go to "APIs & Services".', 'powerkit' ), 'post' ); ?></li>
|
||||
<li><?php echo wp_kses( __( 'On the tab "OAuth consent screen" register the application, select "User Type" (External) and click "Create".', 'powerkit' ), 'post' ); ?></li>
|
||||
<li><?php echo wp_kses( __( 'On the tab "OAuth consent screen" enter "Application name" and add your domain.', 'powerkit' ), 'post' ); ?></li>
|
||||
<li><?php echo wp_kses( sprintf( __( 'Then, create an OAuth Client ID in "APIs & Services > Credentials" (Select "Web application", enter this URL %s for the "Authorized redirect URIs" field). ', 'powerkit' ), '<code>' . powerkit_get_page_url( $this->slug ) . '</code>' ), 'post' ); ?></li>
|
||||
<li><?php echo wp_kses( __( 'Enter your access below and connect to Google Analytics (if you have received a notice - "This app is not verified", then you can continue by clicking on "Advanced" link and follow the instructions).', 'powerkit' ), 'post' ); ?></li>
|
||||
</ol>
|
||||
|
||||
<!-- Client ID -->
|
||||
<tr>
|
||||
<th scope="row"><label for="powerkit_post_views_clientid"><?php esc_html_e( 'Client ID', 'powerkit' ); ?></label></th>
|
||||
<td><input class="regular-text" id="powerkit_post_views_clientid" name="powerkit_post_views_clientid" type="text" value="<?php echo esc_attr( $options['clientid'] ); ?>"></td>
|
||||
</tr>
|
||||
<!-- Client secret -->
|
||||
<tr>
|
||||
<th scope="row"><label for="powerkit_post_views_psecret"><?php esc_html_e( 'Client secret', 'powerkit' ); ?></label></th>
|
||||
<td><input class="regular-text" id="powerkit_post_views_psecret" name="powerkit_post_views_psecret" type="text" value="<?php echo esc_attr( $options['psecret'] ); ?>"></td>
|
||||
</tr>
|
||||
<?php
|
||||
} else {
|
||||
$googleapis_auth = add_query_arg( array(
|
||||
'client_id' => $options['clientid'],
|
||||
'redirect_uri' => wp_nonce_url( powerkit_get_page_url( $this->slug ) ),
|
||||
'scope' => 'https://www.googleapis.com/auth/analytics.readonly+https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile&response_type=code&access_type=offline&state=init&approval_prompt=force',
|
||||
), 'https://accounts.google.com/o/oauth2/auth' );
|
||||
?>
|
||||
<tr>
|
||||
<td colspan="2">
|
||||
<p><a class="button" href="<?php echo esc_url( $googleapis_auth ); ?>"><?php esc_html_e( 'Connect to Google Analytics', 'powerkit' ); ?> »</a></p>
|
||||
|
||||
<p><a class="button" href="<?php echo esc_url( wp_nonce_url( powerkit_get_page_url( $this->slug . '&state=clear-api' ) ) ); ?>"><?php esc_html_e( 'Clear the API keys', 'powerkit' ); ?> »</a></p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr><td colspan="2"><hr></td></tr>
|
||||
<?php
|
||||
}
|
||||
} else {
|
||||
?>
|
||||
<p><?php esc_html_e( 'You are connected to Google Analytics with the e-mail address', 'powerkit' ); ?> <?php echo esc_html( $options['gmail'] ); ?></p>
|
||||
|
||||
<p><a href="<?php echo esc_url( wp_nonce_url( powerkit_get_page_url( $this->slug . '&state=disconnect' ) ) ); ?>"><?php esc_html_e( 'Disconnect from Google Analytics', 'powerkit' ); ?> »</a></p>
|
||||
|
||||
<p><a href="<?php echo esc_url( wp_nonce_url( powerkit_get_page_url( $this->slug . '&state=reset-cache' ) ) ); ?>"><?php esc_html_e( 'Empty page views cache', 'powerkit' ); ?> »</a></p>
|
||||
|
||||
<!-- Use this website to retrieve pageviews numbers -->
|
||||
<tr>
|
||||
<th scope="row"><label for="powerkit_post_views_wid"><?php esc_html_e( 'Use this website to retrieve pageviews numbers', 'powerkit' ); ?></label></th>
|
||||
<td>
|
||||
<select id="powerkit_post_views_wid" name="powerkit_post_views_wid">
|
||||
<option value="" <?php selected( empty( $options['wid'] ), true ); ?>><?php esc_html_e( 'None', 'powerkit' ); ?></option>
|
||||
|
||||
<?php
|
||||
$wjson = powerkit_post_views_api_call( 'https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties/~all/profiles', array() );
|
||||
|
||||
if ( is_array( $wjson->items ) ) {
|
||||
|
||||
foreach ( $wjson->items as $item ) {
|
||||
|
||||
if ( 'WEB' !== $item->type ) {
|
||||
continue;
|
||||
}
|
||||
?>
|
||||
<option value="<?php echo esc_attr( $item->id ); ?>" <?php selected( $options['wid'], $item->id ); ?>>
|
||||
<?php echo esc_html( $item->name .' (' . $item->websiteUrl . ')' ); ?>
|
||||
</option>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
<!-- Metrics to retrieve -->
|
||||
<tr>
|
||||
<th scope="row"><label for="powerkit_post_views_metric"><?php esc_html_e( 'Metrics to retrieve', 'powerkit' ); ?></label></th>
|
||||
<td>
|
||||
<select id="powerkit_post_views_metric" name="powerkit_post_views_metric">
|
||||
<option value="ga:pageviews" <?php selected( $options['metric'], 'ga:pageviews' ); ?>>
|
||||
<?php esc_html_e( 'Page views', 'powerkit' ); ?>
|
||||
</option>
|
||||
<option value="ga:uniquePageviews" <?php selected( $options['metric'], 'ga:uniquePageviews' ); ?>>
|
||||
<?php esc_html_e( 'Unique page views', 'powerkit' ); ?>
|
||||
</option>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
<!-- Start date for the analytics -->
|
||||
<tr>
|
||||
<th scope="row"><label for="powerkit_post_views_startdate"><?php esc_html_e( 'Start date for the analytics', 'powerkit' ); ?></label></th>
|
||||
<td><input class="regular-text" id="powerkit_post_views_startdate" name="powerkit_post_views_startdate" type="date" value="<?php echo esc_attr( $options['startdate'] ); ?>"></td>
|
||||
</tr>
|
||||
<!-- Default value when a count cannot be fetched -->
|
||||
<tr>
|
||||
<th scope="row"><label for="powerkit_post_views_defaultval"><?php esc_html_e( 'Default value when a count cannot be fetched', 'powerkit' ); ?></label></th>
|
||||
<td><input class="regular-text" id="powerkit_post_views_defaultval" name="powerkit_post_views_defaultval" type="text" value="<?php echo esc_attr( $options['defaultval'] ); ?>"></td>
|
||||
</tr>
|
||||
<!-- Display the Views column in Posts list -->
|
||||
<tr>
|
||||
<th scope="row"><label for="powerkit_post_views_column"><?php esc_html_e( 'Display the Views column in Posts list', 'powerkit' ); ?></label></th>
|
||||
<td><input class="regular-text" id="powerkit_post_views_column" name="powerkit_post_views_column" type="checkbox" value="true" <?php checked( (bool) $options['column'] ); ?>></td>
|
||||
</tr>
|
||||
<!-- Search pageviews slugs with trailing slash -->
|
||||
<tr>
|
||||
<th scope="row"><label for="powerkit_post_views_trailing"><?php esc_html_e( 'Search pageviews slugs with trailing slash', 'powerkit' ); ?></label></th>
|
||||
<td><input class="regular-text" id="powerkit_post_views_trailing" name="powerkit_post_views_trailing" type="checkbox" value="true" <?php checked( (bool) $options['trailing'] ); ?>></td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<?php wp_nonce_field(); ?>
|
||||
|
||||
<p class="submit"><input class="button button-primary" name="save_settings" type="submit" value="<?php esc_html_e( 'Save changes', 'powerkit' ); ?>" /></p>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Process options page
|
||||
*/
|
||||
public function process_options_page() {
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$options = powerkit_post_views_options();
|
||||
|
||||
/** Save settings */
|
||||
/** ------------------- */
|
||||
|
||||
if ( isset( $_POST['save_settings'] ) ) { // Input var ok; sanitization ok.
|
||||
|
||||
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
|
||||
return;
|
||||
}
|
||||
|
||||
if ( isset( $_POST['powerkit_post_views_clientid'] ) ) { // Input var ok; sanitization ok.
|
||||
$options['clientid'] = sanitize_text_field( $_POST['powerkit_post_views_clientid'] ); // Input var ok; sanitization ok.
|
||||
}
|
||||
|
||||
if ( isset( $_POST['powerkit_post_views_psecret'] ) ) { // Input var ok; sanitization ok.
|
||||
$options['psecret'] = sanitize_text_field( $_POST['powerkit_post_views_psecret'] ); // Input var ok; sanitization ok.
|
||||
}
|
||||
|
||||
if ( isset( $_POST['powerkit_post_views_wid'] ) ) { // Input var ok; sanitization ok.
|
||||
$options['wid'] = sanitize_text_field( $_POST['powerkit_post_views_wid'] ); // Input var ok; sanitization ok.
|
||||
}
|
||||
|
||||
if ( isset( $_POST['powerkit_post_views_startdate'] ) ) { // Input var ok; sanitization ok.
|
||||
$options['startdate'] = sanitize_text_field( $_POST['powerkit_post_views_startdate'] ); // Input var ok; sanitization ok.
|
||||
}
|
||||
|
||||
if ( isset( $_POST['powerkit_post_views_defaultval'] ) ) { // Input var ok; sanitization ok.
|
||||
$options['defaultval'] = sanitize_text_field( $_POST['powerkit_post_views_defaultval'] ); // Input var ok; sanitization ok.
|
||||
}
|
||||
|
||||
if ( isset( $_POST['powerkit_post_views_metric'] ) ) { // Input var ok; sanitization ok.
|
||||
$options['metric'] = sanitize_text_field( $_POST['powerkit_post_views_metric'] ); // Input var ok; sanitization ok.
|
||||
}
|
||||
|
||||
$options['column'] = ( isset( $_POST['powerkit_post_views_column'] ) ); // Input var ok; sanitization ok.
|
||||
$options['trailing'] = ( isset( $_POST['powerkit_post_views_trailing'] ) ); // Input var ok; sanitization ok.
|
||||
|
||||
do_action( 'powerkit_post_views_save_options', $options );
|
||||
|
||||
update_option( 'powerkit_post_views_options', $options );
|
||||
|
||||
printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Settings saved.', 'powerkit' ) );
|
||||
}
|
||||
|
||||
/** Actions */
|
||||
/** ------------------- */
|
||||
|
||||
if ( isset( $_GET['state'] ) && 'init' === $_GET['state'] ) { // Input var ok; sanitization ok.
|
||||
|
||||
$request = new WP_Http();
|
||||
|
||||
$result = $request->request( 'https://accounts.google.com/o/oauth2/token', array(
|
||||
'method' => 'POST',
|
||||
'body' => array(
|
||||
'code' => sanitize_text_field( isset( $_GET['code'] ) ? $_GET['code'] : null ), // Input var ok; sanitization ok.
|
||||
'client_id' => $options['clientid'],
|
||||
'client_secret' => $options['psecret'],
|
||||
'redirect_uri' => powerkit_get_page_url( $this->slug ),
|
||||
'grant_type' => 'authorization_code',
|
||||
),
|
||||
) );
|
||||
|
||||
if ( ! is_array( $result ) || ! isset( $result['response']['code'] ) && 200 !== $result['response']['code'] ) {
|
||||
?>
|
||||
<div id="message" class="error">
|
||||
<p>
|
||||
<?php esc_html_e( 'There was something wrong with Google!', 'powerkit' ); ?>
|
||||
</p>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
$tjson = json_decode( $result['body'] );
|
||||
|
||||
$options['token'] = $tjson->access_token;
|
||||
$options['token_refresh'] = $tjson->refresh_token;
|
||||
$options['expires'] = time() + $tjson->expires_in;
|
||||
|
||||
update_option( 'powerkit_post_views_options', $options );
|
||||
|
||||
$ijson = powerkit_post_views_api_call( 'https://www.googleapis.com/oauth2/v1/userinfo', array() );
|
||||
|
||||
$options['gid'] = $ijson->id;
|
||||
$options['gmail'] = $ijson->email;
|
||||
|
||||
update_option( 'powerkit_post_views_options', $options );
|
||||
|
||||
if ( ! empty( $options['token'] ) && ! empty( $options['gmail'] ) ) {
|
||||
?>
|
||||
<script>window.location = '<?php echo esc_url( powerkit_get_page_url( $this->slug ) ); ?> ';</script>
|
||||
<?php
|
||||
exit;
|
||||
}
|
||||
} elseif ( isset( $_GET['state'] ) && 'disconnect' === $_GET['state'] ) { // Input var ok; sanitization ok.
|
||||
|
||||
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'] ) ) { // Input var ok; sanitization ok.
|
||||
return;
|
||||
}
|
||||
|
||||
$options['error'] = null;
|
||||
$options['gid'] = null;
|
||||
$options['gmail'] = null;
|
||||
$options['token'] = null;
|
||||
$options['token_refresh'] = null;
|
||||
$options['expires'] = null;
|
||||
$options['defaultval'] = 0;
|
||||
|
||||
update_option( 'powerkit_post_views_options', $options );
|
||||
|
||||
} elseif ( isset( $_GET['state'] ) && 'clear-api' === $_GET['state'] ) { // Input var ok; sanitization ok.
|
||||
|
||||
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'] ) ) { // Input var ok; sanitization ok.
|
||||
return;
|
||||
}
|
||||
|
||||
$options['error'] = null;
|
||||
$options['clientid'] = null;
|
||||
$options['psecret'] = null;
|
||||
|
||||
update_option( 'powerkit_post_views_options', $options );
|
||||
|
||||
printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'API Keys removed.', 'powerkit' ) );
|
||||
|
||||
} elseif ( isset( $_GET['state'] ) && 'refresh-token' === $_GET['state'] ) { // Input var ok; sanitization ok.
|
||||
|
||||
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'] ) ) { // Input var ok; sanitization ok.
|
||||
return;
|
||||
}
|
||||
|
||||
powerkit_post_views_refresh_token();
|
||||
|
||||
printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Token refreshed successfully.', 'powerkit' ) );
|
||||
|
||||
} elseif ( isset( $_GET['state'] ) && 'reset-cache' === $_GET['state'] ) { // Input var ok; sanitization ok.
|
||||
|
||||
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'] ) ) { // Input var ok; sanitization ok.
|
||||
return;
|
||||
}
|
||||
|
||||
$wpdb->query( "UPDATE {$wpdb->prefix}pk_post_views SET period = 0" ); // db call ok; no-cache ok.
|
||||
|
||||
printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Cache flushed successfully.', 'powerkit' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the columns displayed in the Posts list table.
|
||||
*
|
||||
* @param array $post_columns An associative array of column headings.
|
||||
*/
|
||||
public function column_views( $post_columns ) {
|
||||
|
||||
$options = powerkit_post_views_options();
|
||||
|
||||
if ( ! empty( $options['token'] ) && $options['column'] ) {
|
||||
|
||||
$post_columns['pk_post_views'] = esc_html__( 'Views', 'powerkit' );
|
||||
|
||||
}
|
||||
|
||||
return $post_columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires in each custom column in the Posts list table.
|
||||
*
|
||||
* @param string $column_name The name of the column to display.
|
||||
* @param int $post_id The current post ID.
|
||||
*/
|
||||
public function custom_column_views( $column_name, $post_id ) {
|
||||
|
||||
if ( 'pk_post_views' === $column_name ) {
|
||||
|
||||
echo powerkit_get_post_views( $post_id, true ); // XSS.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add column style.
|
||||
*/
|
||||
public function column_style() {
|
||||
echo '<style>.column-pk_post_views { width: 120px; }</style>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Output notice.
|
||||
*/
|
||||
public function admin_notice() {
|
||||
|
||||
$options = powerkit_post_views_options();
|
||||
|
||||
if ( current_user_can( 'manage_options' ) ) {
|
||||
|
||||
if ( isset( $options['token'] ) && empty( $options['token'] ) ) {
|
||||
|
||||
echo '<div class="error"><p>' . esc_html__( 'Google Post Views Warning: You have to (re)connect the plugin to your Google account.' ) . '<br><a href="' . esc_url( powerkit_get_page_url( $this->slug ) ) . '">' . esc_html__( 'Update settings', 'powerkit' ) . ' →</a></p></div>';
|
||||
|
||||
} elseif ( isset( $options['error'] ) && ! empty( $options['error'] ) ) {
|
||||
|
||||
echo '<div class="error"><p>' . esc_html__( 'Google Post Views Error: ', 'powerkit' ) . wp_kses( $options['error'], 'post' ) . '<br><a href="' . esc_url( powerkit_get_page_url( $this->slug ) ) . '">' . esc_html__( 'Update settings', 'powerkit' ) . ' →</a></p></div>';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* Post Views
|
||||
*
|
||||
* @package Powerkit
|
||||
* @subpackage Modules
|
||||
*/
|
||||
|
||||
if ( class_exists( 'Powerkit_Module' ) ) {
|
||||
/**
|
||||
* Init module
|
||||
*/
|
||||
class Powerkit_Post_Views extends Powerkit_Module {
|
||||
|
||||
/**
|
||||
* Register module
|
||||
*/
|
||||
public function register() {
|
||||
$this->name = esc_html__( 'Post Views', 'powerkit' );
|
||||
$this->desc = esc_html__( 'This module links to your Google Analytics account to retrieve the pageviews for your posts.', 'powerkit' );
|
||||
$this->slug = 'post_views';
|
||||
$this->type = 'default';
|
||||
$this->category = 'tools';
|
||||
$this->priority = 140;
|
||||
$this->public = true;
|
||||
$this->enabled = true;
|
||||
|
||||
$this->initialize_database();
|
||||
|
||||
// Check Post Views Counter.
|
||||
if ( $this->post_views_counter() ) {
|
||||
|
||||
// Making the module inactive.
|
||||
add_filter( 'powerkit_module_enabled', function( $status, $slug ) {
|
||||
|
||||
if ( 'post_views' === $slug ) {
|
||||
$status = false;
|
||||
}
|
||||
|
||||
return $status;
|
||||
}, 10, 2 );
|
||||
|
||||
// Set message.
|
||||
ob_start();
|
||||
?>
|
||||
<div class="update-message notice inline notice-warning notice-alt">
|
||||
<?php esc_html_e( 'Please deactivate the Post Views Counter plugin.', 'powerkit' ); ?>
|
||||
</div>
|
||||
<?php
|
||||
$this->desc .= ob_get_clean();
|
||||
} else {
|
||||
|
||||
$this->links = array(
|
||||
array(
|
||||
'name' => esc_html__( 'Go to settings', 'powerkit' ),
|
||||
'url' => powerkit_get_page_url( $this->slug ),
|
||||
),
|
||||
array(
|
||||
'name' => esc_html__( 'View documentation', 'powerkit' ),
|
||||
'url' => powerkit_get_setting( 'documentation' ) . '/post-views/',
|
||||
'target' => '_blank',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check post views plugin.
|
||||
*/
|
||||
public function post_views_counter() {
|
||||
return class_exists( 'Post_Views_Counter' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize database
|
||||
*/
|
||||
public function initialize_database() {
|
||||
require_once dirname( __FILE__ ) . '/helpers/db-powerkit-post-views.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize module
|
||||
*/
|
||||
public function initialize() {
|
||||
if ( $this->post_views_counter() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Load the required dependencies for this module */
|
||||
|
||||
// Helpers Functions for the module.
|
||||
require_once dirname( __FILE__ ) . '/helpers/helper-powerkit-post-views.php';
|
||||
require_once dirname( __FILE__ ) . '/helpers/query-powerkit-post-views.php';
|
||||
|
||||
// Admin and public area.
|
||||
require_once dirname( __FILE__ ) . '/admin/class-powerkit-post-views-admin.php';
|
||||
require_once dirname( __FILE__ ) . '/public/class-powerkit-post-views-public.php';
|
||||
|
||||
new Powerkit_Post_Views_Admin( $this->slug );
|
||||
new Powerkit_Post_Views_Public( $this->slug );
|
||||
}
|
||||
}
|
||||
|
||||
new Powerkit_Post_Views();
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
/**
|
||||
* Post Views DB
|
||||
*
|
||||
* @package Powerkit
|
||||
* @subpackage Modules/DB
|
||||
*/
|
||||
|
||||
class Powerkit_Post_Views_DB {
|
||||
|
||||
/**
|
||||
* Construct.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'powerkit_post_views_save_options', array( $this, 'multisite_activation' ) );
|
||||
add_action( 'powerkit_plugin_activation', array( $this, 'multisite_activation' ) );
|
||||
add_action( 'powerkit_plugin_deactivation', array( $this, 'multisite_deactivation' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Multisite activation.
|
||||
*
|
||||
* @global object $wpdb
|
||||
* @param bool $networkwide The networkwide.
|
||||
*/
|
||||
public function multisite_activation( $networkwide ) {
|
||||
if ( is_multisite() && $networkwide ) {
|
||||
global $wpdb;
|
||||
|
||||
$activated_blogs = array();
|
||||
$current_blog_id = $wpdb->blogid;
|
||||
$blogs_ids = $wpdb->get_col( $wpdb->prepare( 'SELECT blog_id FROM ' . $wpdb->blogs, '' ) );
|
||||
|
||||
foreach ( $blogs_ids as $blog_id ) {
|
||||
switch_to_blog( $blog_id );
|
||||
$this->activate_single();
|
||||
$activated_blogs[] = (int) $blog_id;
|
||||
}
|
||||
|
||||
switch_to_blog( $current_blog_id );
|
||||
|
||||
update_site_option( 'pk_post_views_activated_blogs', $activated_blogs, array() );
|
||||
} else {
|
||||
$this->activate_single();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Single site activation.
|
||||
*
|
||||
* @global array $wp_roles
|
||||
*/
|
||||
public function activate_single() {
|
||||
global $wpdb;
|
||||
|
||||
$table_name = $wpdb->prefix . 'pk_post_views';
|
||||
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
// Required for dbdelta.
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
|
||||
// Create post views table.
|
||||
maybe_create_table( $table_name, 'CREATE TABLE ' . $table_name . ' ( id bigint unsigned NOT NULL, type tinyint(1) unsigned NOT NULL, period varchar(20) NOT NULL, count bigint unsigned NOT NULL, PRIMARY KEY (id)) ' . $charset_collate . ';' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Multisite deactivation.
|
||||
*
|
||||
* @global array $wpdb
|
||||
* @param bool $networkwide The networkwide.
|
||||
*/
|
||||
public function multisite_deactivation( $networkwide ) {
|
||||
if ( is_multisite() && $networkwide ) {
|
||||
global $wpdb;
|
||||
|
||||
$current_blog_id = $wpdb->blogid;
|
||||
$blogs_ids = $wpdb->get_col( $wpdb->prepare( 'SELECT blog_id FROM ' . $wpdb->blogs, '' ) );
|
||||
|
||||
if ( ! ( $activated_blogs = get_site_option( 'pk_post_views_activated_blogs', false, false ) ) ) {
|
||||
$activated_blogs = array();
|
||||
}
|
||||
|
||||
foreach ( $blogs_ids as $blog_id ) {
|
||||
switch_to_blog( $blog_id );
|
||||
$this->deactivate_single( true );
|
||||
|
||||
if ( in_array( (int) $blog_id, $activated_blogs, true ) ) {
|
||||
unset( $activated_blogs[ array_search( $blog_id, $activated_blogs ) ] );
|
||||
}
|
||||
}
|
||||
|
||||
switch_to_blog( $current_blog_id );
|
||||
update_site_option( 'post_views_counter_activated_blogs', $activated_blogs );
|
||||
} else {
|
||||
$this->deactivate_single();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Single site deactivation.
|
||||
*
|
||||
* @param bool $multi The multi.
|
||||
*/
|
||||
public function deactivate_single( $multi = false ) {
|
||||
global $wpdb;
|
||||
|
||||
// Delete table from database.
|
||||
$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'pk_post_views' );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
new Powerkit_Post_Views_DB();
|
||||
+379
@@ -0,0 +1,379 @@
|
||||
<?php
|
||||
/**
|
||||
* Helpers Post Views
|
||||
*
|
||||
* @package Powerkit
|
||||
* @subpackage Modules/Helper
|
||||
*/
|
||||
|
||||
/**
|
||||
* Google Analytics API Call
|
||||
*
|
||||
* @param string $url The url.
|
||||
* @param array $params The params.
|
||||
* @param bool $url_encode The url encode.
|
||||
*/
|
||||
function powerkit_post_views_api_call( $url, $params = array(), $url_encode = true ) {
|
||||
|
||||
$options = powerkit_post_views_options();
|
||||
|
||||
if ( time() >= $options['expires'] ) {
|
||||
|
||||
$options = powerkit_post_views_refresh_token();
|
||||
|
||||
}
|
||||
|
||||
$qs = '?access_token=' . rawurlencode( $options['token'] );
|
||||
|
||||
foreach ( $params as $k => $v ) {
|
||||
|
||||
$qs .= '&' . $k . '=' . ( $url_encode ? rawurlencode( $v ) : $v );
|
||||
|
||||
}
|
||||
|
||||
$request = new WP_Http();
|
||||
|
||||
$result = $request->request( $url . $qs );
|
||||
|
||||
$json = new stdClass();
|
||||
|
||||
$options['error'] = null;
|
||||
|
||||
if ( is_array( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) {
|
||||
|
||||
$json = json_decode( $result['body'] );
|
||||
|
||||
update_option( 'powerkit_post_views_options', $options );
|
||||
|
||||
return $json;
|
||||
|
||||
} else {
|
||||
|
||||
if ( is_array( $result ) && isset( $result['response']['code'] ) && 403 === $result['response']['code'] ) {
|
||||
|
||||
$json = json_decode( $result['body'], true );
|
||||
|
||||
$options['error'] = $json['error']['errors'][0]['message'];
|
||||
|
||||
update_option( 'powerkit_post_views_options', $options );
|
||||
|
||||
} else {
|
||||
$options['error'] = esc_html__( 'Failed to get data of Google Analytics.', 'powerkit' );
|
||||
|
||||
update_option( 'powerkit_post_views_options', $options );
|
||||
}
|
||||
|
||||
return new stdClass();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Google Analytics refresh token.
|
||||
*/
|
||||
function powerkit_post_views_refresh_token() {
|
||||
|
||||
$options = powerkit_post_views_options();
|
||||
|
||||
/* If the token has expired, we create it again */
|
||||
if ( ! empty( $options['token_refresh'] ) ) {
|
||||
|
||||
$request = new WP_Http();
|
||||
|
||||
$result = $request->request(
|
||||
'https://accounts.google.com/o/oauth2/token', array(
|
||||
'method' => 'POST',
|
||||
'body' => array(
|
||||
'client_id' => $options['clientid'],
|
||||
'client_secret' => $options['psecret'],
|
||||
'refresh_token' => $options['token_refresh'],
|
||||
'grant_type' => 'refresh_token',
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$options['error'] = null;
|
||||
|
||||
if ( is_array( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) {
|
||||
|
||||
$tjson = json_decode( $result['body'] );
|
||||
|
||||
$request = new WP_Http();
|
||||
|
||||
$result = $request->request( 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' . rawurlencode( $tjson->access_token ) );
|
||||
|
||||
if ( is_array( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) {
|
||||
|
||||
$ijson = json_decode( $result['body'] );
|
||||
|
||||
$options['token'] = $tjson->access_token;
|
||||
|
||||
if ( isset( $tjson->refresh_token ) && ! empty( $tjson->refresh_token ) ) {
|
||||
$options['token_refresh'] = $tjson->refresh_token;
|
||||
}
|
||||
|
||||
$options['expires'] = time() + $tjson->expires_in;
|
||||
$options['gid'] = $ijson->id;
|
||||
|
||||
} elseif ( is_array( $result ) && isset( $result['response']['code'] ) && 403 === $result['response']['code'] ) {
|
||||
|
||||
$json = json_decode( $result['body'], true );
|
||||
|
||||
$options['error'] = $json['error']['errors'][0]['message'];
|
||||
} else {
|
||||
$options['error'] = esc_html__( 'Failed to update token of Google Analytics.', 'powerkit' );
|
||||
}
|
||||
} else {
|
||||
$options['error'] = esc_html__( 'Failed to refresh token of Google Analytics.', 'powerkit' );
|
||||
}
|
||||
|
||||
update_option( 'powerkit_post_views_options', $options );
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get post views settings.
|
||||
*/
|
||||
function powerkit_post_views_options() {
|
||||
|
||||
$options = get_option( 'powerkit_post_views_options', array() );
|
||||
|
||||
$options['clientid'] = isset( $options['clientid'] ) ? $options['clientid'] : null;
|
||||
$options['psecret'] = isset( $options['psecret'] ) ? $options['psecret'] : null;
|
||||
$options['gid'] = isset( $options['gid'] ) ? $options['gid'] : null;
|
||||
$options['gmail'] = isset( $options['gmail'] ) ? $options['gmail'] : null;
|
||||
$options['token'] = isset( $options['token'] ) ? $options['token'] : null;
|
||||
$options['defaultval'] = isset( $options['defaultval'] ) ? $options['defaultval'] : 0;
|
||||
$options['token_refresh'] = isset( $options['token_refresh'] ) ? $options['token_refresh'] : null;
|
||||
$options['expires'] = isset( $options['expires'] ) ? $options['expires'] : null;
|
||||
$options['wid'] = isset( $options['wid'] ) ? $options['wid'] : null;
|
||||
$options['column'] = isset( $options['column'] ) ? $options['column'] : false;
|
||||
$options['trailing'] = isset( $options['trailing'] ) ? $options['trailing'] : false;
|
||||
$options['metric'] = isset( $options['metric'] ) ? $options['metric'] : 'ga:pageviews';
|
||||
|
||||
if ( ! isset( $options['startdate'] ) ) {
|
||||
$options['startdate'] = date( 'Y-m-d', strtotime( '-1 year' ) );
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache time
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string|int $post_id Post ID.
|
||||
*/
|
||||
function powerkit_post_views_get_cache_time( $post_id = false ) {
|
||||
|
||||
// Post Id.
|
||||
if ( ! $post_id ) {
|
||||
$post_id = get_the_ID();
|
||||
}
|
||||
|
||||
// Post age in seconds.
|
||||
$post_age = floor( intval( date( 'U' ) ) - intval( get_post_time( 'U', true, $post_id ) ) );
|
||||
|
||||
$two_months_period = apply_filters( 'powerkit_post_views_two_months', 5184000 );
|
||||
$three_weeks_period = apply_filters( 'powerkit_post_views_three_weeks', 1814400 );
|
||||
|
||||
if ( isset( $post_age ) && $post_age > $two_months_period ) {
|
||||
|
||||
// Post older than 60 days - expire cache after 12 hours.
|
||||
$seconds = apply_filters( 'powerkit_post_views_refresh_60_days', 43200 );
|
||||
|
||||
} elseif ( isset( $post_age ) && $post_age > $three_weeks_period ) {
|
||||
|
||||
// Post older than 21 days - expire cache after 4 hours.
|
||||
$seconds = apply_filters( 'powerkit_post_views_refresh_21_days', 14400 );
|
||||
|
||||
} else {
|
||||
|
||||
// Expire cache after one hour.
|
||||
$seconds = apply_filters( 'powerkit_post_views_refresh_1_hour', 3600 );
|
||||
}
|
||||
|
||||
return $seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get no-cached post views
|
||||
*
|
||||
* @param int $post_id The post id.
|
||||
*/
|
||||
function powerkit_get_nocached_post_views( $post_id ) {
|
||||
if ( empty( $post_id ) ) {
|
||||
$post_id = get_the_ID();
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$query = 'SELECT * FROM ' . $wpdb->prefix . 'pk_post_views WHERE id = ' . $post_id . ' AND type = 1';
|
||||
|
||||
// Get cached data.
|
||||
$post_views = wp_cache_get( md5( $query ), 'pk-get-no-post-views' );
|
||||
|
||||
// Cached data not found?
|
||||
if ( false === $post_views ) {
|
||||
$post_row = $wpdb->get_row( $query );
|
||||
|
||||
if ( $post_row ) {
|
||||
$post_views = (float) $post_row->count;
|
||||
|
||||
// Set the cache expiration, 5 minutes by default.
|
||||
$expire = absint( apply_filters( 'pk_object_cache_expire', 5 * 60, 'post-views' ) );
|
||||
|
||||
wp_cache_add( md5( $query ), $post_views, 'pk-get-no-post-views', $expire );
|
||||
}
|
||||
}
|
||||
|
||||
return $post_views;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached post views
|
||||
*
|
||||
* @param int $post_id The post id.
|
||||
*/
|
||||
function powerkit_get_cached_post_views( $post_id ) {
|
||||
if ( empty( $post_id ) ) {
|
||||
$post_id = get_the_ID();
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$query = 'SELECT * FROM ' . $wpdb->prefix . 'pk_post_views WHERE id = ' . $post_id . ' AND type = 1';
|
||||
|
||||
// Get cached data.
|
||||
$post_views = wp_cache_get( md5( $query ), 'pk-get-post-views' );
|
||||
|
||||
// Cached data not found?
|
||||
if ( false === $post_views ) {
|
||||
$post_row = $wpdb->get_row( $query );
|
||||
|
||||
if ( $post_row && ( intval( date( 'U' ) ) < intval( $post_row->period ) ) ) {
|
||||
$post_views = (float) $post_row->count;
|
||||
|
||||
// Set the cache expiration, 5 minutes by default.
|
||||
$expire = absint( apply_filters( 'pk_object_cache_expire', 5 * 60, 'post-views' ) );
|
||||
|
||||
wp_cache_add( md5( $query ), $post_views, 'pk-get-post-views', $expire );
|
||||
}
|
||||
}
|
||||
|
||||
return $post_views;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cached post views
|
||||
*
|
||||
* @param int $post_id The post id.
|
||||
* @param int $count The count.
|
||||
*/
|
||||
function powerkit_set_cached_post_views( $post_id, $count ) {
|
||||
global $wpdb;
|
||||
|
||||
$period = powerkit_post_views_get_cache_time( $post_id ) + intval( date( 'U' ) );
|
||||
|
||||
$count = (float) $count;
|
||||
|
||||
return $wpdb->query(
|
||||
$wpdb->prepare(
|
||||
'INSERT INTO ' . $wpdb->prefix . 'pk_post_views (id, type, period, count)
|
||||
VALUES (%1$d, %2$d, %3$s, %4$d)
|
||||
ON DUPLICATE KEY UPDATE period = %3$s, count = %4$d', $post_id, 1, $period, $count
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get post views
|
||||
*
|
||||
* @param int $post_id The post id.
|
||||
* @param array $format The format.
|
||||
* @param bool $cached The cached.
|
||||
*/
|
||||
function powerkit_get_post_views( $post_id = null, $format = true, $cached = true ) {
|
||||
|
||||
if ( ! $post_id ) {
|
||||
$post_id = get_the_ID();
|
||||
}
|
||||
|
||||
if ( ! $post_id ) {
|
||||
return apply_filters( 'powerkit_get_post_views', $options['defaultval'], $post_id, $format );
|
||||
}
|
||||
|
||||
$options = powerkit_post_views_options();
|
||||
|
||||
if ( empty( $options['token'] ) ) {
|
||||
return apply_filters( 'powerkit_get_post_views', $options['defaultval'], $post_id, $format );
|
||||
}
|
||||
|
||||
// Set start date.
|
||||
$start_date = $options['startdate'];
|
||||
|
||||
// Get permalink.
|
||||
$basename = basename( get_permalink( $post_id ) );
|
||||
|
||||
if ( $options['trailing'] ) {
|
||||
$basename .= '/';
|
||||
}
|
||||
|
||||
$permalink = '/' . $basename;
|
||||
|
||||
// Get post date.
|
||||
$post_date = get_the_date( 'Y-m-d', $post_id );
|
||||
|
||||
// Check if the published date is earlier than default start date.
|
||||
if ( strtotime( $post_date ) > strtotime( $options['startdate'] ) ) {
|
||||
$start_date = $post_date;
|
||||
}
|
||||
|
||||
// Get cached post views.
|
||||
$result = $cached ? powerkit_get_cached_post_views( $post_id ) : false;
|
||||
|
||||
if ( false === $result || '' === $result ) {
|
||||
|
||||
$json = powerkit_post_views_api_call(
|
||||
'https://www.googleapis.com/analytics/v3/data/ga', array(
|
||||
'ids' => 'ga:' . $options['wid'],
|
||||
'filters' => 'ga:pagePath=@' . $permalink,
|
||||
'max-results' => 1000,
|
||||
'metrics' => $options['metric'],
|
||||
'start-date' => $start_date,
|
||||
'end-date' => date( 'Y-m-d' ),
|
||||
), false
|
||||
);
|
||||
|
||||
if ( isset( $json->totalsForAllResults->{$options['metric']} ) ) {
|
||||
|
||||
$total_result = $json->totalsForAllResults->{$options['metric']};
|
||||
|
||||
// Set cached views.
|
||||
powerkit_set_cached_post_views( $post_id, $total_result );
|
||||
|
||||
$result = $total_result;
|
||||
} else {
|
||||
|
||||
$value = $options['defaultval'];
|
||||
|
||||
// If we have an old value let's put that instead of the default one in case of an error.
|
||||
$db_value = powerkit_get_nocached_post_views( $post_id );
|
||||
|
||||
if ( false !== $db_value && '' !== $db_value ) {
|
||||
$value = $db_value;
|
||||
}
|
||||
|
||||
// Set cached views.
|
||||
powerkit_set_cached_post_views( $post_id, $value );
|
||||
|
||||
$result = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$result = apply_filters( 'powerkit_get_post_views', $result, $post_id, $format );
|
||||
|
||||
return ( $format ) ? number_format_i18n( (float) $result ) : $result;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* Post Views Query
|
||||
*
|
||||
* @package Powerkit
|
||||
* @subpackage Modules/Query
|
||||
*/
|
||||
|
||||
class Powerkit_Post_Views_Query {
|
||||
|
||||
/**
|
||||
* Construct.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'pre_get_posts', array( $this, 'extend_pre_query' ), 1 );
|
||||
add_filter( 'posts_join', array( $this, 'posts_join' ), 10, 2 );
|
||||
add_filter( 'posts_groupby', array( $this, 'posts_groupby' ), 10, 2 );
|
||||
add_filter( 'posts_orderby', array( $this, 'posts_orderby' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend query with post_views orderby parameter.
|
||||
*
|
||||
* @param object $query The query.
|
||||
*/
|
||||
public function extend_pre_query( $query ) {
|
||||
if ( isset( $query->query_vars['orderby'] ) && 'pk_post_views' === $query->query_vars['orderby'] ) {
|
||||
$query->pk_post_views = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify the db query to use post_views parameter.
|
||||
*
|
||||
* @param string $join The join.
|
||||
* @param object $query The query.
|
||||
*/
|
||||
public function posts_join( $join, $query ) {
|
||||
|
||||
if ( isset( $query->pk_post_views ) && $query->pk_post_views ) {
|
||||
global $wpdb;
|
||||
|
||||
$join .= ' LEFT JOIN ' . $wpdb->prefix . 'pk_post_views ON ' . $wpdb->prefix . 'pk_post_views.id = ' . $wpdb->prefix . 'posts.ID';
|
||||
}
|
||||
|
||||
return $join;
|
||||
}
|
||||
|
||||
/**
|
||||
* Group posts using the post ID.
|
||||
*
|
||||
* @param string $groupby The groupby.
|
||||
* @param object $query The query.
|
||||
*/
|
||||
public function posts_groupby( $groupby, $query ) {
|
||||
|
||||
if ( isset( $query->pk_post_views ) && $query->pk_post_views ) {
|
||||
global $wpdb;
|
||||
|
||||
$groupby = trim( $groupby );
|
||||
|
||||
if ( false === strpos( $groupby, $wpdb->prefix . 'posts.ID' ) ) {
|
||||
$groupby = ( '' !== $groupby ? $groupby . ', ' : '' ) . $wpdb->prefix . 'posts.ID';
|
||||
}
|
||||
}
|
||||
|
||||
return $groupby;
|
||||
}
|
||||
|
||||
/**
|
||||
* Order posts by post views.
|
||||
*
|
||||
* @param string $orderby The orderby.
|
||||
* @param object $query The query.
|
||||
*/
|
||||
public function posts_orderby( $orderby, $query ) {
|
||||
if ( isset( $query->pk_post_views ) && $query->pk_post_views ) {
|
||||
global $wpdb;
|
||||
|
||||
$order = $query->get( 'order' );
|
||||
|
||||
$orderby = $wpdb->prefix . 'pk_post_views.count ' . $order . ', ' . $wpdb->prefix . 'posts.ID ' . $order;
|
||||
}
|
||||
|
||||
return $orderby;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
new Powerkit_Post_Views_Query();
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* The public-facing functionality of the module.
|
||||
*
|
||||
* @link https://codesupply.co
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package Powerkit
|
||||
* @subpackage Modules/public
|
||||
*/
|
||||
|
||||
/**
|
||||
* The public-facing functionality of the module.
|
||||
*/
|
||||
class Powerkit_Post_Views_Public extends Powerkit_Module_Public {
|
||||
|
||||
/**
|
||||
* Initialize
|
||||
*/
|
||||
public function initialize() {
|
||||
add_filter( 'wp_ajax_post_views_reset', array( $this, 'post_views_reset' ) );
|
||||
add_filter( 'wp_ajax_nopriv_post_views_reset', array( $this, 'post_views_reset' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Post Views Reset
|
||||
*/
|
||||
public function post_views_reset() {
|
||||
powerkit_uuid_hash();
|
||||
|
||||
// Check wpnonce.
|
||||
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
|
||||
return;
|
||||
}
|
||||
|
||||
if ( isset( $_REQUEST['post_id'] ) && $_REQUEST['post_id'] ) { // Input var ok; sanitization ok.
|
||||
$post_id = (int) sanitize_text_field( $_REQUEST['post_id'] ); // Input var ok; sanitization ok.
|
||||
|
||||
$result = esc_html( powerkit_get_post_views( $post_id, true, false ) );
|
||||
|
||||
wp_send_json_success( $result );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the stylesheets for the public-facing side of the site.
|
||||
*/
|
||||
public function wp_enqueue_scripts() {
|
||||
if ( ! is_singular() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$options = powerkit_post_views_options();
|
||||
|
||||
if ( $options['token'] && $options['clientid'] && $options['psecret'] ) {
|
||||
// Scripts.
|
||||
wp_enqueue_script( 'powerkit-post-views', plugin_dir_url( __FILE__ ) . 'js/public-powerkit-post-views.js', array( 'jquery' ), powerkit_get_setting( 'version' ), true );
|
||||
|
||||
// Localize scripts.
|
||||
wp_localize_script( 'powerkit-post-views', 'pkPostViews', array(
|
||||
'ajaxurl' => admin_url( 'admin-ajax.php?action=post_views_reset' ),
|
||||
'post_id' => get_queried_object_id(),
|
||||
'nonce' => wp_create_nonce( ),
|
||||
) );
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Post Views Reset.
|
||||
*/
|
||||
|
||||
( function( $ ) {
|
||||
|
||||
$( document ).ready( function() {
|
||||
setTimeout(function(){
|
||||
$.post( pkPostViews.ajaxurl, {
|
||||
'_wpnonce': pkPostViews.nonce,
|
||||
'post_id' : pkPostViews.post_id,
|
||||
} );
|
||||
}, 1500);
|
||||
} );
|
||||
|
||||
} )( jQuery );
|
||||
Reference in New Issue
Block a user