PluginProbe
Squeeze – Image Optimization & Compression, WEBP Conversion / trunk
Squeeze – Image Optimization & Compression, WEBP Conversion vtrunk
1.7.15 1.7.14 1.7.13 1.7.12 1.7.11 1.7.10 trunk 1.0 1.1 1.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5 1.5.1 1.5.2 1.6 All 42 releases
squeeze / inc / review-notice.php

review-notice.php in Squeeze – Image Optimization & Compression, WEBP Conversion trunk, at inc/review-notice.php

192 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace SqueezeFree;
3
4 // Exit if accessed directly.
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 /**
10 * Soft wordpress.org review prompt after several successful squeezes.
11 */
12 class SqueezeReviewNotice extends SqueezeInit {
13
14 const THRESHOLD = 5;
15 const SNOOZE_DAYS = 30;
16 const OPTION_COUNT = 'squeeze_success_count';
17 const USER_META = 'squeeze_review_notice';
18 const REVIEW_URL = 'https://wordpress.org/support/plugin/squeeze/reviews/#new-post';
19
20 public function __construct() {
21 add_action( 'admin_notices', array( $this, 'maybe_render' ) );
22 add_action( 'wp_ajax_squeeze_dismiss_review', array( $this, 'ajax_dismiss' ) );
23 add_action( 'squeeze_successful_squeeze', array( $this, 'increment_success_count' ) );
24 }
25
26 /**
27 * Bump the site-wide success counter (one per successfully squeezed image).
28 */
29 public function increment_success_count() {
30 $count = get_option( self::OPTION_COUNT, null );
31 if ( null === $count ) {
32 $count = $this->seed_success_count();
33 }
34 update_option( self::OPTION_COUNT, (int) $count + 1, false );
35 }
36
37 /**
38 * @return int
39 */
40 public function get_success_count() {
41 $count = get_option( self::OPTION_COUNT, null );
42 if ( null === $count ) {
43 return $this->seed_success_count();
44 }
45 return (int) $count;
46 }
47
48 /**
49 * First-time seed from already-compressed attachments (existing installs).
50 *
51 * @return int
52 */
53 private function seed_success_count() {
54 global $wpdb;
55 $seed = (int) $wpdb->get_var(
56 $wpdb->prepare(
57 "SELECT COUNT(*) FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s",
58 'squeeze_is_compressed',
59 '1'
60 )
61 );
62 update_option( self::OPTION_COUNT, $seed, false );
63 return $seed;
64 }
65
66 /**
67 * @return bool
68 */
69 public function should_display() {
70 if ( ! current_user_can( 'manage_options' ) ) {
71 return false;
72 }
73 if ( ! $this->is_target_screen() ) {
74 return false;
75 }
76 if ( $this->is_dismissed() ) {
77 return false;
78 }
79 return $this->get_success_count() >= self::THRESHOLD;
80 }
81
82 /**
83 * @return bool
84 */
85 private function is_target_screen() {
86 if ( ! function_exists( 'get_current_screen' ) ) {
87 return false;
88 }
89 $screen = get_current_screen();
90 if ( ! $screen ) {
91 return false;
92 }
93 $target_screens = array(
94 'settings_page_squeeze',
95 'media_page_squeeze-bulk',
96 'upload',
97 'attachment',
98 );
99 return in_array( $screen->id, $target_screens, true );
100 }
101
102 /**
103 * @return bool
104 */
105 private function is_dismissed() {
106 $user_id = get_current_user_id();
107 if ( ! $user_id ) {
108 return true;
109 }
110 $value = get_user_meta( $user_id, self::USER_META, true );
111 if ( 'dismissed' === $value ) {
112 return true;
113 }
114 if ( is_numeric( $value ) && (int) $value > time() ) {
115 return true; // snoozed until timestamp
116 }
117 return false;
118 }
119
120 public function maybe_render() {
121 if ( ! $this->should_display() ) {
122 return;
123 }
124
125 $review_url = self::REVIEW_URL;
126 ?>
127 <div class="notice notice-info squeeze-review-notice" id="squeeze-review-notice">
128 <p>
129 <strong><?php esc_html_e( 'Enjoying Squeeze?', 'squeeze' ); ?></strong>
130 <?php esc_html_e( 'If the plugin helped you shrink your images, would you mind leaving a quick 5-star review? It really helps other WordPress users find it.', 'squeeze' ); ?>
131 </p>
132 <p>
133 <a class="button button-primary" href="<?php echo esc_url( $review_url ); ?>" target="_blank" rel="noopener noreferrer">
134 <?php esc_html_e( 'Leave a review', 'squeeze' ); ?>
135 </a>
136 &nbsp;
137 <button type="button" class="button button-secondary" data-squeeze-review-dismiss="snooze">
138 <?php esc_html_e( 'Maybe later', 'squeeze' ); ?>
139 </button>
140 &nbsp;
141 <button type="button" class="button-link" data-squeeze-review-dismiss="dismiss">
142 <?php esc_html_e( 'Already did / Don\'t show again', 'squeeze' ); ?>
143 </button>
144 </p>
145 </div>
146 <script>
147 (function () {
148 const notice = document.getElementById('squeeze-review-notice');
149 if (!notice) return;
150 notice.addEventListener('click', function (e) {
151 const btn = e.target.closest('[data-squeeze-review-dismiss]');
152 if (!btn) return;
153 e.preventDefault();
154 const action = btn.getAttribute('data-squeeze-review-dismiss');
155 const body = new FormData();
156 body.append('action', 'squeeze_dismiss_review');
157 body.append('_ajax_nonce', <?php echo wp_json_encode( wp_create_nonce( 'squeeze-nonce' ) ); ?>);
158 body.append('dismiss_action', action);
159 fetch(<?php echo wp_json_encode( admin_url( 'admin-ajax.php' ) ); ?>, {
160 method: 'POST',
161 credentials: 'same-origin',
162 body: body
163 }).finally(function () {
164 notice.style.display = 'none';
165 });
166 });
167 })();
168 </script>
169 <?php
170 }
171
172 public function ajax_dismiss() {
173 check_ajax_referer( 'squeeze-nonce', '_ajax_nonce' );
174
175 if ( ! current_user_can( 'manage_options' ) ) {
176 wp_send_json_error( null, 403 );
177 }
178
179 $user_id = get_current_user_id();
180 $action = isset( $_POST['dismiss_action'] ) ? sanitize_key( wp_unslash( $_POST['dismiss_action'] ) ) : 'dismiss';
181
182 if ( 'snooze' === $action ) {
183 $until = time() + ( DAY_IN_SECONDS * self::SNOOZE_DAYS );
184 update_user_meta( $user_id, self::USER_META, $until );
185 } else {
186 update_user_meta( $user_id, self::USER_META, 'dismissed' );
187 }
188
189 wp_send_json_success();
190 }
191 }
192