diff --git a/includes/class-content.php b/includes/class-content.php index 8445fca..f431dd7 100644 --- a/includes/class-content.php +++ b/includes/class-content.php @@ -171,14 +171,7 @@ public static function get_republishable_plain_text_content( $post_object ) { // Add attribution. $display_attribution = get_option( 'republication_tracker_tool_display_attribution', 'on' ); if ( 'on' === $display_attribution ) { - $license_key = get_option( 'republication_tracker_tool_license', REPUBLICATION_TRACKER_TOOL_DEFAULT_LICENSE ); - $license_description = REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['description']; - $plain_text_content .= "\n" . sprintf( - // translators: %1$s is the site name, %2$s is the license description. - esc_html__( 'This article first appeared on %1$s and is republished here under a %2$s.', 'republication-tracker-tool' ), - get_bloginfo( 'name' ), - $license_description - ) . "\n\n"; + $plain_text_content .= Republication_Tracker_Tool::get_attribution( $post_object, true ) . "\n\n"; } /** diff --git a/includes/class-settings.php b/includes/class-settings.php index 9b9716a..0798dc2 100644 --- a/includes/class-settings.php +++ b/includes/class-settings.php @@ -257,21 +257,37 @@ public function republication_tracker_tool_license_callback() { ?>

- $license_values ) : ?> + $license_values ) : ?> + +
+ +
- + +

