includes/class-freemius.php
private function execute_cron( $name, $callable ) {
            $this->_logger->entrance( $name );

            // Store the last time data sync was executed.
            $this->set_cron_execution_timestamp( $name );

            // Check if API is temporary down.
            if ( FS_Api::is_temporary_down() ) {
                return;
            }

            // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.

            $users_2_blog_ids = array();

            if ( ! is_multisite() ) {
                // Add dummy blog.
                $users_2_blog_ids[0] = array( 0 );
            } else {
                $installs = $this->get_blog_install_map();
                foreach ( $installs as $blog_id => $install ) {
                    if ( $this->is_premium() || $install->is_tracking_allowed() ) {
                        if ( ! isset( $users_2_blog_ids[ $install->user_id ] ) ) {
                            $users_2_blog_ids[ $install->user_id ] = array();
                        }

                        $users_2_blog_ids[ $install->user_id ][] = $blog_id;
                    }
                }
            }

            $current_blog_id = get_current_blog_id();

            foreach ( $users_2_blog_ids as $user_id => $blog_ids ) {
                if ( 0 < $blog_ids[0] ) {
                    $this->switch_to_blog( $blog_ids[0] );
                }

                call_user_func_array( $callable, array( $blog_ids, ( is_multisite() ? $current_blog_id : null ) ) );

                foreach ( $blog_ids as $blog_id ) {
                    $this->do_action( "after_{$name}_cron", $blog_id );
                }
            }

            if ( is_multisite() ) {
                $this->switch_to_blog( $current_blog_id );

                $this->do_action( "after_{$name}_cron_multisite" );
            }
        }
private function execute_cron( $name, $callable ) {
            $this->_logger->entrance( $name );

            // Store the last time data sync was executed.
            $this->set_cron_execution_timestamp( $name );

            // Check if API is temporary down.
            if ( FS_Api::is_temporary_down() ) {
                return;
            }

            // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.

            $users_2_blog_ids = array();

            if ( ! is_multisite() ) {
                // Add dummy blog.
                $users_2_blog_ids[0] = array( 0 );
            } else {
                $installs = $this->get_blog_install_map();
                foreach ( $installs as $blog_id => $install ) {
                    if ( $this->is_premium() || $install->is_tracking_allowed() ) {
                        if ( ! isset( $users_2_blog_ids[ $install->user_id ] ) ) {
                            $users_2_blog_ids[ $install->user_id ] = array();
                        }

                        $users_2_blog_ids[ $install->user_id ][] = $blog_id;
                    }
                }
            }

            $current_blog_id = get_current_blog_id();

            foreach ( $users_2_blog_ids as $user_id => $blog_ids ) {
                if ( 0 < $blog_ids[0] ) {
                    $this->switch_to_blog( $blog_ids[0] );
                }

                call_user_func_array( $callable, array( $blog_ids, ( is_multisite() ? $current_blog_id : null ) ) );

                foreach ( $blog_ids as $blog_id ) {
                    $this->do_action( "after_{$name}_cron", $blog_id );
                }
            }

            if ( is_multisite() ) {
                $this->switch_to_blog( $current_blog_id );

                $this->do_action( "after_{$name}_cron_multisite" );
            }
        }
private function execute_cron ( $ name , $ callable ) { $ this -> _logger -> entrance ( $ name ) ; // Store the last time data sync was executed.
 $ this -> set_cron_execution_timestamp ( $ name ) ; // Check if API is temporary down.
 if ( FS_Api :: is_temporary_down ( ) ) { return ; } // @todo Add logic that identifies API latency, and reschedule the next background sync randomly between 8-16 hours.
 $ users_2_blog_ids = array ( ) ; if ( ! is_multisite ( ) ) { // Add dummy blog.
 $ users_2_blog_ids [ 0 ] = array ( 0 ) ; } else { $ installs = $ this -> get_blog_install_map ( ) ; foreach ( $ installs as $ blog_id => $ install ) { if ( $ this -> is_premium ( ) || $ install -> is_tracking_allowed ( ) ) { if ( ! isset ( $ users_2_blog_ids [ $ install -> user_id ] ) ) { $ users_2_blog_ids [ $ install -> user_id ] = array ( ) ; } $ users_2_blog_ids [ $ install -> user_id ] [ ] = $ blog_id ; } } } $ current_blog_id = get_current_blog_id ( ) ; foreach ( $ users_2_blog_ids as $ user_id => $ blog_ids ) { if ( 0 < $ blog_ids [ 0 ] ) { $ this -> switch_to_blog ( $ blog_ids [ 0 ] ) ; } call_user_func_array ( $ callable , array ( $ blog_ids , ( is_multisite ( ) ? $ current_blog_id : null ) ) ) ; foreach ( $ blog_ids as $ blog_id ) { $ this -> do_action ( "after_{$name}_cron" , $ blog_id ) ; } } if ( is_multisite ( ) ) { $ this -> switch_to_blog ( $ current_blog_id ) ; $ this -> do_action ( "after_{$name}_cron_multisite" ) ; } }
Consolidated cron execution for performance optimization. The max number of API requests is based on the number of unique opted-in users.
that doesn't halt page loading.

@author Vova Feldman (@svovaf)
@since  2.0.0

@param string   $name     Cron name.
@param callable $callable The function that should be executed.
Consolidated cron execution for performance optimization . The max number of API requests is based on the number of unique opted - in users . that doesn t halt page loading .