LLMS_Admin_Metabox::save( int $post_id )

Save field data Loops through fields and saves the data to postmeta Called by $this->save_actions()


Description Description

This function is dumb. If the fields need to output error messages or do validation Override this method and create a custom save method to accommodate the validations or conditions


Parameters Parameters

$post_id

(int) (Required) WP Post ID of the post being saved.


Top ↑

Return Return

(int) -1 When no user or user is missing required capabilities or when there's no or invalid nonce. 0 during inline saves or ajax requests or when no fields are found for the metabox. 1 if fields were found. This doesn't mean there weren't errors during saving.


Top ↑

Source Source

File: includes/abstracts/abstract.llms.admin.metabox.php

	protected function save( $post_id ) {

		if ( ! current_user_can( $this->capability, $post_id ) || ! llms_verify_nonce( 'lifterlms_meta_nonce', 'lifterlms_save_data' ) ) {
			return -1;
		}

		// Return early during quick saves and ajax requests.
		if ( isset( $_POST['action'] ) && 'inline-save' === $_POST['action'] ) {
			return 0;
		} elseif ( llms_is_ajax() ) {
			return 0;
		}

		// Get all defined fields.
		$fields = $this->get_fields();

		if ( ! is_array( $fields ) ) {
			return 0;
		}

		// Loop through the fields.
		foreach ( $fields as $group => $data ) {

			// Find the fields in each tab.
			if ( isset( $data['fields'] ) && is_array( $data['fields'] ) ) {

				// loop through the fields.
				foreach ( $data['fields'] as $field ) {

					// don't save things that don't have an ID.
					if ( isset( $field['id'] ) ) {

						$val = '';

						// get the posted value & sanitize it.
						if ( isset( $_POST[ $field['id'] ] ) ) {

							if ( isset( $field['sanitize'] ) && 'shortcode' === $field['sanitize'] ) {
								$val = llms_filter_input( INPUT_POST, $field['id'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES );
							} elseif ( isset( $field['multi'] ) && $field['multi'] ) {
								$val = llms_filter_input( INPUT_POST, $field['id'], FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
							} else {
								$val = llms_filter_input( INPUT_POST, $field['id'], FILTER_SANITIZE_STRING );
							}
						}

						update_post_meta( $post_id, $field['id'], $val );

					}
				}
			}
		}

		return 1;

	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.36.1 Check metabox capability during saves. Return an int depending on return condition. Automatically add FILTER_REQUIRE_ARRAY flag when sanitizing a multi field.
3.36.0 Allow quotes when sanitizing some special fields that store a shortcode.
3.35.0 Added nonce verification before processing data; only access $_POST data via llms_filter_input().
3.14.1 Unknown.
3.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

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