PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.16
Booktics – Appointment Booking Calendar for Service Businesses v1.0.16
1.0.29 1.0.28 1.0.27 1.0.26 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 All 31 releases
booktics / core / assets / base-assets.php

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

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