Skip to content

Commit

Permalink
Merge pull request #4 from alleyinteractive/feature
Browse files Browse the repository at this point in the history
Base functionality of the modified date control plugin
  • Loading branch information
srtfisher authored Aug 7, 2024
2 parents 25d8899 + 8e7b30c commit c431df8
Show file tree
Hide file tree
Showing 28 changed files with 831 additions and 565 deletions.
17 changes: 0 additions & 17 deletions .scaffolder/plugin-feature/config.yml

This file was deleted.

27 changes: 0 additions & 27 deletions .scaffolder/plugin-feature/feature.php.hbs

This file was deleted.

25 changes: 0 additions & 25 deletions .scaffolder/plugin-feature/test.php.hbs

This file was deleted.

2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

All notable changes to `Modified Date Control` will be documented in this file.

## 0.1.0 - 202X-XX-XX
## 1.0.0 - 2024-08-02

- Initial release
126 changes: 46 additions & 80 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Contributors: alleyinteractive

Tags: alleyinteractive, wp-modified-date-control

Stable tag: 0.0.0
Stable tag: 1.0.0

Requires at least: 5.9

Expand All @@ -14,110 +14,76 @@ Requires PHP: 8.1

License: GPL v2 or later

[![Coding Standards](https://github.com/alleyinteractive/wp-modified-date-control/actions/workflows/coding-standards.yml/badge.svg)](https://github.com/alleyinteractive/wp-modified-date-control/actions/workflows/coding-standards.yml)
[![Testing Suite](https://github.com/alleyinteractive/wp-modified-date-control/actions/workflows/unit-test.yml/badge.svg)](https://github.com/alleyinteractive/wp-modified-date-control/actions/workflows/unit-test.yml)
[![Testing Suite](https://github.com/alleyinteractive/wp-modified-date-control/actions/workflows/all-pr-tests.yml/badge.svg)](https://github.com/alleyinteractive/wp-modified-date-control/actions/workflows/all-pr-tests.yml)

Control the modified date for a post with Gutenberg..
Control the modified date for a post with Gutenberg.

## Installation

You can install the package via composer:
You can install the package via Composer:

```bash
composer require alleyinteractive/wp-modified-date-control
```

## Usage

Activate the plugin in WordPress and use it like so:
Activate the plugin in WordPress and you will see a new panel available under
the "Summary" section in Gutenberg:

```php
$plugin = Create_WordPress_Plugin\Modified_Date_Control\Modified_Date_Control();
$plugin->perform_magic();
```

## Testing

Run `npm run test` to run Jest tests against JavaScript files. Run
`npm run test:watch` to keep the test runner open and watching for changes.

Run `npm run lint` to run ESLint against all JavaScript files. Linting will also
happen when running development or production builds.
![Screenshot](./assets/screenshot.png)

Run `composer test` to run tests against PHPUnit and the PHP code in the plugin.
Unit testing code is written in PSR-4 format and can be found in the `tests`
directory.

### The `entries` directory and entry points

All directories created in the `entries` directory can serve as entry points and will be compiled with [@wordpress/scripts](https://github.com/WordPress/gutenberg/blob/trunk/packages/scripts/README.md#scripts) into the `build` directory with an accompanied `index.asset.php` asset map.
The panel will allow you to set a modified date for a post or allow the modified
date to be set to the current date and time upon update. Out of the box, the
plugin will continue to allow updates to the modified date unless disabled on a
per-post basis.

#### Scaffolding an entry point
If you check `Allow Updates to Modified`, the modified date will be set to the
current date and time when the post is updated. You will be unable to set your
own modified date. If you uncheck the box, you will be able to set your own
modified date. Saving the post will not cause the modified date to be updated,
either, unless you manually updated it.

To generate a new entry point, run the following command:
> [!NOTE]
> This plugin does not have support for non-Gutenberg editors.
```sh
npm run create-entry
```

To generate a new slotfill, run the following command:
### Filters

```sh
npm run create-slotfill
```
#### `wp_modified_date_control_default_allow_updates`

The command will prompt the user through several options for creating an entry or slotfill. The entries are scaffolded with the `@alleyinteractive/create-entry` script. Run the help command to see all the options:

```sh
npx @alleyinteractive/create-entry --help
```
[Visit the package README](https://www.npmjs.com/package/@alleyinteractive/create-entry) for more information.
Modify the default meta value to allow updates to the modified date. By default,
the plugin will return true and allow updates to the modified date. This filter
allows you to override that behavior.

#### Enqueuing Entry Points

You can also include an `index.php` file in the entry point directory for enqueueing or registering a script. This file will then be moved to the build directory and will be auto-loaded with the `load_scripts()` function in the `functions.php` file. Alternatively, if a script is to be enqueued elsewhere there are helper functions in the `src/assets.php` file for getting the assets.

### Scaffold a dynamic block with `create-block`

Use the `create-block` command to create custom blocks with [@alleyinteractive/create-block](https://github.com/alleyinteractive/alley-scripts/tree/main/packages/create-block) script and follow the prompts to generate all the block assets in the `blocks/` directory.
Block registration, script creation, etc will be scaffolded from the `create-block` script. Run `npm run build` to compile and build the custom block. Blocks are enqueued using the `load_scripts()` function in `src/assets.php`.

### Updating WP Dependencies

Update the [WordPress dependency packages](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#packages-update) used in the project to their latest version.

To update `@wordpress` dependencies to their latest version use the packages-update command:

```sh
npx wp-scripts packages-update
```php
// Disable updates to the modified date by default.
add_filter( 'wp_modified_date_control_default_allow_updates', fn () => false );
```

This script provides the following custom options:
#### `wp_modified_date_control_prevent_updates`

- `--dist-tag` – allows specifying a custom dist-tag when updating npm packages. Defaults to `latest`. This is especially useful when using [`@wordpress/dependency-extraction-webpack-plugin`](https://www.npmjs.com/package/@wordpress/dependency-extraction-webpack-plugin). It lets installing the npm dependencies at versions used by the given WordPress major version for local testing, etc. Example:
Modify the default behavior of the plugin to allow/deny updates to the modified
date. By default, the plugin will prevent updates if the meta to allow updates
is set to `'false'`. This filter allows you to override that behavior.

```sh
npx wp-scripts packages-update --dist-tag=wp-WPVERSION`
```php
add_filter( 'wp_modified_date_control_prevent_updates', function( bool $prevent, int $post_id, ?\WP_REST_Request $request ) {
// Always allow updates to the modified date.
return false;
}, 10, 3 );
```

Where `WPVERSION` is the version of WordPress you are targeting. The version
must include both the major and minor version (e.g., `6.1`). For example:

```sh
npx wp-scripts packages-update --dist-tag=wp-6.1`
```
## Testing

## Releasing the Plugin
Run `npm run test` to run Jest tests against JavaScript files. Run
`npm run test:watch` to keep the test runner open and watching for changes.

The plugin uses a [built release workflow](./.github/workflows/built-release.yml)
to compile and tag releases. Whenever a new version is detected in the root
`composer.json` file or in the plugin's headers, the workflow will automatically
build the plugin and tag it with a new version. The built tag will contain all
the required front-end assets the plugin may require. This works well for
publishing to WordPress.org or for submodule-ing.
Run `npm run lint` to run ESLint against all JavaScript files. Linting will also
happen when running development or production builds.

When you are ready to release a new version of the plugin, you can run
`npm run release` to start the process of setting up a new release.
Run `composer test` to run tests against PHPUnit and the PHP code in the plugin.
Unit testing code is written in PSR-4 format and can be found in the `tests`
directory.

## Changelog

Expand All @@ -127,11 +93,11 @@ Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed re

This project is actively maintained by [Alley
Interactive](https://github.com/alleyinteractive). Like what you see? [Come work
with us](https://alley.co/careers/).
with us](https://alley.com/careers/).

- [Sean Fisher](https://github.com/Sean Fisher)
- [Sean Fisher](https://github.com/srtfisher)
- [All Contributors](../../contributors)

## License

The GNU General Public License (GPL) license. Please see [License File](LICENSE) for more information.
The GNU General Public License (GPL) license. Please see [License File](LICENSE) for more information.
Binary file added assets/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
"require": {
"php": "^8.1",
"alleyinteractive/composer-wordpress-autoloader": "^1.0",
"alleyinteractive/wp-type-extensions": "^2.1"
"alleyinteractive/wp-type-extensions": "^2.2"
},
"require-dev": {
"alleyinteractive/alley-coding-standards": "^2.0",
"mantle-framework/testkit": "^1.0",
"spatie/ray": "^1.41",
"szepeviktor/phpstan-wordpress": "^1.1"
},
"config": {
Expand Down
49 changes: 49 additions & 0 deletions entries/plugin/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Entry point "plugin" script registration and enqueue.
*
* This file will be copied to the assets output directory
* with Webpack using wp-scripts build. The build command must
* be run before this file will be available.
*
* This file must be included from the build output directory in a project.
* and will be loaded from there.
*
* @package wp-modified-date-control
*/

/**
* Register the plugin entry point assets so that they can be enqueued.
*/
function wp_modified_date_control_register_plugin_scripts(): void {
// Automatically load dependencies and version.
$asset_file = include __DIR__ . '/index.asset.php';

// Register the plugin script.
wp_register_script(
'wp-modified-date-control-plugin-js',
plugins_url( 'index.js', __FILE__ ),
$asset_file['dependencies'],
$asset_file['version'],
true
);
wp_set_script_translations( 'wp-modified-date-control-plugin-js', 'wp-modified-date-control' );

// Register the plugin style.
wp_register_style(
'wp-modified-date-control-plugin-css',
plugins_url( 'index.css', __FILE__ ),
[],
$asset_file['version'],
);
}
add_action( 'init', 'wp_modified_date_control_register_plugin_scripts' );

/**
* Enqueue styles/scripts for the plugin entry point.
*/
function wp_modified_date_control_enqueue_plugin(): void {
wp_enqueue_script( 'wp-modified-date-control-plugin-js' );
wp_enqueue_style( 'wp-modified-date-control-plugin-css' );
}
add_action( 'enqueue_block_assets', 'wp_modified_date_control_enqueue_plugin' );
10 changes: 10 additions & 0 deletions entries/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Entry for plugin sidebar.
*/

import { registerPlugin } from '@wordpress/plugins';
import Sidebar from './sidebar';

registerPlugin('wp-modified-date-control', {
render: Sidebar,
});
35 changes: 35 additions & 0 deletions entries/plugin/sidebar.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.postModifiedRow {
display: flex;
align-items: center;
flex-direction: row;
gap: calc(8px);
justify-content: space-between;
width: 100%;

button {
text-align: left;
white-space: unset;
}
}

.postModifiedPopover {
min-width: 320px;
padding: 16px;
}

.postModifiedPopoverHeader {
display: flex;

h3 {
line-height: 1.2;
margin: 0px;
color: rgb(30, 30, 30);
font-size: calc(13px);
font-weight: 600;
display: block;
}

button {
margin-left: auto;
}
}
Loading

0 comments on commit c431df8

Please sign in to comment.