Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add Syndication Links support #331

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
run: npm install

- name: Set the core version and plugins config
run: ./tests/bin/set-wp-config.js --core=${{ matrix.core.version }} --plugins=./${{ github.event.repository.name }},https://downloads.wordpress.org/plugin/classic-editor.1.6.1.zip,./tests/test-plugin
run: ./tests/bin/set-wp-config.js --core=${{ matrix.core.version }} --plugins=./${{ github.event.repository.name }},https://downloads.wordpress.org/plugin/classic-editor.1.6.1.zip,https://downloads.wordpress.org/plugin/syndication-links.4.4.20.zip,./tests/test-plugin

- name: Set up WP environment
run: npm run env:start
Expand Down
1 change: 1 addition & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"plugins": [
"https://downloads.wordpress.org/plugin/classic-editor.1.6.1.zip",
"https://downloads.wordpress.org/plugin/syndication-links.4.4.20.zip",
".",
"./tests/test-plugin"
],
Expand Down
62 changes: 62 additions & 0 deletions assets/js/admin-autoshare-for-twitter-classic-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,64 @@
}
}

function addSyndicatedLinks( data ) {
if ( ! data ) {
return;
}

// Get the Syndication URL inputs.
const syndicationUrlInputs = Array.from(
document.querySelectorAll( 'input[name="syndication_urls[]"]' )
);

// Bail if there are no Syndication URL inputs.
if ( ! syndicationUrlInputs.length ) {
return;
}

// Get the URLs from the status messages.
// We'll use these to compare and populate the Syndication URL inputs.
const statusMessagesUrls = Array.from(document.querySelectorAll('.autoshare-for-twitter-status-logs-wrapper a')).map( ( link ) => {
return link.getAttribute('href');
} );

// Get the existing URLs from the Syndication URL inputs.
const syndicationUrlInputsUrls = syndicationUrlInputs.map(
( input ) => {
return input.value;
}
);

// Get the Syndication URL list.
const syndicationUrlList = document.querySelector(
'.syndication_url_list ul'
);

statusMessagesUrls.forEach( ( url ) => {
// If the URL is already in the Syndication URL inputs, bail.
if ( syndicationUrlInputsUrls.includes( url ) ) {
return;
}

// Create the Syndication URL input list item.
const syndicationUrlInputListItem = document.createElement( 'li' );

// Create the Syndication URL input.
const syndicationUrlInput = document.createElement( 'input' );
syndicationUrlInput.classList.add( 'widefat' );
syndicationUrlInput.type = 'text';
syndicationUrlInput.name = 'syndication_urls[]';
syndicationUrlInput.value = url;

// Append the Syndication URL input to the Syndication URL list.
syndicationUrlInputListItem.appendChild( syndicationUrlInput );
syndicationUrlList.appendChild( syndicationUrlInputListItem );

// Add the URL to the Syndication URL inputs URLs so we don't repeat ourselves.
syndicationUrlInputsUrls.push( url );
} );
}

