PluginProbe
FormFacade – Embed Google Forms in your website / trunk
FormFacade – Embed Google Forms in your website vtrunk
trunk 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 2.0
formfacade / formfacade.php

formfacade.php in FormFacade – Embed Google Forms in your website trunk, at formfacade.php

384 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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: 2.0
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 // Cache-bust the plugin's own CSS/JS by file modification time so browsers
39 // always pick up edits instead of serving a stale cached copy.
40 function formfacade_asset_ver($relative_path) {
41 $full = plugin_dir_path(__FILE__) . ltrim($relative_path, '/');
42 return file_exists($full) ? filemtime($full) : '1.4.1';
43 }
44
45 // Shared CSS that makes an embedded iframe fill the admin canvas with no scrollbars.
46 function formfacade_fullscreen_iframe_css() {
47 ?>
48 <style>
49 #wpcontent { padding-left: 0; }
50 #wpbody-content { padding-bottom: 0; }
51 #wpfooter { display: none; }
52 .wrap.ff-fullscreen {
53 margin: 0;
54 height: calc(100vh - var(--wp-admin--admin-bar--height, 32px));
55 }
56 .wrap.ff-fullscreen iframe {
57 display: block;
58 width: 100%;
59 height: 100%;
60 border: 0;
61 }
62 </style>
63 <?php
64 }
65
66
67 // Add menu items
68 add_action('admin_menu', 'formfacade_plugin_menu');
69
70 function formfacade_plugin_menu() {
71 // Add top-level menu item
72 add_menu_page(
73 'Formfacade Plugin',
74 'Formfacade',
75 'manage_options',
76 'formfacade_home',
77 'formfacade_home_page',
78 'dashicons-forms',
79 6
80 );
81
82 // Add submenu page
83 add_submenu_page(
84 'formfacade_home',
85 'About',
86 'About',
87 'manage_options',
88 'formfacade_home',
89 'formfacade_home_page'
90 );
91
92 add_submenu_page(
93 'formfacade_home',
94 'Dashboard',
95 'Dashboard',
96 'manage_options',
97 'formfacade_dashboard',
98 'formfacade_dashboard_page'
99 );
100
101 add_submenu_page(
102 'formfacade_home',
103 'Embed Google Forms',
104 'Embed Google Forms',
105 'manage_options',
106 'embed_google_forms',
107 'embed_google_forms_page'
108 );
109 }
110
111 function formfacade_home_page() {
112 wp_enqueue_style('formfacade_fonts', 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Onest:wght@400;500;600;700&display=swap', [], null);
113 wp_enqueue_style('formfacade_styles_bootstrap', plugins_url('assets/css/bootstrap.min.css', __FILE__), [], '1.0.0');
114 wp_enqueue_style('formfacade_styles_custom', plugins_url('assets/css/style.css', __FILE__), [], formfacade_asset_ver('assets/css/style.css'));
115
116 wp_enqueue_script('formfacade_home_script', plugins_url('assets/js/home.js', __FILE__), [], formfacade_asset_ver('assets/js/home.js'), true);
117 wp_enqueue_script('lottie_script', plugins_url('assets/js/lottie.js', __FILE__), [], '5.7.13', true);
118 include(plugin_dir_path(__FILE__) . 'templates/home.php');
119 }
120
121 function formfacade_dashboard_page() {
122 $domain = "https://formfacade.com";
123 $url = $domain . "/login/website.html";
124
125 if ( isset($_GET['redirectURL']) ) {
126 // Sanitize and validate the redirectURL parameter
127 $redirectURL = sanitize_url($_GET['redirectURL']);
128
129 // Ensure redirectURL only contains alphanumeric characters and slashes
130 if (preg_match('/^[a-zA-Z0-9\/\-_.]+$/', $redirectURL)) {
131 // Ensure redirectURL starts with a single slash
132 if (substr($redirectURL, 0, 1) === '/') {
133 $redirectURL = substr($redirectURL, 1);
134 }
135
136 $url = $domain . '/' . $redirectURL;
137 } else {
138 // Handle invalid redirectURL, possibly log an error or redirect to a default URL
139 $url = $domain . "/404.html";
140 }
141 }
142
143 formfacade_fullscreen_iframe_css();
144 ?>
145 <div class="wrap ff-fullscreen">
146 <iframe id="myIframe" src="<?php echo esc_url($url); ?>" title="Formfacade Dashboard">Loading…</iframe>
147 </div>
148 <?php
149 }
150
151 // Function to run on plugin activation
152 function formfacade_plugin_activate() {
153 // Set an option to indicate the plugin was just activated
154 add_option('formfacade_plugin_activated', true);
155 }
156
157 // Register the activation hook
158 register_activation_hook(__FILE__, 'formfacade_plugin_activate');
159
160
161 // Show admin notice and redirect to custom page
162 function formfacade_plugin_redirect() {
163 // Check if the plugin was just activated
164 if (get_option('formfacade_plugin_activated', false)) {
165 // Remove the activation option
166 delete_option('formfacade_plugin_activated');
167
168 // Redirect to custom page
169 wp_safe_redirect(admin_url('admin.php?page=embed_google_forms'));
170 exit;
171 }
172 }
173 add_action('admin_init', 'formfacade_plugin_redirect');
174
175 function get_site_unique_identifier() {
176 $site_url = get_option('siteurl');
177 $unique_identifier = md5($site_url);
178 return $site_url;
179 }
180
181 function embed_google_forms_page() {
182 $pages = get_pages();
183 foreach ($pages as &$page) {
184 $page->edit_url = get_edit_post_link($page->ID);
185 }
186
187 $domain = "https://formfacade.com";
188 $url = $domain . "/wordpress/onboard.html";
189 $preview_url = '';
190 $admin_url = admin_url('admin.php?page=formfacade_dashboard');
191
192 $user = wp_get_current_user();
193 $user_details = array(
194 'email' => $user->user_email,
195 'displayName' => $user->display_name,
196 'siteName' => get_bloginfo('name'),
197 'siteDesc' => get_bloginfo('description'),
198 'siteURL' => get_site_url(),
199 'homeURL' => get_home_url(),
200 'emailHash' => md5($user->user_email),
201 'siteHash' => md5(get_site_url())
202 );
203
204 // Sanitize and validate the input
205 $page_id = isset($_GET['pageId']) ? intval(sanitize_text_field(wp_unslash($_GET['pageId']))) : 0;
206 $user_id = isset($_GET['userId']) ? sanitize_text_field(wp_unslash($_GET['userId'])) : '';
207 $publish_id = isset($_GET['publishId']) ? sanitize_text_field(wp_unslash($_GET['publishId'])) : '';
208 $page_name = isset($_GET['pageName']) ? sanitize_text_field(wp_unslash($_GET['pageName'])) : '';
209
210 // Validate that the IDs are numeric or alphanumeric as appropriate
211 if ($page_id > 0 && $user_id && $publish_id) {
212 $url = $url . "?pageId=" . esc_attr($page_id) . "&userId=" . esc_attr($user_id) . "&publishId=" . esc_attr($publish_id);
213 $preview_url = get_permalink($page_id) . '?preview=true';
214 emebd_wordpress_script($page_id, $user_id, $publish_id);
215 } else if($page_name){
216 $page_id = formfacade_new_page($page_name, $user_id, $publish_id);
217 $url = $url . "?pageId=" . esc_attr($page_id) . "&userId=" . esc_attr($user_id) . "&publishId=" . esc_attr($publish_id);
218 $preview_url = get_permalink($page_id) . '?preview=true';
219 }
220
221
222 formfacade_fullscreen_iframe_css();
223 ?>
224 <div class="wrap ff-fullscreen">
225 <iframe id="myIframe" src="<?php echo esc_url($url); ?>" title="Formfacade">Loading…</iframe>
226 </div>
227 <?php
228
229 // Register and enqueue the script
230 wp_register_script('formfacade_script', '', [], time(), true);
231 wp_enqueue_script('formfacade_script');
232
233 // Add inline script
234 $inline_script = "
235 document.addEventListener('DOMContentLoaded', function() {
236 var iframe = document.getElementById('myIframe');
237 var postedMessage = false;
238
239 function postMessageToIframe() {
240 var pages = " . wp_json_encode($pages) . ";
241 var preview_url = '" . esc_url($preview_url) . "';
242 var user = " . wp_json_encode($user_details) . ";
243 var data = {pages: pages, previewURL: preview_url, wordpressUser: user };
244 // console.log('Before postinggggggg', data);
245 iframe.contentWindow.postMessage(data, '" . esc_url($domain) . "');
246 postedMessage = true;
247 clearInterval(interval);
248 }
249
250 iframe.addEventListener('load', function() {
251 if (!postedMessage) postMessageToIframe();
252 });
253
254 var interval = setInterval(function() {
255 var iframe = document.getElementById('myIframe');
256 if (!postedMessage && iframe) postMessageToIframe();
257 }, 1000);
258
259 window.addEventListener('message', function(event) {
260 if (event.origin !== '" . esc_url($domain) . "') return; // Verify the origin
261 var formData = event.data; // This will contain the form data sent from the iframe
262 var admin_url = '" . esc_url($admin_url) . "';
263 if (typeof formData !== 'string') return; // only handle string (JSON) messages from the iframe
264 if (formData && formData.indexOf('pageId') > -1) {
265 var data = JSON.parse(formData);
266 var url = window.location.href;
267 var separator = url.indexOf('?') !== -1 ? '&' : '?';
268 url += separator + 'pageId=' + data.pageId + '&userId=' + data.userId + '&publishId=' + data.publishId;
269 window.location.href = url;
270 } else if(formData && formData.indexOf('pageName') > -1) {
271 var data = JSON.parse(formData);
272 var url = window.location.href;
273 var separator = url.indexOf('?') !== -1 ? '&' : '?';
274 url += separator + 'pageName=' + data.pageName + '&userId=' + data.userId + '&publishId=' + data.publishId;
275 window.location.href = url;
276 }
277
278 if (formData && formData.indexOf('redirectURL') > -1) {
279 var data = JSON.parse(formData);
280 url = admin_url + '&redirectURL=' + data.redirectURL;
281 window.location.href = url;
282 }
283 });
284 });
285 ";
286 wp_add_inline_script('formfacade_script', $inline_script);
287 }
288
289 function emebd_wordpress_script($pageId, $userId, $publishId) {
290 $page = get_post($pageId);
291 if ($page) {
292 // Get the current post content
293 $existing_content = $page->post_content;
294 $embedUrl = 'https://formfacade.com/include/' . $userId . '/form/' . $publishId . '/wordpress.js?div=ff-compose';
295
296 if (strpos($existing_content, $publishId) === false) {
297 $block_content = '<!-- wp:html -->';
298 $block_content .= '<!-- Custom HTML block -->';
299
300 wp_register_script('formfacade_embed_script', $embedUrl, [], time(), true);
301 wp_enqueue_script('formfacade_embed_script');
302 $script_tag = wp_get_inline_script_tag('', ['src' => $embedUrl, 'async' => true, 'defer' => true]);
303 $block_content .= '<div id="ff-compose"></div>' . $script_tag;
304
305 $block_content .= '<!-- /Custom HTML block -->';
306 $block_content .= '<!-- /wp:html -->';
307 $updated_content = $existing_content . "\n\n" . $block_content;
308 wp_update_post([ 'ID' => $pageId, 'post_content' => $updated_content, ]);
309 }
310 }
311 }
312
313 function formfacade_new_page($pageName, $userId, $publishId) {
314 $page = array(
315 'post_title' => $pageName,
316 'post_content' => '',
317 'post_status' => 'publish',
318 'post_type' => 'page'
319 );
320
321 $embedUrl = 'https://formfacade.com/include/' . $userId . '/form/' . $publishId . '/wordpress.js?div=ff-compose';
322 $pageId = wp_insert_post($page);
323
324 if ($pageId) {
325 $block_content = '<!-- wp:html -->';
326 $block_content .= '<!-- Custom HTML block -->';
327
328 wp_register_script('formfacade_embed_script', $embedUrl, [], time(), true);
329 wp_enqueue_script('formfacade_embed_script');
330 $script_tag = wp_get_inline_script_tag('', ['src' => $embedUrl, 'async' => true, 'defer' => true]);
331 $block_content .= '<div id="ff-compose"></div>' . $script_tag;
332
333 $block_content .= '<!-- /Custom HTML block -->';
334 $block_content .= '<!-- /wp:html -->';
335 // New page starts empty, so the embed block is the entire content.
336 $updated_content = $block_content;
337 wp_update_post([ 'ID' => $pageId, 'post_content' => $updated_content, ]);
338 }
339
340 return $pageId;
341 }
342
343 class FormFacade
344 {
345 function activate()
346 {
347 }
348
349 function deactivate()
350 {
351 echo 'FormFacade plugin deactivated';
352 }
353
354
355
356 function renderFacade($atts = [])
357 {
358 $id = sanitize_text_field($atts['id']);
359 $appearance = 'wordpress';
360
361 // Check for appearance attributes
362 if (array_key_exists('appearance', $atts)) {
363 $appearance = sanitize_text_field($atts['appearance']);
364 }
365
366 if (array_key_exists('owner', $atts)) {
367 $owner = sanitize_text_field($atts['owner']);
368 $script_url = 'https://formfacade.com/include/' . esc_attr($owner) . '/form/' . esc_attr($id) . '/' . esc_attr($appearance) . '.js?div=ff-' . esc_attr($id);
369 $script_tag = wp_get_inline_script_tag('', ['src' => $script_url, 'async' => true, 'defer' => true]);
370 return '<div id="ff-' . esc_url($id) . '"></div>' . $script_tag;
371 } else if ($id) {
372 $embedUrl = 'https://formfacade.com/forms/d/e/' . esc_attr($id) . '/' . esc_attr($appearance) . '.js?div=ff-' . esc_attr($id);
373 wp_register_script('formfacade_render_script', $embedUrl, [], null, true);
374 wp_enqueue_script('formfacade_render_script');
375 $script_tag = wp_get_inline_script_tag('', ['src' => $embedUrl, 'async' => true, 'defer' => true]);
376 return '<div id="ff-' . esc_url($id) . '"></div>' . $script_tag;
377 } else {
378 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>';
379 }
380 }
381 }
382 $formfacade = new FormFacade();
383 add_shortcode('formfacade', array($formfacade, 'renderFacade'));
384 ?>