SPATH . '/wp-admin/includes/plugin.php'; return 0 === validate_plugin( self::PLUGIN_FILE ); } /** * Install and activate WooCommerce Payments. * * @return boolean Whether the plugin was successfully activated. */ private function install_and_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? if ( ! isset( $_GET['page'] ) || 'wc-admin' !== $_GET['page'] || ! isset( $_GET['action'] ) || 'setup-woocommerce-payments' !== $_GET['action'] ) { return; } $data_store = Notes::load_data_store(); // We already have this note? Then mark the note as actioned. $note_ids = $data_store->get_notes_with_name( self::NOTE_NAME ); if ( empty( $note_ids ) ) { return; } $note_id = array_pop( $note_ids ); $note = Notes::get_note( $note_id ); if ( false === $note ) { return; } $action = $note->get_action( 'get-started' ); if ( ! $action || ( isset( $action->nonce_action ) && ( empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), $action->nonce_action ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput ) ) ) { return; } 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; } }