/
wc-install-notice.php
374 lines (322 loc) · 9.07 KB
/
wc-install-notice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* Description: Display a notice to install WooCommerce if it's not installed or activated.
* Author: Mahdi Yazdani
* Author URI: https://mahdiyazdani.com
* Version: 1.0.0
* Text Domain: wc-install-notice
* License: GPL-3.0+
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
*
* @package WC_Install_Notice
*/
namespace WC_Install_Notice;
if ( ! defined( 'ABSPATH' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit;
}
/**
* Class Nag.
*/
class Nag {
/**
* The plugin slug.
*
* @since 1.0.0
*/
const PLUGIN_SLUG = 'woocommerce';
/**
* The plugin file.
*
* @since 1.0.0
*/
const PLUGIN_FILE = 'woocommerce.php';
/**
* The plugin name.
*
* @since 1.0.0
*
* @var string
*/
private $plugin_name = '';
/**
* The plugin file path.
*
* @since 1.0.0
*
* @var string
*/
private $plugin_file = '';
/**
* The dismiss notice transient.
*
* @since 1.0.0
*
* @var string
*/
private $dismiss_transient = '';
/**
* The plugin learn more link.
*
* @since 1.0.0
*
* @var string
*/
private $learn_more_link = 'https://wordpress.org/plugins/woocommerce/';
/**
* Set the plugin name for the admin notice.
*
* @since 1.0.0
*
* @param string $name Name of the plugin to be used in admin notices.
*
* @return __CLASS__
*/
public function set_plugin_name( $name ) {
$this->plugin_name = $name;
return $this;
}
/**
* Set the plugin file path.
*
* @since 1.0.0
*
* @param string $path The plugin file path.
*
* @return __CLASS__
*/
public function set_file_path( $path ) {
$this->plugin_file = $path;
$this->dismiss_transient = 'wc_install_notice_dismiss_transient_' . sanitize_title( plugin_basename( $path ) );
return $this;
}
/**
* Set the WooCommerce plugin learn more link.
*
* @since 1.0.0
*
* @param string $link The learn more link.
*
* @return __CLASS__
*/
public function set_learn_more_link( $link ) {
$this->learn_more_link = $link;
return $this;
}
/**
* Conditionally hook in an admin notice.
*
* @since 1.0.0
*
* @return bool
*/
public function does_it_requires_nag() {
// Bail early if the admin screen is not loaded.
if ( ! is_admin() ) {
return false;
}
// Bail early if the WooCommerce plugin is activated.
if ( $this->is_activated() ) {
return false;
}
// Bail early if the notice has been dismissed.
if ( $this->dismiss_transient && get_transient( $this->dismiss_transient ) ) {
return false;
}
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
add_action( 'admin_notices', array( $this, 'print_nag' ) );
add_action( 'network_admin_notices', array( $this, 'print_nag' ) );
add_action( 'wp_ajax_wc_install_notice_dismiss_notice', array( $this, 'dismiss_notice' ) );
return true;
}
/**
* Enqueue the admin notice assets.
*
* @since 1.0.0
*
* @return void
*/
public function enqueue() {
// Bail early if the plugin file path is not set.
if ( empty( $this->plugin_file ) ) {
return;
}
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : trailingslashit( 'minified' );
wp_enqueue_style( 'wc-install-notice', plugin_dir_url( $this->plugin_file ) . 'vendor/mahdiyazdani/wc-install-notice/assets/css/' . $min . 'admin.css', array(), '1.0.0' );
wp_enqueue_script( 'wc-install-notice', plugin_dir_url( $this->plugin_file ) . 'vendor/mahdiyazdani/wc-install-notice/assets/js/' . $min . 'admin.js', array( 'jquery' ), '1.0.0', true );
wp_localize_script(
'wc-install-notice',
'wc_install_notice',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'wc_install_notice_dismiss_notice_nonce' ),
)
);
}
/**
* Print the admin notice.
*
* @since 1.0.0
*
* @return void
*/
public function print_nag() {
?>
<div class="notice notice-error wc-install-notice-nux<?php echo ! empty( $this->dismiss_transient ) ? ' is-dismissible' : ''; ?>">
<span class="notice-icon"></span>
<div class="notice-content">
<?php $this->get_admin_notice(); ?>
<?php $this->install_button(); ?>
</div>
</div>
<?php
}
/**
* AJAX dismiss notice.
*
* @since 1.0.0
*
* @return void
*/
public function dismiss_notice() {
// Bail early if the nonce is invalid.
if ( ! isset( $_POST['nonce'] )
|| ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['nonce'] ) ), 'wc_install_notice_dismiss_notice_nonce' )
|| ! current_user_can( 'manage_options' )
) {
die();
}
// Set the transient to dismiss the notice for 1 hour.
set_transient( $this->dismiss_transient, true, HOUR_IN_SECONDS );
}
/**
* Return the string to be shown in the admin notice.
*
* @since 1.0.0
*
* @return void
*/
public function get_admin_notice() {
// Ignore the plugin name if it's not set.
if ( ! empty( $this->plugin_name ) ) {
/* translators: %s: Plugin name */
echo '<h2>' . sprintf( esc_html__( 'Thanks for installing %s, you rock! 🤘', 'wc-install-notice' ), $this->plugin_name ) . '</h2>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo '<p>' . esc_html__( 'To enable eCommerce features you need to install the WooCommerce plugin.', 'wc-install-notice' ) . '</p>';
}
/**
* Output a button that will install or activate a plugin if it doesn't exist, or display a disabled button if the
* plugin is already activated.
*
* @since 1.0.0
*
* @return void
*/
public function install_button() {
// Bail early if the current user doesn't have the required capabilities.
if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) {
return;
}
// Check if the WooCommerce plugin is installed.
if ( $this->is_installed() ) {
$url = $this->is_installed();
// The plugin exists but isn't activated yet.
$button = array(
'message' => __( 'Activate WooCommerce', 'wc-install-notice' ),
'url' => $url,
'classes' => 'activate-now',
);
} else {
// The plugin doesn't exist.
$url = wp_nonce_url(
add_query_arg(
array(
'action' => 'install-plugin',
'plugin' => self::PLUGIN_SLUG,
),
self_admin_url( 'update.php' )
),
'install-plugin_' . self::PLUGIN_SLUG
);
$button = array(
'message' => __( 'Install WooCommerce', 'wc-install-notice' ),
'url' => $url,
'classes' => ' install-now install-' . self::PLUGIN_SLUG,
);
}
?>
<p>
<span class="plugin-card-woocommerce">
<a
href="<?php echo esc_url( $button['url'] ); ?>"
class="wc-install-notice <?php echo esc_attr( $button['classes'] ); ?>"
data-originaltext="<?php echo esc_attr( $button['message'] ); ?>"
data-slug="<?php echo esc_attr( self::PLUGIN_SLUG ); ?>"
aria-label="<?php echo esc_attr( $button['message'] ); ?>"
><?php echo esc_html( $button['message'] ); ?></a>
</span>
<?php esc_html_e( 'or', 'wc-install-notice' ); ?>
<a
href="<?php echo esc_url( $this->learn_more_link ); ?>"
target="_blank"
rel="noopener noreferrer"
><?php esc_html_e( 'Learn more', 'wc-install-notice' ); ?></a>
</p>
<?php
}
/**
* Check if the WooCommerce plugin is activated.
*
* @since 1.0.0
*
* @return bool
*/
public function is_activated() {
// This statement prevents from producing fatal errors,
// in case the WooCommerce plugin is not activated on the site.
// phpcs:ignore WooCommerce.Commenting.CommentHooks.HookCommentWrongStyle
$woocommerce_plugin = apply_filters( 'wc_install_notice_woocommerce_path', self::PLUGIN_SLUG . '/' . self::PLUGIN_FILE );
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
$subsite_active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
$network_active_plugins = apply_filters( 'active_plugins', get_site_option( 'active_sitewide_plugins' ) );
// Bail early in case the plugin is not activated on the website.
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
if ( ( empty( $subsite_active_plugins ) || ! in_array( $woocommerce_plugin, $subsite_active_plugins ) ) && ( empty( $network_active_plugins ) || ! array_key_exists( $woocommerce_plugin, $network_active_plugins ) ) ) {
return false;
}
return true;
}
/**
* Check if the WooCommerce plugin is installed and return the url to activate it if so.
*
* @since 1.0.0
*
* @return string|bool
*/
private function is_installed() {
// Check if the plugin directory exists.
if ( ! file_exists( WP_PLUGIN_DIR . '/' . self::PLUGIN_SLUG ) ) {
return false;
}
$plugins = get_plugins( '/' . self::PLUGIN_SLUG );
// Check if the plugin is installed.
if ( empty( $plugins ) ) {
return false;
}
$keys = array_keys( $plugins );
$plugin_file = self::PLUGIN_SLUG . '/' . $keys[0];
$url = wp_nonce_url(
add_query_arg(
array(
'action' => 'activate',
'plugin' => $plugin_file,
),
admin_url( 'plugins.php' )
),
'activate-plugin_' . $plugin_file
);
return $url;
}
}