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(); ?>

Google API Console and activate the Analytics API in "APIs & Services"', 'powerkit' ), 'post' ); ?>

  1. Credentials" (Select "Web application", enter this URL %s for the "Authorized redirect URIs" field). ', 'powerkit' ), '' . powerkit_get_page_url( $this->slug ) . '' ), 'post' ); ?>
$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' ); ?>

»

»

»

»


>
>

%s

', 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'] ) { ?>

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'] ) ) { ?>

%s

', 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( '

%s

', 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( '

%s

', 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 ''; } /** * 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 '

' . esc_html__( 'Google Post Views Warning: You have to (re)connect the plugin to your Google account.' ) . '
' . esc_html__( 'Update settings', 'powerkit' ) . ' →

'; } elseif ( isset( $options['error'] ) && ! empty( $options['error'] ) ) { echo '

' . esc_html__( 'Google Post Views Error: ', 'powerkit' ) . wp_kses( $options['error'], 'post' ) . '
' . esc_html__( 'Update settings', 'powerkit' ) . ' →

'; } } } }