Skip to content

Instantly share code, notes, and snippets.

@gabriserra
Created March 24, 2019 21:13
Show Gist options
  • Save gabriserra/aada445477d83884803995f57462b796 to your computer and use it in GitHub Desktop.
Save gabriserra/aada445477d83884803995f57462b796 to your computer and use it in GitHub Desktop.
Permit to close a Woocommerce store, enabling the use of a message to warn users.
<?php
/*
Plugin Name: Woo Close Store
Plugin URI: https://github.com/gabriserra/woo-close-store
Description: Woo Close Store handle store closing phase.
Version: 0.1
Author: Gabriele Serra
Author URI: https://gabripr0.altervista.org
Text Domain: woo-close-store
License: MIT
License URI: https://www.linux.it/scegli-una-licenza/licenses/mit/
*/
/**
* A cutted version of Woo Store Vacation by Mahdi Yazdani
* @see https://wordpress.org/plugins/woo-store-vacation/
*/
/**
* Close Store Class
*
* Handle Woocommerce stuff
*
*/
defined( 'ABSPATH' ) || exit;
class Woo_Close_Store {
/**
* Bootstraps the class and hooks required actions & filters.
*/
public static function init() {
add_action('admin_notices', __CLASS__ . '::is_woocommerce_present' , 10);
add_action('admin_menu', __CLASS__ . '::add_woosubmenu_page' , 99);
add_action('admin_init', __CLASS__ . '::register_settings' , 10);
add_action('init', __CLASS__ . '::closed_store', 10);
}
/**
* Check if WooCommerce plugin is installed
*/
public static function is_woocommerce_present() {
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins'))))
printf('<div class="notice notice-error is-dismissible"><p>%s</p></div>', esc_html__('Woocommerce non è attivo!', 'woo-close-store'));
}
/**
* Create options and page under Woocommerce menu
*/
public static function add_woosubmenu_page() {
add_submenu_page( 'woocommerce', 'Woo Close Store', 'Chiudi negozio', 'manage_woocommerce', 'woo-close-store', __CLASS__ . '::render_woosubmenu_page');
}
/**
* Register plugin settings
*/
public static function register_settings() {
register_setting('woo_close_store_settings_fields', 'woo_close_store_options', __CLASS__ . '::sanitize');
add_settings_section('woo_close_store_settings_section', '', '', 'woo_close_store_settings_sections');
add_settings_field('closed_store', 'Imposta chiusura store.' , __CLASS__ . '::closed_store_callback' , 'woo_close_store_settings_sections', 'woo_close_store_settings_section');
add_settings_field('closed_message', 'Messaggio per gli utenti <abbr class="required" title="required">*</abbr>', __CLASS__ . '::closed_message_callback', 'woo_close_store_settings_sections', 'woo_close_store_settings_section');
}
/**
* Sanitization Callback
*/
private static function sanitize($input) {
$sanitary_values = array();
if (isset($input['closed_store']))
$sanitary_values['closed_store'] = $input['closed_store'];
if (isset($input['closed_message']))
$sanitary_values['closed_message'] = esc_textarea($input['closed_message']);
return $sanitary_values;
}
/**
* Closed store mode
*/
public static function closed_store_callback() {
$options = get_option('woo_close_store_options');
if (!isset($options['closed_store']))
$closed_mode = '';
else if ($options['closed_store'] === 'closed_store')
$closed_mode = 'checked';
printf('<input type="checkbox" name="woo_close_store_options[closed_store]" id="closed_store" value="closed_store" %s /> <label for="closed_store"><em><small>Vuoi impostare la modalità chiusura?</small></em></label>', $closed_mode);
}
/**
* Notice Content.
*/
public static function closed_message_callback() {
$options = get_option('woo_close_store_options');
if (!isset($options['closed_message']))
$message = '';
else
$message = esc_attr($options['closed_message']);
$placeholder = 'Il servizio di delivery è momentaneamente chiuso per manutenzione. Torniamo prima possibile!';
printf('<textarea class="large-text" rows="5" name="woo_close_store_options[closed_message]" id="closed_message" placeholder="%s">%s</textarea>', $placeholder, $message);
}
/**
* Retrieve plugin option value(s)
*/
public static function closed_store() {
$options = get_option('woo_close_store_options');
if (!isset($options['closed_store'])) {
update_option( 'woocommerce_demo_store', 'no');
return;
}
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
remove_action('woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20);
remove_action('woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20);
update_option( 'woocommerce_demo_store_notice', $options['closed_message'] );
update_option( 'woocommerce_demo_store', 'yes');
}
/**
* Override Woocommerce notice message.
*/
public static function wc_custom_store_notice_updated( $text ) {
$options = get_option('woo_close_store_options');
$text = $options['closed_message'];
return $text;
}
/**
* Render and display plugin options page.
*/
public static function render_woosubmenu_page() {
?>
<div class="wrap">
<h1><?php esc_html_e('Woo Close Store', 'woo-close-store'); ?></h1>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-1">
<?php settings_errors(); ?>
<!-- main content -->
<div id="post-body-content">
<form method="POST" id="woo-close-store" autocomplete="off" action="options.php">
<div class="meta-box-sortables ui-sortable">
<div class="postbox">
<h2 class="hndle">
<span>
<?php esc_attr_e('Impostazioni', 'woo-close-store'); ?>
</span>
</h2>
<div class="inside">
<?php
// Get settings fields
settings_fields('woo_close_store_settings_fields');
do_settings_sections('woo_close_store_settings_sections');
?>
</div>
<!-- .inside -->
</div>
<!-- .postbox -->
</div>
<!-- .meta-box-sortables .ui-sortable -->
<?php submit_button(); ?>
</form>
</div>
<!-- post-body-content -->
</div>
<!-- #post-body .metabox-holder .columns-1 -->
<br class="clear">
</div>
<!-- #poststuff -->
</div> <!-- .wrap -->
<?php
}
}
Woo_Close_Store::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment