first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,740 @@
<?php
/**
* Alerts Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class PUM_Utils_Alerts
*/
class PUM_Utils_Alerts {
/**
*
*/
public static function init() {
add_action( 'admin_init', [ __CLASS__, 'hooks' ] );
add_action( 'admin_init', [ __CLASS__, 'php_handler' ] );
add_action( 'wp_ajax_pum_alerts_action', [ __CLASS__, 'ajax_handler' ] );
add_filter( 'pum_alert_list', [ __CLASS__, 'whats_new_alerts' ], 0 );
add_filter( 'pum_alert_list', [ __CLASS__, 'integration_alerts' ], 5 );
add_filter( 'pum_alert_list', [ __CLASS__, 'translation_request' ], 10 );
add_action( 'admin_menu', [ __CLASS__, 'append_alert_count' ], 999 );
}
/**
* Gets a count of current alerts.
*
* @return int
*/
public static function alert_count() {
return count( self::get_alerts() );
}
/**
* Append alert count to Popup Maker menu item.
*/
public static function append_alert_count() {
global $menu;
$count = self::alert_count();
foreach ( $menu as $key => $item ) {
if ( 'edit.php?post_type=popup' === $item[2] ) {
$menu[ $key ][0] .= $count ? ' <span class="update-plugins count-' . $count . '"><span class="plugin-count pum-alert-count" aria-hidden="true">' . $count . '</span></span>' : '';
}
}
}
/**
* @param array $alerts
*
* @return array
*/
public static function translation_request( $alerts = [] ) {
$version = explode( '.', Popup_Maker::$VER );
// Get only the major.minor version exclude the point releases.
$version = $version[0] . '.' . $version[1];
$code = 'translation_request_' . $version;
// Bail Early if they have already dismissed.
if ( self::has_dismissed_alert( $code ) ) {
return $alerts;
}
// Get locales based on the HTTP accept language header.
$locales_from_header = PUM_Utils_I10n::get_http_locales();
// Abort early if no locales in header.
if ( empty( $locales_from_header ) ) {
return $alerts;
}
// Get acceptable non EN WordPress locales based on the HTTP accept language header.
// Used when the current locale is EN only I believe.
$non_en_locales_from_header = PUM_Utils_I10n::get_non_en_accepted_wp_locales_from_header();
// If no additional languages are supported abort
if ( empty( $non_en_locales_from_header ) ) {
return $alerts;
}
/**
* Assume all at this point are possible polyglots.
*
* Viewing in English!
* -- Translation available in one additional language!
* ---- Show notice that there other language is available and we need help translating.
* -- Translation available in more than one language!
* ---- Show notice that their other languages are available and need help translating.
* -- Translation not available!
* ---- Show notice that plugin is not translated and we need help.
* Else If translation for their language(s) exists, but isn't up to date!
* -- Show notice that their language is available, but out of date and need help translating.
* Else If translations for their language doesn't exist!
* -- Show notice that plugin is not translated and we need help.
*/
$current_locale = function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
// Get the active language packs of the plugin.
$translation_status = PUM_Utils_I10n::translation_status();
// Retrieve all the WordPress locales in which the plugin is translated.
$locales_with_translations = wp_list_pluck( $translation_status, 'language' );
$locale_translation_versions = wp_list_pluck( $translation_status, 'version' );
// Suggests existing langpacks
$suggested_locales_with_langpack = array_values( array_intersect( $non_en_locales_from_header, $locales_with_translations ) );
$current_locale_is_suggested = in_array( $current_locale, $suggested_locales_with_langpack );
$current_locale_is_translated = in_array( $current_locale, $locales_with_translations );
// Last chance to abort early before querying all available languages.
// We abort here if the user is already using a translated language that is up to date!
if ( $current_locale_is_suggested && $current_locale_is_translated && version_compare( $locale_translation_versions[ $current_locale ], Popup_Maker::$VER, '>=' ) ) {
return $alerts;
}
// Retrieve all the WordPress locales.
$locales_supported_by_wordpress = PUM_Utils_I10n::available_locales();
// Get the native language names of the locales.
$suggest_translated_locale_names = [];
foreach ( $suggested_locales_with_langpack as $locale ) {
$suggest_translated_locale_names[ $locale ] = $locales_supported_by_wordpress[ $locale ]['native_name'];
}
$suggest_string = '';
// If we get this far, they clearly have multiple language available
// If current locale is english but they have others available, they are likely polyglots.
$currently_in_english = strpos( $current_locale, 'en' ) === 0;
// Currently in English.
if ( $currently_in_english ) {
// Only one locale suggestion.
if ( 1 === count( $suggest_translated_locale_names ) ) {
$language = current( $suggest_translated_locale_names );
$suggest_string = sprintf( /* translators: %s: native language name. */
__( 'This plugin is also available in %1$s. <a href="%2$s" target="_blank">Help improve the translation!</a>', 'popup-maker' ),
$language,
esc_url( 'https://translate.wordpress.org/projects/wp-plugins/popup-maker' )
);
// Multiple locale suggestions.
} elseif ( ! empty( $suggest_translated_locale_names ) ) {
$primary_language = current( $suggest_translated_locale_names );
array_shift( $suggest_translated_locale_names );
$other_suggest = '';
foreach ( $suggest_translated_locale_names as $language ) {
$other_suggest .= $language . ', ';
}
$suggest_string = sprintf( /* translators: 1: native language name, 2: other native language names, comma separated */
__( 'This plugin is also available in %1$s (also: %2$s). <a href="%3$s" target="_blank">Help improve the translation!</a>', 'popup-maker' ),
$primary_language,
trim( $other_suggest, ' ,' ),
esc_url( 'https://translate.wordpress.org/projects/wp-plugins/popup-maker' )
);
// Non-English locale in header, no translations.
} elseif ( ! empty( $non_en_locales_from_header ) ) {
if ( 1 === count( $non_en_locales_from_header ) ) {
$locale = reset( $non_en_locales_from_header );
$suggest_string = sprintf( /* translators: 1: native language name, 2: URL to translate.wordpress.org */
__( 'This plugin is not translated into %1$s yet. <a href="%2$s" target="_blank">Help translate it!</a>', 'popup-maker' ),
$locales_supported_by_wordpress[ $locale ]['native_name'],
esc_url( 'https://translate.wordpress.org/projects/wp-plugins/popup-maker' )
);
} else {
$primary_locale = reset( $non_en_locales_from_header );
$primary_language = $locales_supported_by_wordpress[ $primary_locale ]['native_name'];
array_shift( $non_en_locales_from_header );
$other_suggest = '';
foreach ( $non_en_locales_from_header as $locale ) {
$other_suggest .= $locales_supported_by_wordpress[ $locale ]['native_name'] . ', ';
}
$suggest_string = sprintf( /* translators: 1: native language name, 2: other native language names, comma separated */
__( 'This plugin is also available in %1$s (also: %2$s). <a href="%3$s" target="_blank">Help improve the translation!</a>', 'popup-maker' ),
$primary_language,
trim( $other_suggest, ' ,' ),
esc_url( 'https://translate.wordpress.org/projects/wp-plugins/popup-maker' )
);
}
}
// The plugin has no translation for the current locale.
} elseif ( ! $current_locale_is_suggested && ! $current_locale_is_translated ) {
$suggest_string = sprintf( __( 'This plugin is not translated into %1$s yet. <a href="%2$s" target="_blank">Help translate it!</a>', 'popup-maker' ), $locales_supported_by_wordpress[ $current_locale ]['native_name'], esc_url( 'https://translate.wordpress.org/projects/wp-plugins/popup-maker' ) );
// The plugin has translations for current locale, but they are out of date.
} elseif ( $current_locale_is_suggested && $current_locale_is_translated && version_compare( $locale_translation_versions[ $current_locale ], Popup_Maker::$VER, '<' ) ) {
$suggest_string = sprintf( /* translators: %s: native language name. */
__( 'This plugin\'s translation for %1$s is out of date. <a href="%2$s" target="_blank">Help improve the translation!</a>', 'popup-maker' ),
$locales_supported_by_wordpress[ $current_locale ]['native_name'],
esc_url( 'https://translate.wordpress.org/projects/wp-plugins/popup-maker' )
);
}
if ( ! empty( $suggest_string ) ) {
$alerts[] = [
'code' => $code,
'message' => $suggest_string,
'type' => 'info',
];
}
return $alerts;
}
/**
* @param array $alerts
*
* @return array
*/
public static function whats_new_alerts( $alerts = [] ) {
$upgraded_from = PUM_Utils_Upgrades::$upgraded_from;
if ( version_compare( $upgraded_from, '0.0.0', '>' ) ) {
if ( version_compare( $upgraded_from, '1.8.0', '<' ) ) {
$alerts[] = [
'code' => 'whats_new_1_8_0',
'type' => 'success',
'message' => sprintf(
'<strong>' . __( 'See whats new in v%1$s - (%2$sview all changes%3$s)', 'popup-maker' ) . '</strong>',
'1.8.0',
'<a href="' . add_query_arg(
[
'tab' => 'plugin-information',
'plugin' => 'popup-maker',
'section' => 'changelog',
'TB_iframe' => true,
'width' => 722,
'height' => 949,
],
admin_url( 'plugin-install.php' )
) . '" target="_blank">',
'</a>'
),
'html' => "<ul class='ul-disc'>" . '<li>' . 'New UX for the Popup Theme editor.' . '</li>' . '<li>' . 'New close button positions: top center, bottom center, middle left & middle right.' . '</li>' . '<li>' . 'New option to position close button outside of popup.' . '</li>' . '</ul>',
'priority' => 100,
];
}
}
return $alerts;
}
/**
* @param array $alerts
*
* @return array
*/
public static function integration_alerts( $alerts = [] ) {
$integrations = [
'buddypress' => [
'label' => __( 'BuddyPress', 'buddypress' ),
'learn_more_url' => 'https://wppopupmaker.com/works-with/buddypress/',
'conditions' => ! class_exists( 'PUM_BuddyPress' ) && ( function_exists( 'buddypress' ) || class_exists( 'BuddyPress' ) ),
'slug' => 'popup-maker-buddypress-integration',
'name' => 'Popup Maker - BuddyPress Integration',
'free' => true,
],
];
foreach ( $integrations as $key => $integration ) {
if ( $integration['conditions'] ) {
$path = "{$integration['slug']}/{$integration['slug']}.php";
$plugin_data = file_exists( WP_PLUGIN_DIR . '/' . $path ) ? get_plugin_data( WP_PLUGIN_DIR . '/' . $path, false, false ) : false;
$installed = $plugin_data && ! empty( $plugin_data['Name'] ) && $plugin_data['Name'] === $integration['name'];
$text = $installed ? __( 'activate it now', 'popup-maker' ) : __( 'install it now', 'popup-maker' );
$url = $installed ? esc_url( wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=' . $path ), 'activate-plugin_' . $path ) ) : esc_url( wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=popup-maker-buddypress-integration' ), 'install-plugin_popup-maker-buddypress-integration' ) );
$alerts[] = [
'code' => $key . '_integration_available',
'message' => sprintf( __( '%1$sDid you know:%2$s Popup Maker has custom integrations with %3$s, %4$slearn more%5$s or %6$s%7$s%8$s!', 'popup-maker' ), '<strong>', '</strong>', $integration['label'], '<a href="' . $integration['learn_more_url'] . '" target="_blank">', '</a>', '<a href="' . $url . '">', $text, '</a>' ),
'dismissible' => true,
'global' => false,
'type' => $installed ? 'warning' : 'info',
];
}
}
return $alerts;
}
/**
* Hook into relevant WP actions.
*/
public static function hooks() {
if ( is_admin() && current_user_can( 'edit_posts' ) ) {
add_action( 'admin_notices', [ __CLASS__, 'admin_notices' ] );
add_action( 'network_admin_notices', [ __CLASS__, 'admin_notices' ] );
add_action( 'user_admin_notices', [ __CLASS__, 'admin_notices' ] );
}
}
/**
* @return bool
*/
public static function should_show_alerts() {
return in_array(
true,
[
pum_is_admin_page(),
count( self::get_global_alerts() ) > 0,
]
);
}
/**
* Allow additional style properties for notice alerts.
*
* @param array $styles Array of allowed style properties.
* @return array
*/
public static function allow_inline_styles( $styles ) {
$styles[] = 'display';
$styles[] = 'list-style';
$styles[] = 'margin';
$styles[] = 'padding';
$styles[] = 'margin-left';
$styles[] = 'margin-right';
$styles[] = 'margin-top';
$styles[] = 'margin-bottom';
return $styles;
}
/**
* Return array of allowed html tags.
*
* @return array
*/
public static function allowed_tags() {
return array_merge_recursive(
wp_kses_allowed_html( 'post' ),
// Allow script tags with type="" attribute.
[
'script' => [ 'type' => true ],
'progress' => [
'class' => true,
'max' => true,
'min' => true,
],
'input' => [
'type' => true,
'class' => true,
'id' => true,
'name' => true,
'value' => true,
'checked' => true,
],
'button' => [
'type' => true,
'class' => true,
'id' => true,
'name' => true,
'value' => true,
],
'fieldset' => [
'class' => true,
'id' => true,
'name' => true,
],
'legend' => [
'class' => true,
'id' => true,
'name' => true,
],
'div' => [
'class' => true,
'id' => true,
'name' => true,
],
'span' => [
'class' => true,
'id' => true,
'name' => true,
],
'ul' => [
'class' => true,
'id' => true,
'name' => true,
],
'li' => [
'class' => true,
'id' => true,
'name' => true,
],
'label' => [
'class' => true,
'id' => true,
'name' => true,
'for' => true,
],
'select' => [
'class' => true,
'id' => true,
'name' => true,
'for' => true,
],
'option' => [
'class' => true,
'id' => true,
'name' => true,
'for' => true,
],
'form' => [
'action' => true,
'method' => true,
'id' => true,
'class' => true,
'style' => true,
'data-*' => true,
],
'img' => [
'class' => true,
'id' => true,
'name' => true,
'src' => true,
'alt' => true,
'width' => true,
'height' => true,
],
]
);
}
/**
* Render admin alerts if available.
*/
public static function admin_notices() {
if ( ! self::should_show_alerts() ) {
return;
}
$global_only = ! pum_is_admin_page();
$alerts = $global_only ? self::get_global_alerts() : self::get_alerts();
$count = count( $alerts );
if ( ! $count ) {
return;
}
wp_enqueue_script( 'pum-admin-general' );
wp_enqueue_style( 'pum-admin-general' );
$nonce = wp_create_nonce( 'pum_alerts_action' );
?>
<script type="text/javascript">
window.pum_alerts_nonce = '<?php echo $nonce; ?>';
</script>
<div class="pum-alerts">
<h3>
<img alt="" class="logo" width="30" src="<?php echo Popup_Maker::$URL; ?>assets/images/logo.png" /> <?php printf( '%s%s (%s)', ( $global_only ? __( 'Popup Maker', 'popup-maker' ) . ' ' : '' ), __( 'Notifications', 'popup-maker' ), '<span class="pum-alert-count">' . $count . '</span>' ); ?>
</h3>
<p><?php __( 'Check out the following notifications from Popup Maker.', 'popup-maker' ); ?></p>
<?php
add_filter( 'safe_style_css', [ __CLASS__, 'allow_inline_styles' ] );
foreach ( $alerts as $alert ) {
$expires = 1 === $alert['dismissible'] ? '' : $alert['dismissible'];
$dismiss_url = add_query_arg(
[
'nonce' => $nonce,
'code' => $alert['code'],
'pum_dismiss_alert' => 'dismiss',
'expires' => $expires,
]
);
?>
<div class="pum-alert-holder" data-code="<?php echo esc_attr( $alert['code'] ); ?>" class="<?php echo $alert['dismissible'] ? 'is-dismissible' : ''; ?>" data-dismissible="<?php echo esc_attr( $alert['dismissible'] ); ?>">
<div class="pum-alert <?php echo '' !== $alert['type'] ? 'pum-alert__' . esc_attr( $alert['type'] ) : ''; ?>">
<?php if ( ! empty( $alert['message'] ) ) : ?>
<p><?php echo wp_kses_post( $alert['message'] ); ?></p>
<?php endif; ?>
<?php if ( ! empty( $alert['html'] ) ) : ?>
<?php
echo wp_kses(
function_exists( 'wp_encode_emoji' ) ? wp_encode_emoji( $alert['html'] ) : $alert['html'],
self::allowed_tags()
);
?>
<?php endif; ?>
<?php if ( ! empty( $alert['actions'] ) && is_array( $alert['actions'] ) ) : ?>
<ul>
<?php
foreach ( $alert['actions'] as $action ) {
$link_text = ! empty( $action['primary'] ) && true === $action['primary'] ? '<strong>' . esc_html( $action['text'] ) . '</strong>' : esc_html( $action['text'] );
if ( 'link' === $action['type'] ) {
$url = $action['href'];
$attributes = 'target="_blank" rel="noreferrer noopener"';
} else {
$url = add_query_arg(
[
'nonce' => $nonce,
'code' => $alert['code'],
'pum_dismiss_alert' => $action['action'],
'expires' => $expires,
]
);
$attributes = 'class="pum-dismiss"';
}
?>
<li><a data-action="<?php echo esc_attr( $action['action'] ); ?>" href="<?php echo esc_url( $url ); ?>" <?php echo $attributes; ?> ><?php echo $link_text; ?></a></li>
<?php } ?>
</ul>
<?php endif; ?>
</div>
<?php if ( $alert['dismissible'] ) : ?>
<a href="<?php echo esc_url( $dismiss_url ); ?>" data-action="dismiss" class="button dismiss pum-dismiss">
<span class="screen-reader-text"><?php _e( 'Dismiss this item.', 'popup-maker' ); ?></span> <span class="dashicons dashicons-no-alt"></span>
</a>
<?php endif; ?>
</div>
<?php } ?>
</div>
<?php
remove_filter( 'safe_style_css', [ __CLASS__, 'allow_inline_styles' ] );
}
/**
* @return array
*/
public static function get_global_alerts() {
$alerts = self::get_alerts();
$global_alerts = [];
foreach ( $alerts as $alert ) {
if ( $alert['global'] ) {
$global_alerts[] = $alert;
}
}
return $global_alerts;
}
/**
* @return array
*/
public static function get_alerts() {
static $alert_list;
if ( ! isset( $alert_list ) ) {
$alert_list = apply_filters( 'pum_alert_list', [] );
}
$alerts = [];
foreach ( $alert_list as $alert ) {
// Ignore dismissed alerts.
if ( self::has_dismissed_alert( $alert['code'] ) ) {
continue;
}
$alerts[] = wp_parse_args(
$alert,
[
'code' => 'default',
'priority' => 10,
'message' => '',
'type' => 'info',
'html' => '',
'dismissible' => true,
'global' => false,
]
);
}
// Sort alerts by priority, highest to lowest.
$alerts = PUM_Utils_Array::sort( $alerts, 'priority', true );
return $alerts;
}
/**
* Handles if alert was dismissed AJAX
*/
public static function ajax_handler() {
$args = wp_parse_args(
$_REQUEST,
[
'code' => '',
'expires' => '',
'pum_dismiss_alert' => '',
]
);
if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'pum_alerts_action' ) ) {
wp_send_json_error();
}
$results = self::action_handler( $args['code'], $args['pum_dismiss_alert'], $args['expires'] );
if ( true === $results ) {
wp_send_json_success();
} else {
wp_send_json_error();
}
}
/**
* Handles if alert was dismissed by page reload instead of AJAX
*
* @since 1.11.0
*/
public static function php_handler() {
if ( ! isset( $_REQUEST['pum_dismiss_alert'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'pum_alerts_action' ) ) {
return;
}
$args = wp_parse_args(
$_REQUEST,
[
'code' => '',
'expires' => '',
'pum_dismiss_alert' => '',
]
);
self::action_handler( $args['code'], $args['pum_dismiss_alert'], $args['expires'] );
}
/**
* Handles the action taken on the alert.
*
* @param string $code The specific alert.
* @param string $action Which action was taken
* @param string $expires When the dismissal expires, if any.
*
* @return bool
* @uses PUM_Utils_Logging::instance
* @uses PUM_Utils_Logging::log
* @since 1.11.0
*/
public static function action_handler( $code, $action, $expires ) {
if ( empty( $action ) || 'dismiss' === $action ) {
try {
$dismissed_alerts = self::dismissed_alerts();
$dismissed_alerts[ $code ] = ! empty( $expires ) ? strtotime( '+' . $expires ) : true;
$user_id = get_current_user_id();
update_user_meta( $user_id, '_pum_dismissed_alerts', $dismissed_alerts );
return true;
} catch ( Exception $e ) {
PUM_Utils_Logging::instance()->log( 'Error dismissing alert. Exception: ' . $e->getMessage() );
return false;
}
}
do_action( 'pum_alert_dismissed', $code, $action );
}
/**
* @param string $code
*
* @return bool
*/
public static function has_dismissed_alert( $code = '' ) {
$dimissed_alerts = self::dismissed_alerts();
$alert_dismissed = array_key_exists( $code, $dimissed_alerts );
// If the alert was dismissed and has a non true type value, it is an expiry time.
if ( $alert_dismissed && true !== $dimissed_alerts[ $code ] ) {
return strtotime( 'now' ) < $dimissed_alerts[ $code ];
}
return $alert_dismissed;
}
/**
* Returns an array of dismissed alert groups.
*
* @return array
*/
public static function dismissed_alerts() {
$user_id = get_current_user_id();
$dismissed_alerts = get_user_meta( $user_id, '_pum_dismissed_alerts', true );
if ( ! is_array( $dismissed_alerts ) ) {
$dismissed_alerts = [];
update_user_meta( $user_id, '_pum_dismissed_alerts', $dismissed_alerts );
}
return $dismissed_alerts;
}
}

