PluginProbe
Assistant – Every Day Productivity Apps / trunk
Assistant – Every Day Productivity Apps vtrunk
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
assistant / backend / src / Data / Repository / PostsRepository.php

PostsRepository.php in Assistant – Every Day Productivity Apps trunk, at backend/src/Data/Repository/PostsRepository.php

263 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FL\Assistant\Data\Repository;
4
5 use FL\Assistant\Data\Pager;
6 use FL\Assistant\System\Contracts\RepositoryAbstract;
7 use FL\Assistant\Data\Repository\NotationsRepository;
8
9
10 /**
11 * Class PostsRepository
12 * @package FL\Assistant\Data
13 */
14 class PostsRepository extends RepositoryAbstract {
15
16 protected $notations;
17
18 public function __construct( NotationsRepository $notations ) {
19 $this->notations = $notations;
20 }
21
22 /**
23 * @param $id
24 * @param callable|null $transform
25 *
26 * @return array|mixed|\WP_Post|null
27 */
28 public function find( $id, ?callable $transform = null ) {
29 $post = get_post( $id );
30
31 if ( ! is_null( $transform ) ) {
32 $post = call_user_func( $transform, $post );
33 }
34
35 return $post;
36 }
37
38 /**
39 * @param array $args
40 * @param callable|null $transform
41 *
42 * @return array
43 */
44 public function find_where( array $args = [], ?callable $transform = null ) {
45 $posts = $this->query( $args )->posts;
46
47 if ( ! is_null( $transform ) ) {
48 $posts = array_map( $transform, $posts );
49 }
50
51 return $posts;
52 }
53
54 /**
55 * @param int $label_id
56 * @param callable|null $transform
57 *
58 * @return array
59 */
60 public function find_by_label( int $label_id, ?callable $transform = null ) {
61 $labels = $this->notations->get_labels_by_id( 'post', $label_id );
62 $post_ids = [];
63
64 foreach ( $labels as $label ) {
65 $post_ids[] = $label['object_id'];
66 }
67
68 $args = [
69 'post__in' => $post_ids,
70 ];
71
72 $posts = $this->query( $args )->posts;
73
74 if ( ! is_null( $transform ) ) {
75 $posts = array_map( $transform, $posts );
76 }
77
78 return $posts;
79 }
80
81 /**
82 * Return Pager object for Posts
83 *
84 * @param array $args
85 *
86 * @param callable|null $transform
87 *
88 * @return Pager
89 */
90 public function paginate( array $args = [], ?callable $transform = null ) {
91 $query = $this->query( $args );
92
93 $pager = new Pager(
94 $query->posts,
95 $query->found_posts,
96 $query->post_count,
97 $query->get( 'offset' )
98 );
99
100 if ( ! is_null( $transform ) && $pager->total() > 0 ) {
101 $pager->apply_transform( $transform );
102 }
103
104 return $pager;
105 }
106
107 /**
108 * Get post status slugs and names.
109 * @return array
110 */
111 public function get_stati() {
112 $data = [];
113 $stati = get_post_stati( [], 'objects' );
114
115 foreach ( $stati as $slug => $status ) {
116 $data[ $slug ] = esc_html( $status->label );
117 }
118
119 return $data;
120 }
121
122 /**
123 * Plural label from registered post type (WP_Post_Type::$label), not contextual list titles.
124 * @param \WP_Post_Type $type
125 * @return string
126 */
127 private function get_stable_plural_label( \WP_Post_Type $type ) {
128 if ( isset( $type->label ) && '' !== $type->label ) {
129 return $type->label;
130 }
131
132 return $type->labels->name;
133 }
134
135 /**
136 * Get array of post types registered in WordPress
137 * @return array
138 */
139 public function get_types() {
140 $data = [];
141 $types = get_post_types( [], 'objects' );
142
143 // Types with show_ui false that we want to show
144 $known = apply_filters( 'fl_assistant_post_types_known', [
145 'wp_template',
146 'wp_template_part',
147 'fl_code',
148 ] );
149
150 // Of the types above, those whose items are portable enough to bulk add to a
151 // cloud library. Opt-in only: block templates are scoped to a theme via the
152 // wp_theme taxonomy and code snippets have their own library item type, so
153 // neither belongs in the upload picker. Design systems are portable by UUID,
154 // so they do.
155 $library = apply_filters( 'fl_assistant_post_types_library', [
156 'fl-design-system',
157 ] );
158
159 // Types to never show
160 $ignore = [
161 'attachment',
162 'wp_navigation',
163 'vcv_tutorials',
164 'elementor_snippet',
165 'elementor_font',
166 'elementor_icons',
167 ];
168
169 foreach ( $types as $slug => $type ) {
170 if ( ! isset( $type->cap->edit_others_posts ) ) {
171 continue;
172 }
173 if ( ! current_user_can( $type->cap->edit_others_posts ) ) {
174 continue;
175 }
176 if ( ! $type->show_ui && ! in_array( $slug, $known ) ) {
177 continue;
178 }
179 if ( in_array( $slug, $ignore ) ) {
180 continue;
181 }
182 if ( ! function_exists( 'get_page_templates' ) ) {
183 require_once ABSPATH . 'wp-admin/includes/theme.php';
184 }
185
186 $data[ $slug ] = [
187 'canView' => $type->public || $type->publicly_queryable || in_array( $slug, $library ),
188 'hasPublicUrl' => $type->public || $type->publicly_queryable,
189 'canExport' => $type->can_export,
190 'hasArchive' => $type->has_archive,
191 'isHierarchical' => $type->hierarchical,
192 'builtin' => $type->_builtin,
193 'templates' => get_page_templates( null, $slug ),
194 'taxonomies' => $type->taxonomies,
195 'supports' => [
196 'comments' => post_type_supports( $slug, 'comments' ),
197 'trackbacks' => post_type_supports( $slug, 'trackbacks' ),
198 'excerpt' => post_type_supports( $slug, 'excerpt' ),
199 'thumbnail' => post_type_supports( $slug, 'thumbnail' ),
200 'order' => post_type_supports( $slug, 'page-attributes' ),
201 ],
202 'labels' => [
203 'singular' => esc_html( $type->labels->singular_name ),
204 'plural' => esc_html( $this->get_stable_plural_label( $type ) ),
205 'newItem' => esc_html( $type->labels->new_item ),
206 'editItem' => esc_html( $type->labels->edit_item ),
207 'viewItem' => esc_html( $type->labels->view_item ),
208 ],
209 ];
210
211 if ( 'wp_template' === $slug || 'wp_template_part' === $slug ) {
212 $data[ $slug ]['labels']['singular'] = sprintf( esc_html_x( 'Block %s', 'Singular type name.', 'assistant' ), $type->labels->singular_name );
213 $data[ $slug ]['labels']['plural'] = sprintf( esc_html_x( 'Block %s', 'Plural type name.', 'assistant' ), $this->get_stable_plural_label( $type ) );
214 }
215
216 $taxonomies = get_object_taxonomies( $slug, 'objects' );
217
218 foreach ( $taxonomies as $tax_slug => $tax ) {
219 if ( ! $tax->public || ! $tax->show_ui ) {
220 continue;
221 }
222 $data[ $slug ]['taxonomies'][] = $tax_slug;
223 }
224 }
225
226 return $data;
227 }
228
229 /**
230 * Get taxonomy slugs and names.
231 */
232 public function get_taxononies() {
233 $data = [];
234 $types = static::get_types();
235
236 foreach ( $types as $type_slug => $type ) {
237 $taxonomies = get_object_taxonomies( $type_slug, 'objects' );
238 foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
239 if ( ! $taxonomy->public || ! $taxonomy->show_ui || 'post_format' === $taxonomy_slug ) {
240 continue;
241 }
242 $data[ $taxonomy_slug ] = [
243 'description' => $taxonomy->description,
244 'isHierarchical' => $taxonomy->hierarchical,
245 'labels' => [
246 'singular' => esc_html( $taxonomy->labels->singular_name ),
247 'plural' => esc_html( $taxonomy->labels->name ),
248 'newItem' => sprintf( esc_html_x( 'New %s', 'Singular term name.', 'assistant' ), $taxonomy->labels->singular_name ),
249 'addNewItem' => esc_html( $taxonomy->labels->add_new_item ),
250 'editItem' => esc_html( $taxonomy->labels->edit_item ),
251 ],
252 ];
253 }
254 }
255
256 return $data;
257 }
258
259 public function query( array $args = [] ) {
260 return new \WP_Query( $args );
261 }
262 }
263