containing authentication parameters. * @return string|null Rendered HTML for the API connection sidebar part, or null on failure. * @throws Exception If required parameters are missing or no backup jobs are set. */ public function authenticate( WP_REST_Request $request ): ?string { $params = $request->get_params(); if ( ! isset( $params['sugaremail'] ) || '' === $params['sugaremail'] ) { throw new Exception( __( 'No email set.', 'backwpup' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } if ( ! isset( $params['sugarpass'] ) || '' === $params['sugarpass'] ) { throw new Exception( __( 'No password set.', 'backwpup' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } if ( ! empty( $params['job_id'] ) ) { $jobs_ids = [ $params['job_id'] ]; } else { $jobs_ids = $this->get_job_ids(); } $sugarsync = new \BackWPup_Destination_SugarSync_API(); $refresh_token = $sugarsync->get_Refresh_Token( sanitize_email( $params['sugaremail'] ), $params['sugarpass'] ); if ( ! empty( $refresh_token ) ) { foreach ( $jobs_ids as $jobid ) { $this->option_adapter->update( (int) $jobid, 'sugarrefreshtoken', $refresh_token ); } } return $this->helpers_adapter->children( 'sidebar/sugar-sync-parts/api-connexion', true, [ 'job_id' => $jobs_ids[0] ] ); } /** * Return jobs id * * @return array * @throws Exception Throw exception when $first_job_id is missing. */ private function get_job_ids(): array { $first_job_id = get_site_option( Plugin::FIRST_JOB_ID, false ); if ( false === $first_job_id ) { throw new Exception( __( 'No backup jobs set.', 'backwpup' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped } return [ $first_job_id ]; } }