View File

@@ -0,0 +1,606 @@
<?php
/**
* Array Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class PUM_Utils_Array
*
* Various functions to help manipulating arrays.
*/
class PUM_Utils_Array {
/**
* Filters out null values.
*
* @param array $array
*
* @return array
*/
public static function filter_null( $array = [] ) {
return array_filter( $array, [ __CLASS__, '_filter_null' ] );
}
/**
* @param null $val
*
* @return bool
*/
public static function _filter_null( $val = null ) {
return isset( $val );
}
/**
* Clean variables using sanitize_text_field.
*
* @param $var
*
* @return array|string
*/
public static function sanitize( $var ) {
if ( is_string( $var ) ) {
return sanitize_text_field( $var );
}
return array_map( [ __CLASS__, 'sanitize' ], (array) $var );
}
/**
* Helper function to move or swap array keys in various ways.
*
* PUM_Utils_Array::move_item($arr, 'move me', 'up'); //move it one up
* PUM_Utils_Array::move_item($arr, 'move me', 'down'); //move it one down
* PUM_Utils_Array::move_item($arr, 'move me', 'top'); //move it to top
* PUM_Utils_Array::move_item($arr, 'move me', 'bottom'); //move it to bottom
*
* PUM_Utils_Array::move_item($arr, 'move me', -1); //move it one up
* PUM_Utils_Array::move_item($arr, 'move me', 1); //move it one down
* PUM_Utils_Array::move_item($arr, 'move me', 2); //move it two down
*
* PUM_Utils_Array::move_item($arr, 'move me', 'before', 'b'); //move it before ['b']
* PUM_Utils_Array::move_item($arr, 'move me', 'up', 'b'); //move it before ['b']
* PUM_Utils_Array::move_item($arr, 'move me', -1, 'b'); //move it before ['b']
* PUM_Utils_Array::move_item($arr, 'move me', 'after', 'b'); //move it after ['b']
* PUM_Utils_Array::move_item($arr, 'move me', 'down', 'b'); //move it after ['b']
* PUM_Utils_Array::move_item($arr, 'move me', 1, 'b'); //move it after ['b']
* PUM_Utils_Array::move_item($arr, 'move me', 2, 'b'); //move it two positions after ['b']
*
* Special syntax, to swap two elements:
* PUM_Utils_Array::move_item($arr, 'a', 0, 'd'); //Swap ['a'] with ['d']
* PUM_Utils_Array::move_item($arr, 'a', 'swap', 'd'); //Swap ['a'] with ['d']
*
* @param array $ref_arr
* @param string $key1
* @param int|string $move
* @param string|null $key2
*
* @return bool
*/
public static function move_item( &$ref_arr, $key1, $move, $key2 = null ) {
$arr = $ref_arr;
if ( null === $key2 ) {
$key2 = $key1;
}
if ( ! isset( $arr[ $key1 ] ) || ! isset( $arr[ $key2 ] ) ) {
return false;
}
$i = 0;
foreach ( $arr as &$val ) {
$val = [
'sort' => ( ++ $i * 10 ),
'val' => $val,
];
}
// Add a quick keyword `swap` to make syntax simpler to remember.
if ( 'swap' === $move ) {
$move = 0;
}
if ( is_numeric( $move ) ) {
if ( 0 === $move && $key1 === $key2 ) {
return true;
} elseif ( 0 === $move ) {
$tmp = $arr[ $key1 ]['sort'];
$arr[ $key1 ]['sort'] = $arr[ $key2 ]['sort'];
$arr[ $key2 ]['sort'] = $tmp;
} else {
$arr[ $key1 ]['sort'] = $arr[ $key2 ]['sort'] + ( $move * 10 + ( $key1 === $key2 ? ( $move < 0 ? - 5 : 5 ) : 0 ) );
}
} else {
switch ( $move ) {
case 'up':
case 'before':
$arr[ $key1 ]['sort'] = $arr[ $key2 ]['sort'] - ( $key1 === $key2 ? 15 : 5 );
break;
case 'down':
case 'after':
$arr[ $key1 ]['sort'] = $arr[ $key2 ]['sort'] + ( $key1 === $key2 ? 15 : 5 );
break;
case 'top':
$arr[ $key1 ]['sort'] = 5;
break;
case 'bottom':
$arr[ $key1 ]['sort'] = $i * 10 + 5;
break;
default:
return false;
}
}
uasort( $arr, [ __CLASS__, 'sort_by_sort' ] );
foreach ( $arr as &$val ) {
$val = $val['val'];
}
$ref_arr = $arr;
return true;
}
/**
* Pluck all array keys beginning with string.
*
* @param array $array
* @param bool|string|array $strings
*
* @return array
*/
public static function pluck_keys_starting_with( $array, $strings = [] ) {
$to_be_removed = self::remove_keys_starting_with( $array, $strings );
return array_diff_key( $array, $to_be_removed );
}
/**
* Pluck all array keys ending with string.
*
* @param array $array
* @param bool|string|array $strings
*
* @return array
*/
public static function pluck_keys_ending_with( $array, $strings = [] ) {
$to_be_removed = self::remove_keys_ending_with( $array, $strings );
return array_diff_key( $array, $to_be_removed );
}
/**
* Extract only allowed keys from an array.
*
* @param array $array Array to be extracted from.
* @param string[] $allowed_keys List of keys.
*
* @return array
*/
public static function allowed_keys( $array, $allowed_keys = [] ) {
return array_intersect_key( $array, array_flip( $allowed_keys ) );
}
/**
* This works exactly the same as wp_parse_args, except we remove unused keys for sanitization.
*
* @param array $array Array to be parsed.
* @param array $allowed_args Array of key=>defaultValue pairs for each allowed argument.
*
* @return array
*/
public static function parse_allowed_args( $array, $allowed_args = [] ) {
$array = wp_parse_args( $array, $allowed_args );
return self::allowed_keys( $array, array_keys( $allowed_args ) );
}
/**
* Pluck specified array keys.
*
* @param array $array
* @param string[] $keys
*
* @return array
*/
public static function pluck( $array, $keys = [] ) {
return self::pluck_keys_containing( $array, $keys );
}
/**
* Pluck all array keys containing a string or strings.
*
* @param array $array
* @param string[] $strings
*
* @return array
*/
public static function pluck_keys_containing( $array, $strings = [] ) {
$to_be_removed = self::remove_keys_containing( $array, $strings );
return array_diff_key( $array, $to_be_removed );
}
/**
* Remove all array keys beginning with string.
*
* @param array $array
* @param string[] $strings
*
* @return array
*/
public static function remove_keys_starting_with( $array, $strings = [] ) {
if ( ! $strings ) {
return $array;
}
if ( ! is_array( $strings ) ) {
$strings = [ $strings ];
}
foreach ( $array as $key => $value ) {
foreach ( $strings as $string ) {
if ( strpos( $key, $string ) === 0 ) {
unset( $array[ $key ] );
}
}
}
return $array;
}
/**
* Remove all array keys ending with string.
*
* @param array $array
* @param bool|string|array $strings
*
* @return array
*/
public static function remove_keys_ending_with( $array, $strings = [] ) {
if ( ! $strings ) {
return $array;
}
if ( ! is_array( $strings ) ) {
$strings = [ $strings ];
}
foreach ( $array as $key => $value ) {
foreach ( $strings as $string ) {
$length = strlen( $string );
if ( substr( $key, - $length ) === $string ) {
unset( $array[ $key ] );
}
}
}
return $array;
}
/**
* Remove all array keys containing string.
*
* @param array $array
* @param bool|string|array $strings
*
* @return array
*/
public static function remove_keys_containing( $array, $strings = [] ) {
if ( ! $strings ) {
return $array;
}
if ( ! is_array( $strings ) ) {
$strings = [ $strings ];
}
foreach ( $array as $key => $value ) {
foreach ( $strings as $string ) {
if ( strpos( $key, $string ) !== false ) {
unset( $array[ $key ] );
}
}
}
return $array;
}
/**
* Remove all array keys containing string.
*
* @param array $array
* @param string|array $keys
*
* @return array
*/
public static function remove_keys( $array, $keys = [] ) {
if ( empty( $keys ) ) {
return $array;
}
if ( is_string( $keys ) ) {
$keys = [ $keys ];
}
foreach ( (array) $keys as $key ) {
if ( is_string( $key ) && array_key_exists( $key, $array ) ) {
unset( $array[ $key ] );
}
}
return $array;
}
/**
* Sort nested arrays with various options.
*
* @param array $array
* @param string $type
* @param bool $reverse
*
* @return array
*/
public static function sort( $array = [], $type = 'key', $reverse = false ) {
if ( ! is_array( $array ) ) {
return $array;
}
switch ( $type ) {
case 'key':
if ( ! $reverse ) {
ksort( $array );
} else {
krsort( $array );
}
break;
case 'natural':
natsort( $array );
break;
case 'priority':
if ( ! $reverse ) {
uasort( $array, [ __CLASS__, 'sort_by_priority' ] );
} else {
uasort( $array, [ __CLASS__, 'rsort_by_priority' ] );
}
break;
}
return $array;
}
/**
* @param $a
* @param $b
*
* @return bool
*/
public static function sort_by_sort( $a, $b ) {
return $a['sort'] > $b['sort'];
}
/**
* Sort array by priority value
*
* @param $a
* @param $b
*
* @return int
*/
public static function sort_by_priority( $a, $b ) {
$pri_a = isset( $a['pri'] ) ? $a['pri'] : ( isset( $a['priority'] ) ? $a['priority'] : false );
$pri_b = isset( $b['pri'] ) ? $b['pri'] : ( isset( $b['priority'] ) ? $b['priority'] : false );
if ( ! is_numeric( $pri_a ) || ! is_numeric( $pri_b ) || $pri_a === $pri_b ) {
return 0;
}
return ( $pri_a < $pri_b ) ? - 1 : 1;
}
/**
* Sort array in reverse by priority value
*
* @param $a
* @param $b
*
* @return int
*/
public static function rsort_by_priority( $a, $b ) {
$pri_a = isset( $a['pri'] ) ? $a['pri'] : ( isset( $a['priority'] ) ? $a['priority'] : false );
$pri_b = isset( $b['pri'] ) ? $b['pri'] : ( isset( $b['priority'] ) ? $b['priority'] : false );
if ( ! is_numeric( $pri_a ) || ! is_numeric( $pri_b ) || $pri_a === $pri_b ) {
return 0;
}
return ( $pri_a < $pri_b ) ? 1 : - 1;
}
/**
* Replace array key with new key name in same order
*
* @param $array
* @param $old_key
* @param $new_key
*
* @return array
*/
public static function replace_key( $array, $old_key, $new_key ) {
$keys = array_keys( $array );
if ( false === $index = array_search( $old_key, $keys, true ) ) {
// throw new \Exception( sprintf( 'Key "%s" does not exit', $old_key ) );
}
$keys[ $index ] = $new_key;
return array_combine( $keys, array_values( $array ) );
}
/**
* Converts 'false' & 'true' string values in any array to proper boolean values.
*
* @param array|mixed $data
*
* @return array|mixed
*/
public static function fix_json_boolean_values( $data ) {
if ( is_array( $data ) ) {
foreach ( (array) $data as $key => $value ) {
if ( is_string( $value ) && in_array( $value, [ 'true', 'false' ] ) ) {
$data[ $key ] = json_decode( $value );
} elseif ( is_array( $value ) ) {
$data[ $key ] = self::fix_json_boolean_values( $value );
}
}
}
return $data;
}
/**
* @param $obj
*
* @return array
*/
public static function from_object( $obj ) {
if ( is_object( $obj ) ) {
$obj = (array) $obj;
}
if ( is_array( $obj ) ) {
$new = [];
foreach ( $obj as $key => $val ) {
$new[ $key ] = self::from_object( $val );
}
} else {
$new = $obj;
}
return $new;
}
/**
* @param $array
*
* @return array
*/
public static function safe_json_decode( $array ) {
if ( ! empty( $array ) && is_string( $array ) ) {
if ( strpos( $array, '\"' ) >= 0 ) {
$array = stripslashes( $array );
}
$array = json_decode( $array );
$array = self::from_object( $array );
$array = self::fix_json_boolean_values( $array );
}
return (array) $array;
}
/**
* Ensures proper encoding for strings before json_encode is used.
*
* @param array|string $data
*
* @return mixed|string
*/
public static function safe_json_encode( $data = [] ) {
return wp_json_encode( self::make_safe_for_json_encode( $data ) );
}
/**
* json_encode only accepts valid UTF8 characters, thus we need to properly convert translations and other data to proper utf.
*
* This function does that recursively.
*
* @param array|string $data
*
* @return array|string
*/
public static function make_safe_for_json_encode( $data = [] ) {
if ( is_scalar( $data ) ) {
return html_entity_decode( (string) $data, ENT_QUOTES, 'UTF-8' );
}
if ( is_array( $data ) ) {
foreach ( (array) $data as $key => $value ) {
if ( is_scalar( $value ) && ! is_bool( $value ) ) {
$data[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
} elseif ( is_array( $value ) ) {
$data[ $key ] = self::make_safe_for_json_encode( $value );
}
}
}
return $data;
}
/**
* @param $d
*
* @return array|string
*/
public static function utf8_encode_recursive( $d ) {
if ( is_array( $d ) ) {
foreach ( $d as $k => $v ) {
$d[ $k ] = self::utf8_encode_recursive( $v );
}
} elseif ( is_string( $d ) ) {
return utf8_encode( $d );
}
return $d;
}
/**
* @param $value
* @param bool $encode
*
* @return string
*/
public static function maybe_json_attr( $value, $encode = false ) {
if ( is_object( $value ) || is_array( $value ) ) {
return $encode ? htmlspecialchars( json_encode( $value ) ) : json_encode( $value );
}
return $value;
}
/**
* Remaps array keys.
*
* @param array $array an array values.
* @param array $remap_array an array of $old_key => $new_key values.
*
* @return array
*/
public static function remap_keys( $array, $remap_array = [] ) {
foreach ( $remap_array as $old_key => $new_key ) {
$value = isset( $array[ $old_key ] ) ? $array[ $old_key ] : false;
if ( ! empty( $value ) ) {
$array[ $new_key ] = $value;
}
unset( $array[ $old_key ] );
}
return $array;
}
}

View File

@@ -0,0 +1,111 @@
<?php
/**
* CSS Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
class PUM_Utils_CSS {
/**
* @param string $hex
* @param string $return_type
*
* @return array|string
*/
public static function hex2rgb( $hex = '#ffffff', $return_type = 'rgb' ) {
if ( is_array( $hex ) ) {
$hex = implode( '', $hex );
}
$hex = str_replace( '#', '', $hex );
if ( strlen( $hex ) === 3 ) {
$r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) );
$g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) );
$b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) );
} else {
$r = hexdec( substr( $hex, 0, 2 ) );
$g = hexdec( substr( $hex, 2, 2 ) );
$b = hexdec( substr( $hex, 4, 2 ) );
}
$rgb = [ $r, $g, $b ];
if ( 'array' === $return_type ) {
return $rgb; // returns an array with the rgb values
}
return 'rgb(' . implode( ',', $rgb ) . ')'; // returns the rgb values separated by commas
}
/**
* @param string $hex
* @param int $opacity
*
* @return string
*/
public static function hex2rgba( $hex = '#ffffff', $opacity = 100 ) {
$rgb = self::hex2rgb( $hex, 'array' );
$opacity = number_format( intval( $opacity ) / 100, 2 );
return 'rgba( ' . implode( ', ', $rgb ) . ', ' . $opacity . ' )';
}
/**
* @param int $thickness
* @param string $style
* @param string $color
*
* @return string
*/
public static function border_style( $thickness = 1, $style = 'solid', $color = '#cccccc' ) {
return "{$thickness}px {$style} {$color}";
}
/**
* @param int $horizontal
* @param int $vertical
* @param int $blur
* @param int $spread
* @param string $hex
* @param int $opacity
* @param string $inset
*
* @return string
*/
public static function box_shadow_style( $horizontal = 0, $vertical = 0, $blur = 0, $spread = 0, $hex = '#000000', $opacity = 50, $inset = 'no' ) {
return "{$horizontal}px {$vertical}px {$blur}px {$spread}px " . self::hex2rgba( $hex, $opacity ) . ( 'yes' === $inset ? ' inset' : '' );
}
/**
* @param int $horizontal
* @param int $vertical
* @param int $blur
* @param string $hex
* @param int $opacity
*
* @return string
*/
public static function text_shadow_style( $horizontal = 0, $vertical = 0, $blur = 0, $hex = '#000000', $opacity = 50 ) {
return "{$horizontal}px {$vertical}px {$blur}px " . self::hex2rgba( $hex, $opacity );
}
/**
* @param int|string $size
* @param int|string $weight
* @param float|int|string $line_height
* @param string $family
* @param string|null $style
* @param string|null $variant
*
* @return mixed
*/
public static function font_style( $size = 16, $weight = 300, $line_height = 1.2, $family = 'Times New Roman', $style = null, $variant = null ) {
$size = is_int( $size ) ? "{$size}px" : $size;
$line_height = is_int( $line_height ) ? "{$line_height}px" : $line_height;
return str_replace( ' ', ' ', trim( "$style $variant $weight {$size}/{$line_height} \"$family\"" ) );
}
}