ID, 'republication-tracker-tool-hide-widget', true ), $post ); @@ -83,12 +87,16 @@ public function widget( $args, $instance ) { ); } - echo sprintf( - '

%s

', - REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['url'], - esc_html__( 'Creative Commons License', 'republication-tracker-tool' ), - esc_url( plugin_dir_url( dirname( __FILE__ ) ) ) . 'assets/img/' . $license_key . '.png' - ); + if ( $using_license ) { + echo sprintf( + '

%s

', + REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['url'], + esc_html__( 'Creative Commons License', 'republication-tracker-tool' ), + esc_url( plugin_dir_url( dirname( __FILE__ ) ) ) . 'assets/img/' . $license_key . '.png' + ); + } + + echo "
$license_statement
"; echo ''; diff --git a/includes/shareable-content.php b/includes/shareable-content.php index f2d6154..6f87a33 100644 --- a/includes/shareable-content.php +++ b/includes/shareable-content.php @@ -66,6 +66,7 @@ */ $license_statement = wp_kses_post( get_option( 'republication_tracker_tool_policy' ) ); $license_key = get_option( 'republication_tracker_tool_license', REPUBLICATION_TRACKER_TOOL_DEFAULT_LICENSE ); +$using_license = isset( REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ] ); echo '
'; echo '
'; // .cc-policy // what we display to the embedder echo '
'; diff --git a/republication-tracker-tool.php b/republication-tracker-tool.php index 0fd3be0..a3a9797 100644 --- a/republication-tracker-tool.php +++ b/republication-tracker-tool.php @@ -276,7 +276,7 @@ public static function create_additional_tracking_code_markup( $post_id ) { } /** - * Get attribution text, which will be inserted at the end of the copyable content. + * Get attribution text and tracking code to be added to the content footer * * @param $post The shared post. */ @@ -286,35 +286,94 @@ public static function create_content_footer( $post = null ) { $additional_tracking_code = self::create_additional_tracking_code_markup( $post->ID ); $tracking_html = htmlentities( $pixel ) . htmlentities( $parsely_tracking ) . htmlentities( $additional_tracking_code ); - $license_key = get_option( 'republication_tracker_tool_license', 'cc-by-nd-4.0' ); - $license_url = REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['url']; - $license_description = REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['description']; - $display_attribution = get_option( 'republication_tracker_tool_display_attribution', 'on' ); + + $attribution = ''; + if ( 'on' === $display_attribution && null !== $post ) { - $site_icon_markup = ''; - $site_icon_url = get_site_icon_url( 150 ); - if ( ! empty( $site_icon_url ) ) { - $site_icon_markup = sprintf( - '', - esc_attr( $site_icon_url ), - ); - } + $attribution = self::get_attribution( $post ); + } + + return $attribution . $tracking_html; + } + + /** + * Get attribution text and tracking code to be added to the content footer + * + * @param \WP_Post $post The shared post. + * @param bool $plain_text Whether to return plain text. + */ + public static function get_attribution( \WP_Post $post, $plain_text = false ) { + $license_key = get_option( 'republication_tracker_tool_license', 'cc-by-nd-4.0' ); - return wpautop( - sprintf( - // translators: %1$s is a URL, %2$s is the site home URL, %3$s is the site title. %4$s is the license URL, %5$s is the license description. - esc_html__( 'This article first appeared on %3$s and is republished here under a %5$s.', 'republication-tracker-tool' ), - get_permalink( $post ), - home_url(), + $site_icon_markup = ''; + $site_icon_url = get_site_icon_url( 150 ); + if ( ! empty( $site_icon_url ) ) { + $site_icon_markup = sprintf( + '', + esc_attr( $site_icon_url ), + ); + } + + if ( isset( REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ] ) ) { + $license_url = REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['url']; + $license_description = REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['description']; + + if ( $plain_text ) { + $attribution = sprintf( + // translators: %1$s is the site title, %2$s is the license description. + esc_html__( 'This article first appeared on %1$s and is republished here under a %2$s.', 'republication-tracker-tool' ), esc_html( get_bloginfo() ), - $license_url, $license_description - ) . htmlentities( $site_icon_markup ) . $tracking_html - ); + ); + } else { + $attribution = wpautop( + sprintf( + // translators: %1$s is a URL, %2$s is the site home URL, %3$s is the site title. %4$s is the license URL, %5$s is the license description. + esc_html__( 'This article first appeared on %3$s and is republished here under a %5$s.', 'republication-tracker-tool' ), + get_permalink( $post ), + home_url(), + esc_html( get_bloginfo() ), + $license_url, + $license_description + ) . htmlentities( $site_icon_markup ) + ); + } + + + } else { + + if ( $plain_text ) { + $attribution = sprintf( + // translators: %s is the site title. + esc_html__( 'This article first appeared on %s.', 'republication-tracker-tool' ), + esc_html( get_bloginfo() ) + ); + } else { + + $attribution = wpautop( + sprintf( + // translators: %1$s is a URL, %2$s is the site home URL, %3$s is the site title. + esc_html__( 'This article first appeared on %3$s.', 'republication-tracker-tool' ), + get_permalink( $post ), + home_url(), + esc_html( get_bloginfo() ), + ) . htmlentities( $site_icon_markup ) + ); + } } - return $tracking_html; + + /** + * Filters the attribution HTML for the given post. + * + * @param string $attribution The attribution HTML. + * @param \WP_Post $post The post object. + * @param bool $plain_text Whether the attribution is plain text. + */ + return apply_filters( 'republication_tracker_tool_attribution', $attribution, $post, $plain_text ); + } + } /** diff --git a/templates/republish-template.php b/templates/republish-template.php index 66ad738..093dbb8 100644 --- a/templates/republish-template.php +++ b/templates/republish-template.php @@ -24,12 +24,17 @@ $license_statement = get_option( 'republication_tracker_tool_policy' ); $license_key = get_option( 'republication_tracker_tool_license', REPUBLICATION_TRACKER_TOOL_DEFAULT_LICENSE ); -$license_badge = sprintf( - '%s', - REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['url'], - REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['description'], - REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['badge'] -); + +$using_license = isset( REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ] ); + +if ( $using_license ) { + $license_badge = sprintf( + '%s', + REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['url'], + REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['description'], + REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['badge'] + ); +} // Article title. $article_title = get_the_title( $republish_post_id ); @@ -177,21 +182,23 @@
-
- %2$s.', 'republication-tracker-tool' ), - REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['url'], - REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['description'], + +
+ %2$s.', 'republication-tracker-tool' ), + REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['url'], + REPUBLICATION_TRACKER_TOOL_LICENSES[ $license_key ]['description'], + ) ) - ) - ); - ?> -
+ ); + ?> +
+