| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
class ESHB_Metabox_Settings { |
| 4 |
|
| 5 |
protected $title_actions = array(); |
| 6 |
protected $screen; |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
add_action( 'add_meta_boxes', [$this, 'customize_meta_boxes'], 20 ); |
| 10 |
add_action( 'init', [$this, 'register_booking_post_statuses'], 5 ); |
| 11 |
add_action( 'admin_footer', [$this, 'title_actions_script'] ); |
| 12 |
add_action( 'admin_head-edit.php', [$this, 'disable_new_booking']); |
| 13 |
} |
| 14 |
|
| 15 |
function register_booking_post_statuses(){ |
| 16 |
|
| 17 |
$booking_statuses = ESHB_Helper::eshb_get_booking_statuses(); |
| 18 |
|
| 19 |
foreach ( $booking_statuses as $slug => $label ) { |
| 20 |
register_post_status( $slug, array( |
| 21 |
'label' => esc_html( $label ), |
| 22 |
'public' => true, |
| 23 |
'exclude_from_search' => false, |
| 24 |
'show_in_admin_all_list' => true, |
| 25 |
'show_in_admin_status_list' => true, |
| 26 |
/* translators: 1: booking count, 2: booking count */ |
| 27 |
'label_count' => _n_noop( |
| 28 |
'Booking <span class="count">(%s)</span>', |
| 29 |
'Booking <span class="count">(%s)</span>', |
| 30 |
'easy-hotel' |
| 31 |
), |
| 32 |
) ); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
// remove unnecessary thirdparty metaboxes |
| 37 |
function customize_meta_boxes() { |
| 38 |
remove_meta_box( 'slider_revolution_metabox', ['eshb_booking', 'eshb_accomodation', 'eshb_service', 'eshb_session', 'eshb_coupon', 'eshb_booking_request', 'eshb_payment', 'eshb_email_template'], 'side' ); // Custom Fields meta box |
| 39 |
remove_meta_box( 'astra_settings_meta_box', ['eshb_booking', 'eshb_accomodation', 'eshb_service', 'eshb_session', 'eshb_coupon', 'eshb_booking_request', 'eshb_payment', 'eshb_email_template'], 'side' ); // Custom Fields meta box |
| 40 |
|
| 41 |
remove_meta_box( 'submitdiv', ['eshb_booking'], 'side' ); // Custom Fields meta box |
| 42 |
add_meta_box( 'submitdiv', __( 'Update Booking', 'easy-hotel' ), [$this, 'render_submit_meta_box_booking'], 'eshb_booking', 'side', 'default' ); |
| 43 |
|
| 44 |
$plugin_name = 'EHB Manual Booking'; |
| 45 |
$plugin_slug = 'ehb-manual-booking'; |
| 46 |
$plugin_url = 'https://themewant.com/downloads/'.$plugin_slug; |
| 47 |
$plugin_main_file = $plugin_slug . '/' . $plugin_slug . '.php'; |
| 48 |
|
| 49 |
if (! is_plugin_active( $plugin_main_file ) ) { |
| 50 |
remove_meta_box( 'submitdiv', ['eshb_payment'], 'side' ); // Custom Fields meta box |
| 51 |
add_meta_box( 'submitdiv', __( 'Update Payment', 'easy-hotel' ), [$this, 'render_submit_meta_box_payment'], 'eshb_payment', 'side', 'default' ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public function render_submit_meta_box_booking( $post, $metabox ) { |
| 56 |
$postTypeObject = get_post_type_object( 'eshb_booking' ); |
| 57 |
$can_publish = current_user_can( $postTypeObject->cap->publish_posts ); |
| 58 |
$postStatus = get_post_status( $post->ID ); |
| 59 |
$statuses = ESHB_Helper::eshb_get_booking_statuses(); |
| 60 |
$post_date = date_i18n( 'F j, Y g:i a', strtotime($post->post_date) ); |
| 61 |
?> |
| 62 |
<div class="submitbox" id="submitpost"> |
| 63 |
<div id="minor-publishing"> |
| 64 |
<div id="minor-publishing-actions"> |
| 65 |
</div> |
| 66 |
<div id="misc-publishing-actions"> |
| 67 |
<div class="misc-pub-section"> |
| 68 |
<label for="eshb_post_status"><?php echo esc_html__( 'Status:', 'easy-hotel' )?></label> |
| 69 |
<select name="post_status" id="eshb_post_status"> |
| 70 |
<?php foreach ( $statuses as $statusName => $statusDetails ) { ?> |
| 71 |
<option value="<?php echo esc_attr( $statusName ); ?>" <?php selected( $statusName, $postStatus ); ?>> |
| 72 |
<?php echo esc_html( $statusDetails ) ; ?> |
| 73 |
</option> |
| 74 |
<?php } ?> |
| 75 |
</select> |
| 76 |
</div> |
| 77 |
<div class="misc-pub-section"> |
| 78 |
<span><?php esc_html_e( 'Created on:', 'easy-hotel' ); ?></span> |
| 79 |
<strong><?php echo esc_html($post_date) ; ?></strong> |
| 80 |
</div> |
| 81 |
</div> |
| 82 |
</div> |
| 83 |
<div id="major-publishing-actions"> |
| 84 |
<div id="delete-action"> |
| 85 |
<?php |
| 86 |
if ( current_user_can( 'delete_post', $post->ID ) ) { |
| 87 |
if ( ! EMPTY_TRASH_DAYS ) { |
| 88 |
$delete_text = __( 'Delete Permanently', 'easy-hotel' ); |
| 89 |
} else { |
| 90 |
$delete_text = __( 'Move to Trash', 'easy-hotel' ); |
| 91 |
} |
| 92 |
?> |
| 93 |
<a class="submitdelete deletion" href="<?php echo esc_url( get_delete_post_link( $post->ID ) ); ?>"><?php echo esc_html( $delete_text ); ?></a> |
| 94 |
<?php } ?> |
| 95 |
</div> |
| 96 |
<div id="publishing-action"> |
| 97 |
<span class="spinner"></span> |
| 98 |
<input name="original_publish" type="hidden" id="original_publish" value="<?php esc_attr_e( 'Update Booking', 'easy-hotel' ); ?>" /> |
| 99 |
<input name="save" type="submit" class="button button-primary button-large" id="publish" accesskey="p" value=" |
| 100 |
<?php |
| 101 |
in_array( $post->post_status, array( 'new', 'auto-draft' ) ) ? esc_attr_e( 'Create Booking', 'easy-hotel' ) : esc_attr_e( 'Update Booking', 'easy-hotel' ); |
| 102 |
?> |
| 103 |
" /> |
| 104 |
</div> |
| 105 |
<div class="clear"></div> |
| 106 |
</div> |
| 107 |
<p class="eshb-error-message eshb-text-danger"><?php echo esc_html__( 'Full up all required field!', 'easy-hotel' )?></p> |
| 108 |
</div> |
| 109 |
<?php |
| 110 |
} |
| 111 |
|
| 112 |
public function disable_new_booking(){ |
| 113 |
$screen = get_current_screen(); |
| 114 |
if ( $screen && in_array($screen->post_type, ['eshb_booking', 'eshb_payment']) ) { |
| 115 |
$plugin_name = 'EHB Manual Booking'; |
| 116 |
$plugin_slug = 'ehb-manual-booking'; |
| 117 |
$plugin_url = 'https://themewant.com/downloads/'.$plugin_slug; |
| 118 |
$plugin_main_file = $plugin_slug . '/' . $plugin_slug . '.php'; |
| 119 |
|
| 120 |
if (! is_plugin_active( $plugin_main_file ) ) { |
| 121 |
remove_submenu_page( 'edit.php?post_type=eshb_booking', 'post-new.php?post_type=eshb_booking' ); |
| 122 |
// Remove the "Add New" button on top of the list table |
| 123 |
if(in_array($screen->post_type, ['eshb_booking'])){ |
| 124 |
$this->modify_title_actions( __( 'New Booking', 'easy-hotel' ), '#', array( 'class' => 'button-disabled', 'after' => $this->eshb_upgrade_message($plugin_name, $plugin_url) ) ); |
| 125 |
}else{ |
| 126 |
$this->modify_title_actions( __( 'New Payment', 'easy-hotel' ), '#', array( 'class' => 'button-disabled', 'after' => $this->eshb_upgrade_message($plugin_name, $plugin_url) ) ); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public function render_submit_meta_box_payment ($post, $metabox){ |
| 133 |
$plugin_name = 'EHB Manual Booking'; |
| 134 |
$plugin_slug = 'ehb-manual-booking'; |
| 135 |
$plugin_url = 'https://themewant.com/downloads/'.$plugin_slug; |
| 136 |
echo esc_html($this->eshb_upgrade_message($plugin_name, $plugin_url, 'div')); |
| 137 |
} |
| 138 |
|
| 139 |
public function modify_title_actions( $label, $url, $options = array() ) { |
| 140 |
$this->title_actions[] = array( |
| 141 |
'label' => $label, |
| 142 |
'class' => 'eshb-page-title-action button ' . ( $options['class'] ?? '' ), |
| 143 |
'url' => $url, |
| 144 |
'after' => ! empty( $options['after'] ) ? ' ' . $options['after'] : '', |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
public function title_actions_script() { |
| 149 |
if ( empty( $this->title_actions ) ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
$actions = array_map( function( $action ) { |
| 154 |
$html = '<a href="' . esc_url( $action['url'] ) . '"'; |
| 155 |
$html .= ' class="' . esc_attr( $action['class'] ) . '">'; |
| 156 |
$html .= esc_html( $action['label'] ) . '</a>'; |
| 157 |
$html .= ! empty( $action['after'] ) ? wp_kses_post( $action['after'] ) : ''; |
| 158 |
return $html; |
| 159 |
}, $this->title_actions ); |
| 160 |
|
| 161 |
?> |
| 162 |
<script type="text/javascript"> |
| 163 |
jQuery( function( $ ) { |
| 164 |
var actions = <?php echo wp_json_encode( $actions ); ?>; |
| 165 |
var $heading = $( '#wpbody-content > .wrap > .wp-heading-inline' ); |
| 166 |
$('.page-title-action').remove(); |
| 167 |
if ( $heading.length ) { |
| 168 |
$heading.after( actions.join('') ); |
| 169 |
} |
| 170 |
}); |
| 171 |
</script> |
| 172 |
<?php |
| 173 |
} |
| 174 |
|
| 175 |
public static function eshb_upgrade_message( $plugin_name, $plugin_url, $wrapper = 'span', $wrapperClass = 'eshb-admin-notice eshb-admin-notice-small' ) { |
| 176 |
$message = sprintf( |
| 177 |
/* translators: 1: plugin URL, 2: plugin name */ |
| 178 |
__( 'Please activate <a href="%1$s" target="_blank">%2$s</a> extension to access this feature.', 'easy-hotel' ), |
| 179 |
esc_url( $plugin_url ), |
| 180 |
esc_html( $plugin_name ) |
| 181 |
); |
| 182 |
|
| 183 |
|
| 184 |
if ( ! empty( $wrapper ) ) { |
| 185 |
if ( $wrapper === 'div' ) { |
| 186 |
$message = '<div class="' . esc_attr( $wrapperClass ) . '">' . $message . '</div>'; |
| 187 |
}elseif($wrapper === 'p'){ |
| 188 |
$message = '<p class="' . esc_attr( $wrapperClass ) . '">' . $message . '</p>'; |
| 189 |
} else { |
| 190 |
$message = '<span class="' . esc_attr( $wrapperClass ) . '">' . $message . '</span>'; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return $message; |
| 195 |
} |
| 196 |
} |
| 197 |
new ESHB_Metabox_Settings(); |
| 198 |
|
| 199 |
add_action( 'admin_footer-post.php', 'eshb_add_all_custom_statuses_to_dropdown' ); |
| 200 |
add_action( 'admin_footer-post-new.php', 'eshb_add_all_custom_statuses_to_dropdown' ); |
| 201 |
|
| 202 |
function eshb_add_all_custom_statuses_to_dropdown() { |
| 203 |
global $post; |
| 204 |
if ( $post->post_type !== 'eshb_booking'|| $post->post_type !== 'eshb_payment' ) return; |
| 205 |
|
| 206 |
$statuses = ESHB_Helper::eshb_get_booking_statuses(); |
| 207 |
|
| 208 |
?> |
| 209 |
<script> |
| 210 |
jQuery(document).ready(function($){ |
| 211 |
<?php foreach ( $statuses as $slug => $label ) : ?> |
| 212 |
var selected = '<?php echo esc_js( $post->post_status ); ?>' === '<?php echo esc_js( $slug ); ?>' ? 'selected="selected"' : ''; |
| 213 |
$('#post_status').append('<option value="<?php echo esc_js( $slug ); ?>" ' + selected + '><?php echo esc_js( $label ); ?></option>'); |
| 214 |
<?php endforeach; ?> |
| 215 |
}); |
| 216 |
</script> |
| 217 |
<?php |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
// add custom order status |
| 222 |
add_action('init', 'eshb_register_custom_order_status'); |
| 223 |
add_filter('wc_order_statuses', 'eshb_add_custom_order_status_to_woocommerce'); |
| 224 |
function eshb_register_custom_order_status() { |
| 225 |
register_post_status( 'wc-deposit-payment', array( |
| 226 |
'label' => _x( 'Deposit Payment', 'Order status', 'easy-hotel' ), |
| 227 |
'public' => true, |
| 228 |
'exclude_from_search' => false, |
| 229 |
'show_in_admin_all_list' => true, |
| 230 |
'show_in_admin_status_list' => true, |
| 231 |
/* translators: 1: depoist payment count, 2: depoist payment count */ |
| 232 |
'label_count' => _n_noop( |
| 233 |
'Deposit Payment <span class="count">(%s)</span>', |
| 234 |
'Deposit Payment <span class="count">(%s)</span>', |
| 235 |
'easy-hotel' |
| 236 |
), |
| 237 |
) ); |
| 238 |
} |
| 239 |
|
| 240 |
function eshb_add_custom_order_status_to_woocommerce($order_statuses) { |
| 241 |
// Place it after processing |
| 242 |
$new_order_statuses = array(); |
| 243 |
|
| 244 |
foreach ($order_statuses as $key => $status) { |
| 245 |
$new_order_statuses[$key] = $status; |
| 246 |
if ('wc-processing' === $key) { |
| 247 |
$new_order_statuses['wc-deposit-payment'] = __('Deposit Payment', 'easy-hotel'); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
return $new_order_statuses; |
| 252 |
} |
| 253 |
|
| 254 |
// allow payment for this status |
| 255 |
add_filter( 'wc_order_is_pending_status', 'eshb_enable_payment_for_custom_status' ); |
| 256 |
function eshb_enable_payment_for_custom_status( $statuses ) { |
| 257 |
$statuses[] = 'deposit-payment'; |
| 258 |
return $statuses; |
| 259 |
} |
| 260 |
add_filter( 'woocommerce_resend_order_emails_available', 'eshb_enable_resend_email_for_custom_status', 10, 1 ); |
| 261 |
function eshb_enable_resend_email_for_custom_status( $emails ) { |
| 262 |
$emails[] = 'customer_invoice'; // Invoice email with payment link |
| 263 |
return $emails; |
| 264 |
} |