acekyd / display-medium-posts

Display Medium Posts is a wordpress plugin that allows users display posts from medium.com on any part of their website.
GNU General Public License v2.0
85 stars 19 forks source link

Comments are being displayed in the carousel #29

Open jscultureshift opened 4 years ago

jscultureshift commented 4 years ago

I am using the recommended code to add the Medium carousel to the homepage and if there's a comment added to one of the posts it shows up in the carousel.

mike820324 commented 4 years ago

Hello everyone, I'm also encounter this issue in my wordpress site.

After a further investigate about the solution, I have found a workaround propsose by this link The workaround provide a simple heuristic way to distinguish between comment and post.

As the website suggest

BUT! it’s always an empty array of categories when the item is a comment. So it’s nothing harder than just filter the whole response array from items, where categories == array.length > 0. Let’s do this! We can check the categories array length and if it's greater than zero, we show the post.

A simple digging about the source code, I found out that we can simply add a comparison below the for each scope https://github.com/acekyd/display-medium-posts/blob/929079cf7d2aa4b8c2586881ad7e7ffd3a79f055/display-medium-posts.php#L133

The code snippet may look something like the following

if (count($post->categories) == 0) {
  continue;
}

The only side effect I can think of is that if a post does not have any category, it will be consider as comment and as a result will not be shown.

@acekyd Any thoughts about the above fix. I would love to submit a MR if the above workaround is okay for you.

acekyd commented 4 years ago

Hello @mike820324, I came across this same fix online but the edge cases were why I hesitated to implement it. Doesn't seem like there's a better alternative.

To send the PR, would be great to include an attribute so the user can set if they want to turn comments off instead e,g [display_medium_posts handle="@acekyd" comments=false]

mike820324 commented 4 years ago

@acekyd thanks for the reply. After digging the medium API for a while, I have found a solution to get the user post with the medium GraphQL API. By using the GraphAPI, the above side effect that mentioned earlier will no longer exist.

The underline investigation and code snippet can be found in my medium post https://medium.com/@mike820324/web-crawler-getting-medium-post-a6e52fd36fd6

I'll try to implement the GraphQL into the PHP version.

[display_medium_posts handle="@acekyd" comments=false]

Will do.