PluginProbe
wpForo Forum / 1.4.10
wpForo Forum v1.4.10
3.1.6 3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 All 138 releases
wpforo / wpf-includes / class-phrases.php

class-phrases.php in wpForo Forum 1.4.10, at wpf-includes/class-phrases.php

249 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if( !defined( 'ABSPATH' ) ) exit;
4
5 class wpForoPhrase{
6 public $phrases;
7 public $__phrases;
8
9 public function __construct(){}
10
11 public function init(){
12 if( WPF()->is_installed() ) {
13 if ( $phrases = $this->get_phrases( array('langid' => WPF()->general_options['lang']) ) ) {
14 foreach ($phrases as $phrase) {
15 $this->phrases[addslashes(strtolower($phrase['phrase_key']))] = $phrase['phrase_value'];
16 $this->__phrases[addslashes(strtolower($phrase['phrase_key']))] = wpforo_phrase($phrase['phrase_key'], false);
17 }
18 }
19 }
20 }
21
22 function add( $args = array() ){
23 if( empty($args) && empty($_REQUEST['phrase']) ) return FALSE;
24 if( empty($args) && !empty($_REQUEST['phrase']) ) $args = $_REQUEST['phrase'];
25 extract($args);
26
27 if( empty($package) ) $package = 'wpforo';
28 $sql = WPF()->db->prepare( "INSERT IGNORE INTO `".WPF()->tables->phrases."`
29 (`langid`, `phrase_key`, `phrase_value`, `package`)
30 VALUES (%d, %s, %s, %s)",
31 WPF()->general_options['lang'],
32 stripslashes(esc_html($key)),
33 stripslashes(esc_html($value)),
34 stripslashes(esc_html($package)) );
35 if(WPF()->db->query( $sql )){
36 WPF()->notice->add('Phrase successfully added', 'success');
37 $this->clear_cache();
38 return WPF()->db->insert_id;
39 }
40 WPF()->notice->add('Phrase add error', 'error');
41 return FALSE;
42 }
43
44 function edit(){
45 if( !empty($_POST['phrase']['data']) && is_array($_POST['phrase']['data']) ){
46 foreach($_POST['phrase']['data'] as $key => $phrase){
47 WPF()->db->update(
48 WPF()->tables->phrases,
49 array( 'phrase_value' => sanitize_text_field(stripslashes($phrase['title']))),
50 array( 'phraseid' => intval($key) ),
51 array( '%s' ),
52 array( '%d' )
53 );
54
55 }
56 $this->clear_cache();
57 WPF()->notice->add('Phrase successfully updates', 'success');
58 return TRUE;
59 }
60
61 WPF()->notice->add('Phrase update error', 'error');
62 return FALSE;
63 }
64
65 function get_wpforo_phrase($phraseid){
66 $sql = 'SELECT * FROM '.WPF()->tables->phrases.' WHERE `phraseid` ='.intval($phraseid);
67 return WPF()->db->get_row($sql, ARRAY_A);
68 }
69
70 function get_phrases($args = array(), &$items_count = 0){
71 $default = array(
72 'include' => array(), // array( 2, 10, 25 )
73 'exclude' => array(), // array( 2, 10, 25 )
74 'langid' => WPF()->general_options['lang'],
75 'package' => array(),
76
77 'orderby' => 'phraseid',
78 'order' => 'ASC', // ASC DESC
79 'offset' => '', // this use when you give row_count
80 'row_count' => ''
81 );
82
83 $args = wpforo_parse_args( $args, $default );
84 if(is_array($args) && !empty($args)){
85
86 $key = substr(md5(serialize($args)), 0, 10);
87
88 extract($args, EXTR_OVERWRITE);
89
90 $package = wpforo_parse_args( $package );
91 $include = wpforo_parse_args( $include );
92 $exclude = wpforo_parse_args( $exclude );
93
94 $wheres = array();
95
96 if(!empty($package)) $wheres[] = "`package` IN('" . implode("','", array_map('esc_sql', array_map('sanitize_text_field', $package)) ) . "')";
97 if(!empty($include)) $wheres[] = "`phraseid` IN(" . implode(', ', array_map('intval', $include)) . ")";
98 if(!empty($exclude)) $wheres[] = "`phraseid` NOT IN(" . implode(', ', array_map('intval', $exclude)) . ")";
99 if($langid != NULL) $wheres[] = "`langid` = " . intval($langid);
100
101 $sql = "SELECT * FROM `".WPF()->tables->phrases."`";
102 if(!empty($wheres)){
103 $sql .= " WHERE " . implode($wheres, " AND ");
104 }
105
106 $item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql);
107 if( $item_count_sql ) $items_count = WPF()->db->get_var($item_count_sql);
108
109 $sql .= esc_sql(" ORDER BY `$orderby` " . $order);
110
111 if($row_count != '' && $offset == ''){ // If you give only row_count this if fixed problam
112 $offset = $row_count;
113 $row_count = '';
114 }
115 $sql .= $offset != '' ? esc_sql(' LIMIT '.$offset) : '';
116 $sql .= $row_count != '' ? esc_sql(', '.$row_count) : '';
117
118 if ( false === ( $phrases = get_transient( 'wpforo_get_phrases_' . $key ) ) ) {
119 $phrases = WPF()->db->get_results($sql, ARRAY_A);
120 set_transient( 'wpforo_get_phrases_' . $key , $phrases, 60*60*24 );
121 }
122 return get_transient( 'wpforo_get_phrases_' . $key );
123 }
124 }
125
126 function search( $needle = '', $fields = array( 'phrase_key', 'phrase_value' )){
127 if( !$needle ) return array();
128 $phreseids = array();
129 if(!is_array($fields)) $fields = array($fields);
130 $needle = substr(sanitize_text_field($needle), 0, 60);
131 foreach($fields as $field){
132 $field = sanitize_text_field($field);
133 $matches = WPF()->db->get_col( "SELECT `phraseid` FROM ".WPF()->tables->phrases." WHERE `".esc_sql($field)."` LIKE '%".esc_sql($needle)."%'" );
134 $phreseids = array_merge( $phreseids, $matches );
135 }
136 return array_unique($phreseids);
137 }
138
139 public function xml_import($xmlfile, $type = 'import'){
140 $file = WPFORO_DIR . '/wpf-admin/xml/' . $xmlfile;
141 if( file_exists( $file ) && function_exists('xml_parser_create') ) {
142 $xr = xml_parser_create();
143 $fp = fopen($file, "r");
144 $xml = fread($fp, filesize($file));
145
146 xml_parser_set_option( $xr, XML_OPTION_CASE_FOLDING, 1 );
147 xml_parse_into_struct( $xr, $xml, $vals, $index );
148 xml_parser_free( $xr );
149
150 delete_transient( 'wpforo_get_phrases' );
151
152 if(!empty($vals)){
153
154 if( isset($vals[0]['tag']) && $vals[0]['tag'] == 'LANGUAGE' && isset($vals[0]['attributes']['LANGUAGE']) && $vals[0]['attributes']['LANGUAGE'] ){
155
156 $sql = "SELECT `langid` FROM `".WPF()->tables->languages."` WHERE `name` LIKE '". esc_sql(sanitize_text_field($vals[0]['attributes']['LANGUAGE'])) ."'";
157 $langid = WPF()->db->get_var( $sql );
158
159 if( !$langid ){
160 $sql = "INSERT INTO `".WPF()->tables->languages."` (`name`) VALUES ( '".esc_sql(sanitize_text_field($vals[0]['attributes']['LANGUAGE']))."' )";
161 if( WPF()->db->query($sql) ){
162 $langid = WPF()->db->insert_id;
163 }
164 }
165
166 if( $langid ){
167 foreach($vals as $val){
168 if( isset($val['tag']) && $val['tag'] == 'PHRASE' && isset($val['attributes']['NAME']) && trim($val['attributes']['NAME']) && isset($val['value']) && trim($val['value']) ){
169 $sql = "INSERT IGNORE INTO `".WPF()->tables->phrases."`
170 (`phraseid`, `langid`, `phrase_key`, `phrase_value`)
171 VALUES( NULL,
172 '".esc_sql(trim($langid))."',
173 '".esc_sql(trim($val['attributes']['NAME']))."',
174 '".esc_sql(trim($val['value']))."')";
175 WPF()->db->query($sql);
176 }
177 }
178 if( !isset(WPF()->general_options['lang']) ){
179 $blogname = get_option('blogname');
180 $general_options = array(
181 'title' => $blogname . __(' Forum', 'wpforo'),
182 'description' => $blogname . __(' Discussion Board', 'wpforo'),
183 'lang' => sanitize_text_field($langid),
184 );
185 }else{
186 $general_options = WPF()->general_options;
187 $general_options['lang'] = sanitize_text_field($langid);
188 }
189 if( $type == 'install' ){
190 add_option('wpforo_general_options', $general_options);
191 }
192 else{
193 update_option('wpforo_general_options', $general_options);
194 }
195 return $langid;
196 }
197 }
198 }
199 }
200
201 return FALSE;
202 }
203
204 function add_lang(){
205 if( is_array($_FILES['add_lang']['name']) && !empty($_FILES['add_lang']['name']) && isset($_FILES['add_lang']['name']['xml']) ){
206 if(!is_dir( WPFORO_DIR . '/wpf-admin/xml' )) wp_mkdir_p( WPFORO_DIR . '/wpf-admin/xml' );
207
208 $error = $_FILES['add_lang']['error']['xml'];
209
210 if( $error ){
211 $error = wpforo_file_upload_error($error);
212 WPF()->notice->add($error, 'error');
213 return FALSE;
214 }
215
216 $xmlfile = strtolower(sanitize_file_name($_FILES['add_lang']['name']['xml']));
217 if( move_uploaded_file(sanitize_text_field($_FILES['add_lang']['tmp_name']['xml']), WPFORO_DIR . '/wpf-admin/xml/' . $xmlfile) ){
218 if($langid = $this->xml_import($xmlfile) ){
219 delete_transient( 'wpforo_get_phrases' );
220 WPF()->notice->add('New language successfully added and changed wpforo language to new language', 'success');
221 return $langid;
222 }
223 }
224 }
225
226 WPF()->notice->add('Can\'t add new language', 'error');
227 return FALSE;
228 }
229
230 function get_languages(){
231 return WPF()->db->get_results( "SELECT * FROM `".WPF()->tables->languages."`", ARRAY_A );
232 }
233
234 function show_lang_list(){
235 $langs = $this->get_languages();
236 if(!empty($langs)){
237 foreach($langs as $lang) :
238 extract($lang, EXTR_OVERWRITE); ?>
239 <option value="<?php echo esc_attr($langid) ?>"<?php if($langid == WPF()->general_options['lang']) echo ' selected' ?>><?php echo esc_html($name) ?></option>
240 <?php
241 endforeach;
242 }
243 }
244
245 function clear_cache(){
246 WPF()->db->query("DELETE FROM ".WPF()->db->options." WHERE `option_name` LIKE '%_wpforo_get_phrases_%'");
247 }
248
249 }