View File

@@ -0,0 +1,176 @@
<?php
/**
* Cache Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class PUM_Utils_Cache
*/
class PUM_Utils_Cache {
/**
* @var string
*/
static $prefix = 'pum';
/**
* @return bool
*/
public static function enabled() {
return (bool) ! pum_get_option( 'disable_cache', false );
}
/**
* Returns the general
*
* @param string $string
*
* @return string
*/
public static function prefix_( $string = '' ) {
return empty( $string ) ? self::$prefix : self::$prefix . '_' . $string;
}
/**
* @param $key
* @param string $group
*
* @return mixed
*/
public static function get_timeout( $key, $group = '' ) {
return apply_filters( 'pum_cache_timeout', pum_cache_timeout( $group ), $key, $group );
}
/**
* @param $key
* @param $data
* @param string $group
*
* @return bool
*/
public static function add( $key, $data, $group = '' ) {
if ( ! self::enabled() ) {
return true;
}
return wp_cache_add( $key, $data, self::prefix_( $group ), self::get_timeout( $key, $group ) );
}
/**
* @param $key
* @param $data
* @param string $group
*
* @return bool
*/
public static function replace( $key, $data, $group = '' ) {
if ( ! self::enabled() ) {
return true;
}
return wp_cache_replace( $key, $data, self::prefix_( $group ), self::get_timeout( $key, $group ) );
}
/**
* @param $key
* @param $data
* @param string $group
*
* @return bool
*/
public static function set( $key, $data, $group = '' ) {
if ( ! self::enabled() ) {
return true;
}
return wp_cache_set( $key, $data, self::prefix_( $group ), self::get_timeout( $key, $group ) );
}
/**
* @param $key
* @param string $group
* @param bool $force
* @param null $found
*
* @return bool|mixed
*/
public static function get( $key, $group = '', $force = false, &$found = null ) {
if ( ! self::enabled() ) {
return false;
}
return wp_cache_get( $key, self::prefix_( $group ), $force, $found );
}
/**
* @param $key
* @param string $group
*
* @return bool
*/
public static function delete( $key, $group = '' ) {
if ( ! self::enabled() ) {
return true;
}
return wp_cache_delete( $key, self::prefix_( $group ) );
}
/**
* @param string $group
*
* @return bool
*/
public static function delete_group( $group = '' ) {
if ( ! self::enabled() ) {
return true;
}
if ( ! function_exists( 'wp_cache_delete_group' ) ) {
return false;
}
return wp_cache_delete_group( self::prefix_( $group ) );
}
/**
* @param $key
* @param int $offset
* @param string $group
*
* @return bool|false|int
*/
public static function incr( $key, $offset = 1, $group = '' ) {
if ( ! self::enabled() ) {
return true;
}
return wp_cache_incr( $key, $offset, self::prefix_( $group ) );
}
/**
* @param $key
* @param int $offset
* @param string $group
*
* @return bool|false|int
*/
public static function decr( $key, $offset = 1, $group = '' ) {
if ( ! self::enabled() ) {
return true;
}
return wp_cache_decr( $key, $offset, self::prefix_( $group ) );
}
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* Config Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class PUM_Utils_Config
*/
class PUM_Utils_Config {
/**
* Config
*
* @param $file_name
*
* @return mixed
*/
public static function load( $file_name ) {
$file_name = str_replace( '\\', DIRECTORY_SEPARATOR, $file_name );
$file = plugin_dir_path( __DIR__ ) . DIRECTORY_SEPARATOR . 'configs' . DIRECTORY_SEPARATOR . $file_name . '.php';
if ( ! file_exists( $file ) ) {
return [];
}
return include $file;
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* Cron Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class PUM_Utils_Cron
*
* @since 1.8.0
*/
class PUM_Utils_Cron {
/**
* PUM_Utils_Cron constructor.
*/
public function __construct() {
add_filter( 'cron_schedules', [ $this, 'add_schedules' ] );
add_action( 'wp', [ $this, 'schedule_events' ] );
}
/**
* Registers new cron schedules
*
* @param array $schedules
*
* @return array
*/
public function add_schedules( $schedules = [] ) {
// Adds once weekly to the existing schedules.
$schedules['weekly'] = [
'interval' => 604800,
'display' => __( 'Once Weekly', 'popup-maker' ),
];
return $schedules;
}
/**
* Schedules our events
*/
public function schedule_events() {
$this->weekly_events();
$this->daily_events();
}
/**
* Schedule weekly events
*/
private function weekly_events() {
if ( ! wp_next_scheduled( 'pum_weekly_scheduled_events' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'pum_weekly_scheduled_events' );
}
}
/**
* Schedule daily events
*/
private function daily_events() {
if ( ! wp_next_scheduled( 'pum_daily_scheduled_events' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'pum_daily_scheduled_events' );
}
}
}

View File

@@ -0,0 +1,124 @@
<?php
/**
* DataStorage Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Initializes a temporary data storage engine used by core in various capacities.
*/
class PUM_Utils_DataStorage {
/**
* Retrieves stored data by key.
*
* Given a key, get the information from the database directly.
*
* @param string $key The stored option key.
* @param null|mixed $default Optional. A default value to retrieve should `$value` be empty.
* Default null.
*
* @return mixed|false The stored data, value of `$default` if not null, otherwise false.
*/
public static function get( $key, $default = null ) {
global $wpdb;
$value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = '%s'", $key ) );
if ( empty( $value ) && ! is_null( $default ) ) {
return $default;
}
return empty( $value ) ? false : maybe_unserialize( $value );
}
/**
* Write some data based on key and value.
*
* @param string $key The option_name.
* @param mixed $value The value to store.
*/
public static function write( $key, $value ) {
global $wpdb;
$value = maybe_serialize( $value );
$data = [
'option_name' => $key,
'option_value' => $value,
'autoload' => 'no',
];
$formats = self::get_data_formats( $value );
$wpdb->replace( $wpdb->options, $data, $formats );
}
/**
* Derives the formats array based on the type of $value.
*
* @param mixed $value Value to store.
*
* @return array Formats array. First and last values will always be string ('%s').
*/
public static function get_data_formats( $value ) {
switch ( gettype( $value ) ) {
case 'integer':
$formats = [ '%s', '%d', '%s' ];
break;
case 'double':
$formats = [ '%s', '%f', '%s' ];
break;
default:
case 'string':
$formats = [ '%s', '%s', '%s' ];
break;
}
return $formats;
}
/**
* Deletes a piece of stored data by key.
*
* @param string $key The stored option name to delete.
*
* @return int|false The number of rows deleted, or false on error.
*/
public static function delete( $key ) {
global $wpdb;
return $wpdb->delete( $wpdb->options, [ 'option_name' => $key ] );
}
/**
* Deletes all options matching a given RegEx pattern.
*
* @param string $pattern Pattern to match against option keys.
*
* @return int|false The number of rows deleted, or false on error.
*/
public static function delete_by_match( $pattern ) {
global $wpdb;
// Double check to make sure the batch_id got included before proceeding.
if ( '^[0-9a-z\\_]+' !== $pattern && ! empty( $pattern ) ) {
$query = "DELETE FROM $wpdb->options WHERE option_name REGEXP %s";
$result = $wpdb->query( $wpdb->prepare( $query, $pattern ) );
} else {
$result = false;
}
return $result;
}
}

View File

@@ -0,0 +1,397 @@
<?php
/**
* Fields Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class PUM_Utils_Fields
*/
class PUM_Utils_Fields {
/**
* @param $fields
* @param $field_id
*
* @return bool|mixed
*/
public static function get_field( $fields, $field_id ) {
$fields = static::flatten_fields_array( $fields );
return isset( $fields[ $field_id ] ) ? $fields[ $field_id ] : false;
}
/**
* @param array $fields
*
* @return array
*/
public static function get_form_default_values( $fields = [] ) {
$fields = static::flatten_fields_array( $fields );
return static::get_field_default_values( $fields );
}
/**
* @param array $fields
*
* @return array
*/
public static function get_field_default_values( $fields = [] ) {
$defaults = [];
foreach ( $fields as $field_id => $field ) {
switch ( $field['type'] ) {
case 'checkbox':
$defaults[ $field_id ] = ! empty( $field['std'] ) ? $field['std'] : false;
break;
default:
$defaults[ $field_id ] = isset( $field['std'] ) ? $field['std'] : null;
}
}
return $defaults;
}
/**
* @param $tabs
*
* @return array
*/
public static function flatten_fields_array( $tabs ) {
$fields = [];
foreach ( $tabs as $tab_id => $tab_sections ) {
if ( self::is_field( $tab_sections ) ) {
$fields[ $tab_id ] = $tab_sections;
continue;
} else {
foreach ( $tab_sections as $section_id => $section_fields ) {
if ( self::is_field( $tab_sections ) ) {
$fields[ $section_id ] = $section_fields;
continue;
}
foreach ( $section_fields as $field_id => $field ) {
$fields[ $field_id ] = $field;
continue;
}
}
}
}
return $fields;
}
/**
* @param $field
*
* @return array
*/
public static function parse_field( $field ) {
return wp_parse_args(
$field,
[
'section' => 'main',
'type' => 'text',
'id' => null,
'label' => '',
'desc' => '',
'name' => null,
'templ_name' => null,
'size' => 'regular',
'options' => [],
'std' => null,
'rows' => 5,
'cols' => 50,
'min' => 0,
'max' => 50,
'force_minmax' => false,
'step' => 1,
'select2' => null,
'object_type' => 'post_type',
'object_key' => 'post',
'post_type' => null,
'taxonomy' => null,
'multiple' => null,
'as_array' => false,
'placeholder' => null,
'checkbox_val' => 1,
'allow_blank' => true,
'readonly' => false,
'required' => false,
'disabled' => false,
'hook' => null,
'unit' => __( 'ms', 'popup-maker' ),
'desc_position' => 'bottom',
'units' => [
'px' => 'px',
'%' => '%',
'em' => 'em',
'rem' => 'rem',
],
'priority' => 10,
'doclink' => '',
'button_type' => 'submit',
'class' => '',
'messages' => [],
'license_status' => '',
'value' => null,
'private' => false,
]
);
}
/**
* @param $fields
* @param array $args
*
* @return mixed
*/
public static function parse_tab_fields( $fields, $args = [] ) {
$args = wp_parse_args(
$args,
[
'has_sections' => false,
'name' => '%s',
]
);
if ( $args['has_sections'] ) {
foreach ( $fields as $tab_id => $tab_sections ) {
foreach ( $tab_sections as $section_id => $section_fields ) {
if ( self::is_field( $section_fields ) ) {
// Allow for flat tabs with no sections.
$section_id = 'main';
$section_fields = [
$section_id => $section_fields,
];
}
$fields[ $tab_id ][ $section_id ] = self::parse_fields( $section_fields, $args['name'] );
}
}
} else {
foreach ( $fields as $tab_id => $tab_fields ) {
$fields[ $tab_id ] = self::parse_fields( $tab_fields, $args['name'] );
}
}
return $fields;
}
/**
* @param array $fields
* @param string $name
*
* @return mixed
*/
public static function parse_fields( $fields, $name = '%' ) {
if ( is_array( $fields ) && ! empty( $fields ) ) {
foreach ( $fields as $field_id => $field ) {
if ( ! is_array( $field ) || ! self::is_field( $field ) ) {
continue;
}
// Remap old settings.
if ( is_numeric( $field_id ) && ! empty( $field['id'] ) ) {
try {
$fields = PUM_Utils_Array::replace_key( $fields, $field_id, $field['id'] );
} catch ( Exception $e ) {
}
$field_id = $field['id'];
} elseif ( empty( $field['id'] ) && ! is_numeric( $field_id ) ) {
$field['id'] = $field_id;
}
if ( empty( $field['name'] ) ) {
$field['name'] = sprintf( $name, $field_id );
}
$fields[ $field_id ] = self::parse_field( $field );
}
}
$fields = PUM_Utils_Array::sort( $fields, 'priority' );
return $fields;
}
/**
* Checks if an array is a field.
*
* @param array $array
*
* @return bool
*/
public static function is_field( $array = [] ) {
$field_tests = [
! isset( $array['type'] ) && ( isset( $array['label'] ) || isset( $array['desc'] ) ),
isset( $array['type'] ) && is_string( $array['type'] ),
];
return in_array( true, $field_tests );
}
/**
* Checks if an array is a section.
*
* @param array $array
*
* @return bool
*/
public static function is_section( $array = [] ) {
return ! self::is_field( $array );
}
/**
* @param array $args
*/
public static function render_field( $args = [] ) {
$args = static::parse_field( $args );
// If no type default to text.
$type = ! empty( $args['type'] ) ? $args['type'] : 'text';
/**
* Check if any actions hooked to this type of field and load run those.
*/
if ( has_action( "pum_{$type}_field" ) ) {
do_action( "pum_{$type}_field", $args );
} else {
if ( method_exists( 'PUM_Form_Fields', $type . '_callback' ) ) {
/**
* Check if renderer method exists and load that.
*/
$function_name = [ 'PUM_Form_Fields', $type . '_callback' ];
} elseif ( function_exists( "pum_{$type}_callback" ) ) {
/**
* Check if function exists and load that.
*/
$function_name = "pum_{$type}_callback";
} else {
/**
* No method exists, lets notify them the field type doesn't exist.
*/
$function_name = [ 'PUM_Form_Fields', 'missing_callback' ];
}
/**
* Call the determined method, passing the field args & $value to the callback.
*/
call_user_func_array( $function_name, [ $args ] );
}
}
/**
* @param PUM_Form $form
*/
public static function render_form_fields( $form ) {
$tabs = $form->get_tabs();
$sections = $form->get_sections();
$fields = $form->get_fields();
if ( $form->has_tabs() ) {
if ( $form->has_sections() ) {
foreach ( $tabs as $tab_id => $tab_label ) {
foreach ( $sections as $section_id => $section_label ) {
foreach ( $fields[ $tab_id ][ $section_id ] as $field_id => $field_args ) {
static::render_field( $field_args );
}
}
}
} else {
foreach ( $tabs as $tab_id => $label ) {
foreach ( $fields[ $tab_id ] as $field_id => $field_args ) {
static::render_field( $field_args );
}
}
}
} else {
foreach ( $fields as $field_id => $field_args ) {
static::render_field( $field_args );
}
}
}
/**
* Sanitizes an array of field values.
*
* @param $fields
* @param $values
*
* @return mixed
*/
public static function sanitize_fields( $values, $fields = [] ) {
foreach ( $values as $key => $value ) {
if ( is_string( $value ) ) {
$values[ $key ] = sanitize_text_field( $value );
}
$field = self::get_field( $fields, $key );
if ( $field ) {
$values[ $key ] = self::sanitize_field( $field, $value );
}
}
return $values;
}
/**
* @param array $args
* @param mixed $value
* @param array $fields
* @param array $values
*
* @return mixed|null
*/
public static function sanitize_field( $args, $value = null, $fields = [], $values = [] ) {
// If no type default to text.
$type = ! empty( $args['type'] ) ? $args['type'] : 'text';
/**
* Check if any actions hooked to this type of field and load run those.
*/
if ( has_filter( "pum_{$type}_sanitize" ) ) {
$value = apply_filters( "pum_{$type}_sanitize", $value, $args, $fields, $values );
} else {
/**
* Check if override or custom function exists and load that.
*/
if ( function_exists( "pum_{$type}_sanitize" ) ) {
$function_name = "pum_{$type}_sanitize";
} /**
* Check if core method exists and load that.
*/ elseif ( method_exists( 'PUM_Utils_Sanitize', $type ) ) {
$function_name = [ 'PUM_Utils_Sanitize', $type ];
} else {
$function_name = null;
}
if ( $function_name ) {
/**
* Call the determined method, passing the field args & $value to the callback.
*/
$value = call_user_func_array( $function_name, [ $value, $args, $fields, $values ] );
}
}
$value = apply_filters( 'pum_settings_sanitize', $value, $args, $fields, $values );
return $value;
}
}

View File

@@ -0,0 +1,159 @@
<?php
/**
* Format Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class PUM_Utils_Format
*/
class PUM_Utils_Format {
/**
* Removes <p></p> around URLs
*
* @param string $content
*
* @return mixed|string
*/
public static function unwrap_urls( $content = '' ) {
$content = preg_replace( '/<\\w+>((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[-;:&=\\+\$,\\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\\+\$,\\w]+@)[A-Za-z0-9.-]+)((?:\\/[\\+~%\\/.\\w-_]*)?\\??(?:[-\\+=&;%@.\\w_]*)#?(?:[.\\!\\/\\\\w]*))?)<\\/\\w+>/', "$1\n\n", $content );
$content = str_replace( '</p>', "</p>\n\n", $content );
return $content;
}
/**
* @param $time
* @param string $format U|human
*
* @return false|int|mixed
*/
public static function time( $time, $format = 'U' ) {
if ( ! PUM_Utils_Time::is_timestamp( $time ) ) {
$time = strtotime( $time );
}
switch ( $format ) {
case 'human':
case 'human-readable':
return self::human_time( $time );
break;
default:
case 'U':
return $time;
break;
}
}
/**
* @param int|float $number
* @param string $format
*
* @return int|string
*/
public static function number( $number, $format = '' ) {
switch ( $format ) {
default:
case 'abbreviated':
return self::abbreviated_number( $number );
break;
}
}
/**
* Convert the timestamp to a nice time format
*
* @param int $time
* @param int|null $current
*
* @return mixed
*/
public static function human_time( $time, $current = null ) {
if ( empty( $current ) ) {
$current = time();
}
$diff = (int) abs( $current - $time );
if ( $diff < 60 ) {
$since = sprintf( __( '%ss', 'popup-maker' ), $diff );
} elseif ( $diff < HOUR_IN_SECONDS ) {
$mins = round( $diff / MINUTE_IN_SECONDS );
if ( $mins <= 1 ) {
$mins = 1;
}
$since = sprintf( __( '%smin', 'popup-maker' ), $mins );
} elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
$hours = round( $diff / HOUR_IN_SECONDS );
if ( $hours <= 1 ) {
$hours = 1;
}
$since = sprintf( __( '%shr', 'popup-maker' ), $hours );
} elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
$days = round( $diff / DAY_IN_SECONDS );
if ( $days <= 1 ) {
$days = 1;
}
$since = sprintf( __( '%sd', 'popup-maker' ), $days );
} elseif ( $diff < MONTH_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
$weeks = round( $diff / WEEK_IN_SECONDS );
if ( $weeks <= 1 ) {
$weeks = 1;
}
$since = sprintf( __( '%sw', 'popup-maker' ), $weeks );
} else {
$since = '';
}
return apply_filters( 'pum_human_time_diff', $since, $diff, $time, $current );
}
/**
* K, M number formatting
*
* @param int|float $n
* @param string $point
* @param string $sep
*
* @return int|string
*/
public static function abbreviated_number( $n, $point = '.', $sep = ',' ) {
if ( $n < 0 ) {
return 0;
}
if ( $n < 10000 ) {
return number_format( $n, 0, $point, $sep );
}
$d = $n < 1000000 ? 1000 : 1000000;
$f = round( $n / $d, 1 );
return number_format( $f, $f - intval( $f ) ? 1 : 0, $point, $sep ) . ( 1000 === $d ? 'K' : 'M' );
}
/**
* Strips line breaks, tabs & carriage returns from html.
*
* Used to prevent WP from adding <br> and <p> tags.
*
* @param string $string
*
* @return mixed
*/
public static function strip_white_space( $string = '' ) {
return str_replace( [ "\t", "\r", "\n" ], '', $string );
}
}

View File

@@ -0,0 +1,169 @@
<?php
/**
* Utility for I10n
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class PUM_Utils_I10n {
/**
* Fetches translation status data from WordPress.org API.
*
* Stores it for 1 week.
*
* @return array
*/
public static function translation_status() {
$translations = get_transient( 'pum_alerts_translation_status' );
if ( ! $translations ) {
$response = wp_remote_get( 'https://api.wordpress.org/translations/plugins/1.0/?slug=popup-maker&version=' . Popup_Maker::$VER );
$response_body = json_decode( wp_remote_retrieve_body( $response ), true );
$translations = $response_body['translations'];
set_transient( 'pum_alerts_translation_status', $translations, 604800 );
}
$ret = [];
foreach ( $translations as $translation ) {
$ret[ $translation['language'] ] = $translation;
}
return $ret;
}
/**
* Get locales matching the HTTP accept language header.
*
* @return array List of locales.
*/
public static function get_non_en_accepted_wp_locales_from_header() {
$res = [];
$http_locales = self::get_http_locales();
if ( empty( $http_locales ) ) {
return $res;
}
if ( is_array( $http_locales ) ) {
foreach ( $http_locales as $http_locale ) {
$http_locale = explode( '-', $http_locale );
$lang = $http_locale[0];
$region = ! empty( $http_locale[1] ) ? $http_locale[1] : null;
if ( is_null( $region ) ) {
$region = $lang;
}
/*
* Discard English -- it's the default for all browsers,
* ergo not very reliable information
*/
if ( 'en' === $lang ) {
continue;
}
// Region should be uppercase.
$region = strtoupper( $region );
$mapped = self::map_locale( $lang, $region );
if ( $mapped ) {
$res[] = $mapped;
}
}
$res = array_unique( $res );
}
return $res;
}
/**
* @return array
*/
public static function available_locales() {
static $available_locales;
if ( ! isset( $available_locales ) ) {
if ( ! function_exists( 'wp_get_available_translations' ) ) {
require_once ABSPATH . 'wp-admin/includes/translation-install.php';
}
$available_locales = wp_get_available_translations();
}
return $available_locales;
}
/**
* Tries to map a lang/region pair to one of our locales.
*
* @param string $lang Lang part of the HTTP accept header.
* @param string $region Region part of the HTTP accept header.
*
* @return string|false Our locale matching $lang and $region, false otherwise.
*/
public static function map_locale( $lang, $region ) {
$uregion = strtoupper( $region );
$ulang = strtoupper( $lang );
$variants = [
"$lang-$region",
"{$lang}_$region",
"$lang-$uregion",
"{$lang}_$uregion",
"{$lang}_$ulang",
$lang,
];
$available_locales = self::available_locales();
$available_locales = array_keys( $available_locales );
foreach ( $variants as $variant ) {
if ( in_array( $variant, $available_locales ) ) {
return $variant;
}
}
foreach ( $available_locales as $locale ) {
list( $locale_lang, ) = preg_split( '/[_-]/', $locale );
if ( $lang === $locale_lang ) {
return $locale;
}
}
return false;
}
/**
* Given a HTTP Accept-Language header $header
* returns all the locales in it.
*
* @return array Matched locales.
*/
public static function get_http_locales() {
$locale_part_re = '[a-z]{2,}';
$locale_re = "($locale_part_re(\-$locale_part_re)?)";
if ( preg_match_all( "/$locale_re/i", isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '', $matches ) ) {
return $matches[0];
} else {
return [];
}
}
}

