LLMS_Meta_Box_Membership::save( int $post_id )

Save field data.


Description Description

See also See also


Top ↑

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/admin/post-types/meta-boxes/class.llms.meta.box.membership.php

	public function save( $post_id ) {

		if ( ! 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;
		}

		$membership = new LLMS_Membership( $post_id );

		if ( ! isset( $_POST[ $this->prefix . 'restriction_add_notice' ] ) ) {
			$_POST[ $this->prefix . 'restriction_add_notice' ] = 'no';
		}

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

		// save all the fields.
		$save_fields = array(
			$this->prefix . 'restriction_redirect_type',
			$this->prefix . 'redirect_page_id',
			$this->prefix . 'redirect_custom_url',
			$this->prefix . 'restriction_add_notice',
			$this->prefix . 'restriction_notice',
			$this->prefix . 'sales_page_content_page_id',
			$this->prefix . 'sales_page_content_type',
			$this->prefix . 'sales_page_content_url',
		);

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

		$to_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 or that are not listed in $save_fields.
					if ( isset( $field['id'] ) && in_array( $field['id'], $save_fields, true ) ) {

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

						$membership->set( substr( $field['id'], strlen( $this->prefix ) ), $val );
						$to_return = 1;
					}
				}
			}
		}

		return $to_return;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
5.9.0 Stop using deprecated FILTER_SANITIZE_STRING.
3.36.3 Added logic to correctly sanitize fields of type 'multi' (array) and 'shortcode' (preventing quotes encode). Also align the return type to the parent save() method.
3.35.0 Verify nonces and sanitize $_POST data.
3.30.0 Autoenroll courses saved via AJAX and removed from this method.
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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