saving_product = $product; } } /** * Note the featured image a REST request is about to save. * * @param mixed $response Response so far (passed through). * @param array $handler Route handler. * @param \WP_REST_Request $request Request. * @return mixed */ public function remember_rest_image( $response, $handler, $request ) { if ( $request instanceof \WP_REST_Request && $request->has_param( 'featured_media' ) ) { $this->rest_image = absint( $request->get_param( 'featured_media' ) ); } return $response; } /** * Block a publish for a post that doesn't have a qualifying featured * image, rolling its status back and halting the request. * * @param string $new_status New post status. * @param string $old_status Old post status. * @param WP_Post $post The post. * @return void */ public function guard( string $new_status, string $old_status, WP_Post $post ): void { if ( 'publish' !== $new_status || ! $this->is_enabled() ) { return; } // Only guard a person publishing from an editor. Programmatic saves // can't show the wp_die() page and are left broken by it: the // WooCommerce CSV importer publishes each product before its image // meta is written, so the guard killed every batch's AJAX request // and the import hung forever. WP-CLI and cron have no one to show // the warning to either. if ( did_action( 'woocommerce_product_import_before_process_item' ) || wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { return; } if ( ! in_array( $post->post_type, $this->post_types(), true ) ) { return; } if ( strtotime( $post->post_date ) <= $this->enforcement_start() ) { return; } if ( $this->has_qualifying_image( $post ) ) { return; } // transition_post_status fires after the status change already // landed in the database, so roll it back here. A publish->publish // "transition" (re-saving a post that was already published when // this was turned on) must become an actual status change -- // otherwise wp_update_post() below would re-trigger this same // hook with new_status still 'publish', looping forever. A brand-new // post (published straight away, e.g. through the REST API) has the // pseudo-status 'new', which would hide it everywhere: use 'draft'. $old_status = in_array( $old_status, array( 'publish', 'new', 'auto-draft' ), true ) ? 'draft' : $old_status; wp_update_post( array( 'ID' => $post->ID, 'post_status' => $old_status, ) ); wp_die( esc_html( $this->warning_message() ), esc_html__( 'Featured image required', 'all-in-one-utilities' ), array( 'back_link' => true ) ); } /** * Whether a post has a featured image meeting the configured minimum * size (0 x 0, the default, means any size is accepted -- but an image * must still be present). * * @param WP_Post $post The post. * @return bool */ private function has_qualifying_image( WP_Post $post ): bool { $image_id = $this->pending_image_id( $post ); if ( ! $image_id ) { return false; } $min_width = $this->min_width(); $min_height = $this->min_height(); if ( ! $min_width && ! $min_height ) { return true; } $image = wp_get_attachment_image_src( $image_id, 'full' ); if ( ! $image ) { return false; } list( , $width, $height ) = $image; return $width >= $min_width && $height >= $min_height; } /** * The featured image a post is being published with: the one sent with * the REST request, else the one on the WooCommerce product being * saved (a new product has no ID until its insert finishes, so 0 * matches too), else the one already stored. * * @param WP_Post $post The post. * @return int */ private function pending_image_id( WP_Post $post ): int { if ( null !== $this->rest_image ) { return $this->rest_image; } if ( null !== $this->saving_product && in_array( $this->saving_product->get_id(), array( 0, $post->ID ), true ) ) { return (int) $this->saving_product->get_image_id(); } return (int) get_post_thumbnail_id( $post ); } /** * The warning shown when a publish attempt is blocked. * * @return string */ private function warning_message(): string { $min_width = $this->min_width(); $min_height = $this->min_height(); if ( ! $min_width && ! $min_height ) { return __( 'You cannot publish without a featured image.', 'all-in-one-utilities' ); } return sprintf( /* translators: 1: minimum width in pixels, 2: minimum height in pixels. */ __( 'You cannot publish without a featured image that is at least %1$s x %2$s pixels.', 'all-in-one-utilities' ), number_format_i18n( $min_width ), number_format_i18n( $min_height ) ); } /** * When enforcement started -- lazily set the first time this runs, and * never reset afterward. Backdated by a day (matching the reference * plugin's own fix for this exact problem, per its changelog: "without * it first run behavior was confusing") so that a post being drafted * at the moment this feature is turned on -- whose `post_date` is * therefore a few seconds older than this lazily-set cutoff -- doesn't * get wrongly exempted as "pre-existing content" the instant someone * tries to publish it. * * @return int */ private function enforcement_start(): int { add_option( self::ENFORCEMENT_START_OPTION, time() - DAY_IN_SECONDS ); return (int) get_option( self::ENFORCEMENT_START_OPTION ); } /** * Whether the feature is on at all. * * @return bool */ private function is_enabled(): bool { return (bool) aiou_get_setting( 'media', 'require_featured_image', 'enabled', 0 ); } /** * Configured post types to enforce on. * * @return string[] */ private function post_types(): array { $types = aiou_get_setting( 'media', 'require_featured_image', 'post_types', array( 'post' ) ); return is_array( $types ) ? $types : array(); } /** * Configured minimum width, in pixels (0 = unconstrained). * * @return int */ private function min_width(): int { return absint( aiou_get_setting( 'media', 'require_featured_image', 'min_width', 0 ) ); } /** * Configured minimum height, in pixels (0 = unconstrained). * * @return int */ private function min_height(): int { return absint( aiou_get_setting( 'media', 'require_featured_image', 'min_height', 0 ) ); } }