$data ) {
if ( 'Play.ht' === $data['Name'] && is_plugin_active( $file ) ) {
return $file;
}
}
return '';
}
/** On the Dashboard, the Plugins page and Leelo's settings: switch Play.ht's plugin off. */
public static function playht_notice() {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( ! $screen || ! in_array( $screen->id, array( 'dashboard', 'plugins', 'settings_page_' . self::PAGE ), true ) || ! current_user_can( 'activate_plugins' ) ) {
return;
}
$file = self::playht_file();
if ( '' === $file ) {
return;
}
echo '
' . esc_html__( 'Deactivate the Play.ht plugin to use Leelo.', 'leelo' ) . ' ';
echo esc_html__( 'Play.ht\'s WordPress plugin was closed on WordPress.org in March 2024 for a security issue, and its player no longer plays. While it\'s on, its player shows on your posts instead of Leelo\'s. Your [playht_player] shortcodes stay where they are and play Leelo\'s audio once it\'s off.', 'leelo' );
echo '
', esc_attr( $notice[1] ), esc_html( $notice[0] ) );
}
}
/** The site as Leelo has it (voice, look, credits), kept for five minutes; saving settings refreshes it. */
private static function site() {
$site = get_transient( 'leelo_site' );
if ( false === $site ) {
$site = Leelo_Api::get( '/site' );
if ( is_wp_error( $site ) ) {
return $site;
}
set_transient( 'leelo_site', $site, 5 * MINUTE_IN_SECONDS );
Leelo_Settings::update( array( 'plan' => empty( $site['account']['website_plan'] ) ? 0 : 1 ) );
}
return $site;
}
/** Voices in one language, from Leelo. */
private static function voices( $locale ) {
$key = 'leelo_voices_v2_' . md5( $locale );
$voices = get_transient( $key );
if ( false !== $voices ) {
return $voices;
}
$voices = array();
$after = '';
$complete = false;
// A language has a few hundred voices, and Leelo sends them 200 at a time.
for ( $page = 0; $page < 5; $page++ ) {
$result = Leelo_Api::get( '/voices?locale=' . rawurlencode( $locale ) . '&limit=200' . ( '' !== $after ? '&after=' . rawurlencode( $after ) : '' ) );
if ( is_wp_error( $result ) ) {
break;
}
if ( isset( $result['data'] ) && is_array( $result['data'] ) ) {
$voices = array_merge( $voices, $result['data'] );
}
$after = isset( $result['next'] ) && is_string( $result['next'] ) ? $result['next'] : '';
if ( '' === $after ) {
$complete = true;
break;
}
}
// Website audio uses the catalogue's Core, Pro and Ultra voices (Leelo refuses the others).
$voices = array_values(
array_filter(
$voices,
function ( $v ) {
return is_array( $v ) && ! empty( $v['id'] ) && empty( $v['own'] ) && (int) ( $v['credits_per_word'] ?? 0 ) <= 3;
}
)
);
// By name; the same name always in the same order, so "Emma (2)" stays the same voice.
usort(
$voices,
function ( $a, $b ) {
$by_name = strcasecmp( (string) ( $a['name'] ?? '' ), (string) ( $b['name'] ?? '' ) );
return $by_name ? $by_name : strcmp( (string) $a['id'], (string) $b['id'] );
}
);
if ( $voices ) {
// A list cut short (Leelo out of reach half-way) is asked for again sooner.
set_transient( $key, $voices, $complete ? HOUR_IN_SECONDS : 5 * MINUTE_IN_SECONDS );
}
return $voices;
}
/**
* One part of the voice list, labelled so that no two voices read the same: name, gender, tier,
* how it sounds where Leelo says, and for a voice from another language, that language.
*
* @param array $voices The voices, from voices().
* @param string $locale The language chosen.
* @param array $locales Language names by code.
* @param bool $native The language's own voices (true) or the others that also read it.
* @return array[] Each with id, label and sample (an https address, or '').
*/
private static function voice_options( $voices, $locale, $locales, $native ) {
$tier = array(
'standard' => __( 'Core', 'leelo' ),
'premium' => __( 'Pro', 'leelo' ),
'hd' => __( 'Ultra', 'leelo' ),
);
$out = array();
$seen = array();
foreach ( $voices as $v ) {
$home = (string) ( $v['locale'] ?? '' );
if ( ( $home === $locale ) !== $native ) {
continue;
}
$parts = array(
(string) ( $v['name'] ?? '' ),
ucfirst( (string) ( $v['gender'] ?? '' ) ),
$tier[ $v['quality'] ?? '' ] ?? (string) ( $v['quality'] ?? '' ),
(string) ( $v['description'] ?? '' ),
$native ? '' : self::language_name( $home, $locales ),
);
$label = implode( ' · ', array_filter( $parts, 'strlen' ) );
$seen[ $label ] = ( $seen[ $label ] ?? 0 ) + 1;
if ( $seen[ $label ] > 1 ) {
$label .= ' (' . $seen[ $label ] . ')';
}
$sample = (string) ( $v['sample_url'] ?? '' );
$out[] = array(
'id' => (string) $v['id'],
'label' => $label,
'sample' => 0 === strpos( $sample, 'https://' ) ? $sample : '',
);
}
return $out;
}
/** A language's name from its code ("en-PH" → "English (Philippines)"), or the code. */
private static function language_name( $code, $locales ) {
if ( isset( $locales[ $code ] ) ) {
return $locales[ $code ];
}
if ( function_exists( 'locale_get_display_name' ) ) {
$name = locale_get_display_name( $code, 'en' );
if ( $name ) {
return $name;
}
}
return $code;
}
/** Languages to pick a voice in: the site's own first. */
private static function locales() {
$list = array(
'en-US' => 'English (US)', 'en-GB' => 'English (UK)', 'en-AU' => 'English (Australia)', 'en-CA' => 'English (Canada)',
'es-ES' => 'Spanish (Spain)', 'es-MX' => 'Spanish (Mexico)', 'fr-FR' => 'French (France)', 'fr-CA' => 'French (Canada)',
'de-DE' => 'German', 'it-IT' => 'Italian', 'pt-BR' => 'Portuguese (Brazil)', 'pt-PT' => 'Portuguese (Portugal)',
'nl-NL' => 'Dutch', 'nl-BE' => 'Flemish', 'sv-SE' => 'Swedish', 'da-DK' => 'Danish', 'nb-NO' => 'Norwegian', 'fi-FI' => 'Finnish',
'pl-PL' => 'Polish', 'cs-CZ' => 'Czech', 'ro-RO' => 'Romanian', 'hu-HU' => 'Hungarian', 'el-GR' => 'Greek', 'tr-TR' => 'Turkish',
'ru-RU' => 'Russian', 'uk-UA' => 'Ukrainian', 'sr-Latn-RS' => 'Serbian', 'hr-HR' => 'Croatian', 'ar-SA' => 'Arabic (Saudi)',
'ar-EG' => 'Arabic (Egypt)', 'he-IL' => 'Hebrew', 'hi-IN' => 'Hindi', 'ja-JP' => 'Japanese', 'ko-KR' => 'Korean',
'zh-CN' => 'Chinese (Mandarin)', 'id-ID' => 'Indonesian', 'vi-VN' => 'Vietnamese', 'th-TH' => 'Thai',
);
$own = str_replace( '_', '-', get_locale() );
if ( isset( $list[ $own ] ) ) {
$list = array( $own => $list[ $own ] ) + $list;
}
return $list;
}
public static function page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
echo '
' . esc_html__( 'Leelo – Text to Speech & Listen to this Article', 'leelo' ) . '
';
self::notice();
if ( ! Leelo_Settings::connected() ) {
echo '
' . esc_html__( 'Give every post a "listen to this article" player, read in a natural voice. Connect this site to your Leelo account to start. Automatic audio for your posts comes with a monthly plan, and you can give one post audio free to hear it first.', 'leelo' ) . '
' . esc_html( sprintf( _n( '%d older post is waiting for its audio.', '%d older posts are waiting for their audio, a few every five minutes.', count( $waiting ), 'leelo' ), count( $waiting ) ) ) . '
';
} elseif ( is_array( $estimate ) ) {
echo '
';
printf(
/* translators: 1: number of posts, 2: credits, 3: credits left */
esc_html__( '%1$s posts without audio would cost about %2$s credits. You have %3$s.', 'leelo' ),
esc_html( number_format_i18n( count( $estimate['ids'] ) ) ),
'' . esc_html( number_format_i18n( $estimate['credits'] ) ) . '',
esc_html( number_format_i18n( $estimate['left'] ) )
);
echo '