connections = $connections; $this->sites = $sites; $this->health = $health; } /** * {@inheritDoc} * * @return string */ public function menu_title() { return __( 'Connections', 'turtlereport' ); } /** * {@inheritDoc} * * @return string */ public function capability() { return Capabilities::MANAGE; } /** * {@inheritDoc} * * @return void */ public function render() { $rows = array(); foreach ( $this->connections->all() as $connection ) { $rows[] = array( 'connection' => $connection, 'sites' => $this->sites->using_connection( $connection->id ), 'health' => $this->health->status( $connection->id ), 'diagnosis' => $this->health->diagnosis( $connection->id ), ); } View::render( 'connections/list', array( 'rows' => $rows, 'redirect_uri' => ConnectionActions::redirect_uri(), 'guide_url' => GuidePage::url(), 'is_https' => 0 === strpos( ConnectionActions::redirect_uri(), 'https://' ), 'checklist' => $this->checklist( $rows ), 'sites_url' => SitesPage::url( array( 'action' => 'new' ) ), ) ); } /** * The setup checklist: each step done (true), a problem (false) or not known yet (null). * * Worked out from the connections and their last test results, so it * costs no Google request. * * @param array[] $rows Connection rows with diagnosis. * @return array[] Steps with key, label, state, text and link. */ private function checklist( array $rows ) { $state = array( 'apis' => null, 'connect' => false, 'account' => null, 'gsc' => null, 'ga' => null, 'gbp' => null, 'site' => (bool) $this->sites->all(), ); $api_problem = false; foreach ( $rows as $row ) { if ( $row['connection']->is_connected() ) { $state['connect'] = true; $state['account'] = ( null === $state['account'] || true === $state['account'] ) && '' !== $row['connection']->account_email; } if ( ! $row['diagnosis'] ) { continue; } $by = array_column( $row['diagnosis']['checks'], null, 'key' ); $ok = array( 'gsc' => isset( $by['gsc'] ) ? $by['gsc']['ok'] : null, 'ga' => isset( $by['ga'] ) ? ( true === $by['ga']['ok'] && isset( $by['ga_data'] ) ? $by['ga_data']['ok'] : $by['ga']['ok'] ) : null, 'gbp' => isset( $by['gbp'] ) ? $by['gbp']['ok'] : null, ); foreach ( $ok as $key => $value ) { // One connection that works is enough for a step to be done. if ( true === $value ) { $state[ $key ] = true; } elseif ( false === $value && true !== $state[ $key ] ) { $state[ $key ] = false; } } foreach ( $row['diagnosis']['checks'] as $c ) { $api_problem = $api_problem || ( false === $c['ok'] && isset( $c['reason'] ) && \TurtleReport\Auth\ApiException::API_DISABLED === $c['reason'] ); } } $state['apis'] = $api_problem ? false : ( $state['gsc'] || $state['ga'] ? true : null ); $guide = GuidePage::url(); return array( array( 'apis', __( 'Required Google APIs enabled', 'turtlereport' ), $state['apis'], __( 'In your Google Cloud project: Search Console API, Analytics Data API and Analytics Admin API.', 'turtlereport' ), $guide . '#project' ), array( 'connect', __( 'Google account connected', 'turtlereport' ), $state['connect'], __( 'Sign in with Google below, or add a service-account key.', 'turtlereport' ), '#tly-add-connection' ), array( 'account', __( 'Google account identified', 'turtlereport' ), $state['account'], __( 'Each connection shows the account it reads as.', 'turtlereport' ), '' ), array( 'gsc', __( 'Search Console access', 'turtlereport' ), $state['gsc'], __( 'The account can see at least one property.', 'turtlereport' ), $guide . '#access' ), array( 'ga', __( 'GA4 access', 'turtlereport' ), $state['ga'], __( 'The account can see a property and read its reports.', 'turtlereport' ), $guide . '#access' ), array( 'gbp', __( 'Business Profile (optional)', 'turtlereport' ), $state['gbp'], __( 'Needs Google\'s API approval and a Google sign-in connection.', 'turtlereport' ), $guide . '#gbp' ), array( 'site', __( 'A site added', 'turtlereport' ), $state['site'], __( 'Link a website to its properties and choose who sees it.', 'turtlereport' ), SitesPage::url( array( 'action' => 'new' ) ) ), ); } /** * {@inheritDoc} * * @return void */ public function enqueue() { $this->enqueue_admin_style(); AdminAssets::enqueue( 'admin', 'turtlereport-admin' ); } }