| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Settings\Components; |
| 4 |
|
| 5 |
use ImageOptimization\Modules\Core\Components\Pointers; |
| 6 |
use ImageOptimization\Modules\Settings\Module; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Settings_Pointer { |
| 13 |
const CURRENT_POINTER_SLUG = 'image-optimizer-settings'; |
| 14 |
|
| 15 |
public function admin_print_script() { |
| 16 |
if ( $this->is_dismissed() ) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
wp_enqueue_script( 'wp-pointer' ); |
| 21 |
wp_enqueue_style( 'wp-pointer' ); |
| 22 |
|
| 23 |
$pointer_content = '<h3>' . esc_html__( 'Image Optimization settings', 'image-optimization' ) . '</h3>'; |
| 24 |
$pointer_content .= '<p>' . esc_html__( 'Head over to the Image Optimization Settings to fine-tune how your media uploads are managed.', 'image-optimization' ) . '</p>'; |
| 25 |
|
| 26 |
$pointer_content .= sprintf( |
| 27 |
'<p><a class="button button-primary image-optimization-pointer-settings-link" href="%s">%s</a></p>', |
| 28 |
admin_url( 'admin.php?page=' . Module::SETTING_BASE_SLUG ), |
| 29 |
esc_html__( 'Take me there', 'image-optimization' ) |
| 30 |
); |
| 31 |
$allowed_tags = [ |
| 32 |
'h3' => [], |
| 33 |
'p' => [], |
| 34 |
'a' => [ |
| 35 |
'class' => [], |
| 36 |
'href' => [], |
| 37 |
], |
| 38 |
]; |
| 39 |
?> |
| 40 |
<script> |
| 41 |
const onClose = () => { |
| 42 |
return wp.ajax.post( 'image_optimizer_pointer_dismissed', { |
| 43 |
data: { |
| 44 |
pointer: '<?php echo esc_attr( static::CURRENT_POINTER_SLUG ); ?>', |
| 45 |
}, |
| 46 |
nonce: '<?php echo esc_attr( wp_create_nonce( 'image-optimization-pointer-dismissed' ) ); ?>', |
| 47 |
} ); |
| 48 |
} |
| 49 |
|
| 50 |
jQuery( document ).ready( function( $ ) { |
| 51 |
$( '#menu-media' ).pointer( { |
| 52 |
content: '<?php echo wp_kses( $pointer_content, $allowed_tags ); ?>', |
| 53 |
position: { |
| 54 |
edge: <?php echo is_rtl() ? "'right'" : "'left'"; ?>, |
| 55 |
align: 'center' |
| 56 |
}, |
| 57 |
close: onClose |
| 58 |
} ).pointer( 'open' ); |
| 59 |
|
| 60 |
$( '.image-optimization-pointer-settings-link' ).first().on( 'click', function( e ) { |
| 61 |
e.preventDefault(); |
| 62 |
|
| 63 |
$(this).attr( 'disabled', true ); |
| 64 |
|
| 65 |
onClose().promise().done(() => { |
| 66 |
location = $(this).attr( 'href' ); |
| 67 |
}); |
| 68 |
}) |
| 69 |
} ); |
| 70 |
</script> |
| 71 |
<?php |
| 72 |
} |
| 73 |
|
| 74 |
public function is_dismissed(): bool { |
| 75 |
$meta = (array) get_user_meta( get_current_user_id(), Pointers::DISMISSED_POINTERS_META_KEY, true ); |
| 76 |
|
| 77 |
return key_exists( static::CURRENT_POINTER_SLUG, $meta ); |
| 78 |
} |
| 79 |
|
| 80 |
public function __construct() { |
| 81 |
add_action( 'in_admin_header', [ $this, 'admin_print_script' ] ); |
| 82 |
} |
| 83 |
} |
| 84 |
|