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

fix: feed image when using chained actions #946

Merged
merged 8 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
40 changes: 20 additions & 20 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -1663,20 +1663,20 @@ private function run_job( $job, $max ) {
$image_source_url = '';
$img_success = true;
$new_post_id = 0;
$default_img_tag = ! empty( $import_featured_img ) && is_string( $import_featured_img ) ? $import_featured_img : '';
$feed_img_tag = ! empty( $import_featured_img ) && is_string( $import_featured_img ) ? $import_featured_img : '';

// image tag
if ( strpos( $default_img_tag, '[#item_image]' ) !== false ) {
if ( strpos( $feed_img_tag, '[#item_image]' ) !== false ) {
// image exists in item
if ( ! empty( $item['item_img_path'] ) ) {
$image_source_url = str_replace( '[#item_image]', $item['item_img_path'], $default_img_tag );
$image_source_url = str_replace( '[#item_image]', $item['item_img_path'], $feed_img_tag );
} else {
$img_success = false;
}
} elseif ( strpos( $default_img_tag, '[#item_custom' ) !== false ) {
} elseif ( strpos( $feed_img_tag, '[#item_custom' ) !== false ) {
// custom image tag
if ( $this->feedzy_is_business() || $this->feedzy_is_personal() ) {
$value = apply_filters( 'feedzy_parse_custom_tags', $default_img_tag, $item_obj );
$value = apply_filters( 'feedzy_parse_custom_tags', $feed_img_tag, $item_obj );
}

if ( ! empty( $value ) && strpos( $value, '[#item_custom' ) === false ) {
Expand All @@ -1685,7 +1685,7 @@ private function run_job( $job, $max ) {
$img_success = false;
}
} else {
$image_source_url = $default_img_tag;
$image_source_url = $feed_img_tag;
}

if ( ! empty( $image_source_url ) ) {
Expand Down Expand Up @@ -1770,24 +1770,24 @@ function( $term ) {

do_action( 'feedzy_import_extra', $job, $item_obj, $new_post_id, $import_errors, $import_info );

$default_img_tag = ! empty( $import_featured_img ) && is_string( $import_featured_img ) ? $import_featured_img : '';
if ( ! empty( $default_img_tag ) && 'attachment' !== $import_post_type ) {
if ( ! empty( $import_featured_img ) && 'attachment' !== $import_post_type ) {
$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_source_url = str_replace( '[#item_image]', $item['item_img_path'], $default_img_tag );
$feed_img_tag = false === strpos( $import_featured_img, '[[{"value":' ) ? $import_featured_img : '[#item_image]'; // Use feed default image when we are using chained actions.

// Set the feed image as default value for the image source.
if ( strpos( $feed_img_tag, '[#item_image]' ) !== false ) {
if ( ! empty( $item['item_img_path'] ) ) { // image exists in item
$image_source_url = str_replace( '[#item_image]', $item['item_img_path'], $feed_img_tag );
} else {
$img_success = false;
}
} elseif ( strpos( $default_img_tag, '[#item_custom' ) !== false ) {
// custom image tag
if ( $this->feedzy_is_business() || $this->feedzy_is_personal() ) {
$value = apply_filters( 'feedzy_parse_custom_tags', $default_img_tag, $item_obj );
}
} elseif (
( $this->feedzy_is_business() || $this->feedzy_is_personal() ) && // PRO feature.
false !== strpos( $feed_img_tag, '[#item_custom' )
) {
$value = apply_filters( 'feedzy_parse_custom_tags', $feed_img_tag, $item_obj ); // custom image tag
if ( ! empty( $value ) && strpos( $value, '[#item_custom' ) === false ) {
$image_source_url = $value;
} else {
Expand Down Expand Up @@ -1840,12 +1840,12 @@ function( $term ) {
}

if ( 'yes' === $import_item_img_url || ! $this->tryReuseExistingFeaturedImage( $img_success, $item['item_title'], $new_post_id ) ) {
// Item image action.
// Run chained actions.
$import_featured_img = rawurldecode( $import_featured_img );
$import_featured_img = trim( $import_featured_img );
$img_action = $this->get_actions_runner( $import_featured_img, 'item_image' );
// Item image action process.
$image_source_url = $img_action->run_action_job( $import_featured_img, $import_translation_lang, $job, $language_code, $item, $image_source_url );
$image_source_url = $img_action->run_action_job( $img_action->get_serialized_actions(), $import_translation_lang, $job, $language_code, $item, $image_source_url );

if ( ! empty( $image_source_url ) ) {
if ( 'yes' === $import_item_img_url ) {
Expand Down
56 changes: 48 additions & 8 deletions tests/e2e/specs/import.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
addFeeds,
runFeedImport,
addFeaturedImage,
addContentMapping
addContentMapping,
getEmptyChainedActions,
serializeChainedActions,
wrapSerializedChainedActions, setItemLimit
} from '../utils';

test.describe( 'Feed Import', () => {
Expand Down Expand Up @@ -41,7 +44,7 @@
});

test( 'import lazy loading feeds with shortcode', async({ editor, page, admin }) => {
const lazyShortcode = "[feedzy-rss feeds='https://s3.amazonaws.com/verti-utils/sample-feed.xml' max='11' offset='1' feed_title='yes' refresh='1_hours' meta='yes' multiple_meta='yes' summary='yes' price='yes' mapping='price=im:price' thumb='yes' keywords_title='God, Mendes, Cyrus, Taylor' keywords_ban='Cyrus' template='style1' lazy='yes']";
const lazyShortcode = "[feedzy-rss feeds='https://s3.amazonaws.com/verti-utils/sample-feed.xml' max='2' offset='1' feed_title='yes' refresh='1_hours' meta='yes' multiple_meta='yes' summary='yes' price='yes' mapping='price=im:price' thumb='yes' keywords_title='God, Mendes, Cyrus, Taylor' keywords_ban='Cyrus' template='style1' lazy='yes']";

await admin.createNewPost();

Expand All @@ -63,7 +66,7 @@
} );

test( 'import multiple feeds with shortcode', async({ editor, page, admin }) => {
const multipleFeedsShortCode = "[feedzy-rss feeds='https://s3.amazonaws.com/verti-utils/sample-feed-multiple1.xml, https://s3.amazonaws.com/verti-utils/sample-feed-multiple2.xml' max='10' feed_title='no' refresh='1_hours' meta='yes' multiple_meta='yes' summary='yes' thumb='yes' template='style1']";
const multipleFeedsShortCode = "[feedzy-rss feeds='https://s3.amazonaws.com/verti-utils/sample-feed-multiple1.xml, https://s3.amazonaws.com/verti-utils/sample-feed-multiple2.xml' max='1' feed_title='no' refresh='1_hours' meta='yes' multiple_meta='yes' summary='yes' thumb='yes' template='style1']";

await admin.createNewPost();

Expand Down Expand Up @@ -114,6 +117,7 @@

await page.getByPlaceholder('Add a name for your import').fill(importName);
await addFeeds( page, [FEED_URL] );
await setItemLimit(page, 1);
await page.getByRole('button', { name: 'Save & Activate importing' }).click({ force: true });

await runFeedImport( page );
Expand All @@ -130,6 +134,7 @@
await page.getByPlaceholder('Add a name for your import').fill(importName);
await addFeeds( page, [FEED_URL] );
await addFeaturedImage( page, '[#item_image]' );
await setItemLimit(page, 1);
await page.getByRole('button', { name: 'Save & Activate importing' }).click({ force: true });

await runFeedImport( page );
Expand All @@ -151,8 +156,17 @@

await page.getByPlaceholder('Add a name for your import').fill(importName);
await addFeeds( page, [FEED_URL] );
await addContentMapping( page, '[[{"value":"%5B%7B%22id%22%3A%22trim%22%2C%22tag%22%3A%22item_content%22%2C%22data%22%3A%7B%22trimLength%22%3A%2230%22%7D%7D%5D"}]] '); // Trim the content with 30 words max.
await addFeaturedImage( page, '[[{"value":"%5B%7B%22id%22%3A%22%22%2C%22tag%22%3A%22item_image%22%2C%22data%22%3A%7B%7D%7D%5D"}]] ' );
await addContentMapping( page, wrapSerializedChainedActions( serializeChainedActions( [
{
"id": "trim",
"tag": "item_content",
"data": {
"trimLength": "30"
}
}
] ) ) );
await addFeaturedImage( page, getEmptyChainedActions( 'item_image' ) );
await setItemLimit(page, 1);

await page.getByRole('button', { name: 'Save & Activate importing' }).click({ force: true });

Expand All @@ -169,8 +183,34 @@
expect( content ).toContain('<p>');
expect( content.split(' ').length ).toBeLessThanOrEqual(30);

// Enable this test for https://github.com/Codeinwp/feedzy-rss-feeds/pull/946 when the PR is merged.
// await page.getByRole('button', { name: 'Featured image' }).click({ force: true });
// await expect( page.getByLabel('Edit or replace the image') ).toBeVisible(); // Featured image is added.
if( ! await page.getByLabel('Edit or replace the image').isVisible() ) { // Should we open the featured image tab?
await page.getByRole('button', { name: 'Featured image' }).click({ force: true });
}
await page.waitForSelector('.editor-post-featured-image');

Check failure on line 189 in tests/e2e/specs/import.spec.js

View workflow job for this annotation

GitHub Actions / E2E

[chromium] › import.spec.js:149:6 › Feed Import › importing feed with chained actions

1) [chromium] › import.spec.js:149:6 › Feed Import › importing feed with chained actions ───────── TimeoutError: page.waitForSelector: Timeout 10000ms exceeded. Call log: - waiting for locator('.editor-post-featured-image') to be visible 187 | await page.getByRole('button', { name: 'Featured image' }).click({ force: true }); 188 | } > 189 | await page.waitForSelector('.editor-post-featured-image'); | ^ 190 | await expect( page.getByLabel('Edit or replace the image') ).toBeVisible(); // Featured image is added. 191 | }); 192 | at /home/runner/work/feedzy-rss-feeds/feedzy-rss-feeds/tests/e2e/specs/import.spec.js:189:14
await expect( page.getByLabel('Edit or replace the image') ).toBeVisible(); // Featured image is added.
});

test( 'importing feed with chained actions for featured image only', async({ admin, page }) => {
await admin.createNewPost();

const importName = 'Test Title: importing feed with chained actions for featured image only';

await page.goto('/wp-admin/post-new.php?post_type=feedzy_imports');
await tryCloseTourModal( page );

await page.getByPlaceholder('Add a name for your import').fill(importName);
await addFeeds( page, [FEED_URL] );
await addFeaturedImage( page, getEmptyChainedActions( 'item_image' ) );

await page.getByRole('button', { name: 'Save & Activate importing' }).click({ force: true });

await page.goto('/wp-admin/edit.php?post_type=feedzy_imports');

await runFeedImport( page );

// Check the gallery.
await page.goto('/wp-admin/upload.php');
await page.waitForSelector('#wp-media-grid');
await expect( page.locator('.attachment').count() ).resolves.toBeGreaterThan(0); // We should have some imported images.
});
});
39 changes: 39 additions & 0 deletions tests/e2e/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,45 @@ export async function addContentMapping( page, mapping ) {
}, mapping );
}

/**
* Set the item limit on the Feed Edit page.
* @param {import('playwright').Page} page The page object.
* @param {number} limit The limit to set.
* @returns {Promise<void>}
*/
export async function setItemLimit( page, limit ) {
await page.evaluate( ( limit ) => {
document.querySelector( 'input[name="feedzy_meta_data[import_feed_limit]"]' ).value = limit;
} , limit );
}

/**
* Create an empty chained actions.
* @param {string} defaultFeedTag The default feed tag.
* @returns {string} The empty chained actions.
*/
export function getEmptyChainedActions( defaultFeedTag ) {
return wrapSerializedChainedActions(serializeChainedActions([ { id: '', tag: defaultFeedTag, data: {} } ] ) );
}

/**
* Serialize the chained actions.
* @param {any[]} actions The actions to serialize.
* @returns {string} The serialized actions.
*/
export function serializeChainedActions( actions ) {
return encodeURIComponent( JSON.stringify( actions ) );
}

/**
* Wrap the serialized chained actions to match the format used in the input.
* @param {string} actions The serialized actions.
* @returns {string} The wrapped actions.
*/
export function wrapSerializedChainedActions( actions ) {
return `[[{"value":"${actions}"}]]`;
}

/**
* Run the feed import.
*
Expand Down
Loading