/** * Check to make sure we are on the right tab. * * Redirect if we are not on the right tab based on authentication status or invalid tab, * OR return if not even on this plugin's Admin URL * Register an error message for later display if not authenticated. * * @return bool Returns true if we are on a verified tab, false or URL redirect otherwise. */ function verify_current_tab() { /* * If we have no tabs trying to verify is a moot point. Punt. */ if (!$this->has_tabs()) { return true; } $needs_grant = $this->plugin->needs_grant(); $has_grant = $this->plugin->has_grant(); if (!$this->is_current_tab_valid()) { /** * If we don't have a valid tab and we are using an API, redirect to first tab if we have an grant or authentication tab if not. * * We redirect to avoid having multiple URLs mean the same thing which would not be optimal for bookmarking, caching, etc. */ if ($needs_grant && $has_grant) { /** * If authenticated we redirect with a "301 - This URL has changed" status code so the browser can know * to go to 'usage' whenever is sees this URL and avoid the round trip next time. */ wp_safe_redirect($this->get_tab_url($this->get_default_tab()->tab_slug), 301); exit; } else { if ($needs_grant && ($auth_tab = $this->get_authentication_tab())) { /** * If not authenticated we redirect with a "302 - This URL has moved temporarily" status code * because normally we'd want to go to usage, so don't cause browser to thing this URL w/o a * valid tab should always go to 'account.' */ wp_safe_redirect($this->get_tab_url($auth_tab->tab_slug), 302); exit; } } } else { if ($needs_grant && !$has_grant) { /** * If we are using an API but we don't have a grant */ $auth_tab = $this->get_authentication_tab(); /** * If there is a tab and we are not already on the authentication tab */ if ($auth_tab && (!isset($_GET['tab']) || $auth_tab->tab_slug != $_GET['tab'])) { /** * ...and we are NOT on the authentication tab then redirect to the 'account' tab. * * We redirect with a "302 - This URL has moved temporarily" because it's still a good URL and * we want the browser to be happy to return here later. */ wp_safe_redirect($this->get_tab_url($auth_tab), 302); exit; } else { /** * ...and we ARE on the authentication tab then prepare a "Need to authenticate" message for later display. */ add_settings_error($this->plugin->plugin_slug, 'need-info', __('You must have an account to use this plugin. Please enter your credentials.', 'sidecar')); } } } return true; }