PluginProbe
Remove Schema / trunk
Remove Schema vtrunk
2.0.0 trunk 1.6.1 1.6.2
remove-schema / src / class-plugincontext.php

class-plugincontext.php in Remove Schema trunk, at src/class-plugincontext.php

157 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin context value object.
4 *
5 * @package TimVanIersel\RemoveSchema
6 */
7
8 declare(strict_types=1);
9
10 namespace TimVanIersel\RemoveSchema;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Immutable value object that holds plugin-wide configuration.
18 */
19 final class PluginContext {
20 /**
21 * Constructor.
22 *
23 * @param string $plugin_file Absolute path to the main plugin file.
24 * @param string $plugin_path Absolute path to the plugin root directory (with trailing slash).
25 * @param string $plugin_url URL to the plugin root directory (with trailing slash).
26 * @param string $version Plugin version string.
27 * @param string $slug Plugin slug (kebab-case).
28 * @param string $text_domain Text domain for translations.
29 */
30 public function __construct(
31 private readonly string $plugin_file,
32 private readonly string $plugin_path,
33 private readonly string $plugin_url,
34 private readonly string $version,
35 private readonly string $slug,
36 private readonly string $text_domain
37 ) {
38 }
39
40 /**
41 * Return the absolute path to the main plugin file.
42 *
43 * @return string
44 */
45 public function plugin_file(): string {
46 return $this->plugin_file;
47 }
48
49 /**
50 * Return the plugin basename (relative path from the plugins directory).
51 *
52 * @return string
53 */
54 public function plugin_basename(): string {
55 return plugin_basename( $this->plugin_file );
56 }
57
58 /**
59 * Return the absolute path to the plugin root directory.
60 *
61 * @return string
62 */
63 public function plugin_path(): string {
64 return $this->plugin_path;
65 }
66
67 /**
68 * Return the URL to the plugin root directory.
69 *
70 * @return string
71 */
72 public function plugin_url(): string {
73 return $this->plugin_url;
74 }
75
76 /**
77 * Return the plugin version string.
78 *
79 * @return string
80 */
81 public function version(): string {
82 return $this->version;
83 }
84
85 /**
86 * Return the plugin slug.
87 *
88 * @return string
89 */
90 public function slug(): string {
91 return $this->slug;
92 }
93
94 /**
95 * Return the text domain for translations.
96 *
97 * @return string
98 */
99 public function text_domain(): string {
100 return $this->text_domain;
101 }
102
103 /**
104 * Return the option name used to store plugin options.
105 *
106 * @return string
107 */
108 public function option_name(): string {
109 return 'remove-schema';
110 }
111
112 /**
113 * Return the option name used to store the installed plugin version.
114 *
115 * @return string
116 */
117 public function version_option_name(): string {
118 return 'remove_schema_version';
119 }
120
121 /**
122 * Return the absolute path to a file inside the build asset directory.
123 *
124 * @param string $relative Relative path under assets/build/ (optional).
125 * @return string
126 */
127 public function build_path( string $relative = '' ): string {
128 $path = $this->plugin_path . 'assets/build/';
129
130 return '' === $relative ? $path : $path . ltrim( $relative, '/' );
131 }
132
133 /**
134 * Return the URL to a file inside the build asset directory.
135 *
136 * @param string $relative Relative path under assets/build/ (optional).
137 * @return string
138 */
139 public function build_url( string $relative = '' ): string {
140 $url = $this->plugin_url . 'assets/build/';
141
142 return '' === $relative ? $url : $url . ltrim( $relative, '/' );
143 }
144
145 /**
146 * Return the absolute path to a file inside the source asset directory.
147 *
148 * @param string $relative Relative path under assets/src/ (optional).
149 * @return string
150 */
151 public function source_path( string $relative = '' ): string {
152 $path = $this->plugin_path . 'assets/src/';
153
154 return '' === $relative ? $path : $path . ltrim( $relative, '/' );
155 }
156 }
157