class-vamtam-updates.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. class Vamtam_Updates_2 {
  3. private $slug;
  4. private $main_file;
  5. private $full_path;
  6. private $api_url;
  7. public function __construct( $file ) {
  8. $this->slug = basename( dirname( $file ) );
  9. $this->main_file = trailingslashit( $this->slug ) . basename( $file );
  10. $this->full_path = $file;
  11. $this->api_url = 'https://updates.api.vamtam.com/0/envato/check';
  12. // delete_site_transient( 'update_plugins' );
  13. add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check' ) );
  14. add_filter( 'plugins_api', array( $this, 'plugins_api' ), 10, 3 );
  15. }
  16. public function check( $updates ) {
  17. $response = $this->api_request();
  18. if ( false === $response ) {
  19. return $updates;
  20. }
  21. if ( ! isset( $updates->response ) ) {
  22. $updates->response = array();
  23. }
  24. $updates->response = array_merge( $updates->response, $response );
  25. // Small trick to ensure the updates get shown in the network admin
  26. if ( is_multisite() && ! is_main_site() ) {
  27. global $current_site;
  28. switch_to_blog( $current_site->blog_id );
  29. set_site_transient( 'update_plugins', $updates );
  30. restore_current_blog();
  31. }
  32. return $updates;
  33. }
  34. public function plugins_api( $data, $action = '', $args = null ) {
  35. if ( 'plugin_information' !== $action ) {
  36. return $data;
  37. }
  38. if ( ! isset( $args->slug ) || ( $args->slug !== $this->slug ) ) {
  39. return $data;
  40. }
  41. $data = new stdClass;
  42. return $data;
  43. }
  44. private function api_request() {
  45. global $wp_version;
  46. $update_cache = get_site_transient( 'update_plugins' );
  47. $plugin_data = get_plugin_data( $this->full_path );
  48. $raw_response = wp_remote_post( $this->api_url, array(
  49. 'body' => array(
  50. 'slug' => $this->slug,
  51. 'main_file' => $this->main_file,
  52. 'version' => $plugin_data['Version'],
  53. 'purchase_key' => apply_filters( 'wpv_purchase_code', '' ),
  54. ),
  55. 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(),
  56. ) );
  57. if ( is_wp_error( $raw_response ) || 200 !== wp_remote_retrieve_response_code( $raw_response ) ) {
  58. return false;
  59. }
  60. $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
  61. foreach ( $response['plugins'] as &$plugin ) {
  62. $plugin = (object) $plugin;
  63. }
  64. unset( $plugin );
  65. return $response['plugins'];
  66. }
  67. }