PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.5.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.5.0
4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / app / src / Support / Scripts / AdminModelEditController.php
surecart / app / src / Support / Scripts Last commit date
AdminModelEditController.php 5 days ago
AdminModelEditController.php
279 lines
1 <?php
2
3 namespace SureCart\Support\Scripts;
4
5 use SureCart\Support\Currency;
6
7 /**
8 * Class for model edit pages to extend.
9 */
10 abstract class AdminModelEditController {
11 /**
12 * Script path.
13 *
14 * @var string
15 */
16 protected $path = '';
17
18 /**
19 * Script handle.
20 *
21 * @var string
22 */
23 protected $handle = '';
24
25 /**
26 * What types of data to add the the page.
27 *
28 * @var array
29 */
30 protected $with_data = [ 'links' ];
31
32 /**
33 * Additional dependencies
34 *
35 * @var array
36 */
37 protected $dependencies = [ 'sc-core-data', 'sc-ui-data' ];
38
39 /**
40 * Whether this admin screen renders a `@wordpress/dataviews` table and
41 * needs the bundled DataViews stylesheet.
42 *
43 * @var bool
44 */
45 protected $needs_dataviews_style = false;
46
47 /**
48 * Data to pass to the page.
49 *
50 * @var array
51 */
52 protected $data = [];
53
54 /**
55 * Optional conditionally load.
56 */
57 protected function condition() {
58 return true;
59 }
60
61 /**
62 * Enqueue needed scripts
63 *
64 * @return void
65 */
66 public function enqueueScriptDependencies() {
67 wp_enqueue_media();
68 wp_enqueue_style( 'wp-components' );
69 wp_enqueue_style( 'wp-editor' );
70 }
71
72 /**
73 * Enqueue components
74 *
75 * @return void
76 */
77 public function enqueueComponents() {
78 wp_enqueue_script( 'surecart-components' );
79 wp_enqueue_style( 'surecart-themes-default' );
80 wp_add_inline_style(
81 'surecart-themes-default',
82 ':root { --sc-color-primary-text: #fff; }' // this is important in case the user has a dark primary text.
83 );
84 wp_add_inline_style(
85 'surecart-themes-default',
86 '.sc-dragging { z-index: 1 }' // this is required for dragging.
87 );
88 }
89
90 /**
91 * Enqueue the bundled `@wordpress/dataviews` stylesheet copied to
92 * `dist/vendor/` at build time. See `webpack.config.js` for the
93 * CopyPlugin pattern that produces this file.
94 *
95 * Only enqueues when `$needs_dataviews_style` is set on the controller.
96 */
97 protected function enqueueDataviewsStyle(): void {
98 if ( ! $this->needs_dataviews_style ) {
99 return;
100 }
101 $relative = is_rtl() ? 'dist/vendor/dataviews-rtl.css' : 'dist/vendor/dataviews.css';
102 $abs_path = plugin_dir_path( SURECART_PLUGIN_FILE ) . $relative;
103 if ( ! file_exists( $abs_path ) ) {
104 return;
105 }
106 wp_enqueue_style(
107 'sc-dataviews',
108 trailingslashit( \SureCart::core()->assets()->getUrl() ) . $relative,
109 [],
110 defined( 'SURECART_VERSION' ) ? SURECART_VERSION : (string) filemtime( $abs_path )
111 );
112 }
113
114 /**
115 * Enqueue scripts
116 *
117 * @return void
118 */
119 public function enqueue() {
120 if ( ! $this->condition() ) {
121 return;
122 }
123
124 // components are also used on index pages.
125 $this->enqueueComponents();
126 $this->enqueueDataviewsStyle();
127
128 // match url query for the scripts.
129 if ( ! empty( $this->url_query ) ) {
130 foreach ( $this->url_query as $param => $value ) {
131 // phpcs:ignore
132 if ( ! isset( $_GET[ $param ] ) || $value !== sanitize_text_field( wp_unslash( $_GET[ $param ] ) ) ) {
133 return;
134 }
135 }
136 }
137
138 // enqueue dependencies.
139 $this->enqueueScriptDependencies();
140
141 // Fix Jetpack script key hijacking issues.
142 add_filter(
143 'admin_head',
144 function () {
145 wp_dequeue_script( 'wpcom-notes-common' );
146 wp_dequeue_script( 'wpcom-notes-admin-bar' );
147 wp_dequeue_style( 'wpcom-notes-admin-bar' );
148 wp_dequeue_style( 'noticons' );
149 },
150 200
151 );
152
153 // automatically load dependencies and version.
154 $asset_file_path = plugin_dir_path( SURECART_PLUGIN_FILE ) . "dist/$this->path.asset.php";
155 if ( ! file_exists( $asset_file_path ) ) {
156 // Bundle hasn't been built yet (e.g. fresh install or the entry
157 // was just added without running `yarn build`). Bail rather than
158 // crashing on an `array_merge( null, ... )`.
159 return;
160 }
161 $asset_file = include $asset_file_path;
162
163 // Fall back to plugin version so we still cache-bust on upgrade if
164 // the asset manifest is missing or malformed.
165 $version = $asset_file['version'] ?? ( defined( 'SURECART_VERSION' ) ? SURECART_VERSION : null );
166
167 // Enqueue scripts.
168 wp_enqueue_script(
169 $this->handle,
170 trailingslashit( \SureCart::core()->assets()->getUrl() ) . "dist/$this->path.js",
171 array_merge( $asset_file['dependencies'] ?? [], $this->dependencies ),
172 $version,
173 true
174 );
175
176 // Enqueue extracted CSS if the build produced a stylesheet for this entry.
177 $style_path = plugin_dir_path( SURECART_PLUGIN_FILE ) . "dist/$this->path.css";
178 if ( file_exists( $style_path ) ) {
179 wp_enqueue_style(
180 $this->handle,
181 trailingslashit( \SureCart::core()->assets()->getUrl() ) . "dist/$this->path.css",
182 array(),
183 $version
184 );
185 }
186
187 // Enqueue the vendor/library stylesheet (style-{entry}.css) produced by splitChunks.
188 $vendor_style_basename = 'style-' . basename( $this->path );
189 $vendor_style_dir = dirname( $this->path );
190 $vendor_style_rel = ( '.' === $vendor_style_dir ? '' : $vendor_style_dir . '/' ) . $vendor_style_basename . '.css';
191 $vendor_style_path = plugin_dir_path( SURECART_PLUGIN_FILE ) . "dist/$vendor_style_rel";
192 if ( file_exists( $vendor_style_path ) ) {
193 wp_enqueue_style(
194 $this->handle . '-vendor',
195 trailingslashit( \SureCart::core()->assets()->getUrl() ) . "dist/$vendor_style_rel",
196 array(),
197 $version
198 );
199 }
200
201 if ( $this->needs_dataviews_style ) {
202 $this->data['enhanced_admin_views_enabled'] = (bool) get_option( 'surecart_enhanced_admin_views', true );
203 $this->data['modern_view_intro'] = \SureCart\Settings\SettingService::getModernViewIntroData();
204 }
205
206 // pass app url.
207 $this->data['upgrade_url'] = \SureCart::config()->links->purchase;
208 $this->data['surecart_app_url'] = defined( 'SURECART_APP_URL' ) ? SURECART_APP_URL : '';
209 $this->data['account_id'] = \SureCart::account()->id ?? '';
210 $this->data['account_slug'] = \SureCart::account()->slug ?? '';
211 $this->data['api_url'] = \SureCart::requests()->getBaseUrl();
212 $this->data['plugin_url'] = \SureCart::core()->assets()->getUrl();
213 $this->data['locale'] = str_replace( '_', '-', get_locale() );
214 $this->data['root_url'] = esc_url_raw( get_rest_url() );
215 $this->data['home_url'] = untrailingslashit( get_home_url() );
216 $this->data['site_name'] = get_bloginfo( 'name' );
217 $this->data['site_icon_url'] = get_site_icon_url( 96 );
218 $this->data['buy_page_slug'] = untrailingslashit( \SureCart::settings()->permalinks()->getBase( 'buy_page' ) );
219 $this->data['product_page_slug'] = untrailingslashit( \SureCart::settings()->permalinks()->getBase( 'product_page' ) );
220 $this->data['collection_page_slug'] = untrailingslashit( \SureCart::settings()->permalinks()->getBase( 'collection_page' ) );
221 $this->data['is_block_theme'] = \SureCart::utility()->blockTemplates()->isFSETheme();
222 $this->data['claim_url'] = ! \SureCart::account()->claimed ? \SureCart::routeUrl( 'account.claim' ) : '';
223 $this->data['claim_expired'] = \SureCart::account()->claim_expired ?? false;
224 $this->data['is_woocommerce_active'] = class_exists( 'WooCommerce' );
225
226 if ( in_array( 'currency', $this->with_data ) ) {
227 $this->data['currency_code'] = \SureCart::account()->currency;
228 }
229 if ( in_array( 'review_protocol', $this->with_data ) ) {
230 $this->data['review_protocol'] = \SureCart::account()->review_protocol;
231 }
232 if ( in_array( 'tax_protocol', $this->with_data ) ) {
233 $this->data['tax_protocol'] = \SureCart::account()->tax_protocol;
234 }
235 if ( in_array( 'shipping_protocol', $this->with_data ) ) {
236 $this->data['shipping_protocol'] = \SureCart::account()->shipping_protocol;
237 }
238 if ( in_array( 'checkout_page_url', $this->with_data ) ) {
239 $this->data['checkout_page_url'] = \SureCart::getUrl()->checkout();
240 }
241 if ( in_array( 'supported_currencies', $this->with_data ) ) {
242 $this->data['supported_currencies'] = Currency::list();
243 }
244 if ( in_array( 'links', $this->with_data ) ) {
245 $this->data['links'] = [];
246 foreach ( array_keys( \SureCart::getAdminPageNames() ) as $name ) {
247 $this->data['links'][ $name ] = esc_url_raw( add_query_arg( [ 'action' => 'edit' ], \SureCart::getUrl()->index( $name ) ) );
248 }
249 }
250 if ( in_array( 'google_map_api_key', $this->with_data ) ) {
251 $this->data['google_map_api_key'] = \SureCart::googleMaps()->getApiKey();
252 }
253
254 // pass entitlements to page.
255 $this->data['entitlements'] = \SureCart::account()->entitlements;
256 $this->data['get_locale'] = str_replace( '_', '-', determine_locale() );
257
258 // pass wp user roles to page.
259 $this->data['wp_user_roles'] = get_editable_roles();
260
261 wp_set_script_translations( $this->handle, 'surecart' );
262
263 // common localizations.
264 wp_localize_script(
265 $this->handle,
266 'scData',
267 apply_filters( "$this->handle/data", $this->data )
268 );
269
270 wp_localize_script( $this->handle, 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
271
272 // custom localizations.
273 $this->localize( $this->handle );
274 }
275
276 protected function localize( $handle ) {
277 }
278 }
279