class-wc-updates-screen-updates.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Manages WooCommerce plugin updating on the Updates screen.
  4. *
  5. * @package WooCommerce/Admin
  6. * @version 3.2.0
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. if ( ! class_exists( 'WC_Plugin_Updates' ) ) {
  12. include_once dirname( __FILE__ ) . '/class-wc-plugin-updates.php';
  13. }
  14. /**
  15. * Class WC_Updates_Screen_Updates
  16. */
  17. class WC_Updates_Screen_Updates extends WC_Plugin_Updates {
  18. /**
  19. * Constructor.
  20. */
  21. public function __construct() {
  22. add_action( 'admin_print_footer_scripts', array( $this, 'update_screen_modal' ) );
  23. }
  24. /**
  25. * Show a warning message on the upgrades screen if the user tries to upgrade and has untested plugins.
  26. */
  27. public function update_screen_modal() {
  28. $updateable_plugins = get_plugin_updates();
  29. if ( empty( $updateable_plugins['woocommerce/woocommerce.php'] )
  30. || empty( $updateable_plugins['woocommerce/woocommerce.php']->update )
  31. || empty( $updateable_plugins['woocommerce/woocommerce.php']->update->new_version ) ) {
  32. return;
  33. }
  34. $this->new_version = wc_clean( $updateable_plugins['woocommerce/woocommerce.php']->update->new_version );
  35. $this->major_untested_plugins = $this->get_untested_plugins( $this->new_version, 'major' );
  36. if ( ! empty( $this->major_untested_plugins ) ) {
  37. echo $this->get_extensions_modal_warning(); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
  38. $this->update_screen_modal_js();
  39. }
  40. }
  41. /**
  42. * JS for the modal window on the updates screen.
  43. */
  44. protected function update_screen_modal_js() {
  45. ?>
  46. <script>
  47. ( function( $ ) {
  48. var modal_dismissed = false;
  49. // Show the modal if the WC upgrade checkbox is checked.
  50. var show_modal_if_checked = function() {
  51. if ( modal_dismissed ) {
  52. return;
  53. }
  54. var $checkbox = $( 'input[value="woocommerce/woocommerce.php"]' );
  55. if ( $checkbox.prop( 'checked' ) ) {
  56. $( '#wc-upgrade-warning' ).click();
  57. }
  58. }
  59. $( '#plugins-select-all, input[value="woocommerce/woocommerce.php"]' ).on( 'change', function() {
  60. show_modal_if_checked();
  61. } );
  62. // Add a hidden thickbox link to use for bringing up the modal.
  63. $('body').append( '<a href="#TB_inline?height=600&width=550&inlineId=wc_untested_extensions_modal" class="wc-thickbox" id="wc-upgrade-warning" style="display:none"></a>' );
  64. // Don't show the modal again once it's been accepted.
  65. $( '#wc_untested_extensions_modal .accept' ).on( 'click', function( evt ) {
  66. evt.preventDefault();
  67. modal_dismissed = true;
  68. tb_remove();
  69. });
  70. // Uncheck the WC update checkbox if the modal is canceled.
  71. $( '#wc_untested_extensions_modal .cancel' ).on( 'click', function( evt ) {
  72. evt.preventDefault();
  73. $( 'input[value="woocommerce/woocommerce.php"]' ).prop( 'checked', false );
  74. tb_remove();
  75. });
  76. })( jQuery );
  77. </script>
  78. <?php
  79. $this->generic_modal_js();
  80. }
  81. }
  82. new WC_Updates_Screen_Updates();