diff --git a/autoload/controls/class.Cron.php b/autoload/controls/class.Cron.php index 23c368f..d3d9f01 100644 --- a/autoload/controls/class.Cron.php +++ b/autoload/controls/class.Cron.php @@ -4381,24 +4381,6 @@ class Cron ]; } - // Blokada ponownego pobrania w tym samym dniu (chyba ze wymuszone lub konkretny klient) - if ( !$is_forced_refresh && $requested_client_id <= 0 ) - { - $last_synced_date = (string) \services\FacebookAdsApi::get_setting( 'cron_facebook_ads_last_active_date' ); - if ( $last_synced_date === $until ) - { - return [ - 'result' => 'Synchronizacja Facebook Ads juz wykonana dzisiaj (' . $since . ' - ' . $until . ').', - 'success' => true, - 'skipped' => true, - 'days' => $days, - 'since' => $since, - 'until' => $until, - 'last_synced_date' => $last_synced_date - ]; - } - } - $clients = \factory\FacebookAds::get_clients_for_sync( $client_id ); if ( empty( $clients ) ) { @@ -4436,6 +4418,41 @@ class Cron ]; } + // Blokada ponownego pobrania w tym samym dniu (chyba ze wymuszone lub konkretny klient) + $is_incremental_sync = false; + if ( !$is_forced_refresh && $requested_client_id <= 0 ) + { + $last_synced_date = (string) \services\FacebookAdsApi::get_setting( 'cron_facebook_ads_last_active_date' ); + if ( $last_synced_date === $until ) + { + // Sprawdz czy pojawili sie nowi klienci nieobecni w ostatniej synchronizacji + $last_ids_str = (string) \services\FacebookAdsApi::get_setting( 'cron_facebook_ads_last_synced_client_ids' ); + $last_ids = array_filter( array_map( 'intval', explode( ',', $last_ids_str ) ) ); + $current_ids = array_map( function ( $c ) { return (int) ( $c['id'] ?? 0 ); }, $clients ); + $new_ids = array_values( array_diff( $current_ids, $last_ids ) ); + + if ( empty( $new_ids ) ) + { + return [ + 'result' => 'Synchronizacja Facebook Ads juz wykonana dzisiaj (' . $since . ' - ' . $until . ').', + 'success' => true, + 'skipped' => true, + 'days' => $days, + 'since' => $since, + 'until' => $until, + 'last_synced_date' => $last_synced_date + ]; + } + + // Sa nowi klienci - ogranicz synchronizacje tylko do nich + $clients = array_values( array_filter( $clients, function ( $c ) use ( $new_ids ) + { + return in_array( (int) ( $c['id'] ?? 0 ), $new_ids ); + } ) ); + $is_incremental_sync = true; + } + } + $api = new \services\FacebookAdsApi( $token, $api_version ); if ( !$api -> is_configured() ) { @@ -4518,6 +4535,19 @@ class Cron { \services\FacebookAdsApi::set_setting( 'cron_facebook_ads_last_success_at', date( 'Y-m-d H:i:s' ) ); } + if ( $is_incremental_sync ) + { + $prev_ids_str = (string) \services\FacebookAdsApi::get_setting( 'cron_facebook_ads_last_synced_client_ids' ); + $prev_ids = array_filter( array_map( 'intval', explode( ',', $prev_ids_str ) ) ); + $synced_ids = array_map( function ( $item ) { return (int) ( $item['client_id'] ?? 0 ); }, $items ); + $all_ids = array_values( array_unique( array_merge( $prev_ids, $synced_ids ) ) ); + \services\FacebookAdsApi::set_setting( 'cron_facebook_ads_last_synced_client_ids', implode( ',', $all_ids ) ); + } + else + { + $synced_ids = array_map( function ( $item ) { return (int) ( $item['client_id'] ?? 0 ); }, $items ); + \services\FacebookAdsApi::set_setting( 'cron_facebook_ads_last_synced_client_ids', implode( ',', $synced_ids ) ); + } \services\FacebookAdsApi::set_setting( 'cron_facebook_ads_last_active_date', $until ); if ( $forced_client_id > 0 )