Version 5.0.6

This commit is contained in:
Neo committed 2025-12-19 18:55:49 -08:00
1 parent a3a4a9ccbd
commit b89858b2ff
1214 files changed
+92828 -14859

No files matched your search

@@ -29,10 +29,6 @@ use IPS\Settings;
use IPS\Theme;
use function count;
use function defined;
use const IPS\CACHE_CONFIG;
use const IPS\CACHE_METHOD;
use const IPS\REDIS_CONFIG;
use const IPS\REDIS_ENABLED;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
@@ -116,10 +112,9 @@ class Advertisement extends ActiveRecord
*
* @param string $location Advertisement location
* @param int|null $position Position within a list
* @param int $adsShown Total ads already shown in this list
* @return Advertisement|NULL
*/
public static function loadByLocation( string $location, ?int $position=null, int $adsShown=0 ) : ?static
public static function loadByLocation( string $location, ?int $position=null ) : ?static
{
/* If we know there are no ads, we don't need to bother */
if ( !Settings::i()->ads_exist )
@@ -132,57 +127,63 @@ class Advertisement extends ActiveRecord
{
static::$advertisements = array();
$where[] = array( "ad_type!=?", static::AD_EMAIL );
$where[] = array( "ad_type!=?", static::AD_EMAIL );
$where[] = array( "ad_active=1" );
$where[] = array( "ad_start<?", time() );
$where[] = array( "(ad_end=0 OR ad_end>?)", time() );
if( Dispatcher::hasInstance() AND ( !isset( Dispatcher::i()->dispatcherController ) OR !Dispatcher::i()->dispatcherController->isContentPage ) )
if ( Dispatcher::hasInstance() and ( !isset( Dispatcher::i()->dispatcherController ) or !Dispatcher::i()->dispatcherController->isContentPage ) )
{
$where[] =array( 'ad_nocontent_page_output=?', 1 );
$where[] = array( 'ad_nocontent_page_output=?', 1 );
}
foreach( Db::i()->select( '*' ,'core_advertisements', $where ) as $row )
foreach ( Db::i()->select( '*', 'core_advertisements', $where ) as $row )
{
foreach ( explode( ',', $row['ad_location'] ) as $_location )
{
static::$advertisements[ $_location ][] = static::constructFromData( $row );
}
}
}
foreach( static::$advertisements as $adLocation => $ads )
{
foreach( $ads as $index => $ad )
foreach ( static::$advertisements as $adLocation => $ads )
{
/* Weed out any we don't see due to our group. This is done after loading the advertisements so that the cache can be properly primed regardless of group. Note that $ad->exempt, is, confusingly who to SHOW to, not who is exempt */
if ( ! empty( $ad->exempt ) and $ad->exempt != '*' )
foreach ( $ads as $index => $ad )
{
$groupsToHideFrom = array_diff( array_keys( Group::groups() ), json_decode( $ad->exempt, TRUE ) );
if ( Member::loggedIn()->inGroup( $groupsToHideFrom ) )
/* Weed out any we don't see due to our group. This is done after loading the advertisements so that the cache can be properly primed regardless of group. Note that $ad->exempt, is, confusingly who to SHOW to, not who is exempt */
if ( !empty( $ad->exempt ) and $ad->exempt != '*' )
{
unset( static::$advertisements[ $adLocation ][ $index ] );
continue;
$groupsToHideFrom = array_diff( array_keys( Group::groups() ), json_decode( $ad->exempt, TRUE ) );
if ( Member::loggedIn()->inGroup( $groupsToHideFrom ) )
{
unset( static::$advertisements[ $adLocation ][ $index ] );
continue;
}
}
/* Weed out any ads that we don't see due to the application settings */
if ( in_array( $adLocation, static::$defaultLocations ) and isset( $ad->_additional_settings['ad_apps'] ) and is_array( $ad->_additional_settings['ad_apps'] ) and $ad->_additional_settings != 0 )
{
if ( !in_array( Dispatcher::i()->application->directory, $ad->_additional_settings['ad_apps'] ) )
{
unset( static::$advertisements[ $adLocation ][ $index ] );
continue;
}
}
}
/* Weed out any ads that we don't see due to the application settings */
if( in_array( $adLocation, static::$defaultLocations ) AND isset( $ad->_additional_settings['ad_apps'] ) AND is_array( $ad->_additional_settings['ad_apps'] ) AND $ad->_additional_settings != 0 )
if( !in_array( $adLocation, static::$defaultLocations ) )
{
if( !in_array( Dispatcher::i()->application->directory, $ad->_additional_settings['ad_apps'] ) )
foreach ( $ads as $index => $ad )
{
unset( static::$advertisements[ $adLocation ][ $index ] );
continue;
}
}
/* Remove advertisements that can't be shown based on settings */
if( $extension = static::loadExtension( $location ) )
{
if( !$extension->canShow( $ad, $location ) )
{
unset( static::$advertisements[ $adLocation][ $index ] );
/* Remove advertisements that can't be shown based on settings */
if ( $extension = static::loadExtension( $location ) )
{
if ( !$extension->canShow( $ad, $location ) )
{
unset( static::$advertisements[ $adLocation ][ $index ] );
}
}
}
}
}
@@ -194,9 +195,15 @@ class Advertisement extends ActiveRecord
return NULL;
}
return static::selectAdvertisement( static::$advertisements[ $location ], $position, $adsShown );
return static::selectAdvertisement( static::$advertisements[ $location ], $position );
}
/**
* Store loaded extensions
* @var array
*/
static array $_loadedExtensions = [];
/**
* Find the Extension that handles this ad location
*
@@ -205,13 +212,19 @@ class Advertisement extends ActiveRecord
*/
protected static function loadExtension( string $location ) : ?AdvertisementLocationsAbstract
{
/* Check for a loaded extension */
if( isset( static::$_loadedExtensions[ $location ] ) )
{
return static::$_loadedExtensions[ $location ];
}
foreach( Application::allExtensions( 'core', 'AdvertisementLocations' ) as $ext )
{
/* @var AdvertisementLocationsAbstract $ext */
$extensionSettings = $ext->getSettings( array() );
if( array_key_exists( $location, $extensionSettings['locations'] ) )
{
return $ext;
return static::$_loadedExtensions[ $location ] = $ext;
}
}
@@ -381,15 +394,20 @@ class Advertisement extends ActiveRecord
return static::selectAdvertisement( $adsToCheckFrom );
}
/**
* @brief Track positioning of each ad in a listing view
* @var array
*/
protected static array $listingView = [];
/**
* Select an advertisement from an array and return it
*
* @param array $ads Array of advertisements to select from
* @param int|null $position Position within a list
* @param int $adsShown Total ads already shown in this list
* @return static|null
*/
static protected function selectAdvertisement( array $ads, ?int $position=null, int $adsShown=0 ) : ?static
static protected function selectAdvertisement( array $ads, ?int $position=null ) : ?static
{
/* Reset so we don't throw an error */
$ads = array_values( $ads );
@@ -448,13 +466,30 @@ class Advertisement extends ActiveRecord
if( $indexNumber !== false )
{
/* Check the position and see if it's a match */
if( $position > $indexNumber AND $position % $indexNumber === 1 )
/* Figure out the last position it was shown */
if( empty( static::$listingView[ $ad->id ] ) )
{
static::$listingView[ $ad->id ] = [];
$lastShown = 0;
}
else
{
$lastIndex = count( static::$listingView[ $ad->id ] ) - 1;
$lastShown = static::$listingView[ $ad->id ][ $lastIndex ];
}
/* Total times it was shown */
$adsShown = count( static::$listingView[ $ad->id ] );
/* Check the position and see if it's a match.
We use >= instead of == because of circulation settings; we might not have
an exact match to the interval. */
if( $position - $lastShown >= $indexNumber )
{
/* Did we already show it the maximum times? */
if( $repeat !== false )
{
if( $repeat === -1 OR ( isset( $adsShown ) AND $repeat > $adsShown ) )
if( $repeat === -1 OR $repeat > $adsShown )
{
$advertisement = $ad;
break;
@@ -469,6 +504,12 @@ class Advertisement extends ActiveRecord
}
}
/* Store the position so that we can track it for the next round */
if( isset( $advertisement ) and $position !== null )
{
static::$listingView[ $advertisement->id ][] = $position;
}
return $advertisement ?? null;
}