Bez popisu

woocommerce-services.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Installs and activates the WooCommerce Services plugin.
  7. */
  8. class WC_Services_Installer {
  9. /**
  10. * The instance of the Jetpack class.
  11. *
  12. * @var Jetpack
  13. */
  14. private $jetpack;
  15. /**
  16. * The singleton instance of this class.
  17. *
  18. * @var WC_Services_Installer
  19. */
  20. private static $instance = null;
  21. /**
  22. * Returns the singleton instance of this class.
  23. *
  24. * @return object The WC_Services_Installer object.
  25. */
  26. public static function init() {
  27. if ( is_null( self::$instance ) ) {
  28. self::$instance = new WC_Services_Installer();
  29. }
  30. return self::$instance;
  31. }
  32. /**
  33. * Constructor
  34. */
  35. public function __construct() {
  36. add_action( 'jetpack_loaded', array( $this, 'on_jetpack_loaded' ) );
  37. add_action( 'admin_init', array( $this, 'add_error_notice' ) );
  38. add_action( 'admin_init', array( $this, 'try_install' ) );
  39. }
  40. /**
  41. * Runs on Jetpack being ready to load its packages.
  42. *
  43. * @param Jetpack $jetpack object.
  44. */
  45. public function on_jetpack_loaded( $jetpack ) {
  46. $this->jetpack = $jetpack;
  47. }
  48. /**
  49. * Verify the intent to install WooCommerce Services, and kick off installation.
  50. */
  51. public function try_install() {
  52. if ( ! isset( $_GET['wc-services-action'] ) ) {
  53. return;
  54. }
  55. check_admin_referer( 'wc-services-install' );
  56. $result = false;
  57. switch ( $_GET['wc-services-action'] ) {
  58. case 'install':
  59. if ( current_user_can( 'install_plugins' ) ) {
  60. $this->jetpack->stat( 'jitm', 'wooservices-install-' . JETPACK__VERSION );
  61. $result = $this->install();
  62. if ( $result ) {
  63. $result = $this->activate();
  64. }
  65. }
  66. break;
  67. case 'activate':
  68. if ( current_user_can( 'activate_plugins' ) ) {
  69. $this->jetpack->stat( 'jitm', 'wooservices-activate-' . JETPACK__VERSION );
  70. $result = $this->activate();
  71. }
  72. break;
  73. }
  74. if ( isset( $_GET['redirect'] ) ) {
  75. $redirect = home_url( esc_url_raw( wp_unslash( $_GET['redirect'] ) ) );
  76. } else {
  77. $redirect = admin_url();
  78. }
  79. if ( $result ) {
  80. $this->jetpack->stat( 'jitm', 'wooservices-activated-' . JETPACK__VERSION );
  81. } else {
  82. $redirect = add_query_arg( 'wc-services-install-error', true, $redirect );
  83. }
  84. wp_safe_redirect( $redirect );
  85. exit;
  86. }
  87. /**
  88. * Set up installation error admin notice.
  89. */
  90. public function add_error_notice() {
  91. if ( ! empty( $_GET['wc-services-install-error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  92. add_action( 'admin_notices', array( $this, 'error_notice' ) );
  93. }
  94. }
  95. /**
  96. * Notify the user that the installation of WooCommerce Services failed.
  97. */
  98. public function error_notice() {
  99. ?>
  100. <div class="notice notice-error is-dismissible">
  101. <p><?php esc_html_e( 'There was an error installing WooCommerce Services.', 'jetpack' ); ?></p>
  102. </div>
  103. <?php
  104. }
  105. /**
  106. * Download and install the WooCommerce Services plugin.
  107. *
  108. * @return bool result of installation
  109. */
  110. private function install() {
  111. jetpack_require_lib( 'plugins' );
  112. $result = Jetpack_Plugins::install_plugin( 'woocommerce-services' );
  113. if ( is_wp_error( $result ) ) {
  114. return false;
  115. } else {
  116. return true;
  117. }
  118. }
  119. /**
  120. * Activate the WooCommerce Services plugin.
  121. *
  122. * @return bool result of activation
  123. */
  124. private function activate() {
  125. $result = activate_plugin( 'woocommerce-services/woocommerce-services.php' );
  126. // Activate_plugin() returns null on success.
  127. return is_null( $result );
  128. }
  129. }
  130. WC_Services_Installer::init();