<?php
/**
* The Events Calendar + Community Events Tickets: Force Global Stock to be
* checked and then hidden (to avoid unchecking), only on the Add New form.
*
* Because this does not apply to the Edit Event form, you may want to modify
* this code to your needs, such as hiding the checkbox even on the Edit form
* but not actually forcing it checked (to avoid affecting events prior to
* implementing this snippet.
*
* @link https://gist.github.com/cliffordp/c483a67a35f7032f586c6813083a998c
*/
add_action( 'wp_footer', 'cliff_community_tickets_force_global_stock' );
function cliff_community_tickets_force_global_stock() {
if ( false === cliff_is_community_add_new_form() ) {
return;
}
wp_enqueue_script( 'jquery' );
?>
<script type="text/javascript">
jQuery( document ).ready( function () {
jQuery( 'body.tribe_community_edit #tribe-global-stock-settings input#tribe-tickets-enable-global-stock' ).prop( 'checked', true ).hide();
jQuery( 'body.tribe_community_edit #tribe-global-stock-settings label[for="tribe-tickets-enable-global-stock"]' ).hide();
jQuery( 'body.tribe_community_edit #tribe-global-stock-settings #tribe-tickets-global-stock-level' ).show();
} );
</script>
<?php
}
/**
* Return TRUE if we are in the Community Events Add Event form. Return FALSE
* if we are in the Edit form or Events List view.
*
* @return bool
*/
function cliff_is_community_add_new_form() {
$main = tribe( 'community.main' );
if ( empty( $main ) ) {
// Community Events is not active (or may somehow not be loaded correctly)
return false;
}
$event_post_type = Tribe__Events__Main::POSTTYPE;
$action = Tribe__Utils__Array::get( $main->context, 'action' );
$ce_post_type = Tribe__Utils__Array::get( $main->context, 'post_type', $event_post_type ); // assume event post type if not set
// bail if we are not doing what is expected from the start
if ( $event_post_type !== $ce_post_type ) {
return false;
}
if ( 'add' === $action ) {
return true;
} else {
// 'edit' for Edit form
// or, if empty, you might be in the Community Events List view
return false;
}
}