woocommerce-services.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. class WC_Services_Installer {
  6. /**
  7. * @var Jetpack
  8. **/
  9. private $jetpack;
  10. /**
  11. * @var WC_Services_Installer
  12. **/
  13. private static $instance = null;
  14. static function init() {
  15. if ( is_null( self::$instance ) ) {
  16. self::$instance = new WC_Services_Installer();
  17. }
  18. return self::$instance;
  19. }
  20. public function __construct() {
  21. $this->jetpack = Jetpack::init();
  22. add_action( 'admin_init', array( $this, 'add_error_notice' ) );
  23. add_action( 'admin_init', array( $this, 'try_install' ) );
  24. }
  25. /**
  26. * Verify the intent to install WooCommerce Services, and kick off installation.
  27. */
  28. public function try_install() {
  29. if ( ! isset( $_GET['wc-services-action'] ) ) {
  30. return;
  31. }
  32. check_admin_referer( 'wc-services-install' );
  33. $result = false;
  34. switch ( $_GET['wc-services-action'] ) {
  35. case 'install':
  36. if ( current_user_can( 'install_plugins' ) ) {
  37. $this->jetpack->stat( 'jitm', 'wooservices-install-' . JETPACK__VERSION );
  38. $result = $this->install();
  39. if ( $result ) {
  40. $result = $this->activate();
  41. }
  42. }
  43. break;
  44. case 'activate':
  45. if ( current_user_can( 'activate_plugins' ) ) {
  46. $this->jetpack->stat( 'jitm', 'wooservices-activate-' . JETPACK__VERSION );
  47. $result = $this->activate();
  48. }
  49. break;
  50. }
  51. $redirect = isset( $_GET['redirect'] ) ? admin_url( $_GET['redirect'] ) : wp_get_referer();
  52. if ( $result ) {
  53. $this->jetpack->stat( 'jitm', 'wooservices-activated-' . JETPACK__VERSION );
  54. } else {
  55. $redirect = add_query_arg( 'wc-services-install-error', true, $redirect );
  56. }
  57. wp_safe_redirect( $redirect );
  58. exit;
  59. }
  60. /**
  61. * Set up installation error admin notice.
  62. */
  63. public function add_error_notice() {
  64. if ( ! empty( $_GET['wc-services-install-error'] ) ) {
  65. add_action( 'admin_notices', array( $this, 'error_notice' ) );
  66. }
  67. }
  68. /**
  69. * Notify the user that the installation of WooCommerce Services failed.
  70. */
  71. public function error_notice() {
  72. ?>
  73. <div class="notice notice-error is-dismissible">
  74. <p><?php _e( 'There was an error installing WooCommerce Services.', 'jetpack' ); ?></p>
  75. </div>
  76. <?php
  77. }
  78. /**
  79. * Download and install the WooCommerce Services plugin.
  80. *
  81. * @return bool result of installation
  82. */
  83. private function install() {
  84. include_once( ABSPATH . '/wp-admin/includes/admin.php' );
  85. include_once( ABSPATH . '/wp-admin/includes/plugin-install.php' );
  86. include_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  87. include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
  88. include_once( ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php' );
  89. $api = plugins_api( 'plugin_information', array( 'slug' => 'woocommerce-services' ) );
  90. if ( is_wp_error( $api ) ) {
  91. return false;
  92. }
  93. $upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
  94. $result = $upgrader->install( $api->download_link );
  95. return true === $result;
  96. }
  97. /**
  98. * Activate the WooCommerce Services plugin.
  99. *
  100. * @return bool result of activation
  101. */
  102. private function activate() {
  103. $result = activate_plugin( 'woocommerce-services/woocommerce-services.php' );
  104. // activate_plugin() returns null on success
  105. return is_null( $result );
  106. }
  107. }
  108. WC_Services_Installer::init();