Skip to content

Commit

Permalink
4.4.63
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelbart committed Nov 11, 2021
1 parent 880be56 commit 150694b
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 8 deletions.
38 changes: 32 additions & 6 deletions core/helpers/class-feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* @package Helpful
* @subpackage Core\Helpers
* @version 4.4.51
* @version 4.4.63
* @since 1.0.0
*/
namespace Helpful\Core\Helpers;
Expand Down Expand Up @@ -114,7 +114,7 @@ public static function insert_feedback()
$pro = 0;
$contra = 0;
$message = null;

do_action('helpful/insert_feedback');

if (!isset($_REQUEST['post_id'])) {
Expand Down Expand Up @@ -190,9 +190,11 @@ public static function insert_feedback()
$instance = sanitize_text_field($_REQUEST['instance']);
}

$user_id = esc_attr($_REQUEST['user_id']);

$data = [
'time' => current_time('mysql'),
'user' => esc_attr($_REQUEST['user_id']),
'user' => $user_id,
'pro' => $pro,
'contra' => $contra,
'post_id' => $post_id,
Expand All @@ -212,10 +214,30 @@ public static function insert_feedback()
return $wpdb->insert_id;
}

/**
* Checks if the feedback already exists.
*
* @global $wdpb
*
* @version 4.4.63
*
* @param array $feedback_data
*
* @return bool
*/
public static function feedback_exists($data)
{
global $wpdb;
$table_name = $wpdb->prefix . 'helpful_feedback';
$sql = "SELECT COUNT(*) FROM $table_name WHERE user = %s AND pro = %d AND contra = %d AND post_id = %d AND message = %s AND instance_id = %d";
$var = $wpdb->get_var($wpdb->prepare($sql, $data['user'], $data['pro'], $data['contra'], $data['post_id'], $data['message'], $data['instance_id']));
return ($var) ? true : false;
}

/**
* Send feedback email.
*
* @version 4.4.59
* @version 4.4.63
*
* @param array $feedback feedback data.
*
Expand All @@ -225,6 +247,10 @@ public static function send_email($feedback)
{
$options = new Services\Options();

if (true === self::feedback_exists($feedback)) {
return;
}

/**
* Send email to voter.
*/
Expand All @@ -239,7 +265,7 @@ public static function send_email($feedback)
if (!$post) {
return;
}

do_action('helpful/send_email');

$feedback['fields'] = maybe_unserialize($feedback['fields']);
Expand Down Expand Up @@ -350,7 +376,7 @@ public static function send_email_voter($feedback)
if (!$post) {
return;
}

do_action('helpful/send_email_voter');

$feedback['fields'] = maybe_unserialize($feedback['fields']);
Expand Down
198 changes: 198 additions & 0 deletions core/modules/class-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php
/**
* @package Helpful
* @subpackage Core\Modules
* @version 4.4.63
* @since 4.4.63
*/
namespace Helpful\Core\Modules;

use Helpful\Core\Helper;
use Helpful\Core\Helpers as Helpers;
use Helpful\Core\Services as Services;

/* Prevent direct access */
if (!defined('ABSPATH')) {
exit;
}

class Api
{
/**
* Instance
*
* @var Api
*/
public static $instance;

/**
* Set instance and fire class
*
* @return Api
*/
public static function get_instance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}

/**
* Class constructor
*
* @return void
*/
public function __construct()
{
add_action('rest_api_init', [ & $this, 'init_pro']);
add_action('rest_api_init', [ & $this, 'init_contra']);
}

/**
* Returns the default options for the init of the REST API.
*
* @version 4.4.63
*
* @return array
*/
public function default_options()
{
$options = [
'methods' => 'POST',
'permission_callback' => '__return_empty_string',
];

return apply_filters('helpful/api/options', $options);
}

/**
* Allows saving a positive vote, or returns positive votes of a post.
*
* @version 4.4.63
*
* @return void
*/
public function init_pro()
{
$options = $this->default_options();

$options['callback'] = function ($data) {

if (!isset($data['user_id']) || '' === $data['user_id']) {
return 0;
}

if (!isset($data['post_id']) || !is_numeric($data['post_id'])) {
return 0;
}

$user_id = sanitize_text_field($data['user_id']);
$post_id = intval(sanitize_text_field($data['post_id']));

Helpful\Core\Helpers::insert_vote($user_id, $post_id, 'pro', null);

return 1;
};

/**
* POST domain.com/wp-json/helpful/pro/
*
* @param int $post_id
* @param string $user_id
*
* @return int
*/
register_rest_route('helpful', '/pro/', $options);

$options['methods'] = 'GET';

$options['callback'] = function ($data) {
if (!isset($data['post_id']) || !is_numeric($data['post_id'])) {
return 0;
}

$result = Helpers\Stats::get_pro($data['post_id']);

if (is_numeric($result)) {
return intval($result);
}

return 0;
};

/**
* GET domain.com/wp-json/helpful/pro/{post_id}
*
* @param int $post_id
*
* @return int
*/
register_rest_route('helpful', '/pro/(?P<post_id>\d+)', $options);
}

/**
* Allows saving a contra vote, or returns negative votes of a post.
*
* @version 4.4.63
*
* @return void
*/
public function init_contra()
{
$options = $this->default_options();

$options['callback'] = function ($data) {

if (!isset($data['user_id']) || '' === $data['user_id']) {
return 0;
}

if (!isset($data['post_id']) || !is_numeric($data['post_id'])) {
return 0;
}

$user_id = sanitize_text_field($data['user_id']);
$post_id = intval(sanitize_text_field($data['post_id']));

Helpful\Core\Helpers::insert_vote($user_id, $post_id, 'contra', null);

return 1;
};

/**
* POST domain.com/wp-json/helpful/contra/
*
* @param int $post_id
* @param string $user_id
*
* @return int
*/
register_rest_route('helpful', '/contra/', $options);

$options['methods'] = 'GET';

$options['callback'] = function ($data) {
if (!isset($data['post_id']) || !is_numeric($data['post_id'])) {
return 0;
}

$result = Helpers\Stats::get_contra($data['post_id']);

if (is_numeric($result)) {
return intval($result);
}

return 0;
};

/**
* GET domain.com/wp-json/helpful/contra/{post_id}
*
* @param int $post_id
*
* @return int
*/
register_rest_route('helpful', '/contra/(?P<post_id>\d+)', $options);
}
}
3 changes: 2 additions & 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.62
* Version: 4.4.63
* Author: Pixelbart
* Author URI: https://pixelbart.de
* Text Domain: helpful
Expand Down Expand Up @@ -153,6 +153,7 @@ public function init()
Helpful\Core\Modules\Widget::get_instance();
Helpful\Core\Modules\Customizer::get_instance();
Helpful\Core\Modules\Frontend::get_instance();
Helpful\Core\Modules\Api::get_instance();

Helpful\Core\Tabs\Start::get_instance();
Helpful\Core\Tabs\Details::get_instance();
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.8
Requires PHP: 5.6.20
Stable tag: 4.4.62
Stable tag: 4.4.63
License: MIT License
License URI: https://opensource.org/licenses/MIT

Expand Down

0 comments on commit 150694b

Please sign in to comment.