[ create a new paste ] login | about

Link: http://codepad.org/WZVmodoB    [ raw code | fork ]

chris_mcclellan - PHP, pasted on Nov 17:
<?php

	// Updated `set_custom_fields_value()` to retreive all fields properly
	function set_custom_fields_value() {

		global $json_api;

		if ($json_api->include_value('custom_fields') && $json_api->query->custom_fields) {

			// Query string params for this query var
			$params = trim($json_api->query->custom_fields);

			// Get all custom fields if true|all|* is passed
			if ($params === "*" || $params === "true" || $params === "all") {

				$wp_custom_fields = get_post_custom($this->id);
				$this->custom_fields = new stdClass();

				// Loop through our custom fields and place on property
				foreach($wp_custom_fields as $key => $val) {
					
					if ($val) {

						// Some fields are stored as serialized arrays.
						// This method does not support multidimensionals... 
						// but didn't see anything wrong with this approach
						$current_custom_field = @unserialize($wp_custom_fields[$key][0]);

						if (is_array($current_custom_field)) {

							// Loop through the unserialized array
							foreach($current_custom_field as $sub_key => $sub_val) {

								// Lets append these for correct JSON output
								$this->custom_fields->$key->$sub_key = $sub_val;
							}

						} else {

							// Break this value of this custom field out of its array
							// and place it on the stack like usual
							$this->custom_fields->$key = $wp_custom_fields[$key][0];

						}
					}
				}
			} else {

				// Well this is the old way but with the unserialized array fix
				$params = explode(',', $params);
				$wp_custom_fields = get_post_custom($this->id);
				$this->custom_fields = new stdClass();

				foreach ($params as $key) {

					if (isset($wp_custom_fields[$key]) && $wp_custom_fields[$key][0] ) {
						$current_custom_field = @unserialize($wp_custom_fields[$key][0]);

						if (is_array($current_custom_field)) {
							foreach($current_custom_field as $sub_key => $sub_val) {
								$this->custom_fields->$key->$sub_key = $sub_val;
							}

						} else {
							$this->custom_fields->$key = $wp_custom_fields[$key][0];

						}

					}
				}
			}

		} else {
			unset($this->custom_fields);

		}
	}

?>


Create a new paste based on this one


Comments: