[
self::DISMISSED => 'image-optimization-once',
self::CAPABILITY => 'install_plugins',
self::DEFINED => 'IMAGE_OPTIMIZATION_VERSION',
],
'image-optimization-once-media-modal' => [
self::DISMISSED => 'image-optimization-once-media-modal',
self::CAPABILITY => 'install_plugins',
self::DEFINED => 'IMAGE_OPTIMIZATION_VERSION',
],
'image-optimization' => [
self::DISMISSED => 'image_optimizer_hint',
self::CAPABILITY => 'install_plugins',
self::DEFINED => 'IMAGE_OPTIMIZATION_VERSION',
],
'image-optimization-media-modal' => [
self::DISMISSED => 'image-optimization-media-modal',
self::CAPABILITY => 'install_plugins',
self::DEFINED => 'IMAGE_OPTIMIZATION_VERSION',
],
];
if ( ! $hint_key ) {
return $hints;
}
return $hints[ $hint_key ] ?? [];
}
/**
* get_notice_icon
* @return string
*/
public static function get_notice_icon(): string {
return '
';
}
/**
* get_notice_template
*
* Print or Retrieve the notice template.
* @param array $notice
* @param bool $return
*
* @return string|void
*/
public static function get_notice_template( array $notice, bool $return = false ) {
$default_settings = [
'type' => 'info',
'icon' => false,
'heading' => '',
'content' => '',
'dismissible' => false,
'button_text' => '',
'button_event' => '',
'button_data' => [],
'display' => false,
];
$notice_settings = array_merge( $default_settings, $notice );
if ( empty( $notice_settings['heading'] ) && empty( $notice_settings['content'] ) ) {
return '';
}
if ( ! in_array( $notice_settings['type'], self::get_notice_types(), true ) ) {
$notice_settings['type'] = 'info';
}
$icon = '';
$heading = '';
$content = '';
$dismissible = '';
$button = '';
if ( $notice_settings['icon'] ) {
$icon = self::get_notice_icon();
}
if ( ! empty( $notice_settings['heading'] ) ) {
$heading = '' . $notice_settings['heading'] . '
';
}
if ( ! empty( $notice_settings['content'] ) ) {
$content = '' . $notice_settings['content'] . '
';
}
if ( ! empty( $notice_settings['button_text'] ) ) {
$button_settings = ( ! empty( $notice_settings['button_data'] ) ) ? ' data-settings="' . esc_attr( json_encode( $notice_settings['button_data'] ) ) . '"' : '';
$button = '
';
}
if ( $notice_settings['dismissible'] ) {
$dismissible = '';
}
$notice_template = sprintf( '',
$notice_settings['type'],
$icon,
$heading,
$content,
$button,
$dismissible,
$notice_settings['display']
);
if ( $return ) {
return $notice_template;
}
echo wp_kses( $notice_template, self::get_notice_allowed_html() );
}
/**
* get_plugin_install_url
* @param $plugin_slug
*
* @return string
*/
public static function get_plugin_install_url( $plugin_slug ): string {
$action = 'install-plugin';
return wp_nonce_url(
add_query_arg(
[
'action' => $action,
'plugin' => $plugin_slug,
],
admin_url( 'update.php' )
),
$action . '_' . $plugin_slug
);
}
/**
* get_plugin_activate_url
* @param $plugin_slug
*
* @return string
*/
public static function get_plugin_activate_url( $plugin_slug ): string {
$path = "$plugin_slug/$plugin_slug.php";
return wp_nonce_url(
admin_url( 'plugins.php?action=activate&plugin=' . $path ),
'activate-plugin_' . $path
);
}
/**
* is_dismissed
* @param $key
*
* @return bool
*/
public static function is_dismissed( $key ): bool {
$dismissed = User::get_dismissed_editor_notices();
return in_array( $key, $dismissed, true );
}
/**
* should_display_hint
* @param $hint_key
*
* @return bool
*/
public static function should_display_hint( $hint_key ): bool {
$hint = self::get_hints( $hint_key );
if ( empty( $hint ) ) {
return false;
}
foreach ( $hint as $key => $value ) {
switch ( $key ) {
case self::DISMISSED:
if ( self::is_dismissed( $value ) ) {
return false;
}
break;
case self::CAPABILITY:
if ( ! current_user_can( $value ) ) {
return false;
}
break;
case self::DEFINED:
if ( defined( $value ) ) {
return false;
}
break;
case self::PLUGIN_INSTALLED:
if ( ! self::is_plugin_installed( $value ) ) {
return false;
}
break;
case self::PLUGIN_ACTIVE:
if ( ! self::is_plugin_active( $value ) ) {
return false;
}
break;
}
}
return true;
}
private static function is_conflict_plugin_installed(): bool {
if ( ! Utils::has_pro() ) {
return false;
}
$conflicting_plugins = [
'imagify/imagify.php',
'optimole-wp/optimole-wp.php',
'ewww-image-optimizer/ewww-image-optimizer.php',
'ewww-image-optimizer-cloud/ewww-image-optimizer-cloud.php',
'kraken-image-optimizer/kraken.php',
'shortpixel-image-optimiser/wp-shortpixel.php',
'wp-smushit/wp-smush.php',
'wp-smush-pro/wp-smush.php',
'tiny-compress-images/tiny-compress-images.php',
];
foreach ( $conflicting_plugins as $plugin ) {
if ( self::is_plugin_active( $plugin ) ) {
return true;
}
}
return false;
}
/**
* is_plugin_installed
* @param $plugin
*
* @return bool
*/
public static function is_plugin_installed( $plugin ) : bool {
$plugins = get_plugins();
$plugin = self::ensure_plugin_folder( $plugin );
return ! empty( $plugins[ $plugin ] );
}
/**
* is_plugin_active
* @param $plugin
*
* @return bool
*/
public static function is_plugin_active( $plugin ): bool {
$plugin = self::ensure_plugin_folder( $plugin );
return is_plugin_active( $plugin );
}
/**
* get_plugin_action_url
* @param $plugin
*
* @return string
*/
public static function get_plugin_action_url( $plugin ): string {
if ( ! self::is_plugin_installed( $plugin ) ) {
return self::get_plugin_install_url( $plugin );
}
if ( ! self::is_plugin_active( $plugin ) ) {
return self::get_plugin_activate_url( $plugin );
}
return '';
}
/**
* ensure_plugin_folder
* @param $plugin
*
* @return string
*/
private static function ensure_plugin_folder( $plugin ): string {
if ( false === strpos( $plugin, '/' ) ) {
$plugin = $plugin . '/' . $plugin . '.php';
}
return $plugin;
}
/**
* get_notice_allowed_html
* @return array[]
*/
public static function get_notice_allowed_html(): array {
return [
'div' => [
'class' => [],
'data-display' => [],
],
'svg' => [
'width' => [],
'height' => [],
'viewbox' => [],
'fill' => [],
'xmlns' => [],
],
'path' => [
'd' => [],
'stroke' => [],
'stroke-width' => [],
'stroke-linecap' => [],
'stroke-linejoin' => [],
],
'button' => [
'class' => [],
'data-event' => [],
'data-settings' => [],
'data-tooltip' => [],
],
'i' => [
'class' => [],
'aria-hidden' => [],
],
'span' => [
'class' => [],
],
'a' => [
'href' => [],
'style' => [],
'target' => [],
],
];
}
}