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