From 69ec05cd6a328ef6fcefcfa601fff7f8bcc20178 Mon Sep 17 00:00:00 2001 From: Michael Thomas Date: Mon, 14 Feb 2022 16:55:39 -0500 Subject: [PATCH] Bring back support for legacy shortcodes --- api.php | 51 ++++++++++++++++++++++++++++++++++++++++++ cognito-forms.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 api.php diff --git a/api.php b/api.php new file mode 100644 index 0000000..f347647 --- /dev/null +++ b/api.php @@ -0,0 +1,51 @@ + + EOF; + } + } +} + +?> diff --git a/cognito-forms.php b/cognito-forms.php index 03befd1..908fecf 100644 --- a/cognito-forms.php +++ b/cognito-forms.php @@ -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';