Skip to content

Commit 4b3f973

Browse files
committed
Made changes to repositories; Added meta example for users; Changed RestApiNamespaceWhitelist - added carbon-fields
1 parent 0db670f commit 4b3f973

File tree

11 files changed

+651
-87
lines changed

11 files changed

+651
-87
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"require": {
4141
"php": ">=8.1",
42-
"htmlburger/carbon-fields": "3.6.2",
42+
"htmlburger/carbon-fields": "3.6.5",
4343
"laminas/laminas-config-aggregator": "1.14.0",
4444
"monolog/monolog": "3.5.0",
4545
"php-di/php-di": "7.0.6",

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/common/security.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
'security' => [
66
'restrictRestApiToWhitelistOnly' => true,
77
'RestApiNamespaceWhitelist' => [
8-
'/contact-form-7/v1',
8+
'/contact-form-7/',
9+
'/carbon-fields/', // TODO Checks how can disable on front
910
]
1011
// ToDo add key to .env
1112
//'restApiKey' => defined('REST_API_KEY') ? REST_API_KEY,

src/Base/Hooks.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static function initHooks(): void
6767
add_action('carbon_fields_register_fields', [Handlers\Meta\TaxonomyMeta\NewsCategory::class, 'make']);
6868
add_action('carbon_fields_register_fields', [Handlers\Meta\PostMeta\Pricing::class, 'make']);
6969
add_action('carbon_fields_register_fields', [Handlers\Meta\PostMeta\Page::class, 'make']);
70+
add_action('carbon_fields_register_fields', [Handlers\Meta\UserMeta\UserMeta::class, 'make']);
7071

7172
/************************************
7273
* Admin aria customizations
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace StarterKit\Handlers\Meta\UserMeta;
4+
5+
defined('ABSPATH') || exit;
6+
7+
use Carbon_Fields\Container;
8+
use Carbon_Fields\Field;
9+
10+
class UserMeta
11+
{
12+
public static function make(): void
13+
{
14+
$metaPrefix = SK_PREFIX;
15+
16+
Container::make('user_meta', __('Additional settings', 'starter-kit'))
17+
->add_fields([
18+
Field::make('image', $metaPrefix . 'avatar_id', __('Avatar', 'starter-kit'))
19+
->set_type(['image/jpeg', 'image/jpg', 'image/png', 'image/svg+xml', 'image/webp',]),
20+
Field::make('text', $metaPrefix . 'position', __('Position', 'starter-kit')),
21+
]);
22+
}
23+
}

src/Handlers/Security/RestApiFilter.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,8 @@ public static function restApiWhitelistOnly(
4040
$referer = $_SERVER['HTTP_REFERER'] ?? '';
4141
$route = $request->get_route();
4242

43-
// Allow access for editors and administrators only from the admin panel
44-
if (
45-
(current_user_can('editor') || current_user_can('administrator')) &&
46-
str_starts_with($referer, site_url('/') . 'wp-admin/')
47-
) {
43+
// Allow access for editors and administrators only
44+
if (current_user_can('edit_posts')) {
4845
return $result;
4946
}
5047

src/Repository/NewsRepository.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,67 @@
1313
*/
1414
class NewsRepository extends WpPostRepositoryAbstract
1515
{
16+
/**
17+
* Gets the key of the post type for this repository
18+
*
19+
* @return string
20+
*/
1621
public static function getPostTypeKey(): string
1722
{
1823
return PostTypes\News::getKey();
1924
}
2025

26+
2127
/**
22-
* @param array $args
28+
* Gets recent News
2329
*
24-
* @return array
30+
* @param int $limit
31+
* @param bool $withThumbnailOnly
32+
* @param array $exclude
33+
*
34+
* @return WP_Post[]
2535
*/
26-
public static function getRecentNews(array $args): array
36+
public static function getRecentNews(int $limit, bool $withThumbnailOnly = false, array $exclude = []): array
2737
{
28-
return [];
38+
return static::getRecentPosts($limit, $withThumbnailOnly, $exclude);
2939
}
3040

41+
3142
/**
32-
* @param array $args
43+
* Gets related News
3344
*
34-
* @return array
45+
* @param int $primaryPostId
46+
* @param int $limit
47+
* @param string $taxonomy
48+
* @param bool $withThumbnailOnly
49+
*
50+
* @return WP_Post[]
3551
*/
36-
public static function getRelatedNews(array $args): array
37-
{
38-
return [];
52+
public static function getRelatedNews(
53+
int $primaryPostId,
54+
int $limit,
55+
string $taxonomy = 'category',
56+
bool $withThumbnailOnly = false
57+
): array {
58+
return static::getRelatedPosts($primaryPostId, $limit, $taxonomy, $withThumbnailOnly);
3959
}
4060

61+
4162
/**
42-
* @param array $args
63+
* Gets random News
4364
*
44-
* @return array
65+
* @param int $limit
66+
* @param bool $withThumbnailOnly
67+
* @param array $exclude
68+
*
69+
* @return WP_Post[]
4570
*/
46-
public static function getRandomNews(array $args): array
71+
public static function getRandomNews(int $limit, bool $withThumbnailOnly = false, array $exclude = []): array
4772
{
48-
return [];
73+
return static::getRandomPosts($limit, $withThumbnailOnly, $exclude);
4974
}
5075

76+
5177
/**
5278
* @param float $impact
5379
*

src/Repository/UserRepository.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace StarterKit\Repository;
4+
5+
defined('ABSPATH') || exit;
6+
7+
use WP_User;
8+
use StarterKit\Helper\Utils;
9+
10+
class UserRepository implements WpUserRepositoryInterface
11+
{
12+
/**
13+
* Gets List of users.
14+
*
15+
* @param array $args
16+
*
17+
* @return \stdclass [] | int[] | WP_User[]
18+
*/
19+
public static function get(array $args = []): array
20+
{
21+
$defaults = [
22+
'role' => '',
23+
'role__in' => [],
24+
'role__not_in' => [],
25+
'meta_key' => '',
26+
'meta_value' => '',
27+
'meta_compare' => '',
28+
'meta_query' => [],
29+
'include' => [],
30+
'exclude' => [],
31+
'orderby' => 'login',
32+
'order' => 'ASC',
33+
'offset' => '',
34+
'search' => '',
35+
'search_columns' => [],
36+
'number' => '',
37+
'paged' => 1,
38+
'count_total' => false,
39+
'fields' => 'all',
40+
'who' => '',
41+
'has_published_posts' => null,
42+
'date_query' => [] // see WP_Date_Query
43+
];
44+
45+
$args = (array)\wp_parse_args($args, $defaults);
46+
47+
return get_users($args);
48+
}
49+
50+
51+
/**
52+
* Gets user by Id.
53+
*
54+
* @param int $userId
55+
*
56+
* @return \WP_User|null
57+
*/
58+
public static function getById(int $userId): WP_User|null
59+
{
60+
return get_user_by('ID', $userId) ?: null;
61+
}
62+
63+
64+
/**
65+
* Gets User Avatar Image Id
66+
*
67+
* @param int $userId
68+
*
69+
* @return int
70+
*/
71+
public static function getAvatarId(int $userId): int
72+
{
73+
return (int)Utils::getUserMeta($userId, 'avatar_id');
74+
}
75+
76+
77+
/**
78+
* Gets User Avatar Image URL.
79+
*
80+
* @param int $userId
81+
* @param string|int[] $size
82+
*
83+
* @return string
84+
*/
85+
public static function getAvatarUrl(int $userId, string|array $size = 'full'): string
86+
{
87+
$avatarId = static::getAvatarId($userId);
88+
89+
return \wp_get_attachment_image_url($avatarId, $size) ?: '';
90+
}
91+
92+
93+
/**
94+
* Gets User position.
95+
*
96+
* @param int $userId
97+
*
98+
* @return string
99+
*/
100+
public static function getPosition(int $userId): string
101+
{
102+
return (string)Utils::getUserMeta($userId, 'position');
103+
}
104+
}

0 commit comments

Comments
 (0)