samtiria / nextgen-gallery

Automatically exported from code.google.com/p/nextgen-gallery
0 stars 0 forks source link

admin/ajax.php should not use die() to finish #451

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Write a plugin that defines an action 'wp_ajax_ngg_ajax_operation' with a 
priority > 10
2. Perform an AJAX bulk action (for example Resize images) from a 'manage 
gallery' page
3. See your code NOT executed

What version of the product are you using? On what operating system?

NGG 1.9.1 on WP 3.3

Please provide any additional information below.

I am writing a plugin that tries to hook into 'ngg_ajax_operation', defined in 
admin/ajax.php. My plugin needs to do some stuff both before AND after NextGEN 
gallery has performed the requested action, so I have two

 add_action ('wp_ajax_ngg_ajax_operation')

operations, one with prio 8, and one with prio 12. The one with prio 8 runs 
fine, the one with prio 12 doesn't.

The reason is, that function ngg_ajax_operation() finishes with a die() 
statement, so code that is supposed to run later never runs.

I highly doubt that Wordpress is intended to be (ab)used this way. I can't see 
any reason to use die() here, and the ugly side effect is, that it cripples the 
plugin architecture.

Please fix admin/ajax.php to not use die() to finish code execution. The one on 
line 69 is especially disturbing in my case, but all of them should go.

Original issue reported on code.google.com by martijng...@gmail.com on 19 Dec 2011 at 3:31

GoogleCodeExporter commented 8 years ago
After further inspection of the code, I understand that the Javascript reads 
the output of the AJAX call and expects a '1' for a successful operation.

Still, replacing

 die($result)

with 

 print "$result\n";

has the same effect and solves the problem.

Original comment by martijng...@gmail.com on 19 Dec 2011 at 4:12

GoogleCodeExporter commented 8 years ago
Then again, if you insist on using die(), please consider doing it in a 
separate hook with an extremely high prio:

  add_action('wp_ajax_ngg_ajax_operation', 'ngg_ajax_operation_die', 100 );

  function ngg_ajax_operation_die ()
  {
     die $result;
  }

How to pass the result from one function to the other, I leave to your 
imagination ;-)

Original comment by martijng...@gmail.com on 19 Dec 2011 at 4:23

GoogleCodeExporter commented 8 years ago
What's about a filter / action hook , just before the die() ?

Original comment by alex.cologne on 16 Jan 2012 at 12:14

GoogleCodeExporter commented 8 years ago
Will close this for now, if there is a need for a filter/hook let me know

Original comment by alex.cologne on 18 Feb 2012 at 3:07

GoogleCodeExporter commented 8 years ago
I am sorry for not replying to this any sooner, I haven't looked at the issue 
for a while.

Your suggestion of adding a hook just before the die() will work for me; I added

    do_action ('ngg_ajax_final operation');

on line 68 of admin/ajax.php and changed my code to use that action instead of 
'wp_ajax_ngg_ajax_operation' with prio > 10.

Still, I think this is not a good solution because the plugin architecture 
remains crippled, but it will work. Please do add a hook like you suggested.

Original comment by martijng...@gmail.com on 12 Apr 2012 at 9:18