PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / trunk
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Controllers / Assets.php

Assets.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More trunk, at classes/Controllers/Assets.php

213 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin assets controller.
4 *
5 * @package ContentControl\Admin
6 * @copyright (c) 2023 Code Atlantic LLC.
7 */
8
9 namespace ContentControl\Controllers;
10
11 use ContentControl\Base\Controller;
12
13 use function ContentControl\get_all_plugin_options;
14 use function ContentControl\Rules\allowed_user_roles;
15
16 defined( 'ABSPATH' ) || exit;
17
18 /**
19 * Admin assets controller.
20 *
21 * @package ContentControl\Admin
22 */
23 class Assets extends Controller {
24
25 /**
26 * Initialize the assets controller.
27 */
28 public function init() {
29 add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ], 0 );
30 add_action( 'admin_enqueue_scripts', [ $this, 'register_scripts' ], 0 );
31 add_action( 'wp_print_scripts', [ $this, 'autoload_styles_for_scripts' ], 0 );
32 add_action( 'admin_print_scripts', [ $this, 'autoload_styles_for_scripts' ], 0 );
33 }
34
35 /**
36 * Get list of plugin packages.
37 *
38 * @return array<string,array<string,mixed>>
39 */
40 public function get_packages() {
41 $permissions = $this->container->get_permissions();
42
43 foreach ( $permissions as $permission => $cap ) {
44 $permissions[ $permission ] = current_user_can( $cap );
45 }
46
47 $wp_version = get_bloginfo( 'version' );
48 // Strip last number from version as they won't be breaking changes.
49 $wp_version = preg_replace( '/\.\d+$/', '', $wp_version );
50
51 $is_pro_installed = \ContentControl\is_plugin_installed( 'content-control-pro/content-control-pro.php' );
52 $is_pro_active = $is_pro_installed && is_plugin_active( 'content-control-pro/content-control-pro.php' );
53 $legacy_pro = $this->container->is_legacy_pro_active();
54
55 $packages = [
56 'block-editor' => [
57 'handle' => 'content-control-block-editor',
58 'styles' => true,
59 'varsName' => 'contentControlBlockEditor',
60 'vars' => [
61 'adminUrl' => admin_url(),
62 'wpVersion' => $wp_version,
63 'pluginUrl' => $this->container->get_url(),
64 'advancedMode' => $this->container->get_option( 'advanced_mode', false ),
65 'allowedBlocks' => [],
66 'userRoles' => allowed_user_roles(),
67 'excludedBlocks' => array_merge( $this->container->get_option( 'excludedBlocks', [] ), [
68 'core/nextpage',
69 'core/freeform',
70 ] ),
71 'permissions' => $permissions,
72 ],
73 ],
74 'components' => [
75 'handle' => 'content-control-components',
76 'styles' => true,
77 'deps' => [
78 // This is required for tinymce components.
79 'wp-tinymce',
80 // This is required for all tinyMCE plugins.
81 'wp-block-library',
82 ],
83 ],
84 'core-data' => [
85 'handle' => 'content-control-core-data',
86 'deps' => [
87 'wp-api',
88 ],
89 'varsName' => 'contentControlCoreData',
90 'vars' => [
91 'currentSettings' => get_all_plugin_options(),
92 'legacyProLicense' => $legacy_pro,
93 ],
94 ],
95 'data' => [
96 'handle' => 'content-control-data',
97 ],
98 'fields' => [
99 'handle' => 'content-control-fields',
100 ],
101 'icons' => [
102 'handle' => 'content-control-icons',
103 'styles' => true,
104 ],
105 'rule-engine' => [
106 'handle' => 'content-control-rule-engine',
107 'varsName' => 'contentControlRuleEngine',
108 'vars' => [
109 'adminUrl' => admin_url(),
110 'registeredRules' => $this->container->get( 'rules' )->get_block_editor_rules(),
111 ],
112 'styles' => true,
113 ],
114 'settings-page' => [
115 'handle' => 'content-control-settings-page',
116 'varsName' => 'contentControlSettingsPage',
117 'vars' => [
118 'pluginUrl' => $this->container->get( 'url' ),
119 'wpVersion' => $wp_version,
120 'adminUrl' => admin_url(),
121 'restBase' => 'content-control/v2',
122 'userRoles' => allowed_user_roles(),
123 'logUrl' => current_user_can( 'manage_options' ) ? $this->container->get( 'logging' )->get_file_url() : false,
124 'rolesAndCaps' => wp_roles()->roles,
125 'version' => $this->container->get( 'version' ),
126 'permissions' => $permissions,
127 'isProInstalled' => $is_pro_installed,
128 'isProActivated' => $is_pro_active,
129 'legacyProLicense' => $legacy_pro,
130 ],
131 'styles' => true,
132 ],
133 'utils' => [
134 'handle' => 'content-control-utils',
135 ],
136 'widget-editor' => [
137 'handle' => 'content-control-widget-editor',
138 'styles' => true,
139 ],
140 ];
141
142 return $packages;
143 }
144
145 /**
146 * Register all package scripts & styles.
147 *
148 * @return void
149 */
150 public function register_scripts() {
151 $packages = $this->get_packages();
152
153 // Register front end block styles.
154 wp_register_style( 'content-control-block-styles', $this->container->get_url( 'dist/style-block-editor.css' ), [], $this->container->get( 'version' ) );
155
156 foreach ( $packages as $package => $package_data ) {
157 $handle = $package_data['handle'];
158 $meta = $this->get_asset_meta( $package );
159
160 $js_deps = isset( $package_data['deps'] ) ? $package_data['deps'] : [];
161
162 wp_register_script( $handle, $this->container->get_url( "dist/$package.js" ), array_merge( $meta['dependencies'], $js_deps ), $meta['version'], true );
163
164 if ( isset( $package_data['styles'] ) && $package_data['styles'] ) {
165 wp_register_style( $handle, $this->container->get_url( "dist/$package.css" ), [ 'wp-components', 'wp-block-editor', 'dashicons' ], $meta['version'] );
166 }
167
168 if ( isset( $package_data['varsName'] ) && ! empty( $package_data['vars'] ) ) {
169 $localized_vars = apply_filters( "content_control/{$package}_localized_vars", $package_data['vars'] );
170 wp_localize_script( $handle, $package_data['varsName'], $localized_vars );
171 }
172
173 /**
174 * May be extended to wp_set_script_translations( 'my-handle', 'my-domain',
175 * plugin_dir_path( MY_PLUGIN ) . 'languages' ) ). For details see
176 * https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/
177 */
178 wp_set_script_translations( $handle, 'content-control' );
179 }
180 }
181
182 /**
183 * Auto load styles if scripts are enqueued.
184 *
185 * @return void
186 */
187 public function autoload_styles_for_scripts() {
188 $packages = $this->get_packages();
189
190 foreach ( $packages as $package => $package_data ) {
191 if ( wp_script_is( $package_data['handle'], 'enqueued' ) ) {
192 if ( isset( $package_data['styles'] ) && $package_data['styles'] ) {
193 wp_enqueue_style( $package_data['handle'] );
194 }
195 }
196 }
197 }
198
199 /**
200 * Get asset meta from generated files.
201 *
202 * @param string $package Package name.
203 * @return array{dependencies:string[],version:string}
204 */
205 public function get_asset_meta( $package ) {
206 $meta_path = $this->container->get_path( "dist/$package.asset.php" );
207 return file_exists( $meta_path ) ? require $meta_path : [
208 'dependencies' => [],
209 'version' => $this->container->get( 'version' ),
210 ];
211 }
212 }
213