autoupdate.php 5.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Filters the auto update plugin routine to allow MonsterInsights to be
  4. * automatically updated.
  5. *
  6. * @since 6.3.0
  7. *
  8. * @param bool $update Flag to update the plugin or not.
  9. * @param array $item Update data about a specific plugin.
  10. * @return bool $update The new update state.
  11. */
  12. function monsterinsights_automatic_updates( $update, $item ) {
  13. // If this is multisite and is not on the main site, return early.
  14. if ( is_multisite() && ! is_main_site() ) {
  15. return $update;
  16. }
  17. // If we don't have everything we need, return early.
  18. $item = (array) $item;
  19. if ( ! isset( $item['new_version'] ) || ! isset( $item['slug'] ) ) {
  20. return $update;
  21. }
  22. // If the plugin isn't ours, return early.
  23. $is_free = 'google-analytics-for-wordpress' === $item['slug'];
  24. $is_paid = isset( $item['monsterinsights_plugin'] ); // see updater class
  25. if ( ! $is_free && ! $is_paid ) {
  26. return $update;
  27. }
  28. $version = $is_free ? MONSTERINSIGHTS_LITE_VERSION : $item['old_version'];
  29. $automatic_updates = monsterinsights_get_option( 'automatic_updates', false );
  30. $current_major = monsterinsights_get_major_version( $version );
  31. $new_major = monsterinsights_get_major_version( $item['new_version'] );
  32. // If the opt in update allows major updates but there is no major version update, return early.
  33. if ( $current_major < $new_major ) {
  34. if ( $automatic_updates === 'all' ) {
  35. return true;
  36. } else {
  37. return $update;
  38. }
  39. }
  40. // If the opt in update allows minor updates but there is no minor version update, return early.
  41. if ( $current_major == $new_major ) {
  42. if ( $automatic_updates === 'all' || $automatic_updates === 'minor' ) {
  43. return true;
  44. } else {
  45. return $update;
  46. }
  47. }
  48. // All our checks have passed - this plugin can be updated!
  49. return true;
  50. }
  51. add_filter( 'auto_update_plugin', 'monsterinsights_automatic_updates', 10, 2 );
  52. /**
  53. * Notes about autoupdater:
  54. * This runs on the normal WordPress auto-update sequence:
  55. * 1. In wp-includes/update.php, wp_version_check() is called by the WordPress update cron (every 8 or 12 hours; can be overriden to be faster/long or turned off by plugins)
  56. * 2. In wp-includes/update.php, wp_version_check() ends with a action call to do_action( 'wp_maybe_auto_update' ) if cron is running
  57. * 3. In wp-includes/update.php, wp_maybe_auto_update() hooks into wp_maybe_auto_update action, creates a new WP_Automatic_Updater instance and calls WP_Automatic_Updater->run
  58. * 4. In wp-admin/includes/class-wp-automatic-updater.php $this->run() checks to make sure we're on the main site if on a network, and also if the autoupdates are disabled (by plugin, by being on a version controlled site, etc )
  59. * 5. In wp-admin/includes/class-wp-automatic-updater.php $this->run() then checks to see which plugins have new versions (version/update check)
  60. * 6. In wp-admin/includes/class-wp-automatic-updater.php $this->run() then calls $this->update() for each plugin installed who has an upgrade.
  61. * 7 In wp-admin/includes/class-wp-automatic-updater.php $this->update() double checks filesystem access and then installs the plugin if able
  62. *
  63. * Notes:
  64. * - This autoupdater only works if WordPress core detects no version control. If you want to test this, do it on a new WP site without any .git folders anywhere.
  65. * - This autoupdater only works if the file access is able to be written to
  66. * - This autoupdater only works if a new version has been detected, and will run not the second the update is released, but whenever the cron for wp_version_check is next released. This is generally run every 8-12 hours.
  67. * - However, that cron can be disabled, the autoupdater can be turned off via constant or filter, version control or file lock can be detected, and other plugins can be installed (incl in functions of theme) that turn off all
  68. * all automatic plugin updates.
  69. * - If you want to test this is working, you have to manually run the wp_version_check cron. Install the WP Crontrol plugin or Core Control plugin, and run the cron manually using it.
  70. * - Again, because you skimmed over it the first time, if you want to test this manually you need to test this on a new WP install without version control for core, plugins, etc, without file lock, with license key entered (for pro only)
  71. * and use the WP Crontrol or Core Control plugin to run wp_version_check
  72. * - You may have to manually remove an option called "auto_update.lock" from the WP options table
  73. * - You may need to run wp_version_check multiple times (note though that they must be spaced at least 60 seconds apart)
  74. * - Because WP's updater asks the OS if the file is writable, make sure you do not have any files/folders for the plugin you are trying to autoupdate open when testing.
  75. * - You may need to delete the plugin info transient to get it to hard refresh the plugin info.
  76. */
  77. function monsterinsights_get_major_version( $version ) {
  78. $exploded_version = explode( '.', $version );
  79. if ( isset( $exploded_version[2] ) ) {
  80. return $exploded_version[0] . '.' . $exploded_version[1] . '.' . $exploded_version[2];
  81. } else {
  82. return $exploded_version[0] . '.' . $exploded_version[1] . '.0';
  83. }
  84. }