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/README.txt b/README.txt index 67ee153..2eb0d7e 100644 --- a/README.txt +++ b/README.txt @@ -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 @@ -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. \ No newline at end of file +Only upgrade if using Wordpress version 5.5 or greater. diff --git a/changelog.md b/changelog.md index 714eb56..9fe93de 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/classes/class-sailthru-content.php b/classes/class-sailthru-content.php index a4d59b5..7d395b6 100644 --- a/classes/class-sailthru-content.php +++ b/classes/class-sailthru-content.php @@ -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() { @@ -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' ), @@ -116,7 +116,7 @@ public function page_layout() { } // Admin Page Layout - echo '
' . "\n"; + echo '
' . "\n"; echo '

Sailthru for WordPress

' . "\n"; echo '
' . "\n"; @@ -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 '

' . esc_attr__( 'Choose which type of post types should be synced to Sailthru', 'text_domain' ) . '

'; @@ -197,7 +197,7 @@ function render_sailthru_content_post_types_field() { echo ' ' . esc_attr__( ucwords ( $type ) , 'text_domain' ) . '
'; } - } + } } @@ -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 ' ' . esc_attr__( $tag, 'text_domain' ) . '
'; } - + } @@ -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 ); @@ -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 @@ -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); @@ -450,26 +457,26 @@ 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 ) { @@ -477,14 +484,14 @@ function generate_tags( $post_id ) { } } - // 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']; } @@ -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, @@ -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 ) ) { @@ -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 */ @@ -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] ); } @@ -573,7 +580,7 @@ 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 @@ -581,7 +588,7 @@ function sailthru_save_post( $post_id, $post, $update ) { 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; } @@ -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 @@ -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/js/admin.js b/js/admin.js index b44b21d..3b40315 100755 --- a/js/admin.js +++ b/js/admin.js @@ -38,8 +38,8 @@ // datepicker for meta box // but since Datepicker causes a jQuery conflict, wrap it // and prevent from initializing unless necessary - if ($('.datepicker').length) { - $('.datepicker').datepicker({ + if ($('.datepicker', 'sailthru-wrap').length) { + $('.datepicker', 'sailthru-wrap').datepicker({ dateFormat: 'yy-mm-dd' }); } @@ -47,7 +47,7 @@ // custom form fields - $('.selection').parent().parent().hide(); + $('.selection', '.sailthru-wrap').parent().parent().hide(); $('#type').closest('table').find("tr").last().hide(); // hide hidden field $("#sailthru_customfield_hidden_value").closest('tr').hide(); @@ -70,14 +70,14 @@ if ($(this).attr('value') == 'select' || $(this).attr('value') == 'radio' || $(this).attr('value') == 'checkbox') { $(this).closest('table').find("tr").last().show(); - $('#add_value').show(); + $('#add_value', 'sailthru-wrap').show(); $("input[name*=sailthru_customfield_value1]").show(); - $('.selection').parent().parent().show(); - $('#add_value').show(); + $('.selection', 'sailthru-wrap').parent().parent().show(); + $('#add_value', 'sailthru-wrap').show(); $("input[name*=sailthru_customfield_value1]").show(); if ($(this).attr('value') == 'hidden') { - $('#add_value').hide(); + $('#add_value', 'sailthru-wrap').hide(); $("input[name*=sailthru_customfield_value1]").hide(); $("input[name*=sailthru_customfield_value1]").parent().parent().find('th').html('Field Value'); } @@ -87,11 +87,10 @@ } })); - $('#add_value').on("click", (function(e) { + $('#add_value', 'sailthru-wrap').on("click", (function(e) { e.preventDefault(); var new_val = parseInt($('#value_amount').attr('value'), 10); new_val = new_val + 1; - var second_val = new_val + 1; $('#sailthru_value_fields_block').append('
'); $('#value_amount').attr('value', parseInt(new_val, 10)); })); @@ -118,12 +117,12 @@ $("#sailthru_setup_email_template").parents('tr').addClass('grayBorder'); // datepickerfor meta box - $('.datepicker').datepicker({ + $('.datepicker', 'sailthru-wrap').datepicker({ dateFormat: 'yy-mm-dd' }); - $("#sortable").disableSelection(); - var sort = $("#sortable").sortable({ + $("#sortable", 'sailthru-wrap').disableSelection(); + var sort = $("#sortable", 'sailthru-wrap').sortable({ axis: 'y', stop: function(event, ui) { var data = sort.sortable("serialize"); @@ -143,4 +142,4 @@ }); -}(jQuery)); \ No newline at end of file +}(jQuery)); 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) { diff --git a/plugin.php b/plugin.php index b2faf46..9c78b33 100644 --- a/plugin.php +++ b/plugin.php @@ -3,7 +3,7 @@ Plugin Name: Sailthru for WordPress Plugin URI: http://sailthru.com/ Description: Add the power of Sailthru to your WordPress set up. -Version: 4.0.3 +Version: 4.1.0 Requires at least: 5.5 Author: Sailthru Author URI: http://sailthru.com @@ -36,7 +36,7 @@ * @var const $version The current version of the plugin. */ if ( ! defined( 'SAILTHRU_PLUGIN_VERSION' ) ) { - define( 'SAILTHRU_PLUGIN_VERSION', '4.0.3' ); + define( 'SAILTHRU_PLUGIN_VERSION', '4.1.0' ); } if ( ! defined( 'SAILTHRU_PLUGIN_PATH' ) ) { diff --git a/views/admin.functions.subscribe.options.php b/views/admin.functions.subscribe.options.php index d80c9d0..c6bdfc3 100644 --- a/views/admin.functions.subscribe.options.php +++ b/views/admin.functions.subscribe.options.php @@ -82,7 +82,7 @@ function sailthru_forms_callback( $args ) { ** Custom and Extra Sections should be in a first column. ** Begin the column here. It ends in delete_field() */ - echo '
'; + echo '
'; $customfields = get_option( 'sailthru_forms_options' ); $key = get_option( 'sailthru_forms_key' ); @@ -202,7 +202,7 @@ function sailthru_success_field( $args ) { echo '

'; echo '

Use the field above to update the message that the user sees after subscribing

'; - /* ends the wrapper */ + /* ends the wrapper */ echo '
'; echo '
'; @@ -607,7 +607,7 @@ function sailthru_forms_handler( $input ) { $values = rtrim( $values, ',' ); $output[ $new_key ]['sailthru_customfield_value'] = $values; } - + if ( 'hidden' === $input['sailthru_customfield_type'] ) { $output[ $new_key ]['sailthru_customfield_value'] = sanitize_text_field( $input['sailthru_customfield_hidden_value'] ); } diff --git a/views/admin.php b/views/admin.php index e5c635d..b93b180 100644 --- a/views/admin.php +++ b/views/admin.php @@ -1,5 +1,5 @@ -
+