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