Skip to content

Commit

Permalink
4.4.51
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelbart committed Aug 27, 2021
1 parent 08e2359 commit ab15439
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 152 deletions.
23 changes: 15 additions & 8 deletions core/helpers/class-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,16 @@ public static function set_user()
*
* @global $wpdb
*
* @version 4.4.51
* @since 4.4.0
*
* @param string $user_id user id.
* @param int $post_id post id.
* @param string $instance
*
* @return bool
*/
public static function check_user($user_id, $post_id)
public static function check_user($user_id, $post_id, $instance = null)
{
if (get_option('helpful_multiple')) {
return false;
Expand All @@ -158,15 +162,18 @@ public static function check_user($user_id, $post_id)
}

global $wpdb;

$table_name = $wpdb->prefix . 'helpful';

$sql = "
SELECT user, post_id
FROM {$table_name}
WHERE user = %s AND post_id = %d
ORDER BY id DESC
LIMIT 1
";
$query = $wpdb->prepare($sql, $user_id, $post_id);
SELECT user, post_id, instance_id
FROM {$table_name}
WHERE user = %s AND post_id = %d AND instance_id = %s
ORDER BY id DESC
LIMIT 1
";

$query = $wpdb->prepare($sql, $user_id, $post_id, $instance);
$results = $wpdb->get_results($query);

if ($results) {
Expand Down
281 changes: 142 additions & 139 deletions core/modules/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,148 @@ public function enqueue_scripts()
wp_localize_script('helpful', 'helpful', $vars);
}

/**
* Add helpful to post content
*
* @global $post
*
* @version 4.4.51
* @since 4.3.0
*
* @param string $content post content.
*
* @return string
*/
public function the_content($content)
{
global $post;

if (helpful_is_amp()) {
return $content;
}

if (!isset($post->ID)) {
return $content;
}

if (apply_filters('helpful/the_content/disabled', false, $post)) {
return $content;
}

$helpful = Helpers\Values::get_defaults();
$post_types = get_option('helpful_post_types');
$user_id = Helpers\User::get_user();

if ('on' === get_post_meta($helpful['post_id'], 'helpful_hide_on_post', true)) {
return $content;
}

if (!is_array($post_types) || !in_array($post->post_type, $post_types, true)) {
return $content;
}

if ('on' === get_option('helpful_hide_in_content')) {
return $content;
}

$conditions = Helper::get_conditions();

if (!empty($conditions)) {
return $content;
}

$shortcode = '[helpful post_id="' . $helpful['post_id'] . '"]';

return $content . $shortcode;
}

/**
* Callback for helpful shortcode
*
* @global $post
*
* @version 4.4.51
* @since 4.3.0
*
* @param array $atts shortcode attributes.
* @param string $content shortcode content.
*
* @return string
*/
public function helpful($atts, $content = '')
{
global $post;

if (helpful_is_amp()) {
return $content;
}

if (apply_filters('helpful/shortcode/disabled', false, $post)) {
return $content;
}

$defaults = Helpers\Values::get_defaults();
$defaults = apply_filters('helpful_shortcode_defaults', $defaults);

$helpful = shortcode_atts($defaults, $atts);
$helpful = apply_filters('helpful_shortcode_atts', $helpful);

$user_id = Helpers\User::get_user();

$object = new Services\Helpful($helpful['post_id'], $helpful);

if ('on' === get_option('helpful_exists_hide') && Helpers\User::check_user($user_id, $helpful['post_id'], $object->get_id())) {
return $content;
}

$exists = false;
$hidden = false;
$class = '';

if (isset($helpful['exists']) && 1 === $helpful['exists']) {
if (isset($helpful['exists-hide']) && 1 === $helpful['exists-hide']) {
return $content;
}

$exists = true;
$hidden = true;
$class = 'helpful-exists';
$helpful['content'] = $helpful['exists_text'];
}

if (null === $helpful['post_id']) {
return $content;
}

if (false !== $exists && get_option('helpful_feedback_after_vote')) {
if (!Helper::is_feedback_disabled()) {
$content = Helpers\Feedback::after_vote($helpful['post_id'], true);
$content = Helpers\Values::convert_tags($content, $helpful['post_id']);
return $content;
}
}

if (get_post_meta($helpful['post_id'], 'helpful_heading', true)) {
$helpful['heading'] = do_shortcode(get_post_meta($helpful['post_id'], 'helpful_heading', true));
}

if (get_post_meta($helpful['post_id'], 'helpful_pro', true)) {
$helpful['button_pro'] = do_shortcode(get_post_meta($helpful['post_id'], 'helpful_pro', true));
}

if (get_post_meta($helpful['post_id'], 'helpful_contra', true)) {
$helpful['button_contra'] = do_shortcode(get_post_meta($helpful['post_id'], 'helpful_contra', true));
}

$helpful['shortcode_exists'] = $exists;
$helpful['shortcode_hidden'] = $hidden;
$helpful['shortcode_class'] = $class;

$object->set_atts($helpful);

return $object->get_template();
}

/**
* Ajax save user vote and render response.
*
Expand Down Expand Up @@ -339,145 +481,6 @@ public function save_feedback()
wp_die();
}

/**
* Add helpful to post content
*
* @global $post
*
* @version 4.4.49
* @since 4.3.0
*
* @param string $content post content.
*
* @return string
*/
public function the_content($content)
{
global $post;

if (helpful_is_amp()) {
return $content;
}

if (!isset($post->ID)) {
return $content;
}

if (apply_filters('helpful/the_content/disabled', false, $post)) {
return $content;
}

$helpful = Helpers\Values::get_defaults();
$post_types = get_option('helpful_post_types');
$user_id = Helpers\User::get_user();

if ('on' === get_post_meta($helpful['post_id'], 'helpful_hide_on_post', true)) {
return $content;
}

if (!is_array($post_types) || !in_array($post->post_type, $post_types, true)) {
return $content;
}

if ('on' === get_option('helpful_hide_in_content')) {
return $content;
}

$conditions = Helper::get_conditions();

if (!empty($conditions)) {
return $content;
}

$shortcode = '[helpful post_id="' . $helpful['post_id'] . '"]';

return $content . $shortcode;
}

/**
* Callback for helpful shortcode
*
* @global $post
*
* @version 4.4.49
* @since 4.3.0
*
* @param array $atts shortcode attributes.
* @param string $content shortcode content.
*
* @return string
*/
public function helpful($atts, $content = '')
{
global $post;

if (helpful_is_amp()) {
return $content;
}

if (apply_filters('helpful/shortcode/disabled', false, $post)) {
return $content;
}

$defaults = Helpers\Values::get_defaults();
$defaults = apply_filters('helpful_shortcode_defaults', $defaults);

$helpful = shortcode_atts($defaults, $atts);
$helpful = apply_filters('helpful_shortcode_atts', $helpful);

$user_id = Helpers\User::get_user();

if ('on' === get_option('helpful_exists_hide') && Helpers\User::check_user($user_id, $helpful['post_id'])) {
return $content;
}

$exists = false;
$hidden = false;
$class = '';

if (isset($helpful['exists']) && 1 === $helpful['exists']) {
if (isset($helpful['exists-hide']) && 1 === $helpful['exists-hide']) {
return $content;
}

$exists = true;
$hidden = true;
$class = 'helpful-exists';
$helpful['content'] = $helpful['exists_text'];
}

if (null === $helpful['post_id']) {
return esc_html__('No post found. Helpful must be placed in a post loop.', 'helpful');
}

if (false !== $exists && get_option('helpful_feedback_after_vote')) {
if (!Helper::is_feedback_disabled()) {
$content = Helpers\Feedback::after_vote($helpful['post_id'], true);
$content = Helpers\Values::convert_tags($content, $helpful['post_id']);
return $content;
}
}

if (get_post_meta($helpful['post_id'], 'helpful_heading', true)) {
$helpful['heading'] = do_shortcode(get_post_meta($helpful['post_id'], 'helpful_heading', true));
}

if (get_post_meta($helpful['post_id'], 'helpful_pro', true)) {
$helpful['button_pro'] = do_shortcode(get_post_meta($helpful['post_id'], 'helpful_pro', true));
}

if (get_post_meta($helpful['post_id'], 'helpful_contra', true)) {
$helpful['button_contra'] = do_shortcode(get_post_meta($helpful['post_id'], 'helpful_contra', true));
}

$helpful['shortcode_exists'] = $exists;
$helpful['shortcode_hidden'] = $hidden;
$helpful['shortcode_class'] = $class;

$object = new Services\Helpful($helpful['post_id'], $helpful);
return $object->get_template();
}

/**
* Filters the frontend nonces and set the value to false, using option.
*
Expand Down
15 changes: 10 additions & 5 deletions core/services/class-helpful.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,22 @@ public function get_id()
global $wp;

$url = home_url($wp->request);
$id = md5($url . maybe_serialize($this->atts));
$id = md5($url . $this->atts['heading']);

$instance_id = Helpers\Instance::insert_instance($id, $this->get_post_id(), $this->atts['heading']);

return $instance_id;
}

/**
* @param array $atts
* @return void
*/
public function set_atts($atts = [])
{
$this->atts = $atts;
}

/**
* @return int
*/
Expand Down Expand Up @@ -101,10 +110,6 @@ public function get_template()

$content = Helpers\Values::convert_tags($content, $helpful['post_id']);

if (is_user_logged_in()) {
$content .= sprintf('<pre>%s</pre>', print_r($this->get_id(), true));
}

return $content;
}
}

0 comments on commit ab15439

Please sign in to comment.