@@ -4,12 +4,12 @@ class Sailthru_Content_Settings {
4
4
5
5
public function __construct () {
6
6
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.
8
8
add_action ( 'admin_menu ' , array ( $ this , 'add_admin_menu ' ), 11 );
9
9
add_action ( 'admin_init ' , array ( $ this , 'init_settings ' ), 11 );
10
10
add_action ( 'save_post ' , array ( $ this , 'sailthru_save_post ' ), 11 , 3 );
11
11
add_action ( 'wp_head ' , array ( $ this , 'generate_meta_tags ' ) );
12
-
12
+ add_action ( ' wp_trash_post ' , array ( $ this , ' sailthru_delete_post ' ), 11 , 2 );
13
13
}
14
14
15
15
public function add_admin_menu () {
@@ -51,7 +51,7 @@ public function init_settings() {
51
51
52
52
// Only show these fields if the status has been enabled
53
53
if ( isset ( $ options ['sailthru_content_api_status ' ] ) && 'true ' === $ options ['sailthru_content_api_status ' ] ) {
54
-
54
+
55
55
add_settings_field (
56
56
'sailthru_spider_status ' ,
57
57
__ ( 'Spider ' , 'text_domain ' ),
@@ -116,7 +116,7 @@ public function page_layout() {
116
116
}
117
117
118
118
// Admin Page Layout
119
- echo '<div class="wrap"> ' . "\n" ;
119
+ echo '<div class="wrap sailthru-wrap "> ' . "\n" ;
120
120
echo ' <h1>Sailthru for WordPress</h1> ' . "\n" ;
121
121
echo ' <form action="options.php" method="post"> ' . "\n" ;
122
122
@@ -182,11 +182,11 @@ function render_sailthru_content_post_types_field() {
182
182
$ value = isset ( $ options ['sailthru_content_post_types ' ] ) ? $ options ['sailthru_content_post_types ' ] : '' ;
183
183
184
184
if ( false != $ options ) {
185
-
185
+
186
186
$ post_type_args = ['public ' => true ];
187
187
$ 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.
190
190
unset ($ post_types ['attachment ' ]);
191
191
192
192
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() {
197
197
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> ' ;
198
198
}
199
199
200
- }
200
+ }
201
201
202
202
}
203
203
@@ -252,10 +252,10 @@ function render_sailthru_taxonomy_tag_options_field() {
252
252
$ value = isset ( $ options ['sailthru_taxonomy_tag_options ' ] ) ? $ options ['sailthru_taxonomy_tag_options ' ] : array ();
253
253
254
254
foreach ($ taxonomies as $ tag ) {
255
-
255
+
256
256
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> ' ;
257
257
}
258
-
258
+
259
259
}
260
260
261
261
@@ -317,17 +317,17 @@ function generate_payload( $post, $post_id ) {
317
317
$ data ['images ' ]['thumb ' ]['url ' ] = $ post_thumbnail ;
318
318
}
319
319
320
- // Add any galleries from the post to the images.
320
+ // Add any galleries from the post to the images.
321
321
$ data ['images ' ]['galleries ' ] = get_post_galleries_images ( $ post );
322
322
323
323
$ data ['tags ' ] = $ this ->generate_tags ( $ post ->ID );
324
324
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 ' ] ] ) ;
327
327
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.
329
329
if ( is_array ( $ data ['tags ' ] ) && isset ( $ data ['tags ' ]['sailthru.tags ' ] ) ) {
330
- $ data ['tags ' ] = $ data ['tags ' ]['sailthru.tags ' ];
330
+ $ data ['tags ' ] = $ data ['tags ' ]['sailthru.tags ' ];
331
331
}
332
332
333
333
$ post_expiration = get_post_meta ( $ post ->ID , 'sailthru_post_expiration ' , true );
@@ -341,10 +341,17 @@ function generate_payload( $post, $post_id ) {
341
341
// Add the vars
342
342
$ data ['vars ' ] = $ this ->generate_vars ( $ post ->ID , $ post );
343
343
344
-
344
+
345
345
return $ data ;
346
346
}
347
347
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
+ }
348
355
349
356
/*-------------------------------------------
350
357
* Utility Functions
@@ -387,7 +394,7 @@ function generate_meta_tags() {
387
394
$ post_title = get_the_title ();
388
395
$ horizon_tags ['sailthru.title ' ] = esc_attr ( $ post_title );
389
396
390
- // Get the tags.
397
+ // Get the tags.
391
398
$ content = new Sailthru_Content_Settings ;
392
399
$ post_tags = $ content ->generate_tags ( $ post ->ID );
393
400
@@ -450,41 +457,41 @@ function generate_meta_tags() {
450
457
}
451
458
452
459
/**
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.
454
461
*
455
462
* @param integer $post_id
456
463
*/
457
464
458
465
function generate_tags ( $ post_id ) {
459
-
466
+
460
467
$ options = get_option ( 'sailthru_content_settings ' );
461
468
$ post_tags = get_post_meta ( $ post_id , 'sailthru_meta_tags ' , true );
462
469
463
- // Add WordPress tags if option set.
470
+ // Add WordPress tags if option set.
464
471
if ( isset ( $ options ['sailthru_interest_tag_options ' ] ) && in_array ( 'wordpress_tags ' ,$ options ['sailthru_interest_tag_options ' ] ) ) {
465
-
472
+
466
473
$ wp_tags = get_the_tags ();
467
474
if ( $ wp_tags ) {
468
475
$ post_tags .= ', ' .esc_attr ( implode ( ', ' , wp_list_pluck ( $ wp_tags , 'name ' ) ) );
469
476
}
470
477
}
471
478
472
- // Add WordPress categories if option set.
479
+ // Add WordPress categories if option set.
473
480
if ( isset ( $ options ['sailthru_interest_tag_options ' ] ) && in_array ( 'wordpress_categories ' , $ options ['sailthru_interest_tag_options ' ] ) ) {
474
481
$ post_categories = get_the_category ( $ post ->ID );
475
482
foreach ( $ post_categories as $ post_category ) {
476
483
$ post_tags .= ', ' . $ post_category ->name ;
477
484
}
478
485
}
479
486
480
- // Add WordPress taxonomies if option set.
487
+ // Add WordPress taxonomies if option set.
481
488
if ( !empty ( $ options ['sailthru_taxonomy_tag_options ' ] ) ) {
482
489
$ terms = wp_get_post_terms ( $ post_id , $ options ['sailthru_taxonomy_tag_options ' ] );
483
490
$ post_tags .= ', ' .esc_attr ( implode ( ', ' , wp_list_pluck ( $ terms , 'name ' ) ) );
484
491
}
485
492
486
493
487
- // check if there's any global tags needing added.
494
+ // check if there's any global tags needing added.
488
495
if ( ! empty ($ options ['sailthru_content_interest_tags ' ] ) ) {
489
496
$ post_tags .= ', ' . $ options ['sailthru_content_interest_tags ' ];
490
497
}
@@ -493,13 +500,13 @@ function generate_tags( $post_id ) {
493
500
}
494
501
495
502
/**
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.
497
504
*
498
505
* @param integer $post_id
499
506
*/
500
507
501
508
function generate_vars ( $ post_id , $ post ) {
502
-
509
+
503
510
$ vars = [
504
511
'post_type ' => $ post ->post_type ,
505
512
'id ' => $ post ->ID ,
@@ -511,16 +518,16 @@ function generate_vars( $post_id, $post ) {
511
518
$ field_names = array_keys ( array_merge ( $ custom_fields , $ vars ) );
512
519
513
520
// 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 '
521
528
);
522
529
523
- // Set vars from the custom fields.
530
+ // Set vars from the custom fields.
524
531
foreach ( $ custom_fields as $ key => $ val ) {
525
532
526
533
if ( ! in_array ( $ key , $ exclude_fields , true ) ) {
@@ -535,7 +542,7 @@ function generate_vars( $post_id, $post ) {
535
542
}
536
543
537
544
/**
538
- * Generates vars from the whitelisted vars and filters
545
+ * Generates vars from the whitelisted vars and filters
539
546
*
540
547
* @param array $vars
541
548
*/
@@ -544,13 +551,13 @@ function whitelist_vars( $vars ) {
544
551
$ options = get_option ( 'sailthru_content_settings ' );
545
552
546
553
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.
549
556
$ whitelist = explode (', ' , $ options ['sailthru_content_vars ' ]);
550
557
$ whitelist = apply_filters ( 'sailthru_content_whitelist_vars ' , $ whitelist );
551
558
552
559
foreach ($ vars as $ key => $ val ) {
553
-
560
+
554
561
if ( !in_array ($ key , $ whitelist ) ) {
555
562
unset( $ vars [$ key ] );
556
563
}
@@ -573,15 +580,15 @@ function whitelist_vars( $vars ) {
573
580
574
581
function sailthru_save_post ( $ post_id , $ post , $ update ) {
575
582
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.
577
584
$ options = get_option ( 'sailthru_content_settings ' );
578
585
579
586
// Check to see if Content API is disabled in the UI
580
587
if ( !isset ($ options ['sailthru_content_api_status ' ] ) || "false " === $ options ['sailthru_content_api_status ' ] ) {
581
588
return ;
582
589
}
583
590
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.
585
592
if ( false === apply_filters ( 'sailthru_content_api_enable ' , true ) ) {
586
593
return ;
587
594
}
@@ -597,7 +604,7 @@ function sailthru_save_post( $post_id, $post, $update ) {
597
604
$ api_secret = $ sailthru ['sailthru_api_secret ' ];
598
605
$ client = new WP_Sailthru_Client ( $ api_key , $ api_secret );
599
606
try {
600
-
607
+
601
608
if ( $ client ) {
602
609
$ data = $ this ->generate_payload ($ post , $ post_id );
603
610
// Make the API call to Sailthru
@@ -613,6 +620,52 @@ function sailthru_save_post( $post_id, $post, $update ) {
613
620
}
614
621
}
615
622
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
+
616
669
}
617
670
618
671
new Sailthru_Content_Settings ;
0 commit comments