BaseSettings.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\Settings; |
| 4 | |
| 5 | use SureCart\Support\Currency; |
| 6 | use SureCart\Support\TimeDate; |
| 7 | |
| 8 | /** |
| 9 | * Controls the settings page. |
| 10 | */ |
| 11 | abstract class BaseSettings { |
| 12 | /** |
| 13 | * Script handles for pages |
| 14 | * |
| 15 | * @var array |
| 16 | */ |
| 17 | protected $scripts = []; |
| 18 | |
| 19 | /** |
| 20 | * The template for the page. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $template = 'admin/settings-page'; |
| 25 | |
| 26 | /** |
| 27 | * Additional dependencies for this page. |
| 28 | * |
| 29 | * @var array |
| 30 | */ |
| 31 | protected $dependencies = []; |
| 32 | |
| 33 | /** |
| 34 | * Show the page. |
| 35 | * |
| 36 | * @param \SureCartCore\Requests\RequestInterface $request Request. |
| 37 | * @return function |
| 38 | */ |
| 39 | public function show( \SureCartCore\Requests\RequestInterface $request ) { |
| 40 | add_action( 'admin_enqueue_scripts', [ $this, 'showScripts' ] ); |
| 41 | |
| 42 | return \SureCart::view( $this->template )->with( |
| 43 | [ |
| 44 | 'tab' => $request->query( 'tab' ) ?? '', |
| 45 | 'status' => $request->query( 'status' ), |
| 46 | ] |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Enqueue the show scripts. |
| 52 | * |
| 53 | * @return void |
| 54 | */ |
| 55 | public function showScripts() { |
| 56 | if ( ! empty( $this->scripts['show'] ) ) { |
| 57 | $this->enqueue( $this->scripts['show'][0], $this->scripts['show'][1] ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Enqueue a script. |
| 63 | * |
| 64 | * @param string $handle Script handle. |
| 65 | * @param string $path Path to script. |
| 66 | * @param array $deps Dependencies. |
| 67 | * |
| 68 | * @return void |
| 69 | */ |
| 70 | public function enqueue( $handle, $path, $deps = [] ) { |
| 71 | $deps = array_merge( $deps, $this->dependencies ); |
| 72 | $deps = array_merge( $deps, [ 'sc-ui-data', 'wp-data', 'wp-core-data' ] ); |
| 73 | |
| 74 | wp_enqueue_media(); |
| 75 | wp_enqueue_style( 'wp-components' ); |
| 76 | wp_enqueue_style( 'wp-block-editor' ); |
| 77 | wp_enqueue_style( 'surecart-themes-default' ); |
| 78 | wp_enqueue_script( 'surecart-components' ); |
| 79 | wp_enqueue_script( 'wp-format-library' ); |
| 80 | wp_enqueue_style( 'wp-format-library' ); |
| 81 | |
| 82 | // automatically load dependencies and version. |
| 83 | $asset_file = include plugin_dir_path( SURECART_PLUGIN_FILE ) . "dist/$path.asset.php"; |
| 84 | |
| 85 | // Enqueue scripts. |
| 86 | \SureCart::core()->assets()->enqueueScript( |
| 87 | $handle, |
| 88 | trailingslashit( \SureCart::core()->assets()->getUrl() ) . "dist/$path.js", |
| 89 | array_merge( $asset_file['dependencies'], $deps ), |
| 90 | $asset_file['version'] . '-' . \SureCart::plugin()->version() |
| 91 | ); |
| 92 | |
| 93 | wp_set_script_translations( $handle, 'surecart', WP_LANG_DIR . '/plugins/' ); |
| 94 | |
| 95 | wp_localize_script( |
| 96 | $handle, |
| 97 | 'scData', |
| 98 | [ |
| 99 | 'supported_currencies' => Currency::getSupportedCurrencies(), |
| 100 | 'app_url' => defined( 'SURECART_APP_URL' ) ? untrailingslashit( SURECART_APP_URL ) : 'https://app.surecart.com', |
| 101 | 'api_url' => defined( 'SURECART_API_URL' ) ? untrailingslashit( SURECART_API_URL ) : \SureCart::requests()->getBaseUrl(), |
| 102 | 'time_zones' => TimeDate::timezoneOptions(), |
| 103 | ] |
| 104 | ); |
| 105 | } |
| 106 | } |
| 107 |