Skip to content

Commit

Permalink
update to v1.4.12
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelRaiu committed Oct 2, 2024
1 parent 837c951 commit 357886d
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 345 deletions.
71 changes: 57 additions & 14 deletions admin/assets/js/chart-controller.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,72 @@
(function (_, $) {

_.loadCharts = function () {
var lineStyle = $('#chart-type').val();
var chartRanges = $('#chart-ranges').val();
var chartContent = $('#chart-content');
const initCharts = function (dates, searches) {
const lineStyle = $('#chart-type').val();
const ctx = document.getElementById('mwtsa-stats-chart').getContext('2d');
const stepSize = Math.ceil(Math.max(searches[0]) / 15);

new Chart(ctx, {
type: 'line',
data: {
labels: dates,
datasets: searches.map(function (dataSet, index) {
return {
label: index === 0 ? mwtsa_chart_obj.strings.currentPeriod : mwtsa_chart_obj.strings.previousPeriod,
data: Object.values(dataSet),
borderColor: index === 0 ? 'rgba(255,99,132,1)' : 'rgba(0,0,0,1)',
borderWidth: 1,
steppedLine: lineStyle === 'stepped'
};
}),
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function (value) {
if (Number.isInteger(value)) {
return value;
}
},
stepSize: stepSize
}
}]
},
elements: {
line: {
tension: 0
}
},
tooltips: {
mode: 'index'
}
}
});
};

