Controls
2 weeks ago
Tabs
2 weeks ago
CFF_Builder_Customizer.php
2 weeks ago
CFF_Db.php
2 weeks ago
CFF_Feed_Builder.php
2 weeks ago
CFF_Feed_Saver.php
2 weeks ago
CFF_Feed_Saver_Manager.php
2 weeks ago
CFF_Post_Set.php
2 weeks ago
CFF_Source.php
2 weeks ago
CFF_Theme_CSS.php
2 weeks ago
CFF_Tooltip_Wizard.php
2 weeks ago
index.php
2 weeks ago
CFF_Db.php
1245 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Custom Facebook Feed Database |
| 5 | * |
| 6 | * @since 4.0 |
| 7 | */ |
| 8 | |
| 9 | namespace CustomFacebookFeed\Builder; |
| 10 | |
| 11 | use CustomFacebookFeed\SB_Facebook_Data_Encryption; |
| 12 | use CustomFacebookFeed\CFF_Utils; |
| 13 | |
| 14 | if (!defined('ABSPATH')) { |
| 15 | exit; // Exit if accessed directly |
| 16 | } |
| 17 | |
| 18 | class CFF_Db |
| 19 | { |
| 20 | const RESULTS_PER_PAGE = 20; |
| 21 | |
| 22 | const RESULTS_PER_CRON_UPDATE = 6; |
| 23 | |
| 24 | /** |
| 25 | * Query the cff_sources table |
| 26 | * |
| 27 | * @param array $args |
| 28 | * |
| 29 | * @return array|bool |
| 30 | * |
| 31 | * @since 4.0 |
| 32 | */ |
| 33 | public static function source_query($args = array()) |
| 34 | { |
| 35 | global $wpdb; |
| 36 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 37 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 38 | $encryption = new SB_Facebook_Data_Encryption(); |
| 39 | |
| 40 | $page = 0; |
| 41 | if (isset($args['page'])) { |
| 42 | $page = (int)$args['page'] - 1; |
| 43 | unset($args['page']); |
| 44 | } |
| 45 | |
| 46 | $offset = max(0, $page * self::RESULTS_PER_PAGE); |
| 47 | |
| 48 | if (empty($args)) { |
| 49 | $limit = 200; |
| 50 | $sql = "SELECT s.id, s.account_id, s.account_type, s.privilege, s.access_token, s.username, s.info, s.error, s.expires, count(f.id) as used_in |
| 51 | FROM $sources_table_name s |
| 52 | LEFT JOIN $feeds_table_name f ON f.settings LIKE CONCAT('%', s.account_id, '%') |
| 53 | GROUP BY s.id, s.account_id |
| 54 | LIMIT $limit |
| 55 | OFFSET $offset; |
| 56 | "; |
| 57 | |
| 58 | $results = $wpdb->get_results($sql, ARRAY_A); |
| 59 | |
| 60 | if (empty($results)) { |
| 61 | return array(); |
| 62 | } |
| 63 | |
| 64 | $i = 0; |
| 65 | foreach ($results as $result) { |
| 66 | if ((int)$result['used_in'] > 0) { |
| 67 | $account_id = "'" . esc_sql($result['account_id']) . "'"; |
| 68 | $sql = "SELECT * |
| 69 | FROM $feeds_table_name |
| 70 | WHERE settings LIKE CONCAT('%', $account_id, '%') |
| 71 | GROUP BY id |
| 72 | LIMIT 100; |
| 73 | "; |
| 74 | |
| 75 | $results[ $i ]['instances'] = $wpdb->get_results($sql, ARRAY_A); |
| 76 | } |
| 77 | $i++; |
| 78 | if (isset($result['access_token']) && strpos($result['access_token'], 'IG') === false && strpos($result['access_token'], 'EA') === false && ! $encryption->decrypt($result['access_token'])) { |
| 79 | // Add the Error for global notifications |
| 80 | CFF_Source::add_report_error_option($result); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | return $results; |
| 86 | } |
| 87 | if (isset($args['access_token']) && ! isset($args['id'])) { |
| 88 | $sql = $wpdb->prepare(" |
| 89 | SELECT * FROM $sources_table_name |
| 90 | WHERE access_token = %s; |
| 91 | ", $args['access_token']); |
| 92 | |
| 93 | return $wpdb->get_results($sql, ARRAY_A); |
| 94 | } |
| 95 | |
| 96 | if (isset($args['type']) && ! isset($args['id'])) { |
| 97 | $sql = $wpdb->prepare(" |
| 98 | SELECT * FROM $sources_table_name |
| 99 | WHERE account_type = %s; |
| 100 | ", $args['type']); |
| 101 | |
| 102 | return $wpdb->get_results($sql, ARRAY_A); |
| 103 | } |
| 104 | |
| 105 | if (! isset($args['id'])) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | if (is_array($args['id'])) { |
| 110 | $id_array = array(); |
| 111 | foreach ($args['id'] as $id) { |
| 112 | $id_array[] = esc_sql($id); |
| 113 | } |
| 114 | } elseif (strpos($args['id'], ',') !== false) { |
| 115 | $id_array = explode(',', str_replace(' ', '', esc_sql($args['id']))); |
| 116 | } |
| 117 | if (isset($id_array)) { |
| 118 | $id_string = "'" . implode("' , '", array_map('esc_sql', $id_array)) . "'"; |
| 119 | } |
| 120 | |
| 121 | $privilege = isset($args['privilege']) ? $args['privilege'] : ''; |
| 122 | |
| 123 | if (isset($id_string)) { |
| 124 | $sql = $wpdb->prepare(" |
| 125 | SELECT * FROM $sources_table_name |
| 126 | WHERE account_id IN ($id_string) |
| 127 | AND privilege = %s; |
| 128 | ", $privilege); |
| 129 | } else { |
| 130 | $sql = $wpdb->prepare(" |
| 131 | SELECT * FROM $sources_table_name |
| 132 | WHERE account_id = %s |
| 133 | AND privilege = %s; |
| 134 | ", $args['id'], $privilege); |
| 135 | } |
| 136 | |
| 137 | return $wpdb->get_results($sql, ARRAY_A); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Update a source (connected account) |
| 142 | * |
| 143 | * @param array $to_update |
| 144 | * @param array $where_data |
| 145 | * |
| 146 | * @return false|int |
| 147 | * |
| 148 | * @since 4.0 |
| 149 | */ |
| 150 | public static function source_update($to_update, $where_data) |
| 151 | { |
| 152 | global $wpdb; |
| 153 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 154 | $encryption = new SB_Facebook_Data_Encryption(); |
| 155 | |
| 156 | $data = array(); |
| 157 | $where = array(); |
| 158 | $format = array(); |
| 159 | $where_format = array(); |
| 160 | if (isset($to_update['type'])) { |
| 161 | $data['account_type'] = $to_update['type']; |
| 162 | $format[] = '%s'; |
| 163 | } |
| 164 | if (isset($to_update['privilege'])) { |
| 165 | $data['privilege'] = $to_update['privilege']; |
| 166 | $format[] = '%s'; |
| 167 | } |
| 168 | if (isset($to_update['id'])) { |
| 169 | $where['account_id'] = $to_update['id']; |
| 170 | $where_format[] = '%s'; |
| 171 | } |
| 172 | if (isset($to_update['access_token'])) { |
| 173 | $data['access_token'] = $encryption->maybe_encrypt($to_update['access_token']); |
| 174 | $format[] = '%s'; |
| 175 | } |
| 176 | if (isset($to_update['username'])) { |
| 177 | $data['username'] = $to_update['username']; |
| 178 | $format[] = '%s'; |
| 179 | } |
| 180 | if (isset($to_update['info'])) { |
| 181 | $data['info'] = $encryption->maybe_encrypt($to_update['info']); |
| 182 | $format[] = '%s'; |
| 183 | } |
| 184 | if (isset($to_update['error'])) { |
| 185 | $data['error'] = $to_update['error']; |
| 186 | $format[] = '%s'; |
| 187 | } |
| 188 | if (isset($to_update['expires'])) { |
| 189 | $data['expires'] = $to_update['expires']; |
| 190 | $format[] = '%s'; |
| 191 | } |
| 192 | if (isset($to_update['last_updated'])) { |
| 193 | $data['last_updated'] = $to_update['last_updated']; |
| 194 | $format[] = '%s'; |
| 195 | } |
| 196 | if (isset($to_update['author'])) { |
| 197 | $data['author'] = $to_update['author']; |
| 198 | $format[] = '%d'; |
| 199 | } |
| 200 | |
| 201 | if (isset($where_data['type'])) { |
| 202 | $where['account_type'] = $where_data['type']; |
| 203 | $where_format[] = '%s'; |
| 204 | } |
| 205 | if (isset($where_data['privilege'])) { |
| 206 | $where['privilege'] = $where_data['privilege']; |
| 207 | $where_format[] = '%s'; |
| 208 | } |
| 209 | if (isset($where_data['author'])) { |
| 210 | $where['author'] = $where_data['author']; |
| 211 | $where_format[] = '%d'; |
| 212 | } |
| 213 | if (isset($where_data['id'])) { |
| 214 | $where['account_id'] = $where_data['id']; |
| 215 | $where_format[] = '%s'; |
| 216 | } |
| 217 | if (isset($where_data['record_id'])) { |
| 218 | $where['id'] = $where_data['record_id']; |
| 219 | $where_format[] = '%d'; |
| 220 | } |
| 221 | |
| 222 | $affected = $wpdb->update($sources_table_name, $data, $where, $format, $where_format); |
| 223 | |
| 224 | return $affected; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * New source (connected account) data is added to the |
| 229 | * cff_sources table and the new insert ID is returned |
| 230 | * |
| 231 | * @param array $to_insert |
| 232 | * |
| 233 | * @return false|int |
| 234 | * |
| 235 | * @since 4.0 |
| 236 | */ |
| 237 | public static function source_insert($to_insert) |
| 238 | { |
| 239 | global $wpdb; |
| 240 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 241 | $encryption = new SB_Facebook_Data_Encryption(); |
| 242 | |
| 243 | $data = array(); |
| 244 | $format = array(); |
| 245 | if (isset($to_insert['id'])) { |
| 246 | $data['account_id'] = $to_insert['id']; |
| 247 | $format[] = '%s'; |
| 248 | } |
| 249 | if (isset($to_insert['type'])) { |
| 250 | $data['account_type'] = $to_insert['type']; |
| 251 | $format[] = '%s'; |
| 252 | } else { |
| 253 | $data['account_type'] = 'page'; |
| 254 | $format[] = '%s'; |
| 255 | } |
| 256 | if (isset($to_insert['privilege'])) { |
| 257 | $data['privilege'] = $to_insert['privilege']; |
| 258 | $format[] = '%s'; |
| 259 | } |
| 260 | if (isset($to_insert['access_token'])) { |
| 261 | $data['access_token'] = $encryption->maybe_encrypt($to_insert['access_token']); |
| 262 | $format[] = '%s'; |
| 263 | } |
| 264 | if (isset($to_insert['username'])) { |
| 265 | $data['username'] = $to_insert['username']; |
| 266 | $format[] = '%s'; |
| 267 | } |
| 268 | if (isset($to_insert['info'])) { |
| 269 | $data['info'] = $encryption->maybe_encrypt($to_insert['info']); |
| 270 | $format[] = '%s'; |
| 271 | } |
| 272 | if (isset($to_insert['error'])) { |
| 273 | $data['error'] = $to_insert['error']; |
| 274 | $format[] = '%s'; |
| 275 | } |
| 276 | if (isset($to_insert['expires'])) { |
| 277 | $data['expires'] = $to_insert['expires']; |
| 278 | $format[] = '%s'; |
| 279 | } else { |
| 280 | $data['expires'] = '2100-12-30 00:00:00'; |
| 281 | $format[] = '%s'; |
| 282 | } |
| 283 | $data['last_updated'] = date('Y-m-d H:i:s'); |
| 284 | $format[] = '%s'; |
| 285 | if (isset($to_insert['author'])) { |
| 286 | $data['author'] = $to_insert['author']; |
| 287 | $format[] = '%d'; |
| 288 | } else { |
| 289 | $data['author'] = get_current_user_id(); |
| 290 | $format[] = '%d'; |
| 291 | } |
| 292 | |
| 293 | $affected = $wpdb->insert($sources_table_name, $data, $format); |
| 294 | |
| 295 | return $affected; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Query the to get feeds list for Elementor |
| 300 | * |
| 301 | * @return array|bool |
| 302 | * |
| 303 | * @since 4.0 |
| 304 | */ |
| 305 | public static function elementor_feeds_query() |
| 306 | { |
| 307 | global $wpdb; |
| 308 | $feeds_elementor = []; |
| 309 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 310 | $feeds_list = $wpdb->get_results(" |
| 311 | SELECT id, feed_name FROM $feeds_table_name; |
| 312 | "); |
| 313 | if (! empty($feeds_list)) { |
| 314 | foreach ($feeds_list as $feed) { |
| 315 | $feeds_elementor[$feed->id] = $feed->feed_name; |
| 316 | } |
| 317 | } |
| 318 | $feeds_elementor[0] = __('Select A Feed', 'custom-facebook-feed'); |
| 319 | return $feeds_elementor; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Query feeds list for the modern Elementor widget. |
| 324 | * |
| 325 | * Returns feed objects with id and feed_name properties, |
| 326 | * suitable for localized script data. |
| 327 | * |
| 328 | * @since 4.3.0 |
| 329 | * |
| 330 | * @return array |
| 331 | */ |
| 332 | public static function elementor_feeds_list() { |
| 333 | global $wpdb; |
| 334 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 335 | // Table name is composed from $wpdb->prefix and a hardcoded suffix (no user input); identifiers cannot be bound via $wpdb->prepare(). |
| 336 | return $wpdb->get_results( "SELECT id, feed_name FROM $feeds_table_name" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /** |
| 341 | * Count the cff_feeds table |
| 342 | * |
| 343 | * @param array $args |
| 344 | * |
| 345 | * @return array|bool |
| 346 | * |
| 347 | * @since 4.0 |
| 348 | */ |
| 349 | public static function feeds_count() |
| 350 | { |
| 351 | global $wpdb; |
| 352 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 353 | $results = $wpdb->get_results( |
| 354 | "SELECT COUNT(*) AS num_entries FROM $feeds_table_name", |
| 355 | ARRAY_A |
| 356 | ); |
| 357 | return isset($results[0]['num_entries']) ? (int)$results[0]['num_entries'] : 0; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /** |
| 362 | * Query the cff_feeds table |
| 363 | * |
| 364 | * @param array $args |
| 365 | * |
| 366 | * @return array|bool |
| 367 | * |
| 368 | * @since 4.0 |
| 369 | */ |
| 370 | public static function feeds_query($args = array()) |
| 371 | { |
| 372 | global $wpdb; |
| 373 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 374 | $page = 0; |
| 375 | if (isset($args['page'])) { |
| 376 | $page = (int)$args['page'] - 1; |
| 377 | unset($args['page']); |
| 378 | } |
| 379 | |
| 380 | $offset = max(0, $page * self::RESULTS_PER_PAGE); |
| 381 | |
| 382 | if (isset($args['id'])) { |
| 383 | $sql = $wpdb->prepare(" |
| 384 | SELECT * FROM $feeds_table_name |
| 385 | WHERE id = %d; |
| 386 | ", $args['id']); |
| 387 | } else { |
| 388 | $sql = $wpdb->prepare(" |
| 389 | SELECT * FROM $feeds_table_name |
| 390 | LIMIT %d |
| 391 | OFFSET %d;", self::RESULTS_PER_PAGE, $offset); |
| 392 | } |
| 393 | |
| 394 | return $wpdb->get_results($sql, ARRAY_A); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Update feed data in the cff_feed table |
| 399 | * |
| 400 | * @param array $to_update |
| 401 | * @param array $where_data |
| 402 | * |
| 403 | * @return false|int |
| 404 | * |
| 405 | * @since 4.0 |
| 406 | */ |
| 407 | public static function feeds_update($to_update, $where_data) |
| 408 | { |
| 409 | global $wpdb; |
| 410 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 411 | |
| 412 | $data = array(); |
| 413 | $where = array(); |
| 414 | $format = array(); |
| 415 | foreach ($to_update as $single_insert) { |
| 416 | if ($single_insert['key']) { |
| 417 | $data[ $single_insert['key'] ] = $single_insert['values'][0]; |
| 418 | $format[] = '%s'; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | if (isset($where_data['id'])) { |
| 423 | $where['id'] = $where_data['id']; |
| 424 | $where_format = array( '%d' ); |
| 425 | } elseif (isset($where_data['feed_name'])) { |
| 426 | $where['feed_name'] = $where_data['feed_name']; |
| 427 | $where_format = array( '%s' ); |
| 428 | } else { |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | $data['last_modified'] = date('Y-m-d H:i:s'); |
| 433 | $format[] = '%s'; |
| 434 | |
| 435 | $affected = $wpdb->update($feeds_table_name, $data, $where, $format, $where_format); |
| 436 | |
| 437 | return $affected; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * New feed data is added to the cff_feeds table and |
| 442 | * the new insert ID is returned |
| 443 | * |
| 444 | * @param array $to_insert |
| 445 | * |
| 446 | * @return false|int |
| 447 | * |
| 448 | * @since 4.0 |
| 449 | */ |
| 450 | public static function feeds_insert($to_insert) |
| 451 | { |
| 452 | global $wpdb; |
| 453 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 454 | |
| 455 | $data = array(); |
| 456 | $format = array(); |
| 457 | foreach ($to_insert as $single_insert) { |
| 458 | if ($single_insert['key']) { |
| 459 | $data[ $single_insert['key'] ] = $single_insert['values'][0]; |
| 460 | $format[] = '%s'; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | $data['last_modified'] = date('Y-m-d H:i:s'); |
| 465 | $format[] = '%s'; |
| 466 | |
| 467 | $data['author'] = get_current_user_id(); |
| 468 | $format[] = '%d'; |
| 469 | |
| 470 | $wpdb->insert($feeds_table_name, $data, $format); |
| 471 | return $wpdb->insert_id; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Query the cff_feeds table |
| 476 | * Porcess to define the name of the feed when adding new |
| 477 | * |
| 478 | * @param array $args |
| 479 | * |
| 480 | * @return array|bool |
| 481 | * |
| 482 | * @since 4.0 |
| 483 | */ |
| 484 | public static function feeds_query_name($sourcename) |
| 485 | { |
| 486 | global $wpdb; |
| 487 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 488 | $sql = $wpdb->prepare( |
| 489 | "SELECT * FROM $feeds_table_name |
| 490 | WHERE feed_name LIKE %s;", |
| 491 | $wpdb->esc_like($sourcename) . '%' |
| 492 | ); |
| 493 | $count = sizeof($wpdb->get_results($sql, ARRAY_A)); |
| 494 | return ($count == 0) ? $sourcename : $sourcename . ' (' . ($count + 1) . ')'; |
| 495 | } |
| 496 | |
| 497 | |
| 498 | |
| 499 | /** |
| 500 | * Query to Remove Feeds from Database |
| 501 | * |
| 502 | * @param array $args |
| 503 | * |
| 504 | * @return array|bool |
| 505 | * |
| 506 | * @since 4.0 |
| 507 | */ |
| 508 | public static function delete_feeds_query($feed_ids_array) |
| 509 | { |
| 510 | global $wpdb; |
| 511 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 512 | $feed_caches_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 513 | $sanitized_feed_ids_array = array(); |
| 514 | foreach ($feed_ids_array as $id) { |
| 515 | $sanitized_feed_ids_array[] = absint($id); |
| 516 | } |
| 517 | $feed_ids_array = implode(',', $sanitized_feed_ids_array); |
| 518 | |
| 519 | $wpdb->query( |
| 520 | $wpdb->prepare( |
| 521 | "DELETE FROM $feeds_table_name WHERE id IN ($feed_ids_array)" |
| 522 | ) |
| 523 | ); |
| 524 | $wpdb->query( |
| 525 | $wpdb->prepare( |
| 526 | "DELETE FROM $feed_caches_table_name WHERE feed_id IN ($feed_ids_array)" |
| 527 | ) |
| 528 | ); |
| 529 | |
| 530 | echo \CustomFacebookFeed\CFF_Utils::cff_json_encode(CFF_Feed_Builder::get_feed_list()); |
| 531 | wp_die(); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Query to Remove Source from Database |
| 536 | * |
| 537 | * @param array $args |
| 538 | * |
| 539 | * @return array|bool |
| 540 | * |
| 541 | * @since 4.0 |
| 542 | */ |
| 543 | public static function delete_source_query($source_id) |
| 544 | { |
| 545 | global $wpdb; |
| 546 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 547 | $wpdb->query( |
| 548 | $wpdb->prepare( |
| 549 | "DELETE FROM $sources_table_name WHERE id = %d; ", |
| 550 | $source_id |
| 551 | ) |
| 552 | ); |
| 553 | |
| 554 | echo \CustomFacebookFeed\CFF_Utils::cff_json_encode(CFF_Feed_Builder::get_source_list()); |
| 555 | wp_die(); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Query to Duplicate a Single Feed |
| 560 | * |
| 561 | * @param array $args |
| 562 | * |
| 563 | * @return array|bool |
| 564 | * |
| 565 | * @since 4.0 |
| 566 | */ |
| 567 | public static function duplicate_feed_query($feed_id) |
| 568 | { |
| 569 | global $wpdb; |
| 570 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 571 | $wpdb->query( |
| 572 | $wpdb->prepare( |
| 573 | "INSERT INTO $feeds_table_name (feed_name, settings, author, status) |
| 574 | SELECT CONCAT(feed_name, ' (copy)'), settings, author, status |
| 575 | FROM $feeds_table_name |
| 576 | WHERE id = %d; ", |
| 577 | $feed_id |
| 578 | ) |
| 579 | ); |
| 580 | |
| 581 | |
| 582 | |
| 583 | echo \CustomFacebookFeed\CFF_Utils::cff_json_encode(CFF_Feed_Builder::get_feed_list()); |
| 584 | wp_die(); |
| 585 | } |
| 586 | |
| 587 | |
| 588 | /** |
| 589 | * Get cache records in the cff_feed_caches table |
| 590 | * |
| 591 | * @param array $args |
| 592 | * |
| 593 | * @return array|object|null |
| 594 | */ |
| 595 | public static function feed_caches_query($args) |
| 596 | { |
| 597 | global $wpdb; |
| 598 | $feed_cache_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 599 | |
| 600 | if (! isset($args['cron_update'])) { |
| 601 | $sql = " |
| 602 | SELECT * FROM $feed_cache_table_name;"; |
| 603 | } else { |
| 604 | if (! isset($args['additional_batch'])) { |
| 605 | $sql = $wpdb->prepare(" |
| 606 | SELECT * FROM $feed_cache_table_name |
| 607 | WHERE cron_update = 'yes' |
| 608 | ORDER BY last_updated ASC |
| 609 | LIMIT %d;", self::RESULTS_PER_CRON_UPDATE); |
| 610 | } else { |
| 611 | $sql = $wpdb->prepare(" |
| 612 | SELECT * FROM $feed_cache_table_name |
| 613 | WHERE cron_update = 'yes' |
| 614 | AND last_updated < %s |
| 615 | ORDER BY last_updated ASC |
| 616 | LIMIT %d;", date('Y-m-d H:i:s', time() - HOUR_IN_SECONDS), self::RESULTS_PER_CRON_UPDATE); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | return $wpdb->get_results($sql, ARRAY_A); |
| 621 | } |
| 622 | |
| 623 | |
| 624 | /** |
| 625 | * Creates all database tables used in the new admin area in |
| 626 | * the 4.0 update. |
| 627 | * |
| 628 | * TODO: Add error reporting |
| 629 | * |
| 630 | * @since 4.0 |
| 631 | */ |
| 632 | public static function create_tables() |
| 633 | { |
| 634 | if (!function_exists('dbDelta')) { |
| 635 | require_once ABSPATH . '/wp-admin/includes/upgrade.php'; |
| 636 | } |
| 637 | |
| 638 | global $wpdb; |
| 639 | $max_index_length = 191; |
| 640 | $charset_collate = ''; |
| 641 | if (method_exists($wpdb, 'get_charset_collate')) { // get_charset_collate introduced in WP 3.5 |
| 642 | $charset_collate = $wpdb->get_charset_collate(); |
| 643 | } |
| 644 | |
| 645 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 646 | |
| 647 | if ($wpdb->get_var("show tables like '$feeds_table_name'") != $feeds_table_name) { |
| 648 | $sql = " |
| 649 | CREATE TABLE $feeds_table_name ( |
| 650 | id bigint(20) unsigned NOT NULL auto_increment, |
| 651 | feed_name text NOT NULL default '', |
| 652 | feed_title text NOT NULL default '', |
| 653 | settings longtext NOT NULL default '', |
| 654 | author bigint(20) unsigned NOT NULL default '1', |
| 655 | status varchar(255) NOT NULL default '', |
| 656 | last_modified datetime NOT NULL, |
| 657 | PRIMARY KEY (id), |
| 658 | KEY author (author) |
| 659 | ) $charset_collate; |
| 660 | "; |
| 661 | // dbDelta( $sql ); |
| 662 | $wpdb->query($sql); |
| 663 | } |
| 664 | $error = $wpdb->last_error; |
| 665 | $query = $wpdb->last_query; |
| 666 | $had_error = false; |
| 667 | if ($wpdb->get_var("show tables like '$feeds_table_name'") != $feeds_table_name) { |
| 668 | $had_error = true; |
| 669 | // $sb_instagram_posts_manager->add_error( 'database_create', '<strong>' . __( 'There was an error when trying to create the database tables used to locate feeds.', 'instagram-feed' ) .'</strong><br>' . $error . '<br><code>' . $query . '</code>' ); |
| 670 | } |
| 671 | |
| 672 | if (! $had_error) { |
| 673 | // $sb_instagram_posts_manager->remove_error( 'database_create' ); |
| 674 | } |
| 675 | |
| 676 | $feed_caches_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 677 | |
| 678 | if ($wpdb->get_var("show tables like '$feed_caches_table_name'") != $feed_caches_table_name) { |
| 679 | $sql = " |
| 680 | CREATE TABLE " . $feed_caches_table_name . " ( |
| 681 | id bigint(20) unsigned NOT NULL auto_increment, |
| 682 | feed_id bigint(20) unsigned NOT NULL default '1', |
| 683 | cache_key varchar(255) NOT NULL default '', |
| 684 | cache_value longtext NOT NULL default '', |
| 685 | cron_update varchar(20) NOT NULL default 'yes', |
| 686 | last_updated datetime NOT NULL, |
| 687 | PRIMARY KEY (id), |
| 688 | KEY feed_id (feed_id) |
| 689 | ) $charset_collate;"; |
| 690 | // dbDelta( $sql ); |
| 691 | $wpdb->query($sql); |
| 692 | } |
| 693 | $error = $wpdb->last_error; |
| 694 | $query = $wpdb->last_query; |
| 695 | $had_error = false; |
| 696 | if ($wpdb->get_var("show tables like '$feed_caches_table_name'") != $feed_caches_table_name) { |
| 697 | $had_error = true; |
| 698 | // $sb_instagram_posts_manager->add_error( 'database_create', '<strong>' . __( 'There was an error when trying to create the database tables used to locate feeds.', 'instagram-feed' ) .'</strong><br>' . $error . '<br><code>' . $query . '</code>' ); |
| 699 | } |
| 700 | |
| 701 | if (! $had_error) { |
| 702 | // $sb_instagram_posts_manager->remove_error( 'database_create' ); |
| 703 | } |
| 704 | |
| 705 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 706 | |
| 707 | if ($wpdb->get_var("show tables like '$sources_table_name'") != $sources_table_name) { |
| 708 | $sql = " |
| 709 | CREATE TABLE " . $sources_table_name . " ( |
| 710 | id bigint(20) unsigned NOT NULL auto_increment, |
| 711 | account_id varchar(255) NOT NULL default '', |
| 712 | account_type varchar(255) NOT NULL default '', |
| 713 | privilege varchar(255) NOT NULL default '', |
| 714 | access_token varchar(1000) NOT NULL default '', |
| 715 | username varchar(255) NOT NULL default '', |
| 716 | info text NOT NULL default '', |
| 717 | error text NOT NULL default '', |
| 718 | expires datetime NOT NULL, |
| 719 | last_updated datetime NOT NULL, |
| 720 | author bigint(20) unsigned NOT NULL default '1', |
| 721 | PRIMARY KEY (id), |
| 722 | KEY account_type (account_type($max_index_length)), |
| 723 | KEY author (author) |
| 724 | ) $charset_collate;"; |
| 725 | // dbDelta( $sql ); |
| 726 | $wpdb->query($sql); |
| 727 | } |
| 728 | $error = $wpdb->last_error; |
| 729 | $query = $wpdb->last_query; |
| 730 | $had_error = false; |
| 731 | if ($wpdb->get_var("show tables like '$sources_table_name'") != $sources_table_name) { |
| 732 | $had_error = true; |
| 733 | // $sb_instagram_posts_manager->add_error( 'database_create', '<strong>' . __( 'There was an error when trying to create the database tables used to locate feeds.', 'instagram-feed' ) .'</strong><br>' . $error . '<br><code>' . $query . '</code>' ); |
| 734 | } |
| 735 | |
| 736 | if (! $had_error) { |
| 737 | // $sb_instagram_posts_manager->remove_error( 'database_create' ); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * Creates the sources table and adds existing sources from 3.x |
| 743 | * to it. |
| 744 | */ |
| 745 | public static function create_sources_database() |
| 746 | { |
| 747 | if (!function_exists('dbDelta')) { |
| 748 | require_once ABSPATH . '/wp-admin/includes/upgrade.php'; |
| 749 | } |
| 750 | |
| 751 | global $wpdb; |
| 752 | $max_index_length = 191; |
| 753 | $charset_collate = ''; |
| 754 | if (method_exists($wpdb, 'get_charset_collate')) { // get_charset_collate introduced in WP 3.5 |
| 755 | $charset_collate = $wpdb->get_charset_collate(); |
| 756 | } |
| 757 | |
| 758 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 759 | |
| 760 | if ($wpdb->get_var("show tables like '$sources_table_name'") != $sources_table_name) { |
| 761 | $sql = " |
| 762 | CREATE TABLE " . $sources_table_name . " ( |
| 763 | id bigint(20) unsigned NOT NULL auto_increment, |
| 764 | account_id varchar(255) NOT NULL default '', |
| 765 | account_type varchar(255) NOT NULL default '', |
| 766 | privilege varchar(255) NOT NULL default '', |
| 767 | access_token varchar(255) NOT NULL default '', |
| 768 | username varchar(255) NOT NULL default '', |
| 769 | info text NOT NULL default '', |
| 770 | error text NOT NULL default '', |
| 771 | expires datetime NOT NULL, |
| 772 | last_updated datetime NOT NULL, |
| 773 | author bigint(20) unsigned NOT NULL default '1', |
| 774 | PRIMARY KEY (id), |
| 775 | KEY account_type (account_type($max_index_length)), |
| 776 | KEY author (author) |
| 777 | ) $charset_collate;"; |
| 778 | dbDelta($sql); |
| 779 | |
| 780 | $connected_accounts = (array)json_decode(stripcslashes(get_option('cff_connected_accounts')), true); |
| 781 | |
| 782 | foreach ($connected_accounts as $connected_account) { |
| 783 | $source_data = array( |
| 784 | 'access_token' => $connected_account['accesstoken'], |
| 785 | 'id' => $connected_account['id'], |
| 786 | 'type' => $connected_account['pagetype'], |
| 787 | 'name' => $connected_account['name'], |
| 788 | 'privilege' => '', // see if events token? |
| 789 | ); |
| 790 | |
| 791 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data($source_data['id'], $source_data['type'] === 'group', $source_data['access_token'], 0, false, ''); |
| 792 | |
| 793 | if (isset($header_details->shortcode_options)) { |
| 794 | unset($header_details->shortcode_options); |
| 795 | } |
| 796 | |
| 797 | if (isset($header_details->name)) { |
| 798 | $source_data['name'] = $header_details->name; |
| 799 | } |
| 800 | $source_data['info'] = $header_details; |
| 801 | |
| 802 | // don't update or insert the access token if there is an API error |
| 803 | if (! isset($header_details->error) && ! isset($header_details->cached_error)) { |
| 804 | \CustomFacebookFeed\Builder\CFF_Source::update_or_insert($source_data); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | $db_access_token_option = get_option('cff_access_token'); |
| 809 | $db_page_access_token = get_option('cff_page_access_token'); |
| 810 | $db_page_id_option = get_option('cff_page_id'); |
| 811 | $db_page_type = get_option('cff_page_type'); |
| 812 | |
| 813 | if ( |
| 814 | (! empty($db_access_token_option) || ! empty($db_page_access_token)) |
| 815 | && ! empty($db_page_id_option) |
| 816 | ) { |
| 817 | $db_access_tokens = explode(',', str_replace(' ', '', $db_access_token_option)); |
| 818 | $db_page_ids = explode(',', str_replace(' ', '', $db_page_id_option)); |
| 819 | |
| 820 | $i = 0; |
| 821 | foreach ($db_access_tokens as $db_access_token) { |
| 822 | $db_page_id = $db_page_ids[ $i ]; |
| 823 | $source_data = array( |
| 824 | 'access_token' => ! empty($db_page_access_token) ? $db_page_access_token : $db_access_token, |
| 825 | 'id' => $db_page_id, |
| 826 | 'type' => $db_page_type === 'group' ? 'group' : 'page', |
| 827 | 'name' => $db_page_id, |
| 828 | 'privilege' => '', // see if events token? |
| 829 | ); |
| 830 | |
| 831 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data($source_data['id'], $source_data['type'] === 'group', $source_data['access_token'], 0, false, ''); |
| 832 | |
| 833 | if (isset($header_details->shortcode_options)) { |
| 834 | unset($header_details->shortcode_options); |
| 835 | } |
| 836 | |
| 837 | if (isset($header_details->name)) { |
| 838 | $source_data['name'] = $header_details->name; |
| 839 | } |
| 840 | $source_data['info'] = $header_details; |
| 841 | |
| 842 | // don't update or insert the access token if there is an API error |
| 843 | if (! isset($header_details->error) && ! isset($header_details->cached_error)) { |
| 844 | \CustomFacebookFeed\Builder\CFF_Source::update_or_insert($source_data); |
| 845 | } else { |
| 846 | if (! empty($db_page_access_token) && ! empty($db_access_token)) { |
| 847 | $source_data = array( |
| 848 | 'access_token' => $db_access_token, |
| 849 | 'id' => $db_page_id, |
| 850 | 'type' => $db_page_type === 'group' ? 'group' : 'page', |
| 851 | 'name' => $db_page_id, |
| 852 | 'privilege' => '', // see if events token? |
| 853 | ); |
| 854 | |
| 855 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data($source_data['id'], $source_data['type'] === 'group', $source_data['access_token'], 0, false, ''); |
| 856 | |
| 857 | if (isset($header_details->shortcode_options)) { |
| 858 | unset($header_details->shortcode_options); |
| 859 | } |
| 860 | |
| 861 | if (isset($header_details->name)) { |
| 862 | $source_data['name'] = $header_details->name; |
| 863 | } |
| 864 | $source_data['info'] = $header_details; |
| 865 | |
| 866 | if (! isset($header_details->error) && ! isset($header_details->cached_error)) { |
| 867 | \CustomFacebookFeed\Builder\CFF_Source::update_or_insert($source_data); |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | $i++; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | // how many legacy feeds? |
| 876 | $args = array( |
| 877 | 'html_location' => array( 'header', 'footer', 'sidebar', 'content', 'unknown' ), |
| 878 | 'group_by' => 'shortcode_atts', |
| 879 | 'page' => 1 |
| 880 | ); |
| 881 | $feeds_data = \CustomFacebookFeed\CFF_Feed_Locator::legacy_facebook_feed_locator_query($args); |
| 882 | $num_legacy = count($feeds_data); |
| 883 | |
| 884 | $cff_statuses_option['support_legacy_shortcode'] = false; |
| 885 | |
| 886 | if ($num_legacy > 0) { |
| 887 | $options = get_option('cff_style_settings', array()); |
| 888 | |
| 889 | foreach ($feeds_data as $single_legacy_feed) { |
| 890 | $shortcode_atts = $single_legacy_feed['shortcode_atts'] != '[""]' ? json_decode($single_legacy_feed['shortcode_atts'], true) : []; |
| 891 | $shortcode_atts = is_array($shortcode_atts) ? $shortcode_atts : array(); |
| 892 | $fb_settings = new \CustomFacebookFeed\CFF_FB_Settings($shortcode_atts, $options); |
| 893 | $feed_options = $fb_settings->get_settings(); |
| 894 | if ( |
| 895 | ! empty($feed_options['type']) |
| 896 | && $feed_options['type'] === 'events' |
| 897 | && ! empty($feed_options['eventsource']) |
| 898 | && $feed_options['eventsource'] === 'eventspage' |
| 899 | ) { |
| 900 | $args = array( 'id' => $feed_options['id'] ); |
| 901 | $access_token = $feed_options['accesstoken']; |
| 902 | |
| 903 | $source_query = \CustomFacebookFeed\Builder\CFF_Db::source_query($args); |
| 904 | |
| 905 | if (empty($source_query)) { |
| 906 | $source_data = array( |
| 907 | 'access_token' => $access_token, |
| 908 | 'id' => $feed_options['id'], |
| 909 | 'type' => $feed_options['pagetype'], |
| 910 | 'name' => 'Events Feed', |
| 911 | 'privilege' => '', // see if events token? |
| 912 | ); |
| 913 | |
| 914 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data($source_data['id'], $source_data['type'] === 'group', $source_data['access_token'], 0, false, ''); |
| 915 | |
| 916 | if (isset($header_details->shortcode_options)) { |
| 917 | unset($header_details->shortcode_options); |
| 918 | } |
| 919 | |
| 920 | if (isset($header_details->name)) { |
| 921 | $source_data['name'] = $header_details->name; |
| 922 | } |
| 923 | $source_data['info'] = $header_details; |
| 924 | |
| 925 | $event_fields = 'id,name,attending_count,cover,start_time,end_time,event_times,timezone,place,description,ticket_uri,interested_count'; |
| 926 | $cff_events_json_url = "https://graph.facebook.com/v3.2/" . $feed_options['id'] . "/events/?fields=" . $event_fields . "&limit=1&access_token=" . $access_token . "&format=json-strings"; |
| 927 | $events_json = \CustomFacebookFeed\CFF_Utils::cff_get_set_cache($cff_events_json_url, $feed_options['id'], 10, 10, $shortcode_atts, false, $access_token); |
| 928 | |
| 929 | $events_data = json_decode($events_json, true); |
| 930 | |
| 931 | if (isset($events_data['data'])) { |
| 932 | $source_data['privilege'] = 'events'; |
| 933 | } |
| 934 | |
| 935 | // don't update or insert the access token if there is an API error |
| 936 | if (! isset($header_details->error) && ! isset($header_details->cached_error)) { |
| 937 | \CustomFacebookFeed\Builder\CFF_Source::update_or_insert($source_data); |
| 938 | } |
| 939 | } else { |
| 940 | $event_fields = 'id,name,attending_count,cover,start_time,end_time,event_times,timezone,place,description,ticket_uri,interested_count'; |
| 941 | $cff_events_json_url = "https://graph.facebook.com/v3.2/" . $feed_options['id'] . "/events/?fields=" . $event_fields . "&limit=1&access_token=" . $access_token . "&format=json-strings"; |
| 942 | $events_json = \CustomFacebookFeed\CFF_Utils::cff_get_set_cache($cff_events_json_url, $feed_options['id'], 10, 10, $shortcode_atts, false, $access_token); |
| 943 | |
| 944 | $events_data = json_decode($events_json, true); |
| 945 | |
| 946 | if (isset($events_data['data'])) { |
| 947 | $source_data = [ |
| 948 | 'id' => $feed_options['id'], |
| 949 | 'access_token' => $access_token, |
| 950 | 'privilege' => 'events' |
| 951 | ]; |
| 952 | |
| 953 | \CustomFacebookFeed\Builder\CFF_Source::update($source_data, false); |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | public static function clear_cff_feed_caches() |
| 963 | { |
| 964 | global $wpdb; |
| 965 | $feed_caches_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 966 | |
| 967 | if ($wpdb->get_var("show tables like '$feed_caches_table_name'") === $feed_caches_table_name) { |
| 968 | $wpdb->query("DELETE FROM $feed_caches_table_name"); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | public static function clear_cff_sources() |
| 973 | { |
| 974 | global $wpdb; |
| 975 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 976 | |
| 977 | if ($wpdb->get_var("show tables like '$sources_table_name'") === $sources_table_name) { |
| 978 | $wpdb->query("DELETE FROM $sources_table_name"); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | public static function reset_tables() |
| 983 | { |
| 984 | global $wpdb; |
| 985 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 986 | |
| 987 | $wpdb->query("DROP TABLE IF EXISTS $feeds_table_name"); |
| 988 | $feed_caches_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 989 | |
| 990 | $wpdb->query("DROP TABLE IF EXISTS $feed_caches_table_name"); |
| 991 | |
| 992 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 993 | $wpdb->query("DROP TABLE IF EXISTS $sources_table_name"); |
| 994 | } |
| 995 | |
| 996 | public static function reset_db_update() |
| 997 | { |
| 998 | update_option('cff_db_version', 1.9); |
| 999 | delete_option('cff_legacy_feed_settings'); |
| 1000 | delete_option('cff_page_slugs'); |
| 1001 | |
| 1002 | // are there existing feeds to toggle legacy onboarding? |
| 1003 | $cff_statuses_option = get_option('cff_statuses', array()); |
| 1004 | |
| 1005 | if (isset($cff_statuses_option['legacy_onboarding'])) { |
| 1006 | unset($cff_statuses_option['legacy_onboarding']); |
| 1007 | } |
| 1008 | if (isset($cff_statuses_option['support_legacy_shortcode'])) { |
| 1009 | unset($cff_statuses_option['support_legacy_shortcode']); |
| 1010 | } |
| 1011 | |
| 1012 | global $wpdb; |
| 1013 | |
| 1014 | $table_name = $wpdb->prefix . "usermeta"; |
| 1015 | $wpdb->query(" |
| 1016 | DELETE |
| 1017 | FROM $table_name |
| 1018 | WHERE `meta_key` LIKE ('cff\_%') |
| 1019 | "); |
| 1020 | |
| 1021 | |
| 1022 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 1023 | |
| 1024 | $results = $wpdb->query(" |
| 1025 | DELETE |
| 1026 | FROM $feed_locator_table_name |
| 1027 | WHERE feed_id LIKE '*%';"); |
| 1028 | |
| 1029 | update_option('cff_statuses', $cff_statuses_option); |
| 1030 | } |
| 1031 | |
| 1032 | |
| 1033 | /** |
| 1034 | * Query the cff_sources table |
| 1035 | * |
| 1036 | * @param array $args |
| 1037 | * |
| 1038 | * @return array|bool |
| 1039 | * |
| 1040 | * @since 4.0 |
| 1041 | */ |
| 1042 | public static function source_query_byid($args = array()) |
| 1043 | { |
| 1044 | global $wpdb; |
| 1045 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 1046 | $sql = $wpdb->prepare(" |
| 1047 | SELECT * FROM $sources_table_name |
| 1048 | WHERE id = %s OR account_id = %s |
| 1049 | ", $args['id'], $args['id']); |
| 1050 | return $wpdb->get_results($sql, ARRAY_A); |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * Query to Remove Source from Database |
| 1055 | * |
| 1056 | * @param array $args |
| 1057 | * |
| 1058 | * @return array|bool |
| 1059 | * |
| 1060 | * @since 4.0 |
| 1061 | */ |
| 1062 | public static function delete_source_by_id($source_id) |
| 1063 | { |
| 1064 | global $wpdb; |
| 1065 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 1066 | // @phpstan-ignore return.missing |
| 1067 | $wpdb->query( |
| 1068 | $wpdb->prepare( |
| 1069 | "DELETE FROM $sources_table_name WHERE id = %d; ", |
| 1070 | $source_id |
| 1071 | ) |
| 1072 | ); |
| 1073 | } |
| 1074 | |
| 1075 | /** |
| 1076 | * Check if there is a Group Source |
| 1077 | * |
| 1078 | * @param array $args |
| 1079 | * |
| 1080 | * @return int |
| 1081 | * |
| 1082 | * @since 4.0 |
| 1083 | */ |
| 1084 | public static function check_group_source() |
| 1085 | { |
| 1086 | global $wpdb; |
| 1087 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 1088 | $number = $wpdb->get_var(" |
| 1089 | SELECT count(*) FROM $sources_table_name |
| 1090 | WHERE account_type = 'group'"); |
| 1091 | return $number; |
| 1092 | } |
| 1093 | /** |
| 1094 | * Summary of get_feed_source_info |
| 1095 | * |
| 1096 | * @param mixed $feed_id |
| 1097 | * |
| 1098 | * @return array |
| 1099 | */ |
| 1100 | public static function get_feed_source_info($feed_id) |
| 1101 | { |
| 1102 | global $wpdb; |
| 1103 | $feeds_table = $wpdb->prefix . 'cff_feeds'; |
| 1104 | $sources_table = $wpdb->prefix . 'cff_sources'; |
| 1105 | $feed = $wpdb->get_row( |
| 1106 | $wpdb->prepare( |
| 1107 | "SELECT * FROM $feeds_table WHERE id = %d", |
| 1108 | $feed_id |
| 1109 | ), |
| 1110 | ARRAY_A |
| 1111 | ); |
| 1112 | |
| 1113 | if (empty($feed['settings'])) { |
| 1114 | return []; |
| 1115 | } |
| 1116 | |
| 1117 | $settings = json_decode($feed['settings'], true); |
| 1118 | |
| 1119 | // Sources can be array | so we get fist source |
| 1120 | $source_id = is_array($settings['sources']) && !empty($settings['sources'][0]) |
| 1121 | ? $settings['sources'][0] |
| 1122 | : $settings['sources']; |
| 1123 | |
| 1124 | // A feed can have No Source!! |
| 1125 | if (empty($source_id)) { |
| 1126 | return []; |
| 1127 | } |
| 1128 | |
| 1129 | $source = $wpdb->get_row( |
| 1130 | $wpdb->prepare( |
| 1131 | "SELECT * FROM $sources_table WHERE account_id = %s;", |
| 1132 | $source_id |
| 1133 | ), |
| 1134 | ARRAY_A |
| 1135 | ); |
| 1136 | |
| 1137 | if (empty($source['info'])) { |
| 1138 | return []; |
| 1139 | } |
| 1140 | |
| 1141 | $encryption = new SB_Facebook_Data_Encryption(); |
| 1142 | $data = json_decode($encryption->maybe_decrypt($source['info']), true); |
| 1143 | |
| 1144 | if (empty($data['name'])) { |
| 1145 | return []; |
| 1146 | } |
| 1147 | |
| 1148 | return [ |
| 1149 | 'id' => $data['id'], |
| 1150 | 'name' => $data['name'], |
| 1151 | 'picture' => !empty($data['picture']['data']['url']) |
| 1152 | ? $data['picture']['data']['url'] |
| 1153 | : '' |
| 1154 | ]; |
| 1155 | } |
| 1156 | |
| 1157 | /** |
| 1158 | * Summary of get_posts_by_ids |
| 1159 | * |
| 1160 | * @param mixed $post_ids |
| 1161 | * |
| 1162 | * @return array |
| 1163 | */ |
| 1164 | public static function get_posts_by_ids($post_ids) |
| 1165 | { |
| 1166 | global $wpdb; |
| 1167 | $posts_table = $wpdb->prefix . 'cff_posts'; |
| 1168 | |
| 1169 | if (empty($post_ids)) { |
| 1170 | return []; |
| 1171 | } |
| 1172 | |
| 1173 | $post_ids = esc_sql($post_ids); |
| 1174 | $sql_ids = "'" . implode('\', \'', $post_ids) . "'"; |
| 1175 | |
| 1176 | $posts = $wpdb->get_results( |
| 1177 | "SELECT * FROM $posts_table |
| 1178 | WHERE facebook_id IN ($sql_ids)", |
| 1179 | ARRAY_A |
| 1180 | ); |
| 1181 | $results = []; |
| 1182 | |
| 1183 | $encryption = new SB_Facebook_Data_Encryption(); |
| 1184 | |
| 1185 | foreach ($posts as $post) { |
| 1186 | $transformed = self::transform_single_post_schema($post, $encryption); |
| 1187 | if (!empty($transformed)) { |
| 1188 | array_push($results, $transformed); |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | return $results; |
| 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * Summary of transform_single_post_schema |
| 1197 | * |
| 1198 | * @param mixed $post |
| 1199 | * @param mixed $encryption |
| 1200 | * |
| 1201 | * @return array |
| 1202 | */ |
| 1203 | public static function transform_single_post_schema($post, $encryption) |
| 1204 | { |
| 1205 | $data = CFF_Utils::parse_json_data( |
| 1206 | $encryption->maybe_decrypt($post['json_data']) |
| 1207 | ); |
| 1208 | if (empty($data) || empty($data['id'])) { |
| 1209 | return []; |
| 1210 | } |
| 1211 | |
| 1212 | return [ |
| 1213 | 'plugin' => [ |
| 1214 | 'slug' => 'facebook', |
| 1215 | ], |
| 1216 | 'feed_post_id' => $data['id'], |
| 1217 | 'text' => CFF_Utils::get_post_text($data), |
| 1218 | 'imageSrc' => '', |
| 1219 | 'updated_time_ago' => !empty($data['created_time']) ? $data['created_time'] : '', |
| 1220 | 'profile' => [ |
| 1221 | 'label' => !empty($data['from']['name']) ? $data['from']['name'] : '', |
| 1222 | 'url' => !empty($data['from']['link']) ? $data['from']['link'] : '', |
| 1223 | 'id' => !empty($data['from']['id']) ? $data['from']['id'] : '', |
| 1224 | ] |
| 1225 | ]; |
| 1226 | } |
| 1227 | |
| 1228 | /** |
| 1229 | * Query the cff_feeds table |
| 1230 | * |
| 1231 | * @param array $args |
| 1232 | * |
| 1233 | * @return array|bool |
| 1234 | * |
| 1235 | * @since 4.0 |
| 1236 | */ |
| 1237 | public static function all_feeds_query() |
| 1238 | { |
| 1239 | global $wpdb; |
| 1240 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 1241 | $sql = "SELECT * FROM $feeds_table_name"; |
| 1242 | return $wpdb->get_results($sql, ARRAY_A); |
| 1243 | } |
| 1244 | } |
| 1245 |