Skip to content

Commit

Permalink
Merge pull request formbricks#19 from formbricks/plugin-refactor
Browse files Browse the repository at this point in the history
chore: addressed feedback from wp team
  • Loading branch information
mattinannt authored Oct 18, 2024
2 parents 3816f39 + 25366b7 commit e5e2ed7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
37 changes: 22 additions & 15 deletions formbricks.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ function formbricks_settings_page_content()
$nonce = wp_create_nonce('formbricks_settings_nonce');
$form_submitted = isset($_POST['formbricks_settings_nonce_field']);

if ($form_submitted && wp_verify_nonce($_POST['formbricks_settings_nonce_field'], 'formbricks_settings_nonce')) {
// Nonce verification passed, process the form data
update_option('formbricks_environment_id', sanitize_text_field($_POST['formbricks_environment_id']));
update_option('formbricks_api_host', esc_url_raw($_POST['formbricks_api_host']));
if ($form_submitted && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['formbricks_settings_nonce_field'])), 'formbricks_settings_nonce')) {
$environment_id = isset($_POST['formbricks_environment_id']) ? sanitize_text_field(wp_unslash($_POST['formbricks_environment_id'])) : '';
$api_host = isset($_POST['formbricks_api_host']) ? esc_url_raw(wp_unslash($_POST['formbricks_api_host'])) : '';
update_option('formbricks_environment_id', $environment_id);
update_option('formbricks_api_host', $api_host);
echo '<div id="formbricks-settings-saved" class="updated notice is-dismissible"><p>Settings saved successfully!</p></div>';
} elseif ($form_submitted) {
echo '<div id="formbricks-settings-error" class="error notice is-dismissible"><p>Error saving settings: Nonce verification failed. Please try again.</p></div>';
Expand Down Expand Up @@ -238,7 +239,7 @@ function handlePingError() {
if (environmentId && apiHost) {
// Perform AJAX request
$.ajax({
url: apiHost + '/api/v1/client/' + environmentId + '/in-app/sync',
url: apiHost + '/api/v1/client/' + environmentId + '/environment',
type: 'GET',
dataType: 'json',
success: function (response) {
Expand Down Expand Up @@ -276,7 +277,7 @@ function handlePingError() {
function formbricks_register_toggle_settings()
{
register_setting('formbricks_toggle_group', 'formbricks_global_toggle', function ($option) {
if (isset($_POST['formbricks_toggle_nonce_field']) && wp_verify_nonce($_POST['formbricks_toggle_nonce_field'], 'formbricks_toggle_nonce')) {
if (isset($_POST['formbricks_toggle_nonce_field']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['formbricks_toggle_nonce_field'])), 'formbricks_toggle_nonce')) {
return $option;
} else {
add_settings_error('formbricks_global_toggle', 'formbricks_toggle_nonce_error', 'Error saving settings: Nonce verification failed. Please try again.');
Expand All @@ -301,28 +302,25 @@ function formbricks_register_settings()
function formbricks_enqueue_script()
{
if (!is_admin()) {
// Check if the global toggle is on
$globalToggle = get_option('formbricks_global_toggle');

if ($globalToggle == 'on') {
// Get options
$environmentId = get_option('formbricks_environment_id');
$apiHost = get_option('formbricks_api_host');

if (!empty($environmentId) && !empty($apiHost)) {
wp_enqueue_script(
'formbricks',
$apiHost . '/api/packages/website',
$apiHost . '/js/formbricks.umd.cjs',
array('jquery'),
'1.0.1',
"3.0.1",
true
);

// Enqueue index.js after formbricks
wp_enqueue_script(
'formbricks-init',
plugin_dir_url(__FILE__) . 'public/js/index.js',
array('jquery', 'formbricks'), // Add 'formbricks' as a dependency
array('jquery', 'formbricks'),
'1.0.1',
true
);
Expand All @@ -336,10 +334,19 @@ function formbricks_enqueue_script()
'before'
);
}
} else {
// Formbricks is disabled
}
}
}

add_action('wp_enqueue_scripts', 'formbricks_enqueue_script');
add_action('wp_enqueue_scripts', 'formbricks_enqueue_script');

// Enqueue Scripts and Styles
function formbricks_enqueue_admin_scripts($hook) {
if ('toplevel_page_formbricks-settings' !== $hook) {
return;
}
wp_enqueue_script('formbricks-admin', plugin_dir_url(__FILE__) . 'admin/js/formbricks-admin.js', array('jquery'), '1.0.1', true);
wp_add_inline_script('formbricks-admin', 'jQuery(document).ready(function ($) { setTimeout(function () { $("#formbricks-settings-saved").fadeOut("slow"); }, 5000); });');
}

add_action('admin_enqueue_scripts', 'formbricks_enqueue_admin_scripts');
7 changes: 4 additions & 3 deletions public/class-formbricks-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ public function enqueue_styles()
* between the defined hooks and the functions defined in this
* class.
*/

wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/formbricks-public.css', array(), $this->version, 'all');
if (!is_admin()) {
wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/formbricks-public.css', array(), $this->version, 'all');
}
}

/**
Expand Down Expand Up @@ -103,7 +104,7 @@ public function enqueue_scripts()
$api_host = get_option('formbricks_api_host');

if (!empty($environment_id) && !empty($api_host)) {
wp_enqueue_script($this->plugin_name, $api_host . '/api/packages/website', array('jquery'), $this->version, false);
wp_enqueue_script($this->plugin_name, $api_host . '/js/formbricks.umd.cjs', array('jquery'), "3.0.1", false);
wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/index.js', array('jquery'), $this->version, false);
}
}
Expand Down

0 comments on commit e5e2ed7

Please sign in to comment.