// Tweet Now functionality.
$('#tweet_now').on('click', function () {
$('#autoshare-for-twitter-error-message').html('');
Expand Down Expand Up @@ -272,9 +330,13 @@
(false === response.success &&
false === response.data.is_retweeted))
) {
console.log({ data: response.data })
$('.autoshare-for-twitter-status-logs-wrapper').html(
response.data.message
);

addSyndicatedLinks( response.data );

if (response.data.is_retweeted) {
$tweetText.val(''); // Reset the tweet text.
}
Expand Down
24 changes: 21 additions & 3 deletions includes/admin/post-transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,30 @@ function validate_response( $response ) {
);

} else {
$errors = $response->errors;
if ( empty( $response->errors ) && ! empty( $response->detail ) ) {
// Build error message based on response structure.
$errors = array();

if ( ! empty( $response->errors ) ) {
// Error response structure with errors array.
$errors = $response->errors;
} elseif ( ! empty( $response->detail ) ) {
// Error response structure with status and detail.
$errors = array(
(object) array(
'code' => $response->status,
'message' => $response->detail,
),
);
} else {
// Fallback for unknown error response structure.
$errors = array(
(object) array(
'code' => 0,
'message' => __( 'Unknown error occurred', 'autoshare-for-twitter' ),
),
);
}

$validated_response = new \WP_Error(
'autoshare_for_twitter_failed',
__( 'Something happened during Twitter update.', 'autoshare-for-twitter' ),
Expand Down Expand Up @@ -316,8 +331,11 @@ function update_autoshare_for_twitter_meta_from_response( $post_id, $data, $hand

/**
* Fires after the response from Twitter has been written as meta to the post.
*
* @param int $post_id The post ID
* @param array $tweet_meta The tweet meta array containing tweet status data
*/
do_action( 'autoshare_for_twitter_post_tweet_status_updated' );
do_action( 'autoshare_for_twitter_post_tweet_status_updated', $post_id, $tweet_meta );
}

/**
Expand Down
67 changes: 67 additions & 0 deletions includes/class-syndication-links.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Handles integration with the Syndication Links plugin.
* This class is loaded only if the Syndication Links plugin is active.
*
* @link https://wordpress.org/plugins/syndication-links
* @package TenUp\AutoshareForTwitter\Core
*/

namespace TenUp\AutoshareForTwitter\Core\Syndication;

use TenUp\AutoshareForTwitter\Utils;

/**
* Class Syndication_Links
*/
class Syndication_Links {

/**
* Initialize the hook.
*/
public static function init() {
// Hook for when tweet is posted and saved into post metadata.
add_action( 'autoshare_for_twitter_post_tweet_status_updated', [ __CLASS__, 'handle_syndication_after_tweet_status_updated' ], 10, 2 );
}

/**
* Handle syndication after tweet status meta is updated.
*
* @param int $post_id The post ID.
* @param array $tweet_meta The tweet meta array containing tweet status data.
*/
public static function handle_syndication_after_tweet_status_updated( $post_id, $tweet_meta ) {
if ( empty( $tweet_meta ) ) {
return;
}

// Handle both single and multiple tweet formats.
$tweets = isset( $tweet_meta['twitter_id'] ) ? array( $tweet_meta ) : $tweet_meta;

foreach ( $tweets as $tweet ) {
if ( 'published' === $tweet['status'] && ! empty( $tweet['twitter_id'] ) ) {
$uri = Utils\link_from_twitter( $tweet );

// Only add valid tweet URLs.
if ( self::is_valid_tweet_url( $uri ) ) {
\Syn_Meta::add_syndication_link( get_post_type( $post_id ), $post_id, $uri );
}
}
}
}

/**
* Check if URL is a valid tweet URL.
*
* @param string $url URL to check.
* @return bool
*/
private static function is_valid_tweet_url( $url ) {
return (
! empty( $url ) &&
false !== strpos( $url, '/status/' ) &&
strlen( $url ) > 30 &&
wp_http_validate_url( $url )
);
}
}
12 changes: 12 additions & 0 deletions includes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ function setup() {
require_once plugin_dir_path( AUTOSHARE_FOR_TWITTER ) . 'includes/class-twitter-api.php';
require_once plugin_dir_path( AUTOSHARE_FOR_TWITTER ) . 'includes/class-twitter-accounts.php';

// Initialize the Syndication Links integration if plugin is active.
add_action(
'plugins_loaded',
function() {
if ( class_exists( 'Syn_Meta' ) ) {
require_once plugin_dir_path( AUTOSHARE_FOR_TWITTER ) . 'includes/class-syndication-links.php';
\TenUp\AutoshareForTwitter\Core\Syndication\Syndication_Links::init();
}
},
99
);

\TenUp\AutoshareForTwitter\Admin\Assets\add_hook_callbacks();
\TenUp\AutoshareForTwitter\REST\add_hook_callbacks();

Expand Down
72 changes: 72 additions & 0 deletions src/js/AutoshareForTwitterPostStatusInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,77 @@ export function AutoshareForTwitterPostStatusInfo() {

const [ statusMessages, setStatusMessages ] = useState( messages );

// Syndication Links plugin support.
// This is all so that the Syndication Links plugin can show updates in real time with our status messages.
// In addition, this fixes an issue where a user might actually remove a syndicated link unintentionally when clicking "update" on the post.
function addSyndicatedLinks( data ) {
// No data, bail.
if ( ! data ) {
return;
}

const { message } = data;

// No messages, bail.
if ( ! message?.length ) {
return;
}

// Get the Syndication URL inputs.
const syndicationUrlInputs = Array.from(
document.querySelectorAll( 'input[name="syndication_urls[]"]' )
);

// Bail if there are no Syndication URL inputs.
if ( ! syndicationUrlInputs.length ) {
return;
}

// Get the URLs from the status messages.
// We'll use these to compare and populate the Syndication URL inputs.
const statusMessagesUrls = message
.map( ( entry ) => {
return entry.url;
} )
.filter( ( url ) => url );

// Get the existing URLs from the Syndication URL inputs.
const syndicationUrlInputsUrls = syndicationUrlInputs.map(
( input ) => {
return input.value;
}
);

// Get the Syndication URL list.
const syndicationUrlList = document.querySelector(
'.syndication_url_list ul'
);

statusMessagesUrls.forEach( ( url ) => {
// If the URL is already in the Syndication URL inputs, bail.
if ( syndicationUrlInputsUrls.includes( url ) ) {
return;
}

// Create the Syndication URL input list item.
const syndicationUrlInputListItem = document.createElement( 'li' );

// Create the Syndication URL input.
const syndicationUrlInput = document.createElement( 'input' );
syndicationUrlInput.classList.add( 'widefat' );
syndicationUrlInput.type = 'text';
syndicationUrlInput.name = 'syndication_urls[]';
syndicationUrlInput.value = url;

// Append the Syndication URL input to the Syndication URL list.
syndicationUrlInputListItem.appendChild( syndicationUrlInput );
syndicationUrlList.appendChild( syndicationUrlInputListItem );

// Add the URL to the Syndication URL inputs URLs so we don't repeat ourselves.
syndicationUrlInputsUrls.push( url );
} );
}

useSaveTwitterData();

const reTweetHandler = async () => {
Expand All @@ -54,6 +125,7 @@ export function AutoshareForTwitterPostStatusInfo() {
setTweetText( '' );
}
setStatusMessages( data );
addSyndicatedLinks( data );
setReTweet( false );
};

Expand Down
2 changes: 1 addition & 1 deletion tests/bin/set-wp-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require( 'fs' );

const path = `${ process.cwd() }/.wp-env.json`;

let config = fs.existsSync( path ) ? require( path ) : { plugins: [ '.', 'https://downloads.wordpress.org/plugin/classic-editor.zip' ] };
let config = fs.existsSync( path ) ? require( path ) : { plugins: [ '.', 'https://downloads.wordpress.org/plugin/classic-editor.zip', 'https://downloads.wordpress.org/plugin/syndication-links.zip' ] };

const args = {};
process.argv
Expand Down
Loading
Loading