1 | <?php |
||
29 | class WP_Review_Me { |
||
30 | |||
31 | /** |
||
32 | * Library version |
||
33 | * |
||
34 | * @since 1.0 |
||
35 | * @var string |
||
36 | */ |
||
37 | public $version = '1.0'; |
||
38 | |||
39 | /** |
||
40 | * Required version of PHP. |
||
41 | * |
||
42 | * @since 1.0 |
||
43 | * @var string |
||
44 | */ |
||
45 | public $php_version_required = '5.5'; |
||
46 | |||
47 | /** |
||
48 | * Minimum version of WordPress required to use the library |
||
49 | * |
||
50 | * @since 1.0 |
||
51 | * @var string |
||
52 | */ |
||
53 | public $wordpress_version_required = '4.2'; |
||
54 | |||
55 | /** |
||
56 | * Holds the unique identifying key for this particular instance |
||
57 | * |
||
58 | * @since 1.0 |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $key; |
||
62 | |||
63 | /** |
||
64 | * Link unique ID |
||
65 | * |
||
66 | * @since 1.0 |
||
67 | * @var string |
||
68 | */ |
||
69 | public $link_id; |
||
70 | |||
71 | /** |
||
72 | * WP_Review_Me constructor. |
||
73 | * |
||
74 | * @since 1.0 |
||
75 | * |
||
76 | * @param array $args Object settings |
||
77 | */ |
||
78 | public function __construct( $args ) { |
||
79 | |||
80 | $args = wp_parse_args( $args, $this->get_defaults() ); |
||
81 | $this->days = $args['days_after']; |
||
82 | $this->type = $args['type']; |
||
83 | $this->slug = $args['slug']; |
||
84 | $this->rating = $args['rating']; |
||
85 | $this->message = $args['message']; |
||
86 | $this->link_label = $args['link_label']; |
||
87 | $this->cap = $args['cap']; |
||
88 | $this->scope = $args['scope']; |
||
89 | |||
90 | // Set the unique identifying key for this instance |
||
91 | $this->key = 'wrm_' . substr( md5( plugin_basename( __FILE__ ) ), 0, 20 ); |
||
92 | $this->link_id = 'wrm-review-link-' . $this->key; |
||
93 | |||
94 | // Instantiate |
||
95 | $this->init(); |
||
96 | |||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Get default object settings |
||
101 | * |
||
102 | * @since 1.0 |
||
103 | * @return array |
||
104 | */ |
||
105 | protected function get_defaults() { |
||
122 | |||
123 | /** |
||
124 | * Initialize the library |
||
125 | * |
||
126 | * @since 1.0 |
||
127 | * @return void |
||
128 | */ |
||
129 | private function init() { |
||
130 | |||
131 | // Make sure WordPress is compatible |
||
132 | if ( ! $this->is_wp_compatible() ) { |
||
133 | $this->spit_error( |
||
134 | sprintf( |
||
135 | esc_html__( 'The library can not be used because your version of WordPress is too old. You need version %s at least.', 'wp-review-me' ), |
||
136 | $this->wordpress_version_required |
||
137 | ) |
||
138 | ); |
||
139 | |||
140 | return; |
||
141 | } |
||
142 | |||
143 | // Make sure PHP is compatible |
||
144 | if ( ! $this->is_php_compatible() ) { |
||
145 | $this->spit_error( |
||
146 | sprintf( |
||
147 | esc_html__( 'The library can not be used because your version of PHP is too old. You need version %s at least.', 'wp-review-me' ), |
||
148 | $this->php_version_required |
||
149 | ) |
||
150 | ); |
||
151 | |||
152 | return; |
||
153 | } |
||
154 | |||
155 | // Make sure the dependencies are loaded |
||
156 | if ( ! function_exists( 'dnh_register_notice' ) ) { |
||
157 | |||
158 | $dnh_file = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'vendor/julien731/wp-dismissible-notices-handler/handler.php'; |
||
159 | |||
160 | if ( file_exists( $dnh_file ) ) { |
||
161 | require( $dnh_file ); |
||
162 | } |
||
163 | |||
164 | if ( ! function_exists( 'dnh_register_notice' ) ) { |
||
165 | $this->spit_error( |
||
166 | sprintf( |
||
167 | esc_html__( 'Dependencies are missing. Please run a %s.', 'wp-review-me' ), |
||
168 | '<code>composer install</code>' |
||
169 | ) |
||
170 | ); |
||
171 | |||
172 | return; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | add_action( 'admin_footer', array( $this, 'script' ) ); |
||
177 | add_action( 'wp_ajax_wrm_clicked_review', array( $this, 'dismiss_notice' ) ); |
||
178 | |||
179 | // And let's roll... maybe. |
||
180 | $this->maybe_prompt(); |
||
181 | |||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Check if the current WordPress version fits the requirements |
||
186 | * |
||
187 | * @since 1.0 |
||
188 | * @return boolean |
||
189 | */ |
||
190 | private function is_wp_compatible() { |
||
199 | |||
200 | /** |
||
201 | * Check if the version of PHP is compatible with this library |
||
202 | * |
||
203 | * @since 1.0 |
||
204 | * @return boolean |
||
205 | */ |
||
206 | private function is_php_compatible() { |
||
215 | |||
216 | /** |
||
217 | * Spits an error message at the top of the admin screen |
||
218 | * |
||
219 | * @since 1.0 |
||
220 | * |
||
221 | * @param string $error Error message to spit |
||
222 | * |
||
223 | * @return void |
||
224 | */ |
||
225 | protected function spit_error( $error ) { |
||
232 | |||
233 | /** |
||
234 | * Check if it is time to ask for a review |
||
235 | * |
||
236 | * @since 1.0 |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function is_time() { |
||
255 | |||
256 | /** |
||
257 | * Save the current date as the installation date |
||
258 | * |
||
259 | * @since 1.0 |
||
260 | * @return void |
||
261 | */ |
||
262 | protected function setup_date() { |
||
265 | |||
266 | /** |
||
267 | * Get the review link |
||
268 | * |
||
269 | * @since 1.0 |
||
270 | * @return string |
||
271 | */ |
||
272 | protected function get_review_link() { |
||
295 | |||
296 | /** |
||
297 | * Get the complete link tag |
||
298 | * |
||
299 | * @since 1.0 |
||
300 | * @return string |
||
301 | */ |
||
302 | protected function get_review_link_tag() { |
||
303 | |||
304 | $link = $this->get_review_link(); |
||
305 | |||
306 | return "<a href='$link' target='_blank' id='$this->link_id'>$this->link_label</a>"; |
||
307 | |||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Trigger the notice if it is time to ask for a review |
||
312 | * |
||
313 | * @since 1.0 |
||
314 | * @return void |
||
315 | */ |
||
316 | protected function maybe_prompt() { |
||
328 | |||
329 | /** |
||
330 | * Echo the JS script in the admin footer |
||
331 | * |
||
332 | * @since 1.0 |
||
333 | * @return void |
||
334 | */ |
||
335 | public function script() { ?> |
||
361 | |||
362 | /** |
||
363 | * Dismiss the notice when the review link is clicked |
||
364 | * |
||
365 | * @since 1.0 |
||
366 | * @return void |
||
367 | */ |
||
368 | public function dismiss_notice() { |
||
407 | |||
408 | /** |
||
409 | * Get the review prompt message |
||
410 | * |
||
411 | * @since 1.0 |
||
412 | * @return string |
||
413 | */ |
||
414 | protected function get_message() { |
||
423 | |||
424 | } |
||
425 | |||
426 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.