2021-06-16 23:05:18 -04:00
< ? 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
*/
2022-02-02 09:41:34 -05:00
/**
2022-02-14 16:54:46 -05:00
* Plugin Name : Cognito Forms
* Plugin URI : http :// wordpress . org / plugins / cognito - forms /
* Description : Cognito Forms is a free online form builder that integrates seamlessly with WordPress . Create contact forms , registrations forms , surveys , and more !
* Version : 2.0 . 0
* Author : Cognito Apps
* Author URI : https :// www . cognitoforms . com
* License : GPL v2 or later
2022-02-02 09:41:34 -05:00
* License URI : https :// www . gnu . org / licenses / gpl - 2.0 . html
*/
2021-06-16 23:05:18 -04:00
// Exit if accessed directly.
2022-02-17 21:35:02 -05:00
if ( ! defined ( 'ABSPATH' ) ) {
2021-06-16 23:05:18 -04:00
exit ;
}
/**
* The Plugin
*/
2022-02-17 21:35:02 -05:00
if ( ! class_exists ( 'CognitoFormsPlugin' ) ) {
require_once dirname ( __FILE__ ) . '/api.php' ;
2022-02-14 16:55:39 -05:00
2021-06-16 23:05:18 -04:00
class CognitoFormsPlugin {
// Initialization actions
private static $actions = array (
2021-12-23 15:50:33 -05:00
'admin_init' ,
2022-03-30 11:03:12 -04:00
'init' ,
'wp_ajax_cognito_tinymce_dialog'
2021-06-16 23:05:18 -04:00
);
2022-02-14 16:55:39 -05:00
// Supported shortcodes
private static $shortcodes = array (
2022-02-17 21:35:02 -05:00
'CognitoForms' => 'render_cognito_shortcode' ,
'cognitoforms' => 'render_cognito_shortcode'
2022-02-14 16:55:39 -05:00
);
2021-06-16 23:05:18 -04:00
// Registers plug-in actions
2022-02-17 21:35:02 -05:00
private function add_actions ( $actions ) {
foreach ( $actions as $action )
add_action ( $action , array ( $this , $action ) );
2022-02-14 16:54:46 -05:00
}
2021-06-16 23:05:18 -04:00
2022-02-14 16:55:39 -05:00
// Registers shortcodes
2022-02-17 21:35:02 -05:00
private function add_shortcodes ( $shortcodes ) {
foreach ( $shortcodes as $tag => $func )
add_shortcode ( $tag , array ( $this , $func ) );
2022-02-14 16:55:39 -05:00
}
// 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 );
}
}
}
2021-06-16 23:05:18 -04:00
// Entrypoint
public function __construct () {
2022-02-17 21:35:02 -05:00
$this -> add_actions ( self :: $actions );
$this -> add_shortcodes ( self :: $shortcodes );
2021-06-16 23:05:18 -04:00
}
2022-01-22 22:17:27 -05:00
public function init () {
// Initialize Gutenberg Block
$this -> block_init ();
2022-01-28 10:05:53 -05:00
// Add support for oEmbed
$this -> oembed_init ();
2022-01-22 22:17:27 -05:00
}
2021-12-23 15:50:33 -05:00
// Initialize plug-in
public function admin_init () {
2022-02-14 16:55:39 -05:00
if ( ! current_user_can ( 'edit_posts' ) && ! current_user_can ( 'edit_pages' ) )
return ;
2022-02-17 21:35:02 -05:00
add_option ( 'cognito_public_key' );
2022-02-14 16:55:39 -05:00
// 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' ) {
2022-02-17 21:35:02 -05:00
delete_option ( 'cognito_public_key' );
2022-02-14 16:55:39 -05:00
}
2021-12-23 15:50:33 -05:00
2022-01-22 22:17:27 -05:00
// Initialize TinyMCE Plugin
$this -> tinymce_init ();
2022-01-13 18:26:04 -05:00
}
// Initialize block
public function block_init () {
2022-02-17 21:35:02 -05:00
$asset_file = include ( plugin_dir_path ( __FILE__ ) . 'dist/index.asset.php' );
2022-01-13 18:26:04 -05:00
// Register global block styles
wp_register_style (
'cognito-block-global-css' , // Handle.
2022-02-14 16:54:46 -05:00
plugins_url ( 'dist/style-main.css' , __FILE__ ), // Public CSS
2022-01-13 18:26:04 -05:00
is_admin () ? array ( 'wp-editor' ) : null , // Dependency to include the CSS after it.
2022-02-14 16:54:46 -05:00
$asset_file [ 'version' ]
2022-01-13 18:26:04 -05:00
);
// Register block editor script for backend
wp_register_script (
'cognito-block-editor-js' ,
plugins_url ( 'dist/index.js' , __FILE__ ),
$asset_file [ 'dependencies' ],
$asset_file [ 'version' ]
);
2022-02-18 13:48:59 -05:00
wp_add_inline_script (
'cognito-block-editor-js' ,
'window.COGNITO_BASEURL = "' . CognitoAPI :: $formsBase . '";' ,
'before'
);
2022-01-13 18:26:04 -05:00
// Register block editor styles for backend.
wp_register_style (
'cognito-block-editor-css' , // Handle.
2022-02-10 18:01:32 -05:00
plugins_url ( 'dist/main.css' , __FILE__ ), // Block editor CSS.
2022-01-13 18:26:04 -05:00
array ( 'wp-edit-blocks' ), // Dependency to include the CSS after it.
2022-02-14 16:54:46 -05:00
$asset_file [ 'version' ]
2022-01-13 18:26:04 -05:00
);
register_block_type (
'cognito-forms/cognito-embed' , array (
// Enqueue global block styles on both frontend and backend
'style' => 'cognito-block-global-css' ,
// Enqueue block js in the editor only
'editor_script' => 'cognito-block-editor-js' ,
// Enqueue editor block styles in the editor only
'editor_style' => 'cognito-block-editor-css'
2022-02-14 16:54:46 -05:00
)
);
2022-01-13 18:26:04 -05:00
}
2022-01-22 22:17:27 -05:00
// Initialize classic editor (TinyMCE)
public function tinymce_init () {
2022-02-17 21:35:02 -05:00
if ( get_user_option ( 'rich_editing' ) == 'true' ) {
2022-03-20 23:01:10 -04:00
add_filter ( 'mce_buttons' , array ( $this , 'tinymce_buttons' ) );
add_filter ( 'mce_external_plugins' , array ( $this , 'tinymce_external_plugins' ) );
2022-01-22 22:17:27 -05:00
}
}
// Set up TinyMCE buttons
2022-02-17 21:35:02 -05:00
public function tinymce_buttons ( $buttons ) {
2021-12-23 15:50:33 -05:00
array_push ( $buttons , '|' , 'cognito' );
return $buttons ;
}
2022-02-02 09:41:34 -05:00
// Set up TinyMCE plug-in
2022-02-17 21:35:02 -05:00
public function tinymce_external_plugins ( $plugin_array ) {
2022-01-22 22:17:27 -05:00
$plugin_array [ 'cognito_mce_plugin' ] = plugins_url ( '/tinymce/plugin.js' , __FILE__ );
return $plugin_array ;
2021-12-23 15:50:33 -05:00
}
2022-03-30 11:03:12 -04:00
public function wp_ajax_cognito_tinymce_dialog () {
include 'tinymce/dialog.php' ;
wp_die ();
}
2022-02-14 16:55:39 -05:00
// Called when a 'CognitoForms' shortcode is encountered, renders form embed script
2022-02-17 21:35:02 -05:00
public function render_cognito_shortcode ( $atts , $content = null , $code = '' ) {
2022-02-14 16:55:39 -05:00
// Default to key setting, unless overridden in shortcode (allows for modules from multiple orgs)
2022-02-17 21:35:02 -05:00
$key = empty ( $atts [ 'key' ] ) ? get_option ( 'cognito_public_key' ) : $atts [ 'key' ];
if ( empty ( $atts [ 'id' ] ) || empty ( $key ) ) return '' ;
2022-02-14 16:55:39 -05:00
2022-02-17 21:35:02 -05:00
return CognitoAPI :: get_form_embed_script ( $key , $atts [ 'id' ] );
2022-02-14 16:55:39 -05:00
}
2022-01-28 10:05:53 -05:00
// Add support for oEmbed using the generic Gutenberg Embed block
public function oembed_init () {
wp_oembed_add_provider ( '#https?://(www\.)?cognitoforms\.com/.*#i' , 'https://www.cognitoforms.com/f/oembed/' , true );
}
2021-06-16 23:05:18 -04:00
}
2022-01-22 22:17:27 -05:00
2021-06-16 23:05:18 -04:00
new CognitoFormsPlugin ;
2021-12-23 15:50:33 -05:00
}