/gads_dash.php

  1. <?php 
  2. /** 
  3. * Plugin Name: Google Adsense Dashboard 
  4. * Plugin URI: https://deconf.com  
  5. * Description: Earnings Dashboard will display Google Adsense earnings and statistics into Admin Dashboard. 
  6. * Author: Alin Marcu  
  7. * Version: 2.2.1  
  8. * Author URI: https://deconf.com 
  9. */ 
  10.  
  11. /** 
  12. * Install 
  13. */ 
  14. register_activation_hook ( __FILE__, 'gads_dash_install' ); 
  15. /** 
  16. * Uninstall 
  17. */ 
  18. register_uninstall_hook ( __FILE__, 'gads_dash_uninstall' ); 
  19. function gads_dash_admin() { 
  20. include ('gads_dash_admin.php'); 
  21. function gads_dash_init() { 
  22. load_plugin_textdomain ( 'gads-dash', false, dirname ( plugin_basename ( __FILE__ ) ) . '/languages' ); 
  23. function gads_dash_admin_actions() { 
  24. if (current_user_can ( 'manage_options' )) { 
  25. add_options_page ( "Earnings Dashboard", "Earnings Dashboard", "manage_options", "Earnings_Dashboard", "gads_dash_admin" ); 
  26.  
  27. $plugin = plugin_basename ( __FILE__ ); 
  28. add_action ( 'admin_menu', 'gads_dash_admin_actions' ); 
  29. add_action ( 'wp_dashboard_setup', 'gads_dash_setup' ); 
  30. add_action ( 'admin_enqueue_scripts', 'gads_dash_admin_enqueue_styles' ); 
  31. add_action ( 'plugins_loaded', 'gads_dash_init' ); 
  32. add_filter ( "plugin_action_links_$plugin", 'gads_dash_settings_link' ); 
  33. function gads_dash_admin_enqueue_styles($hook) { 
  34.  
  35. $valid_hooks = array ( 
  36. 'settings_page_Earnings_Dashboard'  
  37. ); 
  38.  
  39. if (! in_array ( $hook, $valid_hooks ) and 'index.php' != $hook) { 
  40. return; 
  41.  
  42. if ('green' == get_option ( 'gads_dash_style' ) || 'index.php' != $hook) { 
  43. wp_register_style ( 'gads_dash', plugins_url ( 'gads_dash.css', __FILE__ ) ); 
  44. wp_enqueue_style ( 'gads_dash' ); 
  45. function gads_dash_settings_link($links) { 
  46. $settings_link = '<a href="options-general.php?page=Earnings_Dashboard">' . __ ( "Settings", 'gads-dash' ) . '</a>'; 
  47. array_unshift ( $links, $settings_link ); 
  48. return $links; 
  49. function gads_dash_setup() { 
  50. if (current_user_can ( get_option ( 'gads_dash_access' ) )) { 
  51. wp_add_dashboard_widget ( 'gads-dash-widget', 'Earnings Dashboard for Google Adsense*', 'gads_dash_content', $control_callback = null ); 
  52. function gads_dash_install() { 
  53. update_option ( 'gads_dash_ads', 0 ); 
  54. update_option ( 'gads_dash_channels', 0 ); 
  55. update_option ( 'gads_dash_access', 'manage_options' ); 
  56. update_option ( 'gads_dash_style', 'green' ); 
  57. update_option ( 'gads_dash_cachetime', 3600 ); 
  58. update_option ( 'gads_dash_timezone', true ); 
  59. update_option ( 'gads_dash_userapi', 0 ); 
  60. update_option ( 'gads_dash_apikey', '' ); 
  61. update_option ( 'gads_dash_clientid', '' ); 
  62. update_option ( 'gads_dash_clientsecret', '' ); 
  63. function gads_dash_uninstall() { 
  64. global $wpdb; 
  65. if (is_multisite ()) { // Cleanup Network install 
  66. foreach ( wp_get_sites () as $blog ) { 
  67. switch_to_blog ( $blog ['blog_id'] ); 
  68. $sqlquery = $wpdb->query ( "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_gadsdash%%'" ); 
  69. $sqlquery = $wpdb->query ( "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timeout_gadsdash%%'" ); 
  70. delete_option ( 'gads_dash_ads', 0 ); 
  71. delete_option ( 'gads_dash_channels', 0 ); 
  72. delete_option ( 'gads_dash_access', 'manage_options' ); 
  73. delete_option ( 'gads_dash_style', 'green' ); 
  74. delete_option ( 'gads_dash_cachetime', 3600 ); 
  75. delete_option ( 'gads_dash_timezone', true ); 
  76. delete_option ( 'gads_dash_userapi', 1 ); 
  77. delete_option ( 'gads_dash_apikey', '' ); 
  78. delete_option ( 'gads_dash_clientid', '' ); 
  79. delete_option ( 'gads_dash_clientsecret', '' ); 
  80. restore_current_blog (); 
  81. } else { // Cleanup Single install 
  82. $sqlquery = $wpdb->query ( "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_gadsdash%%'" ); 
  83. $sqlquery = $wpdb->query ( "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timeout_gadsdash%%'" ); 
  84. delete_option ( 'gads_dash_ads', 0 ); 
  85. delete_option ( 'gads_dash_channels', 0 ); 
  86. delete_option ( 'gads_dash_access', 'manage_options' ); 
  87. delete_option ( 'gads_dash_style', 'green' ); 
  88. delete_option ( 'gads_dash_cachetime', 3600 ); 
  89. delete_option ( 'gads_dash_timezone', true ); 
  90. delete_option ( 'gads_dash_userapi', 1 ); 
  91. delete_option ( 'gads_dash_apikey', '' ); 
  92. delete_option ( 'gads_dash_clientid', '' ); 
  93. delete_option ( 'gads_dash_clientsecret', '' ); 
  94. function gads_dash_content() { 
  95. require_once 'functions.php'; 
  96.  
  97. $auth = new AdSenseAuth (); 
  98.  
  99. $result = $auth->authenticate ( 'default' ); 
  100.  
  101. $adSense = $auth->getAdSenseService (); 
  102.  
  103. if (isset ( $_REQUEST ['query_adsense'] )) 
  104. $query_adsense = $_REQUEST ['query_adsense']; 
  105. else 
  106. $query_adsense = "EARNINGS"; 
  107.  
  108. if (isset ( $_REQUEST ['period_adsense'] )) 
  109. $period_adsense = $_REQUEST ['period_adsense']; 
  110. else 
  111. $period_adsense = "last30days"; 
  112.  
  113. if (get_option ( 'gads_dash_style' ) == "light") { 
  114. $css = "colors:['gray', 'darkgray'], "; 
  115. $colors = "black"; 
  116. } else { 
  117. $css = "colors:['green', 'darkgreen'], "; 
  118. $colors = "green"; 
  119.  
  120. switch ($period_adsense) { 
  121.  
  122. case 'today' : 
  123. $from = date ( 'Y-m-d' ); 
  124. $to = date ( 'Y-m-d' ); 
  125. break; 
  126.  
  127. case 'yesterday' : 
  128. $from = date ( 'Y-m-d', time () - 24 * 60 * 60 ); 
  129. $to = date ( 'Y-m-d', time () - 24 * 60 * 60 ); 
  130. break; 
  131.  
  132. case 'last7days' : 
  133. $from = date ( 'Y-m-d', time () - 7 * 24 * 60 * 60 ); 
  134. $to = date ( 'Y-m-d', time () - 24 * 60 * 60 ); 
  135. break; 
  136.  
  137. case 'last14days' : 
  138. $from = date ( 'Y-m-d', time () - 14 * 24 * 60 * 60 ); 
  139. $to = date ( 'Y-m-d', time () - 24 * 60 * 60 ); 
  140. break; 
  141.  
  142. default : 
  143. $from = date ( 'Y-m-d', time () - 30 * 24 * 60 * 60 ); 
  144. $to = date ( 'Y-m-d', time () - 24 * 60 * 60 ); 
  145. break; 
  146.  
  147. switch ($query_adsense) { 
  148.  
  149. case 'COST_PER_CLICK' : 
  150. $title = "Cost per Click"; 
  151. break; 
  152.  
  153. case 'CLICKS' : 
  154. $title = "Clicks"; 
  155. break; 
  156.  
  157. case 'PAGE_VIEWS' : 
  158. $title = "Page Views"; 
  159. break; 
  160.  
  161. case 'PAGE_VIEWS_CTR' : 
  162. $title = "Click Through Rate"; 
  163. break; 
  164.  
  165. case 'PAGE_VIEWS_RPM' : 
  166. $title = "Revenue per thousand impressions"; 
  167. break; 
  168.  
  169. default : 
  170. $title = "Earnings"; 
  171.  
  172. $optParams = array ( 
  173. 'metric' => array ( 
  174. $query_adsense  
  175. ),  
  176. 'dimension' => 'DATE',  
  177. 'sort' => 'DATE',  
  178. 'useTimezoneReporting' => get_option ( 'gads_dash_timezone' )  
  179. ); 
  180. try { 
  181.  
  182. $serial = 'gadsdash_qr1' . str_replace ( array ( 
  183. ', ',  
  184. '-',  
  185. date ( 'Y' )  
  186. ), "", $from . $to . get_option ( 'gads_dash_timezone' ) . $query_adsense ); 
  187. $transient = get_transient ( $serial ); 
  188. if (empty ( $transient )) { 
  189. $data = $adSense->reports->generate ( $from, $to, $optParams ); 
  190. set_transient ( $serial, $data, get_option ( 'gads_dash_cachetime' ) ); 
  191. } else { 
  192. $data = $transient; 
  193. } catch ( exception $e ) { 
  194. if (get_option ( 'gads_dash_token' )) { 
  195. echo gads_dash_pretty_error ( $e ); 
  196. return; 
  197.  
  198. if (! isset ( $data )) { 
  199. return; 
  200.  
  201. $gads_chart1_data = ""; 
  202. for($i = 0; $i < $data ['totalMatchedRows']; $i ++) { 
  203. if ($query_adsense == 'PAGE_VIEWS_CTR') 
  204. $gads_chart1_data .= "['" . $data ['rows'] [$i] [0] . "', " . ($data ['rows'] [$i] [1] * 100) . "], "; 
  205. else 
  206. $gads_chart1_data .= "['" . $data ['rows'] [$i] [0] . "', " . $data ['rows'] [$i] [1] . "], "; 
  207.  
  208. $gads_chart1_data = wp_kses ( rtrim ( $gads_chart1_data, ', ' ), '' ); 
  209.  
  210. $optParams = array ( 
  211. 'metric' => array ( 
  212. 'EARNINGS',  
  213. 'COST_PER_CLICK',  
  214. 'CLICKS',  
  215. 'PAGE_VIEWS',  
  216. 'PAGE_VIEWS_CTR',  
  217. 'PAGE_VIEWS_RPM'  
  218. ),  
  219. 'dimension' => 'YEAR',  
  220. 'useTimezoneReporting' => get_option ( 'gads_dash_timezone' )  
  221. ); 
  222.  
  223. try { 
  224. $serial = 'gadsdash_qr2' . str_replace ( array ( 
  225. ', ',  
  226. '-',  
  227. date ( 'Y' )  
  228. ), "", $from . $to . get_option ( 'gads_dash_timezone' ) ); 
  229. $transient = get_transient ( $serial ); 
  230. if (empty ( $transient )) { 
  231. $gads_sum_data = $adSense->reports->generate ( $from, $to, $optParams ); 
  232. set_transient ( $serial, $gads_sum_data, get_option ( 'gads_dash_cachetime' ) ); 
  233. } else { 
  234. $gads_sum_data = $transient; 
  235. } catch ( exception $e ) { 
  236. if (get_option ( 'gads_dash_token' )) { 
  237. echo gads_dash_pretty_error ( $e ); 
  238. return; 
  239.  
  240. // Channel Performance 
  241.  
  242. $optParams = array ( 
  243. 'metric' => array ( 
  244. 'EARNINGS',  
  245. 'COST_PER_CLICK',  
  246. 'CLICKS',  
  247. 'PAGE_VIEWS',  
  248. 'PAGE_VIEWS_CTR',  
  249. 'PAGE_VIEWS_RPM'  
  250. ),  
  251. 'dimension' => 'CUSTOM_CHANNEL_NAME',  
  252. 'sort' => '-EARNINGS',  
  253. 'useTimezoneReporting' => get_option ( 'gads_dash_timezone' )  
  254. ); 
  255. try { 
  256.  
  257. $serial = 'gadsdash_qr3' . str_replace ( array ( 
  258. ', ',  
  259. '-',  
  260. date ( 'Y' )  
  261. ), "", $from . $to . get_option ( 'gads_dash_timezone' ) ); 
  262. $transient = get_transient ( $serial ); 
  263. if (empty ( $transient )) { 
  264. $chdata = $adSense->reports->generate ( $from, $to, $optParams ); 
  265. set_transient ( $serial, $chdata, get_option ( 'gads_dash_cachetime' ) ); 
  266. } else { 
  267. $chdata = $transient; 
  268. } catch ( exception $e ) { 
  269. if (get_option ( 'gads_dash_token' )) { 
  270. echo gads_dash_pretty_error ( $e ); 
  271. return; 
  272. // print_r($chdata); 
  273. $gads_ch_data = ""; 
  274. for($i = 0; $i < $chdata ['totalMatchedRows']; $i ++) { 
  275. $validate_rpm = ($chdata ['rows'] [$i] [6] == "") ? 0.00 : $chdata ['rows'] [$i] [6]; 
  276. $gads_ch_data .= "['" . $chdata ['rows'] [$i] [0] . "', " . $chdata ['rows'] [$i] [1] . ", " . $chdata ['rows'] [$i] [2] . ", " . $chdata ['rows'] [$i] [3] . ", " . $chdata ['rows'] [$i] [4] . ", " . ($chdata ['rows'] [$i] [5] * 100) . ", " . $validate_rpm . "], "; 
  277. $gads_ch_data = wp_kses ( rtrim ( $gads_ch_data, ', ' ), '' ); 
  278.  
  279. // Ads Performance 
  280.  
  281. $optParams = array ( 
  282. 'metric' => array ( 
  283. 'EARNINGS',  
  284. 'COST_PER_CLICK',  
  285. 'CLICKS',  
  286. 'AD_REQUESTS',  
  287. 'AD_REQUESTS_CTR',  
  288. 'AD_REQUESTS_RPM'  
  289. ),  
  290. 'dimension' => 'AD_UNIT_NAME',  
  291. 'sort' => '-EARNINGS',  
  292. 'useTimezoneReporting' => get_option ( 'gads_dash_timezone' )  
  293. ); 
  294. try { 
  295.  
  296. $serial = 'gadsdash_qr4' . str_replace ( array ( 
  297. ', ',  
  298. '-',  
  299. date ( 'Y' )  
  300. ), "", $from . $to . get_option ( 'gads_dash_timezone' ) ); 
  301. $transient = get_transient ( $serial ); 
  302. if (empty ( $transient )) { 
  303. $adsdata = $adSense->reports->generate ( $from, $to, $optParams ); 
  304. set_transient ( $serial, $adsdata, get_option ( 'gads_dash_cachetime' ) ); 
  305. } else { 
  306. $adsdata = $transient; 
  307. } catch ( exception $e ) { 
  308. if (get_option ( 'gads_dash_token' )) { 
  309. echo gads_dash_pretty_error ( $e ); 
  310. return; 
  311. // print_r($adsdata); 
  312. $gads_ads_data = ""; 
  313. for($i = 0; $i < $adsdata ['totalMatchedRows']; $i ++) { 
  314. $gads_ads_data .= "['" . $adsdata ['rows'] [$i] [0] . "', " . $adsdata ['rows'] [$i] [1] . ", " . $adsdata ['rows'] [$i] [2] . ", " . $adsdata ['rows'] [$i] [3] . ", " . $adsdata ['rows'] [$i] [4] . ", " . ($adsdata ['rows'] [$i] [5] * 100) . ", " . $adsdata ['rows'] [$i] [6] . "], "; 
  315. $gads_ads_data = wp_kses ( rtrim ( $gads_ads_data, ', ' ), '' ); 
  316.  
  317. $code = '<script type="text/javascript" src="https://www.google.com/jsapi"></script> 
  318. <script type="text/javascript"> 
  319. google.load("visualization", "1", {packages:["corechart"]}); 
  320. google.setOnLoadCallback(gads_dash_callback); 
  321.  
  322. function gads_dash_callback() { 
  323. adsense_drawChart1(); 
  324. if(typeof gads_dash_channels == "function") { 
  325. gads_dash_channels(); 
  326. if(typeof gads_dash_ads == "function") { 
  327. gads_dash_ads(); 
  328. }  
  329.  
  330. function adsense_drawChart1() { 
  331. var data = google.visualization.arrayToDataTable([' . " 
  332. ['Date', '" . $title . "'], " . $gads_chart1_data . "  
  333. ]); 
  334.  
  335. var options = { 
  336. legend: {position: 'none'},  
  337. pointSize: 3, ".$css." 
  338. title: '".$title."',  
  339. chartArea: {width: '99%', height: '90%'},  
  340. vAxis: { textPosition: 'in', minValue: 0},  
  341. hAxis: { textPosition: 'none' } 
  342. }; 
  343.  
  344. var chart = new google.visualization.AreaChart(document.getElementById('adsense_chart1_div')); 
  345. chart.draw(data, options); 
  346.  
  347. }"; 
  348.  
  349. if (get_option ( 'gads_dash_channels' )) { 
  350.  
  351. if ($gads_ch_data) { 
  352. $code .= ' 
  353. google.load("visualization", "1", {packages:["table"]}) 
  354. function gads_dash_channels() { 
  355. var data = google.visualization.arrayToDataTable([' . " 
  356. ['" . __ ( "Channel", 'gads-dash' ) . "', '" . __ ( "Earnings", 'gads-dash' ) . "', '" . __ ( "CPC", 'gads-dash' ) . "', '" . __ ( "Clicks", 'gads-dash' ) . "', '" . __ ( "Views", 'gads-dash' ) . "', '" . __ ( "CTR", 'gads-dash' ) . "', '" . __ ( "RPM", 'gads-dash' ) . "'], " . $gads_ch_data . "  
  357. ]); 
  358.  
  359. var options = { 
  360. page: 'enable',  
  361. pageSize: 5,  
  362. width: '100%' 
  363. };  
  364.  
  365. var chart = new google.visualization.Table(document.getElementById('gads_dash_channels')); 
  366. chart.draw(data, options); 
  367.  
  368. }"; 
  369.  
  370. if (get_option ( 'gads_dash_ads' )) { 
  371.  
  372. if ($gads_ads_data) { 
  373. $code .= ' 
  374. google.load("visualization", "1", {packages:["table"]}) 
  375. function gads_dash_ads() { 
  376. var data = google.visualization.arrayToDataTable([' . " 
  377. ['" . __ ( "Ad Slot", 'gads-dash' ) . "', '" . __ ( "Earnings", 'gads-dash' ) . "', '" . __ ( "CPC", 'gads-dash' ) . "', '" . __ ( "Clicks", 'gads-dash' ) . "', '" . __ ( "Requests", 'gads-dash' ) . "', '" . __ ( "CTR", 'gads-dash' ) . "', '" . __ ( "RPM", 'gads-dash' ) . "'], " . $gads_ads_data . "  
  378. ]); 
  379.  
  380. var options = { 
  381. page: 'enable',  
  382. pageSize: 5,  
  383. width: '100%' 
  384. };  
  385.  
  386. var chart = new google.visualization.Table(document.getElementById('gads_dash_ads')); 
  387. chart.draw(data, options); 
  388.  
  389. }"; 
  390. $btn_class = (get_option ( 'gads_dash_style' ) == 'green') ? 'gadsbutton' : 'button button-secondary'; 
  391. $code .= "</script>" . ' 
  392. <div id="gads-dash"> 
  393. <center> 
  394. <div id="buttons_div_adsense"> 
  395. <center> 
  396. <input class="' . $btn_class . '" type="button" value="' . __ ( "Today", 'gads-dash' ) . '" onClick="window.location=\'?period_adsense=today&query_adsense=' . $query_adsense . '\'" /> 
  397. <input class="' . $btn_class . '" type="button" value="' . __ ( "Yesterday", 'gads-dash' ) . '" onClick="window.location=\'?period_adsense=yesterday&query_adsense=' . $query_adsense . '\'" /> 
  398. <input class="' . $btn_class . '" type="button" value="' . __ ( "7 Days", 'gads-dash' ) . '" onClick="window.location=\'?period_adsense=last7days&query_adsense=' . $query_adsense . '\'" /> 
  399. <input class="' . $btn_class . '" type="button" value="' . __ ( "14 Days", 'gads-dash' ) . '" onClick="window.location=\'?period_adsense=last14days&query_adsense=' . $query_adsense . '\'" /> 
  400. <input class="' . $btn_class . '" type="button" value="' . __ ( "30 Days", 'gads-dash' ) . '" onClick="window.location=\'?period_adsense=last30days&query_adsense=' . $query_adsense . '\'" /> 
  401. </center> 
  402. </div> 
  403.  
  404. <div id="adsense_chart1_div"></div> 
  405.  
  406. <div id="adsense_details_div"> 
  407. <table class="adsensetable" cellpadding="4"> 
  408. <tr> 
  409. <td width=="24%">' . __ ( "Earnings:", 'gads-dash' ) . '</td> 
  410. <td width="12%" class="adsensevalue"><a href="?query_adsense=EARNINGS&period_adsense=' . $period_adsense . '" class="adsensetable">' . (float) $gads_sum_data ['rows'] [0] [0] . '</td> 
  411. <td width="30%">' . __ ( "Views:", 'gads-dash' ) . '</td> 
  412. <td width="12%" class="adsensevalue"><a href="?query_adsense=PAGE_VIEWS&period_adsense=' . $period_adsense . '" class="adsensetable">' . (int) $gads_sum_data ['rows'] [0] [3] . '</a></td> 
  413. <td width="24%">' . __ ( "Clicks:", 'gads-dash' ) . '</td> 
  414. <td width="12%" class="adsensevalue"><a href="?query_adsense=CLICKS&period_adsense=' . $period_adsense . '" class="adsensetable">' . (int) $gads_sum_data ['rows'] [0] [2] . '</a></td> 
  415. </tr> 
  416. <tr> 
  417. <td>' . __ ( "CPC:", 'gads-dash' ) . '</td> 
  418. <td class="adsensevalue"><a href="?query_adsense=COST_PER_CLICK&period_adsense=' . $period_adsense . '" class="adsensetable">' . (float) $gads_sum_data ['rows'] [0] [1] . '</a></td> 
  419. <td>' . __ ( "CTR:", 'gads-dash' ) . '</td> 
  420. <td class="adsensevalue"><a href="?query_adsense=PAGE_VIEWS_CTR&period_adsense=' . $period_adsense . '" class="adsensetable">' . (float) ($gads_sum_data ['rows'] [0] [4] * 100) . '</a></td> 
  421. <td>' . __ ( "RPM:", 'gads-dash' ) . '</td> 
  422. <td class="adsensevalue"><a href="?query_adsense=PAGE_VIEWS_RPM&period_adsense=' . $period_adsense . '" class="adsensetable">' . (float) $gads_sum_data ['rows'] [0] [5] . '</a></td> 
  423. </tr> 
  424. </table> 
  425. </div> 
  426. </center> 
  427. </div>'; 
  428.  
  429. if (get_option ( 'gads_dash_channels' )) 
  430. $code .= '<br /><br /><div id="gads_dash_channels"></div>'; 
  431.  
  432. if (get_option ( 'gads_dash_ads' )) 
  433. $code .= '<br /><div id="gads_dash_ads"></div>'; 
  434.  
  435. echo $code; 
  436. ?> 
.