KnowMyRankingsAdmin

The KnowMyRankings KnowMyRankingsAdmin class.

Defined (1)

The class is defined in the following location(s).

/includes/KnowMyRankingsAdmin.php  
  1. class KnowMyRankingsAdmin { 
  2.  
  3. public $options_name = "know_my_rankings_options"; 
  4. public $page_slug = "know_my_rankings"; 
  5. public $user; 
  6.  
  7. private $kmr; 
  8. private $burst_kmr; 
  9. private $burst_api_key = "B-Ct0OplaKRv-s3dbAs6Ig"; 
  10.  
  11. function __construct() { 
  12.  
  13. // Get current user options 
  14. $this->user = $this->get_user_options(); 
  15.  
  16. // Check for API key and create KMR instance 
  17. if ( $this->user['api_key'] ) 
  18. $this->kmr = new KnowMyRankings( $this->user['api_key'] ); 
  19. else 
  20. $this->kmr = new KnowMyRankings(''); 
  21.  
  22. // Create burst instance 
  23. $this->burst_kmr = new KnowMyRankings( $this->burst_api_key ); 
  24.  
  25. // Create notices instance 
  26. $this->notices = new KnowMyRankingsNotices(); 
  27.  
  28. // Add actions 
  29. $this->add_actions(); 
  30.  
  31. // Activation hook 
  32. register_activation_hook( 'knowmyrankings-wp-plugin/know-my-rankings.php', array( $this, 'kmr_plugin_activate' ) ); 
  33.  
  34.  
  35. /** 
  36. * Add WordPress action hooks 
  37. *  
  38. */ 
  39.  
  40. function add_actions() { 
  41.  
  42. // Register plugin menu page 
  43. add_action( 'admin_menu', array( $this, 'add_plugin_page' ) ); 
  44. // Run on admin init 
  45. add_action( 'admin_init', array( $this, 'admin_init' ) ); 
  46.  
  47. // Register dashboard widget with the 'wp_dashboard_setup' action 
  48. add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) ); 
  49.  
  50. // AJAX 
  51. add_action( 'wp_ajax_kmr_callback', array( $this, 'kmr_ajax_callback' ) ); 
  52.  
  53.  
  54. /** 
  55. * Plugin init 
  56. *  
  57. */ 
  58.  
  59. function admin_init() { 
  60.  
  61. wp_enqueue_style( 'know-my-rankings', plugins_url( 'css/know-my-rankings.css', __FILE__ ) ); 
  62. wp_enqueue_script( 'know-my-rankings', plugins_url( 'js/know-my-rankings.js', __FILE__ ), array('jquery') ); 
  63.  
  64. // jQuery confirm 
  65. wp_enqueue_script( 'jquery-confirm', plugins_url( 'js/jquery.confirm.min.js', __FILE__ ), array('jquery') ); 
  66.  
  67. // Bootstrap for modal 
  68. wp_enqueue_style( 'kmr-bootstrap', plugins_url( 'bootstrap/css/bootstrap.min.css', __FILE__ ) ); 
  69. wp_enqueue_script( 'kmr-bootstrap', plugins_url( 'bootstrap/js/bootstrap.min.js', __FILE__ ), array('jquery') ); 
  70.  
  71. // Check if "Add new report" form submitted 
  72. $this->add_new_action(); 
  73.  
  74. // Check if user want to delte URL or keyword 
  75. $this->delete_action(); 
  76.  
  77. // Redirect if plugin activeted 
  78. if ( get_option( 'kmr_plugin_do_activation_redirect', false ) ) { 
  79.  
  80. delete_option('kmr_plugin_do_activation_redirect'); 
  81.  
  82. if ( ! isset( $_GET['activate-multi'] ) ) { 
  83. // Redirect to Settings page 
  84. wp_redirect( $this->get_url('settings') ); 
  85. exit(); 
  86.  
  87.  
  88. /** 
  89. * Activation hook 
  90. *  
  91. */ 
  92.  
  93. function kmr_plugin_activate() {  
  94. // Option to redirect on admin_init 
  95. add_option('kmr_plugin_do_activation_redirect', true); 
  96.  
  97.  
  98. /** 
  99. * Add all plugin's pages 
  100. *  
  101. */ 
  102.  
  103. public function add_plugin_page() { 
  104.  
  105. // Main menu page 
  106. add_menu_page( 
  107. 'KnowMyRankings Settings', // page Title 
  108. 'KnowMyRankings', // menu item name 
  109. 'manage_options',  
  110. $this->page_slug,  
  111. array( $this, 'reports_page' ),  
  112. plugins_url( 'img/icon.png', __FILE__ ) 
  113. ); 
  114.  
  115. // Reports Page 
  116. add_submenu_page(  
  117. $this->page_slug,  
  118. 'View Reports',  
  119. 'View Reports',  
  120. 'manage_options',  
  121. $this->page_slug 
  122. ); 
  123.  
  124. // Add new Page 
  125. add_submenu_page(  
  126. $this->page_slug,  
  127. 'New Report',  
  128. 'New Report',  
  129. 'manage_options',  
  130. $this->page_slug . '_add_new',  
  131. array( $this, 'add_new_page' ) 
  132. ); 
  133.  
  134. // Settings Page 
  135. add_submenu_page(  
  136. $this->page_slug,  
  137. 'Settings',  
  138. 'Settings',  
  139. 'manage_options',  
  140. $this->page_slug . '_settings',  
  141. array( $this, 'settings_page' ) 
  142. ); 
  143.  
  144.  
  145. /** 
  146. * Settings page callback 
  147. */ 
  148.  
  149. public function settings_page() { 
  150.  
  151. // DEBUG 
  152. //$this->set_user_options( array() ); 
  153. //var_dump($this->user); 
  154.  
  155. /** 
  156. * If user settings form submit 
  157. *  
  158. */ 
  159.  
  160. if ( isset( $_POST['update_user'] ) || wp_verify_nonce( $_POST['update_user'], 'settings' ) ) { 
  161.  
  162. // $_POST validation here 
  163. $user = array ( 
  164. 'email' => $_POST['email'],  
  165. 'password' => $_POST['password'] 
  166. ); 
  167.  
  168. // Get API key 
  169. $response = $this->kmr->retrieve_api_key( $user ); 
  170.  
  171. if ( $response['status'] == "OK" ) { 
  172.  
  173. // Update KMR with recent api_key 
  174. $this->kmr = new KnowMyRankings( $response['body']['api_key'] ); 
  175.  
  176. // Get current reports for widget dropdown 
  177. $urls = $this->kmr->get_urls(); 
  178.  
  179. if ( $urls['status'] == "OK" ) { 
  180.  
  181. $first_url = $urls['body']['urls'][0]; 
  182. $default_report_id = $first_url['id']; 
  183.  
  184. $user = array ( 
  185. 'api_key' => $response['body']['api_key'],  
  186. 'default_report_id' => $default_report_id 
  187. ); 
  188.  
  189. $this->set_user_options( $user ); 
  190. $this->user = $user; 
  191. }  
  192.  
  193. require_once('views/settings.view.php'); 
  194.  
  195. return; 
  196.  
  197. /** 
  198. * If widget id form submit 
  199. *  
  200. */ 
  201.  
  202. if ( isset( $_POST['update_widget'] ) || wp_verify_nonce( $_POST['update_widget'], 'settings' ) ) { 
  203.  
  204. // $_POST validation here 
  205. $default_report_id = $_POST['default_report']; 
  206.  
  207. // Update user 
  208. $user = $this->user; 
  209. $user['default_report_id'] = $default_report_id; 
  210.  
  211. $this->set_user_options( $user ); 
  212. $this->user = $user; 
  213.  
  214. require_once('views/settings.view.php'); 
  215.  
  216. return; 
  217.  
  218. /** 
  219. * If user submit Step 1 
  220. *  
  221. */ 
  222.  
  223. if ( isset( $_POST['step_one_nonce'] ) || wp_verify_nonce( $_POST['step_one_nonce'], 'setup' ) ) { 
  224.  
  225.  
  226. $this->burst_kmr = new KnowMyRankings( $this->burst_api_key ); 
  227.  
  228. // Simple POST validation 
  229. if ( ! isset( $_POST['url'] ) || empty( $_POST['url'] ) ) 
  230. return; 
  231.  
  232. if ( ! isset( $_POST['keywords'] ) || empty( $_POST['keywords'] ) ) 
  233. return; 
  234.  
  235. $url = $_POST['url']; 
  236. $keywords = $_POST['keywords']; 
  237. $keywords = str_replace(", ", "", $keywords); 
  238. $keywords = str_replace("\r\n", ", ", $keywords); 
  239.  
  240. $args = array ( 
  241. 'url' => $url,  
  242. 'keywords' => $keywords 
  243. ); 
  244.  
  245. // Add URL to burst account to show user 
  246. $response = $this->burst_kmr->add_url( $args ); 
  247.  
  248. // Try to delete this URL 
  249. if ( $response['status'] == "OK" ) { 
  250. $id = $response['body']['id']; 
  251. $this->burst_kmr->delete_url( $id ); 
  252.  
  253. require_once('views/setup.two.view.php'); 
  254.  
  255. return; 
  256.  
  257.  
  258. /** 
  259. * If user submit Step 2 
  260. *  
  261. */ 
  262.  
  263. if ( isset( $_POST['step_two_nonce'] ) || wp_verify_nonce( $_POST['step_two_nonce'], 'setup' ) ) { 
  264.  
  265. // $_POST validation here 
  266. $user = array ( 
  267. 'email' => $_POST['email'],  
  268. 'password' => $_POST['password'] 
  269. ); 
  270.  
  271. // If user already have an accaunt 
  272. if ( isset( $_POST['have_account'] ) && $_POST['have_account'] == 'true' ) { 
  273.  
  274. // Get API key 
  275. $response = $this->kmr->retrieve_api_key( $user ); 
  276. // If user create new accaunt 
  277. else { 
  278. // POST Validation 
  279. if ( $_POST['password'] != $_POST['password_confirm'] ) { 
  280.  
  281. $response = array(  
  282. 'status' => "ERR",  
  283. 'body' => "Passwords mismatch" 
  284. ); 
  285. // Show view 
  286. require_once('views/setup.two.view.php'); 
  287. return; 
  288.  
  289. // Try to create user 
  290. $response = $this->kmr->create_user( $user ); 
  291.  
  292. // Check response 
  293. if ( $response['status'] == "OK" ) { 
  294.  
  295. // Update KMR with recent api_key 
  296. $this->kmr = new KnowMyRankings( $response['body']['api_key'] ); 
  297.  
  298. // Get report info from previous step 
  299. $url = $_POST['url']; 
  300. $keywords = $_POST['keywords']; 
  301.  
  302. $args = array ( 
  303. 'url' => $url,  
  304. 'keywords' => $keywords 
  305. ); 
  306.  
  307. // Add report from Step 1 to current user 
  308. $response_new_url = $this->kmr->add_url( $args ); 
  309.  
  310. // Show notice if URL not added 
  311. if ( $response_new_url['status'] == "ERR" ) 
  312. add_action( 'kmr_settings_notice', array( $this->notices, 'add_url_error' ) ); 
  313. else 
  314. $default_report_id = $response_new_url['body']['id']; 
  315.  
  316. // Update user info in DB and instance 
  317. $user = array ( 
  318. 'api_key' => $response['body']['api_key'],  
  319. 'default_report_id' => $default_report_id 
  320. ); 
  321.  
  322. $this->set_user_options( $user ); 
  323. $this->user = $user; 
  324.  
  325. add_action( 'kmr_settings_notice', array( $this->notices, 'installation_success' ) ); 
  326.  
  327. // Show settings view 
  328. require_once('views/setup.final.view.php'); 
  329. }  
  330. else { 
  331. // Show same view 
  332. require_once('views/setup.two.view.php'); 
  333.  
  334. return; 
  335.  
  336. /** 
  337. * If we have no user saved show Setup step 1 
  338. *  
  339. */ 
  340.  
  341. if ( ! isset ( $this->user["api_key"] ) ) { 
  342.  
  343. require_once('views/setup.one.view.php');  
  344. return; 
  345.  
  346. /** 
  347. * If we have user saved show settings screen 
  348. *  
  349. */ 
  350.  
  351. else { 
  352.  
  353. require_once('views/settings.view.php'); 
  354.  
  355.  
  356. /** 
  357. * Reports page callback 
  358. */ 
  359.  
  360. public function reports_page() { 
  361.  
  362. /** 
  363. * If we have no user saved show Setup step 1 
  364. *  
  365. */ 
  366.  
  367. if ( ! isset ( $this->user["api_key"] ) ) { 
  368.  
  369. require_once('views/setup.one.view.php'); 
  370. return; 
  371.  
  372.  
  373. /** 
  374. * Show certain report 
  375. *  
  376. */ 
  377.  
  378. if ( isset( $_GET['report'] ) ) { 
  379.  
  380. // Send request 
  381. $response = $this->kmr->get_url( intval ( $_GET['report'] ) ); 
  382.  
  383. if ( $response["status"] == "OK" ) { 
  384.  
  385. // Check for sorting 
  386. if ( isset( $_GET['orderby'] ) ) { 
  387.  
  388. $keywords = $response['body']['keywords']; 
  389. $orderby = $_GET['orderby']; 
  390. $sortable = array(); 
  391.  
  392. if ( $_GET['order'] == "desc" ) 
  393. $order = SORT_DESC; 
  394. else  
  395. $order = SORT_ASC; 
  396.  
  397. // Get keys 
  398. foreach ( $keywords as $key => $keyword ) { 
  399. $sortable[$key] = $keyword[$orderby]; 
  400.  
  401. // Sort 
  402. if ( $orderby == "kw" ) { 
  403.  
  404. array_multisort( $sortable, $order, SORT_STRING, $keywords ); 
  405. else { 
  406. array_multisort( $sortable, $order, $keywords ); 
  407.  
  408. // Move 0 values to the end if ASC or to front if DESC 
  409. foreach ( $keywords as $key => $keyword ) { 
  410. if ( $keyword['current_rank'] == 0 ) { 
  411.  
  412. $zero_element = $keywords[$key]; 
  413. unset( $keywords[$key] ); 
  414.  
  415. if ( $_GET['order'] == "asc" ) 
  416. $keywords[] = $zero_element; 
  417. else 
  418. array_unshift( $keywords, $zero_element ); 
  419. }; 
  420.  
  421. $response['body']['keywords'] = $keywords; 
  422.  
  423. //var_dump($response); 
  424.  
  425. require_once('views/edit.report.view.php'); 
  426.  
  427. /** 
  428. * Show all reports list 
  429. *  
  430. */ 
  431.  
  432. else { 
  433. // Send request 
  434. $response = $this->kmr->get_urls(); 
  435.  
  436. if ( $response['status'] == "OK" ) { 
  437. // get urls 
  438. $urls = $response['body']['urls']; 
  439.  
  440. // paginate 
  441. $per_page = 10; 
  442. $total_urls = sizeof( $urls ); 
  443. $total_pages = ceil ( $total_urls / $per_page ); 
  444.  
  445. $paged = ( isset( $_GET['paged'] ) ) ? $_GET['paged'] : 1; 
  446. $paged = ( $total_pages >= $paged ) ? $paged : 1; 
  447. $paged = ( $paged > 1 ) ? $paged : 1; 
  448.  
  449. $offset = $per_page * ( $paged - 1 ); 
  450.  
  451. // slice only current page 
  452. $urls = array_slice( $urls, $offset, $per_page ); 
  453.  
  454. require_once('views/all.reports.view.php'); 
  455.  
  456.  
  457. /** 
  458. * Send a request to add new URL or new keywrods when form submitted 
  459. */ 
  460.  
  461. public function add_new_action() { 
  462.  
  463. if ( isset( $_POST['add_new_report'] ) && wp_verify_nonce( $_POST['add_new_report'], 'reports' ) ) { 
  464.  
  465. // Simple POST validation 
  466. if ( ! isset( $_POST['url'] ) || empty( $_POST['url'] ) ) 
  467. return; 
  468.  
  469. if ( ! isset( $_POST['keywords'] ) || empty( $_POST['keywords'] ) ) 
  470. return; 
  471.  
  472. $url = $_POST['url']; 
  473. $keywords = $_POST['keywords']; 
  474. $keywords = str_replace(", ", "", $keywords); 
  475. $keywords = str_replace("\r\n", ", ", $keywords); 
  476.  
  477. $args = array ( 
  478. 'url' => $url,  
  479. 'keywords' => $keywords 
  480. ); 
  481.  
  482. $response = $this->kmr->add_url( $args ); 
  483.  
  484. if ( $response['status'] != "ERR" ) { 
  485.  
  486. $id = $response['body']['id']; 
  487.  
  488. wp_redirect( $this->get_url( "view_report", array ( "report" => $id ) ) ); 
  489. exit; 
  490. else  
  491. add_action( 'kmr_add_new_view_notice', array( $this->notices, 'add_url_error' ) ); 
  492.  
  493.  
  494. /** 
  495. * Send a request to delete URL or keywrod 
  496. */ 
  497.  
  498. public function delete_action() { 
  499.  
  500. if ( isset( $_GET['delete_url'] ) ) { 
  501.  
  502. $id = $_GET['delete_url']; 
  503.  
  504. $response = $this->kmr->delete_url( $id ); 
  505.  
  506. if ( $response['status'] == "ERR" ) { 
  507.  
  508. add_action( 'kmr_all_reports_notice', array( $this->notices, 'delete_url_error' ) ); 
  509.  
  510. if ( isset( $_GET['delete_keyword'] ) ) { 
  511.  
  512. $id = $_GET['delete_keyword']; 
  513.  
  514. $response = $this->kmr->delete_keyword( $id ); 
  515.  
  516. if ( $response['status'] == "ERR" ) { 
  517.  
  518. add_action( 'kmr_edit_report_notice', array( $this->notices, 'delete_keyword_error' ) ); 
  519.  
  520.  
  521. /** 
  522. * Add new page callback 
  523. */ 
  524.  
  525. public function add_new_page() { 
  526.  
  527. /** 
  528. * If we have no user saved show Setup step 1 
  529. *  
  530. */ 
  531.  
  532. if ( ! isset ( $this->user["api_key"] ) ) { 
  533. require_once('views/setup.one.view.php'); 
  534. return; 
  535.  
  536. // Simple POST validation 
  537. $validation = ""; 
  538.  
  539. if ( isset( $_POST['add_new_report'] ) && wp_verify_nonce( $_POST['add_new_report'], 'reports' ) ) { 
  540.  
  541. if ( ! isset( $_POST['url'] ) || empty( $_POST['url'] ) ) 
  542. $validation .= __( "URL cannot be empty.</br>", "know-my-rankings" ); 
  543.  
  544. if ( ! isset( $_POST['keywords'] ) || empty( $_POST['keywords'] ) ) 
  545. $validation .= __( "Keywords cannot be empty.", "know-my-rankings" ); 
  546.  
  547. // Show Add new screen 
  548. require_once('views/add.new.view.php'); 
  549.  
  550.  
  551.  
  552. /** 
  553. * Show rank report table based response 
  554. */ 
  555.  
  556. public function show_report( $response, $action = "" ) { 
  557.  
  558. require_once('views/report.view.php'); 
  559.  
  560.  
  561. /** 
  562. * Show rank report table based response 
  563. */ 
  564.  
  565. public function show_keyword( $keyword, $url_id ) { 
  566.  
  567. require('views/keyword.view.php'); 
  568.  
  569.  
  570.  
  571. /** 
  572. * Add WP dashboard widget 
  573. */ 
  574.  
  575. public function add_dashboard_widget() { 
  576.  
  577. // Get default URL for title 
  578. $default_report_id = $this->user['default_report_id']; 
  579.  
  580. if ( $default_report_id ) { 
  581. $response = $this->kmr->get_url( $default_report_id ); 
  582.  
  583. if ( $response['status'] == "OK" ) 
  584. $report_url = " — " . $response['body']['url']; 
  585. }  
  586.  
  587. wp_add_dashboard_widget(  
  588. 'know-my-rankings-widget',  
  589. __( 'KnowMyRankings.com', "know-my-rankings" ) . $report_url,  
  590. array( $this, 'show_dashboard_widget' )  
  591. ); 
  592.  
  593.  
  594.  
  595. /** 
  596. * Show WP dashboard widget 
  597. */ 
  598.  
  599. public function show_dashboard_widget() { 
  600.  
  601. $default_report_id = $this->user['default_report_id']; 
  602.  
  603. if ( $default_report_id ) { 
  604.  
  605. $response = $this->kmr->get_url( $default_report_id ); 
  606.  
  607. if ( $response['status'] == "OK" ) { 
  608.  
  609. $url_id = $response['body']['id']; 
  610.  
  611. $this->show_report( $response ); 
  612.  
  613. require_once('views/dashboard.widget.view.php'); 
  614. }  
  615. else { 
  616.  
  617. echo "<p style=\"padding: 0 1em 1em; \"><a href=\"" . $this->get_url( "settings" ) . "\">" . __( "Setup Widget on Settings Page", "know-my-rankings" ) . "</a></p>"; 
  618.  
  619.  
  620.  
  621. /** 
  622. * Get user options 
  623. *  
  624. */ 
  625.  
  626. public function get_user_options() { 
  627.  
  628. $options = get_option( $this->options_name ); 
  629.  
  630. return $options; 
  631.  
  632.  
  633. /** 
  634. * Set user options 
  635. *  
  636. */ 
  637.  
  638. public function set_user_options( $options ) { 
  639.  
  640. update_option( $this->options_name, $options ); 
  641.  
  642.  
  643. /** 
  644. * Get dashboard page URL 
  645. */ 
  646.  
  647. public function get_url( $page, $args = null ) { 
  648.  
  649. switch ( $page ) { 
  650.  
  651. case "add_new" : 
  652. $url = admin_url( "admin.php?page=" . $this->page_slug . "_add_new" ); 
  653. break; 
  654.  
  655. case "settings" : 
  656. $url = admin_url( "admin.php?page=" . $this->page_slug . "_settings" ); 
  657. break; 
  658.  
  659. case "view_report" : 
  660. $url = admin_url( "admin.php?page=" . $this->page_slug ); 
  661. break; 
  662.  
  663. case "all_reports" : 
  664. $url = admin_url( "admin.php?page=" . $this->page_slug ); 
  665. break; 
  666.  
  667.  
  668. // Add URL parameters if set 
  669. if ( is_array( $args ) ) { 
  670. foreach ( $args as $key => $value ) { 
  671. $url .= "&$key=$value"; 
  672.  
  673. return $url; 
  674.  
  675.  
  676. /** 
  677. * Get specific KMR page URL 
  678. *  
  679. */ 
  680.  
  681. public function get_kmr_url( $page, $param = "" ) { 
  682.  
  683. $base_url = "http://knowmyrankings.com/"; 
  684.  
  685. switch ( $page ) { 
  686.  
  687. case "keywords" : 
  688. $url = $base_url . "keywords/" . intval( $param ); 
  689. break; 
  690.  
  691. case "view_url" : 
  692. $url = $base_url . "urls/" . intval( $param ); 
  693. break; 
  694.  
  695. case "dashboard" : 
  696. $url = $base_url . "dashboard"; 
  697. break; 
  698.  
  699. case "upgrade" : 
  700. $url = $base_url . "upgrade"; 
  701. break; 
  702.  
  703. default : 
  704. $url = $base_url; 
  705.  
  706. // Add api_key to immediately login user 
  707. $url .= "?api_key=" . $this->user['api_key']; 
  708.  
  709. return $url; 
  710.  
  711.  
  712. /** 
  713. * Get rank classes: color, fill etc 
  714. *  
  715. */ 
  716.  
  717. public function get_rank_classes( $rank ) { 
  718.  
  719. $classes = array(); 
  720.  
  721. if ( $rank == 1 ) { 
  722. $classes[] = "green"; 
  723. $classes[] = "fill"; 
  724. if ( $rank > 1 && $rank <= 10 ) { 
  725. $classes[] = "green"; 
  726. if ( $rank > 10 && $rank <= 20 ) { 
  727. $classes[] = "yellow"; 
  728. if ( $rank > 20 && $rank <= 100 ) { 
  729. $classes[] = "red"; 
  730.  
  731. if ( ! $rank ) { 
  732. $classes[] = "gray"; 
  733. $classes[] = "fill"; 
  734.  
  735. return implode( " ", $classes ); 
  736.  
  737.  
  738. /** 
  739. * Get rank value: number, 100+ or 'no data' 
  740. *  
  741. */ 
  742.  
  743. public function get_rank_value( $rank ) { 
  744.  
  745. if ( $rank === 0 ) 
  746. echo '100+'; 
  747.  
  748. if ( is_null( $rank ) ) 
  749. echo "<span class=\"no-data\">" . __( "no data", "know-my-rankings" ) . "</span>"; 
  750.  
  751. if ( is_numeric( $rank ) && $rank > 0 ) 
  752. echo $rank; 
  753.  
  754.  
  755. /** 
  756. * Echo rank classes: color, fill etc 
  757. *  
  758. */ 
  759.  
  760. public function position( $keyword ) { 
  761.  
  762. if ( isset ( $keyword['last_7day'] ) && $keyword['last_7day'] != 0 ) { 
  763.  
  764. $position = $keyword['last_7day'] - $keyword['current_rank']; 
  765.  
  766. if ( $position > 0 ) { 
  767. $class = "up"; 
  768. $label = "Up $position"; 
  769. if ( $position < 0 ) { 
  770. $class = "down"; 
  771. $label = "Down " . abs( $position ); 
  772. if ( $position == 0 ) { 
  773. $label = "No change"; 
  774.  
  775. echo "<div class=\"position $class\"> 
  776. <span class=\"triangle\"></span> 
  777. <span class=\"desc\">$label</span> " 
  778. .__( "since last week", "know-my-rankings" ). 
  779. "</div>"; 
  780. else 
  781. echo "<div class=\"position\"> 
  782. <span class=\"desc\">$label</span> </div>"; 
  783.  
  784. /** 
  785. * Output pagination 
  786. *  
  787. */ 
  788.  
  789. public function pagination( $total_urls, $per_page, $paged ) {  
  790.  
  791. $total_pages = ceil ( $total_urls / $per_page ); 
  792.  
  793. // Check if last page 
  794. if ( $paged == $total_pages ) { 
  795.  
  796. $next_disabled = "disabled"; 
  797. $next_page = $paged; 
  798. else 
  799. $next_page = $paged + 1; 
  800.  
  801. // Check if first page 
  802. if ( $paged == 1 ) { 
  803.  
  804. $previous_disabled = "disabled"; 
  805. $previous_page = $paged; 
  806. else 
  807. $previous_page = $paged - 1; 
  808.  
  809. ?> 
  810. <form method="GET" action="<?php echo $this->get_url( "all_reports" ); ?>"> 
  811. <input type="hidden" name="page" value="know_my_rankings"> 
  812. <div class="tablenav-pages"> 
  813. <span class="displaying-num"><?php printf( __( '%d items', 'know-my-rankings' ), $total_urls ); ?></span> 
  814. <span class="pagination-links"> 
  815. <a class="first-page <?php echo $previous_disabled; ?>"  
  816. title="<?php _e('Go to the first page', 'know-my-rankings'); ?>"  
  817. href="<?php echo $this->get_url( "all_reports" ); ?>"></a> 
  818. <a class="prev-page <?php echo $previous_disabled; ?>"  
  819. title="<?php _e('Go to the previous page', 'know-my-rankings'); ?>"  
  820. href="<?php echo $this->get_url( "all_reports", array( 'paged' => $previous_page ) ); ?>">*</a> 
  821. <span class="paging-input"> 
  822. <input class="current-page" title="<?php _e( 'Current page', 'know-my-rankings' ); ?>" type="text" name="paged" value="<?php echo $paged; ?>" size="1">  
  823. <?php _e( 'of', 'know-my-rankings' ); ?> <span class="total-pages"><?php echo $total_pages; ?></span> 
  824. </span> 
  825. <a class="next-page <?php echo $next_disabled; ?>"  
  826. title="<?php _e('Go to the next page', 'know-my-rankings'); ?>"  
  827. href="<?php echo $this->get_url( "all_reports", array( 'paged' => $next_page ) );?>">*</a> 
  828. <a class="last-page <?php echo $next_disabled; ?>"  
  829. title="<?php _e('Go to the last page', 'know-my-rankings'); ?>"  
  830. href="<?php echo $this->get_url( "all_reports", array( 'paged' => $total_pages ) );?>"></a> 
  831. </span> 
  832. </div> 
  833. </form> 
  834. <?php } 
  835.  
  836. /** 
  837. * AJAX handler 
  838. *  
  839. */ 
  840.  
  841. function kmr_ajax_callback() { 
  842.  
  843. global $wpdb; // this is how you get access to the database 
  844.  
  845. /** 
  846. * Price Check 
  847. */ 
  848.  
  849. if ( $_POST['kmr_action'] == 'check_price' ) { 
  850.  
  851. $args = array ( 
  852. 'url' => $_POST['url'],  
  853. 'keywords' => $_POST['keywords'] 
  854. ); 
  855.  
  856. $response = $this->kmr->check_price( $args ); 
  857.  
  858. if ( $response['status'] == 'OK' ) { 
  859.  
  860. $upgrade_url = ""; 
  861. $html = ""; 
  862. $plan = $response['body']; 
  863.  
  864. // Check if upgrade required 
  865. if ( $plan['upgrade_required'] ) { 
  866.  
  867. $html = __("<h2>You need to upgrade to track more than 10 keywords!</h2> 
  868. Your free KnowMyRankings.com account allows you to track up to 10 keywords. Upgrading your account to a monthly subscription will let you add <b>unlimited</b> keywords and reports.", "know-my-rankings"); 
  869. $upgrade_url = $this->get_kmr_url('upgrade'); 
  870. else { 
  871. $keywords = $plan['new_keywords']; 
  872. $keywords_to_add = sizeof( $keywords ); 
  873.  
  874. $current_price = $plan['current_price'] / 100; 
  875. $new_price = $plan['new_price'] / 100; 
  876. $price_increase = $new_price - $current_price; 
  877.  
  878. $current_total_keywords = $plan['current_total_keywords']; 
  879. $new_total_keywords = $plan['new_total_keywords']; 
  880.  
  881. ob_start(); 
  882. require_once('views/keywords.confirm.view.php');  
  883. $html = ob_get_clean(); 
  884.  
  885. //$html = print_r($plan, true); 
  886.  
  887. $json = array( 
  888. 'url' => $args['url'],  
  889. 'keywords' => $args['keywords'],  
  890. 'message' => $html,  
  891. 'upgrade_url' => $upgrade_url 
  892. ); 
  893.  
  894. echo json_encode( $json ); 
  895.  
  896. /** 
  897. * Add keywords 
  898. */ 
  899.  
  900. if ( $_POST['kmr_action'] == 'add_keywords' ) { 
  901.  
  902. $args = array ( 
  903. 'url' => $_POST['url'],  
  904. 'keywords' => $_POST['keywords'] 
  905. ); 
  906.  
  907. $response = $this->kmr->add_url( $args ); 
  908.  
  909. if ( $response['status'] == 'OK' ) { 
  910.  
  911. $this->show_report( $response ); 
  912. else { 
  913. echo $response['body']; 
  914.  
  915. /** 
  916. * Get keywords 
  917. */ 
  918.  
  919. if ( $_POST['kmr_action'] == 'get_keyword' ) { 
  920.  
  921. $keyword_id = $_POST['keyword_id']; 
  922. $url_id = $_POST['url_id']; 
  923. $update_action = $_POST['update_action']; 
  924.  
  925. if ( $update_action == 'burst' ) { 
  926. $response = $this->burst_kmr->get_keyword( $keyword_id ); 
  927. else 
  928. $response = $this->kmr->get_keyword( $keyword_id ); 
  929.  
  930. if ( $response['status'] == 'OK' ) { 
  931.  
  932. $is_loaded = false; 
  933. $i = 1; // requests iterator 
  934.  
  935. while ( ! $is_loaded && $i < 20 ) { 
  936.  
  937. $is_loaded = true; 
  938. $i++; 
  939.  
  940. sleep(2); 
  941.  
  942. // send request 
  943. if ( $update_action == 'burst' ) { 
  944. $response = $this->burst_kmr->get_keyword( $keyword_id ); 
  945. else 
  946. $response = $this->kmr->get_keyword( $keyword_id ); 
  947.  
  948. $keyword = $response['body']; 
  949.  
  950. if ( $keyword['current_rank'] === null ) 
  951. $is_loaded = false; 
  952.  
  953. $this->show_keyword( $keyword, $url_id ); 
  954.  
  955. else { 
  956. echo "<td colspan=\"6\"><b>"; 
  957. echo $response['body']; 
  958. echo "</b></td>"; 
  959.  
  960.  
  961. die(); // this is required to return a proper result 
  962.  
  963.