Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions includes/Classifai/Admin/Onboarding.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,6 @@ public function is_onboarding_completed(): bool {
* @param array $options The options to update.
*/
public function update_onboarding_options( array $options ) {
if ( ! is_array( $options ) ) {
return;
}

$onboarding_options = $this->get_onboarding_options();
$onboarding_options = array_merge( $onboarding_options, $options );

Expand Down
12 changes: 7 additions & 5 deletions includes/Classifai/Admin/SimilarTermsListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ public function prepare_items() {
]
);

$total = is_wp_error( $total ) ? 0 : (int) $total;

$this->set_pagination_args(
array(
'total_items' => $total, // WE have to calculate the total number of items.
'per_page' => $per_page, // WE have to determine how many items to show on a page.
'total_pages' => ceil( $total / $per_page ), // WE have to calculate the total number of pages.
'total_pages' => (int) ceil( $total / $per_page ), // WE have to calculate the total number of pages.
)
);

Expand Down Expand Up @@ -143,9 +145,9 @@ public function prepare_items() {
/**
* Generate term html to show it in Similar terms list table
*
* @param WP_Term $term Term Object.
* @param WP_Term $similar_term Similar Term Object.
* @param float $score Similarity score.
* @param \WP_Term $term Term Object.
* @param \WP_Term $similar_term Similar Term Object.
* @param float $score Similarity score.
* @return string
*/
public function generate_term_html( $term, $similar_term, $score = null ) {
Expand Down Expand Up @@ -173,7 +175,7 @@ public function generate_term_html( $term, $similar_term, $score = null ) {
( $score ? __( '<span><strong>Similarity:</strong> %6$s</span><br/>', 'classifai' ) : '%6$s' ) .
'<a href="%7$s" class="button button-primary term-merge-button">%8$s</a>',
esc_html( $term->name ),
'<a href="' . esc_url( get_edit_term_link( $term->term_id, $term->taxonomy ) ) . '" target="_blank">' . esc_html( $term->term_id ) . '</a>',
'<a href="' . esc_url( get_edit_term_link( $term->term_id, $term->taxonomy ) ) . '" target="_blank">' . esc_html( (string) $term->term_id ) . '</a>',
esc_html( $term->slug ),
// translators: %d: Term count.
'<a href="' . esc_url( admin_url( 'edit.php?tag=' . $term->slug ) ) . '" target="_blank">' . esc_html( sprintf( _n( '%d time', '%d times', $term->count, 'classifai' ), $term->count ) ) . '</a>',
Expand Down
21 changes: 17 additions & 4 deletions includes/Classifai/Admin/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Classifai\Admin;

use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
use YahnisElsts\PluginUpdateChecker\v5p1\Vcs\PluginUpdateChecker;

if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand All @@ -28,7 +29,7 @@ class Update {
/**
* The update checker object.
*
* @var YahnisElsts\PluginUpdateChecker\v5p1\Vcs\PluginUpdateChecker
* @var \YahnisElsts\PluginUpdateChecker\v5p1\Vcs\PluginUpdateChecker
*/
protected $updater;

Expand Down Expand Up @@ -56,13 +57,25 @@ public function register() {
* @return void
*/
public function init() {
$this->updater = PucFactory::buildUpdateChecker(
$updater = PucFactory::buildUpdateChecker(
self::$repo_url,
CLASSIFAI_PLUGIN,
'classifai'
);

$this->updater->getVcsApi()->enableReleaseAssets();
// We build from a VCS (GitHub) URL, so this is always a VCS plugin update checker.
if ( ! $updater instanceof PluginUpdateChecker ) {
return;
}

$this->updater = $updater;

$vcs_api = $this->updater->getVcsApi();

// Release assets are only supported by the GitHub/GitLab VCS APIs.
if ( method_exists( $vcs_api, 'enableReleaseAssets' ) ) {
$vcs_api->enableReleaseAssets();
}

$this->updater->addResultFilter(
function ( $plugin_info ) {
Expand Down Expand Up @@ -93,7 +106,7 @@ public function maybe_update( $transient ) {

// Adding the plugin info to the `no_update` property is required
// for the enable/disable auto-update links to appear correctly in the UI.
if ( $update ) {
if ( $update && property_exists( $update, 'filename' ) ) {
$transient->no_update[ $update->filename ] = $update;
}
}
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Admin/UserProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function get_allowed_features( int $user_id ): array {
return array();
}

$user_roles = $user->roles ?? [];
$user_roles = $user->roles;
$allowed_features = array();

$services = get_plugin()->services;
Expand Down
50 changes: 27 additions & 23 deletions includes/Classifai/Command/ClassifaiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public function text_to_speech( $args = [], $opts = [] ) {
// If we have a post type specified, process all items in that type.
if ( ! empty( $opts['post_type'] ) ) {
// Only allow processing post types that are enabled in settings.
if ( $opts['post_type'] && ! in_array( $opts['post_type'], $allowed_post_types, true ) ) {
if ( ! in_array( $opts['post_type'], $allowed_post_types, true ) ) {
\WP_CLI::error( sprintf( 'The "%s" post type is not enabled for Text to Speech processing', $opts['post_type'] ) );
}

Expand All @@ -301,11 +301,11 @@ public function text_to_speech( $args = [], $opts = [] ) {
do {
$posts = get_posts(
array(
'post_type' => $opts['post_type'],
'posts_per_page' => $opts['per_page'],
'post_type' => (string) $opts['post_type'],
'posts_per_page' => (int) $opts['per_page'],
'paged' => $paged,
'post_status' => $opts['post_status'],
'suppress_filters' => 'false',
'post_status' => (string) $opts['post_status'],
'suppress_filters' => false,
'fields' => 'ids',
)
);
Expand Down Expand Up @@ -571,10 +571,10 @@ public function transcribe_audio( $args = [], $opts = [] ) {
$attachments = get_posts(
array(
'post_type' => 'attachment',
'posts_per_page' => $opts['per_page'],
'post_mime_type' => array_unique( $mime_types ),
'posts_per_page' => (int) $opts['per_page'],
'post_mime_type' => implode( ',', array_unique( $mime_types ) ),
'paged' => $paged,
'suppress_filters' => 'false',
'suppress_filters' => false,
'fields' => 'ids',
)
);
Expand Down Expand Up @@ -700,11 +700,11 @@ public function generate_excerpt( $args = [], $opts = [] ) {
do {
$posts = get_posts(
array(
'post_type' => $opts['post_type'],
'posts_per_page' => $opts['per_page'],
'post_type' => (string) $opts['post_type'],
'posts_per_page' => (int) $opts['per_page'],
'paged' => $paged,
'post_status' => $opts['post_status'],
'suppress_filters' => 'false',
'post_status' => (string) $opts['post_status'],
'suppress_filters' => false,
)
);
$total = count( $posts );
Expand All @@ -728,7 +728,7 @@ public function generate_excerpt( $args = [], $opts = [] ) {
\WP_CLI::log( sprintf( 'Excerpt returned for item ID %d: %s', $post->ID, $result ) );

// Update excerpt if not doing a dry run and we have a valid result.
if ( ! $dry_run && ! is_wp_error( $result ) ) {
if ( ! $dry_run ) {
wp_update_post(
array(
'ID' => $post->ID,
Expand Down Expand Up @@ -781,7 +781,7 @@ public function generate_excerpt( $args = [], $opts = [] ) {
\WP_CLI::log( sprintf( 'Excerpt returned for item ID %d: %s', $post_id, $result ) );

// Update excerpt if not doing a dry run and we have a valid result.
if ( ! $dry_run && ! is_wp_error( $result ) ) {
if ( ! $dry_run ) {
wp_update_post(
array(
'ID' => $post_id,
Expand Down Expand Up @@ -954,9 +954,13 @@ public function image( $args = [], $opts = [] ) {
public function crop( $args = [], $opts = [] ) {
$image_cropping = new ImageCropping();
$provider = $image_cropping->get_feature_provider_instance();
$provider_class = get_class( $provider );
$settings = $image_cropping->get_settings( $provider_class::ID );
$default_opts = [

if ( ! $provider ) {
\WP_CLI::error( 'No provider is configured for the Image Cropping feature.' );
}

$settings = $image_cropping->get_settings( $provider::ID );
$default_opts = [
'limit' => false,
];

Expand Down Expand Up @@ -1058,7 +1062,7 @@ public function embeddings( $args = [], $opts = [] ) {
\WP_CLI::error( 'This command is only available for the OpenAI Embeddings and Azure OpenAI Embeddings providers.' );
}

$embeddings = new Embeddings( false );
$embeddings = new Embeddings( null );
$opts = wp_parse_args( $opts, $defaults );
$opts['per_page'] = (int) $opts['per_page'] > 0 ? $opts['per_page'] : 100;
$allowed_post_types = $feature->get_supported_post_types();
Expand All @@ -1085,7 +1089,7 @@ public function embeddings( $args = [], $opts = [] ) {
// If we have a post type specified, process all items in that type.
if ( ! empty( $opts['post_type'] ) ) {
// Only allow processing post types that are enabled in settings.
if ( $opts['post_type'] && ! in_array( $opts['post_type'], $allowed_post_types, true ) ) {
if ( ! in_array( $opts['post_type'], $allowed_post_types, true ) ) {
\WP_CLI::error( sprintf( 'The "%s" post type is not enabled for OpenAI Embeddings processing', $opts['post_type'] ) );
}

Expand All @@ -1101,11 +1105,11 @@ public function embeddings( $args = [], $opts = [] ) {
do {
$posts = get_posts(
array(
'post_type' => $opts['post_type'],
'posts_per_page' => $opts['per_page'],
'post_type' => (string) $opts['post_type'],
'posts_per_page' => (int) $opts['per_page'],
'paged' => $paged,
'post_status' => $opts['post_status'],
'suppress_filters' => 'false',
'post_status' => (string) $opts['post_status'],
'suppress_filters' => false,
'fields' => 'ids',
)
);
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Features/APIUsageTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function rest_api_init() {
*
* @param WP_REST_Request $request The full request object.
*
* @return \WP_REST_Response
* @return \WP_REST_Response|\WP_Error
*/
public function rest_endpoint_callback( WP_REST_Request $request ) {

Expand Down
20 changes: 15 additions & 5 deletions includes/Classifai/Features/AudioTranscriptsGeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function add_buttons_to_media_modal( array $form_fields, ?\WP_Post $attac
$form_fields['retranscribe'] = [
'label' => __( 'Transcribe audio', 'classifai' ),
'input' => 'html',
'html' => '<button class="button secondary" id="classifai-retranscribe" data-id="' . esc_attr( absint( $attachment->ID ) ) . '">' . esc_html( $text ) . '</button><span class="spinner" style="display:none;float:none;"></span><span class="error" style="display:none;color:#bc0b0b;padding:5px;"></span>',
'html' => '<button class="button secondary" id="classifai-retranscribe" data-id="' . esc_attr( (string) absint( $attachment->ID ) ) . '">' . esc_html( $text ) . '</button><span class="spinner" style="display:none;float:none;"></span><span class="error" style="display:none;color:#bc0b0b;padding:5px;"></span>',
'show_in_edit' => false,
];

Expand Down Expand Up @@ -248,11 +248,11 @@ public function transcribe_audio( int $attachment_id ) {
*/
public function maybe_transcribe_audio( int $attachment_id ) {
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! current_user_can( 'edit_post', $attachment_id ) ) {
return;
return null;
}

if ( empty( $_POST['classifai_audio_transcript_meta'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['classifai_audio_transcript_meta'] ) ), 'classifai_audio_transcript_meta_action' ) ) {
return;
return null;
}

if ( clean_input( 'retranscribe' ) ) {
Expand All @@ -261,6 +261,8 @@ public function maybe_transcribe_audio( int $attachment_id ) {

return $this->transcribe_audio( $attachment_id );
}

return null;
}

/**
Expand Down Expand Up @@ -296,20 +298,28 @@ public function should_process( int $attachment_id ): bool {
$provider_id = $settings['provider'];
$provider_instance = $this->get_feature_provider_instance( $provider_id );

$file_formats = [ 'mp3' ];
$max_file_size = 25 * MB_IN_BYTES;

if ( $provider_instance instanceof OpenAISpeechToText || $provider_instance instanceof ElevenLabsSpeechToText ) {
$file_formats = $provider_instance->file_formats;
$max_file_size = $provider_instance->max_file_size;
}

$mime_type = get_post_mime_type( $attachment_id );
$matched_extensions = explode( '|', array_search( $mime_type, wp_get_mime_types(), true ) );
$process = false;

foreach ( $matched_extensions as $ext ) {
if ( in_array( $ext, $provider_instance->file_formats ?? [ 'mp3' ], true ) ) {
if ( in_array( $ext, $file_formats, true ) ) {
$process = true;
}
}

// If we have a proper file format, check the file size.
if ( $process ) {
$filesize = filesize( get_attached_file( $attachment_id ) );
if ( ! $filesize || $filesize > $provider_instance->max_file_size ?? 25 * MB_IN_BYTES ) {
if ( ! $filesize || $filesize > $max_file_size ) {
$process = false;
}
}
Expand Down
19 changes: 9 additions & 10 deletions includes/Classifai/Features/Classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,14 @@ public function save( int $post_id, array $results, bool $link = true ) {
*/
$results = apply_filters( 'classifai_' . static::ID . '_pre_save_results', $results, $post_id, $link, $this );

switch ( $provider_instance::ID ) {
case NLU::ID:
$results = $provider_instance->link( $post_id, $results, $link );
break;
case AzureEmbeddings::ID:
case OpenAIEmbeddings::ID:
case OllamaEmbeddings::ID:
$results = $provider_instance->set_terms( $post_id, $results, $link );
break;
if ( $provider_instance instanceof NLU ) {
$results = $provider_instance->link( $post_id, $results, $link );
} elseif (
$provider_instance instanceof AzureEmbeddings
|| $provider_instance instanceof OpenAIEmbeddings
|| $provider_instance instanceof OllamaEmbeddings
) {
$results = $provider_instance->set_terms( $post_id, $results, $link );
}

return $results;
Expand Down Expand Up @@ -717,7 +716,7 @@ public function render_previewer( string $active_feature ) {
<div id="classifai-post-preview-controls">
<select id="classifai-preview-post-selector">
<?php foreach ( $posts_to_preview as $post ) : ?>
<option value="<?php echo esc_attr( $post->ID ); ?>"><?php echo esc_html( $post->post_title ); ?></option>
<option value="<?php echo esc_attr( (string) $post->ID ); ?>"><?php echo esc_html( $post->post_title ); ?></option>
<?php endforeach; ?>
</select>

Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Features/ContentGeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function get_feature_default_settings(): array {
/**
* Returns the settings for the feature.
*
* @param string $index The index of the setting to return.
* @param string|false $index The index of the setting to return.
* @return array|mixed
*/
public function get_settings( $index = false ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Features/ContentResizing.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function get_feature_default_settings(): array {
/**
* Returns the settings for the feature.
*
* @param string $index The index of the setting to return.
* @param string|false $index The index of the setting to return.
* @return array|mixed
*/
public function get_settings( $index = false ) {
Expand Down
4 changes: 2 additions & 2 deletions includes/Classifai/Features/DescriptiveTextGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function add_rescan_button_to_media_modal( array $form_fields, ?\WP_Post
'label' => __( 'Descriptive text', 'classifai' ),
'input' => 'html',
'show_in_edit' => false,
'html' => '<button class="button secondary" id="classifai-rescan-alt-tags" data-id="' . esc_attr( absint( $post->ID ) ) . '">' . esc_html( $alt_tags_text ) . '</button><span class="spinner" style="display:none;float:none;"></span><span class="error" style="display:none;color:#bc0b0b;padding:5px;"></span>',
'html' => '<button class="button secondary" id="classifai-rescan-alt-tags" data-id="' . esc_attr( (string) absint( $post->ID ) ) . '">' . esc_html( $alt_tags_text ) . '</button><span class="spinner" style="display:none;float:none;"></span><span class="error" style="display:none;color:#bc0b0b;padding:5px;"></span>',
];

return $form_fields;
Expand Down Expand Up @@ -411,7 +411,7 @@ public function get_feature_default_settings(): array {
/**
* Returns the settings for the feature.
*
* @param string $index The index of the setting to return.
* @param string|false $index The index of the setting to return.
* @return array|mixed
*/
public function get_settings( $index = false ) {
Expand Down
4 changes: 2 additions & 2 deletions includes/Classifai/Features/ExcerptGeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function rest_endpoint_callback( WP_REST_Request $request ) {
if ( empty( $author_name ) && $post_id ) {
$post = get_post( $post_id );
if ( $post ) {
$author_name = get_the_author_meta( 'display_name', $post->post_author );
$author_name = get_the_author_meta( 'display_name', (int) $post->post_author );
}
}

Expand Down Expand Up @@ -393,7 +393,7 @@ public function get_feature_default_settings(): array {
/**
* Returns the settings for the feature.
*
* @param string $index The index of the setting to return.
* @param string|false $index The index of the setting to return.
* @return array|mixed
*/
public function get_settings( $index = false ) {
Expand Down
Loading
Loading