class-plugin-update-manager.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. if( class_exists( 'Yoast_Update_Manager' ) && ! class_exists( "Yoast_Plugin_Update_Manager", false ) ) {
  3. class Yoast_Plugin_Update_Manager extends Yoast_Update_Manager {
  4. /**
  5. * Constructor
  6. *
  7. * @param Yoast_Product $product The Product.
  8. * @param string $license_key The License entered.
  9. */
  10. public function __construct( Yoast_Product $product, $license_key ) {
  11. parent::__construct( $product, $license_key );
  12. // setup hooks
  13. $this->setup_hooks();
  14. }
  15. /**
  16. * Setup hooks
  17. */
  18. private function setup_hooks() {
  19. // check for updates
  20. add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'set_updates_available_data' ) );
  21. // get correct plugin information (when viewing details)
  22. add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
  23. }
  24. /**
  25. * Check for updates and if so, add to "updates available" data
  26. *
  27. * @param object $data
  28. * @return object $data
  29. */
  30. public function set_updates_available_data( $data ) {
  31. if ( empty( $data ) ) {
  32. return $data;
  33. }
  34. // send of API request to check for updates
  35. $remote_data = $this->get_remote_data();
  36. // did we get a response?
  37. if( $remote_data === false ) {
  38. return $data;
  39. }
  40. // compare local version with remote version
  41. if ( version_compare( $this->product->get_version(), $remote_data->new_version, '<' ) ) {
  42. // remote version is newer, add to data
  43. $data->response[ $this->product->get_file() ] = $remote_data;
  44. }
  45. return $data;
  46. }
  47. /**
  48. * Gets new plugin version details (view version x.x.x details)
  49. *
  50. * @uses api_request()
  51. *
  52. * @param object $data
  53. * @param string $action
  54. * @param object $args (optional)
  55. *
  56. * @return object $data
  57. */
  58. public function plugins_api_filter( $data, $action = '', $args = null ) {
  59. // only do something if we're checking for our plugin
  60. if ( $action !== 'plugin_information' || ! isset( $args->slug ) || $args->slug !== $this->product->get_slug() ) {
  61. return $data;
  62. }
  63. $api_response = $this->get_remote_data();
  64. // did we get a response?
  65. if ( $api_response === false ) {
  66. return $data;
  67. }
  68. // return api response
  69. return $api_response;
  70. }
  71. }
  72. }