View File

@@ -0,0 +1,349 @@
<?php
/**
* Debug Logging Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
/**
* Class PUM_Utils_Logging
*
* @since 1.8.0
*/
class PUM_Utils_Logging {
/**
* Whether the log file is writable.
*
* @var bool
*/
public $is_writable = false;
/**
* Log file name.
*
* @var string
*/
private $filename = '';
/**
* Log file path.
*
* @var string
*/
private $file = '';
/**
* File system API.
*
* @var WP_Filesystem_Base
*/
private $fs;
/**
* Log file content.
*
* @var string
*/
private $content;
/**
* Instance.
*
* @var PUM_Utils_Logging
*/
public static $instance;
/**
* Get instance.
*
* @return PUM_Utils_Logging
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Get things started
*/
public function __construct() {
// On shutdown, save the log file.
add_action( 'shutdown', [ $this, 'save_logs' ] );
$this->fs = $this->file_system();
$this->is_writable = false !== $this->fs && 'direct' === $this->fs->method;
$upload_dir = PUM_Helpers::get_upload_dir();
if ( ! $this->fs->is_writable( $upload_dir['basedir'] ) ) {
$this->is_writable = false;
}
$this->init();
}
/**
* Check if logging is enabled.
*
* @return bool
*/
public function enabled() {
// Disable logging by adding define( 'PUM_DISABLE_LOGGING', true );.
return $this->is_writable && ( ! defined( 'PUM_DISABLE_LOGGING' ) || ! PUM_DISABLE_LOGGING );
}
/**
* Get working WP Filesystem instance
*
* @return WP_Filesystem_Base|false
*/
public function file_system() {
global $wp_filesystem;
require_once ABSPATH . 'wp-admin/includes/file.php';
// If for some reason the include doesn't work as expected just return false.
if ( ! function_exists( 'WP_Filesystem' ) ) {
return false;
}
$writable = WP_Filesystem( false, '', true );
// We consider the directory as writable if it uses the direct transport,
// otherwise credentials would be needed.
return ( $writable && 'direct' === $wp_filesystem->method ) ? $wp_filesystem : false;
}
/**
* Get things started
*/
public function init() {
$upload_dir = PUM_Helpers::get_upload_dir();
$file_token = get_option( 'pum_debug_log_token' );
if ( false === $file_token ) {
$file_token = uniqid( wp_rand(), true );
update_option( 'pum_debug_log_token', $file_token );
}
$this->filename = "pum-debug-{$file_token}.log"; // ex. pum-debug-5c2f6a9b9b5a3.log.
$this->file = trailingslashit( $upload_dir['basedir'] ) . $this->filename;
$old_file = trailingslashit( $upload_dir['basedir'] ) . 'pum-debug.log';
// If old file exists, move it.
if ( $this->fs->exists( $old_file ) ) {
$old_content = $this->get_file( $old_file );
$this->set_log_content( $old_content, true );
// Move old log file to new obfuscated location() .
$this->log_unique( 'Renaming log file.' );
// Move old file to new location.
$this->fs->move( $old_file, $this->file );
if ( $this->fs->exists( $old_file ) ) {
$this->fs->delete( $old_file );
}
} elseif ( ! $this->fs->exists( $this->file ) ) {
$this->setup_new_log();
} else {
$this->content = $this->get_file( $this->file );
}
// Truncate long log files.
if ( $this->fs->exists( $this->file ) && $this->fs->size( $this->file ) >= 1048576 ) {
$this->truncate_log();
}
}
/**
* Retrieves the url to the file
*
* @returns string
* @since 1.12.0
*/
public function get_file_url() {
return PUM_Helpers::get_upload_dir_url( $this->filename );
}
/**
* Retrieve the log data
*
* @return string
*/
public function get_log() {
return $this->get_log_content();
}
/**
* Log message to file
*
* @param string $message The message to log.
*/
public function log( $message = '' ) {
$this->write_to_log( wp_date( 'Y-n-d H:i:s' ) . ' - ' . $message );
}
/**
* Log unique message to file.
*
* @param string $message The unique message to log.
*/
public function log_unique( $message = '' ) {
$contents = $this->get_log_content();
if ( strpos( $contents, $message ) !== false ) {
return;
}
$this->log( $message );
}
/**
* Get the log file contents.
*
* @return string
*/
public function get_log_content() {
if ( ! isset( $this->content ) ) {
$this->content = $this->get_file();
}
return $this->content;
}
/**
* Set the log file contents in memory.
*
* @param mixed $content The content to set.
* @param bool $save Whether to save the content to the file immediately.
* @return void
*/
private function set_log_content( $content, $save = false ) {
$this->content = $content;
if ( $save ) {
$this->save_logs();
}
}
/**
* Retrieve the contents of a file.
*
* @param string|boolean $file File to get contents of.
*
* @return string
*/
protected function get_file( $file = false ) {
$file = $file ? $file : $this->file;
if ( ! $this->enabled() ) {
return '';
}
$content = '';
if ( $this->fs->exists( $file ) ) {
$content = $this->fs->get_contents( $file );
}
return $content;
}
/**
* Write the log message
*
* @param string $message The message to write.
*/
protected function write_to_log( $message = '' ) {
if ( ! $this->enabled() ) {
return;
}
$contents = $this->get_log_content();
// If it doesn't end with a new line, add one. \r\n length is 2.
if ( substr( $contents, -2 ) !== "\r\n" ) {
$contents .= "\r\n";
}
$this->set_log_content( $contents . $message );
}
/**
* Save the current contents to file.
*/
public function save_logs() {
if ( ! $this->enabled() ) {
return;
}
$this->fs->put_contents( $this->file, $this->content, FS_CHMOD_FILE );
}
/**
* Get a line count.
*
* @return int
*/
public function count_lines() {
$file = $this->get_log_content();
$lines = explode( "\r\n", $file );
return count( $lines );
}
/**
* Truncates a log file to maximum of 250 lines.
*/
public function truncate_log() {
$content = $this->get_log_content();
$lines = explode( "\r\n", $content );
$lines = array_slice( $lines, 0, 250 ); // 50 is how many lines you want to keep
$truncated_content = implode( "\r\n", $lines );
$this->set_log_content( $truncated_content, true );
}
/**
* Set up a new log file.
*
* @return void
*/
public function setup_new_log() {
$this->set_log_content( "Popup Maker Debug Logs:\r\n" . wp_date( 'Y-n-d H:i:s' ) . " - Log file initialized\r\n", true );
}
/**
* Delete the log file.
*/
public function clear_log() {
// Delete the file.
@$this->fs->delete( $this->file );
if ( $this->enabled() ) {
$this->setup_new_log();
}
}
/**
* Log a deprecated notice.
*
* @param string $func_name Function name.
* @param string $version Versoin deprecated.
* @param string $replacement Replacement function (optional).
*/
public function log_deprecated_notice( $func_name, $version, $replacement = null ) {
if ( ! is_null( $replacement ) ) {
$notice = sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $func_name, $version, $replacement );
} else {
$notice = sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $func_name, $version );
}
$this->log_unique( $notice );
}
}

