Prevent infinite AJAX loop when using own API The Next CEO of Stack OverflowPrevent browser caching of jQuery AJAX call resultAjax Validation Using jquery?Symfony2 REST API implementation with session authentication on AJAX loginWordpress form plugin only get one field in hookvalidating form before it submitHow to write wordPress Hook?Mailchimp API call using AJAXPrevent infinite loop on update function that updates itself?Email exists check in jquery add method validation with ajax call, but it returns later and not validating correctlyWordPress AJAX is_admin is true, causing issues.

How to start emacs in "nothing" mode (`fundamental-mode`)

Are there any unintended negative consequences to allowing PCs to gain multiple levels at once in a short milestone-XP game?

Unreliable Magic - Is it worth it?

Non-deterministic sum of floats

Why am I allowed to create multiple unique pointers from a single object?

Return the Closest Prime Number

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

What exact does MIB represent in SNMP? How is it different from OID?

How does the mv command work with external drives?

What does convergence in distribution "in the Gromov–Hausdorff" sense mean?

Interfacing a button to MCU (and PC) with 50m long cable

Complex fractions

How to count occurrences of text in a file?

What was the first Unix version to run on a microcomputer?

Is it professional to write unrelated content in an almost-empty email?

Why didn't Khan get resurrected in the Genesis Explosion?

Should I tutor a student who I know has cheated on their homework?

Would a galaxy be visible from outside, but nearby?

How do I avoid eval and parse?

Why do professional authors make "consistency" mistakes? And how to avoid them?

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

What happens if you roll doubles 3 times then land on "Go to jail?"

What is the result of assigning to std::vector<T>::begin()?

Why do we use the plural of movies in this phrase "We went to the movies last night."?



Prevent infinite AJAX loop when using own API



The Next CEO of Stack OverflowPrevent browser caching of jQuery AJAX call resultAjax Validation Using jquery?Symfony2 REST API implementation with session authentication on AJAX loginWordpress form plugin only get one field in hookvalidating form before it submitHow to write wordPress Hook?Mailchimp API call using AJAXPrevent infinite loop on update function that updates itself?Email exists check in jquery add method validation with ajax call, but it returns later and not validating correctlyWordPress AJAX is_admin is true, causing issues.










7















I am currently trying to figure out integration between 2 Wordpress plugins: the WooCommerce Follow Up Emails plugin, and the Ninja Forms plugin (with the end goal that we can send a manual-type followup email template as an action in response to ninja forms submission). We are using Ninja Forms 3, for what it's worth.



When defining the options for the Action class I am providing a list of the templates to the user, so that when defining the action they can choose the template to send. To get the email templates from the follow-up emails plugin I am using their API client, specifically the get_emails() method (which, in turn, translates to a GET call to the /emails endpoint under their API URL).



The problem is this: On every page load the ninja_forms_register_actions action is called, during which I instantiate my action class. During the __construct call, we populate the settings for the action, and in order to do so, we call the Follow Up Emails API. This initiates a page load, during which the ninja_forms_register_actions action is called...



Although I did anticipate this problem, my planned solution did not help: that is, I had planned to use transients to store the result of the API call, like so:



private static function _get_templates()

error_log('_get_templates() started - ' . microtime(true));
if (false === ($templates = get_transient(self::TEMPLATE_TRANSIENT)))
error_log('_get_templates() fetching - ' . microtime(true));
$fue_api = self::fue_api();
$templates = $fue_api->get_emails();
set_transient(self::TEMPLATE_TRANSIENT, $templates, self::TEMPLATE_TRANSIENT_EXPIRY);
error_log('_get_templates() fetched - ' . microtime(true));

error_log('_get_templates() done - ' . microtime(true));

return $templates;



However the result in my logs is the following:



