Skip to content

Commit 5b79e9d

Browse files
authored
Merge pull request #99 from sailthru/IN-1884-IN-1850-Combined
In 1884 in 1850 combined
2 parents 629815f + 0283f9e commit 5b79e9d

10 files changed

+126
-63
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ img/.DS_Store
55
*.sublime-workspace
66
.vscode/launch.json
77
.vscode/settings.json
8+
.idea/shelf
9+
.idea/sonarlint

README.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: sailthru-wp
33
Tags: personalization, email,
44
Requires at least: 5.5
55
Tested up to: 5.7
6-
Stable tag: 4.0.3
6+
Stable tag: 4.1.0
77

88
Provides an integration with Sailthru
99

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

2929
= v4.0.0 =
3030
This version supports Wordpress version 5.5 and isn't backwards compatible with older Wordpress versions.
31-
Only upgrade if using Wordpress version 5.5 or greater.
31+
Only upgrade if using Wordpress version 5.5 or greater.

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v4.1.0 (2021-03-23)
4+
Fixed JQUery queries without scope hidding non-Sailthru elements
5+
Added delete from Sailthru when item is sent to trash in Wordpress
6+
37
## v4.0.3 (2021-01-20)
48
Fixed bug where an unset Welcome Template breaks the signup widget
59

classes/class-sailthru-content.php

