| 1 |
<?php |
| 2 |
/** |
| 3 |
* BBP Core Installer - No Class Version |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( ! function_exists( 'bbpcore_admin_notice' ) ) : |
| 7 |
|
| 8 |
/** |
| 9 |
* Show admin notice for bbPress requirement |
| 10 |
*/ |
| 11 |
function bbpcore_admin_notice() { |
| 12 |
if ( class_exists( 'bbPress' ) ) { |
| 13 |
return; // bbPress is active, no notice needed |
| 14 |
} |
| 15 |
|
| 16 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 17 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 18 |
} |
| 19 |
|
| 20 |
$has_installed = get_plugins(); |
| 21 |
$button_text = isset( $has_installed['bbpress/bbpress.php'] ) |
| 22 |
? esc_html__( 'Activate Now!', 'bbp-core' ) |
| 23 |
: esc_html__( 'Install Now!', 'bbp-core' ); |
| 24 |
?> |
| 25 |
<div class="notice notice-error is-dismissible"> |
| 26 |
<p> |
| 27 |
<?php |
| 28 |
printf( |
| 29 |
'<strong>%1$s</strong> %2$s <strong>%3$s</strong> %4$s', |
| 30 |
esc_html__( 'BBP Core', 'bbp-core' ), |
| 31 |
esc_html__( 'requires', 'bbp-core' ), |
| 32 |
esc_html__( 'bbPress', 'bbp-core' ), |
| 33 |
esc_html__( 'plugin to be installed and active. Please install or activate it now!', 'bbp-core' ) |
| 34 |
); |
| 35 |
?> |
| 36 |
</p> |
| 37 |
<p> |
| 38 |
<button id="bbp-core-install-core" class="button button-primary"> |
| 39 |
<?php echo esc_html( $button_text ); ?> |
| 40 |
</button> |
| 41 |
</p> |
| 42 |
</div> |
| 43 |
|
| 44 |
<script> |
| 45 |
jQuery(document).ready(function ($) { |
| 46 |
$('#bbp-core-install-core').on('click', function (e) { |
| 47 |
e.preventDefault(); |
| 48 |
var self = $(this); |
| 49 |
self.addClass('install-now updating-message'); |
| 50 |
self.text('<?php echo esc_js( __( 'Installing...', 'bbp-core' ) ); ?>'); |
| 51 |
|
| 52 |
$.ajax({ |
| 53 |
url: ajaxurl, |
| 54 |
type: 'POST', |
| 55 |
data: { |
| 56 |
action: 'bbpcore_install_core', |
| 57 |
_wpnonce: '<?php echo esc_js( wp_create_nonce( 'bbpcore_install_core' ) ); ?>', |
| 58 |
}, |
| 59 |
success: function (response) { |
| 60 |
if (response.success) { |
| 61 |
self.text('<?php echo esc_js( __( 'Installed', 'bbp-core' ) ); ?>'); |
| 62 |
window.location.reload(); |
| 63 |
} else { |
| 64 |
alert(response.data || '<?php echo esc_js( __( 'Something went wrong.', 'bbp-core' ) ); ?>'); |
| 65 |
self.text('<?php echo esc_js( __( 'Failed', 'bbp-core' ) ); ?>'); |
| 66 |
} |
| 67 |
}, |
| 68 |
error: function (xhr) { |
| 69 |
alert('AJAX Error: ' + xhr.responseText); |
| 70 |
self.removeClass('install-now updating-message'); |
| 71 |
}, |
| 72 |
complete: function () { |
| 73 |
self.attr('disabled', 'disabled'); |
| 74 |
self.removeClass('install-now updating-message'); |
| 75 |
} |
| 76 |
}); |
| 77 |
}); |
| 78 |
}); |
| 79 |
</script> |
| 80 |
<?php |
| 81 |
} |
| 82 |
add_action( 'admin_notices', 'bbpcore_admin_notice' ); |
| 83 |
|
| 84 |
endif; |
| 85 |
|
| 86 |
if ( ! function_exists( 'bbpcore_install_core' ) ) : |
| 87 |
|
| 88 |
/** |
| 89 |
* Handle AJAX install/activation of bbPress |
| 90 |
*/ |
| 91 |
function bbpcore_install_core() { |
| 92 |
check_ajax_referer( 'bbpcore_install_core' ); |
| 93 |
|
| 94 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 95 |
wp_send_json_error( __( 'You don\'t have permission to install plugins.', 'bbp-core' ) ); |
| 96 |
} |
| 97 |
|
| 98 |
$result = bbpcore_install_plugin( 'bbpress', 'bbpress.php' ); |
| 99 |
|
| 100 |
if ( is_wp_error( $result ) ) { |
| 101 |
wp_send_json_error( $result->get_error_message() ); |
| 102 |
} |
| 103 |
|
| 104 |
wp_send_json_success(); |
| 105 |
} |
| 106 |
add_action( 'wp_ajax_bbpcore_install_core', 'bbpcore_install_core' ); |
| 107 |
|
| 108 |
endif; |
| 109 |
|
| 110 |
if ( ! function_exists( 'bbpcore_install_plugin' ) ) : |
| 111 |
|
| 112 |
/** |
| 113 |
* Install and activate a plugin |
| 114 |
*/ |
| 115 |
function bbpcore_install_plugin( $slug, $file ) { |
| 116 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 117 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 118 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 119 |
|
| 120 |
$plugin_basename = $slug . '/' . $file; |
| 121 |
|
| 122 |
// Already installed? Just activate |
| 123 |
if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_basename ) ) { |
| 124 |
return activate_plugin( $plugin_basename ); |
| 125 |
} |
| 126 |
|
| 127 |
// Download from WP.org |
| 128 |
$api = plugins_api( 'plugin_information', array( |
| 129 |
'slug' => $slug, |
| 130 |
'fields' => array( 'sections' => false ), |
| 131 |
) ); |
| 132 |
|
| 133 |
if ( is_wp_error( $api ) ) { |
| 134 |
return $api; |
| 135 |
} |
| 136 |
|
| 137 |
$upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() ); |
| 138 |
$result = $upgrader->install( $api->download_link ); |
| 139 |
|
| 140 |
if ( is_wp_error( $result ) ) { |
| 141 |
return $result; |
| 142 |
} |
| 143 |
|
| 144 |
// Activate after install |
| 145 |
return activate_plugin( $plugin_basename ); |
| 146 |
} |
| 147 |
|
| 148 |
endif; |