| @@ -1,218 +1,239 @@ | ||
| 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 | - * Get array of post types registered in WordPress | |
| 124 | - * @return array | |
| 125 | - */ | |
| 126 | - public function get_types() { | |
| 127 | - $data = []; | |
| 128 | - $types = get_post_types( | |
| 129 | - [ | |
| 130 | - 'public' => true, | |
| 131 | - ], | |
| 132 | - 'objects' | |
| 133 | - ); | |
| 134 | - | |
| 135 | - foreach ( $types as $slug => $type ) { | |
| 136 | - if ( ! isset( $type->cap->edit_others_posts ) ) { | |
| 137 | - continue; | |
| 138 | - } | |
| 139 | - if ( ! current_user_can( $type->cap->edit_others_posts ) ) { | |
| 140 | - continue; | |
| 141 | - } | |
| 142 | - if ( 'attachment' === $slug ) { | |
| 143 | - continue; | |
| 144 | - } | |
| 145 | - if ( ! function_exists( 'get_page_templates' ) ) { | |
| 146 | - require_once ABSPATH . 'wp-admin/includes/theme.php'; | |
| 147 | - } | |
| 148 | - | |
| 149 | - $data[ $slug ] = [ | |
| 150 | - 'canExport' => $type->can_export, | |
| 151 | - 'hasArchive' => $type->has_archive, | |
| 152 | - 'isHierarchical' => $type->hierarchical, | |
| 153 | - 'builtin' => $type->_builtin, | |
| 154 | - 'templates' => get_page_templates( null, $slug ), | |
| 155 | - 'taxonomies' => $type->taxonomies, | |
| 156 | - 'supports' => [ | |
| 157 | - 'comments' => post_type_supports( $slug, 'comments' ), | |
| 158 | - 'trackbacks' => post_type_supports( $slug, 'trackbacks' ), | |
| 159 | - 'excerpt' => post_type_supports( $slug, 'excerpt' ), | |
| 160 | - 'thumbnail' => post_type_supports( $slug, 'thumbnail' ), | |
| 161 | - 'order' => post_type_supports( $slug, 'page-attributes' ), | |
| 162 | - ], | |
| 163 | - 'labels' => [ | |
| 164 | - 'singular' => esc_html( $type->labels->singular_name ), | |
| 165 | - 'plural' => esc_html( $type->labels->name ), | |
| 166 | - 'newItem' => esc_html( $type->labels->new_item ), | |
| 167 | - 'editItem' => esc_html( $type->labels->edit_item ), | |
| 168 | - 'viewItem' => esc_html( $type->labels->view_item ), | |
| 169 | - ], | |
| 170 | - ]; | |
| 171 | - | |
| 172 | - $taxonomies = get_object_taxonomies( $slug, 'objects' ); | |
| 173 | - | |
| 174 | - foreach ( $taxonomies as $tax_slug => $tax ) { | |
| 175 | - if ( ! $tax->public || ! $tax->show_ui ) { | |
| 176 | - continue; | |
| 177 | - } | |
| 178 | - $data[ $slug ]['taxonomies'][] = $tax_slug; | |
| 179 | - } | |
| 180 | - } | |
| 181 | - | |
| 182 | - return $data; | |
| 183 | - } | |
| 184 | - | |
| 185 | - /** | |
| 186 | - * Get taxonomy slugs and names. | |
| 187 | - */ | |
| 188 | - public function get_taxononies() { | |
| 189 | - $data = []; | |
| 190 | - $types = static::get_types(); | |
| 191 | - | |
| 192 | - foreach ( $types as $type_slug => $type ) { | |
| 193 | - $taxonomies = get_object_taxonomies( $type_slug, 'objects' ); | |
| 194 | - foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) { | |
| 195 | - if ( ! $taxonomy->public || ! $taxonomy->show_ui || 'post_format' === $taxonomy_slug ) { | |
| 196 | - continue; | |
| 197 | - } | |
| 198 | - $data[ $taxonomy_slug ] = [ | |
| 199 | - 'description' => $taxonomy->description, | |
| 200 | - 'isHierarchical' => $taxonomy->hierarchical, | |
| 201 | - 'labels' => [ | |
| 202 | - 'singular' => esc_html( $taxonomy->labels->singular_name ), | |
| 203 | - 'plural' => esc_html( $taxonomy->labels->name ), | |
| 204 | - 'newItem' => sprintf( esc_html_x( 'New %s', 'Singular term name.', 'fl-assistant' ), $taxonomy->labels->singular_name ), | |
| 205 | - 'addNewItem' => esc_html( $taxonomy->labels->add_new_item ), | |
| 206 | - 'editItem' => esc_html( $taxonomy->labels->edit_item ), | |
| 207 | - ], | |
| 208 | - ]; | |
| 209 | - } | |
| 210 | - } | |
| 211 | - | |
| 212 | - return $data; | |
| 213 | - } | |
| 214 | - | |
| 215 | - public function query( array $args = [] ) { | |
| 216 | - return new \WP_Query( $args ); | |
| 217 | - } | |
| 218 | -} | |
| 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 | + * Get array of post types registered in WordPress | |
| 124 | + * @return array | |
| 125 | + */ | |
| 126 | + public function get_types() { | |
| 127 | + $data = []; | |
| 128 | + $types = get_post_types( [], 'objects' ); | |
| 129 | + | |
| 130 | + // Types with show_ui false that we want to show | |
| 131 | + $known = [ | |
| 132 | + 'wp_template', | |
| 133 | + 'wp_template_part', | |
| 134 | + 'fl_code', | |
| 135 | + ]; | |
| 136 | + | |
| 137 | + // Types to never show | |
| 138 | + $ignore = [ | |
| 139 | + 'attachment', | |
| 140 | + 'wp_navigation', | |
| 141 | + 'vcv_tutorials', | |
| 142 | + 'elementor_snippet', | |
| 143 | + 'elementor_font', | |
| 144 | + 'elementor_icons', | |
| 145 | + ]; | |
| 146 | + | |
| 147 | + foreach ( $types as $slug => $type ) { | |
| 148 | + if ( ! isset( $type->cap->edit_others_posts ) ) { | |
| 149 | + continue; | |
| 150 | + } | |
| 151 | + if ( ! current_user_can( $type->cap->edit_others_posts ) ) { | |
| 152 | + continue; | |
| 153 | + } | |
| 154 | + if ( ! $type->show_ui && ! in_array( $slug, $known ) ) { | |
| 155 | + continue; | |
| 156 | + } | |
| 157 | + if ( in_array( $slug, $ignore ) ) { | |
| 158 | + continue; | |
| 159 | + } | |
| 160 | + if ( ! function_exists( 'get_page_templates' ) ) { | |
| 161 | + require_once ABSPATH . 'wp-admin/includes/theme.php'; | |
| 162 | + } | |
| 163 | + | |
| 164 | + $data[ $slug ] = [ | |
| 165 | + 'canView' => $type->public || $type->publicly_queryable, | |
| 166 | + 'canExport' => $type->can_export, | |
| 167 | + 'hasArchive' => $type->has_archive, | |
| 168 | + 'isHierarchical' => $type->hierarchical, | |
| 169 | + 'builtin' => $type->_builtin, | |
| 170 | + 'templates' => get_page_templates( null, $slug ), | |
| 171 | + 'taxonomies' => $type->taxonomies, | |
| 172 | + 'supports' => [ | |
| 173 | + 'comments' => post_type_supports( $slug, 'comments' ), | |
| 174 | + 'trackbacks' => post_type_supports( $slug, 'trackbacks' ), | |
| 175 | + 'excerpt' => post_type_supports( $slug, 'excerpt' ), | |
| 176 | + 'thumbnail' => post_type_supports( $slug, 'thumbnail' ), | |
| 177 | + 'order' => post_type_supports( $slug, 'page-attributes' ), | |
| 178 | + ], | |
| 179 | + 'labels' => [ | |
| 180 | + 'singular' => esc_html( $type->labels->singular_name ), | |
| 181 | + 'plural' => esc_html( $type->labels->name ), | |
| 182 | + 'newItem' => esc_html( $type->labels->new_item ), | |
| 183 | + 'editItem' => esc_html( $type->labels->edit_item ), | |
| 184 | + 'viewItem' => esc_html( $type->labels->view_item ), | |
| 185 | + ], | |
| 186 | + ]; | |
| 187 | + | |
| 188 | + if ( 'wp_template' === $slug || 'wp_template_part' === $slug ) { | |
| 189 | + $data[ $slug ]['labels']['singular'] = sprintf( esc_html_x( 'Block %s', 'Singular type name.', 'assistant' ), $type->labels->singular_name ); | |
| 190 | + $data[ $slug ]['labels']['plural'] = sprintf( esc_html_x( 'Block %s', 'Plural type name.', 'assistant' ), $type->labels->name ); | |
| 191 | + } | |
| 192 | + | |
| 193 | + $taxonomies = get_object_taxonomies( $slug, 'objects' ); | |
| 194 | + | |
| 195 | + foreach ( $taxonomies as $tax_slug => $tax ) { | |
| 196 | + if ( ! $tax->public || ! $tax->show_ui ) { | |
| 197 | + continue; | |
| 198 | + } | |
| 199 | + $data[ $slug ]['taxonomies'][] = $tax_slug; | |
| 200 | + } | |
| 201 | + } | |
| 202 | + | |
| 203 | + return $data; | |
| 204 | + } | |
| 205 | + | |
| 206 | + /** | |
| 207 | + * Get taxonomy slugs and names. | |
| 208 | + */ | |
| 209 | + public function get_taxononies() { | |
| 210 | + $data = []; | |
| 211 | + $types = static::get_types(); | |
| 212 | + | |
| 213 | + foreach ( $types as $type_slug => $type ) { | |
| 214 | + $taxonomies = get_object_taxonomies( $type_slug, 'objects' ); | |
| 215 | + foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) { | |
| 216 | + if ( ! $taxonomy->public || ! $taxonomy->show_ui || 'post_format' === $taxonomy_slug ) { | |
| 217 | + continue; | |
| 218 | + } | |
| 219 | + $data[ $taxonomy_slug ] = [ | |
| 220 | + 'description' => $taxonomy->description, | |
| 221 | + 'isHierarchical' => $taxonomy->hierarchical, | |
| 222 | + 'labels' => [ | |
| 223 | + 'singular' => esc_html( $taxonomy->labels->singular_name ), | |
| 224 | + 'plural' => esc_html( $taxonomy->labels->name ), | |
| 225 | + 'newItem' => sprintf( esc_html_x( 'New %s', 'Singular term name.', 'assistant' ), $taxonomy->labels->singular_name ), | |
| 226 | + 'addNewItem' => esc_html( $taxonomy->labels->add_new_item ), | |
| 227 | + 'editItem' => esc_html( $taxonomy->labels->edit_item ), | |
| 228 | + ], | |
| 229 | + ]; | |
| 230 | + } | |
| 231 | + } | |
| 232 | + | |
| 233 | + return $data; | |
| 234 | + } | |
| 235 | + | |
| 236 | + public function query( array $args = [] ) { | |
| 237 | + return new \WP_Query( $args ); | |
| 238 | + } | |
| 239 | +} | |