| 1 |
<?php |
| 2 |
|
| 3 |
class LSD_Main extends LSD_Base |
| 4 |
{ |
| 5 |
public function get_installed_db_version() |
| 6 |
{ |
| 7 |
$installed_db_ver = get_option('lsd_db_version'); |
| 8 |
if (trim($installed_db_ver) === '') $installed_db_ver = 0; |
| 9 |
|
| 10 |
return $installed_db_ver; |
| 11 |
} |
| 12 |
|
| 13 |
public function is_db_update_required(): bool |
| 14 |
{ |
| 15 |
$installed_db_ver = $this->get_installed_db_version(); |
| 16 |
$db = new LSD_db(); |
| 17 |
|
| 18 |
return version_compare($installed_db_ver, LSD_Base::DB_VERSION, '<') |
| 19 |
|| !$db->exists('lsd_data') |
| 20 |
|| !$db->exists('lsd_jobs') |
| 21 |
|| !$db->exists('lsd_ai_embeddings'); |
| 22 |
} |
| 23 |
|
| 24 |
public function geopoint($address): array |
| 25 |
{ |
| 26 |
$address = urlencode(apply_filters('lsd_geopoint_address', $address)); |
| 27 |
|
| 28 |
$user_agent = 'listdom'; |
| 29 |
if (isset($_SERVER['HTTP_USER_AGENT']) && is_string($_SERVER['HTTP_USER_AGENT'])) |
| 30 |
{ |
| 31 |
$user_agent = sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])); |
| 32 |
if ($user_agent === '') $user_agent = 'listdom'; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* OSM (OpenStreetMap) |
| 37 |
*/ |
| 38 |
|
| 39 |
$url = 'https://nominatim.openstreetmap.org/search?format=json&q=' . $address; |
| 40 |
|
| 41 |
// Getting Geo Point |
| 42 |
$JSON = LSD_Main::download($url, [ |
| 43 |
'timeout' => 10, |
| 44 |
'user-agent' => $user_agent, |
| 45 |
]); |
| 46 |
|
| 47 |
$data = json_decode($JSON, true); |
| 48 |
$place = is_array($data) ? ($data[0] ?? null) : null; |
| 49 |
|
| 50 |
if (isset($place['lat']) && $place['lat'] && isset($place['lon']) && $place['lon']) return [$place['lat'], $place['lon']]; |
| 51 |
|
| 52 |
// Listdom Settings |
| 53 |
$settings = LSD_Options::settings(); |
| 54 |
|
| 55 |
/** |
| 56 |
* Google Geocoding |
| 57 |
*/ |
| 58 |
|
| 59 |
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $address; |
| 60 |
|
| 61 |
// API Key |
| 62 |
if (isset($settings['google_geocoding_api_key']) && trim($settings['google_geocoding_api_key']) !== '') $url .= '&key=' . $settings['google_geocoding_api_key']; |
| 63 |
|
| 64 |
// Getting Geo Point |
| 65 |
$JSON = LSD_Main::download($url, [ |
| 66 |
'timeout' => 10, |
| 67 |
'user-agent' => $user_agent, |
| 68 |
]); |
| 69 |
|
| 70 |
$data = json_decode($JSON, true); |
| 71 |
$geopoint = is_array($data) && isset($data['results'][0]) ? $data['results'][0]['geometry']['location'] : null; |
| 72 |
|
| 73 |
if (isset($geopoint['lat']) && $geopoint['lat'] && isset($geopoint['lng']) && $geopoint['lng']) return [$geopoint['lat'], $geopoint['lng']]; |
| 74 |
|
| 75 |
return [0, 0]; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Returns weekdays |
| 80 |
* @return array |
| 81 |
*/ |
| 82 |
public static function get_weekdays(): array |
| 83 |
{ |
| 84 |
$week_start = LSD_Main::get_first_day_of_week(); |
| 85 |
|
| 86 |
/** |
| 87 |
* Don't change it to translate-able strings |
| 88 |
*/ |
| 89 |
$raw = [ |
| 90 |
['day' => 'Sunday', 'code' => 7, 'label' => esc_html__('Sunday', 'listdom')], |
| 91 |
['day' => 'Monday', 'code' => 1, 'label' => esc_html__('Monday', 'listdom')], |
| 92 |
['day' => 'Tuesday', 'code' => 2, 'label' => esc_html__('Tuesday', 'listdom')], |
| 93 |
['day' => 'Wednesday', 'code' => 3, 'label' => esc_html__('Wednesday', 'listdom')], |
| 94 |
['day' => 'Thursday', 'code' => 4, 'label' => esc_html__('Thursday', 'listdom')], |
| 95 |
['day' => 'Friday', 'code' => 5, 'label' => esc_html__('Friday', 'listdom')], |
| 96 |
['day' => 'Saturday', 'code' => 6, 'label' => esc_html__('Saturday', 'listdom')], |
| 97 |
]; |
| 98 |
|
| 99 |
$labels = array_slice($raw, $week_start); |
| 100 |
$rest = array_slice($raw, 0, $week_start); |
| 101 |
|
| 102 |
foreach ($rest as $label) $labels[] = $label; |
| 103 |
return apply_filters('lsd_weekdays', $labels); |
| 104 |
} |
| 105 |
|
| 106 |
public static function get_post_ids(string $key, $value): array |
| 107 |
{ |
| 108 |
$key = trim($key); |
| 109 |
if ($key === '') return []; |
| 110 |
|
| 111 |
$db = new LSD_db(); |
| 112 |
|
| 113 |
$meta_key = esc_sql($key); |
| 114 |
$meta_value = maybe_serialize($value); |
| 115 |
$meta_value = is_null($meta_value) ? '' : (string) $meta_value; |
| 116 |
$meta_value = esc_sql($meta_value); |
| 117 |
|
| 118 |
$query = "SELECT post_id FROM #__postmeta WHERE meta_key = '" . $meta_key . "'"; |
| 119 |
|
| 120 |
if ($meta_value === '') $query .= " AND (meta_value = '' OR meta_value IS NULL)"; |
| 121 |
else $query .= " AND meta_value = '" . $meta_value . "'"; |
| 122 |
|
| 123 |
$ids = $db->select($query, 'loadColumn'); |
| 124 |
if (!is_array($ids)) return []; |
| 125 |
|
| 126 |
$ids = array_map('intval', $ids); |
| 127 |
$ids = array_filter($ids); |
| 128 |
|
| 129 |
return array_values(array_unique($ids)); |
| 130 |
} |
| 131 |
|
| 132 |
public static function get_first_day_of_week() |
| 133 |
{ |
| 134 |
return get_option('start_of_week', 1); |
| 135 |
} |
| 136 |
|
| 137 |
public static function is_grecaptcha_enabled(array $settings): bool |
| 138 |
{ |
| 139 |
// Recaptcha is enabled |
| 140 |
if ( |
| 141 |
isset($settings['grecaptcha_status'], $settings['grecaptcha_sitekey'], $settings['grecaptcha_secretkey']) |
| 142 |
&& $settings['grecaptcha_status'] |
| 143 |
&& trim($settings['grecaptcha_sitekey']) |
| 144 |
&& trim($settings['grecaptcha_secretkey']) |
| 145 |
) return true; |
| 146 |
|
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
public static function grecaptcha_field($class = ''): string |
| 151 |
{ |
| 152 |
// Listdom Options |
| 153 |
$settings = LSD_Options::settings(); |
| 154 |
|
| 155 |
// Recaptcha is not enabled! |
| 156 |
if (!LSD_Main::is_grecaptcha_enabled($settings)) return ''; |
| 157 |
|
| 158 |
// Site Key |
| 159 |
$sitekey = $settings['grecaptcha_sitekey']; |
| 160 |
|
| 161 |
// Include JS Library |
| 162 |
$assets = new LSD_Assets(); |
| 163 |
$assets->grecaptcha(); |
| 164 |
|
| 165 |
return '<div class="g-recaptcha ' . sanitize_html_class($class) . '" data-sitekey="' . esc_attr($sitekey) . '"></div>'; |
| 166 |
} |
| 167 |
|
| 168 |
public static function grecaptcha_check($g_recaptcha_response, $remote_ip = null): bool |
| 169 |
{ |
| 170 |
// Listdom Options |
| 171 |
$settings = LSD_Options::settings(); |
| 172 |
|
| 173 |
// Recaptcha is not enabled! |
| 174 |
if (!LSD_Main::is_grecaptcha_enabled($settings)) return true; |
| 175 |
|
| 176 |
// Secret Key |
| 177 |
$secretkey = $settings['grecaptcha_secretkey']; |
| 178 |
|
| 179 |
// Get the IP |
| 180 |
if (is_null($remote_ip)) |
| 181 |
{ |
| 182 |
if (isset($_SERVER['REMOTE_ADDR']) && is_string($_SERVER['REMOTE_ADDR'])) $remote_ip = sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])); |
| 183 |
else $remote_ip = ''; |
| 184 |
} |
| 185 |
|
| 186 |
// Data |
| 187 |
$data = ['secret' => $secretkey, 'remoteip' => $remote_ip, 'response' => $g_recaptcha_response]; |
| 188 |
|
| 189 |
// Request |
| 190 |
$request = ''; |
| 191 |
foreach ($data as $key => $value) $request .= $key . '=' . urlencode(stripslashes($value)) . '&'; |
| 192 |
|
| 193 |
// Validating the re-captcha |
| 194 |
$JSON = LSD_Main::download('https://www.google.com/recaptcha/api/siteverify?' . trim($request, '& ')); |
| 195 |
$response = json_decode($JSON, true); |
| 196 |
|
| 197 |
if (isset($response['success']) && trim($response['success'])) return true; |
| 198 |
else return false; |
| 199 |
} |
| 200 |
|
| 201 |
public static function download($url, $args = []): string |
| 202 |
{ |
| 203 |
$args = wp_parse_args($args, [ |
| 204 |
'sslverify' => apply_filters('lsd_http_sslverify', true, $url, $args), |
| 205 |
]); |
| 206 |
|
| 207 |
$response = wp_remote_get($url, $args); |
| 208 |
if (is_wp_error($response)) |
| 209 |
{ |
| 210 |
do_action('lsd_http_request_failed', $response, $url, $args); |
| 211 |
return ''; |
| 212 |
} |
| 213 |
|
| 214 |
return wp_remote_retrieve_body($response); |
| 215 |
} |
| 216 |
|
| 217 |
public static function assign($post_id, $user_id) |
| 218 |
{ |
| 219 |
// DB Library |
| 220 |
$db = new LSD_db(); |
| 221 |
|
| 222 |
// Assign Listing |
| 223 |
return $db->q("UPDATE `#__posts` SET `post_author`=" . ((int) $user_id) . " WHERE `ID`=" . ((int) $post_id)); |
| 224 |
} |
| 225 |
|
| 226 |
public function standardize_format($date, $from, $to = 'Y-m-d'): string |
| 227 |
{ |
| 228 |
if (!trim($date)) return ''; |
| 229 |
|
| 230 |
$date = str_replace('.', '-', $date); |
| 231 |
if ($from === 'dd/mm/yyyy') |
| 232 |
{ |
| 233 |
$d = explode('/', $date); |
| 234 |
$date = $d[2] . '-' . $d[1] . '-' . $d[0]; |
| 235 |
} |
| 236 |
|
| 237 |
return lsd_date($to, LSD_Base::strtotime($date)); |
| 238 |
} |
| 239 |
|
| 240 |
public function jstophp_format($js_format = 'yyyy-mm-dd'): string |
| 241 |
{ |
| 242 |
if ($js_format === 'dd-mm-yyyy') $php_format = 'd-m-Y'; |
| 243 |
else if ($js_format === 'yyyy/mm/dd') $php_format = 'Y/m/d'; |
| 244 |
else if ($js_format === 'dd/mm/yyyy') $php_format = 'd/m/Y'; |
| 245 |
else if ($js_format === 'yyyy.mm.dd') $php_format = 'Y.m.d'; |
| 246 |
else if ($js_format === 'dd.mm.yyyy') $php_format = 'd.m.Y'; |
| 247 |
else $php_format = 'Y-m-d'; |
| 248 |
|
| 249 |
return $php_format; |
| 250 |
} |
| 251 |
|
| 252 |
public static function get_name_parts($fullname): array |
| 253 |
{ |
| 254 |
$ex = explode(' ', trim($fullname)); |
| 255 |
|
| 256 |
// First Name |
| 257 |
$first_name = $ex[0]; |
| 258 |
unset($ex[0]); |
| 259 |
|
| 260 |
// Last Name |
| 261 |
$last_name = implode(' ', $ex); |
| 262 |
|
| 263 |
return [$first_name, $last_name]; |
| 264 |
} |
| 265 |
|
| 266 |
public function is_geo_request(): bool |
| 267 |
{ |
| 268 |
$vars = array_merge($_GET, $_POST); |
| 269 |
|
| 270 |
$sf = isset($vars['sf']) && is_array($vars['sf']) ? $vars['sf'] : []; |
| 271 |
return isset($sf['min_latitude'], $sf['max_latitude'], $sf['min_longitude'], $sf['max_longitude']); |
| 272 |
} |
| 273 |
|
| 274 |
public static function get_post_id_by_meta(string $key, $value): ?int |
| 275 |
{ |
| 276 |
$db = new LSD_db(); |
| 277 |
return $db->select("SELECT `post_id` FROM `#__postmeta` WHERE `meta_key`='" . esc_sql($key) . "' AND `meta_value`='" . esc_sql($value) . "'", 'loadResult'); |
| 278 |
} |
| 279 |
|
| 280 |
public static function get_attributes() |
| 281 |
{ |
| 282 |
$attributes = get_terms([ |
| 283 |
'taxonomy' => LSD_Base::TAX_ATTRIBUTE, |
| 284 |
'hide_empty' => false, |
| 285 |
'meta_key' => 'lsd_index', |
| 286 |
'orderby' => 'meta_value_num', |
| 287 |
'order' => 'ASC', |
| 288 |
]); |
| 289 |
|
| 290 |
// Always Array |
| 291 |
return is_wp_error($attributes) ? [] : $attributes; |
| 292 |
} |
| 293 |
|
| 294 |
public static function get_attributes_details(): array |
| 295 |
{ |
| 296 |
$terms = self::get_attributes(); |
| 297 |
|
| 298 |
$attributes = []; |
| 299 |
foreach ($terms as $term) |
| 300 |
{ |
| 301 |
$field_type = get_term_meta($term->term_id, 'lsd_field_type', true); |
| 302 |
$values_raw = get_term_meta($term->term_id, 'lsd_values', true); |
| 303 |
$icon = get_term_meta($term->term_id, 'lsd_icon', true); |
| 304 |
$required = get_term_meta($term->term_id, 'lsd_required', true); |
| 305 |
$editor = get_term_meta($term->term_id, 'lsd_editor', true); |
| 306 |
$link_label = get_term_meta($term->term_id, 'lsd_link_label', true); |
| 307 |
|
| 308 |
$values_array = is_string($values_raw) |
| 309 |
? explode(',', $values_raw) |
| 310 |
: (is_array($values_raw) ? $values_raw : []); |
| 311 |
|
| 312 |
$values = array_combine($values_array, $values_array); |
| 313 |
|
| 314 |
$attributes[] = [ |
| 315 |
'id' => $term->term_id, |
| 316 |
'name' => $term->name, |
| 317 |
'slug' => $term->slug, |
| 318 |
'field_type' => $field_type, |
| 319 |
'values' => $values, |
| 320 |
'icon' => $icon, |
| 321 |
'required' => $required, |
| 322 |
'editor' => $editor, |
| 323 |
'link_label' => $link_label, |
| 324 |
]; |
| 325 |
} |
| 326 |
|
| 327 |
return $attributes; |
| 328 |
} |
| 329 |
|
| 330 |
public static function get_attr_id($id): int |
| 331 |
{ |
| 332 |
if (!is_numeric($id)) |
| 333 |
{ |
| 334 |
$term = get_term_by('slug', $id, LSD_Base::TAX_ATTRIBUTE); |
| 335 |
return $term->term_id ?? 0; |
| 336 |
} |
| 337 |
|
| 338 |
return (int) $id; |
| 339 |
} |
| 340 |
|
| 341 |
public static function get_attr_slug($slug): string |
| 342 |
{ |
| 343 |
if (is_numeric($slug)) |
| 344 |
{ |
| 345 |
$term = get_term($slug); |
| 346 |
return $term->slug ?? ''; |
| 347 |
} |
| 348 |
|
| 349 |
return $slug; |
| 350 |
} |
| 351 |
|
| 352 |
public static function get_uncategorized_category_id(): int |
| 353 |
{ |
| 354 |
$term = get_term_by('slug', 'uncategorized', LSD_Base::TAX_CATEGORY); |
| 355 |
return $term instanceof WP_Term ? $term->term_id : 0; |
| 356 |
} |
| 357 |
|
| 358 |
public static function allowed_statuses(): array |
| 359 |
{ |
| 360 |
$default = [ |
| 361 |
'publish', |
| 362 |
'trash', |
| 363 |
'pending', |
| 364 |
'draft', |
| 365 |
'future', |
| 366 |
]; |
| 367 |
|
| 368 |
$custom = array_keys((new LSD_Statuses())->statuses()); |
| 369 |
|
| 370 |
return array_merge($default, $custom); |
| 371 |
} |
| 372 |
|
| 373 |
public function is_request(string $type): bool |
| 374 |
{ |
| 375 |
switch ($type) |
| 376 |
{ |
| 377 |
case 'admin': |
| 378 |
return is_admin(); |
| 379 |
case 'ajax': |
| 380 |
return defined('DOING_AJAX'); |
| 381 |
case 'cron': |
| 382 |
return defined('DOING_CRON'); |
| 383 |
case 'frontend': |
| 384 |
return (!is_admin() || defined('DOING_AJAX')) && !defined('DOING_CRON'); |
| 385 |
default: |
| 386 |
return false; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
public static function update_geopoints(int $limit = 100) |
| 391 |
{ |
| 392 |
$listings = get_posts([ |
| 393 |
'post_type' => LSD_Base::PTYPE_LISTING, |
| 394 |
'numberposts' => $limit, |
| 395 |
]); |
| 396 |
|
| 397 |
// Listing Entity |
| 398 |
$entity = new LSD_Entity_Listing(); |
| 399 |
|
| 400 |
foreach ($listings as $listing) |
| 401 |
{ |
| 402 |
$lat = get_post_meta($listing->ID, 'lsd_latitude', true); |
| 403 |
$lng = get_post_meta($listing->ID, 'lsd_longitude', true); |
| 404 |
|
| 405 |
$entity->update_geopoint($listing->ID, $lat, $lng); |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|