Skip to content

Commit

Permalink
Merge pull request #79 from sailthru/IN-1255_Codesniffer_Little_Wins
Browse files Browse the repository at this point in the history
Little Wins in PHP Only - 11 files + Widget Title Removal
  • Loading branch information
henrivrod authored Aug 4, 2020
2 parents 2b70b32 + dc62107 commit bdd5770
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 325 deletions.
2 changes: 1 addition & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: brownac, lcooper, asilverman, nickgundry, sailthru-wp, automattic,
Tags: personalization, email,
Requires at least: 3.6
Tested up to: 5.4
Stable tag: 3.5.0
Stable tag: 3.6.0

This plugin provides fast and easy integration of the core Sailthru features into your Wordpress site.

Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v3.6.0 (2020-08-04)
Added option for users to have Sailthru Subscription widget title to disappear after user sign-up
Changed code which produced some errors and warnings in Wordpress PHP Codesniffer

## v3.5.0 (2020-07-15)
Added option for users to change Spider value in Content API calls (defaults to enabled)
Renamed Global Vars to Custom Fields and updated helper text on Content Settings page
Expand Down
44 changes: 0 additions & 44 deletions classes/class-sailthru-concierge.php

This file was deleted.

4 changes: 2 additions & 2 deletions classes/class-sailthru-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function render_sailthru_content_post_types_field() {
$selected_types = isset($options['sailthru_content_post_types']) ? $options['sailthru_content_post_types'] : [];
foreach ( $post_types as $type ) {
$selected = in_array( $type , $selected_types, true ) ? 'checked="checked"' : '';
echo '<input type="checkbox" name="sailthru_content_settings[sailthru_content_post_types][]" class="sailthru_content_post_types_field" value="' . esc_attr( $type ) . '" ' . $selected . '> ' . esc_attr__( ucwords ( $type ) , 'text_domain' ) . '<br>';
echo '<input type="checkbox" name="sailthru_content_settings[sailthru_content_post_types][]" class="sailthru_content_post_types_field" value="' . esc_attr( $type ) . '" ' . esc_attr( $selected ) . '> ' . esc_attr__( ucwords ( $type ) , 'text_domain' ) . '<br>';
}

}
Expand All @@ -214,7 +214,7 @@ function render_sailthru_content_vars_field() {

// Field output.
echo '<input type="text" name="sailthru_content_settings[sailthru_content_vars]" class="regular-text sailthru_content_vars_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $value ) . '">';
echo '<p class="description">' . __( '<p>Please provide a comma-separated list of WordPress custom fields to include in the Sailthru Content Library.</p>', 'text_domain' ) . '</p>';
echo '<p class="description"><p>' . esc_attr__( 'Please provide a comma-separated list of WordPress custom fields to include in the Sailthru Content Library.', 'text_domain' ) . '</p></p>';
echo '<p class="description">' . esc_attr__( 'These fields will be usable within Sailthru messages and content feeds. If blank, all fields will be sent to Sailthru.', 'text_domain' ) . '</p>';

}
Expand Down
3 changes: 1 addition & 2 deletions classes/class-sailthru-horizon.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static function sailthru_update_check() {

update_option( 'sailthru_forms_options', $updatedcustomfields );

} // end if $customfields isset
}
}

}
Expand Down Expand Up @@ -120,7 +120,6 @@ public function register_admin_scripts( $hook ) {
}
}
}
// wp_enqueue_script( 'sailthru-subscribe-widget-admin-jquery-script', SAILTHRU_PLUGIN_URL . 'js/widget.subscribe.admin.js', array( 'jquery' ) );
wp_enqueue_script( 'jquery-ui-datepicker', array( 'jquery' ) );

