-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclick2copy.php
202 lines (152 loc) · 7.63 KB
/
click2copy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/*
Plugin Name: click2copy
Plugin URI: https://github.com/harryjackson1221/click2copy
Description: One Plugin to Rule Them All! and it also adds clipboard.js functionality to WordPress, to copy code snippets and create smiles :-)
Version: 1.0
Author: Harry Jackson
Author URI: http://harryj.us
License: GPL2
*/
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// Protections
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
//add the scripts and fallback styles for pre box and button - test
wp_register_script( 'clipboardjs', 'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js' , array() , '1.7', 'all' );
wp_enqueue_script('clipboardjs');
wp_register_script('copyjs', plugins_url('/js/copy.js', __FILE__), false, '0.4', 'all');
wp_enqueue_script( 'copyjs');
wp_register_style('click2copy', plugins_url('/css/copy.css', __FILE__), false, '0.4', 'all');
wp_enqueue_style( 'click2copy');
function c2c_custom_option_logo() {
echo '<style type="text/css">.c2c:before {content: url(' . plugins_url( 'images/logo-small.png', __FILE__ ) . ') !important;background-repeat: no-repeat;position:relative;top:5px;}</style>';
}
//hook into the administrative header output, so we can add the logo :before
add_action('wp_before_admin_bar_render', 'c2c_custom_option_logo');
/** add plugin settings page **/
add_action( 'admin_menu', 'c2c__add_admin_menu' );
add_action( 'admin_init', 'c2c__settings_init' );
function c2c__add_admin_menu( ) {
add_options_page( 'click2copy', 'click2copy', 'manage_options', 'click2copy', 'c2c__options_page' );
}
function c2c__settings_init( ) {
register_setting( 'pluginPage', 'c2c__settings');
add_settings_section(
'c2c__pluginPage_section',
__( 'click2copy Options', 'click2copy' ),
'c2c__settings_section_callback',
'pluginPage'
);
add_settings_field(
'c2c__text_field_0',
__( '<pre> CSS class', 'click2copy' ),
'c2c__text_field_0_render',
'pluginPage',
'c2c__pluginPage_section'
);
add_settings_field(
'c2c__text_field_1',
__( '<button> CSS Class', 'click2copy' ),
'c2c__text_field_1_render',
'pluginPage',
'c2c__pluginPage_section'
);
add_settings_field(
'c2c__text_field_2',
__( '<button> Text', 'click2copy' ),
'c2c__text_field_2_render',
'pluginPage',
'c2c__pluginPage_section'
);
}
function c2c__text_field_0_render( ) {
$options = get_option( 'c2c__settings' );
?>
<input type='text' name='c2c__settings[c2c__text_field_0]' placeholder="c2cpre" value='<?php echo $options['c2c__text_field_0']; ?>'>
<?php
}
function c2c__text_field_1_render( ) {
$options = get_option( 'c2c__settings' );
?>
<input type='text' name='c2c__settings[c2c__text_field_1]' placeholder="button-primary" value='<?php echo $options['c2c__text_field_1']; ?>'>
<?php
}
function c2c__text_field_2_render( ) {
$options = get_option( 'c2c__settings' );
?>
<input type='text' name='c2c__settings[c2c__text_field_2]' placeholder="Copy" value='<?php echo $options['c2c__text_field_2']; ?>'>
<?php
}
function c2c__settings_section_callback( ) {
echo __( '<p><ul><li><b>id:</b> This identifies the code being copied, and needs to be unique for each snippet you have on the page/post. This is set within the shortcode as id="unique ID" and allows the button to target the correct element</li><li><b>pclass:</b> This applies a custom CSS class to the <pre> HTML tag for styling purposes</li><li><b>bclass:</b> This applies a custom CSS class to the <button> HTML tag for styling purposes</li><li><b>button-text:</b> This allows you to set the button text</li></ul></p>' , 'click2copy' );
}
// Create the settings page content
function c2c__options_page( ) {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
?>
<form action='options.php' method='post'>
<?php echo __('<h1 class="c2c"> click2copy</h1><p><h2>Usage</h2><p>The shortcode can be applied in either the Page or Post Editor using the following syntax: [c2c id="unique-ID"]content[/c2c]</p>');
settings_fields( 'pluginPage' );
do_settings_sections( 'pluginPage' );
submit_button();
?>
</form>
<?php echo __('<h2>Example Use</h2><p>[c2c id="code1"]<br/><html></html> <br/><?php ?> <br/>.css { display:code; }<br/>[/c2c]<br/></p>
<h2>Built With</h2><ul><li><a href="https://clipboardjs.com/" target="_blank">clipboard.js</a> - the JavaScript (thanks!)</li><li><a href="http://harryj.us/" target="_blank">harryj.us</a> - the person</li><li><a href="https://github.com/harryjackson1221/click2copy" target="_blank">click2copy</a> - the project</li></ul></p>');
}
// stop the curly quotes in the shortcode c2c
add_filter( 'no_texturize_shortcodes', 'shortcodes_to_exempt_from_wptexturize' );
function shortcodes_to_exempt_from_wptexturize( $shortcodes ) {
$shortcodes[] = 'c2c';
return $shortcodes;
}
//autop adds some line breaks so remove it, add it back with a higher priority
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 99);
add_filter( 'the_content', 'shortcode_unautop',100 );
//add_filter('the_content', 'wpautop','99');
// add a shortcode to echo and copy the content tag in a shortcode
add_shortcode('c2c', 'click2copy');
function c2c_remove_added_markup( $string ) {
$patterns = array(
'#^\s*</p>#',
'#<p>\s*$#',
'/<br \/>/iU'
);
return preg_replace($patterns, '', $string);
}
// set the function for the shortcode
function click2copy($atts, $content) {
// Create default options for the array, in case someone doesnt set an option
$c2c_options = get_option('c2c__settings');
//set constants for options, and defaults for empty fields
$c2c_pre = $c2c_options['c2c__text_field_0'];
if ( empty($c2c_pre) ) $c2c_pre = 'c2cpre';
$c2c_button = $c2c_options['c2c__text_field_1'];
if ( empty($c2c_button) ) $c2c_button = 'button-c2c';
$c2c_btn_text = $c2c_options['c2c__text_field_2'];
if ( empty($c2c_btn_text) ) $c2c_btn_text = '<img src="' . plugins_url( 'images/logo-small.png', __FILE__ ) . '" width="23px">';
$content = c2c_remove_added_markup( $content );
$escaped_copytext = htmlspecialchars( "$content" );
// Testing stuffs
// echo '<pre>';
// var_dump( $content );
// echo '</pre>';
// Return the content
return '<div class="c2c-wrapper"><pre id="' . $atts['id'] . '" class="' . $c2c_pre . '">' . $escaped_copytext . '</pre><button data-toggle="tooltip" class="' . $c2c_button . '" data-clipboard-target="pre#' . $atts['id'] . '">' . $c2c_btn_text . '</button></div>';
}
?>