Skip to content

Commit

Permalink
Add job shortcode block
Browse files Browse the repository at this point in the history
  • Loading branch information
ice9js committed Jul 9, 2018
1 parent 87dca88 commit b03cb49
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
33 changes: 33 additions & 0 deletions assets/blocks/job/edit.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* External dependencies
*/
import { find } from 'lodash';

/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import JobListing from './job-listing.jsx';
import SelectJobListing from './select-job-listing.jsx';

/**
* Edit UI for the Job block.
*/
const EditJob = ( { attributes, isSelected, jobListings, jobTypes, setAttributes } ) => {
const listing = find( jobListings, { id: attributes.jobId } );

if ( ! isSelected && listing ) {
return <JobListing listing={ listing } jobTypes={ jobTypes } />;
}

return <SelectJobListing attributes={ attributes } jobListings={ jobListings } setAttributes={ setAttributes } />;
};

export default withSelect( ( select, props ) => ( {
jobListings: select( 'wp-job-manager' ).getAllJobListings(),
jobTypes: select( 'wp-job-manager' ).getAllJobTypes()
} ) )( EditJob );
44 changes: 44 additions & 0 deletions assets/blocks/job/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';
import { applyFilters } from '@wordpress/hooks';

/**
* Internal dependencies
*/
import EditJob from './edit.jsx';
import './style.scss';
import registerStore from '../data';

const attributesConfig = {
jobId: {
type: 'integer',
default: 0,
}
};

const edit = ( { className, ...props } ) => (
<div className={ className }>
<EditJob { ...props } />
</div>
);

const save = ( { attributes } ) => {
if ( ! attributes.id ) {
return;
}

return `[job id="${ attributes.id }"]`;
};

registerStore();

registerBlockType( 'wp-job-manager/job', {
title: 'Job',
icon: 'list-view',
category: 'common',
attributes: applyFilters( 'wpjm_block_job_attributes_config', attributesConfig ),
edit,
save
} );
1 change: 1 addition & 0 deletions assets/build/blocks/job.js

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions includes/class-wp-job-manager-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,27 @@ public static function get_instance() {
* Instance constructor
*/
private function __construct() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}

add_action( 'init', array( $this, 'register_blocks' ) );
}

/**
* Register all Gutenblocks
*/
public function register_blocks() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}

// Add script includes for gutenblocks.
wp_register_script(
'wp-job-manager-block-job',
JOB_MANAGER_PLUGIN_URL . '/assets/build/blocks/job.js',
array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-hooks' ),
'0.1.0'
);
register_block_type( 'wp-job-manager/job', array(
'editor_script' => 'wp-job-manager-block-job',
) );
}
}

Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const webpackConfig = ( env, argv ) => {
},
plugins: [
new cleanWebpackPlugin( [ 'build/blocks' ] ),
new lodashModuleReplacementPlugin(),
new lodashModuleReplacementPlugin( { shorthands: true } ),
new miniCssExtractPlugin( {
filename: 'build/blocks/[name]/style.css'
} ),
Expand Down

0 comments on commit b03cb49

Please sign in to comment.