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

[Publish Now] Use account active state as default value for toggle check #908

Merged
merged 5 commits into from
Feb 12, 2024
Merged
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
17 changes: 12 additions & 5 deletions includes/admin/class-rop-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -799,17 +799,21 @@ public function publish_now_attributes( $default ) {
$default['action'] = 'yes' === get_post_meta( $post->ID, 'rop_publish_now', true );
$default['instant_share_by_default'] = $default['action'];
}
$default['active'] = get_post_meta( $post->ID, 'rop_publish_now_accounts', true );
$default['page_active_accounts'] = get_post_meta( $post->ID, 'rop_publish_now_accounts', true );

return $default;
}

/**
* Publish now, if enabled.
*
* This is hooked to the `save_post` action.
* The values from the Publish Now metabox are saved to the post meta.
*
* @param int $post_id The post ID.
*/
public function maybe_publish_now( $post_id ) {

if ( empty( $_POST['rop_publish_now_nonce'] ) ) {
return;
}
Expand Down Expand Up @@ -851,19 +855,22 @@ public function maybe_publish_now( $post_id ) {
// reject the extra.
$enabled = array_diff( $enabled, $extra );

$instant_share_custom_content = array();
/**
* Save an account as active to instant share via its ID along with the custom message in the post meta.
*/
$publish_now_active_accounts_settings = array();

foreach ( $enabled as $account_id ) {
$custom_message = ! empty( $_POST[ $account_id ] ) ? $_POST[ $account_id ] : '';
$instant_share_custom_content[ $account_id ] = $custom_message;
$publish_now_active_accounts_settings[ $account_id ] = $custom_message;
}

update_post_meta( $post_id, 'rop_publish_now', 'yes' );
update_post_meta( $post_id, 'rop_publish_now_accounts', $instant_share_custom_content );
update_post_meta( $post_id, 'rop_publish_now_accounts', $publish_now_active_accounts_settings );

// If user wants to run this operation on page refresh instead of via Cron.
if ( $settings->get_true_instant_share() ) {
$this->rop_cron_job_publish_now( $post_id, $instant_share_custom_content );
$this->rop_cron_job_publish_now( $post_id, $publish_now_active_accounts_settings );
return;
}

Expand Down
56 changes: 42 additions & 14 deletions vue/src/vue-elements/pro/publish-now.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
</template>

<script>
/**
* This component is responsible for rendering the "Publish Now" section of the Pro version of the plugin.
*
* The section is located in the "Publish" metabox of the post/page editor.
*/
import ButtonCheckbox from '../reusables/button-checkbox.vue'

export default {
Expand All @@ -91,7 +96,7 @@
choose_accounts_manually: this.$store.state.publish_now.choose_accounts_manually,
showField: fields,
toggle_accounts: this.$store.state.publish_now.instant_share_by_default,
active_accounts: this.$store.state.publish_now.active
page_active_accounts: this.$store.state.publish_now.page_active_accounts
}
},
computed: {
Expand All @@ -104,6 +109,12 @@
created() {
},
methods: {

/**
* Get the class for the Font Awesome icon of a social media service.
*
* @param {string} service - The slug of the social media service to get the icon for.
*/
getServiceClass: function (service) {
let serviceIcon = 'fa-'
if (service === 'facebook') serviceIcon = serviceIcon.concat('facebook')
Expand All @@ -117,29 +128,46 @@
return serviceIcon;
},

toggleServices: function(event, value){
/**
* Toggle the sharing feature for a specific account.
*
* @param {Event} event - The toggle event.
* @param {string} account_id - The ID of the account to toggle the services for.
*/
toggleServices: function(event, account_id){
var self = this;
if( event.target.checked ) {
return;
}

return self.showField[value] = false;
return self.showField[account_id] = false;
},

togglefields: function(value){
/**
* Toggle the custom share message field for a specific account.
*
* @param {string} account_id - The ID of the account to toggle the custom share message field for.
*/
togglefields: function(account_id) {
var self = this;
return self.showField[value] = ! self.showField[value];
},

containsKey(obj, key ) {
return Object.keys(obj).includes(key);
return self.showField[account_id] = ! self.showField[account_id];
},

isActiveAccount: function( key ) {
var self = this;
return this.containsKey(self.active_accounts, key);

/**
*
* Check if the account is active for the page.
*
* @param {string} account_id - The ID of the account to check the active state for.
*/
isActiveAccount: function( account_id ) {

// If the active accounts for the page are set, check if the account is active for the page.
if ( this.page_active_accounts ) {
return {...(this.page_active_accounts ?? {})}?.hasOwnProperty(account_id);
}

return ! this.choose_accounts_manually;
},

}
}
</script>
Expand Down
Loading