PluginProbe
Blocks Kit – Gutenberg Blocks for Freelancers / 1.2.1
Blocks Kit – Gutenberg Blocks for Freelancers v1.2.1
blocks-kit / includes / feedback.php

feedback.php in Blocks Kit – Gutenberg Blocks for Freelancers 1.2.1, at includes/feedback.php

214 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin review notice class.
4 *
5 * Prompts users to review the plugin on WordPress.org after a period of usage.
6 *
7 * @package BlocksKit
8 * @since 1.0.0
9 */
10
11 // Exit if accessed directly.
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 if ( ! class_exists( 'Blockskit_Plugin_Review' ) ) :
17
18 /**
19 * Handles the admin review prompt.
20 *
21 * @since 1.0.0
22 */
23 class Blockskit_Plugin_Review {
24
25 /**
26 * Plugin slug.
27 *
28 * @var string
29 */
30 private $slug;
31
32 /**
33 * Plugin name.
34 *
35 * @var string
36 */
37 private $name;
38
39 /**
40 * Minimum seconds before showing the notice.
41 *
42 * @var int
43 */
44 private $time_limit;
45
46 /**
47 * Option name used to suppress the notice permanently.
48 *
49 * @var string
50 */
51 public $nobug_option;
52
53 /**
54 * Constructor.
55 *
56 * @param array $args Plugin configuration args (slug, name, time_limit).
57 */
58 public function __construct( $args ) {
59 $this->slug = $args['slug'];
60 $this->name = $args['name'];
61 $this->time_limit = isset( $args['time_limit'] ) ? $args['time_limit'] : WEEK_IN_SECONDS;
62
63 $this->nobug_option = $this->slug . '-no-bug';
64
65 add_action( 'admin_init', array( $this, 'check_installation_date' ) );
66 add_action( 'admin_init', array( $this, 'set_no_bug' ), 5 );
67 }
68
69 /**
70 * Convert a seconds value to a human-readable duration string.
71 *
72 * @param int $seconds Number of seconds elapsed since activation.
73 * @return string Human-readable duration.
74 */
75 public function seconds_to_words( $seconds ) {
76 $since_activation = time() - (int) get_site_option( $this->slug . '-activation-date' );
77
78 $years = (int) floor( $since_activation / YEAR_IN_SECONDS ) % 100;
79 if ( $years > 1 ) {
80 /* translators: %s: Number of years. */
81 return sprintf( __( '%s years', 'blocks-kit' ), $years );
82 } elseif ( $years > 0 ) {
83 return __( 'a year', 'blocks-kit' );
84 }
85
86 $weeks = (int) floor( $since_activation / WEEK_IN_SECONDS ) % 52;
87 if ( $weeks > 1 ) {
88 /* translators: %s: Number of weeks. */
89 return sprintf( __( '%s weeks', 'blocks-kit' ), $weeks );
90 } elseif ( $weeks > 0 ) {
91 return __( 'a week', 'blocks-kit' );
92 }
93
94 $days = ( (int) $seconds / DAY_IN_SECONDS ) % 7;
95 if ( $days > 1 ) {
96 /* translators: %s: Number of days. */
97 return sprintf( __( '%s days', 'blocks-kit' ), $days );
98 } elseif ( $days > 0 ) {
99 return __( 'a day', 'blocks-kit' );
100 }
101
102 $hours = ( (int) $seconds / HOUR_IN_SECONDS ) % 24;
103 if ( $hours > 1 ) {
104 /* translators: %s: Number of hours. */
105 return sprintf( __( '%s hours', 'blocks-kit' ), $hours );
106 } elseif ( $hours > 0 ) {
107 return __( 'an hour', 'blocks-kit' );
108 }
109
110 $minutes = ( (int) $seconds / MINUTE_IN_SECONDS ) % 60;
111 if ( $minutes > 1 ) {
112 /* translators: %s: Number of minutes. */
113 return sprintf( __( '%s minutes', 'blocks-kit' ), $minutes );
114 } elseif ( $minutes > 0 ) {
115 return __( 'a minute', 'blocks-kit' );
116 }
117
118 $secs = (int) $seconds % 60;
119 if ( $secs > 1 ) {
120 /* translators: %s: Number of seconds. */
121 return sprintf( __( '%s seconds', 'blocks-kit' ), $secs );
122 } elseif ( $secs > 0 ) {
123 return __( 'a second', 'blocks-kit' );
124 }
125
126 return '';
127 }
128
129 /**
130 * Check activation date on admin init and schedule the notice if time limit exceeded.
131 */
132 public function check_installation_date() {
133 if ( get_site_option( $this->nobug_option ) ) {
134 return;
135 }
136
137 $install_date = get_site_option( $this->slug . '-activation-date' );
138 if ( '' === $install_date || false === $install_date ) {
139 add_site_option( $this->slug . '-activation-date', time() );
140 $install_date = time();
141 }
142
143 if ( ( time() - (int) $install_date ) > $this->time_limit ) {
144 add_action( 'admin_notices', array( $this, 'display_admin_notice' ) );
145 }
146 }
147
148 /**
149 * Display the admin notice asking users to review the plugin.
150 */
151 public function display_admin_notice() {
152 $screen = get_current_screen();
153 if ( ! isset( $screen->base ) || 'plugins' !== $screen->base ) {
154 return;
155 }
156
157 $no_bug_url = esc_url(
158 wp_nonce_url(
159 admin_url( '?' . rawurlencode( $this->nobug_option ) . '=true' ),
160 'review-nonce'
161 )
162 );
163
164 $time = $this->seconds_to_words( time() - (int) get_site_option( $this->slug . '-activation-date' ) );
165
166 $message = sprintf(
167 /* translators: 1: Plugin name, 2: Duration of usage. */
168 esc_html__( 'You have been using the %1$s plugin for %2$s now, do you like it? If so, please leave us a review with your feedback!', 'blocks-kit' ),
169 '<strong>' . esc_html( $this->name ) . '</strong>',
170 esc_html( $time )
171 );
172
173 printf(
174 '<div class="updated notice is-dismissible"><p>%s &nbsp;<a href="%s">%s</a></p></div>',
175 wp_kses( $message, array( 'strong' => array() ) ),
176 $no_bug_url,
177 esc_html__( 'Dismiss', 'blocks-kit' )
178 );
179 }
180
181 /**
182 * Permanently suppress the notice when the user clicks Dismiss.
183 */
184 public function set_no_bug() {
185 if ( ! isset( $_GET['_wpnonce'] ) ) {
186 return;
187 }
188
189 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_GET['_wpnonce'] ) ), 'review-nonce' ) ) {
190 return;
191 }
192
193 if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
194 return;
195 }
196
197 if ( ! isset( $_GET[ $this->nobug_option ] ) ) {
198 return;
199 }
200
201 add_site_option( $this->nobug_option, true );
202 }
203 }
204
205 endif;
206
207 new Blockskit_Plugin_Review(
208 array(
209 'slug' => 'blocks-kit', // The plugin slug.
210 'name' => 'Blocks Kit', // The plugin name.
211 'time_limit' => DAY_IN_SECONDS, // The time limit at which notice is shown.
212 )
213 );
214