[22-May-2016 23:53:33 UTC] _get_templates() started - 1463961213.692187
[22-May-2016 23:53:33 UTC] _get_templates() fetching - 1463961213.694222
[22-May-2016 23:53:34 UTC] _get_templates() started - 1463961214.05998
[22-May-2016 23:53:34 UTC] _get_templates() fetching - 1463961214.061054
[22-May-2016 23:53:38 UTC] _get_templates() started - 1463961218.660683
[22-May-2016 23:53:38 UTC] _get_templates() fetching - 1463961218.661265
[22-May-2016 23:53:40 UTC] _get_templates() started - 1463961220.772228
[22-May-2016 23:53:40 UTC] _get_templates() fetching - 1463961220.774142
[22-May-2016 23:53:41 UTC] _get_templates() started - 1463961221.150277
[22-May-2016 23:53:41 UTC] _get_templates() fetching - 1463961221.654757
[22-May-2016 23:53:45 UTC] _get_templates() started - 1463961225.306565
[22-May-2016 23:53:45 UTC] _get_templates() fetching - 1463961225.308898
[22-May-2016 23:53:46 UTC] _get_templates() started - 1463961226.281794
[22-May-2016 23:53:46 UTC] _get_templates() fetching - 1463961226.283803


Which continues until I kill the web server process or something else drastic like deleting/renaming the plugin folder, at which point the transient is filled with an HTTP error code (which is, in itself, unsurprising). So clearly my transient solution doesn't work as the transient is still unset until after the request.



In some situations like this I would add a check for DOING_AJAX, however this doesn't fit for two reasons - I still need this data to be available to the Ninja Forms AJAX processes, and also I am not sure if DOING_AJAX would actually be set here, as the FUE API does not use admin-ajax.php. I was considering changing to something like the following:



private static function _get_templates()

error_log('_get_templates() started - ' . microtime(true));
if (false === get_option(self::TEMPLATE_LOCK_OPTION, false) && false === ($templates = get_transient(self::TEMPLATE_TRANSIENT)))
delete_option(self::TEMPLATE_LOCK_OPTION);
add_option(self::TEMPLATE_LOCK_OPTION, true, '', 'no');
error_log('_get_templates() fetching - ' . microtime(true));
$fue_api = self::fue_api();
$templates = $fue_api->get_emails();
delete_option(self::TEMPLATE_LOCK_OPTION);
set_transient(self::TEMPLATE_TRANSIENT, $templates, self::TEMPLATE_TRANSIENT_EXPIRY);
error_log('_get_templates() fetched - ' . microtime(true));

error_log('_get_templates() done - ' . microtime(true));

return $templates;



But using options as locks feel dirty and wrong, and I feel like it leaves room for errors when object caching is in use (eg WPEngine et al). Is there a better/normal way to deal with this, or, alternatively, is there no real problem with the above?



Edit: So the lock solution doesn't work 100% either - I've ended up doing this with a WP Cron job - every ten minutes we fetch the list of templates, rather than as needed, and store it in an option. I don't like particularly like this solution - but I haven't been able to come up with a better one as of yet. Still interested if there is a common solution for this problem.










