PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.1.3
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.1.3
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
174 lines 4.7 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 }
63
64 public function enqueueComponents() {
65 wp_enqueue_script( 'surecart-components' );
66 wp_enqueue_style( 'surecart-themes-default' );
67 wp_add_inline_style(
68 'surecart-themes-default',
69 ':root { --sc-color-primary-text: #fff; }' // this is important in case the user has a dark primary text.
70 );
71 }
72
73 /**
74 * Enqueue scripts
75 *
76 * @return void
77 */
78 public function enqueue() {
79 if ( ! $this->condition() ) {
80 return;
81 }
82
83 // components are also used on index pages.
84 $this->enqueueComponents();
85
86 // match url query for the scripts.
87 if ( ! empty( $this->url_query ) ) {
88 foreach ( $this->url_query as $param => $value ) {
89 // phpcs:ignore
90 if ( ! isset( $_GET[ $param ] ) || $value !== sanitize_text_field( wp_unslash( $_GET[ $param ] ) ) ) {
91 return;
92 }
93 }
94 }
95
96 // enqueue dependencies.
97 $this->enqueueScriptDependencies();
98
99 // fix shitty jetpack issues key hijacking issues.
100 add_filter(
101 'admin_head',
102 function() {
103 wp_dequeue_script( 'wpcom-notes-common' );
104 wp_dequeue_script( 'wpcom-notes-admin-bar' );
105 wp_dequeue_style( 'wpcom-notes-admin-bar' );
106 wp_dequeue_style( 'noticons' );
107 },
108 200
109 );
110
111 // automatically load dependencies and version.
112 $asset_file = include plugin_dir_path( SURECART_PLUGIN_FILE ) . "dist/$this->path.asset.php";
113
114 // Enqueue scripts.
115 \SureCart::core()->assets()->enqueueScript(
116 $this->handle,
117 trailingslashit( \SureCart::core()->assets()->getUrl() ) . "dist/$this->path.js",
118 array_merge( $asset_file['dependencies'], $this->dependencies ),
119 $asset_file['version'] . '-' . \SureCart::plugin()->version()
120 );
121
122 // pass app url.
123 $this->data['upgrade_url'] = \SureCart::config()->links->purchase;
124 $this->data['surecart_app_url'] = defined( 'SURECART_APP_URL' ) ? SURECART_APP_URL : '';
125 $this->data['api_url'] = \SureCart::requests()->getBaseUrl();
126 $this->data['plugin_url'] = \SureCart::core()->assets()->getUrl();
127 $this->data['home_url'] = untrailingslashit( get_home_url() );
128 $this->data['buy_page_slug'] = untrailingslashit( \SureCart::settings()->permalinks()->getBase( 'buy_page' ) );
129 $this->data['product_page_slug'] = untrailingslashit( \SureCart::settings()->permalinks()->getBase( 'product_page' ) );
130 $this->data['is_block_theme'] = \SureCart::utility()->blockTemplates()->isFSETheme();
131 $this->data['claim_url'] = ! \SureCart::account()->claimed ? \SureCart::routeUrl( 'account.claim' ) : '';
132
133 if ( in_array( 'currency', $this->with_data ) ) {
134 $this->data['currency_code'] = \SureCart::account()->currency;
135 }
136 if ( in_array( 'tax_protocol', $this->with_data ) ) {
137 $this->data['tax_protocol'] = \SureCart::account()->tax_protocol;
138 }
139 if ( in_array( 'checkout_page_url', $this->with_data ) ) {
140 $this->data['checkout_page_url'] = \SureCart::getUrl()->checkout();
141 }
142 if ( in_array( 'supported_currencies', $this->with_data ) ) {
143 $this->data['supported_currencies'] = Currency::getSupportedCurrencies();
144 }
145 if ( in_array( 'links', $this->with_data ) ) {
146 $this->data['links'] = [];
147 foreach ( array_keys( \SureCart::getAdminPageNames() ) as $name ) {
148 $this->data['links'][ $name ] = esc_url_raw( add_query_arg( [ 'action' => 'edit' ], \SureCart::getUrl()->index( $name ) ) );
149 }
150 }
151
152 // pass entitlements to page.
153 $this->data['entitlements'] = \SureCart::account()->entitlements;
154 $this->data['get_locale'] = str_replace( '_', '-', get_locale() );
155
156 wp_set_script_translations( $this->handle, 'surecart' );
157
158 // common localizations.
159 wp_localize_script(
160 $this->handle,
161 'scData',
162 apply_filters( "$this->handle/data", $this->data )
163 );
164
165 wp_localize_script( $this->handle, 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
166
167 // custom localizations.
168 $this->localize( $this->handle );
169 }
170
171 protected function localize( $handle ) {
172 }
173 }
174