/inc/upgrades/upgrade-functions.php

  1. <?php 
  2. /** 
  3. * Handles Upgrade Functionality 
  4. * 
  5. * @description : This is the Reference ID that Place ID 
  6. * @copyright Copyright (c) 2016, WordImpress 
  7. * @since : 1.3 
  8. */ 
  9.  
  10. // Exit if accessed directly 
  11. if ( ! defined( 'ABSPATH' ) ) { 
  12. exit; 
  13.  
  14. /** 
  15. * Display Upgrade Notices 
  16. * 
  17. * @since 1.3 
  18. * @return void 
  19. */ 
  20. function gpr_show_upgrade_notices() { 
  21.  
  22. //Uncomment for testing ONLY - Never leave uncommented unless testing: 
  23. //delete_option( 'gpr_refid_upgraded' ); 
  24.  
  25. // Don't show notices on the upgrades page 
  26. if ( isset( $_GET['page'] ) && $_GET['page'] == 'gpr-upgrades' ) { 
  27. return; 
  28.  
  29. $gpr_widget_version = get_option( 'gpr_widget_version' ); 
  30.  
  31. if ( ! $gpr_widget_version ) { 
  32. // 1.3 is the first version to use this option so we must add it 
  33. $gpr_widget_version = '1.3'; 
  34.  
  35. $gpr_widget_version = preg_replace( '/[^0-9.].*/', '', $gpr_widget_version ); 
  36.  
  37. if ( version_compare( $gpr_widget_version, '1.3', '<=' ) && ! get_option( 'gpr_refid_upgraded' ) && is_active_widget( false, false, 'gpr_widget', true ) ) { 
  38. printf( 
  39. '<div class="updated"><p><strong>Google Places Reviews Notice:</strong> ' . esc_html__( 'Hey there! We noticed you have active Google Places Reviews widget(s). Google has updated their API to use the new Google Places ID rather than previous Reference ID, which will soon be deprecated and eventually go offline. We are being proactive and would like to update your widgets to use the new Places ID for you. Once you upgrade, your widgets should work just fine. If you choose not to upgrade Google will eventually take the old reference ID offline (no date has been given). Please contact %1sWordImpress support%2s if you have any further questions or issues. %3sClick here to upgrade your widgets to use the new Places ID%4s.', 'google-places-reviews' ) . '</p></div>',  
  40. '<a href="http://wordpress.org/support/plugin/google-places-reviews" target="_blank">',  
  41. '</a>',  
  42. '<br><br><strong><a href="' . esc_url( admin_url( 'options.php?page=gpr-upgrades' ) ) . '">',  
  43. '</a></strong>' 
  44. ); 
  45.  
  46. update_option( 'gpr_widget_version', GPR_VERSION ); 
  47.  
  48.  
  49.  
  50. add_action( 'admin_notices', 'gpr_show_upgrade_notices' ); 
  51.  
  52.  
  53. /** 
  54. * Creates the upgrade page 
  55. * 
  56. * links to global variables 
  57. * 
  58. * @since 1.3 
  59. */ 
  60. function gpr_add_upgrade_submenu_page() { 
  61.  
  62. $gpr_upgrades_screen = add_submenu_page( null, __( 'Google Places Reviews Upgrades', 'google-places-reviews' ), __( 'Google Places Reviews Upgrades', 'google-places-reviews' ), 'activate_plugins', 'gpr-upgrades', 'gpr_upgrades_screen' ); 
  63.  
  64.  
  65. add_action( 'admin_menu', 'gpr_add_upgrade_submenu_page', 10 ); 
  66. /** 
  67. * Triggers all upgrade functions 
  68. * 
  69. * This function is usually triggered via AJAX 
  70. * 
  71. * @since 1.3 
  72. * @return void 
  73. */ 
  74. function gpr_trigger_upgrades() { 
  75.  
  76. if ( ! current_user_can( 'activate_plugins' ) ) { 
  77. wp_die( __( 'You do not have permission to do plugin upgrades', 'google-places-reviews' ), __( 'Error', 'google-places-reviews' ), array( 'response' => 403 ) ); 
  78.  
  79. $gpr_widget_version = get_option( 'gpr_widget_version' ); 
  80.  
  81. if ( ! $gpr_widget_version ) { 
  82. // 1.3 is the first version to use this option so we must add it 
  83. $gpr_widget_version = '1.3'; 
  84. add_option( 'gpr_widget_version', $gpr_widget_version ); 
  85.  
  86. if ( version_compare( GPR_VERSION, $gpr_widget_version, '>=' ) && ! get_option( 'gpr_refid_upgraded' ) ) { 
  87. gpr_v13_upgrades(); 
  88.  
  89. update_option( 'gpr_widget_version', $gpr_widget_version ); 
  90.  
  91. if ( DOING_AJAX ) { 
  92. die( 'complete' ); 
  93. } // Let AJAX know that the upgrade is complete 
  94.  
  95. add_action( 'wp_ajax_gpr_trigger_upgrades', 'gpr_trigger_upgrades' ); 
  96.  
  97.  
  98. /** 
  99. * Upgrade from Google Reference ID to Places ID 
  100. * 
  101. * @since 1.3 
  102. * @uses WP_Query 
  103. * @return void 
  104. */ 
  105. function gpr_v13_upgrades() { 
  106.  
  107. //Upgrade the Reference ID 
  108. $gpr_widget_options = get_option( 'widget_gpr_widget' ); 
  109. $plugin_options = get_option( 'googleplacesreviews_options' ); 
  110. $google_api_key = $plugin_options['google_places_api_key']; 
  111.  
  112. //Loop through widgets' options 
  113. foreach ( $gpr_widget_options as $key => $widget ) { 
  114.  
  115. $ref_id = isset( $widget['reference'] ) ? $widget['reference'] : ''; 
  116. $place_id = isset( $widget['place_id'] ) ? $widget['place_id'] : ''; 
  117.  
  118. //If no place AND there's a ref ID proceed 
  119. if ( empty( $place_id ) && ! empty( $ref_id ) ) { 
  120.  
  121. //cURL the Google API for the Google Place ID 
  122. $google_places_url = add_query_arg( 
  123. array( 
  124. 'reference' => $ref_id,  
  125. 'key' => $google_api_key 
  126. ),  
  127. 'https://maps.googleapis.com/maps/api/place/details/json' 
  128. ); 
  129.  
  130. $response = wp_remote_get( $google_places_url,  
  131. array( 
  132. 'timeout' => 15,  
  133. 'sslverify' => false 
  134. ) ); 
  135.  
  136. // make sure the response came back okay 
  137. if ( is_wp_error( $response ) ) { 
  138. return; 
  139.  
  140. // decode the license data 
  141. $response = json_decode( $response['body'], true ); 
  142.  
  143. //Place ID is there, now let's update the widget data 
  144. if ( isset( $response['result']['place_id'] ) ) { 
  145.  
  146. //Add Place ID to GPR widgets options array 
  147. $gpr_widget_options[ $key ]['place_id'] = $response['result']['place_id']; 
  148.  
  149.  
  150.  
  151. //Pause for 3 seconds so we don't overwhelm the Google API with requests 
  152. sleep( 2 ); 
  153.  
  154. //Update our options and GTF out 
  155. update_option( 'gpr_refid_upgraded', 'upgraded' ); 
  156. update_option( 'widget_gpr_widget', $gpr_widget_options ); 
  157.  
.