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 / AttachmentsRepository.php

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

109 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace FL\Assistant\Data\Repository;
5
6
7 use FL\Assistant\Data\Pager;
8 use FL\Assistant\System\Contracts\RepositoryAbstract;
9
10 /**
11 * Class AttachmentsRepository
12 * @package FL\Assistant\Data\Repository
13 */
14 class AttachmentsRepository extends RepositoryAbstract {
15
16
17 /**
18 * @param array $args
19 *
20 * @param callable|null $transform
21 *
22 * @return array
23 */
24 public function find_where( array $args = [], ?callable $transform = null ) {
25 $query = $this->query( $args );
26 $attachments = $query->posts;
27 if ( ! is_null( $transform ) ) {
28 $attachments = array_map( $transform, $attachments );
29 }
30 return $attachments;
31 }
32
33 /**
34 * @param array $args
35 *
36 * @return \WP_Query
37 */
38 public function query( array $args = [] ) {
39 $args = array_merge(
40 $args, [
41 // attachments have an empty post status.
42 // removing this arg will give empty results
43 'post_status' => 'any',
44 'perm' => 'editable',
45 'post_type' => 'attachment',
46 ]
47 );
48
49 return new \WP_Query( $args );
50 }
51
52 /**
53 * @param $id
54 *
55 * @return $this
56 */
57 public function delete( $id ) {
58 wp_delete_attachment( $id );
59 return $this;
60 }
61
62
63 public function restore( $id, ?callable $transform = null ) {
64 wp_untrash_post( $id );
65 if ( ! is_null( $transform ) ) {
66 return $this->find( $id, $transform );
67 }
68 return $this->find( $id );
69 }
70
71 /**
72 * @param $id
73 *
74 * @param callable|null $transform
75 *
76 * @return array|\WP_Post|null
77 */
78 public function find( $id, ?callable $transform = null ) {
79 $attachment = get_post( $id );
80 if ( ! is_null( $transform ) ) {
81 $attachment = call_user_func( $transform, $attachment );
82 }
83 return $attachment;
84 }
85
86 /**
87 * @param array $args
88 *
89 * @param callable|null $transform
90 *
91 * @return Pager
92 */
93 public function paginate( array $args = [], ?callable $transform = null ) {
94 $query = $this->query( $args );
95
96 $pager = new Pager(
97 $query->posts,
98 $query->found_posts,
99 $query->post_count,
100 $query->get( 'offset' )
101 );
102
103 if ( ! is_null( $transform ) ) {
104 $pager->apply_transform( $transform );
105 }
106 return $pager;
107 }
108 }
109