Skip to content

Commit

Permalink
4.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelbart committed Oct 2, 2019
1 parent 9ec1475 commit a29d7bb
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 210 deletions.
102 changes: 100 additions & 2 deletions core/classes/class-helpful-helper-feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,17 @@ public static function insertFeedback() {
$pro = 0;
$contra = 0;
$message = null;
$post_id = absint( $_REQUEST['post_id'] );

if ( ! isset( $_REQUEST['message'] ) || helpful_backlist_check( $_REQUEST['message'] ) ) {
if ( ! isset( $_REQUEST['message'] ) ) {
$message = 'Helpful Notice: Feedback was not saved because the message is empty in %s on line %d.';
helpful_error_log( sprintf( $message, __FILE__, __LINE__ ) );
return null;
}

if ( helpful_backlist_check( $_REQUEST['message'] ) ) {
$message = 'Helpful Notice: Feedback was not saved because the message contains blacklisted words in %s on line %d.';
helpful_error_log( sprintf( $message, __FILE__, __LINE__ ) );
return null;
}

Expand Down Expand Up @@ -154,13 +163,102 @@ public static function insertFeedback() {
'user' => esc_attr( $_REQUEST['user_id'] ),
'pro' => $pro,
'contra' => $contra,
'post_id' => absint( $_REQUEST['post_id'] ),
'post_id' => $post_id,
'message' => $message,
'fields' => maybe_serialize( $fields ),
];

/* send email */
self::send_email( $data );

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

/**
* Send feedback email.
*
* @param array $feedback feedback data.
* @return void
*/
public static function send_email( $feedback ) {
if ( ! get_option( 'helpful_feedback_email' ) ) {
return;
}

$post = get_post( $feedback['post_id'] );

if ( ! $post ) {
return;
}

/* email headers */
$headers = [ 'Content-Type: text/html; charset=UTF-8' ];

/* email subject */
$subject = get_option( 'helpful_feedback_subject' );

/* unserialize feedback fields */
$feedback['fields'] = maybe_unserialize( $feedback['fields'] );

$type = esc_html__( 'positive', 'helpful' );
if ( 1 === $feedback['contra'] ) {
$type = esc_html__( 'negative', 'helpful' );
}

/* body tags */
$tags = [
'{type}' => $type,
'{name}' => $feedback['fields']['name'],
'{email}' => $feedback['fields']['email'],
'{message}' => $feedback['message'],
'{post_url}' => get_permalink( $post ),
'{post_title}' => $post->post_title,
'{blog_name}' => get_bloginfo( 'name' ),
'{blog_url}' => site_url(),
];

$body = get_option( 'helpful_feedback_email_content' );
$body = str_replace( array_keys( $tags ), array_values( $tags ), $body );

/* receivers by post meta */
$post_receivers = [];

if ( get_post_meta( $post->ID, 'helpful_feedback_receivers', true ) ) {
$post_receivers = get_post_meta( $post->ID, 'helpful_feedback_receivers', true );
$post_receivers = helpful_trim_all( $post_receivers );
$post_receivers = explode( ',', $post_receivers );
}

/* receivers by helpful options */
$helpful_receivers = [];

if ( get_option( 'helpful_feedback_receivers' ) ) {
$helpful_receivers = get_option( 'helpful_feedback_receivers' );
$helpful_receivers = helpful_trim_all( $helpful_receivers );
$helpful_receivers = explode( ',', $helpful_receivers );
}

$receivers = array_merge( $helpful_receivers, $post_receivers );
$receivers = array_unique( $receivers );

/* receivers array is empty */
if ( empty( $receivers ) ) {
return;
}

/* filters */
$receivers = apply_filters( 'helpful_feedback_email_receivers', $receivers );
$subject = apply_filters( 'helpful_feedback_email_subject', $subject );
$body = apply_filters( 'helpful_feedback_email_body', $body );
$headers = apply_filters( 'helpful_feedback_email_headers', $headers );

$response = wp_mail( $receivers, $subject, $body, $headers );

if ( false === $response ) {
$message = 'Helpful Warning: Email could not be sent in %s on line %d.';
helpful_error_log( sprintf( $message, __FILE__, __LINE__ ) );
}
}
}
47 changes: 47 additions & 0 deletions core/classes/class-helpful-helper-optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function optimize_plugin() {
$response = array_merge( $response, self::remove_incorrect_entries() );
$response = array_merge( $response, self::fix_incorrect_feedback() );
$response = array_merge( $response, self::clear_cache() );
$response = array_merge( $response, self::update_metas() );

array_filter( $response );

Expand Down Expand Up @@ -232,4 +233,50 @@ public static function clear_cache() {

return $response;
}

/**
* Update meta fields
*
* @return array
*/
public static function update_metas() {
$results = [];
$response = [];
$post_types = get_option( 'helpful_post_types' );

$args = [
'post_type' => $post_types,
'post_status' => 'any',
'fields' => 'ids',
];

$query = new WP_Query( $args );

if ( $query->found_posts ) {
foreach ( $query->posts as $post_id ) :

$percentages = false;

if ( get_option( 'helpful_percentages' ) ) {
$percentages = true;
}

$pro = Helpful_Helper_Stats::getPro( $post_id, $percentages );
$contra = Helpful_Helper_Stats::getContra( $post_id, $percentages );

update_post_meta( $post_id, 'helpful-pro', $pro );
update_post_meta( $post_id, 'helpful-contra', $contra );

endforeach;
}

$count = $query->found_posts;
$response[] = sprintf(
/* translators: %1$d = amount of entries */
esc_html_x( '%1$d post meta fields have been updated.', 'maintenance response', 'helpful' ),
$count
);

return $response;
}
}
8 changes: 7 additions & 1 deletion core/classes/class-helpful-metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function render_metabox() {
$contra = Helpful_Helper_Stats::getContra( $post->ID );
$contra_percent = Helpful_Helper_Stats::getContra( $post->ID, true );
$hide = get_post_meta( $post->ID, 'helpful_hide_on_post', true );
$receivers = get_post_meta( $post->ID, 'helpful_feedback_receivers', true );

wp_nonce_field( 'helpful_remove_data', 'helpful_remove_data_nonce' );
include HELPFUL_PATH . 'templates/admin-metabox.php';
Expand All @@ -98,5 +99,10 @@ public function save_metabox_data( $post_id ) {
} else {
update_post_meta( $post_id, 'helpful_hide_on_post', 'off' );
}

if ( isset( $_POST['helpful_feedback_receivers'] ) ) {
$receivers = sanitize_text_field( $_POST['helpful_feedback_receivers'] );
update_post_meta( $post_id, 'helpful_feedback_receivers', $receivers );
}
}
}
}
10 changes: 9 additions & 1 deletion core/classes/class-helpful-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ public function set_defaults( $status = false ) {
return false;
}

