Skip to content

Commit 158658e

Browse files
manzoorwanijkmatticbot
authored andcommitted
Social | Add resharing for classic editor (#37810)
* Social | Add resharing for classic editor * Fix up versions * Ensure that republicize feature is available * Fix up versions * Use connection_id instead of id for skipped connections * Fix up versions Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/9578267633 Upstream-Ref: Automattic/jetpack@fd1471d
1 parent dbdc84f commit 158658e

File tree

5 files changed

+102
-8
lines changed

5 files changed

+102
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
This is an alpha version! The changes listed here are not final.
1111

12+
### Added
13+
- Added Social resharing for classic editor
14+
1215
### Changed
1316
- Removed unneeded check for connection management wpcom
1417

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('jquery', 'wp-i18n'), 'version' => '1ec19d7b9335a0fb05a5');
1+
<?php return array('dependencies' => array('jquery', 'wp-api-fetch', 'wp-i18n'), 'version' => '0ebde3d731b1f531ea1f');

build/classic-editor-connections.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/class-publicize-ui.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Automattic\Jetpack\Publicize;
99

1010
use Automattic\Jetpack\Assets;
11+
use Automattic\Jetpack\Current_Plan;
1112

1213
/**
1314
* Only user facing pieces of Publicize are found here.
@@ -178,6 +179,8 @@ public function post_page_metabox_assets() {
178179
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
179180
'connectionsUrl' => esc_url( $this->publicize_settings_url ),
180181
'isEnhancedPublishingEnabled' => $this->publicize->has_enhanced_publishing_feature(),
182+
'resharePath' => '/jetpack/v4/publicize/{postId}',
183+
'isReshareSupported' => Current_Plan::supports( 'republicize' ),
181184
)
182185
),
183186
'before'
@@ -592,17 +595,17 @@ private function get_metabox_form_connected( $connections_data ) {
592595
<li>
593596
<label
594597
for="wpas-submit-<?php echo esc_attr( $connection_data['id'] ); ?>"
595-
<?php echo ! $connection_data['toggleable'] ? 'class="wpas-disabled"' : ''; ?>
596598
>
597599
<input
598600
type="checkbox"
599601
name="wpas[submit][<?php echo esc_attr( $connection_data['id'] ); ?>]"
600602
id="wpas-submit-<?php echo esc_attr( $connection_data['id'] ); ?>"
601603
class="wpas-submit-<?php echo esc_attr( $connection_data['service_name'] ); ?>"
602604
value="1"
605+
data-id="<?php echo esc_attr( $connection_data['id'] ); ?>"
603606
<?php
604607
checked( true, $connection_data['enabled'] && $connection_healthy );
605-
disabled( false, $connection_data['toggleable'] && $connection_healthy );
608+
disabled( false, $connection_healthy );
606609
?>
607610
/>
608611
<?php if ( $connection_data['enabled'] && $connection_healthy && ! $connection_data['toggleable'] ) : // Need to submit a value to force a global connection to POST. ?>
@@ -627,6 +630,8 @@ class="wpas-submit-<?php echo esc_attr( $connection_data['service_name'] ); ?>"
627630

628631
$is_social_note = 'jetpack-social-note' === get_post_type( $post->ID );
629632

633+
$is_post_published = 'publish' === get_post_status( $post->ID );
634+
630635
$all_done = $all_done || $all_connections_done;
631636

632637
?>
@@ -636,10 +641,16 @@ class="wpas-submit-<?php echo esc_attr( $connection_data['service_name'] ); ?>"
636641
<?php if ( ! $is_social_note ) : ?>
637642
<label for="wpas-title"><?php esc_html_e( 'Custom Message:', 'jetpack-publicize-pkg' ); ?></label>
638643
<span id="wpas-title-counter" class="alignright hide-if-no-js">0</span>
639-
<textarea name="wpas_title" id="wpas-title"<?php disabled( $all_done ); ?>><?php echo esc_textarea( $title ); ?></textarea>
644+
<textarea name="wpas_title" id="wpas-title"><?php echo esc_textarea( $title ); ?></textarea>
640645
<a href="#" class="hide-if-no-js button" id="publicize-form-hide"><?php esc_html_e( 'OK', 'jetpack-publicize-pkg' ); ?></a>
641646
<input type="hidden" name="wpas[0]" value="1" />
642647
<?php endif; ?>
648+
<?php if ( $is_post_published && Current_Plan::supports( 'republicize' ) ) : ?>
649+
<button type="button" class="hide-if-no-js button" id="publicize-share-now">
650+
<?php esc_html_e( 'Share now', 'jetpack-publicize-pkg' ); ?>
651+
</button>
652+
<span id="publicize-share-now-notice" class="hidden"></span>
653+
<?php endif; ?>
643654
</div>
644655

645656
<div id="pub-connection-needs-media"></div>

src/js/classic-editor-connections.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import apiFetch from '@wordpress/api-fetch';
12
import { _n, __ } from '@wordpress/i18n';
23
import jQuery from 'jquery';
34

4-
const { ajaxUrl, connectionsUrl, isEnhancedPublishingEnabled } =
5+
const { ajaxUrl, connectionsUrl, isEnhancedPublishingEnabled, resharePath, isReshareSupported } =
56
window.jetpackSocialClassicEditorOptions;
67
const CONNECTIONS_NEED_MEDIA = [ 'instagram-business' ];
78

@@ -158,4 +159,83 @@ jQuery( function ( $ ) {
158159
if ( $( '#pub-connection-tests' ).length ) {
159160
publicizeConnTestStart();
160161
}
162+
163+
//#region Share post NOW
164+
const shareNowButton = $( '#publicize-share-now' );
165+
const shareNowNotice = $( '#publicize-share-now-notice' );
166+
const publicizeForm = $( '#publicize-form' );
167+
const connections = publicizeForm.find( 'li input[type="checkbox"]' );
168+
169+
const showNotice = ( text, type = 'warning' ) => {
170+
shareNowNotice
171+
.removeClass( 'notice-warning notice-success hidden' )
172+
.addClass( 'publicize__notice-warning notice-' + type )
173+
.text( text );
174+
};
175+
176+
const hideNotice = () => {
177+
shareNowNotice.removeClass( 'publicize__notice-warning' ).addClass( 'hidden' ).text( '' );
178+
};
179+
180+
const getEnabledConnections = () => {
181+
return connections.filter( ( index, element ) => {
182+
return $( element ).prop( 'checked' );
183+
} );
184+
};
185+
186+
const getDisabledConnections = () => {
187+
return connections.filter( ( index, element ) => {
188+
return ! $( element ).prop( 'checked' );
189+
} );
190+
};
191+
192+
shareNowButton.on( 'click', function ( e ) {
193+
e.preventDefault();
194+
195+
if ( ! isReshareSupported ) {
196+
return;
197+
}
198+
199+
hideNotice();
200+
201+
if ( ! getEnabledConnections().length ) {
202+
showNotice(
203+
__( 'Please select at least one connection to share with.', 'jetpack-publicize-pkg' )
204+
);
205+
return;
206+
}
207+
208+
const postId = $( 'input[name="post_ID"]' ).val();
209+
210+
const path = resharePath.replace( '{postId}', postId );
211+
212+
const skipped_connections = getDisabledConnections()
213+
.map( ( index, element ) => {
214+
return $( element ).data( 'id' );
215+
} )
216+
.toArray();
217+
218+
const message = $( 'textarea[name="wpas_title"]' ).val();
219+
220+
shareNowButton.prop( 'disabled', true ).text( __( 'Sharing…', 'jetpack-publicize-pkg' ) );
221+
222+
apiFetch( {
223+
path,
224+
method: 'POST',
225+
data: {
226+
message,
227+
skipped_connections,
228+
},
229+
} )
230+
.then( () => {
231+
showNotice( __( 'Your post has been shared!', 'jetpack-publicize-pkg' ), 'success' );
232+
} )
233+
.catch( () => {
234+
showNotice( __( 'An error occurred while sharing your post.', 'jetpack-publicize-pkg' ) );
235+
} )
236+
.finally( () => {
237+
shareNowButton.prop( 'disabled', false ).text( __( 'Share now', 'jetpack-publicize-pkg' ) );
238+
} );
239+
} );
240+
//#endregion
161241
} );

0 commit comments

Comments
 (0)