<commit_before><?php

/**
 * Admin class that handle meta data, like post or term.
 */
final class Papi_Admin_Meta_Handler extends Papi_Core_Data_Handler {

	/**
	 * Get meta type so we know where the data should be saved.
	 *
	 * @return string
	 */
	private function get_meta_type() {
		if ( $current_filter = current_filter() ) {
			return papi_get_meta_type( explode( '_', $current_filter )[1] );
		}

		return papi_get_meta_type();
	}

	/**
	 * Overwrite post data in `posts` table.
	 *
	 * @param int $post_id
	 */
	private function overwrite_post_data( $post_id ) {
		global $wpdb;

		if ( empty( $post_id ) || empty( $this->overwrite ) ) {
			return;
		}

		$wpdb->update( $wpdb->posts, $this->overwrite, ['ID' => $post_id] );

		// Delete cache since it can be cached even if not saved in the database.
		foreach ( array_keys( $this->overwrite ) as $key ) {
			papi_cache_delete( $key, $post_id );
		}
	}

	/**
	 * Pre save page template and page type.
	 *
	 * @param int $id
	 */
	private function pre_save( $id ) {
		if ( empty( $id ) ) {
			return;
		}

		$data = $this->get_pre_data();

		foreach ( $data as $key => $value ) {
			if ( empty( $value ) ) {
				continue;
			}

			if ( is_array( $value ) ) {
				list( $keys, $value ) = $this->get_pre_deep_keys_value( $value );
				$key = sprintf( '%s_%s', $key, implode( '_', $keys ) );
			}

			update_metadata( $this->get_meta_type(), $id, $key, $value );
		}
	}

	/**
	 * Save meta boxes.
	 *
	 * @param int    $id
	 * @param object $post
	 */
	public function save_meta_boxes( $id, $post = null ) {
		// Can't proceed without a id.
		if ( empty( $id ) ) {
			return;
		}

		$meta_type = $this->get_meta_type();
		$post      = is_array( $post ) ? (object) $post : $post;

		if ( $meta_type === 'post' && $post_type = get_post_type_object( $post->post_type ) ) {
			// Check so the id is a post id and not a autosave post.
			if ( $this->valid_post_id( $id ) ) {
				return;
			}

			// Check the `edit_posts` capability before we continue.
			if ( ! current_user_can( $post_type->cap->edit_posts ) ) {
				return;
			}

			// Save post revision data.
			if ( $parent_id = wp_is_post_revision( $id ) ) {
				$slugs = papi_get_slugs( $id, true );

				foreach ( $slugs as $slug ) {
					papi_update_field( $id, $slug, papi_get_field( $parent_id, $slug ) );<commit_msg>Check if there was a multisite switch before saving meta data<commit_after><?php

/**
 * Admin class that handle meta data, like post or term.
 */
final class Papi_Admin_Meta_Handler extends Papi_Core_Data_Handler {

	/**
	 * Get meta type so we know where the data should be saved.
	 *
	 * @return string
	 */
	private function get_meta_type() {
		if ( $current_filter = current_filter() ) {
			return papi_get_meta_type( explode( '_', $current_filter )[1] );
		}

		return papi_get_meta_type();
	}

	/**
	 * Overwrite post data in `posts` table.
	 *
	 * @param int $post_id
	 */
	private function overwrite_post_data( $post_id ) {
		global $wpdb;

		if ( empty( $post_id ) || empty( $this->overwrite ) ) {
			return;
		}

		$wpdb->update( $wpdb->posts, $this->overwrite, ['ID' => $post_id] );

		// Delete cache since it can be cached even if not saved in the database.
		foreach ( array_keys( $this->overwrite ) as $key ) {
			papi_cache_delete( $key, $post_id );
		}
	}

	/**
	 * Pre save page template and page type.
	 *
	 * @param int $id
	 */
	private function pre_save( $id ) {
		if ( empty( $id ) ) {
			return;
		}

		$data = $this->get_pre_data();

		foreach ( $data as $key => $value ) {
			if ( empty( $value ) ) {
				continue;
			}

			if ( is_array( $value ) ) {
				list( $keys, $value ) = $this->get_pre_deep_keys_value( $value );
				$key = sprintf( '%s_%s', $key, implode( '_', $keys ) );
			}

			update_metadata( $this->get_meta_type(), $id, $key, $value );
		}
	}

	/**
	 * Save meta boxes.
	 *
	 * @param int    $id
	 * @param object $post
	 */
	public function save_meta_boxes( $id, $post = null ) {
		// Check if there was a multisite switch before.
		if ( is_multisite() && ms_is_switched() ) {
			return;
		}

		// Can't proceed without a id.
		if ( empty( $id ) ) {
			return;
		}

		$meta_type = $this->get_meta_type();
		$post      = is_array( $post ) ? (object) $post : $post;

		if ( $meta_type === 'post' && $post_type = get_post_type_object( $post->post_type ) ) {
			// Check so the id is a post id and not a autosave post.
			if ( $this->valid_post_id( $id ) ) {
				return;
			}

			// Check the `edit_posts` capability before we continue.
			if ( ! current_user_can( $post_type->cap->edit_posts ) ) {
				return;
			}

			// Save post revision data.
			if ( $parent_id = wp_is_post_revision( $id ) ) {
				$slugs = papi_get_slugs( $id, true );

				foreach ( $slugs as $slug ) {
					papi_update_field( $id, $slug, papi_get_field( $parent_id, $slug ) );
{'fraction_non_alphanumeric': 0.15384615384615385, 'fraction_numerical': 0.00041467965996267884, 'mean_word_length': 2.8653846153846154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '6428936', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1752, 'n_words': 476}