WordPress.org

Make WordPress Core

Opened 6 years ago

Last modified 2 years ago

#22589 reviewing enhancement

Network Admin + Ajax requests

Reported by: dd32 Owned by: jeremyfelt
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Networks and Sites Keywords: has-patch
Focuses: multisite, administration Cc:

Description

Currently there is no Network admin based ajax handler, forcing plugins which exist solely within the Network Admin to either use wp-admin/admin-ajax.php or using the load-{$page_hook} hook to perform any actions.

This is primarily a problem when the plugin only includes code on a conditional such as is_network_admin(), which would be false for a /wp-admin/admin-ajax.php request, as a result, only the load-{$page_hook} option above is viable for those.
As a work around, plugins can include code on is_network_admin() || DOING_AJAX to allow the usage of admin-ajax.php.

Of course, the User Admin also suffers the same issue.

I didn't see any previous tickets, and kind of want to say the existing 'workaround' is appropriate, but recording it here so we can at least close it as wontfix if appropriate.

Attachments (3)

22589.diff (1.9 KB) - added by thomaswm 2 years ago.
Adds a wp-admin/network/admin-ajax.php file
22589.2.diff (1.9 KB) - added by flixos90 2 years ago.
22589.3.diff (2.7 KB) - added by jeremyfelt 2 years ago.

Download all attachments as: .zip

Change History (24)

#1 @toscho
6 years ago

  • Cc info@… added

#2 @scribu
6 years ago

  • Type changed from feature request to enhancement

I don't think the workaround is appropriate; it's a hack.

We should have a dedicated /wp-admin/network/admin-ajax.php file, which contains all the network-specific ajax handlers.

#3 @TroyDesign
5 years ago

+1 I have same problem. I wrote a small workaroud:

if (defined('DOING_AJAX') && DOING_AJAX && is_multisite() && preg_match('#^'.network_admin_url().'#i',$_SERVER['HTTP_REFERER'])) {
	define('WP_NETWORK_ADMIN',true);
}

#4 @ocean90
5 years ago

  • Cc ocean90 added

#5 @jeremyfelt
4 years ago

  • Component changed from Administration to Network Admin
  • Focuses multisite added

#6 @nacin
4 years ago

  • Component changed from Network Admin to Networks and Sites
  • Focuses administration added

#7 @chriscct7
3 years ago

  • Keywords needs-patch added

This ticket was mentioned in Slack in #core-multisite by jeremyfelt. View the logs.


3 years ago

#9 @danieliser
2 years ago

Any news on this? Currently my hack includes passing a salted token with request and verifying it in the normal admin ajax hook to ensure its network admin. Definitely seems there should be a better / native alternative.

#10 @jeremyfelt
2 years ago

  • Milestone changed from Awaiting Review to Future Release

If anyone wants to patch this, I'd be interested in taking a look at that patch for 4.6. :)

Some examples of plugins that are hacking together a wp-admin/admin-ajax.php solution would be good so we know what the target audience is.

Last edited 2 years ago by jeremyfelt (previous) (diff)

@thomaswm
2 years ago

Adds a wp-admin/network/admin-ajax.php file

#11 @thomaswm
2 years ago

  • Keywords has-patch needs-testing added; needs-patch removed

22589.diff adds a wp-admin/network/admin-ajax.php file and wp_network_ajax(_nopriv)_ actions.

#12 @danieliser
2 years ago

@thomaswm - Awesome, wonder if we can get this tested and push for it to be included in 4.6.

#13 @jeremyfelt
2 years ago

  • Milestone changed from Future Release to 4.6

@flixos90
2 years ago

#14 @flixos90
2 years ago

22589.2.diff has some minor adjustments:

  • if not a multisite, bail, similar like in other network areas
  • do not include the ajax-actions.php file since it is not required at this point; if Core starts using the network AJAX functionality itself, we can either re-add the file or (what I'd prefer) create a separate network-ajax-actions.php file for this
  • some docs adjustments (version numbers, "network AJAX")

#15 @chriscct7
2 years ago

  • Owner set to flixos90
  • Status changed from new to assigned

This ticket was mentioned in Slack in #core-multisite by jeremyfelt. View the logs.


2 years ago

#17 @jeremyfelt
2 years ago

  • Owner changed from flixos90 to jeremyfelt
  • Status changed from assigned to reviewing

@jeremyfelt
2 years ago

#18 follow-up: @jeremyfelt
2 years ago

  • Keywords needs-testing removed

22589.3.diff:

  • Fixes require() for wp-load.php
  • Updates a few areas to match code standards.
  • Adds the JS global var networkajaxurl on admin-header.php if is_network_admin().

Does networkajaxurl make sense?

#19 in reply to: ↑ 18 @flixos90
2 years ago

Replying to jeremyfelt:

Does networkajaxurl make sense?

It's a really long lower-case term, so not sure if it could have a better name. On the other hand it matches ajaxurl.

What we could also do to improve the infrastructure is to add a JS function for making network AJAX requests (or adjust the existing wp.ajax.post(). Probably a separate ticket though.

#20 @danieliser
2 years ago

Just a thought as I have been following this for a bit, could a plugin dev not just fire a request to ajaxurl with a network=true argument?

With that argument redirect the request any way you need, but leaves one endpoint to maintain, and JS can easily switch between network and single mode.

Just a thought that may simplify things.

#21 @jeremyfelt
2 years ago

  • Milestone changed from 4.6 to Future Release

I know we're close, but I'm going to punt this to a future release. There is room for more conversation here about the approach.

Note: See TracTickets for help on using tickets.