View File

@@ -0,0 +1,250 @@
<?php
/**
* Options Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class PUM_Utils_Options
*/
class PUM_Utils_Options {
/**
* Unique Prefix per plugin.
*
* @var string
*/
public static $_prefix = 'popmake_';
/**
* Keeps static copy of the options during runtime.
*
* @var null|array
*/
private static $_data;
/**
* Initialize Options on run.
*
* @param bool $force
*/
public static function init( $force = false ) {
global $popmake_options;
if ( ! isset( self::$_data ) || $force ) {
self::$_data = self::get_all();
/** @deprecated 1.7.0 */
$popmake_options = self::$_data;
}
}
/**
* Get Settings
*
* Retrieves all plugin settings
*
* @return array settings
*/
public static function get_all() {
$settings = get_option( self::$_prefix . 'settings', [] );
if ( ! is_array( $settings ) ) {
$settings = [];
}
/* @deprecated filter. */
$settings = apply_filters( 'popmake_get_settings', $settings );
return apply_filters( self::$_prefix . 'get_options', $settings );
}
/**
* Get an option
*
* Looks to see if the specified setting exists, returns default if not
*
* @param string $key
* @param bool $default
*
* @return mixed
*/
public static function get( $key = '', $default = false ) {
// Passive initialization.
self::init();
$value = isset( self::$_data[ $key ] ) ? self::$_data[ $key ] : $default;
return apply_filters( self::$_prefix . 'get_option', $value, $key, $default );
}
/**
* Update an option
*
* Updates an setting value in both the db and the global variable.
* Warning: Passing in an empty, false or null string value will remove
* the key from the _options array.
*
* @param string $key The Key to update
* @param string|bool|int $value The value to set the key to
*
* @return boolean True if updated, false if not.
*/
public static function update( $key = '', $value = false ) {
// Passive initialization.
self::init();
// If no key, exit
if ( empty( $key ) ) {
return false;
}
if ( empty( $value ) ) {
$remove_option = self::delete( $key );
return $remove_option;
}
// First let's grab the current settings
$options = get_option( self::$_prefix . 'settings' );
// Let's let devs alter that value coming in
$value = apply_filters( self::$_prefix . 'update_option', $value, $key );
// Next let's try to update the value
$options[ $key ] = $value;
$did_update = update_option( self::$_prefix . 'settings', $options );
// If it updated, let's update the global variable
if ( $did_update ) {
self::$_data[ $key ] = $value;
}
return $did_update;
}
/**
* Update the entire settings array from a new array.
*
* @param array $new_options
*
* @return bool
*/
public static function update_all( $new_options = [] ) {
// First let's grab the current settings
$options = get_option( self::$_prefix . 'settings' );
// Lets merge options that may exist previously that are not existing now.
$new_options = wp_parse_args( $new_options, $options );
$did_update = update_option( self::$_prefix . 'settings', $new_options );
// If it updated, let's update the global variable
if ( $did_update ) {
self::$_data = $new_options;
}
return $did_update;
}
/**
* Merge the new options into the settings array.
*
* @param array $new_options
*
* @return bool
*/
public static function merge( $new_options = [] ) {
$options = self::get_all();
// Merge new options.
foreach ( $new_options as $key => $val ) {
$options[ $key ] = ! empty( $val ) ? $val : false;
}
$did_update = update_option( self::$_prefix . 'settings', $options );
// If it updated, let's update the global variable
if ( $did_update ) {
self::$_data = $options;
}
return $did_update;
}
/**
* Remove an option or multiple
*
* Removes a setting value in both the db and the global variable.
*
* @param string|array $keys The Key/s to delete
*
* @return boolean True if updated, false if not.
*/
public static function delete( $keys = '' ) {
// Passive initialization.
self::init();
// If no key, exit
if ( empty( $keys ) ) {
return false;
} elseif ( is_string( $keys ) ) {
$keys = [ $keys ];
}
// First let's grab the current settings
$options = get_option( self::$_prefix . 'settings' );
// Remove each key/value pair.
foreach ( $keys as $key ) {
if ( isset( $options[ $key ] ) ) {
unset( $options[ $key ] );
}
}
$did_update = update_option( self::$_prefix . 'settings', $options );
// If it updated, let's update the global variable
if ( $did_update ) {
self::$_data = $options;
}
return $did_update;
}
/**
* Remaps option keys.
*
* @param array $remap_array an array of $old_key => $new_key values.
*
* @return bool
*/
public static function remap_keys( $remap_array = [] ) {
$options = self::get_all();
foreach ( $remap_array as $key => $new_key ) {
$value = self::get( $key, false );
if ( ! empty( $value ) ) {
$options[ $new_key ] = $value;
}
unset( $options[ $key ] );
}
$did_update = update_option( self::$_prefix . 'settings', $options );
// If it updated, let's update the global variable
if ( $did_update ) {
self::$_data = $options;
}
return $did_update;
}
}

