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

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

- Page: https://pluginprobe.com/plugins/stockpack/2.6.0/code/src/class-stockpackadmin.php
- Raw: https://pluginprobe.com/plugins/stockpack/2.6.0/raw/src/class-stockpackadmin.php
- Modified: 2020-02-26T09:15:46+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.6.0/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( 'init', array( $this, 'init' ) );
    }

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

    /**
     *
     */
    public function actions() {
        add_action( 'wp_loaded', array( $this, 'maybe_show_notice' ) );
        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() {
        $capability = apply_filters( 'stockpack_settings_capability', 'upload_files' );

        add_options_page( __( 'StockPack', 'stockpack' ), __( 'StockPack', 'stockpack' ), $capability, 'stockpack',
            array( $this, 'plugin_page' )
        );
    }

    public function maybe_show_notice() {
        if ( ! PAnD::is_admin_notice_active( 'disable-media-notice-forever' ) ) {
            return;
        }
        add_action( 'admin_init', array( 'PAnD', 'init' ) );

        add_action( 'admin_notices', array( $this, 'media_notice' ) );
    }

    public function media_notice() {
        global $pagenow;
        if ( $pagenow != 'upload.php' ) {
            return;
        }

        $capability = apply_filters( 'stockpack_notices_capability', 'upload_files' );
        if ( current_user_can( $capability ) ) {
            echo '<div data-dismissible="disable-media-notice-forever" class="notice notice-warning is-dismissible">
          <p>' . __( 'If you are looking for StockPack you will find it on the Media Tab when you use the WordPress uploader. Just go to a post or page and try to insert an image. You will see the StockPack tab at the top.  You can read more here:', 'stockpack' ) . ' <a href="https://stockpack.co/blog/what-to-do-if-stockpack-tab-is-not-showing-up/">' . __( 'What to do if stockpack tab is not showing up', 'stockpack' ) . '</a></p>
         </div>';
        }


    }

    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' );
    }

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

    public function get_premium_providers_caption_setting() {
        return $this->get_option( 'caption_premium_providers', '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'    => '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',
                ),
                array(
                    'name'    => 'file_name_change',
                    'label'   => __( 'Enable file name change', 'stockpack' ),
                    'desc'    => __( 'You can set the desired filename prior to download. This is a good SEO idea if you want it, but it\'s a small extra step', 'stockpack' ),
                    'options' => array(
                        'yes' => __( 'Yes', 'stockpack' ),
                        'no'  => __( 'No', 'stockpack' )
                    ),
                    'type'    => 'radio',
                    'default' => 'no',
                ),
                array(
                    'name'    => 'caption_premium_providers',
                    'label'   => __( 'Enable premium providers caption', 'stockpack' ),
                    'desc'    => __( 'You can enable automatic caption for premium providers if you want to', 'stockpack' ),
                    'options' => array(
                        'yes' => __( 'Yes', 'stockpack' ),
                        'no'  => __( 'No', 'stockpack' )
                    ),
                    'type'    => 'radio',
                    'default' => 'no',
                ),
                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',
                ),
            ),
        );
    }

    /**
     * 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 );
    }
}

```
