PluginProbe
wpForo Forum / 1.2.0
wpForo Forum v1.2.0
3.1.7 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 All 139 releases
wpforo / wpf-includes / class-phrases.php

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

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