Repository navigation
Expand file tree
/
Copy path_snippets.plugin.php
More file actions
201 lines (176 loc) · 5.54 KB
/
Copy path_snippets.plugin.php
File metadata and controls
201 lines (176 loc) · 5.54 KB
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
<?php
/**
* This file implements the Snippet Toolbar plugin for b2evolution
*
* This is Ron's remix!
* Includes code from the WordPress team -
* http://sourceforge.net/project/memberlist.php?group_id=51422
*
* b2evolution - {@link http://b2evolution.net/}
* Released under GNU GPL License - {@link http://b2evolution.net/about/gnu-gpl-license}
* @copyright (c)2003-2020 by Francois Planque - {@link http://fplanque.com/}
*
* @package plugins
*/
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
/**
* @package plugins
*/
class snippets_plugin extends Plugin
{
var $code = 'snippets';
var $name = 'Snippets';
var $priority = 60;
var $version = '7.3.0';
var $group = 'editor';
var $number_of_installs = 1;
/**
* Init
*/
function PluginInit( & $params )
{
$this->short_desc = T_('Toolbar with buttons to insert text snippets');
$this->long_desc = T_('This plugin displays a toolbar with buttons which allow to quickly insert text snippets into items or comments.');
}
/**
* Define here default collection/blog settings that are to be made available in the backoffice.
*
* @param array Associative array of parameters.
* @return array See {@link Plugin::GetDefaultSettings()}.
*/
function get_coll_setting_definitions( & $params )
{
return array_merge( parent::get_coll_setting_definitions( $params ), array(
'snippets' => array(
'label' => T_('Snippets'),
'type' => 'array',
'entries' => array(
'title' => array(
'label' => T_('Title'),
'defaultvalue' => '',
),
'text' => array(
'label' => T_('Text'),
'type' => 'textarea',
'defaultvalue' => '',
),
'min_user_level' => array(
'label' => T_('Min user level'),
'type' => 'integer',
'defaultvalue' => 0,
'valid_range' => array(
'min' => 0,
'max' => 10,
),
),
)
),
) );
}
/**
* Event handler: Called when displaying editor toolbars on post/item form.
*
* This is for post/item edit forms only. Comments, PMs and emails use different events.
*
* @param array Associative array of parameters
* @return boolean did we display a toolbar?
*/
function AdminDisplayToolbar( & $params )
{
$Item = & $params['Item'];
if( empty( $Item ) ||
! ( $item_Blog = & $Item->get_Blog() ) ||
! $this->get_coll_setting( 'coll_use_for_posts', $item_Blog ) )
{ // This plugin cannot be used for current Item:
return false;
}
$params['Blog'] = $item_Blog;
return $this->DisplayCodeToolbar( $params );
}
/**
* Event handler: Called when displaying editor toolbars on comment form.
*
* @param array Associative array of parameters
* @return boolean did we display a toolbar?
*/
function DisplayCommentToolbar( & $params )
{
$Comment = & $params['Comment'];
if( empty( $Comment ) ||
! ( $comment_Item = & $Comment->get_Item() ) ||
! ( $item_Blog = & $comment_Item->get_Blog() ) ||
! $this->get_coll_setting( 'coll_use_for_comments', $item_Blog ) )
{ // This plugin cannot be used for current Comment:
return false;
}
$params['Blog'] = $item_Blog;
return $this->DisplayCodeToolbar( $params );
}
/**
* Display a code toolbar
*
* @param array Associative array of parameters
* @return boolean did we display a toolbar?
*/
function DisplayCodeToolbar( & $params )
{
global $current_User;
$params = array_merge( array(
'js_prefix' => '', // Use different prefix if you use several toolbars on one page
), $params );
if( ! is_logged_in() )
{ // Don't try to display any snippet because they are restricted by user level:
return false;
}
// Get snippets for current Collection and current User:
$snippets = $this->get_coll_setting( 'snippets', $params['Blog'] );
if( is_array( $snippets ) && ! empty( $snippets ) )
{
foreach( $snippets as $s => $snippet )
{
if( $current_User->get( 'level' ) < $snippet['min_user_level'] )
{ // Don't display the snippet because of min user level:
unset( $snippets[ $s ] );
}
}
}
if( empty( $snippets ) )
{ // No snippets for current user:
return false;
}
// Load js to work with textarea
require_js_defer( 'functions.js', 'blog', true );
?><script>
//<![CDATA[
var <?php echo $params['js_prefix']; ?>b2evo_snippets = new Array( <?php
foreach( $snippets as $snippet )
{
echo '\''.format_to_js( $snippet['text'] ).'\',';
} ?> );
function <?php echo $params['js_prefix']; ?>b2evo_insert_snippet( canvas_field, i )
{
textarea_wrap_selection( canvas_field, <?php echo $params['js_prefix']; ?>b2evo_snippets[i], '', 0 );
}
//]]>
</script><?php
// Display toolbar with snippets:
echo $this->get_template( 'toolbar_before', array( '$toolbar_class$' => $params['js_prefix'].$this->code.'_toolbar' ) );
echo $this->get_template( 'toolbar_title_before' ).TS_('Snippets').': '.$this->get_template( 'toolbar_title_after' );
echo $this->get_template( 'toolbar_group_before' );
$snippet_index = 0; // Index of the snippets, Used in the js func b2evo_insert_snippet to get a snippet text:
foreach( $snippets as $snippet )
{
echo '<input '.get_field_attribs_as_string( array(
'type' => 'button',
'value' => $snippet['title'],
'data-func' => $params['js_prefix'].'b2evo_insert_snippet|'.$params['js_prefix'].'b2evoCanvas|'.$snippet_index,
'class' => $this->get_template( 'toolbar_button_class' ),
) ).'/>';
$snippet_index++;
}
echo $this->get_template( 'toolbar_group_after' );
echo $this->get_template( 'toolbar_after' );
return true;
}
}
?>