-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeveloper.php
688 lines (527 loc) · 27.6 KB
/
developer.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
<?php /*
**************************************************************************
Plugin Name: Ultimate Comment Pack
Plugin URI: http://wordpress.org/extend/plugins/ultimate-comment-pack/
Description: A curated collection of best practices commenting plugins.
Version: 1.2.6
Author: Postmatic
Author URI: http://gopostmatic.com
License: GPLv2 or later
Text Domain: ucp-developer
Domain Path: /languages/
**************************************************************************/
// Load helper class if installing a plugin
if ( ! empty( $_POST['action'] ) && 'ucp_developer_install_plugin' == $_POST['action'] )
require_once( dirname( __FILE__ ) . '/includes/class-empty-upgrader-skin.php' );
class Automattic_Developer {
public $settings = array();
public $default_settings = array();
const VERSION = '1.2.6';
const OPTION = 'ucp_developer';
const PAGE_SLUG = 'ucp_developer';
private $recommended_plugins = array();
function __construct() {
add_action( 'init', array( $this, 'load_plugin_textdomain') );
add_action( 'init', array( $this, 'init' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'register_settings_page' ) );
add_action( 'admin_bar_menu', array( $this, 'add_node_to_admin_bar' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'load_settings_page_script_and_style' ) );
add_action( 'wp_ajax_ucp_developer_lightbox_step_1', array( $this, 'ajax_handler' ) );
add_action( 'wp_ajax_ucp_developer_install_plugin', array( $this, 'ajax_handler' ) );
add_action( 'wp_ajax_ucp_developer_activate_plugin', array( $this, 'ajax_handler' ) );
}
// Internationalization
function load_plugin_textdomain () {
load_plugin_textdomain ( 'ucp-developer', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
// Allows private variables to be read. Basically implements read-only variables.
function __get( $var ) {
return ( isset( $this->$var ) ) ? $this->$var : null;
}
public function init() {
$this->default_settings = array(
'project_type' => false,
);
$this->settings = wp_parse_args( (array) get_option( self::OPTION ), $this->default_settings );
}
public function admin_init() {
if ( ! empty( $_GET['developer_plugin_reset'] ) && current_user_can( 'manage_options' ) ) {
delete_option( self::OPTION );
}
$this->recommended_plugins = array(
'postmatic' => array(
'project_type' => 'all',
'name' => esc_html__( 'Postmatic - Email comment subscriptions for huge increases in commenting.', 'ucp-developer' ),
'active' => class_exists( 'postmatic' ),
),
'epoch' => array(
'project_type' => 'all',
'name' => esc_html__( 'Epoch - Replace your comment form with one that is fast, beautiful, and fun.', 'ucp-developer' ),
'active' => function_exists( 'epoch' ),
),
'postmatic-social-commenting' => array(
'project_type' => 'all',
'name' => esc_html__( 'Postmatic Social Commenting - Let users comment using their twitter/facebook/google/wordpress profiles.', 'ucp-developer' ),
'active' => function_exists( 'postmatic-social-commenting' ),
),
'akismet' => array(
'project_type' => 'all',
'name' => esc_html__( 'Akismet - The standard for anti-spam', 'ucp-developer' ),
'active' => class_exists( 'Akismet' ),
),
'basic-comment-quicktags' => array(
'project_type' => 'all',
'name' => esc_html__( 'Basic Comment Quicktags - Add minimal bold/italic/link buttons to your comment form.', 'ucp-developer' ),
'active' => function_exists( 'BasicCommentsQuicktagsHELF' ),
),
'show-comment-policy' => array(
'project_type' => 'all',
'name' => esc_html__( 'Show Comment Policy - Set behavioural expectations before users leave a comment.', 'ucp-developer' ),
'active' => class_exists( 'show-comment-policy' ),
),
'goodbye-captcha' => array(
'project_type' => 'all',
'name' => esc_html__( 'WPBruiser - Front-end antispam plugin that keeps bots from submitting the comment form.', 'ucp-developer' ),
'active' => class_exists( 'goodbye-captcha' ),
),
'crowd-control' => array(
'project_type' => 'large',
'name' => esc_html__( 'Crowd Control', 'ucp-developer' ),
'active' => class_exists( 'postmatic-crowd' ),
),
'jetpack' => array(
'project_type' => 'large',
'name' => esc_html__( 'Jetpack', 'ucp-developer' ),
'active' => class_exists( 'Jetpack' ),
),
'polldaddy' => array(
'project_type' => 'large',
'name' => esc_html__( 'Polldaddy Polls & Ratings', 'ucp-developer' ),
'active' => class_exists( 'WP_Polldaddy' ),
),
'monster-widget' => array(
'project_type' => 'all',
'name' => esc_html__( 'Monster Widget', 'ucp-developer' ),
'active' => class_exists( 'Monster_Widget' ),
),
'user-switching' => array(
'project_type' => 'all',
'name' => esc_html__( 'User Switching', 'ucp-developer' ),
'active' => class_exists( 'user_switching' ),
),
'piglatin' => array(
'project_type' => array( 'small', 'wporg' ),
'name' => esc_html__( 'Pig Latin', 'ucp-developer' ),
'active' => class_exists( 'PigLatin' ),
),
// Theme Developer
'rtl-tester' => array(
'project_type' => 'small',
'name' => esc_html__( 'RTL Tester', 'ucp-developer' ),
'active' => class_exists( 'RTLTester' ),
),
'regenerate-thumbnails' => array(
'project_type' => 'small',
'name' => esc_html__( 'Regenerate Thumbnails', 'ucp-developer' ),
'active' => class_exists( 'RegenerateThumbnails' ),
),
'simply-show-ids' => array(
'project_type' => 'small',
'name' => esc_html__( 'Simply Show IDs', 'ucp-developer' ),
'active' => function_exists( 'ssid_add' ),
),
'theme-test-drive' => array(
'project_type' => 'small',
'name' => esc_html__( 'Theme Test Drive', 'ucp-developer' ),
'active' => function_exists( 'TTD_filters' ),
'filename' => 'themedrive.php',
),
'theme-check' => array(
'project_type' => 'small',
'name' => esc_html__( 'Theme Check', 'ucp-developer' ),
'active' => class_exists( 'ThemeCheckMain' ),
),
);
register_setting( self::OPTION, self::OPTION, array( $this, 'settings_validate' ) );
wp_register_script( 'ucp-developer', plugins_url( 'developer.js', __FILE__ ), array( 'jquery' ), self::VERSION );
$strings = array(
'settings_slug' => self::PAGE_SLUG,
'go_to_step_2' => ( current_user_can( 'install_plugins' ) && current_user_can( 'activate_plugins' ) && 'direct' == get_filesystem_method() ) ? 'yes' : 'no',
'lightbox_title' => __( 'Ultimate Comment Pack', 'ucp-developer' ),
'saving' => __( 'Saving...', 'ucp-developer' ),
'installing' => '' . esc_html__( 'Installing...', 'ucp-developer' ),
'installed' => __( '✓ Activated', 'ucp-developer' ),
'activating' => '' . esc_html__( 'Activating...', 'ucp-developer' ),
'activated' => __( '✓ Activated', 'ucp-developer' ),
'error' => __( 'Error!', 'ucp-developer' ),
'ACTIVE' => __( 'ACTIVE', 'ucp-developer' ),
'INSTALLED' => __( 'INSTALLED', 'ucp-developer' ),
'ERROR' => __( 'ERROR!', 'ucp-developer' ),
);
wp_localize_script( 'ucp-developer', 'ucp_developer_i18n', $strings );
wp_register_style( 'ucp-developer', plugins_url( 'developer.css', __FILE__ ), array(), self::VERSION );
// Handle the submission of the lightbox form if step 2 won't be shown
if ( ! empty( $_POST['action'] ) && 'ucp_developer_lightbox_step_1' == $_POST['action'] && ! empty( $_POST['ucp_developer_project_type'] ) && check_admin_referer( 'ucp_developer_lightbox_step_1' ) ) {
$this->save_project_type( $_POST['ucp_developer_project_type'] );
add_settings_error( 'general', 'settings_updated', __( 'Settings saved.' ), 'updated' );
}
if ( ! get_option( self::OPTION ) ) {
if ( ! empty( $_GET['ucpdev_errorsaving'] ) ) {
add_settings_error( self::PAGE_SLUG, self::PAGE_SLUG . '_error_saving', __( 'Error saving settings. Please try again.', 'ucp-developer' ) );
} elseif ( ! is_network_admin() && current_user_can( 'manage_options' ) ) {
add_action( 'admin_enqueue_scripts', array( $this, 'load_lightbox_scripts_and_styles' ) );
add_action( 'admin_footer', array( $this, 'output_setup_box_html' ) );
}
}
}
public function register_settings_page() {
add_submenu_page( 'options-general.php', esc_html__( 'Ultimate Comment Pack', 'ucp-developer' ), esc_html__( 'Ultimate Comment Pack', 'ucp-developer' ), 'manage_options', self::PAGE_SLUG, array( $this, 'settings_page' ) );
}
public function add_node_to_admin_bar( $wp_admin_bar ) {
if ( !current_user_can( 'manage_options' ) )
return;
$wp_admin_bar->add_node( array(
'id' => self::PAGE_SLUG,
'title' => esc_html__( 'Developer', 'ucp-developer' ),
'parent' => 'top-secondary', // Off on the right side
'href' => admin_url( 'settings.php?page=' . self::PAGE_SLUG ),
'meta' => array(
'title' => esc_html__( 'View the Ultimate Comment Pack settings and status page', 'ucp-developer' ),
),
) );
}
public function load_settings_page_script_and_style( $hook_suffix ) {
if ( 'setttings_page_' . self::PAGE_SLUG != $hook_suffix )
return;
wp_enqueue_script( 'ucp-developer' );
wp_enqueue_style( 'ucp-developer' );
}
public function load_lightbox_scripts_and_styles() {
wp_enqueue_script( 'colorbox', plugins_url( 'colorbox/jquery.colorbox-min.js', __FILE__ ), array( 'jquery' ), '1.3.19' );
wp_enqueue_style( 'ucp-developer-colorbox', plugins_url( 'colorbox/colorbox.css', __FILE__ ), array(), '1.3.19' );
wp_enqueue_script( 'ucp-developer' );
wp_enqueue_style( 'ucp-developer' );
}
public function output_setup_box_html() {
?>
<div style="display:none">
<div id="ucp-developer-setup-dialog-step-1" class="ucp-developer-dialog">
<strong><?php esc_html_e( "Tell us a little bit about your site. We'll recommend the best free commenting plugins for your community.", 'ucp-developer' ); ?></strong>
<p><?php esc_html_e( 'What best describes the kinds of commenting you hope to achieve?', 'ucp-developer' ); ?></p>
<form id="ucp-developer-setup-dialog-step-1-form" action="settings.php?page=ucp_developer" method="post">
<?php wp_nonce_field( 'ucp_developer_lightbox_step_1' ); ?>
<input type="hidden" name="action" value="ucp_developer_lightbox_step_1" />
<?php $i = 0; ?>
<?php foreach ( $this->get_project_types() as $project_slug => $project_description ) : ?>
<?php $i++; ?>
<p>
<label>
<input type="radio" name="ucp_developer_project_type" value="<?php echo esc_attr( $project_slug ); ?>" <?php checked( $i, 1 ); ?> />
<?php echo $project_description; ?>
</label>
</p>
<?php endforeach; ?>
<?php submit_button( null, 'primary', 'ucp-developer-setup-dialog-step-1-submit' ); ?>
</form>
</div>
<div id="ucp-developer-setup-dialog-step-2" class="ucp-developer-dialog">
<!-- This gets populated via AJAX -->
</div>
</div>
<script type="text/javascript">ucp_developer_lightbox();</script>
<?php
}
public function ajax_handler( $action ) {
$action = isset( $_POST['action'] ) ? $_POST['action'] : $action;
switch ( $action ) {
case 'ucp_developer_lightbox_step_1':
check_ajax_referer( 'ucp_developer_lightbox_step_1' );
if ( empty( $_POST['ucp_developer_project_type'] ) )
die( '-1' );
$this->save_project_type( $_POST['ucp_developer_project_type'] );
$to_install_or_enable = 0;
$recommended_plugins = $this->get_recommended_plugins();
foreach ( $recommended_plugins as $plugin_slug => $plugin_details ) {
if ( ! $plugin_details['active'] ) {
$to_install_or_enable++;
}
}
// If no plugins to take action on, head to the settings page
if ( ! $to_install_or_enable )
die( 'redirect' );
echo '<strong>' . esc_html__( 'Plugins', 'ucp-developer' ) . '</strong>';
echo '<p>' . esc_html__( 'Here is a curated list of commenting plugins that would work well for your site:', 'ucp-developer' ) . '</p>';
echo '<table class="recommended-plugins">';
foreach ( $recommended_plugins as $plugin_slug => $plugin_details ) {
if ( $plugin_details['active'] )
continue;
echo '<tr>';
$details = $this->get_plugin_details( $plugin_slug );
if ( is_wp_error( $details ) )
$details = array();
$plugin_details = array_merge( (array) $details, array( 'slug' => $plugin_slug ), $plugin_details );
echo '<td><strong>' . $plugin_details['name'] . '</strong></td>';
echo '<td>';
if ( ! empty( $plugin_details['short_description'] ) )
echo '<span class="description">' . esc_html__( $plugin_details['short_description'] ) . '</span>';
if ( $this->is_recommended_plugin_installed( $plugin_slug ) ) {
$path = $this->get_path_for_recommended_plugin( $plugin_slug );
echo '<button type="button" class="ucp-developer-button-activate" data-path="' . esc_attr( $path ) . '" data-nonce="' . wp_create_nonce( 'ucp_developer_activate_plugin_' . $path ) . '">' . esc_html__( 'Activate', 'ucp-developer' ) . '</button>';
} else {
echo '<button type="button" class="ucp-developer-button-install" data-pluginslug="' . esc_attr( $plugin_slug ) . '" data-nonce="' . wp_create_nonce( 'ucp_developer_install_plugin_' . $plugin_slug ) . '">' . esc_html__( 'Install', 'ucp-developer' ) . '</button>';
}
echo '</td>';
echo '</tr>';
}
echo '<tr><td colspan="2"><button type="button" class="ucp-developer-button-close">' . esc_html__( 'All set!', 'ucp-developer' ) . '</button></td></tr>';
echo '</table>';
echo '<script type="text/javascript">ucp_developer_bind_events();</script>';
exit();
case 'ucp_developer_install_plugin':
if ( empty( $_POST['plugin_slug'] ) )
die( __( 'ERROR: No slug was passed to the AJAX callback.', 'ucp-developer' ) );
check_ajax_referer( 'ucp_developer_install_plugin_' . $_POST['plugin_slug'] );
if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) )
die( __( 'ERROR: You lack permissions to install and/or activate plugins.', 'ucp-developer' ) );
include_once ( ABSPATH . 'wp-admin/includes/plugin-install.php' );
$api = plugins_api( 'plugin_information', array( 'slug' => $_POST['plugin_slug'], 'fields' => array( 'sections' => false ) ) );
if ( is_wp_error( $api ) )
die( sprintf( __( 'ERROR: Error fetching plugin information: %s', 'ucp-developer' ), $api->get_error_message() ) );
$upgrader = new Plugin_Upgrader( new Automattic_Developer_Empty_Upgrader_Skin( array(
'nonce' => 'install-plugin_' . $_POST['plugin_slug'],
'plugin' => $_POST['plugin_slug'],
'api' => $api,
) ) );
$install_result = $upgrader->install( $api->download_link );
if ( ! $install_result || is_wp_error( $install_result ) ) {
// $install_result can be false if the file system isn't writeable.
$error_message = __( 'Please ensure the file system is writeable', 'ucp-developer' );
if ( is_wp_error( $install_result ) )
$error_message = $install_result->get_error_message();
die( sprintf( __( 'ERROR: Failed to install plugin: %s', 'ucp-developer' ), $error_message ) );
}
$activate_result = activate_plugin( $this->get_path_for_recommended_plugin( $_POST['plugin_slug'] ) );
if ( is_wp_error( $activate_result ) )
die( sprintf( __( 'ERROR: Failed to activate plugin: %s', 'ucp-developer' ), $activate_result->get_error_message() ) );
exit( '1' );
case 'ucp_developer_activate_plugin':
if ( empty( $_POST['path'] ) )
die( __( 'ERROR: No slug was passed to the AJAX callback.', 'ucp-developer' ) );
check_ajax_referer( 'ucp_developer_activate_plugin_' . $_POST['path'] );
if ( ! current_user_can( 'activate_plugins' ) )
die( __( 'ERROR: You lack permissions to activate plugins.', 'ucp-developer' ) );
$activate_result = activate_plugin( $_POST['path'] );
if ( is_wp_error( $activate_result ) )
die( sprintf( __( 'ERROR: Failed to activate plugin: %s', 'ucp-developer' ), $activate_result->get_error_message() ) );
exit( '1' );
}
// Unknown action
die( '-1' );
}
public function settings_page() {
add_settings_section( 'ucp_developer_main', esc_html__( 'Main Configuration', 'ucp-developer' ), '__return_false', self::PAGE_SLUG . '_settings' );
add_settings_field( 'ucp_developer_project_type', esc_html__( 'Project Type', 'ucp-developer' ), array( $this, 'settings_field_radio' ), self::PAGE_SLUG . '_settings', 'ucp_developer_main', array(
'name' => 'project_type',
'description' => '',
'options' => $this->get_project_types(),
) );
echo '<script type="text/javascript">
jQuery(function( $ ) {
ucp_developer_bind_settings_events();
});
</script>';
// Plugins
add_settings_section( 'ucp_developer_plugins', esc_html__( 'Plugins', 'ucp-developer' ), array( $this, 'settings_section_plugins' ), self::PAGE_SLUG . '_status' );
wp_enqueue_script( 'plugin-install' );
add_thickbox();
$recommended_plugins = $this->get_recommended_plugins();
foreach ( $recommended_plugins as $plugin_slug => $plugin_details ) {
$details = $this->get_plugin_details( $plugin_slug );
if ( is_wp_error( $details ) )
$details = array();
$plugin_details = array_merge( (array) $details, array( 'slug' => $plugin_slug ), $plugin_details );
$label = '<strong>' . esc_html( $plugin_details['name'] ) . '</strong>';
$label .= '<br /><a href="' . self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin_slug .
'&TB_iframe=true&width=600&height=750' ) . '" class="thickbox" title="' .
esc_attr( sprintf( __( 'More information about %s' ), $plugin_details['name'] ) ) . '">' . __( 'Details' ) . '</a>';
add_settings_field( 'ucp_developer_plugin_' . $plugin_slug, $label, array( $this, 'settings_field_plugin' ), self::PAGE_SLUG . '_status', 'ucp_developer_plugins', $plugin_details );
}
# Add more sections and fields here as needed
?>
<div class="wrap">
<?php screen_icon( 'comments' ); ?>
<h2><?php esc_html_e( 'Ultimate Comment Pack', 'ucp-developer' ); ?></h2>
<form action="options.php" method="post">
<?php settings_fields( self::OPTION ); // matches value from register_setting() ?>
<?php do_settings_sections( self::PAGE_SLUG . '_settings' ); // matches values from add_settings_section/field() ?>
<?php submit_button(); ?>
<?php do_settings_sections( self::PAGE_SLUG . '_status' ); ?>
</form>
</div>
<?php
}
public function settings_field_radio( $args ) {
if ( empty( $args['name'] ) || ! is_array( $args['options'] ) )
return false;
$selected = ( isset( $this->settings[ $args['name'] ] ) ) ? $this->settings[ $args['name'] ] : '';
foreach ( (array) $args['options'] as $value => $label )
echo '<p><label><input type="radio" name="ucp_developer[' . esc_attr( $args['name'] ) . ']" value="' . esc_attr( $value ) . '"' . checked( $value, $selected, false ) . '> ' . $label . '</input></label></p>';
if ( ! empty( $args['description'] ) )
echo ' <p class="description">' . $args['description'] . '</p>';
}
public function settings_field_select( $args ) {
if ( empty( $args['name'] ) || ! is_array( $args['options'] ) )
return false;
$selected = ( isset( $this->settings[ $args['name'] ] ) ) ? $this->settings[ $args['name'] ] : '';
echo '<select name="ucp_developer[' . esc_attr( $args['name'] ) . ']">';
foreach ( (array) $args['options'] as $value => $label )
echo '<option value="' . esc_attr( $value ) . '"' . selected( $value, $selected, false ) . '>' . $label . '</option>';
echo '</select>';
if ( ! empty( $args['description'] ) )
echo ' <p class="description">' . $args['description'] . '</p>';
}
public function settings_section_plugins() {
echo '<p>' . esc_html__( 'Here is a list of recommended plugins for your site:', 'ucp-developer' ) . '</p>';
}
public function settings_field_plugin( $args ) {
if ( $args['active'] ) {
echo '<span class="ucp-developer-active">' . esc_html__( 'ACTIVE', 'ucp-developer' ) . '</span>';
} elseif ( $this->is_recommended_plugin_installed( $args['slug'] ) ) {
// Needs to be activated
if ( current_user_can('activate_plugins') ) {
$path = $this->get_path_for_recommended_plugin( $args['slug'] );
echo '<a class="ucp-developer-notactive ucp-developer-button-activate" href="' . esc_url( wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=' . $path ), 'activate-plugin_' . $path ) ) . '" data-path="' . esc_attr( $path ) . '" data-nonce="' . wp_create_nonce( 'ucp_developer_activate_plugin_' . $path ) . '" title="' . esc_attr__( 'Click here to activate', 'ucp-developer' ) . '">' . esc_html__( 'INACTIVE', 'ucp-developer' ) . ' - <em>' . esc_html__( 'Click to Activate', 'ucp-developer' ) . '</em></a>';
} else {
echo '<span class="ucp-developer-notactive">' . esc_html__( 'INACTIVE', 'ucp-developer' ) . '</span>';
}
} else {
// Needs to be installed
if ( current_user_can('install_plugins') ) {
echo '<a class="ucp-developer-notactive ucp-developer-button-install" href="' . esc_url( wp_nonce_url( admin_url( 'update.php?action=install-plugin&plugin=' . $args['slug'] ), 'install-plugin_' . $args['slug'] ) ) . '" data-pluginslug="' . esc_attr( $args['slug'] ) . '" data-nonce="' . wp_create_nonce( 'ucp_developer_install_plugin_' . $args['slug'] ) . '" title="' . esc_attr__( 'Click here to install', 'ucp-developer' ) . '">' . esc_html__( 'NOT INSTALLED', 'ucp-developer' ) . ' - <em>' . esc_html__( 'Click to Install', 'ucp-developer' ) . '</em></a>';
} else {
echo '<span class="ucp-developer-notactive">' . esc_html__( 'NOT INSTALLED', 'ucp-developer' ) . '</span>';
}
}
if ( ! empty( $args['short_description'] ) )
echo '<br /><span class="description">' . $args['short_description'] . '</span>';
}
public function settings_validate( $raw_settings ) {
$settings = array();
$project_type_slugs = array_keys( $this->get_project_types() );
if ( empty( $raw_settings['project_type'] ) || ! in_array( $raw_settings['project_type'], $project_type_slugs ) )
$settings['project_type'] = current( $project_type_slugs );
else
$settings['project_type'] = $raw_settings['project_type'];
return $settings;
}
public function save_project_type( $type ) {
$settings = $this->settings;
$settings['project_type'] = $type;
$this->settings = $this->settings_validate( $settings );
update_option( self::OPTION, $this->settings );
}
public function get_path_for_recommended_plugin( $slug ) {
$filename = ( ! empty( $this->recommended_plugins[$slug]['filename'] ) ) ? $this->recommended_plugins[$slug]['filename'] : $slug . '.php';
return $slug . '/' . $filename;
}
public function is_recommended_plugin_active( $slug ) {
if ( empty( $this->recommended_plugins[$slug] ) )
return false;
return $this->recommended_plugins[$slug]['active'];
}
public function is_recommended_plugin_installed( $slug ) {
if ( empty( $this->recommended_plugins[$slug] ) )
return false;
if ( $this->is_recommended_plugin_active( $slug ) || file_exists( WP_PLUGIN_DIR . '/' . $this->get_path_for_recommended_plugin( $slug ) ) )
return true;
return false;
}
/**
* Retrieve plugin information for a given $slug
*
* Note that this does not use plugins_api(), as the .org api does not return
* short descriptions in POST requests (that api endpoint is different from this one)
*
* @param string $slug The plugin slug
* @return object The response object containing plugin details
*/
public function get_plugin_details( $slug ){
$cache_key = md5( 'ucp_developer_plugin_details_' . $slug );
if ( false === ( $details = get_transient( $cache_key ) ) ) {
$request = wp_remote_get( 'http://api.wordpress.org/plugins/info/1.0/' . esc_url( $slug ), array( 'timeout' => 15 ) );
if ( is_wp_error( $request ) ) {
$details = new WP_Error('ucp_developer_plugins_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), $request->get_error_message() );
} else {
$details = maybe_unserialize( wp_remote_retrieve_body( $request ) );
if ( ! is_object( $details ) && ! is_array( $details ) )
$details = new WP_Error('ucp_developer_plugins_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), wp_remote_retrieve_body( $request ) );
else
set_transient( $cache_key, $details, WEEK_IN_SECONDS );
}
}
return $details;
}
/**
* Return an array of all plugins recommended for the current project type
*
* Only returns plugins that have been recommended for the project type defined
* in $this->settings['project_type']
*
* @return array An array of plugins recommended for the current project type
*/
public function get_recommended_plugins() {
return $this->get_recommended_plugins_by_type( $this->settings['project_type'] );
}
/**
* Return an array of all plugins recommended for the given project type
*
* @param string $type The project type to return plugins for
* @return array An associative array of plugins for the project type
*/
public function get_recommended_plugins_by_type( $type ) {
$plugins_by_type = array();
foreach( $this->recommended_plugins as $plugin_slug => $plugin_details ) {
if ( ! $this->plugin_is_recommended_for_project_type( $plugin_slug, $type ) )
continue;
$plugins_by_type[ $plugin_slug ] = $plugin_details;
}
return $plugins_by_type;
}
/**
* Should the given plugin be recommended for the given project type?
*
* Determines whether or not a given $plugin_slug is recommended for a given $project_type
* by checking the project types defined for it
*
* @param string $plugin_slug The plugin slug to check
* @param string $project_type The project type to check the plugin against
* @return bool Boolean indicating if the plugin is recommended for the project type
*/
public function plugin_is_recommended_for_project_type( $plugin_slug, $project_type = null ) {
if ( null == $project_type )
$project_type = $this->settings['project_type'];
$plugin_details = $this->recommended_plugins[ $plugin_slug ];
if ( 'all' == $plugin_details['project_type'] )
return true;
return self::is_project_type( $plugin_details, $project_type );
}
public function get_project_types() {
return array(
'wporg' => __( 'I\'m setting up a brand new WordPress site and need the best commenting setup available.', 'ucp-developer' ),
'small' => __( 'I run a small site with only a few comments per post, but hope to increase engagement.', 'ucp-developer' ),
'large' => __( 'I run a large site with dozens or hundreds of comments per post and need help managing them.', 'ucp-developer' ),
);
}
private static function is_dev_version() {
$cur = get_preferred_from_update_core();
return isset( $cur->response ) && $cur->response == 'development';
}
private static function is_project_type( $project, $type ) {
$project_type = $project['project_type'];
if ( is_array( $project_type ) )
return in_array( $type, $project_type );
return $project_type == $type;
}
}
$automattic_developer = new Automattic_Developer();