WordPress에서 새로운 등록이 발생하면 Discourse에서 사용자를 자동으로 생성하고 싶지만, 지원서를 작성하기 전에 액세스 권한을 부여하고 싶지 않습니다.
로그인 시 동기화와 사용자 승인 필수 설정을 사용하여 대부분의 기능을 구현할 수 있지만, Discourse에서 사용자가 승인되었는지 여부를 어떻게 조회해야 하는지 모르겠습니다.
Discourse가 사용자의 승인 상태를 저장하는 데 사용하는 WP 사용자 메타 필드가 있습니까? 그렇지 않다면, Discourse에서 해당 정보를 가져오기 위해 API를 사용해야 하는 것입니까?
제 목표는 WP 대시보드에 동적 링크가 포함된 버튼을 표시하는 것입니다. 이 링크는 승인 상태에 따라 변경되어야 합니다.
- 미승인 사용자: 동적 링크가 WP의 지원서 양식으로 이동합니다.
- 승인된 사용자: 동적 링크가 Discourse 커뮤니티로 이동합니다.
몇몇 게시물을 찾아보았지만 해결책을 찾지 못했습니다. 도움을 주시면 감사하겠습니다!
데이터 탐색기 쿼리를 작성하고 API를 통해 호출하는 방식으로 이 문제를 해결하는 방법을 찾았습니다. 잘 작동하지만, 이제 두 시스템 간에 호출을 수행하게 되므로, 데이터를 WP에 저장해 두는 것보다 당연히 느려집니다.
페이지 로딩이 느리다면, 저는 훅을 사용해 승인 데이터를 사용자 커스텀 필드에 직접 저장할 계획입니다. 진행하기 전에, WP-Discourse 플러그인이 이미 그렇게 하고 있는지 확인하는 것이 좋겠습니다.
angus
(Angus McLeod)
3
Hey @Himanshu_Singh, 간단히 말하면 WP Discourse 플러그인은 Discourse 사용자의 승인 상태를 저장하지 않습니다.
사용자가 WordPress에 로그인한 후 사용자 기록이 동기화된 후에 실행되는 wpdc_after_sync_sso 액션을 살펴보는 것을 권장합니다.
여기에 로그를 추가하면, 예를 들어
function wpdc_after_sync_sso_callback($discourse_user, $user_id) {
error_log(print_r($discourse_user, true));
}
add_action( 'wpdc_after_sync_sso', 'wpdc_after_sync_sso_callback', 10, 2 );
discourse_user 객체에 사용자의 승인 상태가 포함되어 있는 것을 확인할 수 있습니다. 예를 들어
stdClass Object
(
[id] => 43
[username] => angus4
[name] => angus4
...
[approved] =>
)
이 값을 사용자 커스텀 필드에 직접 저장하여 동적 링크를 생성하는 데 사용할 수 있습니다.
Angus, 감사합니다. 당신의 해결책은 우아합니다. 저는 약간 더 긴 경로를 따랐습니다.
- Data explorer API를 사용하여 승인 상태를 가져왔습니다.
- 이를 template_redirect 훅에 연결하여 로그인 시 승인 상태를 확인했습니다.
- 로그인 시 WP의 커스텀 필드를 업데이트했습니다.
- 사용자가 승인된 경우 API가 다시 실행되지 않도록 방지했습니다.
add_action('template_redirect', 'update_discourse_approval_status',1);
function update_discourse_approval_status () {
global $post;
$post_slug = $post->post_name;
if ($post_slug == 'page-slug'){
$current_user_id = get_current_user_id();
$approval_status_stored_in_wp = get_user_meta($current_user_id, 'custom-field', true);
if( $approval_status_stored_in_wp != 1) {
$user_discourse_id = (int)get_user_meta($current_user_id,'discourse_sso_user_id')[0];
//api credentials,
$api_key = 'api-key';
$api_username = 'name';
$discourse_user_param_string = 'params={"user_param":';
$discourse_user_param_string .= '"';
$discourse_user_param_string .= $user_discourse_id;
$discourse_user_param_string .= '"}';
//header is a must for Discourse API.
$headers = array("Content-Type: multipart/form-data;","Api-Key:$api_key","Api-Username:$api_username",);
$url = 'https://community.showprowess.com/admin/plugins/explorer/queries/<query-number>/run';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $discourse_user_param_string);
$result = curl_exec( $ch );
if ( curl_errno( $ch ) !== 0 ) {
// Handle error, call curl_close( $ch ) and return.
echo 'error';
curl_close ($ch);
return;
}
curl_close( $ch );
$decoded_result = json_decode( $result, true );
$user_approved = $decoded_result['rows'][0][0];
$updated = update_user_meta( $current_user_id, 'custom-field', $user_approved);
}
} //close if statement to check data stored in custom field
} // close if to check the right page
?>