PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.5.4
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.5.4
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.4, at src/class-stockpackadmin.php

239 lines 7.1 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 public function get_premium_providers_caption_setting() {
106 return $this->get_option( 'caption_premium_providers', 'stockpack_basics' );
107 }
108
109 /**
110 * @return mixed
111 */
112 public function get_api_key() {
113 return $this->get_option( 'auth_token', 'stockpack_basics' );
114 }
115
116 /**
117 * @return mixed
118 */
119 public function set_api_key( $token ) {
120 return $this->set_option( 'auth_token', 'stockpack_basics', $token );
121 }
122
123
124 /**
125 * @return array
126 */
127 private function get_settings_sections() {
128 return array(
129 array(
130 'id' => 'stockpack_basics',
131 'title' => __( 'StockPack', 'stockpack' ),
132 ),
133 );
134 }
135
136 /**
137 * @return array
138 */
139 private function get_settings_fields() {
140 return array(
141 'stockpack_basics' => array(
142 array(
143 'name' => 'auth_token',
144 'label' => __( 'License key', 'stockpack' ),
145 'desc' => __( 'This is the license key that is used for authentication', 'stockpack' ),
146 'type' => 'text',
147 'default' => '',
148 'size' => 'validate-stockpack-key regular' //small hack to add class
149 ),
150 array(
151 'name' => 'safe_search',
152 'label' => __( 'Apply safe search', 'stockpack' ),
153 'desc' => __( 'Some providers support this mode to prevent nude images to show up in the search', 'stockpack' ),
154 'options' => array(
155 'yes' => __( 'Yes', 'stockpack' ),
156 'no' => __( 'No', 'stockpack' )
157 ),
158 'type' => 'radio',
159 'default' => 'yes',
160 ),
161 array(
162 'name' => 'file_name_change',
163 'label' => __( 'Enable file name change', 'stockpack' ),
164 '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' ),
165 'options' => array(
166 'yes' => __( 'Yes', 'stockpack' ),
167 'no' => __( 'No', 'stockpack' )
168 ),
169 'type' => 'radio',
170 'default' => 'no',
171 ),
172 array(
173 'name' => 'caption_premium_providers',
174 'label' => __( 'Enable premium providers caption', 'stockpack' ),
175 'desc' => __( 'You can enable automatic caption for premium providers if you want to', 'stockpack' ),
176 'options' => array(
177 'yes' => __( 'Yes', 'stockpack' ),
178 'no' => __( 'No', 'stockpack' )
179 ),
180 'type' => 'radio',
181 'default' => 'no',
182 ),
183 array(
184 'name' => 'license_state',
185 'label' => __( 'Search with authorization', 'stockpack' ),
186 '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' ),
187 'options' => array(
188 'yes' => __( 'Yes', 'stockpack' ),
189 'no' => __( 'No', 'stockpack' )
190 ),
191 'type' => 'radio',
192 'default' => 'no',
193 ),
194 ),
195 );
196 }
197
198 /**
199 * Get the value of a settings field
200 *
201 * @param string $option settings field name
202 * @param string $section the section name this field belongs to
203 * @param string $default default text if it's not found
204 *
205 * @return mixed
206 */
207 private function get_option( $option, $section, $default = '' ) {
208
209 $options = get_option( $section, array() );
210
211 if ( isset( $options[ $option ] ) ) {
212 return $options[ $option ];
213 }
214
215 return $default;
216 }
217
218 /**
219 * Set the value of a settings field
220 *
221 * @param string $option settings field name
222 * @param string $section the section name this field belongs to
223 * @param string $value
224 *
225 * @return mixed
226 */
227 private function set_option( $option, $section, $value ) {
228
229 $options = get_option( $section, array() );
230 if ( ! is_array( $options ) ) {
231 $options = array();
232 }
233
234 $options[ $option ] = $value;
235
236 update_option( $section, $options );
237 }
238 }
239