| 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 {product}? Become a contributor by opting in to our anonymous plugin 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( $this->logging_url, array( |
| 81 |
'method' => 'POST', |
| 82 |
'timeout' => 3, |
| 83 |
'redirection' => 5, |
| 84 |
'headers' => array( |
| 85 |
'X-ThemeIsle-Event' => 'log_site', |
| 86 |
), |
| 87 |
'body' => array( |
| 88 |
'site' => get_site_url(), |
| 89 |
'slug' => $this->product->get_slug(), |
| 90 |
'version' => $this->product->get_version(), |
| 91 |
'data' => apply_filters( $this->product->get_key() . '_logger_data', array() ), |
| 92 |
), |
| 93 |
) ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Dismiss the notification |
| 98 |
*/ |
| 99 |
function dismiss() { |
| 100 |
check_ajax_referer( (string) __CLASS__, 'nonce' ); |
| 101 |
|
| 102 |
$flag = intval( $_POST['enable'] ) === 1; |
| 103 |
update_option( $this->product->logger_option, ( $flag ? 'yes' : 'no' ) ); |
| 104 |
|
| 105 |
if ( true === $flag ) { |
| 106 |
$this->enable(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Shows the notification |
| 112 |
*/ |
| 113 |
function show_notification() { |
| 114 |
$show = $this->product->is_logger_active(); |
| 115 |
$checked = get_option( $this->product->logger_option, '' ); |
| 116 |
if ( ! $show && $checked == '' ) { |
| 117 |
add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 118 |
|
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Shows the admin notice |
| 127 |
*/ |
| 128 |
function admin_notices() { |
| 129 |
$id = $this->product->get_key() . '_logger'; |
| 130 |
|
| 131 |
$this->add_media( $this->product->get_key() ); |
| 132 |
|
| 133 |
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>'; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Generates the HTML |
| 138 |
* |
| 139 |
* @param string $key The product key. |
| 140 |
*/ |
| 141 |
function get_html( $key ) { |
| 142 |
$heading = apply_filters( $this->product->get_key() . '_logger_heading', $this->heading ); |
| 143 |
$heading = str_replace( array( '{product}' ), array( |
| 144 |
trim( str_replace( 'Lite', '', $this->product->get_name() ) ) |
| 145 |
), |
| 146 |
$heading |
| 147 |
); |
| 148 |
$button_submit = apply_filters( $this->product->get_key() . '_logger_button_submit', $this->button_submit ); |
| 149 |
$button_cancel = apply_filters( $this->product->get_key() . '_logger_button_cancel', $this->button_cancel ); |
| 150 |
|
| 151 |
return '<div >' |
| 152 |
. '<p>' . $heading . '</p>' |
| 153 |
. '<div class="actions">' |
| 154 |
. get_submit_button( __( $button_submit ), 'primary ' . $this->product->get_key() . '-ti-logger', $this->product->get_key() . 'ti-logger-yes', false, array( |
| 155 |
'data-ti-log-enable' => 1, |
| 156 |
) ) |
| 157 |
. get_submit_button( __( $button_cancel ), 'secondary ' . $this->product->get_key() . '-ti-logger', $this->product->get_key() . 'ti-logger-no', false, array( |
| 158 |
'data-ti-log-enable' => 0, |
| 159 |
) ) |
| 160 |
. '</div></div>'; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Loads the js |
| 165 |
* |
| 166 |
* @param string $key The product key. |
| 167 |
*/ |
| 168 |
function add_media( $key ) { |
| 169 |
?> |
| 170 |
<style type="text/css"> |
| 171 |
#<?php echo $key; ?>-logger-notification { |
| 172 |
padding-bottom: 5px; |
| 173 |
} |
| 174 |
|
| 175 |
#<?php echo $key; ?>-logger-notification .button { |
| 176 |
margin-left: 5px; |
| 177 |
} |
| 178 |
</style> |
| 179 |
<script type="text/javascript" id="<?php echo $key; ?>ti-logger-js"> |
| 180 |
(function ($) { |
| 181 |
$(document).ready(function () { |
| 182 |
$('.<?php echo $key?>-ti-logger').on('click', function (e) { |
| 183 |
|
| 184 |
$.ajax({ |
| 185 |
url: ajaxurl, |
| 186 |
method: "post", |
| 187 |
data: { |
| 188 |
'nonce': '<?php echo wp_create_nonce( (string) __CLASS__ );?>', |
| 189 |
'action': '<?php echo $this->product->get_key() . __CLASS__;?>', |
| 190 |
'enable': $(this).attr('data-ti-log-enable') |
| 191 |
}, |
| 192 |
success: function () { |
| 193 |
$('#<?php echo $key;?>-logger-notification').hide(); |
| 194 |
} |
| 195 |
}); |
| 196 |
}); |
| 197 |
}); |
| 198 |
})(jQuery); |
| 199 |
</script> |
| 200 |
<?php |
| 201 |
} |
| 202 |
|
| 203 |
} |
| 204 |
endif; |
| 205 |
|