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

209 lines 6.4 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
53 $packages = [
54 'block-editor' => [
55 'handle' => 'content-control-block-editor',
56 'styles' => true,
57 'varsName' => 'contentControlBlockEditor',
58 'vars' => [
59 'adminUrl' => admin_url(),
60 'wpVersion' => $wp_version,
61 'pluginUrl' => $this->container->get_url(),
62 'advancedMode' => $this->container->get_option( 'advanced_mode', false ),
63 'allowedBlocks' => [],
64 'userRoles' => allowed_user_roles(),
65 'excludedBlocks' => array_merge( $this->container->get_option( 'excludedBlocks', [] ), [
66 'core/nextpage',
67 'core/freeform',
68 ] ),
69 'permissions' => $permissions,
70 ],
71 ],
72 'components' => [
73 'handle' => 'content-control-components',
74 'styles' => true,
75 'deps' => [
76 // This is required for tinymce components.
77 'wp-tinymce',
78 // This is required for all tinyMCE plugins.
79 'wp-block-library',
80 ],
81 ],
82 'core-data' => [
83 'handle' => 'content-control-core-data',
84 'deps' => [
85 'wp-api',
86 ],
87 'varsName' => 'contentControlCoreData',
88 'vars' => [
89 'currentSettings' => get_all_plugin_options(),
90 ],
91 ],
92 'data' => [
93 'handle' => 'content-control-data',
94 ],
95 'fields' => [
96 'handle' => 'content-control-fields',
97 ],
98 'icons' => [
99 'handle' => 'content-control-icons',
100 'styles' => true,
101 ],
102 'rule-engine' => [
103 'handle' => 'content-control-rule-engine',
104 'varsName' => 'contentControlRuleEngine',
105 'vars' => [
106 'adminUrl' => admin_url(),
107 'registeredRules' => $this->container->get( 'rules' )->get_block_editor_rules(),
108 ],
109 'styles' => true,
110 ],
111 'settings-page' => [
112 'handle' => 'content-control-settings-page',
113 'varsName' => 'contentControlSettingsPage',
114 'vars' => [
115 'pluginUrl' => $this->container->get( 'url' ),
116 'wpVersion' => $wp_version,
117 'adminUrl' => admin_url(),
118 'restBase' => 'content-control/v2',
119 'userRoles' => allowed_user_roles(),
120 'logUrl' => current_user_can( 'manage_options' ) ? $this->container->get( 'logging' )->get_file_url() : false,
121 'rolesAndCaps' => wp_roles()->roles,
122 'version' => $this->container->get( 'version' ),
123 'permissions' => $permissions,
124 'isProInstalled' => $is_pro_installed,
125 'isProActivated' => $is_pro_installed && is_plugin_active( 'content-control-pro/content-control-pro.php' ),
126 ],
127 'styles' => true,
128 ],
129 'utils' => [
130 'handle' => 'content-control-utils',
131 ],
132 'widget-editor' => [
133 'handle' => 'content-control-widget-editor',
134 'styles' => true,
135 ],
136 ];
137
138 return $packages;
139 }
140
141 /**
142 * Register all package scripts & styles.
143 *
144 * @return void
145 */
146 public function register_scripts() {
147 $packages = $this->get_packages();
148
149 // Register front end block styles.
150 wp_register_style( 'content-control-block-styles', $this->container->get_url( 'dist/style-block-editor.css' ), [], $this->container->get( 'version' ) );
151
152 foreach ( $packages as $package => $package_data ) {
153 $handle = $package_data['handle'];
154 $meta = $this->get_asset_meta( $package );
155
156 $js_deps = isset( $package_data['deps'] ) ? $package_data['deps'] : [];
157
158 wp_register_script( $handle, $this->container->get_url( "dist/$package.js" ), array_merge( $meta['dependencies'], $js_deps ), $meta['version'], true );
159
160 if ( isset( $package_data['styles'] ) && $package_data['styles'] ) {
161 wp_register_style( $handle, $this->container->get_url( "dist/$package.css" ), [ 'wp-components', 'wp-block-editor', 'dashicons' ], $meta['version'] );
162 }
163
164 if ( isset( $package_data['varsName'] ) && ! empty( $package_data['vars'] ) ) {
165 $localized_vars = apply_filters( "content_control/{$package}_localized_vars", $package_data['vars'] );
166 wp_localize_script( $handle, $package_data['varsName'], $localized_vars );
167 }
168
169 /**
170 * May be extended to wp_set_script_translations( 'my-handle', 'my-domain',
171 * plugin_dir_path( MY_PLUGIN ) . 'languages' ) ). For details see
172 * https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/
173 */
174 wp_set_script_translations( $handle, 'content-control' );
175 }
176 }
177
178 /**
179 * Auto load styles if scripts are enqueued.
180 *
181 * @return void
182 */
183 public function autoload_styles_for_scripts() {
184 $packages = $this->get_packages();
185
186 foreach ( $packages as $package => $package_data ) {
187 if ( wp_script_is( $package_data['handle'], 'enqueued' ) ) {
188 if ( isset( $package_data['styles'] ) && $package_data['styles'] ) {
189 wp_enqueue_style( $package_data['handle'] );
190 }
191 }
192 }
193 }
194
195 /**
196 * Get asset meta from generated files.
197 *
198 * @param string $package Package name.
199 * @return array{dependencies:string[],version:string}
200 */
201 public function get_asset_meta( $package ) {
202 $meta_path = $this->container->get_path( "dist/$package.asset.php" );
203 return file_exists( $meta_path ) ? require $meta_path : [
204 'dependencies' => [],
205 'version' => $this->container->get( 'version' ),
206 ];
207 }
208 }
209