Skip to content

Commit

Permalink
Fix the default options not being set, the settings menu ordering, do…
Browse files Browse the repository at this point in the history
…uble status creations and the lack of settings updated message
  • Loading branch information
ingeniumed committed Oct 8, 2024
1 parent a7b56a2 commit 086e8f9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
32 changes: 17 additions & 15 deletions modules/custom-status/custom-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,13 @@ public static function register_custom_status_taxonomy(): void {
public static function register_custom_statuses(): void {
global $wp_post_statuses;

// Users can delete the pending status if they want, so let's get rid of that
// It'll get re-added if the user hasn't "deleted" them
unset( $wp_post_statuses['pending'] );

$custom_statuses = self::get_custom_statuses();
$custom_statuses = self::get_custom_statuses();

// Unfortunately, register_post_status() doesn't accept a
// post type argument, so we have to register the post
// statuses for all post types. This results in
// all post statuses for a post type appearing at the top
// of manage posts if there is a post with the status
// Unfortunately, register_post_status() doesn't accept a
// post type argument, so we have to register the post
// statuses for all post types. This results in
// all post statuses for a post type appearing at the top
// of manage posts if there is a post with the status
foreach ( $custom_statuses as $status ) {
register_post_status( $status->slug, [
'label' => $status->name,
Expand Down Expand Up @@ -642,14 +638,17 @@ public static function update_custom_status( int $status_id, array $args = [] ):
// Reset our internal object cache
self::$custom_statuses_cache = [];

// Don't allow changing the name or slug of the Draft or pending status as they are core statuses
$banned_statuses = [ 'draft', 'pending' ];

// Prevent user from changing draft name or slug
if ( 'draft' === $old_status->slug
if ( in_array( $old_status->slug, $banned_statuses )
&& (
( isset( $args['name'] ) && $args['name'] !== $old_status->name )
||
( isset( $args['slug'] ) && $args['slug'] !== $old_status->slug )
) ) {
return new WP_Error( 'invalid', __( 'Changing the name and slug of "Draft" is not allowed', 'vip-workflow' ) );
return new WP_Error( 'restricted', __( 'Changing the name and slug of a restricted status ', 'vip-workflow' ) . '(' . $old_status->name . ') is not allowed.' );
}

// If the name was changed, we need to change the slug
Expand Down Expand Up @@ -727,16 +726,19 @@ public static function delete_custom_status( int $status_id ): bool|WP_Error {

$old_status_slug = $old_status->slug;

if ( self::is_restricted_status( $old_status_slug ) || 'draft' === $old_status_slug ) {
return new WP_Error( 'restricted', __( 'Restricted status ', 'vip-workflow' ) . '(' . $old_status->name . ')' );
// Don't allow changing the name or slug of the Draft or pending status as they are core statuses
$banned_statuses = [ 'draft', 'pending' ];

if ( self::is_restricted_status( $old_status_slug ) || in_array( $old_status->slug, $banned_statuses ) ) {
return new WP_Error( 'restricted', __( 'Restricted status ', 'vip-workflow' ) . '(' . $old_status->name . ') cannot be deleted.' );
}

// Reset our internal object cache
self::$custom_statuses_cache = [];

// Get the new status to reassign posts to, which would be the first custom status.
// In the event that the first custom status is being deleted, we'll reassign to the second custom status.
// Since draft cannot be deleted, we don't need to worry about ever getting index out of bounds.
// Since draft and pending review cannot be deleted, we don't need to worry about ever getting index out of bounds.
$custom_statuses = self::get_custom_statuses();
$new_status_slug = $custom_statuses[0]->slug;
if ( $old_status_slug === $new_status_slug ) {
Expand Down
4 changes: 2 additions & 2 deletions modules/settings/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Settings {
* Initialize the rest of the stuff in the class if the module is active
*/
public static function init(): void {
add_action( 'admin_menu', [ __CLASS__, 'add_admin_menu' ] );
// Ensures that the settings page shows up at the bottom of the menu list
add_action( 'admin_menu', [ __CLASS__, 'add_admin_menu' ], 50 );

add_action( 'admin_init', [ __CLASS__, 'helper_settings_validate_and_save' ], 100 );
add_action( 'admin_init', [ __CLASS__, 'register_settings' ] );
Expand Down Expand Up @@ -218,7 +219,6 @@ public static function settings_validate( $new_options ): array {
* This method is called automatically/ doesn't need to be registered anywhere
*/
public static function helper_settings_validate_and_save(): bool {

if ( ! isset( $_POST['action'], $_POST['_wpnonce'], $_POST['option_page'], $_POST['_wp_http_referer'], $_POST['submit'] ) || ! is_admin() ) {
return false;
}
Expand Down
8 changes: 7 additions & 1 deletion modules/settings/views/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@
<h3>
<?php esc_html_e( 'Configure VIP Workflow settings.', 'vip-workflow' ); ?>
<?php if ( $message_slug && isset( $messages[ $message_slug ] ) ) { ?>
<?php printf( '<span class="vip-workflow-updated-message vip-workflow-message">%s</span>', esc_html( $messages[ $message_slug ] ) ); ?>
<?php
wp_admin_notice( esc_html( $messages[ $message_slug ] ), [
'type' => 'success',
'dismissible' => true,
'additional_classes' => [ 'inline', 'notice-alt' ],
] );
?>
<?php } ?>
</h3>
</div>
Expand Down
22 changes: 21 additions & 1 deletion modules/shared/php/options-utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ class OptionsUtilities {
const OPTIONS_GROUP = 'vip_workflow_';
const OPTIONS_GROUP_NAME = 'vip_workflow_options';

// Its key to centralize these here, so we can easily update them in the future
private const DEFAULT_OPTIONS = [
'post_types' => [
'post' => 'on',
'page' => 'on',
],
'publish_guard' => 'on',
'email_address' => '',
'webhook_url' => '',
];

/**
* Given a module name, return a set of saved module options
*
Expand All @@ -21,7 +32,16 @@ class OptionsUtilities {
*/
public static function get_module_options( string $module_slug ): object|null {
$module_options_key = self::get_module_options_key( $module_slug );
return get_option( $module_options_key, new stdClass() );
$module_options = get_option( $module_options_key, new stdClass() );

// Ensure all default options are set
foreach ( self::DEFAULT_OPTIONS as $key => $value ) {
if ( ! isset( $module_options->$key ) ) {
$module_options->$key = $value;
}
}

return $module_options;
}

public static function get_module_option_by_key( string $module_slug, string $key ): string|array|bool|null {
Expand Down

0 comments on commit 086e8f9

Please sign in to comment.