= $options['tapp_expires']) { $options = tapp_refresh_token(); } $qs = '?access_token='.urlencode($options['tapp_token']); foreach ($params as $k => $v) { $qs .= '&'.$k.'='.($urlEncode ? urlencode($v) : $v); } $request = new WP_Http; $result = $request->request($url.$qs); $json = new stdClass(); $options['tapp_error'] = null; if ( is_array( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) { $json = json_decode($result['body']); update_option('tapp', $options); return $json; } else { if ( is_array( $result ) && isset( $result['response']['code'] ) && 403 === $result['response']['code'] ) { $json = json_decode($result['body'], true); $options['tapp_error'] = $json['error']['errors'][0]['message']; $options['tapp_token'] = null; $options['tapp_token_refresh'] = null; $options['tapp_expires'] = null; $options['tapp_gid'] = null; update_option('tapp', $options); } return new stdClass(); } } function tapp_refresh_token() { $options = tapp_options(); /* If the token has expired, we create it again */ if (!empty($options['tapp_token_refresh'])) { $request = new WP_Http; $result = $request->request('https://accounts.google.com/o/oauth2/token', array( 'method' => 'POST', 'body' => array( 'client_id' => $options['tapp_clientid'], 'client_secret' => $options['tapp_psecret'], 'refresh_token' => $options['tapp_token_refresh'], 'grant_type' => 'refresh_token', ), )); $options['tapp_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='.urlencode($tjson->access_token)); if ( is_array( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) { $ijson = json_decode($result['body']); $options['tapp_token'] = $tjson->access_token; if (isset($tjson->refresh_token) && !empty($tjson->refresh_token)) { $options['tapp_token_refresh'] = $tjson->refresh_token; } $options['tapp_expires'] = time() + $tjson->expires_in; $options['tapp_gid'] = $ijson->id; update_option('tapp', $options); } elseif ( is_array( $result ) && isset( $result['response']['code'] ) && 403 === $result['response']['code'] ) { $json = json_decode($result['body'], true); $options['tapp_error'] = $json['error']['errors'][0]['message']; $options['tapp_token'] = null; $options['tapp_token_refresh'] = null; $options['tapp_expires'] = null; $options['tapp_gid'] = null; update_option('tapp', $options); } } } return $options; } function tapp_options() { $options = get_option('tapp'); if (!isset($options['tapp_clientid'])) { if (isset($options['tapp_pnumber'])) { if (isset($options['tapp_clientid'])){ $options['tapp_clientid'] = $options['tapp_pnumber'] . '.apps.googleusercontent.com'; } } else { if (isset($options['tapp_clientid'])) { $options = ['tapp_clientid' => null]; } } } if (isset($options['tapp_pnumber'])) unset($options['tapp_pnumber']); if (!isset($options['tapp_psecret'])) $options = ['tapp_psecret' => null]; if (!isset($options['tapp_gid'])) $options =['tapp_gid' => null] ; if (!isset($options['tapp_gmail'])) $options = ['tapp_gmail' => null]; if (!isset($options['tapp_token'])) $options = ['tapp_token' => null]; if (!isset($options['tapp_defaultval'])) $options['tapp_defaultval'] = 0; if (!isset($options['tapp_totalpost'])) $options['tapp_totalpost'] = 1; if (!isset($options['tapp_totalpost_grid'])) $options['tapp_totalpost_grid'] = 1; if (!isset($options['tapp_btn_color'])) $options['tapp_btn_color'] = null; if (!isset($options['tapp_sliderwidth'])) $options['tapp_sliderwidth'] = '100%'; if (!isset($options['tapp_sliderheight'])) $options['tapp_sliderheight'] = '600px'; if (!isset($options['tapp_analytics_code'])) $options['tapp_analytics_code'] = ''; if (!isset($options['tapp_token_refresh'])) $options['tapp_token_refresh'] = null; if (!isset($options['tapp_expires'])) $options['tapp_expires'] = null; if (!isset($options['tapp_wid'])) $options['tapp_wid'] = null; if (!isset($options['tapp_column'])) $options['tapp_column'] = true; if (!isset($options['tapp_onoffswitch'])) $options['tapp_onoffswitch'] = true; if (!isset($options['tapp_onoffswitch_grid'])) $options['tapp_onoffswitch_grid'] = true; if (!isset($options['tapp_trailing'])) $options['tapp_trailing'] = true; if (!isset($options['tapp_cache'])) $options['tapp_cache'] = 60; if (!isset($options['tapp_metric'])) $options['tapp_metric'] = 'ga:pageviews'; if(isset($options['tapp_startdate'])) { if (!preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', $options['tapp_startdate'])) $options['tapp_startdate'] = '2007-09-29'; } return $options; } function tapp_conf() { /** @var $wpdb WPDB */ global $wpdb; $options = tapp_options(); $updated = false; if (isset($_GET['state']) && $_GET['state'] == 'init' && $_GET['code']) { $request = new WP_Http; $result = $request->request('https://accounts.google.com/o/oauth2/token', array( 'method' => 'POST', 'body' => array( 'code' => sanitize_text_field($_GET['code']), 'client_id' => $options['tapp_clientid'], 'client_secret' => $options['tapp_psecret'], 'redirect_uri' => admin_url('options-general.php?page=' . TAPP_SLUG), 'grant_type' => 'authorization_code', ) )); if ( !is_array( $result ) || !isset( $result['response']['code'] ) && 200 !== $result['response']['code'] ) { echo '

'; _e('There was something wrong with Authentication.', 'addweb-traffic-post-page-views'); echo "

"; var_dump($result); } $tjson = json_decode($result['body']); $options['tapp_token'] = $tjson->access_token; $options['tapp_token_refresh'] = $tjson->refresh_token; $options['tapp_expires'] = time() + $tjson->expires_in; update_option('tapp', $options); $ijson = tapp_api_call('https://www.googleapis.com/oauth2/v1/userinfo', array()); $options['tapp_gid'] = $ijson->id; $options['tapp_gmail'] = $ijson->email; update_option('tapp', $options); if (!empty($options['tapp_token']) && !empty($options['tapp_gmail'])) { echo ''; exit; } } elseif (isset($_GET['state']) && $_GET['state'] == 'reset') { if (current_user_can('manage_options')) { $options['tapp_gid'] = null; $options['tapp_gmail'] = null; $options['tapp_token'] = null; $options['tapp_token_refresh'] = null; $options['tapp_expires'] = null; $options['tapp_defaultval'] = 0; $options['tapp_totalpost'] = 1; $options['tapp_totalpost_grid'] = 1; $options['tapp_btn_color'] = null; $options['onoffswitch'] = 1; $options['tapp_sliderwidth'] = '100%'; $options['tapp_sliderheight'] = '600px'; $options['tapp_analytics_code'] = ''; update_option('tapp', $options); $updated = true; } } elseif (isset($_GET['state']) && $_GET['state'] == 'clear') { $options['tapp_clientid'] = null; $options['tapp_psecret'] = null; update_option('tapp', $options); $updated = true; } elseif (isset($_GET['refresh'])) { tapp_refresh_token(); $options = tapp_options(); $updated = true; } elseif (isset($_GET['reset'])) { $wpdb->query("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '_transient_tapp-transient-%'"); $wpdb->query("DELETE FROM `" . $wpdb->options . "` WHERE `option_name` LIKE '_transient_timeout_tapp-transient-%'"); set_transient('tapp-namespace-key', uniqid(), 86400 * 365); $updated = true; } if (isset($_REQUEST['submit'])) { check_admin_referer('tapp', 'tapp-admin'); if (current_user_can('manage_options')) { if (isset($_POST['tapp_clientid'])) { $options['tapp_clientid'] = sanitize_text_field($_POST['tapp_clientid']); } if (isset($_POST['tapp_psecret'])) { $options['tapp_psecret'] = sanitize_text_field($_POST['tapp_psecret']); } if (isset($_POST['tapp_wid'])) { $options['tapp_wid'] = sanitize_text_field($_POST['tapp_wid']); } if (isset($_POST['tapp_cache'])) { $options['tapp_cache'] = sanitize_text_field($_POST['tapp_cache']); } if (isset($_POST['tapp_startdate'])) { $options['tapp_startdate'] = sanitize_text_field($_POST['tapp_startdate']); } if (isset($_POST['tapp_defaultval'])) { $options['tapp_defaultval'] = sanitize_text_field($_POST['tapp_defaultval']); } if (isset($_POST['tapp_totalpost'])) { $options['tapp_totalpost'] = sanitize_text_field($_POST['tapp_totalpost']); } if (isset($_POST['tapp_totalpost_grid'])) { $options['tapp_totalpost_grid'] = sanitize_text_field($_POST['tapp_totalpost_grid']); } if (isset($_POST['tapp_btn_color'])) { $options['tapp_btn_color'] = sanitize_text_field($_POST['tapp_btn_color']); } if (isset($_POST['tapp_sliderwidth'])) { $options['tapp_sliderwidth'] = sanitize_text_field($_POST['tapp_sliderwidth']); } if (isset($_POST['tapp_sliderheight'])) { $options['tapp_sliderheight'] = sanitize_text_field($_POST['tapp_sliderheight']); } if (isset($_POST['tapp_analytics_code'])) { $options['tapp_analytics_code'] = sanitize_text_field($_POST['tapp_analytics_code']); } if (isset($_POST['tapp_metric'])) { $options['tapp_metric'] = sanitize_text_field($_POST['tapp_metric']); } $options['tapp_onoffswitch'] = (isset($_POST['tapp_onoffswitch'])); $options['tapp_onoffswitch_grid'] = (isset($_POST['tapp_onoffswitch_grid'])); $options['tapp_column'] = (isset($_POST['tapp_column'])); $options['tapp_trailing'] = (isset($_POST['tapp_trailing'])); update_option('tapp', $options); $updated = true; } } echo '
'; if ($updated) { echo '

'; _e('Configuration updated.', 'addweb-traffic-post-page-views'); echo '

'; } echo '

'.__('Traffic Post Page Views Settings', 'addweb-traffic-post-page-views').'

'; if (empty($options['tapp_token'])) { if (empty($options['tapp_clientid']) || empty($options['tapp_psecret'])) { echo '

'.__('In order to connect to your G-Analytics Account, you need to create a new project in the G-API Console and activate the Analytics API in "APIs & auth > APIs".', 'addweb-traffic-post-page-views').'

'; echo '
'; echo '

'.__('Then, create an OAuth Client ID in "APIs & auth > Credentials". Enter this URL for the Redirect URI field:', 'addweb-traffic-post-page-views').'
'; echo admin_url('options-general.php?page=' . TAPP_SLUG); echo '

'; echo '

'.__('You also have to fill the Product Name field in "APIs & auth" -> "Consent screen" — you need to select e-mail address as well.').'

'; echo '
'; echo '
'; echo ' WeWp '; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; wp_nonce_field('tapp', 'tapp-admin'); echo '

'; echo '
'; $arrAddwebPlugins = array( 'woo-cart-customizer' => 'Simple Customization of Add to Cart Button', 'aws-cookies-popup' => 'AWS Cookies Popup', 'addweb-google-popular-post' => 'Traffic Post Page Views', 'post-timer' => 'Post Timer', 'wc-past-orders' => 'Track Order History for WooCommerce', 'widget-social-share' => 'WSS: Widget Social Share' ); echo ''; echo '
'; echo 'WeWp'; echo '
'; echo '
'; echo '

'.__('Developed with').' AddwebSolution By ADDWEB SOLUTION

'; echo '
'; echo '
'; echo '
'; } else { $url_auth = 'https://accounts.google.com/o/oauth2/auth?client_id='.$options['tapp_clientid'].'&redirect_uri='; $url_auth .= admin_url('options-general.php?page=' . TAPP_SLUG); $url_auth .= '&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'; echo '

'.__('Connect to G-Analytics', 'addweb-traffic-post-page-views').'

'; echo '

'.__('Clear the API keys').' »

'; } } else { echo '
'; echo '
'; echo '

'.__('You are connected to G-Analytics with the e-mail address:', 'addweb-traffic-post-page-views').' '.esc_attr($options['tapp_gmail']).'.

'; echo '

'.__('Your token expires on:', 'addweb-traffic-post-page-views').' '.date_i18n( 'Y/m/d \a\t g:ia', esc_attr($options['tapp_expires']) + ( get_option( 'gmt_offset' ) * 3600 ) , 1 ).'.

'; echo '

'.__('Disconnect from G-Analytics', 'addweb-traffic-post-page-views').' »

'; echo '

'.__('Refresh G-API token', 'addweb-traffic-post-page-views').' »

'; echo '

'.__('Empty pageviews cache', 'addweb-traffic-post-page-views').' »

'; echo '
'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '
'; echo ''; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '

'; echo '
'; echo ''; echo '

'; echo '

'; echo '
'; echo '

'; echo '

'; echo '

'; echo '
'; echo '

'; wp_nonce_field('tapp', 'tapp-admin'); echo '

'; } } function tapp_get_post_pageviews($ID = null, $format = true, $save = true) { $options = tapp_options(); if ($ID) { $basename = basename(get_permalink($ID)); if ($options['tapp_trailing']) { $basename .= '/'; } $gaTransName = 'tapp-transient-'.$ID; $permalink = '/' . (($ID != 1) ? $basename : null); $postID = $ID; $postDate = get_the_date('Y-m-d', $postID); } else { $basename = basename(get_permalink()); if ($options['tapp_trailing']) { $basename .= '/'; } $gaTransName = 'tapp-transient-'.get_the_ID(); $permalink = '/' . $basename; $postID = get_the_ID(); $postDate = get_the_date('Y-m-d'); } // Check if the published date is earlier than default start date if (strtotime($postDate) > strtotime($options['tapp_startdate'])) { $startDate = $postDate; } else { $startDate = $options['tapp_startdate']; } $namespaceKey = get_transient('tapp-namespace-key'); if ($namespaceKey === false) { $namespaceKey = uniqid(); set_transient('tapp-namespace-key', $namespaceKey, YEAR_IN_SECONDS); } $gaTransName .= '-' . $namespaceKey; $totalResult = get_transient($gaTransName); if ($totalResult !== false && is_numeric($totalResult)) { if ($save && !add_post_meta($postID, '_tapp_post_views', $totalResult, true)) { update_post_meta($postID, '_tapp_post_views', $totalResult); } return ($format) ? number_format_i18n($totalResult) : $totalResult; } else { if (empty($options['tapp_token'])) { return $options['tapp_defaultval']; } if (!$ID || $ID != 1) { if ($ID) { $status = get_post_status($ID); } else { $status = get_post_status(get_the_ID()); } if ($status !== 'publish') { set_transient($gaTransName, '0', 60 * $options['tapp_cache']); if (!add_post_meta($postID, '_tapp_post_views', '0', true)) { update_post_meta($postID, '_tapp_post_views', '0'); } return 0; } } $json = tapp_api_call('https://www.googleapis.com/analytics/v3/data/ga', array('ids' => 'ga:'.$options['tapp_wid'], 'start-date' => $startDate, 'end-date' => date('Y-m-d'), 'metrics' => $options['tapp_metric'], 'filters' => 'ga:pagePath=@' . $permalink, 'max-results' => 1000) , false); if ( isset( $json->totalsForAllResults->{$options['tapp_metric']} ) ) { $totalResult = $json->totalsForAllResults->{$options['tapp_metric']}; echo $totalResult; set_transient($gaTransName, $totalResult, 60 * $options['tapp_cache']); if (!add_post_meta($postID, '_tapp_post_views', $totalResult, true)) { update_post_meta($postID, '_tapp_post_views', $totalResult); } return ($format) ? number_format_i18n($totalResult) : $totalResult; } else { $default_value = $options['tapp_defaultval']; // If we have an old value let's put that instead of the default one in case of an error $meta_value = get_post_meta($postID, '_tapp_post_views', true); if ($meta_value !== false) { $default_value = $meta_value; } set_transient($gaTransName, $default_value, 60 * $options['tapp_cache']); return $options['tapp_defaultval']; } } } // Add a column in Posts list (Optional) add_filter('manage_posts_columns', 'tapp_column_views'); add_action('manage_posts_custom_column', 'tapp_custom_column_views', 6, 2); add_action('admin_head', 'tapp_column_style'); add_filter('manage_edit-post_sortable_columns', 'tapp_manage_sortable_columns'); add_action('pre_get_posts', 'tapp_pre_get_posts', 1); function tapp_column_views($defaults) { $options = tapp_options(); if (!empty($options['tapp_token']) && $options['tapp_column']) { $defaults['post_views'] = __('Views'); } return $defaults; } function tapp_custom_column_views($column_name, $id) { if ($column_name === 'post_views') { echo tapp_get_post_pageviews(get_the_ID(), true, true); } } function tapp_column_style() { echo ''; } function tapp_manage_sortable_columns($sortable_columns) { $sortable_columns['post_views'] = 'post_views'; return $sortable_columns; } function tapp_pre_get_posts($query) { if ($query->is_main_query() && ($orderby = $query->get('orderby'))) { switch ($orderby) { case 'post_views': $query->set('meta_key', '_tapp_post_views'); $query->set('orderby', 'meta_value_num'); break; } } return $query; } function tapp_admin_notice() { $options = tapp_options(); if (current_user_can('manage_options')) { if (isset($options['tapp_token']) && empty($options['tapp_token'])) { echo '

'.__('Traffic Post Page Views Warning: You have to (re)connect the plugin to your G-account.') . '
'.__('Update settings', 'addweb-traffic-post-page-views').' →

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

'.__('Traffic Post Page Views Error: ') . esc_attr($options['tapp_error']) . '
'.__('Update settings', 'addweb-traffic-post-page-views').' →

'; } } } // Admin notice add_action('admin_notices', 'tapp_admin_notice'); function tapp_ganalytics_scipt() { wp_register_style( 'admin-custom-css', plugin_dir_url( __FILE__ ) . 'inc/css/admin/admin-custom.css'); wp_register_style( 'jquery-ui-css', plugin_dir_url( __FILE__ ) . 'inc/css/admin/jquery-ui.css'); if( isset( $_GET['page']) && $_GET['page'] == 'addweb-traffic-post-page-views' ){ wp_enqueue_style('admin-custom-css', plugin_dir_url( __FILE__ ) . 'inc/css/admin/admin-custom.css'); wp_enqueue_style('jquery-ui-css', plugin_dir_url( __FILE__ ) . 'inc/css/admin/jquery-ui.css'); } wp_enqueue_script( 'custom', plugin_dir_url( __FILE__ ) . 'inc/js/admin/tabs.js', array('jquery-ui-tabs'), '', true ); } add_action('admin_enqueue_scripts', 'tapp_ganalytics_scipt'); function tapp_add_ganalytics() { $options = tapp_options(); if($options['tapp_analytics_code'] != ' ') { ?> '