Skip to content

Commit

Permalink
Merge branch 'develop' into feature/11-curated-post-title
Browse files Browse the repository at this point in the history
  • Loading branch information
ravichdev committed Apr 8, 2021
2 parents 72e802f + 3168e21 commit fe92a90
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 88 deletions.
4 changes: 4 additions & 0 deletions plugin/assets/css/src/settings/api.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@
height: 100%;
width: 100%;
}

& .mdc-action__remove {
color: #dd5d5f;
}
}
1 change: 1 addition & 0 deletions plugin/assets/css/src/settings/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

@import "@material/dialog/dist/mdc.dialog.css";
@import "./base.css";
@import "./updater.css";
@import "./switch.css";
Expand Down
4 changes: 4 additions & 0 deletions plugin/assets/css/src/settings/updater.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@
border-radius: 8px;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
margin-bottom: 15px;

&.no__last-update .mdc-typography--headline6 {
margin-bottom: 0;
}
}
1 change: 0 additions & 1 deletion plugin/assets/src/common/components/dialog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
import classNames from 'classnames';
import { MDCDialog } from '@material/dialog';
import '@material/dialog/dist/mdc.dialog.css';

/**
* WordPress dependencies
Expand Down
2 changes: 1 addition & 1 deletion plugin/assets/src/settings/components/integrations/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const Api = () => {
</button>
<button
type="button"
className="mdc-button mdc-dialog__button"
className="mdc-button mdc-dialog__button mdc-action__remove"
onClick={ removeApiKey }
>
<div className="mdc-button__ripple"></div>
Expand Down
2 changes: 2 additions & 0 deletions plugin/assets/src/settings/components/integrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const Integrations = () => {
checked={ state.updaters[ key ].autoUpdates }
lastUpdated={ state.updaters[ key ].lastUpdated }
type={ UPDATERS[ key ].type }
displayUpdatedOn={ UPDATERS[ key ].displayUpdatedOn }
versionAvailable={ UPDATERS[ key ].versionAvailable }
/>
) ) }
</div>
Expand Down
30 changes: 26 additions & 4 deletions plugin/assets/src/settings/components/integrations/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/**
* External dependencies
*/
import classNames from 'classnames';
import _uniqueId from 'lodash/uniqueId';

