*/ public function all(): array { $blocks = array(); foreach ( \Blocks::find( array( 'posts_per_page' => -1 ) ) as $block ) { if ( $block instanceof \Blocks ) { $blocks[] = $block; } } return $blocks; } public function find( string $title, string $requested_locale, string $effective_locale ): ?\Blocks { $locale_clause = '' === $requested_locale ? array( 'relation' => 'OR', array( 'key' => '_locale', 'compare' => 'NOT EXISTS', ), array( 'key' => '_locale', 'value' => '', 'compare' => '=', ), array( 'key' => '_locale', 'value' => $effective_locale, 'compare' => '=', ), ) : array( 'key' => '_locale', 'value' => $requested_locale, 'compare' => '=', ); $query = new \WP_Query( array( 'post_type' => \Blocks::POST_TYPE, 'post_status' => 'any', 'title' => $title, 'posts_per_page' => 1, 'fields' => 'ids', 'no_found_rows' => true, 'ignore_sticky_posts' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Locale is part of the transfer identity contract. 'meta_query' => array( $locale_clause ), ), ); $post_id = $query->posts[0] ?? null; if ( ! \is_int( $post_id ) && ! ( \is_string( $post_id ) && \ctype_digit( $post_id ) ) ) { return null; } $block = \Blocks::get_instance( (int) $post_id ); return $block instanceof \Blocks ? $block : null; } public function sync_feature_indexes( \Blocks $block, int $post_id ): void { \update_post_meta( $post_id, '_blocks_popup_enabled', \absint( $block->prop( 'popup_enabled' ) ) ); \update_post_meta( $post_id, '_blocks_banner_enabled', \absint( $block->prop( 'banner_enabled' ) ) ); } /** * @param list $categories Category names. * @param list $tags Tag names. */ public function assign_terms( int $post_id, array $categories, array $tags ): bool|\WP_Error { foreach ( array( 'blocks_category' => $categories, 'blocks_tag' => $tags, ) as $taxonomy => $names ) { $result = \wp_set_object_terms( $post_id, $names, $taxonomy ); if ( \is_wp_error( $result ) ) { return $result; } } return true; } /** @return list */ public function term_names( int $post_id, string $taxonomy ): array { $terms = \get_the_terms( $post_id, $taxonomy ); if ( ! \is_array( $terms ) ) { return array(); } $names = array(); foreach ( $terms as $term ) { if ( $term instanceof \WP_Term ) { $names[] = $term->name; } } return $names; } }