| 1 |
<?php |
| 2 |
/** |
| 3 |
* POS Settings. |
| 4 |
* |
| 5 |
* @author Paul Kilmurray <paul@kilbot.com> |
| 6 |
* |
| 7 |
* @see http://wcpos.com |
| 8 |
* @package WCPOS\WooCommercePOS |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WCPOS\WooCommercePOS\Admin; |
| 12 |
|
| 13 |
use WCPOS\WooCommercePOS\API\V1\Logs; |
| 14 |
use WCPOS\WooCommercePOS\Services\Cloudflare_Detector; |
| 15 |
use WCPOS\WooCommercePOS\Services\Extensions as ExtensionsService; |
| 16 |
use WCPOS\WooCommercePOS\Services\Settings as SettingsService; |
| 17 |
use const WCPOS\WooCommercePOS\PLUGIN_NAME; |
| 18 |
use const WCPOS\WooCommercePOS\PLUGIN_URL; |
| 19 |
use const WCPOS\WooCommercePOS\TRANSLATION_VERSION; |
| 20 |
use const WCPOS\WooCommercePOS\VERSION; |
| 21 |
|
| 22 |
/** |
| 23 |
* Class Settings. |
| 24 |
*/ |
| 25 |
class Settings { |
| 26 |
/** |
| 27 |
* Settings constructor. |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 31 |
add_action( 'admin_head', array( $this, 'override_wpcontent_padding' ) ); |
| 32 |
add_action( 'in_admin_header', array( $this, 'remove_admin_notices' ) ); |
| 33 |
|
| 34 |
/* |
| 35 |
* Initializes the settings for WCPOS in the admin panel. |
| 36 |
* |
| 37 |
* This action hook can be used to run additional initialization routines |
| 38 |
* when the WCPOS settings are being set up in the admin panel. |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
* |
| 42 |
* @param Settings $this Settings class instance for the current admin context. |
| 43 |
*/ |
| 44 |
do_action( 'woocommerce_pos_admin_settings_init', $this ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Output the settings pages. |
| 49 |
*/ |
| 50 |
public static function display_settings_page(): void { |
| 51 |
printf( |
| 52 |
'<div id="woocommerce-pos-settings"> |
| 53 |
<div id="woocommerce-pos-js-error" class="wrap"> |
| 54 |
<h1>%s</h1> |
| 55 |
<p>%s <a href="mailto:support@wcpos.com">support@wcpos.com</a></p> |
| 56 |
</div> |
| 57 |
</div>', |
| 58 |
/* translators: Short WCPOS UI label; keep concise. */ |
| 59 |
esc_html__( 'Error', 'woocommerce-pos' ), |
| 60 |
esc_html__( 'Settings failed to load, please contact support', 'woocommerce-pos' ) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Zero out #wpcontent padding so the settings page fills edge-to-edge. |
| 66 |
* |
| 67 |
* WordPress sets padding-left on #wpcontent, but the value varies across |
| 68 |
* themes, plugins, and screen sizes. Rather than guessing the value with |
| 69 |
* a negative margin hack, we remove it entirely on our settings page. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function override_wpcontent_padding(): void { |
| 74 |
echo '<style>#wpcontent { padding-left: 0 !important; }</style>'; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Remove all admin notices on our settings page. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function remove_admin_notices(): void { |
| 83 |
remove_all_actions( 'admin_notices' ); |
| 84 |
remove_all_actions( 'all_admin_notices' ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Enqueue assets. |
| 89 |
* |
| 90 |
* Note: SCRIPT_DEBUG should be set in the wp-config.php file for debugging |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function enqueue_assets(): void { |
| 95 |
$is_development = isset( $_ENV['DEVELOPMENT'] ) |
| 96 |
&& wp_validate_boolean( sanitize_text_field( wp_unslash( $_ENV['DEVELOPMENT'] ) ) ); |
| 97 |
$dir = $is_development ? 'build' : 'assets'; |
| 98 |
|
| 99 |
wp_enqueue_style( |
| 100 |
PLUGIN_NAME . '-settings-styles', |
| 101 |
PLUGIN_URL . $dir . '/css/settings.css', |
| 102 |
array(), |
| 103 |
VERSION |
| 104 |
); |
| 105 |
|
| 106 |
wp_enqueue_script( |
| 107 |
PLUGIN_NAME . '-settings', |
| 108 |
PLUGIN_URL . $dir . '/js/settings.js', |
| 109 |
array( |
| 110 |
'react', |
| 111 |
'react-dom', |
| 112 |
'wp-api-fetch', |
| 113 |
\WCPOS\WooCommercePOS\Admin::API_FETCH_METHOD_PARAM_HANDLE, |
| 114 |
'wp-url', |
| 115 |
'lodash', |
| 116 |
), |
| 117 |
VERSION, |
| 118 |
true |
| 119 |
); |
| 120 |
|
| 121 |
wp_add_inline_script( PLUGIN_NAME . '-settings', Menu::get_posthog_inline_script(), 'before' ); |
| 122 |
|
| 123 |
// Add inline script. |
| 124 |
wp_add_inline_script( PLUGIN_NAME . '-settings', $this->inline_script(), 'before' ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Generate the inline script for settings. |
| 129 |
* |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
private function inline_script(): string { |
| 133 |
$settings_service = SettingsService::instance(); |
| 134 |
$barcodes = array_values( $settings_service->get_barcodes() ); |
| 135 |
$order_statuses = $settings_service->get_order_statuses(); |
| 136 |
$update_ext_count = $this->get_update_available_count(); |
| 137 |
$unread_logs = Logs::get_unread_counts( get_current_user_id() ); |
| 138 |
$current_user_id = get_current_user_id(); |
| 139 |
$countries = function_exists( 'WC' ) |
| 140 |
? WC()->countries->get_countries() |
| 141 |
: array(); |
| 142 |
$store_country = function_exists( 'WC' ) |
| 143 |
? WC()->countries->get_base_country() |
| 144 |
: ''; |
| 145 |
$store_options = apply_filters( 'woocommerce_pos_cloud_print_store_options', array() ); |
| 146 |
$anon_id = ( new \WCPOS\WooCommercePOS\Services\Anon_ID() )->get(); |
| 147 |
$site_uuid = get_option( 'woocommerce_pos_uuid', '' ); |
| 148 |
|
| 149 |
return \sprintf( |
| 150 |
'var wcpos = wcpos || {}; wcpos.settings = { |
| 151 |
environment: %s, |
| 152 |
barcodes: %s, |
| 153 |
order_statuses: %s, |
| 154 |
countries: %s, |
| 155 |
storeCountry: %s, |
| 156 |
cloudPrintStoreOptions: %s, |
| 157 |
updateExtensionsCount: %s, |
| 158 |
unreadLogCounts: %s, |
| 159 |
currentUserId: %s, |
| 160 |
anon_id: %s, |
| 161 |
site_uuid: %s |
| 162 |
}; wcpos.translationVersion = %s;', |
| 163 |
json_encode( array( 'cloudflare' => ( new Cloudflare_Detector() )->detect() ) ), |
| 164 |
json_encode( $barcodes ), |
| 165 |
json_encode( $order_statuses ), |
| 166 |
json_encode( $countries ), |
| 167 |
json_encode( $store_country ), |
| 168 |
json_encode( array_values( (array) $store_options ) ), |
| 169 |
json_encode( $update_ext_count ), |
| 170 |
json_encode( $unread_logs ), |
| 171 |
json_encode( $current_user_id ), |
| 172 |
json_encode( $anon_id ), |
| 173 |
json_encode( $site_uuid ), |
| 174 |
json_encode( TRANSLATION_VERSION ) |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Get the count of extensions that have updates available. |
| 180 |
* |
| 181 |
* Uses the cached catalog transient and local plugin versions to avoid |
| 182 |
* remote fetches on every page load. Returns null if the catalog hasn't |
| 183 |
* been fetched yet. |
| 184 |
* |
| 185 |
* @return int|null |
| 186 |
*/ |
| 187 |
private function get_update_available_count(): ?int { |
| 188 |
$cached = get_transient( ExtensionsService::TRANSIENT_KEY ); |
| 189 |
|
| 190 |
if ( false === $cached || ! \is_array( $cached ) ) { |
| 191 |
return null; |
| 192 |
} |
| 193 |
|
| 194 |
if ( ! \function_exists( 'get_plugins' ) ) { |
| 195 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 196 |
} |
| 197 |
|
| 198 |
$installed_plugins = get_plugins(); |
| 199 |
$installed_by_slug = array(); |
| 200 |
$count = 0; |
| 201 |
|
| 202 |
foreach ( $installed_plugins as $plugin_file => $plugin_data ) { |
| 203 |
$parts = explode( '/', $plugin_file, 2 ); |
| 204 |
if ( isset( $parts[1] ) ) { |
| 205 |
$installed_by_slug[ $parts[0] ] = $plugin_data['Version'] ?? ''; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
foreach ( $cached as $entry ) { |
| 210 |
if ( ! \is_array( $entry ) ) { |
| 211 |
continue; |
| 212 |
} |
| 213 |
$slug = $entry['slug'] ?? ''; |
| 214 |
$remote_ver = $entry['latest_version'] ?? $entry['version'] ?? ''; |
| 215 |
|
| 216 |
if ( $slug && $remote_ver && isset( $installed_by_slug[ $slug ] ) ) { |
| 217 |
if ( version_compare( $installed_by_slug[ $slug ], $remote_ver, '<' ) ) { |
| 218 |
++$count; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return $count; |
| 224 |
} |
| 225 |
} |
| 226 |
|