| 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 |
/** |
| 20 |
* Plugin Name: Cognito Forms |
| 21 |
* Plugin URI: http://wordpress.org/plugins/cognito-forms/ |
| 22 |
* Description: Cognito Forms is a free online form builder that integrates seamlessly with WordPress. Create contact forms, registrations forms, surveys, and more! |
| 23 |
* Version: 2.0.4 |
| 24 |
* Author: Cognito Apps |
| 25 |
* Author URI: https://www.cognitoforms.com |
| 26 |
* License: GPL v2 or later |
| 27 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 28 |
*/ |
| 29 |
|
| 30 |
// Exit if accessed directly. |
| 31 |
if ( !defined( 'ABSPATH' ) ) { |
| 32 |
exit; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* The Plugin |
| 37 |
*/ |
| 38 |
|
| 39 |
if ( !class_exists( 'CognitoFormsPlugin' ) ) { |
| 40 |
require_once dirname( __FILE__ ) . '/api.php'; |
| 41 |
|
| 42 |
class CognitoFormsPlugin { |
| 43 |
// Initialization actions |
| 44 |
private static $actions = array( |
| 45 |
'admin_init', |
| 46 |
'init', |
| 47 |
'wp_ajax_cognito_tinymce_dialog' |
| 48 |
); |
| 49 |
|
| 50 |
// Supported shortcodes |
| 51 |
private static $shortcodes = array( |
| 52 |
'CognitoForms' => 'render_cognito_shortcode', |
| 53 |
'cognitoforms' => 'render_cognito_shortcode' |
| 54 |
); |
| 55 |
|
| 56 |
// Registers plug-in actions |
| 57 |
private function add_actions( $actions ) { |
| 58 |
foreach ( $actions as $action ) |
| 59 |
add_action( $action, array( $this, $action ) ); |
| 60 |
} |
| 61 |
|
| 62 |
// Registers shortcodes |
| 63 |
private function add_shortcodes( $shortcodes ) { |
| 64 |
foreach ( $shortcodes as $tag => $func ) |
| 65 |
add_shortcode( $tag, array( $this, $func ) ); |
| 66 |
} |
| 67 |
|
| 68 |
// Checks if an option exists in the database |
| 69 |
private function option_exists( $option_name, $site_wide = false ) { |
| 70 |
global $wpdb; |
| 71 |
return $wpdb->query( $wpdb->prepare( "SELECT * FROM ". ($site_wide ? $wpdb->base_prefix : $wpdb->prefix). "options WHERE option_name ='%s' LIMIT 1", $option_name ) ); |
| 72 |
} |
| 73 |
|
| 74 |
// Removes a list of options if they exist |
| 75 |
private function remove_options( $options ) { |
| 76 |
foreach ($options as $option) { |
| 77 |
if ( $this->option_exists( $option ) ) { |
| 78 |
delete_option( $option ); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// Entrypoint |
| 84 |
public function __construct() { |
| 85 |
$this->add_actions( self::$actions ); |
| 86 |
$this->add_shortcodes( self::$shortcodes ); |
| 87 |
} |
| 88 |
|
| 89 |
public function init() { |
| 90 |
// Initialize Gutenberg Block |
| 91 |
$this->block_init(); |
| 92 |
// Add support for oEmbed |
| 93 |
$this->oembed_init(); |
| 94 |
} |
| 95 |
|
| 96 |
// Initialize plug-in |
| 97 |
public function admin_init() { |
| 98 |
if( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) |
| 99 |
return; |
| 100 |
|
| 101 |
add_option( 'cognito_public_key' ); |
| 102 |
|
| 103 |
add_option( 'cognito_dev_environment' ); |
| 104 |
|
| 105 |
// Remove old API keys from the database |
| 106 |
$this->remove_options( array( |
| 107 |
'cognito_api_key', |
| 108 |
'cognito_admin_key', |
| 109 |
'cognito_organization' |
| 110 |
) ); |
| 111 |
|
| 112 |
// If the flag to delete options was passed in, delete them |
| 113 |
if ( isset( $_GET['cog_clear'] ) && $_GET['cog_clear'] == '1' ) { |
| 114 |
delete_option( 'cognito_public_key' ); |
| 115 |
} |
| 116 |
|
| 117 |
// Initialize TinyMCE Plugin |
| 118 |
$this->tinymce_init(); |
| 119 |
} |
| 120 |
|
| 121 |
// Initialize block |
| 122 |
public function block_init() { |
| 123 |
$asset_file = include( plugin_dir_path( __FILE__ ) . 'dist/index.asset.php' ); |
| 124 |
|
| 125 |
// Register global block styles |
| 126 |
wp_register_style( |
| 127 |
'cognito-block-global-css', // Handle. |
| 128 |
plugins_url( 'dist/style-main.css', __FILE__ ), // Public CSS |
| 129 |
is_admin() ? array( 'wp-editor' ) : null, // Dependency to include the CSS after it. |
| 130 |
$asset_file['version'] |
| 131 |
); |
| 132 |
|
| 133 |
// Register block editor script for backend |
| 134 |
wp_register_script( |
| 135 |
'cognito-block-editor-js', |
| 136 |
plugins_url( 'dist/index.js', __FILE__ ), |
| 137 |
$asset_file['dependencies'], |
| 138 |
$asset_file['version'] |
| 139 |
); |
| 140 |
|
| 141 |
wp_add_inline_script( |
| 142 |
'cognito-block-editor-js', |
| 143 |
'window.COGNITO_BASEURL = "' . CognitoAPI::$formsBase . '";', |
| 144 |
'before' |
| 145 |
); |
| 146 |
|
| 147 |
// Register block editor styles for backend. |
| 148 |
wp_register_style( |
| 149 |
'cognito-block-editor-css', // Handle. |
| 150 |
plugins_url( 'dist/main.css', __FILE__ ), // Block editor CSS. |
| 151 |
array( 'wp-edit-blocks' ), // Dependency to include the CSS after it. |
| 152 |
$asset_file['version'] |
| 153 |
); |
| 154 |
|
| 155 |
register_block_type( |
| 156 |
'cognito-forms/cognito-embed', array( |
| 157 |
// Enqueue global block styles on both frontend and backend |
| 158 |
'style' => 'cognito-block-global-css', |
| 159 |
// Enqueue block js in the editor only |
| 160 |
'editor_script' => 'cognito-block-editor-js', |
| 161 |
// Enqueue editor block styles in the editor only |
| 162 |
'editor_style' => 'cognito-block-editor-css' |
| 163 |
) |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
// Initialize classic editor (TinyMCE) |
| 168 |
public function tinymce_init() { |
| 169 |
if ( get_user_option( 'rich_editing' ) == 'true' ) { |
| 170 |
add_filter( 'mce_buttons', array( $this, 'tinymce_buttons' ) ); |
| 171 |
add_filter( 'mce_external_plugins', array( $this, 'tinymce_external_plugins' ) ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// Set up TinyMCE buttons |
| 176 |
public function tinymce_buttons( $buttons ) { |
| 177 |
array_push($buttons, '|', 'cognito'); |
| 178 |
return $buttons; |
| 179 |
} |
| 180 |
|
| 181 |
// Set up TinyMCE plug-in |
| 182 |
public function tinymce_external_plugins( $plugin_array ) { |
| 183 |
$plugin_array['cognito_mce_plugin'] = plugins_url( '/tinymce/plugin.js', __FILE__ ); |
| 184 |
return $plugin_array; |
| 185 |
} |
| 186 |
|
| 187 |
public function wp_ajax_cognito_tinymce_dialog() { |
| 188 |
include 'tinymce/dialog.php'; |
| 189 |
wp_die(); |
| 190 |
} |
| 191 |
|
| 192 |
// Called when a 'CognitoForms' shortcode is encountered, renders form embed script |
| 193 |
public function render_cognito_shortcode( $atts, $content = null, $code = '' ) { |
| 194 |
// Default to key setting, unless overridden in shortcode (allows for modules from multiple orgs) |
| 195 |
$key = empty( $atts['key'] ) ? get_option( 'cognito_public_key' ) : $atts['key']; |
| 196 |
if ( empty( $atts['id'] ) || empty( $key ) ) return ''; |
| 197 |
|
| 198 |
return CognitoAPI::get_form_embed_script( $key, $atts['id'] ); |
| 199 |
} |
| 200 |
|
| 201 |
// Add support for oEmbed using the generic Gutenberg Embed block |
| 202 |
public function oembed_init() { |
| 203 |
wp_oembed_add_provider( '#https?://(.*\.)?cognitoforms\.com/.*#i', CognitoAPI::$formsBase . '/f/oembed/', true ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
new CognitoFormsPlugin; |
| 208 |
} |
| 209 |
|