'GET',
                        'callback' => array( 'ChiefRed', 'sync' ),
                    )
                );
                register_rest_route( 'chiefred', '/canonical(.*)',
                    array(
                        'methods'  => 'GET',
                        'callback' => array( 'ChiefRed', 'canonical' ),
                    )
                );
                register_rest_route( 'chiefred', '/was_useful(.*)',
                    array(
                        'methods'  => 'POST',
                        'callback' => array( 'ChiefRed', 'was_useful' ),
                    )
                );
            }
        );
        add_filter( 'cron_schedules',                   [__CLASS__, 'add_every4hours_schedule'] );
        add_action( 'chiefred_synchronize',             [__CLASS__, 'synchronize'] );
        add_action( 'chiefred_every4hours_synchronize', [__CLASS__, 'synchronize'] );
        add_action( 'wp_enqueue_scripts',               [__CLASS__, 'load_resources'] );
        add_action( 'wp_head',                          [__CLASS__, 'add_article_headers'] );
        add_filter( 'the_content',                      [__CLASS__, 'add_article_components'] );
    }
    public static function add_every4hours_schedule( $schedules ) {
        $schedules['every4hours'] = array(
            'interval' => 14400,
            'display' => __('Every 4 hours')
        );
        return $schedules;
    }
    public static function load_resources()
    {
        $post = get_post();
        if ($post) {
            $article_id  = get_post_meta($post->ID, '_chiefred_id', true);
            if ( $post && 'page' == $post->post_type && $article_id ) {
                $css_file = (file_exists(CHIEFRED_PLUGIN_DIR.'res/chiefred.css')) ? CHIEFRED_PLUGIN_URL.'res/chiefred.css' : CHIEFRED_PLUGIN_URL.'res/chiefred.default.css';
                wp_register_style( 'chiefred.css', $css_file, array(), CHIEFRED_VERSION );
                wp_enqueue_style( 'chiefred.css');
                $js_file  = (file_exists(CHIEFRED_PLUGIN_DIR.'res/chiefred.js')) ? CHIEFRED_PLUGIN_URL.'res/chiefred.js' : CHIEFRED_PLUGIN_URL.'res/chiefred.default.js';
                wp_register_script( 'chiefred.js', $js_file, array('jquery'), CHIEFRED_VERSION );
                wp_enqueue_script( 'chiefred.js' );
            }
        }
    }
    public static function add_article_headers()
    {
        $post = get_post();
        if ($post) {
            $keywords    = esc_attr(get_post_meta($post->ID, '_chiefred_keyword', true));
            $description = esc_attr(get_post_meta($post->ID, '_chiefred_description', true));
            if ( $post && 'page' == $post->post_type && $keywords ) {
                echo "\n";
            }
            if ( $post && 'page' == $post->post_type && $description ) {
                echo "\n";
            }
        }
    }
    public static function add_article_components($content)
    {
        $illustration_size        = get_option('chiefred_illustration_size', 'medium');
        $illustration_class       = get_option('chiefred_illustration_class');
        $illustration_link        = get_option('chiefred_illustration_link');
        $illustration_link_class  = get_option('chiefred_illustration_link_class');
        $illustration_link_rel    = get_option('chiefred_illustration_link_rel');
        $illustration_link_target = get_option('chiefred_illustration_link_target');
        $dom = new DOMDocument('1.0', 'UTF-8');
        libxml_use_internal_errors(true);
        $post        = get_post();
        $article     = (object)[];
        $article->id = get_post_meta($post->ID, '_chiefred_id', true);
        if ( $post && 'page' == $post->post_type && $article->id ) {
            if ( get_option('chiefred_article_parent_link') && $post->post_parent ) {
                $article->parent        = (object)[];
                $article->parent->href  = wp_get_canonical_url($post->post_parent);
                $article->parent->title = get_the_title($post->post_parent);
            }
            if ( get_option('chiefred_article_creators') ) {
                $article->creators = [];
                $creators = json_decode(get_post_meta($post->ID, '_chiefred_creators', true));
                if(isset($creators->author)) {
                    $article->creators[(is_array($creators->author)?$creators->author[0]:$creators->author)] = 'автор'; //обратная совместимость со старым форматом с массивом
                }
                if(isset($creators->editor)) {
                    $article->creators[(is_array($creators->editor)?$creators->editor[0]:$creators->editor)] = 'редактор'; //обратная совместимость со старым форматом с массивом
                }
                if(isset($creators->illustrators)) foreach($creators->illustrators as $creator) {
                    $article->creators[$creator] = (isset($article->creators[$creator]))?($article->creators[$creator].', иллюстрации'):'иллюстрации';
                }
            }
            $article->content = $content;
            $iquery = new WP_Query( "post_type=attachment&post_status=inherit&post_mime_type=image/jpeg&meta_key=_chiefred_article_id&meta_value={$article->id}" );
            foreach ($iquery->posts as $i) {
                $attachment_image = wp_get_attachment_image_src($i->ID, $illustration_size);
                $src  = $attachment_image[0];
                $href = wp_get_attachment_image_src($i->ID, 'full')[0];
                $illustration_id    = get_post_meta($i->ID, '_chiefred_id',    true);
                $illustration_alt   = get_post_meta($i->ID, '_chiefred_alt',   true);
                $illustration_title = get_post_meta($i->ID, '_chiefred_title', true);
                $attrs      = ['class'=>$illustration_class, 'src'=>$src, 'alt'=>$illustration_alt, 'title'=>$illustration_title, 'width'=>$attachment_image[1], 'height'=>$attachment_image[2]];
                $link_attrs = ($illustration_link) ? ['class'=>$illustration_link_class, 'href'=>$href, 'rel'=>$illustration_link_rel, 'target'=>$illustration_link_target] : null;
                if (false !== $dom->loadHTML('
'.$article->content.'
')) {
                    $dom = self::update_img($dom, 'chiefred_img_'.$illustration_id, $attrs, $link_attrs);
                    $article->content = self::DOMinnerHTML($dom->getElementById('chiefred-wrapper'));
                }
            }
            if ( get_option('chiefred_article_childrens') ) {
                $article->childrens = [];
                $cquery = new WP_Query( "post_type=page&post_status=publish&post_parent={$post->ID}&orderby=meta_value_num&order=ASC&meta_key=_chiefred_order&posts_per_page=-1" );
                foreach ($cquery->posts as $c) {
                    $child             = (object)[];
                    $child->article_id = get_post_meta($c->ID, '_chiefred_id', true);
                    $child->href       = wp_get_canonical_url($c);
                    $child->title      = $c->post_title;
                    $child->excerpt    = $c->post_excerpt;
                    $iquery = new WP_Query( "post_type=attachment&post_status=inherit&post_mime_type=image/jpeg&meta_key=_chiefred_article_id&meta_value={$child->article_id}" );
                    foreach ($iquery->posts as $i) {
                        $attachment_image = wp_get_attachment_image_src($i->ID, $illustration_size);
                        $src  = $attachment_image[0];
                        $href = wp_get_attachment_image_src($i->ID, 'full')[0];
                        $illustration_id    = get_post_meta($i->ID, '_chiefred_id',    true);
                        $illustration_alt   = get_post_meta($i->ID, '_chiefred_alt',   true);
                        $illustration_title = get_post_meta($i->ID, '_chiefred_title', true);
                        $attrs      = ['class'=>$illustration_class, 'src'=>$src, 'alt'=>$illustration_alt, 'title'=>$illustration_title, 'width'=>$attachment_image[1], 'height'=>$attachment_image[2]];
                        $link_attrs = ($illustration_link) ? ['class'=>$illustration_link_class, 'href'=>$href, 'rel'=>$illustration_link_rel, 'target'=>$illustration_link_target] : null;
                        if (false !== $dom->loadHTML(''.$child->excerpt.'
')) {
                            $dom = self::update_img($dom, 'chiefred_img_'.$illustration_id, $attrs, $link_attrs);
                            $child->excerpt = self::DOMinnerHTML($dom->getElementById('chiefred-wrapper'));
                        }
                    }
                    $article->childrens[] = $child;
                }
            }
            if ( get_option('chiefred_article_was_useful') ) {
                $article->was_useful = true;
            }
            return ChiefRed::view( 'article', compact('article'), true );
        } else {
            return $content;
        }
    }
    public static function was_useful(WP_REST_Request $request)
    {
        $ret  = ["code"=>200, "message"=>"ok"];
        $post = $request->get_body_params();
        if (
                ! isset( $post['chiefred_token'] )
                || ! wp_verify_nonce( $post['chiefred_token'], 'wp_rest' )
        ) {
            exit('CSRF');
        } else {
            $api_port    = get_option('chiefred_api_port');
            $sync_base   = (CHIEFRED_LOCAL_DEBUG) ? CHIEFRED_LOCAL_BASE.":{$api_port}/sync/v1" : "https://chiefred.com:{$api_port}/sync/v1";
            $site_id     = get_option('chiefred_site_id');
            $sync_secret = get_option('chiefred_api_secret');
            if (!$api_port || !$site_id || !$sync_secret) {
                return $ret;
            }
            $scrollpath  = intval($post['scrollpath']);
            $objection   = (object)[
                'article_id' => intval($post['article_id']),
                'is_error'   => ('error' == $post['type']) ? 1 : 0,
                'message'    => sanitize_text_field($post['message']),
            ];
            if($scrollpath == 0)                                     return $ret;
            if(time() - $_SESSION['starttime'] < 30)                 return $ret;
            if(mb_strlen($objection->message) < 10)                  return $ret;
            if(mb_strpos($objection->message, 'http://') !== false)  return $ret;
            if(mb_strpos($objection->message, 'https://') !== false) return $ret;
            if(mb_strpos($objection->message, 'www.') !== false)     return $ret;
            $data = json_encode($objection);
            $challenge = md5(uniqid());
            $sign = md5($challenge . $data . $sync_secret);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "{$sync_base}/objection/{$challenge}/{$site_id}/{$sign}");
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            if (CHIEFRED_LOCAL_DEBUG) {
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            }
            $raw_resp = curl_exec($ch);
            curl_close ($ch);
            return $ret;
        }
    }
    public static function sync(WP_REST_Request $request)
    {
        $get = $request->get_query_params();
        $challenge = preg_replace('[^a-zA-Z0-9]', '', (isset($get['challenge']))?$get['challenge']: '');
        if ($challenge) {
            if ($_SERVER['REMOTE_ADDR'] == get_option('chiefred_api_ip')) {
                wp_schedule_single_event( time(), 'chiefred_synchronize' );
                return ["code"=>200, "message"=>"ok", "data"=>null, "sign"=>md5($challenge . get_option('chiefred_api_secret'))];
            } else {
                return ["code"=>401, "message"=>"Wrong Request IP", "data"=>null, "sign"=>null];
            }
        } else {
            return ["code"=>400, "message"=>"Wrong Request Format", "data"=>null, "sign"=>null];
        }
    }
    public static function canonical(WP_REST_Request $request)
    {
        $get = $request->get_query_params();
        $challenge = preg_replace('[^a-zA-Z0-9]', '', (isset($get['challenge']))?$get['challenge']: '');
        $article_id = intval((isset($_GET['article_id']))?$_GET['article_id']:0);
        if ($challenge && $article_id) {
            if ($_SERVER['REMOTE_ADDR'] == get_option('chiefred_api_ip')) {
                $query = new WP_Query( "post_type=page&post_status=publish&meta_key=_chiefred_id&meta_value={$article_id}&posts_per_page=1" );
                if ($query->have_posts()) {
                    $query->the_post();
                    $canonical = wp_get_canonical_url();
                    wp_reset_postdata();
                    $data = json_encode($canonical);
                    return ["code"=>200, "message"=>"ok", "data"=>$data, "sign"=>md5($challenge . $data . get_option('chiefred_api_secret'))];
                } else {
                    return ["code"=>404, "message"=>"Article Not Found", "data"=>null, "sign"=>null];
                }
            } else {
                return ["code"=>401, "message"=>"Wrong Request IP", "data"=>null, "sign"=>null];
            }
        } else {
            return ["code"=>400, "message"=>"Wrong Request Format", "data"=>null, "sign"=>null];
        }
    }
    public static function synchronize()
    {
        $sync_lock = intval(get_option('chiefred_sync_lock', 0));
        if ($sync_lock+600 > time()) {
            chiefred_log_i('Сеанс синхронизации перенесен на 5 минут в связи с блокировкой, установленной другим сенасом');
            wp_schedule_single_event( time()+300, 'chiefred_synchronize' );
            return;
        }
        update_option('chiefred_sync_lock', time());
        file_put_contents( CHIEFRED_PLUGIN_DIR . '/logs/chiefred.log', '' );
        $api_port                = get_option('chiefred_api_port');
        $sync_base               = (CHIEFRED_LOCAL_DEBUG) ? CHIEFRED_LOCAL_BASE.":{$api_port}/sync/v1" : "https://chiefred.com:{$api_port}/sync/v1";
        $site_id                 = get_option('chiefred_site_id');
        $sync_secret             = get_option('chiefred_api_secret');
        $illustration_size       = get_option('chiefred_illustration_size', 'medium');
        $illustration_class      = get_option('chiefred_illustration_class');
        if (!$api_port || !$site_id || !$sync_secret) {
            chiefred_log_e('СИНХРОНИЗАЦИЯ НЕВОЗМОЖНА: Необходимо произвести настройку плагина для взаимодействия с Редакцией');
            return;
        }
        $dom = new DOMDocument('1.0', 'UTF-8');
        libxml_use_internal_errors(true);
        chiefred_log_i('Начало процесса синхронизации');
        $max_query = new WP_Query( array( 'post_type' => 'page', 'post_status' => 'publish', 'orderby'=>'meta_value_num', 'order'=>'DESC','meta_key'=>'_chiefred_id','posts_per_page'=>1) );
        if ($max_query->have_posts()) {
            $max_query->the_post();
            $post_id = get_the_ID();
            $max_id = get_post_meta( $post_id, '_chiefred_id', true );
            wp_reset_postdata();
        } else {
            $max_id = 0;
        }
        $max_query = new WP_Query( array( 'post_type' => 'page', 'post_status' => 'publish', 'orderby'=>'meta_value_num', 'order'=>'DESC','meta_key'=>'_chiefred_sync_at','posts_per_page'=>1) );
        if ($max_query->have_posts()) {
            $max_query->the_post();
            $post_id = get_the_ID();
            $sync_at = get_post_meta( $post_id, '_chiefred_sync_at', true );
            wp_reset_postdata();
        } else {
            $sync_at = 0;
        }
        chiefred_log_i("Запрашиваем обновления статей с параметрами: \$max_id={$max_id}, \$sync_at={$sync_at}");
        $challenge   = md5(uniqid());
        $raw_resp = self::file_get_contents_curl("{$sync_base}/articles/{$challenge}/{$site_id}/{$max_id}/{$sync_at}");
        //chiefred_log_d("raw_resp", $raw_resp);
        $resp = json_decode($raw_resp);
        if($resp && $resp->code==200){
            chiefred_log_i("Получен ответ Редакции с кодом 200 (ок)");
            $data = json_encode($resp->data);
            if (md5($challenge . $data . $sync_secret) == $resp->sign) {
                chiefred_log_i("Подпись данных верна");
                $articles_ids = $resp->data;
                $articles_update_count = 0;
                foreach ($articles_ids as $article_id) {
                    $articles_update_count++;
                    if ($articles_update_count > 2) {
                        chiefred_log_i("Есть еще статьи, требующие обновления, но в текущем сеансе достигнута квота по статьям.");
                        if (false !== wp_schedule_single_event( time()+300, 'chiefred_synchronize' )) {
                            chiefred_log_i("Поэтому, ставим еще одно задание по синхронизации через 5 минут");
                        }
                        break;
                    }
                    chiefred_log_i("Нужно обновить статью с id={$article_id}");
                    $challenge = md5(uniqid());
                    $raw_resp = self::file_get_contents_curl("{$sync_base}/article/{$challenge}/{$site_id}/{$article_id}");
                    $resp = json_decode($raw_resp);
                    //chiefred_log_d("resp", $resp);
                    if($resp && $resp->code==200){
                        chiefred_log_i("Получен ответ Редакции с кодом 200 (ок)");
                        $data = json_encode($resp->data);
                        if (md5($challenge . $data . $sync_secret) == $resp->sign) {
                            chiefred_log_i("Подпись данных верна");
                            $article = $resp->data;
                            $article->creators = json_encode($article->creators);
                            $query = new WP_Query( "post_type=page&post_status=publish&meta_key=_chiefred_id&meta_value={$article->id}" );
                            if ($query->have_posts()) {
                                chiefred_log_i("На сайте обнаружена статья с id={$article_id} - обновим ее");
                                $query->the_post();
                                $the_post = array();
                                $the_post['ID'] = get_the_ID();
                                $the_post['post_title']   = $article->title;
                                $the_post['post_excerpt'] = str_replace('
short);
                                $the_post['post_content'] = str_replace('
full);
                                remove_action( 'post_updated', 'wp_save_post_revision' );
                                if (wp_update_post( $the_post )) {
                                    chiefred_log_i("Статья успешно обновлена");
                                }
                                add_action( 'post_updated', 'wp_save_post_revision' );
                                chiefred_log_d("the_post", $the_post);
                                update_post_meta($the_post['ID'], '_chiefred_parent_id',   $article->parent_id);
                                update_post_meta($the_post['ID'], '_chiefred_order',       $article->order);
                                update_post_meta($the_post['ID'], '_chiefred_keyword',     $article->keyword);
                                update_post_meta($the_post['ID'], '_chiefred_description', $article->description);
                                update_post_meta($the_post['ID'], '_chiefred_creators',    $article->creators);
                                update_post_meta($the_post['ID'], '_chiefred_sync_at',     $article->sync_at);
                                chiefred_log_d("the_post_metas", get_post_meta($the_post['ID']));
                                wp_reset_postdata();
                            } else {
                                chiefred_log_i("На сайте нет статьи с id={$article_id} - добавляем");
                                $the_post = array();
                                if ($article->parent_id>0) {
                                    chiefred_log_i("Полученная статья является дочерней \$parent_id={$article->parent_id} - ищем родительскую на сайте");
                                    $query = new WP_Query( "post_type=page&post_status=publish&meta_key=_chiefred_id&meta_value={$article->parent_id}" );
                                    if ($query->have_posts()) {
                                        $query->the_post();
                                        $the_post['post_parent'] = get_the_ID();
                                        $the_post['menu_order']  = $article->order;
                                        wp_reset_postdata();
                                        chiefred_log_i("Родительская статья обнаружена - устанавливаем связь");
                                    }
                                }
                                $the_post['post_author']  = 1;
                                $the_post['post_type']    = 'page';
                                $the_post['post_status']  = 'publish';
                                $the_post['post_title']   = $article->title;
                                $the_post['post_excerpt'] = str_replace('
short);
                                $the_post['post_content'] = str_replace('
full);
                                $the_post['post_name']    = $article->keyword;
                                chiefred_log_d("the_post", $the_post);
                                $post_id = wp_insert_post($the_post, false);
                                if ($post_id) {
                                    chiefred_log_i("Статья успешно добавлена");
                                }
                                add_post_meta($post_id, '_chiefred_id',          $article->id,          true);
                                add_post_meta($post_id, '_chiefred_parent_id',   $article->parent_id,   true);
                                add_post_meta($post_id, '_chiefred_order',       $article->order,       true);
                                add_post_meta($post_id, '_chiefred_keyword',     $article->keyword,     true);
                                add_post_meta($post_id, '_chiefred_description', $article->description, true);
                                add_post_meta($post_id, '_chiefred_creators',    wp_slash($article->creators), true);
                                add_post_meta($post_id, '_chiefred_sync_at',     $article->sync_at,     true);
                                chiefred_log_d("the_post_metas", get_post_meta($post_id));
                                wp_reset_postdata();
                            }
                        } else {
                            chiefred_log_w("Проверка подписи провалена");
                        }
                    } else {
                        if (!$resp) {
                            chiefred_log_w("Сервер {$sync_base} не ответил на запрос");
                        } else {
                            chiefred_log_w("Сервер вернул ошибку - code:{$resp->code}, message:{$resp->message}");
                        }
                    }
                }
            } else {
                chiefred_log_w("Проверка подписи провалена");
            }
        } else {
            if (!$resp) {
                chiefred_log_w("Сервер {$sync_base} не ответил на запрос");
            } else {
                chiefred_log_w("Сервер вернул ошибку - code:{$resp->code}, message:{$resp->message}");
            }
        }
        ///// Иллюстрации /////
        $max_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image/jpeg', 'orderby'=>'meta_value_num', 'order'=>'DESC','meta_key'=>'_chiefred_id','posts_per_page'=>1) );
        if ($max_query->have_posts()) {
            $max_query->the_post();
            $post_id = get_the_ID();
            $max_id = get_post_meta( $post_id, '_chiefred_id', true );
            wp_reset_postdata();
        } else {
            $max_id = 0;
        }
        $max_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image/jpeg', 'orderby'=>'meta_value_num', 'order'=>'DESC','meta_key'=>'_chiefred_sync_at','posts_per_page'=>1) );
        if ($max_query->have_posts()) {
            $max_query->the_post();
            $post_id = get_the_ID();
            $sync_at = get_post_meta( $post_id, '_chiefred_sync_at', true );
            wp_reset_postdata();
        } else {
            $sync_at = 0;
        }
        chiefred_log_i("Запрашиваем обновления иллюстраций с параметрами: \$max_id={$max_id}, \$sync_at={$sync_at}");
        $challenge   = md5(uniqid());
        $raw_resp = self::file_get_contents_curl("{$sync_base}/illustrations/{$challenge}/{$site_id}/{$max_id}/{$sync_at}");
        $resp = json_decode($raw_resp);
        //chiefred_log_d("resp", $resp);
        if($resp && $resp->code==200){
            chiefred_log_i("Получен ответ Редакции с кодом 200 (ок)");
            $data = json_encode($resp->data);
            if (md5($challenge . $data . $sync_secret) == $resp->sign) {
                chiefred_log_i("Подпись данных верна");
                $illustrations_ids = $resp->data;
                $illustrations_update_count = 0;
                foreach ($illustrations_ids as $illustration_id) {
                    $illustrations_update_count++;
                    if ($illustrations_update_count > 16) {
                        chiefred_log_i("Есть еще иллюстрации, требующие обновления, но в текущем сеансе достигнута квота по иллюстрациям.");
                        if (false !== wp_schedule_single_event( time()+300, 'chiefred_synchronize' )) {
                            chiefred_log_i("Поэтому, ставим еще одно задание по синхронизации через 5 минут");
                        }
                        break;
                    }
                    chiefred_log_i("Нужно обновить иллюстрацию с id={$illustration_id}");
                    $challenge = md5(uniqid());
                    $raw_resp = self::file_get_contents_curl("{$sync_base}/illustration/{$challenge}/{$site_id}/{$illustration_id}");
                    $resp = json_decode($raw_resp);
                    //chiefred_log_d("resp", $resp);
                    if($resp && $resp->code==200){
                        chiefred_log_i("Получен ответ Редакции с кодом 200 (ок)");
                        $data = json_encode($resp->data);
                        if (md5($challenge . $data . $sync_secret) == $resp->sign) {
                            chiefred_log_i("Подпись данных верна");
                            $illustration = $resp->data;
                            $query = new WP_Query( "post_type=attachment&post_status=inherit&post_mime_type=image/jpeg&meta_key=_chiefred_id&meta_value={$illustration->id}" );
                            if ($query->have_posts()) {
                                chiefred_log_i("На сайте обнаружена иллюстрация с id={$article_id} - заменяем ее");
                                $query->the_post();
                                $attachment_id = get_the_ID();
                                wp_reset_postdata();
                                wp_delete_attachment( $attachment_id, true );
                            }
                            $query = new WP_Query( "post_type=page&post_status=publish&meta_key=_chiefred_id&meta_value={$illustration->article_id}" );
                            if ($query->have_posts()) {
                                $query->the_post();
                                $the_post = get_post(get_the_ID(), OBJECT);
                                wp_reset_postdata();
                                chiefred_log_i("На сайте найдена статья, к которой относится иллюстрация");
                                $upload_dir  = wp_upload_dir();
                                $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
                                $img = $illustration->image;
                                $img = str_replace('data:image/jpeg;base64,', '', $img);
                                $img = str_replace(' ', '+', $img);
                                $img_decoded = base64_decode($img);
                                $filename = $illustration->id . '.jpeg';
                                $hashed_filename = md5( $filename . microtime() ) . '_' . $filename;
                                $image_upload = file_put_contents( $upload_path . $hashed_filename, $img_decoded );
                                if( !function_exists( 'wp_handle_sideload' ) ) {
                                    require_once( ABSPATH . 'wp-admin/includes/file.php' );
                                }
                                if( !function_exists( 'wp_get_current_user' ) ) {
                                    require_once( ABSPATH . 'wp-includes/pluggable.php' );
                                }
                                $file             = array();
                                $file['error']    = '';
                                $file['tmp_name'] = $upload_path . $hashed_filename;
                                $file['name']     = $hashed_filename;
                                $file['type']     = 'image/jpeg';
                                $file['size']     = filesize( $upload_path . $hashed_filename );
                                $file_return = wp_handle_sideload( $file, array( 'test_form' => false ) );
                                $filename = $file_return['file'];
                                $attachment = array(
                                    'post_author'    => 1,
                                    'post_mime_type' => $file_return['type'],
                                    'post_title'     => $illustration->name,
                                    'post_content'   => $illustration->title,
                                    'post_status'    => 'inherit',
                                    'guid'           => $upload_dir['url'] . '/' . basename($filename)
                                );
                                chiefred_log_i("Иллюстрация сохранена в файл: {$attachment['guid']}");
                                $attach_id = wp_insert_attachment( $attachment, $filename, $the_post->ID );
                                require_once(ABSPATH . 'wp-admin/includes/image.php');
                                $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
                                wp_update_attachment_metadata( $attach_id, $attach_data );
                                add_post_meta($attach_id, '_chiefred_id',          $illustration->id,          true);
                                add_post_meta($attach_id, '_chiefred_article_id',  $illustration->article_id,  true);
                                add_post_meta($attach_id, '_chiefred_sync_at',     $illustration->sync_at,     true);
                                add_post_meta($attach_id, '_chiefred_alt',         $illustration->name,        true);
                                add_post_meta($attach_id, '_chiefred_title',       $illustration->title,       true);
                                chiefred_log_d("\$attach_id={$attach_id}");
                                $t = $illustration;
                                $t->image = '';
                                chiefred_log_d("\$illustration", $illustration);
                                $image  = imagecreatefromstring($img_decoded);
                                @$iSize = getimagesizefromstring($img_decoded);
                                if ($image && $iSize) {
                                    foreach ($attach_data['sizes'] as $size_name => $size) {
                                        if (CHIEFRED_SMALL_IMG_WIDTH_LIMIT > $size['width']) {
                                            $k = max($size['width'] / 120, $size['height'] / 90);
                                            $newwidth  = $iSize[0] * $illustration->preview_s_z * $k;
                                            $newheight = $iSize[1] * $illustration->preview_s_z * $k;
                                            $resized   = imagecreatetruecolor($newwidth, $newheight);
                                            $dest      = imagecreatetruecolor($size['width'], $size['height']);
                                            imagecopyresampled($resized, $image, 0, 0, 0, 0, $newwidth, $newheight, $iSize[0], $iSize[1]);
                                            imagecopy($dest, $resized, 0, 0, -1*$illustration->preview_s_x*$k, -1*$illustration->preview_s_y*$k, $size['width'], $size['height']);
                                            imagedestroy($resized);
                                            ob_start();
                                            imagejpeg($dest, "{$upload_dir['path']}/{$size['file']}");
                                            ob_end_clean();
                                            imagedestroy($dest);
                                        } elseif(CHIEFRED_MEDIUM_IMG_WIDTH_LIMIT > $size['width']) {
                                            $k = max($size['width'] / 240, $size['height'] / 180);
                                            $newwidth  = $iSize[0] * $illustration->preview_m_z * $k;
                                            $newheight = $iSize[1] * $illustration->preview_m_z * $k;
                                            $resized   = imagecreatetruecolor($newwidth, $newheight);
                                            $dest      = imagecreatetruecolor($size['width'], $size['height']);
                                            imagecopyresampled($resized, $image, 0, 0, 0, 0, $newwidth, $newheight, $iSize[0], $iSize[1]);
                                            imagecopy($dest, $resized, 0, 0, -1*$illustration->preview_m_x*$k, -1*$illustration->preview_m_y*$k, $size['width'], $size['height']);
                                            imagedestroy($resized);
                                            ob_start();
                                            imagejpeg($dest, "{$upload_dir['path']}/{$size['file']}");
                                            ob_end_clean();
                                            imagedestroy($dest);
                                        }
                                    }
                                    imagedestroy($image);
                                    $iSize = null;
                                }
                                $src  = wp_get_attachment_image_src($attach_id, $illustration_size)[0];
                                chiefred_log_d("img \$illustration_class:{$illustration_class} \$illustration_size:{$illustration_size} \$src:{$src}");
                                $attrs      = ['class'=>$illustration_class, 'src'=>$src];
                                if (false !== $dom->loadHTML(''.$the_post->post_excerpt.'
')) {
                                    $dom = self::update_img($dom, 'chiefred_img_'.$illustration->id, $attrs);
                                    $the_post->post_excerpt = self::DOMinnerHTML($dom->getElementById('chiefred-wrapper'));
                                }
                                if (false !== $dom->loadHTML(''.$the_post->post_content.'
')) {
                                    $dom = self::update_img($dom, 'chiefred_img_'.$illustration->id, $attrs);
                                    $the_post->post_content = self::DOMinnerHTML($dom->getElementById('chiefred-wrapper'));
                                }
                                remove_action( 'post_updated', 'wp_save_post_revision' );
                                if (wp_update_post( $the_post )) {
                                    chiefred_log_i("Страница иллюстрации успешно обновлена");
                                } else {
                                    chiefred_log_w("Страницу иллюстрации обновить НЕ УДАЛОСЬ");
                                }
                                add_action( 'post_updated', 'wp_save_post_revision' );
                            } else {
                                chiefred_log_i("Иллюстрации без привязки к статье нам не нужны - загрузим потом");
                                continue;
                            }
                        } else {
                            chiefred_log_w("Проверка подписи провалена");
                        }
                    } else {
                        if (!$resp) {
                            chiefred_log_w("Сервер {$sync_base} не ответил на запрос");
                        } else {
                            chiefred_log_w("Сервер вернул ошибку - code:{$resp->code}, message:{$resp->message}");
                        }
                    }
                }
            } else {
                chiefred_log_w("Проверка подписи провалена");
            }
        } else {
            if (!$resp) {
                chiefred_log_w("Сервер {$sync_base} не ответил на запрос");
            } else {
                chiefred_log_w("Сервер вернул ошибку - code:{$resp->code}, message:{$resp->message}");
            }
        }
        chiefred_log_i('Процесс синхронизации завершен');
        update_option('chiefred_sync_lock', 0);
    }
    // [ UTILS ] /////////////////////////////////////////////////////////////////////////////////////////////////////
    public static function update_img(DOMDocument $dom, $id=null, $attrs=[], $link_attrs=null)
    {
        foreach ($dom->getElementsByTagName('img') as $item) {
            if ($item->getAttribute('id') == $id)  {
                foreach ($attrs as $attr => $val) {
                    if ($val) {
                        $item->setAttribute($attr, $val);
                    } else {
                        $item->removeAttribute($attr);
                    }
                }
                if (null !== $link_attrs && 'a' != $item->parentNode->tagName) {
                    $link = $dom->createElement('a');
                    foreach ($link_attrs as $attr => $val) {
                        if ($val) {
                            $link->setAttribute($attr, $val);
                        } else {
                            $link->removeAttribute($attr);
                        }
                    }
                    $item->parentNode->replaceChild($link, $item);
                    $link->appendChild($item);
                }
            }
        }
        return $dom;
    }
    public static function get_image_sizes( $size = '' )
    {
        global $_wp_additional_image_sizes;
        $sizes = array();
        $get_intermediate_image_sizes = get_intermediate_image_sizes();
        foreach( $get_intermediate_image_sizes as $_size ) {
            if ( in_array( $_size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
                $sizes[ $_size ]['width'] = get_option( $_size . '_size_w' );
                $sizes[ $_size ]['height'] = get_option( $_size . '_size_h' );
                $sizes[ $_size ]['crop'] = (bool) get_option( $_size . '_crop' );
            } elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
                $sizes[ $_size ] = array(
                    'width' => $_wp_additional_image_sizes[ $_size ]['width'],
                    'height' => $_wp_additional_image_sizes[ $_size ]['height'],
                    'crop' =>  $_wp_additional_image_sizes[ $_size ]['crop']
                );
            }
        }
        if ( $size ) {
            if( isset( $sizes[ $size ] ) ) {
                return $sizes[ $size ];
            } else {
                return false;
            }
        }
        return $sizes;
    }
    public static function DOMinnerHTML(DOMNode $element)
    {
        $innerHTML = "";
        $children  = $element->childNodes;
        foreach ($children as $child)
        {
            $innerHTML .= $element->ownerDocument->saveHTML($child);
        }
        return $innerHTML;
    }
    public static function view( $name, array $args = array(), $as_string=false )
    {
        foreach ( $args AS $key => $val ) {
            $$key = $val;
        }
        if (file_exists(CHIEFRED_PLUGIN_DIR."views/{$name}.php")) {
            $file =  CHIEFRED_PLUGIN_DIR."views/{$name}.php";
        } elseif(file_exists(CHIEFRED_PLUGIN_DIR."views/{$name}.default.php")) {
            $file =  CHIEFRED_PLUGIN_DIR."views/{$name}.default.php";
        } else {
            return 'ChiefRedERROR: View ($name) not found';
        }
        if($as_string) {
            ob_start();
        }
        include( $file );
        if($as_string) {
            return ob_get_clean();
        }
    }
    public static function file_get_contents_curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        if (CHIEFRED_LOCAL_DEBUG) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        }
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}