Skip to content

Commit

Permalink
Merge pull request #131 from material-components/feature/hide-title
Browse files Browse the repository at this point in the history
Allow hiding page title
  • Loading branch information
emeaguiar committed Jun 1, 2021
2 parents 7e6a289 + 6dad89a commit b731d37
Show file tree
Hide file tree
Showing 14 changed files with 6,093 additions and 1,619 deletions.
17 changes: 17 additions & 0 deletions bin/phpunit-theme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


docker-compose run --rm -u 1000 --workdir=/var/www/html/wp-content/themes/material-design-google wordpress -- composer test
7,173 changes: 5,565 additions & 1,608 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@
"@wordpress/dom": "2.8.0",
"@wordpress/dom-ready": "2.8.0",
"@wordpress/e2e-test-utils": "4.16.0",
"@wordpress/edit-post": "^4.1.0",
"@wordpress/element": "^3.1.0",
"@wordpress/escape-html": "1.11.0",
"@wordpress/eslint-plugin": "4.1.0",
"@wordpress/i18n": "3.10.0",
"@wordpress/jest-preset-default": "5.5.0",
"@wordpress/jest-puppeteer-axe": "1.6.0",
"@wordpress/plugins": "^3.1.0",
"@wordpress/postcss-themes": "2.3.0",
"@wordpress/rich-text": "3.24.1",
"@wordpress/scripts": "12.6.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe( 'EditWithSelect', () => {
} );

