PluginProbe
Cognito Forms / 2.0.11
Cognito Forms v2.0.11
2.0.12 2.0.11 trunk 1.0 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
cognito-forms / cognito-forms.php

cognito-forms.php in Cognito Forms 2.0.11, at cognito-forms.php

214 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.11
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 (
114 isset( $_GET['cog_clear'], $_GET['_wpnonce'] ) &&
115 '1' === sanitize_text_field( wp_unslash( $_GET['cog_clear'] ) ) &&
116 current_user_can( 'manage_options' ) &&
117 wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'cog_clear_key' )
118 ) {
119 delete_option( 'cognito_public_key' );
120 }
121
122 // Initialize TinyMCE Plugin
123 $this->tinymce_init();
124 }
125
126 // Initialize block
127 public function block_init() {
128 $asset_file = include( plugin_dir_path( __FILE__ ) . 'dist/index.asset.php' );
129
130 // Register global block styles
131 wp_register_style(
132 'cognito-block-global-css', // Handle.
133 plugins_url( 'dist/style-main.css', __FILE__ ), // Public CSS
134 is_admin() ? array( 'wp-editor' ) : null, // Dependency to include the CSS after it.
135 $asset_file['version']
136 );
137
138 // Register block editor script for backend
139 wp_register_script(
140 'cognito-block-editor-js',
141 plugins_url( 'dist/index.js', __FILE__ ),
142 $asset_file['dependencies'],
143 $asset_file['version']
144 );
145
146 wp_add_inline_script(
147 'cognito-block-editor-js',
148 'window.COGNITO_BASEURL = "' . CognitoAPI::$formsBase . '";',
149 'before'
150 );
151
152 // Register block editor styles for backend.
153 wp_register_style(
154 'cognito-block-editor-css', // Handle.
155 plugins_url( 'dist/main.css', __FILE__ ), // Block editor CSS.
156 array( 'wp-edit-blocks' ), // Dependency to include the CSS after it.
157 $asset_file['version']
158 );
159
160 register_block_type(
161 'cognito-forms/cognito-embed', array(
162 // Enqueue global block styles on both frontend and backend
163 'style' => 'cognito-block-global-css',
164 // Enqueue block js in the editor only
165 'editor_script' => 'cognito-block-editor-js',
166 // Enqueue editor block styles in the editor only
167 'editor_style' => 'cognito-block-editor-css'
168 )
169 );
170 }
171
172 // Initialize classic editor (TinyMCE)
173 public function tinymce_init() {
174 if ( get_user_option( 'rich_editing' ) == 'true' ) {
175 add_filter( 'mce_buttons', array( $this, 'tinymce_buttons' ) );
176 add_filter( 'mce_external_plugins', array( $this, 'tinymce_external_plugins' ) );
177 }
178 }
179
180 // Set up TinyMCE buttons
181 public function tinymce_buttons( $buttons ) {
182 array_push($buttons, '|', 'cognito');
183 return $buttons;
184 }
185
186 // Set up TinyMCE plug-in
187 public function tinymce_external_plugins( $plugin_array ) {
188 $plugin_array['cognito_mce_plugin'] = plugins_url( '/tinymce/plugin.js', __FILE__ );
189 return $plugin_array;
190 }
191
192 public function wp_ajax_cognito_tinymce_dialog() {
193 include 'tinymce/dialog.php';
194 wp_die();
195 }
196
197 // Called when a 'CognitoForms' shortcode is encountered, renders form embed script
198 public function render_cognito_shortcode( $atts, $content = null, $code = '' ) {
199 // Default to key setting, unless overridden in shortcode (allows for modules from multiple orgs)
200 $key = empty( $atts['key'] ) ? get_option( 'cognito_public_key' ) : $atts['key'];
201 if ( empty( $atts['id'] ) || empty( $key ) ) return '';
202
203 return CognitoAPI::get_form_embed_script( $key, $atts['id'] );
204 }
205
206 // Add support for oEmbed using the generic Gutenberg Embed block
207 public function oembed_init() {
208 wp_oembed_add_provider( '#https?://(.*\.)?cognitoforms\.com/.*#i', CognitoAPI::$formsBase . '/f/oembed/', true );
209 }
210 }
211
212 new CognitoFormsPlugin;
213 }
214