PluginProbe
FormFacade – Embed Google Forms in your website / 1.3.2
FormFacade – Embed Google Forms in your website v1.3.2
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 1.3.2, at formfacade.php

263 lines 9.5 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: 1.3.2
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 include(plugin_dir_path(__FILE__) . 'templates/home.php');
84 }
85
86 function formfacade_dashboard_page() {
87 $domain = "https://formfacade.com";
88 $url = $domain . "/login/website.html";
89
90 if ( isset($_GET['redirectURL']) ) {
91 $url = $domain . $_GET['redirectURL'];
92 }
93
94 ?>
95 <div class="wrap" style="height: 100vh;">
96 <iframe id="myIframe" src="<?php echo $url; ?>" width="100%" height="100%" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>
97 </div>
98 <?php
99 }
100
101 // Function to run on plugin activation
102 function formfacade_plugin_activate() {
103 // Set an option to indicate the plugin was just activated
104 add_option('formfacade_plugin_activated', true);
105 }
106
107 // Register the activation hook
108 register_activation_hook(__FILE__, 'formfacade_plugin_activate');
109
110
111 // Show admin notice and redirect to custom page
112 function formfacade_plugin_redirect() {
113 // Check if the plugin was just activated
114 if (get_option('formfacade_plugin_activated', false)) {
115 // Remove the activation option
116 delete_option('formfacade_plugin_activated');
117
118 // Redirect to custom page
119 wp_safe_redirect(admin_url('admin.php?page=embed_google_forms'));
120 exit;
121 }
122 }
123 add_action('admin_init', 'formfacade_plugin_redirect');
124
125 function get_site_unique_identifier() {
126 $site_url = get_option('siteurl');
127 $unique_identifier = md5($site_url);
128 return $site_url;
129 }
130
131 function embed_google_forms_page() {
132 $pages = get_pages();
133 foreach ( $pages as &$page ) {
134 $page->edit_url = get_edit_post_link( $page->ID );
135 }
136
137 $pages_json = json_encode($pages);
138 $domain = "https://formfacade.com";
139 $url = $domain . "/wordpress/onboard.html";
140 $preview_url = '';
141
142 $admin_url = admin_url('admin.php?page=formfacade_dashboard');
143
144
145 $user = wp_get_current_user();
146 $user_details = array(
147 'email' => $user->user_email,
148 'displayName' => $user->display_name,
149 'siteName' => get_bloginfo('name'),
150 'siteDesc' => get_bloginfo('description'),
151 'siteURL' => get_site_url(),
152 'homeURL' => get_home_url(),
153 'emailHash' => md5($user->user_email),
154 'siteHash' => md5(get_site_url())
155 );
156 $user_json = json_encode($user_details);
157
158 // Handle form submission
159 if ( isset($_GET['pageId']) && isset($_GET['userId']) && isset($_GET['publishId'])) {
160 $page_id = intval($_GET['pageId']);
161 $url = $url . "?pageId=" . $page_id . "&userId=" . $_GET['userId'] . "&publishId=" . $_GET['publishId'];
162 $preview_url = get_permalink($page_id) . '?preview=true';
163 emebd_wordpress_script($page_id, $_GET['userId'], $_GET['publishId']);
164 }
165
166 ?>
167 <div class="wrap" style="height: 100vh;">
168 <iframe id="myIframe" src="<?php echo $url; ?>" width="100%" height="100%" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>
169 </div>
170 <script type="text/javascript">
171 document.addEventListener('DOMContentLoaded', function() {
172 var iframe = document.getElementById('myIframe');
173 iframe.addEventListener('load', function() {
174 var pages = <?php echo $pages_json; ?>;
175 var preview_url = "<?php echo $preview_url; ?>";
176 var user = <?php echo $user_json; ?>;
177
178 var data = { pages: pages, previewURL: preview_url, wordpressUser: user };
179 iframe.contentWindow.postMessage(data, "<?php echo $domain; ?>");
180 });
181 });
182
183 window.addEventListener('message', function(event) {
184 if (event.origin !== "<?php echo $domain; ?>") return; // Verify the origin
185 var formData = event.data; // This will contain the form data sent from the iframe
186 console.log('Form data received 1111:', formData);
187 var admin_url = "<?php echo $admin_url; ?>";
188 if(formData && formData.indexOf('pageId') > -1) {
189 var data = JSON.parse(formData)
190 var url = window.location.href;
191 var separator = url.indexOf('?') !== -1 ? '&' : '?';
192 url += separator + `pageId=${data.pageId}&userId=${data.userId}&publishId=${data.publishId}`;
193 window.location.href = url;
194 }
195
196 if(formData && formData.indexOf('redirectURL') > -1) {
197 var data = JSON.parse(formData);
198 url = admin_url + `&redirectURL=${data.redirectURL}`;
199 window.location.href = url;
200 }
201 });
202 </script>
203 <?php
204
205 }
206
207 function emebd_wordpress_script($pageId, $userId, $publishId) {
208 $page = get_post($pageId);
209 if ($page) {
210 // Get the current post content
211 $existing_content = $page->post_content;
212
213 if (strpos($existing_content, $publishId) === false) {
214 $block_content = '<!-- wp:html -->';
215 $block_content .= '<!-- Custom HTML block -->';
216 $block_content .= '<div id="ff-compose"></div>';
217 $block_content .= '<script async defer src="https://formfacade.com/include/' . $userId . '/form/' . $publishId . '/wordpress.js?div=ff-compose"></script>';
218 $block_content .= '<!-- /Custom HTML block -->';
219 $block_content .= '<!-- /wp:html -->';
220 $updated_content = $existing_content . "\n\n" . $block_content;
221 wp_update_post([ 'ID' => $pageId, 'post_content' => $updated_content, ]);
222 }
223 }
224 }
225
226
227 class FormFacade
228 {
229 function activate()
230 {
231 }
232
233 function deactivate()
234 {
235 echo 'FormFacade plugin deactivated';
236 }
237
238
239
240 function renderFacade($atts = [])
241 {
242 $id = sanitize_text_field($atts['id']);
243 $appearance = 'wordpress';
244
245 // Check for appearance attributes
246 if (array_key_exists('appearance', $atts)) {
247 $appearance = sanitize_text_field($atts['appearance']);
248 }
249
250 // Check for owner attributes
251 if (array_key_exists('owner', $atts)) {
252 $owner = sanitize_text_field($atts['owner']);
253 return '<div id="ff-' . esc_attr($id) . '"></div><script async defer src="https://formfacade.com/include/' . esc_attr($owner) . '/form/' . esc_attr($id) . '/' . esc_attr($appearance) . '.js?div=ff-' . esc_attr($id) . '"></script>';
254 } else if ($id) {
255 return '<div id="ff-' . esc_attr($id) . '"></div><script async defer src="https://formfacade.com/forms/d/e/' . esc_attr($id) . '/' . esc_attr($appearance) . '.js?div=ff-' . esc_attr($id) . '"></script>';
256 } else {
257 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>';
258 }
259 }
260 }
261 $formfacade = new FormFacade();
262 add_shortcode('formfacade', array($formfacade, 'renderFacade'));
263 ?>