/**
Expand All @@ -35,7 +36,15 @@ import Switch from './switch';
import Button from '../../../wizard/components/navigation/button';
import { update, toggleAutoUpdate } from '../../utils';

const Updater = ( { title, lastUpdated, needsKey, checked, type } ) => {
const Updater = ( {
title,
lastUpdated,
needsKey,
checked,
type,
displayUpdatedOn,
versionAvailable,
} ) => {
const [ id ] = useState( _uniqueId( 'updater-' ) );
const { state, dispatch } = useContext( SettingsContext );
const isDisabled = needsKey && 'ok' !== state.apiStatus;
Expand Down Expand Up @@ -76,11 +85,15 @@ const Updater = ( { title, lastUpdated, needsKey, checked, type } ) => {

const handleAutoUpdateToggle = () => {
dispatch( { type: ACTIONS.TOGGLE_UPDATES, payload: { type } } );
toggleAutoUpdate( type );
toggleAutoUpdate( type, checked );
};

return (
<div className="material-settings__updater">
<div
className={ classNames( 'material-settings__updater', {
'no__last-update': false === displayUpdatedOn && ! versionAvailable,
} ) }
>
<div className="mdc-layout-grid">
<div className="mdc-layout-grid__inner">
<div className="mdc-layout-grid__cell mdc-layout-grid__cell--span-7 mdc-layout-grid__cell--align-middle">
Expand All @@ -104,14 +117,23 @@ const Updater = ( { title, lastUpdated, needsKey, checked, type } ) => {
></p>
) }

{ ! isDisabled && (
{ ! isDisabled && false !== displayUpdatedOn && (
<p className="mdc-typography--body1">
{ sprintf(
__( 'Last update on %s', 'material-design' ),
updatedDate
) }
</p>
) }

{ versionAvailable && (
<p className="mdc-typography--body1">
{ sprintf(
__( 'New version %s is available.', 'material-design' ),
versionAvailable
) }
</p>
) }
</div>

<div className="mdc-layout-grid__cell mdc-layout-grid__cell--span-3 mdc-layout-grid__cell--align-middle material-settings__cell--justify-end">
Expand Down
30 changes: 29 additions & 1 deletion plugin/assets/src/settings/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const ACTIONS = {
SET_UPDATED: 'SET_UPDATED',
};

export const UPDATERS = {
const ASSET_UPDATES = {
FONTS: {
title: __( 'Google Fonts', 'material-design' ),
type: 'FONTS',
Expand All @@ -53,4 +53,32 @@ export const UPDATERS = {
},
};

const CORE_UPDATES = {
PLUGIN: {
title: __( 'Material Design Plugin', 'material-design' ),
type: 'PLUGIN',
lastUpdated: false,
needsKey: false,
updateAvailable: !! String( getConfig( 'pluginUpdateStatus' ) ),
versionAvailable: getConfig( 'pluginUpdateStatus' ),
autoUpdates: parseInt( getConfig( 'pluginAutoUpdate' ) || 0, 10 ),
displayUpdatedOn: false,
},
THEME: {
title: __( 'Material Design Theme', 'material-design' ),
type: 'THEME',
lastUpdated: false,
needsKey: false,
updateAvailable: !! String( getConfig( 'themeUpdateStatus' ) ),
versionAvailable: getConfig( 'themeUpdateStatus' ),
autoUpdates: parseInt( getConfig( 'themeAutoUpdate' ) || 0, 10 ),
displayUpdatedOn: false,
},
};

export const UPDATERS = {
...ASSET_UPDATES,
...( getConfig( 'coreUpdatesEnabled' ) ? CORE_UPDATES : {} ),
};

export const KEY_PLACEHOLDER = '•••••••••••••••••••••••••••••';
32 changes: 31 additions & 1 deletion plugin/assets/src/settings/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export const update = type => {
if ( type === UPDATERS.ICONS.type ) {
return updateIcons();
}

if ( isCoreUpdate( type ) ) {
return new Promise( () => {
window.location.href = getConfig( 'coreUpdateUrl' );
} );
}
};

/**
Expand Down Expand Up @@ -74,6 +80,9 @@ const updateIcons = () => {
} );
};

const isCoreUpdate = type =>
[ UPDATERS.PLUGIN.type, UPDATERS.THEME.type ].includes( type );

/**
* Save API Key in database
*
Expand All @@ -99,10 +108,31 @@ export const setApiKey = key => {
* Turn auto updates on/off
*
* @param {string} type Item to toggle updates
* @param {boolean} currentStatus Current status of the toggle.
* @return {Promise} Request response
*/
export const toggleAutoUpdate = type => {
export const toggleAutoUpdate = ( type, currentStatus ) => {
return new Promise( ( resolve, reject ) => {
if ( isCoreUpdate( type ) ) {
// eslint-disable-next-line no-undef
const body = new FormData();
body.append( 'action', 'toggle-auto-updates' );
body.append( '_ajax_nonce', getConfig( 'autoUpdateNonce' ) );
body.append( 'state', currentStatus ? 'disable' : 'enable' );
body.append( 'type', type.toLowerCase() );
body.append( 'asset', getConfig( `${ type.toLowerCase() }AssetName` ) );

apiFetch( {
url: getConfig( 'autoUpdateUrl' ),
method: 'POST',
body,
} )
.then( resolve )
.catch( reject );

return;
}

apiFetch( {
path: getConfig( 'assetsRestPath' ) + 'toggle-auto-updates',
method: 'POST',
Expand Down
78 changes: 32 additions & 46 deletions plugin/php/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public function init() {
add_action( 'switch_theme', [ $this, 'switch_theme_material' ], 10, 2 );
add_action( 'admin_notices', [ $this, 'theme_not_installed_notice' ], 10, 2 );
add_action( 'admin_notices', [ $this, 'plugin_activated_notice' ], 9, 2 );
add_filter( 'auto_update_plugin', [ $this, 'enable_plugin_auto_update' ], 10, 2 );
add_filter( 'auto_update_theme', [ $this, 'enable_theme_auto_update' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -291,22 +289,42 @@ public function enqueue_assets() {
true
);

$plugin_file = 'material-design/material-design.php';
$plugin_updates = get_site_transient( 'update_plugins' );
$theme_updates = get_site_transient( 'update_themes' );

$plugin_update = isset( $plugin_updates->response[ $plugin_file ] ) ? (object) $plugin_updates->response[ $plugin_file ] : false;
$theme_update = isset( $theme_updates->response[ Plugin::THEME_SLUG ] ) ? (object) $theme_updates->response[ Plugin::THEME_SLUG ] : false;

$plugin_status = ! empty( $plugin_update ) ? $plugin_update->new_version : '';
$theme_status = ! empty( $theme_update ) ? $theme_update->new_version : '';

wp_localize_script(
'material-settings',
'materialDesignWizard',
[
'restPath' => esc_url( $this->plugin->onboarding_rest_controller->get_base_path() ),
'redirect' => esc_url( admin_url( 'themes.php' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'themeStatus' => esc_html( $this->plugin->theme_status() ),
'assetsRestPath' => esc_url( $this->plugin->assets_rest_controller->get_base_path() ),
'apiStatus' => esc_html( $this->plugin->assets_rest_controller->get_api_status() ),
'fontsLastUpdated' => esc_html( $this->plugin->assets_rest_controller->get_fonts_last_updated() ),
'iconsLastUpdated' => esc_html( $this->plugin->assets_rest_controller->get_icons_last_updated() ),
'fontsAutoUpdate' => esc_html( $this->plugin->assets_rest_controller->get_fonts_auto_update() ),
'iconsAutoUpdate' => esc_html( $this->plugin->assets_rest_controller->get_icons_auto_update() ),
'fontsUpdateStatus' => esc_html( $this->plugin->assets_rest_controller->get_fonts_update_status() ),
'iconsUpdateStatus' => esc_html( $this->plugin->assets_rest_controller->get_icons_update_status() ),
'restPath' => esc_url( $this->plugin->onboarding_rest_controller->get_base_path() ),
'redirect' => esc_url( admin_url( 'themes.php' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'themeStatus' => esc_html( $this->plugin->theme_status() ),
'assetsRestPath' => esc_url( $this->plugin->assets_rest_controller->get_base_path() ),
'apiStatus' => esc_html( $this->plugin->assets_rest_controller->get_api_status() ),
'fontsLastUpdated' => esc_html( $this->plugin->assets_rest_controller->get_fonts_last_updated() ),
'iconsLastUpdated' => esc_html( $this->plugin->assets_rest_controller->get_icons_last_updated() ),
'fontsAutoUpdate' => esc_html( $this->plugin->assets_rest_controller->get_fonts_auto_update() ),
'iconsAutoUpdate' => esc_html( $this->plugin->assets_rest_controller->get_icons_auto_update() ),
'fontsUpdateStatus' => esc_html( $this->plugin->assets_rest_controller->get_fonts_update_status() ),
'iconsUpdateStatus' => esc_html( $this->plugin->assets_rest_controller->get_icons_update_status() ),
'pluginAutoUpdate' => in_array( $plugin_file, get_site_option( 'auto_update_plugins', [] ), true ),
'themeAutoUpdate' => in_array( Plugin::THEME_SLUG, get_site_option( 'auto_update_themes', [] ), true ),
'pluginUpdateStatus' => $plugin_status,
'themeUpdateStatus' => $theme_status,
'coreUpdatesEnabled' => version_compare( get_bloginfo( 'version' ), '5.5', '>=' ),
'coreUpdateUrl' => admin_url( 'update-core.php' ),
'autoUpdateNonce' => wp_create_nonce( 'updates' ),
'autoUpdateUrl' => admin_url( 'admin-ajax.php' ),
'pluginAssetName' => $plugin_file,
'themeAssetName' => Plugin::THEME_SLUG,
]
);
}
Expand Down Expand Up @@ -462,36 +480,4 @@ public function plugin_activated_notice() {
)
);
}

/**
* Enable auto updates for the plugin.
*
* @param bool|null $update Whether to update.
* @param object $item The update offer.
*
* @return bool
*/
public function enable_plugin_auto_update( $update, $item ) {
if ( 'material-design' === $item->slug ) {
return true;
}

return $update;
}

/**
* Enable auto updates for the theme.
*
* @param bool|null $update Whether to update.
* @param object $item The update offer.
*
* @return bool
*/
public function enable_theme_auto_update( $update, $item ) {
if ( Plugin::THEME_SLUG === $item->slug ) {
return true;
}

return $update;
}
}
34 changes: 0 additions & 34 deletions plugin/tests/phpunit/php/class-test-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public function test_construct() {
$this->assertEquals( 10, has_action( 'switch_theme', [ $this->plugin->admin, 'switch_theme_material' ] ) );
$this->assertEquals( 10, has_action( 'admin_notices', [ $this->plugin->admin, 'theme_not_installed_notice' ] ) );
$this->assertEquals( 9, has_action( 'admin_notices', [ $this->plugin->admin, 'plugin_activated_notice' ] ) );
$this->assertEquals( 10, has_filter( 'auto_update_plugin', [ $this->plugin->admin, 'enable_plugin_auto_update' ] ) );
$this->assertEquals( 10, has_filter( 'auto_update_theme', [ $this->plugin->admin, 'enable_theme_auto_update' ] ) );
}


Expand Down Expand Up @@ -274,38 +272,6 @@ public function test_plugin_activated_notice() {
$this->assertContains( '<div class="notice notice-info is-dismissible material-notice-container">', $output );
}

/**
* Test for enable_plugin_auto_update() method.
*
* @see Plugin::enable_plugin_auto_update()
*/
public function test_enable_plugin_auto_update() {
$obj = new \stdClass();
$obj->slug = 'generic-plugin';

$this->assertEquals( null, $this->admin->enable_plugin_auto_update( null, $obj ) );
$this->assertEquals( false, $this->admin->enable_plugin_auto_update( false, $obj ) );

$obj->slug = 'material-design';
$this->assertEquals( true, $this->admin->enable_plugin_auto_update( null, $obj ) );
}

/**
* Test for enable_theme_auto_update() method.
*
* @see Plugin::enable_theme_auto_update()
*/
public function enable_theme_auto_update() {
$obj = new \stdClass();
$obj->slug = 'generic-theme';

$this->assertEquals( null, $this->admin->enable_theme_auto_update( null, $obj ) );
$this->assertEquals( false, $this->admin->enable_theme_auto_update( false, $obj ) );

$obj->slug = Plugin::THEME_SLUG;
$this->assertEquals( true, $this->admin->enable_theme_auto_update( null, $obj ) );
}

/**
* Return the material theme template.
*
Expand Down

0 comments on commit fe92a90

Please sign in to comment.