PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 1.1.4
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v1.1.4
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
151 lines 3.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 }
68
69 /**
70 * Enqueue scripts
71 *
72 * @return void
73 */
74 public function enqueue() {
75 if ( ! $this->condition() ) {
76 return;
77 }
78
79 // components are also used on index pages.
80 $this->enqueueComponents();
81
82 // match url query for the scripts.
83 if ( ! empty( $this->url_query ) ) {
84 foreach ( $this->url_query as $param => $value ) {
85 // phpcs:ignore
86 if ( ! isset( $_GET[ $param ] ) || $value !== sanitize_text_field( wp_unslash( $_GET[ $param ] ) ) ) {
87 return;
88 }
89 }
90 }
91
92 // enqueue dependencies.
93 $this->enqueueScriptDependencies();
94
95 // automatically load dependencies and version
96 $asset_file = include plugin_dir_path( SURECART_PLUGIN_FILE ) . "dist/$this->path.asset.php";
97
98 // Enqueue scripts.
99 \SureCart::core()->assets()->enqueueScript(
100 $this->handle,
101 trailingslashit( \SureCart::core()->assets()->getUrl() ) . "dist/$this->path.js",
102 array_merge( $asset_file['dependencies'], $this->dependencies ),
103 $asset_file['version'] . '-' . \SureCart::plugin()->version()
104 );
105
106 // pass app url.
107 $this->data['surecart_app_url'] = defined( 'SURECART_APP_URL' ) ? SURECART_APP_URL : '';
108 $this->data['api_url'] = defined( 'SURECART_API_URL' ) ? untrailingslashit( SURECART_API_URL ) : \SureCart::requests()->getBaseUrl();
109 $this->data['plugin_url'] = \SureCart::core()->assets()->getUrl();
110
111 if ( in_array( 'currency', $this->with_data ) ) {
112 $this->data['currency_code'] = \SureCart::account()->currency;
113 }
114 if ( in_array( 'tax_protocol', $this->with_data ) ) {
115 $this->data['tax_protocol'] = \SureCart::account()->tax_protocol;
116 }
117 if ( in_array( 'checkout_page_url', $this->with_data ) ) {
118 $this->data['checkout_page_url'] = \SureCart::getUrl()->checkout();
119 }
120 if ( in_array( 'supported_currencies', $this->with_data ) ) {
121 $this->data['supported_currencies'] = Currency::getSupportedCurrencies();
122 }
123 if ( in_array( 'links', $this->with_data ) ) {
124 $this->data['links'] = [];
125 foreach ( array_keys( \SureCart::getAdminPageNames() ) as $name ) {
126 $this->data['links'][ $name ] = esc_url_raw( add_query_arg( [ 'action' => 'edit' ], \SureCart::getUrl()->index( $name ) ) );
127 }
128 }
129
130 // pass entitlements to page.
131 $this->data['entitlements'] = \SureCart::account()->entitlements;
132
133 wp_set_script_translations( $this->handle, 'surecart' );
134
135 // common localizations.
136 wp_localize_script(
137 $this->handle,
138 'scData',
139 apply_filters( "$this->handle/data", $this->data )
140 );
141
142 wp_localize_script( $this->handle, 'scIcons', [ 'path' => esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'dist/icon-assets' ) ] );
143
144 // custom localizations.
145 $this->localize( $this->handle );
146 }
147
148 protected function localize( $handle ) {
149 }
150 }
151