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