Skip to content

Commit

Permalink
PLANET-7340: Make Post title mandatory for publishing (#2158)
Browse files Browse the repository at this point in the history
  • Loading branch information
lithrel authored Nov 30, 2023
1 parent 54cd7f5 commit 046d2ad
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/MasterSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ protected function hooks(): void
add_action('add_meta_boxes', [$this, 'add_meta_box_search'], 10, 2);
add_action('save_post', [$this, 'save_meta_box_search'], 10, 2);
add_action('save_post', [$this, 'set_featured_image'], 10, 2);
add_filter('wp_insert_post_data', [$this, 'require_post_title'], 10, 1);
// Save "p4_global_project_tracking_id" on post save.
add_action('save_post', [$this, 'save_global_project_id'], 10, 1);
add_action('post_updated', [$this, 'clean_post_cache'], 10, 3);
Expand Down Expand Up @@ -401,6 +402,24 @@ public function set_featured_image(int $post_id, WP_Post $post): void
set_post_thumbnail($post_id, $matches[1][0]);
}

/**
* Make post title mandatory on publish.
*/
public static function require_post_title(array $data): ?array
{
$types = Search\Filters\ContentTypes::get_all();
if (
empty($data['post_title'])
&& !empty($data['post_status'])
&& $data['post_status'] === 'publish'
&& in_array($data['post_type'], array_keys($types))
) {
wp_die(__('Title is a required field.', 'planet4-master-theme-backend'));
}

return $data;
}

/**
* Generates hreflang metadata from countries.json template.
*/
Expand Down

0 comments on commit 046d2ad

Please sign in to comment.