| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName,Squiz.Commenting.FileComment.Missing -- File naming is acceptable and doc comment exists |
| 2 |
|
| 3 |
/** |
| 4 |
* Class to Show Analytify Dashboard Addon Notice |
| 5 |
* |
| 6 |
* @since 2.1.23 |
| 7 |
*/ |
| 8 |
class Analytify_Dashboard_Addon_Install { |
| 9 |
|
| 10 |
/** |
| 11 |
* Whether the addon is already installed. |
| 12 |
* |
| 13 |
* @var bool |
| 14 |
*/ |
| 15 |
private $is_already_installed = false; |
| 16 |
/** |
| 17 |
* Constructor. |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
|
| 23 |
add_action( 'wp_dashboard_setup', array( $this, 'add_analytify_widget' ) ); |
| 24 |
add_action( 'wp_ajax_activate-analytify-dashboard-free', array( $this, 'activate_free' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Register Widget. |
| 29 |
* |
| 30 |
* @since 2.1.23 |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public function add_analytify_widget() { |
| 34 |
|
| 35 |
$allowed_roles = $GLOBALS['WP_ANALYTIFY']->settings->get_option( 'show_analytics_roles_dashboard', 'wp-analytify-dashboard', array( 'administrator' ) ); |
| 36 |
// if dont have Analytify Dashboard access, Return. |
| 37 |
if ( ! $GLOBALS['WP_ANALYTIFY']->pa_check_roles( $allowed_roles ) ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$plugin_file = 'analytify-analytics-dashboard-widget/wp-analytify-dashboard.php'; |
| 42 |
|
| 43 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 44 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 45 |
} |
| 46 |
|
| 47 |
$this->is_already_installed = (bool) file_exists( WP_PLUGIN_DIR . '/' . $plugin_file ); |
| 48 |
|
| 49 |
// Only hide the CTA when the dashboard widget addon is installed and active. |
| 50 |
if ( $this->is_already_installed && is_plugin_active( $plugin_file ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
wp_add_dashboard_widget( 'analytify-dashboard-addon', __( 'Google Analytics Dashboard By Analytify', 'wp-analytify' ), array( $this, 'wpa_general_dashboard_area' ), null, null ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Create Widget Container. |
| 59 |
* |
| 60 |
* @since 2.1.23 |
| 61 |
* @param mixed $var Unused parameter. |
| 62 |
* @param mixed $dashboard_id Unused parameter. |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function wpa_general_dashboard_area( $var, $dashboard_id ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed,Universal.NamingConventions.NoReservedKeywordParameterNames.varFound -- Parameters required by WordPress hook |
| 66 |
|
| 67 |
$activate_url = wp_nonce_url( |
| 68 |
add_query_arg( |
| 69 |
array( |
| 70 |
'action' => 'activate', |
| 71 |
'plugin' => 'analytify-analytics-dashboard-widget/wp-analytify-dashboard.php', |
| 72 |
), |
| 73 |
admin_url( 'plugins.php' ) |
| 74 |
), |
| 75 |
'activate-plugin_analytify-analytics-dashboard-widget/wp-analytify-dashboard.php' |
| 76 |
); |
| 77 |
?> |
| 78 |
<div class="inside"> |
| 79 |
|
| 80 |
<div class="install-analytify-dashboard-widget"> |
| 81 |
<div class="install-analytify-dashboard-widget-content"> |
| 82 |
<h2><?php esc_html_e( 'View All Analytics on the WordPress Dashboard', 'wp-analytify' ); ?></h2> |
| 83 |
<p><?php esc_html_e( 'Once you install Analytify Dashboard Widget Addon, this dashboard widget will be filled with Analytics.', 'wp-analytify' ); ?></p> |
| 84 |
|
| 85 |
<?php if ( $this->is_already_installed ) : ?> |
| 86 |
<a href="<?php echo esc_url( $activate_url ); ?>" class="button button-primary button-hero activate-analytify-dashboard-free"><?php esc_html_e( 'Activate Dashboard Add-on', 'wp-analytify' ); ?></a> |
| 87 |
<img src="<?php echo esc_url( admin_url( 'images/spinner.gif' ) ); ?> " style=" display: none; margin: 0 auto; padding-top: 20px;" class='install-analytify-dashboard-widget-loader'> |
| 88 |
<?php else : ?> |
| 89 |
<a href="" target="_blank" class="button button-primary button-hero install-analytify-dashboard-free" data-nonce="<?php echo esc_attr( wp_create_nonce( 'updates' ) ); ?>"><?php esc_html_e( 'Install & Activate Dashboard Add-on Free', 'wp-analytify' ); ?></a> |
| 90 |
<a href="<?php echo esc_url( $activate_url ); ?>" class="button button-primary button-hero activate-analytify-dashboard-free" style="display: none"><?php esc_html_e( 'Activate Dashboard Add-on', 'wp-analytify' ); ?></a> |
| 91 |
<img src="<?php echo esc_url( admin_url( 'images/spinner.gif' ) ); ?> " style=" display: none; margin: 0 auto; padding-top: 20px;" class='install-analytify-dashboard-widget-loader'> |
| 92 |
<?php endif; ?> |
| 93 |
|
| 94 |
</div> |
| 95 |
</div> |
| 96 |
</div> |
| 97 |
|
| 98 |
<script type="text/javascript"> |
| 99 |
|
| 100 |
(function($, window, document) { |
| 101 |
var activateNonce = '<?php echo esc_js( wp_create_nonce( 'activate-analytify-dashboard' ) ); ?>'; |
| 102 |
|
| 103 |
function activateDashboardWidget(button) { |
| 104 |
return $.ajax({ |
| 105 |
url: ajaxurl, |
| 106 |
type: 'POST', |
| 107 |
data: { |
| 108 |
action: 'activate-analytify-dashboard-free', |
| 109 |
security: activateNonce |
| 110 |
}, |
| 111 |
beforeSend: function() { |
| 112 |
$('.activate-analytify-dashboard-free').attr('disabled', 'disabled'); |
| 113 |
$('.install-analytify-dashboard-widget-loader').css('display', 'block'); |
| 114 |
} |
| 115 |
}); |
| 116 |
} |
| 117 |
|
| 118 |
$('.install-analytify-dashboard-free').on('click', function(event) { |
| 119 |
event.preventDefault(); |
| 120 |
var nonce = $(this).data('nonce'); |
| 121 |
$.ajax({ |
| 122 |
url: ajaxurl, |
| 123 |
type: 'POST', |
| 124 |
data: { |
| 125 |
slug: 'analytify-analytics-dashboard-widget', |
| 126 |
action: 'install-plugin', |
| 127 |
_ajax_nonce: nonce |
| 128 |
}, |
| 129 |
beforeSend: function() { |
| 130 |
$('.install-analytify-dashboard-free').attr('disabled', 'disabled'); |
| 131 |
$('.install-analytify-dashboard-widget-loader').css('display', 'block'); |
| 132 |
} |
| 133 |
}) |
| 134 |
.done(function(response) { |
| 135 |
if ( response && false === response.success ) { |
| 136 |
$('.install-analytify-dashboard-widget-loader').css('display', 'none'); |
| 137 |
$('.install-analytify-dashboard-free').removeAttr('disabled'); |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
activateDashboardWidget().always(function() { |
| 142 |
location.reload(); |
| 143 |
}); |
| 144 |
}); |
| 145 |
}); |
| 146 |
|
| 147 |
// Activate plugin |
| 148 |
$(document).on('click', '.activate-analytify-dashboard-free', function(event) { |
| 149 |
event.preventDefault(); |
| 150 |
var button = $(this); |
| 151 |
activateDashboardWidget(button).done(function(response) { |
| 152 |
if ( response && response.success ) { |
| 153 |
location.reload(); |
| 154 |
} |
| 155 |
}).fail(function() { |
| 156 |
button.removeAttr('disabled'); |
| 157 |
button.siblings('.install-analytify-dashboard-widget-loader').css('display', 'none'); |
| 158 |
}); |
| 159 |
}); |
| 160 |
}(window.jQuery, window, document)); |
| 161 |
</script> |
| 162 |
<?php |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Activate Dashboard Widget. |
| 167 |
* |
| 168 |
* @version 9.1.0 |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public function activate_free() { |
| 172 |
// Ensure the user has the capability to manage plugins. |
| 173 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 174 |
wp_send_json_error( |
| 175 |
array( 'message' => __( 'You do not have permission to activate plugins.', 'wp-analytify' ) ), |
| 176 |
403 |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
check_ajax_referer( 'activate-analytify-dashboard', 'security' ); |
| 181 |
|
| 182 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 183 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 184 |
} |
| 185 |
|
| 186 |
$plugin = 'analytify-analytics-dashboard-widget/wp-analytify-dashboard.php'; |
| 187 |
|
| 188 |
if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) { |
| 189 |
wp_send_json_error( |
| 190 |
array( 'message' => __( 'Dashboard widget add-on is not installed.', 'wp-analytify' ) ) |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
if ( ! is_plugin_active( $plugin ) ) { |
| 195 |
$result = activate_plugin( $plugin ); |
| 196 |
|
| 197 |
if ( is_wp_error( $result ) ) { |
| 198 |
wp_send_json_error( |
| 199 |
array( 'message' => $result->get_error_message() ) |
| 200 |
); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
wp_send_json_success( __( 'Plugin activated successfully.', 'wp-analytify' ) ); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
new Analytify_Dashboard_Addon_Install(); |
| 209 |
|