Created
June 23, 2023 10:38
-
-
Save radicalappdev/0eb8f308dc567a1c33cf752e5add8737 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Rad Webhook Demo | |
Plugin URI: <https://vrhermit.com/?p=4154> | |
Description: Adds custom WP JSON endpoints that use the FileMaker OData API to submit form data to FileMaker. | |
Version: 1.0 | |
Author: radicalappdev | |
Author URI: <https://radicalappdev.com/> | |
*/ | |
// --- Boilerplate code --- | |
// Activation function | |
function rad_webhook_demo_activate() | |
{ | |
// Perform any necessary actions upon activation | |
} | |
register_activation_hook(__FILE__, 'rad_webhook_demo_activate'); | |
// Deactivation function | |
function rad_webhook_demo_deactivate() | |
{ | |
// Perform any necessary actions upon deactivation | |
} | |
register_deactivation_hook(__FILE__, 'rad_webhook_demo_deactivate'); | |
// HELPER FUNCTIONS | |
function rad_helper_fmp_to_wp($value) | |
{ | |
// Convert FMP line breaks to WP line breaks | |
$value = str_replace(["\\r", "\\n"], '<br>', $value); | |
return $value; | |
} | |
function rad_helper_wp_to_fmp($value) | |
{ | |
// Convert WP line breaks to FMP line breaks | |
$value = str_replace(['<br>', '<br/>', '<br />'], "\\n", $value); | |
return $value; | |
} | |
// --- Custom endpoints start here --- | |
// Create a new endpoint for the wp-json API called rad-webhook-demo | |
// For example: https://example.com/wp-json/rad-webhook-demo/submit | |
add_action('rest_api_init', function () { | |
register_rest_route('rad-webhook-demo/', '/submit', array( | |
'methods' => 'POST', | |
'callback' => 'rad_webhook_demo_submit', | |
)); | |
}); | |
// The function that is called when we hit the endpoing `rad_webhook_demo/submit` | |
function rad_webhook_demo_submit($request) | |
{ | |
// Check if the request method is POST | |
if (strtolower($_SERVER['REQUEST_METHOD']) !== 'post') { | |
wp_send_json_error('Invalid request method', 405); | |
} | |
// Get the form data | |
$form_name = sanitize_text_field($_POST['form_name']); | |
$email = sanitize_email($_POST['email']); | |
$name = sanitize_text_field($_POST['name']); | |
$note = sanitize_text_field($_POST['note']); | |
// convert WP line breaks to FMP line breaks | |
$note = rad_helper_wp_to_fmp($note); | |
// Define an object: fields in FileMaker | |
$data = array( | |
'form_name' => $form_name, | |
'name' => $name, | |
'email' => $email, | |
'note' => $note | |
); | |
// Encode the data object as JSON | |
$json_data = json_encode($data); | |
// Get username and password from plugin settings | |
$username = get_option('rad-webhook-demo-username'); | |
$password = get_option('rad-webhook-demo-password'); | |
$auth = base64_encode($username . ':' . $password); | |
// Execute the API call | |
$baseurl = get_option('rad-webhook-demo-baseurl'); | |
$url = $baseurl . "/FormEntries"; | |
$response = wp_remote_post($url, array( | |
'headers' => array( | |
'Content-Type' => 'application/json', | |
'Authorization' => 'Basic ' . $auth, | |
'Prefer: return=minimal' | |
), | |
'body' => $json_data, | |
)); | |
if (is_wp_error($response)) { | |
wp_send_json_error($response->get_error_message(), $response->get_error_code()); | |
} else { | |
$response_body = wp_remote_retrieve_body($response); | |
$response_body = rad_helper_fmp_to_wp($response_body); | |
$result = json_decode($response_body); // any data transformation can be done here | |
wp_send_json($result); | |
} | |
} | |
// --- Plugin settings page --- | |
// Add API settings to the database | |
add_action('admin_init', 'rad_webhook_demo_register_settings'); | |
function rad_webhook_demo_register_settings() | |
{ | |
add_option('rad-webhook-demo-username', ''); | |
add_option('rad-webhook-demo-password', ''); | |
add_option('rad-webhook-demo-baseurl', ''); | |
register_setting('rad-webhook-demo-settings', 'rad-webhook-demo-username'); | |
register_setting('rad-webhook-demo-settings', 'rad-webhook-demo-password'); | |
register_setting('rad-webhook-demo-settings', 'rad-webhook-demo-baseurl'); | |
} | |
add_action('admin_menu', 'rad_webhook_demo_add_options_page'); | |
function rad_webhook_demo_add_options_page() | |
{ | |
add_options_page( | |
'Settings for Rad Webhook Demo', | |
'Rad Webhook Demo', | |
'manage_options', | |
'rad-webhook-demo-settings', | |
'rad_webhook_demo_render_options_page' | |
); | |
} | |
function rad_webhook_demo_render_options_page() | |
{ | |
?> | |
<div class="wrap"> | |
<h1>Settings for Rad Webhook Demo</h1> | |
<form method="post" action="options.php"> | |
<?php settings_fields('rad-webhook-demo-settings'); ?> | |
<?php do_settings_sections('rad-webhook-demo-settings'); ?> | |
<p>Enter a FileMaker User account with OData access</p> | |
<table class="form-table"> | |
<tr valign="top"> | |
<th scope="row">Username</th> | |
<td><input type="text" name="rad-webhook-demo-username" value="<?php echo esc_attr(get_option('rad-webhook-demo-username')); ?>" /></td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row">Password</th> | |
<td><input type="password" name="rad-webhook-demo-password" value="<?php echo esc_attr(get_option('rad-webhook-demo-password')); ?>" /></td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row">Base URL</th> | |
<td><input type="text" name="rad-webhook-demo-baseurl" value="<?php echo esc_attr(get_option('rad-webhook-demo-baseurl')); ?>" /></td> | |
</tr> | |
</table> | |
<?php submit_button(); ?> | |
</form> | |
</div> | |
<?php | |
} | |
add_filter('option_page_capability_rad-webhook-demo-settings', function () { | |
return 'manage_options'; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment