Created
November 26, 2019 03:46
-
-
Save korynorthrop/774bc6f7a5ef4f4c038f72ff24673a00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Interactions with MultilingualPress on the admin side ( hooks into the 'admin_enqueue_scripts' action ) | |
* | |
* @var {Array} multilingualpress_settings Array of variables passed to this script via wp_localize_script | |
*/ | |
wp.domReady( () => { | |
// If editing a post in the main site language | |
if ( multilingualpress_settings && multilingualpress_settings.isPostEditor == 'true' && multilingualpress_settings.currentBlogID == '1' ) { | |
const { subscribe } = wp.data; | |
// const thePostID = wp.data.select( 'core/editor' ).getCurrentPostId(); | |
const isNewPost = wp.data.select( 'core/editor' ).isCleanNewPost(); // if this is a new post being created | |
const sites = multilingualpress_settings.siteIDs; // the multisite subsite IDs passed from wp_localize_script | |
// if it's a new post, connect all subsites to this post | |
if ( isNewPost ) { | |
// loop through each subsite and duplicate the new post's content/title/etc. | |
Array.prototype.forEach.call( sites, function( id, i ) { | |
// choose to create a new translated post and connect it to this source post | |
let duplicateRadioOption = document.querySelector( `#multilingualpress-site-${id}-relationship-new` ); | |
duplicateRadioOption.click(); | |
// choose to copy the source content to the translated post | |
let copyContent = document.querySelector( `#multilingualpress-site-${id}-remote-content-copy` ); | |
copyContent.click(); | |
// choose to copy the source featured image to the translated post | |
let copyFeaturedImg = document.querySelector( `#multilingualpress-site-${id}-remote-thumbnail-copy` ); | |
copyFeaturedImg.click(); | |
// choose to copy the source taxonomies to the translated post | |
let copyTaxonomies = document.querySelector( `#multilingualpress-site-${id}-remote-taxonomies-copy` ); | |
copyTaxonomies.click(); | |
} ); | |
} | |
// Loop through the items of a select menu and return the appropriate value | |
function changeSelectMenuOption( selectMenu, valsearch ) { | |
for ( i = 0; i< selectMenu.options.length; i++ ) { | |
if ( selectMenu.options[i].value == valsearch ) { | |
selectMenu.options[i].selected = true; // Item is found. Set its property and exit | |
break; | |
} | |
} | |
return; | |
} | |
// listen for changes to the source post's publish status and sync the translate posts' status | |
const unssubscribe = subscribe( () => { | |
const currentPostStatus = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'status' ); // get status | |
if ( 'auto-draft' === currentPostStatus ) return; // do nothing if auto draft | |
// if the status changes to be published or a saved draft then sync the status of its translated posts | |
if ( 'publish' === currentPostStatus || 'draft' === currentPostStatus ) { | |
Array.prototype.forEach.call( sites, function( id, i ) { | |
let translatedPostStatus = document.querySelector( `#multilingualpress-site-${id}-remote-status` ); | |
changeSelectMenuOption( translatedPostStatus, currentPostStatus ); | |
} ); | |
} | |
} ); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment