<commit_before><?php
namespace Kiku;

class Image {

    public function get_entry_image(bool $datauri = true, int $post_id = 0): string {
        global $post;
        $image_src = '';

        if ($post_id == 0) {
            $post_id = $post->ID;
        }

        // denied post
        if ( post_password_required() ) {
            return "";
        }

        // has thumbnail custom field
        $image_src = get_post_meta($post_id, CF_THUMBNAIL, true);
        if ( $this->is_correct_image($image_src) ) {
            return $image_src;
        }

        // has ASIN custom field
        $image_src = $this->get_asin_image($post_id);
        if (!empty($image_src)) {
            return $image_src;
        }

        // has thumbnail
        if ( has_post_thumbnail() ) {
            $image_src = $this->get_post_thumbnail_image();
            if ( $this->is_correct_image($image_src) ) {
                return $image_src;
            }
        }

        // has img tag
        $image_src = $this->get_post_image_from_tag($datauri);

        return $image_src;
    }

    public function get_post_thumbnail_image($size = null): string {
        global $post;
        $url = "";
        $attachment_image_src = null;

        if ( !has_post_thumbnail() ) {
            return "";
        }

        $thumbnail_id = get_post_thumbnail_id();

        if ($size == null) {
            // wp_get_attachment_image_src -> thumbnail, medium, large, full
            // try large size
            $attachment_image_src = wp_get_attachment_image_src($thumbnail_id, 'large');

            // try full
            if ( empty($attachment_image_src) ) {
                $attachment_image_src = wp_get_attachment_image_src($thumbnail_id, 'full');
            }

            // retry medium
            if ( empty($attachment_image_src) ) {
                $attachment_image_src = wp_get_attachment_image_src($thumbnail_id, 'medium');
            }

        } else {
            $attachment_image_src = wp_get_attachment_image_src($thumbnail_id, $size);
        }

        if ($attachment_image_src) {
            $url = (string)$attachment_image_src[0];
        }

        return $url;
    }

    public function get_post_image_from_tag($datauri = true): string {
        global $post;
        $src = "";
        $content = "";

        // get from Amazon Product Tag
        $content = get_post_meta($post->ID, CF_AMAZON_PRODUCT_TAG, true);

        if ( empty($content) ) {
            $content = $post->post_content;
        }

        // maybe URL or Path, sortcode?
        if ( preg_match('/<img.*?src=(["\'])(.+?)\1.*?>/i', $content, $match) ) {
            $src = $match[2];
        }

        // bye
        if ( empty($src) ) {
            return "";
        }

        // image file?
        if ( Util::is_image($src) ) {
            return Util::relative_to_absolute_url($src);
        }

        // shortcode?
        if ( Util::is_shortcode($src) ) {
            $src = do_shortcode($src);

            if ( Util::is_url($src) && Util::is_image($src) ) {
                return Util::relative_to_absolute_url($src);
            }
            // denied DataURI
            if ( !$datauri ) {
                return "";
            }
            // not Data URI
            if ( !Util::is_dataURI($src) ) {
                return "";
            }
        }

        return $src;
    }

    private function is_correct_image(string $src): bool {
        return Util::is_url($src) && Util::is_image($src);
    }

    public function get_asin_image($post_id) {
        $product_data = get_post_meta($post_id, CF_AMAZON_PRODUCT_TAG, true);<commit_msg>clean: Imageクラスのリファクタリング<commit_after><?php
namespace Kiku;

class Image {

    public function get_entry_image(bool $datauri = true, int $post_id = 0): string {
        global $post;
        $image_src = "";

        if ($post_id == 0) {
            $post_id = $post->ID;
        }

        // denied post
        if ( post_password_required() ) {
            return "";
        }

        // has thumbnail custom field
        $image_src = $this->get_meta_thumbnail_image($post_id);
        if (!empty($image_src)) {
            return $image_src;
        }

        // has ASIN custom field
        $image_src = $this->get_asin_image($post_id);
        if (!empty($image_src)) {
            return $image_src;
        }

        // has thumbnail
        $image_src = $this->get_post_thumbnail_image();
        if (!empty($image_src)) {
            return $image_src;
        }

        // has img tag
        $image_src = $this->get_post_image_from_tag($datauri);

        return $image_src;
    }

    public function get_post_thumbnail_image($size = null): string {
        global $post;
        $url = "";
        $attachment_image_src = null;

        if ( !has_post_thumbnail() ) {
            return "";
        }

        $thumbnail_id = get_post_thumbnail_id();

        if ($size == null) {
            // wp_get_attachment_image_src -> thumbnail, medium, large, full
            // try large size
            $attachment_image_src = wp_get_attachment_image_src($thumbnail_id, 'large');

            // try full
            if ( empty($attachment_image_src) ) {
                $attachment_image_src = wp_get_attachment_image_src($thumbnail_id, 'full');
            }

            // retry medium
            if ( empty($attachment_image_src) ) {
                $attachment_image_src = wp_get_attachment_image_src($thumbnail_id, 'medium');
            }

        } else {
            $attachment_image_src = wp_get_attachment_image_src($thumbnail_id, $size);
        }

        if ($attachment_image_src) {
            $url = (string)$attachment_image_src[0];
        }

        return $this->is_correct_image($url) ? $url : "";
    }

    public function get_post_image_from_tag($datauri = true): string {
        global $post;
        $src = "";
        $content = "";

        // get from Amazon Product Tag
        $content = get_post_meta($post->ID, CF_AMAZON_PRODUCT_TAG, true);

        if ( empty($content) ) {
            $content = $post->post_content;
        }

        // maybe URL or Path, sortcode?
        if ( preg_match('/<img.*?src=(["\'])(.+?)\1.*?>/i', $content, $match) ) {
            $src = $match[2];
        }

        // bye
        if ( empty($src) ) {
            return "";
        }

        // image file?
        if ( Util::is_image($src) ) {
            return Util::relative_to_absolute_url($src);
        }

        // shortcode?
        if ( Util::is_shortcode($src) ) {
            $src = do_shortcode($src);

            if ( Util::is_url($src) && Util::is_image($src) ) {
                return Util::relative_to_absolute_url($src);
            }
            // denied DataURI
            if ( !$datauri ) {
                return "";
            }
            // not Data URI
            if ( !Util::is_dataURI($src) ) {
                return "";
            }
        }

        return $src;
    }

    private function is_correct_image(string $src): bool {
        return Util::is_url($src) && Util::is_image($src);
    }

    public function get_meta_thumbnail_image($post_id) {
        $thumbnail_data = get_post_meta($post_id, CF_THUMBNAIL, true);

        return $this->is_correct_image($thumbnail_data) ? $thumbnail_data : "";
    }

    public function get_asin_image($post_id) {
        $product_data = get_post_meta($post_id, CF_AMAZON_PRODUCT_TAG, true);
{'fraction_non_alphanumeric': 0.12901069518716576, 'fraction_numerical': 0.001336898395721925, 'mean_word_length': 1.7312887915297555, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '200588', 'n_tokens_mistral': 2333, 'n_tokens_neox': 2255, 'n_words': 407}