Skip to content

Commit

Permalink
Merge branch 'development' into fix/title-with-chain-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Mar 29, 2024
2 parents 1103ca7 + 2e6c802 commit 294c291
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 59 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,9 @@ input.fz-switch-toggle[type=checkbox]:checked:before{
}
.fz-form-wrap .tagify__tag{
position: relative;
margin-left: 0;
margin-right: 16px;
margin-bottom: 16px;
margin-left: 2px;
margin-right: 2px;
margin-bottom: 2px;
vertical-align: top;
}
.fz-form-wrap .tagify__tag>div{
Expand Down
1 change: 1 addition & 0 deletions feedzy-rss-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function run_feedzy_rss_feeds() {
define( 'FEEDZY_REST_VERSION', '1' );
// to redirect all themeisle_log_event to error log.
define( 'FEEDZY_LOCAL_DEBUG', false );
define( 'FEEDZY_FEED_CUSTOM_TAG_NAMESPACE', 'http://feedzy.themeisle.com' );

// always make this true before testing
// also used in gutenberg.
Expand Down
22 changes: 14 additions & 8 deletions includes/abstract/feedzy-rss-feeds-admin-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ public function get_short_code_attributes( $atts ) {
*
* @param string $raw Url or list of urls.
*
* @return mixed|void Urls of the feeds.
* @return string|array<string> Urls of the feeds.
*/
public function normalize_urls( $raw ) {
$feeds = apply_filters( 'feedzy_process_feed_source', $raw );
Expand Down Expand Up @@ -1412,12 +1412,12 @@ public function get_feed_array( $feed_items, $sc, $feed, $feed_url, $sizes ) {
* @since 3.0.0
* @access private
*
* @param array $sc The shorcode attributes array.
* @param array $sizes The sizes array.
* @param object $item The feed item object.
* @param string $feed_url The feed url.
* @param int $index The item number (may not be the same as the item_index).
* @param int $item_index The real index of this items in the feed (maybe be different from $index if filters are used).
* @param array $sc The shorcode attributes array.
* @param array $sizes The sizes array.
* @param \SimplePie_Item $item The feed item object.
* @param string $feed_url The feed url.
* @param int $index The item number (may not be the same as the item_index).
* @param int $item_index The real index of this items in the feed (maybe be different from $index if filters are used).
*
* @return array
*/
Expand Down Expand Up @@ -1515,8 +1515,14 @@ private function get_feed_item_filter( $sc, $sizes, $item, $feed_url, $index, $i

// multiple sources?
$is_multiple = is_array( $feed_url );
$feed_source = $item->get_feed()->get_title();

$feed_source = '';
$feed_source_tags = $item->get_item_tags( FEEDZY_FEED_CUSTOM_TAG_NAMESPACE, 'parent-source' );
if ( ! empty( $feed_source_tags ) && ! empty( $feed_source_tags[0]['data'] ) ) {
$feed_source = $feed_source_tags[0]['data'];
} else {
$feed_source = $item->get_feed()->get_title();
}
// author.
if ( $item->get_author() && $meta_args['author'] ) {
$author = $item->get_author();
Expand Down
18 changes: 11 additions & 7 deletions includes/admin/feedzy-rss-feeds-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,21 +473,25 @@ private function summarize_content() {
}

/**
* Generate item image.
* Generate item image using OpenAI.
* Return default value if OpenAI is not available or `Generate only for missing images` option is enabled and feed has image.
*
* @return string
* @return string Image URL to download.
*/
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 ) ) {

$feed_has_image = false !== filter_var( $this->default_value, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED );
if ( ( ! isset( $this->current_job->data->generateOnlyMissingImages ) || ! empty( $this->current_job->data->generateOnlyMissingImages ) ) && $feed_has_image ) {
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;

$prompt = call_user_func( array( $this, 'item_title' ) );
$openai = new \Feedzy_Rss_Feeds_Pro_Openai();
return $openai->call_api( $this->settings, $prompt, 'image', array() );
}
}
}
69 changes: 39 additions & 30 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,9 @@ private function run_job( $job, $max ) {
$item_date = date( get_option( 'date_format' ) . ' at ' . get_option( 'time_format' ), $item['item_date'] );
$item_date = $item['item_date_formatted'];

// Transform any structure like [[{"value":"[#item_title]"}]] to [#item_title].
$import_title = preg_replace( '/\[\[\{"value":"(\[#[^]]+\])"\}\]\]/', '$1', $import_title );

// Get translated item title.
$translated_title = '';
if ( $import_auto_translation && ( false !== strpos( $import_title, '[#translated_title]' ) || false !== strpos( $post_excerpt, '[#translated_title]' ) ) ) {
Expand Down Expand Up @@ -1654,16 +1657,16 @@ private function run_job( $job, $max ) {
}

if ( 'attachment' === $import_post_type ) {
$image_url = '';
$img_success = true;
$new_post_id = 0;
$default_img_tag = ! empty( $import_featured_img ) ? '[#item_image]' : '';
$image_source_url = '';
$img_success = true;
$new_post_id = 0;
$default_img_tag = ! empty( $import_featured_img ) ? '[#item_image]' : '';

// image tag
if ( strpos( $default_img_tag, '[#item_image]' ) !== false ) {
// image exists in item
if ( ! empty( $item['item_img_path'] ) ) {
$image_url = str_replace( '[#item_image]', $item['item_img_path'], $default_img_tag );
$image_source_url = str_replace( '[#item_image]', $item['item_img_path'], $default_img_tag );
} else {
$img_success = false;
}
Expand All @@ -1674,16 +1677,16 @@ private function run_job( $job, $max ) {
}

if ( ! empty( $value ) && strpos( $value, '[#item_custom' ) === false ) {
$image_url = $value;
$image_source_url = $value;
} else {
$img_success = false;
}
} else {
$image_url = $default_img_tag;
$image_source_url = $default_img_tag;
}

if ( ! empty( $image_url ) ) {
$img_success = $this->generate_featured_image( $image_url, 0, $item['item_title'], $import_errors, $import_info, $new_post );
if ( ! empty( $image_source_url ) ) {
$img_success = $this->try_save_featured_image( $image_source_url, 0, $item['item_title'], $import_errors, $import_info, $new_post );
$new_post_id = $img_success;
}

Expand Down Expand Up @@ -1766,14 +1769,14 @@ function( $term ) {

$default_img_tag = ! empty( $import_featured_img ) ? '[#item_image]' : '';
if ( ! empty( $default_img_tag ) && 'attachment' !== $import_post_type ) {
$image_url = '';
$image_source_url = '';
$img_success = true;

// image tag
if ( strpos( $default_img_tag, '[#item_image]' ) !== false ) {
// image exists in item
if ( ! empty( $item['item_img_path'] ) ) {
$image_url = str_replace( '[#item_image]', $item['item_img_path'], $default_img_tag );
$image_source_url = str_replace( '[#item_image]', $item['item_img_path'], $default_img_tag );
} else {
$img_success = false;
}
Expand All @@ -1783,14 +1786,14 @@ function( $term ) {
$value = apply_filters( 'feedzy_parse_custom_tags', $default_img_tag, $item_obj );
}
if ( ! empty( $value ) && strpos( $value, '[#item_custom' ) === false ) {
$image_url = $value;
$image_source_url = $value;
} else {
$img_success = false;
}
}

// Fetch image from graby.
if ( empty( $image_url ) && ( wp_doing_cron() && defined( 'FEEDZY_PRO_FETCH_ITEM_IMG_URL' ) ) ) {
if ( empty( $image_source_url ) && ( wp_doing_cron() && defined( 'FEEDZY_PRO_FETCH_ITEM_IMG_URL' ) ) ) {
// if license does not exist, use the site url
// this should obviously never happen unless on dev instances.
$license = apply_filters( 'product_feedzy_license_key', sprintf( 'n/a - %s', get_site_url() ) );
Expand Down Expand Up @@ -1821,7 +1824,7 @@ function( $term ) {
if ( ! is_wp_error( $body ) ) {
$response_data = json_decode( $body, true );
if ( isset( $response_data['url'] ) ) {
$image_url = $response_data['url'];
$image_source_url = $response_data['url'];
}
} else {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
Expand All @@ -1839,15 +1842,15 @@ function( $term ) {
$import_featured_img = trim( $import_featured_img );
$img_action = $this->get_actions_runner( $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 );
$image_source_url = $img_action->run_action_job( $import_featured_img, $import_translation_lang, $job, $language_code, $item, $image_source_url );

if ( ! empty( $image_url ) ) {
if ( ! empty( $image_source_url ) ) {
if ( 'yes' === $import_item_img_url ) {
// Set external image URL.
update_post_meta( $new_post_id, 'feedzy_item_external_url', $image_url );
update_post_meta( $new_post_id, 'feedzy_item_external_url', $image_source_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 );
$img_success = $this->try_save_featured_image( $image_source_url, $new_post_id, $item['item_title'], $import_errors, $import_info );
}
}
}
Expand Down Expand Up @@ -1985,33 +1988,39 @@ public function tryReuseExistingFeaturedImage( &$result, $title_feed, $post_id =
/**
* Downloads and sets a post featured image if possible.
*
* @param string $file The file URL.
* @param string $img_source_url The download source URL for the image.
* @param integer $post_id The post ID.
* @param string $desc Description.
* @param string $post_title Post title. Used to check if an attachment with the same title exists.
* @param array $import_errors Array of import error messages.
* @param array $import_info Array of import information messages.
*
* @return bool
* @return bool|int Return the attachment ID if the image was successfully attached to the post, false otherwise.
* @since 1.2.0
* @access private
*/
private function generate_featured_image( $file, $post_id, $desc, &$import_errors, &$import_info, $post_data = array() ) {
private function try_save_featured_image( $img_source_url, $post_id, $post_title, &$import_errors, &$import_info, $post_data = array() ) {
if ( ! function_exists( 'post_exists' ) ) {
require_once ABSPATH . 'wp-admin/includes/post.php';
}
// Find existing attachment by item title.
$id = post_exists( $desc, '', '', 'attachment' );
$id = post_exists( $post_title, '', '', '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__ );

if ( filter_var( $img_source_url, FILTER_VALIDATE_URL ) === false ) {
$import_errors[] = 'Invalid Featured Image URL: ' . $img_source_url;
return false;
}

do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Trying to save the featured image for %s and postID %d', $img_source_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';

$file_array = array();
$file = trim( $file, chr( 0xC2 ) . chr( 0xA0 ) );
$local_file = download_url( $file );
$file_array = array();
$img_source_url = trim( $img_source_url, chr( 0xC2 ) . chr( 0xA0 ) );
$local_file = download_url( $img_source_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__ );

Expand All @@ -2038,15 +2047,15 @@ private function generate_featured_image( $file, $post_id, $desc, &$import_error
$file_array['tmp_name'] = $local_file;
$file_array['name'] = basename( $local_file );

$id = media_handle_sideload( $file_array, $post_id, $desc, $post_data );
$id = media_handle_sideload( $file_array, $post_id, $post_title, $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'] );

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__ );
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Found an existing attachment(ID: %d) image for %s and postID %d', $id, $img_source_url, $post_id ), 'debug', __FILE__, __LINE__ );
}

if ( ! empty( $post_data ) ) {
Expand Down Expand Up @@ -2929,7 +2938,7 @@ private function wizard_import_feed() {
*
* @param string $actions Item content actions.
* @param string $type Action type.
* @return Feedzy_Rss_Feeds_Actions The class instance.
* @return Feedzy_Rss_Feeds_Actions Instance of Feedzy_Rss_Feeds_Actions.
*/
public function get_actions_runner( $actions = '', $type = '' ) {
$action_instance = Feedzy_Rss_Feeds_Actions::instance();
Expand Down
5 changes: 5 additions & 0 deletions includes/views/js/import-metabox-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@
}
} );

// Tagify the title field.
$( '.fz-input-tagify[name="feedzy_meta_data[import_post_title]"]:not(.fz-tagify-image)' ).tagify( {
mode: 'mix'
} );

// Tagify for normal mix content field.
$( '.fz-tagify-image' ).tagify( {
mode: 'mix',
Expand Down
6 changes: 3 additions & 3 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 All @@ -299,4 +299,4 @@ const SortableItem = ({ propRef, loopIndex, item }) => {
}
}

export default SortableElement(SortableItem);
export default SortableElement(SortableItem);
2 changes: 1 addition & 1 deletion js/ActionPopup/action-popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const ActionModal = () => {
actionData.data[actionId] = true;
}
if ( ['fz_image'].indexOf( actionId ) > -1 ) {
actionData.data['generateImgWithChatGPT'] = true;
actionData.data['generateOnlyMissingImages'] = true;
}

let newAction = [actionData];
Expand Down
2 changes: 1 addition & 1 deletion js/ActionPopup/action-popup.min.js

Large diffs are not rendered by default.

0 comments on commit 294c291

Please sign in to comment.