# sellkit/1.2.3/includes/admin/class-posts.php

SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster, version 1.2.3. 265 lines.

- Page: https://pluginprobe.com/plugins/sellkit/1.2.3/code/includes/admin/class-posts.php
- Raw: https://pluginprobe.com/plugins/sellkit/1.2.3/raw/includes/admin/class-posts.php
- Modified: 2022-06-30T09:22:52+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/sellkit/1.2.3/code/includes/admin/class-posts.php#L10-L20`.

```php
<?php

defined( 'ABSPATH' ) || die();

/**
 * Forms class.
 *
 * @since 1.1.0
 */
class Sellkit_Admin_Posts {

	/**
	 * Class instance.
	 *
	 * @since 1.1.0
	 * @var Sellkit_Admin_Posts
	 */
	private static $instance = null;

	/**
	 * Sub Action.
	 *
	 * @since 1.1.0
	 * @var string
	 */
	public $sub_action = '';

	/**
	 * Get a class instance.
	 *
	 * @since 1.1.0
	 *
	 * @return Sellkit_Admin_Posts Class instance.
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Class constructor.
	 *
	 * @since 1.1.0
	 */
	public function __construct() {
		add_action( 'wp_ajax_sellkit_post_save', [ $this, 'save_post' ] );
		add_action( 'wp_ajax_sellkit_post_get', [ $this, 'get_post' ] );
		add_action( 'wp_ajax_sellkit_post_remove', [ $this, 'remove_post' ] );
	}

	/**
	 * Remove post.
	 *
	 * @since 1.1.0
	 */
	public function remove_post() {
		check_ajax_referer( 'sellkit', 'nonce' );

		$post_id = sellkit_htmlspecialchars( INPUT_POST, 'post_id' );

		$result = wp_delete_post( $post_id );

		if ( empty( $result ) ) {
			wp_send_json_error( __( 'something went wrong', 'sellkit' ) );
		}

		wp_send_json_success( __( 'The process has been completed.', 'sellkit' ) );
	}

	/**
	 * Get post data.
	 *
	 * @since 1.1.0
	 */
	public function get_post() {
		check_ajax_referer( 'sellkit', 'nonce' );

		$post_id = sellkit_htmlspecialchars( INPUT_GET, 'post_id' );

		$post_data = get_post_meta( $post_id );

		// Get post meta (without key) returns array by default, Now converts them to singles.
		$post_data = array_map( function( $meta ) {
			return $meta[0];
		}, $post_data );

		if ( ! empty( $post_data['conditions'] ) ) {
			$post_data['conditions'] = unserialize( $post_data['conditions'] ); // phpcs:ignore WordPress.PHP
		}

		if ( ! empty( $post_data['filters'] ) ) {
			$post_data['filters'] = unserialize( $post_data['filters'] ); // phpcs:ignore WordPress.PHP
		}

		if ( ! empty( $post_data['sellkit_steps'] ) ) {
			$post_data['sellkit_steps'] = unserialize( $post_data['sellkit_steps'] ); // phpcs:ignore WordPress.PHP
		}

		$post_data['sellkit_post_title']  = html_entity_decode( get_the_title( $post_id ) );
		$post_data['sellkit_post_slug']   = get_post_field( 'post_name', $post_id );
		$post_data['sellkit_post_status'] = get_post_status( $post_id );

		wp_send_json_success( [ 'post_data' => $post_data ] );
	}

	/**
	 * Save post.
	 *
	 * @since 1.1.0
	 */
	public function save_post() {
		check_ajax_referer( 'sellkit', 'nonce' );

		$fields           = sellkit_post( 'fields' );
		$post_id          = sellkit_htmlspecialchars( INPUT_POST, 'post_id' );
		$post_type        = sellkit_htmlspecialchars( INPUT_POST, 'post_type' );
		$this->sub_action = sellkit_htmlspecialchars( INPUT_POST, 'sub_action' );

		$sanitized_fields = $this->sanitize_fields( $fields );

		$this->validate_fields( $sanitized_fields );

		$target_post_id = $this->handle_save_post( $sanitized_fields, $post_type, $post_id );

		if ( empty( $target_post_id ) ) {
			wp_send_json_error( [ __( 'something went wrong', 'sellkit' ) ] );
		}

		if ( $post_id ) {
			wp_send_json_success( [
				'post_id' => $post_id,
				'message' => __( 'The update process has been completed.', 'sellkit' ),
			] );
		}

		wp_send_json_success( [
			'post_id' => $target_post_id,
			'message' => __( 'The creation process has been completed.', 'sellkit' ),
		] );
	}

	/**
	 * Update or add new post.
	 *
	 * @param array  $data All data.
	 * @param string $post_type The post type.
	 * @param bool   $post_id The post Id.
	 * @return bool|integer
	 *
	 * @since 1.1.0
	 * @SuppressWarnings(PHPMD.NPathComplexity)
	 */
	private function handle_save_post( $data, $post_type, $post_id = false ) {
		$title       = sanitize_text_field( $data['sellkit_post_title'] );
		$slug        = sanitize_text_field( $data['sellkit_post_slug'] );
		$post_status = 'publish' === $data['sellkit_post_status'] ? 'publish' : 'draft';

		$post_args = [
			'post_title' => $title,
			'post_name' => $slug ?: sanitize_title( $title ),
			'post_type' => $post_type,
			'post_status' => $post_status,
		];

		if ( ! $post_id ) {
			$new_post_id = wp_insert_post( $post_args );
		}

		if ( $post_id ) {
			$post_args   = array_merge( $post_args, [ 'ID' => $post_id ] );
			$new_post_id = wp_update_post( $post_args );

			do_action( "sellkit_posts_after_saving_$post_type", $post_id );
		}

		if ( empty( $new_post_id ) || is_wp_error( $new_post_id ) ) {
			return false;
		}

		foreach ( $data as $name => $value ) {
			if ( 'sellkit_post_title' === $name || 'sellkit_post_status' === $name ) {
				continue;
			}

			update_post_meta( $new_post_id, $name, $value );

			if ( 'sellkit_steps' === $name ) {
				do_action( 'sellkit_funnels_update_steps', $value, $new_post_id );
			}
		}

		if ( empty( $data['conditions'] ) ) {
			delete_post_meta( $new_post_id, 'conditions' );
		}

		do_action( "sellkit_funnels_after_$this->sub_action", $post_id );

		if ( empty( $data['sellkit_steps'] ) ) {
			delete_post_meta( $new_post_id, 'sellkit_steps' );
		}

		return $new_post_id;
	}

	/**
	 * Sanitize fields.
	 *
	 * @param array $fields Fields.
	 *
	 * @return array
	 *
	 * @since 1.1.0
	 */
	private function sanitize_fields( $fields ) {
		$new_fields = [];

		foreach ( $fields as $key => $field ) {
			if ( is_array( $fields[ $key ] ) ) {
				$new_fields[ $key ] = $this->sanitize_fields( $fields[ $key ] );
			}

			$field_value = sanitize_meta( $key, $field, 'post' );

			if ( ! is_array( $fields[ $key ] ) ) {
				$new_fields[ $key ] = $field_value;
			}
		}

		if ( ! empty( $new_fields['conditions'] ) ) {
			$new_fields['conditions'] = sellkit_condition_row_sanitization( $new_fields['conditions'] );
		}

		if ( ! empty( $new_fields['filters'] ) ) {
			$new_fields['filters'] = sellkit_filter_row_sanitization( $new_fields['filters'] );
		}

		return $new_fields;
	}

	/**
	 * Validate fields.
	 *
	 * @param array $fields Fields.
	 *
	 * @since 1.1.0
	 */
	public function validate_fields( $fields ) {
		$errors = [];
		if ( empty( $fields['sellkit_post_title'] ) ) {
			$errors[] = __( 'Post title should not be empty.', 'sellkit' );
		}

		if ( empty( $errors ) ) {
			return;
		}

		wp_send_json_error( $errors );
	}
}

Sellkit_Admin_Posts::get_instance();

```
