Automated Re-fetching on removed item - redux

I've got a problem with preventing query from refetching after deleting resource (and invalidating tag).
I have similar situation like the one described here, but I've got additional getPost query for fetching post details. I'm on the post details page, and there is a remove button that will redirect visitor to the list of posts after 200 from backend.
Using code from RTK-Q example I'm getting following result:
[DELETE] 200 /deletePost
[GET] 404 /getPost
After receiving 200 from delete action I'm invalidating { type: 'Posts' as const, id } tag which causes refetch, because I still have an active connection to the query on the post details page and get 404 from backend because post do not exists anymore.
Is there any chance to prevent getPost query from refetching after invalidating tag?

Either don't provide the tag or don't invalidate the tag. You can also choose to introduce more tags like "post_del" and "post_edit" and provide both, but only invalidate one to describe exactly when you want to invalidate something.

Related

User input handler that returns data to page

I'm new to Wordpress developing and I have found what feels like a clumsy solution to a problem that seems like it should be common. Please help me find a cleaner solution with less potential for buggy behavior.
Objective: Get date range from user on a Wordpress page, use date range in API request, display data from API response on Wordpress page.
Current solution: Start and end dates are submitted via an HTML form in a Wordpress page with POST method. The POST request does not route to any other page. In addition to start and end parameters, the form has a hidden action parameter with a custom value I have specified: <input type="hidden" name="action" value="custom-value">. I have written a plugin that uses the init hook. The hooked function checks $_REQUEST['action'] to see if it matches the custom value in the form, and if so it does a GET request to an API (recreation.gov) using the start and end dates from the form. Up to this point, the solution seems pretty solid to me. Here's where the tutorials ran out and I had to get......inventive.
The data from the API request is processed and formatted into HTML, and this string is stored as a transient using set_transient with a 3 second lifespan. In the original page that the form was submitted from, there is a shortcode that simply returns the transient. Now I have the data from the API on the same page that the user specified the date range from.
This solution works, but seems like there is a high probability of things going awry if multiple users are using the page at the same time, and seeing or overwriting each others' results. There must be a better way to get the data from the plugin that does the API request back to the Wordpress page.
Since this question is about methodology, not syntax, I haven't included any example code, but I could if that would be helpful. Thanks in advance for your advice.
The solution I ended up finding was ditching the plugin and doing everything from inside a shortcode. Shortcode has access to $_REQUEST['action'], which I wasn't aware of, so I was able to get the user input and do the API call in the shortcode, then format the results and return them. No need to deal with plugins and hooks because the shortcode is executed as the page is loaded.

assign post ID to new wp.media object

I have a custom form for publishing posts. When the new post is first created, there is no existing post ID, so attaching photos to the post doesn't work until after the post is saved and an ID is generated for the post.
The first thing I've done is initiate an auto-save so that a post ID is generated to represent the new post. However, I'm can't figure out how to update the wp.media object with this new ID so that uploaded posts are assigned to the ID generated during the auto-save.
Is there a way to reinitialize the wp.media object with the new post ID?
edit
I found this resource, but it doesn't seem to cover how to assign an post ID, and I can't seem to locate it in the source code referenced by the git either...
https://github.com/thomasgriffin/New-Media-Image-Uploader/blob/master/js/media.js
update
Looks like I was able to reassign the post ID to the wp.media object by simply doing this:
wp.media.model.settings.post.id = new_id;
However, I'm experiencing some permission issues now. As an admin, everything works fine, but as any other user, I get an error "An error occurred in the upload. Please try again later."
Uploading works fine with all users once the post is saved, but for some reason it won't allow me to upload with non-admin users after assigning a new id to the wp.media object.
R
Finally sorted it out.
AJAX is used to initiate the auto-save in the background while the user edits the post. When the auto-save ID is returned, it's re-assigned to the wp.media object like this:
wp.media.model.settings.post.id = autos_save_id;
The permission issue I was experiencing came from the simple fact that my user role didn't have the 'edit_post' capability. Duh.
If I remember correct the media has an ID just like post/pages. It is up to the editor/system to associate media to post.
When I upload an image (media) to my local dev I edit it via :
site.com/wp-admin/post.php?post=19&action=edit
Where 19 is the media ID.
if I go to site.com/?p=19 I am forwarded to site.com/?attachment_id=19 and get to see the media.
Regarding the post/page (custom form) - when implemented correctly, on first save (submit) the post gets an ID. That ID remains the same. (beware of autosave post IDs and similar).
Tell me more about what you know regarding the "wp.media object" or let me know if I missed something?

Drupal 6: Want to remove access restrictions on a View

