PluginProbe
Always Edit In HTML / 2.4.4
Always Edit In HTML v2.4.4
trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6
always-edit-in-html / always-edit-in-html.php

always-edit-in-html.php in Always Edit In HTML 2.4.4, at always-edit-in-html.php

183 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.4
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', function () {
68 return 'html';
69 });
70 }
71 }
72
73 /**
74 * Adds the option box to Pages, Posts and any other publicly declared Custom Post Types in the RHS column
75 */
76 function always_edit_in_html_create_options_box(){
77
78 // Add to pages
79 add_meta_box( 'always-edit-in-html', __( 'Always edit in HTML', 'always-edit-in-html' ),
80 'always_edit_in_html_custom_box', 'page' , 'side');
81
82 // Add to posts
83 add_meta_box( 'always-edit-in-html', __( 'Always edit in HTML', 'always-edit-in-html' ),
84 'always_edit_in_html_custom_box','post','side');
85
86 // Add to any other public declared post types
87 $args = array(
88 'public' => true,
89 '_builtin' => false
90 );
91 $post_types = get_post_types( $args, 'names' );
92
93 foreach ( $post_types as $post_type ) {
94 add_meta_box( 'always-edit-in-html', __( 'Always edit in HTML', 'always-edit-in-html' ),
95 'always_edit_in_html_custom_box',$post_type,'side');
96 }
97
98 }
99
100
101 /**
102 * Creates the Edit in HTML options box on post/page
103 *
104 * @param $post
105 */
106 function always_edit_in_html_custom_box( $post ){
107
108 // Check that data is from this post
109 wp_nonce_field( plugin_basename( __FILE__ ), 'always_edit_in_html_noncename' );
110
111 // Get the current status for this post
112 $editInHTML = always_edit_in_html_get_html_edit_status( $post->ID );
113
114 // Create the form with the options field and brief explaination of what it does.
115 echo '<p>'.__( 'Removes the Visual and Text editor tabs and opens this page/post in HTML mode', 'always-edit-in-html' ).'</p>';
116 echo '<label for="always_edit_in_html">'.__( 'Always edit in HTML?', 'always-edit-in-html' ).'</label> ';
117 echo '<input type="checkbox" id="always_edit_in_html" name="always_edit_in_html" value="on" ';
118
119 // If the option is currently being used then check the options box
120 if ( $editInHTML ){
121 echo 'checked="checked"';
122 }
123 echo ' />';
124 }
125
126 /**
127 * Grabs the Always Edit in HTML option field and checks to see if it's set
128 *
129 * @param $id
130 * @return bool
131 */
132 function always_edit_in_html_get_html_edit_status( $id ){
133 $editInHTML=get_post_meta( $id, 'editInHTML', true);
134
135 if( $editInHTML === "on" ){
136 return true;
137 }
138 else{
139 return false;
140 }
141 }
142
143 /**
144 * Save the Always Edit in HTML options along with the post update
145 *
146 * @param $post_id
147 * @return mixed
148 */
149 function always_edit_in_html_save_postdata( $post_id ){
150 // Quick check to make sure data belongs to this post
151 if( ( !isset($_POST['always_edit_in_html_noncename']) ) || ( !wp_verify_nonce( $_POST['always_edit_in_html_noncename'], plugin_basename( __FILE__ ) ) ) ){
152 return $post_id;
153 }
154
155 // Don't do anything for an autosave
156 if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
157 return $post_id;
158 }
159
160 // Make sure we have the permissions to update a post or page
161 if( 'page' === $_POST['post_type'] ){
162 if( !current_user_can( 'edit_page', $post_id ) ){
163 return $post_id;
164 }
165 }
166 else{
167 if( !current_user_can( 'edit_post', $post_id ) ){
168 return $post_id;
169 }
170 }
171
172 // Checks all done so save the option
173 if( isset( $_POST['always_edit_in_html'] ) ){
174 update_post_meta( $post_id, 'editInHTML', 'on' );
175 }
176 else{
177 update_post_meta( $post_id, 'editInHTML', 'off' );
178 }
179
180 // Returns $post_id to preserve other filters
181 return $post_id ;
182 }
183