| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
Plugin Name: Always Edit in HTML |
| 5 |
Plugin URI: https:/zeropointdevelopment.com/wordpress-plugins/always-edit-in-html-wordpress-plugin/ |
| 6 |
Description: Opens page and post editor in HTML mode to preserve formatting. Hides Visual editor tab. |
| 7 |
Version: 2.4.0 |
| 8 |
Author: DeveloperWil |
| 9 |
Author URI: https://profiles.wordpress.org/developerwil |
| 10 |
|
| 11 |
Copyright © 2016 Zero Point Development. zeropointdevelopment.com |
| 12 |
|
| 13 |
This program is free software: you can redistribute it and/or modify |
| 14 |
it under the terms of the GNU General Public License as published by |
| 15 |
the Free Software Foundation, either version 3 of the License, or |
| 16 |
(at your option) any later version. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, |
| 19 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
GNU General Public License for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License |
| 24 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 |
|
| 26 |
*/ |
| 27 |
global $post; |
| 28 |
|
| 29 |
/** |
| 30 |
* Add language options |
| 31 |
*/ |
| 32 |
load_plugin_textdomain( 'always-edit-in-html', false, basename( dirname( __FILE__ ) ) . '/lang' ); |
| 33 |
|
| 34 |
// Add actions for adding to post/page and saving the option |
| 35 |
if ( is_admin() ){ |
| 36 |
add_action( 'admin_init', 'always_edit_in_html_create_options_box' ); |
| 37 |
add_action( 'admin_head', 'always_edit_in_html_handler' ); |
| 38 |
add_action( 'save_post', 'always_edit_in_html_save_postdata', $post ); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Turn off the rich editing capability |
| 44 |
* |
| 45 |
* Removes the tab that switches to the visual editor |
| 46 |
*/ |
| 47 |
function always_edit_in_html_handler(){ |
| 48 |
global $post; |
| 49 |
|
| 50 |
// Check that we have a post object here, else return |
| 51 |
if( $post == null ) return; |
| 52 |
|
| 53 |
echo '<style type="text/css">'; |
| 54 |
echo '#always-edit-in-html .inside{background: url('.plugins_url( '/images/zeropointdevelopment-mark.png', __FILE__ ).') no-repeat 98% 0%;padding-right:42px;}'; |
| 55 |
echo '</style>'; |
| 56 |
|
| 57 |
|
| 58 |
// Get the meta value and check that it's switched on |
| 59 |
$editInHTML = always_edit_in_html_get_html_edit_status( $post->ID ); |
| 60 |
if ( $editInHTML ){ |
| 61 |
// Hide "Visual" tab |
| 62 |
echo '<style type="text/css">'; |
| 63 |
echo '#content-tmce.wp-switch-editor.switch-tmce{display:none;}'; |
| 64 |
echo '</style>'; |
| 65 |
|
| 66 |
// Set the editor to HTML mode ("Text") |
| 67 |
add_filter( 'wp_default_editor', create_function(null,'return "html";') ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Adds the option box to Pages, Posts and any other publicly declared Custom Post Types in the RHS column |
| 73 |
*/ |
| 74 |
function always_edit_in_html_create_options_box(){ |
| 75 |
|
| 76 |
// Add to pages |
| 77 |
add_meta_box( 'always-edit-in-html', __( 'Always edit in HTML', 'always-edit-in-html' ), |
| 78 |
'always_edit_in_html_custom_box', 'page' , 'side'); |
| 79 |
|
| 80 |
// Add to posts |
| 81 |
add_meta_box( 'always-edit-in-html', __( 'Always edit in HTML', 'always-edit-in-html' ), |
| 82 |
'always_edit_in_html_custom_box','post','side'); |
| 83 |
|
| 84 |
// Add to any other public declared post types |
| 85 |
$args = array( |
| 86 |
'public' => true, |
| 87 |
'_builtin' => false |
| 88 |
); |
| 89 |
$post_types = get_post_types( $args, 'names' ); |
| 90 |
|
| 91 |
foreach ( $post_types as $post_type ) { |
| 92 |
add_meta_box( 'always-edit-in-html', __( 'Always edit in HTML', 'always-edit-in-html' ), |
| 93 |
'always_edit_in_html_custom_box',$post_type,'side'); |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* Creates the Edit in HTML options box on post/page |
| 101 |
* |
| 102 |
* @param $post |
| 103 |
*/ |
| 104 |
function always_edit_in_html_custom_box( $post ){ |
| 105 |
|
| 106 |
// Check that data is from this post |
| 107 |
wp_nonce_field( plugin_basename( __FILE__ ), 'always_edit_in_html_noncename' ); |
| 108 |
|
| 109 |
// Get the current status for this post |
| 110 |
$editInHTML = always_edit_in_html_get_html_edit_status( $post->ID ); |
| 111 |
|
| 112 |
// Create the form with the options field and brief explaination of what it does. |
| 113 |
echo '<p>'.__( 'Removes the Visual and Text editor tabs and opens this page/post in HTML mode', 'always-edit-in-html' ).'</p>'; |
| 114 |
echo '<label for="always_edit_in_html">'.__( 'Always edit in HTML?', 'always-edit-in-html' ).'</label> '; |
| 115 |
echo '<input type="checkbox" id="always_edit_in_html" name="always_edit_in_html" value="on" '; |
| 116 |
|
| 117 |
// If the option is currently being used then check the options box |
| 118 |
if ( $editInHTML ){ |
| 119 |
echo 'checked="checked"'; |
| 120 |
} |
| 121 |
echo ' />'; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Grabs the Always Edit in HTML option field and checks to see if it's set |
| 126 |
* |
| 127 |
* @param $id |
| 128 |
* @return bool |
| 129 |
*/ |
| 130 |
function always_edit_in_html_get_html_edit_status( $id ){ |
| 131 |
$editInHTML=get_post_meta( $id, 'editInHTML', true); |
| 132 |
|
| 133 |
if( $editInHTML === "on" ){ |
| 134 |
return true; |
| 135 |
} |
| 136 |
else{ |
| 137 |
return false; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Save the Always Edit in HTML options along with the post update |
| 143 |
* |
| 144 |
* @param $post_id |
| 145 |
* @return mixed |
| 146 |
*/ |
| 147 |
function always_edit_in_html_save_postdata( $post_id ){ |
| 148 |
// Quick check to make sure data belongs to this post |
| 149 |
if( ( !isset($_POST['always_edit_in_html_noncename']) ) || ( !wp_verify_nonce( $_POST['always_edit_in_html_noncename'], plugin_basename( __FILE__ ) ) ) ){ |
| 150 |
return $post_id; |
| 151 |
} |
| 152 |
|
| 153 |
// Don't do anything for an autosave |
| 154 |
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){ |
| 155 |
return $post_id; |
| 156 |
} |
| 157 |
|
| 158 |
// Make sure we have the permissions to update a post or page |
| 159 |
if( 'page' === $_POST['post_type'] ){ |
| 160 |
if( !current_user_can( 'edit_page', $post_id ) ){ |
| 161 |
return $post_id; |
| 162 |
} |
| 163 |
} |
| 164 |
else{ |
| 165 |
if( !current_user_can( 'edit_post', $post_id ) ){ |
| 166 |
return $post_id; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
// Checks all done so save the option |
| 171 |
if( isset( $_POST['always_edit_in_html'] ) ){ |
| 172 |
update_post_meta( $post_id, 'editInHTML', 'on' ); |
| 173 |
} |
| 174 |
else{ |
| 175 |
update_post_meta( $post_id, 'editInHTML', 'off' ); |
| 176 |
} |
| 177 |
|
| 178 |
// Returns $post_id to preserve other filters |
| 179 |
return $post_id ; |
| 180 |
} |
| 181 |
|