share|improve this question




























    7















    I am currently trying to figure out integration between 2 Wordpress plugins: the WooCommerce Follow Up Emails plugin, and the Ninja Forms plugin (with the end goal that we can send a manual-type followup email template as an action in response to ninja forms submission). We are using Ninja Forms 3, for what it's worth.



    When defining the options for the Action class I am providing a list of the templates to the user, so that when defining the action they can choose the template to send. To get the email templates from the follow-up emails plugin I am using their API client, specifically the get_emails() method (which, in turn, translates to a GET call to the /emails endpoint under their API URL).



    The problem is this: On every page load the ninja_forms_register_actions action is called, during which I instantiate my action class. During the __construct call, we populate the settings for the action, and in order to do so, we call the Follow Up Emails API. This initiates a page load, during which the ninja_forms_register_actions action is called...



    Although I did anticipate this problem, my planned solution did not help: that is, I had planned to use transients to store the result of the API call, like so:



    private static function _get_templates()

    error_log('_get_templates() started - ' . microtime(true));
    if (false === ($templates = get_transient(self::TEMPLATE_TRANSIENT)))
    error_log('_get_templates() fetching - ' . microtime(true));
    $fue_api = self::fue_api();
    $templates = $fue_api->get_emails();
    set_transient(self::TEMPLATE_TRANSIENT, $templates, self::TEMPLATE_TRANSIENT_EXPIRY);
    error_log('_get_templates() fetched - ' . microtime(true));

    error_log('_get_templates() done - ' . microtime(true));

    return $templates;



    However the result in my logs is the following:



    [22-May-2016 23:53:33 UTC] _get_templates() started - 1463961213.692187
    [22-May-2016 23:53:33 UTC] _get_templates() fetching - 1463961213.694222
    [22-May-2016 23:53:34 UTC] _get_templates() started - 1463961214.05998
    [22-May-2016 23:53:34 UTC] _get_templates() fetching - 1463961214.061054
    [22-May-2016 23:53:38 UTC] _get_templates() started - 1463961218.660683
    [22-May-2016 23:53:38 UTC] _get_templates() fetching - 1463961218.661265
    [22-May-2016 23:53:40 UTC] _get_templates() started - 1463961220.772228
    [22-May-2016 23:53:40 UTC] _get_templates() fetching - 1463961220.774142
    [22-May-2016 23:53:41 UTC] _get_templates() started - 1463961221.150277
    [22-May-2016 23:53:41 UTC] _get_templates() fetching - 1463961221.654757
    [22-May-2016 23:53:45 UTC] _get_templates() started - 1463961225.306565
    [22-May-2016 23:53:45 UTC] _get_templates() fetching - 1463961225.308898
    [22-May-2016 23:53:46 UTC] _get_templates() started - 1463961226.281794
    [22-May-2016 23:53:46 UTC] _get_templates() fetching - 1463961226.283803


    Which continues until I kill the web server process or something else drastic like deleting/renaming the plugin folder, at which point the transient is filled with an HTTP error code (which is, in itself, unsurprising). So clearly my transient solution doesn't work as the transient is still unset until after the request.



    In some situations like this I would add a check for DOING_AJAX, however this doesn't fit for two reasons - I still need this data to be available to the Ninja Forms AJAX processes, and also I am not sure if DOING_AJAX would actually be set here, as the FUE API does not use admin-ajax.php. I was considering changing to something like the following:



    private static function _get_templates()

    error_log('_get_templates() started - ' . microtime(true));
    if (false === get_option(self::TEMPLATE_LOCK_OPTION, false) && false === ($templates = get_transient(self::TEMPLATE_TRANSIENT)))
    delete_option(self::TEMPLATE_LOCK_OPTION);
    add_option(self::TEMPLATE_LOCK_OPTION, true, '', 'no');
    error_log('_get_templates() fetching - ' . microtime(true));
    $fue_api = self::fue_api();
    $templates = $fue_api->get_emails();
    delete_option(self::TEMPLATE_LOCK_OPTION);
    set_transient(self::TEMPLATE_TRANSIENT, $templates, self::TEMPLATE_TRANSIENT_EXPIRY);
    error_log('_get_templates() fetched - ' . microtime(true));

    error_log('_get_templates() done - ' . microtime(true));

    return $templates;



    But using options as locks feel dirty and wrong, and I feel like it leaves room for errors when object caching is in use (eg WPEngine et al). Is there a better/normal way to deal with this, or, alternatively, is there no real problem with the above?



    Edit: So the lock solution doesn't work 100% either - I've ended up doing this with a WP Cron job - every ten minutes we fetch the list of templates, rather than as needed, and store it in an option. I don't like particularly like this solution - but I haven't been able to come up with a better one as of yet. Still interested if there is a common solution for this problem.










    share|improve this question


























      7












      7








      7








      I am currently trying to figure out integration between 2 Wordpress plugins: the WooCommerce Follow Up Emails plugin, and the Ninja Forms plugin (with the end goal that we can send a manual-type followup email template as an action in response to ninja forms submission). We are using Ninja Forms 3, for what it's worth.



      When defining the options for the Action class I am providing a list of the templates to the user, so that when defining the action they can choose the template to send. To get the email templates from the follow-up emails plugin I am using their API client, specifically the get_emails() method (which, in turn, translates to a GET call to the /emails endpoint under their API URL).



      The problem is this: On every page load the ninja_forms_register_actions action is called, during which I instantiate my action class. During the __construct call, we populate the settings for the action, and in order to do so, we call the Follow Up Emails API. This initiates a page load, during which the ninja_forms_register_actions action is called...



      Although I did anticipate this problem, my planned solution did not help: that is, I had planned to use transients to store the result of the API call, like so:



      private static function _get_templates()

      error_log('_get_templates() started - ' . microtime(true));
      if (false === ($templates = get_transient(self::TEMPLATE_TRANSIENT)))
      error_log('_get_templates() fetching - ' . microtime(true));
      $fue_api = self::fue_api();
      $templates = $fue_api->get_emails();
      set_transient(self::TEMPLATE_TRANSIENT, $templates, self::TEMPLATE_TRANSIENT_EXPIRY);
      error_log('_get_templates() fetched - ' . microtime(true));

      error_log('_get_templates() done - ' . microtime(true));

      return $templates;



      However the result in my logs is the following:



      [22-May-2016 23:53:33 UTC] _get_templates() started - 1463961213.692187
      [22-May-2016 23:53:33 UTC] _get_templates() fetching - 1463961213.694222
      [22-May-2016 23:53:34 UTC] _get_templates() started - 1463961214.05998
      [22-May-2016 23:53:34 UTC] _get_templates() fetching - 1463961214.061054
      [22-May-2016 23:53:38 UTC] _get_templates() started - 1463961218.660683
      [22-May-2016 23:53:38 UTC] _get_templates() fetching - 1463961218.661265
      [22-May-2016 23:53:40 UTC] _get_templates() started - 1463961220.772228
      [22-May-2016 23:53:40 UTC] _get_templates() fetching - 1463961220.774142
      [22-May-2016 23:53:41 UTC] _get_templates() started - 1463961221.150277
      [22-May-2016 23:53:41 UTC] _get_templates() fetching - 1463961221.654757
      [22-May-2016 23:53:45 UTC] _get_templates() started - 1463961225.306565
      [22-May-2016 23:53:45 UTC] _get_templates() fetching - 1463961225.308898
      [22-May-2016 23:53:46 UTC] _get_templates() started - 1463961226.281794
      [22-May-2016 23:53:46 UTC] _get_templates() fetching - 1463961226.283803


      Which continues until I kill the web server process or something else drastic like deleting/renaming the plugin folder, at which point the transient is filled with an HTTP error code (which is, in itself, unsurprising). So clearly my transient solution doesn't work as the transient is still unset until after the request.



      In some situations like this I would add a check for DOING_AJAX, however this doesn't fit for two reasons - I still need this data to be available to the Ninja Forms AJAX processes, and also I am not sure if DOING_AJAX would actually be set here, as the FUE API does not use admin-ajax.php. I was considering changing to something like the following:



      private static function _get_templates()

      error_log('_get_templates() started - ' . microtime(true));
      if (false === get_option(self::TEMPLATE_LOCK_OPTION, false) && false === ($templates = get_transient(self::TEMPLATE_TRANSIENT)))
      delete_option(self::TEMPLATE_LOCK_OPTION);
      add_option(self::TEMPLATE_LOCK_OPTION, true, '', 'no');
      error_log('_get_templates() fetching - ' . microtime(true));
      $fue_api = self::fue_api();
      $templates = $fue_api->get_emails();
      delete_option(self::TEMPLATE_LOCK_OPTION);
      set_transient(self::TEMPLATE_TRANSIENT, $templates, self::TEMPLATE_TRANSIENT_EXPIRY);
      error_log('_get_templates() fetched - ' . microtime(true));

      error_log('_get_templates() done - ' . microtime(true));

      return $templates;



      But using options as locks feel dirty and wrong, and I feel like it leaves room for errors when object caching is in use (eg WPEngine et al). Is there a better/normal way to deal with this, or, alternatively, is there no real problem with the above?



      Edit: So the lock solution doesn't work 100% either - I've ended up doing this with a WP Cron job - every ten minutes we fetch the list of templates, rather than as needed, and store it in an option. I don't like particularly like this solution - but I haven't been able to come up with a better one as of yet. Still interested if there is a common solution for this problem.










      share|improve this question
















      I am currently trying to figure out integration between 2 Wordpress plugins: the WooCommerce Follow Up Emails plugin, and the Ninja Forms plugin (with the end goal that we can send a manual-type followup email template as an action in response to ninja forms submission). We are using Ninja Forms 3, for what it's worth.



      When defining the options for the Action class I am providing a list of the templates to the user, so that when defining the action they can choose the template to send. To get the email templates from the follow-up emails plugin I am using their API client, specifically the get_emails() method (which, in turn, translates to a GET call to the /emails endpoint under their API URL).



      The problem is this: On every page load the ninja_forms_register_actions action is called, during which I instantiate my action class. During the __construct call, we populate the settings for the action, and in order to do so, we call the Follow Up Emails API. This initiates a page load, during which the ninja_forms_register_actions action is called...



      Although I did anticipate this problem, my planned solution did not help: that is, I had planned to use transients to store the result of the API call, like so:



      private static function _get_templates()

      error_log('_get_templates() started - ' . microtime(true));
      if (false === ($templates = get_transient(self::TEMPLATE_TRANSIENT)))
      error_log('_get_templates() fetching - ' . microtime(true));
      $fue_api = self::fue_api();
      $templates = $fue_api->get_emails();
      set_transient(self::TEMPLATE_TRANSIENT, $templates, self::TEMPLATE_TRANSIENT_EXPIRY);
      error_log('_get_templates() fetched - ' . microtime(true));

      error_log('_get_templates() done - ' . microtime(true));

      return $templates;



      However the result in my logs is the following:



      [22-May-2016 23:53:33 UTC] _get_templates() started - 1463961213.692187
      [22-May-2016 23:53:33 UTC] _get_templates() fetching - 1463961213.694222
      [22-May-2016 23:53:34 UTC] _get_templates() started - 1463961214.05998
      [22-May-2016 23:53:34 UTC] _get_templates() fetching - 1463961214.061054
      [22-May-2016 23:53:38 UTC] _get_templates() started - 1463961218.660683
      [22-May-2016 23:53:38 UTC] _get_templates() fetching - 1463961218.661265
      [22-May-2016 23:53:40 UTC] _get_templates() started - 1463961220.772228
      [22-May-2016 23:53:40 UTC] _get_templates() fetching - 1463961220.774142
      [22-May-2016 23:53:41 UTC] _get_templates() started - 1463961221.150277
      [22-May-2016 23:53:41 UTC] _get_templates() fetching - 1463961221.654757
      [22-May-2016 23:53:45 UTC] _get_templates() started - 1463961225.306565
      [22-May-2016 23:53:45 UTC] _get_templates() fetching - 1463961225.308898
      [22-May-2016 23:53:46 UTC] _get_templates() started - 1463961226.281794
      [22-May-2016 23:53:46 UTC] _get_templates() fetching - 1463961226.283803


      Which continues until I kill the web server process or something else drastic like deleting/renaming the plugin folder, at which point the transient is filled with an HTTP error code (which is, in itself, unsurprising). So clearly my transient solution doesn't work as the transient is still unset until after the request.



      In some situations like this I would add a check for DOING_AJAX, however this doesn't fit for two reasons - I still need this data to be available to the Ninja Forms AJAX processes, and also I am not sure if DOING_AJAX would actually be set here, as the FUE API does not use admin-ajax.php. I was considering changing to something like the following:



      private static function _get_templates()

      error_log('_get_templates() started - ' . microtime(true));
      if (false === get_option(self::TEMPLATE_LOCK_OPTION, false) && false === ($templates = get_transient(self::TEMPLATE_TRANSIENT)))
      delete_option(self::TEMPLATE_LOCK_OPTION);
      add_option(self::TEMPLATE_LOCK_OPTION, true, '', 'no');
      error_log('_get_templates() fetching - ' . microtime(true));
      $fue_api = self::fue_api();
      $templates = $fue_api->get_emails();
      delete_option(self::TEMPLATE_LOCK_OPTION);
      set_transient(self::TEMPLATE_TRANSIENT, $templates, self::TEMPLATE_TRANSIENT_EXPIRY);
      error_log('_get_templates() fetched - ' . microtime(true));

      error_log('_get_templates() done - ' . microtime(true));

      return $templates;



      But using options as locks feel dirty and wrong, and I feel like it leaves room for errors when object caching is in use (eg WPEngine et al). Is there a better/normal way to deal with this, or, alternatively, is there no real problem with the above?



      Edit: So the lock solution doesn't work 100% either - I've ended up doing this with a WP Cron job - every ten minutes we fetch the list of templates, rather than as needed, and store it in an option. I don't like particularly like this solution - but I haven't been able to come up with a better one as of yet. Still interested if there is a common solution for this problem.







      php ajax wordpress ninja-forms






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 7 at 15:25









      Gufran Hasan

      3,66941628




      3,66941628










      asked May 23 '16 at 0:33









      Chris O'KellyChris O'Kelly

      1,27421128




      1,27421128






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37381088%2fprevent-infinite-ajax-loop-when-using-own-api%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37381088%2fprevent-infinite-ajax-loop-when-using-own-api%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          1928 у кіно

          Захаров Федір Захарович

          Ель Греко