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

259 lines 8.3 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( 'init', array( $this, 'init' ) );
37 }
38
39 /**
40 *
41 */
42 public function init() {
43 $this->actions();
44 }
45
46 /**
47 *
48 */
49 public function actions() {
50 add_action( 'wp_loaded', array( $this, 'maybe_show_notice' ) );
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 public function register_settings_page() {
70 $capability = apply_filters( 'stockpack_settings_capability', 'upload_files' );
71
72 add_options_page( __( 'StockPack', 'stockpack' ), __( 'StockPack', 'stockpack' ), $capability, 'stockpack',
73 array( $this, 'plugin_page' )
74 );
75 }
76
77 public function maybe_show_notice() {
78 if ( ! PAnD::is_admin_notice_active( 'disable-media-notice-forever' ) ) {
79 return;
80 }
81 add_action( 'admin_init', array( 'PAnD', 'init' ) );
82
83 add_action( 'admin_notices', array( $this, 'media_notice' ) );
84 }
85
86 public function media_notice() {
87 global $pagenow;
88 if ( $pagenow != 'upload.php' ) {
89 return;
90 }
91
92 $capability = apply_filters( 'stockpack_notices_capability', 'upload_files' );
93 if ( current_user_can( $capability ) ) {
94 echo '<div data-dismissible="disable-media-notice-forever" class="notice notice-warning is-dismissible">
95 <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>
96 </div>';
97 }
98
99
100 }
101
102 public function plugin_page() {
103
104 echo '<div class="wrap">';
105 $this->settings_api->show_navigation();
106 $this->settings_api->show_forms();
107 $link_text = esc_textarea( __( 'Get your license key', 'stockpack' ) );
108 echo '<a class="license-key-link" href="https://stockpack.co/register" target="_blank">' . $link_text . '</a>';
109
110 echo '</div>';
111 }
112
113 public function get_license_state() {
114 return $this->get_option( 'license_state', 'stockpack_basics' );
115 }
116
117 public function get_safe_search() {
118 return $this->get_option( 'safe_search', 'stockpack_basics' );
119 }
120
121 public function get_file_name_change_setting() {
122 return $this->get_option( 'file_name_change', 'stockpack_basics' );
123 }
124
125 public function get_premium_providers_caption_setting() {
126 return $this->get_option( 'caption_premium_providers', 'stockpack_basics' );
127 }
128
129 /**
130 * @return mixed
131 */
132 public function get_api_key() {
133 return $this->get_option( 'auth_token', 'stockpack_basics' );
134 }
135
136 /**
137 * @return mixed
138 */
139 public function set_api_key( $token ) {
140 return $this->set_option( 'auth_token', 'stockpack_basics', $token );
141 }
142
143
144 /**
145 * @return array
146 */
147 private function get_settings_sections() {
148 return array(
149 array(
150 'id' => 'stockpack_basics',
151 'title' => __( 'StockPack', 'stockpack' ),
152 ),
153 );
154 }
155
156 /**
157 * @return array
158 */
159 private function get_settings_fields() {
160 return array(
161 'stockpack_basics' => array(
162 array(
163 'name' => 'auth_token',
164 'label' => __( 'License key', 'stockpack' ),
165 'desc' => __( 'This is the license key that is used for authentication', 'stockpack' ),
166 'type' => 'text',
167 'default' => '',
168 'size' => 'validate-stockpack-key regular' //small hack to add class
169 ),
170 array(
171 'name' => 'safe_search',
172 'label' => __( 'Apply safe search', 'stockpack' ),
173 'desc' => __( 'Some providers support this mode to prevent nude images to show up in the search', 'stockpack' ),
174 'options' => array(
175 'yes' => __( 'Yes', 'stockpack' ),
176 'no' => __( 'No', 'stockpack' )
177 ),
178 'type' => 'radio',
179 'default' => 'yes',
180 ),
181 array(
182 'name' => 'file_name_change',
183 'label' => __( 'Enable file name change', 'stockpack' ),
184 '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' ),
185 'options' => array(
186 'yes' => __( 'Yes', 'stockpack' ),
187 'no' => __( 'No', 'stockpack' )
188 ),
189 'type' => 'radio',
190 'default' => 'no',
191 ),
192 array(
193 'name' => 'caption_premium_providers',
194 'label' => __( 'Enable premium providers caption', 'stockpack' ),
195 'desc' => __( 'You can enable automatic caption for premium providers if you want to', 'stockpack' ),
196 'options' => array(
197 'yes' => __( 'Yes', 'stockpack' ),
198 'no' => __( 'No', 'stockpack' )
199 ),
200 'type' => 'radio',
201 'default' => 'no',
202 ),
203 array(
204 'name' => 'license_state',
205 'label' => __( 'Search with authorization', 'stockpack' ),
206 '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' ),
207 'options' => array(
208 'yes' => __( 'Yes', 'stockpack' ),
209 'no' => __( 'No', 'stockpack' )
210 ),
211 'type' => 'radio',
212 'default' => 'no',
213 ),
214 ),
215 );
216 }
217
218 /**
219 * Get 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 $default default text if it's not found
224 *
225 * @return mixed
226 */
227 private function get_option( $option, $section, $default = '' ) {
228
229 $options = get_option( $section, array() );
230
231 if ( isset( $options[ $option ] ) ) {
232 return $options[ $option ];
233 }
234
235 return $default;
236 }
237
238 /**
239 * Set the value of a settings field
240 *
241 * @param string $option settings field name
242 * @param string $section the section name this field belongs to
243 * @param string $value
244 *
245 * @return mixed
246 */
247 private function set_option( $option, $section, $value ) {
248
249 $options = get_option( $section, array() );
250 if ( ! is_array( $options ) ) {
251 $options = array();
252 }
253
254 $options[ $option ] = $value;
255
256 update_option( $section, $options );
257 }
258 }
259