Changeset 10376 for sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/blocks.php
- Timestamp:
- 10/13/2020 11:55:05 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/blocks.php
r10273 r10376 5 5 use Error; 6 6 use function WordPressdotorg\Locales\get_locale_name_from_code; 7 use function WPOrg_Learn\{ get_build_path, get_build_url }; 8 use function WPOrg_Learn\Form\render_workshop_application_form; 7 9 use function WPOrg_Learn\Post_Meta\get_workshop_duration; 8 10 … … 12 14 * Actions and filters. 13 15 */ 14 add_action( 'init', __NAMESPACE__ . '\ workshop_details_init' );16 add_action( 'init', __NAMESPACE__ . '\register_types' ); 15 17 add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_block_style_assets' ); 16 18 add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_block_style_assets' ); 17 19 18 20 /** 19 * Registers all block assets so that they can be enqueued through the block editor 20 * in the corresponding context. 21 * 22 * @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/applying-styles-with-stylesheets/ 21 * Register block types. 22 * 23 * @return void 24 */ 25 function register_types() { 26 register_workshop_details(); 27 register_workshop_application_form(); 28 } 29 30 /** 31 * Register Workshop Details block type and related assets. 32 * 23 33 * @throws Error If the build files are not found. 24 34 */ 25 function workshop_details_init() { 26 $dir = dirname( __DIR__ ); 27 28 $script_asset_path = "$dir/build/workshop-details.asset.php"; 29 if ( ! file_exists( $script_asset_path ) ) { 35 function register_workshop_details() { 36 $script_asset_path = get_build_path() . 'workshop-details.asset.php'; 37 if ( ! is_readable( $script_asset_path ) ) { 30 38 throw new Error( 31 39 'You need to run `npm start` or `npm run build` for the "wporg-learn/workshop-details" block first.' … … 36 44 wp_register_script( 37 45 'workshop-details-editor-script', 38 plugins_url( 'build/workshop-details.js', 'wporg-learn/wporg-learn.php' ),46 get_build_url() . 'workshop-details.js', 39 47 $script_asset['dependencies'], 40 48 $script_asset['version'] 41 49 ); 42 50 43 $editor_css = 'build/workshop-details.css';44 51 wp_register_style( 45 52 'workshop-details-editor-style', 46 plugins_url( $editor_css, 'wporg-learn/wporg-learn.php' ),53 get_build_url() . 'workshop-details.css', 47 54 array(), 48 filemtime( "$dir/$editor_css" ) 49 ); 50 51 $style_css = 'build/style-workshop-details.css'; 55 filemtime( get_build_path() . 'workshop-details.css' ) 56 ); 57 52 58 wp_register_style( 53 59 'workshop-details-style', 54 plugins_url( $style_css, 'wporg-learn/wporg-learn.php' ),60 get_build_url() . 'style-workshop-details.css', 55 61 array(), 56 filemtime( "$dir/$style_css")62 filemtime( get_build_path() . 'style-workshop-details.css' ) 57 63 ); 58 64 … … 123 129 124 130 /** 131 * Register Workshop Application Form block type and related assets. 132 * 133 * @throws Error If the build files are not found. 134 */ 135 function register_workshop_application_form() { 136 $script_asset_path = get_build_path() . 'workshop-application-form.asset.php'; 137 if ( ! is_readable( $script_asset_path ) ) { 138 throw new Error( 139 'You need to run `npm start` or `npm run build` for the "wporg-learn/workshop-application-form" block first.' 140 ); 141 } 142 143 $script_asset = require $script_asset_path; 144 wp_register_script( 145 'workshop-application-form-editor-script', 146 get_build_url() . 'workshop-application-form.js', 147 $script_asset['dependencies'], 148 $script_asset['version'] 149 ); 150 151 $script_asset_path = get_build_path() . 'form.asset.php'; 152 if ( ! is_readable( $script_asset_path ) ) { 153 throw new Error( 154 'You need to run `npm start` or `npm run build` first.' 155 ); 156 } 157 158 $script_asset = require $script_asset_path; 159 wp_register_script( 160 'workshop-application-form-script', 161 get_build_url() . 'form.js', 162 array_merge( $script_asset['dependencies'], array( 'jquery', 'select2' ) ), 163 $script_asset['version'], 164 true 165 ); 166 167 register_block_type( 'wporg-learn/workshop-application-form', array( 168 'editor_script' => 'workshop-application-form-editor-script', 169 'script' => 'workshop-application-form-script', 170 'style' => 'select2', 171 'render_callback' => __NAMESPACE__ . '\workshop_application_form_render_callback', 172 ) ); 173 } 174 175 /** 176 * Render the Workshop Application Form block markup. 177 * 178 * @return string 179 */ 180 function workshop_application_form_render_callback() { 181 return render_workshop_application_form(); 182 } 183 184 /** 125 185 * Enqueue scripts and stylesheets for custom block styles. 126 186 * … … 128 188 */ 129 189 function enqueue_block_style_assets() { 130 $dir = dirname( __DIR__ );131 132 190 if ( is_admin() ) { 133 $script_asset_path = "$dir/build/workshop-details.asset.php";191 $script_asset_path = get_build_path() . 'block-styles.asset.php'; 134 192 if ( ! file_exists( $script_asset_path ) ) { 135 193 throw new Error( 136 'You need to run `npm start` or `npm run build` for the "wporg-learn/workshop-details" blockfirst.'194 'You need to run `npm start` or `npm run build` for block styles first.' 137 195 ); 138 196 } … … 141 199 wp_enqueue_script( 142 200 'wporg-learn-block-styles', 143 plugins_url( 'build/block-styles.js', 'wporg-learn/wporg-learn.php' ),201 get_build_url() . 'block-styles.js', 144 202 $script_asset['dependencies'], 145 203 $script_asset['version'] … … 149 207 wp_enqueue_style( 150 208 'wporg-learn-block-styles', 151 plugins_url( 'build/style-block-styles.css', 'wporg-learn/wporg-learn.php' ),209 get_build_url() . 'style-block-styles.css', 152 210 array(), 153 filemtime( $dir . '/build/style-block-styles.css' )154 ); 155 } 211 filemtime( get_build_path() . 'style-block-styles.css' ) 212 ); 213 }
Note: See TracChangeset
for help on using the changeset viewer.