PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.5.1
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.5.1
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
stockpack / src / class-stockpackadmin.php

class-stockpackadmin.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 2.5.1, at src/class-stockpackadmin.php

224 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class StockpackAdmin
5 *
6 * Handle the settings admin page
7 */
8 class StockpackAdmin {
9 /**
10 * @var Singleton The reference the *Singleton* instance of this class
11 */
12 private static $instance;
13
14 /**
15 * @var WeDevs_Settings_API
16 */
17 public $settings_api;
18
19 /**
20 * Returns the *Singleton* instance of this class.
21 *
22 * @return Singleton The *Singleton* instance.
23 */
24 public static function get_instance() {
25 if ( null === self::$instance ) {
26 self::$instance = new self();
27 }
28
29 return self::$instance;
30 }
31
32 /**
33 * Stockpack constructor.
34 */
35 protected function __construct() {
36 add_action( 'plugins_loaded', array( $this, 'init' ) );
37 }
38
39 /**
40 *
41 */
42 public function init() {
43 $this->actions();
44 }
45
46
47 /**
48 *
49 */
50 public function actions() {
51 add_action( 'admin_init', array( $this, 'register_settings' ) );
52 add_action( 'admin_menu', array( $this, 'register_settings_page' ) );
53 }
54
55 /**
56 * Register settings
57 */
58 public function register_settings() {
59
60 $this->settings_api = new WeDevs_Settings_API();
61 //set sections and fields
62 $this->settings_api->set_sections( $this->get_settings_sections() );
63 $this->settings_api->set_fields( $this->get_settings_fields() );
64
65 //initialize them
66 $this->settings_api->admin_init();
67 }
68
69 /**
70 *
71 */
72 public function register_settings_page() {
73 add_options_page( __( 'StockPack', 'stockpack' ), __( 'StockPack', 'stockpack' ), 'delete_posts', 'stockpack',
74 array( $this, 'plugin_page' )
75 );
76 }
77
78
79 /**
80 *
81 */
82 public function plugin_page() {
83
84 echo '<div class="wrap">';
85 $this->settings_api->show_navigation();
86 $this->settings_api->show_forms();
87 $link_text = esc_textarea( __( 'Get your license key', 'stockpack' ) );
88 echo '<a class="license-key-link" href="https://stockpack.co/register" target="_blank">' . $link_text . '</a>';
89
90 echo '</div>';
91 }
92
93 public function get_license_state() {
94 return $this->get_option( 'license_state', 'stockpack_basics' );
95 }
96
97 public function get_safe_search() {
98 return $this->get_option( 'safe_search', 'stockpack_basics' );
99 }
100
101 public function get_file_name_change_setting() {
102 return $this->get_option( 'file_name_change', 'stockpack_basics' );
103 }
104
105 /**
106 * @return mixed
107 */
108 public function get_api_key() {
109 return $this->get_option( 'auth_token', 'stockpack_basics' );
110 }
111
112 /**
113 * @return mixed
114 */
115 public function set_api_key( $token ) {
116 return $this->set_option( 'auth_token', 'stockpack_basics', $token );
117 }
118
119
120 /**
121 * @return array
122 */
123 private function get_settings_sections() {
124 return array(
125 array(
126 'id' => 'stockpack_basics',
127 'title' => __( 'StockPack', 'stockpack' ),
128 ),
129 );
130 }
131
132 /**
133 * @return array
134 */
135 private function get_settings_fields() {
136 return array(
137 'stockpack_basics' => array(
138 array(
139 'name' => 'auth_token',
140 'label' => __( 'License key', 'stockpack' ),
141 'desc' => __( 'This is the license key that is used for authentication', 'stockpack' ),
142 'type' => 'text',
143 'default' => '',
144 'size' => 'validate-stockpack-key regular' //small hack to add class
145 ),
146 array(
147 'name' => 'file_name_change',
148 'label' => __( 'Enable file name change', 'stockpack' ),
149 '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' ),
150 'options' => array(
151 'yes' => __( 'Yes', 'stockpack' ),
152 'no' => __( 'No', 'stockpack' )
153 ),
154 'type' => 'radio',
155 'default' => 'no',
156 ),
157 array(
158 'name' => 'license_state',
159 'label' => __( 'Search with authorization', 'stockpack' ),
160 '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' ),
161 'options' => array(
162 'yes' => __( 'Yes', 'stockpack' ),
163 'no' => __( 'No', 'stockpack' )
164 ),
165 'type' => 'radio',
166 'default' => 'no',
167 ),
168 array(
169 'name' => 'safe_search',
170 'label' => __( 'Apply safe search', 'stockpack' ),
171 'desc' => __( 'Some providers support this mode to prevent nude images to show up in the search', 'stockpack' ),
172 'options' => array(
173 'yes' => __( 'Yes', 'stockpack' ),
174 'no' => __( 'No', 'stockpack' )
175 ),
176 'type' => 'radio',
177 'default' => 'yes',
178 ),
179 ),
180 );
181 }
182
183 /**
184 * Get the value of a settings field
185 *
186 * @param string $option settings field name
187 * @param string $section the section name this field belongs to
188 * @param string $default default text if it's not found
189 *
190 * @return mixed
191 */
192 private function get_option( $option, $section, $default = '' ) {
193
194 $options = get_option( $section, array() );
195
196 if ( isset( $options[ $option ] ) ) {
197 return $options[ $option ];
198 }
199
200 return $default;
201 }
202
203 /**
204 * Set the value of a settings field
205 *
206 * @param string $option settings field name
207 * @param string $section the section name this field belongs to
208 * @param string $value
209 *
210 * @return mixed
211 */
212 private function set_option( $option, $section, $value ) {
213
214 $options = get_option( $section, array() );
215 if ( ! is_array( $options ) ) {
216 $options = array();
217 }
218
219 $options[ $option ] = $value;
220
221 update_option( $section, $options );
222 }
223 }
224