| 1 |
<?php |
| 2 |
/** |
| 3 |
* The main loader class for ThemeIsle SDK |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Logger |
| 7 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
if ( ! class_exists( 'ThemeIsle_SDK_Logger' ) ) : |
| 12 |
/** |
| 13 |
* Class ThemeIsle_SDK_Logger |
| 14 |
* |
| 15 |
* Send the statistics to the Themeisle Endpoint |
| 16 |
*/ |
| 17 |
/** |
| 18 |
* Class ThemeIsle_SDK_Logger |
| 19 |
*/ |
| 20 |
class ThemeIsle_SDK_Logger { |
| 21 |
|
| 22 |
/** |
| 23 |
* @var string $logging_url Url where to send the logs |
| 24 |
*/ |
| 25 |
private $logging_url = 'http://log.themeisle.com/wp-json/v1/logs/'; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var ThemeIsle_SDK_Product $product Themeisle Product. |
| 29 |
*/ |
| 30 |
private $product; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var string $product_cron Cron name handler |
| 34 |
*/ |
| 35 |
private $product_cron; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var string $heading The heading of the modal |
| 39 |
*/ |
| 40 |
private $heading = 'Do you enjoy <b>{product}</b>? Become a contributor by opting in to our anonymous data tracking. We guarantee no sensitive data is collected.'; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var string $button_submit The text of the submit button |
| 44 |
*/ |
| 45 |
private $button_submit = 'Sure, I would love to help.'; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var string $button_cancel The text of the cancel button |
| 49 |
*/ |
| 50 |
private $button_cancel = 'No, thanks.'; |
| 51 |
|
| 52 |
/** |
| 53 |
* ThemeIsle_SDK_Logger constructor. |
| 54 |
* |
| 55 |
* @param ThemeIsle_SDK_Product $product_object Product Object. |
| 56 |
*/ |
| 57 |
public function __construct( $product_object ) { |
| 58 |
if ( $product_object instanceof ThemeIsle_SDK_Product ) { |
| 59 |
$this->product = $product_object; |
| 60 |
$this->product_cron = $product_object->get_key() . '_log_activity'; |
| 61 |
} |
| 62 |
add_action( 'wp_ajax_' . $this->product->get_key() . __CLASS__, array( $this, 'dismiss' ) ); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Start the cron to send the log. It will randomize the interval in order to not send all the logs at the same time. |
| 68 |
*/ |
| 69 |
public function enable() { |
| 70 |
if ( ! wp_next_scheduled( $this->product_cron ) ) { |
| 71 |
wp_schedule_single_event( time() + ( rand( 15, 24 ) * 3600 ), $this->product_cron ); |
| 72 |
} |
| 73 |
add_action( $this->product_cron, array( $this, 'send_log' ) ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Send the statistics to the api endpoint |
| 78 |
*/ |
| 79 |
public function send_log() { |
| 80 |
wp_remote_post( |
| 81 |
$this->logging_url, array( |
| 82 |
'method' => 'POST', |
| 83 |
'timeout' => 3, |
| 84 |
'redirection' => 5, |
| 85 |
'headers' => array( |
| 86 |
'X-ThemeIsle-Event' => 'log_site', |
| 87 |
), |
| 88 |
'body' => array( |
| 89 |
'site' => get_site_url(), |
| 90 |
'slug' => $this->product->get_slug(), |
| 91 |
'version' => $this->product->get_version(), |
| 92 |
'data' => apply_filters( $this->product->get_key() . '_logger_data', array() ), |
| 93 |
), |
| 94 |
) |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Dismiss the notification |
| 100 |
*/ |
| 101 |
function dismiss() { |
| 102 |
check_ajax_referer( (string) __CLASS__, 'nonce' ); |
| 103 |
|
| 104 |
$flag = intval( $_POST['enable'] ) === 1; |
| 105 |
update_option( $this->product->logger_option, ( $flag ? 'yes' : 'no' ) ); |
| 106 |
|
| 107 |
if ( true === $flag ) { |
| 108 |
$this->enable(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Either we should show the notification or not. |
| 114 |
* |
| 115 |
* @return bool Valida notification. |
| 116 |
*/ |
| 117 |
function can_notify() { |
| 118 |
$show = $this->product->is_logger_active(); |
| 119 |
$checked = get_option( $this->product->logger_option, '' ); |
| 120 |
if ( ! $show && $checked == '' ) { |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Shows the notification |
| 129 |
*/ |
| 130 |
function show_notification() { |
| 131 |
add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Shows the admin notice |
| 136 |
*/ |
| 137 |
function admin_notices() { |
| 138 |
$id = $this->product->get_key() . '_logger'; |
| 139 |
|
| 140 |
$this->add_media( $this->product->get_key() ); |
| 141 |
|
| 142 |
echo '<div class="notice notice-success is-dismissible " id="' . $this->product->get_key() . '-logger-notification" ><div id="' . $id . '" class="themeisle-logger-box">' . $this->get_html( $this->product->get_key() ) . '</div></div>'; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Generates the HTML |
| 147 |
* |
| 148 |
* @param string $key The product key. |
| 149 |
*/ |
| 150 |
function get_html( $key ) { |
| 151 |
$heading = apply_filters( $this->product->get_key() . '_logger_heading', $this->heading ); |
| 152 |
$heading = str_replace( |
| 153 |
array( '{product}' ), array( |
| 154 |
trim( str_replace( 'Lite', '', $this->product->get_name() ) ), |
| 155 |
), |
| 156 |
$heading |
| 157 |
); |
| 158 |
$button_submit = apply_filters( $this->product->get_key() . '_logger_button_submit', $this->button_submit ); |
| 159 |
$button_cancel = apply_filters( $this->product->get_key() . '_logger_button_cancel', $this->button_cancel ); |
| 160 |
|
| 161 |
return '<div >' |
| 162 |
. '<p>' . $heading . '</p>' |
| 163 |
. '<div class="actions">' |
| 164 |
. get_submit_button( |
| 165 |
$button_submit, 'primary ' . $this->product->get_key() . '-ti-logger', $this->product->get_key() . 'ti-logger-yes', false, array( |
| 166 |
'data-ti-log-enable' => 1, |
| 167 |
) |
| 168 |
) |
| 169 |
. get_submit_button( |
| 170 |
$button_cancel, 'secondary ' . $this->product->get_key() . '-ti-logger', $this->product->get_key() . 'ti-logger-no', false, array( |
| 171 |
'data-ti-log-enable' => 0, |
| 172 |
) |
| 173 |
) |
| 174 |
. '</div></div>'; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Loads the js |
| 179 |
* |
| 180 |
* @param string $key The product key. |
| 181 |
*/ |
| 182 |
function add_media( $key ) { |
| 183 |
?> |
| 184 |
<style type="text/css"> |
| 185 |
#<?php echo $key; ?>-logger-notification { |
| 186 |
padding-bottom: 5px; |
| 187 |
} |
| 188 |
|
| 189 |
#<?php echo $key; ?>-logger-notification .button { |
| 190 |
margin-left: 5px; |
| 191 |
} |
| 192 |
</style> |
| 193 |
<script type="text/javascript" id="<?php echo $key; ?>ti-logger-js"> |
| 194 |
(function ($) { |
| 195 |
$(document).ready(function () { |
| 196 |
$('.<?php echo $key; ?>-ti-logger').on('click', function (e) { |
| 197 |
|
| 198 |
$.ajax({ |
| 199 |
url: ajaxurl, |
| 200 |
method: "post", |
| 201 |
data: { |
| 202 |
'nonce': '<?php echo wp_create_nonce( (string) __CLASS__ ); ?>', |
| 203 |
'action': '<?php echo $this->product->get_key() . __CLASS__; ?>', |
| 204 |
'enable': $(this).attr('data-ti-log-enable') |
| 205 |
}, |
| 206 |
success: function () { |
| 207 |
$('#<?php echo $key; ?>-logger-notification').hide(); |
| 208 |
} |
| 209 |
}); |
| 210 |
}); |
| 211 |
}); |
| 212 |
})(jQuery); |
| 213 |
</script> |
| 214 |
<?php |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
endif; |
| 219 |
|