PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.1
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.1
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / includes / Admin / Settings.php

Settings.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.1, at includes/Admin/Settings.php

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