Fragen\GitHub_Updater\Settings::ghu_tokens PHP Метод

ghu_tokens() публичный Метод

Create and return settings fields for private repositories.
public ghu_tokens ( )
    public function ghu_tokens()
    {
        $ghu_options_keys = array();
        $ghu_plugins = Plugin::instance()->get_plugin_configs();
        $ghu_themes = Theme::instance()->get_theme_configs();
        $ghu_tokens = array_merge($ghu_plugins, $ghu_themes);
        foreach ($ghu_tokens as $token) {
            $type = '<span class="dashicons dashicons-admin-plugins"></span>&nbsp;';
            $setting_field = array();
            $ghu_options_keys[$token->repo] = null;
            /*
             * Set boolean for Enterprise headers.
             */
            if ($token->enterprise) {
                /*
                 * Set boolean if GitHub Enterprise header found.
                 */
                if (false !== strpos($token->type, 'github') && !parent::$auth_required['github_enterprise']) {
                    parent::$auth_required['github_enterprise'] = true;
                }
                /*
                 * Set boolean if GitLab CE/Enterprise header found.
                 */
                if (false !== strpos($token->type, 'gitlab') && !empty($token->enterprise) && !parent::$auth_required['gitlab_enterprise']) {
                    parent::$auth_required['gitlab_enterprise'] = true;
                }
            }
            /*
             * Check to see if it's a private repo and set variables.
             */
            if ($this->is_private($token)) {
                if (false !== strpos($token->type, 'github') && !parent::$auth_required['github_private']) {
                    parent::$auth_required['github_private'] = true;
                }
                if (false !== strpos($token->type, 'bitbucket') && !parent::$auth_required['bitbucket_private']) {
                    parent::$auth_required['bitbucket_private'] = true;
                }
            }
            /*
             * Set boolean if GitLab header found.
             */
            if (false !== strpos($token->type, 'gitlab') && empty($token->enterprise) && !parent::$auth_required['gitlab']) {
                parent::$auth_required['gitlab'] = true;
            }
            /*
             * Next if not a private repo or token field not empty.
             */
            if (!$this->is_private($token)) {
                continue;
            }
            if (false !== strpos($token->type, 'theme')) {
                $type = '<span class="dashicons dashicons-admin-appearance"></span>&nbsp;';
            }
            $setting_field['id'] = $token->repo;
            $setting_field['title'] = $type . $token->name;
            $token_type = explode('_', $token->type);
            switch ($token_type[0]) {
                case 'github':
                    $setting_field['page'] = 'github_updater_github_install_settings';
                    $setting_field['section'] = 'github_id';
                    $setting_field['callback_method'] = array(&$this, 'token_callback_text');
                    $setting_field['callback'] = $token->repo;
                    break;
                case 'bitbucket':
                    $setting_field['page'] = 'github_updater_bitbucket_install_settings';
                    $setting_field['section'] = 'bitbucket_id';
                    $setting_field['callback_method'] = array(&$this, 'token_callback_checkbox');
                    $setting_field['callback'] = $token->repo;
                    break;
                case 'gitlab':
                    $setting_field['page'] = 'github_updater_gitlab_install_settings';
                    $setting_field['section'] = 'gitlab_id';
                    $setting_field['callback_method'] = array(&$this, 'token_callback_text');
                    $setting_field['callback'] = $token->repo;
                    break;
            }
            add_settings_field($setting_field['id'], $setting_field['title'], $setting_field['callback_method'], $setting_field['page'], $setting_field['section'], array('id' => $setting_field['callback'], 'token' => true));
        }
        /*
         * Unset options that are no longer present and update options.
         */
        $ghu_unset_keys = array_diff_key(parent::$options, $ghu_options_keys);
        $always_unset = array('db_version', 'branch_switch', 'github_access_token', 'github_enterprise_token', 'bitbucket_username', 'bitbucket_password');
        array_filter($always_unset, function ($e) use(&$ghu_unset_keys) {
            unset($ghu_unset_keys[$e]);
        });
        $auth_required = parent::$auth_required;
        $auth_required_unset = array('github_enterprise' => 'github_enterprise_token', 'gitlab' => 'gitlab_access_token', 'gitlab_enterprise' => 'gitlab_enterprise_token');
        array_filter($auth_required_unset, function ($e) use(&$ghu_unset_keys, $auth_required, $auth_required_unset) {
            $key = array_search($e, $auth_required_unset);
            if ($auth_required[$key]) {
                unset($ghu_unset_keys[$e]);
            }
        });
        // Unset if value set.
        array_filter($ghu_unset_keys, function ($e) use(&$ghu_unset_keys) {
            $key = array_search($e, $ghu_unset_keys);
            if ($ghu_unset_keys[$key] && 'github_updater_install_repo' !== $key) {
                unset($ghu_unset_keys[$key]);
            }
        });
        if (!empty($ghu_unset_keys)) {
            foreach ($ghu_unset_keys as $key => $value) {
                unset(parent::$options[$key]);
            }
            update_site_option('github_updater', parent::$options);
        }
    }