cognitoforms-wordpress/plugin.php

167 lines
5.3 KiB
PHP

<?php
/*
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
*/
/**
* 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
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The Plugin
*/
if ( !class_exists('CognitoFormsPlugin') ) {
class CognitoFormsPlugin {
// Initialization actions
private static $actions = array(
'admin_init',
'admin_menu',
'init'
);
// Registers plug-in actions
private function addActions($actions) {
foreach($actions as $action)
add_action($action, array($this, $action));
}
// Registers tinyMCE filters
private function addFilters($filters) {
foreach($filters as $filter)
add_filter($filter, array($this, $filter));
}
// Entrypoint
public function __construct() {
$this->addActions(self::$actions);
}
// Initialize plug-in
public function admin_init() {
if(!current_user_can('edit_posts') && !current_user_can('edit_pages')) return;
register_setting('cognito_plugin', 'cognito_api_key');
register_setting('cognito_plugin', 'cognito_admin_key');
register_setting('cognito_plugin', 'cognito_public_key');
register_setting('cognito_plugin', '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_api_key');
delete_option('cognito_admin_key');
delete_option('cognito_public_key');
delete_option('cognito_organization');
}
// Add tinyMCE plug-in
if(get_user_option('rich_editing') == 'true') {
$this->addfilters(array(
'mce_buttons',
'mce_external_plugins'
));
}
}
public function init() {
$this->block_init();
}
// Initialize block
public function block_init() {
$dir = dirname( __FILE__ );
$asset_file = include( plugin_dir_path( __FILE__ ) . 'dist/index.asset.php');
// Register global block styles
wp_register_style(
'cognito-block-global-css', // Handle.
plugins_url( 'dist/style.css', __FILE__ ),
is_admin() ? array( 'wp-editor' ) : null, // Dependency to include the CSS after it.
null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.style.build.css' ) // Version: File modification time.
);
// 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']
);
// Register block editor styles for backend.
wp_register_style(
'cognito-block-editor-css', // Handle.
plugins_url( 'dist/editor.css', __FILE__ ), // Block editor CSS.
array( 'wp-edit-blocks' ), // Dependency to include the CSS after it.
null // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time.
);
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'
) );
}
// Set up tinyMCE buttons
public function mce_buttons($buttons) {
array_push($buttons, '|', 'cognito');
return $buttons;
}
// Initialize tinyMCE plug-in
public function mce_external_plugins($plugins) {
$plugins['cognito'] = plugin_dir_url( __FILE__ ) . 'tinymce/plugin.js';
return $plugins;
}
// Initialize administration menu (left-bar)
public function admin_menu() {
add_menu_page('Cognito Forms', 'Cognito Forms', 'manage_options', 'Cognito', array($this, 'main_page'), plugin_dir_url( __FILE__ ).'cogicon.ico');
add_submenu_page('Cognito', 'Cognito Forms', 'View Forms', 'manage_options', 'Cognito', array($this, 'main_page'));
add_submenu_page('Cognito', 'Create Form', 'New Form', 'manage_options', 'CognitoCreateForm', array($this, 'main_page'));
add_submenu_page('Cognito', 'Templates', 'Templates', 'manage_options', 'CognitoTemplates', array($this, 'main_page'));
add_options_page('Cognito Options', 'Cognito Forms', 'manage_options', 'CognitoOptions', array($this, 'options_page'));
}
// Entrypoint for Cognito Forms access
public function main_page() {
include 'templates/main.php';
}
public function options_page() {
include 'templates/options.php';
}
}
new CognitoFormsPlugin;
}