| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! class_exists( 'BBPCore_Install_Core' ) ) : |
| 4 |
|
| 5 |
/** |
| 6 |
* BetterDocs Upsell Class |
| 7 |
*/ |
| 8 |
class BBPCore_Install_Core { |
| 9 |
|
| 10 |
/** |
| 11 |
* Instantiate the class |
| 12 |
* |
| 13 |
* @param string $affiliate |
| 14 |
*/ |
| 15 |
function __construct() { |
| 16 |
add_action( 'init', array( $this, 'init_hooks' ) ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Initialize the hooks |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function init_hooks() { |
| 25 |
|
| 26 |
if ( class_exists( 'bbpress' ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
add_action( 'admin_notices', array( $this, 'activation_notice' ) ); |
| 35 |
|
| 36 |
add_action( 'wp_ajax_BBPCore_Install_Core_installer', array( $this, 'install_bbpcore_core' ) ); |
| 37 |
} |
| 38 |
/** |
| 39 |
* Show the plugin installation notice |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function activation_notice() { |
| 44 |
?> |
| 45 |
<script> |
| 46 |
jQuery(document).ready( function($) { |
| 47 |
$('#bbp-core-install-core').on('click', function (e) { |
| 48 |
var self = $(this); |
| 49 |
e.preventDefault(); |
| 50 |
self.addClass('install-now updating-message'); |
| 51 |
self.text('<?php echo esc_js( 'Installing...' ); ?>'); |
| 52 |
|
| 53 |
$.ajax({ |
| 54 |
url: '<?php echo admin_url( 'admin-ajax.php' ); ?>', |
| 55 |
type: 'post', |
| 56 |
data: { |
| 57 |
action: 'BBPCore_Install_Core_installer', |
| 58 |
_wpnonce: '<?php echo wp_create_nonce('BBPCore_Install_Core_installer'); ?>', |
| 59 |
}, |
| 60 |
success: function(response) { |
| 61 |
self.text('<?php echo esc_js( 'Installed' ); ?>'); |
| 62 |
window.location.href = '<?php echo admin_url( 'admin.php?page=bbp-core' ); ?>'; |
| 63 |
}, |
| 64 |
error: function(error) { |
| 65 |
self.removeClass('install-now updating-message'); |
| 66 |
alert( error ); |
| 67 |
}, |
| 68 |
complete: function() { |
| 69 |
self.attr('disabled', 'disabled'); |
| 70 |
self.removeClass('install-now updating-message'); |
| 71 |
} |
| 72 |
}); |
| 73 |
}); |
| 74 |
} ); |
| 75 |
</script> |
| 76 |
<?php |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Fail if plugin installtion/activation fails |
| 82 |
* |
| 83 |
* @param Object $thing |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function fail_on_error( $thing ) { |
| 88 |
if ( is_wp_error( $thing ) ) { |
| 89 |
wp_send_json_error( $thing->get_error_message() ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Install BetterDocs |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function install_bbpcore_core() { |
| 99 |
check_ajax_referer( 'BBPCore_Install_Core_installer' ); |
| 100 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 101 |
wp_send_json_error( __( 'You don\'t have permission to install the plugins' ) ); |
| 102 |
} |
| 103 |
$is_installed = isset( $_POST['installed'] ) ? $_POST['installed'] : false; |
| 104 |
$nx_status = $this->install_plugin( 'bbpress', 'bbpress.php' ); |
| 105 |
$this->fail_on_error( $nx_status ); |
| 106 |
|
| 107 |
wp_send_json_success(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Install and activate a plugin |
| 112 |
* |
| 113 |
* @param string $slug |
| 114 |
* @param string $file |
| 115 |
* |
| 116 |
* @return WP_Error|null |
| 117 |
*/ |
| 118 |
public function install_plugin( $slug, $file ) { |
| 119 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 120 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 121 |
|
| 122 |
$plugin_basename = $slug . '/' . $file; |
| 123 |
|
| 124 |
// if exists and not activated |
| 125 |
if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_basename ) ) { |
| 126 |
return activate_plugin( $plugin_basename ); |
| 127 |
} |
| 128 |
|
| 129 |
// seems like the plugin doesn't exists. Download and activate it |
| 130 |
$upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() ); |
| 131 |
|
| 132 |
$api = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) ); |
| 133 |
$result = $upgrader->install( $api->download_link ); |
| 134 |
|
| 135 |
if ( is_wp_error( $result ) ) { |
| 136 |
return $result; |
| 137 |
} |
| 138 |
|
| 139 |
return activate_plugin( $plugin_basename ); |
| 140 |
} |
| 141 |
} |
| 142 |
endif; |