PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.0
Booktics – Appointment Booking Calendar for Service Businesses v1.0.0
1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 All 27 releases
booktics / core / assets / base-assets.php

base-assets.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.0, at core/assets/base-assets.php

160 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Booktics\Assets;
3
4 use Booktics\Contracts\Hookable_Service_Contract;
5
6 /**
7 * Main assets management class
8 */
9 abstract class Base_Assets implements Hookable_Service_Contract {
10 /**
11 * Get all scripts
12 *
13 * @return array list of script to register
14 */
15 abstract public function get_scripts(): array;
16
17 /**
18 * Get all styles
19 *
20 * @return array list of styles to regiser
21 */
22 abstract public function get_styles(): array;
23
24 /**
25 * Register scripts and styles
26 *
27 * @return void
28 */
29 public function register_styles_scripts() {
30 $this->register_scripts();
31 $this->register_styles();
32 }
33
34 /**
35 * Register scripts
36 *
37 * @param array $scripts
38 *
39 * @return void
40 */
41 public function register_scripts() {
42 $scripts = $this->get_scripts();
43
44 foreach ( $scripts as $handle => $script ) {
45 $deps = isset( $script['deps'] ) ? $script['deps'] : [];
46 $in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : true;
47 $version = isset( $script['version'] ) ? $script['version'] : $this->get_version( $script['src'] );
48
49 $deps = $this->get_dependencies( $script['src'], $deps );
50
51 wp_register_script( $handle, $script['src'], $deps, $version, $in_footer );
52 }
53 }
54
55 /**
56 * Register styles
57 *
58 * @param array $styles
59 *
60 * @return void
61 */
62 public function register_styles() {
63 $styles = $this->get_styles();
64
65 foreach ( $styles as $handle => $style ) {
66 $deps = isset( $style['deps'] ) ? $style['deps'] : false;
67 $version = booktics()->version;
68
69 wp_register_style( $handle, $style['src'], $deps, $version );
70 }
71 }
72
73 /**
74 * Get script and style file dependencies
75 *
76 * @param string $file_name
77 * @param array $deps
78 *
79 * @return array
80 */
81 private function get_dependencies( $file_name, $deps = [] ) {
82 $assets = $this->get_file_assets( $file_name );
83
84 $assets_deps = ! empty( $assets['dependencies'] ) ? $assets['dependencies'] : [];
85
86 $merged_deps = array_merge( $assets_deps, $deps );
87
88 return $merged_deps;
89 }
90
91 /**
92 * Get file assets
93 *
94 * @param string $file_name
95 *
96 * @return array
97 */
98 private function get_file_assets( $file_url ) {
99 $file = $this->get_file_path( $file_url );
100 $assets = [];
101
102 if ( file_exists( $file ) ) {
103 $assets = include $file;
104 }
105
106 return $assets;
107 }
108
109 /**
110 * Get script file version
111 *
112 * @param string $file_name
113 *
114 * @return string
115 */
116 private function get_version( $file_name ) {
117 $assets = $this->get_file_assets( $file_name );
118 $assets_vers = ! empty( $assets['version'] ) ? $assets['version'] : booktics()->version;
119 return $assets_vers;
120 }
121
122 /**
123 * Get file path from url
124 *
125 * @param string $url
126 *
127 * @return string
128 */
129 private function get_file_path( $url ) {
130 // Check if the URL is valid
131 if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) {
132 return false;
133 }
134
135 // Parse the URL
136 $url_parts = wp_parse_url( $url );
137
138 // Check if the URL has a path component
139 if ( ! isset( $url_parts['path'] ) ) {
140 return false; // URL does not contain a path
141 }
142
143 $clean_path = str_replace( '.js', '.asset.php', $url_parts['path'] );
144
145 // Get the file path from the URL path.
146 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
147 $file_path = sanitize_text_field( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) ) . $clean_path;
148 } else {
149 $file_path = ''; // Or another appropriate fallback.
150 }
151
152 // Check if the file exists
153 if ( ! file_exists( $file_path ) ) {
154 return false; // File does not exist
155 }
156
157 return $file_path;
158 }
159 }
160