자체 API 사용 시 무한 AJAX 루프 방지
WooCommerce Follow Up Emails 플러그인과 Ninja Forms 플러그인의 통합을 검토하고 있습니다(최종 목표는 Ninja Forms 제출에 대응하여 수동형 Follow up E-메일 템플릿을 보내는 것입니다).Ninja Forms 3를 사용하고 있습니다.
Action 클래스의 옵션을 정의할 때 사용자에게 템플릿 목록을 제공하여 사용자가 액션을 정의할 때 전송할 템플릿을 선택할 수 있도록 합니다.후속 이메일 플러그인에서 이메일 템플릿을 가져오기 위해 API 클라이언트, 특히get_emails()
method(이것이 GET 콜로 변환됩니다)/emails
(API URL에 엔드포인트)를 표시합니다.
문제는 다음과 같습니다.페이지마다ninja_forms_register_actions
action이 호출되며, 이 시간 동안 액션클래스를 인스턴스화합니다.의 기간 동안__construct
call, 액션 설정을 입력하고 이를 위해 Follow up Emails API를 호출합니다.이것에 의해, 페이지 로드가 개시됩니다.그 사이,ninja_forms_register_actions
액션은...라고 불립니다.
저는 이 문제를 예상했지만, 계획된 솔루션은 도움이 되지 않았습니다.즉, API 호출 결과를 저장하기 위해 다음과 같이 과도기를 사용할 계획이었습니다.
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;
}
그러나 로그의 결과는 다음과 같습니다.
[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
웹 서버 프로세스를 종료하거나 플러그인 폴더 삭제/이름 변경과 같은 급격한 작업이 발생할 때까지 계속됩니다.이 시점에서 일시적인 HTTP 오류 코드가 채워집니다(그 자체로는 놀랍지 않습니다).즉, 요청 후까지 과도 솔루션이 설정되지 않았기 때문에 과도 솔루션이 작동하지 않습니다.
이런 상황에서는 다음 항목에 대한 수표를 추가합니다.DOING_AJAX
단, 이것은 두 가지 이유로 적합하지 않습니다.Ninja Forms AJAX 프로세스에서 이 데이터를 사용할 수 있어야 합니다.또한 FUE API가 사용하지 않기 때문에 DOIN_AJAX가 실제로 여기서 설정될지는 잘 모르겠습니다.admin-ajax.php
다음과 같은 것으로 바꾸려고 생각하고 있습니다.
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;
}
그러나 옵션을 잠금으로 사용하는 것은 더럽고 잘못된 느낌이며 오브젝트 캐시를 사용 중일 때 오류가 발생할 여지가 있는 것 같습니다(WPEgine 등).이 문제를 해결하는 더 나은/정상적인 방법이 있습니까?아니면 위의 문제에 실제로 문제가 없습니까?
편집: 따라서 잠금 솔루션도 100% 작동하지 않습니다. WP Cron 작업을 수행하게 되었습니다.필요에 따라 템플릿 목록을 가져와 옵션에 저장합니다.저는 이 솔루션을 특별히 좋아하지는 않지만, 아직 더 나은 솔루션을 생각해내지 못했습니다.이 문제에 대한 일반적인 해결 방법이 있는지 여부에 관심이 있습니다.
마지막으로 출력된 error_log와 예상되는 첫 번째 error_log 사이의 메서드/함수 중 하나가 메서드를 다시 호출하는 것입니다.루프/재귀가 어디서 시작되는지 알아보려면 debug_backtrace()를 사용하여 콜스택과 루프/재귀가 시작되는 지점을 가져옵니다.
debug_backtrace는 마지막으로 작업한error_log 바로 뒤에 배치하는 것이 가장 좋습니다.
중 는 음음음음음음음음음음음음음 some라고 부른다._get_templates()
$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);
언급URL : https://stackoverflow.com/questions/37381088/prevent-infinite-ajax-loop-when-using-own-api
'source' 카테고리의 다른 글
Ubuntu에 루비 2.2.3이 있는 json gem을 설치할 수 없습니다. (0) | 2023.02.28 |
---|---|
리액트 라우터는 새로고침 시 동작하지만 링크를 클릭할 때는 동작하지 않는다. (0) | 2023.02.28 |
Babel "7.0.0-0"이 필요하지만 "6.26"이 로드되었습니다.3" (0) | 2023.02.28 |
woocommerce의 특정 제품에 대한 카트 품목 수량 변경 (0) | 2023.02.23 |
모듈을 찾을 수 없습니다.'@date-io/date-fns'을(를) 확인할 수 없습니다. (0) | 2023.02.23 |