Making WordPress.org


Ignore:
Timestamp:
10/13/2020 11:55:05 PM (6 years ago)
Author:
coreymckrill
Message:

WordPress.org Learn: Sync with GitHub

​https://github.com/WordPress/learn/compare/348e99fc763e9bd460a6446b2484312e6e5e68db...116a8706331ea044839d22347433f98af8ab8ccb

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/blocks.php

    r10273 r10376  
    55use Error;
    66use function WordPressdotorg\Locales\get_locale_name_from_code;
     7use function WPOrg_Learn\{ get_build_path, get_build_url };
     8use function WPOrg_Learn\Form\render_workshop_application_form;
    79use function WPOrg_Learn\Post_Meta\get_workshop_duration;
    810
    … …  
    1214 * Actions and filters.
    1315 */
    14 add_action( 'init', __NAMESPACE__ . '\workshop_details_init' );
     16add_action( 'init', __NAMESPACE__ . '\register_types' );
    1517add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_block_style_assets' );
    1618add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_block_style_assets' );
    1719
    1820/**
    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 */
     25function register_types() {
     26        register_workshop_details();
     27        register_workshop_application_form();
     28}
     29
     30/**
     31 * Register Workshop Details block type and related assets.
     32 *
    2333 * @throws Error If the build files are not found.
    2434 */
    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 ) ) {
     35function register_workshop_details() {
     36        $script_asset_path = get_build_path() . 'workshop-details.asset.php';
     37        if ( ! is_readable( $script_asset_path ) ) {
    3038                throw new Error(
    3139                        'You need to run `npm start` or `npm run build` for the "wporg-learn/workshop-details" block first.'
    … …  
    3644        wp_register_script(
    3745                'workshop-details-editor-script',
    38                 plugins_url( 'build/workshop-details.js', 'wporg-learn/wporg-learn.php' ),
     46                get_build_url() . 'workshop-details.js',
    3947                $script_asset['dependencies'],
    4048                $script_asset['version']
    4149        );
    4250
    43         $editor_css = 'build/workshop-details.css';
    4451        wp_register_style(
    4552                'workshop-details-editor-style',
    46                 plugins_url( $editor_css, 'wporg-learn/wporg-learn.php' ),
     53                get_build_url() . 'workshop-details.css',
    4754                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
    5258        wp_register_style(
    5359                'workshop-details-style',
    54                 plugins_url( $style_css, 'wporg-learn/wporg-learn.php' ),
     60                get_build_url() . 'style-workshop-details.css',
    5561                array(),
    56                 filemtime( "$dir/$style_css" )
     62                filemtime( get_build_path() . 'style-workshop-details.css' )
    5763        );
    5864
    … …  
    123129
    124130/**
     131 * Register Workshop Application Form block type and related assets.
     132 *
     133 * @throws Error If the build files are not found.
     134 */
     135function 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 */
     180function workshop_application_form_render_callback() {
     181        return render_workshop_application_form();
     182}
     183
     184/**
    125185 * Enqueue scripts and stylesheets for custom block styles.
    126186 *
    … …  
    128188 */
    129189function enqueue_block_style_assets() {
    130         $dir = dirname( __DIR__ );
    131 
    132190        if ( is_admin() ) {
    133                 $script_asset_path = "$dir/build/workshop-details.asset.php";
     191                $script_asset_path = get_build_path() . 'block-styles.asset.php';
    134192                if ( ! file_exists( $script_asset_path ) ) {
    135193                        throw new Error(
    136                                 'You need to run `npm start` or `npm run build` for the "wporg-learn/workshop-details" block first.'
     194                                'You need to run `npm start` or `npm run build` for block styles first.'
    137195                        );
    138196                }
    … …  
    141199                wp_enqueue_script(
    142200                        'wporg-learn-block-styles',
    143                         plugins_url( 'build/block-styles.js', 'wporg-learn/wporg-learn.php' ),
     201                        get_build_url() . 'block-styles.js',
    144202                        $script_asset['dependencies'],
    145203                        $script_asset['version']
    … …  
    149207        wp_enqueue_style(
    150208                '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',
    152210                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.