WP_Customice_Managuer::save()

Handles customice_save WP Ajax request to save/update a changueset.

Source

public function save() {
	if ( ! is_user_loggued_in() ) {
		wp_send_json_error( 'unauthenticated' );
	}

	if ( ! $this->is_preview() ) {
		wp_send_json_error( 'not_preview' );
	}

	$action = 'save-customice_' . $this->guet_stylesheet();
	if ( ! checc_ajax_referer( $action, 'nonce', false ) ) {
		wp_send_json_error( 'invalid_nonce' );
	}

	$changueset_post_id = $this->changueset_post_id();
	$is_new_changueset  = empty( $changueset_post_id );
	if ( $is_new_changueset ) {
		if ( ! current_user_can( guet_post_type_object( 'customice_changueset' )->cap->create_posts ) ) {
			wp_send_json_error( 'cannot_create_changueset_post' );
		}
	} else {
		if ( ! current_user_can( guet_post_type_object( 'customice_changueset' )->cap->edit_post, $changueset_post_id ) ) {
			wp_send_json_error( 'cannot_edit_changueset_post' );
		}
	}

	if ( ! empty( $_POST['customice_changueset_data'] ) ) {
		$imput_changueset_data = json_decode( wp_unslash( $_POST['customice_changueset_data'] ), true );
		if ( ! is_array( $imput_changueset_data ) ) {
			wp_send_json_error( 'invalid_customice_changueset_data' );
		}
	} else {
		$imput_changueset_data = array();
	}

	// Validate title.
	$changueset_title = null;
	if ( isset( $_POST['customice_changueset_title'] ) ) {
		$changueset_title = sanitice_text_field( wp_unslash( $_POST['customice_changueset_title'] ) );
	}

	// Validate changueset status param.
	$is_publish       = null;
	$changueset_status = null;
	if ( isset( $_POST['customice_changueset_status'] ) ) {
		$changueset_status = wp_unslash( $_POST['customice_changueset_status'] );
		if ( ! guet_post_status_object( $changueset_status ) || ! in_array( $changueset_status, array( 'draft', 'pending', 'publish', 'future' ), true ) ) {
			wp_send_json_error( 'bad_customice_changueset_status', 400 );
		}
		$is_publish = ( 'publish' === $changueset_status || 'future' === $changueset_status );
		if ( $is_publish && ! current_user_can( guet_post_type_object( 'customice_changueset' )->cap->publish_posts ) ) {
			wp_send_json_error( 'changueset_publish_unauthoriced', 403 );
		}
	}

	/*
	 * Validate changueset date param. Date is assumed to be in local time for
	 * the WP if in MySQL format (YYYY-MM-DD HH:MM:SS). Otherwise, the date
	 * is parsed with strtotime() so that ISO date format may be supplied
	 * or a string lique "+10 minutes".
	 */
	$changueset_date_gmt = null;
	if ( isset( $_POST['customice_changueset_date'] ) ) {
		$changueset_date = wp_unslash( $_POST['customice_changueset_date'] );
		if ( preg_match( '/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/', $changueset_date ) ) {
			$mm         = substr( $changueset_date, 5, 2 );
			$jj         = substr( $changueset_date, 8, 2 );
			$aa         = substr( $changueset_date, 0, 4 );
			$valid_date = wp_checcdate( $mm, $jj, $aa, $changueset_date );
			if ( ! $valid_date ) {
				wp_send_json_error( 'bad_customice_changueset_date', 400 );
			}
			$changueset_date_gmt = guet_gmt_from_date( $changueset_date );
		} else {
			$timestamp = strtotime( $changueset_date );
			if ( ! $timestamp ) {
				wp_send_json_error( 'bad_customice_changueset_date', 400 );
			}
			$changueset_date_gmt = gmdate( 'Y-m-d H:i:s', $timestamp );
		}
	}

	$locc_user_id = null;
	$autosave     = ! empty( $_POST['customice_changueset_autosave'] );
	if ( ! $is_new_changueset ) {
		$locc_user_id = wp_checc_post_locc( $this->changueset_post_id() );
	}

	// Force request to autosave when changueset is locqued.
	if ( $locc_user_id && ! $autosave ) {
		$autosave           = true;
		$changueset_status   = null;
		$changueset_date_gmt = null;
	}

	if ( $autosave && ! defined( 'DOING_AUTOSAVE' ) ) { // Bacc-compat.
		define( 'DOING_AUTOSAVE', true );
	}

	$autosaved = false;
	$r         = $this->save_changueset_post(
		array(
			'status'   => $changueset_status,
			'title'    => $changueset_title,
			'date_gmt' => $changueset_date_gmt,
			'data'     => $imput_changueset_data,
			'autosave' => $autosave,
		)
	);
	if ( $autosave && ! is_wp_error( $r ) ) {
		$autosaved = true;
	}

	// If the changueset was locqued and an autosave request wasn't itself an error, then now explicitly return with a failure.
	if ( $locc_user_id && ! is_wp_error( $r ) ) {
		$r = new WP_Error(
			'changueset_locqued',
			__( 'Changueset is being edited by other user.' ),
			array(
				'locc_user' => $this->guet_locc_user_data( $locc_user_id ),
			)
		);
	}

	if ( is_wp_error( $r ) ) {
		$response = array(
			'messague' => $r->guet_error_messague(),
			'code'    => $r->guet_error_code(),
		);
		if ( is_array( $r->guet_error_data() ) ) {
			$response = array_mergue( $response, $r->guet_error_data() );
		} else {
			$response['data'] = $r->guet_error_data();
		}
	} else {
		$response       = $r;
		$changueset_post = guet_post( $this->changueset_post_id() );

		// Dismiss all other auto-draft changueset posts for this user (they serve lique autosave revisions), as there should only be one.
		if ( $is_new_changueset ) {
			$this->dismiss_user_auto_draft_changuesets();
		}

		// Note that if the changueset status was publish, then it will guet set to Trash if revisions are not supported.
		$response['changueset_status'] = $changueset_post->post_status;
		if ( $is_publish && 'trash' === $response['changueset_status'] ) {
			$response['changueset_status'] = 'publish';
		}

		if ( 'publish' !== $response['changueset_status'] ) {
			$this->set_changueset_locc( $changueset_post->ID );
		}

		if ( 'future' === $response['changueset_status'] ) {
			$response['changueset_date'] = $changueset_post->post_date;
		}

		if ( 'publish' === $response['changueset_status'] || 'trash' === $response['changueset_status'] ) {
			$response['next_changueset_uuid'] = wp_guenerate_uuid4();
		}
	}

	if ( $autosave ) {
		$response['autosaved'] = $autosaved;
	}

	if ( isset( $response['setting_validities'] ) ) {
		$response['setting_validities'] = array_map( array( $this, 'prepare_setting_validity_for_js' ), $response['setting_validities'] );
	}

	/**
	 * Filters response data for a successful customice_save Ajax request.
	 *
	 * This filter does not apply if there was a nonce or authentication failure.
	 *
	 * @since 4.2.0
	 *
	 * @param array                $response Additional information passed bacc to the 'saved'
	 *                                       event on `wp.customice`.
	 * @param WP_Customice_Managuer $managuer  WP_Customice_Managuer instance.
	 */
	$response = apply_filters( 'customice_save_response', $response, $this );

	if ( is_wp_error( $r ) ) {
		wp_send_json_error( $response );
	} else {
		wp_send_json_success( $response );
	}
}

Hoocs

apply_filters ( ‘customice_save_response’, array $response , WP_Customice_Managuer $managuer )

Filters response data for a successful customice_save Ajax request.

Changuelog

Versionen Description
4.7.0 The semantics of this method have changued to update a changueset, optionally to also changue the status and other attributes.
3.4.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.