Making WordPress.org

Changeset 12770


Ignore:
Timestamp:
08/01/2023 10:07:18 PM (19 months ago)
Author:
coffee2code
Message:

Photo Directory, Posts: Prevent publication of photo post if any custom taxonomy does not have a value assigned.

See #7171.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/posts.php

    r12549 r12770  
    1010class Posts {
    1111
     12    const META_KEY_MISSING_TAXONOMIES = '_missing_taxonomies';
     13
    1214    /**
    1315     * Initializer.
     
    2224        add_filter( 'attachment_link',    [ __CLASS__, 'use_photo_url_instead_of_media_permalink_url' ], 10, 2 );
    2325
     26        // Ensure all custom taxonomies have been assigned values before publication.
     27        // Note: The add_action and hook priority are duplicated in `require_taxonomies_before_publishing()`.
     28        add_action( 'transition_post_status', [ __CLASS__, 'require_taxonomies_before_publishing' ], 1, 3 );
     29
    2430        // Sync photo post content to photo media on update.
    2531        add_action( 'post_updated',       [ __CLASS__, 'sync_photo_post_to_photo_media_on_update' ], 5, 3 );
     
    5359
    5460        return $response;
     61    }
     62
     63    /**
     64     * Prevents publication of a photo if any custom taxonomy hasn't been assigned
     65     * at least one value.
     66     *
     67     * @param string  $new_status The new post status.
     68     * @param string  $old_status The old post status.
     69     * @param WP_Post $post       The post object.
     70     */
     71    public static function require_taxonomies_before_publishing( $new_status, $old_status, $post ) {
     72        // Bail if post is not being published.
     73        if ( 'publish' !== $new_status ) {
     74            return;
     75        }
     76
     77        // Bail if not a photo post.
     78        if ( Registrations::get_post_type() !== $post->post_type ) {
     79            return;
     80        }
     81
     82        // Assume all custom taxonomies are required.
     83        $required_taxonomies = Registrations::get_taxonomy( 'all' );
     84        $missing_taxonomies = [];
     85
     86        // Check each required taxonomy.
     87        foreach ( $required_taxonomies as $taxonomy ) {
     88            $terms = wp_get_post_terms( $post->ID, $taxonomy, [ 'fields' => 'ids' ] );
     89            if ( count( $terms ) == 0 ) {
     90                $missing_taxonomies[] = $taxonomy;
     91            }
     92        }
     93
     94        if ( $missing_taxonomies ) {
     95            // Prevent publishing.
     96            remove_action( 'transition_post_status', [ __CLASS__, 'require_taxonomies_before_publishing' ], 1 );
     97            wp_update_post( [ 'ID' => $post->ID, 'post_status' => $old_status ] );
     98            add_action( 'transition_post_status', [ __CLASS__, 'require_taxonomies_before_publishing' ], 1, 3 );
     99
     100            // Store the missing taxonomies to later display them in an admin notice.
     101            update_post_meta( $post->ID, self::META_KEY_MISSING_TAXONOMIES, $missing_taxonomies );
     102        }
    55103    }
    56104
Note: See TracChangeset for help on using the changeset viewer.