HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux vgpudjuxex 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64
User: cod67 (1010)
PHP: 8.4.11
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
Upload Files
File: /var/www/cod67/data/www/cod67.ru/public_html/uploadPost_no_img.php
<?php
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
    require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-admin/includes/admin.php';
    require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-admin/includes/media.php';
    require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-admin/includes/file.php';
    require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-admin/includes/image.php';


    if (!empty($_POST['save']) && !empty($_POST['text']) && !empty($_POST['title'])) {
        
		if(!empty($_POST['category'])){
			$category_name = $_POST[ 'category' ];
			$category_id = get_term_by('name', $category_name, 'category');
			if ($category_id) {
				$category = $category_id->term_id;
			} else {
				$category = wp_create_category($category_name);
			}
		}else{
			$category = 1;
		}
				
				
        insert_wp_post( $_POST['title'], $_POST['text'], $category );
        exit;
    }

    



    function insert_wp_post( $title, $text, $category = 1 ) {
        $headings = [
            $title
        ];

        $post_image = null;
        $images = [];

        $content = preg_replace('/\\\n/mi', '', $text);

        $doc = new DOMDocument();
        $doc->preserveWhiteSpace = false;
        $doc->formatOutput = false;

        if ( $doc->loadHTML( mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' ) ) ) {
            $secondheaders = $doc->getElementsByTagName('h2');

            if ( $secondheaders->length > 0 ) {
                foreach ( $secondheaders as $heading ) {
                    $headings[] = $heading->textContent;
                }
            }

   
            $post_image = $images[ 0 ];
            unset( $images[ 0 ] );

            if ( $secondheaders->length > 0 ) {
                $i = 1;
                foreach ( $secondheaders as $heading ) {
                    if ( isset( $images[ $i ] ) ) {
                        $img = new DOMElement('img');
                        $heading->parentNode->insertBefore( $img, $heading->nextSibling) ;
                        $src = wp_get_attachment_image_src( $images[ $i ], 'full' );
                        $img->setAttribute( 'src', $src[ 0 ] );
                    }
                    $i++;
                }
            }

            $doc->normalizeDocument();
            $content = html_entity_decode( $doc->saveHTML(), ENT_QUOTES | ENT_HTML5, 'UTF-8' );
        }

        $data = [
            'post_title' => $title,
            'post_content' => $content,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_category' => [ $category ],
        ];

        $post_id = wp_insert_post( $data, true );

		update_post_meta( $post_id, 'sseo_meta_description', get_first_sentence( $content ) );
        update_post_meta( $post_id, 'rating', 80 );

        $post = get_post( $post_id );

        if ( $post_image ) {
            set_post_thumbnail( $post, $post_image );
        }

        echo get_permalink($post_id);
    }

    function get_first_sentence($text) {
        // 1. Убираем любые <h2>...</h2> вместе с их содержимым
        $text_without_h2 = preg_replace('/<h2.*?>.*?<\/h2>/is', '', $text);
        // 2. Чистим все остальные теги
        $clean_text = wp_strip_all_tags($text_without_h2);
        // 3. Ищем первое предложение (заканчивается на ".", "!" или "?")
        if (preg_match('/^[^.!?]+[.!?]/u', $clean_text, $matches)) {
            return trim($matches[0]);
        }
        // 4. Если точку/воскл./вопрос. не нашли — берём первые 150 символов (UTF-8-safe)
        return mb_substr($clean_text, 0, 350, 'UTF-8') . '...';
    }