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/Action/AddMediaFromUrlAction.php
<?php


namespace Vehica\Action;


/**
 * Class AddMediaFromUrlAction
 * @package Vehica\Action
 */
class AddMediaFromUrlAction
{
    /**
     * @param $url
     * @return int|false
     */
    public static function add($url)
    {
        $uploadDirectory = wp_upload_dir();
        $imageData = file_get_contents($url);
        $filename = basename($url);

        if (wp_mkdir_p($uploadDirectory['path'])) {
            $file = $uploadDirectory['path'] . '/' . $filename;
        } else {
            $file = $uploadDirectory['basedir'] . '/' . $filename;
        }

        file_put_contents($file, $imageData);

        $fileType = wp_check_filetype($filename);

        $attachment = [
            'post_mime_type' => $fileType['type'],
            'post_title' => sanitize_file_name($filename),
            'post_content' => '',
            'post_status' => 'inherit'
        ];

        $attachmentId = wp_insert_attachment($attachment, $file);
        if (is_wp_error($attachmentId)) {
            return false;
        }

        require_once ABSPATH . 'wp-admin/includes/image.php';
        $attachmentData = wp_generate_attachment_metadata($attachmentId, $file);
        wp_update_attachment_metadata($attachmentId, $attachmentData);

        return $attachmentId;
    }

}