Skip to content

Commit

Permalink
[IN-1884] Basic implementation of delete posts
Browse files Browse the repository at this point in the history
  • Loading branch information
rzsouza-st committed Mar 18, 2021
1 parent 629815f commit f2cf73a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 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
55 changes: 54 additions & 1 deletion classes/class-sailthru-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function __construct() {
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 @@ -345,6 +345,13 @@ function generate_payload( $post, $post_id ) {
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 @@ -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
3 changes: 3 additions & 0 deletions lib/Sailthru_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,8 @@ protected function httpRequestWithoutCurl($action, $data, $method = 'POST', $opt

/**
* Perform an HTTP request, checking for curl extension support
*
* @see WP_Sailthru_Client::httpRequestCurl() for override of httpRequestCurl
*
* @param $action
* @param array $data
Expand All @@ -1507,6 +1509,7 @@ protected function httpRequestWithoutCurl($action, $data, $method = 'POST', $opt
* @throws Sailthru_Client_Exception
*/
protected function httpRequest($action, $data, $method = 'POST', $options = [ ]) {
// See WP_Sailthru_Client::httpRequestCurl() for this override
$response = $this->{$this->http_request_type}($action, $data, $method, $options);
$json = json_decode($response, true);
if ($json === NULL) {
Expand Down

0 comments on commit f2cf73a

Please sign in to comment.