views
1 week ago
BlockSettings.php
1 week ago
Notice.php
1 week ago
PluginCompatibility.php
1 week ago
PluginCompatibilityChecker.php
1 week ago
PluginDetails.php
1 week ago
BlockSettings.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Block Settings |
| 5 | * |
| 6 | * @package WPDesk\FS\Compatibility |
| 7 | */ |
| 8 | namespace FSVendor\WPDesk\FS\Compatibility; |
| 9 | |
| 10 | use WC_Shipping_Zones; |
| 11 | use FSVendor\WPDesk_Flexible_Shipping; |
| 12 | /** |
| 13 | * Can prevent Flexible Shipping settings saving. |
| 14 | */ |
| 15 | class BlockSettings |
| 16 | { |
| 17 | /** |
| 18 | * @var PluginCompatibilityChecker . |
| 19 | */ |
| 20 | private $plugin_compatibility_checker; |
| 21 | /** |
| 22 | * Notice constructor. |
| 23 | * |
| 24 | * @param PluginCompatibilityChecker $plugin_compatibility_checker . |
| 25 | */ |
| 26 | public function __construct(PluginCompatibilityChecker $plugin_compatibility_checker) |
| 27 | { |
| 28 | $this->plugin_compatibility_checker = $plugin_compatibility_checker; |
| 29 | } |
| 30 | /** |
| 31 | * Add hooks. |
| 32 | */ |
| 33 | public function hooks() |
| 34 | { |
| 35 | add_action('admin_init', array($this, 'block_save_settings')); |
| 36 | add_action('flexible-shipping/method-rules-settings/table/before', array($this, 'add_flexible_shipping_method_message')); |
| 37 | add_action('flexible_shipping_method_script', array($this, 'add_flexible_shipping_method_message')); |
| 38 | } |
| 39 | /** |
| 40 | * Action when setting are saving. |
| 41 | */ |
| 42 | public function block_save_settings() |
| 43 | { |
| 44 | if (!isset($_POST['save'], $_POST['settings_saving_block'])) { |
| 45 | return; |
| 46 | } |
| 47 | $tab = filter_input(\INPUT_GET, 'tab'); |
| 48 | $page = filter_input(\INPUT_GET, 'page'); |
| 49 | if ('wc-settings' !== $page || 'shipping' !== $tab) { |
| 50 | return; |
| 51 | } |
| 52 | $_wpnonce = filter_input(\INPUT_POST, '_wpnonce'); |
| 53 | if (!wp_verify_nonce(wp_unslash($_wpnonce), 'woocommerce-settings')) { |
| 54 | return; |
| 55 | } |
| 56 | $method_title = filter_input(\INPUT_POST, 'woocommerce_flexible_shipping_method_title'); |
| 57 | $instance_id = absint(wp_unslash(filter_input(\INPUT_GET, 'instance_id'))); |
| 58 | if (!$method_title || !$instance_id) { |
| 59 | return; |
| 60 | } |
| 61 | $shipping_method = WC_Shipping_Zones::get_shipping_method($instance_id); |
| 62 | if (!$shipping_method) { |
| 63 | return; |
| 64 | } |
| 65 | wp_die($this->get_general_message(), '', array('link_url' => $this->get_plugin_update_url(), 'link_text' => $this->get_plugin_update_label(), 'back_link' => \true)); |
| 66 | // WPCS: XSS OK. |
| 67 | } |
| 68 | /** |
| 69 | * Add scripts for FS method. |
| 70 | */ |
| 71 | public function add_flexible_shipping_method_message() |
| 72 | { |
| 73 | $status = (bool) apply_filters('plugin_compatibility_checker/js_added', \false); |
| 74 | if ($status) { |
| 75 | return; |
| 76 | } |
| 77 | $action = filter_input(\INPUT_GET, 'action'); |
| 78 | if (!$action) { |
| 79 | return; |
| 80 | } |
| 81 | add_filter('plugin_compatibility_checker/js_added', '__return_true'); |
| 82 | $message = $this->get_message_for_settings(); |
| 83 | include wp_normalize_path(__DIR__ . '/views/html-update-settings-alert.php'); |
| 84 | } |
| 85 | /** |
| 86 | * Message for incompatible plugins. |
| 87 | * |
| 88 | * @return string |
| 89 | */ |
| 90 | private function get_message_for_settings() |
| 91 | { |
| 92 | return sprintf('%s %s', $this->get_general_message(), sprintf('%s%s%s', sprintf('<a target="_blank" href="%s">', $this->get_plugin_update_url()), $this->get_plugin_update_label(), '</a>')); |
| 93 | } |
| 94 | /** |
| 95 | * @return string |
| 96 | */ |
| 97 | private function get_general_message() |
| 98 | { |
| 99 | $plugins = implode(', ', $this->plugin_compatibility_checker->get_list_of_incompatible_plugins()); |
| 100 | return sprintf(__('In order to prevent any further issues with the plugin configuration or its proper functioning, before saving the changes please update the following: %s.', 'flexible-shipping'), sprintf('<strong>%s</strong>', $plugins)); |
| 101 | } |
| 102 | /** |
| 103 | * @return string |
| 104 | */ |
| 105 | private function get_plugin_update_label() |
| 106 | { |
| 107 | return __('Go to the plugins list →', 'flexible-shipping'); |
| 108 | } |
| 109 | /** |
| 110 | * @return string |
| 111 | */ |
| 112 | private function get_plugin_update_url() |
| 113 | { |
| 114 | return add_query_arg('s', 'Flexible Shipping', admin_url('plugins.php')); |
| 115 | } |
| 116 | } |
| 117 |