diff --git a/.gitignore b/.gitignore index 454a19e..60868f2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ img/.DS_Store *.sublime-workspace .vscode/launch.json .vscode/settings.json +.idea/shelf +.idea/sonarlint diff --git a/classes/class-sailthru-content.php b/classes/class-sailthru-content.php index a4d59b5..b45123f 100644 --- a/classes/class-sailthru-content.php +++ b/classes/class-sailthru-content.php @@ -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() { @@ -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 @@ -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; diff --git a/classes/class-wp-sailthru-client.php b/classes/class-wp-sailthru-client.php index 66e24a6..324f58c 100644 --- a/classes/class-wp-sailthru-client.php +++ b/classes/class-wp-sailthru-client.php @@ -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 ); @@ -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 ); } diff --git a/lib/Sailthru_Client.php b/lib/Sailthru_Client.php index dfae76c..bdcf2c2 100755 --- a/lib/Sailthru_Client.php +++ b/lib/Sailthru_Client.php @@ -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 @@ -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) {