Admin
2 weeks ago
Builder
2 weeks ago
Helpers
2 weeks ago
Integrations
2 weeks ago
UsageTracking
2 weeks ago
CFF_Autolink.php
2 weeks ago
CFF_Blocks.php
2 weeks ago
CFF_Cache.php
2 weeks ago
CFF_Education.php
2 weeks ago
CFF_Elementor_Base.php
2 weeks ago
CFF_Elementor_Widget.php
2 weeks ago
CFF_Error_Reporter.php
2 weeks ago
CFF_FB_Settings.php
2 weeks ago
CFF_Feed_Elementor_Control.php
2 weeks ago
CFF_Feed_Locator.php
2 weeks ago
CFF_Feed_Pro.php
2 weeks ago
CFF_GDPR_Integrations.php
2 weeks ago
CFF_Group_Posts.php
2 weeks ago
CFF_HTTP_Request.php
2 weeks ago
CFF_Oembed.php
2 weeks ago
CFF_Parse.php
2 weeks ago
CFF_Resizer.php
2 weeks ago
CFF_Response.php
2 weeks ago
CFF_Shortcode.php
2 weeks ago
CFF_Shortcode_Display.php
2 weeks ago
CFF_SiteHealth.php
2 weeks ago
CFF_Utils.php
2 weeks ago
CFF_View.php
2 weeks ago
Custom_Facebook_Feed.php
2 weeks ago
Email_Notification.php
2 weeks ago
Platform_Data.php
2 weeks ago
SB_Facebook_Data_Encryption.php
2 weeks ago
SB_Facebook_Data_Manager.php
2 weeks ago
index.php
2 weeks ago
CFF_Feed_Locator.php
708 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class CFF_Feed_Locator |
| 5 | * |
| 6 | * @since X.X.X |
| 7 | */ |
| 8 | |
| 9 | namespace CustomFacebookFeed; |
| 10 | |
| 11 | use CustomFacebookFeed\Builder\CFF_Db; |
| 12 | |
| 13 | if (! defined('ABSPATH')) { |
| 14 | exit; // Exit if accessed directly |
| 15 | } |
| 16 | |
| 17 | class CFF_Feed_Locator |
| 18 | { |
| 19 | private $feed_details; |
| 20 | |
| 21 | private $expiration_time; |
| 22 | |
| 23 | private $matching_entries; |
| 24 | |
| 25 | public function __construct($feed_details) |
| 26 | { |
| 27 | // for non-legacy feeds. A simple ID based on the CFF_Feeds table will be more useful |
| 28 | if (isset($feed_details['atts']) && ! empty($feed_details['atts']['feed'])) { |
| 29 | $feed_details['feed_id'] = '*' . $feed_details['atts']['feed']; |
| 30 | } |
| 31 | $this->feed_details = $feed_details; |
| 32 | |
| 33 | $this->matching_entries = array(); |
| 34 | |
| 35 | $this->expiration_time = time() - 2 * WEEK_IN_SECONDS; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Returns records that match the post ID and feed ID |
| 40 | * of the feed being located |
| 41 | * |
| 42 | * @return array |
| 43 | * |
| 44 | * @since X.X.X |
| 45 | */ |
| 46 | public function retrieve_matching_entries() |
| 47 | { |
| 48 | global $wpdb; |
| 49 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 50 | |
| 51 | $results = $wpdb->get_results($wpdb->prepare(" |
| 52 | SELECT * |
| 53 | FROM $feed_locator_table_name |
| 54 | WHERE post_id = %d |
| 55 | AND feed_id = %s", $this->feed_details['location']['post_id'], $this->feed_details['feed_id']), ARRAY_A); |
| 56 | |
| 57 | return $results; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add feed being located to the database |
| 62 | * |
| 63 | * @since X.X.X |
| 64 | */ |
| 65 | public function insert_entry() |
| 66 | { |
| 67 | global $wpdb; |
| 68 | |
| 69 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 70 | $two_minutes_ago = date('Y-m-d H:i:s', time() - 120); |
| 71 | |
| 72 | $results_recent_entries = $wpdb->get_results($wpdb->prepare(" |
| 73 | SELECT COUNT(*) AS num_entries |
| 74 | FROM $feed_locator_table_name |
| 75 | WHERE last_update > %s; |
| 76 | ", $two_minutes_ago), ARRAY_A); |
| 77 | |
| 78 | // Only allow 5 new entries within 5 minutes |
| 79 | if (isset($results_recent_entries[0]['num_entries']) && (int)$results_recent_entries[0]['num_entries'] > 5) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // Only allow 1000 total entries |
| 84 | $results_total_entries = $wpdb->get_results(" |
| 85 | SELECT COUNT(*) AS num_entries |
| 86 | FROM $feed_locator_table_name", ARRAY_A); |
| 87 | if (isset($results_total_entries[0]['num_entries']) && (int)$results_total_entries[0]['num_entries'] > 1000) { |
| 88 | $this->delete_oldest_entry(); |
| 89 | } |
| 90 | |
| 91 | $affected = $wpdb->query($wpdb->prepare( |
| 92 | "INSERT INTO $feed_locator_table_name |
| 93 | (feed_id, |
| 94 | post_id, |
| 95 | html_location, |
| 96 | shortcode_atts, |
| 97 | last_update) |
| 98 | VALUES ( |
| 99 | %s, |
| 100 | %d, |
| 101 | %s, |
| 102 | %s, |
| 103 | %s);", |
| 104 | $this->feed_details['feed_id'], |
| 105 | $this->feed_details['location']['post_id'], |
| 106 | $this->feed_details['location']['html'], |
| 107 | CFF_Utils::cff_json_encode($this->feed_details['atts']), |
| 108 | date('Y-m-d H:i:s') |
| 109 | )); |
| 110 | } |
| 111 | public function delete_oldest_entry() |
| 112 | { |
| 113 | global $wpdb; |
| 114 | |
| 115 | $feed_locator_table_name = esc_sql($wpdb->prefix . SBI_INSTAGRAM_FEED_LOCATOR); |
| 116 | |
| 117 | $affected = $wpdb->query( |
| 118 | "DELETE FROM $feed_locator_table_name |
| 119 | ORDER BY last_update ASC |
| 120 | LIMIT 1;" |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Update a record based on the existing "id" column. Location can change |
| 126 | * from "unknown" to one of footer, content, header, or sidebar. |
| 127 | * |
| 128 | * @param $id |
| 129 | * @param $location |
| 130 | * |
| 131 | * @since X.X.X |
| 132 | */ |
| 133 | public function update_entry($id, $location) |
| 134 | { |
| 135 | global $wpdb; |
| 136 | |
| 137 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 138 | |
| 139 | $query = $wpdb->query($wpdb->prepare(" |
| 140 | UPDATE $feed_locator_table_name |
| 141 | SET last_update = %s, html_location = %s |
| 142 | WHERE id = %d;", date('Y-m-d H:i:s'), $location, $id)); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Processes a feed being located based on whether or not the record |
| 147 | * exists as well as whether or not an unknown location needs to be |
| 148 | * updated. |
| 149 | * |
| 150 | * @since X.X.X |
| 151 | */ |
| 152 | public function add_or_update_entry() |
| 153 | { |
| 154 | if (empty($this->feed_details['feed_id'])) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | $this->matching_entries = $this->retrieve_matching_entries(); |
| 159 | |
| 160 | if (empty($this->matching_entries)) { |
| 161 | $this->insert_entry(); |
| 162 | } else { |
| 163 | $matching_indices = array(); |
| 164 | $matched_location = false; |
| 165 | $non_unknown_match = false; |
| 166 | $unknown_match = false; |
| 167 | |
| 168 | foreach ($this->matching_entries as $index => $matching_entry) { |
| 169 | $shortcode_atts = json_decode($matching_entry['shortcode_atts'], true); |
| 170 | $shortcode_atts = ( $shortcode_atts == null ) ? [] : $shortcode_atts; |
| 171 | $atts = is_array($this->feed_details['atts']) ? $this->feed_details['atts'] : array(); |
| 172 | $atts_diff = array_diff($shortcode_atts, $atts); // determines if the shortcode settings match the shortcode settings of an existing feed |
| 173 | |
| 174 | if (empty($atts_diff)) { |
| 175 | $matching_indices[] = $matching_entry['id']; |
| 176 | if ($matching_entry['html_location'] === $this->feed_details['location']['html']) { |
| 177 | $matched_location = $index; |
| 178 | $this->update_entry($matching_entry['id'], $matching_entry['html_location']); |
| 179 | } |
| 180 | if ($matching_entry['html_location'] !== 'unknown') { |
| 181 | $non_unknown_match = $index; |
| 182 | } else { |
| 183 | $unknown_match = $index; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (false === $matched_location) { |
| 189 | // if there is no matched location, there is only one feed on the page, and the feed being checked has an unknown location, update the known location |
| 190 | if ( |
| 191 | count($matching_indices) === 1 |
| 192 | && $this->feed_details['location']['html'] === 'unknown' |
| 193 | && false !== $non_unknown_match |
| 194 | ) { |
| 195 | $this->update_entry($this->matching_entries[ $non_unknown_match ]['id'], $this->matching_entries[ $non_unknown_match ]['html_location']); |
| 196 | } else { |
| 197 | if ( |
| 198 | $this->feed_details['location']['html'] !== 'unknown' |
| 199 | && false !== $unknown_match |
| 200 | ) { |
| 201 | $this->update_entry($this->matching_entries[ $unknown_match ]['id'], $this->feed_details['location']['html']); |
| 202 | } else { |
| 203 | $this->insert_entry(); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Queries the locator table for feeds by feed_id |
| 212 | * |
| 213 | * @param $args |
| 214 | * |
| 215 | * @return array|object|null |
| 216 | * |
| 217 | * @since 4.0 |
| 218 | */ |
| 219 | public static function facebook_feed_locator_query($args) |
| 220 | { |
| 221 | global $wpdb; |
| 222 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 223 | |
| 224 | $group_by = ''; |
| 225 | if (isset($args['group_by'])) { |
| 226 | $group_by = "GROUP BY " . esc_sql($args['group_by']); |
| 227 | } |
| 228 | |
| 229 | $location_string = 'content'; |
| 230 | if (isset($args['html_location'])) { |
| 231 | $locations = array_map('esc_sql', $args['html_location']); |
| 232 | $location_string = implode("', '", $locations); |
| 233 | } |
| 234 | |
| 235 | $page = 0; |
| 236 | if (isset($args['page'])) { |
| 237 | $page = (int)$args['page'] - 1; |
| 238 | unset($args['page']); |
| 239 | } |
| 240 | |
| 241 | $offset = max(0, $page * CFF_Db::RESULTS_PER_PAGE); |
| 242 | |
| 243 | if (isset($args['shortcode_atts'])) { |
| 244 | $results = $wpdb->get_results($wpdb->prepare(" |
| 245 | SELECT * |
| 246 | FROM $feed_locator_table_name |
| 247 | WHERE shortcode_atts = %s |
| 248 | AND html_location IN ( '$location_string' ) |
| 249 | $group_by |
| 250 | LIMIT %d |
| 251 | OFFSET %d;", $args['shortcode_atts'], CFF_Db::RESULTS_PER_PAGE, $offset), ARRAY_A); |
| 252 | } else { |
| 253 | $results = $wpdb->get_results($wpdb->prepare(" |
| 254 | SELECT * |
| 255 | FROM $feed_locator_table_name |
| 256 | WHERE feed_id = %s |
| 257 | AND html_location IN ( '$location_string' ) |
| 258 | $group_by |
| 259 | LIMIT %d |
| 260 | OFFSET %d;", $args['feed_id'], CFF_Db::RESULTS_PER_PAGE, $offset), ARRAY_A); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | return $results; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Queries all legacy feeds that have been located |
| 269 | * |
| 270 | * @param $args |
| 271 | * |
| 272 | * @return array|object|null |
| 273 | * |
| 274 | * @since 4.0 |
| 275 | */ |
| 276 | public static function legacy_facebook_feed_locator_query($args) |
| 277 | { |
| 278 | global $wpdb; |
| 279 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 280 | |
| 281 | $group_by = ''; |
| 282 | if (isset($args['group_by'])) { |
| 283 | $group_by = "GROUP BY " . esc_sql($args['group_by']); |
| 284 | } |
| 285 | |
| 286 | $location_string = 'content'; |
| 287 | if (isset($args['html_location'])) { |
| 288 | $locations = array_map('esc_sql', $args['html_location']); |
| 289 | $location_string = implode("', '", $locations); |
| 290 | } |
| 291 | |
| 292 | $page = 0; |
| 293 | if (isset($args['page'])) { |
| 294 | $page = (int)$args['page'] - 1; |
| 295 | unset($args['page']); |
| 296 | } |
| 297 | |
| 298 | $offset = max(0, $page * CFF_Db::RESULTS_PER_PAGE); |
| 299 | $limit = CFF_Db::RESULTS_PER_PAGE; |
| 300 | |
| 301 | $results = $wpdb->get_results(" |
| 302 | SELECT * |
| 303 | FROM $feed_locator_table_name |
| 304 | WHERE feed_id NOT LIKE '*%' |
| 305 | AND html_location IN ( '$location_string' ) |
| 306 | $group_by |
| 307 | LIMIT $limit |
| 308 | OFFSET $offset;", ARRAY_A); |
| 309 | |
| 310 | return $results; |
| 311 | } |
| 312 | |
| 313 | public static function update_legacy_to_builder($args) |
| 314 | { |
| 315 | global $wpdb; |
| 316 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 317 | |
| 318 | $data = array( |
| 319 | 'feed_id' => '*' . $args['new_feed_id'], |
| 320 | 'shortcode_atts' => '{"feed":"' . $args['new_feed_id'] . '"}' |
| 321 | ); |
| 322 | |
| 323 | $affected = $wpdb->query( |
| 324 | $wpdb->prepare( |
| 325 | "UPDATE $feed_locator_table_name |
| 326 | SET feed_id = %s, shortcode_atts = %s", |
| 327 | $data['feed_id'], |
| 328 | $data['shortcode_atts'] |
| 329 | ) |
| 330 | ); |
| 331 | |
| 332 | return $affected; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Simple count of rows based on args |
| 337 | * |
| 338 | * @param array $args |
| 339 | * |
| 340 | * @return int |
| 341 | * |
| 342 | * @since 4.0 |
| 343 | */ |
| 344 | public static function count($args) |
| 345 | { |
| 346 | global $wpdb; |
| 347 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 348 | |
| 349 | if (isset($args['shortcode_atts'])) { |
| 350 | $results = $wpdb->get_results($wpdb->prepare(" |
| 351 | SELECT COUNT(*) AS num_entries |
| 352 | FROM $feed_locator_table_name |
| 353 | WHERE shortcode_atts = %s |
| 354 | ", $args['shortcode_atts']), ARRAY_A); |
| 355 | } else { |
| 356 | $results = $wpdb->get_results($wpdb->prepare(" |
| 357 | SELECT COUNT(*) AS num_entries |
| 358 | FROM $feed_locator_table_name |
| 359 | WHERE feed_id = %s |
| 360 | ", $args['feed_id']), ARRAY_A); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | if (isset($results[0]['num_entries'])) { |
| 365 | return (int)$results[0]['num_entries']; |
| 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Old feeds are only detected once a day to keep load on the server low. |
| 373 | * |
| 374 | * @return bool |
| 375 | * |
| 376 | * @since X.X.X |
| 377 | */ |
| 378 | public static function should_clear_old_locations() |
| 379 | { |
| 380 | $cff_statuses_option = get_option('cff_statuses', array()); |
| 381 | $last_old_feed_check = isset($cff_statuses_option['feed_locator']['last_check']) ? $cff_statuses_option['feed_locator']['last_check'] : 0; |
| 382 | |
| 383 | return $last_old_feed_check < time() - DAY_IN_SECONDS; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Old feeds are removed if they haven't been updated in two weeks. |
| 388 | * |
| 389 | * @since X.X.X |
| 390 | */ |
| 391 | public static function delete_old_locations() |
| 392 | { |
| 393 | global $wpdb; |
| 394 | |
| 395 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 396 | $two_weeks_ago = date('Y-m-d H:i:s', time() - 2 * WEEK_IN_SECONDS); |
| 397 | |
| 398 | $affected = $wpdb->query($wpdb->prepare( |
| 399 | "DELETE FROM $feed_locator_table_name WHERE last_update < %s;", |
| 400 | $two_weeks_ago |
| 401 | )); |
| 402 | |
| 403 | $cff_statuses_option = get_option('cff_statuses', array()); |
| 404 | $cff_statuses_option['feed_locator']['last_check'] = time(); |
| 405 | if (! isset($cff_statuses_option['feed_locator']['initialized'])) { |
| 406 | $cff_statuses_option['feed_locator']['initialized'] = time(); |
| 407 | } |
| 408 | |
| 409 | update_option('cff_statuses', $cff_statuses_option, true); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Feeds are located with the page load randomly (5% or 1/30 loads) |
| 414 | * to decrease load on the server. |
| 415 | * |
| 416 | * If the locating just started (within 5 minutes) it is run more often |
| 417 | * to collect feed locations quickly. |
| 418 | * |
| 419 | * @return bool |
| 420 | * |
| 421 | * @since X.X.X |
| 422 | */ |
| 423 | public static function should_do_locating() |
| 424 | { |
| 425 | $cff_statuses_option = get_option('cff_statuses', array()); |
| 426 | if ( |
| 427 | isset($cff_statuses_option['feed_locator']['initialized']) |
| 428 | && $cff_statuses_option['feed_locator']['initialized'] < (time() - 300) |
| 429 | ) { |
| 430 | $should_do_locating = rand(1, 10) === 10; |
| 431 | } else { |
| 432 | $should_do_locating = rand(1, 30) === 30; |
| 433 | } |
| 434 | $should_do_locating = apply_filters('cff_should_do_locating', $should_do_locating); |
| 435 | |
| 436 | return $should_do_locating; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /** |
| 441 | * Simliar to the should_do_locating method but will add an additional |
| 442 | * database query to see if there is a feed with an unknown location that |
| 443 | * matches the details of the feed in question. |
| 444 | * |
| 445 | * @param $feed_id |
| 446 | * @param $post_id |
| 447 | * |
| 448 | * @return bool |
| 449 | * |
| 450 | * @since X.X.X |
| 451 | */ |
| 452 | public static function should_do_ajax_locating($feed_id, $post_id) |
| 453 | { |
| 454 | $cff_statuses_option = get_option('cff_statuses', array()); |
| 455 | if ( |
| 456 | isset($cff_statuses_option['feed_locator']['initialized']) |
| 457 | && $cff_statuses_option['feed_locator']['initialized'] < (time() - 300) |
| 458 | ) { |
| 459 | $should_do_locating = rand(1, 10) === 10; |
| 460 | } else { |
| 461 | $should_do_locating = rand(1, 30) === 30; |
| 462 | } |
| 463 | if ($should_do_locating) { |
| 464 | $should_do_locating = CFF_Feed_Locator::entries_need_locating($feed_id, $post_id); |
| 465 | } |
| 466 | |
| 467 | $should_do_locating = apply_filters('cff_should_do_ajax_locating', $should_do_locating); |
| 468 | |
| 469 | return $should_do_locating; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Feeds are located with the page load randomly (1/30 loads) |
| 474 | * to decrease load on the server. |
| 475 | * |
| 476 | * If the locating just started (within 5 minutes) it is run more often |
| 477 | * to collect feed locations quickly. |
| 478 | * |
| 479 | * @param $feed_id |
| 480 | * @param $post_id |
| 481 | * |
| 482 | * @return bool |
| 483 | * |
| 484 | * @since X.X.X |
| 485 | */ |
| 486 | public static function entries_need_locating($feed_id, $post_id) |
| 487 | { |
| 488 | global $wpdb; |
| 489 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 490 | |
| 491 | $one_day_ago = date('Y-m-d H:i:s', time() - DAY_IN_SECONDS); |
| 492 | |
| 493 | $results = $wpdb->get_results($wpdb->prepare(" |
| 494 | SELECT id |
| 495 | FROM $feed_locator_table_name |
| 496 | WHERE html_location = 'unknown' |
| 497 | AND last_update < %s |
| 498 | AND feed_id = %s |
| 499 | AND post_id = %d |
| 500 | LIMIT 1;", $one_day_ago, $feed_id, $post_id), ARRAY_A); |
| 501 | |
| 502 | return isset($results[0]); |
| 503 | } |
| 504 | |
| 505 | |
| 506 | |
| 507 | /** |
| 508 | * A custom table stores locations |
| 509 | * |
| 510 | * @since X.X.X |
| 511 | */ |
| 512 | public static function create_table() |
| 513 | { |
| 514 | global $wpdb; |
| 515 | |
| 516 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 517 | |
| 518 | if ($wpdb->get_var("show tables like '$feed_locator_table_name'") != $feed_locator_table_name) { |
| 519 | $sql = "CREATE TABLE " . $feed_locator_table_name . " ( |
| 520 | id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 521 | feed_id VARCHAR(50) DEFAULT '' NOT NULL, |
| 522 | post_id BIGINT(20) UNSIGNED NOT NULL, |
| 523 | html_location VARCHAR(50) DEFAULT 'unknown' NOT NULL, |
| 524 | shortcode_atts LONGTEXT NOT NULL, |
| 525 | last_update DATETIME, |
| 526 | PRIMARY KEY (id), |
| 527 | KEY feed_id (feed_id), |
| 528 | KEY post_id (post_id) |
| 529 | );"; |
| 530 | $wpdb->query($sql); |
| 531 | } |
| 532 | $error = $wpdb->last_error; |
| 533 | $query = $wpdb->last_query; |
| 534 | $had_error = false; |
| 535 | if ($wpdb->get_var("show tables like '$feed_locator_table_name'") != $feed_locator_table_name) { |
| 536 | $had_error = true; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Counts the number of unique feeds in the database. |
| 542 | * |
| 543 | * @return int |
| 544 | * |
| 545 | * @since X.X.X |
| 546 | */ |
| 547 | public static function count_unique() |
| 548 | { |
| 549 | global $wpdb; |
| 550 | |
| 551 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 552 | $results_content = $wpdb->get_results(" |
| 553 | SELECT COUNT(*) AS num_entries |
| 554 | FROM $feed_locator_table_name |
| 555 | WHERE html_location = 'content' |
| 556 | ", ARRAY_A); |
| 557 | |
| 558 | $results_other = $wpdb->get_results(" |
| 559 | SELECT COUNT(*) AS num_entries |
| 560 | FROM $feed_locator_table_name |
| 561 | WHERE html_location != 'content' |
| 562 | AND html_location != 'unknown' |
| 563 | GROUP BY feed_id |
| 564 | ", ARRAY_A); |
| 565 | // var_dump( $results_other ); |
| 566 | |
| 567 | $total = 0; |
| 568 | if (isset($results_content[0]['num_entries'])) { |
| 569 | $total += (int)$results_content[0]['num_entries']; |
| 570 | } |
| 571 | if (isset($results_other[0]['num_entries'])) { |
| 572 | $total += (int)$results_other[0]['num_entries']; |
| 573 | } |
| 574 | |
| 575 | return $total; |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Creates a summary of the located feeds in an array |
| 580 | * |
| 581 | * @return array |
| 582 | * |
| 583 | * @since X.X.X |
| 584 | */ |
| 585 | public static function summary() |
| 586 | { |
| 587 | global $wpdb; |
| 588 | |
| 589 | $feed_locator_table_name = esc_sql($wpdb->prefix . CFF_FEED_LOCATOR); |
| 590 | |
| 591 | $locations = array( |
| 592 | array( |
| 593 | 'label' => __('Content', 'custom-facebook-feed'), |
| 594 | 'html_locations' => array( 'content', 'unknown' ) |
| 595 | ), |
| 596 | array( |
| 597 | 'label' => __('Header', 'custom-facebook-feed'), |
| 598 | 'html_locations' => array( 'header' ), |
| 599 | 'group_by' => 'feed_id' |
| 600 | ), |
| 601 | array( |
| 602 | 'label' => __('Sidebar', 'custom-facebook-feed'), |
| 603 | 'html_locations' => array( 'sidebar' ), |
| 604 | 'group_by' => 'feed_id' |
| 605 | ), |
| 606 | array( |
| 607 | 'label' => __('Footer', 'custom-facebook-feed'), |
| 608 | 'html_locations' => array( 'footer' ), |
| 609 | 'group_by' => 'feed_id' |
| 610 | ) |
| 611 | ); |
| 612 | |
| 613 | $one_result_found = false; |
| 614 | |
| 615 | foreach ($locations as $key => $location) { |
| 616 | $in = implode("', '", $location['html_locations']); |
| 617 | $group_by = isset($location['group_by']) ? "GROUP BY " . $location['group_by'] : ""; |
| 618 | $results = $wpdb->get_results(" |
| 619 | SELECT * |
| 620 | FROM $feed_locator_table_name |
| 621 | WHERE html_location IN ('$in') |
| 622 | $group_by |
| 623 | ORDER BY last_update ASC", ARRAY_A); |
| 624 | |
| 625 | if (isset($results[0])) { |
| 626 | $one_result_found = true; |
| 627 | } |
| 628 | |
| 629 | $locations[ $key ]['results'] = $results; |
| 630 | } |
| 631 | |
| 632 | if (! $one_result_found) { |
| 633 | return array(); |
| 634 | } |
| 635 | |
| 636 | return $locations; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Do Locator Ajax Process |
| 641 | * |
| 642 | * @since X.X.X |
| 643 | */ |
| 644 | public static function cff_do_locator() |
| 645 | { |
| 646 | if (! isset($_POST['feed_id'])) { |
| 647 | die('invalid feed ID'); |
| 648 | } |
| 649 | $feed_id = sanitize_text_field($_POST['feed_id']); |
| 650 | $atts_raw = isset($_POST['atts']) ? json_decode(stripslashes($_POST['atts']), true) : array(); |
| 651 | if (is_array($atts_raw)) { |
| 652 | array_map('sanitize_text_field', $atts_raw); |
| 653 | } else { |
| 654 | $atts_raw = array(); |
| 655 | } |
| 656 | $atts = $atts_raw; // now sanitized |
| 657 | |
| 658 | $location = isset($_POST['location']) && in_array($_POST['location'], array( 'header', 'footer', 'sidebar', 'content' ), true) ? sanitize_text_field($_POST['location']) : 'unknown'; |
| 659 | $post_id = isset($_POST['post_id']) && $_POST['post_id'] !== 'unknown' ? (int)$_POST['post_id'] : 'unknown'; |
| 660 | $feed_details = array( |
| 661 | 'feed_id' => $feed_id, |
| 662 | 'atts' => $atts, |
| 663 | 'location' => array( |
| 664 | 'post_id' => $post_id, |
| 665 | 'html' => $location |
| 666 | ) |
| 667 | ); |
| 668 | $can_do_background_tasks = false; |
| 669 | |
| 670 | $cap = current_user_can('manage_custom_facebook_feed_options') ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 671 | $cap = apply_filters('cff_settings_pages_capability', $cap); |
| 672 | if (current_user_can($cap)) { |
| 673 | $can_do_background_tasks = true; |
| 674 | } |
| 675 | |
| 676 | $nonce = isset($_POST['locator_nonce']) ? sanitize_text_field(wp_unslash($_POST['locator_nonce'])) : ''; |
| 677 | if (!wp_verify_nonce($nonce, 'cff-locator-nonce-' . $post_id . '-' . $atts['feed'])) { |
| 678 | wp_send_json_error('nonce check failed, details do not match'); |
| 679 | } |
| 680 | |
| 681 | if ($can_do_background_tasks) { |
| 682 | CFF_Feed_Locator::do_background_tasks($feed_details); |
| 683 | wp_die('locating success'); |
| 684 | } |
| 685 | |
| 686 | wp_die('skipped locating'); |
| 687 | } |
| 688 | |
| 689 | |
| 690 | /** |
| 691 | * Do Background tasks |
| 692 | * |
| 693 | * @since X.X.X |
| 694 | */ |
| 695 | public static function do_background_tasks($feed_details) |
| 696 | { |
| 697 | if (isset($feed_details['shortcode_atts']) && trim($feed_details['shortcode_atts']) == "") { |
| 698 | $feed_details['shortcode_atts'] = []; |
| 699 | } |
| 700 | |
| 701 | $locator = new CFF_Feed_Locator($feed_details); |
| 702 | $locator->add_or_update_entry(); |
| 703 | if ($locator->should_clear_old_locations()) { |
| 704 | $locator->delete_old_locations(); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 |