// abandon if we're not on our own pages.
Expand Down
10 changes: 5 additions & 5 deletions classes/class-sailthru-subscribe-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,12 @@ public function post_metabox_display( $post ) {
*/
public function save_custom_meta_data( $post_id ) {

$is_valid_nonce = ( isset( $_POST[ $this->nonce ] ) && wp_verify_nonce( sanitize_text_field( $_POST[ $this->nonce ] ), plugin_basename( __FILE__ ) ) );
// First, make sure the user can save the post
if ( $this->user_can_save( $post_id, $this->nonce ) ) {
if ( $is_valid_nonce && $this->user_can_save( $post_id ) ) {

// Did the user set an expiry date, or are they clearing an old one?
if ( ! empty( $_POST['sailthru_post_expiration'] ) && isset( $_POST['sailthru_post_expiration'] )
if ( ! empty( $_POST['sailthru_post_expiration'] ) && isset( $_POST['sailthru_post_expiration'] )
|| get_post_meta( $post_id, 'sailthru_post_expiration', true ) ) {

$expiry_time = strtotime( sanitize_text_field( $_POST['sailthru_post_expiration'] ) );
Expand Down Expand Up @@ -464,14 +465,13 @@ public function save_custom_meta_data( $post_id ) {
* @param int $post_id The ID of the post being save
* @param bool Whether or not the user has the ability to save this post.
*/
function user_can_save( $post_id, $nonce ) {
function user_can_save( $post_id ) {

$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], plugin_basename( __FILE__ ) ) );

// Return true if the user is able to save; otherwise, false.
return ! ( $is_autosave || $is_revision ) && $is_valid_nonce;
return ! ( $is_autosave || $is_revision );

} // end user_can_save
}
125 changes: 59 additions & 66 deletions js/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,97 +30,90 @@
$return['error'] = false;
$return['message'] = '';

switch( $_POST['sailthru_action'] )
{
if( isset( $_POST['sailthru_action'] ) ) {

case "add_subscriber":
$email = trim( $_POST['email'] );
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) || empty( $email ) ) {
$return['error'] = true;
$return['message'] = 'Please enter a valid email address.';
} else {
$email = filter_var( $email, FILTER_VALIDATE_EMAIL );
}
switch( $_POST['sailthru_action'] ) {

if ( isset( $_POST['first_name'] ) && !empty( $_POST['first_name'] ) ){
$first_name = filter_var(trim($_POST['first_name']), FILTER_SANITIZE_STRING );
} else {
$first_name = '';
}

if ( isset( $_POST['last_name'] ) && !empty( $_POST['last_name'] ) ){
$last_name = filter_var( trim( $_POST['last_name'] ), FILTER_SANITIZE_STRING ) ;
} else {
$last_name = '';
}

if ( $first_name || $last_name ) {

$options = array(
'vars' => array(
'first_name' => $first_name,
'last_name' => $last_name,
)
);

}

$subscribe_to_lists = array();
if ( !empty($_POST['sailthru_email_list'] ) ) {

$lists = explode(',', $_POST['sailthru_email_list']);
case "add_subscriber":
$email = isset( $_POST['email'] ) ? trim( sanitize_email( $_POST['email'] ) ) : '';
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) || empty( $email ) ) {
$return['error'] = true;
$return['message'] = 'Please enter a valid email address.';
} else {
$email = filter_var( $email, FILTER_VALIDATE_EMAIL );
}

foreach( $lists as $key => $list ) {
if ( isset( $_POST['first_name'] ) && !empty( $_POST['first_name'] ) ){
$first_name = filter_var( trim( sanitize_text_field( $_POST['first_name'] ) ), FILTER_SANITIZE_STRING );
} else {
$first_name = '';
}

$subscribe_to_lists[ $list ] = 1;
if ( isset( $_POST['last_name'] ) && !empty( $_POST['last_name'] ) ){
$last_name = filter_var( trim( sanitize_text_field( $_POST['last_name'] ) ), FILTER_SANITIZE_STRING ) ;
} else {
$last_name = '';
}

}
if ( $first_name || $last_name ) {
$options = [
'vars' => [
'first_name' => $first_name,
'last_name' => $last_name
]
];
}

$options['lists'] = $subscribe_to_lists;
$subscribe_to_lists = [];
if ( !empty($_POST['sailthru_email_list'] ) ) {

} else {
$lists = explode(',', sanitize_text_field( $_POST['sailthru_email_list'] ) );

$options['lists'] = array('Sailthru Subscribe Widget' => 1); // subscriber is an orphan
foreach( $lists as $key => $list ) {
$subscribe_to_lists[ $list ] = 1;
}

}
$options['lists'] = $subscribe_to_lists;

} else {

$options['vars']['source'] = get_bloginfo('url');
$options['lists'] = array('Sailthru Subscribe Widget' => 1); // subscriber is an orphan

}

$return['data'] = array(
'email' => $email,
'options' => $options
);
$options['vars']['source'] = get_bloginfo('url');

if ( false === $return['error'] ) {
$return['data'] = [
'email' => $email,
'options' => $options
];

$sailthru = get_option('sailthru_setup_options');
$api_key = $sailthru['sailthru_api_key'];
$api_secret = $sailthru['sailthru_api_secret'];
if ( false === $return['error'] ) {
$sailthru = get_option('sailthru_setup_options');
$api_key = $sailthru['sailthru_api_key'];
$api_secret = $sailthru['sailthru_api_secret'];

$client = new WP_Sailthru_Client( $api_key, $api_secret );
$res = $client->saveUser($email, $options);
$client = new WP_Sailthru_Client( $api_key, $api_secret );
$res = $client->saveUser($email, $options);

if( $res['ok'] !== 'true' ) {
$result['error'] = true;
$result['message'] = "There was an error subscribing you. Please try again later.";
}

if( $res['ok'] !== 'true' ) {
$result['error'] = true;
$result['message'] = "There was an error subscribing you. Please try again later.";
$return['result'] = $res;
}

$return['result'] = $res;

}
break;

break;
default:

default:
$return['error'] = true;
$return['message'] = 'No action defined. None taken.';

$return['error'] = true;
$return['message'] = 'No action defined. None taken.';
}

}


echo wp_json_encode( $return );
die();
51 changes: 25 additions & 26 deletions js/widget.subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
e.preventDefault();
var posTop = $(this).offset().top;
var modal = $( "#sailthru-modal");
modal.css("top", '100px');
modal.css("left", Math.max(0, (($(window).width() - $(modal).outerWidth()) / 2) + $(window).scrollLeft()) + "px");

$('.sailthru_shortcode_hidden .sailthru-signup-widget-close').show();
modal.fadeIn();
modal.css("top", '100px');
modal.css("left", Math.max(0, (($(window).width() - $(modal).outerWidth()) / 2) + $(window).scrollLeft()) + "px");

$('.sailthru_shortcode_hidden .sailthru-signup-widget-close').show();
modal.fadeIn();
});

