PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.7
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.7
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.7, at includes/Admin/Settings.php

223 lines 6.3 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 \WCPOS\WooCommercePOS\Admin::API_FETCH_METHOD_PARAM_HANDLE,
113 'wp-url',
114 'lodash',
115 ),
116 VERSION,
117 true
118 );
119
120 wp_add_inline_script( PLUGIN_NAME . '-settings', Menu::get_posthog_inline_script(), 'before' );
121
122 // Add inline script.
123 wp_add_inline_script( PLUGIN_NAME . '-settings', $this->inline_script(), 'before' );
124 }
125
126 /**
127 * Generate the inline script for settings.
128 *
129 * @return string
130 */
131 private function inline_script(): string {
132 $settings_service = SettingsService::instance();
133 $barcodes = array_values( $settings_service->get_barcodes() );
134 $order_statuses = $settings_service->get_order_statuses();
135 $update_ext_count = $this->get_update_available_count();
136 $unread_logs = Logs::get_unread_counts( get_current_user_id() );
137 $current_user_id = get_current_user_id();
138 $countries = function_exists( 'WC' )
139 ? WC()->countries->get_countries()
140 : array();
141 $store_country = function_exists( 'WC' )
142 ? WC()->countries->get_base_country()
143 : '';
144 $store_options = apply_filters( 'woocommerce_pos_cloud_print_store_options', array() );
145 $anon_id = ( new \WCPOS\WooCommercePOS\Services\Anon_ID() )->get();
146 $site_uuid = get_option( 'woocommerce_pos_uuid', '' );
147
148 return \sprintf(
149 'var wcpos = wcpos || {}; wcpos.settings = {
150 barcodes: %s,
151 order_statuses: %s,
152 countries: %s,
153 storeCountry: %s,
154 cloudPrintStoreOptions: %s,
155 updateExtensionsCount: %s,
156 unreadLogCounts: %s,
157 currentUserId: %s,
158 anon_id: %s,
159 site_uuid: %s
160 }; wcpos.translationVersion = %s;',
161 json_encode( $barcodes ),
162 json_encode( $order_statuses ),
163 json_encode( $countries ),
164 json_encode( $store_country ),
165 json_encode( array_values( (array) $store_options ) ),
166 json_encode( $update_ext_count ),
167 json_encode( $unread_logs ),
168 json_encode( $current_user_id ),
169 json_encode( $anon_id ),
170 json_encode( $site_uuid ),
171 json_encode( TRANSLATION_VERSION )
172 );
173 }
174
175 /**
176 * Get the count of extensions that have updates available.
177 *
178 * Uses the cached catalog transient and local plugin versions to avoid
179 * remote fetches on every page load. Returns null if the catalog hasn't
180 * been fetched yet.
181 *
182 * @return int|null
183 */
184 private function get_update_available_count(): ?int {
185 $cached = get_transient( ExtensionsService::TRANSIENT_KEY );
186
187 if ( false === $cached || ! \is_array( $cached ) ) {
188 return null;
189 }
190
191 if ( ! \function_exists( 'get_plugins' ) ) {
192 require_once ABSPATH . 'wp-admin/includes/plugin.php';
193 }
194
195 $installed_plugins = get_plugins();
196 $installed_by_slug = array();
197 $count = 0;
198
199 foreach ( $installed_plugins as $plugin_file => $plugin_data ) {
200 $parts = explode( '/', $plugin_file, 2 );
201 if ( isset( $parts[1] ) ) {
202 $installed_by_slug[ $parts[0] ] = $plugin_data['Version'] ?? '';
203 }
204 }
205
206 foreach ( $cached as $entry ) {
207 if ( ! \is_array( $entry ) ) {
208 continue;
209 }
210 $slug = $entry['slug'] ?? '';
211 $remote_ver = $entry['latest_version'] ?? $entry['version'] ?? '';
212
213 if ( $slug && $remote_ver && isset( $installed_by_slug[ $slug ] ) ) {
214 if ( version_compare( $installed_by_slug[ $slug ], $remote_ver, '<' ) ) {
215 ++$count;
216 }
217 }
218 }
219
220 return $count;
221 }
222 }
223