var data = {
_.loadCharts = function () {
const data = {
'nonce': mwtsa_chart_obj.nonce,
'action': 'render_chart_data',
'line_style': lineStyle,
'chart_ranges': chartRanges
'chart_ranges': $('#chart-ranges').val()
};

$.post(mwtsa_chart_obj.ajax_url, data, function (response) {
chartContent.html(response);
if (!response.success) {
return;
}

initCharts(response.data.dates, response.data.searches);
});
};

_.saveAsDefault = function () {
var lineStyle = $('#chart-type').val();
var chartRanges = $('#chart-ranges').val();

var data = {
const data = {
'nonce': mwtsa_chart_obj.nonce,
'action': 'save_default_chart_settings',
'line_style': lineStyle,
'chart_ranges': chartRanges
'line_style': $('#chart-type').val(),
'chart_ranges': $('#chart-ranges').val()
};

$.post(mwtsa_chart_obj.ajax_url, data, function (response) {
Expand Down
114 changes: 41 additions & 73 deletions admin/includes/class.charts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

class MWTSA_Admin_Charts {

private $nonce_action = 'mwtsa_chart_nonce';

public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_assets' ) );

Expand All @@ -20,7 +22,12 @@ public function load_admin_assets( $hook ) {
wp_enqueue_script( 'mwtsa-chart-controller-script', MWTSAI()->plugin_admin_url . 'assets/js/chart-controller.js', array( 'mwtsa-chart-bundle-script' ) );

wp_localize_script( 'mwtsa-chart-controller-script', 'mwtsa_chart_obj', array(
'ajax_url' => admin_url( 'admin-ajax.php' )
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( $this->nonce_action ),
'strings' => array(
'currentPeriod' => __( 'Current Period', 'search-analytics' ),
'previousPeriod' => __( 'Previous Period', 'search-analytics' ),
)
)
);
}
Expand All @@ -46,40 +53,40 @@ public function render_stats_chart() {
?>
<div class="col-content">

<h2><?php _e( "Search Results Charts", 'search-analytics' ) ?></h2>
<h2><?php esc_html_e( "Search Results Charts", 'search-analytics' ) ?></h2>
<div class="mwtsa-chart-options">
<label for="chart-type">
<select id="chart-type" onchange="loadCharts()">
<?php foreach ( $line_options as $value => $label ) : ?>
<option value="<?php echo $value ?>" <?php selected( $value, $default_line_style ) ?>><?php echo $label ?></option>
<option value="<?php echo $value ?>" <?php selected( $value, $default_line_style ) ?>><?php echo esc_html( $label ) ?></option>
<?php endforeach; ?>
</select>
</label>
<label for="chart-ranges">
<select id="chart-ranges" onchange="loadCharts()">
<?php foreach ( $range_options as $value => $label ) : ?>
<option value="<?php echo $value ?>" <?php selected( $value, $default_range ) ?>><?php echo $label ?></option>
<option value="<?php echo $value ?>" <?php selected( $value, $default_range ) ?>><?php echo esc_html( $label ) ?></option>
<?php endforeach; ?>
</select>
</label>
<span onclick="saveAsDefault()" class="button"><?php _e( 'Save as default', 'search-analytics' ) ?></span>
<span onclick="saveAsDefault()" class="button"><?php esc_html_e( 'Save as default', 'search-analytics' ) ?></span>
</div>

<div id="chart-content"></div>
<div id="chart-content">
<canvas id="mwtsa-stats-chart" width="400" height="100"></canvas>
</div>
</div>
<?php endif;
}

public function render_chart_data() {


if ( empty( $_REQUEST['line_style'] ) || empty( $_REQUEST['chart_ranges'] ) ) {
echo 'Bad Request!';
if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], $this->nonce_action ) ) {
wp_send_json_error( 'Bad Request!' );
}

$stepped_line = ( $_REQUEST['line_style'] == 'stepped' ) ? 'true' : 'false';
$ranges = ! empty( $_REQUEST['chart_ranges'] ) ? $_REQUEST['chart_ranges'] : '2w';

switch ( $_REQUEST['chart_ranges'] ) {
switch ( $ranges ) {
case '1m':
$args = array(
'since' => 1,
Expand Down Expand Up @@ -112,75 +119,36 @@ public function render_chart_data() {

}

list( $dates, $history ) = ( new MWTSA_History_Data )->get_daily_search_count_for_period_chart( $args );

?>

<canvas id="mwtsa-stats-chart" width="400" height="100"></canvas>
<script>
const ctx = document.getElementById("mwtsa-stats-chart").getContext("2d");
const stepSize = parseInt( <?php echo ceil( max( $history[0] ) / 15 ) ?> );
const stackedLine = new Chart(ctx, {
type: 'line',
data: {
labels: ["<?php echo implode( '", "', $dates ) ?>"],
datasets: [
<?php foreach ( $history as $k => $set ) :
$set = implode( '", "', $set );
?>
{
label: "<?php echo $k == 1 ? __( 'Previous Period', 'search-analytics' ) : __( 'Current Period', 'search-analytics' ) ?>",
data: ["<?php echo $set ?>"],
borderColor: [
"<?php echo ( $k == 1 ) ? 'rgba(0,0,0,1)' : 'rgba(255,99,132,1)' ?>"
],
borderWidth: 1,
steppedLine: <?php echo $stepped_line ?>
},
<?php endforeach; ?>
]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function (value) {
if (Number.isInteger(value)) {
return value;
}
},
stepSize: stepSize
}
}]
},
elements: {
line: {
tension: 0
}
},
tooltips: {
mode: 'index'
}
}
});
</script>
<?php

exit();
wp_send_json_success( ( new MWTSA_History_Data )->get_daily_search_count_for_period_chart( $args ) );
}

public function save_default_chart_settings() {
if ( empty( $_REQUEST['line_style'] ) || empty( $_REQUEST['chart_ranges'] ) ) {
echo 'Bad Request!';

if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], $this->nonce_action ) ) {
wp_send_json_error( 'Bad Request!' );
}

if ( empty( $_POST['line_style'] ) || empty( $_POST['chart_ranges'] ) ) {
wp_send_json_error( 'Bad Request!' );
}

$chart_ranges = $_POST['chart_ranges'];
$line_style = $_POST['line_style'];

if ( ! in_array( $chart_ranges, array( '1m', '1mc', '2w', '2wc' ) ) ) {
wp_send_json_error( 'Bad Request!' );
}

if ( ! in_array( $line_style, array( 'basic', 'stepped' ) ) ) {
wp_send_json_error( 'Bad Request!' );
}

MWTSA_Options::set_options( array(
'chart_default_line_style' => sanitize_text_field( $_REQUEST['line_style'] ),
'chart_default_range' => sanitize_text_field( $_REQUEST['chart_ranges'] )
'chart_default_range' => $chart_ranges,
'chart_default_line_style' => $line_style
) );

exit();
wp_send_json_success();
}
}
}
12 changes: 6 additions & 6 deletions admin/includes/class.dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function last_week_stats_widget() {
) );

