PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.11.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.11.0
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.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 All 281 releases
surecart / app / src / Support / Scripts / AdminModelEditController.php
AdminModelEditController.php
194 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Support\Scripts;
4
5 use SureCart\Models\Account;
6 use SureCart\Support\Currency;
7
8 /**
9 * Class for model edit pages to extend.
10 */
11 abstract class AdminModelEditController {
12 /**
13 * Script path.
14 *
15 * @var string
16 */
17 protected $path = '';
18
19 /**
20 * Script handle.
21 *
22 * @var string
23 */
24 protected $handle = '';
25
26 /**
27 * What types of data to add the the page.
28 *
29 * @var array
30 */
31 protected $with_data = [ 'links' ];
32
33 /**
34 * Additional dependencies
35 *
36 * @var array
37 */
38 protected $dependencies = [ 'sc-core-data', 'sc-ui-data' ];
39
40 /**
41 * Data to pass to the page.
42 *
43 * @var array
44 */
45 protected $data = [];
46
47 /**
48 * Optional conditionally load.
49 */
50 protected function condition() {
51 return true;
52 }
53
54 /**
55 * Enqueue needed scripts
56 *
57 * @return void
58 */
59 public function enqueueScriptDependencies() {
60 wp_enqueue_media();
61 wp_enqueue_style( 'wp-components' );
62 wp_enqueue_style( 'wp-editor' );
63 }
64
65 /**
66 * Enqueue components
67 *
68 * @return void
69 */
70 public function enqueueComponents() {
71 wp_enqueue_script( 'surecart-components' );
72 wp_enqueue_style( 'surecart-themes-default' );
73 wp_add_inline_style(
74 'surecart-themes-default',
75 ':root { --sc-color-primary-text: #fff; }' // this is important in case the user has a dark primary text.
76 );
77 wp_add_inline_style(
78 'surecart-themes-default',
79 '.sc-dragging { z-index: 1 }' // this is required for dragging.
80 );
81 }
82
83 /**
84 * Enqueue scripts
85 *
86 * @return void
87 */
88 public function enqueue() {
89 if ( ! $this->condition() ) {
90 return;
91 }
92
93 // components are also used on index pages.
94 $this->enqueueComponents();
95
96 // match url query for the scripts.
97 if ( ! empty( $this->url_query ) ) {
98 foreach ( $this->url_query as $param => $value ) {
99 // phpcs:ignore
100 if ( ! isset( $_GET[ $param ] ) || $value !== sanitize_text_field( wp_unslash( $_GET[ $param ] ) ) ) {
101 return;
102 }
103 }
104 }
105
106 // enqueue dependencies.
107 $this->enqueueScriptDependencies();
108
109 // fix shitty jetpack issues key hijacking issues.
110 add_filter(
111 'admin_head',
112 function () {
113 wp_dequeue_script( 'wpcom-notes-common' );
114 wp_dequeue_script( 'wpcom-notes-admin-bar' );
115 wp_dequeue_style( 'wpcom-notes-admin-bar' );
116 wp_dequeue_style( 'noticons' );
117 },
118 200
119 );
120
121 // automatically load dependencies and version.
122 $asset_file = include plugin_dir_path( SURECART_PLUGIN_FILE ) . "dist/$this->path.asset.php";
123
124 // Enqueue scripts.
125 wp_enqueue_script(
126 $this->handle,
127 trailingslashit( \SureCart::core()->assets()->getUrl() ) . "dist/$this->path.js",
128 array_merge( $asset_file['dependencies'], $this->dependencies ),
129 $asset_file['version'],
130 true
131 );
132
133 // pass app url.
134 $this->data['upgrade_url'] = \SureCart::config()->links->purchase;
135 $this->data['surecart_app_url'] = defined( 'SURECART_APP_URL' ) ? SURECART_APP_URL : '';
136 $this->data['account_id'] = \SureCart::account()->id ?? '';
137 $this->data['account_slug'] = \SureCart::account()->slug ?? '';
138 $this->data['api_url'] = \SureCart::requests()->getBaseUrl();
139 $this->data['plugin_url'] = \SureCart::core()->assets()->getUrl();
140 $this->data['home_url'] = untrailingslashit( get_home_url() );
141 $this->data['buy_page_slug'] = untrailingslashit( \SureCart::settings()->permalinks()->getBase( 'buy_page' ) );
142 $this->data['product_page_slug'] = untrailingslashit( \SureCart::settings()->permalinks()->getBase( 'product_page' ) );
143 $this->data['collection_page_slug'] = untrailingslashit( \SureCart::settings()->permalinks()->getBase( 'collection_page' ) );
144 $this->data['is_block_theme'] = \SureCart::utility()->blockTemplates()->isFSETheme();
145 $this->data['claim_url'] = ! \SureCart::account()->claimed ? \SureCart::routeUrl( 'account.claim' ) : '';
146
147 if ( in_array( 'currency', $this->with_data ) ) {
148 $this->data['currency_code'] = \SureCart::account()->currency;
149 }
150 if ( in_array( 'tax_protocol', $this->with_data ) ) {
151 $this->data['tax_protocol'] = \SureCart::account()->tax_protocol;
152 }
153 if ( in_array( 'shipping_protocol', $this->with_data ) ) {
154 $this->data['shipping_protocol'] = \SureCart::account()->shipping_protocol;
155 }
156 if ( in_array( 'checkout_page_url', $this->with_data ) ) {
157 $this->data['checkout_page_url'] = \SureCart::getUrl()->checkout();
158 }
159 if ( in_array( 'supported_currencies', $this->with_data ) ) {
160 $this->data['supported_currencies'] = Currency::list();
161 }
162 if ( in_array( 'links', $this->with_data ) ) {
163 $this->data['links'] = [];
164 foreach ( array_keys( \SureCart::getAdminPageNames() ) as $name ) {
165 $this->data['links'][ $name ] = esc_url_raw( add_query_arg( [ 'action' => 'edit' ], \SureCart::getUrl()->index( $name ) ) );
166 }
167 }
168 if ( in_array( 'i18n', $this->with_data ) ) {
169 $this->data['i18n'] = \SureCart::state()->i18n()->get();
170 }
171
172 // pass entitlements to page.
173 $this->data['entitlements'] = \SureCart::account()->entitlements;
174 $this->data['get_locale'] = str_replace( '_', '-', get_locale() );
175
176 wp_set_script_translations( $this->handle, 'surecart' );
177
178 // common localizations.
179 wp_localize_script(
180 $this->handle,
181 'scData',
182 apply_filters( "$this->handle/data", $this->data )
183 );
184
185 wp_localize_script( $this->handle, 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
186
187 // custom localizations.
188 $this->localize( $this->handle );
189 }
190
191 protected function localize( $handle ) {
192 }
193 }
194