Lines changed: 94 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ class Sailthru_Content_Settings {
44

55
public function __construct() {
66

7-
// make sure the priority is above the default of 10, the meta boxes are saved first.
7+
// make sure the priority is above the default of 10, the meta boxes are saved first.
88
add_action( 'admin_menu', array( $this, 'add_admin_menu' ), 11 );
99
add_action( 'admin_init', array( $this, 'init_settings' ), 11 );
1010
add_action( 'save_post', array( $this, 'sailthru_save_post' ), 11, 3 );
1111
add_action( 'wp_head', array( $this, 'generate_meta_tags' ) );
12-
12+
add_action( 'wp_trash_post', array( $this, 'sailthru_delete_post'), 11, 2);
1313
}
1414

1515
public function add_admin_menu() {
@@ -51,7 +51,7 @@ public function init_settings() {
5151

5252
// Only show these fields if the status has been enabled
5353
if ( isset ( $options['sailthru_content_api_status'] ) && 'true' === $options['sailthru_content_api_status'] ) {
54-
54+
5555
add_settings_field(
5656
'sailthru_spider_status',
5757
__( 'Spider', 'text_domain' ),
@@ -116,7 +116,7 @@ public function page_layout() {
116116
}
117117

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

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

184184
if ( false != $options ) {
185-
185+
186186
$post_type_args = ['public' => true];
187187
$post_types = get_post_types( $post_type_args, 'names');
188-
189-
// Always remove the attachment post type, we never need this.
188+
189+
// Always remove the attachment post type, we never need this.
190190
unset ($post_types['attachment']);
191191

192192
echo '<p class="description">' . esc_attr__( 'Choose which type of post types should be synced to Sailthru', 'text_domain' ) . '</p>';
@@ -197,7 +197,7 @@ function render_sailthru_content_post_types_field() {
197197
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>';
198198
}
199199

200-
}
200+
}
201201

202202
}
203203

@@ -252,10 +252,10 @@ function render_sailthru_taxonomy_tag_options_field() {
252252
$value = isset( $options['sailthru_taxonomy_tag_options'] ) ? $options['sailthru_taxonomy_tag_options'] : array();
253253

254254
foreach ($taxonomies as $tag) {
255-
255+
256256
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>';
257257
}
258-
258+
259259
}
260260

261261

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

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

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

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

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

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

344-
344+
345345
return $data;
346346
}
347347

348+
private function generate_content_delete_payload( WP_Post $post ): array {
349+
350+
$url = get_permalink( $post->ID );
351+
$url_with_correct_protocol = set_url_scheme( $url );
352+
353+
return array( 'url' => $url_with_correct_protocol );
354+
}
348355

349356
/*-------------------------------------------
350357
* Utility Functions
@@ -387,7 +394,7 @@ function generate_meta_tags() {
387394
$post_title = get_the_title();
388395
$horizon_tags['sailthru.title'] = esc_attr( $post_title );
389396

390-
// Get the tags.
397+
// Get the tags.
391398
$content = new Sailthru_Content_Settings;
392399
$post_tags = $content->generate_tags( $post->ID);
393400

@@ -450,41 +457,41 @@ function generate_meta_tags() {
450457
}
451458

452459
/**
453-
* Generates the output of the interest tags for both the Content API and the meta tags.
460+
* Generates the output of the interest tags for both the Content API and the meta tags.
454461
*
455462
* @param integer $post_id
456463
*/
457464

458465
function generate_tags( $post_id ) {
459-
466+
460467
$options = get_option( 'sailthru_content_settings' );
461468
$post_tags = get_post_meta( $post_id, 'sailthru_meta_tags', true );
462469

463-
// Add WordPress tags if option set.
470+
// Add WordPress tags if option set.
464471
if ( isset( $options['sailthru_interest_tag_options'] ) && in_array( 'wordpress_tags',$options['sailthru_interest_tag_options'] ) ) {
465-
472+
466473
$wp_tags = get_the_tags();
467474
if ( $wp_tags ) {
468475
$post_tags .= ',' .esc_attr( implode( ',', wp_list_pluck( $wp_tags, 'name' ) ) );
469476
}
470477
}
471478

472-
// Add WordPress categories if option set.
479+
// Add WordPress categories if option set.
473480
if ( isset( $options['sailthru_interest_tag_options'] ) && in_array( 'wordpress_categories', $options['sailthru_interest_tag_options'] ) ) {
474481
$post_categories = get_the_category( $post->ID );
475482
foreach ( $post_categories as $post_category ) {
476483
$post_tags .= ','. $post_category->name;
477484
}
478485
}
479486

480-
// Add WordPress taxonomies if option set.
487+
// Add WordPress taxonomies if option set.
481488
if ( !empty( $options['sailthru_taxonomy_tag_options'] ) ) {
482489
$terms = wp_get_post_terms( $post_id, $options['sailthru_taxonomy_tag_options'] );
483490
$post_tags .= ',' .esc_attr( implode( ',', wp_list_pluck( $terms, 'name' ) ) );
484491
}
485492

486493

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

495502
/**
496-
* Generates the output of the interest tags for both the Content API and the meta tags.
503+
* Generates the output of the interest tags for both the Content API and the meta tags.
497504
*
498505
* @param integer $post_id
499506
*/
500507

501508
function generate_vars( $post_id, $post ) {
502-
509+
503510
$vars = [
504511
'post_type' => $post->post_type,
505512
'id' => $post->ID,
@@ -511,16 +518,16 @@ function generate_vars( $post_id, $post ) {
511518
$field_names = array_keys( array_merge( $custom_fields, $vars ) );
512519

513520
// always exclude these vars
514-
$exclude_fields = array( '_edit_lock',
515-
'_edit_last',
516-
'_encloseme',
517-
'_pingme',
518-
'sailthru_meta_tags',
519-
'sailthru_post_expiration',
520-
'sailthru_sailthru_tags_extra_data'
521+
$exclude_fields = array( '_edit_lock',
522+
'_edit_last',
523+
'_encloseme',
524+
'_pingme',
525+
'sailthru_meta_tags',
526+
'sailthru_post_expiration',
527+
'sailthru_sailthru_tags_extra_data'
521528
);
522529

523-
// Set vars from the custom fields.
530+
// Set vars from the custom fields.
524531
foreach ( $custom_fields as $key => $val ) {
525532

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

537544
/**
538-
* Generates vars from the whitelisted vars and filters
545+
* Generates vars from the whitelisted vars and filters
539546
*
540547
* @param array $vars
541548
*/
@@ -544,13 +551,13 @@ function whitelist_vars( $vars ) {
544551
$options = get_option( 'sailthru_content_settings' );
545552

546553
if (! empty($options['sailthru_content_vars'] ) ) {
547-
548-
// Get the Whitelisted vars from the settings screen.
554+
555+
// Get the Whitelisted vars from the settings screen.
549556
$whitelist = explode(', ', $options['sailthru_content_vars']);
550557
$whitelist = apply_filters( 'sailthru_content_whitelist_vars', $whitelist);
551558

552559
foreach ($vars as $key => $val) {
553-
560+
554561
if ( !in_array($key, $whitelist) ) {
555562
unset( $vars[$key] );
556563
}
@@ -573,15 +580,15 @@ function whitelist_vars( $vars ) {
573580

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

576-
// Get the content options to see if we want to fire the API.
583+
// Get the content options to see if we want to fire the API.
577584
$options = get_option( 'sailthru_content_settings' );
578585

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

584-
// See if a filter has disabled the content API, this may be done to override a specific use case.
591+
// See if a filter has disabled the content API, this may be done to override a specific use case.
585592
if ( false === apply_filters( 'sailthru_content_api_enable', true ) ) {
586593
return;
587594
}
@@ -597,7 +604,7 @@ function sailthru_save_post( $post_id, $post, $update ) {
597604
$api_secret = $sailthru['sailthru_api_secret'];
598605
$client = new WP_Sailthru_Client( $api_key, $api_secret );
599606
try {
600-
607+
601608
if ( $client ) {
602609
$data = $this->generate_payload($post, $post_id);
603610
// Make the API call to Sailthru
@@ -613,6 +620,52 @@ function sailthru_save_post( $post_id, $post, $update ) {
613620
}
614621
}
615622

623+
function sailthru_delete_post( int $post_id ) {
624+
625+
$post = get_post( $post_id );
626+
627+
if ( !isset($post) ) {
628+
return;
629+
}
630+
631+
// Get the content options to see if we want to fire the API.
632+
$options = get_option( 'sailthru_content_settings' );
633+
634+
// Check to see if Content API is disabled in the UI
635+
if( !isset ($options['sailthru_content_api_status'] ) || "false" === $options['sailthru_content_api_status'] ) {
636+
return;
637+
}
638+
639+
// See if a filter has disabled the content API, this may be done to override a specific use case.
640+
if ( false === apply_filters( 'sailthru_content_api_enable', true ) ) {
641+
return;
642+
}
643+
644+
if ( in_array( $post->post_type, $options['sailthru_content_post_types'], true ) ) {
645+
646+
if ( 'publish' === $post->post_status ) {
647+
// Make sure Sailthru is setup
648+
if ( get_option( 'sailthru_setup_complete' ) ) {
649+
$sailthru = get_option( 'sailthru_setup_options' );
650+
$api_key = $sailthru['sailthru_api_key'];
651+
$api_secret = $sailthru['sailthru_api_secret'];
652+
$client = new WP_Sailthru_Client( $api_key, $api_secret );
653+
654+
try {
655+
if ( $client ) {
656+
$data = $this->generate_content_delete_payload( $post );
657+
// Make the API call to Sailthru
658+
$api = $client->apiDelete( 'content', $data );
659+
}
660+
} catch ( Sailthru_Client_Exception $e ) {
661+
write_log($e);
662+
return;
663+
}
664+
}
665+
}
666+
}
667+
}
668+
616669
}
617670

618671
new Sailthru_Content_Settings;

classes/class-wp-sailthru-client.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function httpRequestCurl( $action, array $data, $method = 'POST', $options = []
8686

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

89-
if ( 'GET' === $method ) {
89+
if ( $method === 'GET' || $method === 'DELETE' ) {
9090
$url_with_params = $url;
9191
if ( count( $data ) > 0 ) {
9292
$url_with_params .= '?' . http_build_query( $data );
@@ -122,6 +122,8 @@ function httpRequestCurl( $action, array $data, $method = 'POST', $options = []
122122
} else {
123123
$reply = wp_remote_get( $url, $data );
124124
}
125+
} else if ( $method === 'DELETE' ) {
126+
$reply = wp_remote_request( $url, [ 'method' => 'DELETE' ] );
125127
} else {
126128
$reply = wp_remote_post( $url, $data );
127129
}

0 commit comments

Comments
 (0)