Skip to content

Commit

Permalink
Merge pull request #99 from sailthru/IN-1884-IN-1850-Combined
Browse files Browse the repository at this point in the history
In 1884 in 1850 combined
  • Loading branch information
rzsouza-st authored Mar 23, 2021
2 parents 629815f + 0283f9e commit 5b79e9d
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ img/.DS_Store
*.sublime-workspace
.vscode/launch.json
.vscode/settings.json
.idea/shelf
.idea/sonarlint
4 changes: 2 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: sailthru-wp
Tags: personalization, email,
Requires at least: 5.5
Tested up to: 5.7
Stable tag: 4.0.3
Stable tag: 4.1.0

Provides an integration with Sailthru

Expand All @@ -28,4 +28,4 @@ https://getstarted.sailthru.com/integrations/wordpress/wordpress-plugin/

= v4.0.0 =
This version supports Wordpress version 5.5 and isn't backwards compatible with older Wordpress versions.
Only upgrade if using Wordpress version 5.5 or greater.
Only upgrade if using Wordpress version 5.5 or greater.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v4.1.0 (2021-03-23)
Fixed JQUery queries without scope hidding non-Sailthru elements
Added delete from Sailthru when item is sent to trash in Wordpress

## v4.0.3 (2021-01-20)
Fixed bug where an unset Welcome Template breaks the signup widget

