| @@ -18,9 +18,9 @@ | ||
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * @var string plugin version |
| 21 | 21 | */ |
| 22 | - public $version = '3.2.4'; | |
| 22 | + public $version = '3.7.1'; | |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * Returns the *Singleton* instance of this class. |
| 26 | 26 | * |
| @@ -86,8 +86,10 @@ | ||
| 86 | 86 | $this->settings_api->set_sections( $this->get_settings_sections() ); |
| 87 | 87 | $this->settings_api->set_fields( $this->get_settings_fields() ); |
| 88 | 88 | |
| 89 | 89 | //initialize them |
| 90 | + add_action( 'wsa_form_top_stockpack_basics', array( $this, 'credits_line' ) ); | |
| 91 | + | |
| 90 | 92 | $this->settings_api->admin_init(); |
| 91 | 93 | } |
| 92 | 94 | |
| 93 | 95 | public function register_settings_page() { |
| @@ -269,17 +271,31 @@ | ||
| 269 | 271 | } |
| 270 | 272 | |
| 271 | 273 | private function ai_models(): array { |
| 272 | 274 | $prices = StockPack::get_instance()->query->get_ai_prices(); |
| 275 | + $models = isset( $prices['models'] ) && is_array( $prices['models'] ) ? $prices['models'] : array(); | |
| 273 | 276 | |
| 274 | - return isset( $prices['models'] ) && is_array( $prices['models'] ) ? $prices['models'] : array(); | |
| 277 | + if ( ! $models || ! $this->managed_credits() ) { | |
| 278 | + return $models; | |
| 279 | + } | |
| 280 | + | |
| 281 | + $sellable = array_filter( $models, function ( $model ) { | |
| 282 | + return isset( $model['wallet_credits'] ); | |
| 283 | + } ); | |
| 284 | + | |
| 285 | + return $sellable ?: $models; | |
| 275 | 286 | } |
| 276 | 287 | |
| 277 | 288 | private function ai_model_options(): array { |
| 278 | 289 | $options = array(); |
| 290 | + $managed = $this->managed_credits(); | |
| 279 | 291 | |
| 280 | 292 | foreach ( $this->ai_models() as $key => $model ) { |
| 281 | - $price = isset( $model['eur'] ) ? sprintf( ' (€%s)', rtrim( rtrim( number_format( (float) $model['eur'], 3 ), '0' ), '.' ) ) : ''; | |
| 293 | + if ( $managed ) { | |
| 294 | + $price = isset( $model['wallet_credits'] ) ? sprintf( ' (%s)', $this->format_credits( $model['wallet_credits'] ) ) : ''; | |
| 295 | + } else { | |
| 296 | + $price = isset( $model['eur'] ) ? sprintf( ' (€%s)', rtrim( rtrim( number_format( (float) $model['eur'], 3 ), '0' ), '.' ) ) : ''; | |
| 297 | + } | |
| 282 | 298 | |
| 283 | 299 | $options[ $key ] = ( isset( $model['label'] ) ? $model['label'] : $key ) . $price; |
| 284 | 300 | } |
| 285 | 301 | |
| @@ -285,8 +301,57 @@ | ||
| 285 | 301 | |
| 286 | 302 | return $options; |
| 287 | 303 | } |
| 288 | 304 | |
| 305 | + private function ai_wallet(): array { | |
| 306 | + return StockPack::get_instance()->query->get_ai_wallet(); | |
| 307 | + } | |
| 308 | + | |
| 309 | + private function managed_credits(): bool { | |
| 310 | + $wallet = $this->ai_wallet(); | |
| 311 | + | |
| 312 | + return 'managed' === $wallet['mode']; | |
| 313 | + } | |
| 314 | + | |
| 315 | + private function format_credits( $credits ): string { | |
| 316 | + $credits = (int) $credits; | |
| 317 | + | |
| 318 | + return sprintf( | |
| 319 | + /* translators: %s: number of credits, already formatted */ | |
| 320 | + _n( '%s credit', '%s credits', $credits, 'stockpack' ), | |
| 321 | + number_format_i18n( $credits ) | |
| 322 | + ); | |
| 323 | + } | |
| 324 | + | |
| 325 | + public function credits_line() { | |
| 326 | + $wallet = $this->ai_wallet(); | |
| 327 | + | |
| 328 | + if ( 'byok' === $wallet['mode'] ) { | |
| 329 | + return; | |
| 330 | + } | |
| 331 | + | |
| 332 | + if ( 'unavailable' === $wallet['mode'] ) { | |
| 333 | + printf( | |
| 334 | + '<p class="description stockpack-credits-line">%s <a href="%s" target="_blank" rel="noopener">%s</a> · <a href="%s" target="_blank" rel="noopener">%s</a></p>', | |
| 335 | + esc_html__( 'Image generation is not set up for this site yet. Connect your own Magnific key on stockpack.co, or buy StockPack credits.', 'stockpack' ), | |
| 336 | + esc_url( $wallet['connect_url'] ), | |
| 337 | + esc_html__( 'Connect Magnific', 'stockpack' ), | |
| 338 | + esc_url( $wallet['top_up_url'] ), | |
| 339 | + esc_html__( 'Top up credits', 'stockpack' ) | |
| 340 | + ); | |
| 341 | + | |
| 342 | + return; | |
| 343 | + } | |
| 344 | + | |
| 345 | + printf( | |
| 346 | + '<p class="description stockpack-credits-line">%s · <a href="%s" target="_blank" rel="noopener">%s</a></p>', | |
| 347 | + /* translators: %s: credit balance, already formatted */ | |
| 348 | + esc_html( sprintf( __( 'Credits: %s', 'stockpack' ), $wallet['balance_text'] ) ), | |
| 349 | + esc_url( $wallet['top_up_url'] ), | |
| 350 | + esc_html__( 'Manage on stockpack.co', 'stockpack' ) | |
| 351 | + ); | |
| 352 | + } | |
| 353 | + | |
| 289 | 354 | private function ai_model_defaults(): array { |
| 290 | 355 | $defaults = array(); |
| 291 | 356 | |
| 292 | 357 | foreach ( $this->ai_models() as $key => $model ) { |
| @@ -299,8 +364,21 @@ | ||
| 299 | 364 | } |
| 300 | 365 | |
| 301 | 366 | private function ai_models_description(): string { |
| 302 | 367 | $all = count( $this->ai_models() ); |
| 368 | + | |
| 369 | + if ( $this->managed_credits() ) { | |
| 370 | + return sprintf( | |
| 371 | + /* translators: %d: number of models sellable on StockPack credits */ | |
| 372 | + _n( | |
| 373 | + 'Choose which models appear in the generate dropdown. %d is available on StockPack credits and its price is per image. Models Magnific does not let us resell stay available on your own Magnific key.', | |
| 374 | + 'Choose which models appear in the generate dropdown. %d are available on StockPack credits and prices are per image. Models Magnific does not let us resell stay available on your own Magnific key.', | |
| 375 | + $all, | |
| 376 | + 'stockpack' | |
| 377 | + ), | |
| 378 | + $all | |
| 379 | + ); | |
| 380 | + } | |
| 303 | 381 | |
| 304 | 382 | return sprintf( |
| 305 | 383 | /* translators: %d: total number of models offered by the provider */ |
| 306 | 384 | __( 'Choose which models appear in the generate dropdown. %d are available and prices are per image, charged to the Magnific account you connect. Cheaper models are good for trying out a prompt; switch to a dearer one once you are happy with it.', 'stockpack' ), |