# stockpack/2.2/src/class-stockpackadmin.php

StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more, version 2.2. 209 lines.

- Page: https://pluginprobe.com/plugins/stockpack/2.2/code/src/class-stockpackadmin.php
- Raw: https://pluginprobe.com/plugins/stockpack/2.2/raw/src/class-stockpackadmin.php
- Modified: 2019-12-04T08:35:28+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/stockpack/2.2/code/src/class-stockpackadmin.php#L10-L20`.

```php
<?php

/**
 * Class StockpackAdmin
 *
 * Handle the settings admin page
 */
class StockpackAdmin {
    /**
     * @var Singleton The reference the *Singleton* instance of this class
     */
    private static $instance;

    /**
     * @var WeDevs_Settings_API
     */
    public $settings_api;

    /**
     * Returns the *Singleton* instance of this class.
     *
     * @return Singleton The *Singleton* instance.
     */
    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * Stockpack constructor.
     */
    protected function __construct() {
        add_action( 'plugins_loaded', array( $this, 'init' ) );
    }

    /**
     *
     */
    public function init() {
        $this->actions();
    }


    /**
     *
     */
    public function actions() {
        add_action( 'admin_init', array( $this, 'register_settings' ) );
        add_action( 'admin_menu', array( $this, 'register_settings_page' ) );
    }

    /**
     *  Register settings
     */
    public function register_settings() {

        $this->settings_api = new WeDevs_Settings_API();
        //set sections and fields
        $this->settings_api->set_sections( $this->get_settings_sections() );
        $this->settings_api->set_fields( $this->get_settings_fields() );

        //initialize them
        $this->settings_api->admin_init();
    }

    /**
     *
     */
    public function register_settings_page() {
        add_options_page( __( 'StockPack', 'stockpack' ), __( 'StockPack', 'stockpack' ), 'delete_posts', 'stockpack',
            array( $this, 'plugin_page' )
        );
    }


    /**
     *
     */
    public function plugin_page() {

        echo '<div class="wrap">';
        $this->settings_api->show_navigation();
        $this->settings_api->show_forms();
        $link_text = esc_textarea( __( 'Get your license key', 'stockpack' ) );
        echo '<a class="license-key-link" href="https://stockpack.co/register" target="_blank">' . $link_text . '</a>';

        echo '</div>';
    }

    public function get_license_state() {
        return $this->get_option( 'license_state', 'stockpack_basics' );
    }

    public function get_safe_search() {
        return $this->get_option( 'safe_search', 'stockpack_basics' );
    }

    /**
     * @return mixed
     */
    public function get_api_key() {
        return $this->get_option( 'auth_token', 'stockpack_basics' );
    }

    /**
     * @return mixed
     */
    public function set_api_key( $token ) {
        return $this->set_option( 'auth_token', 'stockpack_basics', $token );
    }


    /**
     * @return array
     */
    private function get_settings_sections() {
        return array(
            array(
                'id'    => 'stockpack_basics',
                'title' => __( 'StockPack', 'stockpack' ),
            ),
        );
    }

    /**
     * @return array
     */
    private function get_settings_fields() {
        return array(
            'stockpack_basics' => array(
                array(
                    'name'    => 'auth_token',
                    'label'   => __( 'License key', 'stockpack' ),
                    'desc'    => __( 'This is the license key that is used for authentication', 'stockpack' ),
                    'type'    => 'text',
                    'default' => '',
                    'size'    => 'validate-stockpack-key regular' //small hack to add class
                ),
                array(
                    'name'    => 'license_state',
                    'label'   => __( 'Search with authorization', 'stockpack' ),
                    'desc'    => __( 'Providers that support oauth can get the license state for all images directly. You see it in the sidebar. The downside is that the searches are slower', 'stockpack' ),
                    'options' => array(
                        'yes' => __( 'Yes', 'stockpack' ),
                        'no'  => __( 'No', 'stockpack' )
                    ),
                    'type'    => 'radio',
                    'default' => 'no',
                ),
                array(
                    'name'    => 'safe_search',
                    'label'   => __( 'Apply safe search', 'stockpack' ),
                    'desc'    => __( 'Some providers support this mode to prevent nude images to show up in the search', 'stockpack' ),
                    'options' => array(
                        'yes' => __( 'Yes', 'stockpack' ),
                        'no'  => __( 'No', 'stockpack' )
                    ),
                    'type'    => 'radio',
                    'default' => 'yes',
                ),
            ),
        );
    }

    /**
     * Get the value of a settings field
     *
     * @param string $option settings field name
     * @param string $section the section name this field belongs to
     * @param string $default default text if it's not found
     *
     * @return mixed
     */
    private function get_option( $option, $section, $default = '' ) {

        $options = get_option( $section, array() );

        if ( isset( $options[ $option ] ) ) {
            return $options[ $option ];
        }

        return $default;
    }

    /**
     * Set the value of a settings field
     *
     * @param string $option settings field name
     * @param string $section the section name this field belongs to
     * @param string $value
     *
     * @return mixed
     */
    private function set_option( $option, $section, $value ) {

        $options = get_option( $section, array() );
        if ( ! is_array( $options ) ) {
            $options = array();
        }

        $options[ $option ] = $value;

        update_option( $section, $options );
    }
}

```
