Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Old] Do not override feed featured image with ChatGPT image generation unless it is explicit. #882

Closed
wants to merge 4 commits into from
Closed
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
27 changes: 20 additions & 7 deletions includes/admin/feedzy-rss-feeds-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function( $action ) {
* @param string $language_code Feed language code.
* @param array $item Feed item.
* @param string $default_value Default value.
* @return string
* @return string It can be the modified post content or an image URL.
*/
public function run_action_job( $post_content, $import_translation_lang, $job, $language_code, $item, $default_value = '' ) {
$this->item = $item;
Expand All @@ -244,6 +244,9 @@ public function run_action_job( $post_content, $import_translation_lang, $job, $
$this->result = $this->action_process();
}
if ( 'item_image' === $this->type ) {
/**
* The result will be an image URL which will be downloaded later.
*/
$this->post_content = str_replace( $replace_to, $this->result, wp_json_encode( $replace_with ) );
} else {
$this->post_content = str_replace( $replace_to, $this->result, $this->post_content );
Expand Down Expand Up @@ -453,21 +456,31 @@ private function summarize_content() {
}

/**
* Generate item image.
* Generate item image with OpenAI.
*
* @return string
* @return string - The image URL. If the generation is not possible, it will return the default value or an empty string.
*/
private function generate_image() {
$content = call_user_func( array( $this, 'item_title' ) );
if ( ! class_exists( '\Feedzy_Rss_Feeds_Pro_Openai' ) ) {
return isset( $this->default_value ) ? $this->default_value : '';
}
if ( $this->current_job->data->generateImgWithChatGPT && empty( $this->default_value ) ) {

// Skip the image generation as implicit behavior unless it is explicitly requested.
if (
( ! isset( $this->current_job->data->generateOnlyMissingImages ) || ! empty( $this->current_job->data->generateOnlyMissingImages ) )
&& filter_var( $this->default_value, FILTER_VALIDATE_URL )
) {
// Return default image URL of the feed item.
return isset( $this->default_value ) ? $this->default_value : '';
}

$content = call_user_func( array( $this, 'item_title' ) );
if ( empty( $content ) ) {
return isset( $this->default_value ) ? $this->default_value : '';
}

$openai = new \Feedzy_Rss_Feeds_Pro_Openai();
$content = $openai->call_api( $this->settings, $content, 'image', array() );
return $content;
return $openai->call_api( $this->settings, $content, 'image', array() );
}
}
}
181 changes: 117 additions & 64 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,12 @@ private function get_taxonomies() {
private function run_now() {
check_ajax_referer( FEEDZY_BASEFILE, 'security' );

$job = get_post( filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT ) );
$job = get_post( filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT ) );

if ( null === $job ) {
wp_send_json_error( array( 'msg' => __( 'Could not find the associated Import Settings', 'feedzy-rss-feeds' ) ) );
}

$count = $this->run_job( $job, 100 );

$msg = $count > 0 ? __( 'Successfully run!', 'feedzy-rss-feeds' ) : __( 'Nothing imported!', 'feedzy-rss-feeds' );
Expand Down Expand Up @@ -1195,6 +1200,9 @@ public function run_cron( $max = 100 ) {
/**
* Runs a specific job.
*
* @param \WP_Post|int $job The job to run.
* @param int $max The maximum number of items to import.
*
* @return int
* @since 1.6.1
* @access private
Expand Down Expand Up @@ -1663,7 +1671,20 @@ private function run_job( $job, $max ) {
}

if ( ! empty( $image_url ) ) {
$img_success = $this->generate_featured_image( $image_url, 0, $item['item_title'], $import_errors, $import_info, $new_post );
$img_attachment_id = $this->get_featured_image_by_post_title( $item['item_title'] );
// Add the image if the post doesn't have a featured image.
if ( ! $img_attachment_id ) {
$img_success = $this->try_save_featured_image( $image_url, 0, $item['item_title'], $import_errors, $import_info, $new_post );
} else {
$img_success = $img_attachment_id;

if ( ! empty( $new_post ) ) {
$img_success = set_post_thumbnail( 0, $img_attachment_id );
}

// Get attachment ID using the id in $img_success.
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Found an existing attachment for the post with title: %s', $item['item_title'] ), 'debug', __FILE__, __LINE__ );
}
$new_post_id = $img_success;
}

Expand Down Expand Up @@ -1813,22 +1834,29 @@ function( $term ) {
}
}

// Item image action.
$import_featured_img = rawurldecode( $import_featured_img );
$import_featured_img = trim( $import_featured_img );
$img_action = $this->handle_content_actions( $import_featured_img, 'item_image' );
// Item image action process.
$image_url = $img_action->run_action_job( $import_featured_img, $import_translation_lang, $job, $language_code, $item, $image_url );

if ( ! empty( $image_url ) ) {
if ( 'yes' === $import_item_img_url ) {
// Set external image URL.
update_post_meta( $new_post_id, 'feedzy_item_external_url', $image_url );
} else {
// if import_featured_img is a tag.
$img_success = $this->generate_featured_image( $image_url, $new_post_id, $item['item_title'], $import_errors, $import_info );
// Add a featured image to the post if it doesn't already have one.
$img_attachment_id = $this->get_featured_image_by_post_title( $item['item_title'] );
if ( ! $img_attachment_id ) {
$import_featured_img = rawurldecode( $import_featured_img );
$import_featured_img = trim( $import_featured_img );
$img_action = $this->handle_content_actions( $import_featured_img, 'item_image' );

$featured_image_url = $img_action->run_action_job( $import_featured_img, $import_translation_lang, $job, $language_code, $item, $image_url );
if ( ! empty( $featured_image_url ) ) {
if ( 'yes' === $import_item_img_url ) {
// Set external image URL.
update_post_meta( $new_post_id, 'feedzy_item_external_url', $featured_image_url );
} else {
// if import_featured_img is a tag.
$img_success = $this->try_save_featured_image( $featured_image_url, $new_post_id, $item['item_title'], $import_errors, $import_info );
$new_post_id = $img_success;
}
}
} else {
$img_success = set_post_thumbnail( $new_post_id, $img_attachment_id );
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Found an existing attachment for the post with title: %s', $item['item_title'] ), 'debug', __FILE__, __LINE__ );
}

// Set default thumbnail image.
if ( ! $img_success && ! empty( $default_thumbnail ) ) {
$img_success = set_post_thumbnail( $new_post_id, $default_thumbnail );
Expand Down Expand Up @@ -1936,77 +1964,102 @@ public function get_job_feed( $options, $import_content = null, $raw_feed_also =
}

/**
* Downloads and sets a post featured image if possible.
* Check if the past has a featured image.
*
* @param string $file The file URL.
* @param string $post_title The post title.
*
* @return bool
*/
private function check_post_feature_image( $post_title ) {
return 0 !== $this->get_featured_image_by_post_title( $post_title );
}

/**
* Get the featured image by post title.
*
* @param string $post_title The post title.
*
* @return int
*/
private function get_featured_image_by_post_title( $post_title ) {
if ( ! function_exists( 'post_exists' ) ) {
require_once ABSPATH . 'wp-admin/includes/post.php';
}

return post_exists( $post_title, '', '', 'attachment' );
}

/**
* Save the image (with download) as a Post Featured image if possible.
*
* @param string $image_url The image URL.
* @param integer $post_id The post ID.
* @param string $desc Description.
* @param string $post_title Post title.
* @param array $import_errors Array of import error messages.
* @param array $import_info Array of import information messages.
*
* @return bool
* @return bool|int
* @since 1.2.0
* @access private
*/
private function generate_featured_image( $file, $post_id, $desc, &$import_errors, &$import_info, $post_data = array() ) {
if ( ! function_exists( 'post_exists' ) ) {
require_once ABSPATH . 'wp-admin/includes/post.php';
private function try_save_featured_image( $image_url, $post_id, $post_title, &$import_errors, &$import_info, $post_data = array() ) {

// Check if $file is a valid URL.
if ( ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'The provided File URL is not a valid URL: %s with postID %d', substr( $image_url, 0, 100 ), $post_id ), 'debug', __FILE__, __LINE__ );
return false;
}
// Find existing attachment by item title.
$id = post_exists( $desc, '', '', 'attachment' );

if ( ! $id ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Trying to generate featured image for %s and postID %d', $file, $post_id ), 'debug', __FILE__, __LINE__ );
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Trying to generate featured image for %s and postID %d', $image_url, $post_id ), 'debug', __FILE__, __LINE__ );

require_once ABSPATH . 'wp-admin' . '/includes/image.php';
require_once ABSPATH . 'wp-admin' . '/includes/file.php';
require_once ABSPATH . 'wp-admin' . '/includes/media.php';
require_once ABSPATH . 'wp-admin' . '/includes/image.php';
require_once ABSPATH . 'wp-admin' . '/includes/file.php';
require_once ABSPATH . 'wp-admin' . '/includes/media.php';

$file_array = array();
$file = trim( $file, chr( 0xC2 ) . chr( 0xA0 ) );
$local_file = download_url( $file );
if ( is_wp_error( $local_file ) ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Unable to download file = %s and postID %d', print_r( $local_file, true ), $post_id ), 'error', __FILE__, __LINE__ );
$file_array = array();
$image_url = trim( $image_url, chr( 0xC2 ) . chr( 0xA0 ) );

return false;
}
// Download the image on the server.
$local_file = download_url( $image_url );
if ( is_wp_error( $local_file ) ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Unable to download file = %s and postID %d', print_r( $local_file, true ), $post_id ), 'error', __FILE__, __LINE__ );

$type = mime_content_type( $local_file );
// the file is downloaded with a .tmp extension
// if the URL mentions the extension of the file, the upload succeeds
// but if the URL is like https://source.unsplash.com/random, then the upload fails
// so let's determine the file's mime type and then rename the .tmp file with that extension
if ( in_array( $type, array_values( get_allowed_mime_types() ), true ) ) {
$new_local_file = str_replace( '.tmp', str_replace( 'image/', '.', $type ), $local_file );
$renamed = rename( $local_file, $new_local_file );
if ( $renamed ) {
$local_file = $new_local_file;
} else {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Unable to rename file for postID %d', $post_id ), 'error', __FILE__, __LINE__ );
return false;
}

return false;
}
$type = mime_content_type( $local_file );
// the file is downloaded with a .tmp extension
// if the URL mentions the extension of the file, the upload succeeds
// but if the URL is like https://source.unsplash.com/random, then the upload fails
// so let's determine the file's mime type and then rename the .tmp file with that extension
if ( in_array( $type, array_values( get_allowed_mime_types() ), true ) ) {
$new_local_file = str_replace( '.tmp', str_replace( 'image/', '.', $type ), $local_file );
$renamed = rename( $local_file, $new_local_file );
if ( $renamed ) {
$local_file = $new_local_file;
} else {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Unable to rename file for postID %d', $post_id ), 'error', __FILE__, __LINE__ );

return false;
}
}

$file_array['tmp_name'] = $local_file;
$file_array['name'] = basename( $local_file );
$file_array['tmp_name'] = $local_file;
$file_array['name'] = basename( $local_file );

$id = media_handle_sideload( $file_array, $post_id, $desc, $post_data );
if ( is_wp_error( $id ) ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Unable to attach file for postID %d = %s', $post_id, print_r( $id, true ) ), 'error', __FILE__, __LINE__ );
unlink( $file_array['tmp_name'] );
$image_attachment_id = media_handle_sideload( $file_array, $post_id, $post_title, $post_data );
if ( is_wp_error( $image_attachment_id ) ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Unable to attach file for postID %d = %s', $post_id, print_r( $image_attachment_id, true ) ), 'error', __FILE__, __LINE__ );
unlink( $file_array['tmp_name'] );

return false;
}
} else {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Found an existing attachment(ID: %d) image for %s and postID %d', $id, $file, $post_id ), 'debug', __FILE__, __LINE__ );
return false;
}

if ( ! empty( $post_data ) ) {
return $id;
return $image_attachment_id;
}

$success = set_post_thumbnail( $post_id, $id );
$success = set_post_thumbnail( $post_id, $image_attachment_id );
if ( false === $success ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Unable to attach file for postID %d for no apparent reason', $post_id ), 'error', __FILE__, __LINE__ );
} else {
Expand Down Expand Up @@ -2883,7 +2936,7 @@ private function wizard_import_feed() {
*
* @param string $actions Item content actions.
* @param string $type Action type.
* @return object `Feedzy_Rss_Feeds_Actions` class instance.
* @return Feedzy_Rss_Feeds_Actions Instance of Feedzy_Rss_Feeds_Actions.
*/
public function handle_content_actions( $actions = '', $type = '' ) {
$action_instance = Feedzy_Rss_Feeds_Actions::instance();
Expand Down
4 changes: 2 additions & 2 deletions includes/gutenberg/build/block.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/ActionPopup/SortableItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ const SortableItem = ({ propRef, loopIndex, item }) => {
<UpgradeNotice higherPlanNotice={!feedzyData.isBusinessPlan && !feedzyData.isAgencyPlan} utmCampaign="action-generate-image-chatgpt"/>
<BaseControl>
<ToggleControl
checked={ item.data.generateImgWithChatGPT ?? true }
checked={ item.data.generateOnlyMissingImages ?? true }
label={ __( 'Generate only for missing images', 'feedzy-rss-feeds' ) }
onChange={ ( currentValue ) => propRef.onChangeHandler( { 'index': loopIndex, 'generateImgWithChatGPT': currentValue ?? '' } ) }
onChange={ ( currentValue ) => propRef.onChangeHandler( { 'index': loopIndex, 'generateOnlyMissingImages': currentValue ?? '' } ) }
help={ __( 'Only generate the featured image if it\'s missing in the source XML RSS Feed.', 'feedzy-rss-feeds' ) }
disabled={!feedzyData.isPro || !feedzyData.apiLicenseStatus.openaiStatus}
/>
Expand Down
4 changes: 2 additions & 2 deletions js/ActionPopup/action-popup.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/FeedBack/feedback.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions js/Onboarding/import-onboarding.min.js

Large diffs are not rendered by default.

Loading