| 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.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 |
// 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 |
'Home', |
| 57 |
'Home', |
| 58 |
'manage_options', |
| 59 |
'formfacade_home', |
| 60 |
'formfacade_home_page' |
| 61 |
); |
| 62 |
|
| 63 |
add_submenu_page( |
| 64 |
'formfacade_home', |
| 65 |
'Embed Google Forms', |
| 66 |
'Embed Google Forms', |
| 67 |
'manage_options', |
| 68 |
'embed_google_forms', |
| 69 |
'embed_google_forms_page' |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
function formfacade_home_page() { |
| 74 |
include(plugin_dir_path(__FILE__) . 'templates/home.php'); |
| 75 |
} |
| 76 |
|
| 77 |
// Function to run on plugin activation |
| 78 |
function formfacade_plugin_activate() { |
| 79 |
// Set an option to indicate the plugin was just activated |
| 80 |
add_option('formfacade_plugin_activated', true); |
| 81 |
} |
| 82 |
|
| 83 |
// Register the activation hook |
| 84 |
register_activation_hook(__FILE__, 'formfacade_plugin_activate'); |
| 85 |
|
| 86 |
|
| 87 |
// Show admin notice and redirect to custom page |
| 88 |
function formfacade_plugin_redirect() { |
| 89 |
// Check if the plugin was just activated |
| 90 |
if (get_option('formfacade_plugin_activated', false)) { |
| 91 |
// Remove the activation option |
| 92 |
delete_option('formfacade_plugin_activated'); |
| 93 |
|
| 94 |
// Redirect to custom page |
| 95 |
wp_safe_redirect(admin_url('admin.php?page=embed_google_forms')); |
| 96 |
exit; |
| 97 |
} |
| 98 |
} |
| 99 |
add_action('admin_init', 'formfacade_plugin_redirect'); |
| 100 |
|
| 101 |
function get_site_unique_identifier() { |
| 102 |
$site_url = get_option('siteurl'); |
| 103 |
$unique_identifier = md5($site_url); |
| 104 |
return $site_url; |
| 105 |
} |
| 106 |
|
| 107 |
function embed_google_forms_page() { |
| 108 |
$pages = get_pages(); |
| 109 |
$pages_json = json_encode($pages); |
| 110 |
$domain = "https://formfacade.com"; |
| 111 |
$url = $domain . "/wordpress/onboard.html"; |
| 112 |
$preview_url = ''; |
| 113 |
|
| 114 |
|
| 115 |
// Handle form submission |
| 116 |
if ( isset($_GET['pageId']) && isset($_GET['userId']) && isset($_GET['publishId'])) { |
| 117 |
$page_id = intval($_GET['pageId']); |
| 118 |
$url = $url . "?pageId=" . $page_id . "&userId=" . $_GET['userId'] . "&publishId=" . $_GET['publishId']; |
| 119 |
$preview_url = get_permalink($page_id) . '?preview=true'; |
| 120 |
emebd_wordpress_script($page_id, $_GET['userId'], $_GET['publishId']); |
| 121 |
} |
| 122 |
|
| 123 |
?> |
| 124 |
<div class="wrap" style="height: 100vh;"> |
| 125 |
<iframe id="myIframe" src="<?php echo $url; ?>" width="100%" height="100%" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe> |
| 126 |
</div> |
| 127 |
<script type="text/javascript"> |
| 128 |
document.addEventListener('DOMContentLoaded', function() { |
| 129 |
var iframe = document.getElementById('myIframe'); |
| 130 |
iframe.addEventListener('load', function() { |
| 131 |
var pages = <?php echo $pages_json; ?>; |
| 132 |
var preview_url = "<?php echo $preview_url; ?>"; |
| 133 |
var data = {pages: pages, previewURL: preview_url} |
| 134 |
iframe.contentWindow.postMessage(data, "<?php echo $domain; ?>"); |
| 135 |
}); |
| 136 |
}); |
| 137 |
|
| 138 |
window.addEventListener('message', function(event) { |
| 139 |
if (event.origin !== "<?php echo $domain; ?>") return; // Verify the origin |
| 140 |
var formData = event.data; // This will contain the form data sent from the iframe |
| 141 |
console.log('Form data received 1111:', formData); |
| 142 |
|
| 143 |
if(formData && formData.indexOf('pageId') > -1) { |
| 144 |
var data = JSON.parse(formData) |
| 145 |
var url = window.location.href; |
| 146 |
var separator = url.indexOf('?') !== -1 ? '&' : '?'; |
| 147 |
url += separator + `pageId=${data.pageId}&userId=${data.userId}&publishId=${data.publishId}`; |
| 148 |
window.location.href = url; |
| 149 |
} |
| 150 |
}); |
| 151 |
</script> |
| 152 |
<?php |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
function emebd_wordpress_script($pageId, $userId, $publishId) { |
| 157 |
$page = get_post($pageId); |
| 158 |
if ($page) { |
| 159 |
// Get the current post content |
| 160 |
$existing_content = $page->post_content; |
| 161 |
|
| 162 |
if (strpos($existing_content, $publishId) === false) { |
| 163 |
$block_content = '<!-- wp:html -->'; |
| 164 |
$block_content .= '<!-- Custom HTML block -->'; |
| 165 |
$block_content .= '<div id="ff-compose"></div>'; |
| 166 |
$block_content .= '<script async defer src="https://formfacade.com/include/' . $userId . '/form/' . $publishId . '/wordpress.js?div=ff-compose"></script>'; |
| 167 |
$block_content .= '<!-- /Custom HTML block -->'; |
| 168 |
$block_content .= '<!-- /wp:html -->'; |
| 169 |
$updated_content = $existing_content . "\n\n" . $block_content; |
| 170 |
wp_update_post([ 'ID' => $pageId, 'post_content' => $updated_content, ]); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
class FormFacade |
| 177 |
{ |
| 178 |
function activate() |
| 179 |
{ |
| 180 |
} |
| 181 |
|
| 182 |
function deactivate() |
| 183 |
{ |
| 184 |
echo 'FormFacade plugin deactivated'; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
function renderFacade($atts = []) |
| 190 |
{ |
| 191 |
$id = sanitize_text_field($atts['id']); |
| 192 |
$appearance = 'wordpress'; |
| 193 |
|
| 194 |
// Check for appearance attributes |
| 195 |
if (array_key_exists('appearance', $atts)) { |
| 196 |
$appearance = sanitize_text_field($atts['appearance']); |
| 197 |
} |
| 198 |
|
| 199 |
// Check for owner attributes |
| 200 |
if (array_key_exists('owner', $atts)) { |
| 201 |
$owner = sanitize_text_field($atts['owner']); |
| 202 |
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>'; |
| 203 |
} else if ($id) { |
| 204 |
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>'; |
| 205 |
} else { |
| 206 |
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>'; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
$formfacade = new FormFacade(); |
| 211 |
add_shortcode('formfacade', array($formfacade, 'renderFacade')); |
| 212 |
?> |