I have a View with a Page display that only allows the returned data to be linked if the user is authenticated/logged in.
I simply want to remove that restriction. There is another view that looks the same except it's for Public and shows all the same data but not linked. (the data are pdf files).
On the authenticated display, next to Access in the Basic Settings it has: authenticated user. If I click that user role (not the gear for options), then I'm allowed a list of:
Domain
None
Permission
Role
…for access restrictions. I click "None", save. But, the links are not there as an anon visitor. Same if I try by changing the user to Anonymous User. Note: In the permissions, anon has the right to see webfm attachments.
I noticed the url is like: documents-public or documents-auth so I made it just documents. No luck.
I visited the actual custom-page-type which puts the View in a content pane. I re-ordered the variants, removed all Selection Rules and that didn't do anything either.
Flushed all caches.
Somewhere, the view is still only showing a view based on whether a user is authenticated or not even though I've removed the access (it says "unrestricted" next to access) on both displays and removed all selection rules on the actual page.
I'm stuck where else to look.
The setting you're changing (Access restrictions) is for the entire page - the page URL will return a 403 error if the criteria are met.
As to where the logic is to display/not display the link, four things come to mind:
If the "view mode" is set to node or teaser, it could be in the node template file.
The logic might be in a specific view template (click on the theme information link and see if any of the templates have been overridden).
The developer might have user Views Custom Fields to put the logic directly in the fields.
The linked content is actually restricted.
I'm sure there are other possibilities too. The main point here though is that if access was restricted for anon users, they would see a 403 error, not just some missing fields.

How to redirect to a page after a fivestar vote in Drupal with Rules?

Drupal 6.22
Fivestar 6.x-1.19
Rules 6.x-1.4
These modules were the latest as of this posting.
My goal is to redirect a user to a page when they vote on a node. Should be simple enough.
I've created a rule for when a user votes on a node. No condition. I've added an action to add a message to the log to verify the rule is being triggered. Which it is. Then adding an action to redirect to the homepage (for testing). The redirect never happens. If I check the option "Immediately issue the page redirect", the ajax vote never saves and just hangs at "saving vote...".
I don't think I'm doing anything wrong and will post this to the fivestar module issues. Has anyone else had this issue?
http://drupal.org/node/252859
A "redirect to url ..." will definitely cause issues with Fivestar's AJAX voting mechanism. Here's what's happening:
What normally happens:
- User clicks on a Fivestar widget to rate.
- AJAX request is made by the current page.
- VotingAPI saves the vote.
- Fivestar generates XML and prints the page.
- The current page receives XML, then updates the voting widget with the new values.
What happens with VotingActions:
- User clicks on a Fivestar widget to rate
- AJAX request is made
- VotingAPI records the vote
- VotingActions hooks in and says "oh I'm redirecting this URL" <-- Where things go wrong
- Instead of returning XML to the calling page (which would update the votes), the original page gets back a full HTML page of wherever VotingActions redirected the page.
- Fivestar doesn't get to generate it's XML, because VotingActions has ended the execution by using a drupal_goto().
So I don't think it's a bug in either VotingActions or Fivestar, it's just a case of using two features in incompatible ways. To make this work, Fivestar would have to not be AJAX driven, instead reloading the page to save a vote.
Fivestar provides a nice JavaScript hook that you can use to go to the next page. If you add JavaScript to the page that contains something like this:
function fivestarResult(voteResult) {
window.location.href = 'http://google.com';
}

wordpress form action submit

have an own customized form in wordpress.
action=<?php bloginfo('template_url'); ?>/send_form.php"
on submit, it goes always to send_form.php
but this php is for sending the info - and i cant style with with the theme....
is there a way to stay on the current page while it sends the info and print out and message in a field that the form has been sended succesfully???
anyone have some suggestion for me?
The answer to this is quite involved and getting it right takes a little care, but here are the essential points of one approach:
The action must be set to load the current page and you can do this by changing the action attribute in the <form> tag to: action=""
In the template file for this page, detect when the page loads after a form submission. You can do this by checking the state of the $_POST variable.
If there are post variables, process the form submission in the template file. You need to look at what happens in "send_form.php" to figure out what to do. Take care that you don't introduce security issues (e.g. be sure to use the NONCE).
Redirect back to the same page to get rid of the post variables. If you don't do this people will get those "Do you want to resubmit the form" warning messages. See the PHP header() function for doing the redirect and note that this must happen before any output is sent to the page.
Use server sessions to display a "Form submission successful". Set a session variable to your message before the redirect, then detect it when the page loads, display it, and remove it so the message doesn't display the next time the page loads.
That should give you a starting point.
If anyone else has a simpler solution, I'd love to hear it too.
If you want to do it through plugin instead of the theme, then use "admin_post"(admin_post_YOUR_ACTION) and "admin_post_nopriv" (admin_post_nopriv_YOUR_ACTION) actions. (Btw, you can use those actions through theme also)
Here is a good explanation: https://www.sitepoint.com/handling-post-requests-the-wordpress-way/

Resources