it( 'set the correct arguments when the block is the "Hand-picked posts"', () => {
const lodash = require.requireActual( 'lodash' );
const lodash = jest.requireActual( 'lodash' );
jest.spyOn( lodash, 'pickBy' ).mockImplementation( () => 1 );

const props = {
Expand All @@ -186,7 +186,7 @@ describe( 'EditWithSelect', () => {
} );

it( 'set the correct arguments when the block is the "Hand-picked posts" and title ordering', () => {
const lodash = require.requireActual( 'lodash' );
const lodash = jest.requireActual( 'lodash' );
jest.spyOn( lodash, 'pickBy' ).mockImplementation( () => 1 );

const props = {
Expand All @@ -212,7 +212,7 @@ describe( 'EditWithSelect', () => {
} );

it( 'set the correct arguments when the block is the "Hand-picked posts" and popularity ordering', () => {
const lodash = require.requireActual( 'lodash' );
const lodash = jest.requireActual( 'lodash' );
jest.spyOn( lodash, 'pickBy' ).mockImplementation( () => 1 );

const props = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe( 'EditWithSelect', () => {
} );

it( 'set the correct arguments when the block is the "Hand-picked posts"', () => {
const lodash = require.requireActual( 'lodash' );
const lodash = jest.requireActual( 'lodash' );
jest.spyOn( lodash, 'pickBy' ).mockImplementation( () => 1 );

const props = {
Expand All @@ -188,7 +188,7 @@ describe( 'EditWithSelect', () => {
} );

it( 'set the correct arguments when the block is the "Hand-picked posts" and title ordering', () => {
const lodash = require.requireActual( 'lodash' );
const lodash = jest.requireActual( 'lodash' );
jest.spyOn( lodash, 'pickBy' ).mockImplementation( () => 1 );

const props = {
Expand All @@ -214,7 +214,7 @@ describe( 'EditWithSelect', () => {
} );

it( 'set the correct arguments when the block is the "Hand-picked posts" and popularity ordering', () => {
const lodash = require.requireActual( 'lodash' );
const lodash = jest.requireActual( 'lodash' );
jest.spyOn( lodash, 'pickBy' ).mockImplementation( () => 1 );

const props = {
Expand Down
3 changes: 3 additions & 0 deletions theme/assets/css/src/front-end.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
@import "./base/index.css";
@import "./templates/index.css";
@import "./components/index.css";

/* Block editor */
@import "./../../src/block-editor/plugins/hide-sections/style.css";
17 changes: 17 additions & 0 deletions theme/assets/src/block-editor/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import './plugins/hide-sections';
119 changes: 119 additions & 0 deletions theme/assets/src/block-editor/plugins/hide-sections/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Wordpress Dependencies
*/
import { registerPlugin } from '@wordpress/plugins';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
import { Icon, CheckboxControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { compose, ifCondition } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';

function HideSection( { settings, updateSectionsSettings } ) {
/**
* If you add a new property here, make sure to also add it
* in the "properties" array at block-editor.php.
*/
const settingsOptionsInitial = [
{
label: __( 'Hide Title', 'material' ),
key: 'title',
},
];

settingsOptionsInitial.forEach( settingOption => {
settingOption.value = settings[ settingOption.key ]
? settings[ settingOption.key ]
: false;
} );

const [ settingsOptions, setSettingOptions ] = useState(
settingsOptionsInitial
);

return (
<PluginDocumentSettingPanel
name="sections-control"
title={ __( 'Hide Sections', 'material' ) }
className="custom-panel"
>
{ settingsOptions.map( settingOption => {
return (
<CheckboxControl
checked={
settings[ settingOption.key ]
? settings[ settingOption.key ]
: false
}
onChange={ state => {
const newSettings = { ...settings };
newSettings[ settingOption.key ] = state;
const newSettingsOptions = settingsOptions;
newSettingsOptions.forEach( newSettingOption => {
if ( newSettingOption.key === settingOption.key ) {
newSettingOption.value = state;
}
} );
updateSectionsSettings( newSettings );
setSettingOptions( newSettingsOptions );
} }
label={ settingOption.label }
key={ settingOption.key }
/>
);
} ) }
</PluginDocumentSettingPanel>
);
}

const WrappedSectionControl = compose( [
withSelect( select => {
const { getCurrentPostType } = select( 'core/editor' );
const { getPostType } = select( 'core' );
return {
postTypeObj: getPostType( getCurrentPostType() ),
};
} ),
ifCondition( ( { postTypeObj } ) => {
return postTypeObj?.supports?.[ 'custom-fields' ];
} ),
withSelect( select => {
const { getEditedPostAttribute } = select( 'core/editor' );
return {
settings: getEditedPostAttribute( 'meta' )[ 'material-hide-sections' ],
};
} ),
withDispatch( dispatch => {
return {
updateSectionsSettings( settings ) {
dispatch( 'core/editor' ).editPost( {
meta: { 'material-hide-sections': settings },
} );
},
};
} ),
ifCondition( ( { settings } ) => {
return !! settings;
} ),
] )( HideSection );

registerPlugin( 'material-hide-sections', {
render: WrappedSectionControl,
icon: <Icon icon="admin-customizer" />,
} );
37 changes: 37 additions & 0 deletions theme/assets/src/block-editor/plugins/hide-sections/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

.has-hide-title {

.entry-title {
display: none;
}

.-has-tab-bar + .site-content .content-area {
margin-top: 7rem;
}

.-has-tab-bar + .site-content .content-area {

@media (max-width: 599px) {
margin-top: 6rem;
}
}

.entry-content .wp-block-cover:first-child {
margin-top: 0;
}
}
6 changes: 6 additions & 0 deletions theme/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,16 @@ function material_is_plugin_active() {
require get_template_directory() . '/inc/widgets/class-wp-widget-rss.php';
require get_template_directory() . '/inc/widgets.php';

/**
* Block editor.
*/
require get_template_directory() . '/inc/block-editor.php';

MaterialDesign\Theme\Admin\setup();
MaterialDesign\Theme\Customizer\setup();
MaterialDesign\Theme\Customizer\Colors\setup();
MaterialDesign\Theme\Customizer\Header_Footer\setup();
MaterialDesign\Theme\Customizer\Layout\setup();
MaterialDesign\Theme\Customizer\Menu\setup();
MaterialDesign\Theme\Widgets\setup();
MaterialDesign\Theme\BlockEditor\setup();
98 changes: 98 additions & 0 deletions theme/inc/block-editor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package MaterialDesign
*/

namespace MaterialDesign\Theme\BlockEditor;

/**
* Add hooks and filters.
*/
function setup() {
add_action( 'init', __NAMESPACE__ . '\\register_disable_section_meta' );
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_block_editor_assets' );
add_action( 'body_class', __NAMESPACE__ . '\\filter_body_class' );
}

/**
* Register disable section meta.
*/
function register_disable_section_meta() {
register_post_meta(
'',
'material-hide-sections',
[
'type' => 'object',
'description' => 'Whether the title should be shown in frontend.',
'object_type' => 'post',
'single' => true,
'default' => [],
'show_in_rest' => [
'schema' => [
'type' => 'object',
/**
* If you add a new property here, make sure to also add it
* in the "settingsOptionsInitial" object here:
* assets/src/js/block-editor/plugins/hide-sections/index.js
*/
'properties' => [
'title' => [ 'type' => 'boolean' ],
],
'additionalProperties' => [
'type' => 'boolean',
],
],
],
]
);
}

/**
* Enqueue block editor assets.
*/
function enqueue_block_editor_assets() {
$asset_file = get_stylesheet_directory() . '/assets/js/block-editor.asset.php';
$asset = is_readable( $asset_file ) ? require $asset_file : [];
$version = isset( $asset['version'] ) ? $asset['version'] : wp_get_theme()->get( 'Version' );
$dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : [];

wp_enqueue_script(
'material-block-editor-js-theme',
get_stylesheet_directory_uri() . '/assets/js/block-editor.js',
$dependencies,
$version,
false
);
}

/**
* Filter body class.
*
* @param array $classes Body class.
*
* @return array
*/
function filter_body_class( $classes ) {
if ( is_single() || is_page() ) {
$meta = get_post_meta( get_the_ID(), 'material-hide-sections', true );
if ( ! empty( $meta['title'] ) ) {
$classes[] = 'has-hide-title';
}
}

return $classes;
}
Loading

0 comments on commit b731d37

Please sign in to comment.