version-checker.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /*
  3. * Vamtam CRM Integration, used to check for updates and aiding support queries
  4. */
  5. class Version_Checker {
  6. public $remote;
  7. public $interval;
  8. public $notice;
  9. private $update_api_prefix = 'https://updates.api.vamtam.com/0/envato/';
  10. private $update_api_url;
  11. private $validate_api_url;
  12. private static $instance;
  13. public static function get_instance() {
  14. if ( ! isset( self::$instance ) ) {
  15. self::$instance = new self();
  16. }
  17. return self::$instance;
  18. }
  19. public function __construct() {
  20. $this->remote = 'https://api.vamtam.com/version';
  21. $this->interval = 2 * 3600;
  22. $this->update_api_url = $this->update_api_prefix . 'check-theme';
  23. $this->validate_api_url = $this->update_api_prefix . 'validate-license';
  24. if ( ! isset( $_GET['import'] ) && ( ! isset( $_GET['step'] ) || (int) $_GET['step'] != 2 ) ) {
  25. add_action( 'admin_init', array( $this, 'check_version' ) );
  26. }
  27. add_action( 'wp_ajax_vamtam-check-license', array( $this, 'check_license' ) );
  28. add_action( 'vamtam_saved_options', array( $this, 'check_version' ) );
  29. // set_site_transient('update_themes', null);
  30. add_filter( 'pre_set_site_transient_update_themes', array( $this, 'check_update' ) );
  31. }
  32. public function check_update( $updates ) {
  33. // prevent conflicts with themes hosted on wp.org
  34. $theme_name = wp_get_theme()->get_template();
  35. if (
  36. isset( $updates->response ) &&
  37. isset( $updates->response[ $theme_name ] ) &&
  38. strpos( $updates->response[ $theme_name ]['package'], 'downloads.wordpress.org' ) !== false
  39. ) {
  40. unset( $updates->response[ $theme_name ] );
  41. }
  42. $response = $this->update_api_request( $updates );
  43. if ( false === $response ) {
  44. return $updates;
  45. }
  46. if ( ! isset( $updates->response ) ) {
  47. $updates->response = array();
  48. }
  49. $updates->response = array_merge( $updates->response, $response );
  50. // Small trick to ensure the updates get shown in the network admin
  51. if ( is_multisite() && ! is_main_site() ) {
  52. global $current_site;
  53. switch_to_blog( $current_site->blog_id );
  54. set_site_transient( 'update_themes', $updates );
  55. restore_current_blog();
  56. }
  57. return $updates;
  58. }
  59. private function update_api_request( $update_cache ) {
  60. global $wp_version;
  61. $theme_name = wp_get_theme()->get_template();
  62. $raw_response = wp_remote_post( $this->update_api_url, array(
  63. 'body' => array(
  64. 'version' => isset( $update_cache->checked[ $theme_name ] ) ? $update_cache->checked[ $theme_name ] : VamtamFramework::get_version(),
  65. 'purchase_key' => apply_filters( 'vamtam_purchase_code', '' ),
  66. ),
  67. 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
  68. ) );
  69. if ( is_wp_error( $raw_response ) || 200 !== wp_remote_retrieve_response_code( $raw_response ) ) {
  70. return false;
  71. }
  72. $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
  73. return $response['themes'];
  74. }
  75. public function check_license() {
  76. check_ajax_referer( 'vamtam-check-license', 'nonce' );
  77. global $wp_version;
  78. $raw_response = wp_remote_post( $this->validate_api_url, array(
  79. 'body' => array(
  80. 'purchase_key' => $_POST['license-key'],
  81. ),
  82. 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
  83. ) );
  84. if ( ! is_wp_error( $raw_response ) ) {
  85. if ( $raw_response['response']['code'] >= 200 && $raw_response['response']['code'] < 300 ) {
  86. echo '<span style="color: green">';
  87. esc_html_e( 'Valid Purchase Key', 'vamtam-consulting' );
  88. echo '</span>';
  89. } else {
  90. echo '<span style="color: red">';
  91. esc_html_e( 'Incorrect Purchase Key', 'vamtam-consulting' );
  92. echo '</span>';
  93. }
  94. } else {
  95. echo '<span style="color: red">';
  96. esc_html_e( 'Cannot validate Purchase Key. Please try again later. If the problem persists your server might not have the curl PHP extension enabled.', 'vamtam-consulting' );
  97. echo '</span>';
  98. }
  99. $this->check_version();
  100. die;
  101. }
  102. public function check_version() {
  103. $local_version = VamtamFramework::get_version();
  104. $key = VAMTAM_THEME_SLUG . '_' . $local_version;
  105. $last_license_key = get_option( 'vamtam-envato-license-key-old' );
  106. $current_license_key = get_option( 'vamtam-envato-license-key' );
  107. $system_status_opt_out_old = get_option( 'vamtam-system-status-opt-in-old' );
  108. $system_status_opt_out = get_option( 'vamtam-system-status-opt-in' );
  109. if ( $last_license_key !== $current_license_key || $system_status_opt_out_old !== $system_status_opt_out || false === get_transient( $key ) ) {
  110. global $wp_version;
  111. $data = array(
  112. 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ) . '; ',
  113. 'blocking' => false,
  114. 'body' => array(
  115. 'theme_version' => $local_version,
  116. 'php_version' => phpversion(),
  117. 'server' => $_SERVER['SERVER_SOFTWARE'],
  118. 'theme_name' => VAMTAM_THEME_NAME,
  119. 'license_key' => $current_license_key,
  120. 'active_plugins' => self::active_plugins(),
  121. 'system_status' => self::system_status(),
  122. ),
  123. );
  124. if ( $last_license_key !== $current_license_key ) {
  125. update_option( 'vamtam-envato-license-key-old', $current_license_key );
  126. }
  127. if ( $system_status_opt_out_old !== $system_status_opt_out ) {
  128. update_option( 'vamtam-system-status-opt-in-old', $system_status_opt_out );
  129. }
  130. wp_remote_post( $this->remote, $data );
  131. set_transient( $key, true, $this->interval ); // cache
  132. }
  133. }
  134. public static function active_plugins() {
  135. $active_plugins = (array) get_option( 'active_plugins', array() );
  136. if ( is_multisite() )
  137. $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
  138. return $active_plugins;
  139. }
  140. public static function system_status() {
  141. if ( ! get_option( 'vamtam-system-status-opt-in' ) ) {
  142. return array(
  143. 'disabled' => true,
  144. );
  145. }
  146. $result = array(
  147. 'disabled' => false,
  148. 'wp_debug' => WP_DEBUG,
  149. 'wp_debug_display' => WP_DEBUG_DISPLAY,
  150. 'wp_debug_log' => WP_DEBUG_LOG,
  151. 'active_plugins' => array(),
  152. 'writable' => array(),
  153. 'ziparchive' => class_exists( 'ZipArchive' ),
  154. );
  155. if ( function_exists( 'ini_get' ) ) {
  156. $result['post_max_size'] = ini_get( 'post_max_size' );
  157. $result['max_input_vars'] = ini_get( 'max_input_vars' );
  158. $result['max_execution_time'] = ini_get( 'max_execution_time' );
  159. $result['memory_limit'] = ini_get( 'memory_limit' );
  160. }
  161. $active_plugins = self::active_plugins();
  162. foreach ( $active_plugins as $plugin ) {
  163. $plugin_data = @get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
  164. $result['active_plugins'][ $plugin ] = array(
  165. 'name' => $plugin_data['Name'],
  166. 'version' => $plugin_data['Version'],
  167. 'author' => $plugin_data['AuthorName'],
  168. );
  169. }
  170. $result['writable'][ VAMTAM_CACHE_DIR ] = is_writable( VAMTAM_CACHE_DIR );
  171. $cache_contents = glob( VAMTAM_CACHE_DIR . '*.{less,css}', GLOB_BRACE );
  172. if ( is_array( $cache_contents ) ) {
  173. foreach ( $cache_contents as $filepath ) {
  174. $result['writable'][ $filepath ] = is_writable( $filepath );
  175. }
  176. }
  177. $result['wp_remote_post'] = 'Irrelevant';
  178. return $result;
  179. }
  180. }
  181. Version_Checker::get_instance();