View File

@@ -0,0 +1,350 @@
<?php
/**
* Prerequisites Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
/**
* Prerequisite handler.
*
* @version 1.0.0
*/
class PUM_Utils_Prerequisites {
/**
* Cache accessible across instances.
*
* @var array
*/
public static $cache = [];
/**
* Array of checks to perform.
*
* @var array
*/
protected $checks = [];
/**
* Array of detected failures.
*
* @var array
*/
protected $failures = [];
/**
* Instantiate prerequisite checker.
*
* @param array $requirements Array of requirements.
*/
public function __construct( $requirements = [] ) {
foreach ( $requirements as $arguments ) {
switch ( $arguments['type'] ) {
case 'php':
$this->checks[] = wp_parse_args(
$arguments,
[
'type' => 'php',
'version' => '5.6',
]
);
break;
case 'plugin':
$this->checks[] = wp_parse_args(
$arguments,
[
'type' => 'plugin',
'slug' => '',
'name' => '',
'version' => '',
'check_installed' => false,
'dep_label' => '',
]
);
break;
default:
break;
}
}
}
/**
* Check requirements.
*
* @param boolean $return_on_fail Whether it should stop processing if one fails.
*
* @return bool
*/
public function check( $return_on_fail = false ) {
$end_result = true;
foreach ( $this->checks as $check ) {
$result = $this->check_handler( $check );
if ( false === $result ) {
if ( true === $return_on_fail ) {
return false;
}
$end_result = false;
}
}
return $end_result;
}
/**
* Render notices when appropriate.
*/
public function setup_notices() {
add_action( 'admin_notices', [ $this, 'render_notices' ] );
}
/**
* Handle individual checks by mapping them to methods.
*
* @param array $check Requirement check arguments.
*
* @return bool
*/
public function check_handler( $check ) {
return method_exists( $this, 'check_' . $check['type'] ) ? $this->{'check_' . $check['type']}( $check ) : false;
}
/**
* Report failure notice to the queue.
*
* @param array $check_args Array of check arguments.
*/
public function report_failure( $check_args ) {
$this->failures[] = $check_args;
}
/**
* Get a list of failures.
*
* @return array
*/
public function get_failures() {
return $this->failures;
}
/**
* Check PHP version against args.
*
* @param array $check_args Array of args.
*
* @return bool
*/
public function check_php( $check_args ) {
if ( false === version_compare( phpversion(), $check_args['version'], '>=' ) ) {
$this->report_failure( $check_args );
return false;
}
return true;
}
/**
* Check plugin requirements.
*
* @param array $check_args Array of args.
*
* @return bool
*/
public function check_plugin( $check_args ) {
$active = $this->plugin_is_active( $check_args['slug'] );
/**
* The following checks are performed in this order for performance reasons.
*
* We start with most cached option, to least in hopes of a hit early.
*
* 1. If active and not checking version.
* 2. If active and outdated.
* 3. If not active and installed.
* 4. If not installed
*/
if ( true === $active ) {
// If required version is set & plugin is active, check that first.
if ( isset( $check_args['version'] ) ) {
$version = $this->get_plugin_data( $check_args['slug'], 'Version' );
// If its higher than the required version, we can bail now > true.
if ( version_compare( $version, $check_args['version'], '>=' ) ) {
return true;
} else {
// If not updated, report the failure and bail > false.
$this->report_failure(
array_merge(
$check_args,
[
// Report not_updated status.
'not_updated' => true,
]
)
);
return false;
}
} else {
// If the plugin is active, with no required version, were done > true.
return true;
}
}
if ( $check_args['check_installed'] ) {
// Check if installed, if so the plugin is not activated.
if ( $check_args['name'] === $this->get_plugin_data( $check_args['slug'], 'Name' ) ) {
$this->report_failure(
array_merge(
$check_args,
[
// Report not_activated status.
'not_activated' => true,
]
)
);
} else {
$this->report_failure(
array_merge(
$check_args,
[
// Report not_installed status.
'not_installed' => true,
]
)
);
}
}
return false;
}
/**
* Internally cached get_plugin_data/get_file_data wrapper.
*
* @param string $slug Plugins `folder/file.php` slug.
* @param string $header Specific plugin header needed.
* @return mixed
*/
private function get_plugin_data( $slug, $header = null ) {
if ( ! isset( static::$cache['get_plugin_data'][ $slug ] ) ) {
$headers = \get_file_data(
WP_PLUGIN_DIR . '/' . $slug,
[
'Name' => 'Plugin Name',
'Version' => 'Version',
],
'plugin'
);
static::$cache['get_plugin_data'][ $slug ] = $headers;
}
$plugin_data = static::$cache['get_plugin_data'][ $slug ];
if ( empty( $header ) ) {
return $plugin_data;
}
return isset( $plugin_data[ $header ] ) ? $plugin_data[ $header ] : null;
}
/**
* Check if plugin is active.
*
* @param string $slug Slug to check for.
*
* @return bool
*/
protected function plugin_is_active( $slug ) {
$active_plugins = get_option( 'active_plugins', [] );
return in_array( $slug, $active_plugins, true );
}
/**
* Get php error message.
*
* @param array $failed_check_args Check arguments.
*
* @return string
*/
public function get_php_message( $failed_check_args ) {
/* translators: 1. PHP Version */
$message = __( 'This plugin requires <b>PHP %s</b> or higher in order to run.', 'popup-maker' );
return sprintf( $message, $failed_check_args['version'] );
}
/**
* Get plugin error message.
*
* @param array $failed_check_args Get helpful error message.
*
* @return string
*/
public function get_plugin_message( $failed_check_args ) {
$slug = $failed_check_args['slug'];
// Without file path.
$short_slug = explode( '/', $slug );
$short_slug = $short_slug[0];
$name = $failed_check_args['name'];
$dep_label = $failed_check_args['dep_label'];
if ( isset( $failed_check_args['not_activated'] ) ) {
$url = esc_url( wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=' . $slug ), 'activate-plugin_' . $slug ) );
$link = '<a href="' . $url . '">' . __( 'activate it', 'popup-maker' ) . '</a>';
$text = sprintf(
/* translators: 1. Plugin Name, 2. Required Plugin Name, 4. `activate it` link. */
__( 'The plugin "%1$s" requires %2$s! Please %3$s to continue!', 'popup-maker' ),
$dep_label,
'<strong>' . $name . '</strong>',
$link
);
} elseif ( isset( $failed_check_args['not_updated'] ) ) {
$url = esc_url( wp_nonce_url( admin_url( 'update.php?action=upgrade-plugin&plugin=' . $slug ), 'upgrade-plugin_' . $slug ) );
$link = '<a href="' . $url . '">' . __( 'update it', 'popup-maker' ) . '</a>';
$text = sprintf(
/* translators: 1. Plugin Name, 2. Required Plugin Name, 3. Version number, 4. `update it` link. */
__( 'The plugin "%1$s" requires %2$s v%3$s or higher! Please %4$s to continue!', 'popup-maker' ),
$dep_label,
'<strong>' . $name . '</strong>',
'<strong>' . $failed_check_args['version'] . '</strong>',
$link
);
} else {
$url = esc_url( wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $short_slug ), 'install-plugin_' . $short_slug ) );
$link = '<a href="' . $url . '">' . __( 'install it', 'popup-maker' ) . '</a>';
$text = sprintf(
/* translators: 1. Plugin Name, 2. Required Plugin Name, 3. `install it` link. */
__( 'The plugin "%1$s" requires %2$s! Please %3$s to continue!', 'popup-maker' ),
$dep_label,
'<strong>' . $name . '</strong>',
$link
);
}
return $text;
}
/**
* Render needed admin notices.
*
* @return void
*/
public function render_notices() {
foreach ( $this->failures as $failure ) {
$class = 'notice notice-error';
$message = method_exists( $this, 'get_' . $failure['type'] . '_message' ) ? $this->{'get_' . $failure['type'] . '_message'}( $failure ) : false;
/* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */
echo sprintf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), $message );
}
}
}

