PluginProbe
Blocks – Reusable Content, Shortcodes & Site Variables / trunk
Blocks – Reusable Content, Shortcodes & Site Variables vtrunk
26.09.03.15 26.08.31.21 26.08.22.20 26.08.22.22 26.08.22.17 26.08.22.13 26.08.21.19 26.08.07.23 26.07.19.14 26.07.13.21 26.07.13.17 26.07.12.13 026.07.07.21 026.07.05.18 026.06.26.20 026.06.26.21 026.06.08.20 026.05.13.14 026.04.29.10 trunk 026.02.22.22 026.03.16.23 026.04.23.13
blocks / src / Transfer / BlockTransferRepository.php

BlockTransferRepository.php in Blocks – Reusable Content, Shortcodes & Site Variables trunk, at src/Transfer/BlockTransferRepository.php

119 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace RenzoJohnson\Blocks\Transfer;
6
7 \defined( 'ABSPATH' ) || exit;
8
9 final class BlockTransferRepository {
10
11 /** @return list<\Blocks> */
12 public function all(): array {
13 $blocks = array();
14
15 foreach ( \Blocks::find( array( 'posts_per_page' => -1 ) ) as $block ) {
16 if ( $block instanceof \Blocks ) {
17 $blocks[] = $block;
18 }
19 }
20
21 return $blocks;
22 }
23
24 public function find( string $title, string $requested_locale, string $effective_locale ): ?\Blocks {
25 $locale_clause = '' === $requested_locale
26 ? array(
27 'relation' => 'OR',
28 array(
29 'key' => '_locale',
30 'compare' => 'NOT EXISTS',
31 ),
32 array(
33 'key' => '_locale',
34 'value' => '',
35 'compare' => '=',
36 ),
37 array(
38 'key' => '_locale',
39 'value' => $effective_locale,
40 'compare' => '=',
41 ),
42 )
43 : array(
44 'key' => '_locale',
45 'value' => $requested_locale,
46 'compare' => '=',
47 );
48
49 $query = new \WP_Query(
50 array(
51 'post_type' => \Blocks::POST_TYPE,
52 'post_status' => 'any',
53 'title' => $title,
54 'posts_per_page' => 1,
55 'fields' => 'ids',
56 'no_found_rows' => true,
57 'ignore_sticky_posts' => true,
58 'update_post_meta_cache' => false,
59 'update_post_term_cache' => false,
60 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Locale is part of the transfer identity contract.
61 'meta_query' => array( $locale_clause ),
62 ),
63 );
64
65 $post_id = $query->posts[0] ?? null;
66
67 if ( ! \is_int( $post_id ) && ! ( \is_string( $post_id ) && \ctype_digit( $post_id ) ) ) {
68 return null;
69 }
70
71 $block = \Blocks::get_instance( (int) $post_id );
72
73 return $block instanceof \Blocks ? $block : null;
74 }
75
76 public function sync_feature_indexes( \Blocks $block, int $post_id ): void {
77 \update_post_meta( $post_id, '_blocks_popup_enabled', \absint( $block->prop( 'popup_enabled' ) ) );
78 \update_post_meta( $post_id, '_blocks_banner_enabled', \absint( $block->prop( 'banner_enabled' ) ) );
79 }
80
81 /**
82 * @param list<string> $categories Category names.
83 * @param list<string> $tags Tag names.
84 */
85 public function assign_terms( int $post_id, array $categories, array $tags ): bool|\WP_Error {
86 foreach ( array(
87 'blocks_category' => $categories,
88 'blocks_tag' => $tags,
89 ) as $taxonomy => $names ) {
90 $result = \wp_set_object_terms( $post_id, $names, $taxonomy );
91
92 if ( \is_wp_error( $result ) ) {
93 return $result;
94 }
95 }
96
97 return true;
98 }
99
100 /** @return list<string> */
101 public function term_names( int $post_id, string $taxonomy ): array {
102 $terms = \get_the_terms( $post_id, $taxonomy );
103
104 if ( ! \is_array( $terms ) ) {
105 return array();
106 }
107
108 $names = array();
109
110 foreach ( $terms as $term ) {
111 if ( $term instanceof \WP_Term ) {
112 $names[] = $term->name;
113 }
114 }
115
116 return $names;
117 }
118 }
119