if ( empty( $search_results ) ) {
_e( 'No search statistics found', 'search-analytics' );
esc_html_e( 'No search statistics found', 'search-analytics' );

return;
}
Expand All @@ -47,19 +47,19 @@ public function last_week_stats_widget() {

echo '<ul class="mwtsa-stats-widget-wrapper">';

echo '<li><span class="stats-list-label">' . __( "Total Searches:", 'search-analytics' ) . '</span>';
echo '<li><span class="stats-list-label">' . esc_html__( "Total Searches:", 'search-analytics' ) . '</span>';
echo '<span class="stats-list-value">' . absint( $total_searches ) . '</span></li>';

echo '<li><span class="stats-list-label">' . __( "Most Searched Term:", 'search-analytics' ) . '</span>';
echo '<li><span class="stats-list-label">' . esc_html__( "Most Searched Term:", 'search-analytics' ) . '</span>';
echo '<span class="stats-list-value">' . esc_attr( $most_searched_term['term'] ) . '</span></li>';

echo '<li><span class="stats-list-label">' . __( "Most Searched Term Count:", 'search-analytics' ) . '</span>';
echo '<li><span class="stats-list-label">' . esc_html__( "Most Searched Term Count:", 'search-analytics' ) . '</span>';
echo '<span class="stats-list-value">' . absint( $most_searched_term['count'] ) . '</span></li>';

echo '<li><span class="stats-list-label">' . __( "Last Searched Term:", 'search-analytics' ) . '</span>';
echo '<li><span class="stats-list-label">' . esc_html__( "Last Searched Term:", 'search-analytics' ) . '</span>';
echo '<span class="stats-list-value">' . esc_attr( $last_search_term['term'] ) . '</span></li>';

echo '<li><span class="stats-list-label">' . __( "Last Searched Date:", 'search-analytics' ) . '</span>';
echo '<li><span class="stats-list-label">' . esc_html__( "Last Searched Date:", 'search-analytics' ) . '</span>';
echo '<span class="stats-list-value">' . esc_attr( $last_search_term['last_search_date'] ) . '</span></li>';

echo '</ul>';
Expand Down
34 changes: 26 additions & 8 deletions admin/includes/class.settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ public function add_admin_menu() {

public function mwtsa_settings_init() {

register_setting( 'mwtsa_general_options', MWTSAI()->main_option_name );
register_setting( 'mwtsa_general_options', MWTSAI()->main_option_name, array(
'type' => 'string',
'sanitize_callback' => function ( $values ) {

return array_map( function ( $value ) {
if ( is_array( $value ) ) {
return array_map( 'sanitize_text_field', $value );
}

return sanitize_text_field( $value );
}, $values );
},
) );

add_settings_section(
'mwtsa_general_options_section',
Expand Down Expand Up @@ -206,7 +218,8 @@ public function field_exclude_doubled_search_for_interval_render() {
}

?>
<input type="number" min="0" name="mwtsa_settings[mwtsa_exclude_doubled_search_for_interval]" value="<?php echo $this->existing_options['mwtsa_exclude_doubled_search_for_interval'] ?>"/>
<input type="number" min="0" name="mwtsa_settings[mwtsa_exclude_doubled_search_for_interval]"
value="<?php echo (int) $this->existing_options['mwtsa_exclude_doubled_search_for_interval'] ?>"/>
<span><?php _e( '( Note: set to 0 or leave empty to disable it )', 'search-analytics' ) ?></span>
<br/>
<strong><?php _e( 'Note: this will set a cookie in the browser of the user who made any kind of search on the website.<br />This needs to be treated by the site\'s GDPR terms in case it\'s value is a number larger than 0<br />The cookie name is: ', 'search-analytics' ) ?><i><?php echo $mwtsa->cookie_name ?></i></strong>
Expand All @@ -220,7 +233,9 @@ public function field_exclude_searches_from_ip_addresses_render() {

$admin_ip = isset( $_SERVER['HTTP_CLIENT_IP'] ) ? $_SERVER['HTTP_CLIENT_IP'] : ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'] );
?>
<input type="text" name="mwtsa_settings[mwtsa_exclude_searches_from_ip_addresses]" value="<?php echo $this->existing_options['mwtsa_exclude_searches_from_ip_addresses'] ?>" placeholder="eg. 127.0.0.1"/> <span><?php _e( '( Note: separate IP values by comma )', 'search-analytics' ) ?></span>
<input type="text" name="mwtsa_settings[mwtsa_exclude_searches_from_ip_addresses]"
value="<?php esc_attr_e( $this->existing_options['mwtsa_exclude_searches_from_ip_addresses'] ) ?>" placeholder="eg. 127.0.0.1"/>
<span><?php _e( '( Note: separate IP values by comma )', 'search-analytics' ) ?></span>
<br/>

<strong><?php _e( 'Your IP address is: ' . $admin_ip, 'search-analytics' ) ?></strong>
Expand All @@ -233,7 +248,8 @@ public function field_minimum_characters_render() {
$this->existing_options['mwtsa_minimum_characters'] = 0;
}
?>
<input type="number" min="0" name="mwtsa_settings[mwtsa_minimum_characters]" value="<?php echo $this->existing_options['mwtsa_minimum_characters'] ?>"/>
<input type="number" min="0" name="mwtsa_settings[mwtsa_minimum_characters]"
value="<?php echo (int) $this->existing_options['mwtsa_minimum_characters'] ?>"/>
<span><?php _e( '( Note: set to 0 or leave empty to disable it )', 'search-analytics' ) ?></span>
<br/>
<?php
Expand All @@ -246,7 +262,8 @@ public function field_exclude_if_string_contains_render() {
}

?>
<input type="text" name="mwtsa_settings[mwtsa_exclude_if_string_contains]" value="<?php echo $this->existing_options['mwtsa_exclude_if_string_contains']; ?>" placeholder="eg. text, another one"/>
<input type="text" name="mwtsa_settings[mwtsa_exclude_if_string_contains]"
value="<?php esc_attr_e( $this->existing_options['mwtsa_exclude_if_string_contains'] ); ?>" placeholder="eg. text, another one"/>
<span><?php _e( '( Note: enter comma (,) separated strings )', 'search-analytics' ) ?></span>
<?php
}
Expand All @@ -258,7 +275,8 @@ public function field_custom_search_url_params_render() {
}

?>
<input type="text" name="mwtsa_settings[mwtsa_custom_search_url_params]" value="<?php echo $this->existing_options['mwtsa_custom_search_url_params']; ?>" placeholder="eg. wpv_post_search"/>
<input type="text" name="mwtsa_settings[mwtsa_custom_search_url_params]"
value="<?php esc_attr_e( $this->existing_options['mwtsa_custom_search_url_params'] ); ?>" placeholder="eg. wpv_post_search"/>
<span><?php _e( '( Note: enter comma (,) separated strings )', 'search-analytics' ) ?></span>
<?php
}
Expand Down Expand Up @@ -411,7 +429,7 @@ function erase_history_form() {
<table class="form-table erase-history-table">
<tbody>
<tr>
<th scope="row">Delete all data</th>
<th scope="row"><?php _e( 'Delete all data', 'search-analytics' ) ?></th>
<td>
<form action="" method="post">
<?php wp_nonce_field( 'mwtsa-erase-data' ); ?>
Expand All @@ -423,7 +441,7 @@ function erase_history_form() {
</td>
</tr>
<tr>
<th scope="row">Delete data older than</th>
<th scope="row"><?php _e( 'Delete data older than', 'search-analytics' ) ?></th>
<td>
<form action="" method="post">
<?php wp_nonce_field( 'mwtsa-erase-data' ); ?>
Expand Down
Loading

0 comments on commit 357886d

Please sign in to comment.