| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: CF7 HTML Editor |
| 4 |
* Plugin URI: https://wordpress.org/cf7-coder |
| 5 |
* Description: Add HTML editor to Contact Form 7. |
| 6 |
* Version: 0.1 |
| 7 |
* Author: Wow-Company |
| 8 |
* Author URI: https://wow-estore.com/ |
| 9 |
* License: GPL-2.0+ |
| 10 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
| 11 |
* Text Domain: cf7-coder |
| 12 |
* Domain Path: /languages |
| 13 |
* |
| 14 |
* PHP version 5.6.0 |
| 15 |
* |
| 16 |
* @category Wordpress_Plugin |
| 17 |
* @package Wow_Plugin |
| 18 |
* @author Dmytro Lobov <i@lobov.dev> |
| 19 |
* @copyright 2021 Wow-Company |
| 20 |
* @license GNU Public License |
| 21 |
* @version 0.1 |
| 22 |
*/ |
| 23 |
|
| 24 |
if ( ! defined( "ABSPATH" ) ) { |
| 25 |
exit(); |
| 26 |
} |
| 27 |
|
| 28 |
class CF7_Coder { |
| 29 |
public function __construct() { |
| 30 |
add_action( 'plugins_loaded', [ $this, "text_domain" ] ); |
| 31 |
add_action( "admin_enqueue_scripts", [ $this, "style_script" ] ); |
| 32 |
add_action( 'wpcf7_admin_footer', [ $this, 'add_sidebar' ] ); |
| 33 |
add_action( 'wpcf7_admin_misc_pub_section', [ $this, 'wpcf7_add_test_mode' ] ); |
| 34 |
add_filter( 'wpcf7_contact_form_properties', [ $this, 'wpcf7_add_properties' ] ); |
| 35 |
add_action( 'wpcf7_save_contact_form', [ $this, 'wpcf7_save' ] ); |
| 36 |
add_filter( 'do_shortcode_tag', [ $this, 'wpcf7_frontend' ], 10, 4 ); |
| 37 |
} |
| 38 |
|
| 39 |
// Download the folder with languages |
| 40 |
public function text_domain() { |
| 41 |
$languages_folder = dirname( plugin_basename( __FILE__ ) ) . '/languages/'; |
| 42 |
load_plugin_textdomain( 'cf7-coder', false, $languages_folder ); |
| 43 |
} |
| 44 |
|
| 45 |
// Include script and style in CF7 page |
| 46 |
public function style_script( $hook ) { |
| 47 |
$page_new = 'contact_page_wpcf7-new'; |
| 48 |
$page = 'toplevel_page_wpcf7'; |
| 49 |
|
| 50 |
|
| 51 |
if ( $page == $hook || $page_new == $hook ) { |
| 52 |
|
| 53 |
$version = '0.1'; |
| 54 |
|
| 55 |
wp_enqueue_script( 'code-editor' ); |
| 56 |
wp_enqueue_style( 'code-editor' ); |
| 57 |
wp_enqueue_script( 'htmlhint' ); |
| 58 |
|
| 59 |
|
| 60 |
$url_style = plugin_dir_url( __FILE__ ) . 'assets/style.css'; |
| 61 |
wp_enqueue_style( "coder-wpcf7", $url_style ); |
| 62 |
|
| 63 |
$url_script = plugin_dir_url( __FILE__ ) . 'assets/script.js'; |
| 64 |
wp_enqueue_script( "coder-wpcf7", $url_script, [ "jquery" ], $version, false ); |
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
function add_sidebar() { |
| 70 |
?> |
| 71 |
<div id="informationdiv_coder" class="postbox" style="display:none"> |
| 72 |
<h3>Some helpful information!</h3> |
| 73 |
<div class="inside"> |
| 74 |
<p> You can use the next classes for change the Contact Form 7 style:</p> |
| 75 |
<ol> |
| 76 |
<li><b>wpcf7</b> - for style of the form wrapper</li> |
| 77 |
<li><b>wpcf7-form</b> - for form style</li> |
| 78 |
<li><b>wpcf7-not-valid-tip</b> - field validation text</li> |
| 79 |
<li><b>wpcf7-response-output</b> - send status message</li> |
| 80 |
|
| 81 |
</ol> |
| 82 |
<p><a href="<?php echo esc_url( wp_customize_url() ); ?>" class="button is-primary">Customizing CSS</a> |
| 83 |
</p> |
| 84 |
|
| 85 |
</div> |
| 86 |
</div> |
| 87 |
<?php |
| 88 |
} |
| 89 |
|
| 90 |
// Add checkbox 'Test Mode' in sidebar |
| 91 |
public function wpcf7_add_test_mode() { |
| 92 |
$post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : '-1'; |
| 93 |
$checked = get_post_meta( $post_id, '_wpcf7_test_mode', true ); |
| 94 |
?> |
| 95 |
<div class="misc-pub-section"> |
| 96 |
<label class="wpcf7-test-mode"> |
| 97 |
<input value="1" type="checkbox" name="wpcf7-test-mode" <?php checked( $checked ); ?>> |
| 98 |
<?php esc_html_e( 'Test Mode', 'cf7-coder' ); ?> |
| 99 |
</label> |
| 100 |
</div> |
| 101 |
<?php |
| 102 |
$post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : '-1'; |
| 103 |
$checked = get_post_meta( $post_id, '_wpcf7_remove_auto_tags', true ); |
| 104 |
?> |
| 105 |
<div class="misc-pub-section"> |
| 106 |
<label class="wpcf7-remove-auto-tags"> |
| 107 |
<input value="1" type="checkbox" name="wpcf7-remove-auto-tags" <?php checked( $checked ); ?>> |
| 108 |
<?php esc_html_e( 'Remove Auto tags p and br', 'cf7-coder' ); ?> |
| 109 |
</label> |
| 110 |
</div> |
| 111 |
<?php |
| 112 |
} |
| 113 |
|
| 114 |
// Add properties for form |
| 115 |
function wpcf7_add_properties( $properties ) { |
| 116 |
$more_properties = array( |
| 117 |
'wpcf7_test_mode' => '', |
| 118 |
'wpcf7_remove_auto_tags' => '', |
| 119 |
); |
| 120 |
|
| 121 |
return array_merge( $more_properties, $properties ); |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
// Save custom properties |
| 126 |
function wpcf7_save( $contact_form ) { |
| 127 |
|
| 128 |
$properties = $contact_form->get_properties(); |
| 129 |
|
| 130 |
$properties['wpcf7_test_mode'] = isset( $_POST['wpcf7-test-mode'] ) ? '1' : ''; |
| 131 |
$properties['wpcf7_remove_auto_tags'] = isset( $_POST['wpcf7-remove-auto-tags'] ) ? '1' : ''; |
| 132 |
|
| 133 |
|
| 134 |
$contact_form->set_properties( $properties ); |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
// Work with Frontend |
| 139 |
function wpcf7_frontend( $output, $tag, $atts, $m ) { |
| 140 |
|
| 141 |
if ( $tag === 'contact-form-7' ) { |
| 142 |
$remove_tags = get_post_meta( $atts['id'], '_wpcf7_remove_auto_tags', true ); |
| 143 |
if ( ! empty( $remove_tags ) ) { |
| 144 |
$output = str_replace( array( '<p>', '</p>', '<br/>' ), '', $output );; |
| 145 |
} |
| 146 |
|
| 147 |
$test_mode = get_post_meta( $atts['id'], '_wpcf7_test_mode', true ); |
| 148 |
if ( ! empty( $test_mode ) && ! current_user_can( 'administrator' ) ) { |
| 149 |
$output = ''; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return $output; |
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
add_action( 'plugins_loaded', 'wpcf7_coder_load', 999 ); |
| 159 |
function wpcf7_coder_load() { |
| 160 |
if ( ! class_exists( 'WPCF7' ) ) { |
| 161 |
require_once 'class.wpcf7coder-extension-activation.php'; |
| 162 |
$activation = new wpcf7_Coder_Extension_Activation( plugin_dir_path( __FILE__ ), basename( __FILE__ ) ); |
| 163 |
$activation = $activation->run(); |
| 164 |
deactivate_plugins( plugin_basename( __FILE__ ) ); |
| 165 |
} else { |
| 166 |
new CF7_Coder(); |
| 167 |
} |
| 168 |
} |
| 169 |
|