WC_Services_Installer

The WordPress Core WC Services Installer class.

Defined (1)

The class is defined in the following location(s).

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