| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Pinova\API; |
| 5 |
|
| 6 |
use Illuminate\Database\Eloquent\Builder; |
| 7 |
use Pinova\Helper; |
| 8 |
use Pinova\Helpers\IP; |
| 9 |
use Pinova\Models\Block; |
| 10 |
use Pinova\Objects\Identifier; |
| 11 |
use WP_REST_Request; |
| 12 |
use WP_User; |
| 13 |
use WP_User_Query; |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
class BlocksAPI extends RestAPI { |
| 18 |
|
| 19 |
public function register_routes() { |
| 20 |
|
| 21 |
register_rest_route( 'pinova/admin/blocks', 'filters', [ |
| 22 |
'methods' => 'POST', |
| 23 |
'callback' => [ $this, 'filters' ], |
| 24 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 25 |
] ); |
| 26 |
|
| 27 |
register_rest_route( 'pinova/admin/blocks', 'index', [ |
| 28 |
'methods' => 'POST', |
| 29 |
'callback' => [ $this, 'index' ], |
| 30 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 31 |
'args' => [ |
| 32 |
'identifier' => [ |
| 33 |
'required' => false, |
| 34 |
'sanitize_callback' => 'sanitize_text_field', |
| 35 |
], |
| 36 |
'from_date' => [ |
| 37 |
'required' => false, |
| 38 |
'sanitize_callback' => 'absint', |
| 39 |
], |
| 40 |
'to_date' => [ |
| 41 |
'required' => false, |
| 42 |
'sanitize_callback' => 'absint', |
| 43 |
], |
| 44 |
'blocked_by' => [ |
| 45 |
'required' => false, |
| 46 |
'sanitize_callback' => 'absint', |
| 47 |
], |
| 48 |
'page' => [ |
| 49 |
'required' => false, |
| 50 |
'sanitize_callback' => 'absint', |
| 51 |
'default' => 1, |
| 52 |
], |
| 53 |
'per_page' => [ |
| 54 |
'required' => false, |
| 55 |
'sanitize_callback' => 'absint', |
| 56 |
'default' => 20, |
| 57 |
], |
| 58 |
], |
| 59 |
] ); |
| 60 |
|
| 61 |
register_rest_route( 'pinova/admin/blocks', 'add', [ |
| 62 |
'methods' => 'POST', |
| 63 |
'callback' => [ $this, 'add' ], |
| 64 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 65 |
'args' => [ |
| 66 |
'identifier' => [ |
| 67 |
'required' => true, |
| 68 |
'sanitize_callback' => 'sanitize_text_field', |
| 69 |
], |
| 70 |
'blocked_until' => [ |
| 71 |
'required' => false, |
| 72 |
'sanitize_callback' => 'absint', |
| 73 |
], |
| 74 |
], |
| 75 |
] ); |
| 76 |
|
| 77 |
register_rest_route( 'pinova/admin/blocks', 'delete', [ |
| 78 |
'methods' => 'POST', |
| 79 |
'callback' => [ $this, 'delete' ], |
| 80 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 81 |
'args' => [ |
| 82 |
'block_id' => [ |
| 83 |
'required' => true, |
| 84 |
'sanitize_callback' => 'absint', |
| 85 |
], |
| 86 |
], |
| 87 |
] ); |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
public function filters( WP_REST_Request $request ) { |
| 92 |
|
| 93 |
if ( ! function_exists( 'get_editable_roles' ) ) { |
| 94 |
require_once( ABSPATH . '/wp-admin/includes/user.php' ); |
| 95 |
} |
| 96 |
|
| 97 |
// Users |
| 98 |
$roles = []; |
| 99 |
|
| 100 |
foreach ( array_reverse( wp_roles()->roles ) as $id => $role ) { |
| 101 |
|
| 102 |
if ( $role['capabilities']['manage_options'] ?? false ) { |
| 103 |
$roles[] = $id; |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
$users = new WP_User_Query( [ |
| 109 |
'number' => - 1, |
| 110 |
'role__in' => $roles, |
| 111 |
] ); |
| 112 |
$users = collect( $users->get_results() ); |
| 113 |
|
| 114 |
self::response( true, null, [ |
| 115 |
'users' => $users->pluck( 'display_name', 'ID' )->toArray(), |
| 116 |
] ); |
| 117 |
} |
| 118 |
|
| 119 |
public function index( WP_REST_Request $request ) { |
| 120 |
|
| 121 |
$raw_identifier = $request->get_param( 'identifier' ); |
| 122 |
$blocked_by = $request->get_param( 'blocked_by' ); // @todo check it return null |
| 123 |
$blocked_by = $_POST['blocked_by'] ?? null; |
| 124 |
$from_date = $request->get_param( 'from_date' ); |
| 125 |
$to_date = $request->get_param( 'to_date' ); |
| 126 |
|
| 127 |
$page = max( 1, $request->get_param( 'page' ) ); |
| 128 |
$per_page = max( 1, $request->get_param( 'per_page' ) ); |
| 129 |
|
| 130 |
$timezone = wp_timezone_string(); |
| 131 |
|
| 132 |
if ( ! empty( $from_date ) ) { |
| 133 |
$from_date = verta() |
| 134 |
->timestamp( intval( $from_date ) ) |
| 135 |
->timezone( $timezone ) |
| 136 |
->startDay() |
| 137 |
->toCarbon() |
| 138 |
->utc(); |
| 139 |
} |
| 140 |
|
| 141 |
if ( ! empty( $to_date ) ) { |
| 142 |
$to_date = verta() |
| 143 |
->timestamp( intval( $to_date ) ) |
| 144 |
->timezone( $timezone ) |
| 145 |
->endDay() |
| 146 |
->toCarbon() |
| 147 |
->utc(); |
| 148 |
} |
| 149 |
|
| 150 |
/** @var Block[]|Builder $query */ |
| 151 |
$query = Block::query() |
| 152 |
->orderByDesc( 'id' ) |
| 153 |
->when( $raw_identifier, function ( Builder $query ) use ( $raw_identifier ) { |
| 154 |
|
| 155 |
$query->where( function ( Builder $query ) use ( $raw_identifier ) { |
| 156 |
|
| 157 |
$query->where( 'identifier', 'LIKE', "%{$raw_identifier}%" ); |
| 158 |
|
| 159 |
$identifier = new Identifier( $raw_identifier ); |
| 160 |
|
| 161 |
if ( $identifier->is_valid() ) { |
| 162 |
$query->orWhere( 'identifier', '=', $identifier->get_value() ); |
| 163 |
} |
| 164 |
|
| 165 |
} ); |
| 166 |
|
| 167 |
} ) |
| 168 |
->when( $blocked_by, function ( Builder $query ) use ( $blocked_by ) { |
| 169 |
|
| 170 |
$users = new WP_User_Query( [ |
| 171 |
'search' => "*{$blocked_by}*", |
| 172 |
'fields' => 'ID', |
| 173 |
] ); |
| 174 |
|
| 175 |
$query->where( 'blocked_by', '=', $users->get_results() ); |
| 176 |
} ) |
| 177 |
->when( $from_date, function ( Builder $query ) use ( $from_date ) { |
| 178 |
$query->where( 'blocked_until', '>=', $from_date ); |
| 179 |
} ) |
| 180 |
->when( $to_date, function ( Builder $query ) use ( $to_date ) { |
| 181 |
$query->where( 'blocked_until', '<=', $to_date ); |
| 182 |
} ); |
| 183 |
|
| 184 |
$total_items = $query->count(); |
| 185 |
|
| 186 |
$blocks = $query |
| 187 |
->forPage( $page, $per_page ) |
| 188 |
->get() |
| 189 |
->map( [ $this, 'resource' ] ) |
| 190 |
->toArray(); |
| 191 |
|
| 192 |
self::response( true, null, [ |
| 193 |
'blocks' => $blocks, |
| 194 |
'current_page' => intval( $page ), |
| 195 |
'total_items' => intval( $total_items ), |
| 196 |
] ); |
| 197 |
} |
| 198 |
|
| 199 |
public function add( WP_REST_Request $request ) { |
| 200 |
|
| 201 |
$raw_identifier = $request->get_param( 'identifier' ); |
| 202 |
$blocked_until = $request->get_param( 'blocked_until' ); |
| 203 |
|
| 204 |
if ( empty( $blocked_until ) ) { |
| 205 |
$blocked_until = null; |
| 206 |
} else { |
| 207 |
|
| 208 |
$timezone = wp_timezone_string(); |
| 209 |
|
| 210 |
$blocked_until = verta() |
| 211 |
->timestamp( intval( $blocked_until ) ) |
| 212 |
->timezone( $timezone ) |
| 213 |
->endDay() |
| 214 |
->toCarbon() |
| 215 |
->utc(); |
| 216 |
|
| 217 |
if ( $blocked_until->isPast() ) { |
| 218 |
self::response( false, __( 'تاریخ � |
| 219 |
سدودیت باید در آینده باشد.', 'pinova' ) ); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
$identifier = $this->expand_identifier( $raw_identifier ); |
| 224 |
|
| 225 |
if ( empty( $identifier ) ) { |
| 226 |
self::response( false, __( 'شناسه وارد شده � |
| 227 |
عتبر ن� |
| 228 |
یباشد.', 'pinova' ) ); |
| 229 |
} |
| 230 |
|
| 231 |
/**@var Block $block */ |
| 232 |
$block = Block::query()->updateOrCreate( [ |
| 233 |
'identifier' => $identifier, |
| 234 |
], [ |
| 235 |
'blocked_by' => get_current_user_id(), |
| 236 |
'blocked_until' => $blocked_until, |
| 237 |
] ); |
| 238 |
|
| 239 |
self::response( true, sprintf( 'شناسه %s با � |
| 240 |
وفقیت � |
| 241 |
سدود شد.', $identifier ), [ |
| 242 |
'block_id' => $block->id, |
| 243 |
'blocks' => $this->blocks(), |
| 244 |
] ); |
| 245 |
} |
| 246 |
|
| 247 |
public function delete( WP_REST_Request $request ) { |
| 248 |
|
| 249 |
$block_id = $request->get_param( 'block_id' ); |
| 250 |
|
| 251 |
try { |
| 252 |
|
| 253 |
/** @var Block $block */ |
| 254 |
$block = Block::query()->findOrFail( $block_id ); |
| 255 |
|
| 256 |
} catch ( \Exception $e ) { |
| 257 |
self::response( false, $e->getMessage() ); |
| 258 |
} |
| 259 |
|
| 260 |
if ( empty( $block->blocked_by ) ) { |
| 261 |
self::response( false, '� |
| 262 |
سدودیهای سیست� |
| 263 |
ی قابل حذف نیستند.', [ |
| 264 |
'blocks' => $this->blocks(), |
| 265 |
] ); |
| 266 |
} |
| 267 |
|
| 268 |
$block->delete(); |
| 269 |
|
| 270 |
self::response( true, 'شناسه با � |
| 271 |
وفقیت رفع � |
| 272 |
سدودی شد.', [ |
| 273 |
'blocks' => $this->blocks(), |
| 274 |
] ); |
| 275 |
} |
| 276 |
|
| 277 |
public function permission_callback( WP_REST_Request $request ): bool { |
| 278 |
return current_user_can( 'manage_options' ); |
| 279 |
} |
| 280 |
|
| 281 |
public function blocks(): array { |
| 282 |
return Block::query() |
| 283 |
->orderByDesc( 'id' ) |
| 284 |
->get() |
| 285 |
->map( [ $this, 'resource' ] ) |
| 286 |
->toArray(); |
| 287 |
} |
| 288 |
|
| 289 |
public function resource( Block $block ): array { |
| 290 |
return [ |
| 291 |
'id' => $block->id, |
| 292 |
'identifier' => $block->identifier, |
| 293 |
'blocked_by' => $block->blocked_by_label, |
| 294 |
'blocked_until' => $block->blocked_until ? Helper::date( $block->blocked_until, 'Y/m/d H:i' ) : null, |
| 295 |
]; |
| 296 |
} |
| 297 |
|
| 298 |
private function expand_identifier( string $raw_identifier = '' ): ?string { |
| 299 |
|
| 300 |
if ( empty( $raw_identifier ) ) { |
| 301 |
return null; |
| 302 |
} |
| 303 |
|
| 304 |
$identifier = new Identifier( $raw_identifier ); |
| 305 |
|
| 306 |
if ( IP::is_valid( $raw_identifier ) ) { |
| 307 |
return $raw_identifier; |
| 308 |
} |
| 309 |
|
| 310 |
if ( $identifier->get_type() === 'username' ) { |
| 311 |
|
| 312 |
$user = get_user_by( 'login', $identifier->get_value() ); |
| 313 |
|
| 314 |
if ( $user instanceof WP_User ) { |
| 315 |
return $user->user_login; |
| 316 |
} |
| 317 |
|
| 318 |
return null; |
| 319 |
} |
| 320 |
|
| 321 |
// Phone/Email |
| 322 |
return $identifier->get_value(); |
| 323 |
} |
| 324 |
} |