| 1 |
<?php |
| 2 |
/** |
| 3 |
*@package formfacade |
| 4 |
**/ |
| 5 |
/** |
| 6 |
* Plugin Name: FormFacade |
| 7 |
* Plugin URI: https://formfacade.com/website/how-to-embed-google-forms-in-wordpress.html |
| 8 |
* Description: Customize your Google Form to suit your wordpress site |
| 9 |
* Version: 1.3.9 |
| 10 |
* Author: FormFacade |
| 11 |
* Author URI: https://formfacade.com |
| 12 |
* License: GPL v2 or Later |
| 13 |
* Text Domain: formfacade |
| 14 |
*/ |
| 15 |
/* |
| 16 |
This program is free software; you can redistribute it and/or |
| 17 |
modify it under the terms of the GNU General Public License |
| 18 |
as published by the Free Software Foundation; either version 2 |
| 19 |
of the License, or (at your option) any later version. |
| 20 |
|
| 21 |
This program is distributed in the hope that it will be useful, |
| 22 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
GNU General Public License for more details. |
| 25 |
|
| 26 |
You should have received a copy of the GNU General Public License |
| 27 |
along with this program; if not, write to the Free Software |
| 28 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 29 |
|
| 30 |
Copyright 2019 Mailrecipe LLC. |
| 31 |
*/ |
| 32 |
|
| 33 |
|
| 34 |
defined('ABSPATH') or die('Sorry! This request is not called properly'); |
| 35 |
function_exists('add_action') or die('Sorry! This request is called outside wordpress'); |
| 36 |
|
| 37 |
|
| 38 |
// Add menu items |
| 39 |
add_action('admin_menu', 'formfacade_plugin_menu'); |
| 40 |
|
| 41 |
function formfacade_plugin_menu() { |
| 42 |
// Add top-level menu item |
| 43 |
add_menu_page( |
| 44 |
'Formfacade Plugin', |
| 45 |
'Formfacade', |
| 46 |
'manage_options', |
| 47 |
'formfacade_home', |
| 48 |
'formfacade_home_page', |
| 49 |
'dashicons-forms', |
| 50 |
6 |
| 51 |
); |
| 52 |
|
| 53 |
// Add submenu page |
| 54 |
add_submenu_page( |
| 55 |
'formfacade_home', |
| 56 |
'About', |
| 57 |
'About', |
| 58 |
'manage_options', |
| 59 |
'formfacade_home', |
| 60 |
'formfacade_home_page' |
| 61 |
); |
| 62 |
|
| 63 |
add_submenu_page( |
| 64 |
'formfacade_home', |
| 65 |
'Dashboard', |
| 66 |
'Dashboard', |
| 67 |
'manage_options', |
| 68 |
'formfacade_dashboard', |
| 69 |
'formfacade_dashboard_page' |
| 70 |
); |
| 71 |
|
| 72 |
add_submenu_page( |
| 73 |
'formfacade_home', |
| 74 |
'Embed Google Forms', |
| 75 |
'Embed Google Forms', |
| 76 |
'manage_options', |
| 77 |
'embed_google_forms', |
| 78 |
'embed_google_forms_page' |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
function formfacade_home_page() { |
| 83 |
wp_enqueue_style('formfacade_styles_bootstrap', plugins_url('assets/css/bootstrap.min.css', __FILE__), [], '1.0.0'); |
| 84 |
wp_enqueue_style('formfacade_styles_custom', plugins_url('assets/css/style.css', __FILE__), [], '1.0.0'); |
| 85 |
|
| 86 |
wp_enqueue_script('formfacade_home_script', plugins_url('assets/js/home.js', __FILE__), [], '1.0.0', true); |
| 87 |
wp_enqueue_script('lottie_script', plugins_url('assets/js/lottie.js', __FILE__), [], '5.7.13', true); |
| 88 |
include(plugin_dir_path(__FILE__) . 'templates/home.php'); |
| 89 |
} |
| 90 |
|
| 91 |
function formfacade_dashboard_page() { |
| 92 |
$domain = "https://formfacade.com"; |
| 93 |
$url = $domain . "/login/website.html"; |
| 94 |
|
| 95 |
if ( isset($_GET['redirectURL']) ) { |
| 96 |
// Sanitize and validate the redirectURL parameter |
| 97 |
$redirectURL = sanitize_url($_GET['redirectURL']); |
| 98 |
|
| 99 |
// Ensure redirectURL only contains alphanumeric characters and slashes |
| 100 |
if (preg_match('/^[a-zA-Z0-9\/\-_.]+$/', $redirectURL)) { |
| 101 |
// Ensure redirectURL starts with a single slash |
| 102 |
if (substr($redirectURL, 0, 1) === '/') { |
| 103 |
$redirectURL = substr($redirectURL, 1); |
| 104 |
} |
| 105 |
|
| 106 |
$url = $domain . '/' . $redirectURL; |
| 107 |
} else { |
| 108 |
// Handle invalid redirectURL, possibly log an error or redirect to a default URL |
| 109 |
$url = $domain . "/404.html"; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
?> |
| 114 |
<div class="wrap" style="height: 100vh;"> |
| 115 |
<iframe id="myIframe" src="<?php echo esc_url($url); ?>" width="100%" height="100%" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe> |
| 116 |
</div> |
| 117 |
<?php |
| 118 |
} |
| 119 |
|
| 120 |
// Function to run on plugin activation |
| 121 |
function formfacade_plugin_activate() { |
| 122 |
// Set an option to indicate the plugin was just activated |
| 123 |
add_option('formfacade_plugin_activated', true); |
| 124 |
} |
| 125 |
|
| 126 |
// Register the activation hook |
| 127 |
register_activation_hook(__FILE__, 'formfacade_plugin_activate'); |
| 128 |
|
| 129 |
|
| 130 |
// Show admin notice and redirect to custom page |
| 131 |
function formfacade_plugin_redirect() { |
| 132 |
// Check if the plugin was just activated |
| 133 |
if (get_option('formfacade_plugin_activated', false)) { |
| 134 |
// Remove the activation option |
| 135 |
delete_option('formfacade_plugin_activated'); |
| 136 |
|
| 137 |
// Redirect to custom page |
| 138 |
wp_safe_redirect(admin_url('admin.php?page=embed_google_forms')); |
| 139 |
exit; |
| 140 |
} |
| 141 |
} |
| 142 |
add_action('admin_init', 'formfacade_plugin_redirect'); |
| 143 |
|
| 144 |
function get_site_unique_identifier() { |
| 145 |
$site_url = get_option('siteurl'); |
| 146 |
$unique_identifier = md5($site_url); |
| 147 |
return $site_url; |
| 148 |
} |
| 149 |
|
| 150 |
function embed_google_forms_page() { |
| 151 |
$pages = get_pages(); |
| 152 |
foreach ($pages as &$page) { |
| 153 |
$page->edit_url = get_edit_post_link($page->ID); |
| 154 |
} |
| 155 |
|
| 156 |
$domain = "https://formfacade.com"; |
| 157 |
$url = $domain . "/wordpress/onboard.html"; |
| 158 |
$preview_url = ''; |
| 159 |
$admin_url = admin_url('admin.php?page=formfacade_dashboard'); |
| 160 |
|
| 161 |
$user = wp_get_current_user(); |
| 162 |
$user_details = array( |
| 163 |
'email' => $user->user_email, |
| 164 |
'displayName' => $user->display_name, |
| 165 |
'siteName' => get_bloginfo('name'), |
| 166 |
'siteDesc' => get_bloginfo('description'), |
| 167 |
'siteURL' => get_site_url(), |
| 168 |
'homeURL' => get_home_url(), |
| 169 |
'emailHash' => md5($user->user_email), |
| 170 |
'siteHash' => md5(get_site_url()) |
| 171 |
); |
| 172 |
|
| 173 |
// Sanitize and validate the input |
| 174 |
$page_id = isset($_GET['pageId']) ? intval(sanitize_text_field(wp_unslash($_GET['pageId']))) : 0; |
| 175 |
$user_id = isset($_GET['userId']) ? sanitize_text_field(wp_unslash($_GET['userId'])) : ''; |
| 176 |
$publish_id = isset($_GET['publishId']) ? sanitize_text_field(wp_unslash($_GET['publishId'])) : ''; |
| 177 |
$page_name = isset($_GET['pageName']) ? sanitize_text_field(wp_unslash($_GET['pageName'])) : ''; |
| 178 |
|
| 179 |
// Validate that the IDs are numeric or alphanumeric as appropriate |
| 180 |
if ($page_id > 0 && $user_id && $publish_id) { |
| 181 |
$url = $url . "?pageId=" . esc_attr($page_id) . "&userId=" . esc_attr($user_id) . "&publishId=" . esc_attr($publish_id); |
| 182 |
$preview_url = get_permalink($page_id) . '?preview=true'; |
| 183 |
emebd_wordpress_script($page_id, $user_id, $publish_id); |
| 184 |
} else if($page_name){ |
| 185 |
$page_id = formfacade_new_page($page_name, $user_id, $publish_id); |
| 186 |
$url = $url . "?pageId=" . esc_attr($page_id) . "&userId=" . esc_attr($user_id) . "&publishId=" . esc_attr($publish_id); |
| 187 |
$preview_url = get_permalink($page_id) . '?preview=true'; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
?> |
| 192 |
<div class="wrap" style="height: 100vh;"> |
| 193 |
<iframe id="myIframe" src="<?php echo esc_url($url); ?>" width="100%" height="100%" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe> |
| 194 |
</div> |
| 195 |
<?php |
| 196 |
|
| 197 |
// Register and enqueue the script |
| 198 |
wp_register_script('formfacade_script', '', [], time(), true); |
| 199 |
wp_enqueue_script('formfacade_script'); |
| 200 |
|
| 201 |
// Add inline script |
| 202 |
$inline_script = " |
| 203 |
document.addEventListener('DOMContentLoaded', function() { |
| 204 |
var iframe = document.getElementById('myIframe'); |
| 205 |
var postedMessage = false; |
| 206 |
|
| 207 |
function postMessageToIframe() { |
| 208 |
var pages = " . wp_json_encode($pages) . "; |
| 209 |
var preview_url = '" . esc_url($preview_url) . "'; |
| 210 |
var user = " . wp_json_encode($user_details) . "; |
| 211 |
var data = {pages: pages, previewURL: preview_url, wordpressUser: user }; |
| 212 |
// console.log('Before postinggggggg', data); |
| 213 |
iframe.contentWindow.postMessage(data, '" . esc_url($domain) . "'); |
| 214 |
postedMessage = true; |
| 215 |
clearInterval(interval); |
| 216 |
} |
| 217 |
|
| 218 |
iframe.addEventListener('load', function() { |
| 219 |
if (!postedMessage) postMessageToIframe(); |
| 220 |
}); |
| 221 |
|
| 222 |
var interval = setInterval(function() { |
| 223 |
var iframe = document.getElementById('myIframe'); |
| 224 |
if (!postedMessage && iframe) postMessageToIframe(); |
| 225 |
}, 1000); |
| 226 |
|
| 227 |
window.addEventListener('message', function(event) { |
| 228 |
if (event.origin !== '" . esc_url($domain) . "') return; // Verify the origin |
| 229 |
var formData = event.data; // This will contain the form data sent from the iframe |
| 230 |
var admin_url = '" . esc_url($admin_url) . "'; |
| 231 |
if (formData && formData.indexOf('pageId') > -1) { |
| 232 |
var data = JSON.parse(formData); |
| 233 |
var url = window.location.href; |
| 234 |
var separator = url.indexOf('?') !== -1 ? '&' : '?'; |
| 235 |
url += separator + 'pageId=' + data.pageId + '&userId=' + data.userId + '&publishId=' + data.publishId; |
| 236 |
window.location.href = url; |
| 237 |
} else if(formData && formData.indexOf('pageName') > -1) { |
| 238 |
var data = JSON.parse(formData); |
| 239 |
var url = window.location.href; |
| 240 |
var separator = url.indexOf('?') !== -1 ? '&' : '?'; |
| 241 |
url += separator + 'pageName=' + data.pageName + '&userId=' + data.userId + '&publishId=' + data.publishId; |
| 242 |
window.location.href = url; |
| 243 |
} |
| 244 |
|
| 245 |
if (formData && formData.indexOf('redirectURL') > -1) { |
| 246 |
var data = JSON.parse(formData); |
| 247 |
url = admin_url + '&redirectURL=' + data.redirectURL; |
| 248 |
window.location.href = url; |
| 249 |
} |
| 250 |
}); |
| 251 |
}); |
| 252 |
"; |
| 253 |
wp_add_inline_script('formfacade_script', $inline_script); |
| 254 |
} |
| 255 |
|
| 256 |
function emebd_wordpress_script($pageId, $userId, $publishId) { |
| 257 |
$page = get_post($pageId); |
| 258 |
if ($page) { |
| 259 |
// Get the current post content |
| 260 |
$existing_content = $page->post_content; |
| 261 |
$embedUrl = 'https://formfacade.com/include/' . $userId . '/form/' . $publishId . '/wordpress.js?div=ff-compose'; |
| 262 |
|
| 263 |
if (strpos($existing_content, $publishId) === false) { |
| 264 |
$block_content = '<!-- wp:html -->'; |
| 265 |
$block_content .= '<!-- Custom HTML block -->'; |
| 266 |
|
| 267 |
wp_register_script('formfacade_embed_script', $embedUrl, [], time(), true); |
| 268 |
wp_enqueue_script('formfacade_embed_script'); |
| 269 |
$script_tag = wp_get_inline_script_tag('', ['src' => $embedUrl, 'async' => true, 'defer' => true]); |
| 270 |
$block_content .= '<div id="ff-compose"></div>' . $script_tag; |
| 271 |
|
| 272 |
$block_content .= '<!-- /Custom HTML block -->'; |
| 273 |
$block_content .= '<!-- /wp:html -->'; |
| 274 |
$updated_content = $existing_content . "\n\n" . $block_content; |
| 275 |
wp_update_post([ 'ID' => $pageId, 'post_content' => $updated_content, ]); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
function formfacade_new_page($pageName, $userId, $publishId) { |
| 281 |
$page = array( |
| 282 |
'post_title' => $pageName, |
| 283 |
'post_content' => '', |
| 284 |
'post_status' => 'publish', |
| 285 |
'post_type' => 'page' |
| 286 |
); |
| 287 |
|
| 288 |
$embedUrl = 'https://formfacade.com/include/' . $userId . '/form/' . $publishId . '/wordpress.js?div=ff-compose'; |
| 289 |
$pageId = wp_insert_post($page); |
| 290 |
|
| 291 |
if ($pageId) { |
| 292 |
$block_content = '<!-- wp:html -->'; |
| 293 |
$block_content .= '<!-- Custom HTML block -->'; |
| 294 |
|
| 295 |
wp_register_script('formfacade_embed_script', $embedUrl, [], time(), true); |
| 296 |
wp_enqueue_script('formfacade_embed_script'); |
| 297 |
$script_tag = wp_get_inline_script_tag('', ['src' => $embedUrl, 'async' => true, 'defer' => true]); |
| 298 |
$block_content .= '<div id="ff-compose"></div>' . $script_tag; |
| 299 |
|
| 300 |
$block_content .= '<!-- /Custom HTML block -->'; |
| 301 |
$block_content .= '<!-- /wp:html -->'; |
| 302 |
$updated_content = $existing_content . "\n\n" . $block_content; |
| 303 |
wp_update_post([ 'ID' => $pageId, 'post_content' => $updated_content, ]); |
| 304 |
} |
| 305 |
|
| 306 |
return $pageId; |
| 307 |
} |
| 308 |
|
| 309 |
class FormFacade |
| 310 |
{ |
| 311 |
function activate() |
| 312 |
{ |
| 313 |
} |
| 314 |
|
| 315 |
function deactivate() |
| 316 |
{ |
| 317 |
echo 'FormFacade plugin deactivated'; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
function renderFacade($atts = []) |
| 323 |
{ |
| 324 |
$id = sanitize_text_field($atts['id']); |
| 325 |
$appearance = 'wordpress'; |
| 326 |
|
| 327 |
// Check for appearance attributes |
| 328 |
if (array_key_exists('appearance', $atts)) { |
| 329 |
$appearance = sanitize_text_field($atts['appearance']); |
| 330 |
} |
| 331 |
|
| 332 |
if (array_key_exists('owner', $atts)) { |
| 333 |
$owner = sanitize_text_field($atts['owner']); |
| 334 |
$script_url = 'https://formfacade.com/include/' . esc_attr($owner) . '/form/' . esc_attr($id) . '/' . esc_attr($appearance) . '.js?div=ff-' . esc_attr($id); |
| 335 |
$script_tag = wp_get_inline_script_tag('', ['src' => $script_url, 'async' => true, 'defer' => true]); |
| 336 |
return '<div id="ff-' . esc_url($id) . '"></div>' . $script_tag; |
| 337 |
} else if ($id) { |
| 338 |
$embedUrl = 'https://formfacade.com/forms/d/e/' . esc_attr($id) . '/' . esc_attr($appearance) . '.js?div=ff-' . esc_attr($id); |
| 339 |
wp_register_script('formfacade_render_script', $embedUrl, [], null, true); |
| 340 |
wp_enqueue_script('formfacade_render_script'); |
| 341 |
$script_tag = wp_get_inline_script_tag('', ['src' => $embedUrl, 'async' => true, 'defer' => true]); |
| 342 |
return '<div id="ff-' . esc_url($id) . '"></div>' . $script_tag; |
| 343 |
} else { |
| 344 |
return '<div>Invalid form id.<br/>- For example, if the public url of your Google Form is: https://docs.google.com/forms/d/e/<span style="background:yellow;color:red;">1FAIpQLSdN-M-uIQN8FfjAZul_BQi0MKYARV_vqNKFejV0QFomAjtdGg</span>/viewform<br/>- Your public id issssssssssss: 1FAIpQLSdN-M-uIQN8FfjAZul_BQi0MKYARV_vqNKFejV0QFomAjtdGg<br/>- So, the short code that you need to add to your page will be: <br/>[formfacade id=1FAIpQLSdN-M-uIQN8FfjAZul_BQi0MKYARV_vqNKFejV0QFomAjtdGg]<br/><br/><i>For Support Contact: <b>support@formfacade.com</b></i></div>'; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
$formfacade = new FormFacade(); |
| 349 |
add_shortcode('formfacade', array($formfacade, 'renderFacade')); |
| 350 |
?> |