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

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