Skip to content

Commit

Permalink
4.4.14
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelbart committed Oct 13, 2020
1 parent 847a370 commit 62fc613
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 22 deletions.
1 change: 0 additions & 1 deletion core/assets/js/admin-feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@

request.done(function (response) {
if ("success" === response.status) {
response = response.data;
window.location.href = response.file;
}
});
Expand Down
2 changes: 1 addition & 1 deletion core/helpers/class-database.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Database
public static function table_exists( $table_name )
{
global $wpdb;

if ( $table_name != $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) ) {
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions core/helpers/class-feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ public static function insert_feedback()

$table_name = $wpdb->prefix . 'helpful_feedback';
$wpdb->insert( $table_name, $data );

Stats::delete_widget_transient();

return $wpdb->insert_id;
}

Expand Down
57 changes: 57 additions & 0 deletions core/helpers/class-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -1347,4 +1347,61 @@ public static function get_single_post_stats( $post_id )

return $results;
}

/**
* Returns everything at once and saves the result in a transient to reduce the number of queries for the widget.
*
* @return array
*/
public static function get_widget_stats()
{
$cache_name = 'helpful_widget_stats';
$cache_time = get_option( 'helpful_cache_time', 'minute' );
$cache_active = get_option( 'helpful_caching', 'off' );
$cache_times = Cache::get_cache_times( false );
$cache_time = $cache_times[ $cache_time ];
$results = get_transient( $cache_name );

if ( 'on' !== $cache_active ) {
$results = [
'most_helpful' => get_option( 'helpful_widget_pro' ) ? self::get_most_helpful() : null,
'least_helpful' => get_option( 'helpful_widget_contra' ) ? self::get_least_helpful() : null,
'recently_pro' => get_option( 'helpful_widget_pro_recent' ) ? self::get_recently_pro() : null,
'recently_contra' => get_option( 'helpful_widget_contra_recent' ) ? self::get_recently_contra() : null,
'feedback_items' => get_option( 'helpful_feedback_widget' ) ? Feedback::get_feedback_items() : null,
'pro_total' => intval( self::get_pro_all() ),
'contra_total' => intval( self::get_contra_all() ),
];

return $results;
}

if ( false === $results ) {
$results = [
'most_helpful' => get_option( 'helpful_widget_pro' ) ? self::get_most_helpful() : null,
'least_helpful' => get_option( 'helpful_widget_contra' ) ? self::get_least_helpful() : null,
'recently_pro' => get_option( 'helpful_widget_pro_recent' ) ? self::get_recently_pro() : null,
'recently_contra' => get_option( 'helpful_widget_contra_recent' ) ? self::get_recently_contra() : null,
'feedback_items' => get_option( 'helpful_feedback_widget' ) ? Feedback::get_feedback_items() : null,
'pro_total' => intval( self::get_pro_all() ),
'contra_total' => intval( self::get_contra_all() ),
];

set_transient( $cache_name, maybe_serialize( $results ), $cache_time );
}

$results = maybe_unserialize( $results );

return $results;
}

/**
* Removes the transient for the widget so that current data can be transferred.
*
* @return void
*/
public static function delete_widget_transient()
{
delete_transient( 'helpful_widget_stats' );
}
}
12 changes: 10 additions & 2 deletions core/helpers/class-values.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ public static function get_tags()
*/
public static function insert_pro( $user, $post_id )
{
return Votes::insert_vote( $user, $post_id, 'pro' );
$status = Votes::insert_vote( $user, $post_id, 'pro' );

Stats::delete_widget_transient();

return $status;
}

/**
Expand All @@ -160,7 +164,11 @@ public static function insert_pro( $user, $post_id )
*/
public static function insert_contra( $user, $post_id )
{
return Votes::insert_vote( $user, $post_id, 'contra' );
$status = Votes::insert_vote( $user, $post_id, 'contra' );

Stats::delete_widget_transient();

return $status;
}

/**
Expand Down
10 changes: 8 additions & 2 deletions core/modules/class-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ public function setup_defaults()
*/
public function setup_helpful_table()
{
return Helpers\Database::setup_helpful_table();
if ( false === get_transient( 'setup_helpful_table' ) ) {
Helpers\Database::setup_helpful_table();
set_transient( 'setup_helpful_table', 1, WEEK_IN_SECONDS );
}
}

/**
Expand All @@ -110,7 +113,10 @@ public function setup_helpful_table()
*/
public function setup_feedback_table()
{
return Helpers\Database::setup_feedback_table();
if ( false === get_transient( 'setup_feedback_table' ) ) {
Helpers\Database::setup_feedback_table();
set_transient( 'setup_feedback_table', 1, WEEK_IN_SECONDS );
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/modules/class-feedback-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ public function ajax_export_feedback()
$items[] = [
'post' => get_the_title( $row->post_id ),
'permalink' => get_the_permalink( $row->post_id ),
'name' => $fields['name'],
'email' => $fields['email'],
'name' => isset( $fields['name'] ) ? $fields['name'] : '',
'email' => isset( $fields['email'] ) ? $fields['email'] : '',
'message' => $row->message,
'pro' => $row->pro,
'contra' => $row->contra,
Expand Down
2 changes: 2 additions & 0 deletions core/modules/class-maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,7 @@ public function maintenance_after_update()
}

$response = Helpers\Optimize::optimize_plugin();

update_option( 'helpful_plugin_version', $plugin['Version'] );
}
}
2 changes: 1 addition & 1 deletion helpful.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Helpful
* Description: Add a fancy feedback form under your posts or post-types and ask your visitors a question. Give them the abbility to vote with yes or no.
* Version: 4.4.13
* Version: 4.4.14
* Author: Pixelbart
* Author URI: https://pixelbart.de
* Text Domain: helpful
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: helpful, poll, feedback, reviews, vote, review, voting
Requires at least: 4.6
Tested up to: 5.6
Requires PHP: 5.6.20
Stable tag: 4.4.13
Stable tag: 4.4.14
License: MIT License
License URI: https://opensource.org/licenses/MIT

