| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of PhraseFilter |
| 5 |
* |
| 6 |
* @author ma_group |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
class PhraseFilter |
| 12 |
{ |
| 13 |
|
| 14 |
public $id = 0; |
| 15 |
public $phrase = ''; |
| 16 |
public $phrase_replace = ''; |
| 17 |
private static $_instance_objects = null; |
| 18 |
private static $_instance_array = null; |
| 19 |
|
| 20 |
public function __construct($data = 0) |
| 21 |
{ |
| 22 |
if (is_int($data) && $data) { |
| 23 |
$this->id = $data; |
| 24 |
$this->load($this->id); |
| 25 |
} else if (is_array($data)) { |
| 26 |
foreach ($data as $field => $value) { |
| 27 |
if (property_exists(get_class($this), $field)) { |
| 28 |
$this->$field = stripslashes(trim($value)); |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public function save() |
| 35 |
{ |
| 36 |
PhraseFilter::$_instance_objects = null; |
| 37 |
PhraseFilter::$_instance_array = null; |
| 38 |
|
| 39 |
$formula_list = PhraseFilter::load_phrase_list(false); |
| 40 |
|
| 41 |
if (!intval($this->id)) { |
| 42 |
$this->id = 1; |
| 43 |
foreach ($formula_list as $key => $formula) { |
| 44 |
if (intval($formula['id']) >= $this->id) { |
| 45 |
$this->id = intval($formula['id']) + 1; |
| 46 |
} |
| 47 |
} |
| 48 |
$formula_list[] = get_object_vars($this); |
| 49 |
} else { |
| 50 |
$boolean = false; |
| 51 |
foreach ($formula_list as $key => $formula) { |
| 52 |
if (intval($formula['id']) === intval($this->id)) { |
| 53 |
$formula_list[$key] = get_object_vars($this); |
| 54 |
$boolean = true; |
| 55 |
} |
| 56 |
} |
| 57 |
if (!$boolean) { |
| 58 |
$formula_list[] = get_object_vars($this); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
set_setting('phrase_list', array_values($formula_list)); |
| 63 |
return $this; |
| 64 |
} |
| 65 |
|
| 66 |
public function delete() |
| 67 |
{ |
| 68 |
$formula_list = PhraseFilter::load_phrase_list(false); |
| 69 |
foreach ($formula_list as $key => $formula) { |
| 70 |
if (intval($formula['id']) === intval($this->id)) { |
| 71 |
unset($formula_list[$key]); |
| 72 |
set_setting('phrase_list', array_values($formula_list)); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public static function apply_filter_to_text($text, $phrase_list = array()) |
| 78 |
{ |
| 79 |
if (empty($text)) { |
| 80 |
return $text; |
| 81 |
} |
| 82 |
|
| 83 |
if (!$phrase_list) { |
| 84 |
$phrase_list = PhraseFilter::load_phrase_list(false); |
| 85 |
} |
| 86 |
|
| 87 |
if (!empty($phrase_list)) { |
| 88 |
|
| 89 |
if (function_exists('libxml_use_internal_errors')) {libxml_use_internal_errors(true);} |
| 90 |
|
| 91 |
if ($text && class_exists('DOMDocument')) { |
| 92 |
$urls = array(); |
| 93 |
|
| 94 |
$doc = new \DOMDocument(); |
| 95 |
$doc->loadHTML($text); |
| 96 |
|
| 97 |
$elements = $doc->getElementsByTagName('a'); |
| 98 |
if ($elements->length >= 1) { |
| 99 |
foreach ($elements as $element) { |
| 100 |
$url = $element->getAttribute('href'); |
| 101 |
if (trim($url)) { |
| 102 |
$urls[Utils::buildIdFromUrl($url)] = $url; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
$elements = $doc->getElementsByTagName('img'); |
| 108 |
if ($elements->length >= 1) { |
| 109 |
foreach ($elements as $element) { |
| 110 |
$url = $element->getAttribute('src'); |
| 111 |
if (trim($url)) { |
| 112 |
$urls[Utils::buildImageIdFromPath($url)] = $url; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
$search_a = array(); |
| 118 |
$replace_a = array(); |
| 119 |
foreach ($urls as $key => $url) { |
| 120 |
$search_a[] = '/' . preg_quote($url, '/') . '/u'; |
| 121 |
$replace_a[] = $key; |
| 122 |
} |
| 123 |
$text = preg_replace($search_a, $replace_a, $text); |
| 124 |
|
| 125 |
$phrase = array(); |
| 126 |
$phrase_replace = array(); |
| 127 |
foreach ($phrase_list as $p) { |
| 128 |
$phrase[] = '/' . preg_quote($p['phrase'], '/') . '/u'; |
| 129 |
$phrase_replace[] = $p['phrase_replace']; |
| 130 |
} |
| 131 |
$text = preg_replace($phrase, $phrase_replace, $text); |
| 132 |
|
| 133 |
$search_a = array(); |
| 134 |
$replace_a = array(); |
| 135 |
foreach ($urls as $key => $url) { |
| 136 |
$search_a[] = '/' . preg_quote($key, '/') . '/u'; |
| 137 |
$replace_a[] = $url; |
| 138 |
} |
| 139 |
$text = preg_replace($search_a, $replace_a, $text); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return $text; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Apply phrase filter to wc product (title, description, attributes) |
| 148 |
* |
| 149 |
* @param mixed $product |
| 150 |
*/ |
| 151 |
public static function apply_filter_to_product($product) |
| 152 |
{ |
| 153 |
|
| 154 |
if ($product['title']) { |
| 155 |
$product['title'] = self::apply_filter_to_text($product['title']); |
| 156 |
} |
| 157 |
|
| 158 |
if ($product['description']) { |
| 159 |
$product['description'] = self::apply_filter_to_text($product['description']); |
| 160 |
} |
| 161 |
|
| 162 |
if (isset($product['attribute']) && is_array($product['attribute'])) { |
| 163 |
foreach ($product['attribute'] as &$attr) { |
| 164 |
$attr['name'] = self::apply_filter_to_text($attr['name']); |
| 165 |
if (is_array($attr['value'])) { |
| 166 |
foreach ($attr['value'] as $k => $v) { |
| 167 |
$attr['value'][$k] = self::apply_filter_to_text($attr['value'][$k]); |
| 168 |
} |
| 169 |
} else { |
| 170 |
$attr['value'] = self::apply_filter_to_text($attr['value']); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return $product; |
| 176 |
} |
| 177 |
|
| 178 |
public static function apply_filter_to_products() |
| 179 |
{ |
| 180 |
global $wpdb; |
| 181 |
|
| 182 |
$phrase_list = PhraseFilter::load_phrase_list(false); |
| 183 |
|
| 184 |
$phrase_query_array = array(); |
| 185 |
foreach ($phrase_list as $phrase) { |
| 186 |
$phrase_query_array[] = $wpdb->prepare("post_title LIKE %s OR post_content LIKE %s", array('%' . $wpdb->esc_like($phrase['phrase']) . '%', '%' . $wpdb->esc_like($phrase['phrase']) . '%')); |
| 187 |
} |
| 188 |
|
| 189 |
$sqlQueryPart = implode(" OR ", $phrase_query_array); |
| 190 |
|
| 191 |
$sql = "SELECT p.ID, p.post_title, p.post_content from {$wpdb->posts} p " . |
| 192 |
"LEFT JOIN {$wpdb->postmeta} as pm ON pm.post_id = p.ID " . |
| 193 |
"WHERE (" . $sqlQueryPart . ") AND pm.meta_key = '_a2w_external_id'"; |
| 194 |
|
| 195 |
|
| 196 |
$tmp_product_ids = $wpdb->get_results( |
| 197 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 198 |
$sql, |
| 199 |
ARRAY_N |
| 200 |
); |
| 201 |
foreach ($tmp_product_ids as $row) { |
| 202 |
$new_title = PhraseFilter::apply_filter_to_text($row[1], $phrase_list); |
| 203 |
$new_content = PhraseFilter::apply_filter_to_text($row[2], $phrase_list); |
| 204 |
if ($new_title != $row[1] || $new_content != $row[2]) { |
| 205 |
$wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} p SET p.post_title = %s, p.post_content = %s WHERE p.ID=%s", array($new_title, $new_content, $row[0]))); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Apply phrase filter to reviews keeping in database currently (review author, review text) |
| 212 |
*/ |
| 213 |
public static function apply_filter_to_reviews() |
| 214 |
{ |
| 215 |
global $wpdb; |
| 216 |
|
| 217 |
$phrase_list = PhraseFilter::load_phrase_list(false); |
| 218 |
|
| 219 |
$phrase_query_array = array(); |
| 220 |
foreach ($phrase_list as $phrase) { |
| 221 |
$phrase_query_array[] = $wpdb->prepare("comment_author LIKE %s OR comment_content LIKE %s", array('%' . $wpdb->esc_like($phrase['phrase']) . '%', '%' . $wpdb->esc_like($phrase['phrase']) . '%')); |
| 222 |
} |
| 223 |
|
| 224 |
$sqlQueryPart = implode(" OR ", $phrase_query_array); |
| 225 |
|
| 226 |
$sql = "SELECT c.comment_ID, c.comment_author, c.comment_content FROM {$wpdb->comments} c " . |
| 227 |
"LEFT JOIN {$wpdb->commentmeta} as cm ON cm.comment_id = c.comment_ID " . |
| 228 |
"WHERE (" . $sqlQueryPart . ") AND cm.meta_key = 'a2wl_country'"; |
| 229 |
|
| 230 |
$tmp_comment_ids = $wpdb->get_results( |
| 231 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 232 |
$sql, |
| 233 |
ARRAY_N |
| 234 |
); |
| 235 |
|
| 236 |
foreach ($tmp_comment_ids as $row) { |
| 237 |
$new_author = PhraseFilter::apply_filter_to_text($row[1], $phrase_list); |
| 238 |
$new_content = PhraseFilter::apply_filter_to_text($row[2], $phrase_list); |
| 239 |
if ($new_author != $row[1] || $new_content != $row[2]) { |
| 240 |
$wpdb->query($wpdb->prepare("UPDATE {$wpdb->comments} c SET c.comment_author = %s, c.comment_content = %s WHERE c.comment_ID=%d", array($new_author, $new_content, $row[0]))); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
public static function deleteAll() |
| 246 |
{ |
| 247 |
del_setting('phrase_list'); |
| 248 |
} |
| 249 |
|
| 250 |
public static function load_phrases() |
| 251 |
{ |
| 252 |
return PhraseFilter::load_phrase_list(true); |
| 253 |
} |
| 254 |
|
| 255 |
private static function load_phrase_list($asObject = true) |
| 256 |
{ |
| 257 |
$result = array(); |
| 258 |
|
| 259 |
if ($asObject && PhraseFilter::$_instance_objects) { |
| 260 |
return PhraseFilter::$_instance_objects; |
| 261 |
} else if (!$asObject && PhraseFilter::$_instance_array) { |
| 262 |
return PhraseFilter::$_instance_array; |
| 263 |
} |
| 264 |
|
| 265 |
$formula_list = get_setting('phrase_list'); |
| 266 |
$formula_list = $formula_list && is_array($formula_list) ? $formula_list : array(); |
| 267 |
|
| 268 |
if ($asObject) { |
| 269 |
foreach ($formula_list as $formula) { |
| 270 |
$fo = new PhraseFilter(); |
| 271 |
foreach ($formula as $name => $value) { |
| 272 |
if (property_exists(get_class($fo), $name)) { |
| 273 |
$fo->$name = $value; |
| 274 |
} |
| 275 |
} |
| 276 |
$result[] = $fo; |
| 277 |
PhraseFilter::$_instance_objects = $result; |
| 278 |
} |
| 279 |
} else { |
| 280 |
$result = $formula_list; |
| 281 |
PhraseFilter::$_instance_array = $result; |
| 282 |
} |
| 283 |
|
| 284 |
return $result; |
| 285 |
} |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
function phrase_apply_filter_to_text($text) |
| 290 |
{ |
| 291 |
return PhraseFilter::apply_filter_to_text($text); |
| 292 |
} |
| 293 |
|