Expand Down
135 changes: 94 additions & 41 deletions classes/class-sailthru-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ class Sailthru_Content_Settings {

public function __construct() {

// make sure the priority is above the default of 10, the meta boxes are saved first.
// make sure the priority is above the default of 10, the meta boxes are saved first.
add_action( 'admin_menu', array( $this, 'add_admin_menu' ), 11 );
add_action( 'admin_init', array( $this, 'init_settings' ), 11 );
add_action( 'save_post', array( $this, 'sailthru_save_post' ), 11, 3 );
add_action( 'wp_head', array( $this, 'generate_meta_tags' ) );

add_action( 'wp_trash_post', array( $this, 'sailthru_delete_post'), 11, 2);
}

public function add_admin_menu() {
Expand Down Expand Up @@ -51,7 +51,7 @@ public function init_settings() {

// Only show these fields if the status has been enabled
if ( isset ( $options['sailthru_content_api_status'] ) && 'true' === $options['sailthru_content_api_status'] ) {

add_settings_field(
'sailthru_spider_status',
__( 'Spider', 'text_domain' ),
Expand Down Expand Up @@ -116,7 +116,7 @@ public function page_layout() {
}

// Admin Page Layout
echo '<div class="wrap">' . "\n";
echo '<div class="wrap sailthru-wrap">' . "\n";
echo ' <h1>Sailthru for WordPress</h1>' . "\n";
echo ' <form action="options.php" method="post">' . "\n";

Expand Down Expand Up @@ -182,11 +182,11 @@ function render_sailthru_content_post_types_field() {
$value = isset( $options['sailthru_content_post_types'] ) ? $options['sailthru_content_post_types'] : '';

if ( false != $options ) {

$post_type_args = ['public' => true];
$post_types = get_post_types( $post_type_args, 'names');
// Always remove the attachment post type, we never need this.

// Always remove the attachment post type, we never need this.
unset ($post_types['attachment']);

echo '<p class="description">' . esc_attr__( 'Choose which type of post types should be synced to Sailthru', 'text_domain' ) . '</p>';
Expand All @@ -197,7 +197,7 @@ function render_sailthru_content_post_types_field() {
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 Down Expand Up @@ -252,10 +252,10 @@ function render_sailthru_taxonomy_tag_options_field() {
$value = isset( $options['sailthru_taxonomy_tag_options'] ) ? $options['sailthru_taxonomy_tag_options'] : array();

foreach ($taxonomies as $tag) {

echo '<input type="checkbox" name="sailthru_content_settings[sailthru_taxonomy_tag_options][]" class="sailthru_taxonomy_tag_options_field" value="' . esc_attr( $tag ) . '" ' . ( in_array( $tag, $value, true ) ? 'checked="checked"' : '' ) . '" /> ' . esc_attr__( $tag, 'text_domain' ) . '<br>';
}

}


Expand Down Expand Up @@ -317,17 +317,17 @@ function generate_payload( $post, $post_id ) {
$data['images']['thumb']['url'] = $post_thumbnail;
}

// Add any galleries from the post to the images.
// Add any galleries from the post to the images.
$data['images']['galleries'] = get_post_galleries_images( $post );

$data['tags'] = $this->generate_tags( $post->ID);

// Apply any filters to the tags.
$data['tags'] = apply_filters( 'sailthru_horizon_meta_tags', ['sailthru.tags' => $data['tags'] ] ) ;
// Apply any filters to the tags.
$data['tags'] = apply_filters( 'sailthru_horizon_meta_tags', ['sailthru.tags' => $data['tags'] ] ) ;

// Check if the filter has returned sailthru.tags and convert to string.
// Check if the filter has returned sailthru.tags and convert to string.
if ( is_array( $data['tags'] ) && isset ( $data['tags']['sailthru.tags'] ) ) {
$data['tags'] = $data['tags']['sailthru.tags'];
$data['tags'] = $data['tags']['sailthru.tags'];
}

$post_expiration = get_post_meta( $post->ID, 'sailthru_post_expiration', true );
Expand All @@ -341,10 +341,17 @@ function generate_payload( $post, $post_id ) {
// Add the vars
$data['vars'] = $this->generate_vars( $post->ID, $post );


return $data;
}

private function generate_content_delete_payload( WP_Post $post ): array {

$url = get_permalink( $post->ID );
$url_with_correct_protocol = set_url_scheme( $url );

return array( 'url' => $url_with_correct_protocol );
}

/*-------------------------------------------
* Utility Functions
Expand Down Expand Up @@ -387,7 +394,7 @@ function generate_meta_tags() {
$post_title = get_the_title();
$horizon_tags['sailthru.title'] = esc_attr( $post_title );

// Get the tags.
// Get the tags.
$content = new Sailthru_Content_Settings;
$post_tags = $content->generate_tags( $post->ID);

Expand Down Expand Up @@ -450,41 +457,41 @@ function generate_meta_tags() {
}

/**
* Generates the output of the interest tags for both the Content API and the meta tags.
* Generates the output of the interest tags for both the Content API and the meta tags.
*
* @param integer $post_id
*/

function generate_tags( $post_id ) {

$options = get_option( 'sailthru_content_settings' );
$post_tags = get_post_meta( $post_id, 'sailthru_meta_tags', true );

// Add WordPress tags if option set.
// Add WordPress tags if option set.
if ( isset( $options['sailthru_interest_tag_options'] ) && in_array( 'wordpress_tags',$options['sailthru_interest_tag_options'] ) ) {

$wp_tags = get_the_tags();
if ( $wp_tags ) {
$post_tags .= ',' .esc_attr( implode( ',', wp_list_pluck( $wp_tags, 'name' ) ) );
}
}

// Add WordPress categories if option set.
// Add WordPress categories if option set.
if ( isset( $options['sailthru_interest_tag_options'] ) && in_array( 'wordpress_categories', $options['sailthru_interest_tag_options'] ) ) {
$post_categories = get_the_category( $post->ID );
foreach ( $post_categories as $post_category ) {
$post_tags .= ','. $post_category->name;
}
}

// Add WordPress taxonomies if option set.
// Add WordPress taxonomies if option set.
if ( !empty( $options['sailthru_taxonomy_tag_options'] ) ) {
$terms = wp_get_post_terms( $post_id, $options['sailthru_taxonomy_tag_options'] );
$post_tags .= ',' .esc_attr( implode( ',', wp_list_pluck( $terms, 'name' ) ) );
}


// check if there's any global tags needing added.
// check if there's any global tags needing added.
if ( ! empty($options['sailthru_content_interest_tags'] ) ) {
$post_tags .= ',' . $options['sailthru_content_interest_tags'];
}
Expand All @@ -493,13 +500,13 @@ function generate_tags( $post_id ) {
}

/**
* Generates the output of the interest tags for both the Content API and the meta tags.
* Generates the output of the interest tags for both the Content API and the meta tags.
*
* @param integer $post_id
*/

function generate_vars( $post_id, $post ) {

$vars = [
'post_type' => $post->post_type,
'id' => $post->ID,
Expand All @@ -511,16 +518,16 @@ function generate_vars( $post_id, $post ) {
$field_names = array_keys( array_merge( $custom_fields, $vars ) );

// always exclude these vars
$exclude_fields = array( '_edit_lock',
'_edit_last',
'_encloseme',
'_pingme',
'sailthru_meta_tags',
'sailthru_post_expiration',
'sailthru_sailthru_tags_extra_data'
$exclude_fields = array( '_edit_lock',
'_edit_last',
'_encloseme',
'_pingme',
'sailthru_meta_tags',
'sailthru_post_expiration',
'sailthru_sailthru_tags_extra_data'
);

// Set vars from the custom fields.
// Set vars from the custom fields.
foreach ( $custom_fields as $key => $val ) {

if ( ! in_array( $key, $exclude_fields, true ) ) {
Expand All @@ -535,7 +542,7 @@ function generate_vars( $post_id, $post ) {
}

/**
* Generates vars from the whitelisted vars and filters
* Generates vars from the whitelisted vars and filters
*
* @param array $vars
*/
Expand All @@ -544,13 +551,13 @@ function whitelist_vars( $vars ) {
$options = get_option( 'sailthru_content_settings' );

if (! empty($options['sailthru_content_vars'] ) ) {
// Get the Whitelisted vars from the settings screen.

// Get the Whitelisted vars from the settings screen.
$whitelist = explode(', ', $options['sailthru_content_vars']);
$whitelist = apply_filters( 'sailthru_content_whitelist_vars', $whitelist);

foreach ($vars as $key => $val) {

if ( !in_array($key, $whitelist) ) {
unset( $vars[$key] );
}
Expand All @@ -573,15 +580,15 @@ function whitelist_vars( $vars ) {

function sailthru_save_post( $post_id, $post, $update ) {

// Get the content options to see if we want to fire the API.
// Get the content options to see if we want to fire the API.
$options = get_option( 'sailthru_content_settings' );

// Check to see if Content API is disabled in the UI
if( !isset ($options['sailthru_content_api_status'] ) || "false" === $options['sailthru_content_api_status'] ) {
return;
}

// See if a filter has disabled the content API, this may be done to override a specific use case.
// See if a filter has disabled the content API, this may be done to override a specific use case.
if ( false === apply_filters( 'sailthru_content_api_enable', true ) ) {
return;
}
Expand All @@ -597,7 +604,7 @@ function sailthru_save_post( $post_id, $post, $update ) {
$api_secret = $sailthru['sailthru_api_secret'];
$client = new WP_Sailthru_Client( $api_key, $api_secret );
try {

if ( $client ) {
$data = $this->generate_payload($post, $post_id);
// Make the API call to Sailthru
Expand All @@ -613,6 +620,52 @@ function sailthru_save_post( $post_id, $post, $update ) {
}
}

function sailthru_delete_post( int $post_id ) {

$post = get_post( $post_id );

if ( !isset($post) ) {
return;
}

// Get the content options to see if we want to fire the API.
$options = get_option( 'sailthru_content_settings' );

// Check to see if Content API is disabled in the UI
if( !isset ($options['sailthru_content_api_status'] ) || "false" === $options['sailthru_content_api_status'] ) {
return;
}

// See if a filter has disabled the content API, this may be done to override a specific use case.
if ( false === apply_filters( 'sailthru_content_api_enable', true ) ) {
return;
}

if ( in_array( $post->post_type, $options['sailthru_content_post_types'], true ) ) {

if ( 'publish' === $post->post_status ) {
// Make sure Sailthru is setup
if ( get_option( 'sailthru_setup_complete' ) ) {
$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 );

try {
if ( $client ) {
$data = $this->generate_content_delete_payload( $post );
// Make the API call to Sailthru
$api = $client->apiDelete( 'content', $data );
}
} catch ( Sailthru_Client_Exception $e ) {
write_log($e);
return;
}
}
}
}
}

}

new Sailthru_Content_Settings;
4 changes: 3 additions & 1 deletion classes/class-wp-sailthru-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function httpRequestCurl( $action, array $data, $method = 'POST', $options = []

$url = $this->api_uri . '/' . $action;

if ( 'GET' === $method ) {
if ( $method === 'GET' || $method === 'DELETE' ) {
$url_with_params = $url;
if ( count( $data ) > 0 ) {
$url_with_params .= '?' . http_build_query( $data );
Expand Down Expand Up @@ -122,6 +122,8 @@ function httpRequestCurl( $action, array $data, $method = 'POST', $options = []
} else {
$reply = wp_remote_get( $url, $data );
}
} else if ( $method === 'DELETE' ) {
$reply = wp_remote_request( $url, [ 'method' => 'DELETE' ] );
} else {
$reply = wp_remote_post( $url, $data );
}
Expand Down
Loading

0 comments on commit 5b79e9d

Please sign in to comment.