<commit_before><?php

namespace ContentTranslator;

class Post
{
    public function __construct()
    {
        if (\ContentTranslator\Switcher::isLanguageSet()) {
            add_action('admin_enqueue_scripts', array($this, 'globalPost'));

            add_action('wp', array($this, 'globalPost'));
            add_filter('posts_results', array($this, 'postsResults'));

            add_filter('wp_insert_post_data', array($this, 'save'), 10, 2);
        }
    }

    /**
     * Translates a post object
     * @param  \WP_Post   $post         Post object
     * @param  array|null $translations Array with translations
     * @return \WP_Post                 Translated post object
     */
    protected function get(\WP_Post $post, array $translations = null) : \WP_Post
    {
        if (is_null($translations)) {
            $translations = $this->getTranslationPosts(array($post->ID));
        }

        if (empty($translations) || !isset($translations[$post->ID])) {
            return $post;
        }

        $post->post_title = $translations[$post->ID]->post_title;
        $post->post_content = $translations[$post->ID]->post_content;

        /**
         * Filter translated post object
         * @param WP_Post $post       The post object
         * @param array $translations Possible translations
         */
        return apply_filters('wp-content-translator/post/get', $post, $translations);
    }

    /**
     * Switches db table so that the post is beeing saved in correct lang-table
     * Inserts or updates (does what needs to be done)
     * @param  array  $data    [description]
     * @param  array  $postarr [description]
     * @return [type]          [description]
     */
    public function save(array $data, array $postarr)
    {
        global $wpdb;

        $table = \ContentTranslator\Language::getTable('posts');
        $table = $table['name'];

        $wpdb->posts = $table;

        $exists = 0;

        if ($data['post_type'] !== 'revision' && isset($postarr['post_ID']) && !empty($postarr['post_ID'])) {
            $exists = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE ID = " . $postarr['post_ID']);
            $exists = (int) $exists;
        }

        if (!$exists) {
            remove_filter('wp_insert_post_data', array($this, 'save'), 10);

            // Insert the post
            $insertedPostID = wp_insert_post($data, true);

            if ($data['post_type'] === 'revision') {
                var_dump($table);
                var_dump($insertedPostID);
                exit;
            }

            // Update the post id to match the original
            if (isset($postarr['post_ID']) && !empty($postarr['post_ID'])) {
                $wpdb->update(
                    $wpdb->posts,
                    // Update
                    array(
                        'ID' => (int) $postarr['post_ID']
                    ),
                    // Where
                    array(
                        'ID' => $insertedPostID
                    ),
                    array('%d'),
                    array('%d')
                );<commit_msg>Remove var_dumps and exit<commit_after><?php

namespace ContentTranslator;

class Post
{
    public function __construct()
    {
        if (\ContentTranslator\Switcher::isLanguageSet()) {
            add_action('admin_enqueue_scripts', array($this, 'globalPost'));

            add_action('wp', array($this, 'globalPost'));
            add_filter('posts_results', array($this, 'postsResults'));

            add_filter('wp_insert_post_data', array($this, 'save'), 10, 2);
        }
    }

    /**
     * Translates a post object
     * @param  \WP_Post   $post         Post object
     * @param  array|null $translations Array with translations
     * @return \WP_Post                 Translated post object
     */
    protected function get(\WP_Post $post, array $translations = null) : \WP_Post
    {
        if (is_null($translations)) {
            $translations = $this->getTranslationPosts(array($post->ID));
        }

        if (empty($translations) || !isset($translations[$post->ID])) {
            return $post;
        }

        $post->post_title = $translations[$post->ID]->post_title;
        $post->post_content = $translations[$post->ID]->post_content;

        /**
         * Filter translated post object
         * @param WP_Post $post       The post object
         * @param array $translations Possible translations
         */
        return apply_filters('wp-content-translator/post/get', $post, $translations);
    }

    /**
     * Switches db table so that the post is beeing saved in correct lang-table
     * Inserts or updates (does what needs to be done)
     * @param  array  $data    [description]
     * @param  array  $postarr [description]
     * @return [type]          [description]
     */
    public function save(array $data, array $postarr)
    {
        global $wpdb;

        $table = \ContentTranslator\Language::getTable('posts');
        $table = $table['name'];

        $wpdb->posts = $table;

        $exists = 0;

        if ($data['post_type'] !== 'revision' && isset($postarr['post_ID']) && !empty($postarr['post_ID'])) {
            $exists = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE ID = " . $postarr['post_ID']);
            $exists = (int) $exists;
        }

        if (!$exists) {
            remove_filter('wp_insert_post_data', array($this, 'save'), 10);

            // Insert the post
            $insertedPostID = wp_insert_post($data, true);

            // Update the post id to match the original
            if (isset($postarr['post_ID']) && !empty($postarr['post_ID'])) {
                $wpdb->update(
                    $wpdb->posts,
                    // Update
                    array(
                        'ID' => (int) $postarr['post_ID']
                    ),
                    // Where
                    array(
                        'ID' => $insertedPostID
                    ),
                    array('%d'),
                    array('%d')
                );
{'fraction_non_alphanumeric': 0.1381740698246189, 'fraction_numerical': 0.001966890673660056, 'mean_word_length': 1.8016528925619835, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '4469822', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1668, 'n_words': 379}