| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cognito Forms WordPress Plugin. |
| 4 |
* |
| 5 |
* The Cognito Forms WordPress Plugin is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License, version 2, as |
| 7 |
* published by the Free Software Foundation. |
| 8 |
* |
| 9 |
* The Cognito Forms WordPress Plugin is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
// Cognito API access |
| 20 |
if ( !class_exists('CognitoAPI') ) { |
| 21 |
class CognitoAPI { |
| 22 |
public static $formsBase; |
| 23 |
|
| 24 |
public static function __constructStatic() { |
| 25 |
self::$formsBase = ( get_option( 'cognito_dev_environment' ) !== false && !empty( get_option( 'cognito_dev_environment' ) ) ) |
| 26 |
? get_option( 'cognito_dev_environment' ) |
| 27 |
: 'https://www.cognitoforms.com'; |
| 28 |
} |
| 29 |
|
| 30 |
// Convert MS GUID to Short GUID |
| 31 |
private static function guid_to_short_guid($guid) { |
| 32 |
$guid_byte_order = [ 3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15 ]; |
| 33 |
$guid = preg_replace( "/[^a-zA-Z0-9]+/", "", $guid ); |
| 34 |
$hex = ""; |
| 35 |
for ( $i = 0; $i < 16; $i++ ) |
| 36 |
$hex .= substr( $guid, 2 * $guid_byte_order[$i], 2 ); |
| 37 |
$bin = hex2bin( $hex ); |
| 38 |
$encoded = base64_encode( $bin ); |
| 39 |
$encoded = str_replace( "/", "_", $encoded ); |
| 40 |
$encoded = str_replace( "+", "-", $encoded ); |
| 41 |
$encoded = substr ( $encoded, 0, 22 ); |
| 42 |
return $encoded; |
| 43 |
} |
| 44 |
|
| 45 |
// Builds form embed script |
| 46 |
public static function get_form_embed_script( $public_key, $formId ) { |
| 47 |
$base = self::$formsBase; |
| 48 |
$public_short_guid = esc_attr(self::guid_to_short_guid( $public_key )); |
| 49 |
$formId = esc_attr($formId); |
| 50 |
|
| 51 |
return <<< EOF |
| 52 |
<script src="{$base}/f/seamless.js" data-key="{$public_short_guid}" data-form="{$formId}"></script> |
| 53 |
EOF; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
CognitoAPI::__constructStatic(); |
| 58 |
} |
| 59 |
|
| 60 |
?> |
| 61 |
|