| @@ -9,8 +9,9 @@ | ||
| 9 | 9 | namespace ContentControl\Controllers; |
| 10 | 10 | |
| 11 | 11 | use ContentControl\Base\Controller; |
| 12 | 12 | |
| 13 | +use function ContentControl\get_all_plugin_options; | |
| 13 | 14 | use function ContentControl\Rules\allowed_user_roles; |
| 14 | 15 | |
| 15 | 16 | defined( 'ABSPATH' ) || exit; |
| 16 | 17 | |
| @@ -24,11 +25,11 @@ | ||
| 24 | 25 | /** |
| 25 | 26 | * Initialize the assets controller. |
| 26 | 27 | */ |
| 27 | 28 | public function init() { |
| 28 | - add_action( 'init', [ $this, 'register_scripts' ] ); | |
| 29 | + add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ], 0 ); | |
| 30 | + add_action( 'admin_enqueue_scripts', [ $this, 'register_scripts' ], 0 ); | |
| 29 | 31 | add_action( 'wp_print_scripts', [ $this, 'autoload_styles_for_scripts' ], 0 ); |
| 30 | - add_action( 'admin_enqueue_scripts', [ $this, 'register_scripts' ] ); | |
| 31 | 32 | add_action( 'admin_print_scripts', [ $this, 'autoload_styles_for_scripts' ], 0 ); |
| 32 | 33 | } |
| 33 | 34 | |
| 34 | 35 | /** |
| @@ -33,9 +34,9 @@ | ||
| 33 | 34 | |
| 34 | 35 | /** |
| 35 | 36 | * Get list of plugin packages. |
| 36 | 37 | * |
| 37 | - * @return array | |
| 38 | + * @return array<string,array<string,mixed>> | |
| 38 | 39 | */ |
| 39 | 40 | public function get_packages() { |
| 40 | 41 | $permissions = $this->container->get_permissions(); |
| 41 | 42 | |
| @@ -42,8 +43,14 @@ | ||
| 42 | 43 | foreach ( $permissions as $permission => $cap ) { |
| 43 | 44 | $permissions[ $permission ] = current_user_can( $cap ); |
| 44 | 45 | } |
| 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 | + | |
| 46 | 53 | $packages = [ |
| 47 | 54 | 'block-editor' => [ |
| 48 | 55 | 'handle' => 'content-control-block-editor', |
| 49 | 56 | 'styles' => true, |
| @@ -49,8 +56,9 @@ | ||
| 49 | 56 | 'styles' => true, |
| 50 | 57 | 'varsName' => 'contentControlBlockEditor', |
| 51 | 58 | 'vars' => [ |
| 52 | 59 | 'adminUrl' => admin_url(), |
| 60 | + 'wpVersion' => $wp_version, | |
| 53 | 61 | 'pluginUrl' => $this->container->get_url(), |
| 54 | 62 | 'advancedMode' => $this->container->get_option( 'advanced_mode', false ), |
| 55 | 63 | 'allowedBlocks' => [], |
| 56 | 64 | 'userRoles' => allowed_user_roles(), |
| @@ -63,14 +71,24 @@ | ||
| 63 | 71 | ], |
| 64 | 72 | 'components' => [ |
| 65 | 73 | 'handle' => 'content-control-components', |
| 66 | 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 | + ], | |
| 67 | 81 | ], |
| 68 | 82 | 'core-data' => [ |
| 69 | - 'handle' => 'content-control-core-data', | |
| 70 | - 'deps' => [ | |
| 83 | + 'handle' => 'content-control-core-data', | |
| 84 | + 'deps' => [ | |
| 71 | 85 | 'wp-api', |
| 72 | 86 | ], |
| 87 | + 'varsName' => 'contentControlCoreData', | |
| 88 | + 'vars' => [ | |
| 89 | + 'currentSettings' => get_all_plugin_options(), | |
| 90 | + ], | |
| 73 | 91 | ], |
| 74 | 92 | 'data' => [ |
| 75 | 93 | 'handle' => 'content-control-data', |
| 76 | 94 | ], |
| @@ -93,16 +111,19 @@ | ||
| 93 | 111 | 'settings-page' => [ |
| 94 | 112 | 'handle' => 'content-control-settings-page', |
| 95 | 113 | 'varsName' => 'contentControlSettingsPage', |
| 96 | 114 | '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, | |
| 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' ), | |
| 105 | 126 | ], |
| 106 | 127 | 'styles' => true, |
| 107 | 128 | ], |
| 108 | 129 | 'utils' => [ |
| @@ -118,8 +139,10 @@ | ||
| 118 | 139 | } |
| 119 | 140 | |
| 120 | 141 | /** |
| 121 | 142 | * Register all package scripts & styles. |
| 143 | + * | |
| 144 | + * @return void | |
| 122 | 145 | */ |
| 123 | 146 | public function register_scripts() { |
| 124 | 147 | $packages = $this->get_packages(); |
| 125 | 148 | |
| @@ -153,8 +176,10 @@ | ||
| 153 | 176 | } |
| 154 | 177 | |
| 155 | 178 | /** |
| 156 | 179 | * Auto load styles if scripts are enqueued. |
| 180 | + * | |
| 181 | + * @return void | |
| 157 | 182 | */ |
| 158 | 183 | public function autoload_styles_for_scripts() { |
| 159 | 184 | $packages = $this->get_packages(); |
| 160 | 185 | |
| @@ -170,9 +195,9 @@ | ||
| 170 | 195 | /** |
| 171 | 196 | * Get asset meta from generated files. |
| 172 | 197 | * |
| 173 | 198 | * @param string $package Package name. |
| 174 | - * @return array | |
| 199 | + * @return array{dependencies:string[],version:string} | |
| 175 | 200 | */ |
| 176 | 201 | public function get_asset_meta( $package ) { |
| 177 | 202 | $meta_path = $this->container->get_path( "dist/$package.asset.php" ); |
| 178 | 203 | return file_exists( $meta_path ) ? require $meta_path : [ |