Controls
4 years ago
Tabs
4 years ago
CFF_Builder_Customizer.php
4 years ago
CFF_Db.php
4 years ago
CFF_Feed_Builder.php
4 years ago
CFF_Feed_Saver.php
4 years ago
CFF_Feed_Saver_Manager.php
4 years ago
CFF_Post_Set.php
4 years ago
CFF_Source.php
4 years ago
CFF_Theme_CSS.php
4 years ago
CFF_Tooltip_Wizard.php
4 years ago
CFF_Db.php
981 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Custom Facebook Feed Database |
| 4 | * |
| 5 | * @since 4.0 |
| 6 | */ |
| 7 | |
| 8 | namespace CustomFacebookFeed\Builder; |
| 9 | use CustomFacebookFeed\SB_Facebook_Data_Encryption; |
| 10 | |
| 11 | class CFF_Db { |
| 12 | |
| 13 | const RESULTS_PER_PAGE = 20; |
| 14 | |
| 15 | const RESULTS_PER_CRON_UPDATE = 6; |
| 16 | |
| 17 | /** |
| 18 | * Query the cff_sources table |
| 19 | * |
| 20 | * @param array $args |
| 21 | * |
| 22 | * @return array|bool |
| 23 | * |
| 24 | * @since 4.0 |
| 25 | */ |
| 26 | public static function source_query( $args = array() ) { |
| 27 | global $wpdb; |
| 28 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 29 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 30 | |
| 31 | $page = 0; |
| 32 | if ( isset( $args['page'] ) ) { |
| 33 | $page = (int)$args['page'] - 1; |
| 34 | unset( $args['page'] ); |
| 35 | } |
| 36 | |
| 37 | $offset = max( 0, $page * self::RESULTS_PER_PAGE ); |
| 38 | |
| 39 | if ( empty( $args ) ) { |
| 40 | |
| 41 | $limit = 200; |
| 42 | $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 |
| 43 | FROM $sources_table_name s |
| 44 | LEFT JOIN $feeds_table_name f ON f.settings LIKE CONCAT('%', s.account_id, '%') |
| 45 | GROUP BY s.id, s.account_id |
| 46 | LIMIT $limit |
| 47 | OFFSET $offset; |
| 48 | "; |
| 49 | |
| 50 | $results = $wpdb->get_results( $sql, ARRAY_A ); |
| 51 | |
| 52 | if ( empty( $results ) ) { |
| 53 | return array(); |
| 54 | } |
| 55 | |
| 56 | $i = 0; |
| 57 | foreach ( $results as $result ) { |
| 58 | if ( (int)$result['used_in'] > 0 ) { |
| 59 | $account_id = "'" . esc_sql( $result['account_id'] ) . "'"; |
| 60 | $sql = "SELECT * |
| 61 | FROM $feeds_table_name |
| 62 | WHERE settings LIKE CONCAT('%', $account_id, '%') |
| 63 | GROUP BY id |
| 64 | LIMIT 100; |
| 65 | "; |
| 66 | |
| 67 | $results[ $i ]['instances'] = $wpdb->get_results( $sql, ARRAY_A ); |
| 68 | } |
| 69 | $i++; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | return $results; |
| 74 | } |
| 75 | if ( isset( $args['access_token'] ) && ! isset( $args['id'] ) ) { |
| 76 | $sql = $wpdb->prepare( " |
| 77 | SELECT * FROM $sources_table_name |
| 78 | WHERE access_token = %s; |
| 79 | ", $args['access_token'] ); |
| 80 | |
| 81 | return $wpdb->get_results( $sql, ARRAY_A ); |
| 82 | } |
| 83 | |
| 84 | if ( isset( $args['type'] ) && ! isset( $args['id'] ) ) { |
| 85 | $sql = $wpdb->prepare( " |
| 86 | SELECT * FROM $sources_table_name |
| 87 | WHERE account_type = %s; |
| 88 | ", $args['type'] ); |
| 89 | |
| 90 | return $wpdb->get_results( $sql, ARRAY_A ); |
| 91 | } |
| 92 | |
| 93 | if ( ! isset( $args['id'] ) ) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | if ( is_array( $args['id'] ) ) { |
| 98 | $id_array = array(); |
| 99 | foreach ( $args['id'] as $id ) { |
| 100 | $id_array[] = esc_sql( $id ); |
| 101 | } |
| 102 | } elseif( strpos( $args['id'], ',' ) !== false ) { |
| 103 | $id_array = explode( ',', str_replace( ' ' , '', esc_sql( $args['id'] ) ) ); |
| 104 | } |
| 105 | if ( isset( $id_array ) ) { |
| 106 | $id_string = "'" . implode( "' , '", array_map( 'esc_sql', $id_array ) ) . "'"; |
| 107 | } |
| 108 | |
| 109 | $privilege = isset( $args['privilege'] ) ? $args['privilege'] : ''; |
| 110 | |
| 111 | if ( isset( $id_string ) ) { |
| 112 | $sql = $wpdb->prepare( " |
| 113 | SELECT * FROM $sources_table_name |
| 114 | WHERE account_id IN ($id_string) |
| 115 | AND privilege = %s; |
| 116 | ", $privilege ); |
| 117 | |
| 118 | } else { |
| 119 | $sql = $wpdb->prepare( " |
| 120 | SELECT * FROM $sources_table_name |
| 121 | WHERE account_id = %s |
| 122 | AND privilege = %s; |
| 123 | ", $args['id'], $privilege ); |
| 124 | } |
| 125 | |
| 126 | return $wpdb->get_results( $sql, ARRAY_A ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Update a source (connected account) |
| 131 | * |
| 132 | * @param array $to_update |
| 133 | * @param array $where_data |
| 134 | * |
| 135 | * @return false|int |
| 136 | * |
| 137 | * @since 4.0 |
| 138 | */ |
| 139 | public static function source_update( $to_update, $where_data ) { |
| 140 | global $wpdb; |
| 141 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 142 | $encryption = new SB_Facebook_Data_Encryption(); |
| 143 | |
| 144 | $data = array(); |
| 145 | $where = array(); |
| 146 | $format = array(); |
| 147 | $where_format = array(); |
| 148 | if ( isset( $to_update['type'] ) ) { |
| 149 | $data['account_type'] = $to_update['type']; |
| 150 | $format[] = '%s'; |
| 151 | } |
| 152 | if ( isset( $to_update['privilege'] ) ) { |
| 153 | $data['privilege'] = $to_update['privilege']; |
| 154 | $format[] = '%s'; |
| 155 | } |
| 156 | if ( isset( $to_update['id'] ) ) { |
| 157 | $where['account_id'] = $to_update['id']; |
| 158 | $where_format[] = '%s'; |
| 159 | } |
| 160 | if ( isset( $to_update['access_token'] ) ) { |
| 161 | $data['access_token'] = $encryption->maybe_encrypt( $to_update['access_token'] ); |
| 162 | $format[] = '%s'; |
| 163 | } |
| 164 | if ( isset( $to_update['username'] ) ) { |
| 165 | $data['username'] = $to_update['username']; |
| 166 | $format[] = '%s'; |
| 167 | } |
| 168 | if ( isset( $to_update['info'] ) ) { |
| 169 | $data['info'] = $encryption->maybe_encrypt( $to_update['info'] ); |
| 170 | $format[] = '%s'; |
| 171 | } |
| 172 | if ( isset( $to_update['error'] ) ) { |
| 173 | $data['error'] = $to_update['error']; |
| 174 | $format[] = '%s'; |
| 175 | } |
| 176 | if ( isset( $to_update['expires'] ) ) { |
| 177 | $data['expires'] = $to_update['expires']; |
| 178 | $format[] = '%s'; |
| 179 | } |
| 180 | if ( isset( $to_update['last_updated'] ) ) { |
| 181 | $data['last_updated'] = $to_update['last_updated']; |
| 182 | $format[] = '%s'; |
| 183 | } |
| 184 | if ( isset( $to_update['author'] ) ) { |
| 185 | $data['author'] = $to_update['author']; |
| 186 | $format[] = '%d'; |
| 187 | } |
| 188 | |
| 189 | if ( isset( $where_data['type'] ) ) { |
| 190 | $where['account_type'] = $where_data['type']; |
| 191 | $where_format[] = '%s'; |
| 192 | } |
| 193 | if ( isset( $where_data['privilege'] ) ) { |
| 194 | $where['privilege'] = $where_data['privilege']; |
| 195 | $where_format[] = '%s'; |
| 196 | } |
| 197 | if ( isset( $where_data['author'] ) ) { |
| 198 | $where['author'] = $where_data['author']; |
| 199 | $where_format[] = '%d'; |
| 200 | } |
| 201 | if ( isset( $where_data['id'] ) ) { |
| 202 | $where['account_id'] = $where_data['id']; |
| 203 | $where_format[] = '%s'; |
| 204 | } |
| 205 | if ( isset( $where_data['record_id'] ) ) { |
| 206 | $where['id'] = $where_data['record_id']; |
| 207 | $where_format[] = '%d'; |
| 208 | } |
| 209 | |
| 210 | $affected = $wpdb->update( $sources_table_name, $data, $where, $format, $where_format ); |
| 211 | |
| 212 | return $affected; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * New source (connected account) data is added to the |
| 217 | * cff_sources table and the new insert ID is returned |
| 218 | * |
| 219 | * @param array $to_insert |
| 220 | * |
| 221 | * @return false|int |
| 222 | * |
| 223 | * @since 4.0 |
| 224 | */ |
| 225 | public static function source_insert( $to_insert ) { |
| 226 | global $wpdb; |
| 227 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 228 | $encryption = new SB_Facebook_Data_Encryption(); |
| 229 | |
| 230 | $data = array(); |
| 231 | $format = array(); |
| 232 | if ( isset( $to_insert['id'] ) ) { |
| 233 | $data['account_id'] = $to_insert['id']; |
| 234 | $format[] = '%s'; |
| 235 | } |
| 236 | if ( isset( $to_insert['type'] ) ) { |
| 237 | $data['account_type'] = $to_insert['type']; |
| 238 | $format[] = '%s'; |
| 239 | } else { |
| 240 | $data['account_type'] = 'page'; |
| 241 | $format[] = '%s'; |
| 242 | } |
| 243 | if ( isset( $to_insert['privilege'] ) ) { |
| 244 | $data['privilege'] = $to_insert['privilege']; |
| 245 | $format[] = '%s'; |
| 246 | } |
| 247 | if ( isset( $to_insert['access_token'] ) ) { |
| 248 | $data['access_token'] = $encryption->maybe_encrypt( $to_insert['access_token'] ); |
| 249 | $format[] = '%s'; |
| 250 | } |
| 251 | if ( isset( $to_insert['username'] ) ) { |
| 252 | $data['username'] = $to_insert['username']; |
| 253 | $format[] = '%s'; |
| 254 | } |
| 255 | if ( isset( $to_insert['info'] ) ) { |
| 256 | $data['info'] = $encryption->maybe_encrypt( $to_insert['info'] ); |
| 257 | $format[] = '%s'; |
| 258 | } |
| 259 | if ( isset( $to_insert['error'] ) ) { |
| 260 | $data['error'] = $to_insert['error']; |
| 261 | $format[] = '%s'; |
| 262 | } |
| 263 | if ( isset( $to_insert['expires'] ) ) { |
| 264 | $data['expires'] = $to_insert['expires']; |
| 265 | $format[] = '%s'; |
| 266 | } else { |
| 267 | $data['expires'] = '2100-12-30 00:00:00'; |
| 268 | $format[] = '%s'; |
| 269 | } |
| 270 | $data['last_updated'] = date( 'Y-m-d H:i:s' ); |
| 271 | $format[] = '%s'; |
| 272 | if ( isset( $to_insert['author'] ) ) { |
| 273 | $data['author'] = $to_insert['author']; |
| 274 | $format[] = '%d'; |
| 275 | } else { |
| 276 | $data['author'] = get_current_user_id(); |
| 277 | $format[] = '%d'; |
| 278 | } |
| 279 | |
| 280 | $affected = $wpdb->insert( $sources_table_name, $data, $format ); |
| 281 | |
| 282 | return $affected; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Query the to get feeds list for Elementor |
| 287 | * |
| 288 | * @return array|bool |
| 289 | * |
| 290 | * @since 4.0 |
| 291 | */ |
| 292 | public static function elementor_feeds_query() { |
| 293 | global $wpdb; |
| 294 | $feeds_elementor = []; |
| 295 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 296 | $feeds_list = $wpdb->get_results( " |
| 297 | SELECT id, feed_name FROM $feeds_table_name; |
| 298 | " |
| 299 | ); |
| 300 | if ( ! empty( $feeds_list ) ) { |
| 301 | foreach($feeds_list as $feed) { |
| 302 | $feeds_elementor[$feed->id] = $feed->feed_name; |
| 303 | } |
| 304 | } |
| 305 | return $feeds_elementor; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /** |
| 310 | * Count the cff_feeds table |
| 311 | * |
| 312 | * @param array $args |
| 313 | * |
| 314 | * @return array|bool |
| 315 | * |
| 316 | * @since 4.0 |
| 317 | */ |
| 318 | public static function feeds_count() { |
| 319 | global $wpdb; |
| 320 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 321 | $results = $wpdb->get_results( |
| 322 | "SELECT COUNT(*) AS num_entries FROM $feeds_table_name", ARRAY_A |
| 323 | ); |
| 324 | return isset($results[0]['num_entries']) ? (int)$results[0]['num_entries'] : 0; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /** |
| 329 | * Query the cff_feeds table |
| 330 | * |
| 331 | * @param array $args |
| 332 | * |
| 333 | * @return array|bool |
| 334 | * |
| 335 | * @since 4.0 |
| 336 | */ |
| 337 | public static function feeds_query( $args = array() ) { |
| 338 | global $wpdb; |
| 339 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 340 | $page = 0; |
| 341 | if ( isset( $args['page'] ) ) { |
| 342 | $page = (int)$args['page'] - 1; |
| 343 | unset( $args['page'] ); |
| 344 | } |
| 345 | |
| 346 | $offset = max( 0, $page * self::RESULTS_PER_PAGE ); |
| 347 | |
| 348 | if ( isset( $args['id'] ) ) { |
| 349 | $sql = $wpdb->prepare( " |
| 350 | SELECT * FROM $feeds_table_name |
| 351 | WHERE id = %d; |
| 352 | ", $args['id'] ); |
| 353 | } else { |
| 354 | $sql = $wpdb->prepare( " |
| 355 | SELECT * FROM $feeds_table_name |
| 356 | LIMIT %d |
| 357 | OFFSET %d;", self::RESULTS_PER_PAGE, $offset ); |
| 358 | } |
| 359 | |
| 360 | return $wpdb->get_results( $sql, ARRAY_A ); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Update feed data in the cff_feed table |
| 365 | * |
| 366 | * @param array $to_update |
| 367 | * @param array $where_data |
| 368 | * |
| 369 | * @return false|int |
| 370 | * |
| 371 | * @since 4.0 |
| 372 | */ |
| 373 | public static function feeds_update( $to_update, $where_data ) { |
| 374 | global $wpdb; |
| 375 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 376 | |
| 377 | $data = array(); |
| 378 | $where = array(); |
| 379 | $format = array(); |
| 380 | foreach ( $to_update as $single_insert ) { |
| 381 | if ( $single_insert['key'] ) { |
| 382 | $data[ $single_insert['key'] ] = $single_insert['values'][0]; |
| 383 | $format[] = '%s'; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if ( isset( $where_data['id'] ) ) { |
| 388 | $where['id'] = $where_data['id']; |
| 389 | $where_format = array( '%d' ); |
| 390 | } elseif ( isset( $where_data['feed_name'] ) ) { |
| 391 | $where['feed_name'] = $where_data['feed_name']; |
| 392 | $where_format = array( '%s' ); |
| 393 | } else { |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | $data['last_modified'] = date( 'Y-m-d H:i:s' ); |
| 398 | $format[] = '%s'; |
| 399 | |
| 400 | $affected = $wpdb->update( $feeds_table_name, $data, $where, $format, $where_format ); |
| 401 | |
| 402 | return $affected; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * New feed data is added to the cff_feeds table and |
| 407 | * the new insert ID is returned |
| 408 | * |
| 409 | * @param array $to_insert |
| 410 | * |
| 411 | * @return false|int |
| 412 | * |
| 413 | * @since 4.0 |
| 414 | */ |
| 415 | public static function feeds_insert( $to_insert ) { |
| 416 | global $wpdb; |
| 417 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 418 | |
| 419 | $data = array(); |
| 420 | $format = array(); |
| 421 | foreach ( $to_insert as $single_insert ) { |
| 422 | if ( $single_insert['key'] ) { |
| 423 | $data[ $single_insert['key'] ] = $single_insert['values'][0]; |
| 424 | $format[] = '%s'; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | $data['last_modified'] = date( 'Y-m-d H:i:s' ); |
| 429 | $format[] = '%s'; |
| 430 | |
| 431 | $data['author'] = get_current_user_id(); |
| 432 | $format[] = '%d'; |
| 433 | |
| 434 | $wpdb->insert( $feeds_table_name, $data, $format ); |
| 435 | return $wpdb->insert_id; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Query the cff_feeds table |
| 440 | * Porcess to define the name of the feed when adding new |
| 441 | * |
| 442 | * @param array $args |
| 443 | * |
| 444 | * @return array|bool |
| 445 | * |
| 446 | * @since 4.0 |
| 447 | */ |
| 448 | public static function feeds_query_name( $sourcename ) { |
| 449 | global $wpdb; |
| 450 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 451 | $sql = $wpdb->prepare( |
| 452 | "SELECT * FROM $feeds_table_name |
| 453 | WHERE feed_name LIKE %s;", |
| 454 | $wpdb->esc_like($sourcename) . '%' |
| 455 | ); |
| 456 | $count = sizeof($wpdb->get_results( $sql, ARRAY_A )); |
| 457 | return ($count == 0) ? $sourcename : $sourcename .' ('. ($count+1) .')'; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | |
| 462 | /** |
| 463 | * Query to Remove Feeds from Database |
| 464 | * |
| 465 | * @param array $args |
| 466 | * |
| 467 | * @return array|bool |
| 468 | * |
| 469 | * @since 4.0 |
| 470 | */ |
| 471 | public static function delete_feeds_query( $feed_ids_array ) { |
| 472 | global $wpdb; |
| 473 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 474 | $feed_caches_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 475 | $feed_ids_array = implode(',', array_map( 'absint', $feed_ids_array ) ); |
| 476 | $wpdb->query( |
| 477 | $wpdb->prepare( |
| 478 | "DELETE FROM $feeds_table_name WHERE id IN ($feed_ids_array)" |
| 479 | ) |
| 480 | ); |
| 481 | $wpdb->query( |
| 482 | $wpdb->prepare( |
| 483 | "DELETE FROM $feed_caches_table_name WHERE feed_id IN ($feed_ids_array)" |
| 484 | ) |
| 485 | ); |
| 486 | |
| 487 | echo \CustomFacebookFeed\CFF_Utils::cff_json_encode(CFF_Feed_Builder::get_feed_list()); |
| 488 | wp_die(); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Query to Remove Source from Database |
| 493 | * |
| 494 | * @param array $args |
| 495 | * |
| 496 | * @return array|bool |
| 497 | * |
| 498 | * @since 4.0 |
| 499 | */ |
| 500 | public static function delete_source_query( $source_id ) { |
| 501 | global $wpdb; |
| 502 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 503 | $wpdb->query( |
| 504 | $wpdb->prepare( |
| 505 | "DELETE FROM $sources_table_name WHERE id = %d; ", $source_id |
| 506 | ) |
| 507 | ); |
| 508 | |
| 509 | echo \CustomFacebookFeed\CFF_Utils::cff_json_encode(CFF_Feed_Builder::get_source_list()); |
| 510 | wp_die(); |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Query to Duplicate a Single Feed |
| 515 | * |
| 516 | * @param array $args |
| 517 | * |
| 518 | * @return array|bool |
| 519 | * |
| 520 | * @since 4.0 |
| 521 | */ |
| 522 | public static function duplicate_feed_query( $feed_id ){ |
| 523 | global $wpdb; |
| 524 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 525 | $wpdb->query( |
| 526 | $wpdb->prepare( |
| 527 | "INSERT INTO $feeds_table_name (feed_name, settings, author, status) |
| 528 | SELECT CONCAT(feed_name, ' (copy)'), settings, author, status |
| 529 | FROM $feeds_table_name |
| 530 | WHERE id = %d; ", $feed_id |
| 531 | ) |
| 532 | ); |
| 533 | |
| 534 | |
| 535 | |
| 536 | echo \CustomFacebookFeed\CFF_Utils::cff_json_encode(CFF_Feed_Builder::get_feed_list()); |
| 537 | wp_die(); |
| 538 | } |
| 539 | |
| 540 | |
| 541 | /** |
| 542 | * Get cache records in the cff_feed_caches table |
| 543 | * |
| 544 | * @param array $args |
| 545 | * |
| 546 | * @return array|object|null |
| 547 | */ |
| 548 | public static function feed_caches_query( $args ) { |
| 549 | global $wpdb; |
| 550 | $feed_cache_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 551 | |
| 552 | if ( ! isset( $args['cron_update'] ) ) { |
| 553 | $sql = " |
| 554 | SELECT * FROM $feed_cache_table_name;"; |
| 555 | } else { |
| 556 | if ( ! isset( $args['additional_batch'] ) ) { |
| 557 | $sql = $wpdb->prepare( " |
| 558 | SELECT * FROM $feed_cache_table_name |
| 559 | WHERE cron_update = 'yes' |
| 560 | ORDER BY last_updated ASC |
| 561 | LIMIT %d;", self::RESULTS_PER_CRON_UPDATE ); |
| 562 | } else { |
| 563 | $sql = $wpdb->prepare( " |
| 564 | SELECT * FROM $feed_cache_table_name |
| 565 | WHERE cron_update = 'yes' |
| 566 | AND last_updated < %s |
| 567 | ORDER BY last_updated ASC |
| 568 | LIMIT %d;", date( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ), self::RESULTS_PER_CRON_UPDATE ); |
| 569 | } |
| 570 | |
| 571 | } |
| 572 | |
| 573 | return $wpdb->get_results( $sql, ARRAY_A ); |
| 574 | } |
| 575 | |
| 576 | |
| 577 | /** |
| 578 | * Creates all database tables used in the new admin area in |
| 579 | * the 4.0 update. |
| 580 | * |
| 581 | * TODO: Add error reporting |
| 582 | * |
| 583 | * @since 4.0 |
| 584 | */ |
| 585 | public static function create_tables() { |
| 586 | if ( !function_exists( 'dbDelta' ) ) { |
| 587 | require_once ABSPATH . '/wp-admin/includes/upgrade.php'; |
| 588 | } |
| 589 | |
| 590 | global $wpdb; |
| 591 | $max_index_length = 191; |
| 592 | $charset_collate = ''; |
| 593 | if ( method_exists( $wpdb, 'get_charset_collate' ) ) { // get_charset_collate introduced in WP 3.5 |
| 594 | $charset_collate = $wpdb->get_charset_collate(); |
| 595 | } |
| 596 | |
| 597 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 598 | |
| 599 | if ( $wpdb->get_var( "show tables like '$feeds_table_name'" ) != $feeds_table_name ) { |
| 600 | $sql = " |
| 601 | CREATE TABLE $feeds_table_name ( |
| 602 | id bigint(20) unsigned NOT NULL auto_increment, |
| 603 | feed_name text NOT NULL default '', |
| 604 | feed_title text NOT NULL default '', |
| 605 | settings longtext NOT NULL default '', |
| 606 | author bigint(20) unsigned NOT NULL default '1', |
| 607 | status varchar(255) NOT NULL default '', |
| 608 | last_modified datetime NOT NULL, |
| 609 | PRIMARY KEY (id), |
| 610 | KEY author (author) |
| 611 | ) $charset_collate; |
| 612 | "; |
| 613 | //dbDelta( $sql ); |
| 614 | $wpdb->query( $sql ); |
| 615 | } |
| 616 | $error = $wpdb->last_error; |
| 617 | $query = $wpdb->last_query; |
| 618 | $had_error = false; |
| 619 | if ( $wpdb->get_var( "show tables like '$feeds_table_name'" ) != $feeds_table_name ) { |
| 620 | $had_error = true; |
| 621 | //$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>' ); |
| 622 | } |
| 623 | |
| 624 | if ( ! $had_error ) { |
| 625 | //$sb_instagram_posts_manager->remove_error( 'database_create' ); |
| 626 | } |
| 627 | |
| 628 | $feed_caches_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 629 | |
| 630 | if ( $wpdb->get_var( "show tables like '$feed_caches_table_name'" ) != $feed_caches_table_name ) { |
| 631 | $sql = " |
| 632 | CREATE TABLE " . $feed_caches_table_name . " ( |
| 633 | id bigint(20) unsigned NOT NULL auto_increment, |
| 634 | feed_id bigint(20) unsigned NOT NULL default '1', |
| 635 | cache_key varchar(255) NOT NULL default '', |
| 636 | cache_value longtext NOT NULL default '', |
| 637 | cron_update varchar(20) NOT NULL default 'yes', |
| 638 | last_updated datetime NOT NULL, |
| 639 | PRIMARY KEY (id), |
| 640 | KEY feed_id (feed_id) |
| 641 | ) $charset_collate;"; |
| 642 | //dbDelta( $sql ); |
| 643 | $wpdb->query( $sql ); |
| 644 | } |
| 645 | $error = $wpdb->last_error; |
| 646 | $query = $wpdb->last_query; |
| 647 | $had_error = false; |
| 648 | if ( $wpdb->get_var( "show tables like '$feed_caches_table_name'" ) != $feed_caches_table_name ) { |
| 649 | $had_error = true; |
| 650 | //$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>' ); |
| 651 | } |
| 652 | |
| 653 | if ( ! $had_error ) { |
| 654 | //$sb_instagram_posts_manager->remove_error( 'database_create' ); |
| 655 | } |
| 656 | |
| 657 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 658 | |
| 659 | if ( $wpdb->get_var( "show tables like '$sources_table_name'" ) != $sources_table_name ) { |
| 660 | $sql = " |
| 661 | CREATE TABLE " . $sources_table_name . " ( |
| 662 | id bigint(20) unsigned NOT NULL auto_increment, |
| 663 | account_id varchar(255) NOT NULL default '', |
| 664 | account_type varchar(255) NOT NULL default '', |
| 665 | privilege varchar(255) NOT NULL default '', |
| 666 | access_token varchar(1000) NOT NULL default '', |
| 667 | username varchar(255) NOT NULL default '', |
| 668 | info text NOT NULL default '', |
| 669 | error text NOT NULL default '', |
| 670 | expires datetime NOT NULL, |
| 671 | last_updated datetime NOT NULL, |
| 672 | author bigint(20) unsigned NOT NULL default '1', |
| 673 | PRIMARY KEY (id), |
| 674 | KEY account_type (account_type($max_index_length)), |
| 675 | KEY author (author) |
| 676 | ) $charset_collate;"; |
| 677 | //dbDelta( $sql ); |
| 678 | $wpdb->query( $sql ); |
| 679 | } |
| 680 | $error = $wpdb->last_error; |
| 681 | $query = $wpdb->last_query; |
| 682 | $had_error = false; |
| 683 | if ( $wpdb->get_var( "show tables like '$sources_table_name'" ) != $sources_table_name ) { |
| 684 | $had_error = true; |
| 685 | //$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>' ); |
| 686 | } |
| 687 | |
| 688 | if ( ! $had_error ) { |
| 689 | //$sb_instagram_posts_manager->remove_error( 'database_create' ); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Creates the sources table and adds existing sources from 3.x |
| 695 | * to it. |
| 696 | */ |
| 697 | public static function create_sources_database() { |
| 698 | if ( !function_exists( 'dbDelta' ) ) { |
| 699 | require_once ABSPATH . '/wp-admin/includes/upgrade.php'; |
| 700 | } |
| 701 | |
| 702 | global $wpdb; |
| 703 | $max_index_length = 191; |
| 704 | $charset_collate = ''; |
| 705 | if ( method_exists( $wpdb, 'get_charset_collate' ) ) { // get_charset_collate introduced in WP 3.5 |
| 706 | $charset_collate = $wpdb->get_charset_collate(); |
| 707 | } |
| 708 | |
| 709 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 710 | |
| 711 | if ( $wpdb->get_var( "show tables like '$sources_table_name'" ) != $sources_table_name ) { |
| 712 | $sql = " |
| 713 | CREATE TABLE " . $sources_table_name . " ( |
| 714 | id bigint(20) unsigned NOT NULL auto_increment, |
| 715 | account_id varchar(255) NOT NULL default '', |
| 716 | account_type varchar(255) NOT NULL default '', |
| 717 | privilege varchar(255) NOT NULL default '', |
| 718 | access_token varchar(255) NOT NULL default '', |
| 719 | username varchar(255) NOT NULL default '', |
| 720 | info text NOT NULL default '', |
| 721 | error text NOT NULL default '', |
| 722 | expires datetime NOT NULL, |
| 723 | last_updated datetime NOT NULL, |
| 724 | author bigint(20) unsigned NOT NULL default '1', |
| 725 | PRIMARY KEY (id), |
| 726 | KEY account_type (account_type($max_index_length)), |
| 727 | KEY author (author) |
| 728 | ) $charset_collate;"; |
| 729 | dbDelta( $sql ); |
| 730 | |
| 731 | $connected_accounts = (array)json_decode(stripcslashes(get_option( 'cff_connected_accounts' )), true); |
| 732 | |
| 733 | foreach ( $connected_accounts as $connected_account ) { |
| 734 | $source_data = array( |
| 735 | 'access_token' => $connected_account['accesstoken'], |
| 736 | 'id' => $connected_account['id'], |
| 737 | 'type' => $connected_account['pagetype'], |
| 738 | 'name' => $connected_account['name'], |
| 739 | 'privilege' => '', // see if events token? |
| 740 | ); |
| 741 | |
| 742 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data( $source_data['id'], $source_data['type'] === 'group', $source_data['access_token'], 0, false, '' ); |
| 743 | |
| 744 | if ( isset( $header_details->shortcode_options ) ) { |
| 745 | unset( $header_details->shortcode_options ); |
| 746 | } |
| 747 | |
| 748 | if ( isset( $header_details->name ) ) { |
| 749 | $source_data['name'] = $header_details->name; |
| 750 | } |
| 751 | $source_data['info'] = $header_details; |
| 752 | |
| 753 | // don't update or insert the access token if there is an API error |
| 754 | if ( ! isset( $header_details->error ) && ! isset( $header_details->cached_error ) ) { |
| 755 | \CustomFacebookFeed\Builder\CFF_Source::update_or_insert( $source_data ); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | $db_access_token_option = get_option( 'cff_access_token' ); |
| 760 | $db_page_access_token = get_option( 'cff_page_access_token' ); |
| 761 | $db_page_id_option = get_option( 'cff_page_id' ); |
| 762 | $db_page_type = get_option( 'cff_page_type' ); |
| 763 | |
| 764 | if ( (! empty( $db_access_token_option ) || ! empty( $db_page_access_token )) |
| 765 | && ! empty( $db_page_id_option ) ) { |
| 766 | $db_access_tokens = explode(',', str_replace( ' ', '', $db_access_token_option ) ); |
| 767 | $db_page_ids = explode(',', str_replace( ' ', '', $db_page_id_option ) ); |
| 768 | |
| 769 | $i = 0; |
| 770 | foreach ( $db_access_tokens as $db_access_token ){ |
| 771 | $db_page_id = $db_page_ids[ $i ]; |
| 772 | $source_data = array( |
| 773 | 'access_token' => ! empty( $db_page_access_token ) ? $db_page_access_token : $db_access_token, |
| 774 | 'id' => $db_page_id, |
| 775 | 'type' => $db_page_type === 'group' ? 'group' : 'page', |
| 776 | 'name' => $db_page_id, |
| 777 | 'privilege' => '', // see if events token? |
| 778 | ); |
| 779 | |
| 780 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data( $source_data['id'], $source_data['type'] === 'group', $source_data['access_token'], 0, false, '' ); |
| 781 | |
| 782 | if ( isset( $header_details->shortcode_options ) ) { |
| 783 | unset( $header_details->shortcode_options ); |
| 784 | } |
| 785 | |
| 786 | if ( isset( $header_details->name ) ) { |
| 787 | $source_data['name'] = $header_details->name; |
| 788 | } |
| 789 | $source_data['info'] = $header_details; |
| 790 | |
| 791 | // don't update or insert the access token if there is an API error |
| 792 | if ( ! isset( $header_details->error ) && ! isset( $header_details->cached_error ) ) { |
| 793 | \CustomFacebookFeed\Builder\CFF_Source::update_or_insert( $source_data ); |
| 794 | } else { |
| 795 | if ( ! empty( $db_page_access_token ) && ! empty( $db_access_token ) ) { |
| 796 | $source_data = array( |
| 797 | 'access_token' => $db_access_token, |
| 798 | 'id' => $db_page_id, |
| 799 | 'type' => $db_page_type === 'group' ? 'group' : 'page', |
| 800 | 'name' => $db_page_id, |
| 801 | 'privilege' => '', // see if events token? |
| 802 | ); |
| 803 | |
| 804 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data( $source_data['id'], $source_data['type'] === 'group', $source_data['access_token'], 0, false, '' ); |
| 805 | |
| 806 | if ( isset( $header_details->shortcode_options ) ) { |
| 807 | unset( $header_details->shortcode_options ); |
| 808 | } |
| 809 | |
| 810 | if ( isset( $header_details->name ) ) { |
| 811 | $source_data['name'] = $header_details->name; |
| 812 | } |
| 813 | $source_data['info'] = $header_details; |
| 814 | |
| 815 | if ( ! isset( $header_details->error ) && ! isset( $header_details->cached_error ) ) { |
| 816 | \CustomFacebookFeed\Builder\CFF_Source::update_or_insert( $source_data ); |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | $i++; |
| 821 | } |
| 822 | |
| 823 | } |
| 824 | |
| 825 | // how many legacy feeds? |
| 826 | $args = array( |
| 827 | 'html_location' => array( 'header', 'footer', 'sidebar', 'content', 'unknown' ), |
| 828 | 'group_by' => 'shortcode_atts', |
| 829 | 'page' => 1 |
| 830 | ); |
| 831 | $feeds_data = \CustomFacebookFeed\CFF_Feed_Locator::legacy_facebook_feed_locator_query( $args ); |
| 832 | $num_legacy = count( $feeds_data ); |
| 833 | |
| 834 | $cff_statuses_option['support_legacy_shortcode'] = false; |
| 835 | |
| 836 | if ( $num_legacy > 0 ) { |
| 837 | $options = get_option( 'cff_style_settings', array() ); |
| 838 | |
| 839 | foreach ( $feeds_data as $single_legacy_feed ) { |
| 840 | $shortcode_atts = $single_legacy_feed['shortcode_atts'] != '[""]' ? json_decode( $single_legacy_feed['shortcode_atts'], true ) : []; |
| 841 | $shortcode_atts = is_array( $shortcode_atts ) ? $shortcode_atts : array(); |
| 842 | $fb_settings = new \CustomFacebookFeed\CFF_FB_Settings( $shortcode_atts, $options ); |
| 843 | $feed_options = $fb_settings->get_settings(); |
| 844 | if ( ! empty( $feed_options['type'] ) |
| 845 | && $feed_options['type'] === 'events' |
| 846 | && ! empty( $feed_options['eventsource'] ) |
| 847 | && $feed_options['eventsource'] === 'eventspage' ) { |
| 848 | |
| 849 | $args = array( 'id' => $feed_options['id'] ); |
| 850 | $access_token = $feed_options['accesstoken']; |
| 851 | if ( strpos( $feed_options['accesstoken'], '02Sb981f26534g75h091287a46p5l63' ) !== false ) { |
| 852 | $access_token = str_replace( "02Sb981f26534g75h091287a46p5l63", "", $feed_options['accesstoken'] ); |
| 853 | } |
| 854 | $source_query = \CustomFacebookFeed\Builder\CFF_Db::source_query( $args ); |
| 855 | |
| 856 | if ( empty( $source_query ) ) { |
| 857 | |
| 858 | $source_data = array( |
| 859 | 'access_token' => $access_token, |
| 860 | 'id' => $feed_options['id'], |
| 861 | 'type' => $feed_options['pagetype'], |
| 862 | 'name' => 'Events Feed', |
| 863 | 'privilege' => '', // see if events token? |
| 864 | ); |
| 865 | |
| 866 | $header_details = \CustomFacebookFeed\CFF_Utils::fetch_header_data( $source_data['id'], $source_data['type'] === 'group', $source_data['access_token'], 0, false, '' ); |
| 867 | |
| 868 | if ( isset( $header_details->shortcode_options ) ) { |
| 869 | unset( $header_details->shortcode_options ); |
| 870 | } |
| 871 | |
| 872 | if ( isset( $header_details->name ) ) { |
| 873 | $source_data['name'] = $header_details->name; |
| 874 | } |
| 875 | $source_data['info'] = $header_details; |
| 876 | |
| 877 | $event_fields = 'id,name,attending_count,cover,start_time,end_time,event_times,timezone,place,description,ticket_uri,interested_count'; |
| 878 | $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"; |
| 879 | $events_json = \CustomFacebookFeed\CFF_Utils::cff_get_set_cache( $cff_events_json_url, $feed_options['id'], 10, 10, $shortcode_atts, false, $access_token ); |
| 880 | |
| 881 | $events_data = json_decode( $events_json, true ); |
| 882 | |
| 883 | if ( isset( $events_data['data'] ) ) { |
| 884 | $source_data['privilege'] = 'events'; |
| 885 | } |
| 886 | |
| 887 | // don't update or insert the access token if there is an API error |
| 888 | if ( ! isset( $header_details->error ) && ! isset( $header_details->cached_error ) ) { |
| 889 | \CustomFacebookFeed\Builder\CFF_Source::update_or_insert( $source_data ); |
| 890 | } |
| 891 | } else { |
| 892 | $event_fields = 'id,name,attending_count,cover,start_time,end_time,event_times,timezone,place,description,ticket_uri,interested_count'; |
| 893 | $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"; |
| 894 | $events_json = \CustomFacebookFeed\CFF_Utils::cff_get_set_cache( $cff_events_json_url, $feed_options['id'], 10, 10, $shortcode_atts, false, $access_token ); |
| 895 | |
| 896 | $events_data = json_decode( $events_json, true ); |
| 897 | |
| 898 | if ( isset( $events_data['data'] ) ) { |
| 899 | $source_data = [ |
| 900 | 'id' => $feed_options['id'], |
| 901 | 'access_token' => $access_token, |
| 902 | 'privilege' => 'events' |
| 903 | ]; |
| 904 | |
| 905 | \CustomFacebookFeed\Builder\CFF_Source::update( $source_data, false ); |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | public static function clear_cff_feed_caches() { |
| 916 | global $wpdb; |
| 917 | $feed_caches_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 918 | |
| 919 | if ( $wpdb->get_var( "show tables like '$feed_caches_table_name'" ) === $feed_caches_table_name ) { |
| 920 | $wpdb->query( "DELETE FROM $feed_caches_table_name" ); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | public static function clear_cff_sources() { |
| 925 | global $wpdb; |
| 926 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 927 | |
| 928 | if ( $wpdb->get_var( "show tables like '$sources_table_name'" ) === $sources_table_name ) { |
| 929 | $wpdb->query( "DELETE FROM $sources_table_name" ); |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | public static function reset_tables() { |
| 934 | global $wpdb; |
| 935 | $feeds_table_name = $wpdb->prefix . 'cff_feeds'; |
| 936 | |
| 937 | $wpdb->query( "DROP TABLE IF EXISTS $feeds_table_name" ); |
| 938 | $feed_caches_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 939 | |
| 940 | $wpdb->query( "DROP TABLE IF EXISTS $feed_caches_table_name" ); |
| 941 | |
| 942 | $sources_table_name = $wpdb->prefix . 'cff_sources'; |
| 943 | $wpdb->query( "DROP TABLE IF EXISTS $sources_table_name" ); |
| 944 | } |
| 945 | |
| 946 | public static function reset_db_update() { |
| 947 | update_option( 'cff_db_version', 1.9 ); |
| 948 | delete_option( 'cff_legacy_feed_settings' ); |
| 949 | delete_option( 'cff_page_slugs' ); |
| 950 | |
| 951 | // are there existing feeds to toggle legacy onboarding? |
| 952 | $cff_statuses_option = get_option( 'cff_statuses', array() ); |
| 953 | |
| 954 | if ( isset( $cff_statuses_option['legacy_onboarding'] ) ) { |
| 955 | unset( $cff_statuses_option['legacy_onboarding'] ); |
| 956 | } |
| 957 | if ( isset( $cff_statuses_option['support_legacy_shortcode'] ) ) { |
| 958 | unset( $cff_statuses_option['support_legacy_shortcode'] ); |
| 959 | } |
| 960 | |
| 961 | global $wpdb; |
| 962 | |
| 963 | $table_name = $wpdb->prefix . "usermeta"; |
| 964 | $wpdb->query( " |
| 965 | DELETE |
| 966 | FROM $table_name |
| 967 | WHERE `meta_key` LIKE ('cff\_%') |
| 968 | " ); |
| 969 | |
| 970 | |
| 971 | $feed_locator_table_name = esc_sql( $wpdb->prefix . CFF_FEED_LOCATOR ); |
| 972 | |
| 973 | $results = $wpdb->query( " |
| 974 | DELETE |
| 975 | FROM $feed_locator_table_name |
| 976 | WHERE feed_id LIKE '*%';" ); |
| 977 | |
| 978 | update_option( 'cff_statuses', $cff_statuses_option ); |
| 979 | } |
| 980 | } |
| 981 |