_activate_wcpay() { $install_request = array( 'plugins' => self::PLUGIN_SLUG ); $installer = new \Automattic\WooCommerce\Admin\API\Plugins(); $result = $installer->install_plugins( $install_request ); if ( is_wp_error( $result ) ) { return false; } wc_admin_record_tracks_event( 'woocommerce_payments_install', array( 'context' => 'inbox' ) ); $activate_request = array( 'plugins' => self::PLUGIN_SLUG ); $result = $installer->activate_plugins( $activate_request ); if ( is_wp_error( $result ) ) { return false; } return true; } /** * Install & activate WooCommerce Payments plugin, and redirect to setup. */ public function install_on_action() { // TODO: Need to validate this request more strictly since we're taking install actions directly? /* phpcs:disable WordPress.Security.NonceVerification */ if ( ! isset( $_GET['page'] ) || 'wc-admin' !== $_GET['page'] || ! isset( $_GET['action'] ) || 'setup-woocommerce-payments' !== $_GET['action'] ) { return; } /* phpcs:enable */ if ( ! current_user_can( 'install_plugins' ) ) { return; } $this->install_and_activate_wcpay(); // WooCommerce Payments is installed at this point, so link straight into the onboarding flow. $connect_url = add_query_arg( array( 'wcpay-connect' => '1', '_wpnonce' => wp_create_nonce( 'wcpay-connect' ), ), admin_url() ); wp_safe_redirect( $connect_url ); exit; } }