PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.12
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.12
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 2.0.12, at classes/Controllers/Assets.php

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