Expand Down
26 changes: 14 additions & 12 deletions templates/admin-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

$widget_stats = Helpers\Stats::get_widget_stats();
?>

<form class="helpful-widget-form">
Expand Down Expand Up @@ -55,7 +57,7 @@

<div class="helpful-widget-panels">

<?php if ( ! empty( Helpers\Stats::get_most_helpful() ) && get_option( 'helpful_widget_pro' ) ) : ?>
<?php if ( isset( $widget_stats['most_helpful'] ) ) : ?>
<div class="helpful-widget-panel">

<button type="button">
Expand All @@ -64,7 +66,7 @@
</button>

<ul>
<?php foreach ( Helpers\Stats::get_most_helpful() as $post ) : ?>
<?php foreach ( $widget_stats['most_helpful'] as $post ) : ?>
<li>
<div><a href="<?php echo esc_url( $post['url'] ); ?>" target="_blank"><?php echo esc_html( $post['name'] ); ?></a></div>
<div><?php printf( esc_html_x( '%d helpful / %d not helpful (%s%% helpful in total)', 'widget item info', 'helpful' ), intval( $post['pro'] ), intval( $post['contra'] ), $post['percentage'] ); ?></div>
Expand All @@ -76,7 +78,7 @@
</div>
<?php endif; ?>

<?php if ( ! empty( Helpers\Stats::get_least_helpful() ) && get_option( 'helpful_widget_contra' ) ) : ?>
<?php if ( isset( $widget_stats['least_helpful'] ) ) : ?>
<div class="helpful-widget-panel">

<button type="button">
Expand All @@ -85,7 +87,7 @@
</button>

<ul>
<?php foreach ( Helpers\Stats::get_least_helpful() as $post ) : ?>
<?php foreach ( $widget_stats['least_helpful'] as $post ) : ?>
<li>
<div><a href="<?php echo esc_url( $post['url'] ); ?>" target="_blank"><?php echo esc_html( $post['name'] ); ?></a></div>
<div><?php printf( esc_html_x( '%d helpful / %d not helpful (%s%% helpful in total)', 'widget item info', 'helpful' ), intval( $post['pro'] ), intval( $post['contra'] ), $post['percentage'] ); ?></div>
Expand All @@ -97,7 +99,7 @@
</div>
<?php endif; ?>

<?php if ( ! empty( Helpers\Stats::get_recently_pro() ) && get_option( 'helpful_widget_pro_recent' ) ) : ?>
<?php if ( isset( $widget_stats['recently_pro'] ) ) : ?>
<div class="helpful-widget-panel">

<button type="button">
Expand All @@ -106,7 +108,7 @@
</button>

<ul>
<?php foreach ( Helpers\Stats::get_recently_pro() as $post ) : ?>
<?php foreach ( $widget_stats['recently_pro'] as $post ) : ?>
<li>
<div><a href="<?php echo esc_url( $post['url'] ); ?>" target="_blank"><?php echo esc_html( $post['name'] ); ?></a></div>
<?php echo esc_html( $post['time'] ); ?>
Expand All @@ -117,7 +119,7 @@
</div>
<?php endif; ?>

<?php if ( ! empty( Helpers\Stats::get_recently_contra() ) && get_option( 'helpful_widget_contra_recent' ) ) : ?>
<?php if ( isset( $widget_stats['recently_contra'] ) ) : ?>
<div class="helpful-widget-panel">

<button type="button">
Expand All @@ -126,7 +128,7 @@
</button>

<ul>
<?php foreach ( Helpers\Stats::get_recently_contra() as $post ) : ?>
<?php foreach ( $widget_stats['recently_contra'] as $post ) : ?>
<li>
<div><a href="<?php echo esc_url( $post['url'] ); ?>" target="_blank"><?php echo esc_html( $post['name'] ); ?></a></div>
<?php echo esc_html( $post['time'] ); ?>
Expand All @@ -137,7 +139,7 @@
</div>
<?php endif; ?>

<?php if ( Helpers\Feedback::get_feedback_items() && get_option( 'helpful_feedback_widget' ) ) : ?>
<?php if ( isset( $widget_stats['feedback_items'] ) ) : ?>
<div class="helpful-widget-panel">

<button type="button">
Expand All @@ -146,7 +148,7 @@
</button>

<ul>
<?php foreach ( Helpers\Feedback::get_feedback_items() as $feedback ) : ?>
<?php foreach ( $widget_stats['feedback_items'] as $feedback ) : ?>
<?php $feedback = Helpers\Feedback::get_feedback( $feedback ); ?>
<li>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=helpful_feedback' ) ); ?>">
Expand All @@ -165,8 +167,8 @@
echo implode( '', $links );

$total = 0;
$total += (int) Helpers\Stats::get_pro_all();
$total += (int) Helpers\Stats::get_contra_all();
$total += isset( $widget_stats['pro_total'] ) ? $widget_stats['pro_total'] : 0;
$total += isset( $widget_stats['contra_total'] ) ? $widget_stats['contra_total'] : 0;
?>
<div class="helpful-widget-total">
<?php printf( esc_html__( '%d Votes', 'helpful' ), intval( $total ) ); ?>
Expand Down

0 comments on commit 62fc613

Please sign in to comment.