| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Core\Components; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Image\Image_Query_Builder; |
| 6 |
use ImageOptimization\Classes\Utils; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class User_Feedback { |
| 13 |
public const NOTICE_FEEDBACK_LINK = 'https://go.elementor.com/io-wp-dash-notice-review'; |
| 14 |
public const FOOTER_FEEDBACK_LINK = 'https://go.elementor.com/io-wp-dash-footer-review'; |
| 15 |
private const FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG = 'image-optimizer-first-image-optimized-feedback'; |
| 16 |
private const THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG = 'image-optimizer-third-images-optimized-feedback'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Checks if the notice after the first optimization should be rendered. |
| 20 |
* |
| 21 |
* @param bool $check_location |
| 22 |
* |
| 23 |
* @return bool |
| 24 |
*/ |
| 25 |
public function should_render_first_optimized_notice( bool $check_location = true ): bool { |
| 26 |
if ( Pointers::is_dismissed( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ) ) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
$is_wrong_page = ( ! Utils::is_media_page() && ! Utils::is_plugin_page() && ! Utils::is_single_attachment_page() ) || |
| 31 |
Utils::is_plugin_settings_page(); |
| 32 |
|
| 33 |
if ( $check_location && $is_wrong_page ) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
$optimized_image_query = ( new Image_Query_Builder() ) |
| 38 |
->return_optimized_images() |
| 39 |
->set_paging_size( 1 ) |
| 40 |
->execute(); |
| 41 |
|
| 42 |
if ( ! $optimized_image_query->post_count ) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Checks if the notice should be rendered after the one third of images were optimized. |
| 51 |
* |
| 52 |
* @param bool $check_location |
| 53 |
* |
| 54 |
* @return bool |
| 55 |
*/ |
| 56 |
public function should_render_third_optimized_notice( bool $check_location = true ): bool { |
| 57 |
if ( Pointers::is_dismissed( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ) ) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
if ( $check_location && ! Utils::is_plugin_settings_page() ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
$images_query = ( new Image_Query_Builder() ) |
| 66 |
->execute(); |
| 67 |
|
| 68 |
$optimized_images_query = ( new Image_Query_Builder() ) |
| 69 |
->return_optimized_images() |
| 70 |
->execute(); |
| 71 |
|
| 72 |
if ( ! $optimized_images_query->post_count ) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
$optimized_percentage = round( $optimized_images_query->post_count / $images_query->post_count * 100 ); |
| 77 |
|
| 78 |
if ( $optimized_percentage < 33.33 ) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Renders the notice after the first optimization. |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function render_first_optimized_notice() { |
| 91 |
?> |
| 92 |
<div class="notice is-dismissible notice-success notice image-optimizer__notice image-optimizer__notice--success image-optimizer__notice--feedback" |
| 93 |
data-notice-slug="<?php echo esc_attr( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ); ?>"> |
| 94 |
<p> |
| 95 |
<b> |
| 96 |
<?php esc_html_e( |
| 97 |
'Your image has been optimized!', |
| 98 |
'image-optimization' |
| 99 |
); ?> |
| 100 |
</b> |
| 101 |
|
| 102 |
<span> |
| 103 |
<?php printf( |
| 104 |
__( |
| 105 |
'If you enjoyed using Image Optimizer, consider leaving a <a href="%1$s" aria-label="%2$s" target="_blank" |
| 106 |
rel="noopener noreferrer">� |
| 107 |
� |
| 108 |
� |
| 109 |
� |
| 110 |
� |
| 111 |
</a> review to spread the word.', |
| 112 |
'image-optimization' |
| 113 |
), |
| 114 |
esc_url( self::NOTICE_FEEDBACK_LINK ), |
| 115 |
esc_attr__( 'Five stars', 'image-optimization' ) |
| 116 |
); ?> |
| 117 |
</span> |
| 118 |
</p> |
| 119 |
</div> |
| 120 |
|
| 121 |
<script> |
| 122 |
const onClose = () => { |
| 123 |
const pointer = '<?php |
| 124 |
echo $this->should_render_third_optimized_notice( false ) ? |
| 125 |
esc_js( join( ',', [ static::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG, static::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ] ) ) : |
| 126 |
esc_js( static::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ) |
| 127 |
?>'; |
| 128 |
|
| 129 |
return wp.ajax.post( 'image_optimizer_pointer_dismissed', { |
| 130 |
data: { |
| 131 |
pointer, |
| 132 |
}, |
| 133 |
nonce: '<?php echo esc_js( wp_create_nonce( 'image-optimization-pointer-dismissed' ) ); ?>', |
| 134 |
} ); |
| 135 |
} |
| 136 |
|
| 137 |
jQuery( document ).ready( function( $ ) { |
| 138 |
setTimeout(() => { |
| 139 |
const $closeButton = $( '[data-notice-slug="<?php echo esc_js( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ); ?>"] .notice-dismiss' ) |
| 140 |
|
| 141 |
$closeButton |
| 142 |
.first() |
| 143 |
.on( 'click', onClose ) |
| 144 |
|
| 145 |
$( '[data-notice-slug="<?php echo esc_js( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ); ?>"] a' ) |
| 146 |
.first() |
| 147 |
.on( 'click', function ( e ) { |
| 148 |
e.preventDefault(); |
| 149 |
|
| 150 |
onClose().promise().done(() => { |
| 151 |
window.open( $( this ).attr( 'href' ), '_blank' ).focus(); |
| 152 |
|
| 153 |
$closeButton.click(); |
| 154 |
}); |
| 155 |
}) |
| 156 |
}, 0); |
| 157 |
} ); |
| 158 |
</script> |
| 159 |
<?php |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Renders the notice after the one third of images were optimized. |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public function render_third_optimized_notice() { |
| 168 |
?> |
| 169 |
<div class="notice is-dismissible notice-success notice image-optimizer__notice image-optimizer__notice--success image-optimizer__notice--feedback" |
| 170 |
data-notice-slug="<?php echo esc_attr( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ); ?>"> |
| 171 |
<p> |
| 172 |
<b> |
| 173 |
<?php esc_html_e( |
| 174 |
'Thanks for using Image Optimizer!', |
| 175 |
'image-optimization' |
| 176 |
); ?> |
| 177 |
</b> |
| 178 |
|
| 179 |
<span> |
| 180 |
<?php printf( |
| 181 |
__( |
| 182 |
'If you\'ve enjoyed it, consider leaving a <a href="%1$s" aria-label="%2$s" target="_blank" |
| 183 |
rel="noopener noreferrer">� |
| 184 |
� |
| 185 |
� |
| 186 |
� |
| 187 |
� |
| 188 |
</a> review to spread the word.', |
| 189 |
'image-optimization' |
| 190 |
), |
| 191 |
esc_url( self::NOTICE_FEEDBACK_LINK ), |
| 192 |
esc_attr__( 'Five stars', 'image-optimization' ) |
| 193 |
); ?> |
| 194 |
</span> |
| 195 |
</p> |
| 196 |
</div> |
| 197 |
|
| 198 |
<script> |
| 199 |
const onClose = () => { |
| 200 |
const pointer = '<?php |
| 201 |
echo $this->should_render_first_optimized_notice( false ) ? |
| 202 |
esc_js( join( ',', [ static::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG, static::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ] ) ) : |
| 203 |
esc_js( static::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ) |
| 204 |
?>'; |
| 205 |
|
| 206 |
return wp.ajax.post( 'image_optimizer_pointer_dismissed', { |
| 207 |
data: { |
| 208 |
pointer, |
| 209 |
}, |
| 210 |
nonce: '<?php echo esc_js( wp_create_nonce( 'image-optimization-pointer-dismissed' ) ); ?>', |
| 211 |
} ); |
| 212 |
} |
| 213 |
|
| 214 |
jQuery( document ).ready( function( $ ) { |
| 215 |
setTimeout(() => { |
| 216 |
const $closeButton = $( '[data-notice-slug="<?php echo esc_js( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ); ?>"] .notice-dismiss' ); |
| 217 |
|
| 218 |
$closeButton |
| 219 |
.first() |
| 220 |
.on( 'click', onClose) |
| 221 |
|
| 222 |
$( '[data-notice-slug="<?php echo esc_js( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ); ?>"] a' ) |
| 223 |
.first() |
| 224 |
.on( 'click', function ( e ) { |
| 225 |
e.preventDefault(); |
| 226 |
|
| 227 |
onClose().promise().done(() => { |
| 228 |
window.open( $( this ).attr( 'href' ), '_blank' ).focus(); |
| 229 |
|
| 230 |
$closeButton.click(); |
| 231 |
}); |
| 232 |
}) |
| 233 |
}, 0); |
| 234 |
} ); |
| 235 |
</script> |
| 236 |
<?php |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Renders the admin footer feedback notice. |
| 241 |
* |
| 242 |
* @return void |
| 243 |
*/ |
| 244 |
public function add_leave_feedback_footer_text(): void { |
| 245 |
printf( |
| 246 |
__( '<b>Found Image Optimizer helpful?</b> Leave us a <a href="%1$s" aria-label="%2$s" target="_blank" |
| 247 |
rel="noopener noreferrer">� |
| 248 |
� |
| 249 |
� |
| 250 |
� |
| 251 |
� |
| 252 |
</a> rating!', |
| 253 |
'image-optimization' |
| 254 |
), |
| 255 |
esc_url( self::FOOTER_FEEDBACK_LINK ), |
| 256 |
esc_attr__( 'Five stars', 'image-optimization' ) |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
public function __construct() { |
| 261 |
add_action('current_screen', function () { |
| 262 |
if ( ! Utils::user_is_admin() ) { |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
if ( $this->should_render_first_optimized_notice() ) { |
| 267 |
add_action( 'admin_notices', [ $this, 'render_first_optimized_notice' ] ); |
| 268 |
} |
| 269 |
|
| 270 |
if ( $this->should_render_third_optimized_notice() ) { |
| 271 |
add_action( 'admin_notices', [ $this, 'render_third_optimized_notice' ] ); |
| 272 |
} |
| 273 |
|
| 274 |
if ( Utils::is_media_page() || Utils::is_plugin_page() || Utils::is_single_attachment_page() ) { |
| 275 |
add_filter( 'admin_footer_text', [ $this, 'add_leave_feedback_footer_text' ] ); |
| 276 |
} |
| 277 |
}); |
| 278 |
} |
| 279 |
} |
| 280 |
|