$('#sailthru-modal .sailthru-signup-widget-close').click(function(){
Expand All @@ -25,34 +24,34 @@
e.preventDefault();
var recaptcha = $("#sailthruToken");
var siteKey = $("#siteKey").val();
if (!recaptcha.val() && siteKey) {
if (!recaptcha.val() && siteKey) {
var token = await grecaptcha.execute(siteKey, {action: 'homepage'});
recaptcha.val(token);
}
recaptcha.val(token);
}

var user_input = $(this).serialize();
var form = $(this);
$.ajax({
url: sailthru_vars.ajaxurl,
type: 'post',
data: user_input,
dataType: "json",
xhrFields: {
withCredentials: true
},
success: function (data, status) {
if (data.success == false) {
$('#' + form.attr('id') + " .sailthru-add-subscriber-errors").html(data.message);
} else {
$('#sailthru-modal .sailthru-signup-widget-close').fadeIn();
$(form).html('');
$(form).parent().find(".success").show();
}
}
} );
url: sailthru_vars.ajaxurl,
type: 'post',
data: user_input,
dataType: "json",
xhrFields: {
withCredentials: true
},
success: function (data, status) {
if (data.success == false) {
$('#' + form.attr('id') + " .sailthru-add-subscriber-errors").html(data.message);
} else {
$('#sailthru-modal .sailthru-signup-widget-close').fadeIn();
$(form).html('');
$(form).parent().find(".success").show();
$(form).parent().find(".hide_toggle").hide();
}
}
});

});


});
}(jQuery));
Loading

0 comments on commit bdd5770

Please sign in to comment.