/
tabasamu.plugin.php
executable file
·179 lines (160 loc) · 4.57 KB
/
tabasamu.plugin.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
<?php
/**
* Tabasamu smilies plugin for Habari.
*
* @package tabasamu
*/
require_once 'tabasamuformat.php';
class Tabasamu extends Plugin
{
const VERSION = '0.8';
const OPTION_NAME = 'tabasamu__package';
const PACKAGE_FILE = 'smilies.xml';
const DEFAULT_PACKAGE = 'phoenity';
private $package;
private $search = array();
private $replace = array();
/**
* Add update beacon support
**/
public function action_update_check()
{
Update::add( 'Tabasamu', '2a236020-7475-11dd-ad8b-0800200c9a66', $this->info->version );
}
/**
* Set the default packages on plugin activation.
*/
public function action_plugin_activation( $file )
{
if ( $file == $this->get_file() ) {
if ( ! Options::get( self::OPTION_NAME ) ) {
Options::set( self::OPTION_NAME, self::DEFAULT_PACKAGE );
}
}
}
/**
* Apply the default Formatters on init.
*/
public function action_init()
{
Format::apply( 'tabasamu', 'post_content_out' );
Format::apply( 'tabasamu', 'comment_content_out' );
}
/**
* Loads the Tabasamu package into memory and builds the
* search/replacement arrays for smiley replacements.
*
*/
private function load_smilies()
{
$package_name = Options::get( self::OPTION_NAME );
$config_file = $this->get_path( $package_name ) . '/' . self::PACKAGE_FILE;
if ( file_exists( $config_file ) ) {
$this->package = simplexml_load_file( $config_file );
foreach( $this->package->smiley as $smiley ) {
$this->search[]= strval( $smiley->text );
$this->replace[]= $this->img( $smiley, $package_name );
}
}
}
/**
* Replaces text smilies with image counterpart.
*/
public function filter_tabasamu_smilies( $content )
{
if ( ( ! $this->search ) || ( ! $this->replace ) ) {
$this->load_smilies();
}
$out = '';
$texts = preg_split( "/(<.*>)/U", $content, -1, PREG_SPLIT_DELIM_CAPTURE );
foreach ( $texts as $text ) {
if ( ( strlen($text) > 0 ) && ( $text{0} != '<' ) ) {
$text = str_replace( $this->search, $this->replace, $text );
}
$out.= $text;
}
return $out;
}
/**
* Outputs the options form on the plugin page.
*/
public function action_plugin_ui( $plugin_id, $action )
{
if ( $plugin_id == $this->plugin_id() ) {
$form = new FormUI( 'tabasamu' );
$control = $form->append('select', 'control', self::OPTION_NAME, _t( 'The active Tabasamu smilies.' ) );
foreach( $this->get_all_packages() as $package_name => $package ) {
$control->options[$package_name]= $package->info->name . ' ' . $package->info->version;
}
$control->add_validator( 'validate_required' );
$form->append( 'submit', 'save', _t( 'Save' ) );
$form->out();
}
}
/**
* Outputs the "configure" button on the plugin page.
*/
public function filter_plugin_config( $actions, $plugin_id ) {
if ( $plugin_id == $this->plugin_id() ) {
return array( _t('Choose Smilies') );
}
return $actions;
}
/**
* get the url to the smilies folder for given package name.
*
* @param string $package_name the smilies package name
*/
private function get_smiley_url( $package_name = NULL )
{
return $this->get_url() . '/' . $package_name;
}
/**
* get the absolute path to the smilies folder for given package name.
*
* @param string $package_name the smilies package name
*/
private function get_path( $package_name = NULL )
{
return dirname( $this->get_file() ) . '/' . $package_name;
}
/**
* Gets an array of all tabasamu packages that are available.
*
* @return array An array of package objects.
**/
public function get_all_packages()
{
$packages = array();
foreach ( glob( $this->get_path() . '*/' . self::PACKAGE_FILE ) as $file ) {
$package_name = basename( dirname( $file ) );
$packages[$package_name]= simplexml_load_file( $file );
}
return $packages;
}
/**
* Returns an html img tag for the given smiley / image. Attributes
* from the smilies config file are merged with the defaults. To set,
* for example, width you would do: <code><smiley width="20px"></code>.
*
* @param obj $smiley A simpleXML smiley object.
* @return string A HTML img tag for the given smiley.
*/
private function img( $smiley, $package_name )
{
$url = $this->get_smiley_url( $package_name );
$atts = array(
'class' => 'habari-smiley',
'alt' => $smiley->text,
'src' => $url . '/' . $smiley->image,
);
if ( $custom_atts = (array) $smiley->attributes() ) {
$atts = array_merge( $atts, $custom_atts['@attributes'] );
}
foreach ( $atts as $k => $v ) {
$atts[$k]= sprintf( "%s=\"%s\"", $k, htmlentities( trim( strval($v) ) ) );
}
return ' <img ' . implode( ' ', $atts ) . ' /> ';
}
}
?>