Skip to content

Conversation

@Rajinsharwar
Copy link
Contributor

@Rajinsharwar Rajinsharwar commented Nov 4, 2023

What?

This PR addresses inconsistencies in excerpt length when making AJAX requests versus regular HTTP requests and REST API requests.

Fixes #53570.

Why?

In 55400, @anton-vlasenko created a patch, which resolved the issue while making the AJAX call, but then it caused the same problem with the REST API call. So, this PR, it aims to make the excerpt length of both the AJAX call, and REST API call consistent, and that to the defined using the "excerpt_length" hook.

How?

Now it uses the rest_api_init action hook to add the filter only in the REST API context. It checks if REST_REQUEST is defined and set to true, indicating that it's a REST request. If so, it adds the filter for editor_excerpt_length, limiting the excerpt length to 100.

Testing Instructions

  1. Activate a block theme, such as Twenty Twenty-Four.
  2. Ensure that Gutenberg is enabled.
  3. Create a new POST in the Site Editor. You can use any title. Use the following text as the post's content (just make sure to insert it into a paragraph block):
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In euismod diam egestas massa facilisis, in imperdiet mi consequat. Etiam interdum vel tellus laoreet posuere. Aliquam ut sagittis elit. Nullam et orci nisl. Morbi quis vehicula ex, eget luctus odio. Vivamus a tincidunt arcu, id euismod ex. Praesent fringilla velit lorem, sit amet commodo mauris vulputate sodales. Vestibulum tempus, ipsum et ultricies interdum, ligula elit accumsan risus, vel tristique dolor est eu diam. Vivamus pharetra eget erat vel rutrum. Aliquam et ligula quis lorem molestie semper. Aenean convallis tortor nec velit ultricies semper. Maecenas felis erat, cursus nec varius et, auctor eu nulla. Phasellus et nibh imperdiet, placerat erat ut, tristique libero. Nam sed scelerisque enim.
  1. Publish the Post
  2. I have bundled the PHP code and JS code required for testing, as a plugin. Simply activate this plugin at first: https://github.com/Rajinsharwar/test-ajax
  3. This will enqueue the custom.js file on your frontend. If you open the frontend and open the browser console, you will see the output. (You can open any page in the frontend).

image

  1. You will see the output from both the REST API and AJAX together. You will find the length difference.

  2. If you open up the plugin folder, in the test-ajax.php, on line 7, modify the excerpt length if you wish. Currently, it's set to 20, and the BUG occurs. 🐞

  3. Now, apply the patch, in both the src/wp-includes/blocks/post-excerpt.php(WordPress Core) and build/wp-includes/blocks/post-excerpt.php(Gutenberg). Generate a new build of the Gutenburg, so that the version gets updated.

  4. Now, check the output in the console of the page again. You will see the below output. The post-excerpt length in both cases is now 20. The issue is resolved. ✅

image

Screenshots or screencast

Before:

image

After:

image

@mikachan mikachan added [Package] Block library /packages/block-library [Type] Bug An existing feature does not function as intended [Block] Post Excerpt Affects the Post Excerpt Block labels Nov 5, 2023
@ZangoFluid
Copy link

I tested the patch provided here, and it solves the issue mentioned.

Environment

  • WordPress: 6.4-RC3
  • PHP: 8.1.23
  • Server: nginx/1.16.0
  • Database: mysqli (Server: 8.0.16 / Client: 8.1.23)
  • Browser: Chrome 119.0.0.0 (Windows 10/11)
  • Theme: Twenty Twenty-Four 1.0
  • Plugins:
    • Gutenberg 16.9.0
    • WordPress Beta Tester 3.5.5

Before

  1. ❌ The AJAX and the REST API responses, both returned excerpts of different lengths.
    https://prnt.sc/uAnL7xBBm2Ym

After

  1. ✅ The AJAX and REST API is now returning the excerpts of the same length, and also the one defined using the excerpt_length hook.
    https://prnt.sc/yS4rtePjFH3T

Copy link
Contributor

@alexstine alexstine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good but no time at present to test this. Hopefully others can test and review soon.

@swissspidy
Copy link
Member

Same concern as with https://github.com/WordPress/gutenberg/pull/55400/files#r1369927043\

The whole approach is still not great because the excerpt will be limited to 100 for any sort of REST request, even when not in the editor. Ideally this should only be added in edit context in the REST API. Plus. plugins cannot easily unhook it because it's an anonymous function added at PHP_INT_MAX.

Unit tests would be great.

@Rajinsharwar
Copy link
Contributor Author

Yeah, how about adding a check like doing_action('edit_post') before adding the 100-length @swissspidy? Will that work in this case?
cc @azaozz if you have any insights on this about what should be done to check if the request is from the editor.

@prasad5898
Copy link

Test Report
Test Report for patch - #55863

Environment
Operating System: Windows
WordPress: v6.4
Theme: Twenty Twenty four
Browser: Chrome

Actual Result:
The Ajax and Rest API returning the different length for the excerpt.

Expected Result:
The Ajax and Rest API excerpt length should be same ✅

Screenshot:
Before : https://prnt.sc/TS88zk4oH6Td
After: https://prnt.sc/Eh_ZGYg3Gf7y

Thanks

@anton-vlasenko
Copy link
Contributor

This PR should probably be closed in favor of WordPress/wordpress-develop#5932, if or when it gets merged.

@github-actions
Copy link

github-actions bot commented Feb 7, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @ZangoFluid, @apeatling.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

Core SVN

Core Committers: Use this line as a base for the props when committing in SVN:

Props rajinsharwar, alexstine, swissspidy, prasad17, antonvlasenko, hellofromtonya.

GitHub Merge commits

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: ZangoFluid, apeatling.

Co-authored-by: Rajinsharwar <[email protected]>
Co-authored-by: alexstine <[email protected]>
Co-authored-by: swissspidy <[email protected]>
Co-authored-by: prasad5898 <[email protected]>
Co-authored-by: anton-vlasenko <[email protected]>
Co-authored-by: hellofromtonya <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@swissspidy
Copy link
Member

After https://core.trac.wordpress.org/changeset/58065 this needs a new approach. Closing this for now so a new dedicated PR can be opened.

@swissspidy swissspidy closed this Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Post Excerpt Affects the Post Excerpt Block [Package] Block library /packages/block-library [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

the_excerpt() function return excerpt with different length in page load and ajax request

7 participants