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