View File

@@ -0,0 +1,50 @@
<?php
/**
* Sanitize Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class PUM_Utils_Sanitize
*/
class PUM_Utils_Sanitize {
/**
* @param string $value
* @param array $args
*
* @return string
*/
public static function text( $value = '', $args = [] ) {
return sanitize_text_field( $value );
}
/**
* @param mixed|int $value
* @param array $args
*
* @return bool|int
*/
public static function checkbox( $value = null, $args = [] ) {
if ( intval( $value ) === 1 ) {
return 1;
}
return 0;
}
public static function measure( $value = '', $args = [], $fields = [], $values = [] ) {
if ( isset( $values[ $args['id'] . '_unit' ] ) ) {
$value .= $values[ $args['id'] . '_unit' ];
}
return sanitize_text_field( $value );
}
}

View File

@@ -0,0 +1,237 @@
<?php
/**
* Template Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class PUM_Utils_Template
*/
class PUM_Utils_Template {
/**
* @return array
*/
public static function paths() {
$template_dir = apply_filters( 'pum_template_path', 'popup-maker' );
$old_template_dir = apply_filters( 'popmake_templates_dir', 'popmake_templates' );
$file_paths = apply_filters(
'pum_template_paths',
[
1 => trailingslashit( get_stylesheet_directory() ) . $template_dir,
2 => trailingslashit( get_stylesheet_directory() ) . $old_template_dir,
10 => trailingslashit( get_template_directory() ) . $template_dir,
11 => trailingslashit( get_template_directory() ) . $old_template_dir,
100 => Popup_Maker::$DIR . 'templates',
]
);
/* @deprecated 1.8.9 */
$file_paths = apply_filters( 'popmake_template_paths', $file_paths );
// sort the file paths based on priority
ksort( $file_paths, SORT_NUMERIC );
return array_map( 'trailingslashit', $file_paths );
}
/**
* Locate a template and return the path for inclusion.
*
* This is the load order:
*
* yourtheme / $template_path / $template_name
* yourtheme / $template_name
* $default_path / $template_name
*
* @access public
*
* @param string|array $template_names
* @param bool $load
* @param bool $require_once
*
* @return string
* @internal param string $template_path (default: '')
* @internal param string $default_path (default: '')
*/
public static function locate( $template_names, $load = false, $require_once = true ) {
// No file found yet
$located = false;
$template_name = '';
// Try to find a template file
foreach ( (array) $template_names as $template_name ) {
// Continue if template is empty
if ( empty( $template_name ) ) {
continue;
}
// Trim off any slashes from the template name
$template_name = ltrim( $template_name, '/' );
// try locating this template file by looping through the template paths
foreach ( self::paths() as $template_path ) {
if ( file_exists( $template_path . $template_name ) ) {
$located = $template_path . $template_name;
break;
}
}
if ( $located ) {
break;
}
}
// Return what we found
$located = apply_filters( 'pum_locate_template', $located, $template_name );
if ( ( true === $load ) && ! empty( $located ) ) {
load_template( $located, $require_once );
}
return $located;
}
/**
* Locate a template part (for templates like the topic-loops).
*
* Popup_Maker::$DEBUG will prevent overrides in themes from taking priority.
*
* @param mixed $slug
* @param string|bool $name (default: false)
* @param bool $load
*
* @return string
*/
public static function locate_part( $slug, $name = null, $load = false ) {
$templates = [];
if ( $name ) {
// slug-name.php
$templates[] = "{$slug}-{$name}.php";
}
// slug.php
$templates[] = "{$slug}.php";
// Allow template parts to be filtered
$templates = apply_filters( 'pum_locate_template_part', $templates, $slug, $name );
/* @deprecated 1.8.0 */
$templates = apply_filters( 'popmake_get_template_part', $templates, $slug, $name );
// Return the part that is found
return self::locate( $templates, $load, false );
}
/**
* Render file with extracted arguments.
*
* @param $template
* @param array $args
*/
public static function render( $template, $args = [] ) {
if ( ! $template || ! file_exists( $template ) ) {
_doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $template ), '1.0.0' );
return;
}
if ( $args && is_array( $args ) ) {
extract( $args );
}
include $template;
}
/**
* Render a template part in $slug-$name.php fashion.
*
* Allows passing arguments that will be globally accessible in the template.
*
* @param string $slug
* @param string $name
* @param array $args
*/
public static function part( $slug, $name = null, $args = [] ) {
echo self::get_part( $slug, $name, $args );
}
/**
* Get a template part in $slug-$name.php fashion.
*
* Allows passing arguments that will be globally accessible in the template.
*
* @param string $slug
* @param string $name
* @param array $args
*
* @return string
*/
public static function get_part( $slug, $name = null, $args = [] ) {
$template = self::locate_part( $slug, $name );
ob_start();
do_action( 'pum_before_template_part', $template, $slug, $name, $args );
/* @deprecated 1.8.0 */
do_action( 'get_template_part_' . $slug, $slug, $name );
self::render( $template, $args );
do_action( 'pum_after_template_part', $template, $slug, $name, $args );
return ob_get_clean();
}
/**
* Gets the rendered contents of the specified template file.
*
* @param $template_name
* @param array $args
*
* @return string
*/
public static function get( $template_name, $args = [] ) {
$template = self::locate( $template_name );
// Allow 3rd party plugin filter template file from their plugin.
$template = apply_filters( 'pum_get_template', $template, $template_name, $args );
ob_start();
do_action( 'pum_before_template', $template_name, $template, $args );
self::render( $template, $args );
do_action( 'pum_after_template', $template_name, $template, $args );
return ob_get_clean();
}
/**
* Get other templates (e.g. popup content) passing attributes and including the file.
*
* @deprecated public
*
* @param string $template_name Template file name with extension: file-name.php
* @param array $args (default: array())
*/
public static function load( $template_name, $args = [] ) {
echo self::get( $template_name, $args );
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* Time Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class PUM_Utils_Time
*/
class PUM_Utils_Time {
public static function is_timestamp( $timestamp ) {
return ( 1 === preg_match( '~^[1-9][0-9]*$~', $timestamp ) );
}
}

View File

@@ -0,0 +1,742 @@
<?php
/**
* Upgrades Utility
*
* @package PUM
* @copyright Copyright (c) 2023, Code Atlantic LLC
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles processing of data migration & upgrade routines.
*/
class PUM_Utils_Upgrades {
/**
* @var PUM_Upgrade_Registry
*/
protected $registry;
/**
* @var self
*/
public static $instance;
/**
* Popup Maker version.
*
* @var string
*/
public static $version;
/**
* Popup Maker upgraded from version.
*
* @var string
*/
public static $upgraded_from;
/**
* Popup Maker initial version.
*
* @var string
*/
public static $initial_version;
/**
* Popup Maker db version.
*
* @var string
*/
public static $db_version;
/**
* Popup Maker install date.
*
* @var string
*/
public static $installed_on;
/**
* Gets everything going with a singleton instance.
*
* @return self
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Sets up the Upgrades class instance.
*/
public function __construct() {
// Update stored plugin version info.
self::update_plugin_version();
// Render upgrade admin notices.
add_filter( 'pum_alert_list', [ $this, 'upgrade_alert' ] );
// Add Upgrade tab to Tools page when upgrades available.
add_filter( 'pum_tools_tabs', [ $this, 'tools_page_tabs' ] );
// Render tools page upgrade tab content.
add_action( 'pum_tools_page_tab_upgrades', [ $this, 'tools_page_tab_content' ] );
// Ajax upgrade handler.
add_action( 'wp_ajax_pum_process_upgrade_request', [ $this, 'process_upgrade_request' ] );
// Register core upgrades.
add_action( 'pum_register_upgrades', [ $this, 'register_processes' ] );
// Initiate the upgrade registry. Must be done after versions update for proper comparisons.
$this->registry = PUM_Upgrade_Registry::instance();
}
/**
* Update version info.
*/
public static function update_plugin_version() {
self::$version = get_option( 'pum_ver' );
self::$upgraded_from = get_option( 'pum_ver_upgraded_from' );
self::$initial_version = get_option( 'pum_initial_version' );
self::$db_version = get_option( 'pum_db_ver' );
self::$installed_on = get_option( 'pum_installed_on' );
/**
* If no version set check if a deprecated one exists.
*/
if ( empty( self::$version ) ) {
$deprecated_ver = get_site_option( 'popmake_version' );
// set to the deprecated version or last version that didn't have the version option set
self::$version = $deprecated_ver ? $deprecated_ver : Popup_Maker::$VER; // Since we had versioning in v1 if there isn't one stored its a new install.
update_option( 'pum_ver', self::$version );
}
/**
* Back fill the initial version with the oldest version we can detect.
*/
if ( ! self::$initial_version ) {
$oldest_known = Popup_Maker::$VER;
if ( self::$version && version_compare( self::$version, $oldest_known, '<' ) ) {
$oldest_known = self::$version;
}
if ( self::$upgraded_from && version_compare( self::$upgraded_from, $oldest_known, '<' ) ) {
$oldest_known = self::$upgraded_from;
}
$deprecated_ver = get_site_option( 'popmake_version' );
if ( $deprecated_ver && version_compare( $deprecated_ver, $oldest_known, '<' ) ) {
$oldest_known = $deprecated_ver;
}
$dep_upgraded_from = get_option( 'popmake_version_upgraded_from' );
if ( $dep_upgraded_from && version_compare( $dep_upgraded_from, $oldest_known, '<' ) ) {
$oldest_known = $dep_upgraded_from;
}
self::$initial_version = $oldest_known;
// Only set this value if it doesn't exist.
update_option( 'pum_initial_version', $oldest_known );
}
if ( version_compare( self::$version, Popup_Maker::$VER, '<' ) ) {
// Allow processing of small core upgrades
do_action( 'pum_update_core_version', self::$version );
// Save Upgraded From option
update_option( 'pum_ver_upgraded_from', self::$version );
update_option( 'pum_ver', Popup_Maker::$VER );
self::$upgraded_from = self::$version;
self::$version = Popup_Maker::$VER;
// Reset JS/CSS assets for regeneration.
pum_reset_assets();
} elseif ( ! self::$upgraded_from || 'false' === self::$upgraded_from ) {
// Here to prevent constant extra queries.
self::$upgraded_from = '0.0.0';
update_option( 'pum_ver_upgraded_from', self::$upgraded_from );
}
// If no current db version, but prior install detected, set db version correctly.
// Here for backward compatibility.
if ( ! self::$db_version || self::$db_version < Popup_Maker::$DB_VER ) {
self::$db_version = Popup_Maker::$DB_VER;
update_option( 'pum_db_ver', self::$db_version );
}
/**
* Back fill the initial version with the oldest version we can detect.
*/
if ( ! self::$installed_on ) {
$installed_on = current_time( 'mysql' );
$review_installed_on = get_option( 'pum_reviews_installed_on' );
if ( ! empty( $review_installed_on ) ) {
$installed_on = $review_installed_on;
}
self::$installed_on = $installed_on;
update_option( 'pum_installed_on', self::$installed_on );
}
}
/**
* @param PUM_Upgrade_Registry $registry
*/
public function register_processes( PUM_Upgrade_Registry $registry ) {
// v1.7 Upgrades
$registry->add_upgrade(
'core-v1_7-popups',
[
'rules' => [
version_compare( self::$initial_version, '1.7', '<' ),
],
'class' => 'PUM_Upgrade_v1_7_Popups',
'file' => Popup_Maker::$DIR . 'includes/batch/upgrade/class-upgrade-v1_7-popups.php',
]
);
$registry->add_upgrade(
'core-v1_7-settings',
[
'rules' => [
version_compare( self::$initial_version, '1.7', '<' ),
],
'class' => 'PUM_Upgrade_v1_7_Settings',
'file' => Popup_Maker::$DIR . 'includes/batch/upgrade/class-upgrade-v1_7-settings.php',
]
);
$registry->add_upgrade(
'core-v1_8-themes',
[
'rules' => [
$this->needs_v1_8_theme_upgrade(),
],
'class' => 'PUM_Upgrade_v1_8_Themes',
'file' => Popup_Maker::$DIR . 'includes/batch/upgrade/class-upgrade-v1_8-themes.php',
]
);
}
/**
* @return bool
*/
public function needs_v1_8_theme_upgrade() {
if ( pum_has_completed_upgrade( 'core-v1_8-themes' ) ) {
return false;
}
$needs_upgrade = get_transient( 'pum_needs_1_8_theme_upgrades' );
if ( false === $needs_upgrade ) {
$query = new WP_Query(
[
'post_type' => 'popup_theme',
'post_status' => 'any',
'fields' => 'ids',
'meta_query' => [
'relation' => 'OR',
[
'key' => 'popup_theme_data_version',
'compare' => 'NOT EXISTS',
'value' => 'deprecated', // Here for WP 3.9 or less.
],
[
'key' => 'popup_theme_data_version',
'compare' => '<',
'value' => 3,
],
],
]
);
$needs_upgrade = $query->post_count;
}
if ( $needs_upgrade <= 0 ) {
pum_set_upgrade_complete( 'core-v1_8-themes' );
delete_transient( 'pum_needs_1_8_theme_upgrades' );
return false;
}
set_transient( 'pum_needs_1_8_theme_upgrades', $needs_upgrade );
return (bool) $needs_upgrade;
}
/**
* Registers a new upgrade routine.
*
* @param string $upgrade_id Upgrade ID.
* @param array $args {
* Arguments for registering a new upgrade routine.
*
* @type array $rules Array of true/false values.
* @type string $class Batch processor class to use.
* @type string $file File containing the upgrade processor class.
* }
*
* @return bool True if the upgrade routine was added, otherwise false.
*/
public function add_routine( $upgrade_id, $args ) {
return $this->registry->add_upgrade( $upgrade_id, $args );
}
/**
* Displays upgrade notices.
*/
public function upgrade_notices() {
if ( ! $this->has_uncomplete_upgrades() || ! current_user_can( 'manage_options' ) ) {
return;
}
// Enqueue admin JS for the batch processor.
wp_enqueue_script( 'pum-admin-batch' );
wp_enqueue_style( 'pum-admin-batch' ); ?>
<div class="notice notice-info is-dismissible">
<?php $this->render_upgrade_notice(); ?>
<?php $this->render_form(); ?>
</div>
<?php
}
/**
* @param array $alerts
*
* @return array
*/
public function upgrade_alert( $alerts = [] ) {
if ( ! $this->has_uncomplete_upgrades() || ! current_user_can( 'manage_options' ) ) {
return $alerts;
}
// Enqueue admin JS for the batch processor.
wp_enqueue_script( 'pum-admin-batch' );
wp_enqueue_style( 'pum-admin-batch' );
ob_start();
$this->render_upgrade_notice();
$this->render_form();
$html = ob_get_clean();
$alerts[] = [
'code' => 'upgrades_required',
'type' => 'warning',
'html' => $html,
'priority' => 1000,
'dismissible' => false,
'global' => true,
];
return $alerts;
}
/**
* Renders the upgrade notification message.
*
* Message only, no form.
*/
public function render_upgrade_notice() {
$resume_upgrade = $this->maybe_resume_upgrade();
?>
<p class="pum-upgrade-notice">
<?php
if ( empty( $resume_upgrade ) ) {
?>
<strong><?php _e( 'The latest version of Popup Maker requires changes to the Popup Maker settings saved on your site.', 'popup-maker' ); ?></strong>
<?php
} else {
_e( 'Popup Maker needs to complete a the update of your settings that was previously started.', 'popup-maker' );
}
?>
</p>
<?php
}
/**
* Renders the upgrade processing form for reuse.
*/
public function render_form() {
$args = [
'upgrade_id' => $this->get_current_upgrade_id(),
'step' => 1,
];
$resume_upgrade = $this->maybe_resume_upgrade();
if ( $resume_upgrade && is_array( $resume_upgrade ) ) {
$args = wp_parse_args( $resume_upgrade, $args );
}
?>
<form method="post" class="pum-form pum-batch-form pum-upgrade-form" data-ays="<?php _e( 'This can sometimes take a few minutes, are you ready to begin?', 'popup-maker' ); ?>" data-upgrade_id="<?php echo $args['upgrade_id']; ?>" data-step="<?php echo (int) $args['step']; ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'pum_upgrade_ajax_nonce' ) ); ?>">
<div class="pum-field pum-field-button pum-field-submit">
<p>
<small><?php _e( 'The button below will process these changes automatically for you.', 'popup-maker' ); ?></small>
</p>
<?php submit_button( ! empty( $resume_upgrade ) ? __( 'Finish Upgrades', 'popup-maker' ) : __( 'Process Changes', 'popup-maker' ), 'secondary', 'submit', false ); ?>
</div>
<div class="pum-batch-progress">
<progress class="pum-overall-progress" max="100">
<div class="progress-bar"><span></span></div>
</progress>
<progress class="pum-task-progress" max="100">
<div class="progress-bar"><span></span></div>
</progress>
<div class="pum-upgrade-messages"></div>
</div>
</form>
<?php
}
/**
* For use when doing 'stepped' upgrade routines, to see if we need to start somewhere in the middle
*
* @return false|array When nothing to resume returns false, otherwise starts the upgrade where it left off
*/
public function maybe_resume_upgrade() {
$doing_upgrade = get_option( 'pum_doing_upgrade', [] );
if ( empty( $doing_upgrade ) ) {
return false;
}
return (array) $doing_upgrade;
}
/**
* Retrieves an upgrade routine from the registry.
*
* @param string $upgrade_id Upgrade ID.
*
* @return array|false Upgrade entry from the registry, otherwise false.
*/
public function get_routine( $upgrade_id ) {
return $this->registry->get( $upgrade_id );
}
/**
* Get all upgrade routines.
*
* Note: Unfiltered.
*
* @return array
*/
public function get_routines() {
return $this->registry->get_upgrades();
}
/**
* Adds an upgrade action to the completed upgrades array
*
* @param string $upgrade_id The action to add to the competed upgrades array
*
* @return bool If the function was successfully added
*/
public function set_upgrade_complete( $upgrade_id = '' ) {
if ( empty( $upgrade_id ) ) {
return false;
}
$completed_upgrades = $this->get_completed_upgrades();
if ( ! in_array( $upgrade_id, $completed_upgrades ) ) {
$completed_upgrades[] = $upgrade_id;
do_action( 'pum_set_upgrade_complete', $upgrade_id );
}
// Remove any blanks, and only show uniques
$completed_upgrades = array_unique( array_values( $completed_upgrades ) );
return update_option( 'pum_completed_upgrades', $completed_upgrades );
}
/**
* Get's the array of completed upgrade actions
*
* @return array The array of completed upgrades
*/
public function get_completed_upgrades() {
$completed_upgrades = get_option( 'pum_completed_upgrades' );
if ( false === $completed_upgrades ) {
$completed_upgrades = [];
update_option( 'pum_completed_upgrades', $completed_upgrades );
}
return get_option( 'pum_completed_upgrades', [] );
}
/**
* Check if the upgrade routine has been run for a specific action
*
* @param string $upgrade_id The upgrade action to check completion for
*
* @return bool If the action has been added to the completed actions array
*/
public function has_completed_upgrade( $upgrade_id = '' ) {
if ( empty( $upgrade_id ) ) {
return false;
}
$completed_upgrades = $this->get_completed_upgrades();
return in_array( $upgrade_id, $completed_upgrades, true );
}
/**
* Conditional function to see if there are upgrades available.
*
* @return bool
*/
public function has_uncomplete_upgrades() {
return (bool) count( $this->get_uncompleted_upgrades() );
}
/**
* Returns array of uncompleted upgrades.
*
* This doesn't return an upgrade if:
* - It was previously complete.
* - If any false values in the upgrades $rules array are found.
*
* @return array
*/
public function get_uncompleted_upgrades() {
$required_upgrades = $this->get_routines();
foreach ( $required_upgrades as $upgrade_id => $upgrade ) {
// If the upgrade has already completed or one of the rules failed remove it from the list.
if ( $this->has_completed_upgrade( $upgrade_id ) || in_array( false, $upgrade['rules'], true ) ) {
unset( $required_upgrades[ $upgrade_id ] );
}
}
return $required_upgrades;
}
/**
* Handles Ajax for processing a upgrade upgrade/que request.
*/
public function process_upgrade_request() {
$upgrade_id = isset( $_REQUEST['upgrade_id'] ) ? sanitize_key( $_REQUEST['upgrade_id'] ) : false;
if ( ! $upgrade_id && ! $this->has_uncomplete_upgrades() ) {
wp_send_json_error(
[
'error' => __( 'A batch process ID must be present to continue.', 'popup-maker' ),
]
);
}
// Nonce.
if ( ! check_ajax_referer( 'pum_upgrade_ajax_nonce', 'nonce' ) ) {
wp_send_json_error(
[
'error' => __( 'You do not have permission to initiate this request. Contact an administrator for more information.', 'popup-maker' ),
]
);
}
if ( ! $upgrade_id ) {
$upgrade_id = $this->get_current_upgrade_id();
}
$step = ! empty( $_REQUEST['step'] ) ? absint( $_REQUEST['step'] ) : 1;
/**
* Instantiate the upgrade class.
*
* @var PUM_Interface_Batch_Process|PUM_Interface_Batch_PrefetchProcess $upgrade
*/
$upgrade = $this->get_upgrade( $upgrade_id, $step );
if ( false === $upgrade ) {
wp_send_json_error(
[
'error' => sprintf( __( '%s is an invalid batch process ID.', 'popup-maker' ), esc_html( $upgrade_id ) ),
]
);
}
/**
* Garbage collect any old temporary data in the case step is 1.
* Here to prevent case ajax passes step 1 without resetting process counts.
*/
$first_step = $step < 2;
if ( $first_step ) {
$upgrade->finish();
}
$using_prefetch = ( $upgrade instanceof PUM_Interface_Batch_PrefetchProcess );
// Handle pre-fetching data.
if ( $using_prefetch ) {
// Initialize any data needed to process a step.
$data = isset( $_REQUEST['form'] ) ? sanitize_key( $_REQUEST['form'] ) : [];
$upgrade->init( $data );
$upgrade->pre_fetch();
}
/** @var int|string|WP_Error $step */
$step = $upgrade->process_step();
if ( ! is_wp_error( $step ) ) {
$response_data = [
'step' => $step,
'next' => null,
];
// Finish and set the status flag if done.
if ( 'done' === $step ) {
$response_data['done'] = true;
$response_data['message'] = $upgrade->get_message( 'done' );
// Once all calculations have finished, run cleanup.
$upgrade->finish();
// Set the upgrade complete.
pum_set_upgrade_complete( $upgrade_id );
if ( $this->has_uncomplete_upgrades() ) {
// Since the other was complete return the next (now current) upgrade_id.
$response_data['next'] = $this->get_current_upgrade_id();
}
} else {
$response_data['done'] = false;
$response_data['message'] = $first_step ? $upgrade->get_message( 'start' ) : '';
$response_data['percentage'] = $upgrade->get_percentage_complete();
}
wp_send_json_success( $response_data );
} else {
wp_send_json_error( $step );
}
}
/**
* Returns the first key in the uncompleted upgrades.
*
* @return string|null
*/
public function get_current_upgrade_id() {
$upgrades = $this->get_uncompleted_upgrades();
reset( $upgrades );
return key( $upgrades );
}
/**
* Returns the current upgrade.
*
* @return bool|PUM_Interface_Batch_PrefetchProcess|PUM_Interface_Batch_Process
*/
public function get_current_upgrade() {
$upgrade_id = $this->get_current_upgrade_id();
return $this->get_upgrade( $upgrade_id );
}
/**
* Gets the upgrade process object.
*
* @param string $upgrade_id
* @param int $step
*
* @return bool|PUM_Interface_Batch_Process|PUM_Interface_Batch_PrefetchProcess
*/
public function get_upgrade( $upgrade_id = '', $step = 1 ) {
$upgrade = $this->registry->get( $upgrade_id );
if ( ! $upgrade ) {
return false;
}
$class = isset( $upgrade['class'] ) ? sanitize_text_field( $upgrade['class'] ) : '';
$class_file = isset( $upgrade['file'] ) ? $upgrade['file'] : '';
if ( ! class_exists( $class ) && ! empty( $class_file ) && file_exists( $class_file ) ) {
require_once $class_file;
} else {
wp_send_json_error(
[
'error' => sprintf( __( 'An invalid file path is registered for the %1$s batch process handler.', 'popup-maker' ), "<code>{$upgrade_id}</code>" ),
]
);
}
if ( empty( $class ) || ! class_exists( $class ) ) {
wp_send_json_error(
[
'error' => sprintf( __( '%1$s is an invalid handler for the %2$s batch process. Please try again.', 'popup-maker' ), "<code>{$class}</code>", "<code>{$upgrade_id}</code>" ),
]
);
}
/**
* @var PUM_Interface_Batch_Process|PUM_Interface_Batch_PrefetchProcess
*/
return new $class( $step );
}
/**
* Add upgrades tab to tools page if there are upgrades available.
*
* @param array $tabs
*
* @return array
*/
public function tools_page_tabs( $tabs = [] ) {
if ( $this->has_uncomplete_upgrades() ) {
$tabs['upgrades'] = __( 'Upgrades', 'popup-maker' );
}
return $tabs;
}
/**
* Renders upgrade form on the tools page upgrade tab.
*/
public function tools_page_tab_content() {
if ( ! $this->has_uncomplete_upgrades() ) {
_e( 'No upgrades currently required.', 'popup-maker' );
return;
}
// Enqueue admin JS for the batch processor.
wp_enqueue_script( 'pum-admin-batch' );
wp_enqueue_style( 'pum-admin-batch' );
$this->render_upgrade_notice();
$this->render_form();
}
}