ob_start();
require_once HELPFUL_PATH . 'templates/feedback-email.php';
$feedback_email_content = ob_get_contents();
ob_end_clean();

$options = [
'helpful_heading' => _x( 'Was this post helpful?', 'default headline', 'helpful' ),
'helpful_content' => _x( 'Let us know if you liked the post. That’s the only way we can improve.', 'default description', 'helpful' ),
Expand All @@ -184,11 +189,14 @@ public function set_defaults( $status = false ) {
'helpful_credits' => true,
'helpful_uninstall' => false,
'helpful_widget' => true,
'helpful_widget_amount' => true,
'helpful_widget_amount' => 3,
'helpful_widget_pro' => true,
'helpful_widget_contra' => true,
'helpful_widget_pro_recent' => true,
'helpful_widget_contra_recent' => true,
'helpful_feedback_subject' => _x( 'There\'s new feedback for you.', 'feedback email subject', 'helpful' ),
'helpful_feedback_receivers' => get_option( 'admin_email' ),
'helpful_feedback_email_content' => $feedback_email_content,
];

$options = apply_filters( 'helpful_options', $options );
Expand Down
6 changes: 6 additions & 0 deletions core/classes/class-helpful-tabs-feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,14 @@ public function register_settings() {
'helpful_feedback_label_submit',
'helpful_feedback_label_cancel',
'helpful_feedback_gravatar',
'helpful_feedback_email',
'helpful_feedback_receivers',
'helpful_feedback_subject',
'helpful_feedback_email_content',
];

$fields = apply_filters( 'helpful_feedback_fields', $fields );

foreach ( $fields as $field ) {
register_setting( 'helpful-feedback-settings-group', $field );
}
Expand Down
86 changes: 63 additions & 23 deletions core/helpers.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,75 @@
<?php
/**
* WordPress blacklist checker
* Helpful helpers loaded before helpful classes.
*
* @param string $content the content to be checked.
*
* @return bool
* @package Helpful
* @author Pixelbart <[email protected]>
*/
function helpful_backlist_check( $content ) {
$mod_keys = trim( get_option( 'blacklist_keys' ) );

if ( '' == $mod_keys ) {
return false;
}
if ( ! function_exists( 'helpful_backlist_check' ) ) {
/**
* WordPress blacklist checker
*
* @param string $content the content to be checked.
*
* @return bool
*/
function helpful_backlist_check( $content ) {
$mod_keys = trim( get_option( 'blacklist_keys' ) );

if ( '' === $mod_keys ) {
return false;
}

$without_html = wp_strip_all_tags( $content );
$words = explode( "\n", $mod_keys );
$without_html = wp_strip_all_tags( $content );
$words = explode( "\n", $mod_keys );

foreach ( (array) $words as $word ) :
$word = trim( $word );
foreach ( (array) $words as $word ) :
$word = trim( $word );

if ( empty( $word ) ) {
continue;
}
if ( empty( $word ) ) {
continue;
}

$word = preg_quote( $word, '#' );
$pattern = "#$word#i";
$word = preg_quote( $word, '#' );
$pattern = "#$word#i";

if ( preg_match( $pattern, $content ) || preg_match( $pattern, $without_html ) ) {
return true;
}
endforeach;
if ( preg_match( $pattern, $content ) || preg_match( $pattern, $without_html ) ) {
return true;
}
endforeach;

return false;
}
}

if ( ! function_exists( 'helpful_trim_all' ) ) {
/**
* Trim all whitespaces.
*
* @param string $string string to trim.
* @return string
*/
function helpful_trim_all( $string ) {
return preg_replace( '/\s+/', '', $string );
}
}

return false;
if ( ! function_exists( 'helpful_error_log' ) ) {
/**
* This allows custom error messages to be placed in the error_logs.
* WP_DEBUG and WP_DEBUG_LOG must be set to true.
*
* @source https://wordpress.org/support/article/debugging-in-wordpress/
* @param string $message error message.
*/
function helpful_error_log( $message ) {
if ( true === WP_DEBUG ) {
if ( is_array( $message ) || is_object( $message ) ) {
error_log( print_r( $message, true ) );
} else {
error_log( $message );
}
}
}
}
Loading

0 comments on commit a29d7bb

Please sign in to comment.