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