Bring back support for legacy shortcodes

This commit is contained in:
Michael Thomas 2022-02-14 16:55:39 -05:00
parent ff1f452e61
commit 69ec05cd6a
2 changed files with 106 additions and 1 deletions

51
api.php Normal file
View File

@ -0,0 +1,51 @@
<?php
/**
* Cognito Forms WordPress Plugin.
*
* The Cognito Forms WordPress Plugin is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* The Cognito Forms WordPress Plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Cognito API access
if ( !class_exists('CognitoAPI') ) {
class CognitoAPI {
public static $formsBase = 'https://www.cognitoforms.com/';
// Convert MS GUID to Short GUID
private static function guid_to_short_guid($guid) {
$guid_byte_order = [ 3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15 ];
$guid = preg_replace( "/[^a-zA-Z0-9]+/", "", $guid );
$hex = "";
for ( $i = 0; $i < 16; $i++ )
$hex .= substr( $guid, 2 * $guid_byte_order[$i], 2 );
$bin = hex2bin( $hex );
$encoded = base64_encode( $bin );
$encoded = str_replace( "/", "_", $encoded );
$encoded = str_replace( "+", "-", $encoded );
$encoded = substr ( $encoded, 0, 22 );
return $encoded;
}
// Builds form embed script
public static function get_form_embed_script( $public_key, $formId ) {
$base = self::$formsBase;
$public_short_guid = self::guid_to_short_guid( $public_key );
return <<< EOF
<script src="{$base}/f/seamless.js" data-key="{$public_short_guid}" data-form="{$formId}"></script>
EOF;
}
}
}
?>

View File

@ -37,6 +37,8 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
if ( !class_exists('CognitoFormsPlugin') ) {
require_once dirname(__FILE__) . '/api.php';
class CognitoFormsPlugin {
// Initialization actions
private static $actions = array(
@ -45,6 +47,12 @@ if ( !class_exists('CognitoFormsPlugin') ) {
'init'
);
// Supported shortcodes
private static $shortcodes = array(
'CognitoForms' => 'render_shortcode',
'cognitoforms' => 'render_shortcode'
);
// Registers plug-in actions
private function addActions($actions) {
foreach($actions as $action)
@ -57,9 +65,31 @@ if ( !class_exists('CognitoFormsPlugin') ) {
add_filter($filter, array($this, $filter));
}
// Registers shortcodes
private function addShortcodes($shortcodes) {
foreach($shortcodes as $tag => $func)
add_shortcode($tag, array($this, $func));
}
// Checks if an option exists in the database
private function option_exists( $option_name, $site_wide = false ) {
global $wpdb;
return $wpdb->query( $wpdb->prepare( "SELECT * FROM ". ($site_wide ? $wpdb->base_prefix : $wpdb->prefix). "options WHERE option_name ='%s' LIMIT 1", $option_name ) );
}
// Removes a list of options if they exist
private function remove_options( $options ) {
foreach ($options as $option) {
if ( $this->option_exists( $option ) ) {
delete_option( $option );
}
}
}
// Entrypoint
public function __construct() {
$this->addActions(self::$actions);
$this->addShortcodes(self::$shortcodes);
}
public function init() {
@ -71,7 +101,22 @@ if ( !class_exists('CognitoFormsPlugin') ) {
// Initialize plug-in
public function admin_init() {
if(!current_user_can('edit_posts') && !current_user_can('edit_pages')) return;
if( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) )
return;
register_setting('cognito_plugin', 'cognito_public_key');
// Remove old API keys from the database
$this->remove_options( array(
'cognito_api_key',
'cognito_admin_key',
'cognito_organization'
) );
// If the flag to delete options was passed in, delete them
if ( isset( $_GET['cog_clear'] ) && $_GET['cog_clear'] == '1' ) {
delete_option('cognito_public_key');
}
// Initialize TinyMCE Plugin
$this->tinymce_init();
@ -147,6 +192,15 @@ if ( !class_exists('CognitoFormsPlugin') ) {
add_submenu_page( 'Cognito', 'Templates', 'Templates', 'manage_options', 'CognitoTemplates', array( $this, 'main_page' ) );
}
// Called when a 'CognitoForms' shortcode is encountered, renders form embed script
public function render_shortcode($atts, $content = null, $code = '') {
// Default to key setting, unless overridden in shortcode (allows for modules from multiple orgs)
$key = empty($atts['key']) ? get_option('cognito_public_key') : $atts['key'];
if (empty($atts['id']) || empty($key)) return '';
return CognitoAPI::get_form_embed_script($key, $atts['id']);
}
// Entrypoint for Cognito Forms access
public function main_page() {
include 'templates/main.php';