hook_callback()
*
* becomes:
*
* Inpsyde\W\H\HookListenersRegistry->hook_callback()
*
* @param string $fqn A fully qualified name.
*
* @return string A shortened version of the name.
*
* @since 1.4.0
*/
public static function shorten_fqn( string $fqn ): string {
if ( substr_count( $fqn, '\\' ) < 3 ) {
return $fqn;
}
return preg_replace_callback(
'#\\\\[a-zA-Z0-9_\\\\]{4,}\\\\#',
static function ( array $matches ): string {
preg_match_all( '#\\\\([a-zA-Z0-9_])#', $matches[0], $m );
return '\\' . implode( '\\', $m[1] ) . '\\';
},
$fqn
);
}
/**
* Attempts to determine the component responsible for a given file name.
*
* @param string $file An absolute file path.
*
* @return array An array representing the component.
*
* @since 1.4.0
*/
public static function get_file_component( string $file ): array {
$file = self::standard_dir( $file );
if ( isset( self::$file_components[ $file ] ) ) {
return self::$file_components[ $file ];
}
$type = '';
foreach ( self::get_file_dirs() as $current_type => $dir ) {
if ( $dir && ( false !== strpos( $file, trailingslashit( $dir ) ) ) ) {
$type = $current_type;
break;
}
}
$context = $type;
switch ( $type ) {
case 'altis-vendor':
$name = self::get_altis_vendor_name( $file );
break;
case 'plugin':
case 'mu-plugin':
case 'mu-vendor':
$name = self::get_plugin_name( $file, $type );
break;
case 'go-plugin':
case 'vip-plugin':
case 'vip-client-mu-plugin':
$name = self::get_vip_plugin_name( $file, $type );
break;
case 'stylesheet':
$name = is_child_theme() ? __( 'Child Theme', '0-day-analytics' ) : __( 'Theme', '0-day-analytics' );
break;
case 'template':
$name = __( 'Parent Theme', '0-day-analytics' );
break;
case 'other':
$name = self::get_other_name( $file );
break;
case 'core':
$name = __( 'WordPress Core', '0-day-analytics' );
break;
case 'core-includes':
$name = __( 'WordPress Core Includes', '0-day-analytics' );
break;
default:
$name = __( 'Unknown', '0-day-analytics' );
break;
}
/*
$name = match ($type) {
'altis-vendor' => self::get_altis_vendor_name( $file ),
'plugin', 'mu-plugin', 'mu-vendor' => self::get_plugin_name( $file, $type ),
'go-plugin', 'vip-plugin', 'vip-client-mu-plugin' => self::get_vip_plugin_name( $file, $type ),
'stylesheet' => is_child_theme() ? __( 'Child Theme', '0-day-analytics' ) : __( 'Theme', '0-day-analytics' ),
'template' => __( 'Parent Theme', '0-day-analytics' ),
'other' => self::get_other_name( $file ),
'core' => __( 'WordPress Core', '0-day-analytics' ),
default => __( 'Unknown', '0-day-analytics' ),
};
}
*/
if ( 'stylesheet' === $type || 'template' === $type ) {
$type = 'theme';
}
if ( 'other' === $type ) {
if ( ! function_exists( '_get_dropins' ) ) {
require_once trailingslashit( constant( 'ABSPATH' ) ) . 'wp-admin/includes/plugin.php';
}
$dropins = array_keys( _get_dropins() );
foreach ( $dropins as $dropin ) {
$dropin_path = trailingslashit( constant( 'WP_CONTENT_DIR' ) ) . $dropin;
if ( $file !== $dropin_path ) {
continue;
}
$type = 'dropin';
$name = sprintf(
// translators: %s is the drop-in file name.
__( 'Drop-in: %s', '0-day-analytics' ),
pathinfo( $dropin, PATHINFO_BASENAME )
);
break;
}
}
$component = array(
'type' => $type,
'name' => $name,
'context' => $context,
);
self::$file_components[ $file ] = $component;
return $component;
}
/**
* Get the name of the altis vendor.
*
* @param string $file The file path.
*
* @return string The name of the altis vendor.
*
* @since 1.4.0
*/
private static function get_altis_vendor_name( string $file ): string {
if ( ! defined( 'Altis\\ROOT_DIR' ) ) {
return __( 'Dependency: unknown', '0-day-analytics' );
}
$plug = str_replace( constant( 'Altis\\ROOT_DIR' ) . '/vendor/', '', $file );
$plug = explode( '/', $plug, 3 );
$plug = $plug[0] . '/' . $plug[1];
return sprintf(
// translators: %s is the altis vendor name.
__( 'Dependency: %s', '0-day-analytics' ),
$plug
);
}
/**
* Get the name of the plugin.
*
* @param string $file The file path.
* @param string $type The type of the plugin.
*
* @return string The name of the plugin.
*
* @since 1.4.0
*/
private static function get_plugin_name( string $file, string $type ): string {
$file = str_replace( '/vendor/', '/', $file );
$plug = \plugin_basename( $file );
if ( strpos( $plug, '/' ) ) {
$plug = explode( '/', $plug )[0];
} else {
$plug = basename( $plug );
}
$name = sprintf(
// translators: %s is the plugin name.
( 'plugin' !== $type ? __( 'MU Plugin: %s', '0-day-analytics' ) : __( 'Plugin: %s', '0-day-analytics' ) ),
$plug
);
return $name;
}
/**
* Get the name of the VIP plugin.
*
* @param string $file The file path.
* @param string $type The type of the plugin.
*
* @return string The name of the VIP plugin.
*
* @since 1.4.0
*/
private static function get_vip_plugin_name( string $file, string $type ): string {
$plug = str_replace( self::$file_dirs[ $type ], '', $file );
$plug = trim( $plug, '/' );
if ( strpos( $plug, '/' ) ) {
$plug = explode( '/', $plug )[0];
} else {
$plug = basename( $plug );
}
$name = sprintf(
(
'vip-client-mu-plugin' === $type ?
// translators: %s is the VIP plugin name.
__( 'VIP Client MU Plugin: %s', '0-day-analytics' ) :
// translators: %s is the VIP plugin name.
__( 'VIP Plugin: %s', '0-day-analytics' )
),
$plug
);
return $name;
}
/**
* Get the name of the other file.
*
* @param string $file The file path.
*
* @return string The name of the other file.
*
* @since 1.4.0
*/
private static function get_other_name( string $file ): string {
$name = self::standard_dir( $file );
$name = str_replace( dirname( self::$file_dirs['other'] ), '', $name );
$parts = explode( '/', trim( $name, '/' ) );
$name = isset( $parts[1] ) ? $parts[0] . '/' . $parts[1] : $parts[0];
return $name;
}
/**
* Get the file directories.
*
* @return array The file directories.
*
* @since 1.4.0
*/
public static function get_file_dirs(): array {
if ( ! empty( self::$file_dirs ) ) {
return self::$file_dirs;
}
self::$file_dirs = array(
'core-includes' => WPINC,
'plugin' => basename( WP_PLUGIN_DIR ),
'mu-vendor' => basename( WPMU_PLUGIN_DIR ) . '/vendor',
'go-plugin' => basename( WPMU_PLUGIN_DIR ) . '/shared-plugins',
'mu-plugin' => basename( WPMU_PLUGIN_DIR ),
'vip-plugin' => basename( get_theme_root() ) . '/vip/plugins',
'vip-client-mu-plugin' => defined( 'WPCOM_VIP_CLIENT_MU_PLUGIN_DIR' ) ? constant( 'WPCOM_VIP_CLIENT_MU_PLUGIN_DIR' ) : null,
'altis-vendor' => defined( 'Altis\\ROOT_DIR' ) ? constant( 'Altis\\ROOT_DIR' ) . '/vendor' : null,
'theme' => null,
'stylesheet' => basename( get_stylesheet_directory() ),
'template' => basename( get_template_directory() ),
'other' => WP_CONTENT_DIR,
'core' => ABSPATH,
'unknown' => null,
);
foreach ( self::$file_dirs as $type => &$dir ) {
if ( null !== $dir ) {
$dir = self::standard_dir( $dir );
}
}
return self::$file_dirs;
}
/**
* Get the name of the time offset setting.
*
* @return string The UTC offset.
*
* @since 1.4.0
*/
public static function get_timezone_location(): string {
$timezone_string = \get_option( 'timezone_string', '' );
$gmt_offset = \get_option( 'gmt_offset', 0 );
if ( 'UTC' === $timezone_string || ( empty( $gmt_offset ) && empty( $timezone_string ) ) ) {
return 'UTC';
}
if ( '' === $timezone_string ) {
return self::get_utc_offset();
}
$parts = explode( '/', $timezone_string );
return str_replace( '_', ' ', end( $parts ) );
}
/**
* Converts a period of time in seconds into a human-readable format representing the interval.
*
* Intervals less than an hour are displayed in minutes, and intervals less than a minute are
* displayed in seconds. All intervals are displayed in the two largest units.
*
* The `$accurate` parameter can be used to display an interval of less than an hour in minutes and seconds.
*
* @param int|float $since A period of time in seconds.
* @param bool $accurate Whether to display the interval in minutes and seconds.
*
* @return string An interval represented as a string.
*
* @since 1.4.0
*/
public static function interval( $since, bool $accurate = false ) {
// Array of time period chunks.
$chunks = array(
/* translators: %s: The number of years in an interval of time. */
array( YEAR_IN_SECONDS, _n_noop( '%s year', '%s years', '0-day-analytics' ) ),
/* translators: %s: The number of months in an interval of time. */
array( MONTH_IN_SECONDS, _n_noop( '%s month', '%s months', '0-day-analytics' ) ),
/* translators: %s: The number of weeks in an interval of time. */
array( WEEK_IN_SECONDS, _n_noop( '%s week', '%s weeks', '0-day-analytics' ) ),
/* translators: %s: The number of days in an interval of time. */
array( DAY_IN_SECONDS, _n_noop( '%s day', '%s days', '0-day-analytics' ) ),
/* translators: %s: The number of hours in an interval of time. */
array( HOUR_IN_SECONDS, _n_noop( '%s hour', '%s hours', '0-day-analytics' ) ),
/* translators: %s: The number of minutes in an interval of time. */
array( MINUTE_IN_SECONDS, _n_noop( '%s minute', '%s minutes', '0-day-analytics' ) ),
/* translators: %s: The number of seconds in an interval of time. */
array( 1, _n_noop( '%s second', '%s seconds', '0-day-analytics' ) ),
);
if ( $since <= 0 ) {
return __( 'now', '0-day-analytics' );
}
if ( ( ! $accurate ) && ( $since >= MINUTE_IN_SECONDS ) && ( $since < HOUR_IN_SECONDS ) ) {
$num = intval( floor( $since / MINUTE_IN_SECONDS ) );
return sprintf(
/* translators: %s: The number of minutes in an interval of time. */
_n( '%s minute', '%s minutes', $num, '0-day-analytics' ),
$num
);
}
/**
* We only want to output two chunks of time here, eg:
* x years, xx months
* x days, xx hours
* so there's only two bits of calculation below:
*/
// Step one: the first chunk.
foreach ( array_keys( $chunks ) as $i ) {
$seconds = $chunks[ $i ][0];
$name = $chunks[ $i ][1];
// Finding the biggest chunk (if the chunk fits, break).
$count = (int) floor( $since / $seconds );
if ( $count ) {
break;
}
}
// Set output var.
$output = sprintf( translate_nooped_plural( $name, $count, '0-day-analytics' ), $count );
// Step two: the second chunk.
if ( $i + 1 < count( $chunks ) ) {
$seconds2 = $chunks[ $i + 1 ][0];
$name2 = $chunks[ $i + 1 ][1];
$count2 = (int) floor( ( $since - ( $seconds * $count ) ) / $seconds2 );
if ( $count2 ) {
// Add to output var.
$output .= ' ' . sprintf( translate_nooped_plural( $name2, $count2, '0-day-analytics' ), $count2 );
}
}
return $output;
}
/**
* Returns a display value for a UTC offset.
*
* Examples:
* - UTC
* - UTC+4
* - UTC-6
*
* @return string The UTC offset display value.
*
* @since 1.4.0
*/
public static function get_utc_offset() {
$offset = \get_option( 'gmt_offset', 0 );
if ( empty( $offset ) ) {
return 'UTC';
}
if ( 0 <= $offset ) {
$formatted_offset = '+' . (string) $offset;
} else {
$formatted_offset = (string) $offset;
}
$formatted_offset = str_replace(
array( '.25', '.5', '.75' ),
array( ':15', ':30', ':45' ),
$formatted_offset
);
return 'UTC' . $formatted_offset;
}
/**
* Checks global WP_Cron constant and its status - returns error if disabled, nothing otherwise.
*
* @return \WP_Error|null
*
* @since 1.7.4
*/
public static function check_cron_status() {
if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
$url = \add_query_arg( array( 'page' => Settings::SETTINGS_MENU_SLUG ), \network_admin_url( 'admin.php' ) ) . '#aadvana-options-tab-cron-list';
$url = \esc_url( $url );
return new \WP_Error(
'advana_cron_info',
sprintf(
/* translators: %s: The name of the PHP constant that is set. %s The url to the cron settings */
__( 'The %1$s constant is set to true. WP-Cron spawning is disabled. That is OK if you are using server side cron, if not the case - try to enable it in the settings - %2$s', '0-day-analytics' ),
'DISABLE_WP_CRON',
'' . __( 'here', '0-day-analytics' ) . '',
)
);
}
}
/**
* Checks global WP_Debug constant and its status - returns error if disabled, nothing otherwise.
*
* @return \WP_Error|null
*
* @since 1.7.4
*/
public static function check_debug_status() {
/**
* If the user has enabled the option to keep the error log, we will not check for the WP Debug and WP Debug Log.
*/
if ( ! Settings::get_option( 'keep_reading_error_log' ) ) {
if ( ! defined( 'WP_DEBUG' ) || ! \WP_DEBUG ) {
$url = \add_query_arg( array( 'page' => Settings::SETTINGS_MENU_SLUG ), network_admin_url( 'admin.php' ) ) . '#aadvana-options-tab-error-log-list';
$url = \esc_url( $url );
return new \WP_Error(
'debug_off',
sprintf(
/* translators: %s: The name of the PHP constant that is set. %s The url to the cron settings */
__( 'The %1$s constant is not set or it is set to false. WP Debug is disabled. Try to enable it in settings - %2$s. If you want plugin to still monitoring errors from error log set outside WP system (direct php ini_set), select "Keep monitoring" in settings.', '0-day-analytics' ),
'WP_DEBUG',
'' . __( 'here', '0-day-analytics' ) . '',
)
);
}
}
}
/**
* Checks global WP_Debug constant and its status - if not set, it checks if there is an error log file detected by the plugin - if there is, it returns an error with the info about that and the link to the settings page.
*
* @return \WP_Error|null
*
* @since 5.0.0
*/
public static function check_non_wp_debug_log_status() {
if ( ! defined( 'WP_DEBUG' ) || ! \WP_DEBUG && ! defined( 'WP_DEBUG_LOG' ) || ! \WP_DEBUG_LOG && ! Settings::get_option( 'keep_reading_error_log' ) ) {
$log_file = Error_Log::extract_file_name( Error_Log::autodetect() );
if ( ! empty( $log_file ) && \is_string( $log_file ) ) {
$url = \add_query_arg( array( 'page' => Settings::SETTINGS_MENU_SLUG ), network_admin_url( 'admin.php' ) ) . '#aadvana-options-tab-error-log-list';
$url = \esc_url( $url );
return new \WP_Error(
'external_debug_log',
sprintf(
/* translators: %s: The name of the PHP constant that is set. %s The url to the cron settings */
__( 'The plugin still detects an PHP error log file. If you want to use it, enable "Keep monitoring" setting here - %1$s.', '0-day-analytics' ),
'' . __( 'here', '0-day-analytics' ) . '',
)
);
}
}
return false;
}
/**
* Checks global WP_Debug_Log constant and its status - returns error if disabled, nothing otherwise.
*
* @return \WP_Error|null
*
* @since 1.7.4
*/
public static function check_debug_log_status() {
/**
* If the user has enabled the option to keep the error log, we will not check for the WP Debug and WP Debug Log.
*/
if ( ! Settings::get_option( 'keep_reading_error_log' ) ) {
if ( ! defined( 'WP_DEBUG_LOG' ) || ! \WP_DEBUG_LOG ) {
$url = \add_query_arg( array( 'page' => Settings::SETTINGS_MENU_SLUG ), network_admin_url( 'admin.php' ) ) . '#aadvana-options-tab-error-log-list';
$url = \esc_url( $url );
return new \WP_Error(
'debug_log_off',
sprintf(
/* translators: %s: The name of the PHP constant that is set. %s The url to the cron settings */
__( 'The %1$s constant is not set or it is set to false. WP Debug Log is disabled. Try to enable it in settings - %2$s.', '0-day-analytics' ),
'WP_DEBUG_LOG',
'' . __( 'here', '0-day-analytics' ) . '',
)
);
}
}
}
/**
* Extracts the admin path of the WordPress installation.
*
* @return string
*
* @since 1.8.0
*/
public static function get_admin_path(): string {
if ( null === self::$admin_path ) {
self::$admin_path = (string) str_replace( \get_home_url( 1 ) . '/', ABSPATH, \network_admin_url() );
}
return self::$admin_path;
}
/**
* Get the blog URL.
*
* @since 1.8.2
*
* @return string
*/
public static function get_blog_domain(): string {
if ( self::is_multisite() ) {
$blog_id = function_exists( 'get_current_blog_id' ) ? \get_current_blog_id() : 0;
$blog_domain = \get_blog_option( $blog_id, 'home' );
} else {
$blog_domain = \get_option( 'home' );
}
// Replace protocols.
return str_replace( array( 'http://', 'https://' ), '', $blog_domain );
}
/**
* Check is this is a multisite setup.
*
* @return bool
*
* @since 1.8.2
*/
public static function is_multisite() {
if ( null === self::$is_multisite ) {
self::$is_multisite = function_exists( 'is_multisite' ) && is_multisite();
}
return self::$is_multisite;
}
/**
* Returns the the wp_screen property.
*
* @return \WP_Screen|null
*
* @since 1.8.4
*/
public static function get_wp_screen() {
if ( empty( self::$wp_screen ) ) {
if ( \function_exists( 'get_current_screen' ) ) {
self::$wp_screen = \get_current_screen();
}
}
return self::$wp_screen;
}
/**
* More unified time formatter for list items table view showing.
*
* @param array $item - Item data.
* @param string $label_string - Label string to show in the badge.
*
* @return string
*
* @since 1.8.4
*/
public static function time_formatter( array $item, string $label_string ): string {
if ( ! isset( $item['schedule'] ) && isset( $item['timestamp'] ) ) {
$item['schedule'] = $item['timestamp'];
}
if ( 1 === $item['schedule'] ) {
return sprintf(
' %s',
\esc_html__( 'Immediately', '0-day-analytics' ),
);
}
$time_format = 'g:i a';
$event_datetime_utc = \gmdate( 'Y-m-d H:i:s', $item['schedule'] );
$timezone_local = \wp_timezone();
$event_local = \get_date_from_gmt( $event_datetime_utc, 'Y-m-d' );
$today_local = ( new \DateTimeImmutable( 'now', $timezone_local ) )->format( 'Y-m-d' );
$tomorrow_local = ( new \DateTimeImmutable( 'tomorrow', $timezone_local ) )->format( 'Y-m-d' );
$yesterday_local = ( new \DateTimeImmutable( 'yesterday', $timezone_local ) )->format( 'Y-m-d' );
// If the offset of the date of the event is different from the offset of the site, add a marker.
if ( \get_date_from_gmt( $event_datetime_utc, 'P' ) !== \get_date_from_gmt( 'now', 'P' ) ) {
$time_format .= ' (P)';
}
$event_time_local = \get_date_from_gmt( $event_datetime_utc, $time_format );
if ( $event_local === $today_local ) {
$date = sprintf(
/* translators: %s: Time */
__( 'Today at %s', '0-day-analytics' ),
$event_time_local,
);
} elseif ( $event_local === $tomorrow_local ) {
$date = sprintf(
/* translators: %s: Time */
__( 'Tomorrow at %s', '0-day-analytics' ),
$event_time_local,
);
} elseif ( $event_local === $yesterday_local ) {
$date = sprintf(
/* translators: %s: Time */
__( 'Yesterday at %s', '0-day-analytics' ),
$event_time_local,
);
} else {
$date = sprintf(
/* translators: 1: Date, 2: Time */
__( '%1$s at %2$s', '0-day-analytics' ),
\get_date_from_gmt( $event_datetime_utc, 'F jS' ),
$event_time_local,
);
}
$time = sprintf(
'',
\esc_attr( gmdate( 'c', $item['schedule'] ) ),
\esc_html( $date )
);
$until = $item['schedule'] - time();
$late = Crons_Helper::is_late( $item );
if ( $late ) {
// Show a warning for events that are late.
$ago = sprintf(
/* translators: %s: Time period, for example "8 minutes" */
__( '%s ago', '0-day-analytics' ),
self::interval( abs( $until ) )
);
return sprintf(
' %s
%s',
esc_html( $ago ),
$time,
) . '
' . \esc_html( $label_string ) . '';
}
if ( $until <= 0 ) {
$in = __( 'Now', '0-day-analytics' );
} else {
$in = sprintf(
/* translators: %s: Time period, for example "8 minutes" */
__( 'In %s', '0-day-analytics' ),
self::interval( $until ),
);
}
return sprintf(
'%s
%s',
\esc_html( $in ),
$time,
);
}
/**
* Check whether we are on an admin and plugin page.
*
* @since 1.8.4
*
* @return bool
*/
public static function is_admin_page(): bool {
return \is_admin() && ! \wp_doing_ajax() && ( Settings::is_plugin_settings_page() );
}
/**
* Converts currently selected timezone into string which can be passed to MySQL server.
*
* @since 3.3.1
*/
public static function get_mysql_time_zone(): string {
$timezone_string = \wp_timezone();
if ( ! $timezone_string instanceof \DateTimeZone ) {
$timezone_string = new \DateTimeZone( (string) $timezone_string );
}
$date = new \DateTime( 'now', $timezone_string );
// create a new date offset by the timezone offset.
// gets the interval as hours & minutes.
$offset = $timezone_string->getOffset( $date ) . ' seconds';
$date_offset = clone $date;
$date_offset->sub( \DateInterval::createFromDateString( $offset ) );
$interval = $date_offset->diff( $date );
$offset = $interval->format( '%R%H:%I' );
return $offset;
}
}
}