HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux mail.btech-izolacje.pl 5.15.0-140-generic #150-Ubuntu SMP Sat Apr 12 06:00:09 UTC 2025 x86_64
User: pewna6876 (1017)
PHP: 8.2.28
Disabled: NONE
Upload Files
File: /home/pewnabryka.pl/public_html/wp-content/plugins/vehica-core/app/Core/CreateConfig.php
<?php

namespace Vehica\Core;

if (!defined('ABSPATH')) {
    exit;
}

use Vehica\Core\Post\PostStatus;
use Vehica\Model\Post\Config\Config;
use WP_Error;

/**
 * Class CreateConfig
 * @package Vehica\Core
 */
class CreateConfig
{
    /**
     * @var string
     */
    public $key;

    /**
     * @var string
     */
    public $name;

    /**
     * CreateConfig constructor.
     * @param string $key
     * @param string $name
     */
    public function __construct($key, $name)
    {
        $this->key = $key;
        $this->name = $name;
    }

    /**
     * @return bool
     */
    public function exists()
    {
        return vehicaApp('configs')->find(function ($config) {
                /* @var Config $config */
                return $config->getType() === $this->key;
            }) !== false;
    }

    /**
     * @return int|WP_Error
     */
    public function create()
    {
        return wp_insert_post([
            'post_title' => $this->name,
            'post_type' => Config::POST_TYPE,
            'post_status' => PostStatus::PUBLISH,
            'meta_input' => [
                Config::TYPE => $this->key
            ]
        ]);
    }

}