. */ $tmve_db_version = 1.0; $tmve_db_tablename = 'tmve_allowed_elements'; $tmve_default_attribte = ''; /** * Activation hook */ function tmve_activate() { global $tmve_db_version; // Test to see if this plugin has been installed before $installed = get_option( 'tmve_db_version' ); if ( !$installed ){ // Never been installed, install db tmve_install(); } else if ( $installed != $tmve_db_version ){ // Install exists and db versions differ, upgrade db tmve_update(); } else { // Install exists, do nothing } } /** * Deactivation hook */ function tmve_deactivate() { global $wpdb; $tablename = $wpdb->prefix . 'tmve_allowed_elements'; if ( get_option('tmve_cascade_on_deactivate') == 'true' ) { delete_option('tmve_db_version'); $wpdb->query("DROP TABLE $tablename"); delete_option('tmve_cascade_on_deactivate'); } } /** * Install db tables */ function tmve_install(){ global $wpdb; $tmve_db_version = '1.0'; $tmve_db_tablename = 'tmve_allowed_elements'; $tablename = $wpdb->prefix . $tmve_db_tablename; $sql = "CREATE TABLE " . $tablename . " ( name varchar(30) NOT NULL, attribute varchar(30) NOT NULL, primary key (name,attribute) )"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); if ( ! get_option('tmve_cascade_on_deactivate') ) { add_option('tmve_cascade_on_deactivate', "false"); } update_option('tmve_db_version', $tmve_db_version); } /** * Future db migrations/upgrades */ function tmve_update(){ } function tmve_get_element_map() { global $wpdb, $tmve_db_tablename, $tmve_default_attribte; $elements = array(); $tablename = $wpdb->prefix . $tmve_db_tablename; $results = $wpdb->get_results("SELECT * FROM " . $tablename); foreach ( $results as $ele ) { if ( ! isset( $elements[ $ele->name ] ) ) { $elements[ $ele->name ] = array(); } if ( $ele->attribute != $tmve_default_attribte ){ $elements[ $ele->name ][] = $ele->attribute; } } ksort( $elements ); foreach ( $elements as $element => $attributes ) { sort( $elements[ $element ] ); } return $elements; } function tmve_add_admin_pages() { add_management_page('TinyMCE Valid Elements', 'TinyMCE Valid Elements', 10, __FILE__, 'tmve_admin_menu'); } function is_alnum( $str ) { return strspn($str, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") == strlen($str); } function is_alpha( $str ) { return strspn($str, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") == strlen($str); } function tmve_add_element( $element = null ) { global $wpdb, $tmve_db_tablename, $tmve_default_attribte; $tablename = $wpdb->prefix . $tmve_db_tablename; if ( count( $wpdb->get_results( "SELECT * FROM $tablename WHERE lower(name) = lower('$element')" ) ) > 0 ) { return false; } return $wpdb->query("INSERT INTO $tablename (name, attribute) VALUES ('$element','$tmve_default_attribte')"); } function tmve_remove_element( $element = null ) { global $wpdb, $tmve_db_tablename; $tablename = $wpdb->prefix . $tmve_db_tablename; return $wpdb->query("DELETE FROM $tablename WHERE lower(name) = lower('$element')"); } function tmve_add_attribute( $element = null, $attribute = null ) { global $wpdb, $tmve_db_tablename, $tmve_default_attribte; $tablename = $wpdb->prefix . $tmve_db_tablename; if ( count( $wpdb->get_results( "SELECT * FROM $tablename WHERE lower(name) = lower('$element') AND lower(attribute) = lower('$attribute')" ) ) > 0 ) { return false; } return $wpdb->query("INSERT INTO $tablename (name, attribute) VALUES ('$element','$attribute')"); } function tmve_remove_attribute( $element = null, $attribute = null ) { global $wpdb, $tmve_db_tablename; $tablename = $wpdb->prefix . $tmve_db_tablename; return $wpdb->query("DELETE FROM $tablename WHERE lower(name) = lower('$element') AND lower(attribute) = lower('$attribute')"); } function tmve_do_actions() { $mesg = ''; $emesg = ''; if ( isset( $_GET[ 'add_ele' ] ) ) { if ( ! empty( $_GET[ 'add_ele' ] ) ) { if ( is_alpha( $_GET[ 'add_ele' ] ) ) { if ( tmve_add_element( $_GET[ 'add_ele' ] ) ) { $mesg = 'Element \'' . $_GET[ 'add_ele' ] . '\' added. Refresh TinyMCE (ctrl + F5).'; } else { $emesg = 'Element \'' . $_GET[ 'add_ele' ] . '\' already exists.'; } } else { $emesg = "New element must contain letters only."; } } else { $emesg = "New element can't be empty."; } } else if ( isset( $_GET[ 'rm_ele' ] ) ) { if ( ! empty( $_GET[ 'rm_ele' ] ) ) { if ( tmve_remove_element( $_GET[ 'rm_ele' ] ) ) { $mesg = 'Element \'' . $_GET[ 'rm_ele' ] . '\' removed.'; } else { $emesg = 'Error. Element \'' . $_GET[ 'rm_ele' ] . '\' not removed.'; } } else { $emesg = "Element can't be empty."; } } else if ( isset( $_GET[ 'add_attr' ] ) ) { if ( ! empty( $_GET[ 'add_attr' ] ) ) { if ( is_alpha( $_GET[ 'add_attr' ] ) ) { if ( tmve_add_attribute( $_GET[ 'ele' ], $_GET[ 'add_attr' ] ) ) { $mesg = 'Attribute \'' . $_GET[ 'add_attr' ] . '\' added to element \''. $_GET[ 'ele' ] .'\'. Refresh TinyMCE (ctrl + F5)'; } else { $emesg = 'Attribute \'' . $_GET[ 'add_attr' ] . '\' already exists.'; } } else { $emesg = "New attribute must contain letters only."; } } else { $emesg = "New attribute can't be empty."; } } else if ( isset( $_GET[ 'rm_attr' ] ) ) { if ( ! empty( $_GET[ 'rm_attr' ] ) ) { if ( tmve_remove_attribute( $_GET[ 'ele' ], $_GET[ 'rm_attr' ] ) ) { $mesg = 'Attribute \'' . $_GET[ 'rm_attr' ] . '\' removed from element \''. $_GET[ 'ele' ] .'\'.'; } else { $emesg = 'Error. Attribute \'' . $_GET[ 'rm_ele' ] . '\' not removed.'; } } else { $emesg = "Attribute can't be empty."; } } else if ( isset( $_GET[ 'delete_on_deactivate' ] ) ) { if ( $_GET[ 'delete_on_deactivate' ] == "yes" ) { update_option( 'tmve_cascade_on_deactivate', "true" ); $mesg = 'Deactivation settings changed. Plugin tables and settings deleted on deactivation.'; } else if ( $_GET[ 'delete_on_deactivate' ] == "no" ) { update_option( 'tmve_cascade_on_deactivate', "false" ); $mesg = 'Deactivation settings changed. Plugin tables and settings saved on deactivation.'; } else { $emesg = 'Invalid deactivation settings used.'; } } if ( ! empty( $mesg ) ) { echo '
'. $mesg . '
'. $emesg . '
By default, WordPress' WYSIWYG editor, TinyMCE, will strip out of your Article and Page HTML code any elements that are not defined as "valid elements"; this can be extremely annoying (especially if you want to include iframes).
This plugin will allow you to extend what TinyMCE defines as "valid elements". By doing so, TinyMCE will no longer remove, delete, or strip-out the additional elements and attributes that you specify.
** NOTE: Make sure after you add elements or attributes you do a hard refresh (ctrl + F5) of your browser on a TinyMCE screen (editing or creating a page/post) so that the TinyMCE cache will be refreshed! You will not see your changes until you do this!
To add an element, enter the element name click "Add Element".
To delete an element, click the minus icon [-] next to the element name. A confirmation box will appear; on confirmation, the element and all of it's child attributes will be deleted.
To add an attribute, enter the attribute name on the desired elements' add attribute box and click "Add Attribute".
To delete an attribute, click the minus icon [-] next to the attribute name. A confirmation box will appear; on confirmation, the attribute will be deleted.
By default, when you deactivate the plugin, your elements and attributes remain stored in the WordPress database; this was done to guard against accidental deactivation and the event where you want to re-activate the plugin
If you would like to change what happens to the database tables and options once this plugin is deactivated, change the options below: